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