Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 1 | /* |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [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 |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are 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 copyright |
| 10 | * notice, this list of conditions and the following disclaimer in |
| 11 | * the documentation and/or other materials provided with the |
| 12 | * distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 15 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 16 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 17 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 18 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 20 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 21 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 22 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 24 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 25 | * SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include <stdlib.h> |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 29 | #include <stdint.h> |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 30 | #include <crypto_hash.h> |
| 31 | #include <boot_verifier.h> |
| 32 | #include <image_verify.h> |
| 33 | #include <mmc.h> |
| 34 | #include <oem_keystore.h> |
| 35 | #include <openssl/asn1t.h> |
| 36 | #include <openssl/x509.h> |
| 37 | #include <partition_parser.h> |
| 38 | #include <rsa.h> |
| 39 | #include <string.h> |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 40 | #include <openssl/err.h> |
Unnati Gandhi | 8a7cdfe | 2015-05-11 13:04:20 +0530 | [diff] [blame] | 41 | #include <platform.h> |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 42 | |
Channagoud Kadabi | 583ea4c | 2015-09-08 14:55:09 -0700 | [diff] [blame^] | 43 | #define ASN1_ENCODED_SHA256_SIZE 0x33 |
| 44 | #define ASN1_ENCODED_SHA256_OFFSET 0x13 |
| 45 | |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 46 | static KEYSTORE *oem_keystore; |
| 47 | static KEYSTORE *user_keystore; |
| 48 | static uint32_t dev_boot_state = RED; |
| 49 | BUF_DMA_ALIGN(keystore_buf, 4096); |
| 50 | char KEYSTORE_PTN_NAME[] = "keystore"; |
| 51 | |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 52 | static const char *VERIFIED_FLASH_ALLOWED_PTN[] = { |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 53 | "aboot", |
| 54 | "boot", |
| 55 | "recovery", |
| 56 | "system", |
| 57 | NULL }; |
| 58 | |
| 59 | ASN1_SEQUENCE(AUTH_ATTR) ={ |
| 60 | ASN1_SIMPLE(AUTH_ATTR, target, ASN1_PRINTABLESTRING), |
| 61 | ASN1_SIMPLE(AUTH_ATTR, len, ASN1_INTEGER) |
| 62 | } ASN1_SEQUENCE_END(AUTH_ATTR) |
| 63 | IMPLEMENT_ASN1_FUNCTIONS(AUTH_ATTR) |
| 64 | |
| 65 | ASN1_SEQUENCE(VERIFIED_BOOT_SIG) = { |
| 66 | ASN1_SIMPLE(VERIFIED_BOOT_SIG, version, ASN1_INTEGER), |
Unnati Gandhi | 9333499 | 2015-02-25 19:38:38 +0530 | [diff] [blame] | 67 | ASN1_SIMPLE(VERIFIED_BOOT_SIG, certificate, X509), |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 68 | ASN1_SIMPLE(VERIFIED_BOOT_SIG, algor, X509_ALGOR), |
| 69 | ASN1_SIMPLE(VERIFIED_BOOT_SIG, auth_attr, AUTH_ATTR), |
| 70 | ASN1_SIMPLE(VERIFIED_BOOT_SIG, sig, ASN1_OCTET_STRING) |
| 71 | } ASN1_SEQUENCE_END(VERIFIED_BOOT_SIG) |
| 72 | IMPLEMENT_ASN1_FUNCTIONS(VERIFIED_BOOT_SIG) |
| 73 | |
| 74 | ASN1_SEQUENCE(KEY) = { |
| 75 | ASN1_SIMPLE(KEY, algorithm_id, X509_ALGOR), |
| 76 | ASN1_SIMPLE(KEY, key_material, RSAPublicKey) |
| 77 | }ASN1_SEQUENCE_END(KEY) |
| 78 | IMPLEMENT_ASN1_FUNCTIONS(KEY); |
| 79 | |
| 80 | ASN1_SEQUENCE(KEYBAG) = { |
| 81 | ASN1_SIMPLE(KEYBAG, mykey, KEY) |
| 82 | }ASN1_SEQUENCE_END(KEYBAG) |
| 83 | IMPLEMENT_ASN1_FUNCTIONS(KEYBAG) |
| 84 | |
| 85 | ASN1_SEQUENCE(KEYSTORE_INNER) = { |
| 86 | ASN1_SIMPLE(KEYSTORE_INNER, version, ASN1_INTEGER), |
| 87 | ASN1_SIMPLE(KEYSTORE_INNER, mykeybag, KEYBAG) |
| 88 | } ASN1_SEQUENCE_END(KEYSTORE_INNER) |
| 89 | IMPLEMENT_ASN1_FUNCTIONS(KEYSTORE_INNER) |
| 90 | |
| 91 | ASN1_SEQUENCE(KEYSTORE) = { |
| 92 | ASN1_SIMPLE(KEYSTORE, version, ASN1_INTEGER), |
| 93 | ASN1_SIMPLE(KEYSTORE, mykeybag, KEYBAG), |
| 94 | ASN1_SIMPLE(KEYSTORE, sig, VERIFIED_BOOT_SIG) |
| 95 | } ASN1_SEQUENCE_END(KEYSTORE) |
| 96 | IMPLEMENT_ASN1_FUNCTIONS(KEYSTORE) |
| 97 | |
| 98 | static uint32_t read_der_message_length(unsigned char* input) |
| 99 | { |
| 100 | uint32_t len = 0; |
| 101 | int pos = 0; |
| 102 | uint8_t len_bytes = 1; |
| 103 | |
| 104 | /* Check if input starts with Sequence id (0X30) */ |
| 105 | if(input[pos] != 0x30) |
| 106 | return len; |
| 107 | pos++; |
| 108 | |
| 109 | /* A length of 0xAABBCCDD in DER encoded messages would be sequence of |
| 110 | following octets 0xAA, 0xBB, 0XCC, 0XDD. |
| 111 | |
| 112 | To read length - read each octet and shift left by 1 octect before |
| 113 | reading next octet. |
| 114 | */ |
| 115 | /* check if short or long length form */ |
| 116 | if(input[pos] & 0x80) |
| 117 | { |
| 118 | len_bytes = (input[pos] & ~(0x80)); |
| 119 | pos++; |
| 120 | } |
| 121 | while(len_bytes) |
| 122 | { |
| 123 | /* Shift len by 1 octet */ |
| 124 | len = len << 8; |
| 125 | |
| 126 | /* Read next octet */ |
| 127 | len = len | input[pos]; |
| 128 | pos++; len_bytes--; |
| 129 | } |
| 130 | |
| 131 | /* Add number of octets representing sequence id and length */ |
| 132 | len += pos; |
| 133 | |
| 134 | return len; |
| 135 | } |
| 136 | |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 137 | static int add_attribute_to_img(unsigned char *ptr, AUTH_ATTR *input) |
| 138 | { |
| 139 | return i2d_AUTH_ATTR(input, &ptr); |
| 140 | } |
| 141 | |
Channagoud Kadabi | 583ea4c | 2015-09-08 14:55:09 -0700 | [diff] [blame^] | 142 | bool boot_verify_compare_sha256(unsigned char *image_ptr, |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 143 | unsigned int image_size, unsigned char *signature_ptr, RSA *rsa) |
| 144 | { |
| 145 | int ret = -1; |
| 146 | bool auth = false; |
| 147 | unsigned char *plain_text = NULL; |
Channagoud Kadabi | 583ea4c | 2015-09-08 14:55:09 -0700 | [diff] [blame^] | 148 | |
| 149 | /* The magic numbers here are drawn from the PKCS#1 standard and are the ASN.1 |
| 150 | *encoding of the SHA256 object identifier that is required for a PKCS#1 |
| 151 | * signature.*/ |
| 152 | uint8_t digest[ASN1_ENCODED_SHA256_SIZE] = {0x30, 0x31, 0x30, 0x0d, 0x06, |
| 153 | 0x09, 0x60, 0x86, 0x48, 0x01, |
| 154 | 0x65, 0x03, 0x04, 0x02, 0x01, |
| 155 | 0x05, 0x00, 0x04, 0x20}; |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 156 | |
| 157 | plain_text = (unsigned char *)calloc(sizeof(char), SIGNATURE_SIZE); |
| 158 | if (plain_text == NULL) { |
| 159 | dprintf(CRITICAL, "boot_verifier: Calloc failed during verification\n"); |
| 160 | goto cleanup; |
| 161 | } |
| 162 | |
Channagoud Kadabi | 583ea4c | 2015-09-08 14:55:09 -0700 | [diff] [blame^] | 163 | /* Calculate SHA256 of image and place it into the ASN.1 structure*/ |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 164 | image_find_digest(image_ptr, image_size, CRYPTO_AUTH_ALG_SHA256, |
Channagoud Kadabi | 583ea4c | 2015-09-08 14:55:09 -0700 | [diff] [blame^] | 165 | digest + ASN1_ENCODED_SHA256_OFFSET); |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 166 | |
Channagoud Kadabi | 583ea4c | 2015-09-08 14:55:09 -0700 | [diff] [blame^] | 167 | /* Find digest from the image. This performs the PKCS#1 padding checks up to |
| 168 | * but not including the ASN.1 OID and hash function check. The return value |
| 169 | * is not positive for a failure or the length of the part after the padding */ |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 170 | ret = image_decrypt_signature_rsa(signature_ptr, plain_text, rsa); |
| 171 | |
Channagoud Kadabi | 583ea4c | 2015-09-08 14:55:09 -0700 | [diff] [blame^] | 172 | /* Make sure the length returned from rsa decrypt is same as x509 signature format |
| 173 | * otherwise the signature is invalid and we fail |
| 174 | */ |
| 175 | if (ret != ASN1_ENCODED_SHA256_SIZE) |
| 176 | { |
| 177 | dprintf(CRITICAL, "boot_verifier: Signature decrypt failed! Signature invalid = %d\n", |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 178 | ret); |
Channagoud Kadabi | 583ea4c | 2015-09-08 14:55:09 -0700 | [diff] [blame^] | 179 | goto cleanup; |
| 180 | } |
| 181 | /* So plain_text contains the ASN.1 encoded hash from the signature and |
| 182 | * digest contains the value that this should be for the image that we're |
| 183 | * verifying, so compare them.*/ |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 184 | |
Channagoud Kadabi | 583ea4c | 2015-09-08 14:55:09 -0700 | [diff] [blame^] | 185 | ret = memcmp(plain_text, digest, ASN1_ENCODED_SHA256_SIZE); |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 186 | if(ret == 0) |
| 187 | { |
| 188 | auth = true; |
Sridhar Parasuram | b27e47c | 2015-05-27 14:33:54 -0700 | [diff] [blame] | 189 | #ifdef TZ_SAVE_KERNEL_HASH |
Channagoud Kadabi | 583ea4c | 2015-09-08 14:55:09 -0700 | [diff] [blame^] | 190 | save_kernel_hash((unsigned char *) digest + ASN1_ENCODED_SHA256_OFFSET, CRYPTO_AUTH_ALG_SHA256); |
Sridhar Parasuram | b27e47c | 2015-05-27 14:33:54 -0700 | [diff] [blame] | 191 | #endif |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | cleanup: |
| 195 | if (plain_text != NULL) |
| 196 | free(plain_text); |
| 197 | EVP_cleanup(); |
| 198 | CRYPTO_cleanup_all_ex_data(); |
| 199 | ERR_remove_thread_state(NULL); |
| 200 | return auth; |
| 201 | |
| 202 | } |
| 203 | |
| 204 | static bool verify_image_with_sig(unsigned char* img_addr, uint32_t img_size, |
| 205 | char *pname, VERIFIED_BOOT_SIG *sig, KEYSTORE *ks) |
| 206 | { |
| 207 | bool ret = false; |
| 208 | uint32_t len; |
| 209 | int shift_bytes; |
| 210 | RSA *rsa = NULL; |
| 211 | bool keystore_verification = false; |
| 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) || |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 219 | (strlen(pname) != (unsigned long) sig->auth_attr->target->length)) |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 220 | { |
| 221 | dprintf(CRITICAL, |
| 222 | "boot_verifier: verification failure due to target name mismatch\n"); |
| 223 | goto verify_image_with_sig_error; |
| 224 | } |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 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 | img_size += add_attribute_to_img((unsigned char*)(img_addr + img_size), |
| 251 | sig->auth_attr); |
| 252 | |
| 253 | /* compare SHA256SUM of image with value in signature */ |
| 254 | if(ks != NULL) |
| 255 | rsa = ks->mykeybag->mykey->key_material; |
| 256 | |
| 257 | ret = boot_verify_compare_sha256(img_addr, img_size, |
| 258 | (unsigned char*)sig->sig->data, rsa); |
| 259 | |
| 260 | if(!ret) |
| 261 | { |
| 262 | dprintf(CRITICAL, |
| 263 | "boot_verifier: Image verification failed.\n"); |
| 264 | } |
| 265 | |
| 266 | verify_image_with_sig_error: |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | static int encode_inner_keystore(unsigned char *ptr, KEYSTORE *ks) |
| 271 | { |
| 272 | int ret = 0; |
| 273 | KEYSTORE_INNER *ks_inner = KEYSTORE_INNER_new(); |
| 274 | if (ks_inner == NULL) |
| 275 | return ret; |
| 276 | ASN1_INTEGER *tmp_version = ks_inner->version; |
| 277 | KEYBAG *tmp_mykeybag = ks_inner->mykeybag; |
| 278 | |
| 279 | ks_inner->version = ks->version; |
| 280 | ks_inner->mykeybag = ks->mykeybag; |
| 281 | ret = i2d_KEYSTORE_INNER(ks_inner, &ptr); |
| 282 | |
| 283 | ks_inner->version = tmp_version; |
| 284 | ks_inner->mykeybag = tmp_mykeybag; |
| 285 | |
| 286 | if(ks_inner != NULL) |
| 287 | KEYSTORE_INNER_free(ks_inner); |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | static bool verify_keystore(unsigned char * ks_addr, KEYSTORE *ks) |
| 292 | { |
| 293 | bool ret = false; |
| 294 | unsigned char * ptr = ks_addr; |
| 295 | uint32_t inner_len = encode_inner_keystore(ptr, ks); |
| 296 | ret = verify_image_with_sig(ks_addr, inner_len, "keystore", ks->sig, |
| 297 | oem_keystore); |
| 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | static void read_oem_keystore() |
| 302 | { |
Shashank Mittal | e679722 | 2014-09-19 18:58:43 -0700 | [diff] [blame] | 303 | KEYSTORE *ks = NULL; |
| 304 | uint32_t len = 0; |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 305 | const unsigned char *input = OEM_KEYSTORE; |
Shashank Mittal | e679722 | 2014-09-19 18:58:43 -0700 | [diff] [blame] | 306 | |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 307 | if(oem_keystore != NULL) |
| 308 | return; |
| 309 | |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 310 | len = read_der_message_length((unsigned char *)input); |
Shashank Mittal | e679722 | 2014-09-19 18:58:43 -0700 | [diff] [blame] | 311 | if(!len) |
| 312 | { |
| 313 | dprintf(CRITICAL, "boot_verifier: oem keystore length is invalid.\n"); |
| 314 | return; |
| 315 | } |
| 316 | |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 317 | ks = d2i_KEYSTORE(NULL, (const unsigned char **) &input, len); |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 318 | if(ks != NULL) |
| 319 | { |
| 320 | oem_keystore = ks; |
| 321 | user_keystore = ks; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | static int read_user_keystore_ptn() |
| 326 | { |
| 327 | int index = INVALID_PTN; |
| 328 | unsigned long long ptn = 0; |
| 329 | |
| 330 | index = partition_get_index(KEYSTORE_PTN_NAME); |
| 331 | ptn = partition_get_offset(index); |
| 332 | if(ptn == 0) { |
| 333 | dprintf(CRITICAL, "boot_verifier: No keystore partition found\n"); |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | if (mmc_read(ptn, (unsigned int *) keystore_buf, mmc_page_size())) { |
| 338 | dprintf(CRITICAL, "boot_verifier: Cannot read user keystore\n"); |
| 339 | return -1; |
| 340 | } |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static void read_user_keystore(unsigned char *user_addr) |
| 345 | { |
| 346 | unsigned char *input = user_addr; |
Shashank Mittal | e679722 | 2014-09-19 18:58:43 -0700 | [diff] [blame] | 347 | KEYSTORE *ks = NULL; |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 348 | uint32_t len = read_der_message_length(input); |
Shashank Mittal | e679722 | 2014-09-19 18:58:43 -0700 | [diff] [blame] | 349 | if(!len) |
| 350 | { |
| 351 | dprintf(CRITICAL, "boot_verifier: user keystore length is invalid.\n"); |
| 352 | return; |
| 353 | } |
| 354 | |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 355 | ks = d2i_KEYSTORE(NULL, (const unsigned char **)&input, len); |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 356 | if(ks != NULL) |
| 357 | { |
| 358 | if(verify_keystore(user_addr, ks) == false) |
| 359 | { |
| 360 | dprintf(CRITICAL, "boot_verifier: Keystore verification failed!\n"); |
| 361 | boot_verify_send_event(KEYSTORE_VERIFICATION_FAIL); |
| 362 | } |
| 363 | else |
| 364 | dprintf(CRITICAL, "boot_verifier: Keystore verification success!\n"); |
| 365 | user_keystore = ks; |
| 366 | } |
| 367 | else |
| 368 | { |
| 369 | user_keystore = oem_keystore; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | uint32_t boot_verify_keystore_init() |
| 374 | { |
| 375 | /* Read OEM Keystore */ |
| 376 | read_oem_keystore(); |
| 377 | |
| 378 | /* Read User Keystore */ |
| 379 | if(!read_user_keystore_ptn()) |
| 380 | read_user_keystore((unsigned char *)keystore_buf); |
| 381 | return dev_boot_state; |
| 382 | } |
| 383 | |
| 384 | bool boot_verify_image(unsigned char* img_addr, uint32_t img_size, char *pname) |
| 385 | { |
| 386 | bool ret = false; |
| 387 | VERIFIED_BOOT_SIG *sig = NULL; |
| 388 | unsigned char* sig_addr = (unsigned char*)(img_addr + img_size); |
| 389 | uint32_t sig_len = read_der_message_length(sig_addr); |
| 390 | |
| 391 | if(dev_boot_state == ORANGE) |
| 392 | { |
| 393 | dprintf(INFO, "boot_verifier: Device is in ORANGE boot state.\n"); |
| 394 | dprintf(INFO, "boot_verifier: Skipping boot verification.\n"); |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | if(!sig_len) |
| 399 | { |
| 400 | dprintf(CRITICAL, "boot_verifier: Error while reading singature length.\n"); |
| 401 | goto verify_image_error; |
| 402 | } |
| 403 | |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 404 | 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] | 405 | { |
| 406 | dprintf(CRITICAL, |
| 407 | "boot_verifier: verification failure due to target name mismatch\n"); |
| 408 | goto verify_image_error; |
| 409 | } |
| 410 | |
| 411 | ret = verify_image_with_sig(img_addr, img_size, pname, sig, user_keystore); |
| 412 | |
| 413 | verify_image_error: |
| 414 | if(sig != NULL) |
| 415 | VERIFIED_BOOT_SIG_free(sig); |
| 416 | if(!ret) |
| 417 | boot_verify_send_event(BOOT_VERIFICATION_FAIL); |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | void boot_verify_send_event(uint32_t event) |
| 422 | { |
| 423 | switch(event) |
| 424 | { |
| 425 | case BOOT_INIT: |
| 426 | dev_boot_state = GREEN; |
| 427 | break; |
| 428 | case KEYSTORE_VERIFICATION_FAIL: |
| 429 | if(dev_boot_state == GREEN) |
| 430 | dev_boot_state = YELLOW; |
| 431 | break; |
| 432 | case BOOT_VERIFICATION_FAIL: |
| 433 | if(dev_boot_state == GREEN || dev_boot_state == YELLOW) |
| 434 | dev_boot_state = RED; |
| 435 | break; |
| 436 | case DEV_UNLOCK: |
| 437 | dev_boot_state = ORANGE; |
| 438 | break; |
| 439 | case USER_DENIES: |
| 440 | if(dev_boot_state == YELLOW || dev_boot_state == ORANGE) |
| 441 | dev_boot_state = RED; |
| 442 | break; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | uint32_t boot_verify_get_state() |
| 447 | { |
| 448 | return dev_boot_state; |
| 449 | } |
| 450 | |
| 451 | void boot_verify_print_state() |
| 452 | { |
| 453 | switch(dev_boot_state) |
| 454 | { |
| 455 | case GREEN: |
| 456 | dprintf(INFO, "boot_verifier: Device is in GREEN boot state.\n"); |
| 457 | break; |
| 458 | case ORANGE: |
| 459 | dprintf(INFO, "boot_verifier: Device is in ORANGE boot state.\n"); |
| 460 | break; |
| 461 | case YELLOW: |
| 462 | dprintf(INFO, "boot_verifier: Device is in YELLOW boot state.\n"); |
| 463 | break; |
| 464 | case RED: |
Unnati Gandhi | 8a7cdfe | 2015-05-11 13:04:20 +0530 | [diff] [blame] | 465 | 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"); |
| 466 | |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 467 | dprintf(INFO, "boot_verifier: Device is in RED boot state.\n"); |
| 468 | break; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | bool boot_verify_validate_keystore(unsigned char * user_addr) |
| 473 | { |
| 474 | bool ret = false; |
| 475 | unsigned char *input = user_addr; |
Shashank Mittal | e679722 | 2014-09-19 18:58:43 -0700 | [diff] [blame] | 476 | KEYSTORE *ks = NULL; |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 477 | uint32_t len = read_der_message_length(input); |
Shashank Mittal | e679722 | 2014-09-19 18:58:43 -0700 | [diff] [blame] | 478 | if(!len) |
| 479 | { |
| 480 | dprintf(CRITICAL, "boot_verifier: keystore length is invalid.\n"); |
| 481 | return ret; |
| 482 | } |
| 483 | |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 484 | ks = d2i_KEYSTORE(NULL, (const unsigned char **)&input, len); |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 485 | if(ks != NULL) |
| 486 | { |
| 487 | ret = true; |
| 488 | } |
| 489 | return ret; |
| 490 | } |
| 491 | |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 492 | static bool check_list(const char **list, const char* entry) |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 493 | { |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 494 | if(list == NULL || entry == NULL) |
| 495 | return false; |
| 496 | |
| 497 | while(*list != NULL) |
| 498 | { |
| 499 | if(!strcmp(entry, *list)) |
| 500 | return true; |
| 501 | |
| 502 | list++; |
| 503 | } |
| 504 | |
| 505 | return false; |
| 506 | } |
| 507 | |
Channagoud Kadabi | 1420b00 | 2015-01-13 14:48:12 -0800 | [diff] [blame] | 508 | bool boot_verify_flash_allowed(const char * entry) |
Shashank Mittal | 64d0485 | 2014-08-28 15:02:46 -0700 | [diff] [blame] | 509 | { |
| 510 | return check_list(VERIFIED_FLASH_ALLOWED_PTN, entry); |
| 511 | } |
Channagoud Kadabi | 583ea4c | 2015-09-08 14:55:09 -0700 | [diff] [blame^] | 512 | |
| 513 | KEYSTORE *boot_gerity_get_oem_keystore() |
| 514 | { |
| 515 | read_oem_keystore(); |
| 516 | return oem_keystore; |
| 517 | } |