blob: 336efa55c1dee547bbbb6d3d7c748c62ce98fc35 [file] [log] [blame]
Shashank Mittal64d04852014-08-28 15:02:46 -07001/*
Monika Singh51876762019-05-22 18:47:22 +05302 * Copyright (c) 2014-2016, 2019 The Linux Foundation. All rights reserved.
Shashank Mittal64d04852014-08-28 15:02:46 -07003 *
4 * Redistribution and use in source and binary forms, with or without
Sridhar Parasuram8b792422015-07-05 11:38:13 -07005 * modification, are permitted provided that the following conditions are
6 * met:
Shashank Mittal64d04852014-08-28 15:02:46 -07007 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
Sridhar Parasuram8b792422015-07-05 11:38:13 -07009 * * 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 Mittal64d04852014-08-28 15:02:46 -070028 */
29
30#include <stdlib.h>
Channagoud Kadabi1420b002015-01-13 14:48:12 -080031#include <stdint.h>
Shashank Mittal64d04852014-08-28 15:02:46 -070032#include <crypto_hash.h>
33#include <boot_verifier.h>
34#include <image_verify.h>
35#include <mmc.h>
36#include <oem_keystore.h>
Monika Singh36cc5392019-11-11 17:28:15 +053037#if VERIFIED_BOOT_2
Monika Singha956e262019-05-24 16:13:49 +053038#include <avb/OEMPublicKey.h>
Monika Singh36cc5392019-11-11 17:28:15 +053039#endif
Shashank Mittal64d04852014-08-28 15:02:46 -070040#include <openssl/asn1t.h>
41#include <openssl/x509.h>
42#include <partition_parser.h>
43#include <rsa.h>
44#include <string.h>
Channagoud Kadabi1420b002015-01-13 14:48:12 -080045#include <openssl/err.h>
Unnati Gandhi8a7cdfe2015-05-11 13:04:20 +053046#include <platform.h>
Sridhar Parasuram8b792422015-07-05 11:38:13 -070047#include <qseecom_lk_api.h>
48#include <secapp_loader.h>
49#include <target.h>
Channagoud Kadabi86b0c112016-03-16 19:23:16 -070050#include "bootimg.h"
Shashank Mittal64d04852014-08-28 15:02:46 -070051
Channagoud Kadabi583ea4c2015-09-08 14:55:09 -070052#define ASN1_ENCODED_SHA256_SIZE 0x33
53#define ASN1_ENCODED_SHA256_OFFSET 0x13
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -070054#define ASN1_SIGNATURE_BUFFER_SZ mmc_page_size()
Channagoud Kadabi583ea4c2015-09-08 14:55:09 -070055
Shashank Mittal64d04852014-08-28 15:02:46 -070056static KEYSTORE *oem_keystore;
57static KEYSTORE *user_keystore;
58static uint32_t dev_boot_state = RED;
Shashank Mittal64d04852014-08-28 15:02:46 -070059char KEYSTORE_PTN_NAME[] = "keystore";
Sridhar Parasuram8b792422015-07-05 11:38:13 -070060RSA *rsa_from_cert = NULL;
lijuangf214e222015-07-16 20:06:22 +080061unsigned char fp[EVP_MAX_MD_SIZE];
62uint32_t fp_size;
Channagoud Kadabi86b0c112016-03-16 19:23:16 -070063#if OSVERSION_IN_BOOTIMAGE
64km_boot_state_t boot_state_info;
65#endif
Shashank Mittal64d04852014-08-28 15:02:46 -070066
67ASN1_SEQUENCE(AUTH_ATTR) ={
68 ASN1_SIMPLE(AUTH_ATTR, target, ASN1_PRINTABLESTRING),
69 ASN1_SIMPLE(AUTH_ATTR, len, ASN1_INTEGER)
70} ASN1_SEQUENCE_END(AUTH_ATTR)
71IMPLEMENT_ASN1_FUNCTIONS(AUTH_ATTR)
72
73 ASN1_SEQUENCE(VERIFIED_BOOT_SIG) = {
74 ASN1_SIMPLE(VERIFIED_BOOT_SIG, version, ASN1_INTEGER),
Unnati Gandhi93334992015-02-25 19:38:38 +053075 ASN1_SIMPLE(VERIFIED_BOOT_SIG, certificate, X509),
Shashank Mittal64d04852014-08-28 15:02:46 -070076 ASN1_SIMPLE(VERIFIED_BOOT_SIG, algor, X509_ALGOR),
77 ASN1_SIMPLE(VERIFIED_BOOT_SIG, auth_attr, AUTH_ATTR),
78 ASN1_SIMPLE(VERIFIED_BOOT_SIG, sig, ASN1_OCTET_STRING)
79 } ASN1_SEQUENCE_END(VERIFIED_BOOT_SIG)
80IMPLEMENT_ASN1_FUNCTIONS(VERIFIED_BOOT_SIG)
81
82 ASN1_SEQUENCE(KEY) = {
83 ASN1_SIMPLE(KEY, algorithm_id, X509_ALGOR),
84 ASN1_SIMPLE(KEY, key_material, RSAPublicKey)
85 }ASN1_SEQUENCE_END(KEY)
86IMPLEMENT_ASN1_FUNCTIONS(KEY);
87
88ASN1_SEQUENCE(KEYBAG) = {
89 ASN1_SIMPLE(KEYBAG, mykey, KEY)
90}ASN1_SEQUENCE_END(KEYBAG)
91IMPLEMENT_ASN1_FUNCTIONS(KEYBAG)
92
93 ASN1_SEQUENCE(KEYSTORE_INNER) = {
94 ASN1_SIMPLE(KEYSTORE_INNER, version, ASN1_INTEGER),
95 ASN1_SIMPLE(KEYSTORE_INNER, mykeybag, KEYBAG)
96 } ASN1_SEQUENCE_END(KEYSTORE_INNER)
97IMPLEMENT_ASN1_FUNCTIONS(KEYSTORE_INNER)
98
99 ASN1_SEQUENCE(KEYSTORE) = {
100 ASN1_SIMPLE(KEYSTORE, version, ASN1_INTEGER),
101 ASN1_SIMPLE(KEYSTORE, mykeybag, KEYBAG),
102 ASN1_SIMPLE(KEYSTORE, sig, VERIFIED_BOOT_SIG)
103 } ASN1_SEQUENCE_END(KEYSTORE)
104IMPLEMENT_ASN1_FUNCTIONS(KEYSTORE)
105
P.V. Phani Kumarbbe8c8e2016-03-09 19:43:24 +0530106uint32_t read_der_message_length(unsigned char* input, unsigned sz)
Shashank Mittal64d04852014-08-28 15:02:46 -0700107{
108 uint32_t len = 0;
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +0530109 uint32_t pos = 0;
Shashank Mittal64d04852014-08-28 15:02:46 -0700110 uint8_t len_bytes = 1;
111
112 /* Check if input starts with Sequence id (0X30) */
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +0530113 if(sz < 3 || input[pos] != 0x30)
Shashank Mittal64d04852014-08-28 15:02:46 -0700114 return len;
115 pos++;
116
117 /* A length of 0xAABBCCDD in DER encoded messages would be sequence of
118 following octets 0xAA, 0xBB, 0XCC, 0XDD.
119
120 To read length - read each octet and shift left by 1 octect before
121 reading next octet.
122 */
123 /* check if short or long length form */
124 if(input[pos] & 0x80)
125 {
126 len_bytes = (input[pos] & ~(0x80));
127 pos++;
128 }
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -0700129
Shashank Mittal64d04852014-08-28 15:02:46 -0700130 while(len_bytes)
131 {
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -0700132 /* Shift len by 1 octet, make sure to check unsigned int overflow */
133 if (len <= (UINT_MAX >> 8))
134 len <<= 8;
135 else
136 {
137 dprintf(CRITICAL, "Error: Length exceeding max size of uintmax\n");
138 return 0;
139 }
Shashank Mittal64d04852014-08-28 15:02:46 -0700140
141 /* Read next octet */
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +0530142 if (pos < (uint32_t) ASN1_SIGNATURE_BUFFER_SZ && pos < sz)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -0700143 len = len | input[pos];
144 else
145 {
146 dprintf(CRITICAL, "Error: Pos index exceeding the input buffer size\n");
147 return 0;
148 }
149
Shashank Mittal64d04852014-08-28 15:02:46 -0700150 pos++; len_bytes--;
151 }
152
153 /* Add number of octets representing sequence id and length */
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -0700154 if ((UINT_MAX - pos) > len)
155 len += pos;
156 else
157 {
158 dprintf(CRITICAL, "Error: Len overflows UINT_MAX value\n");
159 return 0;
160 }
Shashank Mittal64d04852014-08-28 15:02:46 -0700161
162 return len;
163}
164
Shashank Mittal64d04852014-08-28 15:02:46 -0700165static int add_attribute_to_img(unsigned char *ptr, AUTH_ATTR *input)
166{
167 return i2d_AUTH_ATTR(input, &ptr);
168}
169
Channagoud Kadabi583ea4c2015-09-08 14:55:09 -0700170bool boot_verify_compare_sha256(unsigned char *image_ptr,
Shashank Mittal64d04852014-08-28 15:02:46 -0700171 unsigned int image_size, unsigned char *signature_ptr, RSA *rsa)
172{
173 int ret = -1;
174 bool auth = false;
175 unsigned char *plain_text = NULL;
Channagoud Kadabi583ea4c2015-09-08 14:55:09 -0700176
177 /* The magic numbers here are drawn from the PKCS#1 standard and are the ASN.1
178 *encoding of the SHA256 object identifier that is required for a PKCS#1
179 * signature.*/
180 uint8_t digest[ASN1_ENCODED_SHA256_SIZE] = {0x30, 0x31, 0x30, 0x0d, 0x06,
181 0x09, 0x60, 0x86, 0x48, 0x01,
182 0x65, 0x03, 0x04, 0x02, 0x01,
183 0x05, 0x00, 0x04, 0x20};
Shashank Mittal64d04852014-08-28 15:02:46 -0700184
185 plain_text = (unsigned char *)calloc(sizeof(char), SIGNATURE_SIZE);
186 if (plain_text == NULL) {
187 dprintf(CRITICAL, "boot_verifier: Calloc failed during verification\n");
188 goto cleanup;
189 }
190
Channagoud Kadabi583ea4c2015-09-08 14:55:09 -0700191 /* Calculate SHA256 of image and place it into the ASN.1 structure*/
Shashank Mittal64d04852014-08-28 15:02:46 -0700192 image_find_digest(image_ptr, image_size, CRYPTO_AUTH_ALG_SHA256,
Channagoud Kadabi583ea4c2015-09-08 14:55:09 -0700193 digest + ASN1_ENCODED_SHA256_OFFSET);
Shashank Mittal64d04852014-08-28 15:02:46 -0700194
Channagoud Kadabi583ea4c2015-09-08 14:55:09 -0700195 /* Find digest from the image. This performs the PKCS#1 padding checks up to
196 * but not including the ASN.1 OID and hash function check. The return value
197 * is not positive for a failure or the length of the part after the padding */
Shashank Mittal64d04852014-08-28 15:02:46 -0700198 ret = image_decrypt_signature_rsa(signature_ptr, plain_text, rsa);
199
Channagoud Kadabi583ea4c2015-09-08 14:55:09 -0700200 /* Make sure the length returned from rsa decrypt is same as x509 signature format
201 * otherwise the signature is invalid and we fail
202 */
203 if (ret != ASN1_ENCODED_SHA256_SIZE)
204 {
205 dprintf(CRITICAL, "boot_verifier: Signature decrypt failed! Signature invalid = %d\n",
Shashank Mittal64d04852014-08-28 15:02:46 -0700206 ret);
Channagoud Kadabi583ea4c2015-09-08 14:55:09 -0700207 goto cleanup;
208 }
209 /* So plain_text contains the ASN.1 encoded hash from the signature and
210 * digest contains the value that this should be for the image that we're
211 * verifying, so compare them.*/
Shashank Mittal64d04852014-08-28 15:02:46 -0700212
Channagoud Kadabi583ea4c2015-09-08 14:55:09 -0700213 ret = memcmp(plain_text, digest, ASN1_ENCODED_SHA256_SIZE);
Shashank Mittal64d04852014-08-28 15:02:46 -0700214 if(ret == 0)
215 {
216 auth = true;
Sridhar Parasuramb27e47c2015-05-27 14:33:54 -0700217#ifdef TZ_SAVE_KERNEL_HASH
Channagoud Kadabi583ea4c2015-09-08 14:55:09 -0700218 save_kernel_hash((unsigned char *) digest + ASN1_ENCODED_SHA256_OFFSET, CRYPTO_AUTH_ALG_SHA256);
Sridhar Parasuramb27e47c2015-05-27 14:33:54 -0700219#endif
Shashank Mittal64d04852014-08-28 15:02:46 -0700220 }
221
222cleanup:
223 if (plain_text != NULL)
224 free(plain_text);
225 EVP_cleanup();
226 CRYPTO_cleanup_all_ex_data();
227 ERR_remove_thread_state(NULL);
228 return auth;
229
230}
231
232static bool verify_image_with_sig(unsigned char* img_addr, uint32_t img_size,
233 char *pname, VERIFIED_BOOT_SIG *sig, KEYSTORE *ks)
234{
235 bool ret = false;
236 uint32_t len;
237 int shift_bytes;
238 RSA *rsa = NULL;
239 bool keystore_verification = false;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700240 EVP_PKEY* key = NULL;
vijay kumar9fe95642015-12-14 14:14:20 +0530241 int attr = 0;
Shashank Mittal64d04852014-08-28 15:02:46 -0700242
243 if(!strcmp(pname, "keystore"))
244 keystore_verification = true;
245
246 /* Verify target name */
247 if(strncmp((char*)(sig->auth_attr->target->data), pname,
248 sig->auth_attr->target->length) ||
Channagoud Kadabi1420b002015-01-13 14:48:12 -0800249 (strlen(pname) != (unsigned long) sig->auth_attr->target->length))
Shashank Mittal64d04852014-08-28 15:02:46 -0700250 {
251 dprintf(CRITICAL,
252 "boot_verifier: verification failure due to target name mismatch\n");
253 goto verify_image_with_sig_error;
254 }
Shashank Mittal64d04852014-08-28 15:02:46 -0700255 /* Read image size from signature */
256 /* A len = 0xAABBCC (represented by 3 octets) would be stored in
257 len->data as 0X00CCBBAA and len->length as 3(octets).
258
259 To read len we need to left shift data to number of missing octets and
260 then change it to host long
261 */
262 len = *((uint32_t*)sig->auth_attr->len->data);
263 shift_bytes = sizeof(uint32_t) - sig->auth_attr->len->length;
264 if(shift_bytes > 0) {
265 len = len << (shift_bytes*8);
266 }
267 len = ntohl(len);
268
269 /* Verify image size*/
270 if(len != img_size)
271 {
272 dprintf(CRITICAL,
273 "boot_verifier: image length is different. (%d vs %d)\n",
274 len, img_size);
275 goto verify_image_with_sig_error;
276 }
277
278 /* append attribute to image */
279 if(!keystore_verification)
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700280 {
281 // verifying a non keystore partition
vijay kumar9fe95642015-12-14 14:14:20 +0530282 attr = add_attribute_to_img((unsigned char*)(img_addr + img_size),
Shashank Mittal64d04852014-08-28 15:02:46 -0700283 sig->auth_attr);
vijay kumar9fe95642015-12-14 14:14:20 +0530284 if (img_size > (UINT_MAX - attr))
285 {
286 dprintf(CRITICAL,"Interger overflow detected\n");
287 ASSERT(0);
288 }
289 else img_size += attr;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700290 }
Shashank Mittal64d04852014-08-28 15:02:46 -0700291
292 /* compare SHA256SUM of image with value in signature */
293 if(ks != NULL)
Shashank Mittal64d04852014-08-28 15:02:46 -0700294 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700295 // use rsa from keystore
296 rsa = ks->mykeybag->mykey->key_material;
297 }
298 else
299 {
300 dprintf(CRITICAL, "%s:%d: Keystore is null\n", __func__, __LINE__);
301 ASSERT(0);
302 }
303
304 // verify boot.img with rsa from oem keystore
305 if((ret = boot_verify_compare_sha256(img_addr, img_size,
306 (unsigned char*)sig->sig->data, rsa)))
307
308 {
309 dprintf(SPEW, "Verified boot.img with oem keystore\n");
310 boot_verify_send_event(BOOTIMG_KEYSTORE_VERIFICATION_PASS);
311 goto verify_image_with_sig_done;
312 }
313 else
314 {
315 dprintf(INFO, "Verification with oem keystore failed. Use embedded certificate for verification\n");
316 // get the public key from certificate in boot.img
317 if ((key = X509_get_pubkey(sig->certificate)))
318 {
319 // convert to rsa key format
320 dprintf(INFO, "RSA KEY found from the embedded certificate\n");
321 rsa = EVP_PKEY_get1_RSA(key);
322 rsa_from_cert = rsa;
323 }
324 else
325 {
326 dprintf(CRITICAL, "Unable to extract public key from certificate\n");
327 ASSERT(0);
328 }
329 }
330
331 // verify boot.img with rsa from embedded certificate
332 if ((ret = boot_verify_compare_sha256(img_addr, img_size,
333 (unsigned char*)sig->sig->data, rsa)))
334 {
335 dprintf(SPEW, "Verified boot.img with embedded certificate in boot image\n");
336 boot_verify_send_event(BOOTIMG_EMBEDDED_CERT_VERIFICATION_PASS);
337 goto verify_image_with_sig_done;
338 }
339 else
340 {
341 dprintf(INFO, "verified for red state\n");
342 boot_verify_send_event(BOOTIMG_VERIFICATION_FAIL);
343 goto verify_image_with_sig_done;
Shashank Mittal64d04852014-08-28 15:02:46 -0700344 }
345
346verify_image_with_sig_error:
Kishor PK5dac4312016-12-02 17:02:47 +0530347 boot_verify_send_event(BOOTIMG_VERIFICATION_FAIL);
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700348verify_image_with_sig_done:
Shashank Mittal64d04852014-08-28 15:02:46 -0700349 return ret;
350}
351
352static int encode_inner_keystore(unsigned char *ptr, KEYSTORE *ks)
353{
354 int ret = 0;
355 KEYSTORE_INNER *ks_inner = KEYSTORE_INNER_new();
356 if (ks_inner == NULL)
357 return ret;
358 ASN1_INTEGER *tmp_version = ks_inner->version;
359 KEYBAG *tmp_mykeybag = ks_inner->mykeybag;
360
361 ks_inner->version = ks->version;
362 ks_inner->mykeybag = ks->mykeybag;
363 ret = i2d_KEYSTORE_INNER(ks_inner, &ptr);
364
365 ks_inner->version = tmp_version;
366 ks_inner->mykeybag = tmp_mykeybag;
367
368 if(ks_inner != NULL)
369 KEYSTORE_INNER_free(ks_inner);
370 return ret;
371}
372
373static bool verify_keystore(unsigned char * ks_addr, KEYSTORE *ks)
374{
375 bool ret = false;
376 unsigned char * ptr = ks_addr;
377 uint32_t inner_len = encode_inner_keystore(ptr, ks);
378 ret = verify_image_with_sig(ks_addr, inner_len, "keystore", ks->sig,
379 oem_keystore);
380 return ret;
381}
382
383static void read_oem_keystore()
384{
Shashank Mittale6797222014-09-19 18:58:43 -0700385 KEYSTORE *ks = NULL;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -0700386 uint32_t len = sizeof(OEM_KEYSTORE);
Channagoud Kadabi1420b002015-01-13 14:48:12 -0800387 const unsigned char *input = OEM_KEYSTORE;
Shashank Mittale6797222014-09-19 18:58:43 -0700388
Shashank Mittal64d04852014-08-28 15:02:46 -0700389 if(oem_keystore != NULL)
390 return;
391
Channagoud Kadabi1420b002015-01-13 14:48:12 -0800392 ks = d2i_KEYSTORE(NULL, (const unsigned char **) &input, len);
Shashank Mittal64d04852014-08-28 15:02:46 -0700393 if(ks != NULL)
394 {
395 oem_keystore = ks;
396 user_keystore = ks;
397 }
398}
399
Shashank Mittal64d04852014-08-28 15:02:46 -0700400uint32_t boot_verify_keystore_init()
401{
402 /* Read OEM Keystore */
403 read_oem_keystore();
404
Shashank Mittal64d04852014-08-28 15:02:46 -0700405 return dev_boot_state;
406}
407
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700408#if OSVERSION_IN_BOOTIMAGE
409static void boot_verify_send_boot_state(km_boot_state_t *boot_state)
410{
411 km_get_version_req_t version_req;
412 km_get_version_rsp_t version_rsp;
413 int ret;
414 int app_handle = get_secapp_handle();
415 km_set_boot_state_req_t *bs_req = NULL;
416 km_set_boot_state_rsp_t boot_state_rsp;
417 uint8_t *boot_state_ptr;
418
419 version_req.cmd_id = KEYMASTER_GET_VERSION;
420 ret = qseecom_send_command(app_handle, (void*) &version_req, sizeof(version_req), (void*) &version_rsp, sizeof(version_rsp));
421 if (ret < 0 || version_rsp.status < 0)
422 {
423 dprintf(CRITICAL, "QSEEcom command for getting keymaster version returned error: %d\n", version_rsp.status);
424 ASSERT(0);
425 }
426
Mohamed Sunfeer9da93e52018-09-06 15:56:41 +0530427 if (version_rsp.major_version >= 0x2)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700428 {
429 bs_req = malloc(sizeof(km_set_boot_state_req_t) + sizeof(km_boot_state_t));
430 ASSERT(bs_req);
431
432 boot_state_ptr = (uint8_t *) bs_req + sizeof(km_set_boot_state_req_t);
433 /* copy the boot state data */
434 memscpy(boot_state_ptr, sizeof(km_boot_state_t), &boot_state_info, sizeof(boot_state_info));
435
436 bs_req->cmd_id = KEYMASTER_SET_BOOT_STATE;
437 bs_req->version = 0x0;
438 bs_req->boot_state_offset = sizeof(km_set_boot_state_req_t);
439 bs_req->boot_state_size = sizeof(km_boot_state_t);
440
441 ret = qseecom_send_command(app_handle, (void *)bs_req, sizeof(*bs_req) + sizeof(km_boot_state_t), (void *) &boot_state_rsp, sizeof(boot_state_rsp));
442 if (ret < 0 || boot_state_rsp.status < 0)
443 {
444 dprintf(CRITICAL, "QSEEcom command for Sending boot state returned error: %d\n", boot_state_rsp.status);
445 free(bs_req);
446 ASSERT(0);
447 }
448 }
449
450 if (bs_req)
451 free(bs_req);
452}
453#endif
454
Monika Singh4d659d22019-12-05 16:38:04 +0530455#if VERIFIED_BOOT_2 && !VB1_KEY_USED
Monika Singha956e262019-05-24 16:13:49 +0530456bool send_rot_command(uint32_t is_unlocked)
457{
458 int ret = 0;
459 unsigned char *input = (unsigned char *)OEMPublicKey;
460 unsigned int key_len = sizeof(OEMPublicKey);
461 unsigned char *keystatebuf = NULL;
462 unsigned char digest[SHA256_SIZE] = {0}, final_digest[SHA256_SIZE] = {0};
463 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
464 uint32_t boot_device_state = boot_verify_get_state();
465 int app_handle = 0;
466 km_set_rot_req_t *read_req = NULL;
467 km_set_rot_rsp_t read_rsp;
468 app_handle = get_secapp_handle();
469 uint32_t version = 0;
470 void *cpy_ptr;
471
472 if( input == NULL || UINT_MAX - 1 < key_len )
473 {
474 dprintf(CRITICAL, "Failed to read ROT key\n");
475 ASSERT(0);
476 }
477 switch (boot_device_state)
478 {
479 case GREEN:
480 case YELLOW:
481 if(!( keystatebuf = malloc( key_len + 1)))
482 {
483 dprintf(CRITICAL, "Failed to allocate memory for ROT digest\n");
484 ASSERT(0);
485 }
486 memscpy(keystatebuf , key_len + 1, input, key_len);
487 hash_find((unsigned char *)keystatebuf, key_len , (unsigned char *) digest, auth_algo);
488 keystatebuf[key_len] = (unsigned char )is_unlocked;
489 hash_find((unsigned char *)keystatebuf, key_len + 1, (unsigned char *) final_digest, auth_algo);
490 break;
491 case ORANGE:
492 // Unlocked device and no verification done.
493 // Send the hash of boot device state
494 input = NULL;
495 hash_find((unsigned char *) &is_unlocked, sizeof(unsigned char), (unsigned char *)&final_digest, auth_algo);
496 break;
497 case RED:
498 default:
499 dprintf(CRITICAL, "Invalid state to boot!\n");
500 }
501 dprintf(SPEW, "Digest: ");
502 for(uint8_t i = 0; i < SHA256_SIZE; i++)
503 dprintf(SPEW, "0x%x ", final_digest[i]);
504 dprintf(SPEW, "\n");
505
506 if(!(read_req = malloc(sizeof(km_set_rot_req_t) + sizeof(final_digest))))
507 {
508 dprintf(CRITICAL, "Failed to allocate memory for ROT structure\n");
509 ASSERT(0);
510 }
511
512 cpy_ptr = (uint8_t *) read_req + sizeof(km_set_rot_req_t);
513 read_req->cmd_id = KEYMASTER_SET_ROT;
514 read_req->rot_ofset = (uint32_t) sizeof(km_set_rot_req_t);
515 read_req->rot_size = sizeof(final_digest);
516 memscpy(cpy_ptr, sizeof(final_digest), (void *) &final_digest, sizeof(final_digest));
517 dprintf(SPEW, "Sending Root of Trust to trustzone: start\n");
518
519 ret = qseecom_send_command(app_handle, (void*) read_req, sizeof(km_set_rot_req_t) + sizeof(final_digest), (void*) &read_rsp, sizeof(read_rsp));
520 if (ret < 0 || read_rsp.status < 0)
521 {
522 dprintf(CRITICAL, "QSEEcom command for Sending Root of Trust returned error: %d\n", read_rsp.status);
523 free(read_req);
524 return false;
525 }
526
527#if OSVERSION_IN_BOOTIMAGE
528 boot_state_info.is_unlocked = is_unlocked;
529 boot_state_info.color = boot_verify_get_state();
530 memscpy(boot_state_info.public_key, sizeof(boot_state_info.public_key), digest, SHA256_SIZE);
531 boot_verify_send_boot_state(&boot_state_info);
532#endif
533 if ( is_secure_boot_enable()
534 && (dev_boot_state != GREEN))
535 {
536 version = qseecom_get_version();
537 if(allow_set_fuse(version)) {
538 ret = set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
539 if (ret) {
540 ret = false;
541 goto err;
542 }
543 ret = set_tamper_fuse_cmd(HLOS_TAMPER_NOTIFY_FUSE);
544 if (ret) {
545 dprintf(CRITICAL, "send_rot_command: set_tamper_fuse_cmd (TZ_HLOS_TAMPER_NOTIFY_FUSE) fails!\n");
546 ret = false;
547 goto err;
548 }
549 } else {
550 dprintf(CRITICAL, "send_rot_command: TZ didn't support this feature! Version: major = %d, minor = %d, patch = %d\n", (version >> 22) & 0x3FF, (version >> 12) & 0x3FF, version & 0x3FF);
551 goto err;
552 }
553 }
554 dprintf(CRITICAL, "Sending Root of Trust to trustzone: end\n");
555 ret = true;
556err:
557 if(keystatebuf)
558 free(keystatebuf);
559 free(read_req);
560 return ret;
561}
562#else
Sridhar Parasuram96300dc2015-06-11 10:37:11 -0700563bool send_rot_command(uint32_t is_unlocked)
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700564{
565 int ret = 0;
Sridhar Parasuram24fb3952015-08-26 20:25:59 -0700566 unsigned char *input = NULL;
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700567 char *rot_input = NULL;
Sridhar Parasuram96300dc2015-06-11 10:37:11 -0700568 unsigned int digest[9] = {0}, final_digest[8] = {0};
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700569 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
570 uint32_t boot_device_state = boot_verify_get_state();
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700571 int app_handle = 0;
Sridhar Parasuram24fb3952015-08-26 20:25:59 -0700572 uint32_t len_oem_rsa = 0, len_from_cert = 0;
Monika Singh5a2f7de2018-03-16 16:57:44 +0530573 km_set_rot_req_t *read_req = NULL;
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700574 km_set_rot_rsp_t read_rsp;
575 app_handle = get_secapp_handle();
Monika Singh98257462018-06-06 11:28:49 +0530576 uint32_t version = 0;
577
Sridhar Parasuram24fb3952015-08-26 20:25:59 -0700578 int n = 0, e = 0;
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700579 switch (boot_device_state)
580 {
581 case GREEN:
582 // Locked device and boot.img verified against OEM keystore.
Sridhar Parasuram24fb3952015-08-26 20:25:59 -0700583 // Send hash of key from OEM KEYSTORE + Boot device state
584 n = BN_num_bytes(oem_keystore->mykeybag->mykey->key_material->n);
585 e = BN_num_bytes(oem_keystore->mykeybag->mykey->key_material->e);
vijay kumara89b9252015-12-10 16:10:03 +0530586 /*this assumes a valid acceptable range for RSA, including 4096 bits of modulo n. */
587 if (n<0 || n>1024)
588 {
589 dprintf(CRITICAL, "Invalid n value from key_material\n");
590 ASSERT(0);
591 }
592 /* e can assumes 3,5,17,257,65537 as valid values, which should be 1 byte long only, we accept 2 bytes or 16 bits long */
593 if( e < 0 || e >16)
594 {
595 dprintf(CRITICAL, "Invalid e value from key_material\n");
596 ASSERT(0);
597 }
Sridhar Parasuram24fb3952015-08-26 20:25:59 -0700598 len_oem_rsa = n + e;
599 if(!(input = malloc(len_oem_rsa)))
600 {
601 dprintf(CRITICAL, "Failed to allocate memory for ROT structure\n");
602 ASSERT(0);
603 }
604 BN_bn2bin(oem_keystore->mykeybag->mykey->key_material->n, input);
605 BN_bn2bin(oem_keystore->mykeybag->mykey->key_material->e, input+n);
606 hash_find((unsigned char *)input, len_oem_rsa, (unsigned char *) &digest, auth_algo);
Sridhar Parasuram96300dc2015-06-11 10:37:11 -0700607 digest[8] = is_unlocked;
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700608 break;
609 case YELLOW:
610 case RED:
611 // Locked device and boot.img passed (yellow) or failed (red) verification with the certificate embedded to the boot.img.
Sridhar Parasuram24fb3952015-08-26 20:25:59 -0700612 if (!rsa_from_cert)
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700613 {
614 dprintf(CRITICAL, "RSA is null from the embedded certificate\n");
615 ASSERT(0);
616 }
Sridhar Parasuram24fb3952015-08-26 20:25:59 -0700617 // Send hash of key from certificate in boot image + boot device state
618 n = BN_num_bytes(rsa_from_cert->n);
619 e = BN_num_bytes(rsa_from_cert->e);
vijay kumara89b9252015-12-10 16:10:03 +0530620 /*this assumes a valid acceptable range for RSA, including 4096 bits of modulo n. */
621 if (n<0 || n>1024)
622 {
623 dprintf(CRITICAL, "Invalid n value from rsa_from_cert\n");
624 ASSERT(0);
625 }
626 /* e can assumes 3,5,17,257,65537 as valid values, which should be 1 byte long only, we accept 2 bytes or 16 bits long */
627 if( e < 0 || e >16)
628 {
629 dprintf(CRITICAL, "Invalid e value from rsa_from_cert\n");
630 ASSERT(0);
631 }
Sridhar Parasuram24fb3952015-08-26 20:25:59 -0700632 len_from_cert = n + e;
633 if(!(input = malloc(len_from_cert)))
634 {
635 dprintf(CRITICAL, "Failed to allocate memory for ROT structure\n");
636 ASSERT(0);
637 }
638 BN_bn2bin(rsa_from_cert->n, input);
639 BN_bn2bin(rsa_from_cert->e, input+n);
640 hash_find((unsigned char *)input, len_from_cert, (unsigned char *) &digest, auth_algo);
Sridhar Parasuram96300dc2015-06-11 10:37:11 -0700641 digest[8] = is_unlocked;
Sridhar Parasuram24fb3952015-08-26 20:25:59 -0700642 break;
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700643 case ORANGE:
644 // Unlocked device and no verification done.
645 // Send the hash of boot device state
646 input = NULL;
Sridhar Parasuram96300dc2015-06-11 10:37:11 -0700647 digest[0] = is_unlocked;
Sridhar Parasuram24fb3952015-08-26 20:25:59 -0700648 break;
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700649 }
650
Sridhar Parasuram96300dc2015-06-11 10:37:11 -0700651 hash_find((unsigned char *) digest, sizeof(digest), (unsigned char *)&final_digest, auth_algo);
Sridhar Parasuram24fb3952015-08-26 20:25:59 -0700652 dprintf(SPEW, "Digest: ");
653 for(uint8_t i = 0; i < 8; i++)
654 dprintf(SPEW, "0x%x ", final_digest[i]);
655 dprintf(SPEW, "\n");
Sridhar Parasuram96300dc2015-06-11 10:37:11 -0700656 if(!(read_req = malloc(sizeof(km_set_rot_req_t) + sizeof(final_digest))))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700657 {
658 dprintf(CRITICAL, "Failed to allocate memory for ROT structure\n");
659 ASSERT(0);
660 }
661
662 void *cpy_ptr = (uint8_t *) read_req + sizeof(km_set_rot_req_t);
663 // set ROT stucture
664 read_req->cmd_id = KEYMASTER_SET_ROT;
665 read_req->rot_ofset = (uint32_t) sizeof(km_set_rot_req_t);
Sridhar Parasuram96300dc2015-06-11 10:37:11 -0700666 read_req->rot_size = sizeof(final_digest);
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700667 // copy the digest
Sridhar Parasuram96300dc2015-06-11 10:37:11 -0700668 memcpy(cpy_ptr, (void *) &final_digest, sizeof(final_digest));
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700669 dprintf(SPEW, "Sending Root of Trust to trustzone: start\n");
670
Sridhar Parasuram96300dc2015-06-11 10:37:11 -0700671 ret = qseecom_send_command(app_handle, (void*) read_req, sizeof(km_set_rot_req_t) + sizeof(final_digest), (void*) &read_rsp, sizeof(read_rsp));
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700672 if (ret < 0 || read_rsp.status < 0)
673 {
674 dprintf(CRITICAL, "QSEEcom command for Sending Root of Trust returned error: %d\n", read_rsp.status);
Sridhar Parasuram24fb3952015-08-26 20:25:59 -0700675 if(input)
676 free(input);
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700677 free(read_req);
678 free(rot_input);
679 return false;
680 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700681
682#if OSVERSION_IN_BOOTIMAGE
683 boot_state_info.is_unlocked = is_unlocked;
684 boot_state_info.color = boot_verify_get_state();
685 memscpy(boot_state_info.public_key, sizeof(boot_state_info.public_key), digest, 32);
686 boot_verify_send_boot_state(&boot_state_info);
687#endif
Monika Singh98257462018-06-06 11:28:49 +0530688 if ( is_secure_boot_enable()
689 && (dev_boot_state != GREEN))
690 {
691 version = qseecom_get_version();
692 if(allow_set_fuse(version)) {
693 ret = set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
694 if (ret) {
695 ret = false;
696 goto err;
697 }
698 ret = set_tamper_fuse_cmd(HLOS_TAMPER_NOTIFY_FUSE);
699 if (ret) {
Monika Singh475a3952018-06-27 14:29:43 +0530700 dprintf(CRITICAL, "send_rot_command: set_tamper_fuse_cmd (TZ_HLOS_TAMPER_NOTIFY_FUSE) fails!\n");
Monika Singh98257462018-06-06 11:28:49 +0530701 ret = false;
702 goto err;
703 }
704 } else {
Monika Singh475a3952018-06-27 14:29:43 +0530705 dprintf(CRITICAL, "send_rot_command: TZ didn't support this feature! Version: major = %d, minor = %d, patch = %d\n", (version >> 22) & 0x3FF, (version >> 12) & 0x3FF, version & 0x3FF);
Monika Singh98257462018-06-06 11:28:49 +0530706 ret = false;
707 goto err;
708 }
709 }
710 dprintf(CRITICAL, "Sending Root of Trust to trustzone: end\n");
711 ret = true;
712err:
Sridhar Parasuram24fb3952015-08-26 20:25:59 -0700713 if(input)
714 free(input);
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700715 free(read_req);
716 free(rot_input);
Monika Singh98257462018-06-06 11:28:49 +0530717 return ret;
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700718}
Monika Singha956e262019-05-24 16:13:49 +0530719#endif
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -0700720
lijuangf214e222015-07-16 20:06:22 +0800721unsigned char* get_boot_fingerprint(unsigned int* buf_size)
722{
723 *buf_size = fp_size;
724
725 return fp;
726}
727
Monika Singh5a2f7de2018-03-16 16:57:44 +0530728bool boot_verify_image(unsigned char* img_addr, uint32_t img_size, char *pname,
729 uint32_t *bootstate)
Shashank Mittal64d04852014-08-28 15:02:46 -0700730{
731 bool ret = false;
lijuangf214e222015-07-16 20:06:22 +0800732 X509 *cert = NULL;
733 const EVP_MD *fp_type = NULL;
Shashank Mittal64d04852014-08-28 15:02:46 -0700734 VERIFIED_BOOT_SIG *sig = NULL;
735 unsigned char* sig_addr = (unsigned char*)(img_addr + img_size);
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -0700736 uint32_t sig_len = 0;
737 unsigned char *signature = NULL;
Shashank Mittal64d04852014-08-28 15:02:46 -0700738
739 if(dev_boot_state == ORANGE)
740 {
741 dprintf(INFO, "boot_verifier: Device is in ORANGE boot state.\n");
742 dprintf(INFO, "boot_verifier: Skipping boot verification.\n");
743 return false;
744 }
745
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -0700746 signature = malloc(ASN1_SIGNATURE_BUFFER_SZ);
747 ASSERT(signature);
748
749 /* Copy the signature from scratch memory to buffer */
750 memcpy(signature, sig_addr, ASN1_SIGNATURE_BUFFER_SZ);
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +0530751 sig_len = read_der_message_length(signature, ASN1_SIGNATURE_BUFFER_SZ);
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -0700752
Shashank Mittal64d04852014-08-28 15:02:46 -0700753 if(!sig_len)
754 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700755 dprintf(CRITICAL, "boot_verifier: Error while reading signature length.\n");
Sridhar Parasuramf391ec92015-08-27 14:01:42 -0700756 ASSERT(0);
Shashank Mittal64d04852014-08-28 15:02:46 -0700757 }
758
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -0700759 if (sig_len > ASN1_SIGNATURE_BUFFER_SZ)
760 {
761 dprintf(CRITICAL, "boot_verifier: Signature length exceeds size signature buffer\n");
762 goto verify_image_error;
763 }
764
Channagoud Kadabicda7e9b2015-09-17 13:25:35 -0700765 if (sig_len > ASN1_SIGNATURE_BUFFER_SZ)
766 {
767 dprintf(CRITICAL, "boot_verifier: Signature length exceeds size signature buffer\n");
768 goto verify_image_error;
769 }
770
Channagoud Kadabi1420b002015-01-13 14:48:12 -0800771 if((sig = d2i_VERIFIED_BOOT_SIG(NULL, (const unsigned char **) &sig_addr, sig_len)) == NULL)
Shashank Mittal64d04852014-08-28 15:02:46 -0700772 {
773 dprintf(CRITICAL,
774 "boot_verifier: verification failure due to target name mismatch\n");
Sridhar Parasuramf391ec92015-08-27 14:01:42 -0700775 ASSERT(0);
Shashank Mittal64d04852014-08-28 15:02:46 -0700776 }
777
lijuangf214e222015-07-16 20:06:22 +0800778 cert = sig->certificate;
779 fp_type = EVP_sha1();
780 if(!X509_digest(cert, fp_type, (unsigned char *)fp, &fp_size)) {
781 dprintf(INFO,"Fail to create certificate fingerprint.\n");
782 }
783
Shashank Mittal64d04852014-08-28 15:02:46 -0700784 ret = verify_image_with_sig(img_addr, img_size, pname, sig, user_keystore);
785
Shashank Mittal64d04852014-08-28 15:02:46 -0700786 if(sig != NULL)
787 VERIFIED_BOOT_SIG_free(sig);
Monika Singh0f7bfc82018-04-16 23:14:29 +0530788
Monika Singh5a2f7de2018-03-16 16:57:44 +0530789 if(bootstate == NULL)
790 goto verify_image_error;
Monika Singh0f7bfc82018-04-16 23:14:29 +0530791 *bootstate = dev_boot_state;
Monika Singh5a2f7de2018-03-16 16:57:44 +0530792
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -0700793verify_image_error:
794 free(signature);
Shashank Mittal64d04852014-08-28 15:02:46 -0700795 return ret;
796}
797
798void boot_verify_send_event(uint32_t event)
799{
800 switch(event)
801 {
802 case BOOT_INIT:
803 dev_boot_state = GREEN;
804 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700805 case BOOTIMG_KEYSTORE_VERIFICATION_PASS:
806 dev_boot_state = GREEN;
807 break;
808 case BOOTIMG_EMBEDDED_CERT_VERIFICATION_PASS:
Shashank Mittal64d04852014-08-28 15:02:46 -0700809 if(dev_boot_state == GREEN)
810 dev_boot_state = YELLOW;
811 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700812 case BOOTIMG_VERIFICATION_FAIL:
Shashank Mittal64d04852014-08-28 15:02:46 -0700813 if(dev_boot_state == GREEN || dev_boot_state == YELLOW)
814 dev_boot_state = RED;
815 break;
816 case DEV_UNLOCK:
817 dev_boot_state = ORANGE;
818 break;
819 case USER_DENIES:
820 if(dev_boot_state == YELLOW || dev_boot_state == ORANGE)
821 dev_boot_state = RED;
822 break;
823 }
824}
825
826uint32_t boot_verify_get_state()
827{
828 return dev_boot_state;
829}
830
831void boot_verify_print_state()
832{
833 switch(dev_boot_state)
834 {
835 case GREEN:
836 dprintf(INFO, "boot_verifier: Device is in GREEN boot state.\n");
837 break;
838 case ORANGE:
839 dprintf(INFO, "boot_verifier: Device is in ORANGE boot state.\n");
840 break;
841 case YELLOW:
842 dprintf(INFO, "boot_verifier: Device is in YELLOW boot state.\n");
843 break;
844 case RED:
Unnati Gandhi8a7cdfe2015-05-11 13:04:20 +0530845 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");
846
Shashank Mittal64d04852014-08-28 15:02:46 -0700847 dprintf(INFO, "boot_verifier: Device is in RED boot state.\n");
848 break;
849 }
850}
851
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +0530852bool boot_verify_validate_keystore(unsigned char * user_addr, unsigned sz)
Shashank Mittal64d04852014-08-28 15:02:46 -0700853{
854 bool ret = false;
855 unsigned char *input = user_addr;
Shashank Mittale6797222014-09-19 18:58:43 -0700856 KEYSTORE *ks = NULL;
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +0530857 uint32_t len = read_der_message_length(input, sz);
mohamed sunfeer4fc5b962017-08-23 00:29:45 +0530858 if((!len) || (sz < len))
Shashank Mittale6797222014-09-19 18:58:43 -0700859 {
860 dprintf(CRITICAL, "boot_verifier: keystore length is invalid.\n");
861 return ret;
862 }
863
Channagoud Kadabi1420b002015-01-13 14:48:12 -0800864 ks = d2i_KEYSTORE(NULL, (const unsigned char **)&input, len);
Shashank Mittal64d04852014-08-28 15:02:46 -0700865 if(ks != NULL)
866 {
867 ret = true;
868 }
869 return ret;
870}
871
Channagoud Kadabi1420b002015-01-13 14:48:12 -0800872static bool check_list(const char **list, const char* entry)
Shashank Mittal64d04852014-08-28 15:02:46 -0700873{
Shashank Mittal64d04852014-08-28 15:02:46 -0700874 if(list == NULL || entry == NULL)
875 return false;
876
877 while(*list != NULL)
878 {
879 if(!strcmp(entry, *list))
880 return true;
881
882 list++;
883 }
884
885 return false;
886}
887
Channagoud Kadabi583ea4c2015-09-08 14:55:09 -0700888KEYSTORE *boot_gerity_get_oem_keystore()
889{
890 read_oem_keystore();
891 return oem_keystore;
892}
Brahmaji K708fc502017-08-07 21:08:04 +0530893
Monika Singh73f10b02020-02-28 13:43:07 +0530894#if OSVERSION_IN_BOOTIMAGE
Monika Singh51876762019-05-22 18:47:22 +0530895void set_os_version_with_date(unsigned char* img_addr, uint32_t system_security_level)
896{
897 boot_img_hdr *img_hdr = NULL;
898 bool supported;
899 int ret = get_date_support (&supported);
900
901 /* Extract the os version and patch level */
902 if (img_addr) {
903 img_hdr = (boot_img_hdr *)img_addr;
904 boot_state_info.system_version = (img_hdr->os_version & 0xFFFFF800) >> 11;
905 if(!ret && supported && system_security_level)
906 boot_state_info.system_security_level = system_security_level;
907 else
908 boot_state_info.system_security_level = (img_hdr->os_version & 0x7FF);
909 } else {
910 dprintf(CRITICAL, "Image address should not be NULL\n");
911 }
912}
913
Brahmaji K708fc502017-08-07 21:08:04 +0530914void set_os_version(unsigned char* img_addr)
915{
Mayank Grover3dcc95b2018-09-04 20:31:38 +0530916 boot_img_hdr *img_hdr = NULL;
Brahmaji K708fc502017-08-07 21:08:04 +0530917
918 /* Extract the os version and patch level */
919 if (img_addr) {
Mayank Grover3dcc95b2018-09-04 20:31:38 +0530920 img_hdr = (boot_img_hdr *)img_addr;
Brahmaji K708fc502017-08-07 21:08:04 +0530921 boot_state_info.system_version = (img_hdr->os_version & 0xFFFFF800) >> 11;
922 boot_state_info.system_security_level = (img_hdr->os_version & 0x7FF);
923 } else {
924 dprintf(CRITICAL, "Image address should not be NULL\n");
925 }
926}
927#else
928void set_os_version(unsigned char* img_addr)
929{
930 return;
931}
Monika Singh73f10b02020-02-28 13:43:07 +0530932
933void set_os_version_with_date(unsigned char* img_addr, uint32_t system_security_level)
934{
935 return;
936}
Brahmaji K708fc502017-08-07 21:08:04 +0530937#endif
Monika Singh0b15c022019-04-10 15:24:20 +0530938
939int set_verified_boot_hash (const char *vbh, size_t vbh_size)
940{
941 int ret = 0;
942 km_set_vbh_req_t vbh_req = {0};
943 km_set_vbh_rsp_t vbh_rsp = {0};
944 int app_handle = get_secapp_handle();
945
946 if (!vbh || vbh_size != sizeof (vbh_req.vbh)) {
947 dprintf(CRITICAL, "Vbh input params invalid\n");
948 ASSERT(0);
949 }
950 vbh_req.cmd_id = KEYMASTER_SET_VBH;
951 memscpy (vbh_req.vbh, sizeof(vbh_req.vbh), vbh, vbh_size);
952 ret = qseecom_send_command (app_handle, (void *)&vbh_req, sizeof (vbh_req), (void *)&vbh_rsp, sizeof (vbh_rsp));
953
954 if (ret != 0 || vbh_rsp.status != 0) {
955 dprintf(CRITICAL, "QSEEcom command for setting vbh returned error: %d\n",vbh_rsp.status);
956 if (ret == 0 && vbh_rsp.status == KM_ERROR_INVALID_TAG) {
957 dprintf(INFO, "VBH not supported in keymaster, continue boot\n");
958 return ret;
959 }
960 ASSERT(0);
961 }
962 return ret;
963}
Monika Singh51876762019-05-22 18:47:22 +0530964
965int get_date_support (bool *supported)
966{
967 int status = 0;
968 km_get_date_support_req date_support_req = {0};
969 km_get_date_support_rsp date_support_rsp = {0};
970 int app_handle = get_secapp_handle();
971
972 date_support_req.cmd_id = KEYMASTER_GET_DATE_SUPPORT;
973 status = qseecom_send_command (app_handle, (void *)&date_support_req, sizeof (date_support_req), (void *)&date_support_rsp, sizeof (date_support_rsp));
974 if (status != 0 || date_support_rsp.status != 0 ) {
975 dprintf(CRITICAL, "QSEEcom command to get date support returned error, status: %d\n",date_support_rsp.status);
976
977 if (status == 0 && date_support_rsp.status == KM_ERROR_INVALID_TAG) {
978 dprintf(INFO, "Date in patch level NOT supported in keymaster\n");
979 }
980 *supported = false;
981 return status;
982 }
983
984 *supported = true;
985 return status;
986}