Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 1 | // Copyright 2020, 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 | //! KeyParameter is used to express different characteristics of a key requested by the user |
| 16 | //! and enforced by the OEMs. This module implements the internal representation of KeyParameter |
| 17 | //! and the methods to work with KeyParameter. |
| 18 | |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 19 | use crate::error::Error as KeystoreError; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 20 | use crate::error::ResponseCode; |
| 21 | |
Shawn Willden | 708744a | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 22 | pub use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 23 | Algorithm::Algorithm, BlockMode::BlockMode, Digest::Digest, EcCurve::EcCurve, |
| 24 | HardwareAuthenticatorType::HardwareAuthenticatorType, KeyOrigin::KeyOrigin, |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 25 | KeyParameter::KeyParameter as KmKeyParameter, |
| 26 | KeyParameterValue::KeyParameterValue as KmKeyParameterValue, KeyPurpose::KeyPurpose, |
| 27 | PaddingMode::PaddingMode, SecurityLevel::SecurityLevel, Tag::Tag, |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 28 | }; |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 29 | use android_system_keystore2::aidl::android::system::keystore2::Authorization::Authorization; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 30 | use anyhow::{Context, Result}; |
| 31 | use rusqlite::types::{FromSql, Null, ToSql, ToSqlOutput}; |
| 32 | use rusqlite::{Result as SqlResult, Row}; |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 33 | |
| 34 | /// KeyParameter wraps the KeyParameterValue and the security level at which it is enforced. |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 35 | #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)] |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 36 | pub struct KeyParameter { |
| 37 | key_parameter_value: KeyParameterValue, |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 38 | security_level: SecurityLevel, |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /// KeyParameterValue holds a value corresponding to one of the Tags defined in |
| 42 | /// the AIDL spec at hardware/interfaces/keymint |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 43 | #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)] |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 44 | pub enum KeyParameterValue { |
| 45 | /// Associated with Tag:INVALID |
| 46 | Invalid, |
| 47 | /// Set of purposes for which the key may be used |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 48 | KeyPurpose(KeyPurpose), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 49 | /// Cryptographic algorithm with which the key is used |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 50 | Algorithm(Algorithm), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 51 | /// Size of the key , in bits |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 52 | KeySize(i32), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 53 | /// Block cipher mode(s) with which the key may be used |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 54 | BlockMode(BlockMode), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 55 | /// Digest algorithms that may be used with the key to perform signing and verification |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 56 | Digest(Digest), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 57 | /// Padding modes that may be used with the key. Relevant to RSA, AES and 3DES keys. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 58 | PaddingMode(PaddingMode), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 59 | /// Can the caller provide a nonce for nonce-requiring operations |
| 60 | CallerNonce, |
| 61 | /// Minimum length of MAC for HMAC keys and AES keys that support GCM mode |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 62 | MinMacLength(i32), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 63 | /// The elliptic curve |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 64 | EcCurve(EcCurve), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 65 | /// Value of the public exponent for an RSA key pair |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 66 | RSAPublicExponent(i64), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 67 | /// An attestation certificate for the generated key should contain an application-scoped |
| 68 | /// and time-bounded device-unique ID |
| 69 | IncludeUniqueID, |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 70 | //TODO: find out about this |
| 71 | // /// Necessary system environment conditions for the generated key to be used |
| 72 | // KeyBlobUsageRequirements(KeyBlobUsageRequirements), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 73 | /// Only the boot loader can use the key |
| 74 | BootLoaderOnly, |
| 75 | /// When deleted, the key is guaranteed to be permanently deleted and unusable |
| 76 | RollbackResistance, |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 77 | /// The date and time at which the key becomes active |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 78 | ActiveDateTime(i64), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 79 | /// The date and time at which the key expires for signing and encryption |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 80 | OriginationExpireDateTime(i64), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 81 | /// The date and time at which the key expires for verification and decryption |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 82 | UsageExpireDateTime(i64), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 83 | /// Minimum amount of time that elapses between allowed operations |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 84 | MinSecondsBetweenOps(i32), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 85 | /// Maximum number of times that a key may be used between system reboots |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 86 | MaxUsesPerBoot(i32), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 87 | /// ID of the Android user that is permitted to use the key |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 88 | UserID(i32), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 89 | /// A key may only be used under a particular secure user authentication state |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 90 | UserSecureID(i64), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 91 | /// No authentication is required to use this key |
| 92 | NoAuthRequired, |
| 93 | /// The types of user authenticators that may be used to authorize this key |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 94 | HardwareAuthenticatorType(HardwareAuthenticatorType), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 95 | /// The time in seconds for which the key is authorized for use, after user authentication |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 96 | AuthTimeout(i32), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 97 | /// The key may be used after authentication timeout if device is still on-body |
| 98 | AllowWhileOnBody, |
| 99 | /// The key must be unusable except when the user has provided proof of physical presence |
| 100 | TrustedUserPresenceRequired, |
| 101 | /// Applicable to keys with KeyPurpose SIGN, and specifies that this key must not be usable |
| 102 | /// unless the user provides confirmation of the data to be signed |
| 103 | TrustedConfirmationRequired, |
| 104 | /// The key may only be used when the device is unlocked |
| 105 | UnlockedDeviceRequired, |
| 106 | /// When provided to generateKey or importKey, this tag specifies data |
| 107 | /// that is necessary during all uses of the key |
| 108 | ApplicationID(Vec<u8>), |
| 109 | /// When provided to generateKey or importKey, this tag specifies data |
| 110 | /// that is necessary during all uses of the key |
| 111 | ApplicationData(Vec<u8>), |
| 112 | /// Specifies the date and time the key was created |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 113 | CreationDateTime(i64), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 114 | /// Specifies where the key was created, if known |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 115 | KeyOrigin(KeyOrigin), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 116 | /// The key used by verified boot to validate the operating system booted |
| 117 | RootOfTrust(Vec<u8>), |
| 118 | /// System OS version with which the key may be used |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 119 | OSVersion(i32), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 120 | /// Specifies the system security patch level with which the key may be used |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 121 | OSPatchLevel(i32), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 122 | /// Specifies a unique, time-based identifier |
| 123 | UniqueID(Vec<u8>), |
| 124 | /// Used to deliver a "challenge" value to the attestKey() method |
| 125 | AttestationChallenge(Vec<u8>), |
| 126 | /// The set of applications which may use a key, used only with attestKey() |
| 127 | AttestationApplicationID(Vec<u8>), |
| 128 | /// Provides the device's brand name, to attestKey() |
| 129 | AttestationIdBrand(Vec<u8>), |
| 130 | /// Provides the device's device name, to attestKey() |
| 131 | AttestationIdDevice(Vec<u8>), |
| 132 | /// Provides the device's product name, to attestKey() |
| 133 | AttestationIdProduct(Vec<u8>), |
| 134 | /// Provides the device's serial number, to attestKey() |
| 135 | AttestationIdSerial(Vec<u8>), |
| 136 | /// Provides the IMEIs for all radios on the device, to attestKey() |
| 137 | AttestationIdIMEI(Vec<u8>), |
| 138 | /// Provides the MEIDs for all radios on the device, to attestKey() |
| 139 | AttestationIdMEID(Vec<u8>), |
| 140 | /// Provides the device's manufacturer name, to attestKey() |
| 141 | AttestationIdManufacturer(Vec<u8>), |
| 142 | /// Provides the device's model name, to attestKey() |
| 143 | AttestationIdModel(Vec<u8>), |
| 144 | /// Specifies the vendor image security patch level with which the key may be used |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 145 | VendorPatchLevel(i32), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 146 | /// Specifies the boot image (kernel) security patch level with which the key may be used |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 147 | BootPatchLevel(i32), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 148 | /// Provides "associated data" for AES-GCM encryption or decryption |
| 149 | AssociatedData(Vec<u8>), |
| 150 | /// Provides or returns a nonce or Initialization Vector (IV) for AES-GCM, |
| 151 | /// AES-CBC, AES-CTR, or 3DES-CBC encryption or decryption |
| 152 | Nonce(Vec<u8>), |
| 153 | /// Provides the requested length of a MAC or GCM authentication tag, in bits |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 154 | MacLength(i32), |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 155 | /// Specifies whether the device has been factory reset since the |
| 156 | /// last unique ID rotation. Used for key attestation |
| 157 | ResetSinceIdRotation, |
| 158 | /// Used to deliver a cryptographic token proving that the user |
| 159 | /// confirmed a signing request |
| 160 | ConfirmationToken(Vec<u8>), |
| 161 | } |
| 162 | |
| 163 | impl KeyParameter { |
| 164 | /// Create an instance of KeyParameter, given the value and the security level. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 165 | pub fn new(key_parameter_value: KeyParameterValue, security_level: SecurityLevel) -> Self { |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 166 | KeyParameter { key_parameter_value, security_level } |
| 167 | } |
| 168 | |
| 169 | /// Returns the tag given the KeyParameter instance. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 170 | pub fn get_tag(&self) -> Tag { |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 171 | match self.key_parameter_value { |
| 172 | KeyParameterValue::Invalid => Tag::INVALID, |
| 173 | KeyParameterValue::KeyPurpose(_) => Tag::PURPOSE, |
| 174 | KeyParameterValue::Algorithm(_) => Tag::ALGORITHM, |
| 175 | KeyParameterValue::KeySize(_) => Tag::KEY_SIZE, |
| 176 | KeyParameterValue::BlockMode(_) => Tag::BLOCK_MODE, |
| 177 | KeyParameterValue::Digest(_) => Tag::DIGEST, |
| 178 | KeyParameterValue::PaddingMode(_) => Tag::PADDING, |
| 179 | KeyParameterValue::CallerNonce => Tag::CALLER_NONCE, |
| 180 | KeyParameterValue::MinMacLength(_) => Tag::MIN_MAC_LENGTH, |
| 181 | KeyParameterValue::EcCurve(_) => Tag::EC_CURVE, |
| 182 | KeyParameterValue::RSAPublicExponent(_) => Tag::RSA_PUBLIC_EXPONENT, |
| 183 | KeyParameterValue::IncludeUniqueID => Tag::INCLUDE_UNIQUE_ID, |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 184 | KeyParameterValue::BootLoaderOnly => Tag::BOOTLOADER_ONLY, |
| 185 | KeyParameterValue::RollbackResistance => Tag::ROLLBACK_RESISTANCE, |
| 186 | KeyParameterValue::ActiveDateTime(_) => Tag::ACTIVE_DATETIME, |
| 187 | KeyParameterValue::OriginationExpireDateTime(_) => Tag::ORIGINATION_EXPIRE_DATETIME, |
| 188 | KeyParameterValue::UsageExpireDateTime(_) => Tag::USAGE_EXPIRE_DATETIME, |
| 189 | KeyParameterValue::MinSecondsBetweenOps(_) => Tag::MIN_SECONDS_BETWEEN_OPS, |
| 190 | KeyParameterValue::MaxUsesPerBoot(_) => Tag::MAX_USES_PER_BOOT, |
| 191 | KeyParameterValue::UserID(_) => Tag::USER_ID, |
| 192 | KeyParameterValue::UserSecureID(_) => Tag::USER_SECURE_ID, |
| 193 | KeyParameterValue::NoAuthRequired => Tag::NO_AUTH_REQUIRED, |
| 194 | KeyParameterValue::HardwareAuthenticatorType(_) => Tag::USER_AUTH_TYPE, |
| 195 | KeyParameterValue::AuthTimeout(_) => Tag::AUTH_TIMEOUT, |
| 196 | KeyParameterValue::AllowWhileOnBody => Tag::ALLOW_WHILE_ON_BODY, |
| 197 | KeyParameterValue::TrustedUserPresenceRequired => Tag::TRUSTED_USER_PRESENCE_REQUIRED, |
| 198 | KeyParameterValue::TrustedConfirmationRequired => Tag::TRUSTED_CONFIRMATION_REQUIRED, |
| 199 | KeyParameterValue::UnlockedDeviceRequired => Tag::UNLOCKED_DEVICE_REQUIRED, |
| 200 | KeyParameterValue::ApplicationID(_) => Tag::APPLICATION_ID, |
| 201 | KeyParameterValue::ApplicationData(_) => Tag::APPLICATION_DATA, |
| 202 | KeyParameterValue::CreationDateTime(_) => Tag::CREATION_DATETIME, |
| 203 | KeyParameterValue::KeyOrigin(_) => Tag::ORIGIN, |
| 204 | KeyParameterValue::RootOfTrust(_) => Tag::ROOT_OF_TRUST, |
| 205 | KeyParameterValue::OSVersion(_) => Tag::OS_VERSION, |
| 206 | KeyParameterValue::OSPatchLevel(_) => Tag::OS_PATCHLEVEL, |
| 207 | KeyParameterValue::UniqueID(_) => Tag::UNIQUE_ID, |
| 208 | KeyParameterValue::AttestationChallenge(_) => Tag::ATTESTATION_CHALLENGE, |
| 209 | KeyParameterValue::AttestationApplicationID(_) => Tag::ATTESTATION_APPLICATION_ID, |
| 210 | KeyParameterValue::AttestationIdBrand(_) => Tag::ATTESTATION_ID_BRAND, |
| 211 | KeyParameterValue::AttestationIdDevice(_) => Tag::ATTESTATION_ID_DEVICE, |
| 212 | KeyParameterValue::AttestationIdProduct(_) => Tag::ATTESTATION_ID_PRODUCT, |
| 213 | KeyParameterValue::AttestationIdSerial(_) => Tag::ATTESTATION_ID_SERIAL, |
| 214 | KeyParameterValue::AttestationIdIMEI(_) => Tag::ATTESTATION_ID_IMEI, |
| 215 | KeyParameterValue::AttestationIdMEID(_) => Tag::ATTESTATION_ID_MEID, |
| 216 | KeyParameterValue::AttestationIdManufacturer(_) => Tag::ATTESTATION_ID_MANUFACTURER, |
| 217 | KeyParameterValue::AttestationIdModel(_) => Tag::ATTESTATION_ID_MODEL, |
| 218 | KeyParameterValue::VendorPatchLevel(_) => Tag::VENDOR_PATCHLEVEL, |
| 219 | KeyParameterValue::BootPatchLevel(_) => Tag::BOOT_PATCHLEVEL, |
| 220 | KeyParameterValue::AssociatedData(_) => Tag::ASSOCIATED_DATA, |
| 221 | KeyParameterValue::Nonce(_) => Tag::NONCE, |
| 222 | KeyParameterValue::MacLength(_) => Tag::MAC_LENGTH, |
| 223 | KeyParameterValue::ResetSinceIdRotation => Tag::RESET_SINCE_ID_ROTATION, |
| 224 | KeyParameterValue::ConfirmationToken(_) => Tag::CONFIRMATION_TOKEN, |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /// Returns key parameter value. |
| 229 | pub fn key_parameter_value(&self) -> &KeyParameterValue { |
| 230 | &self.key_parameter_value |
| 231 | } |
| 232 | |
| 233 | /// Returns the security level of a KeyParameter. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 234 | pub fn security_level(&self) -> &SecurityLevel { |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 235 | &self.security_level |
| 236 | } |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 237 | |
| 238 | /// An authorization is a KeyParameter with an associated security level that is used |
| 239 | /// to convey the key characteristics to keystore clients. This function consumes |
| 240 | /// an internal KeyParameter representation to produce the Authorization wire type. |
| 241 | pub fn into_authorization(self) -> Authorization { |
| 242 | Authorization { |
Janis Danisevskis | a53c9cf | 2020-10-26 11:52:33 -0700 | [diff] [blame] | 243 | securityLevel: self.security_level, |
| 244 | keyParameter: self.key_parameter_value.convert_to_wire(), |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
Hasini Gunasinghe | 1248636 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 249 | /// This struct is defined to postpone converting rusqlite column value to the |
| 250 | /// appropriate key parameter value until we know the corresponding tag value. |
| 251 | /// Wraps the column index and a rusqlite row. |
| 252 | pub struct SqlField<'a>(usize, &'a Row<'a>); |
| 253 | |
| 254 | impl<'a> SqlField<'a> { |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 255 | /// Creates a new SqlField with the given index and row. |
| 256 | pub fn new(index: usize, row: &'a Row<'a>) -> Self { |
| 257 | Self(index, row) |
| 258 | } |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 259 | /// Returns the column value from the row, when we know the expected type. |
| 260 | pub fn get<T: FromSql>(&self) -> SqlResult<T> { |
| 261 | self.1.get(self.0) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | impl ToSql for KeyParameterValue { |
| 266 | /// Converts KeyParameterValue to be stored in rusqlite database. |
| 267 | /// Note that following variants of KeyParameterValue should not be stored: |
| 268 | /// IncludeUniqueID, ApplicationID, ApplicationData, RootOfTrust, UniqueID, |
| 269 | /// Attestation*, AssociatedData, Nonce, MacLength, ResetSinceIdRotation, ConfirmationToken. |
| 270 | /// This filtering is enforced at a higher level (i.e. enforcement module) and here we support |
| 271 | /// conversion for all the variants, to keep error handling simple. |
| 272 | fn to_sql(&self) -> SqlResult<ToSqlOutput> { |
| 273 | match self { |
| 274 | KeyParameterValue::Invalid => Ok(ToSqlOutput::from(Null)), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 275 | KeyParameterValue::KeyPurpose(k) => Ok(ToSqlOutput::from(k.0 as u32)), |
| 276 | KeyParameterValue::Algorithm(a) => Ok(ToSqlOutput::from(a.0 as u32)), |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 277 | KeyParameterValue::KeySize(k) => Ok(ToSqlOutput::from(*k)), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 278 | KeyParameterValue::BlockMode(b) => Ok(ToSqlOutput::from(b.0 as u32)), |
| 279 | KeyParameterValue::Digest(d) => Ok(ToSqlOutput::from(d.0 as u32)), |
| 280 | KeyParameterValue::PaddingMode(p) => Ok(ToSqlOutput::from(p.0 as u32)), |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 281 | KeyParameterValue::CallerNonce => Ok(ToSqlOutput::from(Null)), |
| 282 | KeyParameterValue::MinMacLength(m) => Ok(ToSqlOutput::from(*m)), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 283 | KeyParameterValue::EcCurve(e) => Ok(ToSqlOutput::from(e.0 as u32)), |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 284 | KeyParameterValue::RSAPublicExponent(r) => Ok(ToSqlOutput::from(*r as i64)), |
| 285 | KeyParameterValue::IncludeUniqueID => Ok(ToSqlOutput::from(Null)), |
| 286 | KeyParameterValue::BootLoaderOnly => Ok(ToSqlOutput::from(Null)), |
| 287 | KeyParameterValue::RollbackResistance => Ok(ToSqlOutput::from(Null)), |
| 288 | KeyParameterValue::ActiveDateTime(a) => Ok(ToSqlOutput::from(*a as i64)), |
| 289 | KeyParameterValue::OriginationExpireDateTime(o) => Ok(ToSqlOutput::from(*o as i64)), |
| 290 | KeyParameterValue::UsageExpireDateTime(u) => Ok(ToSqlOutput::from(*u as i64)), |
| 291 | KeyParameterValue::MinSecondsBetweenOps(m) => Ok(ToSqlOutput::from(*m)), |
| 292 | KeyParameterValue::MaxUsesPerBoot(m) => Ok(ToSqlOutput::from(*m)), |
| 293 | KeyParameterValue::UserID(u) => Ok(ToSqlOutput::from(*u)), |
| 294 | KeyParameterValue::UserSecureID(u) => Ok(ToSqlOutput::from(*u as i64)), |
| 295 | KeyParameterValue::NoAuthRequired => Ok(ToSqlOutput::from(Null)), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 296 | KeyParameterValue::HardwareAuthenticatorType(h) => Ok(ToSqlOutput::from(h.0 as u32)), |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 297 | KeyParameterValue::AuthTimeout(m) => Ok(ToSqlOutput::from(*m)), |
| 298 | KeyParameterValue::AllowWhileOnBody => Ok(ToSqlOutput::from(Null)), |
| 299 | KeyParameterValue::TrustedUserPresenceRequired => Ok(ToSqlOutput::from(Null)), |
| 300 | KeyParameterValue::TrustedConfirmationRequired => Ok(ToSqlOutput::from(Null)), |
| 301 | KeyParameterValue::UnlockedDeviceRequired => Ok(ToSqlOutput::from(Null)), |
| 302 | KeyParameterValue::ApplicationID(a) => Ok(ToSqlOutput::from(a.to_vec())), |
| 303 | KeyParameterValue::ApplicationData(a) => Ok(ToSqlOutput::from(a.to_vec())), |
| 304 | KeyParameterValue::CreationDateTime(c) => Ok(ToSqlOutput::from(*c as i64)), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 305 | KeyParameterValue::KeyOrigin(k) => Ok(ToSqlOutput::from(k.0 as u32)), |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 306 | KeyParameterValue::RootOfTrust(r) => Ok(ToSqlOutput::from(r.to_vec())), |
| 307 | KeyParameterValue::OSVersion(o) => Ok(ToSqlOutput::from(*o)), |
| 308 | KeyParameterValue::OSPatchLevel(o) => Ok(ToSqlOutput::from(*o)), |
| 309 | KeyParameterValue::UniqueID(u) => Ok(ToSqlOutput::from(u.to_vec())), |
| 310 | KeyParameterValue::AttestationChallenge(a) => Ok(ToSqlOutput::from(a.to_vec())), |
| 311 | KeyParameterValue::AttestationApplicationID(a) => Ok(ToSqlOutput::from(a.to_vec())), |
| 312 | KeyParameterValue::AttestationIdBrand(a) => Ok(ToSqlOutput::from(a.to_vec())), |
| 313 | KeyParameterValue::AttestationIdDevice(a) => Ok(ToSqlOutput::from(a.to_vec())), |
| 314 | KeyParameterValue::AttestationIdProduct(a) => Ok(ToSqlOutput::from(a.to_vec())), |
| 315 | KeyParameterValue::AttestationIdSerial(a) => Ok(ToSqlOutput::from(a.to_vec())), |
| 316 | KeyParameterValue::AttestationIdIMEI(a) => Ok(ToSqlOutput::from(a.to_vec())), |
| 317 | KeyParameterValue::AttestationIdMEID(a) => Ok(ToSqlOutput::from(a.to_vec())), |
| 318 | KeyParameterValue::AttestationIdManufacturer(a) => Ok(ToSqlOutput::from(a.to_vec())), |
| 319 | KeyParameterValue::AttestationIdModel(a) => Ok(ToSqlOutput::from(a.to_vec())), |
| 320 | KeyParameterValue::VendorPatchLevel(v) => Ok(ToSqlOutput::from(*v)), |
| 321 | KeyParameterValue::BootPatchLevel(b) => Ok(ToSqlOutput::from(*b)), |
| 322 | KeyParameterValue::AssociatedData(a) => Ok(ToSqlOutput::from(a.to_vec())), |
| 323 | KeyParameterValue::Nonce(n) => Ok(ToSqlOutput::from(n.to_vec())), |
| 324 | KeyParameterValue::MacLength(m) => Ok(ToSqlOutput::from(*m)), |
| 325 | KeyParameterValue::ResetSinceIdRotation => Ok(ToSqlOutput::from(Null)), |
| 326 | KeyParameterValue::ConfirmationToken(c) => Ok(ToSqlOutput::from(c.to_vec())), |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 331 | impl KeyParameter { |
| 332 | /// Construct a KeyParameter from the data from a rusqlite row. |
| 333 | /// Note that following variants of KeyParameterValue should not be stored: |
| 334 | /// IncludeUniqueID, ApplicationID, ApplicationData, RootOfTrust, UniqueID, |
| 335 | /// Attestation*, AssociatedData, Nonce, MacLength, ResetSinceIdRotation, ConfirmationToken. |
| 336 | /// This filtering is enforced at a higher level and here we support conversion for all the |
| 337 | /// variants. |
| 338 | pub fn new_from_sql( |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 339 | tag_val: Tag, |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 340 | data: &SqlField, |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 341 | security_level_val: SecurityLevel, |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 342 | ) -> Result<Self> { |
| 343 | let key_param_value = match tag_val { |
| 344 | Tag::INVALID => KeyParameterValue::Invalid, |
| 345 | Tag::PURPOSE => { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 346 | let key_purpose: i32 = data |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 347 | .get() |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 348 | .map_err(|_| KeystoreError::Rc(ResponseCode::VALUE_CORRUPTED)) |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 349 | .context("Failed to read sql data for tag: PURPOSE.")?; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 350 | KeyParameterValue::KeyPurpose(KeyPurpose(key_purpose)) |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 351 | } |
| 352 | Tag::ALGORITHM => { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 353 | let algorithm: i32 = data |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 354 | .get() |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 355 | .map_err(|_| KeystoreError::Rc(ResponseCode::VALUE_CORRUPTED)) |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 356 | .context("Failed to read sql data for tag: ALGORITHM.")?; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 357 | KeyParameterValue::Algorithm(Algorithm(algorithm)) |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 358 | } |
| 359 | Tag::KEY_SIZE => { |
| 360 | let key_size: i32 = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 361 | data.get().context("Failed to read sql data for tag: KEY_SIZE.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 362 | KeyParameterValue::KeySize(key_size) |
| 363 | } |
| 364 | Tag::BLOCK_MODE => { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 365 | let block_mode: i32 = data |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 366 | .get() |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 367 | .map_err(|_| KeystoreError::Rc(ResponseCode::VALUE_CORRUPTED)) |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 368 | .context("Failed to read sql data for tag: BLOCK_MODE.")?; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 369 | KeyParameterValue::BlockMode(BlockMode(block_mode)) |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 370 | } |
| 371 | Tag::DIGEST => { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 372 | let digest: i32 = data |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 373 | .get() |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 374 | .map_err(|_| KeystoreError::Rc(ResponseCode::VALUE_CORRUPTED)) |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 375 | .context("Failed to read sql data for tag: DIGEST.")?; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 376 | KeyParameterValue::Digest(Digest(digest)) |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 377 | } |
| 378 | Tag::PADDING => { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 379 | let padding: i32 = data |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 380 | .get() |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 381 | .map_err(|_| KeystoreError::Rc(ResponseCode::VALUE_CORRUPTED)) |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 382 | .context("Failed to read sql data for tag: PADDING.")?; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 383 | KeyParameterValue::PaddingMode(PaddingMode(padding)) |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 384 | } |
| 385 | Tag::CALLER_NONCE => KeyParameterValue::CallerNonce, |
| 386 | Tag::MIN_MAC_LENGTH => { |
| 387 | let min_mac_length: i32 = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 388 | data.get().context("Failed to read sql data for tag: MIN_MAC_LENGTH.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 389 | KeyParameterValue::MinMacLength(min_mac_length) |
| 390 | } |
| 391 | Tag::EC_CURVE => { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 392 | let ec_curve: i32 = data |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 393 | .get() |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 394 | .map_err(|_| KeystoreError::Rc(ResponseCode::VALUE_CORRUPTED)) |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 395 | .context("Failed to read sql data for tag: EC_CURVE.")?; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 396 | KeyParameterValue::EcCurve(EcCurve(ec_curve)) |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 397 | } |
| 398 | Tag::RSA_PUBLIC_EXPONENT => { |
| 399 | let rsa_pub_exponent: i64 = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 400 | data.get().context("Failed to read sql data for tag: RSA_PUBLIC_EXPONENT.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 401 | |
| 402 | KeyParameterValue::RSAPublicExponent(rsa_pub_exponent) |
| 403 | } |
| 404 | Tag::INCLUDE_UNIQUE_ID => KeyParameterValue::IncludeUniqueID, |
| 405 | Tag::BOOTLOADER_ONLY => KeyParameterValue::BootLoaderOnly, |
| 406 | Tag::ROLLBACK_RESISTANCE => KeyParameterValue::RollbackResistance, |
| 407 | Tag::ACTIVE_DATETIME => { |
| 408 | let active_datetime: i64 = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 409 | data.get().context("Failed to read sql data for tag: ACTIVE_DATETIME.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 410 | KeyParameterValue::ActiveDateTime(active_datetime) |
| 411 | } |
| 412 | Tag::ORIGINATION_EXPIRE_DATETIME => { |
| 413 | let origination_expire_datetime: i64 = data |
| 414 | .get() |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 415 | .context("Failed to read sql data for tag: ORIGINATION_EXPIRE_DATETIME.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 416 | KeyParameterValue::OriginationExpireDateTime(origination_expire_datetime) |
| 417 | } |
| 418 | Tag::USAGE_EXPIRE_DATETIME => { |
| 419 | let usage_expire_datetime: i64 = data |
| 420 | .get() |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 421 | .context("Failed to read sql data for tag: USAGE_EXPIRE_DATETIME.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 422 | KeyParameterValue::UsageExpireDateTime(usage_expire_datetime) |
| 423 | } |
| 424 | Tag::MIN_SECONDS_BETWEEN_OPS => { |
| 425 | let min_secs_between_ops: i32 = data |
| 426 | .get() |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 427 | .context("Failed to read sql data for tag: MIN_SECONDS_BETWEEN_OPS.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 428 | KeyParameterValue::MinSecondsBetweenOps(min_secs_between_ops) |
| 429 | } |
| 430 | Tag::MAX_USES_PER_BOOT => { |
| 431 | let max_uses_per_boot: i32 = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 432 | data.get().context("Failed to read sql data for tag: MAX_USES_PER_BOOT.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 433 | KeyParameterValue::MaxUsesPerBoot(max_uses_per_boot) |
| 434 | } |
| 435 | Tag::USER_ID => { |
| 436 | let user_id: i32 = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 437 | data.get().context("Failed to read sql data for tag: USER_ID.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 438 | KeyParameterValue::UserID(user_id) |
| 439 | } |
| 440 | Tag::USER_SECURE_ID => { |
| 441 | let user_secure_id: i64 = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 442 | data.get().context("Failed to read sql data for tag: USER_SECURE_ID.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 443 | KeyParameterValue::UserSecureID(user_secure_id) |
| 444 | } |
| 445 | Tag::NO_AUTH_REQUIRED => KeyParameterValue::NoAuthRequired, |
| 446 | Tag::USER_AUTH_TYPE => { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 447 | let user_auth_type: i32 = data |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 448 | .get() |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 449 | .map_err(|_| KeystoreError::Rc(ResponseCode::VALUE_CORRUPTED)) |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 450 | .context("Failed to read sql data for tag: USER_AUTH_TYPE.")?; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 451 | KeyParameterValue::HardwareAuthenticatorType(HardwareAuthenticatorType( |
| 452 | user_auth_type, |
| 453 | )) |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 454 | } |
| 455 | Tag::AUTH_TIMEOUT => { |
| 456 | let auth_timeout: i32 = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 457 | data.get().context("Failed to read sql data for tag: AUTH_TIMEOUT.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 458 | KeyParameterValue::AuthTimeout(auth_timeout) |
| 459 | } |
| 460 | Tag::ALLOW_WHILE_ON_BODY => KeyParameterValue::AllowWhileOnBody, |
| 461 | Tag::TRUSTED_USER_PRESENCE_REQUIRED => KeyParameterValue::TrustedUserPresenceRequired, |
| 462 | Tag::TRUSTED_CONFIRMATION_REQUIRED => KeyParameterValue::TrustedConfirmationRequired, |
| 463 | Tag::UNLOCKED_DEVICE_REQUIRED => KeyParameterValue::UnlockedDeviceRequired, |
| 464 | Tag::APPLICATION_ID => { |
| 465 | let app_id: Vec<u8> = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 466 | data.get().context("Failed to read sql data for tag: APPLICATION_ID.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 467 | KeyParameterValue::ApplicationID(app_id) |
| 468 | } |
| 469 | Tag::APPLICATION_DATA => { |
| 470 | let app_data: Vec<u8> = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 471 | data.get().context("Failed to read sql data for tag: APPLICATION_DATA.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 472 | KeyParameterValue::ApplicationData(app_data) |
| 473 | } |
| 474 | Tag::CREATION_DATETIME => { |
| 475 | let creation_datetime: i64 = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 476 | data.get().context("Failed to read sql data for tag: CREATION_DATETIME.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 477 | KeyParameterValue::CreationDateTime(creation_datetime) |
| 478 | } |
| 479 | Tag::ORIGIN => { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 480 | let origin: i32 = data |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 481 | .get() |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 482 | .map_err(|_| KeystoreError::Rc(ResponseCode::VALUE_CORRUPTED)) |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 483 | .context("Failed to read sql data for tag: ORIGIN.")?; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 484 | KeyParameterValue::KeyOrigin(KeyOrigin(origin)) |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 485 | } |
| 486 | Tag::ROOT_OF_TRUST => { |
| 487 | let root_of_trust: Vec<u8> = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 488 | data.get().context("Failed to read sql data for tag: ROOT_OF_TRUST.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 489 | KeyParameterValue::RootOfTrust(root_of_trust) |
| 490 | } |
| 491 | Tag::OS_VERSION => { |
| 492 | let os_version: i32 = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 493 | data.get().context("Failed to read sql data for tag: OS_VERSION.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 494 | KeyParameterValue::OSVersion(os_version) |
| 495 | } |
| 496 | Tag::OS_PATCHLEVEL => { |
| 497 | let os_patch_level: i32 = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 498 | data.get().context("Failed to read sql data for tag: OS_PATCHLEVEL.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 499 | KeyParameterValue::OSPatchLevel(os_patch_level) |
| 500 | } |
| 501 | Tag::UNIQUE_ID => { |
| 502 | let unique_id: Vec<u8> = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 503 | data.get().context("Failed to read sql data for tag: UNIQUE_ID.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 504 | KeyParameterValue::UniqueID(unique_id) |
| 505 | } |
| 506 | Tag::ATTESTATION_CHALLENGE => { |
| 507 | let attestation_challenge: Vec<u8> = data |
| 508 | .get() |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 509 | .context("Failed to read sql data for tag: ATTESTATION_CHALLENGE.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 510 | KeyParameterValue::AttestationChallenge(attestation_challenge) |
| 511 | } |
| 512 | Tag::ATTESTATION_APPLICATION_ID => { |
| 513 | let attestation_app_id: Vec<u8> = data |
| 514 | .get() |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 515 | .context("Failed to read sql data for tag: ATTESTATION_APPLICATION_ID.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 516 | KeyParameterValue::AttestationApplicationID(attestation_app_id) |
| 517 | } |
| 518 | Tag::ATTESTATION_ID_BRAND => { |
| 519 | let attestation_id_brand: Vec<u8> = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 520 | data.get().context("Failed to read sql data for tag: ATTESTATION_ID_BRAND.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 521 | KeyParameterValue::AttestationIdBrand(attestation_id_brand) |
| 522 | } |
| 523 | Tag::ATTESTATION_ID_DEVICE => { |
| 524 | let attestation_id_device: Vec<u8> = data |
| 525 | .get() |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 526 | .context("Failed to read sql data for tag: ATTESTATION_ID_DEVICE.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 527 | KeyParameterValue::AttestationIdDevice(attestation_id_device) |
| 528 | } |
| 529 | Tag::ATTESTATION_ID_PRODUCT => { |
| 530 | let attestation_id_product: Vec<u8> = data |
| 531 | .get() |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 532 | .context("Failed to read sql data for tag: ATTESTATION_ID_PRODUCT.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 533 | KeyParameterValue::AttestationIdProduct(attestation_id_product) |
| 534 | } |
| 535 | Tag::ATTESTATION_ID_SERIAL => { |
| 536 | let attestation_id_serial: Vec<u8> = data |
| 537 | .get() |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 538 | .context("Failed to read sql data for tag: ATTESTATION_ID_SERIAL.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 539 | KeyParameterValue::AttestationIdSerial(attestation_id_serial) |
| 540 | } |
| 541 | Tag::ATTESTATION_ID_IMEI => { |
| 542 | let attestation_id_imei: Vec<u8> = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 543 | data.get().context("Failed to read sql data for tag: ATTESTATION_ID_IMEI.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 544 | KeyParameterValue::AttestationIdIMEI(attestation_id_imei) |
| 545 | } |
| 546 | Tag::ATTESTATION_ID_MEID => { |
| 547 | let attestation_id_meid: Vec<u8> = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 548 | data.get().context("Failed to read sql data for tag: ATTESTATION_ID_MEID.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 549 | KeyParameterValue::AttestationIdMEID(attestation_id_meid) |
| 550 | } |
| 551 | Tag::ATTESTATION_ID_MANUFACTURER => { |
| 552 | let attestation_id_manufacturer: Vec<u8> = data |
| 553 | .get() |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 554 | .context("Failed to read sql data for tag: ATTESTATION_ID_MANUFACTURER.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 555 | KeyParameterValue::AttestationIdManufacturer(attestation_id_manufacturer) |
| 556 | } |
| 557 | Tag::ATTESTATION_ID_MODEL => { |
| 558 | let attestation_id_model: Vec<u8> = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 559 | data.get().context("Failed to read sql data for tag: ATTESTATION_ID_MODEL.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 560 | KeyParameterValue::AttestationIdModel(attestation_id_model) |
| 561 | } |
| 562 | Tag::VENDOR_PATCHLEVEL => { |
| 563 | let vendor_patch_level: i32 = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 564 | data.get().context("Failed to read sql data for tag: VENDOR_PATCHLEVEL.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 565 | KeyParameterValue::VendorPatchLevel(vendor_patch_level) |
| 566 | } |
| 567 | Tag::BOOT_PATCHLEVEL => { |
| 568 | let boot_patch_level: i32 = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 569 | data.get().context("Failed to read sql data for tag: BOOT_PATCHLEVEL.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 570 | KeyParameterValue::BootPatchLevel(boot_patch_level) |
| 571 | } |
| 572 | Tag::ASSOCIATED_DATA => { |
| 573 | let associated_data: Vec<u8> = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 574 | data.get().context("Failed to read sql data for tag: ASSOCIATED_DATA.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 575 | KeyParameterValue::AssociatedData(associated_data) |
| 576 | } |
| 577 | Tag::NONCE => { |
| 578 | let nonce: Vec<u8> = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 579 | data.get().context("Failed to read sql data for tag: NONCE.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 580 | KeyParameterValue::Nonce(nonce) |
| 581 | } |
| 582 | Tag::MAC_LENGTH => { |
| 583 | let mac_length: i32 = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 584 | data.get().context("Failed to read sql data for tag: MAC_LENGTH.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 585 | KeyParameterValue::MacLength(mac_length) |
| 586 | } |
| 587 | Tag::RESET_SINCE_ID_ROTATION => KeyParameterValue::ResetSinceIdRotation, |
| 588 | Tag::CONFIRMATION_TOKEN => { |
| 589 | let confirmation_token: Vec<u8> = |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 590 | data.get().context("Failed to read sql data for tag: CONFIRMATION_TOKEN.")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 591 | KeyParameterValue::ConfirmationToken(confirmation_token) |
| 592 | } |
| 593 | _ => { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 594 | return Err(KeystoreError::Rc(ResponseCode::VALUE_CORRUPTED)) |
Joel Galenson | 5c0ec0d | 2020-09-02 14:24:30 -0700 | [diff] [blame] | 595 | .context("Failed to decode Tag enum from value.")? |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 596 | } |
| 597 | }; |
| 598 | Ok(KeyParameter::new(key_param_value, security_level_val)) |
| 599 | } |
| 600 | } |
| 601 | |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 602 | /// Macro rules for converting key parameter to/from wire type. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 603 | /// This macro takes between three and four different pieces of information about each |
| 604 | /// of the KeyParameterValue variants: |
| 605 | /// 1. The KeyParameterValue variant name, |
| 606 | /// 2. the tag name corresponding to the variant, |
| 607 | /// 3. the field name in the KmKeyParameter struct, in which information about this variant is |
| 608 | /// stored when converted, and |
| 609 | /// 4. an optional enum type name when the nested value is of enum type. |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 610 | /// The macro takes a set of lines corresponding to each KeyParameterValue variant and generates |
| 611 | /// the two conversion methods: convert_to_wire() and convert_from_wire(). |
| 612 | /// ## Example |
| 613 | /// ``` |
| 614 | /// implement_key_parameter_conversion_to_from_wire! { |
| 615 | /// Invalid, INVALID, na; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 616 | /// KeyPurpose, PURPOSE, integer, KeyPurpose; |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 617 | /// CallerNonce, CALLER_NONCE, boolValue; |
| 618 | /// UserSecureID, USER_SECURE_ID, longInteger; |
| 619 | /// ApplicationID, APPLICATION_ID, blob; |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 620 | /// } |
| 621 | /// ``` |
| 622 | /// expands to: |
| 623 | /// ``` |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 624 | /// pub fn convert_to_wire(self) -> KmKeyParameter { |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 625 | /// match self { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 626 | /// KeyParameterValue::Invalid => KmKeyParameter { |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 627 | /// tag: Tag::INVALID, |
| 628 | /// ..Default::default() |
| 629 | /// }, |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 630 | /// KeyParameterValue::KeyPurpose(v) => KmKeyParameter { |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 631 | /// tag: Tag::PURPOSE, |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 632 | /// integer: v.0, |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 633 | /// ..Default::default() |
| 634 | /// }, |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 635 | /// KeyParameterValue::CallerNonce => KmKeyParameter { |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 636 | /// tag: Tag::CALLER_NONCE, |
| 637 | /// boolValue: true, |
| 638 | /// ..Default::default() |
| 639 | /// }, |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 640 | /// KeyParameterValue::UserSecureID(v) => KmKeyParameter { |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 641 | /// tag: Tag::USER_SECURE_ID, |
| 642 | /// longInteger: v, |
| 643 | /// ..Default::default() |
| 644 | /// }, |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 645 | /// KeyParameterValue::ApplicationID(v) => KmKeyParameter { |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 646 | /// tag: Tag::APPLICATION_ID, |
| 647 | /// blob: v, |
| 648 | /// ..Default::default() |
| 649 | /// }, |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 650 | /// } |
| 651 | /// } |
| 652 | /// ``` |
| 653 | /// and |
| 654 | /// ``` |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 655 | /// pub fn convert_from_wire(aidl_kp: KmKeyParameter) -> KeyParameterValue { |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 656 | /// match aidl_kp { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 657 | /// KmKeyParameter { |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 658 | /// tag: Tag::INVALID, |
| 659 | /// .. |
| 660 | /// } => KeyParameterValue::Invalid, |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 661 | /// KmKeyParameter { |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 662 | /// tag: Tag::PURPOSE, |
| 663 | /// integer: v, |
| 664 | /// .. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 665 | /// } => KeyParameterValue::KeyPurpose(KeyPurpose(v)), |
| 666 | /// KmKeyParameter { |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 667 | /// tag: Tag::CALLER_NONCE, |
| 668 | /// boolValue: true, |
| 669 | /// .. |
| 670 | /// } => KeyParameterValue::CallerNonce, |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 671 | /// KmKeyParameter { |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 672 | /// tag: Tag::USER_SECURE_ID, |
| 673 | /// longInteger: v, |
| 674 | /// .. |
| 675 | /// } => KeyParameterValue::UserSecureID(v), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 676 | /// KmKeyParameter { |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 677 | /// tag: Tag::APPLICATION_ID, |
| 678 | /// blob: v, |
| 679 | /// .. |
| 680 | /// } => KeyParameterValue::ApplicationID(v), |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 681 | /// _ => KeyParameterValue::Invalid, |
| 682 | /// } |
| 683 | /// } |
| 684 | /// |
| 685 | macro_rules! implement_key_parameter_conversion_to_from_wire { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 686 | // There are three groups of rules in this macro. |
| 687 | // 1. The first group contains the rule which acts as the public interface. It takes the input |
| 688 | // given to this macro and prepares it to be given as input to the two groups of rules |
| 689 | // mentioned below. |
| 690 | // 2. The second group starts with the prefix @to and generates convert_to_wire() method. |
| 691 | // 3. The third group starts with the prefix @from and generates convert_from_wire() method. |
| 692 | // |
| 693 | // Input to this macro is first handled by the first macro rule (belonging to the first |
| 694 | // group above), which pre-processes the input such that rules in the other two groups |
| 695 | // generate the code for the two methods, when called recursively. |
| 696 | // Each of convert_to_wire() and convert_from_wire() methods are generated using a set of |
| 697 | // four macro rules in the second two groups. These four rules intend to do the following |
| 698 | // tasks respectively: |
| 699 | // i) generates match arms related to Invalid KeyParameterValue variant. |
| 700 | // ii) generates match arms related to boolValue field in KmKeyParameter struct. |
| 701 | // iii) generates match arms related to all the other fields in KmKeyParameter struct. |
| 702 | // iv) generates the method definition including the match arms generated from the above |
| 703 | // three recursive macro rules. |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 704 | |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 705 | // This rule is applied on the input given to the macro invocations from outside the macro. |
| 706 | ($($variant:ident, $tag_name:ident, $field_name:ident $(,$enum_type:ident)?;)*) => { |
| 707 | // pre-processes input to target the rules that generate convert_to_wire() method. |
| 708 | implement_key_parameter_conversion_to_from_wire! {@to |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 709 | [], $($variant, $tag_name, $field_name;)* |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 710 | } |
| 711 | // pre-processes input to target the rules that generate convert_from_wire() method. |
| 712 | implement_key_parameter_conversion_to_from_wire! {@from |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 713 | [], $($variant, $tag_name, $field_name;)* |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 714 | } |
| 715 | }; |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 716 | |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 717 | // Following four rules (belonging to the aforementioned second group) generate |
| 718 | // convert_to_wire() conversion method. |
| 719 | // ----------------------------------------------------------------------- |
| 720 | // This rule handles Invalid variant. |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 721 | // On an input: `Invalid, INVALID, Invalid;` it generates a match arm like: |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 722 | // KeyParameterValue::Invalid => KmKeyParameter { |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 723 | // tag: Tag::INVALID, |
| 724 | // value: KmKeyParameterValue::Invalid(0), |
| 725 | // }, |
| 726 | (@to [$($out:tt)*], Invalid, INVALID, Invalid; $($in:tt)*) => { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 727 | implement_key_parameter_conversion_to_from_wire! {@to |
| 728 | [$($out)* |
| 729 | KeyParameterValue::Invalid => KmKeyParameter { |
| 730 | tag: Tag::INVALID, |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 731 | value: KmKeyParameterValue::Invalid(0), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 732 | }, |
| 733 | ], $($in)* |
| 734 | } |
| 735 | }; |
| 736 | // This rule handles all variants that correspond to bool values. |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 737 | // On an input like: `CallerNonce, CALLER_NONCE, BoolValue;` it generates |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 738 | // a match arm like: |
| 739 | // KeyParameterValue::CallerNonce => KmKeyParameter { |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 740 | // tag: Tag::CALLER_NONCE, |
| 741 | // value: KmKeyParameterValue::BoolValue(true), |
| 742 | // }, |
| 743 | (@to [$($out:tt)*], $variant:ident, $tag_val:ident, BoolValue; $($in:tt)*) => { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 744 | implement_key_parameter_conversion_to_from_wire! {@to |
| 745 | [$($out)* |
| 746 | KeyParameterValue::$variant => KmKeyParameter { |
| 747 | tag: Tag::$tag_val, |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 748 | value: KmKeyParameterValue::BoolValue(true), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 749 | }, |
| 750 | ], $($in)* |
| 751 | } |
| 752 | }; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 753 | // This rule handles all variants that are neither invalid nor bool values nor enums |
Janis Danisevskis | 85d4793 | 2020-10-23 16:12:59 -0700 | [diff] [blame] | 754 | // (i.e. all variants which correspond to integer, longInteger, and blob fields in |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 755 | // KmKeyParameter). |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 756 | // On an input like: `ConfirmationToken, CONFIRMATION_TOKEN, Blob;` it generates a match arm |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 757 | // like: KeyParameterValue::ConfirmationToken(v) => KmKeyParameter { |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 758 | // tag: Tag::CONFIRMATION_TOKEN, |
| 759 | // value: KmKeyParameterValue::$field(v), |
| 760 | // }, |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 761 | (@to [$($out:tt)*], $variant:ident, $tag_val:ident, $field:ident; $($in:tt)*) => { |
| 762 | implement_key_parameter_conversion_to_from_wire! {@to |
| 763 | [$($out)* |
| 764 | KeyParameterValue::$variant(v) => KmKeyParameter { |
| 765 | tag: Tag::$tag_val, |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 766 | value: KmKeyParameterValue::$field(v), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 767 | }, |
| 768 | ], $($in)* |
| 769 | } |
| 770 | }; |
| 771 | // After all the match arms are generated by the above three rules, this rule combines them |
| 772 | // into the convert_to_wire() method. |
| 773 | (@to [$($out:tt)*], ) => { |
| 774 | /// Conversion of key parameter to wire type |
| 775 | pub fn convert_to_wire(self) -> KmKeyParameter { |
| 776 | match self { |
| 777 | $($out)* |
| 778 | } |
| 779 | } |
| 780 | }; |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 781 | |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 782 | // Following four rules (belonging to the aforementioned third group) generate |
| 783 | // convert_from_wire() conversion method. |
| 784 | // ------------------------------------------------------------------------ |
| 785 | // This rule handles Invalid variant. |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 786 | // On an input: `Invalid, INVALID, Invalid;` it generates a match arm like: |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 787 | // KmKeyParameter { tag: Tag::INVALID, .. } => KeyParameterValue::Invalid, |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 788 | (@from [$($out:tt)*], Invalid, INVALID, Invalid; $($in:tt)*) => { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 789 | implement_key_parameter_conversion_to_from_wire! {@from |
| 790 | [$($out)* |
| 791 | KmKeyParameter { |
| 792 | tag: Tag::INVALID, |
| 793 | .. |
| 794 | } => KeyParameterValue::Invalid, |
| 795 | ], $($in)* |
| 796 | } |
| 797 | }; |
| 798 | // This rule handles all variants that correspond to bool values. |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 799 | // On an input like: `CallerNonce, CALLER_NONCE, BoolValue;` it generates a match arm like: |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 800 | // KmKeyParameter { |
| 801 | // tag: Tag::CALLER_NONCE, |
| 802 | // boolValue: true, |
| 803 | // .. |
| 804 | // } => KeyParameterValue::CallerNonce, |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 805 | (@from [$($out:tt)*], $variant:ident, $tag_val:ident, BoolValue; $($in:tt)*) => { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 806 | implement_key_parameter_conversion_to_from_wire! {@from |
| 807 | [$($out)* |
| 808 | KmKeyParameter { |
| 809 | tag: Tag::$tag_val, |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 810 | value: KmKeyParameterValue::BoolValue(true), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 811 | } => KeyParameterValue::$variant, |
| 812 | ], $($in)* |
| 813 | } |
| 814 | }; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 815 | // This rule handles all variants that are neither invalid nor bool values nor enums |
Janis Danisevskis | 85d4793 | 2020-10-23 16:12:59 -0700 | [diff] [blame] | 816 | // (i.e. all variants which correspond to integer, longInteger, and blob fields in |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 817 | // KmKeyParameter). |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 818 | // On an input like: `ConfirmationToken, CONFIRMATION_TOKEN, Blob;` it generates a match arm |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 819 | // like: |
| 820 | // KmKeyParameter { |
| 821 | // tag: Tag::CONFIRMATION_TOKEN, |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 822 | // value: KmKeyParameterValue::Blob(v), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 823 | // } => KeyParameterValue::ConfirmationToken(v), |
| 824 | (@from [$($out:tt)*], $variant:ident, $tag_val:ident, $field:ident; $($in:tt)*) => { |
| 825 | implement_key_parameter_conversion_to_from_wire! {@from |
| 826 | [$($out)* |
| 827 | KmKeyParameter { |
| 828 | tag: Tag::$tag_val, |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 829 | value: KmKeyParameterValue::$field(v), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 830 | } => KeyParameterValue::$variant(v), |
| 831 | ], $($in)* |
| 832 | } |
| 833 | }; |
| 834 | // After all the match arms are generated by the above three rules, this rule combines them |
| 835 | // into the convert_from_wire() method. |
| 836 | (@from [$($out:tt)*], ) => { |
| 837 | /// Conversion of key parameter from wire type |
| 838 | pub fn convert_from_wire(aidl_kp: KmKeyParameter) -> KeyParameterValue { |
| 839 | match aidl_kp { |
| 840 | $($out)* |
| 841 | _ => KeyParameterValue::Invalid, |
| 842 | } |
| 843 | } |
| 844 | }; |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | impl KeyParameterValue { |
| 848 | // Invoke the macro that generates the code for key parameter conversion to/from wire type |
| 849 | // with all possible variants of KeyParameterValue. Each line corresponding to a variant |
| 850 | // contains: variant identifier, tag value, and the related field name (i.e. |
Janis Danisevskis | 85d4793 | 2020-10-23 16:12:59 -0700 | [diff] [blame] | 851 | // boolValue/integer/longInteger/blob) in the KmKeyParameter. |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 852 | implement_key_parameter_conversion_to_from_wire! { |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 853 | Invalid, INVALID, Invalid; |
| 854 | KeyPurpose, PURPOSE, KeyPurpose; |
| 855 | Algorithm, ALGORITHM, Algorithm; |
| 856 | KeySize, KEY_SIZE, Integer; |
| 857 | BlockMode, BLOCK_MODE, BlockMode; |
| 858 | Digest, DIGEST, Digest; |
| 859 | PaddingMode, PADDING, PaddingMode; |
| 860 | CallerNonce, CALLER_NONCE, BoolValue; |
| 861 | MinMacLength, MIN_MAC_LENGTH, Integer; |
| 862 | EcCurve, EC_CURVE, EcCurve; |
| 863 | RSAPublicExponent, RSA_PUBLIC_EXPONENT, LongInteger; |
| 864 | IncludeUniqueID, INCLUDE_UNIQUE_ID, BoolValue; |
| 865 | BootLoaderOnly, BOOTLOADER_ONLY, BoolValue; |
| 866 | RollbackResistance, ROLLBACK_RESISTANCE, BoolValue; |
| 867 | ActiveDateTime, ACTIVE_DATETIME, DateTime; |
| 868 | OriginationExpireDateTime, ORIGINATION_EXPIRE_DATETIME, DateTime; |
| 869 | UsageExpireDateTime, USAGE_EXPIRE_DATETIME, DateTime; |
| 870 | MinSecondsBetweenOps, MIN_SECONDS_BETWEEN_OPS, Integer; |
| 871 | MaxUsesPerBoot, MAX_USES_PER_BOOT, Integer; |
| 872 | UserID, USER_ID, Integer; |
| 873 | UserSecureID, USER_SECURE_ID, LongInteger; |
| 874 | NoAuthRequired, NO_AUTH_REQUIRED, BoolValue; |
| 875 | HardwareAuthenticatorType, USER_AUTH_TYPE, HardwareAuthenticatorType; |
| 876 | AuthTimeout, AUTH_TIMEOUT, Integer; |
| 877 | AllowWhileOnBody, ALLOW_WHILE_ON_BODY, BoolValue; |
| 878 | TrustedUserPresenceRequired, TRUSTED_USER_PRESENCE_REQUIRED, BoolValue; |
| 879 | TrustedConfirmationRequired, TRUSTED_CONFIRMATION_REQUIRED, BoolValue; |
| 880 | UnlockedDeviceRequired, UNLOCKED_DEVICE_REQUIRED, BoolValue; |
| 881 | ApplicationID, APPLICATION_ID, Blob; |
| 882 | ApplicationData, APPLICATION_DATA, Blob; |
| 883 | CreationDateTime, CREATION_DATETIME, DateTime; |
| 884 | KeyOrigin, ORIGIN, Origin; |
| 885 | RootOfTrust, ROOT_OF_TRUST, Blob; |
| 886 | OSVersion, OS_VERSION, Integer; |
| 887 | OSPatchLevel, OS_PATCHLEVEL, Integer; |
| 888 | UniqueID, UNIQUE_ID, Blob; |
| 889 | AttestationChallenge, ATTESTATION_CHALLENGE, Blob; |
| 890 | AttestationApplicationID, ATTESTATION_APPLICATION_ID, Blob; |
| 891 | AttestationIdBrand, ATTESTATION_ID_BRAND, Blob; |
| 892 | AttestationIdDevice, ATTESTATION_ID_DEVICE, Blob; |
| 893 | AttestationIdProduct, ATTESTATION_ID_PRODUCT, Blob; |
| 894 | AttestationIdSerial, ATTESTATION_ID_SERIAL, Blob; |
| 895 | AttestationIdIMEI, ATTESTATION_ID_IMEI, Blob; |
| 896 | AttestationIdMEID, ATTESTATION_ID_MEID, Blob; |
| 897 | AttestationIdManufacturer, ATTESTATION_ID_MANUFACTURER, Blob; |
| 898 | AttestationIdModel, ATTESTATION_ID_MODEL, Blob; |
| 899 | VendorPatchLevel, VENDOR_PATCHLEVEL, Integer; |
| 900 | BootPatchLevel, BOOT_PATCHLEVEL, Integer; |
| 901 | AssociatedData, ASSOCIATED_DATA, Blob; |
| 902 | Nonce, NONCE, Blob; |
| 903 | MacLength, MAC_LENGTH, Integer; |
| 904 | ResetSinceIdRotation, RESET_SINCE_ID_ROTATION, BoolValue; |
| 905 | ConfirmationToken, CONFIRMATION_TOKEN, Blob; |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | |
| 909 | #[cfg(test)] |
| 910 | mod basic_tests { |
| 911 | use crate::key_parameter::*; |
| 912 | |
| 913 | // Test basic functionality of KeyParameter. |
| 914 | #[test] |
| 915 | fn test_key_parameter() { |
| 916 | let key_parameter = KeyParameter::new( |
| 917 | KeyParameterValue::Algorithm(Algorithm::RSA), |
| 918 | SecurityLevel::STRONGBOX, |
| 919 | ); |
| 920 | |
| 921 | assert_eq!(key_parameter.get_tag(), Tag::ALGORITHM); |
| 922 | |
| 923 | assert_eq!( |
| 924 | *key_parameter.key_parameter_value(), |
| 925 | KeyParameterValue::Algorithm(Algorithm::RSA) |
| 926 | ); |
| 927 | |
| 928 | assert_eq!(*key_parameter.security_level(), SecurityLevel::STRONGBOX); |
| 929 | } |
| 930 | } |
| 931 | |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 932 | /// The storage_tests module first tests the 'new_from_sql' method for KeyParameters of different |
| 933 | /// data types and then tests 'to_sql' method for KeyParameters of those |
| 934 | /// different data types. The five different data types for KeyParameter values are: |
| 935 | /// i) enums of u32 |
| 936 | /// ii) u32 |
| 937 | /// iii) u64 |
| 938 | /// iv) Vec<u8> |
| 939 | /// v) bool |
| 940 | #[cfg(test)] |
| 941 | mod storage_tests { |
| 942 | use crate::error::*; |
| 943 | use crate::key_parameter::*; |
| 944 | use anyhow::Result; |
| 945 | use rusqlite::types::ToSql; |
| 946 | use rusqlite::{params, Connection, NO_PARAMS}; |
| 947 | |
| 948 | /// Test initializing a KeyParameter (with key parameter value corresponding to an enum of i32) |
| 949 | /// from a database table row. |
| 950 | #[test] |
| 951 | fn test_new_from_sql_enum_i32() -> Result<()> { |
| 952 | let db = init_db()?; |
| 953 | insert_into_keyparameter( |
| 954 | &db, |
| 955 | 1, |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 956 | Tag::ALGORITHM.0, |
| 957 | &Algorithm::RSA.0, |
| 958 | SecurityLevel::STRONGBOX.0, |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 959 | )?; |
| 960 | let key_param = query_from_keyparameter(&db)?; |
| 961 | assert_eq!(Tag::ALGORITHM, key_param.get_tag()); |
| 962 | assert_eq!(*key_param.key_parameter_value(), KeyParameterValue::Algorithm(Algorithm::RSA)); |
| 963 | assert_eq!(*key_param.security_level(), SecurityLevel::STRONGBOX); |
| 964 | Ok(()) |
| 965 | } |
| 966 | |
| 967 | /// Test initializing a KeyParameter (with key parameter value which is of i32) |
| 968 | /// from a database table row. |
| 969 | #[test] |
| 970 | fn test_new_from_sql_i32() -> Result<()> { |
| 971 | let db = init_db()?; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 972 | insert_into_keyparameter(&db, 1, Tag::KEY_SIZE.0, &1024, SecurityLevel::STRONGBOX.0)?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 973 | let key_param = query_from_keyparameter(&db)?; |
| 974 | assert_eq!(Tag::KEY_SIZE, key_param.get_tag()); |
| 975 | assert_eq!(*key_param.key_parameter_value(), KeyParameterValue::KeySize(1024)); |
| 976 | Ok(()) |
| 977 | } |
| 978 | |
| 979 | /// Test initializing a KeyParameter (with key parameter value which is of i64) |
| 980 | /// from a database table row. |
| 981 | #[test] |
| 982 | fn test_new_from_sql_i64() -> Result<()> { |
| 983 | let db = init_db()?; |
| 984 | // max value for i64, just to test corner cases |
| 985 | insert_into_keyparameter( |
| 986 | &db, |
| 987 | 1, |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 988 | Tag::RSA_PUBLIC_EXPONENT.0, |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 989 | &(i64::MAX), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 990 | SecurityLevel::STRONGBOX.0, |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 991 | )?; |
| 992 | let key_param = query_from_keyparameter(&db)?; |
| 993 | assert_eq!(Tag::RSA_PUBLIC_EXPONENT, key_param.get_tag()); |
| 994 | assert_eq!( |
| 995 | *key_param.key_parameter_value(), |
| 996 | KeyParameterValue::RSAPublicExponent(i64::MAX) |
| 997 | ); |
| 998 | Ok(()) |
| 999 | } |
| 1000 | |
| 1001 | /// Test initializing a KeyParameter (with key parameter value which is of bool) |
| 1002 | /// from a database table row. |
| 1003 | #[test] |
| 1004 | fn test_new_from_sql_bool() -> Result<()> { |
| 1005 | let db = init_db()?; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 1006 | insert_into_keyparameter(&db, 1, Tag::CALLER_NONCE.0, &Null, SecurityLevel::STRONGBOX.0)?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 1007 | let key_param = query_from_keyparameter(&db)?; |
| 1008 | assert_eq!(Tag::CALLER_NONCE, key_param.get_tag()); |
| 1009 | assert_eq!(*key_param.key_parameter_value(), KeyParameterValue::CallerNonce); |
| 1010 | Ok(()) |
| 1011 | } |
| 1012 | |
| 1013 | /// Test initializing a KeyParameter (with key parameter value which is of Vec<u8>) |
| 1014 | /// from a database table row. |
| 1015 | #[test] |
| 1016 | fn test_new_from_sql_vec_u8() -> Result<()> { |
| 1017 | let db = init_db()?; |
| 1018 | let app_id = String::from("MyAppID"); |
| 1019 | let app_id_bytes = app_id.into_bytes(); |
| 1020 | insert_into_keyparameter( |
| 1021 | &db, |
| 1022 | 1, |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 1023 | Tag::APPLICATION_ID.0, |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 1024 | &app_id_bytes, |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 1025 | SecurityLevel::STRONGBOX.0, |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 1026 | )?; |
| 1027 | let key_param = query_from_keyparameter(&db)?; |
| 1028 | assert_eq!(Tag::APPLICATION_ID, key_param.get_tag()); |
| 1029 | assert_eq!( |
| 1030 | *key_param.key_parameter_value(), |
| 1031 | KeyParameterValue::ApplicationID(app_id_bytes) |
| 1032 | ); |
| 1033 | Ok(()) |
| 1034 | } |
| 1035 | |
| 1036 | /// Test storing a KeyParameter (with key parameter value which corresponds to an enum of i32) |
| 1037 | /// in the database |
| 1038 | #[test] |
| 1039 | fn test_to_sql_enum_i32() -> Result<()> { |
| 1040 | let db = init_db()?; |
| 1041 | let kp = KeyParameter::new( |
| 1042 | KeyParameterValue::Algorithm(Algorithm::RSA), |
| 1043 | SecurityLevel::STRONGBOX, |
| 1044 | ); |
| 1045 | store_keyparameter(&db, 1, &kp)?; |
| 1046 | let key_param = query_from_keyparameter(&db)?; |
| 1047 | assert_eq!(kp.get_tag(), key_param.get_tag()); |
| 1048 | assert_eq!(kp.key_parameter_value(), key_param.key_parameter_value()); |
| 1049 | assert_eq!(kp.security_level(), key_param.security_level()); |
| 1050 | Ok(()) |
| 1051 | } |
| 1052 | |
| 1053 | /// Test storing a KeyParameter (with key parameter value which is of i32) in the database |
| 1054 | #[test] |
| 1055 | fn test_to_sql_i32() -> Result<()> { |
| 1056 | let db = init_db()?; |
| 1057 | let kp = KeyParameter::new(KeyParameterValue::KeySize(1024), SecurityLevel::STRONGBOX); |
| 1058 | store_keyparameter(&db, 1, &kp)?; |
| 1059 | let key_param = query_from_keyparameter(&db)?; |
| 1060 | assert_eq!(kp.get_tag(), key_param.get_tag()); |
| 1061 | assert_eq!(kp.key_parameter_value(), key_param.key_parameter_value()); |
| 1062 | assert_eq!(kp.security_level(), key_param.security_level()); |
| 1063 | Ok(()) |
| 1064 | } |
| 1065 | |
| 1066 | /// Test storing a KeyParameter (with key parameter value which is of i64) in the database |
| 1067 | #[test] |
| 1068 | fn test_to_sql_i64() -> Result<()> { |
| 1069 | let db = init_db()?; |
| 1070 | // max value for i64, just to test corner cases |
| 1071 | let kp = KeyParameter::new( |
| 1072 | KeyParameterValue::RSAPublicExponent(i64::MAX), |
| 1073 | SecurityLevel::STRONGBOX, |
| 1074 | ); |
| 1075 | store_keyparameter(&db, 1, &kp)?; |
| 1076 | let key_param = query_from_keyparameter(&db)?; |
| 1077 | assert_eq!(kp.get_tag(), key_param.get_tag()); |
| 1078 | assert_eq!(kp.key_parameter_value(), key_param.key_parameter_value()); |
| 1079 | assert_eq!(kp.security_level(), key_param.security_level()); |
| 1080 | Ok(()) |
| 1081 | } |
| 1082 | |
| 1083 | /// Test storing a KeyParameter (with key parameter value which is of Vec<u8>) in the database |
| 1084 | #[test] |
| 1085 | fn test_to_sql_vec_u8() -> Result<()> { |
| 1086 | let db = init_db()?; |
| 1087 | let kp = KeyParameter::new( |
| 1088 | KeyParameterValue::ApplicationID(String::from("MyAppID").into_bytes()), |
| 1089 | SecurityLevel::STRONGBOX, |
| 1090 | ); |
| 1091 | store_keyparameter(&db, 1, &kp)?; |
| 1092 | let key_param = query_from_keyparameter(&db)?; |
| 1093 | assert_eq!(kp.get_tag(), key_param.get_tag()); |
| 1094 | assert_eq!(kp.key_parameter_value(), key_param.key_parameter_value()); |
| 1095 | assert_eq!(kp.security_level(), key_param.security_level()); |
| 1096 | Ok(()) |
| 1097 | } |
| 1098 | |
| 1099 | /// Test storing a KeyParameter (with key parameter value which is of i32) in the database |
| 1100 | #[test] |
| 1101 | fn test_to_sql_bool() -> Result<()> { |
| 1102 | let db = init_db()?; |
| 1103 | let kp = KeyParameter::new(KeyParameterValue::CallerNonce, SecurityLevel::STRONGBOX); |
| 1104 | store_keyparameter(&db, 1, &kp)?; |
| 1105 | let key_param = query_from_keyparameter(&db)?; |
| 1106 | assert_eq!(kp.get_tag(), key_param.get_tag()); |
| 1107 | assert_eq!(kp.key_parameter_value(), key_param.key_parameter_value()); |
| 1108 | assert_eq!(kp.security_level(), key_param.security_level()); |
| 1109 | Ok(()) |
| 1110 | } |
| 1111 | |
| 1112 | #[test] |
| 1113 | /// Test Tag::Invalid |
| 1114 | fn test_invalid_tag() -> Result<()> { |
| 1115 | let db = init_db()?; |
| 1116 | insert_into_keyparameter(&db, 1, 0, &123, 1)?; |
| 1117 | let key_param = query_from_keyparameter(&db)?; |
| 1118 | assert_eq!(Tag::INVALID, key_param.get_tag()); |
| 1119 | Ok(()) |
| 1120 | } |
| 1121 | |
| 1122 | #[test] |
| 1123 | fn test_non_existing_enum_variant() -> Result<()> { |
| 1124 | let db = init_db()?; |
| 1125 | insert_into_keyparameter(&db, 1, 100, &123, 1)?; |
| 1126 | tests::check_result_contains_error_string( |
| 1127 | query_from_keyparameter(&db), |
| 1128 | "Failed to decode Tag enum from value.", |
| 1129 | ); |
| 1130 | Ok(()) |
| 1131 | } |
| 1132 | |
| 1133 | #[test] |
| 1134 | fn test_invalid_conversion_from_sql() -> Result<()> { |
| 1135 | let db = init_db()?; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 1136 | insert_into_keyparameter(&db, 1, Tag::ALGORITHM.0, &Null, 1)?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 1137 | tests::check_result_contains_error_string( |
| 1138 | query_from_keyparameter(&db), |
| 1139 | "Failed to read sql data for tag: ALGORITHM.", |
| 1140 | ); |
| 1141 | Ok(()) |
| 1142 | } |
| 1143 | |
| 1144 | /// Helper method to init database table for key parameter |
| 1145 | fn init_db() -> Result<Connection> { |
| 1146 | let db = Connection::open_in_memory().context("Failed to initialize sqlite connection.")?; |
| 1147 | db.execute("ATTACH DATABASE ? as 'persistent';", params![""]) |
| 1148 | .context("Failed to attach databases.")?; |
| 1149 | db.execute( |
| 1150 | "CREATE TABLE IF NOT EXISTS persistent.keyparameter ( |
| 1151 | keyentryid INTEGER, |
| 1152 | tag INTEGER, |
| 1153 | data ANY, |
| 1154 | security_level INTEGER);", |
| 1155 | NO_PARAMS, |
| 1156 | ) |
| 1157 | .context("Failed to initialize \"keyparameter\" table.")?; |
| 1158 | Ok(db) |
| 1159 | } |
| 1160 | |
| 1161 | /// Helper method to insert an entry into key parameter table, with individual parameters |
| 1162 | fn insert_into_keyparameter<T: ToSql>( |
| 1163 | db: &Connection, |
| 1164 | key_id: i64, |
| 1165 | tag: i32, |
| 1166 | value: &T, |
| 1167 | security_level: i32, |
| 1168 | ) -> Result<()> { |
| 1169 | db.execute( |
| 1170 | "INSERT into persistent.keyparameter (keyentryid, tag, data, security_level) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 1171 | VALUES(?, ?, ?, ?);", |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 1172 | params![key_id, tag, *value, security_level], |
| 1173 | )?; |
| 1174 | Ok(()) |
| 1175 | } |
| 1176 | |
| 1177 | /// Helper method to store a key parameter instance. |
| 1178 | fn store_keyparameter(db: &Connection, key_id: i64, kp: &KeyParameter) -> Result<()> { |
| 1179 | db.execute( |
| 1180 | "INSERT into persistent.keyparameter (keyentryid, tag, data, security_level) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 1181 | VALUES(?, ?, ?, ?);", |
| 1182 | params![key_id, kp.get_tag().0, kp.key_parameter_value(), kp.security_level().0], |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 1183 | )?; |
| 1184 | Ok(()) |
| 1185 | } |
| 1186 | |
| 1187 | /// Helper method to query a row from keyparameter table |
| 1188 | fn query_from_keyparameter(db: &Connection) -> Result<KeyParameter> { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 1189 | let mut stmt = |
| 1190 | db.prepare("SELECT tag, data, security_level FROM persistent.keyparameter")?; |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 1191 | let mut rows = stmt.query(NO_PARAMS)?; |
| 1192 | let row = rows.next()?.unwrap(); |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 1193 | Ok(KeyParameter::new_from_sql( |
| 1194 | Tag(row.get(0)?), |
| 1195 | &SqlField(1, row), |
| 1196 | SecurityLevel(row.get(2)?), |
| 1197 | )?) |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 1198 | } |
| 1199 | } |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1200 | |
| 1201 | /// The wire_tests module tests the 'convert_to_wire' and 'convert_from_wire' methods for |
Janis Danisevskis | 85d4793 | 2020-10-23 16:12:59 -0700 | [diff] [blame] | 1202 | /// KeyParameter, for the four different types used in KmKeyParameter, in addition to Invalid |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1203 | /// key parameter. |
| 1204 | /// i) bool |
| 1205 | /// ii) integer |
| 1206 | /// iii) longInteger |
Janis Danisevskis | 85d4793 | 2020-10-23 16:12:59 -0700 | [diff] [blame] | 1207 | /// iv) blob |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1208 | #[cfg(test)] |
| 1209 | mod wire_tests { |
| 1210 | use crate::key_parameter::*; |
| 1211 | /// unit tests for to conversions |
| 1212 | #[test] |
| 1213 | fn test_convert_to_wire_invalid() { |
| 1214 | let kp = KeyParameter::new(KeyParameterValue::Invalid, SecurityLevel::STRONGBOX); |
| 1215 | let actual = KeyParameterValue::convert_to_wire(kp.key_parameter_value); |
| 1216 | assert_eq!(Tag::INVALID, actual.tag); |
| 1217 | } |
| 1218 | #[test] |
| 1219 | fn test_convert_to_wire_bool() { |
| 1220 | let kp = KeyParameter::new(KeyParameterValue::CallerNonce, SecurityLevel::STRONGBOX); |
| 1221 | let actual = KeyParameterValue::convert_to_wire(kp.key_parameter_value); |
| 1222 | assert_eq!(Tag::CALLER_NONCE, actual.tag); |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 1223 | assert_eq!(KmKeyParameterValue::BoolValue(true), actual.value); |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1224 | } |
| 1225 | #[test] |
| 1226 | fn test_convert_to_wire_integer() { |
| 1227 | let kp = KeyParameter::new( |
| 1228 | KeyParameterValue::KeyPurpose(KeyPurpose::ENCRYPT), |
| 1229 | SecurityLevel::STRONGBOX, |
| 1230 | ); |
| 1231 | let actual = KeyParameterValue::convert_to_wire(kp.key_parameter_value); |
| 1232 | assert_eq!(Tag::PURPOSE, actual.tag); |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 1233 | assert_eq!(KmKeyParameterValue::KeyPurpose(KeyPurpose::ENCRYPT), actual.value); |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1234 | } |
| 1235 | #[test] |
| 1236 | fn test_convert_to_wire_long_integer() { |
| 1237 | let kp = |
| 1238 | KeyParameter::new(KeyParameterValue::UserSecureID(i64::MAX), SecurityLevel::STRONGBOX); |
| 1239 | let actual = KeyParameterValue::convert_to_wire(kp.key_parameter_value); |
| 1240 | assert_eq!(Tag::USER_SECURE_ID, actual.tag); |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 1241 | assert_eq!(KmKeyParameterValue::LongInteger(i64::MAX), actual.value); |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1242 | } |
| 1243 | #[test] |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1244 | fn test_convert_to_wire_blob() { |
| 1245 | let kp = KeyParameter::new( |
| 1246 | KeyParameterValue::ConfirmationToken(String::from("ConfirmationToken").into_bytes()), |
| 1247 | SecurityLevel::STRONGBOX, |
| 1248 | ); |
| 1249 | let actual = KeyParameterValue::convert_to_wire(kp.key_parameter_value); |
| 1250 | assert_eq!(Tag::CONFIRMATION_TOKEN, actual.tag); |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 1251 | assert_eq!( |
| 1252 | KmKeyParameterValue::Blob(String::from("ConfirmationToken").into_bytes()), |
| 1253 | actual.value |
| 1254 | ); |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | /// unit tests for from conversion |
| 1258 | #[test] |
| 1259 | fn test_convert_from_wire_invalid() { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 1260 | let aidl_kp = KmKeyParameter { tag: Tag::INVALID, ..Default::default() }; |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1261 | let actual = KeyParameterValue::convert_from_wire(aidl_kp); |
| 1262 | assert_eq!(KeyParameterValue::Invalid, actual); |
| 1263 | } |
| 1264 | #[test] |
| 1265 | fn test_convert_from_wire_bool() { |
| 1266 | let aidl_kp = |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 1267 | KmKeyParameter { tag: Tag::CALLER_NONCE, value: KmKeyParameterValue::BoolValue(true) }; |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1268 | let actual = KeyParameterValue::convert_from_wire(aidl_kp); |
| 1269 | assert_eq!(KeyParameterValue::CallerNonce, actual); |
| 1270 | } |
| 1271 | #[test] |
| 1272 | fn test_convert_from_wire_integer() { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 1273 | let aidl_kp = KmKeyParameter { |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1274 | tag: Tag::PURPOSE, |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 1275 | value: KmKeyParameterValue::KeyPurpose(KeyPurpose::ENCRYPT), |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1276 | }; |
| 1277 | let actual = KeyParameterValue::convert_from_wire(aidl_kp); |
| 1278 | assert_eq!(KeyParameterValue::KeyPurpose(KeyPurpose::ENCRYPT), actual); |
| 1279 | } |
| 1280 | #[test] |
| 1281 | fn test_convert_from_wire_long_integer() { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 1282 | let aidl_kp = KmKeyParameter { |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1283 | tag: Tag::USER_SECURE_ID, |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 1284 | value: KmKeyParameterValue::LongInteger(i64::MAX), |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1285 | }; |
| 1286 | let actual = KeyParameterValue::convert_from_wire(aidl_kp); |
| 1287 | assert_eq!(KeyParameterValue::UserSecureID(i64::MAX), actual); |
| 1288 | } |
| 1289 | #[test] |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1290 | fn test_convert_from_wire_blob() { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 1291 | let aidl_kp = KmKeyParameter { |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1292 | tag: Tag::CONFIRMATION_TOKEN, |
Janis Danisevskis | 398e6be | 2020-12-17 09:29:25 -0800 | [diff] [blame^] | 1293 | value: KmKeyParameterValue::Blob(String::from("ConfirmationToken").into_bytes()), |
Hasini Gunasinghe | 3eb77c2 | 2020-08-28 15:45:06 +0000 | [diff] [blame] | 1294 | }; |
| 1295 | let actual = KeyParameterValue::convert_from_wire(aidl_kp); |
| 1296 | assert_eq!( |
| 1297 | KeyParameterValue::ConfirmationToken(String::from("ConfirmationToken").into_bytes()), |
| 1298 | actual |
| 1299 | ); |
| 1300 | } |
| 1301 | } |