blob: d7a0a12a23174522e07c12cdd06262e8d4159c72 [file] [log] [blame]
Janis Danisevskis1af91262020-08-10 14:58:08 -07001// 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#![allow(unused_variables)]
16
17//! This crate implements the IKeystoreSecurityLevel interface.
18
Janis Danisevskis7e8b4622021-02-13 10:01:59 -080019use crate::globals::get_keymint_device;
Shawn Willden708744a2020-12-11 13:05:27 +000020use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{
Max Bires97f96812021-02-23 23:44:57 -080021 Algorithm::Algorithm, AttestationKey::AttestationKey, Certificate::Certificate,
Shawn Willden8fde4c22021-02-14 13:58:22 -070022 HardwareAuthenticatorType::HardwareAuthenticatorType, IKeyMintDevice::IKeyMintDevice,
23 KeyCreationResult::KeyCreationResult, KeyFormat::KeyFormat,
Max Bires8e93d2b2021-01-14 13:17:59 -080024 KeyMintHardwareInfo::KeyMintHardwareInfo, KeyParameter::KeyParameter,
25 KeyParameterValue::KeyParameterValue, SecurityLevel::SecurityLevel, Tag::Tag,
Janis Danisevskis1af91262020-08-10 14:58:08 -070026};
27use android_system_keystore2::aidl::android::system::keystore2::{
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -070028 AuthenticatorSpec::AuthenticatorSpec, CreateOperationResponse::CreateOperationResponse,
29 Domain::Domain, IKeystoreOperation::IKeystoreOperation,
30 IKeystoreSecurityLevel::BnKeystoreSecurityLevel,
Janis Danisevskis1af91262020-08-10 14:58:08 -070031 IKeystoreSecurityLevel::IKeystoreSecurityLevel, KeyDescriptor::KeyDescriptor,
Janis Danisevskis5ed8c532021-01-11 14:19:42 -080032 KeyMetadata::KeyMetadata, KeyParameters::KeyParameters,
Janis Danisevskis1af91262020-08-10 14:58:08 -070033};
34
Max Bires97f96812021-02-23 23:44:57 -080035use crate::database::{CertificateInfo, KeyIdGuard, KeystoreDB};
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +000036use crate::globals::{DB, ENFORCEMENTS, LEGACY_MIGRATOR, SUPER_KEY};
Hasini Gunasinghe888dd352020-11-17 23:08:39 +000037use crate::key_parameter::KeyParameter as KsKeyParam;
Hasini Gunasinghea020b532021-01-07 21:42:35 +000038use crate::key_parameter::KeyParameterValue as KsKeyParamValue;
Max Bires97f96812021-02-23 23:44:57 -080039use crate::remote_provisioning::RemProvState;
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +000040use crate::super_key::{KeyBlob, SuperKeyManager};
Bram Bonné5d6c5102021-02-24 15:09:18 +010041use crate::utils::{
42 check_device_attestation_permissions, check_key_permission, is_device_id_attestation_tag,
43 uid_to_android_user, Asp,
44};
Max Bires8e93d2b2021-01-14 13:17:59 -080045use crate::{
Janis Danisevskis7e8b4622021-02-13 10:01:59 -080046 database::{
47 BlobMetaData, BlobMetaEntry, DateTime, KeyEntry, KeyEntryLoadBits, KeyMetaData,
48 KeyMetaEntry, KeyType, SubComponentType, Uuid,
49 },
50 operation::KeystoreOperation,
51 operation::OperationDb,
Janis Danisevskisb42fc182020-12-15 08:41:27 -080052 permission::KeyPerm,
53};
54use crate::{
Janis Danisevskis04b02832020-10-26 09:21:40 -070055 error::{self, map_km_error, map_or_log_err, Error, ErrorCode},
56 utils::key_characteristics_to_internal,
57};
Janis Danisevskis212c68b2021-01-14 22:29:28 -080058use anyhow::{anyhow, Context, Result};
Andrew Walbran808e8602021-03-16 13:58:28 +000059use binder::{IBinderInternal, Strong, ThreadState};
Shawn Willden34120872021-02-24 21:56:30 -070060use keystore2_crypto::parse_subject_from_certificate;
Janis Danisevskis1af91262020-08-10 14:58:08 -070061
62/// Implementation of the IKeystoreSecurityLevel Interface.
63pub struct KeystoreSecurityLevel {
64 security_level: SecurityLevel,
65 keymint: Asp,
Max Bires8e93d2b2021-01-14 13:17:59 -080066 #[allow(dead_code)]
67 hw_info: KeyMintHardwareInfo,
68 km_uuid: Uuid,
Janis Danisevskis1af91262020-08-10 14:58:08 -070069 operation_db: OperationDb,
Max Bires97f96812021-02-23 23:44:57 -080070 rem_prov_state: RemProvState,
Janis Danisevskis1af91262020-08-10 14:58:08 -070071}
72
Janis Danisevskis1af91262020-08-10 14:58:08 -070073// Blob of 32 zeroes used as empty masking key.
74static ZERO_BLOB_32: &[u8] = &[0; 32];
75
Janis Danisevskis2c084012021-01-31 22:23:17 -080076// Per RFC 5280 4.1.2.5, an undefined expiration (not-after) field should be set to GeneralizedTime
77// 999912312359559, which is 253402300799000 ms from Jan 1, 1970.
78const UNDEFINED_NOT_AFTER: i64 = 253402300799000i64;
79
Janis Danisevskis1af91262020-08-10 14:58:08 -070080impl KeystoreSecurityLevel {
81 /// Creates a new security level instance wrapped in a
82 /// BnKeystoreSecurityLevel proxy object. It also
Andrew Walbran808e8602021-03-16 13:58:28 +000083 /// calls `IBinderInternal::set_requesting_sid` on the new interface, because
Janis Danisevskis1af91262020-08-10 14:58:08 -070084 /// we need it for checking keystore permissions.
85 pub fn new_native_binder(
86 security_level: SecurityLevel,
Stephen Crane221bbb52020-12-16 15:52:10 -080087 ) -> Result<(Strong<dyn IKeystoreSecurityLevel>, Uuid)> {
Max Bires8e93d2b2021-01-14 13:17:59 -080088 let (dev, hw_info, km_uuid) = get_keymint_device(&security_level)
89 .context("In KeystoreSecurityLevel::new_native_binder.")?;
Janis Danisevskis1af91262020-08-10 14:58:08 -070090 let result = BnKeystoreSecurityLevel::new_binder(Self {
91 security_level,
Max Bires8e93d2b2021-01-14 13:17:59 -080092 keymint: dev,
93 hw_info,
94 km_uuid,
Janis Danisevskis1af91262020-08-10 14:58:08 -070095 operation_db: OperationDb::new(),
Max Bires97f96812021-02-23 23:44:57 -080096 rem_prov_state: RemProvState::new(security_level, km_uuid),
Janis Danisevskis1af91262020-08-10 14:58:08 -070097 });
98 result.as_binder().set_requesting_sid(true);
Max Bires8e93d2b2021-01-14 13:17:59 -080099 Ok((result, km_uuid))
Janis Danisevskis1af91262020-08-10 14:58:08 -0700100 }
101
102 fn store_new_key(
103 &self,
104 key: KeyDescriptor,
Shawn Willdendbdac602021-01-12 22:35:16 -0700105 creation_result: KeyCreationResult,
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000106 user_id: u32,
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000107 flags: Option<i32>,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700108 ) -> Result<KeyMetadata> {
Shawn Willdendbdac602021-01-12 22:35:16 -0700109 let KeyCreationResult {
110 keyBlob: key_blob,
111 keyCharacteristics: key_characteristics,
112 certificateChain: mut certificate_chain,
113 } = creation_result;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700114
Max Bires8e93d2b2021-01-14 13:17:59 -0800115 let mut cert_info: CertificateInfo = CertificateInfo::new(
Shawn Willdendbdac602021-01-12 22:35:16 -0700116 match certificate_chain.len() {
117 0 => None,
118 _ => Some(certificate_chain.remove(0).encodedCertificate),
119 },
120 match certificate_chain.len() {
121 0 => None,
122 _ => Some(
123 certificate_chain
124 .iter()
125 .map(|c| c.encodedCertificate.iter())
126 .flatten()
127 .copied()
128 .collect(),
129 ),
130 },
131 );
132
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000133 let mut key_parameters = key_characteristics_to_internal(key_characteristics);
134
135 key_parameters.push(KsKeyParam::new(
136 KsKeyParamValue::UserID(user_id as i32),
137 SecurityLevel::SOFTWARE,
138 ));
Janis Danisevskis04b02832020-10-26 09:21:40 -0700139
Janis Danisevskisb42fc182020-12-15 08:41:27 -0800140 let creation_date = DateTime::now().context("Trying to make creation time.")?;
141
Janis Danisevskis1af91262020-08-10 14:58:08 -0700142 let key = match key.domain {
Satya Tangirala60671e32021-03-04 16:12:19 -0800143 Domain::BLOB => KeyDescriptor {
144 domain: Domain::BLOB,
145 blob: Some(key_blob.to_vec()),
146 ..Default::default()
147 },
Janis Danisevskis1af91262020-08-10 14:58:08 -0700148 _ => DB
Janis Danisevskis93927dd2020-12-23 12:23:08 -0800149 .with::<_, Result<KeyDescriptor>>(|db| {
Satya Tangirala60671e32021-03-04 16:12:19 -0800150 let mut db = db.borrow_mut();
151
152 let (key_blob, mut blob_metadata) = SUPER_KEY
153 .handle_super_encryption_on_key_init(
154 &mut db,
155 &LEGACY_MIGRATOR,
156 &(key.domain),
157 &key_parameters,
158 flags,
159 user_id,
160 &key_blob,
161 )
162 .context("In store_new_key. Failed to handle super encryption.")?;
163
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800164 let mut key_metadata = KeyMetaData::new();
165 key_metadata.add(KeyMetaEntry::CreationDate(creation_date));
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800166 blob_metadata.add(BlobMetaEntry::KmUuid(self.km_uuid));
Janis Danisevskis93927dd2020-12-23 12:23:08 -0800167
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800168 let key_id = db
Janis Danisevskis93927dd2020-12-23 12:23:08 -0800169 .store_new_key(
Janis Danisevskis66784c42021-01-27 08:40:25 -0800170 &key,
Janis Danisevskis93927dd2020-12-23 12:23:08 -0800171 &key_parameters,
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800172 &(&key_blob, &blob_metadata),
Max Bires8e93d2b2021-01-14 13:17:59 -0800173 &cert_info,
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800174 &key_metadata,
Max Bires8e93d2b2021-01-14 13:17:59 -0800175 &self.km_uuid,
Janis Danisevskis93927dd2020-12-23 12:23:08 -0800176 )
177 .context("In store_new_key.")?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700178 Ok(KeyDescriptor {
179 domain: Domain::KEY_ID,
Janis Danisevskisaec14592020-11-12 09:41:49 -0800180 nspace: key_id.id(),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700181 ..Default::default()
182 })
183 })
184 .context("In store_new_key.")?,
185 };
186
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700187 Ok(KeyMetadata {
188 key,
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700189 keySecurityLevel: self.security_level,
Max Bires8e93d2b2021-01-14 13:17:59 -0800190 certificate: cert_info.take_cert(),
191 certificateChain: cert_info.take_cert_chain(),
Janis Danisevskis04b02832020-10-26 09:21:40 -0700192 authorizations: crate::utils::key_parameters_to_authorizations(key_parameters),
Janis Danisevskisb42fc182020-12-15 08:41:27 -0800193 modificationTimeMs: creation_date.to_millis_epoch(),
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700194 })
Janis Danisevskis1af91262020-08-10 14:58:08 -0700195 }
196
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700197 fn create_operation(
Janis Danisevskis1af91262020-08-10 14:58:08 -0700198 &self,
199 key: &KeyDescriptor,
200 operation_parameters: &[KeyParameter],
201 forced: bool,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700202 ) -> Result<CreateOperationResponse> {
Janis Danisevskis1af91262020-08-10 14:58:08 -0700203 let caller_uid = ThreadState::get_calling_uid();
204 // We use `scoping_blob` to extend the life cycle of the blob loaded from the database,
205 // so that we can use it by reference like the blob provided by the key descriptor.
206 // Otherwise, we would have to clone the blob from the key descriptor.
207 let scoping_blob: Vec<u8>;
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800208 let (km_blob, key_properties, key_id_guard, blob_metadata) = match key.domain {
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700209 Domain::BLOB => {
210 check_key_permission(KeyPerm::use_(), key, &None)
211 .context("In create_operation: checking use permission for Domain::BLOB.")?;
Janis Danisevskis186d9f42021-03-03 14:40:52 -0800212 if forced {
213 check_key_permission(KeyPerm::req_forced_op(), key, &None).context(
214 "In create_operation: checking forced permission for Domain::BLOB.",
215 )?;
216 }
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700217 (
218 match &key.blob {
Janis Danisevskis1af91262020-08-10 14:58:08 -0700219 Some(blob) => blob,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700220 None => {
221 return Err(Error::sys()).context(concat!(
222 "In create_operation: Key blob must be specified when",
223 " using Domain::BLOB."
224 ))
225 }
226 },
227 None,
Hasini Gunasinghe888dd352020-11-17 23:08:39 +0000228 None,
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000229 BlobMetaData::new(),
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700230 )
231 }
232 _ => {
Janis Danisevskisaec14592020-11-12 09:41:49 -0800233 let (key_id_guard, mut key_entry) = DB
234 .with::<_, Result<(KeyIdGuard, KeyEntry)>>(|db| {
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +0000235 LEGACY_MIGRATOR.with_try_migrate(&key, caller_uid, || {
236 db.borrow_mut().load_key_entry(
237 &key,
238 KeyType::Client,
239 KeyEntryLoadBits::KM,
240 caller_uid,
Janis Danisevskis186d9f42021-03-03 14:40:52 -0800241 |k, av| {
242 check_key_permission(KeyPerm::use_(), k, &av)?;
243 if forced {
244 check_key_permission(KeyPerm::req_forced_op(), k, &av)?;
245 }
246 Ok(())
247 },
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +0000248 )
249 })
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700250 })
251 .context("In create_operation: Failed to load key blob.")?;
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800252
253 let (blob, blob_metadata) =
254 key_entry.take_key_blob_info().ok_or_else(Error::sys).context(concat!(
255 "In create_operation: Successfully loaded key entry, ",
256 "but KM blob was missing."
257 ))?;
258 scoping_blob = blob;
259
Qi Wub9433b52020-12-01 14:52:46 +0800260 (
261 &scoping_blob,
262 Some((key_id_guard.id(), key_entry.into_key_parameters())),
263 Some(key_id_guard),
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000264 blob_metadata,
Qi Wub9433b52020-12-01 14:52:46 +0800265 )
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700266 }
267 };
Janis Danisevskis1af91262020-08-10 14:58:08 -0700268
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700269 let purpose = operation_parameters.iter().find(|p| p.tag == Tag::PURPOSE).map_or(
Janis Danisevskis1af91262020-08-10 14:58:08 -0700270 Err(Error::Km(ErrorCode::INVALID_ARGUMENT))
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700271 .context("In create_operation: No operation purpose specified."),
Janis Danisevskis398e6be2020-12-17 09:29:25 -0800272 |kp| match kp.value {
273 KeyParameterValue::KeyPurpose(p) => Ok(p),
274 _ => Err(Error::Km(ErrorCode::INVALID_ARGUMENT))
275 .context("In create_operation: Malformed KeyParameter."),
276 },
Janis Danisevskis1af91262020-08-10 14:58:08 -0700277 )?;
278
Janis Danisevskis5ed8c532021-01-11 14:19:42 -0800279 let (immediate_hat, mut auth_info) = ENFORCEMENTS
280 .authorize_create(
281 purpose,
Qi Wub9433b52020-12-01 14:52:46 +0800282 key_properties.as_ref(),
283 operation_parameters.as_ref(),
Janis Danisevskise3f7d202021-03-20 14:21:22 -0700284 self.hw_info.timestampTokenRequired,
Janis Danisevskis5ed8c532021-01-11 14:19:42 -0800285 )
286 .context("In create_operation.")?;
Hasini Gunasinghe888dd352020-11-17 23:08:39 +0000287
Janis Danisevskis5ed8c532021-01-11 14:19:42 -0800288 let immediate_hat = immediate_hat.unwrap_or_default();
Hasini Gunasinghe888dd352020-11-17 23:08:39 +0000289
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000290 let km_blob = SUPER_KEY
291 .unwrap_key_if_required(&blob_metadata, km_blob)
292 .context("In create_operation. Failed to handle super encryption.")?;
293
Stephen Crane221bbb52020-12-16 15:52:10 -0800294 let km_dev: Strong<dyn IKeyMintDevice> = self
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700295 .keymint
296 .get_interface()
297 .context("In create_operation: Failed to get KeyMint device")?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700298
Janis Danisevskisaec14592020-11-12 09:41:49 -0800299 let (begin_result, upgraded_blob) = self
300 .upgrade_keyblob_if_required_with(
301 &*km_dev,
302 key_id_guard,
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000303 &(&km_blob, &blob_metadata),
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700304 &operation_parameters,
Janis Danisevskisaec14592020-11-12 09:41:49 -0800305 |blob| loop {
306 match map_km_error(km_dev.begin(
307 purpose,
308 blob,
309 &operation_parameters,
Janis Danisevskis5ed8c532021-01-11 14:19:42 -0800310 &immediate_hat,
Janis Danisevskisaec14592020-11-12 09:41:49 -0800311 )) {
312 Err(Error::Km(ErrorCode::TOO_MANY_OPERATIONS)) => {
Janis Danisevskis186d9f42021-03-03 14:40:52 -0800313 self.operation_db.prune(caller_uid, forced)?;
Janis Danisevskisaec14592020-11-12 09:41:49 -0800314 continue;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700315 }
Janis Danisevskisaec14592020-11-12 09:41:49 -0800316 v => return v,
317 }
318 },
319 )
320 .context("In create_operation: Failed to begin operation.")?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700321
Janis Danisevskis5ed8c532021-01-11 14:19:42 -0800322 let operation_challenge = auth_info.finalize_create_authorization(begin_result.challenge);
Hasini Gunasinghe888dd352020-11-17 23:08:39 +0000323
Janis Danisevskis1af91262020-08-10 14:58:08 -0700324 let operation = match begin_result.operation {
Hasini Gunasinghe888dd352020-11-17 23:08:39 +0000325 Some(km_op) => {
Janis Danisevskis186d9f42021-03-03 14:40:52 -0800326 self.operation_db.create_operation(km_op, caller_uid, auth_info, forced)
Hasini Gunasinghe888dd352020-11-17 23:08:39 +0000327 },
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700328 None => return Err(Error::sys()).context("In create_operation: Begin operation returned successfully, but did not return a valid operation."),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700329 };
330
Stephen Crane221bbb52020-12-16 15:52:10 -0800331 let op_binder: binder::public_api::Strong<dyn IKeystoreOperation> =
Janis Danisevskis1af91262020-08-10 14:58:08 -0700332 KeystoreOperation::new_native_binder(operation)
333 .as_binder()
334 .into_interface()
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700335 .context("In create_operation: Failed to create IKeystoreOperation.")?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700336
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700337 Ok(CreateOperationResponse {
338 iOperation: Some(op_binder),
Hasini Gunasinghe888dd352020-11-17 23:08:39 +0000339 operationChallenge: operation_challenge,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700340 parameters: match begin_result.params.len() {
341 0 => None,
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700342 _ => Some(KeyParameters { keyParameter: begin_result.params }),
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700343 },
Satya Tangiralae2016a82021-03-05 09:28:30 -0800344 // An upgraded blob should only be returned if the caller has permission
345 // to use Domain::BLOB keys. If we got to this point, we already checked
346 // that the caller had that permission.
347 upgradedBlob: if key.domain == Domain::BLOB { upgraded_blob } else { None },
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700348 })
Janis Danisevskis1af91262020-08-10 14:58:08 -0700349 }
350
Janis Danisevskise766edc2021-02-06 12:16:26 -0800351 fn add_certificate_parameters(
352 uid: u32,
353 params: &[KeyParameter],
354 key: &KeyDescriptor,
355 ) -> Result<Vec<KeyParameter>> {
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800356 let mut result = params.to_vec();
Janis Danisevskis2c084012021-01-31 22:23:17 -0800357 // If there is an attestation challenge we need to get an application id.
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800358 if params.iter().any(|kp| kp.tag == Tag::ATTESTATION_CHALLENGE) {
359 let aaid = keystore2_aaid::get_aaid(uid).map_err(|e| {
Janis Danisevskis2c084012021-01-31 22:23:17 -0800360 anyhow!(format!("In add_certificate_parameters: get_aaid returned status {}.", e))
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800361 })?;
362 result.push(KeyParameter {
363 tag: Tag::ATTESTATION_APPLICATION_ID,
364 value: KeyParameterValue::Blob(aaid),
365 });
366 }
Janis Danisevskis2c084012021-01-31 22:23:17 -0800367
Janis Danisevskise766edc2021-02-06 12:16:26 -0800368 if params.iter().any(|kp| kp.tag == Tag::INCLUDE_UNIQUE_ID) {
369 check_key_permission(KeyPerm::gen_unique_id(), key, &None).context(concat!(
370 "In add_certificate_parameters: ",
371 "Caller does not have the permission for device unique attestation."
372 ))?;
373 }
374
Bram Bonné5d6c5102021-02-24 15:09:18 +0100375 // If the caller requests any device identifier attestation tag, check that they hold the
376 // correct Android permission.
377 if params.iter().any(|kp| is_device_id_attestation_tag(kp.tag)) {
378 check_device_attestation_permissions().context(concat!(
379 "In add_certificate_parameters: ",
380 "Caller does not have the permission to attest device identifiers."
381 ))?;
382 }
383
Janis Danisevskis2c084012021-01-31 22:23:17 -0800384 // If we are generating/importing an asymmetric key, we need to make sure
385 // that NOT_BEFORE and NOT_AFTER are present.
386 match params.iter().find(|kp| kp.tag == Tag::ALGORITHM) {
387 Some(KeyParameter { tag: _, value: KeyParameterValue::Algorithm(Algorithm::RSA) })
388 | Some(KeyParameter { tag: _, value: KeyParameterValue::Algorithm(Algorithm::EC) }) => {
389 if !params.iter().any(|kp| kp.tag == Tag::CERTIFICATE_NOT_BEFORE) {
390 result.push(KeyParameter {
391 tag: Tag::CERTIFICATE_NOT_BEFORE,
392 value: KeyParameterValue::DateTime(0),
393 })
394 }
395 if !params.iter().any(|kp| kp.tag == Tag::CERTIFICATE_NOT_AFTER) {
396 result.push(KeyParameter {
397 tag: Tag::CERTIFICATE_NOT_AFTER,
398 value: KeyParameterValue::DateTime(UNDEFINED_NOT_AFTER),
399 })
400 }
401 }
402 _ => {}
403 }
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800404 Ok(result)
405 }
406
Janis Danisevskis1af91262020-08-10 14:58:08 -0700407 fn generate_key(
408 &self,
409 key: &KeyDescriptor,
Shawn Willden8fde4c22021-02-14 13:58:22 -0700410 attest_key_descriptor: Option<&KeyDescriptor>,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700411 params: &[KeyParameter],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700412 flags: i32,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700413 entropy: &[u8],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700414 ) -> Result<KeyMetadata> {
Janis Danisevskis1af91262020-08-10 14:58:08 -0700415 if key.domain != Domain::BLOB && key.alias.is_none() {
416 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
417 .context("In generate_key: Alias must be specified");
418 }
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000419 let caller_uid = ThreadState::get_calling_uid();
Janis Danisevskis1af91262020-08-10 14:58:08 -0700420
421 let key = match key.domain {
422 Domain::APP => KeyDescriptor {
423 domain: key.domain,
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000424 nspace: caller_uid as i64,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700425 alias: key.alias.clone(),
426 blob: None,
427 },
428 _ => key.clone(),
429 };
430
431 // generate_key requires the rebind permission.
432 check_key_permission(KeyPerm::rebind(), &key, &None).context("In generate_key.")?;
Satya Tangiralafdb9c762021-03-09 22:34:22 -0800433 let (attest_key, cert_chain) = match (key.domain, attest_key_descriptor) {
434 (Domain::BLOB, None) => (None, None),
435 _ => DB
436 .with::<_, Result<(Option<AttestationKey>, Option<Certificate>)>>(|db| {
437 self.get_attest_key_and_cert_chain(
438 &key,
439 caller_uid,
440 attest_key_descriptor,
441 params,
442 &mut db.borrow_mut(),
443 )
444 })
445 .context("In generate_key: Trying to get an attestation key")?,
446 };
Janis Danisevskise766edc2021-02-06 12:16:26 -0800447 let params = Self::add_certificate_parameters(caller_uid, params, &key)
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800448 .context("In generate_key: Trying to get aaid.")?;
449
Stephen Crane221bbb52020-12-16 15:52:10 -0800450 let km_dev: Strong<dyn IKeyMintDevice> = self.keymint.get_interface()?;
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800451 map_km_error(km_dev.addRngEntropy(entropy))
452 .context("In generate_key: Trying to add entropy.")?;
Max Bires97f96812021-02-23 23:44:57 -0800453 let mut creation_result = map_km_error(km_dev.generateKey(&params, attest_key.as_ref()))
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800454 .context("In generate_key: While generating Key")?;
Max Bires97f96812021-02-23 23:44:57 -0800455 // The certificate chain ultimately gets flattened into a big DER encoded byte array,
456 // so providing that blob upfront in a single certificate entry should be fine.
457 if let Some(cert) = cert_chain {
458 creation_result.certificateChain.push(cert);
459 }
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000460 let user_id = uid_to_android_user(caller_uid);
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000461 self.store_new_key(key, creation_result, user_id, Some(flags)).context("In generate_key.")
Janis Danisevskis1af91262020-08-10 14:58:08 -0700462 }
463
Max Bires97f96812021-02-23 23:44:57 -0800464 fn get_attest_key_and_cert_chain(
465 &self,
466 key: &KeyDescriptor,
467 caller_uid: u32,
468 attest_key_descriptor: Option<&KeyDescriptor>,
469 params: &[KeyParameter],
470 db: &mut KeystoreDB,
471 ) -> Result<(Option<AttestationKey>, Option<Certificate>)> {
472 match attest_key_descriptor {
473 None => self
474 .rem_prov_state
475 .get_remote_provisioning_key_and_certs(&key, caller_uid, params, db),
476 Some(attest_key) => Ok((
477 Some(
478 self.get_attest_key(&attest_key, caller_uid)
479 .context("In generate_key: Trying to load attest key")?,
480 ),
481 None,
482 )),
483 }
484 }
485
Shawn Willden8fde4c22021-02-14 13:58:22 -0700486 fn get_attest_key(&self, key: &KeyDescriptor, caller_uid: u32) -> Result<AttestationKey> {
487 let (km_blob, cert) = self
488 .load_attest_key_blob_and_cert(&key, caller_uid)
489 .context("In get_attest_key: Failed to load blob and cert")?;
490
Shawn Willden34120872021-02-24 21:56:30 -0700491 let issuer_subject: Vec<u8> = parse_subject_from_certificate(&cert)
Shawn Willden8fde4c22021-02-14 13:58:22 -0700492 .context("In get_attest_key: Failed to parse subject from certificate.")?;
493
494 Ok(AttestationKey {
495 keyBlob: km_blob.to_vec(),
496 attestKeyParams: [].to_vec(),
497 issuerSubjectName: issuer_subject,
498 })
499 }
500
501 fn load_attest_key_blob_and_cert(
502 &self,
503 key: &KeyDescriptor,
504 caller_uid: u32,
505 ) -> Result<(Vec<u8>, Vec<u8>)> {
506 match key.domain {
507 Domain::BLOB => Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)).context(
508 "In load_attest_key_blob_and_cert: Domain::BLOB attestation keys not supported",
509 ),
510 _ => {
511 let (key_id_guard, mut key_entry) = DB
512 .with::<_, Result<(KeyIdGuard, KeyEntry)>>(|db| {
513 db.borrow_mut().load_key_entry(
514 &key,
515 KeyType::Client,
516 KeyEntryLoadBits::BOTH,
517 caller_uid,
518 |k, av| check_key_permission(KeyPerm::use_(), k, &av),
519 )
520 })
521 .context("In load_attest_key_blob_and_cert: Failed to load key.")?;
522
523 let (blob, _) =
524 key_entry.take_key_blob_info().ok_or_else(Error::sys).context(concat!(
525 "In load_attest_key_blob_and_cert: Successfully loaded key entry,",
526 " but KM blob was missing."
527 ))?;
528 let cert = key_entry.take_cert().ok_or_else(Error::sys).context(concat!(
529 "In load_attest_key_blob_and_cert: Successfully loaded key entry,",
530 " but cert was missing."
531 ))?;
532 Ok((blob, cert))
533 }
534 }
535 }
536
Janis Danisevskis1af91262020-08-10 14:58:08 -0700537 fn import_key(
538 &self,
539 key: &KeyDescriptor,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700540 attestation_key: Option<&KeyDescriptor>,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700541 params: &[KeyParameter],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700542 flags: i32,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700543 key_data: &[u8],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700544 ) -> Result<KeyMetadata> {
Janis Danisevskis1af91262020-08-10 14:58:08 -0700545 if key.domain != Domain::BLOB && key.alias.is_none() {
546 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
547 .context("In import_key: Alias must be specified");
548 }
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000549 let caller_uid = ThreadState::get_calling_uid();
Janis Danisevskis1af91262020-08-10 14:58:08 -0700550
551 let key = match key.domain {
552 Domain::APP => KeyDescriptor {
553 domain: key.domain,
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000554 nspace: caller_uid as i64,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700555 alias: key.alias.clone(),
556 blob: None,
557 },
558 _ => key.clone(),
559 };
560
561 // import_key requires the rebind permission.
562 check_key_permission(KeyPerm::rebind(), &key, &None).context("In import_key.")?;
563
Janis Danisevskise766edc2021-02-06 12:16:26 -0800564 let params = Self::add_certificate_parameters(caller_uid, params, &key)
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800565 .context("In import_key: Trying to get aaid.")?;
566
Janis Danisevskis1af91262020-08-10 14:58:08 -0700567 let format = params
568 .iter()
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700569 .find(|p| p.tag == Tag::ALGORITHM)
Janis Danisevskis1af91262020-08-10 14:58:08 -0700570 .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
571 .context("No KeyParameter 'Algorithm'.")
Janis Danisevskis398e6be2020-12-17 09:29:25 -0800572 .and_then(|p| match &p.value {
573 KeyParameterValue::Algorithm(Algorithm::AES)
574 | KeyParameterValue::Algorithm(Algorithm::HMAC)
575 | KeyParameterValue::Algorithm(Algorithm::TRIPLE_DES) => Ok(KeyFormat::RAW),
576 KeyParameterValue::Algorithm(Algorithm::RSA)
577 | KeyParameterValue::Algorithm(Algorithm::EC) => Ok(KeyFormat::PKCS8),
578 v => Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
579 .context(format!("Unknown Algorithm {:?}.", v)),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700580 })
581 .context("In import_key.")?;
582
Stephen Crane221bbb52020-12-16 15:52:10 -0800583 let km_dev: Strong<dyn IKeyMintDevice> =
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800584 self.keymint.get_interface().context("In import_key: Trying to get the KM device")?;
Shawn Willdenc2f3acf2021-02-03 07:07:17 -0700585 let creation_result =
586 map_km_error(km_dev.importKey(&params, format, key_data, None /* attestKey */))
587 .context("In import_key: Trying to call importKey")?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700588
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000589 let user_id = uid_to_android_user(caller_uid);
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000590 self.store_new_key(key, creation_result, user_id, Some(flags)).context("In import_key.")
Janis Danisevskis1af91262020-08-10 14:58:08 -0700591 }
592
593 fn import_wrapped_key(
594 &self,
595 key: &KeyDescriptor,
596 wrapping_key: &KeyDescriptor,
597 masking_key: Option<&[u8]>,
598 params: &[KeyParameter],
599 authenticators: &[AuthenticatorSpec],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700600 ) -> Result<KeyMetadata> {
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800601 let wrapped_data: &[u8] = match key {
602 KeyDescriptor { domain: Domain::APP, blob: Some(ref blob), alias: Some(_), .. }
603 | KeyDescriptor {
604 domain: Domain::SELINUX, blob: Some(ref blob), alias: Some(_), ..
605 } => blob,
606 _ => {
607 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)).context(format!(
608 concat!(
609 "In import_wrapped_key: Alias and blob must be specified ",
610 "and domain must be APP or SELINUX. {:?}"
611 ),
612 key
613 ))
614 }
615 };
Janis Danisevskis1af91262020-08-10 14:58:08 -0700616
Janis Danisevskisaec14592020-11-12 09:41:49 -0800617 if wrapping_key.domain == Domain::BLOB {
618 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)).context(
619 "In import_wrapped_key: Import wrapped key not supported for self managed blobs.",
620 );
621 }
622
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000623 let caller_uid = ThreadState::get_calling_uid();
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000624 let user_id = uid_to_android_user(caller_uid);
625
Janis Danisevskis1af91262020-08-10 14:58:08 -0700626 let key = match key.domain {
627 Domain::APP => KeyDescriptor {
628 domain: key.domain,
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000629 nspace: caller_uid as i64,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700630 alias: key.alias.clone(),
631 blob: None,
632 },
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800633 Domain::SELINUX => KeyDescriptor {
634 domain: Domain::SELINUX,
635 nspace: key.nspace,
636 alias: key.alias.clone(),
637 blob: None,
638 },
639 _ => panic!("Unreachable."),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700640 };
641
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800642 // Import_wrapped_key requires the rebind permission for the new key.
Janis Danisevskis1af91262020-08-10 14:58:08 -0700643 check_key_permission(KeyPerm::rebind(), &key, &None).context("In import_wrapped_key.")?;
644
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000645 let (wrapping_key_id_guard, mut wrapping_key_entry) = DB
Janis Danisevskis1af91262020-08-10 14:58:08 -0700646 .with(|db| {
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +0000647 LEGACY_MIGRATOR.with_try_migrate(&key, caller_uid, || {
648 db.borrow_mut().load_key_entry(
649 &wrapping_key,
650 KeyType::Client,
651 KeyEntryLoadBits::KM,
652 caller_uid,
653 |k, av| check_key_permission(KeyPerm::use_(), k, &av),
654 )
655 })
Janis Danisevskis1af91262020-08-10 14:58:08 -0700656 })
657 .context("Failed to load wrapping key.")?;
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000658
659 let (wrapping_key_blob, wrapping_blob_metadata) = wrapping_key_entry
660 .take_key_blob_info()
661 .ok_or_else(error::Error::sys)
662 .context("No km_blob after successfully loading key. This should never happen.")?;
663
664 let wrapping_key_blob =
665 SUPER_KEY.unwrap_key_if_required(&wrapping_blob_metadata, &wrapping_key_blob).context(
666 "In import_wrapped_key. Failed to handle super encryption for wrapping key.",
667 )?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700668
Janis Danisevskis1af91262020-08-10 14:58:08 -0700669 // km_dev.importWrappedKey does not return a certificate chain.
670 // TODO Do we assume that all wrapped keys are symmetric?
671 // let certificate_chain: Vec<KmCertificate> = Default::default();
672
673 let pw_sid = authenticators
674 .iter()
675 .find_map(|a| match a.authenticatorType {
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700676 HardwareAuthenticatorType::PASSWORD => Some(a.authenticatorId),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700677 _ => None,
678 })
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800679 .unwrap_or(-1);
Janis Danisevskis1af91262020-08-10 14:58:08 -0700680
681 let fp_sid = authenticators
682 .iter()
683 .find_map(|a| match a.authenticatorType {
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700684 HardwareAuthenticatorType::FINGERPRINT => Some(a.authenticatorId),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700685 _ => None,
686 })
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800687 .unwrap_or(-1);
Janis Danisevskis1af91262020-08-10 14:58:08 -0700688
689 let masking_key = masking_key.unwrap_or(ZERO_BLOB_32);
690
Stephen Crane221bbb52020-12-16 15:52:10 -0800691 let km_dev: Strong<dyn IKeyMintDevice> = self.keymint.get_interface()?;
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800692 let (creation_result, _) = self
693 .upgrade_keyblob_if_required_with(
694 &*km_dev,
695 Some(wrapping_key_id_guard),
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000696 &(&wrapping_key_blob, &wrapping_blob_metadata),
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800697 &[],
698 |wrapping_blob| {
699 let creation_result = map_km_error(km_dev.importWrappedKey(
700 wrapped_data,
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800701 wrapping_blob,
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800702 masking_key,
703 &params,
704 pw_sid,
705 fp_sid,
706 ))?;
707 Ok(creation_result)
708 },
709 )
710 .context("In import_wrapped_key.")?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700711
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000712 self.store_new_key(key, creation_result, user_id, None)
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800713 .context("In import_wrapped_key: Trying to store the new key.")
Janis Danisevskis1af91262020-08-10 14:58:08 -0700714 }
Janis Danisevskisaec14592020-11-12 09:41:49 -0800715
716 fn upgrade_keyblob_if_required_with<T, F>(
717 &self,
718 km_dev: &dyn IKeyMintDevice,
719 key_id_guard: Option<KeyIdGuard>,
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000720 blob_info: &(&KeyBlob, &BlobMetaData),
Janis Danisevskisaec14592020-11-12 09:41:49 -0800721 params: &[KeyParameter],
722 f: F,
723 ) -> Result<(T, Option<Vec<u8>>)>
724 where
725 F: Fn(&[u8]) -> Result<T, Error>,
726 {
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800727 match f(blob_info.0) {
Janis Danisevskisaec14592020-11-12 09:41:49 -0800728 Err(Error::Km(ErrorCode::KEY_REQUIRES_UPGRADE)) => {
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800729 let upgraded_blob = map_km_error(km_dev.upgradeKey(blob_info.0, params))
Janis Danisevskisaec14592020-11-12 09:41:49 -0800730 .context("In upgrade_keyblob_if_required_with: Upgrade failed.")?;
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000731
732 let (upgraded_blob_to_be_stored, blob_metadata) =
733 SuperKeyManager::reencrypt_on_upgrade_if_required(blob_info.0, &upgraded_blob)
734 .context(
735 "In upgrade_keyblob_if_required_with: Failed to handle super encryption.",
736 )?;
737
738 let mut blob_metadata = blob_metadata.unwrap_or_else(BlobMetaData::new);
739 if let Some(uuid) = blob_info.1.km_uuid() {
740 blob_metadata.add(BlobMetaEntry::KmUuid(*uuid));
741 }
742
Janis Danisevskisaec14592020-11-12 09:41:49 -0800743 key_id_guard.map_or(Ok(()), |key_id_guard| {
744 DB.with(|db| {
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000745 let mut db = db.borrow_mut();
746 db.set_blob(
Janis Danisevskisaec14592020-11-12 09:41:49 -0800747 &key_id_guard,
Janis Danisevskisb42fc182020-12-15 08:41:27 -0800748 SubComponentType::KEY_BLOB,
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000749 Some(&upgraded_blob_to_be_stored),
750 Some(&blob_metadata),
Janis Danisevskisaec14592020-11-12 09:41:49 -0800751 )
752 })
753 .context(concat!(
754 "In upgrade_keyblob_if_required_with: ",
755 "Failed to insert upgraded blob into the database.",
756 ))
757 })?;
758 match f(&upgraded_blob) {
759 Ok(v) => Ok((v, Some(upgraded_blob))),
760 Err(e) => Err(e).context(concat!(
761 "In upgrade_keyblob_if_required_with: ",
762 "Failed to perform operation on second try."
763 )),
764 }
765 }
766 Err(e) => {
767 Err(e).context("In upgrade_keyblob_if_required_with: Failed perform operation.")
768 }
769 Ok(v) => Ok((v, None)),
770 }
771 }
Janis Danisevskis1af91262020-08-10 14:58:08 -0700772}
773
774impl binder::Interface for KeystoreSecurityLevel {}
775
776impl IKeystoreSecurityLevel for KeystoreSecurityLevel {
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700777 fn createOperation(
Janis Danisevskis1af91262020-08-10 14:58:08 -0700778 &self,
779 key: &KeyDescriptor,
780 operation_parameters: &[KeyParameter],
781 forced: bool,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700782 ) -> binder::public_api::Result<CreateOperationResponse> {
783 map_or_log_err(self.create_operation(key, operation_parameters, forced), Ok)
Janis Danisevskis1af91262020-08-10 14:58:08 -0700784 }
785 fn generateKey(
786 &self,
787 key: &KeyDescriptor,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700788 attestation_key: Option<&KeyDescriptor>,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700789 params: &[KeyParameter],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700790 flags: i32,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700791 entropy: &[u8],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700792 ) -> binder::public_api::Result<KeyMetadata> {
793 map_or_log_err(self.generate_key(key, attestation_key, params, flags, entropy), Ok)
Janis Danisevskis1af91262020-08-10 14:58:08 -0700794 }
795 fn importKey(
796 &self,
797 key: &KeyDescriptor,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700798 attestation_key: Option<&KeyDescriptor>,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700799 params: &[KeyParameter],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700800 flags: i32,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700801 key_data: &[u8],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700802 ) -> binder::public_api::Result<KeyMetadata> {
803 map_or_log_err(self.import_key(key, attestation_key, params, flags, key_data), Ok)
Janis Danisevskis1af91262020-08-10 14:58:08 -0700804 }
805 fn importWrappedKey(
806 &self,
807 key: &KeyDescriptor,
808 wrapping_key: &KeyDescriptor,
809 masking_key: Option<&[u8]>,
810 params: &[KeyParameter],
811 authenticators: &[AuthenticatorSpec],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700812 ) -> binder::public_api::Result<KeyMetadata> {
Janis Danisevskis1af91262020-08-10 14:58:08 -0700813 map_or_log_err(
814 self.import_wrapped_key(key, wrapping_key, masking_key, params, authenticators),
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700815 Ok,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700816 )
817 }
818}