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::{ |
Rajesh Nyamagoud | 11912ea | 2021-12-20 20:37:20 +0000 | [diff] [blame] | 20 | Algorithm::Algorithm, BlockMode::BlockMode, Digest::Digest, EcCurve::EcCurve, |
| 21 | KeyParameter::KeyParameter, KeyParameterValue::KeyParameterValue, KeyPurpose::KeyPurpose, |
| 22 | PaddingMode::PaddingMode, Tag::Tag, |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | /// Helper struct to create set of Authorizations. |
Rajesh Nyamagoud | b881d51 | 2021-12-10 00:33:15 +0000 | [diff] [blame] | 26 | #[derive(Debug, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)] |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 27 | pub struct AuthSetBuilder(Vec<KeyParameter>); |
| 28 | |
| 29 | impl Default for AuthSetBuilder { |
| 30 | fn default() -> Self { |
| 31 | Self::new() |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl AuthSetBuilder { |
| 36 | /// Creates new Authorizations list. |
| 37 | pub fn new() -> Self { |
| 38 | Self(Vec::new()) |
| 39 | } |
| 40 | |
| 41 | /// Add Purpose. |
| 42 | pub fn purpose(mut self, p: KeyPurpose) -> Self { |
| 43 | self.0.push(KeyParameter { tag: Tag::PURPOSE, value: KeyParameterValue::KeyPurpose(p) }); |
| 44 | self |
| 45 | } |
| 46 | |
| 47 | /// Add Digest. |
| 48 | pub fn digest(mut self, d: Digest) -> Self { |
| 49 | self.0.push(KeyParameter { tag: Tag::DIGEST, value: KeyParameterValue::Digest(d) }); |
| 50 | self |
| 51 | } |
| 52 | |
| 53 | /// Add Algorithm. |
| 54 | pub fn algorithm(mut self, a: Algorithm) -> Self { |
| 55 | self.0.push(KeyParameter { tag: Tag::ALGORITHM, value: KeyParameterValue::Algorithm(a) }); |
| 56 | self |
| 57 | } |
| 58 | |
| 59 | /// Add EC-Curve. |
| 60 | pub fn ec_curve(mut self, e: EcCurve) -> Self { |
| 61 | self.0.push(KeyParameter { tag: Tag::EC_CURVE, value: KeyParameterValue::EcCurve(e) }); |
| 62 | self |
| 63 | } |
| 64 | |
| 65 | /// Add Attestation-Challenge. |
| 66 | pub fn attestation_challenge(mut self, b: Vec<u8>) -> Self { |
| 67 | self.0.push(KeyParameter { |
| 68 | tag: Tag::ATTESTATION_CHALLENGE, |
| 69 | value: KeyParameterValue::Blob(b), |
| 70 | }); |
| 71 | self |
| 72 | } |
| 73 | |
| 74 | /// Add Attestation-ID. |
| 75 | pub fn attestation_app_id(mut self, b: Vec<u8>) -> Self { |
| 76 | self.0.push(KeyParameter { |
| 77 | tag: Tag::ATTESTATION_APPLICATION_ID, |
| 78 | value: KeyParameterValue::Blob(b), |
| 79 | }); |
| 80 | self |
| 81 | } |
Rajesh Nyamagoud | b881d51 | 2021-12-10 00:33:15 +0000 | [diff] [blame] | 82 | |
| 83 | /// Add No_auth_required. |
| 84 | pub fn no_auth_required(mut self) -> Self { |
| 85 | self.0.push(KeyParameter { |
| 86 | tag: Tag::NO_AUTH_REQUIRED, |
| 87 | value: KeyParameterValue::BoolValue(true), |
| 88 | }); |
| 89 | self |
| 90 | } |
Rajesh Nyamagoud | 11912ea | 2021-12-20 20:37:20 +0000 | [diff] [blame] | 91 | |
| 92 | /// Add RSA_public_exponent. |
| 93 | pub fn rsa_public_exponent(mut self, e: i64) -> Self { |
| 94 | self.0.push(KeyParameter { |
| 95 | tag: Tag::RSA_PUBLIC_EXPONENT, |
| 96 | value: KeyParameterValue::LongInteger(e), |
| 97 | }); |
| 98 | self |
| 99 | } |
| 100 | |
| 101 | /// Add key size. |
| 102 | pub fn key_size(mut self, s: i32) -> Self { |
| 103 | self.0.push(KeyParameter { tag: Tag::KEY_SIZE, value: KeyParameterValue::Integer(s) }); |
| 104 | self |
| 105 | } |
| 106 | |
| 107 | /// Add block mode. |
| 108 | pub fn block_mode(mut self, b: BlockMode) -> Self { |
| 109 | self.0.push(KeyParameter { tag: Tag::BLOCK_MODE, value: KeyParameterValue::BlockMode(b) }); |
| 110 | self |
| 111 | } |
| 112 | |
| 113 | /// Add certificate_not_before. |
| 114 | pub fn cert_not_before(mut self, b: i64) -> Self { |
| 115 | self.0.push(KeyParameter { |
| 116 | tag: Tag::CERTIFICATE_NOT_BEFORE, |
| 117 | value: KeyParameterValue::DateTime(b), |
| 118 | }); |
| 119 | self |
| 120 | } |
| 121 | |
| 122 | /// Add certificate_not_after. |
| 123 | pub fn cert_not_after(mut self, a: i64) -> Self { |
| 124 | self.0.push(KeyParameter { |
| 125 | tag: Tag::CERTIFICATE_NOT_AFTER, |
| 126 | value: KeyParameterValue::DateTime(a), |
| 127 | }); |
| 128 | self |
| 129 | } |
| 130 | |
| 131 | /// Add padding mode. |
| 132 | pub fn padding_mode(mut self, p: PaddingMode) -> Self { |
| 133 | self.0.push(KeyParameter { tag: Tag::PADDING, value: KeyParameterValue::PaddingMode(p) }); |
| 134 | self |
| 135 | } |
| 136 | |
| 137 | /// Add mgf_digest. |
| 138 | pub fn mgf_digest(mut self, d: Digest) -> Self { |
| 139 | self.0.push(KeyParameter { |
| 140 | tag: Tag::RSA_OAEP_MGF_DIGEST, |
| 141 | value: KeyParameterValue::Digest(d), |
| 142 | }); |
| 143 | self |
| 144 | } |
Rajesh Nyamagoud | 4740993 | 2022-01-08 00:37:13 +0000 | [diff] [blame^] | 145 | |
| 146 | /// Add nonce. |
| 147 | pub fn nonce(mut self, b: Vec<u8>) -> Self { |
| 148 | self.0.push(KeyParameter { tag: Tag::NONCE, value: KeyParameterValue::Blob(b) }); |
| 149 | self |
| 150 | } |
| 151 | |
| 152 | /// Add MAC length. |
| 153 | pub fn mac_length(mut self, l: i32) -> Self { |
| 154 | self.0.push(KeyParameter { tag: Tag::MAC_LENGTH, value: KeyParameterValue::Integer(l) }); |
| 155 | self |
| 156 | } |
| 157 | |
| 158 | /// Add min MAC length. |
| 159 | pub fn min_mac_length(mut self, l: i32) -> Self { |
| 160 | self.0 |
| 161 | .push(KeyParameter { tag: Tag::MIN_MAC_LENGTH, value: KeyParameterValue::Integer(l) }); |
| 162 | self |
| 163 | } |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | impl Deref for AuthSetBuilder { |
| 167 | type Target = Vec<KeyParameter>; |
| 168 | |
| 169 | fn deref(&self) -> &Self::Target { |
| 170 | &self.0 |
| 171 | } |
| 172 | } |