Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 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. |
| 28 | */ |
| 29 | |
| 30 | #include <stdlib.h> |
| 31 | #include <stdint.h> |
| 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> |
| 42 | #include <openssl/err.h> |
| 43 | #include <qseecom_lk_api.h> |
| 44 | #include <secapp_loader.h> |
| 45 | #include <target.h> |
| 46 | |
Channagoud Kadabi | c78a320 | 2015-09-08 14:55:09 -0700 | [diff] [blame] | 47 | #define ASN1_ENCODED_SHA256_SIZE 0x33 |
| 48 | #define ASN1_ENCODED_SHA256_OFFSET 0x13 |
| 49 | |
Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 50 | static KEYSTORE *oem_keystore; |
| 51 | static KEYSTORE *user_keystore; |
| 52 | static uint32_t dev_boot_state = RED; |
| 53 | char KEYSTORE_PTN_NAME[] = "keystore"; |
| 54 | RSA *rsa_from_cert = NULL; |
lijuang | f00ffc8 | 2015-07-16 20:06:22 +0800 | [diff] [blame] | 55 | unsigned char fp[EVP_MAX_MD_SIZE]; |
| 56 | uint32_t fp_size; |
Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 57 | |
| 58 | ASN1_SEQUENCE(AUTH_ATTR) ={ |
| 59 | ASN1_SIMPLE(AUTH_ATTR, target, ASN1_PRINTABLESTRING), |
| 60 | ASN1_SIMPLE(AUTH_ATTR, len, ASN1_INTEGER) |
| 61 | } ASN1_SEQUENCE_END(AUTH_ATTR) |
| 62 | IMPLEMENT_ASN1_FUNCTIONS(AUTH_ATTR) |
| 63 | |
| 64 | ASN1_SEQUENCE(VERIFIED_BOOT_SIG) = { |
| 65 | ASN1_SIMPLE(VERIFIED_BOOT_SIG, version, ASN1_INTEGER), |
| 66 | ASN1_SIMPLE(VERIFIED_BOOT_SIG, certificate, X509), |
| 67 | ASN1_SIMPLE(VERIFIED_BOOT_SIG, algor, X509_ALGOR), |
| 68 | ASN1_SIMPLE(VERIFIED_BOOT_SIG, auth_attr, AUTH_ATTR), |
| 69 | ASN1_SIMPLE(VERIFIED_BOOT_SIG, sig, ASN1_OCTET_STRING) |
| 70 | } ASN1_SEQUENCE_END(VERIFIED_BOOT_SIG) |
| 71 | IMPLEMENT_ASN1_FUNCTIONS(VERIFIED_BOOT_SIG) |
| 72 | |
| 73 | ASN1_SEQUENCE(KEY) = { |
| 74 | ASN1_SIMPLE(KEY, algorithm_id, X509_ALGOR), |
| 75 | ASN1_SIMPLE(KEY, key_material, RSAPublicKey) |
| 76 | }ASN1_SEQUENCE_END(KEY) |
| 77 | IMPLEMENT_ASN1_FUNCTIONS(KEY); |
| 78 | |
| 79 | ASN1_SEQUENCE(KEYBAG) = { |
| 80 | ASN1_SIMPLE(KEYBAG, mykey, KEY) |
| 81 | }ASN1_SEQUENCE_END(KEYBAG) |
| 82 | IMPLEMENT_ASN1_FUNCTIONS(KEYBAG) |
| 83 | |
| 84 | ASN1_SEQUENCE(KEYSTORE_INNER) = { |
| 85 | ASN1_SIMPLE(KEYSTORE_INNER, version, ASN1_INTEGER), |
| 86 | ASN1_SIMPLE(KEYSTORE_INNER, mykeybag, KEYBAG) |
| 87 | } ASN1_SEQUENCE_END(KEYSTORE_INNER) |
| 88 | IMPLEMENT_ASN1_FUNCTIONS(KEYSTORE_INNER) |
| 89 | |
| 90 | ASN1_SEQUENCE(KEYSTORE) = { |
| 91 | ASN1_SIMPLE(KEYSTORE, version, ASN1_INTEGER), |
| 92 | ASN1_SIMPLE(KEYSTORE, mykeybag, KEYBAG), |
| 93 | ASN1_SIMPLE(KEYSTORE, sig, VERIFIED_BOOT_SIG) |
| 94 | } ASN1_SEQUENCE_END(KEYSTORE) |
| 95 | IMPLEMENT_ASN1_FUNCTIONS(KEYSTORE) |
| 96 | |
| 97 | static uint32_t read_der_message_length(unsigned char* input) |
| 98 | { |
| 99 | uint32_t len = 0; |
| 100 | int pos = 0; |
| 101 | uint8_t len_bytes = 1; |
| 102 | |
| 103 | /* Check if input starts with Sequence id (0X30) */ |
| 104 | if(input[pos] != 0x30) |
| 105 | return len; |
| 106 | pos++; |
| 107 | |
| 108 | /* A length of 0xAABBCCDD in DER encoded messages would be sequence of |
| 109 | following octets 0xAA, 0xBB, 0XCC, 0XDD. |
| 110 | |
| 111 | To read length - read each octet and shift left by 1 octect before |
| 112 | reading next octet. |
| 113 | */ |
| 114 | /* check if short or long length form */ |
| 115 | if(input[pos] & 0x80) |
| 116 | { |
| 117 | len_bytes = (input[pos] & ~(0x80)); |
| 118 | pos++; |
| 119 | } |
| 120 | while(len_bytes) |
| 121 | { |
| 122 | /* Shift len by 1 octet */ |
| 123 | len = len << 8; |
| 124 | |
| 125 | /* Read next octet */ |
| 126 | len = len | input[pos]; |
| 127 | pos++; len_bytes--; |
| 128 | } |
| 129 | |
| 130 | /* Add number of octets representing sequence id and length */ |
| 131 | len += pos; |
| 132 | |
| 133 | return len; |
| 134 | } |
| 135 | |
Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 136 | static int add_attribute_to_img(unsigned char *ptr, AUTH_ATTR *input) |
| 137 | { |
| 138 | return i2d_AUTH_ATTR(input, &ptr); |
| 139 | } |
| 140 | |
Channagoud Kadabi | c78a320 | 2015-09-08 14:55:09 -0700 | [diff] [blame] | 141 | bool boot_verify_compare_sha256(unsigned char *image_ptr, |
Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 142 | unsigned int image_size, unsigned char *signature_ptr, RSA *rsa) |
| 143 | { |
| 144 | int ret = -1; |
| 145 | bool auth = false; |
| 146 | unsigned char *plain_text = NULL; |
Channagoud Kadabi | c78a320 | 2015-09-08 14:55:09 -0700 | [diff] [blame] | 147 | |
| 148 | /* The magic numbers here are drawn from the PKCS#1 standard and are the ASN.1 |
| 149 | *encoding of the SHA256 object identifier that is required for a PKCS#1 |
| 150 | * signature.*/ |
| 151 | uint8_t digest[ASN1_ENCODED_SHA256_SIZE] = {0x30, 0x31, 0x30, 0x0d, 0x06, |
| 152 | 0x09, 0x60, 0x86, 0x48, 0x01, |
| 153 | 0x65, 0x03, 0x04, 0x02, 0x01, |
| 154 | 0x05, 0x00, 0x04, 0x20}; |
Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 155 | |
| 156 | plain_text = (unsigned char *)calloc(sizeof(char), SIGNATURE_SIZE); |
| 157 | if (plain_text == NULL) { |
| 158 | dprintf(CRITICAL, "boot_verifier: Calloc failed during verification\n"); |
| 159 | goto cleanup; |
| 160 | } |
| 161 | |
Channagoud Kadabi | c78a320 | 2015-09-08 14:55:09 -0700 | [diff] [blame] | 162 | /* Calculate SHA256 of image and place it into the ASN.1 structure*/ |
Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 163 | image_find_digest(image_ptr, image_size, CRYPTO_AUTH_ALG_SHA256, |
Channagoud Kadabi | c78a320 | 2015-09-08 14:55:09 -0700 | [diff] [blame] | 164 | digest + ASN1_ENCODED_SHA256_OFFSET); |
Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 165 | |
Channagoud Kadabi | c78a320 | 2015-09-08 14:55:09 -0700 | [diff] [blame] | 166 | /* Find digest from the image. This performs the PKCS#1 padding checks up to |
| 167 | * but not including the ASN.1 OID and hash function check. The return value |
| 168 | * is not positive for a failure or the length of the part after the padding */ |
Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 169 | ret = image_decrypt_signature_rsa(signature_ptr, plain_text, rsa); |
| 170 | |
Channagoud Kadabi | c78a320 | 2015-09-08 14:55:09 -0700 | [diff] [blame] | 171 | /* Make sure the length returned from rsa decrypt is same as x509 signature format |
| 172 | * otherwise the signature is invalid and we fail |
| 173 | */ |
| 174 | if (ret != ASN1_ENCODED_SHA256_SIZE) |
| 175 | { |
| 176 | dprintf(CRITICAL, "boot_verifier: Signature decrypt failed! Signature invalid = %d\n", |
Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 177 | ret); |
Channagoud Kadabi | c78a320 | 2015-09-08 14:55:09 -0700 | [diff] [blame] | 178 | goto cleanup; |
| 179 | } |
| 180 | /* So plain_text contains the ASN.1 encoded hash from the signature and |
| 181 | * digest contains the value that this should be for the image that we're |
| 182 | * verifying, so compare them.*/ |
Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 183 | |
Channagoud Kadabi | c78a320 | 2015-09-08 14:55:09 -0700 | [diff] [blame] | 184 | ret = memcmp(plain_text, digest, ASN1_ENCODED_SHA256_SIZE); |
Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 185 | if(ret == 0) |
| 186 | { |
| 187 | auth = true; |
| 188 | #ifdef TZ_SAVE_KERNEL_HASH |
Channagoud Kadabi | c78a320 | 2015-09-08 14:55:09 -0700 | [diff] [blame] | 189 | save_kernel_hash((unsigned char *) digest + ASN1_ENCODED_SHA256_OFFSET, CRYPTO_AUTH_ALG_SHA256); |
Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 190 | #endif |
| 191 | } |
| 192 | |
| 193 | cleanup: |
| 194 | if (plain_text != NULL) |
| 195 | free(plain_text); |
| 196 | EVP_cleanup(); |
| 197 | CRYPTO_cleanup_all_ex_data(); |
| 198 | ERR_remove_thread_state(NULL); |
| 199 | return auth; |
| 200 | |
| 201 | } |
| 202 | |
| 203 | static bool verify_image_with_sig(unsigned char* img_addr, uint32_t img_size, |
| 204 | char *pname, VERIFIED_BOOT_SIG *sig, KEYSTORE *ks) |
| 205 | { |
| 206 | bool ret = false; |
| 207 | uint32_t len; |
| 208 | int shift_bytes; |
| 209 | RSA *rsa = NULL; |
| 210 | bool keystore_verification = false; |
| 211 | EVP_PKEY* key = NULL; |
| 212 | |
| 213 | if(!strcmp(pname, "keystore")) |
| 214 | keystore_verification = true; |
| 215 | |
| 216 | /* Verify target name */ |
| 217 | if(strncmp((char*)(sig->auth_attr->target->data), pname, |
| 218 | sig->auth_attr->target->length) || |
| 219 | (strlen(pname) != (unsigned long) sig->auth_attr->target->length)) |
| 220 | { |
| 221 | dprintf(CRITICAL, |
| 222 | "boot_verifier: verification failure due to target name mismatch\n"); |
| 223 | goto verify_image_with_sig_error; |
| 224 | } |
| 225 | /* Read image size from signature */ |
| 226 | /* A len = 0xAABBCC (represented by 3 octets) would be stored in |
| 227 | len->data as 0X00CCBBAA and len->length as 3(octets). |
| 228 | |
| 229 | To read len we need to left shift data to number of missing octets and |
| 230 | then change it to host long |
| 231 | */ |
| 232 | len = *((uint32_t*)sig->auth_attr->len->data); |
| 233 | shift_bytes = sizeof(uint32_t) - sig->auth_attr->len->length; |
| 234 | if(shift_bytes > 0) { |
| 235 | len = len << (shift_bytes*8); |
| 236 | } |
| 237 | len = ntohl(len); |
| 238 | |
| 239 | /* Verify image size*/ |
| 240 | if(len != img_size) |
| 241 | { |
| 242 | dprintf(CRITICAL, |
| 243 | "boot_verifier: image length is different. (%d vs %d)\n", |
| 244 | len, img_size); |
| 245 | goto verify_image_with_sig_error; |
| 246 | } |
| 247 | |
| 248 | /* append attribute to image */ |
| 249 | if(!keystore_verification) |
| 250 | { |
| 251 | // verifying a non keystore partition |
| 252 | img_size += add_attribute_to_img((unsigned char*)(img_addr + img_size), |
| 253 | sig->auth_attr); |
| 254 | } |
| 255 | |
| 256 | /* compare SHA256SUM of image with value in signature */ |
| 257 | if(ks != NULL) |
| 258 | { |
| 259 | // use rsa from keystore |
| 260 | rsa = ks->mykeybag->mykey->key_material; |
| 261 | } |
| 262 | else |
| 263 | { |
| 264 | dprintf(CRITICAL, "%s:%d: Keystore is null\n", __func__, __LINE__); |
| 265 | ASSERT(0); |
| 266 | } |
| 267 | |
| 268 | // verify boot.img with rsa from oem keystore |
| 269 | if((ret = boot_verify_compare_sha256(img_addr, img_size, |
| 270 | (unsigned char*)sig->sig->data, rsa))) |
| 271 | |
| 272 | { |
| 273 | dprintf(SPEW, "Verified boot.img with oem keystore\n"); |
| 274 | boot_verify_send_event(BOOTIMG_KEYSTORE_VERIFICATION_PASS); |
| 275 | goto verify_image_with_sig_done; |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | dprintf(INFO, "Verification with oem keystore failed. Use embedded certificate for verification\n"); |
| 280 | // get the public key from certificate in boot.img |
| 281 | if ((key = X509_get_pubkey(sig->certificate))) |
| 282 | { |
| 283 | // convert to rsa key format |
| 284 | dprintf(INFO, "RSA KEY found from the embedded certificate\n"); |
| 285 | rsa = EVP_PKEY_get1_RSA(key); |
| 286 | rsa_from_cert = rsa; |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | dprintf(CRITICAL, "Unable to extract public key from certificate\n"); |
| 291 | ASSERT(0); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | // verify boot.img with rsa from embedded certificate |
| 296 | if ((ret = boot_verify_compare_sha256(img_addr, img_size, |
| 297 | (unsigned char*)sig->sig->data, rsa))) |
| 298 | { |
| 299 | dprintf(SPEW, "Verified boot.img with embedded certificate in boot image\n"); |
| 300 | boot_verify_send_event(BOOTIMG_EMBEDDED_CERT_VERIFICATION_PASS); |
| 301 | goto verify_image_with_sig_done; |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | dprintf(INFO, "verified for red state\n"); |
| 306 | boot_verify_send_event(BOOTIMG_VERIFICATION_FAIL); |
| 307 | goto verify_image_with_sig_done; |
| 308 | } |
| 309 | |
| 310 | verify_image_with_sig_error: |
Kishor PK | a72bcc3 | 2016-12-02 17:02:47 +0530 | [diff] [blame] | 311 | boot_verify_send_event(BOOTIMG_VERIFICATION_FAIL); |
Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 312 | verify_image_with_sig_done: |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | static int encode_inner_keystore(unsigned char *ptr, KEYSTORE *ks) |
| 317 | { |
| 318 | int ret = 0; |
| 319 | KEYSTORE_INNER *ks_inner = KEYSTORE_INNER_new(); |
| 320 | if (ks_inner == NULL) |
| 321 | return ret; |
| 322 | ASN1_INTEGER *tmp_version = ks_inner->version; |
| 323 | KEYBAG *tmp_mykeybag = ks_inner->mykeybag; |
| 324 | |
| 325 | ks_inner->version = ks->version; |
| 326 | ks_inner->mykeybag = ks->mykeybag; |
| 327 | ret = i2d_KEYSTORE_INNER(ks_inner, &ptr); |
| 328 | |
| 329 | ks_inner->version = tmp_version; |
| 330 | ks_inner->mykeybag = tmp_mykeybag; |
| 331 | |
| 332 | if(ks_inner != NULL) |
| 333 | KEYSTORE_INNER_free(ks_inner); |
| 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | static bool verify_keystore(unsigned char * ks_addr, KEYSTORE *ks) |
| 338 | { |
| 339 | bool ret = false; |
| 340 | unsigned char * ptr = ks_addr; |
| 341 | uint32_t inner_len = encode_inner_keystore(ptr, ks); |
| 342 | ret = verify_image_with_sig(ks_addr, inner_len, "keystore", ks->sig, |
| 343 | oem_keystore); |
| 344 | return ret; |
| 345 | } |
| 346 | |
| 347 | static void read_oem_keystore() |
| 348 | { |
| 349 | KEYSTORE *ks = NULL; |
| 350 | uint32_t len = 0; |
| 351 | const unsigned char *input = OEM_KEYSTORE; |
| 352 | |
| 353 | if(oem_keystore != NULL) |
| 354 | return; |
| 355 | |
| 356 | len = read_der_message_length((unsigned char *)input); |
| 357 | if(!len) |
| 358 | { |
| 359 | dprintf(CRITICAL, "boot_verifier: oem keystore length is invalid.\n"); |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | ks = d2i_KEYSTORE(NULL, (const unsigned char **) &input, len); |
| 364 | if(ks != NULL) |
| 365 | { |
| 366 | oem_keystore = ks; |
| 367 | user_keystore = ks; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | uint32_t boot_verify_keystore_init() |
| 372 | { |
| 373 | /* Read OEM Keystore */ |
| 374 | read_oem_keystore(); |
| 375 | |
| 376 | return dev_boot_state; |
| 377 | } |
| 378 | |
| 379 | bool send_rot_command(uint32_t is_unlocked) |
| 380 | { |
| 381 | int ret = 0; |
| 382 | unsigned char *input = NULL; |
| 383 | char *rot_input = NULL; |
| 384 | unsigned int digest[9] = {0}, final_digest[8] = {0}; |
| 385 | uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256; |
| 386 | uint32_t boot_device_state = boot_verify_get_state(); |
| 387 | int app_handle = 0; |
| 388 | uint32_t len_oem_rsa = 0, len_from_cert = 0; |
| 389 | km_set_rot_req_t *read_req; |
| 390 | km_set_rot_rsp_t read_rsp; |
| 391 | app_handle = get_secapp_handle(); |
| 392 | int n = 0, e = 0; |
| 393 | switch (boot_device_state) |
| 394 | { |
| 395 | case GREEN: |
| 396 | // Locked device and boot.img verified against OEM keystore. |
| 397 | // n is length of modulus and e is length exponent |
| 398 | n = BN_num_bytes(oem_keystore->mykeybag->mykey->key_material->n); |
| 399 | e = BN_num_bytes(oem_keystore->mykeybag->mykey->key_material->e); |
| 400 | len_oem_rsa = n + e; |
| 401 | if(!(input = malloc(len_oem_rsa))) |
| 402 | { |
| 403 | dprintf(CRITICAL, "Failed to allocate memory for ROT structure\n"); |
| 404 | ASSERT(0); |
| 405 | } |
| 406 | // convert the absolute value on n, e to big endian form |
| 407 | BN_bn2bin(oem_keystore->mykeybag->mykey->key_material->n, input); |
| 408 | BN_bn2bin(oem_keystore->mykeybag->mykey->key_material->e, input+n); |
| 409 | // Hash of key from OEM KEYSTORE |
| 410 | hash_find((unsigned char *)input, len_oem_rsa, (unsigned char *) &digest, auth_algo); |
| 411 | digest[8] = is_unlocked; |
| 412 | break; |
| 413 | case YELLOW: |
| 414 | case RED: |
| 415 | // Locked device and boot.img passed (yellow) or failed (red) verification with the certificate embedded to the boot.img. |
| 416 | if (!rsa_from_cert) |
| 417 | { |
| 418 | dprintf(CRITICAL, "RSA is null from the embedded certificate\n"); |
| 419 | ASSERT(0); |
| 420 | } |
| 421 | // n is length of modulus and e is length exponent |
| 422 | n = BN_num_bytes(rsa_from_cert->n); |
| 423 | e = BN_num_bytes(rsa_from_cert->e); |
| 424 | len_from_cert = n + e; |
| 425 | if(!(input = malloc(len_from_cert))) |
| 426 | { |
| 427 | dprintf(CRITICAL, "Failed to allocate memory for ROT structure\n"); |
| 428 | ASSERT(0); |
| 429 | } |
| 430 | // convert the absolute value on n, e to big endian form |
| 431 | BN_bn2bin(rsa_from_cert->n, input); |
| 432 | BN_bn2bin(rsa_from_cert->e, input+n); |
| 433 | // Hash of key from certificate in boot image |
| 434 | hash_find((unsigned char *)input, len_from_cert, (unsigned char *) &digest, auth_algo); |
| 435 | digest[8] = is_unlocked; |
| 436 | break; |
| 437 | case ORANGE: |
| 438 | // Unlocked device and no verification done. |
| 439 | // Send the hash of boot device state |
| 440 | input = NULL; |
| 441 | digest[0] = is_unlocked; |
| 442 | break; |
| 443 | } |
| 444 | // Hash of hash(key) + device state (locked/unlocked) |
| 445 | hash_find((unsigned char *) digest, sizeof(digest), (unsigned char *)&final_digest, auth_algo); |
| 446 | dprintf(SPEW, "Digest: "); |
| 447 | for(uint8_t i = 0; i < 8; i++) |
| 448 | dprintf(SPEW, "0x%x ", final_digest[i]); |
| 449 | dprintf(SPEW, "\n"); |
| 450 | if(!(read_req = malloc(sizeof(km_set_rot_req_t) + sizeof(final_digest)))) |
| 451 | { |
| 452 | dprintf(CRITICAL, "Failed to allocate memory for ROT structure\n"); |
| 453 | ASSERT(0); |
| 454 | } |
| 455 | |
| 456 | void *cpy_ptr = (uint8_t *) read_req + sizeof(km_set_rot_req_t); |
| 457 | // set ROT stucture |
| 458 | read_req->cmd_id = KEYMASTER_SET_ROT; |
| 459 | read_req->rot_ofset = (uint32_t) sizeof(km_set_rot_req_t); |
| 460 | read_req->rot_size = sizeof(final_digest); |
| 461 | // copy the digest |
| 462 | memcpy(cpy_ptr, (void *) &final_digest, sizeof(final_digest)); |
| 463 | dprintf(SPEW, "Sending Root of Trust to trustzone: start\n"); |
| 464 | |
| 465 | ret = qseecom_send_command(app_handle, (void*) read_req, sizeof(km_set_rot_req_t) + sizeof(final_digest), (void*) &read_rsp, sizeof(read_rsp)); |
| 466 | if (ret < 0 || read_rsp.status < 0) |
| 467 | { |
| 468 | dprintf(CRITICAL, "QSEEcom command for Sending Root of Trust returned error: %d\n", read_rsp.status); |
| 469 | if(input) |
| 470 | free(input); |
| 471 | free(read_req); |
| 472 | free(rot_input); |
| 473 | return false; |
| 474 | } |
| 475 | dprintf(SPEW, "Sending Root of Trust to trustzone: end\n"); |
| 476 | if(input) |
| 477 | free(input); |
| 478 | free(read_req); |
| 479 | free(rot_input); |
| 480 | return true; |
| 481 | } |
| 482 | |
lijuang | f00ffc8 | 2015-07-16 20:06:22 +0800 | [diff] [blame] | 483 | unsigned char* get_boot_fingerprint(unsigned int* buf_size) |
| 484 | { |
| 485 | *buf_size = fp_size; |
| 486 | |
| 487 | return fp; |
| 488 | } |
| 489 | |
Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 490 | bool boot_verify_image(unsigned char* img_addr, uint32_t img_size, char *pname) |
| 491 | { |
| 492 | bool ret = false; |
lijuang | f00ffc8 | 2015-07-16 20:06:22 +0800 | [diff] [blame] | 493 | X509 *cert = NULL; |
| 494 | const EVP_MD *fp_type = NULL; |
Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 495 | VERIFIED_BOOT_SIG *sig = NULL; |
| 496 | unsigned char* sig_addr = (unsigned char*)(img_addr + img_size); |
| 497 | uint32_t sig_len = read_der_message_length(sig_addr); |
| 498 | |
| 499 | if(dev_boot_state == ORANGE) |
| 500 | { |
| 501 | dprintf(INFO, "boot_verifier: Device is in ORANGE boot state.\n"); |
| 502 | dprintf(INFO, "boot_verifier: Skipping boot verification.\n"); |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | if(!sig_len) |
| 507 | { |
| 508 | dprintf(CRITICAL, "boot_verifier: Error while reading signature length.\n"); |
| 509 | ASSERT(0); |
| 510 | } |
| 511 | |
| 512 | if((sig = d2i_VERIFIED_BOOT_SIG(NULL, (const unsigned char **) &sig_addr, sig_len)) == NULL) |
| 513 | { |
| 514 | dprintf(CRITICAL, |
| 515 | "boot_verifier: verification failure due to target name mismatch\n"); |
| 516 | ASSERT(0); |
| 517 | } |
| 518 | |
lijuang | f00ffc8 | 2015-07-16 20:06:22 +0800 | [diff] [blame] | 519 | cert = sig->certificate; |
| 520 | fp_type = EVP_sha1(); |
| 521 | if(!X509_digest(cert, fp_type, (unsigned char *)fp, &fp_size)) { |
| 522 | dprintf(INFO,"Fail to create certificate fingerprint.\n"); |
| 523 | } |
| 524 | |
Sridhar Parasuram | 5620ded | 2015-08-29 10:01:57 -0700 | [diff] [blame] | 525 | ret = verify_image_with_sig(img_addr, img_size, pname, sig, user_keystore); |
| 526 | |
| 527 | if(sig != NULL) |
| 528 | VERIFIED_BOOT_SIG_free(sig); |
| 529 | return ret; |
| 530 | } |
| 531 | |
| 532 | void boot_verify_send_event(uint32_t event) |
| 533 | { |
| 534 | switch(event) |
| 535 | { |
| 536 | case BOOT_INIT: |
| 537 | dev_boot_state = GREEN; |
| 538 | break; |
| 539 | case BOOTIMG_KEYSTORE_VERIFICATION_PASS: |
| 540 | dev_boot_state = GREEN; |
| 541 | break; |
| 542 | case BOOTIMG_EMBEDDED_CERT_VERIFICATION_PASS: |
| 543 | if(dev_boot_state == GREEN) |
| 544 | dev_boot_state = YELLOW; |
| 545 | break; |
| 546 | case BOOTIMG_VERIFICATION_FAIL: |
| 547 | if(dev_boot_state == GREEN || dev_boot_state == YELLOW) |
| 548 | dev_boot_state = RED; |
| 549 | break; |
| 550 | case DEV_UNLOCK: |
| 551 | dev_boot_state = ORANGE; |
| 552 | break; |
| 553 | case USER_DENIES: |
| 554 | if(dev_boot_state == YELLOW || dev_boot_state == ORANGE) |
| 555 | dev_boot_state = RED; |
| 556 | break; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | uint32_t boot_verify_get_state() |
| 561 | { |
| 562 | return dev_boot_state; |
| 563 | } |
| 564 | |
| 565 | void boot_verify_print_state() |
| 566 | { |
| 567 | switch(dev_boot_state) |
| 568 | { |
| 569 | case GREEN: |
| 570 | dprintf(INFO, "boot_verifier: Device is in GREEN boot state.\n"); |
| 571 | break; |
| 572 | case ORANGE: |
| 573 | dprintf(INFO, "boot_verifier: Device is in ORANGE boot state.\n"); |
| 574 | break; |
| 575 | case YELLOW: |
| 576 | dprintf(INFO, "boot_verifier: Device is in YELLOW boot state.\n"); |
| 577 | break; |
| 578 | case RED: |
| 579 | dprintf(INFO, "boot_verifier: Device is in RED boot state.\n"); |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | bool boot_verify_validate_keystore(unsigned char * user_addr) |
| 585 | { |
| 586 | bool ret = false; |
| 587 | unsigned char *input = user_addr; |
| 588 | KEYSTORE *ks = NULL; |
| 589 | uint32_t len = read_der_message_length(input); |
| 590 | if(!len) |
| 591 | { |
| 592 | dprintf(CRITICAL, "boot_verifier: keystore length is invalid.\n"); |
| 593 | return ret; |
| 594 | } |
| 595 | |
| 596 | ks = d2i_KEYSTORE(NULL, (const unsigned char **)&input, len); |
| 597 | if(ks != NULL) |
| 598 | { |
| 599 | ret = true; |
| 600 | } |
| 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | static bool check_list(const char **list, const char* entry) |
| 605 | { |
| 606 | if(list == NULL || entry == NULL) |
| 607 | return false; |
| 608 | |
| 609 | while(*list != NULL) |
| 610 | { |
| 611 | if(!strcmp(entry, *list)) |
| 612 | return true; |
| 613 | |
| 614 | list++; |
| 615 | } |
| 616 | |
| 617 | return false; |
| 618 | } |
Channagoud Kadabi | c78a320 | 2015-09-08 14:55:09 -0700 | [diff] [blame] | 619 | |
| 620 | KEYSTORE *boot_gerity_get_oem_keystore() |
| 621 | { |
| 622 | read_oem_keystore(); |
| 623 | return oem_keystore; |
| 624 | } |