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