aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTao Bao2017-03-21 14:09:37 -0500
committerandroid-build-merger2017-03-21 14:09:37 -0500
commit43bd2c8414f44cb2ea79a7987c4401ef2eee28c7 (patch)
tree02d28a8f4945a978bbf449d058d5e9ee05236646 /verifier.cpp
parent43f5ad6e900025c4378f1d4023cd8070efbf70f2 (diff)
parent5b2bf90e13229c8df6a5c02233753a6bd6d1c928 (diff)
downloadplatform-bootable-recovery-43bd2c8414f44cb2ea79a7987c4401ef2eee28c7.tar.gz
platform-bootable-recovery-43bd2c8414f44cb2ea79a7987c4401ef2eee28c7.tar.xz
platform-bootable-recovery-43bd2c8414f44cb2ea79a7987c4401ef2eee28c7.zip
Merge "verify_file: Add constness to a few addresses."
am: 5b2bf90e13 Change-Id: I6e04bf2bc3dc8c978edafafcbb41401189865233
Diffstat (limited to 'verifier.cpp')
-rw-r--r--verifier.cpp107
1 files changed, 53 insertions, 54 deletions
diff --git a/verifier.cpp b/verifier.cpp
index 6daeac94..7f165860 100644
--- a/verifier.cpp
+++ b/verifier.cpp
@@ -24,6 +24,7 @@
24#include <algorithm> 24#include <algorithm>
25#include <functional> 25#include <functional>
26#include <memory> 26#include <memory>
27#include <vector>
27 28
28#include <android-base/logging.h> 29#include <android-base/logging.h>
29#include <openssl/bn.h> 30#include <openssl/bn.h>
@@ -60,51 +61,53 @@ static constexpr size_t MiB = 1024 * 1024;
60 * SEQUENCE (SignatureAlgorithmIdentifier) 61 * SEQUENCE (SignatureAlgorithmIdentifier)
61 * OCTET STRING (SignatureValue) 62 * OCTET STRING (SignatureValue)
62 */ 63 */
63static bool read_pkcs7(uint8_t* pkcs7_der, size_t pkcs7_der_len, uint8_t** sig_der, 64static bool read_pkcs7(const uint8_t* pkcs7_der, size_t pkcs7_der_len,
64 size_t* sig_der_length) { 65 std::vector<uint8_t>* sig_der) {
65 asn1_context_t* ctx = asn1_context_new(pkcs7_der, pkcs7_der_len); 66 CHECK(sig_der != nullptr);
66 if (ctx == NULL) { 67 sig_der->clear();
67 return false; 68
68 } 69 asn1_context_t* ctx = asn1_context_new(pkcs7_der, pkcs7_der_len);
70 if (ctx == NULL) {
71 return false;
72 }
69 73
70 asn1_context_t* pkcs7_seq = asn1_sequence_get(ctx); 74 asn1_context_t* pkcs7_seq = asn1_sequence_get(ctx);
71 if (pkcs7_seq != NULL && asn1_sequence_next(pkcs7_seq)) { 75 if (pkcs7_seq != NULL && asn1_sequence_next(pkcs7_seq)) {
72 asn1_context_t *signed_data_app = asn1_constructed_get(pkcs7_seq); 76 asn1_context_t *signed_data_app = asn1_constructed_get(pkcs7_seq);
73 if (signed_data_app != NULL) { 77 if (signed_data_app != NULL) {
74 asn1_context_t* signed_data_seq = asn1_sequence_get(signed_data_app); 78 asn1_context_t* signed_data_seq = asn1_sequence_get(signed_data_app);
75 if (signed_data_seq != NULL 79 if (signed_data_seq != NULL
76 && asn1_sequence_next(signed_data_seq) 80 && asn1_sequence_next(signed_data_seq)
77 && asn1_sequence_next(signed_data_seq) 81 && asn1_sequence_next(signed_data_seq)
78 && asn1_sequence_next(signed_data_seq) 82 && asn1_sequence_next(signed_data_seq)
79 && asn1_constructed_skip_all(signed_data_seq)) { 83 && asn1_constructed_skip_all(signed_data_seq)) {
80 asn1_context_t *sig_set = asn1_set_get(signed_data_seq); 84 asn1_context_t *sig_set = asn1_set_get(signed_data_seq);
81 if (sig_set != NULL) { 85 if (sig_set != NULL) {
82 asn1_context_t* sig_seq = asn1_sequence_get(sig_set); 86 asn1_context_t* sig_seq = asn1_sequence_get(sig_set);
83 if (sig_seq != NULL 87 if (sig_seq != NULL
84 && asn1_sequence_next(sig_seq) 88 && asn1_sequence_next(sig_seq)
85 && asn1_sequence_next(sig_seq) 89 && asn1_sequence_next(sig_seq)
86 && asn1_sequence_next(sig_seq) 90 && asn1_sequence_next(sig_seq)
87 && asn1_sequence_next(sig_seq)) { 91 && asn1_sequence_next(sig_seq)) {
88 uint8_t* sig_der_ptr; 92 const uint8_t* sig_der_ptr;
89 if (asn1_octet_string_get(sig_seq, &sig_der_ptr, sig_der_length)) { 93 size_t sig_der_length;
90 *sig_der = (uint8_t*) malloc(*sig_der_length); 94 if (asn1_octet_string_get(sig_seq, &sig_der_ptr, &sig_der_length)) {
91 if (*sig_der != NULL) { 95 sig_der->resize(sig_der_length);
92 memcpy(*sig_der, sig_der_ptr, *sig_der_length); 96 std::copy(sig_der_ptr, sig_der_ptr + sig_der_length, sig_der->begin());
93 }
94 }
95 asn1_context_free(sig_seq);
96 }
97 asn1_context_free(sig_set);
98 }
99 asn1_context_free(signed_data_seq);
100 } 97 }
101 asn1_context_free(signed_data_app); 98 asn1_context_free(sig_seq);
99 }
100 asn1_context_free(sig_set);
102 } 101 }
103 asn1_context_free(pkcs7_seq); 102 asn1_context_free(signed_data_seq);
103 }
104 asn1_context_free(signed_data_app);
104 } 105 }
105 asn1_context_free(ctx); 106 asn1_context_free(pkcs7_seq);
107 }
108 asn1_context_free(ctx);
106 109
107 return *sig_der != NULL; 110 return !sig_der->empty();
108} 111}
109 112
110/* 113/*
@@ -115,7 +118,7 @@ static bool read_pkcs7(uint8_t* pkcs7_der, size_t pkcs7_der_len, uint8_t** sig_d
115 * Returns VERIFY_SUCCESS or VERIFY_FAILURE (if any error is encountered or no key matches the 118 * Returns VERIFY_SUCCESS or VERIFY_FAILURE (if any error is encountered or no key matches the
116 * signature). 119 * signature).
117 */ 120 */
118int verify_file(unsigned char* addr, size_t length, const std::vector<Certificate>& keys, 121int verify_file(const unsigned char* addr, size_t length, const std::vector<Certificate>& keys,
119 const std::function<void(float)>& set_progress) { 122 const std::function<void(float)>& set_progress) {
120 if (set_progress) { 123 if (set_progress) {
121 set_progress(0.0); 124 set_progress(0.0);
@@ -136,7 +139,7 @@ int verify_file(unsigned char* addr, size_t length, const std::vector<Certificat
136 return VERIFY_FAILURE; 139 return VERIFY_FAILURE;
137 } 140 }
138 141
139 unsigned char* footer = addr + length - FOOTER_SIZE; 142 const unsigned char* footer = addr + length - FOOTER_SIZE;
140 143
141 if (footer[2] != 0xff || footer[3] != 0xff) { 144 if (footer[2] != 0xff || footer[3] != 0xff) {
142 LOG(ERROR) << "footer is wrong"; 145 LOG(ERROR) << "footer is wrong";
@@ -174,7 +177,7 @@ int verify_file(unsigned char* addr, size_t length, const std::vector<Certificat
174 // (2 bytes) and the comment data. 177 // (2 bytes) and the comment data.
175 size_t signed_len = length - eocd_size + EOCD_HEADER_SIZE - 2; 178 size_t signed_len = length - eocd_size + EOCD_HEADER_SIZE - 2;
176 179
177 unsigned char* eocd = addr + length - eocd_size; 180 const unsigned char* eocd = addr + length - eocd_size;
178 181
179 // If this is really is the EOCD record, it will begin with the magic number $50 $4b $05 $06. 182 // If this is really is the EOCD record, it will begin with the magic number $50 $4b $05 $06.
180 if (eocd[0] != 0x50 || eocd[1] != 0x4b || eocd[2] != 0x05 || eocd[3] != 0x06) { 183 if (eocd[0] != 0x50 || eocd[1] != 0x4b || eocd[2] != 0x05 || eocd[3] != 0x06) {
@@ -183,7 +186,7 @@ int verify_file(unsigned char* addr, size_t length, const std::vector<Certificat
183 } 186 }
184 187
185 for (size_t i = 4; i < eocd_size-3; ++i) { 188 for (size_t i = 4; i < eocd_size-3; ++i) {
186 if (eocd[i ] == 0x50 && eocd[i+1] == 0x4b && eocd[i+2] == 0x05 && eocd[i+3] == 0x06) { 189 if (eocd[i] == 0x50 && eocd[i+1] == 0x4b && eocd[i+2] == 0x05 && eocd[i+3] == 0x06) {
187 // If the sequence $50 $4b $05 $06 appears anywhere after the real one, libziparchive will 190 // If the sequence $50 $4b $05 $06 appears anywhere after the real one, libziparchive will
188 // find the later (wrong) one, which could be exploitable. Fail the verification if this 191 // find the later (wrong) one, which could be exploitable. Fail the verification if this
189 // sequence occurs anywhere after the real one. 192 // sequence occurs anywhere after the real one.
@@ -232,16 +235,14 @@ int verify_file(unsigned char* addr, size_t length, const std::vector<Certificat
232 uint8_t sha256[SHA256_DIGEST_LENGTH]; 235 uint8_t sha256[SHA256_DIGEST_LENGTH];
233 SHA256_Final(sha256, &sha256_ctx); 236 SHA256_Final(sha256, &sha256_ctx);
234 237
235 uint8_t* sig_der = nullptr; 238 const uint8_t* signature = eocd + eocd_size - signature_start;
236 size_t sig_der_length = 0;
237
238 uint8_t* signature = eocd + eocd_size - signature_start;
239 size_t signature_size = signature_start - FOOTER_SIZE; 239 size_t signature_size = signature_start - FOOTER_SIZE;
240 240
241 LOG(INFO) << "signature (offset: " << std::hex << (length - signature_start) << ", length: " 241 LOG(INFO) << "signature (offset: " << std::hex << (length - signature_start) << ", length: "
242 << signature_size << "): " << print_hex(signature, signature_size); 242 << signature_size << "): " << print_hex(signature, signature_size);
243 243
244 if (!read_pkcs7(signature, signature_size, &sig_der, &sig_der_length)) { 244 std::vector<uint8_t> sig_der;
245 if (!read_pkcs7(signature, signature_size, &sig_der)) {
245 LOG(ERROR) << "Could not find signature DER block"; 246 LOG(ERROR) << "Could not find signature DER block";
246 return VERIFY_FAILURE; 247 return VERIFY_FAILURE;
247 } 248 }
@@ -268,22 +269,21 @@ int verify_file(unsigned char* addr, size_t length, const std::vector<Certificat
268 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that the signing tool appends 269 // The 6 bytes is the "(signature_start) $ff $ff (comment_size)" that the signing tool appends
269 // after the signature itself. 270 // after the signature itself.
270 if (key.key_type == Certificate::KEY_TYPE_RSA) { 271 if (key.key_type == Certificate::KEY_TYPE_RSA) {
271 if (!RSA_verify(hash_nid, hash, key.hash_len, sig_der, sig_der_length, key.rsa.get())) { 272 if (!RSA_verify(hash_nid, hash, key.hash_len, sig_der.data(), sig_der.size(),
273 key.rsa.get())) {
272 LOG(INFO) << "failed to verify against RSA key " << i; 274 LOG(INFO) << "failed to verify against RSA key " << i;
273 continue; 275 continue;
274 } 276 }
275 277
276 LOG(INFO) << "whole-file signature verified against RSA key " << i; 278 LOG(INFO) << "whole-file signature verified against RSA key " << i;
277 free(sig_der);
278 return VERIFY_SUCCESS; 279 return VERIFY_SUCCESS;
279 } else if (key.key_type == Certificate::KEY_TYPE_EC && key.hash_len == SHA256_DIGEST_LENGTH) { 280 } else if (key.key_type == Certificate::KEY_TYPE_EC && key.hash_len == SHA256_DIGEST_LENGTH) {
280 if (!ECDSA_verify(0, hash, key.hash_len, sig_der, sig_der_length, key.ec.get())) { 281 if (!ECDSA_verify(0, hash, key.hash_len, sig_der.data(), sig_der.size(), key.ec.get())) {
281 LOG(INFO) << "failed to verify against EC key " << i; 282 LOG(INFO) << "failed to verify against EC key " << i;
282 continue; 283 continue;
283 } 284 }
284 285
285 LOG(INFO) << "whole-file signature verified against EC key " << i; 286 LOG(INFO) << "whole-file signature verified against EC key " << i;
286 free(sig_der);
287 return VERIFY_SUCCESS; 287 return VERIFY_SUCCESS;
288 } else { 288 } else {
289 LOG(INFO) << "Unknown key type " << key.key_type; 289 LOG(INFO) << "Unknown key type " << key.key_type;
@@ -297,7 +297,6 @@ int verify_file(unsigned char* addr, size_t length, const std::vector<Certificat
297 if (need_sha256) { 297 if (need_sha256) {
298 LOG(INFO) << "SHA-256 digest: " << print_hex(sha256, SHA256_DIGEST_LENGTH); 298 LOG(INFO) << "SHA-256 digest: " << print_hex(sha256, SHA256_DIGEST_LENGTH);
299 } 299 }
300 free(sig_der);
301 LOG(ERROR) << "failed to verify whole-file signature"; 300 LOG(ERROR) << "failed to verify whole-file signature";
302 return VERIFY_FAILURE; 301 return VERIFY_FAILURE;
303} 302}