Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 1 | /* |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved. |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame^] | 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame^] | 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | #include <stdlib.h> |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 31 | #include <stdint.h> |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 32 | #include <crypto_hash.h> |
| 33 | #include <boot_verifier.h> |
| 34 | #include <image_verify.h> |
| 35 | #include <mmc.h> |
| 36 | #include <oem_keystore.h> |
| 37 | #include <openssl/asn1t.h> |
| 38 | #include <openssl/x509.h> |
| 39 | #include <partition_parser.h> |
| 40 | #include <rsa.h> |
| 41 | #include <string.h> |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 42 | #include <openssl/err.h> |
Unnati Gandhi | 8a7cdfe | 2015-05-11 13:04:20 +0530 | [diff] [blame] | 43 | #include <platform.h> |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame^] | 44 | #include <qseecom_lk_api.h> |
| 45 | #include <secapp_loader.h> |
| 46 | #include <target.h> |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 47 | |
| 48 | static KEYSTORE *oem_keystore; |
| 49 | static KEYSTORE *user_keystore; |
| 50 | static uint32_t dev_boot_state = RED; |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 51 | char KEYSTORE_PTN_NAME[] = "keystore"; |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame^] | 52 | RSA *rsa_from_cert = NULL; |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 53 | |
| 54 | ASN1_SEQUENCE(AUTH_ATTR) ={ |
| 55 | ASN1_SIMPLE(AUTH_ATTR, target, ASN1_PRINTABLESTRING), |
| 56 | ASN1_SIMPLE(AUTH_ATTR, len, ASN1_INTEGER) |
| 57 | } ASN1_SEQUENCE_END(AUTH_ATTR) |
| 58 | IMPLEMENT_ASN1_FUNCTIONS(AUTH_ATTR) |
| 59 | |
| 60 | ASN1_SEQUENCE(VERIFIED_BOOT_SIG) = { |
| 61 | ASN1_SIMPLE(VERIFIED_BOOT_SIG, version, ASN1_INTEGER), |
Unnati Gandhi | 9333499 | 2015-02-25 19:38:38 +0530 | [diff] [blame] | 62 | ASN1_SIMPLE(VERIFIED_BOOT_SIG, certificate, X509), |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 63 | ASN1_SIMPLE(VERIFIED_BOOT_SIG, algor, X509_ALGOR), |
| 64 | ASN1_SIMPLE(VERIFIED_BOOT_SIG, auth_attr, AUTH_ATTR), |
| 65 | ASN1_SIMPLE(VERIFIED_BOOT_SIG, sig, ASN1_OCTET_STRING) |
| 66 | } ASN1_SEQUENCE_END(VERIFIED_BOOT_SIG) |
| 67 | IMPLEMENT_ASN1_FUNCTIONS(VERIFIED_BOOT_SIG) |
| 68 | |
| 69 | ASN1_SEQUENCE(KEY) = { |
| 70 | ASN1_SIMPLE(KEY, algorithm_id, X509_ALGOR), |
| 71 | ASN1_SIMPLE(KEY, key_material, RSAPublicKey) |
| 72 | }ASN1_SEQUENCE_END(KEY) |
| 73 | IMPLEMENT_ASN1_FUNCTIONS(KEY); |
| 74 | |
| 75 | ASN1_SEQUENCE(KEYBAG) = { |
| 76 | ASN1_SIMPLE(KEYBAG, mykey, KEY) |
| 77 | }ASN1_SEQUENCE_END(KEYBAG) |
| 78 | IMPLEMENT_ASN1_FUNCTIONS(KEYBAG) |
| 79 | |
| 80 | ASN1_SEQUENCE(KEYSTORE_INNER) = { |
| 81 | ASN1_SIMPLE(KEYSTORE_INNER, version, ASN1_INTEGER), |
| 82 | ASN1_SIMPLE(KEYSTORE_INNER, mykeybag, KEYBAG) |
| 83 | } ASN1_SEQUENCE_END(KEYSTORE_INNER) |
| 84 | IMPLEMENT_ASN1_FUNCTIONS(KEYSTORE_INNER) |
| 85 | |
| 86 | ASN1_SEQUENCE(KEYSTORE) = { |
| 87 | ASN1_SIMPLE(KEYSTORE, version, ASN1_INTEGER), |
| 88 | ASN1_SIMPLE(KEYSTORE, mykeybag, KEYBAG), |
| 89 | ASN1_SIMPLE(KEYSTORE, sig, VERIFIED_BOOT_SIG) |
| 90 | } ASN1_SEQUENCE_END(KEYSTORE) |
| 91 | IMPLEMENT_ASN1_FUNCTIONS(KEYSTORE) |
| 92 | |
| 93 | static uint32_t read_der_message_length(unsigned char* input) |
| 94 | { |
| 95 | uint32_t len = 0; |
| 96 | int pos = 0; |
| 97 | uint8_t len_bytes = 1; |
| 98 | |
| 99 | /* Check if input starts with Sequence id (0X30) */ |
| 100 | if(input[pos] != 0x30) |
| 101 | return len; |
| 102 | pos++; |
| 103 | |
| 104 | /* A length of 0xAABBCCDD in DER encoded messages would be sequence of |
| 105 | following octets 0xAA, 0xBB, 0XCC, 0XDD. |
| 106 | |
| 107 | To read length - read each octet and shift left by 1 octect before |
| 108 | reading next octet. |
| 109 | */ |
| 110 | /* check if short or long length form */ |
| 111 | if(input[pos] & 0x80) |
| 112 | { |
| 113 | len_bytes = (input[pos] & ~(0x80)); |
| 114 | pos++; |
| 115 | } |
| 116 | while(len_bytes) |
| 117 | { |
| 118 | /* Shift len by 1 octet */ |
| 119 | len = len << 8; |
| 120 | |
| 121 | /* Read next octet */ |
| 122 | len = len | input[pos]; |
| 123 | pos++; len_bytes--; |
| 124 | } |
| 125 | |
| 126 | /* Add number of octets representing sequence id and length */ |
| 127 | len += pos; |
| 128 | |
| 129 | return len; |
| 130 | } |
| 131 | |
| 132 | static int verify_digest(unsigned char* input, unsigned char *digest, int hash_size) |
| 133 | { |
| 134 | int ret = -1; |
Shashank Mittal | e679722 | 2014-09-19 18:58:43 -0700 | [diff] [blame] | 135 | X509_SIG *sig = NULL; |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 136 | uint32_t len = read_der_message_length(input); |
Shashank Mittal | e679722 | 2014-09-19 18:58:43 -0700 | [diff] [blame] | 137 | if(!len) |
| 138 | { |
| 139 | dprintf(CRITICAL, "boot_verifier: Signature length is invalid.\n"); |
| 140 | return ret; |
| 141 | } |
| 142 | |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 143 | sig = d2i_X509_SIG(NULL, (const unsigned char **) &input, len); |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 144 | if(sig == NULL) |
| 145 | { |
| 146 | dprintf(CRITICAL, "boot_verifier: Reading digest failed\n"); |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | if(sig->digest->length != SHA256_SIZE) |
| 151 | { |
| 152 | dprintf(CRITICAL, "boot_verifier: Digest length error.\n"); |
| 153 | goto verify_digest_error; |
| 154 | } |
| 155 | |
| 156 | if(memcmp(sig->digest->data, digest, hash_size) == 0) |
| 157 | ret = 0; |
| 158 | |
| 159 | verify_digest_error: |
| 160 | if(sig != NULL) |
| 161 | X509_SIG_free(sig); |
| 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | static int add_attribute_to_img(unsigned char *ptr, AUTH_ATTR *input) |
| 167 | { |
| 168 | return i2d_AUTH_ATTR(input, &ptr); |
| 169 | } |
| 170 | |
| 171 | static bool boot_verify_compare_sha256(unsigned char *image_ptr, |
| 172 | unsigned int image_size, unsigned char *signature_ptr, RSA *rsa) |
| 173 | { |
| 174 | int ret = -1; |
| 175 | bool auth = false; |
| 176 | unsigned char *plain_text = NULL; |
| 177 | unsigned int digest[8]; |
| 178 | |
| 179 | plain_text = (unsigned char *)calloc(sizeof(char), SIGNATURE_SIZE); |
| 180 | if (plain_text == NULL) { |
| 181 | dprintf(CRITICAL, "boot_verifier: Calloc failed during verification\n"); |
| 182 | goto cleanup; |
| 183 | } |
| 184 | |
| 185 | /* Calculate SHA256sum */ |
| 186 | image_find_digest(image_ptr, image_size, CRYPTO_AUTH_ALG_SHA256, |
| 187 | (unsigned char *)&digest); |
| 188 | |
| 189 | /* Find digest from the image */ |
| 190 | ret = image_decrypt_signature_rsa(signature_ptr, plain_text, rsa); |
| 191 | |
| 192 | dprintf(SPEW, "boot_verifier: Return of RSA_public_decrypt = %d\n", |
| 193 | ret); |
| 194 | |
| 195 | ret = verify_digest(plain_text, (unsigned char*)digest, SHA256_SIZE); |
| 196 | if(ret == 0) |
| 197 | { |
| 198 | auth = true; |
Sridhar Parasuram | b27e47c | 2015-05-27 14:33:54 -0700 | [diff] [blame] | 199 | #ifdef TZ_SAVE_KERNEL_HASH |
| 200 | save_kernel_hash((unsigned char *) &digest, CRYPTO_AUTH_ALG_SHA256); |
| 201 | #endif |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | cleanup: |
| 205 | if (plain_text != NULL) |
| 206 | free(plain_text); |
| 207 | EVP_cleanup(); |
| 208 | CRYPTO_cleanup_all_ex_data(); |
| 209 | ERR_remove_thread_state(NULL); |
| 210 | return auth; |
| 211 | |
| 212 | } |
| 213 | |
| 214 | static bool verify_image_with_sig(unsigned char* img_addr, uint32_t img_size, |
| 215 | char *pname, VERIFIED_BOOT_SIG *sig, KEYSTORE *ks) |
| 216 | { |
| 217 | bool ret = false; |
| 218 | uint32_t len; |
| 219 | int shift_bytes; |
| 220 | RSA *rsa = NULL; |
| 221 | bool keystore_verification = false; |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame^] | 222 | EVP_PKEY* key = NULL; |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 223 | |
| 224 | if(!strcmp(pname, "keystore")) |
| 225 | keystore_verification = true; |
| 226 | |
| 227 | /* Verify target name */ |
| 228 | if(strncmp((char*)(sig->auth_attr->target->data), pname, |
| 229 | sig->auth_attr->target->length) || |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 230 | (strlen(pname) != (unsigned long) sig->auth_attr->target->length)) |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 231 | { |
| 232 | dprintf(CRITICAL, |
| 233 | "boot_verifier: verification failure due to target name mismatch\n"); |
| 234 | goto verify_image_with_sig_error; |
| 235 | } |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 236 | /* Read image size from signature */ |
| 237 | /* A len = 0xAABBCC (represented by 3 octets) would be stored in |
| 238 | len->data as 0X00CCBBAA and len->length as 3(octets). |
| 239 | |
| 240 | To read len we need to left shift data to number of missing octets and |
| 241 | then change it to host long |
| 242 | */ |
| 243 | len = *((uint32_t*)sig->auth_attr->len->data); |
| 244 | shift_bytes = sizeof(uint32_t) - sig->auth_attr->len->length; |
| 245 | if(shift_bytes > 0) { |
| 246 | len = len << (shift_bytes*8); |
| 247 | } |
| 248 | len = ntohl(len); |
| 249 | |
| 250 | /* Verify image size*/ |
| 251 | if(len != img_size) |
| 252 | { |
| 253 | dprintf(CRITICAL, |
| 254 | "boot_verifier: image length is different. (%d vs %d)\n", |
| 255 | len, img_size); |
| 256 | goto verify_image_with_sig_error; |
| 257 | } |
| 258 | |
| 259 | /* append attribute to image */ |
| 260 | if(!keystore_verification) |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame^] | 261 | { |
| 262 | // verifying a non keystore partition |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 263 | img_size += add_attribute_to_img((unsigned char*)(img_addr + img_size), |
| 264 | sig->auth_attr); |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame^] | 265 | } |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 266 | |
| 267 | /* compare SHA256SUM of image with value in signature */ |
| 268 | if(ks != NULL) |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 269 | { |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame^] | 270 | // use rsa from keystore |
| 271 | rsa = ks->mykeybag->mykey->key_material; |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | dprintf(CRITICAL, "%s:%d: Keystore is null\n", __func__, __LINE__); |
| 276 | ASSERT(0); |
| 277 | } |
| 278 | |
| 279 | // verify boot.img with rsa from oem keystore |
| 280 | if((ret = boot_verify_compare_sha256(img_addr, img_size, |
| 281 | (unsigned char*)sig->sig->data, rsa))) |
| 282 | |
| 283 | { |
| 284 | dprintf(SPEW, "Verified boot.img with oem keystore\n"); |
| 285 | boot_verify_send_event(BOOTIMG_KEYSTORE_VERIFICATION_PASS); |
| 286 | goto verify_image_with_sig_done; |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | dprintf(INFO, "Verification with oem keystore failed. Use embedded certificate for verification\n"); |
| 291 | // get the public key from certificate in boot.img |
| 292 | if ((key = X509_get_pubkey(sig->certificate))) |
| 293 | { |
| 294 | // convert to rsa key format |
| 295 | dprintf(INFO, "RSA KEY found from the embedded certificate\n"); |
| 296 | rsa = EVP_PKEY_get1_RSA(key); |
| 297 | rsa_from_cert = rsa; |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | dprintf(CRITICAL, "Unable to extract public key from certificate\n"); |
| 302 | ASSERT(0); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // verify boot.img with rsa from embedded certificate |
| 307 | if ((ret = boot_verify_compare_sha256(img_addr, img_size, |
| 308 | (unsigned char*)sig->sig->data, rsa))) |
| 309 | { |
| 310 | dprintf(SPEW, "Verified boot.img with embedded certificate in boot image\n"); |
| 311 | boot_verify_send_event(BOOTIMG_EMBEDDED_CERT_VERIFICATION_PASS); |
| 312 | goto verify_image_with_sig_done; |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | dprintf(INFO, "verified for red state\n"); |
| 317 | boot_verify_send_event(BOOTIMG_VERIFICATION_FAIL); |
| 318 | goto verify_image_with_sig_done; |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | verify_image_with_sig_error: |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame^] | 322 | verify_image_with_sig_done: |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | static int encode_inner_keystore(unsigned char *ptr, KEYSTORE *ks) |
| 327 | { |
| 328 | int ret = 0; |
| 329 | KEYSTORE_INNER *ks_inner = KEYSTORE_INNER_new(); |
| 330 | if (ks_inner == NULL) |
| 331 | return ret; |
| 332 | ASN1_INTEGER *tmp_version = ks_inner->version; |
| 333 | KEYBAG *tmp_mykeybag = ks_inner->mykeybag; |
| 334 | |
| 335 | ks_inner->version = ks->version; |
| 336 | ks_inner->mykeybag = ks->mykeybag; |
| 337 | ret = i2d_KEYSTORE_INNER(ks_inner, &ptr); |
| 338 | |
| 339 | ks_inner->version = tmp_version; |
| 340 | ks_inner->mykeybag = tmp_mykeybag; |
| 341 | |
| 342 | if(ks_inner != NULL) |
| 343 | KEYSTORE_INNER_free(ks_inner); |
| 344 | return ret; |
| 345 | } |
| 346 | |
| 347 | static bool verify_keystore(unsigned char * ks_addr, KEYSTORE *ks) |
| 348 | { |
| 349 | bool ret = false; |
| 350 | unsigned char * ptr = ks_addr; |
| 351 | uint32_t inner_len = encode_inner_keystore(ptr, ks); |
| 352 | ret = verify_image_with_sig(ks_addr, inner_len, "keystore", ks->sig, |
| 353 | oem_keystore); |
| 354 | return ret; |
| 355 | } |
| 356 | |
| 357 | static void read_oem_keystore() |
| 358 | { |
Shashank Mittal | e679722 | 2014-09-19 18:58:43 -0700 | [diff] [blame] | 359 | KEYSTORE *ks = NULL; |
| 360 | uint32_t len = 0; |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 361 | const unsigned char *input = OEM_KEYSTORE; |
Shashank Mittal | e679722 | 2014-09-19 18:58:43 -0700 | [diff] [blame] | 362 | |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 363 | if(oem_keystore != NULL) |
| 364 | return; |
| 365 | |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 366 | len = read_der_message_length((unsigned char *)input); |
Shashank Mittal | e679722 | 2014-09-19 18:58:43 -0700 | [diff] [blame] | 367 | if(!len) |
| 368 | { |
| 369 | dprintf(CRITICAL, "boot_verifier: oem keystore length is invalid.\n"); |
| 370 | return; |
| 371 | } |
| 372 | |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 373 | ks = d2i_KEYSTORE(NULL, (const unsigned char **) &input, len); |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 374 | if(ks != NULL) |
| 375 | { |
| 376 | oem_keystore = ks; |
| 377 | user_keystore = ks; |
| 378 | } |
| 379 | } |
| 380 | |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 381 | uint32_t boot_verify_keystore_init() |
| 382 | { |
| 383 | /* Read OEM Keystore */ |
| 384 | read_oem_keystore(); |
| 385 | |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 386 | return dev_boot_state; |
| 387 | } |
| 388 | |
| 389 | bool boot_verify_image(unsigned char* img_addr, uint32_t img_size, char *pname) |
| 390 | { |
| 391 | bool ret = false; |
| 392 | VERIFIED_BOOT_SIG *sig = NULL; |
| 393 | unsigned char* sig_addr = (unsigned char*)(img_addr + img_size); |
| 394 | uint32_t sig_len = read_der_message_length(sig_addr); |
| 395 | |
| 396 | if(dev_boot_state == ORANGE) |
| 397 | { |
| 398 | dprintf(INFO, "boot_verifier: Device is in ORANGE boot state.\n"); |
| 399 | dprintf(INFO, "boot_verifier: Skipping boot verification.\n"); |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | if(!sig_len) |
| 404 | { |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame^] | 405 | dprintf(CRITICAL, "boot_verifier: Error while reading signature length.\n"); |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 406 | goto verify_image_error; |
| 407 | } |
| 408 | |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 409 | if((sig = d2i_VERIFIED_BOOT_SIG(NULL, (const unsigned char **) &sig_addr, sig_len)) == NULL) |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 410 | { |
| 411 | dprintf(CRITICAL, |
| 412 | "boot_verifier: verification failure due to target name mismatch\n"); |
| 413 | goto verify_image_error; |
| 414 | } |
| 415 | |
| 416 | ret = verify_image_with_sig(img_addr, img_size, pname, sig, user_keystore); |
| 417 | |
| 418 | verify_image_error: |
| 419 | if(sig != NULL) |
| 420 | VERIFIED_BOOT_SIG_free(sig); |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 421 | return ret; |
| 422 | } |
| 423 | |
| 424 | void boot_verify_send_event(uint32_t event) |
| 425 | { |
| 426 | switch(event) |
| 427 | { |
| 428 | case BOOT_INIT: |
| 429 | dev_boot_state = GREEN; |
| 430 | break; |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame^] | 431 | case BOOTIMG_KEYSTORE_VERIFICATION_PASS: |
| 432 | dev_boot_state = GREEN; |
| 433 | break; |
| 434 | case BOOTIMG_EMBEDDED_CERT_VERIFICATION_PASS: |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 435 | if(dev_boot_state == GREEN) |
| 436 | dev_boot_state = YELLOW; |
| 437 | break; |
Sridhar Parasuram | 8b79242 | 2015-07-05 11:38:13 -0700 | [diff] [blame^] | 438 | case BOOTIMG_VERIFICATION_FAIL: |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 439 | if(dev_boot_state == GREEN || dev_boot_state == YELLOW) |
| 440 | dev_boot_state = RED; |
| 441 | break; |
| 442 | case DEV_UNLOCK: |
| 443 | dev_boot_state = ORANGE; |
| 444 | break; |
| 445 | case USER_DENIES: |
| 446 | if(dev_boot_state == YELLOW || dev_boot_state == ORANGE) |
| 447 | dev_boot_state = RED; |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | uint32_t boot_verify_get_state() |
| 453 | { |
| 454 | return dev_boot_state; |
| 455 | } |
| 456 | |
| 457 | void boot_verify_print_state() |
| 458 | { |
| 459 | switch(dev_boot_state) |
| 460 | { |
| 461 | case GREEN: |
| 462 | dprintf(INFO, "boot_verifier: Device is in GREEN boot state.\n"); |
| 463 | break; |
| 464 | case ORANGE: |
| 465 | dprintf(INFO, "boot_verifier: Device is in ORANGE boot state.\n"); |
| 466 | break; |
| 467 | case YELLOW: |
| 468 | dprintf(INFO, "boot_verifier: Device is in YELLOW boot state.\n"); |
| 469 | break; |
| 470 | case RED: |
Unnati Gandhi | 8a7cdfe | 2015-05-11 13:04:20 +0530 | [diff] [blame] | 471 | display_fbcon_message("Security Error: This phone has been flashed with unauthorized software & is locked. Call your mobile operator for additional support.Please note that repair/return for this issue may have additional cost.\n"); |
| 472 | |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 473 | dprintf(INFO, "boot_verifier: Device is in RED boot state.\n"); |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | bool boot_verify_validate_keystore(unsigned char * user_addr) |
| 479 | { |
| 480 | bool ret = false; |
| 481 | unsigned char *input = user_addr; |
Shashank Mittal | e679722 | 2014-09-19 18:58:43 -0700 | [diff] [blame] | 482 | KEYSTORE *ks = NULL; |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 483 | uint32_t len = read_der_message_length(input); |
Shashank Mittal | e679722 | 2014-09-19 18:58:43 -0700 | [diff] [blame] | 484 | if(!len) |
| 485 | { |
| 486 | dprintf(CRITICAL, "boot_verifier: keystore length is invalid.\n"); |
| 487 | return ret; |
| 488 | } |
| 489 | |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 490 | ks = d2i_KEYSTORE(NULL, (const unsigned char **)&input, len); |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 491 | if(ks != NULL) |
| 492 | { |
| 493 | ret = true; |
| 494 | } |
| 495 | return ret; |
| 496 | } |
| 497 | |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 498 | static bool check_list(const char **list, const char* entry) |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 499 | { |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 500 | if(list == NULL || entry == NULL) |
| 501 | return false; |
| 502 | |
| 503 | while(*list != NULL) |
| 504 | { |
| 505 | if(!strcmp(entry, *list)) |
| 506 | return true; |
| 507 | |
| 508 | list++; |
| 509 | } |
| 510 | |
| 511 | return false; |
| 512 | } |