David Drysdale | fe41825 | 2023-11-07 09:27:56 +0000 | [diff] [blame] | 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; |
Hasini Gunasinghe | 5df6ed5 | 2023-11-13 09:18:25 +0000 | [diff] [blame] | 27 | use authgraph_core::{error::Error as AgError, keyexchange as ke}; |
David Drysdale | fe41825 | 2023-11-07 09:27:56 +0000 | [diff] [blame] | 28 | use coset::CborSerializable; |
David Drysdale | 8898d2e | 2023-11-07 15:20:15 +0000 | [diff] [blame] | 29 | use std::{cell::RefCell, rc::Rc}; |
David Drysdale | fe41825 | 2023-11-07 09:27:56 +0000 | [diff] [blame] | 30 | |
| 31 | pub mod sink; |
| 32 | pub mod source; |
| 33 | |
Hasini Gunasinghe | 5df6ed5 | 2023-11-13 09:18:25 +0000 | [diff] [blame] | 34 | /// Return an AuthGraphParticipant suitable for testing. |
| 35 | pub fn test_ag_participant() -> Result<ke::AuthGraphParticipant, AgError> { |
| 36 | Ok(ke::AuthGraphParticipant::new( |
| 37 | boring::crypto_trait_impls(), |
David Drysdale | 8898d2e | 2023-11-07 15:20:15 +0000 | [diff] [blame] | 38 | Rc::new(RefCell::new(boring::test_device::AgDevice::default())), |
Hasini Gunasinghe | 5df6ed5 | 2023-11-13 09:18:25 +0000 | [diff] [blame] | 39 | ke::MAX_OPENED_SESSIONS, |
| 40 | )?) |
David Drysdale | fe41825 | 2023-11-07 09:27:56 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | fn build_plain_pub_key(pub_key: &Option<Vec<u8>>) -> PubKey { |
| 44 | PubKey::PlainKey(PlainPubKey { |
| 45 | plainPubKey: pub_key.clone().unwrap(), |
| 46 | }) |
| 47 | } |
| 48 | |
| 49 | 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 | |
David Drysdale | fe41825 | 2023-11-07 09:27:56 +0000 | [diff] [blame] | 57 | fn vec_to_identity(data: &[u8]) -> Identity { |
| 58 | Identity { |
| 59 | identity: data.to_vec(), |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | fn vec_to_signature(data: &[u8]) -> SessionIdSignature { |
| 64 | SessionIdSignature { |
| 65 | signature: data.to_vec(), |
| 66 | } |
| 67 | } |