blob: c6521741b2a9a845ca8e4d97b5d82e680c34e68a [file] [log] [blame]
Rajesh Nyamagoudc946cc42022-04-12 22:49:11 +00001// 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 Nyamagouda42dee62022-04-22 21:15:55 +000015use android_hardware_security_keymint::aidl::android::hardware::security::keymint::Tag::Tag;
Rajesh Nyamagoudc946cc42022-04-12 22:49:11 +000016use keystore2_test_utils::key_generations::Error;
17
18#[cxx::bridge]
19mod 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 Nyamagoud28abde62023-04-01 01:32:32 +000035 fn performCryptoOpUsingKeystoreEngine(grant_id: i64) -> bool;
Rajesh Nyamagouda42dee62022-04-22 21:15:55 +000036 fn getValueFromAttestRecord(cert_buf: Vec<u8>, tag: i32) -> CxxResult;
Rajesh Nyamagoudc946cc42022-04-12 22:49:11 +000037 }
38}
39
40/// Validate given certificate chain.
41pub 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
49fn 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.
60pub 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
81pub fn create_wrapped_key_additional_auth_data() -> Result<Vec<u8>, Error> {
82 get_result(ffi::buildAsn1DerEncodedWrappedKeyDescription())
83}
Rajesh Nyamagoud28abde62023-04-01 01:32:32 +000084
85pub 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 Nyamagouda42dee62022-04-22 21:15:55 +000092
93pub 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}