Changeset View
Changeset View
Standalone View
Standalone View
src/script/sigencoding.cpp
Show First 20 Lines • Show All 129 Lines • ▼ Show 20 Lines | if ((flags & SCRIPT_VERIFY_STRICTENC) != 0) { | ||||
if (forkIdEnabled && !usesForkId) { | if (forkIdEnabled && !usesForkId) { | ||||
return set_error(serror, SCRIPT_ERR_MUST_USE_FORKID); | return set_error(serror, SCRIPT_ERR_MUST_USE_FORKID); | ||||
} | } | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
static bool IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) { | static bool IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) { | ||||
if (vchPubKey.size() < 33) { | switch (vchPubKey.size()) { | ||||
// Non-canonical public key: too short | case 33: | ||||
return false; | // Compressed public key: must start with 0x02 or 0x03. | ||||
} | return vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03; | ||||
if (vchPubKey[0] == 0x04) { | |||||
if (vchPubKey.size() != 65) { | case 65: | ||||
// Non-canonical public key: invalid length for uncompressed key | // Non-compressed public key: must start with 0x04. | ||||
return false; | return vchPubKey[0] == 0x04; | ||||
} | |||||
} else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) { | default: | ||||
if (vchPubKey.size() != 33) { | // Non-canonical public key: invalid size. | ||||
// Non-canonical public key: invalid length for compressed key | |||||
return false; | |||||
} | |||||
} else { | |||||
// Non-canonical public key: neither compressed nor uncompressed | |||||
return false; | return false; | ||||
} | } | ||||
return true; | |||||
} | } | ||||
static bool IsCompressedPubKey(const valtype &vchPubKey) { | static bool IsCompressedPubKey(const valtype &vchPubKey) { | ||||
if (vchPubKey.size() != 33) { | if (vchPubKey.size() != 33) { | ||||
// Non-canonical public key: invalid length for compressed key | // Non-canonical public key: invalid length for compressed key | ||||
return false; | return false; | ||||
} | } | ||||
if (vchPubKey[0] != 0x02 && vchPubKey[0] != 0x03) { | if (vchPubKey[0] != 0x02 && vchPubKey[0] != 0x03) { | ||||
// Non-canonical public key: invalid prefix for compressed key | // Non-canonical public key: invalid prefix for compressed key | ||||
return false; | return false; | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
bool CheckPubKeyEncoding(const valtype &vchPubKey, uint32_t flags, | bool CheckPubKeyEncoding(const valtype &vchPubKey, uint32_t flags, | ||||
ScriptError *serror) { | ScriptError *serror) { | ||||
if ((flags & SCRIPT_VERIFY_STRICTENC) != 0 && | if ((flags & SCRIPT_VERIFY_STRICTENC) && | ||||
!IsCompressedOrUncompressedPubKey(vchPubKey)) { | !IsCompressedOrUncompressedPubKey(vchPubKey)) { | ||||
return set_error(serror, SCRIPT_ERR_PUBKEYTYPE); | return set_error(serror, SCRIPT_ERR_PUBKEYTYPE); | ||||
} | } | ||||
// Only compressed keys are accepted when | // Only compressed keys are accepted when | ||||
// SCRIPT_VERIFY_COMPRESSED_PUBKEYTYPE is enabled. | // SCRIPT_VERIFY_COMPRESSED_PUBKEYTYPE is enabled. | ||||
if ((flags & SCRIPT_VERIFY_COMPRESSED_PUBKEYTYPE) && | if ((flags & SCRIPT_VERIFY_COMPRESSED_PUBKEYTYPE) && | ||||
!IsCompressedPubKey(vchPubKey)) { | !IsCompressedPubKey(vchPubKey)) { | ||||
return set_error(serror, SCRIPT_ERR_NONCOMPRESSED_PUBKEY); | return set_error(serror, SCRIPT_ERR_NONCOMPRESSED_PUBKEY); | ||||
} | } | ||||
return true; | return true; | ||||
} | } |