diff options
author | Rajesh Nyamagoud <nyamagoud@google.com> | 2022-04-12 22:49:11 +0000 |
---|---|---|
committer | Rajesh Nyamagoud <nyamagoud@google.com> | 2023-01-04 22:42:18 +0000 |
commit | c946cc419cd1b0d928d4873b47cec452a4fbb9bb (patch) | |
tree | f2535869b3c5cd59e9ef66d34f35ca935207a098 /keystore2/test_utils | |
parent | 989923b09db257e777d166c55301be7a850812fd (diff) | |
download | security-c946cc419cd1b0d928d4873b47cec452a4fbb9bb.tar.gz |
Tests to verify importWrappedKey functionality.
- Build ASN.1 DER-encoded wrapped key material `SecureKeyWrapper`.
Import the wrapped key material. Test should create DER-encoded
wrapped key material and import the secure key successfully.
Imported key should be used perform crypto operation successfully.
- Build ASN.1 DER-encoded wrapped key material `SecureKeyWrapper` with
invalid AAD. Try to import the wrapped key material. Test should
create DER-encoded wrapped key material. Test should fail to import
the secure key with error code `VERIFICATION_FAILED`.
- Import wrapped AES key and use it for crypto operations. Test should
import wrapped key and user it for performing crypto operations
successfully.
- Import a key wihtout WRAP_KEY purpose and try to use it as wrapping
key while importing wrapped key. Test should fail to import wrapped
key with an error code `INCOMPATIBLE_PURPOSE`.
- Try to import wrapped key using non-existing wrapping key in Android
keystore. Test should fail to import wrapped key with an error code
`KEY_NOT_FOUND`.
Bug: 194359114
Test: atest keystore2_client_test
Change-Id: Ic9ed03b6f945bd41c4873f97a84c1658c284e918
Diffstat (limited to 'keystore2/test_utils')
-rw-r--r-- | keystore2/test_utils/authorizations.rs | 9 | ||||
-rw-r--r-- | keystore2/test_utils/key_generations.rs | 242 |
2 files changed, 245 insertions, 6 deletions
diff --git a/keystore2/test_utils/authorizations.rs b/keystore2/test_utils/authorizations.rs index c2f0279e..7dcee830 100644 --- a/keystore2/test_utils/authorizations.rs +++ b/keystore2/test_utils/authorizations.rs @@ -149,6 +149,15 @@ impl AuthSetBuilder { self } + /// Add CALLER_NONCE. + pub fn caller_nonce(mut self) -> Self { + self.0.push(KeyParameter { + tag: Tag::CALLER_NONCE, + value: KeyParameterValue::BoolValue(true), + }); + self + } + /// Add MAC length. pub fn mac_length(mut self, l: i32) -> Self { self.0.push(KeyParameter { tag: Tag::MAC_LENGTH, value: KeyParameterValue::Integer(l) }); diff --git a/keystore2/test_utils/key_generations.rs b/keystore2/test_utils/key_generations.rs index 175d8bb9..53597af8 100644 --- a/keystore2/test_utils/key_generations.rs +++ b/keystore2/test_utils/key_generations.rs @@ -18,13 +18,14 @@ use anyhow::Result; use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ Algorithm::Algorithm, BlockMode::BlockMode, Digest::Digest, EcCurve::EcCurve, - ErrorCode::ErrorCode, KeyOrigin::KeyOrigin, KeyParameter::KeyParameter, - KeyParameterValue::KeyParameterValue, KeyPurpose::KeyPurpose, PaddingMode::PaddingMode, - Tag::Tag, + ErrorCode::ErrorCode, HardwareAuthenticatorType::HardwareAuthenticatorType, + KeyOrigin::KeyOrigin, KeyParameter::KeyParameter, KeyParameterValue::KeyParameterValue, + KeyPurpose::KeyPurpose, PaddingMode::PaddingMode, Tag::Tag, }; use android_system_keystore2::aidl::android::system::keystore2::{ - Authorization::Authorization, Domain::Domain, IKeystoreSecurityLevel::IKeystoreSecurityLevel, - KeyDescriptor::KeyDescriptor, KeyMetadata::KeyMetadata, ResponseCode::ResponseCode, + AuthenticatorSpec::AuthenticatorSpec, Authorization::Authorization, Domain::Domain, + IKeystoreSecurityLevel::IKeystoreSecurityLevel, KeyDescriptor::KeyDescriptor, + KeyMetadata::KeyMetadata, ResponseCode::ResponseCode, }; use crate::authorizations::AuthSetBuilder; @@ -157,6 +158,131 @@ pub static EC_P_256_KEY: &[u8] = &[ 0xD6, 0x84, 0x98, 0xEA, 0x96, 0x91, 0xFB, 0x78, 0xED, 0x86, ]; +/// DER-encoded PKCS#8 format RSA key - +/// Size: 2048 +/// Public Exponent: 65537 +/// Purpose: WRAP_KEY, ENCRYPT, DECRYPT +/// Encryption scheme: RSAES-PKCS1-v1_5 +/// Digest: SHA_2_256 +/// Padding: RSA_OAEP +/// This sample wrapping_key is taken from KeyMint tests +/// (see hardware/interfaces/security/keymint/aidl/vts/functional/KeyMintTest.cpp). +/// Similarly more test keys can be generated with below command - +/// openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt -outform der | hexdump -e '30/1 "%02X" "\n"' +pub static WRAPPING_KEY: &[u8] = &[ + 0x30, 0x82, 0x04, 0xbe, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, + 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x04, 0xa8, 0x30, 0x82, 0x04, 0xa4, 0x02, 0x01, + 0x00, 0x02, 0x82, 0x01, 0x01, 0x00, 0xae, 0xc3, 0x67, 0x93, 0x1d, 0x89, 0x00, 0xce, 0x56, 0xb0, + 0x06, 0x7f, 0x7d, 0x70, 0xe1, 0xfc, 0x65, 0x3f, 0x3f, 0x34, 0xd1, 0x94, 0xc1, 0xfe, 0xd5, 0x00, + 0x18, 0xfb, 0x43, 0xdb, 0x93, 0x7b, 0x06, 0xe6, 0x73, 0xa8, 0x37, 0x31, 0x3d, 0x56, 0xb1, 0xc7, + 0x25, 0x15, 0x0a, 0x3f, 0xef, 0x86, 0xac, 0xbd, 0xdc, 0x41, 0xbb, 0x75, 0x9c, 0x28, 0x54, 0xea, + 0xe3, 0x2d, 0x35, 0x84, 0x1e, 0xfb, 0x5c, 0x18, 0xd8, 0x2b, 0xc9, 0x0a, 0x1c, 0xb5, 0xc1, 0xd5, + 0x5a, 0xdf, 0x24, 0x5b, 0x02, 0x91, 0x1f, 0x0b, 0x7c, 0xda, 0x88, 0xc4, 0x21, 0xff, 0x0e, 0xba, + 0xfe, 0x7c, 0x0d, 0x23, 0xbe, 0x31, 0x2d, 0x7b, 0xd5, 0x92, 0x1f, 0xfa, 0xea, 0x13, 0x47, 0xc1, + 0x57, 0x40, 0x6f, 0xef, 0x71, 0x8f, 0x68, 0x26, 0x43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, 0xf9, 0xd3, 0x5f, 0x13, 0x7b, + 0x57, 0xd0, 0xd6, 0x5c, 0x39, 0x7c, 0x5e, 0x21, 0xcc, 0x25, 0x1e, 0x47, 0x00, 0x8e, 0xd6, 0x2a, + 0x54, 0x24, 0x09, 0xc8, 0xb6, 0xb6, 0xac, 0x7f, 0x89, 0x67, 0xb3, 0x86, 0x3c, 0xa6, 0x45, 0xfc, + 0xce, 0x49, 0x58, 0x2a, 0x9a, 0xa1, 0x73, 0x49, 0xdb, 0x6c, 0x4a, 0x95, 0xaf, 0xfd, 0xae, 0x0d, + 0xae, 0x61, 0x2e, 0x1a, 0xfa, 0xc9, 0x9e, 0xd3, 0x9a, 0x2d, 0x93, 0x4c, 0x88, 0x04, 0x40, 0xae, + 0xd8, 0x83, 0x2f, 0x98, 0x43, 0x16, 0x3a, 0x47, 0xf2, 0x7f, 0x39, 0x21, 0x99, 0xdc, 0x12, 0x02, + 0xf9, 0xa0, 0xf9, 0xbd, 0x08, 0x30, 0x80, 0x07, 0xcb, 0x1e, 0x4e, 0x7f, 0x58, 0x30, 0x93, 0x66, + 0xa7, 0xde, 0x25, 0xf7, 0xc3, 0xc9, 0xb8, 0x80, 0x67, 0x7c, 0x06, 0x8e, 0x1b, 0xe9, 0x36, 0xe8, + 0x12, 0x88, 0x81, 0x52, 0x52, 0xa8, 0xa1, 0x02, 0x81, 0x80, 0x57, 0xff, 0x8c, 0xa1, 0x89, 0x50, + 0x80, 0xb2, 0xca, 0xe4, 0x86, 0xef, 0x0a, 0xdf, 0xd7, 0x91, 0xfb, 0x02, 0x35, 0xc0, 0xb8, 0xb3, + 0x6c, 0xd6, 0xc1, 0x36, 0xe5, 0x2e, 0x40, 0x85, 0xf4, 0xea, 0x5a, 0x06, 0x32, 0x12, 0xa4, 0xf1, + 0x05, 0xa3, 0x76, 0x47, 0x43, 0xe5, 0x32, 0x81, 0x98, 0x8a, 0xba, 0x07, 0x3f, 0x6e, 0x00, 0x27, + 0x29, 0x8e, 0x1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0xef, 0x33, 0xda, 0x50, 0x7c, 0x9d, 0xf6, + 0xb9, 0x27, 0x7c, 0xff, 0x1e, 0x6a, 0xaa, 0x5d, 0x57, 0xac, 0xa5, 0x28, 0x46, 0x61, 0x02, 0x81, + 0x81, 0x00, 0xc9, 0x31, 0x61, 0x7c, 0x77, 0x82, 0x9d, 0xfb, 0x12, 0x70, 0x50, 0x2b, 0xe9, 0x19, + 0x5c, 0x8f, 0x28, 0x30, 0x88, 0x5f, 0x57, 0xdb, 0xa8, 0x69, 0x53, 0x68, 0x11, 0xe6, 0x86, 0x42, + 0x36, 0xd0, 0xc4, 0x73, 0x6a, 0x00, 0x08, 0xa1, 0x45, 0xaf, 0x36, 0xb8, 0x35, 0x7a, 0x7c, 0x3d, + 0x13, 0x99, 0x66, 0xd0, 0x4c, 0x4e, 0x00, 0x93, 0x4e, 0xa1, 0xae, 0xde, 0x3b, 0xb6, 0xb8, 0xec, + 0x84, 0x1d, 0xc9, 0x5e, 0x3f, 0x57, 0x97, 0x51, 0xe2, 0xbf, 0xdf, 0xe2, 0x7a, 0xe7, 0x78, 0x98, + 0x3f, 0x95, 0x93, 0x56, 0x21, 0x07, 0x23, 0x28, 0x7b, 0x0a, 0xff, 0xcc, 0x9f, 0x72, 0x70, 0x44, + 0xd4, 0x8c, 0x37, 0x3f, 0x1b, 0xab, 0xde, 0x07, 0x24, 0xfa, 0x17, 0xa4, 0xfd, 0x4d, 0xa0, 0x90, + 0x2c, 0x7c, 0x9b, 0x9b, 0xf2, 0x7b, 0xa6, 0x1b, 0xe6, 0xad, 0x02, 0xdf, 0xdd, 0xda, 0x8f, 0x4e, + 0x68, 0x22, +]; + +/// WrappedKeyData as ASN.1 DER-encoded data corresponding to the `SecureKeyWrapper` schema +/// specified in IKeyMintDevice.aidl. Wrapped key parameters are - +/// Algorithm: AES +/// Key size: 256 +/// Block mode: ECB +/// Padding mode: PKCS7 +/// This sample wrapped_key is taken from KeyMint tests (see KeyMintTest.cpp). +pub static WRAPPED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3d, 0xd7, 0x00, 0x9c, 0x96, 0x36, 0x53, 0x4f, 0x6f, 0x23, 0x9c, 0xff, + 0xe1, 0xfc, 0x8d, 0xaa, 0x46, 0x6f, 0x78, 0xb6, 0x76, 0xc7, 0x11, 0x9e, 0xfb, 0x96, 0xbc, 0xe4, + 0xe6, 0x9c, 0xa2, 0xa2, 0x5d, 0x0b, 0x34, 0xed, 0x9c, 0x3f, 0xf9, 0x99, 0xb8, 0x01, 0x59, 0x7d, + 0x52, 0x20, 0xe3, 0x07, 0xea, 0xa5, 0xbe, 0xe5, 0x07, 0xfb, 0x94, 0xd1, 0xfa, 0x69, 0xf9, 0xe5, + 0x19, 0xb2, 0xde, 0x31, 0x5b, 0xac, 0x92, 0xc3, 0x6f, 0x2e, 0xa1, 0xfa, 0x1d, 0xf4, 0x47, 0x8c, + 0x0d, 0xde, 0xde, 0xae, 0x8c, 0x70, 0xe0, 0x23, 0x3c, 0xd0, 0x98, 0x04, 0x0c, 0xd7, 0x96, 0xb0, + 0x2c, 0x37, 0x0f, 0x1f, 0xa4, 0xcc, 0x01, 0x24, 0xf1, 0x30, 0x2e, 0x02, 0x01, 0x03, 0x30, 0x29, + 0xa1, 0x08, 0x31, 0x06, 0x02, 0x01, 0x00, 0x02, 0x01, 0x01, 0xa2, 0x03, 0x02, 0x01, 0x20, 0xa3, + 0x04, 0x02, 0x02, 0x01, 0x00, 0xa4, 0x05, 0x31, 0x03, 0x02, 0x01, 0x01, 0xa6, 0x05, 0x31, 0x03, + 0x02, 0x01, 0x40, 0xbf, 0x83, 0x77, 0x02, 0x05, 0x00, 0x04, 0x20, 0xcc, 0xd5, 0x40, 0x85, 0x5f, + 0x83, 0x3a, 0x5e, 0x14, 0x80, 0xbf, 0xd2, 0xd3, 0x6f, 0xaf, 0x3a, 0xee, 0xe1, 0x5d, 0xf5, 0xbe, + 0xab, 0xe2, 0x69, 0x1b, 0xc8, 0x2d, 0xde, 0x2a, 0x7a, 0xa9, 0x10, 0x04, 0x10, 0x64, 0xc9, 0xf6, + 0x89, 0xc6, 0x0f, 0xf6, 0x22, 0x3a, 0xb6, 0xe6, 0x99, 0x9e, 0x0e, 0xb6, 0xe5, +]; + /// To map Keystore errors. #[derive(thiserror::Error, Debug, Eq, PartialEq)] pub enum Error { @@ -172,6 +298,9 @@ pub enum Error { /// This is returned if the C implementation of extractSubjectFromCertificate failed. #[error("Failed to validate certificate chain.")] ValidateCertChainFailed, + /// Error code to indicate error in ASN.1 DER-encoded data creation. + #[error("Failed to create and encode ASN.1 data.")] + DerEncodeFailed, } /// Keystore2 error mapping. @@ -564,7 +693,8 @@ pub fn generate_ec_256_attested_key( Ok(ec_key_metadata) } -fn check_key_param(authorizations: &[Authorization], key_param: KeyParameter) -> bool { +/// Verify that given key param is listed in given authorizations list. +pub fn check_key_param(authorizations: &[Authorization], key_param: KeyParameter) -> bool { for authrization in authorizations { if authrization.keyParameter == key_param { return true; @@ -836,3 +966,103 @@ pub fn import_hmac_key( Ok(key_metadata) } + +/// Imports RSA encryption key with WRAP_KEY purpose. +pub fn import_wrapping_key( + sec_level: &binder::Strong<dyn IKeystoreSecurityLevel>, + wrapping_key_data: &[u8], + wrapping_key_alias: Option<String>, +) -> binder::Result<KeyMetadata> { + let wrapping_key_params = AuthSetBuilder::new() + .no_auth_required() + .algorithm(Algorithm::RSA) + .digest(Digest::SHA_2_256) + .purpose(KeyPurpose::ENCRYPT) + .purpose(KeyPurpose::DECRYPT) + .purpose(KeyPurpose::WRAP_KEY) + .padding_mode(PaddingMode::RSA_OAEP) + .key_size(2048) + .rsa_public_exponent(65537) + .cert_not_before(0) + .cert_not_after(253402300799000); + + sec_level.importKey( + &KeyDescriptor { domain: Domain::APP, nspace: -1, alias: wrapping_key_alias, blob: None }, + None, + &wrapping_key_params, + 0, + wrapping_key_data, + ) +} + +/// Import wrapped key using given wrapping key. +pub fn import_wrapped_key( + sec_level: &binder::Strong<dyn IKeystoreSecurityLevel>, + alias: Option<String>, + wrapping_key_metadata: &KeyMetadata, + wrapped_key: Option<Vec<u8>>, +) -> binder::Result<KeyMetadata> { + let unwrap_params = + AuthSetBuilder::new().digest(Digest::SHA_2_256).padding_mode(PaddingMode::RSA_OAEP); + + let authenticator_spec: &[AuthenticatorSpec] = &[AuthenticatorSpec { + authenticatorType: HardwareAuthenticatorType::NONE, + authenticatorId: 0, + }]; + + let key_metadata = sec_level.importWrappedKey( + &KeyDescriptor { domain: Domain::APP, nspace: -1, alias, blob: wrapped_key }, + &wrapping_key_metadata.key, + None, + &unwrap_params, + authenticator_spec, + )?; + + Ok(key_metadata) +} + +/// Import wrapping key and then import wrapped key using wrapping key. +pub fn import_wrapping_key_and_wrapped_key( + sec_level: &binder::Strong<dyn IKeystoreSecurityLevel>, + domain: Domain, + nspace: i64, + alias: Option<String>, + wrapping_key_alias: Option<String>, + wrapping_key_params: AuthSetBuilder, +) -> binder::Result<KeyMetadata> { + let wrapping_key_metadata = sec_level.importKey( + &KeyDescriptor { domain, nspace, alias: wrapping_key_alias, blob: None }, + None, + &wrapping_key_params, + 0, + WRAPPING_KEY, + )?; + + import_wrapped_key(sec_level, alias, &wrapping_key_metadata, Some(WRAPPED_KEY.to_vec())) +} + +/// Import given key material as AES-256-GCM-NONE transport key. +pub fn import_transport_key( + sec_level: &binder::Strong<dyn IKeystoreSecurityLevel>, + transport_key_alias: Option<String>, + transport_key: &[u8], +) -> binder::Result<KeyMetadata> { + let transport_key_params = AuthSetBuilder::new() + .no_auth_required() + .algorithm(Algorithm::AES) + .block_mode(BlockMode::GCM) + .padding_mode(PaddingMode::NONE) + .key_size(256) + .caller_nonce() + .min_mac_length(128) + .purpose(KeyPurpose::ENCRYPT) + .purpose(KeyPurpose::DECRYPT); + + sec_level.importKey( + &KeyDescriptor { domain: Domain::APP, nspace: -1, alias: transport_key_alias, blob: None }, + None, + &transport_key_params, + 0, + transport_key, + ) +} |