blob: 4fbe1241e862a6b6bce695594bb53b35d546533c [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.
25pub struct AuthSetBuilder(Vec<KeyParameter>);
26
27impl Default for AuthSetBuilder {
28 fn default() -> Self {
29 Self::new()
30 }
31}
32
33impl AuthSetBuilder {
34 /// Creates new Authorizations list.
35 pub fn new() -> Self {
36 Self(Vec::new())
37 }
38
39 /// Add Purpose.
40 pub fn purpose(mut self, p: KeyPurpose) -> Self {
41 self.0.push(KeyParameter { tag: Tag::PURPOSE, value: KeyParameterValue::KeyPurpose(p) });
42 self
43 }
44
45 /// Add Digest.
46 pub fn digest(mut self, d: Digest) -> Self {
47 self.0.push(KeyParameter { tag: Tag::DIGEST, value: KeyParameterValue::Digest(d) });
48 self
49 }
50
51 /// Add Algorithm.
52 pub fn algorithm(mut self, a: Algorithm) -> Self {
53 self.0.push(KeyParameter { tag: Tag::ALGORITHM, value: KeyParameterValue::Algorithm(a) });
54 self
55 }
56
57 /// Add EC-Curve.
58 pub fn ec_curve(mut self, e: EcCurve) -> Self {
59 self.0.push(KeyParameter { tag: Tag::EC_CURVE, value: KeyParameterValue::EcCurve(e) });
60 self
61 }
62
63 /// Add Attestation-Challenge.
64 pub fn attestation_challenge(mut self, b: Vec<u8>) -> Self {
65 self.0.push(KeyParameter {
66 tag: Tag::ATTESTATION_CHALLENGE,
67 value: KeyParameterValue::Blob(b),
68 });
69 self
70 }
71
72 /// Add Attestation-ID.
73 pub fn attestation_app_id(mut self, b: Vec<u8>) -> Self {
74 self.0.push(KeyParameter {
75 tag: Tag::ATTESTATION_APPLICATION_ID,
76 value: KeyParameterValue::Blob(b),
77 });
78 self
79 }
80}
81
82impl Deref for AuthSetBuilder {
83 type Target = Vec<KeyParameter>;
84
85 fn deref(&self) -> &Self::Target {
86 &self.0
87 }
88}