Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +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 | |
| 15 | //! This module implements test utils to create Autherizations. |
| 16 | |
| 17 | use std::ops::Deref; |
| 18 | |
| 19 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
| 20 | Algorithm::Algorithm, Digest::Digest, EcCurve::EcCurve, KeyParameter::KeyParameter, |
| 21 | KeyParameterValue::KeyParameterValue, KeyPurpose::KeyPurpose, Tag::Tag, |
| 22 | }; |
| 23 | |
| 24 | /// Helper struct to create set of Authorizations. |
Rajesh Nyamagoud | b881d51 | 2021-12-10 00:33:15 +0000 | [diff] [blame^] | 25 | #[derive(Debug, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)] |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 26 | pub struct AuthSetBuilder(Vec<KeyParameter>); |
| 27 | |
| 28 | impl Default for AuthSetBuilder { |
| 29 | fn default() -> Self { |
| 30 | Self::new() |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | impl AuthSetBuilder { |
| 35 | /// Creates new Authorizations list. |
| 36 | pub fn new() -> Self { |
| 37 | Self(Vec::new()) |
| 38 | } |
| 39 | |
| 40 | /// Add Purpose. |
| 41 | pub fn purpose(mut self, p: KeyPurpose) -> Self { |
| 42 | self.0.push(KeyParameter { tag: Tag::PURPOSE, value: KeyParameterValue::KeyPurpose(p) }); |
| 43 | self |
| 44 | } |
| 45 | |
| 46 | /// Add Digest. |
| 47 | pub fn digest(mut self, d: Digest) -> Self { |
| 48 | self.0.push(KeyParameter { tag: Tag::DIGEST, value: KeyParameterValue::Digest(d) }); |
| 49 | self |
| 50 | } |
| 51 | |
| 52 | /// Add Algorithm. |
| 53 | pub fn algorithm(mut self, a: Algorithm) -> Self { |
| 54 | self.0.push(KeyParameter { tag: Tag::ALGORITHM, value: KeyParameterValue::Algorithm(a) }); |
| 55 | self |
| 56 | } |
| 57 | |
| 58 | /// Add EC-Curve. |
| 59 | pub fn ec_curve(mut self, e: EcCurve) -> Self { |
| 60 | self.0.push(KeyParameter { tag: Tag::EC_CURVE, value: KeyParameterValue::EcCurve(e) }); |
| 61 | self |
| 62 | } |
| 63 | |
| 64 | /// Add Attestation-Challenge. |
| 65 | pub fn attestation_challenge(mut self, b: Vec<u8>) -> Self { |
| 66 | self.0.push(KeyParameter { |
| 67 | tag: Tag::ATTESTATION_CHALLENGE, |
| 68 | value: KeyParameterValue::Blob(b), |
| 69 | }); |
| 70 | self |
| 71 | } |
| 72 | |
| 73 | /// Add Attestation-ID. |
| 74 | pub fn attestation_app_id(mut self, b: Vec<u8>) -> Self { |
| 75 | self.0.push(KeyParameter { |
| 76 | tag: Tag::ATTESTATION_APPLICATION_ID, |
| 77 | value: KeyParameterValue::Blob(b), |
| 78 | }); |
| 79 | self |
| 80 | } |
Rajesh Nyamagoud | b881d51 | 2021-12-10 00:33:15 +0000 | [diff] [blame^] | 81 | |
| 82 | /// Add No_auth_required. |
| 83 | pub fn no_auth_required(mut self) -> Self { |
| 84 | self.0.push(KeyParameter { |
| 85 | tag: Tag::NO_AUTH_REQUIRED, |
| 86 | value: KeyParameterValue::BoolValue(true), |
| 87 | }); |
| 88 | self |
| 89 | } |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | impl Deref for AuthSetBuilder { |
| 93 | type Target = Vec<KeyParameter>; |
| 94 | |
| 95 | fn deref(&self) -> &Self::Target { |
| 96 | &self.0 |
| 97 | } |
| 98 | } |