1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //! VTS test library for AuthGraph functionality.
18 //!
19 //! This test code is bundled as a library, not as `[cfg(test)]`, to allow it to be
20 //! re-used inside the (Rust) VTS tests of components that use AuthGraph.
21
22 use android_hardware_security_authgraph::aidl::android::hardware::security::authgraph::{
23 Error::Error, IAuthGraphKeyExchange::IAuthGraphKeyExchange, Identity::Identity,
24 PlainPubKey::PlainPubKey, PubKey::PubKey, SessionIdSignature::SessionIdSignature,
25 };
26 use authgraph_boringssl as boring;
27 use authgraph_core::{error::Error as AgError, keyexchange as ke};
28 use coset::CborSerializable;
29 use std::{cell::RefCell, rc::Rc};
30
31 pub mod sink;
32 pub mod source;
33
34 /// Return an AuthGraphParticipant suitable for testing.
test_ag_participant() -> Result<ke::AuthGraphParticipant, AgError>35 pub fn test_ag_participant() -> Result<ke::AuthGraphParticipant, AgError> {
36 Ok(ke::AuthGraphParticipant::new(
37 boring::crypto_trait_impls(),
38 Rc::new(RefCell::new(boring::test_device::AgDevice::default())),
39 ke::MAX_OPENED_SESSIONS,
40 )?)
41 }
42
build_plain_pub_key(pub_key: &Option<Vec<u8>>) -> PubKey43 fn build_plain_pub_key(pub_key: &Option<Vec<u8>>) -> PubKey {
44 PubKey::PlainKey(PlainPubKey {
45 plainPubKey: pub_key.clone().unwrap(),
46 })
47 }
48
extract_plain_pub_key(pub_key: &Option<PubKey>) -> &PlainPubKey49 fn extract_plain_pub_key(pub_key: &Option<PubKey>) -> &PlainPubKey {
50 match pub_key {
51 Some(PubKey::PlainKey(pub_key)) => pub_key,
52 Some(PubKey::SignedKey(_)) => panic!("expect unsigned public key"),
53 None => panic!("expect pubKey to be populated"),
54 }
55 }
56
vec_to_identity(data: &[u8]) -> Identity57 fn vec_to_identity(data: &[u8]) -> Identity {
58 Identity {
59 identity: data.to_vec(),
60 }
61 }
62
vec_to_signature(data: &[u8]) -> SessionIdSignature63 fn vec_to_signature(data: &[u8]) -> SessionIdSignature {
64 SessionIdSignature {
65 signature: data.to_vec(),
66 }
67 }
68