summaryrefslogtreecommitdiffstats
path: root/verifier.cpp
diff options
context:
space:
mode:
authorTianjie Xu <xunchang@google.com>2018-10-25 19:40:28 +0200
committerandroid-build-merger <android-build-merger@google.com>2018-10-25 19:40:28 +0200
commit23b832fd1d32b1385358729d9f2eae5465f3ebef (patch)
treecbdeff595a2fa616db60afe4ebff00ff0c0e50a6 /verifier.cpp
parentMerge "Add sanity check when loading public keys for OTA package" am: 6793f61795 (diff)
parentMerge "Remove the load_keys function" (diff)
downloadandroid_bootable_recovery-23b832fd1d32b1385358729d9f2eae5465f3ebef.tar
android_bootable_recovery-23b832fd1d32b1385358729d9f2eae5465f3ebef.tar.gz
android_bootable_recovery-23b832fd1d32b1385358729d9f2eae5465f3ebef.tar.bz2
android_bootable_recovery-23b832fd1d32b1385358729d9f2eae5465f3ebef.tar.lz
android_bootable_recovery-23b832fd1d32b1385358729d9f2eae5465f3ebef.tar.xz
android_bootable_recovery-23b832fd1d32b1385358729d9f2eae5465f3ebef.tar.zst
android_bootable_recovery-23b832fd1d32b1385358729d9f2eae5465f3ebef.zip
Diffstat (limited to 'verifier.cpp')
-rw-r--r--verifier.cpp249
1 files changed, 0 insertions, 249 deletions
diff --git a/verifier.cpp b/verifier.cpp
index 2dfc20808..44bd4e180 100644
--- a/verifier.cpp
+++ b/verifier.cpp
@@ -308,144 +308,6 @@ int verify_file(const unsigned char* addr, size_t length, const std::vector<Cert
return VERIFY_FAILURE;
}
-std::unique_ptr<RSA, RSADeleter> parse_rsa_key(FILE* file, uint32_t exponent) {
- // Read key length in words and n0inv. n0inv is a precomputed montgomery
- // parameter derived from the modulus and can be used to speed up
- // verification. n0inv is 32 bits wide here, assuming the verification logic
- // uses 32 bit arithmetic. However, BoringSSL may use a word size of 64 bits
- // internally, in which case we don't have a valid n0inv. Thus, we just
- // ignore the montgomery parameters and have BoringSSL recompute them
- // internally. If/When the speedup from using the montgomery parameters
- // becomes relevant, we can add more sophisticated code here to obtain a
- // 64-bit n0inv and initialize the montgomery parameters in the key object.
- uint32_t key_len_words = 0;
- uint32_t n0inv = 0;
- if (fscanf(file, " %i , 0x%x", &key_len_words, &n0inv) != 2) {
- return nullptr;
- }
-
- if (key_len_words > 8192 / 32) {
- LOG(ERROR) << "key length (" << key_len_words << ") too large";
- return nullptr;
- }
-
- // Read the modulus.
- std::unique_ptr<uint32_t[]> modulus(new uint32_t[key_len_words]);
- if (fscanf(file, " , { %u", &modulus[0]) != 1) {
- return nullptr;
- }
- for (uint32_t i = 1; i < key_len_words; ++i) {
- if (fscanf(file, " , %u", &modulus[i]) != 1) {
- return nullptr;
- }
- }
-
- // Cconvert from little-endian array of little-endian words to big-endian
- // byte array suitable as input for BN_bin2bn.
- std::reverse((uint8_t*)modulus.get(),
- (uint8_t*)(modulus.get() + key_len_words));
-
- // The next sequence of values is the montgomery parameter R^2. Since we
- // generally don't have a valid |n0inv|, we ignore this (see comment above).
- uint32_t rr_value;
- if (fscanf(file, " } , { %u", &rr_value) != 1) {
- return nullptr;
- }
- for (uint32_t i = 1; i < key_len_words; ++i) {
- if (fscanf(file, " , %u", &rr_value) != 1) {
- return nullptr;
- }
- }
- if (fscanf(file, " } } ") != 0) {
- return nullptr;
- }
-
- // Initialize the key.
- std::unique_ptr<RSA, RSADeleter> key(RSA_new());
- if (!key) {
- return nullptr;
- }
-
- key->n = BN_bin2bn((uint8_t*)modulus.get(),
- key_len_words * sizeof(uint32_t), NULL);
- if (!key->n) {
- return nullptr;
- }
-
- key->e = BN_new();
- if (!key->e || !BN_set_word(key->e, exponent)) {
- return nullptr;
- }
-
- return key;
-}
-
-struct BNDeleter {
- void operator()(BIGNUM* bn) const {
- BN_free(bn);
- }
-};
-
-std::unique_ptr<EC_KEY, ECKEYDeleter> parse_ec_key(FILE* file) {
- uint32_t key_len_bytes = 0;
- if (fscanf(file, " %i", &key_len_bytes) != 1) {
- return nullptr;
- }
-
- std::unique_ptr<EC_GROUP, void (*)(EC_GROUP*)> group(
- EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1), EC_GROUP_free);
- if (!group) {
- return nullptr;
- }
-
- // Verify that |key_len| matches the group order.
- if (key_len_bytes != BN_num_bytes(EC_GROUP_get0_order(group.get()))) {
- return nullptr;
- }
-
- // Read the public key coordinates. Note that the byte order in the file is
- // little-endian, so we convert to big-endian here.
- std::unique_ptr<uint8_t[]> bytes(new uint8_t[key_len_bytes]);
- std::unique_ptr<BIGNUM, BNDeleter> point[2];
- for (int i = 0; i < 2; ++i) {
- unsigned int byte = 0;
- if (fscanf(file, " , { %u", &byte) != 1) {
- return nullptr;
- }
- bytes[key_len_bytes - 1] = byte;
-
- for (size_t i = 1; i < key_len_bytes; ++i) {
- if (fscanf(file, " , %u", &byte) != 1) {
- return nullptr;
- }
- bytes[key_len_bytes - i - 1] = byte;
- }
-
- point[i].reset(BN_bin2bn(bytes.get(), key_len_bytes, nullptr));
- if (!point[i]) {
- return nullptr;
- }
-
- if (fscanf(file, " }") != 0) {
- return nullptr;
- }
- }
-
- if (fscanf(file, " } ") != 0) {
- return nullptr;
- }
-
- // Create and initialize the key.
- std::unique_ptr<EC_KEY, ECKEYDeleter> key(EC_KEY_new());
- if (!key || !EC_KEY_set_group(key.get(), group.get()) ||
- !EC_KEY_set_public_key_affine_coordinates(key.get(), point[0].get(),
- point[1].get())) {
- return nullptr;
- }
-
- return key;
-}
-
static std::vector<Certificate> IterateZipEntriesAndSearchForKeys(const ZipArchiveHandle& handle) {
void* cookie;
ZipString suffix("x509.pem");
@@ -603,114 +465,3 @@ bool LoadCertificateFromBuffer(const std::vector<uint8_t>& pem_content, Certific
return true;
}
-
-// Reads a file containing one or more public keys as produced by
-// DumpPublicKey: this is an RSAPublicKey struct as it would appear
-// as a C source literal, eg:
-//
-// "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
-//
-// For key versions newer than the original 2048-bit e=3 keys
-// supported by Android, the string is preceded by a version
-// identifier, eg:
-//
-// "v2 {64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"
-//
-// (Note that the braces and commas in this example are actual
-// characters the parser expects to find in the file; the ellipses
-// indicate more numbers omitted from this example.)
-//
-// The file may contain multiple keys in this format, separated by
-// commas. The last key must not be followed by a comma.
-//
-// A Certificate is a pair of an RSAPublicKey and a particular hash
-// (we support SHA-1 and SHA-256; we store the hash length to signify
-// which is being used). The hash used is implied by the version number.
-//
-// 1: 2048-bit RSA key with e=3 and SHA-1 hash
-// 2: 2048-bit RSA key with e=65537 and SHA-1 hash
-// 3: 2048-bit RSA key with e=3 and SHA-256 hash
-// 4: 2048-bit RSA key with e=65537 and SHA-256 hash
-// 5: 256-bit EC key using the NIST P-256 curve parameters and SHA-256 hash
-//
-// Returns true on success, and appends the found keys (at least one) to certs.
-// Otherwise returns false if the file failed to parse, or if it contains zero
-// keys. The contents in certs would be unspecified on failure.
-bool load_keys(const char* filename, std::vector<Certificate>& certs) {
- std::unique_ptr<FILE, decltype(&fclose)> f(fopen(filename, "re"), fclose);
- if (!f) {
- PLOG(ERROR) << "error opening " << filename;
- return false;
- }
-
- while (true) {
- certs.emplace_back(0, Certificate::KEY_TYPE_RSA, nullptr, nullptr);
- Certificate& cert = certs.back();
- uint32_t exponent = 0;
-
- char start_char;
- if (fscanf(f.get(), " %c", &start_char) != 1) return false;
- if (start_char == '{') {
- // a version 1 key has no version specifier.
- cert.key_type = Certificate::KEY_TYPE_RSA;
- exponent = 3;
- cert.hash_len = SHA_DIGEST_LENGTH;
- } else if (start_char == 'v') {
- int version;
- if (fscanf(f.get(), "%d {", &version) != 1) return false;
- switch (version) {
- case 2:
- cert.key_type = Certificate::KEY_TYPE_RSA;
- exponent = 65537;
- cert.hash_len = SHA_DIGEST_LENGTH;
- break;
- case 3:
- cert.key_type = Certificate::KEY_TYPE_RSA;
- exponent = 3;
- cert.hash_len = SHA256_DIGEST_LENGTH;
- break;
- case 4:
- cert.key_type = Certificate::KEY_TYPE_RSA;
- exponent = 65537;
- cert.hash_len = SHA256_DIGEST_LENGTH;
- break;
- case 5:
- cert.key_type = Certificate::KEY_TYPE_EC;
- cert.hash_len = SHA256_DIGEST_LENGTH;
- break;
- default:
- return false;
- }
- }
-
- if (cert.key_type == Certificate::KEY_TYPE_RSA) {
- cert.rsa = parse_rsa_key(f.get(), exponent);
- if (!cert.rsa) {
- return false;
- }
-
- LOG(INFO) << "read key e=" << exponent << " hash=" << cert.hash_len;
- } else if (cert.key_type == Certificate::KEY_TYPE_EC) {
- cert.ec = parse_ec_key(f.get());
- if (!cert.ec) {
- return false;
- }
- } else {
- LOG(ERROR) << "Unknown key type " << cert.key_type;
- return false;
- }
-
- // if the line ends in a comma, this file has more keys.
- int ch = fgetc(f.get());
- if (ch == ',') {
- // more keys to come.
- continue;
- } else if (ch == EOF) {
- break;
- } else {
- LOG(ERROR) << "unexpected character between keys";
- return false;
- }
- }
- return true;
-}