summaryrefslogtreecommitdiffstats
path: root/verifier.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'verifier.cpp')
-rw-r--r--verifier.cpp456
1 files changed, 220 insertions, 236 deletions
diff --git a/verifier.cpp b/verifier.cpp
index 229cc9dfe..2ef9c4c37 100644
--- a/verifier.cpp
+++ b/verifier.cpp
@@ -14,26 +14,25 @@
* limitations under the License.
*/
+#include "verifier.h"
+
#include <errno.h>
-#include <malloc.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <algorithm>
+#include <functional>
#include <memory>
+#include <vector>
+#include <android-base/logging.h>
+#include <openssl/bn.h>
#include <openssl/ecdsa.h>
#include <openssl/obj_mac.h>
#include "asn1_decoder.h"
-#include "common.h"
#include "print_sha1.h"
-#include "ui.h"
-#include "verifier.h"
-
-//extern RecoveryUI* ui;
-
-#define PUBLIC_KEYS_FILE "/res/keys"
static constexpr size_t MiB = 1024 * 1024;
@@ -62,261 +61,246 @@ static constexpr size_t MiB = 1024 * 1024;
* SEQUENCE (SignatureAlgorithmIdentifier)
* OCTET STRING (SignatureValue)
*/
-static bool read_pkcs7(uint8_t* pkcs7_der, size_t pkcs7_der_len, uint8_t** sig_der,
- size_t* sig_der_length) {
- asn1_context_t* ctx = asn1_context_new(pkcs7_der, pkcs7_der_len);
- if (ctx == NULL) {
- return false;
- }
+static bool read_pkcs7(const uint8_t* pkcs7_der, size_t pkcs7_der_len,
+ std::vector<uint8_t>* sig_der) {
+ CHECK(sig_der != nullptr);
+ sig_der->clear();
- asn1_context_t* pkcs7_seq = asn1_sequence_get(ctx);
- if (pkcs7_seq != NULL && asn1_sequence_next(pkcs7_seq)) {
- asn1_context_t *signed_data_app = asn1_constructed_get(pkcs7_seq);
- if (signed_data_app != NULL) {
- asn1_context_t* signed_data_seq = asn1_sequence_get(signed_data_app);
- if (signed_data_seq != NULL
- && asn1_sequence_next(signed_data_seq)
- && asn1_sequence_next(signed_data_seq)
- && asn1_sequence_next(signed_data_seq)
- && asn1_constructed_skip_all(signed_data_seq)) {
- asn1_context_t *sig_set = asn1_set_get(signed_data_seq);
- if (sig_set != NULL) {
- asn1_context_t* sig_seq = asn1_sequence_get(sig_set);
- if (sig_seq != NULL
- && asn1_sequence_next(sig_seq)
- && asn1_sequence_next(sig_seq)
- && asn1_sequence_next(sig_seq)
- && asn1_sequence_next(sig_seq)) {
- uint8_t* sig_der_ptr;
- if (asn1_octet_string_get(sig_seq, &sig_der_ptr, sig_der_length)) {
- *sig_der = (uint8_t*) malloc(*sig_der_length);
- if (*sig_der != NULL) {
- memcpy(*sig_der, sig_der_ptr, *sig_der_length);
- }
- }
- asn1_context_free(sig_seq);
- }
- asn1_context_free(sig_set);
- }
- asn1_context_free(signed_data_seq);
- }
- asn1_context_free(signed_data_app);
- }
- asn1_context_free(pkcs7_seq);
- }
- asn1_context_free(ctx);
+ asn1_context ctx(pkcs7_der, pkcs7_der_len);
- return *sig_der != NULL;
-}
+ std::unique_ptr<asn1_context> pkcs7_seq(ctx.asn1_sequence_get());
+ if (pkcs7_seq == nullptr || !pkcs7_seq->asn1_sequence_next()) {
+ return false;
+ }
-// Look for an RSA signature embedded in the .ZIP file comment given
-// the path to the zip. Verify it matches one of the given public
-// keys.
-//
-// Return VERIFY_SUCCESS, VERIFY_FAILURE (if any error is encountered
-// or no key matches the signature).
+ std::unique_ptr<asn1_context> signed_data_app(pkcs7_seq->asn1_constructed_get());
+ if (signed_data_app == nullptr) {
+ return false;
+ }
-int verify_file(unsigned char* addr, size_t length) {
- //ui->SetProgress(0.0);
+ std::unique_ptr<asn1_context> signed_data_seq(signed_data_app->asn1_sequence_get());
+ if (signed_data_seq == nullptr ||
+ !signed_data_seq->asn1_sequence_next() ||
+ !signed_data_seq->asn1_sequence_next() ||
+ !signed_data_seq->asn1_sequence_next() ||
+ !signed_data_seq->asn1_constructed_skip_all()) {
+ return false;
+ }
- std::vector<Certificate> keys;
- if (!load_keys(PUBLIC_KEYS_FILE, keys)) {
- LOGE("Failed to load keys\n");
- return INSTALL_CORRUPT;
- }
- LOGI("%d key(s) loaded from %s\n", keys.size(), PUBLIC_KEYS_FILE);
+ std::unique_ptr<asn1_context> sig_set(signed_data_seq->asn1_set_get());
+ if (sig_set == nullptr) {
+ return false;
+ }
+
+ std::unique_ptr<asn1_context> sig_seq(sig_set->asn1_sequence_get());
+ if (sig_seq == nullptr ||
+ !sig_seq->asn1_sequence_next() ||
+ !sig_seq->asn1_sequence_next() ||
+ !sig_seq->asn1_sequence_next() ||
+ !sig_seq->asn1_sequence_next()) {
+ return false;
+ }
+
+ const uint8_t* sig_der_ptr;
+ size_t sig_der_length;
+ if (!sig_seq->asn1_octet_string_get(&sig_der_ptr, &sig_der_length)) {
+ return false;
+ }
+
+ sig_der->resize(sig_der_length);
+ std::copy(sig_der_ptr, sig_der_ptr + sig_der_length, sig_der->begin());
+ return true;
+}
+
+/*
+ * Looks for an RSA signature embedded in the .ZIP file comment given the path to the zip. Verifies
+ * that it matches one of the given public keys. A callback function can be optionally provided for
+ * posting the progress.
+ *
+ * Returns VERIFY_SUCCESS or VERIFY_FAILURE (if any error is encountered or no key matches the
+ * signature).
+ */
+int verify_file(const unsigned char* addr, size_t length, const std::vector<Certificate>& keys,
+ const std::function<void(float)>& set_progress) {
+ if (set_progress) {
+ set_progress(0.0);
+ }
- // An archive with a whole-file signature will end in six bytes:
- //
- // (2-byte signature start) $ff $ff (2-byte comment size)
- //
- // (As far as the ZIP format is concerned, these are part of the
- // archive comment.) We start by reading this footer, this tells
- // us how far back from the end we have to start reading to find
- // the whole comment.
+ // An archive with a whole-file signature will end in six bytes:
+ //
+ // (2-byte signature start) $ff $ff (2-byte comment size)
+ //
+ // (As far as the ZIP format is concerned, these are part of the archive comment.) We start by
+ // reading this footer, this tells us how far back from the end we have to start reading to find
+ // the whole comment.
#define FOOTER_SIZE 6
- if (length < FOOTER_SIZE) {
- LOGE("not big enough to contain footer\n");
- return VERIFY_FAILURE;
- }
+ if (length < FOOTER_SIZE) {
+ LOG(ERROR) << "not big enough to contain footer";
+ return VERIFY_FAILURE;
+ }
- unsigned char* footer = addr + length - FOOTER_SIZE;
+ const unsigned char* footer = addr + length - FOOTER_SIZE;
- if (footer[2] != 0xff || footer[3] != 0xff) {
- LOGE("footer is wrong\n");
- return VERIFY_FAILURE;
- }
+ if (footer[2] != 0xff || footer[3] != 0xff) {
+ LOG(ERROR) << "footer is wrong";
+ return VERIFY_FAILURE;
+ }
- size_t comment_size = footer[4] + (footer[5] << 8);
- size_t signature_start = footer[0] + (footer[1] << 8);
- LOGI("comment is %zu bytes; signature %zu bytes from end\n",
- comment_size, signature_start);
+ size_t comment_size = footer[4] + (footer[5] << 8);
+ size_t signature_start = footer[0] + (footer[1] << 8);
+ LOG(INFO) << "comment is " << comment_size << " bytes; signature is " << signature_start
+ << " bytes from end";
- if (signature_start > comment_size) {
- LOGE("signature start: %zu is larger than comment size: %zu\n", signature_start,
- comment_size);
- return VERIFY_FAILURE;
- }
+ if (signature_start > comment_size) {
+ LOG(ERROR) << "signature start: " << signature_start << " is larger than comment size: "
+ << comment_size;
+ return VERIFY_FAILURE;
+ }
- if (signature_start <= FOOTER_SIZE) {
- LOGE("Signature start is in the footer");
- return VERIFY_FAILURE;
- }
+ if (signature_start <= FOOTER_SIZE) {
+ LOG(ERROR) << "Signature start is in the footer";
+ return VERIFY_FAILURE;
+ }
#define EOCD_HEADER_SIZE 22
- // The end-of-central-directory record is 22 bytes plus any
- // comment length.
- size_t eocd_size = comment_size + EOCD_HEADER_SIZE;
+ // The end-of-central-directory record is 22 bytes plus any comment length.
+ size_t eocd_size = comment_size + EOCD_HEADER_SIZE;
- if (length < eocd_size) {
- LOGE("not big enough to contain EOCD\n");
- return VERIFY_FAILURE;
- }
+ if (length < eocd_size) {
+ LOG(ERROR) << "not big enough to contain EOCD";
+ return VERIFY_FAILURE;
+ }
- // Determine how much of the file is covered by the signature.
- // This is everything except the signature data and length, which
- // includes all of the EOCD except for the comment length field (2
- // bytes) and the comment data.
- size_t signed_len = length - eocd_size + EOCD_HEADER_SIZE - 2;
+ // Determine how much of the file is covered by the signature. This is everything except the
+ // signature data and length, which includes all of the EOCD except for the comment length field
+ // (2 bytes) and the comment data.
+ size_t signed_len = length - eocd_size + EOCD_HEADER_SIZE - 2;
- unsigned char* eocd = addr + length - eocd_size;
+ const unsigned char* eocd = addr + length - eocd_size;
- // If this is really is the EOCD record, it will begin with the
- // magic number $50 $4b $05 $06.
- if (eocd[0] != 0x50 || eocd[1] != 0x4b ||
- eocd[2] != 0x05 || eocd[3] != 0x06) {
- LOGE("signature length doesn't match EOCD marker\n");
- return VERIFY_FAILURE;
- }
+ // If this is really is the EOCD record, it will begin with the magic number $50 $4b $05 $06.
+ if (eocd[0] != 0x50 || eocd[1] != 0x4b || eocd[2] != 0x05 || eocd[3] != 0x06) {
+ LOG(ERROR) << "signature length doesn't match EOCD marker";
+ return VERIFY_FAILURE;
+ }
- for (size_t i = 4; i < eocd_size-3; ++i) {
- if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b &&
- eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
- // if the sequence $50 $4b $05 $06 appears anywhere after
- // the real one, minzip will find the later (wrong) one,
- // which could be exploitable. Fail verification if
- // this sequence occurs anywhere after the real one.
- LOGE("EOCD marker occurs after start of EOCD\n");
- return VERIFY_FAILURE;
- }
+ for (size_t i = 4; i < eocd_size-3; ++i) {
+ if (eocd[i] == 0x50 && eocd[i+1] == 0x4b && eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
+ // If the sequence $50 $4b $05 $06 appears anywhere after the real one, libziparchive will
+ // find the later (wrong) one, which could be exploitable. Fail the verification if this
+ // sequence occurs anywhere after the real one.
+ LOG(ERROR) << "EOCD marker occurs after start of EOCD";
+ return VERIFY_FAILURE;
}
+ }
- bool need_sha1 = false;
- bool need_sha256 = false;
- for (const auto& key : keys) {
- switch (key.hash_len) {
- case SHA_DIGEST_LENGTH: need_sha1 = true; break;
- case SHA256_DIGEST_LENGTH: need_sha256 = true; break;
- }
+ bool need_sha1 = false;
+ bool need_sha256 = false;
+ for (const auto& key : keys) {
+ switch (key.hash_len) {
+ case SHA_DIGEST_LENGTH: need_sha1 = true; break;
+ case SHA256_DIGEST_LENGTH: need_sha256 = true; break;
}
+ }
- SHA_CTX sha1_ctx;
- SHA256_CTX sha256_ctx;
- SHA1_Init(&sha1_ctx);
- SHA256_Init(&sha256_ctx);
-
- double frac = -1.0;
- size_t so_far = 0;
- while (so_far < signed_len) {
- // On a Nexus 5X, experiment showed 16MiB beat 1MiB by 6% faster for a
- // 1196MiB full OTA and 60% for an 89MiB incremental OTA.
- // http://b/28135231.
- size_t size = std::min(signed_len - so_far, 16 * MiB);
-
- if (need_sha1) SHA1_Update(&sha1_ctx, addr + so_far, size);
- if (need_sha256) SHA256_Update(&sha256_ctx, addr + so_far, size);
- so_far += size;
-
- double f = so_far / (double)signed_len;
- if (f > frac + 0.02 || size == so_far) {
- //ui->SetProgress(f);
- frac = f;
- }
+ SHA_CTX sha1_ctx;
+ SHA256_CTX sha256_ctx;
+ SHA1_Init(&sha1_ctx);
+ SHA256_Init(&sha256_ctx);
+
+ double frac = -1.0;
+ size_t so_far = 0;
+ while (so_far < signed_len) {
+ // On a Nexus 5X, experiment showed 16MiB beat 1MiB by 6% faster for a
+ // 1196MiB full OTA and 60% for an 89MiB incremental OTA.
+ // http://b/28135231.
+ size_t size = std::min(signed_len - so_far, 16 * MiB);
+
+ if (need_sha1) SHA1_Update(&sha1_ctx, addr + so_far, size);
+ if (need_sha256) SHA256_Update(&sha256_ctx, addr + so_far, size);
+ so_far += size;
+
+ if (set_progress) {
+ double f = so_far / (double)signed_len;
+ if (f > frac + 0.02 || size == so_far) {
+ set_progress(f);
+ frac = f;
+ }
}
+ }
- uint8_t sha1[SHA_DIGEST_LENGTH];
- SHA1_Final(sha1, &sha1_ctx);
- uint8_t sha256[SHA256_DIGEST_LENGTH];
- SHA256_Final(sha256, &sha256_ctx);
-
- uint8_t* sig_der = nullptr;
- size_t sig_der_length = 0;
-
- uint8_t* signature = eocd + eocd_size - signature_start;
- size_t signature_size = signature_start - FOOTER_SIZE;
-
- LOGI("signature (offset: 0x%zx, length: %zu): %s\n",
- length - signature_start, signature_size,
- print_hex(signature, signature_size).c_str());
+ uint8_t sha1[SHA_DIGEST_LENGTH];
+ SHA1_Final(sha1, &sha1_ctx);
+ uint8_t sha256[SHA256_DIGEST_LENGTH];
+ SHA256_Final(sha256, &sha256_ctx);
- if (!read_pkcs7(signature, signature_size, &sig_der, &sig_der_length)) {
- LOGE("Could not find signature DER block\n");
- return VERIFY_FAILURE;
- }
+ const uint8_t* signature = eocd + eocd_size - signature_start;
+ size_t signature_size = signature_start - FOOTER_SIZE;
- /*
- * Check to make sure at least one of the keys matches the signature. Since
- * any key can match, we need to try each before determining a verification
- * failure has happened.
- */
- size_t i = 0;
- for (const auto& key : keys) {
- const uint8_t* hash;
- int hash_nid;
- switch (key.hash_len) {
- case SHA_DIGEST_LENGTH:
- hash = sha1;
- hash_nid = NID_sha1;
- break;
- case SHA256_DIGEST_LENGTH:
- hash = sha256;
- hash_nid = NID_sha256;
- break;
- default:
- continue;
- }
-
- // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that
- // the signing tool appends after the signature itself.
- if (key.key_type == Certificate::KEY_TYPE_RSA) {
- if (!RSA_verify(hash_nid, hash, key.hash_len, sig_der,
- sig_der_length, key.rsa.get())) {
- LOGI("failed to verify against RSA key %zu\n", i);
- continue;
- }
+ LOG(INFO) << "signature (offset: " << std::hex << (length - signature_start) << ", length: "
+ << signature_size << "): " << print_hex(signature, signature_size);
- LOGI("whole-file signature verified against RSA key %zu\n", i);
- free(sig_der);
- return VERIFY_SUCCESS;
- } else if (key.key_type == Certificate::KEY_TYPE_EC
- && key.hash_len == SHA256_DIGEST_LENGTH) {
- if (!ECDSA_verify(0, hash, key.hash_len, sig_der,
- sig_der_length, key.ec.get())) {
- LOGI("failed to verify against EC key %zu\n", i);
- continue;
- }
+ std::vector<uint8_t> sig_der;
+ if (!read_pkcs7(signature, signature_size, &sig_der)) {
+ LOG(ERROR) << "Could not find signature DER block";
+ return VERIFY_FAILURE;
+ }
- LOGI("whole-file signature verified against EC key %zu\n", i);
- free(sig_der);
- return VERIFY_SUCCESS;
- } else {
- LOGI("Unknown key type %d\n", key.key_type);
- }
- i++;
- }
+ // Check to make sure at least one of the keys matches the signature. Since any key can match,
+ // we need to try each before determining a verification failure has happened.
+ size_t i = 0;
+ for (const auto& key : keys) {
+ const uint8_t* hash;
+ int hash_nid;
+ switch (key.hash_len) {
+ case SHA_DIGEST_LENGTH:
+ hash = sha1;
+ hash_nid = NID_sha1;
+ break;
+ case SHA256_DIGEST_LENGTH:
+ hash = sha256;
+ hash_nid = NID_sha256;
+ break;
+ default:
+ continue;
+ }
+
+ // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that the signing tool appends
+ // after the signature itself.
+ if (key.key_type == Certificate::KEY_TYPE_RSA) {
+ if (!RSA_verify(hash_nid, hash, key.hash_len, sig_der.data(), sig_der.size(),
+ key.rsa.get())) {
+ LOG(INFO) << "failed to verify against RSA key " << i;
+ continue;
+ }
+
+ LOG(INFO) << "whole-file signature verified against RSA key " << i;
+ return VERIFY_SUCCESS;
+ } else if (key.key_type == Certificate::KEY_TYPE_EC && key.hash_len == SHA256_DIGEST_LENGTH) {
+ if (!ECDSA_verify(0, hash, key.hash_len, sig_der.data(), sig_der.size(), key.ec.get())) {
+ LOG(INFO) << "failed to verify against EC key " << i;
+ continue;
+ }
+
+ LOG(INFO) << "whole-file signature verified against EC key " << i;
+ return VERIFY_SUCCESS;
+ } else {
+ LOG(INFO) << "Unknown key type " << key.key_type;
+ }
+ i++;
+ }
- if (need_sha1) {
- LOGI("SHA-1 digest: %s\n", print_hex(sha1, SHA_DIGEST_LENGTH).c_str());
- }
- if (need_sha256) {
- LOGI("SHA-256 digest: %s\n", print_hex(sha256, SHA256_DIGEST_LENGTH).c_str());
- }
- free(sig_der);
- LOGE("failed to verify whole-file signature\n");
- return VERIFY_FAILURE;
+ if (need_sha1) {
+ LOG(INFO) << "SHA-1 digest: " << print_hex(sha1, SHA_DIGEST_LENGTH);
+ }
+ if (need_sha256) {
+ LOG(INFO) << "SHA-256 digest: " << print_hex(sha256, SHA256_DIGEST_LENGTH);
+ }
+ LOG(ERROR) << "failed to verify whole-file signature";
+ return VERIFY_FAILURE;
}
std::unique_ptr<RSA, RSADeleter> parse_rsa_key(FILE* file, uint32_t exponent) {
@@ -336,7 +320,7 @@ std::unique_ptr<RSA, RSADeleter> parse_rsa_key(FILE* file, uint32_t exponent) {
}
if (key_len_words > 8192 / 32) {
- LOGE("key length (%d) too large\n", key_len_words);
+ LOG(ERROR) << "key length (" << key_len_words << ") too large";
return nullptr;
}
@@ -392,7 +376,7 @@ std::unique_ptr<RSA, RSADeleter> parse_rsa_key(FILE* file, uint32_t exponent) {
}
struct BNDeleter {
- void operator()(BIGNUM* bn) {
+ void operator()(BIGNUM* bn) const {
BN_free(bn);
}
};
@@ -492,7 +476,7 @@ std::unique_ptr<EC_KEY, ECKEYDeleter> parse_ec_key(FILE* file) {
bool load_keys(const char* filename, std::vector<Certificate>& certs) {
std::unique_ptr<FILE, decltype(&fclose)> f(fopen(filename, "r"), fclose);
if (!f) {
- LOGE("opening %s: %s\n", filename, strerror(errno));
+ PLOG(ERROR) << "error opening " << filename;
return false;
}
@@ -542,14 +526,14 @@ bool load_keys(const char* filename, std::vector<Certificate>& certs) {
return false;
}
- LOGI("read key e=%d hash=%d\n", exponent, cert.hash_len);
+ 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 {
- LOGE("Unknown key type %d\n", cert.key_type);
+ LOG(ERROR) << "Unknown key type " << cert.key_type;
return false;
}
@@ -561,7 +545,7 @@ bool load_keys(const char* filename, std::vector<Certificate>& certs) {
} else if (ch == EOF) {
break;
} else {
- LOGE("unexpected character between keys\n");
+ LOG(ERROR) << "unexpected character between keys";
return false;
}
}