137a7bc39SJason Zhu /* 237a7bc39SJason Zhu * Copyright (C) 2016 The Android Open Source Project 337a7bc39SJason Zhu * 437a7bc39SJason Zhu * Permission is hereby granted, free of charge, to any person 537a7bc39SJason Zhu * obtaining a copy of this software and associated documentation 637a7bc39SJason Zhu * files (the "Software"), to deal in the Software without 737a7bc39SJason Zhu * restriction, including without limitation the rights to use, copy, 837a7bc39SJason Zhu * modify, merge, publish, distribute, sublicense, and/or sell copies 937a7bc39SJason Zhu * of the Software, and to permit persons to whom the Software is 1037a7bc39SJason Zhu * furnished to do so, subject to the following conditions: 1137a7bc39SJason Zhu * 1237a7bc39SJason Zhu * The above copyright notice and this permission notice shall be 1337a7bc39SJason Zhu * included in all copies or substantial portions of the Software. 1437a7bc39SJason Zhu * 1537a7bc39SJason Zhu * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 1637a7bc39SJason Zhu * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 1737a7bc39SJason Zhu * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 1837a7bc39SJason Zhu * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 1937a7bc39SJason Zhu * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 2037a7bc39SJason Zhu * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 2137a7bc39SJason Zhu * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 2237a7bc39SJason Zhu * SOFTWARE. 2337a7bc39SJason Zhu */ 2437a7bc39SJason Zhu 2537a7bc39SJason Zhu #include <android_avb/avb_atx_validate.h> 2637a7bc39SJason Zhu 2737a7bc39SJason Zhu #include <android_avb/avb_rsa.h> 2837a7bc39SJason Zhu #include <android_avb/avb_sha.h> 2937a7bc39SJason Zhu #include <android_avb/avb_sysdeps.h> 3037a7bc39SJason Zhu #include <android_avb/avb_util.h> 3165f0143bSJason Zhu #include <android_avb/avb_ops_user.h> 3265f0143bSJason Zhu #include <android_avb/rk_avb_ops_user.h> 3365f0143bSJason Zhu #include <malloc.h> 3465f0143bSJason Zhu #include <common.h> 3565f0143bSJason Zhu #ifdef CONFIG_CRYPTO_ROCKCHIP 3665f0143bSJason Zhu #include <rockchip_crypto/rockchip_crypto.h> 3765f0143bSJason Zhu #endif 3837a7bc39SJason Zhu 39ab608f80SJason Zhu /* The most recent unlock challenge generated. */ 40ab608f80SJason Zhu static uint8_t last_unlock_challenge[AVB_ATX_UNLOCK_CHALLENGE_SIZE]; 41ab608f80SJason Zhu 4237a7bc39SJason Zhu /* Computes the SHA256 |hash| of |length| bytes of |data|. */ 4337a7bc39SJason Zhu static void sha256(const uint8_t* data, 4437a7bc39SJason Zhu uint32_t length, 4537a7bc39SJason Zhu uint8_t hash[AVB_SHA256_DIGEST_SIZE]) { 4637a7bc39SJason Zhu AvbSHA256Ctx context; 4737a7bc39SJason Zhu avb_sha256_init(&context); 4837a7bc39SJason Zhu avb_sha256_update(&context, data, length); 4937a7bc39SJason Zhu uint8_t* tmp = avb_sha256_final(&context); 5037a7bc39SJason Zhu avb_memcpy(hash, tmp, AVB_SHA256_DIGEST_SIZE); 5137a7bc39SJason Zhu } 5237a7bc39SJason Zhu 5337a7bc39SJason Zhu /* Computes the SHA512 |hash| of |length| bytes of |data|. */ 5437a7bc39SJason Zhu static void sha512(const uint8_t* data, 5537a7bc39SJason Zhu uint32_t length, 5637a7bc39SJason Zhu uint8_t hash[AVB_SHA512_DIGEST_SIZE]) { 5737a7bc39SJason Zhu AvbSHA512Ctx context; 5837a7bc39SJason Zhu avb_sha512_init(&context); 5937a7bc39SJason Zhu avb_sha512_update(&context, data, length); 6037a7bc39SJason Zhu uint8_t* tmp = avb_sha512_final(&context); 6137a7bc39SJason Zhu avb_memcpy(hash, tmp, AVB_SHA512_DIGEST_SIZE); 6237a7bc39SJason Zhu } 6337a7bc39SJason Zhu 6437a7bc39SJason Zhu /* Computes the SHA256 |hash| of a NUL-terminated |str|. */ 6537a7bc39SJason Zhu static void sha256_str(const char* str, uint8_t hash[AVB_SHA256_DIGEST_SIZE]) { 6637a7bc39SJason Zhu sha256((const uint8_t*)str, avb_strlen(str), hash); 6737a7bc39SJason Zhu } 6837a7bc39SJason Zhu 6937a7bc39SJason Zhu /* Verifies structure and |expected_hash| of permanent |attributes|. */ 7037a7bc39SJason Zhu static bool verify_permanent_attributes( 7137a7bc39SJason Zhu const AvbAtxPermanentAttributes* attributes, 72ab608f80SJason Zhu const uint8_t expected_hash[AVB_SHA256_DIGEST_SIZE]) { 7337a7bc39SJason Zhu uint8_t hash[AVB_SHA256_DIGEST_SIZE]; 74*74b485fbSJason Zhu #ifdef CONFIG_ROCKCHIP_PRELOADER_PUB_KEY 7565f0143bSJason Zhu #ifdef CONFIG_CRYPTO_ROCKCHIP 7665f0143bSJason Zhu struct rk_pub_key pub_key; 7765f0143bSJason Zhu int i; 7865f0143bSJason Zhu uint8_t rsa_hash[256] = {0}; 7965f0143bSJason Zhu uint8_t rsa_hash_revert[256] = {0}; 8065f0143bSJason Zhu unsigned int rsaResult_temp[8]; 8165f0143bSJason Zhu unsigned char rsaResult[32] = {0}; 8265f0143bSJason Zhu char *temp; 8365f0143bSJason Zhu struct rk_crypto_desc crypto_desc; 8465f0143bSJason Zhu int ret = 0; 8537a7bc39SJason Zhu 8665f0143bSJason Zhu if (rk_crypto_probe()) 8765f0143bSJason Zhu return false; 8865f0143bSJason Zhu 8965f0143bSJason Zhu memset(&pub_key, 0, sizeof(struct rk_pub_key)); 9065f0143bSJason Zhu ret = rk_avb_get_pub_key(&pub_key); 9165f0143bSJason Zhu if (ret) 9265f0143bSJason Zhu return false; 9365f0143bSJason Zhu 9465f0143bSJason Zhu ret = rk_avb_get_perm_attr_cer(rsa_hash, 256); 9565f0143bSJason Zhu if (ret) { 9665f0143bSJason Zhu avb_error("get_perm_attr_cer error\n"); 9765f0143bSJason Zhu return false; 9865f0143bSJason Zhu } 9965f0143bSJason Zhu 10065f0143bSJason Zhu for (i = 0; i < 256; i++) 10165f0143bSJason Zhu rsa_hash_revert[255-i] = rsa_hash[i]; 10265f0143bSJason Zhu 10365f0143bSJason Zhu ret = get_rk_crypto_desc(&crypto_desc); 10465f0143bSJason Zhu if (ret) { 10565f0143bSJason Zhu avb_error("get_rk_crypto_desc error\n"); 10665f0143bSJason Zhu return false; 10765f0143bSJason Zhu } 10865f0143bSJason Zhu 10965f0143bSJason Zhu ret = rk_crypto_rsa_init(&crypto_desc); 11065f0143bSJason Zhu if (ret) { 11165f0143bSJason Zhu avb_error("rk_crypto_rsa_init error\n"); 11265f0143bSJason Zhu return false; 11365f0143bSJason Zhu } 11465f0143bSJason Zhu 11565f0143bSJason Zhu ret = rk_crypto_rsa_start(&crypto_desc, (u32 *)(rsa_hash_revert), 11665f0143bSJason Zhu pub_key.rsa_n, pub_key.rsa_e, pub_key.rsa_c); 11765f0143bSJason Zhu if (ret) { 11865f0143bSJason Zhu avb_error("rk_crypto_rsa_start error\n"); 11965f0143bSJason Zhu return false; 12065f0143bSJason Zhu } 12165f0143bSJason Zhu 12265f0143bSJason Zhu ret = rk_crypto_rsa_end(&crypto_desc, rsaResult_temp); 12365f0143bSJason Zhu if (ret) { 12465f0143bSJason Zhu avb_error("rk_crypto_rsa_end error\n"); 12565f0143bSJason Zhu return false; 12665f0143bSJason Zhu } 12765f0143bSJason Zhu 12865f0143bSJason Zhu temp = (char *)rsaResult_temp; 12965f0143bSJason Zhu for (i = 0; i < 32; i++) 13065f0143bSJason Zhu rsaResult[31-i] = temp[i]; 13165f0143bSJason Zhu 13265f0143bSJason Zhu sha256((const uint8_t*)attributes, sizeof(AvbAtxPermanentAttributes), hash); 13365f0143bSJason Zhu if (memcmp((void*)rsaResult, (void*)hash, 32) == 0) 13465f0143bSJason Zhu return true; 13565f0143bSJason Zhu 13665f0143bSJason Zhu return false; 13765f0143bSJason Zhu #endif 13865f0143bSJason Zhu #else 13937a7bc39SJason Zhu if (attributes->version != 1) { 14037a7bc39SJason Zhu avb_error("Unsupported permanent attributes version.\n"); 14137a7bc39SJason Zhu return false; 14237a7bc39SJason Zhu } 14337a7bc39SJason Zhu sha256((const uint8_t*)attributes, sizeof(AvbAtxPermanentAttributes), hash); 14437a7bc39SJason Zhu if (0 != avb_safe_memcmp(hash, expected_hash, AVB_SHA256_DIGEST_SIZE)) { 14537a7bc39SJason Zhu avb_error("Invalid permanent attributes.\n"); 14637a7bc39SJason Zhu return false; 14737a7bc39SJason Zhu } 14837a7bc39SJason Zhu return true; 14965f0143bSJason Zhu #endif 15037a7bc39SJason Zhu } 15137a7bc39SJason Zhu 15237a7bc39SJason Zhu /* Verifies the format, key version, usage, and signature of a certificate. */ 153ab608f80SJason Zhu static bool verify_certificate( 154ab608f80SJason Zhu const AvbAtxCertificate* certificate, 155ab608f80SJason Zhu const uint8_t authority[AVB_ATX_PUBLIC_KEY_SIZE], 15637a7bc39SJason Zhu uint64_t minimum_key_version, 157ab608f80SJason Zhu const uint8_t expected_usage[AVB_SHA256_DIGEST_SIZE]) { 15837a7bc39SJason Zhu const AvbAlgorithmData* algorithm_data; 15937a7bc39SJason Zhu uint8_t certificate_hash[AVB_SHA512_DIGEST_SIZE]; 16037a7bc39SJason Zhu 16137a7bc39SJason Zhu if (certificate->signed_data.version != 1) { 16237a7bc39SJason Zhu avb_error("Unsupported certificate format.\n"); 16337a7bc39SJason Zhu return false; 16437a7bc39SJason Zhu } 16537a7bc39SJason Zhu algorithm_data = avb_get_algorithm_data(AVB_ALGORITHM_TYPE_SHA512_RSA4096); 16637a7bc39SJason Zhu sha512((const uint8_t*)&certificate->signed_data, 16737a7bc39SJason Zhu sizeof(AvbAtxCertificateSignedData), 16837a7bc39SJason Zhu certificate_hash); 16937a7bc39SJason Zhu if (!avb_rsa_verify(authority, 17037a7bc39SJason Zhu AVB_ATX_PUBLIC_KEY_SIZE, 17137a7bc39SJason Zhu certificate->signature, 17237a7bc39SJason Zhu AVB_RSA4096_NUM_BYTES, 17337a7bc39SJason Zhu certificate_hash, 17437a7bc39SJason Zhu AVB_SHA512_DIGEST_SIZE, 17537a7bc39SJason Zhu algorithm_data->padding, 17637a7bc39SJason Zhu algorithm_data->padding_len)) { 17737a7bc39SJason Zhu avb_error("Invalid certificate signature.\n"); 17837a7bc39SJason Zhu return false; 17937a7bc39SJason Zhu } 18037a7bc39SJason Zhu if (certificate->signed_data.key_version < minimum_key_version) { 18137a7bc39SJason Zhu avb_error("Key rollback detected.\n"); 18237a7bc39SJason Zhu return false; 18337a7bc39SJason Zhu } 18437a7bc39SJason Zhu if (0 != avb_safe_memcmp(certificate->signed_data.usage, 18537a7bc39SJason Zhu expected_usage, 18637a7bc39SJason Zhu AVB_SHA256_DIGEST_SIZE)) { 18737a7bc39SJason Zhu avb_error("Invalid certificate usage.\n"); 18837a7bc39SJason Zhu return false; 18937a7bc39SJason Zhu } 19037a7bc39SJason Zhu return true; 19137a7bc39SJason Zhu } 19237a7bc39SJason Zhu 19337a7bc39SJason Zhu /* Verifies signature and fields of a PIK certificate. */ 194ab608f80SJason Zhu static bool verify_pik_certificate( 195ab608f80SJason Zhu const AvbAtxCertificate* certificate, 196ab608f80SJason Zhu const uint8_t authority[AVB_ATX_PUBLIC_KEY_SIZE], 19737a7bc39SJason Zhu uint64_t minimum_version) { 19837a7bc39SJason Zhu uint8_t expected_usage[AVB_SHA256_DIGEST_SIZE]; 19937a7bc39SJason Zhu 20037a7bc39SJason Zhu sha256_str("com.google.android.things.vboot.ca", expected_usage); 20137a7bc39SJason Zhu if (!verify_certificate( 20237a7bc39SJason Zhu certificate, authority, minimum_version, expected_usage)) { 20337a7bc39SJason Zhu avb_error("Invalid PIK certificate.\n"); 20437a7bc39SJason Zhu return false; 20537a7bc39SJason Zhu } 20637a7bc39SJason Zhu return true; 20737a7bc39SJason Zhu } 20837a7bc39SJason Zhu 20937a7bc39SJason Zhu /* Verifies signature and fields of a PSK certificate. */ 21037a7bc39SJason Zhu static bool verify_psk_certificate( 211ab608f80SJason Zhu const AvbAtxCertificate* certificate, 212ab608f80SJason Zhu const uint8_t authority[AVB_ATX_PUBLIC_KEY_SIZE], 21337a7bc39SJason Zhu uint64_t minimum_version, 214ab608f80SJason Zhu const uint8_t product_id[AVB_ATX_PRODUCT_ID_SIZE]) { 21537a7bc39SJason Zhu uint8_t expected_subject[AVB_SHA256_DIGEST_SIZE]; 21637a7bc39SJason Zhu uint8_t expected_usage[AVB_SHA256_DIGEST_SIZE]; 21737a7bc39SJason Zhu 21837a7bc39SJason Zhu sha256_str("com.google.android.things.vboot", expected_usage); 21937a7bc39SJason Zhu if (!verify_certificate( 22037a7bc39SJason Zhu certificate, authority, minimum_version, expected_usage)) { 22137a7bc39SJason Zhu avb_error("Invalid PSK certificate.\n"); 22237a7bc39SJason Zhu return false; 22337a7bc39SJason Zhu } 22437a7bc39SJason Zhu sha256(product_id, AVB_ATX_PRODUCT_ID_SIZE, expected_subject); 22537a7bc39SJason Zhu if (0 != avb_safe_memcmp(certificate->signed_data.subject, 22637a7bc39SJason Zhu expected_subject, 22737a7bc39SJason Zhu AVB_SHA256_DIGEST_SIZE)) { 228ab608f80SJason Zhu avb_error("PSK: Product ID mismatch.\n"); 229ab608f80SJason Zhu return false; 230ab608f80SJason Zhu } 231ab608f80SJason Zhu return true; 232ab608f80SJason Zhu } 233ab608f80SJason Zhu 234ab608f80SJason Zhu /* Verifies signature and fields of a PUK certificate. */ 235ab608f80SJason Zhu static bool verify_puk_certificate( 236ab608f80SJason Zhu const AvbAtxCertificate* certificate, 237ab608f80SJason Zhu const uint8_t authority[AVB_ATX_PUBLIC_KEY_SIZE], 238ab608f80SJason Zhu uint64_t minimum_version, 239ab608f80SJason Zhu const uint8_t product_id[AVB_ATX_PRODUCT_ID_SIZE]) { 240ab608f80SJason Zhu uint8_t expected_subject[AVB_SHA256_DIGEST_SIZE]; 241ab608f80SJason Zhu uint8_t expected_usage[AVB_SHA256_DIGEST_SIZE]; 242ab608f80SJason Zhu 243ab608f80SJason Zhu sha256_str("com.google.android.things.vboot.unlock", expected_usage); 244ab608f80SJason Zhu if (!verify_certificate( 245ab608f80SJason Zhu certificate, authority, minimum_version, expected_usage)) { 246ab608f80SJason Zhu avb_error("Invalid PUK certificate.\n"); 247ab608f80SJason Zhu return false; 248ab608f80SJason Zhu } 249ab608f80SJason Zhu sha256(product_id, AVB_ATX_PRODUCT_ID_SIZE, expected_subject); 250ab608f80SJason Zhu if (0 != avb_safe_memcmp(certificate->signed_data.subject, 251ab608f80SJason Zhu expected_subject, 252ab608f80SJason Zhu AVB_SHA256_DIGEST_SIZE)) { 253ab608f80SJason Zhu avb_error("PUK: Product ID mismatch.\n"); 25437a7bc39SJason Zhu return false; 25537a7bc39SJason Zhu } 25637a7bc39SJason Zhu return true; 25737a7bc39SJason Zhu } 25837a7bc39SJason Zhu 25937a7bc39SJason Zhu AvbIOResult avb_atx_validate_vbmeta_public_key( 26037a7bc39SJason Zhu AvbOps* ops, 26137a7bc39SJason Zhu const uint8_t* public_key_data, 26237a7bc39SJason Zhu size_t public_key_length, 26337a7bc39SJason Zhu const uint8_t* public_key_metadata, 26437a7bc39SJason Zhu size_t public_key_metadata_length, 26537a7bc39SJason Zhu bool* out_is_trusted) { 26637a7bc39SJason Zhu AvbIOResult result = AVB_IO_RESULT_OK; 26737a7bc39SJason Zhu AvbAtxPermanentAttributes permanent_attributes; 26837a7bc39SJason Zhu uint8_t permanent_attributes_hash[AVB_SHA256_DIGEST_SIZE]; 26937a7bc39SJason Zhu AvbAtxPublicKeyMetadata metadata; 27037a7bc39SJason Zhu uint64_t minimum_version; 27137a7bc39SJason Zhu 27237a7bc39SJason Zhu /* Be pessimistic so we can exit early without having to remember to clear. 27337a7bc39SJason Zhu */ 27437a7bc39SJason Zhu *out_is_trusted = false; 27537a7bc39SJason Zhu 27637a7bc39SJason Zhu /* Read and verify permanent attributes. */ 27737a7bc39SJason Zhu result = ops->atx_ops->read_permanent_attributes(ops->atx_ops, 27837a7bc39SJason Zhu &permanent_attributes); 27937a7bc39SJason Zhu if (result != AVB_IO_RESULT_OK) { 28037a7bc39SJason Zhu avb_error("Failed to read permanent attributes.\n"); 28137a7bc39SJason Zhu return result; 28237a7bc39SJason Zhu } 28337a7bc39SJason Zhu result = ops->atx_ops->read_permanent_attributes_hash( 28437a7bc39SJason Zhu ops->atx_ops, permanent_attributes_hash); 28537a7bc39SJason Zhu if (result != AVB_IO_RESULT_OK) { 28637a7bc39SJason Zhu avb_error("Failed to read permanent attributes hash.\n"); 28737a7bc39SJason Zhu return result; 28837a7bc39SJason Zhu } 28937a7bc39SJason Zhu if (!verify_permanent_attributes(&permanent_attributes, 29037a7bc39SJason Zhu permanent_attributes_hash)) { 29137a7bc39SJason Zhu return AVB_IO_RESULT_OK; 29237a7bc39SJason Zhu } 29337a7bc39SJason Zhu 29437a7bc39SJason Zhu /* Sanity check public key metadata. */ 29537a7bc39SJason Zhu if (public_key_metadata_length != sizeof(AvbAtxPublicKeyMetadata)) { 29637a7bc39SJason Zhu avb_error("Invalid public key metadata.\n"); 29737a7bc39SJason Zhu return AVB_IO_RESULT_OK; 29837a7bc39SJason Zhu } 29937a7bc39SJason Zhu avb_memcpy(&metadata, public_key_metadata, sizeof(AvbAtxPublicKeyMetadata)); 30037a7bc39SJason Zhu if (metadata.version != 1) { 30137a7bc39SJason Zhu avb_error("Unsupported public key metadata.\n"); 30237a7bc39SJason Zhu return AVB_IO_RESULT_OK; 30337a7bc39SJason Zhu } 30437a7bc39SJason Zhu 30537a7bc39SJason Zhu /* Verify the PIK certificate. */ 30637a7bc39SJason Zhu result = ops->read_rollback_index( 30737a7bc39SJason Zhu ops, AVB_ATX_PIK_VERSION_LOCATION, &minimum_version); 30837a7bc39SJason Zhu if (result != AVB_IO_RESULT_OK) { 30937a7bc39SJason Zhu avb_error("Failed to read PIK minimum version.\n"); 31037a7bc39SJason Zhu return result; 31137a7bc39SJason Zhu } 31237a7bc39SJason Zhu if (!verify_pik_certificate(&metadata.product_intermediate_key_certificate, 31337a7bc39SJason Zhu permanent_attributes.product_root_public_key, 31437a7bc39SJason Zhu minimum_version)) { 31537a7bc39SJason Zhu return AVB_IO_RESULT_OK; 31637a7bc39SJason Zhu } 31737a7bc39SJason Zhu 31837a7bc39SJason Zhu /* Verify the PSK certificate. */ 31937a7bc39SJason Zhu result = ops->read_rollback_index( 32037a7bc39SJason Zhu ops, AVB_ATX_PSK_VERSION_LOCATION, &minimum_version); 32137a7bc39SJason Zhu if (result != AVB_IO_RESULT_OK) { 32237a7bc39SJason Zhu avb_error("Failed to read PSK minimum version.\n"); 32337a7bc39SJason Zhu return result; 32437a7bc39SJason Zhu } 32537a7bc39SJason Zhu if (!verify_psk_certificate( 32637a7bc39SJason Zhu &metadata.product_signing_key_certificate, 32737a7bc39SJason Zhu metadata.product_intermediate_key_certificate.signed_data.public_key, 32837a7bc39SJason Zhu minimum_version, 32937a7bc39SJason Zhu permanent_attributes.product_id)) { 33037a7bc39SJason Zhu return AVB_IO_RESULT_OK; 33137a7bc39SJason Zhu } 33237a7bc39SJason Zhu 33337a7bc39SJason Zhu /* Verify the PSK is the same key that verified vbmeta. */ 33437a7bc39SJason Zhu if (public_key_length != AVB_ATX_PUBLIC_KEY_SIZE) { 33537a7bc39SJason Zhu avb_error("Public key length mismatch.\n"); 33637a7bc39SJason Zhu return AVB_IO_RESULT_OK; 33737a7bc39SJason Zhu } 33837a7bc39SJason Zhu if (0 != avb_safe_memcmp( 33937a7bc39SJason Zhu metadata.product_signing_key_certificate.signed_data.public_key, 34037a7bc39SJason Zhu public_key_data, 34137a7bc39SJason Zhu AVB_ATX_PUBLIC_KEY_SIZE)) { 34237a7bc39SJason Zhu avb_error("Public key mismatch.\n"); 34337a7bc39SJason Zhu return AVB_IO_RESULT_OK; 34437a7bc39SJason Zhu } 34537a7bc39SJason Zhu 34637a7bc39SJason Zhu /* Report the key versions used during verification. */ 34737a7bc39SJason Zhu ops->atx_ops->set_key_version( 34837a7bc39SJason Zhu ops->atx_ops, 34937a7bc39SJason Zhu AVB_ATX_PIK_VERSION_LOCATION, 35037a7bc39SJason Zhu metadata.product_intermediate_key_certificate.signed_data.key_version); 35137a7bc39SJason Zhu ops->atx_ops->set_key_version( 35237a7bc39SJason Zhu ops->atx_ops, 35337a7bc39SJason Zhu AVB_ATX_PSK_VERSION_LOCATION, 35437a7bc39SJason Zhu metadata.product_signing_key_certificate.signed_data.key_version); 35537a7bc39SJason Zhu 35637a7bc39SJason Zhu *out_is_trusted = true; 35737a7bc39SJason Zhu return AVB_IO_RESULT_OK; 35837a7bc39SJason Zhu } 359ab608f80SJason Zhu 360ab608f80SJason Zhu AvbIOResult avb_atx_generate_unlock_challenge( 361ab608f80SJason Zhu AvbAtxOps* atx_ops, AvbAtxUnlockChallenge* out_unlock_challenge) { 362ab608f80SJason Zhu AvbIOResult result = AVB_IO_RESULT_OK; 363ab608f80SJason Zhu AvbAtxPermanentAttributes permanent_attributes; 364ab608f80SJason Zhu 365ab608f80SJason Zhu /* We need the permanent attributes to compute the product_id_hash. */ 366ab608f80SJason Zhu result = atx_ops->read_permanent_attributes(atx_ops, &permanent_attributes); 367ab608f80SJason Zhu if (result != AVB_IO_RESULT_OK) { 368ab608f80SJason Zhu avb_error("Failed to read permanent attributes.\n"); 369ab608f80SJason Zhu return result; 370ab608f80SJason Zhu } 371ab608f80SJason Zhu result = atx_ops->get_random( 372ab608f80SJason Zhu atx_ops, AVB_ATX_UNLOCK_CHALLENGE_SIZE, last_unlock_challenge); 373ab608f80SJason Zhu if (result != AVB_IO_RESULT_OK) { 374ab608f80SJason Zhu avb_error("Failed to generate random challenge.\n"); 375ab608f80SJason Zhu return result; 376ab608f80SJason Zhu } 377ab608f80SJason Zhu out_unlock_challenge->version = 1; 378ab608f80SJason Zhu sha256(permanent_attributes.product_id, 379ab608f80SJason Zhu AVB_ATX_PRODUCT_ID_SIZE, 380ab608f80SJason Zhu out_unlock_challenge->product_id_hash); 381ab608f80SJason Zhu avb_memcpy(out_unlock_challenge->challenge, 382ab608f80SJason Zhu last_unlock_challenge, 383ab608f80SJason Zhu AVB_ATX_UNLOCK_CHALLENGE_SIZE); 384ab608f80SJason Zhu return result; 385ab608f80SJason Zhu } 386ab608f80SJason Zhu 387ab608f80SJason Zhu AvbIOResult avb_atx_validate_unlock_credential( 388ab608f80SJason Zhu AvbAtxOps* atx_ops, 389ab608f80SJason Zhu const AvbAtxUnlockCredential* unlock_credential, 390ab608f80SJason Zhu bool* out_is_trusted) { 391ab608f80SJason Zhu AvbIOResult result = AVB_IO_RESULT_OK; 392ab608f80SJason Zhu AvbAtxPermanentAttributes permanent_attributes; 393ab608f80SJason Zhu uint8_t permanent_attributes_hash[AVB_SHA256_DIGEST_SIZE]; 394ab608f80SJason Zhu uint64_t minimum_version; 395ab608f80SJason Zhu const AvbAlgorithmData* algorithm_data; 396ab608f80SJason Zhu uint8_t challenge_hash[AVB_SHA512_DIGEST_SIZE]; 397ab608f80SJason Zhu 398ab608f80SJason Zhu /* Be pessimistic so we can exit early without having to remember to clear. 399ab608f80SJason Zhu */ 400ab608f80SJason Zhu *out_is_trusted = false; 401ab608f80SJason Zhu 402ab608f80SJason Zhu /* Sanity check the credential. */ 403ab608f80SJason Zhu if (unlock_credential->version != 1) { 404ab608f80SJason Zhu avb_error("Unsupported unlock credential format.\n"); 405ab608f80SJason Zhu return AVB_IO_RESULT_OK; 406ab608f80SJason Zhu } 407ab608f80SJason Zhu 408ab608f80SJason Zhu /* Read and verify permanent attributes. */ 409ab608f80SJason Zhu result = atx_ops->read_permanent_attributes(atx_ops, &permanent_attributes); 410ab608f80SJason Zhu if (result != AVB_IO_RESULT_OK) { 411ab608f80SJason Zhu avb_error("Failed to read permanent attributes.\n"); 412ab608f80SJason Zhu return result; 413ab608f80SJason Zhu } 414ab608f80SJason Zhu result = atx_ops->read_permanent_attributes_hash(atx_ops, 415ab608f80SJason Zhu permanent_attributes_hash); 416ab608f80SJason Zhu if (result != AVB_IO_RESULT_OK) { 417ab608f80SJason Zhu avb_error("Failed to read permanent attributes hash.\n"); 418ab608f80SJason Zhu return result; 419ab608f80SJason Zhu } 420ab608f80SJason Zhu if (!verify_permanent_attributes(&permanent_attributes, 421ab608f80SJason Zhu permanent_attributes_hash)) { 422ab608f80SJason Zhu return AVB_IO_RESULT_OK; 423ab608f80SJason Zhu } 424ab608f80SJason Zhu 425ab608f80SJason Zhu /* Verify the PIK certificate. */ 426ab608f80SJason Zhu result = atx_ops->ops->read_rollback_index( 427ab608f80SJason Zhu atx_ops->ops, AVB_ATX_PIK_VERSION_LOCATION, &minimum_version); 428ab608f80SJason Zhu if (result != AVB_IO_RESULT_OK) { 429ab608f80SJason Zhu avb_error("Failed to read PIK minimum version.\n"); 430ab608f80SJason Zhu return result; 431ab608f80SJason Zhu } 432ab608f80SJason Zhu if (!verify_pik_certificate( 433ab608f80SJason Zhu &unlock_credential->product_intermediate_key_certificate, 434ab608f80SJason Zhu permanent_attributes.product_root_public_key, 435ab608f80SJason Zhu minimum_version)) { 436ab608f80SJason Zhu return AVB_IO_RESULT_OK; 437ab608f80SJason Zhu } 438ab608f80SJason Zhu 439ab608f80SJason Zhu /* Verify the PUK certificate. The minimum version is shared with the PSK. */ 440ab608f80SJason Zhu result = atx_ops->ops->read_rollback_index( 441ab608f80SJason Zhu atx_ops->ops, AVB_ATX_PSK_VERSION_LOCATION, &minimum_version); 442ab608f80SJason Zhu if (result != AVB_IO_RESULT_OK) { 443ab608f80SJason Zhu avb_error("Failed to read PSK minimum version.\n"); 444ab608f80SJason Zhu return result; 445ab608f80SJason Zhu } 446ab608f80SJason Zhu if (!verify_puk_certificate( 447ab608f80SJason Zhu &unlock_credential->product_unlock_key_certificate, 448ab608f80SJason Zhu unlock_credential->product_intermediate_key_certificate.signed_data 449ab608f80SJason Zhu .public_key, 450ab608f80SJason Zhu minimum_version, 451ab608f80SJason Zhu permanent_attributes.product_id)) { 452ab608f80SJason Zhu return AVB_IO_RESULT_OK; 453ab608f80SJason Zhu } 454ab608f80SJason Zhu 455ab608f80SJason Zhu /* Verify the challenge signature. */ 456ab608f80SJason Zhu algorithm_data = avb_get_algorithm_data(AVB_ALGORITHM_TYPE_SHA512_RSA4096); 457ab608f80SJason Zhu sha512(last_unlock_challenge, AVB_ATX_UNLOCK_CHALLENGE_SIZE, challenge_hash); 458ab608f80SJason Zhu if (!avb_rsa_verify(unlock_credential->product_unlock_key_certificate 459ab608f80SJason Zhu .signed_data.public_key, 460ab608f80SJason Zhu AVB_ATX_PUBLIC_KEY_SIZE, 461ab608f80SJason Zhu unlock_credential->challenge_signature, 462ab608f80SJason Zhu AVB_RSA4096_NUM_BYTES, 463ab608f80SJason Zhu challenge_hash, 464ab608f80SJason Zhu AVB_SHA512_DIGEST_SIZE, 465ab608f80SJason Zhu algorithm_data->padding, 466ab608f80SJason Zhu algorithm_data->padding_len)) { 467ab608f80SJason Zhu avb_error("Invalid unlock challenge signature.\n"); 468ab608f80SJason Zhu return AVB_IO_RESULT_OK; 469ab608f80SJason Zhu } 470ab608f80SJason Zhu 471ab608f80SJason Zhu *out_is_trusted = true; 472ab608f80SJason Zhu return AVB_IO_RESULT_OK; 473ab608f80SJason Zhu } 474