Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +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 | |
Rajesh Nyamagoud | a42dee6 | 2022-04-22 21:15:55 +0000 | [diff] [blame^] | 15 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::Tag::Tag; |
Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +0000 | [diff] [blame] | 16 | use keystore2_test_utils::key_generations::Error; |
| 17 | |
| 18 | #[cxx::bridge] |
| 19 | mod ffi { |
| 20 | struct CxxResult { |
| 21 | data: Vec<u8>, |
| 22 | error: i32, |
| 23 | } |
| 24 | |
| 25 | unsafe extern "C++" { |
| 26 | include!("ffi_test_utils.hpp"); |
| 27 | fn validateCertChain(cert_buf: Vec<u8>, cert_len: u32, strict_issuer_check: bool) -> bool; |
| 28 | fn createWrappedKey( |
| 29 | encrypted_secure_key: Vec<u8>, |
| 30 | encrypted_transport_key: Vec<u8>, |
| 31 | iv: Vec<u8>, |
| 32 | tag: Vec<u8>, |
| 33 | ) -> CxxResult; |
| 34 | fn buildAsn1DerEncodedWrappedKeyDescription() -> CxxResult; |
Rajesh Nyamagoud | 28abde6 | 2023-04-01 01:32:32 +0000 | [diff] [blame] | 35 | fn performCryptoOpUsingKeystoreEngine(grant_id: i64) -> bool; |
Rajesh Nyamagoud | a42dee6 | 2022-04-22 21:15:55 +0000 | [diff] [blame^] | 36 | fn getValueFromAttestRecord(cert_buf: Vec<u8>, tag: i32) -> CxxResult; |
Rajesh Nyamagoud | c946cc4 | 2022-04-12 22:49:11 +0000 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
| 40 | /// Validate given certificate chain. |
| 41 | pub fn validate_certchain(cert_buf: &[u8]) -> Result<bool, Error> { |
| 42 | if ffi::validateCertChain(cert_buf.to_vec(), cert_buf.len().try_into().unwrap(), true) { |
| 43 | return Ok(true); |
| 44 | } |
| 45 | |
| 46 | Err(Error::ValidateCertChainFailed) |
| 47 | } |
| 48 | |
| 49 | fn get_result(result: ffi::CxxResult) -> Result<Vec<u8>, Error> { |
| 50 | if result.error == 0 && !result.data.is_empty() { |
| 51 | Ok(result.data) |
| 52 | } else { |
| 53 | Err(Error::DerEncodeFailed) |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// Creates wrapped key material to import in ASN.1 DER-encoded data corresponding to |
| 58 | /// `SecureKeyWrapper`. See `IKeyMintDevice.aidl` for documentation of the `SecureKeyWrapper` |
| 59 | /// schema. |
| 60 | pub fn create_wrapped_key( |
| 61 | encrypted_secure_key: &[u8], |
| 62 | encrypted_transport_key: &[u8], |
| 63 | iv: &[u8], |
| 64 | tag: &[u8], |
| 65 | ) -> Result<Vec<u8>, Error> { |
| 66 | get_result(ffi::createWrappedKey( |
| 67 | encrypted_secure_key.to_vec(), |
| 68 | encrypted_transport_key.to_vec(), |
| 69 | iv.to_vec(), |
| 70 | tag.to_vec(), |
| 71 | )) |
| 72 | } |
| 73 | |
| 74 | /// Creates ASN.1 DER-encoded data corresponding to `KeyDescription` schema. |
| 75 | /// See `IKeyMintDevice.aidl` for documentation of the `KeyDescription` schema. |
| 76 | /// Below mentioned key parameters are used - |
| 77 | /// Algorithm: AES-256 |
| 78 | /// Padding: PKCS7 |
| 79 | /// Blockmode: ECB |
| 80 | /// Purpose: Encrypt, Decrypt |
| 81 | pub fn create_wrapped_key_additional_auth_data() -> Result<Vec<u8>, Error> { |
| 82 | get_result(ffi::buildAsn1DerEncodedWrappedKeyDescription()) |
| 83 | } |
Rajesh Nyamagoud | 28abde6 | 2023-04-01 01:32:32 +0000 | [diff] [blame] | 84 | |
| 85 | pub fn perform_crypto_op_using_keystore_engine(grant_id: i64) -> Result<bool, Error> { |
| 86 | if ffi::performCryptoOpUsingKeystoreEngine(grant_id) { |
| 87 | return Ok(true); |
| 88 | } |
| 89 | |
| 90 | Err(Error::Keystore2EngineOpFailed) |
| 91 | } |
Rajesh Nyamagoud | a42dee6 | 2022-04-22 21:15:55 +0000 | [diff] [blame^] | 92 | |
| 93 | pub fn get_value_from_attest_record(cert_buf: &[u8], tag: Tag) -> Result<Vec<u8>, Error> { |
| 94 | let result = ffi::getValueFromAttestRecord(cert_buf.to_vec(), tag.0); |
| 95 | if result.error == 0 && !result.data.is_empty() { |
| 96 | return Ok(result.data); |
| 97 | } |
| 98 | Err(Error::AttestRecordGetValueFailed) |
| 99 | } |