Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 1 | // Copyright 2022, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! This module implements test utils to generate various types of keys. |
| 16 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 17 | use crate::authorizations::AuthSetBuilder; |
| 18 | use crate::ffi_test_utils::{ |
| 19 | get_os_patchlevel, get_os_version, get_value_from_attest_record, get_vendor_patchlevel, |
| 20 | validate_certchain_with_strict_issuer_check, |
| 21 | }; |
| 22 | use crate::SecLevel; |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 23 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
Rajesh Nyamagoud | 11912ea | 2021-12-20 20:37:20 +0000 | [diff] [blame] | 24 | Algorithm::Algorithm, BlockMode::BlockMode, Digest::Digest, EcCurve::EcCurve, |
Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +0000 | [diff] [blame] | 25 | ErrorCode::ErrorCode, HardwareAuthenticatorType::HardwareAuthenticatorType, |
| 26 | KeyOrigin::KeyOrigin, KeyParameter::KeyParameter, KeyParameterValue::KeyParameterValue, |
Rajesh Nyamagoud | 5f6db2f | 2023-06-01 17:22:32 +0000 | [diff] [blame] | 27 | KeyPurpose::KeyPurpose, PaddingMode::PaddingMode, SecurityLevel::SecurityLevel, Tag::Tag, |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 28 | }; |
| 29 | use android_system_keystore2::aidl::android::system::keystore2::{ |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 30 | AuthenticatorSpec::AuthenticatorSpec, Authorization::Authorization, |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 31 | CreateOperationResponse::CreateOperationResponse, Domain::Domain, KeyDescriptor::KeyDescriptor, |
Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +0000 | [diff] [blame] | 32 | KeyMetadata::KeyMetadata, ResponseCode::ResponseCode, |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 33 | }; |
Rajesh Nyamagoud | b881d51 | 2021-12-10 00:33:15 +0000 | [diff] [blame] | 34 | use android_system_keystore2::binder::{ExceptionCode, Result as BinderResult}; |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 35 | use anyhow::Result; |
| 36 | use binder::ThreadState; |
| 37 | use core::ops::Range; |
| 38 | use nix::unistd::getuid; |
| 39 | use std::collections::HashSet; |
| 40 | use std::fmt::Write; |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 41 | use std::path::PathBuf; |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 42 | |
Rajesh Nyamagoud | b881d51 | 2021-12-10 00:33:15 +0000 | [diff] [blame] | 43 | /// Shell namespace. |
| 44 | pub const SELINUX_SHELL_NAMESPACE: i64 = 1; |
Rajesh Nyamagoud | dc6fb23 | 2021-12-08 21:27:15 +0000 | [diff] [blame] | 45 | /// Vold namespace. |
| 46 | pub const SELINUX_VOLD_NAMESPACE: i64 = 100; |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 47 | |
Rajesh Nyamagoud | fa7c0f1 | 2021-12-02 17:15:48 +0000 | [diff] [blame] | 48 | /// SU context. |
| 49 | pub const TARGET_SU_CTX: &str = "u:r:su:s0"; |
| 50 | |
| 51 | /// Vold context |
| 52 | pub const TARGET_VOLD_CTX: &str = "u:r:vold:s0"; |
| 53 | |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 54 | const TEE_KEYMINT_RKP_ONLY: &str = "remote_provisioning.tee.rkp_only"; |
| 55 | |
| 56 | const STRONGBOX_KEYMINT_RKP_ONLY: &str = "remote_provisioning.strongbox.rkp_only"; |
| 57 | |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 58 | /// Allowed tags in generated/imported key authorizations. |
| 59 | /// See hardware/interfaces/security/keymint/aidl/android/hardware/security/keymint/Tag.aidl for the |
| 60 | /// list feature tags. |
| 61 | /// Note: This list need to be updated whenever a new Tag is introduced and is expected to be added |
| 62 | /// in key authorizations. |
| 63 | pub const ALLOWED_TAGS_IN_KEY_AUTHS: &[Tag] = &[ |
| 64 | Tag::ACTIVE_DATETIME, |
| 65 | Tag::ALGORITHM, |
| 66 | Tag::ALLOW_WHILE_ON_BODY, |
| 67 | Tag::AUTH_TIMEOUT, |
| 68 | Tag::BLOCK_MODE, |
| 69 | Tag::BOOTLOADER_ONLY, |
| 70 | Tag::BOOT_PATCHLEVEL, |
| 71 | Tag::CALLER_NONCE, |
| 72 | Tag::CREATION_DATETIME, |
| 73 | Tag::DIGEST, |
| 74 | Tag::EARLY_BOOT_ONLY, |
| 75 | Tag::EC_CURVE, |
| 76 | Tag::IDENTITY_CREDENTIAL_KEY, |
| 77 | Tag::INCLUDE_UNIQUE_ID, |
| 78 | Tag::KEY_SIZE, |
| 79 | Tag::MAX_BOOT_LEVEL, |
| 80 | Tag::MAX_USES_PER_BOOT, |
| 81 | Tag::MIN_MAC_LENGTH, |
| 82 | Tag::NO_AUTH_REQUIRED, |
| 83 | Tag::ORIGIN, |
| 84 | Tag::ORIGINATION_EXPIRE_DATETIME, |
| 85 | Tag::OS_PATCHLEVEL, |
| 86 | Tag::OS_VERSION, |
| 87 | Tag::PADDING, |
| 88 | Tag::PURPOSE, |
| 89 | Tag::ROLLBACK_RESISTANCE, |
| 90 | Tag::RSA_OAEP_MGF_DIGEST, |
| 91 | Tag::RSA_PUBLIC_EXPONENT, |
| 92 | Tag::STORAGE_KEY, |
| 93 | Tag::TRUSTED_CONFIRMATION_REQUIRED, |
| 94 | Tag::TRUSTED_USER_PRESENCE_REQUIRED, |
| 95 | Tag::UNLOCKED_DEVICE_REQUIRED, |
| 96 | Tag::USAGE_COUNT_LIMIT, |
| 97 | Tag::USAGE_EXPIRE_DATETIME, |
| 98 | Tag::USER_AUTH_TYPE, |
| 99 | Tag::USER_ID, |
| 100 | Tag::USER_SECURE_ID, |
| 101 | Tag::VENDOR_PATCHLEVEL, |
| 102 | ]; |
| 103 | |
Rajesh Nyamagoud | 11912ea | 2021-12-20 20:37:20 +0000 | [diff] [blame] | 104 | /// Key parameters to generate a key. |
| 105 | pub struct KeyParams { |
| 106 | /// Key Size. |
| 107 | pub key_size: i32, |
| 108 | /// Key Purposes. |
| 109 | pub purpose: Vec<KeyPurpose>, |
| 110 | /// Padding Mode. |
| 111 | pub padding: Option<PaddingMode>, |
| 112 | /// Digest. |
| 113 | pub digest: Option<Digest>, |
| 114 | /// MFG Digest. |
| 115 | pub mgf_digest: Option<Digest>, |
| 116 | /// Block Mode. |
| 117 | pub block_mode: Option<BlockMode>, |
| 118 | /// Attestation challenge. |
| 119 | pub att_challenge: Option<Vec<u8>>, |
Rajesh Nyamagoud | 11912ea | 2021-12-20 20:37:20 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 122 | /// DER-encoded PKCS#8 format RSA key. Generated using: |
| 123 | /// openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt -outform der | hexdump -e '30/1 "%02X" "\n"' |
| 124 | pub static RSA_2048_KEY: &[u8] = &[ |
| 125 | 0x30, 0x82, 0x04, 0xBD, 0x02, 0x01, 0x00, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, |
| 126 | 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x04, 0xA7, 0x30, 0x82, 0x04, 0xA3, 0x02, 0x01, |
| 127 | 0x00, 0x02, 0x82, 0x01, 0x01, 0x00, 0xE5, 0x14, 0xE3, 0xC2, 0x43, 0xF3, 0x0F, 0xCC, 0x22, 0x73, |
| 128 | 0x9C, 0x84, 0xCC, 0x1B, 0x6C, 0x97, 0x4B, 0xC9, 0xDF, 0x1F, 0xE2, 0xB8, 0x80, 0x85, 0xF9, 0x27, |
| 129 | 0xAB, 0x97, 0x94, 0x58, 0x4B, 0xC9, 0x40, 0x94, 0x5A, 0xB4, 0xD4, 0xF8, 0xD0, 0x36, 0xC4, 0x86, |
| 130 | 0x17, 0x7D, 0xA2, 0x48, 0x6D, 0x40, 0xF0, 0xB9, 0x61, 0x4F, 0xCE, 0x65, 0x80, 0x88, 0x81, 0x59, |
| 131 | 0x95, 0x11, 0x24, 0xF4, 0x36, 0xB7, 0xB7, 0x37, 0x44, 0xF4, 0x6C, 0x1C, 0xEB, 0x04, 0x19, 0x78, |
| 132 | 0xB2, 0x29, 0x4D, 0x21, 0x44, 0x16, 0x57, 0x58, 0x6D, 0x7D, 0x56, 0xB5, 0x99, 0xDD, 0xD2, 0xAD, |
| 133 | 0x02, 0x9A, 0x72, 0x16, 0x67, 0xD6, 0x00, 0x9F, 0x69, 0xE0, 0x25, 0xEE, 0x7C, 0x86, 0x54, 0x27, |
| 134 | 0x4B, 0x50, 0xEF, 0x60, 0x52, 0x60, 0x82, 0xAA, 0x09, 0x15, 0x72, 0xD2, 0xEB, 0x01, 0x52, 0x04, |
| 135 | 0x39, 0x60, 0xBC, 0x5E, 0x95, 0x07, 0xC8, 0xC2, 0x3A, 0x3A, 0xE2, 0xA4, 0x99, 0x6B, 0x27, 0xE3, |
| 136 | 0xA3, 0x55, 0x69, 0xC4, 0xB3, 0x2D, 0x19, 0xC4, 0x34, 0x76, 0xFC, 0x27, 0xDA, 0x22, 0xB2, 0x62, |
| 137 | 0x69, 0x25, 0xDE, 0x0D, 0xE7, 0x54, 0x3C, 0xBB, 0x61, 0xD2, 0x20, 0xDA, 0x7B, 0x6E, 0x63, 0xBD, |
| 138 | 0x9A, 0x4B, 0xCD, 0x75, 0xC6, 0xA1, 0x5E, 0x1C, 0x3E, 0xD5, 0x63, 0x59, 0x22, 0x7E, 0xE0, 0x6C, |
| 139 | 0x98, 0x25, 0x63, 0x97, 0x56, 0xDF, 0x71, 0xF5, 0x4C, 0x78, 0xE9, 0xE1, 0xD5, 0xFC, 0xF8, 0x5A, |
| 140 | 0x5B, 0xF6, 0x1D, 0xFA, 0x5A, 0x99, 0x4C, 0x99, 0x19, 0x21, 0x1D, 0xF5, 0x24, 0x07, 0xEF, 0x8A, |
| 141 | 0xC9, 0x9F, 0xE7, 0x3F, 0xBB, 0x46, 0x1A, 0x16, 0x96, 0xC6, 0xD6, 0x12, 0x7E, 0xDA, 0xCB, 0xEB, |
| 142 | 0x2F, 0x1D, 0x3B, 0x31, 0xCC, 0x55, 0x63, 0xA2, 0x6F, 0x8A, 0xDE, 0x35, 0x52, 0x40, 0x04, 0xBF, |
| 143 | 0xE0, 0x82, 0x32, 0xE1, 0x6D, 0x8B, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01, 0x00, 0x2D, |
| 144 | 0x1F, 0x71, 0x41, 0x79, 0xBA, 0xED, 0xD8, 0xAA, 0xCC, 0x94, 0xFE, 0xFF, 0x69, 0x43, 0x79, 0x85, |
| 145 | 0xBF, 0x2C, 0xC9, 0x0E, 0x12, 0x83, 0x96, 0x60, 0x1E, 0x75, 0x49, 0x35, 0x3A, 0x33, 0x2B, 0x60, |
| 146 | 0x22, 0x18, 0xBF, 0xD7, 0xD7, 0x6E, 0xC3, 0xEA, 0xEF, 0xF2, 0xBE, 0x97, 0x71, 0xA6, 0xBB, 0x8C, |
| 147 | 0xEF, 0x27, 0x00, 0xDE, 0x49, 0xD6, 0x08, 0x8D, 0x5A, 0x04, 0xE7, 0xCC, 0x9C, 0xA2, 0x0E, 0x8B, |
| 148 | 0xF3, 0x42, 0x0C, 0xD7, 0x22, 0xD7, 0x14, 0x06, 0xA4, 0x64, 0x8B, 0x88, 0x1A, 0xCE, 0x5B, 0x8C, |
| 149 | 0x36, 0xE9, 0xD2, 0x2F, 0x7B, 0x33, 0xE4, 0xA2, 0xB3, 0xDB, 0x78, 0x6A, 0x92, 0x89, 0x3F, 0x78, |
| 150 | 0xFD, 0xED, 0x8F, 0xEE, 0x48, 0xCC, 0x94, 0x75, 0x0D, 0x0C, 0x63, 0xD3, 0xD2, 0xE8, 0x47, 0x04, |
| 151 | 0x55, 0xD3, 0xD6, 0x3A, 0xB8, 0xDA, 0xFB, 0x76, 0x99, 0x48, 0x68, 0x0A, 0x92, 0xA2, 0xCD, 0xF7, |
| 152 | 0x45, 0x8B, 0x50, 0xFE, 0xF9, 0x1A, 0x33, 0x24, 0x3C, 0x2E, 0xDE, 0x88, 0xAD, 0xB2, 0x5B, 0x9F, |
| 153 | 0x44, 0xEA, 0xD1, 0x9F, 0xC7, 0x9F, 0x02, 0x5E, 0x31, 0x61, 0xB3, 0xD6, 0xE2, 0xE1, 0xBC, 0xFB, |
| 154 | 0x1C, 0xDB, 0xBD, 0xB2, 0x9A, 0xE5, 0xEF, 0xDA, 0xCD, 0x29, 0xA5, 0x45, 0xCC, 0x67, 0x01, 0x8B, |
| 155 | 0x1C, 0x1D, 0x0E, 0x8F, 0x73, 0x69, 0x4D, 0x4D, 0xF6, 0x9D, 0xA6, 0x6C, 0x9A, 0x1C, 0xF4, 0x5C, |
| 156 | 0xE4, 0x83, 0x9A, 0x77, 0x12, 0x01, 0xBD, 0xCE, 0x66, 0x3A, 0x4B, 0x3D, 0x6E, 0xE0, 0x6E, 0x82, |
| 157 | 0x98, 0xDE, 0x74, 0x11, 0x47, 0xEC, 0x7A, 0x3A, 0xA9, 0xD8, 0x48, 0x00, 0x26, 0x64, 0x47, 0x7B, |
| 158 | 0xAE, 0x55, 0x9D, 0x29, 0x22, 0xB4, 0xB3, 0xB9, 0xB1, 0x64, 0xEA, 0x3B, 0x5A, 0xD3, 0x3F, 0x8D, |
| 159 | 0x0F, 0x14, 0x7E, 0x4E, 0xB8, 0x1B, 0x06, 0xFC, 0xB1, 0x7E, 0xCD, 0xB9, 0x1A, 0x4E, 0xA1, 0x02, |
| 160 | 0x81, 0x81, 0x00, 0xF9, 0xDE, 0xEE, 0xED, 0x13, 0x2F, 0xBB, 0xE7, 0xE2, 0xB3, 0x2D, 0x98, 0xD2, |
| 161 | 0xE8, 0x25, 0x07, 0x5A, 0x1E, 0x51, 0x0A, 0xC8, 0xAD, 0x50, 0x4B, 0x80, 0xC6, 0x22, 0xF5, 0x9B, |
| 162 | 0x08, 0xE6, 0x3D, 0x01, 0xC6, 0x3E, 0xC8, 0xD2, 0x54, 0x9F, 0x91, 0x77, 0x95, 0xCD, 0xCA, 0xC7, |
| 163 | 0xE7, 0x47, 0x94, 0xA9, 0x5F, 0x4E, 0xBE, 0x31, 0x3D, 0xB4, 0xAF, 0x43, 0x0F, 0xDC, 0x8D, 0x9C, |
| 164 | 0x1E, 0x52, 0x7B, 0x72, 0x21, 0x34, 0xB3, 0x96, 0x7C, 0x9C, 0xB8, 0x51, 0x65, 0x60, 0xAC, 0x3D, |
| 165 | 0x11, 0x32, 0xB8, 0xD6, 0x34, 0x35, 0x66, 0xD0, 0x30, 0xB9, 0xE9, 0x67, 0x2C, 0x87, 0x73, 0x43, |
| 166 | 0x9C, 0x12, 0x16, 0x7D, 0x4A, 0xD9, 0xA3, 0x4C, 0x24, 0x64, 0x6A, 0x32, 0x8E, 0xC3, 0xD8, 0x00, |
| 167 | 0x90, 0x5C, 0x4D, 0x65, 0x01, 0x53, 0x8A, 0xD0, 0x87, 0xCE, 0x96, 0xEF, 0xFA, 0x73, 0x03, 0xF1, |
| 168 | 0xDC, 0x1B, 0x9B, 0x02, 0x81, 0x81, 0x00, 0xEA, 0xB3, 0x69, 0x00, 0x11, 0x0E, 0x50, 0xAA, 0xD3, |
| 169 | 0x22, 0x51, 0x78, 0x9D, 0xFF, 0x05, 0x62, 0xBC, 0x9A, 0x67, 0x86, 0xE1, 0xC5, 0x02, 0x2D, 0x14, |
| 170 | 0x11, 0x29, 0x30, 0xE7, 0x90, 0x5D, 0x72, 0x6F, 0xC5, 0x62, 0xEB, 0xD4, 0xB0, 0x3F, 0x3D, 0xDC, |
| 171 | 0xB9, 0xFC, 0x2B, 0x5C, 0xBD, 0x9E, 0x71, 0x81, 0x5C, 0xC5, 0xFE, 0xDF, 0x69, 0x73, 0x12, 0x66, |
| 172 | 0x92, 0x06, 0xD4, 0xD5, 0x8F, 0xDF, 0x14, 0x2E, 0x9C, 0xD0, 0x4C, 0xC2, 0x4D, 0x31, 0x2E, 0x47, |
| 173 | 0xA5, 0xDC, 0x8A, 0x83, 0x7B, 0xE8, 0xA5, 0xC3, 0x03, 0x98, 0xD8, 0xBF, 0xF4, 0x7D, 0x6E, 0x87, |
| 174 | 0x55, 0xE4, 0x0F, 0x15, 0x10, 0xC8, 0x76, 0x4F, 0xAD, 0x1D, 0x1C, 0x95, 0x41, 0x9D, 0x88, 0xEC, |
| 175 | 0x8C, 0xDA, 0xBA, 0x90, 0x7F, 0x8D, 0xD9, 0x8B, 0x47, 0x6C, 0x0C, 0xFF, 0xBA, 0x73, 0x00, 0x20, |
| 176 | 0x1F, 0xF7, 0x7E, 0x5F, 0xF4, 0xEC, 0xD1, 0x02, 0x81, 0x80, 0x16, 0xB7, 0x43, 0xB5, 0x5D, 0xD7, |
| 177 | 0x2B, 0x18, 0x0B, 0xAE, 0x0A, 0x69, 0x28, 0x53, 0x5E, 0x7A, 0x6A, 0xA0, 0xF2, 0xF1, 0x2E, 0x09, |
| 178 | 0x43, 0x91, 0x79, 0xA5, 0x89, 0xAC, 0x16, 0x6A, 0x1A, 0xB4, 0x55, 0x22, 0xF6, 0xB6, 0x3F, 0x18, |
| 179 | 0xDE, 0x60, 0xD5, 0x24, 0x53, 0x4F, 0x2A, 0x19, 0x46, 0x92, 0xA7, 0x4B, 0x38, 0xD7, 0x65, 0x96, |
| 180 | 0x9C, 0x84, 0x8A, 0x6E, 0x38, 0xB8, 0xCF, 0x06, 0x9A, 0xAD, 0x0A, 0x55, 0x26, 0x7B, 0x65, 0x24, |
| 181 | 0xF3, 0x02, 0x76, 0xB3, 0xE6, 0xB4, 0x01, 0xE1, 0x3C, 0x61, 0x3D, 0x68, 0x05, 0xAA, 0xD1, 0x26, |
| 182 | 0x7C, 0xE0, 0x51, 0x36, 0xE5, 0x21, 0x7F, 0x76, 0x02, 0xD6, 0xF4, 0x91, 0x07, 0x74, 0x27, 0x09, |
| 183 | 0xEF, 0xEF, 0x0F, 0xA5, 0x96, 0xFC, 0x5E, 0x20, 0xC1, 0xA3, 0x6F, 0x99, 0x4D, 0x45, 0x03, 0x6C, |
| 184 | 0x35, 0x45, 0xD7, 0x8F, 0x47, 0x41, 0x86, 0x8D, 0x62, 0x1D, 0x02, 0x81, 0x81, 0x00, 0xC3, 0x93, |
| 185 | 0x85, 0xA7, 0xFC, 0x8E, 0x85, 0x42, 0x14, 0x76, 0xC0, 0x95, 0x56, 0x73, 0xB0, 0xB5, 0x3A, 0x9D, |
| 186 | 0x20, 0x30, 0x11, 0xEA, 0xED, 0x89, 0x4A, 0xF3, 0x91, 0xF3, 0xA2, 0xC3, 0x76, 0x5B, 0x6A, 0x30, |
| 187 | 0x7D, 0xE2, 0x2F, 0x76, 0x3E, 0xFC, 0xF9, 0xF6, 0x31, 0xE0, 0xA0, 0x83, 0x92, 0x88, 0xDB, 0x57, |
| 188 | 0xC7, 0xD6, 0x3F, 0xAD, 0xCB, 0xAA, 0x45, 0xB6, 0xE1, 0xE2, 0x71, 0xA4, 0x56, 0x2C, 0xA7, 0x3B, |
| 189 | 0x1D, 0x89, 0x19, 0x50, 0xE1, 0xEE, 0xC2, 0xDD, 0xC0, 0x0D, 0xDC, 0xCB, 0x60, 0x6E, 0xE1, 0x37, |
| 190 | 0x1A, 0x23, 0x64, 0xB2, 0x03, 0xE4, 0x1A, 0xFA, 0xC3, 0xF4, 0x9D, 0x85, 0x42, 0xC6, 0xF4, 0x56, |
| 191 | 0x39, 0xB0, 0x1B, 0xE0, 0x75, 0xBA, 0x28, 0x04, 0xA8, 0x30, 0x57, 0x41, 0x33, 0x9F, 0x58, 0xA4, |
| 192 | 0xC7, 0xB1, 0x7D, 0x58, 0x8D, 0x84, 0x49, 0x40, 0xDA, 0x28, 0x81, 0x25, 0xC4, 0x41, 0x02, 0x81, |
| 193 | 0x80, 0x13, 0x20, 0x65, 0xD5, 0x96, 0x98, 0x8D, 0x16, 0x73, 0xA1, 0x31, 0x73, 0x79, 0xBA, 0xEC, |
| 194 | 0xB0, 0xD9, 0x0C, 0xF6, 0xEF, 0x2F, 0xC2, 0xE7, 0x96, 0x9B, 0xA1, 0x2D, 0xE9, 0xFB, 0x45, 0xB9, |
| 195 | 0xD0, 0x30, 0xE2, 0xBD, 0x30, 0x4F, 0xB6, 0xFE, 0x24, 0x02, 0xCF, 0x8D, 0x51, 0x48, 0x45, 0xD9, |
| 196 | 0xF7, 0x20, 0x53, 0x1C, 0x0B, 0xA9, 0x7E, 0xC2, 0xA2, 0x65, 0xCC, 0x3E, 0x0E, 0x0D, 0xF1, 0x62, |
| 197 | 0xDD, 0x5F, 0xBC, 0x55, 0x9B, 0x58, 0x26, 0x40, 0x6A, 0xEE, 0x02, 0x55, 0x36, 0xE9, 0xBA, 0x82, |
| 198 | 0x5A, 0xFD, 0x3C, 0xDF, 0xA6, 0x26, 0x32, 0x81, 0xA9, 0x5E, 0x46, 0xBE, 0xBA, 0xDC, 0xD3, 0x2A, |
| 199 | 0x3A, 0x3B, 0xC1, 0x4E, 0xF7, 0x1A, 0xDC, 0x4B, 0xAF, 0x67, 0x1B, 0x3A, 0x83, 0x0D, 0x04, 0xDE, |
| 200 | 0x27, 0x47, 0xFC, 0xE6, 0x39, 0x89, 0x7B, 0x66, 0xF9, 0x50, 0x4D, 0xF1, 0xAC, 0x20, 0x43, 0x7E, |
| 201 | 0xEE, |
| 202 | ]; |
| 203 | |
| 204 | /// DER-encoded PKCS#8 format EC key. Generated using: |
| 205 | /// openssl ecparam -name prime256v1 -genkey | openssl pkcs8 -topk8 -nocrypt -outform der | hexdump -e '30/1 "%02X" "\n"' |
| 206 | pub static EC_P_256_KEY: &[u8] = &[ |
| 207 | 0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, |
| 208 | 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x04, 0x6D, 0x30, 0x6B, 0x02, |
| 209 | 0x01, 0x01, 0x04, 0x20, 0xB9, 0x1D, 0xAF, 0x50, 0xFD, 0xD8, 0x6A, 0x40, 0xAB, 0x2C, 0xCB, 0x54, |
| 210 | 0x4E, 0xED, 0xF1, 0x64, 0xBC, 0x30, 0x25, 0xFB, 0xC4, 0x69, 0x00, 0x34, 0x1A, 0x82, 0xA3, 0x72, |
| 211 | 0x5D, 0xC7, 0xA9, 0x85, 0xA1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xE8, 0x53, 0x0A, 0xF2, 0xD3, 0x68, |
| 212 | 0x40, 0x48, 0x8C, 0xB4, 0x2F, 0x11, 0x34, 0xD7, 0xF4, 0x4A, 0x5C, 0x33, 0xFF, 0xF6, 0x2B, 0xF7, |
| 213 | 0x98, 0x0F, 0x02, 0xA5, 0xD7, 0x4F, 0xF9, 0xDE, 0x60, 0x9C, 0x6E, 0xB0, 0x45, 0xDA, 0x3F, 0xF4, |
| 214 | 0x34, 0x23, 0x9B, 0x4C, 0x3A, 0x09, 0x9C, 0x5E, 0x5D, 0x37, 0x96, 0xAC, 0x4A, 0xE7, 0x65, 0x2B, |
| 215 | 0xD6, 0x84, 0x98, 0xEA, 0x96, 0x91, 0xFB, 0x78, 0xED, 0x86, |
| 216 | ]; |
| 217 | |
Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +0000 | [diff] [blame] | 218 | /// DER-encoded PKCS#8 format RSA key - |
| 219 | /// Size: 2048 |
| 220 | /// Public Exponent: 65537 |
| 221 | /// Purpose: WRAP_KEY, ENCRYPT, DECRYPT |
| 222 | /// Encryption scheme: RSAES-PKCS1-v1_5 |
| 223 | /// Digest: SHA_2_256 |
| 224 | /// Padding: RSA_OAEP |
| 225 | /// This sample wrapping_key is taken from KeyMint tests |
| 226 | /// (see hardware/interfaces/security/keymint/aidl/vts/functional/KeyMintTest.cpp). |
| 227 | /// Similarly more test keys can be generated with below command - |
| 228 | /// openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt -outform der | hexdump -e '30/1 "%02X" "\n"' |
| 229 | pub static WRAPPING_KEY: &[u8] = &[ |
| 230 | 0x30, 0x82, 0x04, 0xbe, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, |
| 231 | 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x04, 0xa8, 0x30, 0x82, 0x04, 0xa4, 0x02, 0x01, |
| 232 | 0x00, 0x02, 0x82, 0x01, 0x01, 0x00, 0xae, 0xc3, 0x67, 0x93, 0x1d, 0x89, 0x00, 0xce, 0x56, 0xb0, |
| 233 | 0x06, 0x7f, 0x7d, 0x70, 0xe1, 0xfc, 0x65, 0x3f, 0x3f, 0x34, 0xd1, 0x94, 0xc1, 0xfe, 0xd5, 0x00, |
| 234 | 0x18, 0xfb, 0x43, 0xdb, 0x93, 0x7b, 0x06, 0xe6, 0x73, 0xa8, 0x37, 0x31, 0x3d, 0x56, 0xb1, 0xc7, |
| 235 | 0x25, 0x15, 0x0a, 0x3f, 0xef, 0x86, 0xac, 0xbd, 0xdc, 0x41, 0xbb, 0x75, 0x9c, 0x28, 0x54, 0xea, |
| 236 | 0xe3, 0x2d, 0x35, 0x84, 0x1e, 0xfb, 0x5c, 0x18, 0xd8, 0x2b, 0xc9, 0x0a, 0x1c, 0xb5, 0xc1, 0xd5, |
| 237 | 0x5a, 0xdf, 0x24, 0x5b, 0x02, 0x91, 0x1f, 0x0b, 0x7c, 0xda, 0x88, 0xc4, 0x21, 0xff, 0x0e, 0xba, |
| 238 | 0xfe, 0x7c, 0x0d, 0x23, 0xbe, 0x31, 0x2d, 0x7b, 0xd5, 0x92, 0x1f, 0xfa, 0xea, 0x13, 0x47, 0xc1, |
| 239 | 0x57, 0x40, 0x6f, 0xef, 0x71, 0x8f, 0x68, 0x26, 0x43, 0xe4, 0xe5, 0xd3, 0x3c, 0x67, 0x03, 0xd6, |
| 240 | 0x1c, 0x0c, 0xf7, 0xac, 0x0b, 0xf4, 0x64, 0x5c, 0x11, 0xf5, 0xc1, 0x37, 0x4c, 0x38, 0x86, 0x42, |
| 241 | 0x74, 0x11, 0xc4, 0x49, 0x79, 0x67, 0x92, 0xe0, 0xbe, 0xf7, 0x5d, 0xec, 0x85, 0x8a, 0x21, 0x23, |
| 242 | 0xc3, 0x67, 0x53, 0xe0, 0x2a, 0x95, 0xa9, 0x6d, 0x7c, 0x45, 0x4b, 0x50, 0x4d, 0xe3, 0x85, 0xa6, |
| 243 | 0x42, 0xe0, 0xdf, 0xc3, 0xe6, 0x0a, 0xc3, 0xa7, 0xee, 0x49, 0x91, 0xd0, 0xd4, 0x8b, 0x01, 0x72, |
| 244 | 0xa9, 0x5f, 0x95, 0x36, 0xf0, 0x2b, 0xa1, 0x3c, 0xec, 0xcc, 0xb9, 0x2b, 0x72, 0x7d, 0xb5, 0xc2, |
| 245 | 0x7e, 0x5b, 0x2f, 0x5c, 0xec, 0x09, 0x60, 0x0b, 0x28, 0x6a, 0xf5, 0xcf, 0x14, 0xc4, 0x20, 0x24, |
| 246 | 0xc6, 0x1d, 0xdf, 0xe7, 0x1c, 0x2a, 0x8d, 0x74, 0x58, 0xf1, 0x85, 0x23, 0x4c, 0xb0, 0x0e, 0x01, |
| 247 | 0xd2, 0x82, 0xf1, 0x0f, 0x8f, 0xc6, 0x72, 0x1d, 0x2a, 0xed, 0x3f, 0x48, 0x33, 0xcc, 0xa2, 0xbd, |
| 248 | 0x8f, 0xa6, 0x28, 0x21, 0xdd, 0x55, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x82, 0x01, 0x00, 0x43, |
| 249 | 0x14, 0x47, 0xb6, 0x25, 0x19, 0x08, 0x11, 0x2b, 0x1e, 0xe7, 0x6f, 0x99, 0xf3, 0x71, 0x1a, 0x52, |
| 250 | 0xb6, 0x63, 0x09, 0x60, 0x04, 0x6c, 0x2d, 0xe7, 0x0d, 0xe1, 0x88, 0xd8, 0x33, 0xf8, 0xb8, 0xb9, |
| 251 | 0x1e, 0x4d, 0x78, 0x5c, 0xae, 0xee, 0xaf, 0x4f, 0x0f, 0x74, 0x41, 0x4e, 0x2c, 0xda, 0x40, 0x64, |
| 252 | 0x1f, 0x7f, 0xe2, 0x4f, 0x14, 0xc6, 0x7a, 0x88, 0x95, 0x9b, 0xdb, 0x27, 0x76, 0x6d, 0xf9, 0xe7, |
| 253 | 0x10, 0xb6, 0x30, 0xa0, 0x3a, 0xdc, 0x68, 0x3b, 0x5d, 0x2c, 0x43, 0x08, 0x0e, 0x52, 0xbe, 0xe7, |
| 254 | 0x1e, 0x9e, 0xae, 0xb6, 0xde, 0x29, 0x7a, 0x5f, 0xea, 0x10, 0x72, 0x07, 0x0d, 0x18, 0x1c, 0x82, |
| 255 | 0x2b, 0xcc, 0xff, 0x08, 0x7d, 0x63, 0xc9, 0x40, 0xba, 0x8a, 0x45, 0xf6, 0x70, 0xfe, 0xb2, 0x9f, |
| 256 | 0xb4, 0x48, 0x4d, 0x1c, 0x95, 0xe6, 0xd2, 0x57, 0x9b, 0xa0, 0x2a, 0xae, 0x0a, 0x00, 0x90, 0x0c, |
| 257 | 0x3e, 0xbf, 0x49, 0x0e, 0x3d, 0x2c, 0xd7, 0xee, 0x8d, 0x0e, 0x20, 0xc5, 0x36, 0xe4, 0xdc, 0x5a, |
| 258 | 0x50, 0x97, 0x27, 0x28, 0x88, 0xcd, 0xdd, 0x7e, 0x91, 0xf2, 0x28, 0xb1, 0xc4, 0xd7, 0x47, 0x4c, |
| 259 | 0x55, 0xb8, 0xfc, 0xd6, 0x18, 0xc4, 0xa9, 0x57, 0xbb, 0xdd, 0xd5, 0xad, 0x74, 0x07, 0xcc, 0x31, |
| 260 | 0x2d, 0x8d, 0x98, 0xa5, 0xca, 0xf7, 0xe0, 0x8f, 0x4a, 0x0d, 0x6b, 0x45, 0xbb, 0x41, 0xc6, 0x52, |
| 261 | 0x65, 0x9d, 0x5a, 0x5b, 0xa0, 0x5b, 0x66, 0x37, 0x37, 0xa8, 0x69, 0x62, 0x81, 0x86, 0x5b, 0xa2, |
| 262 | 0x0f, 0xbd, 0xd7, 0xf8, 0x51, 0xe6, 0xc5, 0x6e, 0x8c, 0xbe, 0x0d, 0xdb, 0xbf, 0x24, 0xdc, 0x03, |
| 263 | 0xb2, 0xd2, 0xcb, 0x4c, 0x3d, 0x54, 0x0f, 0xb0, 0xaf, 0x52, 0xe0, 0x34, 0xa2, 0xd0, 0x66, 0x98, |
| 264 | 0xb1, 0x28, 0xe5, 0xf1, 0x01, 0xe3, 0xb5, 0x1a, 0x34, 0xf8, 0xd8, 0xb4, 0xf8, 0x61, 0x81, 0x02, |
| 265 | 0x81, 0x81, 0x00, 0xde, 0x39, 0x2e, 0x18, 0xd6, 0x82, 0xc8, 0x29, 0x26, 0x6c, 0xc3, 0x45, 0x4e, |
| 266 | 0x1d, 0x61, 0x66, 0x24, 0x2f, 0x32, 0xd9, 0xa1, 0xd1, 0x05, 0x77, 0x75, 0x3e, 0x90, 0x4e, 0xa7, |
| 267 | 0xd0, 0x8b, 0xff, 0x84, 0x1b, 0xe5, 0xba, 0xc8, 0x2a, 0x16, 0x4c, 0x59, 0x70, 0x00, 0x70, 0x47, |
| 268 | 0xb8, 0xc5, 0x17, 0xdb, 0x8f, 0x8f, 0x84, 0xe3, 0x7b, 0xd5, 0x98, 0x85, 0x61, 0xbd, 0xf5, 0x03, |
| 269 | 0xd4, 0xdc, 0x2b, 0xdb, 0x38, 0xf8, 0x85, 0x43, 0x4a, 0xe4, 0x2c, 0x35, 0x5f, 0x72, 0x5c, 0x9a, |
| 270 | 0x60, 0xf9, 0x1f, 0x07, 0x88, 0xe1, 0xf1, 0xa9, 0x72, 0x23, 0xb5, 0x24, 0xb5, 0x35, 0x7f, 0xdf, |
| 271 | 0x72, 0xe2, 0xf6, 0x96, 0xba, 0xb7, 0xd7, 0x8e, 0x32, 0xbf, 0x92, 0xba, 0x8e, 0x18, 0x64, 0xea, |
| 272 | 0xb1, 0x22, 0x9e, 0x91, 0x34, 0x61, 0x30, 0x74, 0x8a, 0x6e, 0x3c, 0x12, 0x4f, 0x91, 0x49, 0xd7, |
| 273 | 0x1c, 0x74, 0x35, 0x02, 0x81, 0x81, 0x00, 0xc9, 0x53, 0x87, 0xc0, 0xf9, 0xd3, 0x5f, 0x13, 0x7b, |
| 274 | 0x57, 0xd0, 0xd6, 0x5c, 0x39, 0x7c, 0x5e, 0x21, 0xcc, 0x25, 0x1e, 0x47, 0x00, 0x8e, 0xd6, 0x2a, |
| 275 | 0x54, 0x24, 0x09, 0xc8, 0xb6, 0xb6, 0xac, 0x7f, 0x89, 0x67, 0xb3, 0x86, 0x3c, 0xa6, 0x45, 0xfc, |
| 276 | 0xce, 0x49, 0x58, 0x2a, 0x9a, 0xa1, 0x73, 0x49, 0xdb, 0x6c, 0x4a, 0x95, 0xaf, 0xfd, 0xae, 0x0d, |
| 277 | 0xae, 0x61, 0x2e, 0x1a, 0xfa, 0xc9, 0x9e, 0xd3, 0x9a, 0x2d, 0x93, 0x4c, 0x88, 0x04, 0x40, 0xae, |
| 278 | 0xd8, 0x83, 0x2f, 0x98, 0x43, 0x16, 0x3a, 0x47, 0xf2, 0x7f, 0x39, 0x21, 0x99, 0xdc, 0x12, 0x02, |
| 279 | 0xf9, 0xa0, 0xf9, 0xbd, 0x08, 0x30, 0x80, 0x07, 0xcb, 0x1e, 0x4e, 0x7f, 0x58, 0x30, 0x93, 0x66, |
| 280 | 0xa7, 0xde, 0x25, 0xf7, 0xc3, 0xc9, 0xb8, 0x80, 0x67, 0x7c, 0x06, 0x8e, 0x1b, 0xe9, 0x36, 0xe8, |
| 281 | 0x12, 0x88, 0x81, 0x52, 0x52, 0xa8, 0xa1, 0x02, 0x81, 0x80, 0x57, 0xff, 0x8c, 0xa1, 0x89, 0x50, |
| 282 | 0x80, 0xb2, 0xca, 0xe4, 0x86, 0xef, 0x0a, 0xdf, 0xd7, 0x91, 0xfb, 0x02, 0x35, 0xc0, 0xb8, 0xb3, |
| 283 | 0x6c, 0xd6, 0xc1, 0x36, 0xe5, 0x2e, 0x40, 0x85, 0xf4, 0xea, 0x5a, 0x06, 0x32, 0x12, 0xa4, 0xf1, |
| 284 | 0x05, 0xa3, 0x76, 0x47, 0x43, 0xe5, 0x32, 0x81, 0x98, 0x8a, 0xba, 0x07, 0x3f, 0x6e, 0x00, 0x27, |
| 285 | 0x29, 0x8e, 0x1c, 0x43, 0x78, 0x55, 0x6e, 0x0e, 0xfc, 0xa0, 0xe1, 0x4e, 0xce, 0x1a, 0xf7, 0x6a, |
| 286 | 0xd0, 0xb0, 0x30, 0xf2, 0x7a, 0xf6, 0xf0, 0xab, 0x35, 0xfb, 0x73, 0xa0, 0x60, 0xd8, 0xb1, 0xa0, |
| 287 | 0xe1, 0x42, 0xfa, 0x26, 0x47, 0xe9, 0x3b, 0x32, 0xe3, 0x6d, 0x82, 0x82, 0xae, 0x0a, 0x4d, 0xe5, |
| 288 | 0x0a, 0xb7, 0xaf, 0xe8, 0x55, 0x00, 0xa1, 0x6f, 0x43, 0xa6, 0x47, 0x19, 0xd6, 0xe2, 0xb9, 0x43, |
| 289 | 0x98, 0x23, 0x71, 0x9c, 0xd0, 0x8b, 0xcd, 0x03, 0x17, 0x81, 0x02, 0x81, 0x81, 0x00, 0xba, 0x73, |
| 290 | 0xb0, 0xbb, 0x28, 0xe3, 0xf8, 0x1e, 0x9b, 0xd1, 0xc5, 0x68, 0x71, 0x3b, 0x10, 0x12, 0x41, 0xac, |
| 291 | 0xc6, 0x07, 0x97, 0x6c, 0x4d, 0xdc, 0xcc, 0x90, 0xe6, 0x5b, 0x65, 0x56, 0xca, 0x31, 0x51, 0x60, |
| 292 | 0x58, 0xf9, 0x2b, 0x6e, 0x09, 0xf3, 0xb1, 0x60, 0xff, 0x0e, 0x37, 0x4e, 0xc4, 0x0d, 0x78, 0xae, |
| 293 | 0x4d, 0x49, 0x79, 0xfd, 0xe6, 0xac, 0x06, 0xa1, 0xa4, 0x00, 0xc6, 0x1d, 0xd3, 0x12, 0x54, 0x18, |
| 294 | 0x6a, 0xf3, 0x0b, 0x22, 0xc1, 0x05, 0x82, 0xa8, 0xa4, 0x3e, 0x34, 0xfe, 0x94, 0x9c, 0x5f, 0x3b, |
| 295 | 0x97, 0x55, 0xba, 0xe7, 0xba, 0xa7, 0xb7, 0xb7, 0xa6, 0xbd, 0x03, 0xb3, 0x8c, 0xef, 0x55, 0xc8, |
| 296 | 0x68, 0x85, 0xfc, 0x6c, 0x19, 0x78, 0xb9, 0xce, 0xe7, 0xef, 0x33, 0xda, 0x50, 0x7c, 0x9d, 0xf6, |
| 297 | 0xb9, 0x27, 0x7c, 0xff, 0x1e, 0x6a, 0xaa, 0x5d, 0x57, 0xac, 0xa5, 0x28, 0x46, 0x61, 0x02, 0x81, |
| 298 | 0x81, 0x00, 0xc9, 0x31, 0x61, 0x7c, 0x77, 0x82, 0x9d, 0xfb, 0x12, 0x70, 0x50, 0x2b, 0xe9, 0x19, |
| 299 | 0x5c, 0x8f, 0x28, 0x30, 0x88, 0x5f, 0x57, 0xdb, 0xa8, 0x69, 0x53, 0x68, 0x11, 0xe6, 0x86, 0x42, |
| 300 | 0x36, 0xd0, 0xc4, 0x73, 0x6a, 0x00, 0x08, 0xa1, 0x45, 0xaf, 0x36, 0xb8, 0x35, 0x7a, 0x7c, 0x3d, |
| 301 | 0x13, 0x99, 0x66, 0xd0, 0x4c, 0x4e, 0x00, 0x93, 0x4e, 0xa1, 0xae, 0xde, 0x3b, 0xb6, 0xb8, 0xec, |
| 302 | 0x84, 0x1d, 0xc9, 0x5e, 0x3f, 0x57, 0x97, 0x51, 0xe2, 0xbf, 0xdf, 0xe2, 0x7a, 0xe7, 0x78, 0x98, |
| 303 | 0x3f, 0x95, 0x93, 0x56, 0x21, 0x07, 0x23, 0x28, 0x7b, 0x0a, 0xff, 0xcc, 0x9f, 0x72, 0x70, 0x44, |
| 304 | 0xd4, 0x8c, 0x37, 0x3f, 0x1b, 0xab, 0xde, 0x07, 0x24, 0xfa, 0x17, 0xa4, 0xfd, 0x4d, 0xa0, 0x90, |
| 305 | 0x2c, 0x7c, 0x9b, 0x9b, 0xf2, 0x7b, 0xa6, 0x1b, 0xe6, 0xad, 0x02, 0xdf, 0xdd, 0xda, 0x8f, 0x4e, |
| 306 | 0x68, 0x22, |
| 307 | ]; |
| 308 | |
| 309 | /// WrappedKeyData as ASN.1 DER-encoded data corresponding to the `SecureKeyWrapper` schema |
| 310 | /// specified in IKeyMintDevice.aidl. Wrapped key parameters are - |
| 311 | /// Algorithm: AES |
| 312 | /// Key size: 256 |
| 313 | /// Block mode: ECB |
| 314 | /// Padding mode: PKCS7 |
| 315 | /// This sample wrapped_key is taken from KeyMint tests (see KeyMintTest.cpp). |
| 316 | pub static WRAPPED_KEY: &[u8] = &[ |
| 317 | 0x30, 0x82, 0x01, 0x79, 0x02, 0x01, 0x00, 0x04, 0x82, 0x01, 0x00, 0x93, 0x4b, 0xf9, 0x4e, 0x2a, |
| 318 | 0xa2, 0x8a, 0x3f, 0x83, 0xc9, 0xf7, 0x92, 0x97, 0x25, 0x02, 0x62, 0xfb, 0xe3, 0x27, 0x6b, 0x5a, |
| 319 | 0x1c, 0x91, 0x15, 0x9b, 0xbf, 0xa3, 0xef, 0x89, 0x57, 0xaa, 0xc8, 0x4b, 0x59, 0xb3, 0x0b, 0x45, |
| 320 | 0x5a, 0x79, 0xc2, 0x97, 0x34, 0x80, 0x82, 0x3d, 0x8b, 0x38, 0x63, 0xc3, 0xde, 0xef, 0x4a, 0x8e, |
| 321 | 0x24, 0x35, 0x90, 0x26, 0x8d, 0x80, 0xe1, 0x87, 0x51, 0xa0, 0xe1, 0x30, 0xf6, 0x7c, 0xe6, 0xa1, |
| 322 | 0xac, 0xe9, 0xf7, 0x9b, 0x95, 0xe0, 0x97, 0x47, 0x4f, 0xeb, 0xc9, 0x81, 0x19, 0x5b, 0x1d, 0x13, |
| 323 | 0xa6, 0x90, 0x86, 0xc0, 0x86, 0x3f, 0x66, 0xa7, 0xb7, 0xfd, 0xb4, 0x87, 0x92, 0x22, 0x7b, 0x1a, |
| 324 | 0xc5, 0xe2, 0x48, 0x9f, 0xeb, 0xdf, 0x08, 0x7a, 0xb5, 0x48, 0x64, 0x83, 0x03, 0x3a, 0x6f, 0x00, |
| 325 | 0x1c, 0xa5, 0xd1, 0xec, 0x1e, 0x27, 0xf5, 0xc3, 0x0f, 0x4c, 0xec, 0x26, 0x42, 0x07, 0x4a, 0x39, |
| 326 | 0xae, 0x68, 0xae, 0xe5, 0x52, 0xe1, 0x96, 0x62, 0x7a, 0x8e, 0x3d, 0x86, 0x7e, 0x67, 0xa8, 0xc0, |
| 327 | 0x1b, 0x11, 0xe7, 0x5f, 0x13, 0xcc, 0xa0, 0xa9, 0x7a, 0xb6, 0x68, 0xb5, 0x0c, 0xda, 0x07, 0xa8, |
| 328 | 0xec, 0xb7, 0xcd, 0x8e, 0x3d, 0xd7, 0x00, 0x9c, 0x96, 0x36, 0x53, 0x4f, 0x6f, 0x23, 0x9c, 0xff, |
| 329 | 0xe1, 0xfc, 0x8d, 0xaa, 0x46, 0x6f, 0x78, 0xb6, 0x76, 0xc7, 0x11, 0x9e, 0xfb, 0x96, 0xbc, 0xe4, |
| 330 | 0xe6, 0x9c, 0xa2, 0xa2, 0x5d, 0x0b, 0x34, 0xed, 0x9c, 0x3f, 0xf9, 0x99, 0xb8, 0x01, 0x59, 0x7d, |
| 331 | 0x52, 0x20, 0xe3, 0x07, 0xea, 0xa5, 0xbe, 0xe5, 0x07, 0xfb, 0x94, 0xd1, 0xfa, 0x69, 0xf9, 0xe5, |
| 332 | 0x19, 0xb2, 0xde, 0x31, 0x5b, 0xac, 0x92, 0xc3, 0x6f, 0x2e, 0xa1, 0xfa, 0x1d, 0xf4, 0x47, 0x8c, |
| 333 | 0x0d, 0xde, 0xde, 0xae, 0x8c, 0x70, 0xe0, 0x23, 0x3c, 0xd0, 0x98, 0x04, 0x0c, 0xd7, 0x96, 0xb0, |
| 334 | 0x2c, 0x37, 0x0f, 0x1f, 0xa4, 0xcc, 0x01, 0x24, 0xf1, 0x30, 0x2e, 0x02, 0x01, 0x03, 0x30, 0x29, |
| 335 | 0xa1, 0x08, 0x31, 0x06, 0x02, 0x01, 0x00, 0x02, 0x01, 0x01, 0xa2, 0x03, 0x02, 0x01, 0x20, 0xa3, |
| 336 | 0x04, 0x02, 0x02, 0x01, 0x00, 0xa4, 0x05, 0x31, 0x03, 0x02, 0x01, 0x01, 0xa6, 0x05, 0x31, 0x03, |
| 337 | 0x02, 0x01, 0x40, 0xbf, 0x83, 0x77, 0x02, 0x05, 0x00, 0x04, 0x20, 0xcc, 0xd5, 0x40, 0x85, 0x5f, |
| 338 | 0x83, 0x3a, 0x5e, 0x14, 0x80, 0xbf, 0xd2, 0xd3, 0x6f, 0xaf, 0x3a, 0xee, 0xe1, 0x5d, 0xf5, 0xbe, |
| 339 | 0xab, 0xe2, 0x69, 0x1b, 0xc8, 0x2d, 0xde, 0x2a, 0x7a, 0xa9, 0x10, 0x04, 0x10, 0x64, 0xc9, 0xf6, |
| 340 | 0x89, 0xc6, 0x0f, 0xf6, 0x22, 0x3a, 0xb6, 0xe6, 0x99, 0x9e, 0x0e, 0xb6, 0xe5, |
| 341 | ]; |
| 342 | |
Rajesh Nyamagoud | b881d51 | 2021-12-10 00:33:15 +0000 | [diff] [blame] | 343 | /// To map Keystore errors. |
| 344 | #[derive(thiserror::Error, Debug, Eq, PartialEq)] |
| 345 | pub enum Error { |
| 346 | /// Keystore2 error code |
| 347 | #[error("ResponseCode {0:?}")] |
| 348 | Rc(ResponseCode), |
| 349 | /// Keymint error code |
| 350 | #[error("ErrorCode {0:?}")] |
| 351 | Km(ErrorCode), |
| 352 | /// Exception |
| 353 | #[error("Binder exception {0:?}")] |
| 354 | Binder(ExceptionCode), |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 355 | /// This is returned if the C implementation of extractSubjectFromCertificate failed. |
| 356 | #[error("Failed to validate certificate chain.")] |
| 357 | ValidateCertChainFailed, |
Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +0000 | [diff] [blame] | 358 | /// Error code to indicate error in ASN.1 DER-encoded data creation. |
| 359 | #[error("Failed to create and encode ASN.1 data.")] |
| 360 | DerEncodeFailed, |
Rajesh Nyamagoud | 28abde6 | 2023-04-01 01:32:32 +0000 | [diff] [blame] | 361 | /// Error code to indicate error while using keystore-engine API. |
| 362 | #[error("Failed to perform crypto op using keystore-engine APIs.")] |
| 363 | Keystore2EngineOpFailed, |
Rajesh Nyamagoud | a42dee6 | 2022-04-22 21:15:55 +0000 | [diff] [blame] | 364 | /// Error code to indicate error in attestation-id validation. |
| 365 | #[error("Failed to validate attestation-id.")] |
| 366 | ValidateAttestIdFailed, |
| 367 | /// Error code to indicate error in getting value from attest record. |
| 368 | #[error("Failed to get value from attest record.")] |
| 369 | AttestRecordGetValueFailed, |
Rajesh Nyamagoud | b881d51 | 2021-12-10 00:33:15 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /// Keystore2 error mapping. |
| 373 | pub fn map_ks_error<T>(r: BinderResult<T>) -> Result<T, Error> { |
| 374 | r.map_err(|s| { |
| 375 | match s.exception_code() { |
| 376 | ExceptionCode::SERVICE_SPECIFIC => { |
| 377 | match s.service_specific_error() { |
| 378 | se if se < 0 => { |
| 379 | // Negative service specific errors are KM error codes. |
| 380 | Error::Km(ErrorCode(se)) |
| 381 | } |
| 382 | se => { |
| 383 | // Positive service specific errors are KS response codes. |
| 384 | Error::Rc(ResponseCode(se)) |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | // We create `Error::Binder` to preserve the exception code |
| 389 | // for logging. |
| 390 | e_code => Error::Binder(e_code), |
| 391 | } |
| 392 | }) |
| 393 | } |
| 394 | |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 395 | /// Get the value of the given system property, if the given system property doesn't exist |
| 396 | /// then returns an empty byte vector. |
| 397 | pub fn get_system_prop(name: &str) -> Vec<u8> { |
| 398 | match rustutils::system_properties::read(name) { |
| 399 | Ok(Some(value)) => value.as_bytes().to_vec(), |
| 400 | _ => vec![], |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /// Determines whether test is running on GSI. |
| 405 | pub fn is_gsi() -> bool { |
| 406 | // This file is only present on GSI builds. |
| 407 | PathBuf::from("/system/system_ext/etc/init/init.gsi.rc").as_path().is_file() |
| 408 | } |
| 409 | |
| 410 | /// Determines whether the test is on a GSI build where the rkp-only status of the device is |
| 411 | /// unknown. GSI replaces the values for remote_prov_prop properties (since they’re |
| 412 | /// system_internal_prop properties), so on GSI the properties are not reliable indicators of |
| 413 | /// whether StrongBox/TEE is RKP-only or not. |
| 414 | pub fn is_rkp_only_unknown_on_gsi(sec_level: SecurityLevel) -> bool { |
| 415 | if sec_level == SecurityLevel::TRUSTED_ENVIRONMENT { |
| 416 | is_gsi() && get_system_prop(TEE_KEYMINT_RKP_ONLY).is_empty() |
| 417 | } else { |
| 418 | is_gsi() && get_system_prop(STRONGBOX_KEYMINT_RKP_ONLY).is_empty() |
| 419 | } |
| 420 | } |
| 421 | |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 422 | /// Verify that given key param is listed in given authorizations list. |
| 423 | pub fn check_key_param(authorizations: &[Authorization], key_param: &KeyParameter) -> bool { |
| 424 | authorizations.iter().any(|auth| &auth.keyParameter == key_param) |
| 425 | } |
| 426 | |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 427 | /// Verify the given key authorizations with the expected authorizations. |
| 428 | pub fn check_key_authorizations( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 429 | sl: &SecLevel, |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 430 | authorizations: &[Authorization], |
| 431 | expected_params: &[KeyParameter], |
| 432 | expected_key_origin: KeyOrigin, |
| 433 | ) { |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 434 | // Make sure key authorizations contains only `ALLOWED_TAGS_IN_KEY_AUTHS` |
| 435 | authorizations.iter().all(|auth| { |
Rajesh Nyamagoud | 1923636 | 2024-07-10 20:52:28 +0000 | [diff] [blame] | 436 | // Ignore `INVALID` tag |
| 437 | if auth.keyParameter.tag == Tag::INVALID { |
Rajesh Nyamagoud | 7620921 | 2024-02-01 04:45:41 +0000 | [diff] [blame] | 438 | return true; |
| 439 | } |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 440 | assert!( |
| 441 | ALLOWED_TAGS_IN_KEY_AUTHS.contains(&auth.keyParameter.tag), |
| 442 | "key authorization is not allowed: {:#?}", |
| 443 | auth.keyParameter |
| 444 | ); |
| 445 | true |
| 446 | }); |
| 447 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 448 | // Check allowed-expected-key-parameters are present in given key authorizations list. |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 449 | expected_params.iter().all(|key_param| { |
Rajesh Nyamagoud | 17a9261 | 2023-10-17 16:15:05 +0000 | [diff] [blame] | 450 | // `INCLUDE_UNIQUE_ID` is not strictly expected to be in key authorizations but has been |
| 451 | // put there by some implementations so cope with that. |
| 452 | if key_param.tag == Tag::INCLUDE_UNIQUE_ID |
| 453 | && !authorizations.iter().any(|auth| auth.keyParameter.tag == key_param.tag) |
| 454 | { |
| 455 | return true; |
| 456 | } |
Rajesh Nyamagoud | 7620921 | 2024-02-01 04:45:41 +0000 | [diff] [blame] | 457 | |
David Drysdale | 8315359 | 2024-07-16 10:25:08 +0100 | [diff] [blame] | 458 | // `Tag::RSA_OAEP_MGF_DIGEST` was added in KeyMint 1.0, but the KeyMint VTS tests didn't |
| 459 | // originally check for its presence and so some implementations of early versions (< 3) of |
| 460 | // the KeyMint HAL don't include it (cf. b/297306437 and aosp/2758513). |
| 461 | // |
| 462 | // Given that Keymaster implementations will also omit this tag, skip the check for it |
| 463 | // altogether (and rely on the updated KeyMint VTS tests to ensure that up-level KeyMint |
| 464 | // implementations correctly populate this tag). |
| 465 | if matches!(key_param.tag, Tag::RSA_OAEP_MGF_DIGEST) { |
| 466 | return true; |
| 467 | } |
| 468 | |
Rajesh Nyamagoud | c63a83d | 2024-09-04 16:48:10 +0000 | [diff] [blame] | 469 | // Don't check these parameters if the underlying device is a Keymaster implementation. |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 470 | if sl.is_keymaster() { |
Rajesh Nyamagoud | c63a83d | 2024-09-04 16:48:10 +0000 | [diff] [blame] | 471 | if matches!( |
| 472 | key_param.tag, |
| 473 | // `Tag::USAGE_COUNT_LIMIT` was added in KeyMint 1.0. |
| 474 | Tag::USAGE_COUNT_LIMIT | |
| 475 | // Keymaster implementations may not consistently include `Tag::VENDOR_PATCHLEVEL` |
| 476 | // in generated key characteristics. |
| 477 | Tag::VENDOR_PATCHLEVEL |
| 478 | ) { |
Rajesh Nyamagoud | 7620921 | 2024-02-01 04:45:41 +0000 | [diff] [blame] | 479 | return true; |
| 480 | } |
Rajesh Nyamagoud | c63a83d | 2024-09-04 16:48:10 +0000 | [diff] [blame] | 481 | // `KeyPurpose::ATTEST_KEY` was added in KeyMint 1.0. |
Rajesh Nyamagoud | 7620921 | 2024-02-01 04:45:41 +0000 | [diff] [blame] | 482 | if key_param.tag == Tag::PURPOSE |
| 483 | && key_param.value == KeyParameterValue::KeyPurpose(KeyPurpose::ATTEST_KEY) |
| 484 | { |
| 485 | return true; |
| 486 | } |
| 487 | } |
| 488 | |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 489 | if ALLOWED_TAGS_IN_KEY_AUTHS.contains(&key_param.tag) { |
| 490 | assert!( |
| 491 | check_key_param(authorizations, key_param), |
| 492 | "Key parameter not found: {:#?}", |
| 493 | key_param |
| 494 | ); |
| 495 | } |
| 496 | true |
| 497 | }); |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 498 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 499 | check_common_auths(sl, authorizations, expected_key_origin); |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /// Verify common key authorizations. |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 503 | fn check_common_auths( |
| 504 | sl: &SecLevel, |
| 505 | authorizations: &[Authorization], |
| 506 | expected_key_origin: KeyOrigin, |
| 507 | ) { |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 508 | assert!(check_key_param( |
| 509 | authorizations, |
| 510 | &KeyParameter { |
| 511 | tag: Tag::OS_VERSION, |
| 512 | value: KeyParameterValue::Integer(get_os_version().try_into().unwrap()) |
| 513 | } |
| 514 | )); |
| 515 | assert!(check_key_param( |
| 516 | authorizations, |
| 517 | &KeyParameter { |
| 518 | tag: Tag::OS_PATCHLEVEL, |
| 519 | value: KeyParameterValue::Integer(get_os_patchlevel().try_into().unwrap()) |
| 520 | } |
| 521 | )); |
| 522 | |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 523 | assert!(check_key_param( |
| 524 | authorizations, |
| 525 | &KeyParameter { tag: Tag::ORIGIN, value: KeyParameterValue::Origin(expected_key_origin) } |
| 526 | )); |
| 527 | assert!(check_key_param( |
| 528 | authorizations, |
| 529 | &KeyParameter { |
| 530 | tag: Tag::USER_ID, |
| 531 | value: KeyParameterValue::Integer( |
| 532 | rustutils::users::multiuser_get_user_id(ThreadState::get_calling_uid()) |
| 533 | .try_into() |
| 534 | .unwrap() |
| 535 | ) |
| 536 | } |
| 537 | )); |
Rajesh Nyamagoud | 5f6db2f | 2023-06-01 17:22:32 +0000 | [diff] [blame] | 538 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 539 | if sl.is_keymint() { |
Rajesh Nyamagoud | 5f6db2f | 2023-06-01 17:22:32 +0000 | [diff] [blame] | 540 | assert!(authorizations |
| 541 | .iter() |
| 542 | .map(|auth| &auth.keyParameter) |
| 543 | .any(|key_param| key_param.tag == Tag::CREATION_DATETIME)); |
Rajesh Nyamagoud | fcae6fd | 2024-09-11 16:10:16 +0000 | [diff] [blame] | 544 | |
| 545 | // Access denied for finding vendor-patch-level ("ro.vendor.build.security_patch") property |
| 546 | // in a test running with `untrusted_app` context. Keeping this check to verify |
| 547 | // vendor-patch-level in tests running with `su` context. |
| 548 | if getuid().is_root() { |
| 549 | // Keymaster implementations may not consistently include `Tag::VENDOR_PATCHLEVEL` |
| 550 | // in generated key characteristics. So, checking this if the underlying device is a |
| 551 | // KeyMint implementation. |
| 552 | assert!(check_key_param( |
| 553 | authorizations, |
| 554 | &KeyParameter { |
| 555 | tag: Tag::VENDOR_PATCHLEVEL, |
| 556 | value: KeyParameterValue::Integer(get_vendor_patchlevel().try_into().unwrap()) |
| 557 | } |
| 558 | )); |
| 559 | } |
Rajesh Nyamagoud | 5f6db2f | 2023-06-01 17:22:32 +0000 | [diff] [blame] | 560 | } |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Rajesh Nyamagoud | f436a93 | 2023-05-12 01:16:07 +0000 | [diff] [blame] | 563 | /// Get the key `Authorization` for the given auth `Tag`. |
| 564 | pub fn get_key_auth(authorizations: &[Authorization], tag: Tag) -> Option<&Authorization> { |
| 565 | let auths: Vec<&Authorization> = |
| 566 | authorizations.iter().filter(|auth| auth.keyParameter.tag == tag).collect(); |
| 567 | |
| 568 | if !auths.is_empty() { |
| 569 | Some(auths[0]) |
| 570 | } else { |
| 571 | None |
| 572 | } |
| 573 | } |
| 574 | |
Rajesh Nyamagoud | b881d51 | 2021-12-10 00:33:15 +0000 | [diff] [blame] | 575 | /// Generate EC Key using given security level and domain with below key parameters and |
| 576 | /// optionally allow the generated key to be attested with factory provisioned attest key using |
| 577 | /// given challenge and application id - |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 578 | /// Purposes: SIGN and VERIFY |
| 579 | /// Digest: SHA_2_256 |
| 580 | /// Curve: P_256 |
Rajesh Nyamagoud | b881d51 | 2021-12-10 00:33:15 +0000 | [diff] [blame] | 581 | pub fn generate_ec_p256_signing_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 582 | sl: &SecLevel, |
Rajesh Nyamagoud | b881d51 | 2021-12-10 00:33:15 +0000 | [diff] [blame] | 583 | domain: Domain, |
| 584 | nspace: i64, |
| 585 | alias: Option<String>, |
| 586 | att_challenge: Option<&[u8]>, |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 587 | ) -> binder::Result<KeyMetadata> { |
Rajesh Nyamagoud | b881d51 | 2021-12-10 00:33:15 +0000 | [diff] [blame] | 588 | let mut key_attest = false; |
| 589 | let mut gen_params = AuthSetBuilder::new() |
Rajesh Nyamagoud | c7d064d | 2022-08-20 01:45:17 +0000 | [diff] [blame] | 590 | .no_auth_required() |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 591 | .algorithm(Algorithm::EC) |
| 592 | .purpose(KeyPurpose::SIGN) |
| 593 | .purpose(KeyPurpose::VERIFY) |
| 594 | .digest(Digest::SHA_2_256) |
Rajesh Nyamagoud | b881d51 | 2021-12-10 00:33:15 +0000 | [diff] [blame] | 595 | .ec_curve(EcCurve::P_256); |
| 596 | |
| 597 | if let Some(challenge) = att_challenge { |
| 598 | key_attest = true; |
| 599 | gen_params = gen_params.clone().attestation_challenge(challenge.to_vec()); |
| 600 | } |
| 601 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 602 | match sl.binder.generateKey( |
Rajesh Nyamagoud | b881d51 | 2021-12-10 00:33:15 +0000 | [diff] [blame] | 603 | &KeyDescriptor { domain, nspace, alias, blob: None }, |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 604 | None, |
| 605 | &gen_params, |
| 606 | 0, |
| 607 | b"entropy", |
| 608 | ) { |
| 609 | Ok(key_metadata) => { |
| 610 | assert!(key_metadata.certificate.is_some()); |
Rajesh Nyamagoud | b881d51 | 2021-12-10 00:33:15 +0000 | [diff] [blame] | 611 | if key_attest { |
| 612 | assert!(key_metadata.certificateChain.is_some()); |
| 613 | } |
| 614 | if domain == Domain::BLOB { |
| 615 | assert!(key_metadata.key.blob.is_some()); |
| 616 | } |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 617 | |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 618 | check_key_authorizations( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 619 | sl, |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 620 | &key_metadata.authorizations, |
| 621 | &gen_params, |
| 622 | KeyOrigin::GENERATED, |
| 623 | ); |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 624 | Ok(key_metadata) |
| 625 | } |
| 626 | Err(e) => Err(e), |
| 627 | } |
| 628 | } |
Rajesh Nyamagoud | a776645 | 2021-12-13 21:44:19 +0000 | [diff] [blame] | 629 | |
| 630 | /// Generate EC signing key. |
Rajesh Nyamagoud | c7d064d | 2022-08-20 01:45:17 +0000 | [diff] [blame] | 631 | pub fn generate_ec_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 632 | sl: &SecLevel, |
Rajesh Nyamagoud | a776645 | 2021-12-13 21:44:19 +0000 | [diff] [blame] | 633 | domain: Domain, |
| 634 | nspace: i64, |
| 635 | alias: Option<String>, |
| 636 | ec_curve: EcCurve, |
| 637 | digest: Digest, |
| 638 | ) -> binder::Result<KeyMetadata> { |
| 639 | let gen_params = AuthSetBuilder::new() |
| 640 | .no_auth_required() |
| 641 | .algorithm(Algorithm::EC) |
| 642 | .purpose(KeyPurpose::SIGN) |
| 643 | .purpose(KeyPurpose::VERIFY) |
| 644 | .digest(digest) |
| 645 | .ec_curve(ec_curve); |
| 646 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 647 | let key_metadata = sl.binder.generateKey( |
Rajesh Nyamagoud | a776645 | 2021-12-13 21:44:19 +0000 | [diff] [blame] | 648 | &KeyDescriptor { domain, nspace, alias, blob: None }, |
| 649 | None, |
| 650 | &gen_params, |
| 651 | 0, |
| 652 | b"entropy", |
| 653 | )?; |
| 654 | |
| 655 | // Must have a public key. |
| 656 | assert!(key_metadata.certificate.is_some()); |
| 657 | |
| 658 | // Should not have an attestation record. |
| 659 | assert!(key_metadata.certificateChain.is_none()); |
| 660 | |
| 661 | if domain == Domain::BLOB { |
| 662 | assert!(key_metadata.key.blob.is_some()); |
| 663 | } else { |
| 664 | assert!(key_metadata.key.blob.is_none()); |
| 665 | } |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 666 | check_key_authorizations(sl, &key_metadata.authorizations, &gen_params, KeyOrigin::GENERATED); |
Rajesh Nyamagoud | a776645 | 2021-12-13 21:44:19 +0000 | [diff] [blame] | 667 | Ok(key_metadata) |
| 668 | } |
Rajesh Nyamagoud | 11912ea | 2021-12-20 20:37:20 +0000 | [diff] [blame] | 669 | |
| 670 | /// Generate a RSA key with the given key parameters, alias, domain and namespace. |
| 671 | pub fn generate_rsa_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 672 | sl: &SecLevel, |
Rajesh Nyamagoud | 11912ea | 2021-12-20 20:37:20 +0000 | [diff] [blame] | 673 | domain: Domain, |
| 674 | nspace: i64, |
| 675 | alias: Option<String>, |
| 676 | key_params: &KeyParams, |
| 677 | attest_key: Option<&KeyDescriptor>, |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 678 | ) -> binder::Result<Option<KeyMetadata>> { |
Rajesh Nyamagoud | 11912ea | 2021-12-20 20:37:20 +0000 | [diff] [blame] | 679 | let mut gen_params = AuthSetBuilder::new() |
| 680 | .no_auth_required() |
| 681 | .algorithm(Algorithm::RSA) |
| 682 | .rsa_public_exponent(65537) |
| 683 | .key_size(key_params.key_size); |
| 684 | |
| 685 | for purpose in &key_params.purpose { |
| 686 | gen_params = gen_params.purpose(*purpose); |
| 687 | } |
| 688 | if let Some(value) = key_params.digest { |
| 689 | gen_params = gen_params.digest(value) |
| 690 | } |
| 691 | if let Some(value) = key_params.padding { |
| 692 | gen_params = gen_params.padding_mode(value); |
| 693 | } |
| 694 | if let Some(value) = key_params.mgf_digest { |
| 695 | gen_params = gen_params.mgf_digest(value); |
| 696 | } |
| 697 | if let Some(value) = key_params.block_mode { |
| 698 | gen_params = gen_params.block_mode(value) |
| 699 | } |
| 700 | if let Some(value) = &key_params.att_challenge { |
| 701 | gen_params = gen_params.attestation_challenge(value.to_vec()) |
| 702 | } |
Rajesh Nyamagoud | 11912ea | 2021-12-20 20:37:20 +0000 | [diff] [blame] | 703 | |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 704 | let key_metadata = match sl.binder.generateKey( |
Rajesh Nyamagoud | 11912ea | 2021-12-20 20:37:20 +0000 | [diff] [blame] | 705 | &KeyDescriptor { domain, nspace, alias, blob: None }, |
| 706 | attest_key, |
| 707 | &gen_params, |
| 708 | 0, |
| 709 | b"entropy", |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 710 | ) { |
| 711 | Ok(metadata) => metadata, |
| 712 | Err(e) => { |
| 713 | return if is_rkp_only_unknown_on_gsi(sl.level) |
| 714 | && e.service_specific_error() == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED.0 |
| 715 | { |
| 716 | // GSI replaces the values for remote_prov_prop properties (since they’re |
| 717 | // system_internal_prop properties), so on GSI the properties are not |
| 718 | // reliable indicators of whether StrongBox/TEE are RKP-only or not. |
| 719 | // Test can be skipped if it generates a key with attestation but doesn't provide |
| 720 | // an ATTEST_KEY and rkp-only property is undetermined. |
| 721 | Ok(None) |
| 722 | } else { |
| 723 | Err(e) |
| 724 | }; |
| 725 | } |
| 726 | }; |
Rajesh Nyamagoud | 11912ea | 2021-12-20 20:37:20 +0000 | [diff] [blame] | 727 | |
| 728 | // Must have a public key. |
| 729 | assert!(key_metadata.certificate.is_some()); |
| 730 | |
David Drysdale | 38f2ca3 | 2023-01-10 13:10:51 +0000 | [diff] [blame] | 731 | if attest_key.is_none() && key_params.att_challenge.is_some() { |
Rajesh Nyamagoud | 11912ea | 2021-12-20 20:37:20 +0000 | [diff] [blame] | 732 | // Should have an attestation record. |
| 733 | assert!(key_metadata.certificateChain.is_some()); |
| 734 | } else { |
| 735 | // Should not have an attestation record. |
| 736 | assert!(key_metadata.certificateChain.is_none()); |
| 737 | } |
| 738 | |
| 739 | assert!( |
| 740 | (domain == Domain::BLOB && key_metadata.key.blob.is_some()) |
| 741 | || key_metadata.key.blob.is_none() |
| 742 | ); |
| 743 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 744 | check_key_authorizations(sl, &key_metadata.authorizations, &gen_params, KeyOrigin::GENERATED); |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 745 | // If `RSA_OAEP_MGF_DIGEST` tag is not mentioned explicitly while generating/importing a key, |
| 746 | // then make sure `RSA_OAEP_MGF_DIGEST` tag with default value (SHA1) must not be included in |
| 747 | // key authorization list. |
| 748 | if key_params.mgf_digest.is_none() { |
| 749 | assert!(!check_key_param( |
| 750 | &key_metadata.authorizations, |
| 751 | &KeyParameter { |
| 752 | tag: Tag::RSA_OAEP_MGF_DIGEST, |
| 753 | value: KeyParameterValue::Digest(Digest::SHA1) |
| 754 | } |
| 755 | )); |
| 756 | } |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 757 | Ok(Some(key_metadata)) |
Rajesh Nyamagoud | 11912ea | 2021-12-20 20:37:20 +0000 | [diff] [blame] | 758 | } |
Rajesh Nyamagoud | 4740993 | 2022-01-08 00:37:13 +0000 | [diff] [blame] | 759 | |
Rajesh Nyamagoud | c3523ba | 2022-08-05 17:38:25 +0000 | [diff] [blame] | 760 | /// Generate AES/3DES key. |
| 761 | pub fn generate_sym_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 762 | sl: &SecLevel, |
Rajesh Nyamagoud | c3523ba | 2022-08-05 17:38:25 +0000 | [diff] [blame] | 763 | algorithm: Algorithm, |
Rajesh Nyamagoud | 4740993 | 2022-01-08 00:37:13 +0000 | [diff] [blame] | 764 | size: i32, |
| 765 | alias: &str, |
| 766 | padding_mode: &PaddingMode, |
| 767 | block_mode: &BlockMode, |
| 768 | min_mac_len: Option<i32>, |
| 769 | ) -> binder::Result<KeyMetadata> { |
| 770 | let mut gen_params = AuthSetBuilder::new() |
| 771 | .no_auth_required() |
Rajesh Nyamagoud | c3523ba | 2022-08-05 17:38:25 +0000 | [diff] [blame] | 772 | .algorithm(algorithm) |
Rajesh Nyamagoud | 4740993 | 2022-01-08 00:37:13 +0000 | [diff] [blame] | 773 | .purpose(KeyPurpose::ENCRYPT) |
| 774 | .purpose(KeyPurpose::DECRYPT) |
| 775 | .key_size(size) |
| 776 | .padding_mode(*padding_mode) |
| 777 | .block_mode(*block_mode); |
| 778 | |
| 779 | if let Some(val) = min_mac_len { |
| 780 | gen_params = gen_params.min_mac_length(val); |
| 781 | } |
| 782 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 783 | let key_metadata = sl.binder.generateKey( |
Rajesh Nyamagoud | 4740993 | 2022-01-08 00:37:13 +0000 | [diff] [blame] | 784 | &KeyDescriptor { |
| 785 | domain: Domain::APP, |
| 786 | nspace: -1, |
| 787 | alias: Some(alias.to_string()), |
| 788 | blob: None, |
| 789 | }, |
| 790 | None, |
| 791 | &gen_params, |
| 792 | 0, |
| 793 | b"entropy", |
| 794 | )?; |
| 795 | |
| 796 | // Should not have public certificate. |
| 797 | assert!(key_metadata.certificate.is_none()); |
| 798 | |
| 799 | // Should not have an attestation record. |
| 800 | assert!(key_metadata.certificateChain.is_none()); |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 801 | check_key_authorizations(sl, &key_metadata.authorizations, &gen_params, KeyOrigin::GENERATED); |
Rajesh Nyamagoud | 4740993 | 2022-01-08 00:37:13 +0000 | [diff] [blame] | 802 | Ok(key_metadata) |
| 803 | } |
Rajesh Nyamagoud | 4c6193c | 2022-02-03 01:15:34 +0000 | [diff] [blame] | 804 | |
| 805 | /// Generate HMAC key. |
| 806 | pub fn generate_hmac_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 807 | sl: &SecLevel, |
Rajesh Nyamagoud | 4c6193c | 2022-02-03 01:15:34 +0000 | [diff] [blame] | 808 | alias: &str, |
| 809 | key_size: i32, |
| 810 | min_mac_len: i32, |
| 811 | digest: Digest, |
| 812 | ) -> binder::Result<KeyMetadata> { |
| 813 | let gen_params = AuthSetBuilder::new() |
| 814 | .no_auth_required() |
| 815 | .algorithm(Algorithm::HMAC) |
| 816 | .purpose(KeyPurpose::SIGN) |
| 817 | .purpose(KeyPurpose::VERIFY) |
| 818 | .key_size(key_size) |
| 819 | .min_mac_length(min_mac_len) |
| 820 | .digest(digest); |
| 821 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 822 | let key_metadata = sl.binder.generateKey( |
Rajesh Nyamagoud | 4c6193c | 2022-02-03 01:15:34 +0000 | [diff] [blame] | 823 | &KeyDescriptor { |
| 824 | domain: Domain::APP, |
| 825 | nspace: -1, |
| 826 | alias: Some(alias.to_string()), |
| 827 | blob: None, |
| 828 | }, |
| 829 | None, |
| 830 | &gen_params, |
| 831 | 0, |
| 832 | b"entropy", |
| 833 | )?; |
| 834 | |
| 835 | // Should not have public certificate. |
| 836 | assert!(key_metadata.certificate.is_none()); |
| 837 | |
| 838 | // Should not have an attestation record. |
| 839 | assert!(key_metadata.certificateChain.is_none()); |
| 840 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 841 | check_key_authorizations(sl, &key_metadata.authorizations, &gen_params, KeyOrigin::GENERATED); |
Rajesh Nyamagoud | 4c6193c | 2022-02-03 01:15:34 +0000 | [diff] [blame] | 842 | Ok(key_metadata) |
| 843 | } |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 844 | |
| 845 | /// Generate RSA or EC attestation keys using below parameters - |
| 846 | /// Purpose: ATTEST_KEY |
| 847 | /// Digest: Digest::SHA_2_256 |
| 848 | /// Padding: PaddingMode::RSA_PKCS1_1_5_SIGN |
| 849 | /// RSA-Key-Size: 2048 |
| 850 | /// EC-Curve: EcCurve::P_256 |
| 851 | pub fn generate_attestation_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 852 | sl: &SecLevel, |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 853 | algorithm: Algorithm, |
| 854 | att_challenge: &[u8], |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 855 | ) -> binder::Result<Option<KeyMetadata>> { |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 856 | assert!(algorithm == Algorithm::RSA || algorithm == Algorithm::EC); |
| 857 | |
| 858 | if algorithm == Algorithm::RSA { |
| 859 | let alias = "ks_rsa_attest_test_key"; |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 860 | generate_rsa_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 861 | sl, |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 862 | Domain::APP, |
| 863 | -1, |
| 864 | Some(alias.to_string()), |
| 865 | &KeyParams { |
| 866 | key_size: 2048, |
| 867 | purpose: vec![KeyPurpose::ATTEST_KEY], |
| 868 | padding: Some(PaddingMode::RSA_PKCS1_1_5_SIGN), |
| 869 | digest: Some(Digest::SHA_2_256), |
| 870 | mgf_digest: None, |
| 871 | block_mode: None, |
| 872 | att_challenge: Some(att_challenge.to_vec()), |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 873 | }, |
| 874 | None, |
| 875 | ) |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 876 | } else { |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 877 | generate_ec_attestation_key(sl, att_challenge, Digest::SHA_2_256, EcCurve::P_256) |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
| 881 | /// Generate EC attestation key with the given |
| 882 | /// curve, attestation-challenge and attestation-app-id. |
| 883 | pub fn generate_ec_attestation_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 884 | sl: &SecLevel, |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 885 | att_challenge: &[u8], |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 886 | digest: Digest, |
| 887 | ec_curve: EcCurve, |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 888 | ) -> binder::Result<Option<KeyMetadata>> { |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 889 | let alias = "ks_attest_ec_test_key"; |
| 890 | let gen_params = AuthSetBuilder::new() |
| 891 | .no_auth_required() |
| 892 | .algorithm(Algorithm::EC) |
| 893 | .purpose(KeyPurpose::ATTEST_KEY) |
| 894 | .ec_curve(ec_curve) |
| 895 | .digest(digest) |
David Drysdale | 38f2ca3 | 2023-01-10 13:10:51 +0000 | [diff] [blame] | 896 | .attestation_challenge(att_challenge.to_vec()); |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 897 | |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 898 | let attestation_key_metadata = match sl.binder.generateKey( |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 899 | &KeyDescriptor { |
| 900 | domain: Domain::APP, |
| 901 | nspace: -1, |
| 902 | alias: Some(alias.to_string()), |
| 903 | blob: None, |
| 904 | }, |
| 905 | None, |
| 906 | &gen_params, |
| 907 | 0, |
| 908 | b"entropy", |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 909 | ) { |
| 910 | Ok(metadata) => metadata, |
| 911 | Err(e) => { |
| 912 | return if is_rkp_only_unknown_on_gsi(sl.level) |
| 913 | && e.service_specific_error() == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED.0 |
| 914 | { |
| 915 | // GSI replaces the values for remote_prov_prop properties (since they’re |
| 916 | // system_internal_prop properties), so on GSI the properties are not |
| 917 | // reliable indicators of whether StrongBox/TEE are RKP-only or not. |
| 918 | // Test can be skipped if it generates a key with attestation but doesn't provide |
| 919 | // an ATTEST_KEY and rkp-only property is undetermined. |
| 920 | Ok(None) |
| 921 | } else { |
| 922 | Err(e) |
| 923 | }; |
| 924 | } |
| 925 | }; |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 926 | |
| 927 | // Should have public certificate. |
| 928 | assert!(attestation_key_metadata.certificate.is_some()); |
| 929 | // Should have an attestation record. |
| 930 | assert!(attestation_key_metadata.certificateChain.is_some()); |
| 931 | |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 932 | check_key_authorizations( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 933 | sl, |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 934 | &attestation_key_metadata.authorizations, |
| 935 | &gen_params, |
| 936 | KeyOrigin::GENERATED, |
| 937 | ); |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 938 | Ok(Some(attestation_key_metadata)) |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /// Generate EC-P-256 key and attest it with given attestation key. |
| 942 | pub fn generate_ec_256_attested_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 943 | sl: &SecLevel, |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 944 | alias: Option<String>, |
| 945 | att_challenge: &[u8], |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 946 | attest_key: &KeyDescriptor, |
| 947 | ) -> binder::Result<KeyMetadata> { |
| 948 | let ec_gen_params = AuthSetBuilder::new() |
| 949 | .no_auth_required() |
| 950 | .algorithm(Algorithm::EC) |
| 951 | .purpose(KeyPurpose::SIGN) |
| 952 | .purpose(KeyPurpose::VERIFY) |
| 953 | .digest(Digest::SHA_2_256) |
| 954 | .ec_curve(EcCurve::P_256) |
David Drysdale | 38f2ca3 | 2023-01-10 13:10:51 +0000 | [diff] [blame] | 955 | .attestation_challenge(att_challenge.to_vec()); |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 956 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 957 | let ec_key_metadata = sl |
| 958 | .binder |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 959 | .generateKey( |
| 960 | &KeyDescriptor { domain: Domain::APP, nspace: -1, alias, blob: None }, |
| 961 | Some(attest_key), |
| 962 | &ec_gen_params, |
| 963 | 0, |
| 964 | b"entropy", |
| 965 | ) |
| 966 | .unwrap(); |
| 967 | |
| 968 | // Should have public certificate. |
| 969 | assert!(ec_key_metadata.certificate.is_some()); |
| 970 | // Shouldn't have an attestation record. |
| 971 | assert!(ec_key_metadata.certificateChain.is_none()); |
| 972 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 973 | check_key_authorizations( |
| 974 | sl, |
| 975 | &ec_key_metadata.authorizations, |
| 976 | &ec_gen_params, |
| 977 | KeyOrigin::GENERATED, |
| 978 | ); |
Rajesh Nyamagoud | 4d48337 | 2022-02-09 01:38:23 +0000 | [diff] [blame] | 979 | Ok(ec_key_metadata) |
| 980 | } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 981 | |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 982 | /// Imports above defined RSA key - `RSA_2048_KEY` and validates imported key parameters. |
| 983 | pub fn import_rsa_2048_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 984 | sl: &SecLevel, |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 985 | domain: Domain, |
| 986 | nspace: i64, |
| 987 | alias: Option<String>, |
| 988 | import_params: AuthSetBuilder, |
| 989 | ) -> binder::Result<KeyMetadata> { |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 990 | let key_metadata = sl |
| 991 | .binder |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 992 | .importKey( |
| 993 | &KeyDescriptor { domain, nspace, alias, blob: None }, |
| 994 | None, |
| 995 | &import_params, |
| 996 | 0, |
| 997 | RSA_2048_KEY, |
| 998 | ) |
| 999 | .unwrap(); |
| 1000 | |
| 1001 | assert!(key_metadata.certificate.is_some()); |
| 1002 | assert!(key_metadata.certificateChain.is_none()); |
| 1003 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1004 | check_key_authorizations(sl, &key_metadata.authorizations, &import_params, KeyOrigin::IMPORTED); |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1005 | |
| 1006 | // Check below auths explicitly, they might not be addd in import parameters. |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1007 | assert!(check_key_param( |
| 1008 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1009 | &KeyParameter { tag: Tag::ALGORITHM, value: KeyParameterValue::Algorithm(Algorithm::RSA) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1010 | )); |
| 1011 | |
| 1012 | assert!(check_key_param( |
| 1013 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1014 | &KeyParameter { tag: Tag::KEY_SIZE, value: KeyParameterValue::Integer(2048) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1015 | )); |
| 1016 | |
| 1017 | assert!(check_key_param( |
| 1018 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1019 | &KeyParameter { tag: Tag::DIGEST, value: KeyParameterValue::Digest(Digest::SHA_2_256) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1020 | )); |
| 1021 | |
| 1022 | assert!(check_key_param( |
| 1023 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1024 | &KeyParameter { |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1025 | tag: Tag::RSA_PUBLIC_EXPONENT, |
| 1026 | value: KeyParameterValue::LongInteger(65537) |
| 1027 | } |
| 1028 | )); |
| 1029 | |
| 1030 | assert!(check_key_param( |
| 1031 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1032 | &KeyParameter { |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1033 | tag: Tag::PADDING, |
| 1034 | value: KeyParameterValue::PaddingMode(PaddingMode::RSA_PSS) |
| 1035 | } |
| 1036 | )); |
| 1037 | |
| 1038 | assert!(check_key_param( |
| 1039 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1040 | &KeyParameter { tag: Tag::ORIGIN, value: KeyParameterValue::Origin(KeyOrigin::IMPORTED) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1041 | )); |
| 1042 | |
| 1043 | Ok(key_metadata) |
| 1044 | } |
| 1045 | |
| 1046 | /// Imports above defined EC key - `EC_P_256_KEY` and validates imported key parameters. |
| 1047 | pub fn import_ec_p_256_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1048 | sl: &SecLevel, |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1049 | domain: Domain, |
| 1050 | nspace: i64, |
| 1051 | alias: Option<String>, |
| 1052 | import_params: AuthSetBuilder, |
| 1053 | ) -> binder::Result<KeyMetadata> { |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1054 | let key_metadata = sl |
| 1055 | .binder |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1056 | .importKey( |
| 1057 | &KeyDescriptor { domain, nspace, alias, blob: None }, |
| 1058 | None, |
| 1059 | &import_params, |
| 1060 | 0, |
| 1061 | EC_P_256_KEY, |
| 1062 | ) |
| 1063 | .unwrap(); |
| 1064 | |
| 1065 | assert!(key_metadata.certificate.is_some()); |
| 1066 | assert!(key_metadata.certificateChain.is_none()); |
| 1067 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1068 | check_key_authorizations(sl, &key_metadata.authorizations, &import_params, KeyOrigin::IMPORTED); |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1069 | |
| 1070 | // Check below auths explicitly, they might not be addd in import parameters. |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1071 | assert!(check_key_param( |
| 1072 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1073 | &KeyParameter { tag: Tag::ALGORITHM, value: KeyParameterValue::Algorithm(Algorithm::EC) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1074 | )); |
| 1075 | |
| 1076 | assert!(check_key_param( |
| 1077 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1078 | &KeyParameter { tag: Tag::EC_CURVE, value: KeyParameterValue::EcCurve(EcCurve::P_256) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1079 | )); |
| 1080 | |
| 1081 | assert!(check_key_param( |
| 1082 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1083 | &KeyParameter { tag: Tag::DIGEST, value: KeyParameterValue::Digest(Digest::SHA_2_256) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1084 | )); |
| 1085 | assert!(check_key_param( |
| 1086 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1087 | &KeyParameter { tag: Tag::ORIGIN, value: KeyParameterValue::Origin(KeyOrigin::IMPORTED) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1088 | )); |
| 1089 | |
| 1090 | Ok(key_metadata) |
| 1091 | } |
| 1092 | |
| 1093 | /// Import sample AES key and validate its key parameters. |
| 1094 | pub fn import_aes_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1095 | sl: &SecLevel, |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1096 | domain: Domain, |
| 1097 | nspace: i64, |
| 1098 | alias: Option<String>, |
| 1099 | ) -> binder::Result<KeyMetadata> { |
| 1100 | static AES_KEY: &[u8] = &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; |
| 1101 | let key_size = AES_KEY.len() * 8; |
| 1102 | |
| 1103 | let import_params = AuthSetBuilder::new() |
| 1104 | .no_auth_required() |
| 1105 | .algorithm(Algorithm::AES) |
| 1106 | .block_mode(BlockMode::ECB) |
| 1107 | .key_size(key_size.try_into().unwrap()) |
| 1108 | .purpose(KeyPurpose::ENCRYPT) |
| 1109 | .purpose(KeyPurpose::DECRYPT) |
| 1110 | .padding_mode(PaddingMode::PKCS7); |
| 1111 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1112 | let key_metadata = sl.binder.importKey( |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1113 | &KeyDescriptor { domain, nspace, alias, blob: None }, |
| 1114 | None, |
| 1115 | &import_params, |
| 1116 | 0, |
| 1117 | AES_KEY, |
| 1118 | )?; |
| 1119 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1120 | check_key_authorizations(sl, &key_metadata.authorizations, &import_params, KeyOrigin::IMPORTED); |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1121 | |
| 1122 | // Check below auths explicitly, they might not be addd in import parameters. |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1123 | assert!(check_key_param( |
| 1124 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1125 | &KeyParameter { tag: Tag::ALGORITHM, value: KeyParameterValue::Algorithm(Algorithm::AES) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1126 | )); |
| 1127 | assert!(check_key_param( |
| 1128 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1129 | &KeyParameter { tag: Tag::KEY_SIZE, value: KeyParameterValue::Integer(128) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1130 | )); |
| 1131 | assert!(check_key_param( |
| 1132 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1133 | &KeyParameter { |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1134 | tag: Tag::PADDING, |
| 1135 | value: KeyParameterValue::PaddingMode(PaddingMode::PKCS7) |
| 1136 | } |
| 1137 | )); |
| 1138 | assert!(check_key_param( |
| 1139 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1140 | &KeyParameter { tag: Tag::BLOCK_MODE, value: KeyParameterValue::BlockMode(BlockMode::ECB) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1141 | )); |
| 1142 | assert!(check_key_param( |
| 1143 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1144 | &KeyParameter { tag: Tag::ORIGIN, value: KeyParameterValue::Origin(KeyOrigin::IMPORTED) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1145 | )); |
| 1146 | |
| 1147 | Ok(key_metadata) |
| 1148 | } |
| 1149 | |
| 1150 | /// Import sample 3DES key and validate its key parameters. |
| 1151 | pub fn import_3des_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1152 | sl: &SecLevel, |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1153 | domain: Domain, |
| 1154 | nspace: i64, |
| 1155 | alias: Option<String>, |
| 1156 | ) -> binder::Result<KeyMetadata> { |
| 1157 | static TRIPLE_DES_KEY: &[u8] = &[ |
| 1158 | 0xa4, 0x9d, 0x75, 0x64, 0x19, 0x9e, 0x97, 0xcb, 0x52, 0x9d, 0x2c, 0x9d, 0x97, 0xbf, 0x2f, |
| 1159 | 0x98, 0xd3, 0x5e, 0xdf, 0x57, 0xba, 0x1f, 0x73, 0x58, |
| 1160 | ]; |
| 1161 | |
| 1162 | let import_params = AuthSetBuilder::new() |
| 1163 | .no_auth_required() |
| 1164 | .algorithm(Algorithm::TRIPLE_DES) |
| 1165 | .block_mode(BlockMode::ECB) |
| 1166 | .key_size(168) |
| 1167 | .purpose(KeyPurpose::ENCRYPT) |
| 1168 | .purpose(KeyPurpose::DECRYPT) |
| 1169 | .padding_mode(PaddingMode::PKCS7); |
| 1170 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1171 | let key_metadata = sl.binder.importKey( |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1172 | &KeyDescriptor { domain, nspace, alias, blob: None }, |
| 1173 | None, |
| 1174 | &import_params, |
| 1175 | 0, |
| 1176 | TRIPLE_DES_KEY, |
| 1177 | )?; |
| 1178 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1179 | check_key_authorizations(sl, &key_metadata.authorizations, &import_params, KeyOrigin::IMPORTED); |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1180 | |
| 1181 | // Check below auths explicitly, they might not be addd in import parameters. |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1182 | assert!(check_key_param( |
| 1183 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1184 | &KeyParameter { |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1185 | tag: Tag::ALGORITHM, |
| 1186 | value: KeyParameterValue::Algorithm(Algorithm::TRIPLE_DES) |
| 1187 | } |
| 1188 | )); |
| 1189 | assert!(check_key_param( |
| 1190 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1191 | &KeyParameter { tag: Tag::KEY_SIZE, value: KeyParameterValue::Integer(168) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1192 | )); |
| 1193 | assert!(check_key_param( |
| 1194 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1195 | &KeyParameter { |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1196 | tag: Tag::PADDING, |
| 1197 | value: KeyParameterValue::PaddingMode(PaddingMode::PKCS7) |
| 1198 | } |
| 1199 | )); |
| 1200 | assert!(check_key_param( |
| 1201 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1202 | &KeyParameter { tag: Tag::BLOCK_MODE, value: KeyParameterValue::BlockMode(BlockMode::ECB) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1203 | )); |
| 1204 | assert!(check_key_param( |
| 1205 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1206 | &KeyParameter { tag: Tag::ORIGIN, value: KeyParameterValue::Origin(KeyOrigin::IMPORTED) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1207 | )); |
| 1208 | |
| 1209 | Ok(key_metadata) |
| 1210 | } |
| 1211 | |
| 1212 | /// Import sample HMAC key and validate its key parameters. |
| 1213 | pub fn import_hmac_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1214 | sl: &SecLevel, |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1215 | domain: Domain, |
| 1216 | nspace: i64, |
| 1217 | alias: Option<String>, |
| 1218 | ) -> binder::Result<KeyMetadata> { |
| 1219 | static HMAC_KEY: &[u8] = &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; |
| 1220 | let key_size = HMAC_KEY.len() * 8; |
| 1221 | |
| 1222 | let import_params = AuthSetBuilder::new() |
| 1223 | .no_auth_required() |
| 1224 | .algorithm(Algorithm::HMAC) |
| 1225 | .key_size(key_size.try_into().unwrap()) |
| 1226 | .purpose(KeyPurpose::SIGN) |
| 1227 | .purpose(KeyPurpose::VERIFY) |
| 1228 | .digest(Digest::SHA_2_256) |
| 1229 | .min_mac_length(256); |
| 1230 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1231 | let key_metadata = sl.binder.importKey( |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1232 | &KeyDescriptor { domain, nspace, alias, blob: None }, |
| 1233 | None, |
| 1234 | &import_params, |
| 1235 | 0, |
| 1236 | HMAC_KEY, |
| 1237 | )?; |
| 1238 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1239 | check_key_authorizations(sl, &key_metadata.authorizations, &import_params, KeyOrigin::IMPORTED); |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1240 | |
| 1241 | // Check below auths explicitly, they might not be addd in import parameters. |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1242 | assert!(check_key_param( |
| 1243 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1244 | &KeyParameter { tag: Tag::ALGORITHM, value: KeyParameterValue::Algorithm(Algorithm::HMAC) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1245 | )); |
| 1246 | assert!(check_key_param( |
| 1247 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1248 | &KeyParameter { tag: Tag::KEY_SIZE, value: KeyParameterValue::Integer(128) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1249 | )); |
| 1250 | assert!(check_key_param( |
| 1251 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1252 | &KeyParameter { tag: Tag::DIGEST, value: KeyParameterValue::Digest(Digest::SHA_2_256) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1253 | )); |
| 1254 | assert!(check_key_param( |
| 1255 | &key_metadata.authorizations, |
Rajesh Nyamagoud | d0213ef | 2023-05-04 00:29:55 +0000 | [diff] [blame] | 1256 | &KeyParameter { tag: Tag::ORIGIN, value: KeyParameterValue::Origin(KeyOrigin::IMPORTED) } |
Rajesh Nyamagoud | b8402b6 | 2022-04-08 01:05:47 +0000 | [diff] [blame] | 1257 | )); |
| 1258 | |
| 1259 | Ok(key_metadata) |
| 1260 | } |
Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +0000 | [diff] [blame] | 1261 | |
| 1262 | /// Imports RSA encryption key with WRAP_KEY purpose. |
| 1263 | pub fn import_wrapping_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1264 | sl: &SecLevel, |
Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +0000 | [diff] [blame] | 1265 | wrapping_key_data: &[u8], |
| 1266 | wrapping_key_alias: Option<String>, |
| 1267 | ) -> binder::Result<KeyMetadata> { |
| 1268 | let wrapping_key_params = AuthSetBuilder::new() |
| 1269 | .no_auth_required() |
| 1270 | .algorithm(Algorithm::RSA) |
| 1271 | .digest(Digest::SHA_2_256) |
| 1272 | .purpose(KeyPurpose::ENCRYPT) |
| 1273 | .purpose(KeyPurpose::DECRYPT) |
| 1274 | .purpose(KeyPurpose::WRAP_KEY) |
| 1275 | .padding_mode(PaddingMode::RSA_OAEP) |
| 1276 | .key_size(2048) |
| 1277 | .rsa_public_exponent(65537) |
| 1278 | .cert_not_before(0) |
| 1279 | .cert_not_after(253402300799000); |
| 1280 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1281 | sl.binder.importKey( |
Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +0000 | [diff] [blame] | 1282 | &KeyDescriptor { domain: Domain::APP, nspace: -1, alias: wrapping_key_alias, blob: None }, |
| 1283 | None, |
| 1284 | &wrapping_key_params, |
| 1285 | 0, |
| 1286 | wrapping_key_data, |
| 1287 | ) |
| 1288 | } |
| 1289 | |
| 1290 | /// Import wrapped key using given wrapping key. |
| 1291 | pub fn import_wrapped_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1292 | sl: &SecLevel, |
Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +0000 | [diff] [blame] | 1293 | alias: Option<String>, |
| 1294 | wrapping_key_metadata: &KeyMetadata, |
| 1295 | wrapped_key: Option<Vec<u8>>, |
| 1296 | ) -> binder::Result<KeyMetadata> { |
| 1297 | let unwrap_params = |
| 1298 | AuthSetBuilder::new().digest(Digest::SHA_2_256).padding_mode(PaddingMode::RSA_OAEP); |
| 1299 | |
| 1300 | let authenticator_spec: &[AuthenticatorSpec] = &[AuthenticatorSpec { |
| 1301 | authenticatorType: HardwareAuthenticatorType::NONE, |
| 1302 | authenticatorId: 0, |
| 1303 | }]; |
| 1304 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1305 | let key_metadata = sl.binder.importWrappedKey( |
Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +0000 | [diff] [blame] | 1306 | &KeyDescriptor { domain: Domain::APP, nspace: -1, alias, blob: wrapped_key }, |
| 1307 | &wrapping_key_metadata.key, |
| 1308 | None, |
| 1309 | &unwrap_params, |
| 1310 | authenticator_spec, |
| 1311 | )?; |
| 1312 | |
| 1313 | Ok(key_metadata) |
| 1314 | } |
| 1315 | |
| 1316 | /// Import wrapping key and then import wrapped key using wrapping key. |
| 1317 | pub fn import_wrapping_key_and_wrapped_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1318 | sl: &SecLevel, |
Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +0000 | [diff] [blame] | 1319 | domain: Domain, |
| 1320 | nspace: i64, |
| 1321 | alias: Option<String>, |
| 1322 | wrapping_key_alias: Option<String>, |
| 1323 | wrapping_key_params: AuthSetBuilder, |
| 1324 | ) -> binder::Result<KeyMetadata> { |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1325 | let wrapping_key_metadata = sl.binder.importKey( |
Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +0000 | [diff] [blame] | 1326 | &KeyDescriptor { domain, nspace, alias: wrapping_key_alias, blob: None }, |
| 1327 | None, |
| 1328 | &wrapping_key_params, |
| 1329 | 0, |
| 1330 | WRAPPING_KEY, |
| 1331 | )?; |
| 1332 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1333 | import_wrapped_key(sl, alias, &wrapping_key_metadata, Some(WRAPPED_KEY.to_vec())) |
Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | /// Import given key material as AES-256-GCM-NONE transport key. |
| 1337 | pub fn import_transport_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1338 | sl: &SecLevel, |
Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +0000 | [diff] [blame] | 1339 | transport_key_alias: Option<String>, |
| 1340 | transport_key: &[u8], |
| 1341 | ) -> binder::Result<KeyMetadata> { |
| 1342 | let transport_key_params = AuthSetBuilder::new() |
| 1343 | .no_auth_required() |
| 1344 | .algorithm(Algorithm::AES) |
| 1345 | .block_mode(BlockMode::GCM) |
| 1346 | .padding_mode(PaddingMode::NONE) |
| 1347 | .key_size(256) |
| 1348 | .caller_nonce() |
| 1349 | .min_mac_length(128) |
| 1350 | .purpose(KeyPurpose::ENCRYPT) |
| 1351 | .purpose(KeyPurpose::DECRYPT); |
| 1352 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1353 | sl.binder.importKey( |
Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +0000 | [diff] [blame] | 1354 | &KeyDescriptor { domain: Domain::APP, nspace: -1, alias: transport_key_alias, blob: None }, |
| 1355 | None, |
| 1356 | &transport_key_params, |
| 1357 | 0, |
| 1358 | transport_key, |
| 1359 | ) |
| 1360 | } |
Rajesh Nyamagoud | af05056 | 2022-11-11 01:56:45 +0000 | [diff] [blame] | 1361 | |
| 1362 | /// Generate EC key with purpose AGREE_KEY. |
| 1363 | pub fn generate_ec_agree_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1364 | sl: &SecLevel, |
Rajesh Nyamagoud | af05056 | 2022-11-11 01:56:45 +0000 | [diff] [blame] | 1365 | ec_curve: EcCurve, |
| 1366 | digest: Digest, |
| 1367 | domain: Domain, |
| 1368 | nspace: i64, |
| 1369 | alias: Option<String>, |
| 1370 | ) -> binder::Result<KeyMetadata> { |
| 1371 | let gen_params = AuthSetBuilder::new() |
| 1372 | .no_auth_required() |
| 1373 | .algorithm(Algorithm::EC) |
| 1374 | .purpose(KeyPurpose::AGREE_KEY) |
| 1375 | .digest(digest) |
| 1376 | .ec_curve(ec_curve); |
| 1377 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1378 | match sl.binder.generateKey( |
Rajesh Nyamagoud | af05056 | 2022-11-11 01:56:45 +0000 | [diff] [blame] | 1379 | &KeyDescriptor { domain, nspace, alias, blob: None }, |
| 1380 | None, |
| 1381 | &gen_params, |
| 1382 | 0, |
| 1383 | b"entropy", |
| 1384 | ) { |
| 1385 | Ok(key_metadata) => { |
| 1386 | assert!(key_metadata.certificate.is_some()); |
| 1387 | if domain == Domain::BLOB { |
| 1388 | assert!(key_metadata.key.blob.is_some()); |
| 1389 | } |
| 1390 | |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 1391 | check_key_authorizations( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1392 | sl, |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 1393 | &key_metadata.authorizations, |
| 1394 | &gen_params, |
| 1395 | KeyOrigin::GENERATED, |
| 1396 | ); |
Rajesh Nyamagoud | af05056 | 2022-11-11 01:56:45 +0000 | [diff] [blame] | 1397 | Ok(key_metadata) |
| 1398 | } |
| 1399 | Err(e) => Err(e), |
| 1400 | } |
| 1401 | } |
Rajesh Nyamagoud | 6a82349 | 2023-04-07 02:47:27 +0000 | [diff] [blame] | 1402 | |
| 1403 | /// Helper method to import AES keys `total_count` of times. |
| 1404 | pub fn import_aes_keys( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1405 | sl: &SecLevel, |
Rajesh Nyamagoud | 6a82349 | 2023-04-07 02:47:27 +0000 | [diff] [blame] | 1406 | alias_prefix: String, |
| 1407 | total_count: Range<i32>, |
| 1408 | ) -> binder::Result<HashSet<String>> { |
| 1409 | let mut imported_key_aliases = HashSet::new(); |
| 1410 | |
| 1411 | // Import Total number of keys with given alias prefix. |
| 1412 | for count in total_count { |
| 1413 | let mut alias = String::new(); |
| 1414 | write!(alias, "{}_{}", alias_prefix, count).unwrap(); |
| 1415 | imported_key_aliases.insert(alias.clone()); |
| 1416 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1417 | import_aes_key(sl, Domain::APP, -1, Some(alias))?; |
Rajesh Nyamagoud | 6a82349 | 2023-04-07 02:47:27 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | Ok(imported_key_aliases) |
| 1421 | } |
Rajesh Nyamagoud | a42dee6 | 2022-04-22 21:15:55 +0000 | [diff] [blame] | 1422 | |
| 1423 | /// Generate attested EC-P_256 key with device id attestation. |
| 1424 | pub fn generate_key_with_attest_id( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1425 | sl: &SecLevel, |
Rajesh Nyamagoud | a42dee6 | 2022-04-22 21:15:55 +0000 | [diff] [blame] | 1426 | algorithm: Algorithm, |
| 1427 | alias: Option<String>, |
| 1428 | att_challenge: &[u8], |
| 1429 | attest_key: &KeyDescriptor, |
| 1430 | attest_id: Tag, |
| 1431 | value: Vec<u8>, |
| 1432 | ) -> binder::Result<KeyMetadata> { |
| 1433 | assert!(algorithm == Algorithm::RSA || algorithm == Algorithm::EC); |
| 1434 | |
| 1435 | let mut ec_gen_params; |
| 1436 | if algorithm == Algorithm::EC { |
| 1437 | ec_gen_params = AuthSetBuilder::new() |
| 1438 | .no_auth_required() |
| 1439 | .algorithm(Algorithm::EC) |
| 1440 | .purpose(KeyPurpose::SIGN) |
| 1441 | .purpose(KeyPurpose::VERIFY) |
| 1442 | .digest(Digest::SHA_2_256) |
| 1443 | .ec_curve(EcCurve::P_256) |
| 1444 | .attestation_challenge(att_challenge.to_vec()); |
| 1445 | } else { |
| 1446 | ec_gen_params = AuthSetBuilder::new() |
| 1447 | .no_auth_required() |
| 1448 | .algorithm(Algorithm::RSA) |
| 1449 | .rsa_public_exponent(65537) |
| 1450 | .key_size(2048) |
| 1451 | .purpose(KeyPurpose::SIGN) |
| 1452 | .purpose(KeyPurpose::VERIFY) |
| 1453 | .digest(Digest::SHA_2_256) |
| 1454 | .padding_mode(PaddingMode::RSA_PKCS1_1_5_SIGN) |
| 1455 | .attestation_challenge(att_challenge.to_vec()); |
| 1456 | } |
| 1457 | |
| 1458 | match attest_id { |
| 1459 | Tag::ATTESTATION_ID_BRAND => { |
| 1460 | ec_gen_params = ec_gen_params.attestation_device_brand(value); |
| 1461 | } |
| 1462 | Tag::ATTESTATION_ID_DEVICE => { |
| 1463 | ec_gen_params = ec_gen_params.attestation_device_name(value); |
| 1464 | } |
| 1465 | Tag::ATTESTATION_ID_PRODUCT => { |
| 1466 | ec_gen_params = ec_gen_params.attestation_device_product_name(value); |
| 1467 | } |
| 1468 | Tag::ATTESTATION_ID_SERIAL => { |
| 1469 | ec_gen_params = ec_gen_params.attestation_device_serial(value); |
| 1470 | } |
| 1471 | Tag::ATTESTATION_ID_MANUFACTURER => { |
| 1472 | ec_gen_params = ec_gen_params.attestation_device_manufacturer(value); |
| 1473 | } |
| 1474 | Tag::ATTESTATION_ID_MODEL => { |
| 1475 | ec_gen_params = ec_gen_params.attestation_device_model(value); |
| 1476 | } |
| 1477 | Tag::ATTESTATION_ID_IMEI => { |
| 1478 | ec_gen_params = ec_gen_params.attestation_device_imei(value); |
| 1479 | } |
| 1480 | Tag::ATTESTATION_ID_SECOND_IMEI => { |
| 1481 | ec_gen_params = ec_gen_params.attestation_device_second_imei(value); |
| 1482 | } |
| 1483 | _ => { |
| 1484 | panic!("Unknown attestation id"); |
| 1485 | } |
| 1486 | } |
| 1487 | |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1488 | sl.binder.generateKey( |
Rajesh Nyamagoud | a42dee6 | 2022-04-22 21:15:55 +0000 | [diff] [blame] | 1489 | &KeyDescriptor { domain: Domain::APP, nspace: -1, alias, blob: None }, |
| 1490 | Some(attest_key), |
| 1491 | &ec_gen_params, |
| 1492 | 0, |
| 1493 | b"entropy", |
| 1494 | ) |
| 1495 | } |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 1496 | |
| 1497 | /// Generate Key and validate key characteristics. |
| 1498 | pub fn generate_key( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1499 | sl: &SecLevel, |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 1500 | gen_params: &AuthSetBuilder, |
| 1501 | alias: &str, |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 1502 | ) -> binder::Result<Option<KeyMetadata>> { |
| 1503 | let key_metadata = match sl.binder.generateKey( |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 1504 | &KeyDescriptor { |
| 1505 | domain: Domain::APP, |
| 1506 | nspace: -1, |
| 1507 | alias: Some(alias.to_string()), |
| 1508 | blob: None, |
| 1509 | }, |
| 1510 | None, |
| 1511 | gen_params, |
| 1512 | 0, |
| 1513 | b"entropy", |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 1514 | ) { |
| 1515 | Ok(metadata) => metadata, |
| 1516 | Err(e) => { |
| 1517 | return if is_rkp_only_unknown_on_gsi(sl.level) |
| 1518 | && e.service_specific_error() == ErrorCode::ATTESTATION_KEYS_NOT_PROVISIONED.0 |
| 1519 | { |
| 1520 | // GSI replaces the values for remote_prov_prop properties (since they’re |
| 1521 | // system_internal_prop properties), so on GSI the properties are not |
| 1522 | // reliable indicators of whether StrongBox/TEE are RKP-only or not. |
| 1523 | // Test can be skipped if it generates a key with attestation but doesn't provide |
| 1524 | // an ATTEST_KEY and rkp-only property is undetermined. |
| 1525 | Ok(None) |
| 1526 | } else { |
| 1527 | Err(e) |
| 1528 | }; |
| 1529 | } |
| 1530 | }; |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 1531 | |
| 1532 | if gen_params.iter().any(|kp| { |
| 1533 | matches!( |
| 1534 | kp.value, |
| 1535 | KeyParameterValue::Algorithm(Algorithm::RSA) |
| 1536 | | KeyParameterValue::Algorithm(Algorithm::EC) |
| 1537 | ) |
| 1538 | }) { |
| 1539 | assert!(key_metadata.certificate.is_some()); |
| 1540 | if gen_params.iter().any(|kp| kp.tag == Tag::ATTESTATION_CHALLENGE) { |
| 1541 | assert!(key_metadata.certificateChain.is_some()); |
Rajesh Nyamagoud | 5f6db2f | 2023-06-01 17:22:32 +0000 | [diff] [blame] | 1542 | let mut cert_chain: Vec<u8> = Vec::new(); |
| 1543 | cert_chain.extend(key_metadata.certificate.as_ref().unwrap()); |
| 1544 | cert_chain.extend(key_metadata.certificateChain.as_ref().unwrap()); |
Rajesh Nyamagoud | b1c8e83 | 2023-06-06 01:39:44 +0000 | [diff] [blame] | 1545 | let strict_issuer_check = |
| 1546 | !(gen_params.iter().any(|kp| kp.tag == Tag::DEVICE_UNIQUE_ATTESTATION)); |
| 1547 | validate_certchain_with_strict_issuer_check(&cert_chain, strict_issuer_check) |
| 1548 | .expect("Error while validating cert chain"); |
Rajesh Nyamagoud | 5f6db2f | 2023-06-01 17:22:32 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | if let Some(challenge_param) = |
| 1552 | gen_params.iter().find(|kp| kp.tag == Tag::ATTESTATION_CHALLENGE) |
| 1553 | { |
| 1554 | if let KeyParameterValue::Blob(val) = &challenge_param.value { |
| 1555 | let att_challenge = get_value_from_attest_record( |
| 1556 | key_metadata.certificate.as_ref().unwrap(), |
| 1557 | challenge_param.tag, |
| 1558 | key_metadata.keySecurityLevel, |
| 1559 | ) |
| 1560 | .expect("Attestation challenge verification failed."); |
| 1561 | assert_eq!(&att_challenge, val); |
| 1562 | } |
| 1563 | |
| 1564 | let att_app_id = get_value_from_attest_record( |
| 1565 | key_metadata.certificate.as_ref().unwrap(), |
| 1566 | Tag::ATTESTATION_APPLICATION_ID, |
| 1567 | SecurityLevel::KEYSTORE, |
| 1568 | ) |
| 1569 | .expect("Attestation application id verification failed."); |
| 1570 | assert!(!att_app_id.is_empty()); |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 1571 | } |
| 1572 | } |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1573 | check_key_authorizations(sl, &key_metadata.authorizations, gen_params, KeyOrigin::GENERATED); |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 1574 | |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 1575 | Ok(Some(key_metadata)) |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | /// Generate a key using given authorizations and create an operation using the generated key. |
| 1579 | pub fn create_key_and_operation( |
David Drysdale | 7909224 | 2024-06-18 13:13:43 +0100 | [diff] [blame] | 1580 | sl: &SecLevel, |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 1581 | gen_params: &AuthSetBuilder, |
| 1582 | op_params: &AuthSetBuilder, |
| 1583 | alias: &str, |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 1584 | ) -> binder::Result<Option<CreateOperationResponse>> { |
| 1585 | let Some(key_metadata) = generate_key(sl, gen_params, alias)? else { |
| 1586 | return Ok(None); |
| 1587 | }; |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 1588 | |
Rajesh Nyamagoud | 94f2503 | 2024-08-15 21:12:20 +0000 | [diff] [blame] | 1589 | sl.binder.createOperation(&key_metadata.key, op_params, false).map(Some) |
Rajesh Nyamagoud | 75dfa0c | 2023-05-11 00:31:40 +0000 | [diff] [blame] | 1590 | } |