blob: d5a7b7b11f370db840d94bf3ef039f581fc16de3 [file] [log] [blame]
Rajesh Nyamagoud901386c2022-03-21 20:35:18 +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
15//! This module implements test utils to create Autherizations.
16
17use std::ops::Deref;
18
19use 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 Nyamagoudb881d512021-12-10 00:33:15 +000025#[derive(Debug, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
Rajesh Nyamagoud901386c2022-03-21 20:35:18 +000026pub struct AuthSetBuilder(Vec<KeyParameter>);
27
28impl Default for AuthSetBuilder {
29 fn default() -> Self {
30 Self::new()
31 }
32}
33
34impl 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 Nyamagoudb881d512021-12-10 00:33:15 +000081
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 Nyamagoud901386c2022-03-21 20:35:18 +000090}
91
92impl Deref for AuthSetBuilder {
93 type Target = Vec<KeyParameter>;
94
95 fn deref(&self) -> &Self::Target {
96 &self.0
97 }
98}