blob: 2ad8fe11cc648c78dd13863ae2bf5a1639434f87 [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 },
344 })
Janis Danisevskis1af91262020-08-10 14:58:08 -0700345 }
346
Janis Danisevskise766edc2021-02-06 12:16:26 -0800347 fn add_certificate_parameters(
348 uid: u32,
349 params: &[KeyParameter],
350 key: &KeyDescriptor,
351 ) -> Result<Vec<KeyParameter>> {
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800352 let mut result = params.to_vec();
Janis Danisevskis2c084012021-01-31 22:23:17 -0800353 // If there is an attestation challenge we need to get an application id.
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800354 if params.iter().any(|kp| kp.tag == Tag::ATTESTATION_CHALLENGE) {
355 let aaid = keystore2_aaid::get_aaid(uid).map_err(|e| {
Janis Danisevskis2c084012021-01-31 22:23:17 -0800356 anyhow!(format!("In add_certificate_parameters: get_aaid returned status {}.", e))
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800357 })?;
358 result.push(KeyParameter {
359 tag: Tag::ATTESTATION_APPLICATION_ID,
360 value: KeyParameterValue::Blob(aaid),
361 });
362 }
Janis Danisevskis2c084012021-01-31 22:23:17 -0800363
Janis Danisevskise766edc2021-02-06 12:16:26 -0800364 if params.iter().any(|kp| kp.tag == Tag::INCLUDE_UNIQUE_ID) {
365 check_key_permission(KeyPerm::gen_unique_id(), key, &None).context(concat!(
366 "In add_certificate_parameters: ",
367 "Caller does not have the permission for device unique attestation."
368 ))?;
369 }
370
Bram Bonné5d6c5102021-02-24 15:09:18 +0100371 // If the caller requests any device identifier attestation tag, check that they hold the
372 // correct Android permission.
373 if params.iter().any(|kp| is_device_id_attestation_tag(kp.tag)) {
374 check_device_attestation_permissions().context(concat!(
375 "In add_certificate_parameters: ",
376 "Caller does not have the permission to attest device identifiers."
377 ))?;
378 }
379
Janis Danisevskis2c084012021-01-31 22:23:17 -0800380 // If we are generating/importing an asymmetric key, we need to make sure
381 // that NOT_BEFORE and NOT_AFTER are present.
382 match params.iter().find(|kp| kp.tag == Tag::ALGORITHM) {
383 Some(KeyParameter { tag: _, value: KeyParameterValue::Algorithm(Algorithm::RSA) })
384 | Some(KeyParameter { tag: _, value: KeyParameterValue::Algorithm(Algorithm::EC) }) => {
385 if !params.iter().any(|kp| kp.tag == Tag::CERTIFICATE_NOT_BEFORE) {
386 result.push(KeyParameter {
387 tag: Tag::CERTIFICATE_NOT_BEFORE,
388 value: KeyParameterValue::DateTime(0),
389 })
390 }
391 if !params.iter().any(|kp| kp.tag == Tag::CERTIFICATE_NOT_AFTER) {
392 result.push(KeyParameter {
393 tag: Tag::CERTIFICATE_NOT_AFTER,
394 value: KeyParameterValue::DateTime(UNDEFINED_NOT_AFTER),
395 })
396 }
397 }
398 _ => {}
399 }
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800400 Ok(result)
401 }
402
Janis Danisevskis1af91262020-08-10 14:58:08 -0700403 fn generate_key(
404 &self,
405 key: &KeyDescriptor,
Shawn Willden8fde4c22021-02-14 13:58:22 -0700406 attest_key_descriptor: Option<&KeyDescriptor>,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700407 params: &[KeyParameter],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700408 flags: i32,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700409 entropy: &[u8],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700410 ) -> Result<KeyMetadata> {
Janis Danisevskis1af91262020-08-10 14:58:08 -0700411 if key.domain != Domain::BLOB && key.alias.is_none() {
412 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
413 .context("In generate_key: Alias must be specified");
414 }
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000415 let caller_uid = ThreadState::get_calling_uid();
Janis Danisevskis1af91262020-08-10 14:58:08 -0700416
417 let key = match key.domain {
418 Domain::APP => KeyDescriptor {
419 domain: key.domain,
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000420 nspace: caller_uid as i64,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700421 alias: key.alias.clone(),
422 blob: None,
423 },
424 _ => key.clone(),
425 };
426
427 // generate_key requires the rebind permission.
428 check_key_permission(KeyPerm::rebind(), &key, &None).context("In generate_key.")?;
Satya Tangiralafdb9c762021-03-09 22:34:22 -0800429 let (attest_key, cert_chain) = match (key.domain, attest_key_descriptor) {
430 (Domain::BLOB, None) => (None, None),
431 _ => DB
432 .with::<_, Result<(Option<AttestationKey>, Option<Certificate>)>>(|db| {
433 self.get_attest_key_and_cert_chain(
434 &key,
435 caller_uid,
436 attest_key_descriptor,
437 params,
438 &mut db.borrow_mut(),
439 )
440 })
441 .context("In generate_key: Trying to get an attestation key")?,
442 };
Janis Danisevskise766edc2021-02-06 12:16:26 -0800443 let params = Self::add_certificate_parameters(caller_uid, params, &key)
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800444 .context("In generate_key: Trying to get aaid.")?;
445
Stephen Crane221bbb52020-12-16 15:52:10 -0800446 let km_dev: Strong<dyn IKeyMintDevice> = self.keymint.get_interface()?;
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800447 map_km_error(km_dev.addRngEntropy(entropy))
448 .context("In generate_key: Trying to add entropy.")?;
Max Bires97f96812021-02-23 23:44:57 -0800449 let mut creation_result = map_km_error(km_dev.generateKey(&params, attest_key.as_ref()))
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800450 .context("In generate_key: While generating Key")?;
Max Bires97f96812021-02-23 23:44:57 -0800451 // The certificate chain ultimately gets flattened into a big DER encoded byte array,
452 // so providing that blob upfront in a single certificate entry should be fine.
453 if let Some(cert) = cert_chain {
454 creation_result.certificateChain.push(cert);
455 }
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000456 let user_id = uid_to_android_user(caller_uid);
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000457 self.store_new_key(key, creation_result, user_id, Some(flags)).context("In generate_key.")
Janis Danisevskis1af91262020-08-10 14:58:08 -0700458 }
459
Max Bires97f96812021-02-23 23:44:57 -0800460 fn get_attest_key_and_cert_chain(
461 &self,
462 key: &KeyDescriptor,
463 caller_uid: u32,
464 attest_key_descriptor: Option<&KeyDescriptor>,
465 params: &[KeyParameter],
466 db: &mut KeystoreDB,
467 ) -> Result<(Option<AttestationKey>, Option<Certificate>)> {
468 match attest_key_descriptor {
469 None => self
470 .rem_prov_state
471 .get_remote_provisioning_key_and_certs(&key, caller_uid, params, db),
472 Some(attest_key) => Ok((
473 Some(
474 self.get_attest_key(&attest_key, caller_uid)
475 .context("In generate_key: Trying to load attest key")?,
476 ),
477 None,
478 )),
479 }
480 }
481
Shawn Willden8fde4c22021-02-14 13:58:22 -0700482 fn get_attest_key(&self, key: &KeyDescriptor, caller_uid: u32) -> Result<AttestationKey> {
483 let (km_blob, cert) = self
484 .load_attest_key_blob_and_cert(&key, caller_uid)
485 .context("In get_attest_key: Failed to load blob and cert")?;
486
Shawn Willden34120872021-02-24 21:56:30 -0700487 let issuer_subject: Vec<u8> = parse_subject_from_certificate(&cert)
Shawn Willden8fde4c22021-02-14 13:58:22 -0700488 .context("In get_attest_key: Failed to parse subject from certificate.")?;
489
490 Ok(AttestationKey {
491 keyBlob: km_blob.to_vec(),
492 attestKeyParams: [].to_vec(),
493 issuerSubjectName: issuer_subject,
494 })
495 }
496
497 fn load_attest_key_blob_and_cert(
498 &self,
499 key: &KeyDescriptor,
500 caller_uid: u32,
501 ) -> Result<(Vec<u8>, Vec<u8>)> {
502 match key.domain {
503 Domain::BLOB => Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)).context(
504 "In load_attest_key_blob_and_cert: Domain::BLOB attestation keys not supported",
505 ),
506 _ => {
507 let (key_id_guard, mut key_entry) = DB
508 .with::<_, Result<(KeyIdGuard, KeyEntry)>>(|db| {
509 db.borrow_mut().load_key_entry(
510 &key,
511 KeyType::Client,
512 KeyEntryLoadBits::BOTH,
513 caller_uid,
514 |k, av| check_key_permission(KeyPerm::use_(), k, &av),
515 )
516 })
517 .context("In load_attest_key_blob_and_cert: Failed to load key.")?;
518
519 let (blob, _) =
520 key_entry.take_key_blob_info().ok_or_else(Error::sys).context(concat!(
521 "In load_attest_key_blob_and_cert: Successfully loaded key entry,",
522 " but KM blob was missing."
523 ))?;
524 let cert = key_entry.take_cert().ok_or_else(Error::sys).context(concat!(
525 "In load_attest_key_blob_and_cert: Successfully loaded key entry,",
526 " but cert was missing."
527 ))?;
528 Ok((blob, cert))
529 }
530 }
531 }
532
Janis Danisevskis1af91262020-08-10 14:58:08 -0700533 fn import_key(
534 &self,
535 key: &KeyDescriptor,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700536 attestation_key: Option<&KeyDescriptor>,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700537 params: &[KeyParameter],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700538 flags: i32,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700539 key_data: &[u8],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700540 ) -> Result<KeyMetadata> {
Janis Danisevskis1af91262020-08-10 14:58:08 -0700541 if key.domain != Domain::BLOB && key.alias.is_none() {
542 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
543 .context("In import_key: Alias must be specified");
544 }
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000545 let caller_uid = ThreadState::get_calling_uid();
Janis Danisevskis1af91262020-08-10 14:58:08 -0700546
547 let key = match key.domain {
548 Domain::APP => KeyDescriptor {
549 domain: key.domain,
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000550 nspace: caller_uid as i64,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700551 alias: key.alias.clone(),
552 blob: None,
553 },
554 _ => key.clone(),
555 };
556
557 // import_key requires the rebind permission.
558 check_key_permission(KeyPerm::rebind(), &key, &None).context("In import_key.")?;
559
Janis Danisevskise766edc2021-02-06 12:16:26 -0800560 let params = Self::add_certificate_parameters(caller_uid, params, &key)
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800561 .context("In import_key: Trying to get aaid.")?;
562
Janis Danisevskis1af91262020-08-10 14:58:08 -0700563 let format = params
564 .iter()
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700565 .find(|p| p.tag == Tag::ALGORITHM)
Janis Danisevskis1af91262020-08-10 14:58:08 -0700566 .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
567 .context("No KeyParameter 'Algorithm'.")
Janis Danisevskis398e6be2020-12-17 09:29:25 -0800568 .and_then(|p| match &p.value {
569 KeyParameterValue::Algorithm(Algorithm::AES)
570 | KeyParameterValue::Algorithm(Algorithm::HMAC)
571 | KeyParameterValue::Algorithm(Algorithm::TRIPLE_DES) => Ok(KeyFormat::RAW),
572 KeyParameterValue::Algorithm(Algorithm::RSA)
573 | KeyParameterValue::Algorithm(Algorithm::EC) => Ok(KeyFormat::PKCS8),
574 v => Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
575 .context(format!("Unknown Algorithm {:?}.", v)),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700576 })
577 .context("In import_key.")?;
578
Stephen Crane221bbb52020-12-16 15:52:10 -0800579 let km_dev: Strong<dyn IKeyMintDevice> =
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800580 self.keymint.get_interface().context("In import_key: Trying to get the KM device")?;
Shawn Willdenc2f3acf2021-02-03 07:07:17 -0700581 let creation_result =
582 map_km_error(km_dev.importKey(&params, format, key_data, None /* attestKey */))
583 .context("In import_key: Trying to call importKey")?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700584
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000585 let user_id = uid_to_android_user(caller_uid);
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000586 self.store_new_key(key, creation_result, user_id, Some(flags)).context("In import_key.")
Janis Danisevskis1af91262020-08-10 14:58:08 -0700587 }
588
589 fn import_wrapped_key(
590 &self,
591 key: &KeyDescriptor,
592 wrapping_key: &KeyDescriptor,
593 masking_key: Option<&[u8]>,
594 params: &[KeyParameter],
595 authenticators: &[AuthenticatorSpec],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700596 ) -> Result<KeyMetadata> {
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800597 let wrapped_data: &[u8] = match key {
598 KeyDescriptor { domain: Domain::APP, blob: Some(ref blob), alias: Some(_), .. }
599 | KeyDescriptor {
600 domain: Domain::SELINUX, blob: Some(ref blob), alias: Some(_), ..
601 } => blob,
602 _ => {
603 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)).context(format!(
604 concat!(
605 "In import_wrapped_key: Alias and blob must be specified ",
606 "and domain must be APP or SELINUX. {:?}"
607 ),
608 key
609 ))
610 }
611 };
Janis Danisevskis1af91262020-08-10 14:58:08 -0700612
Janis Danisevskisaec14592020-11-12 09:41:49 -0800613 if wrapping_key.domain == Domain::BLOB {
614 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)).context(
615 "In import_wrapped_key: Import wrapped key not supported for self managed blobs.",
616 );
617 }
618
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000619 let caller_uid = ThreadState::get_calling_uid();
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000620 let user_id = uid_to_android_user(caller_uid);
621
Janis Danisevskis1af91262020-08-10 14:58:08 -0700622 let key = match key.domain {
623 Domain::APP => KeyDescriptor {
624 domain: key.domain,
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000625 nspace: caller_uid as i64,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700626 alias: key.alias.clone(),
627 blob: None,
628 },
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800629 Domain::SELINUX => KeyDescriptor {
630 domain: Domain::SELINUX,
631 nspace: key.nspace,
632 alias: key.alias.clone(),
633 blob: None,
634 },
635 _ => panic!("Unreachable."),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700636 };
637
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800638 // Import_wrapped_key requires the rebind permission for the new key.
Janis Danisevskis1af91262020-08-10 14:58:08 -0700639 check_key_permission(KeyPerm::rebind(), &key, &None).context("In import_wrapped_key.")?;
640
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000641 let (wrapping_key_id_guard, mut wrapping_key_entry) = DB
Janis Danisevskis1af91262020-08-10 14:58:08 -0700642 .with(|db| {
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +0000643 LEGACY_MIGRATOR.with_try_migrate(&key, caller_uid, || {
644 db.borrow_mut().load_key_entry(
645 &wrapping_key,
646 KeyType::Client,
647 KeyEntryLoadBits::KM,
648 caller_uid,
649 |k, av| check_key_permission(KeyPerm::use_(), k, &av),
650 )
651 })
Janis Danisevskis1af91262020-08-10 14:58:08 -0700652 })
653 .context("Failed to load wrapping key.")?;
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000654
655 let (wrapping_key_blob, wrapping_blob_metadata) = wrapping_key_entry
656 .take_key_blob_info()
657 .ok_or_else(error::Error::sys)
658 .context("No km_blob after successfully loading key. This should never happen.")?;
659
660 let wrapping_key_blob =
661 SUPER_KEY.unwrap_key_if_required(&wrapping_blob_metadata, &wrapping_key_blob).context(
662 "In import_wrapped_key. Failed to handle super encryption for wrapping key.",
663 )?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700664
Janis Danisevskis1af91262020-08-10 14:58:08 -0700665 // km_dev.importWrappedKey does not return a certificate chain.
666 // TODO Do we assume that all wrapped keys are symmetric?
667 // let certificate_chain: Vec<KmCertificate> = Default::default();
668
669 let pw_sid = authenticators
670 .iter()
671 .find_map(|a| match a.authenticatorType {
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700672 HardwareAuthenticatorType::PASSWORD => Some(a.authenticatorId),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700673 _ => None,
674 })
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800675 .unwrap_or(-1);
Janis Danisevskis1af91262020-08-10 14:58:08 -0700676
677 let fp_sid = authenticators
678 .iter()
679 .find_map(|a| match a.authenticatorType {
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700680 HardwareAuthenticatorType::FINGERPRINT => Some(a.authenticatorId),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700681 _ => None,
682 })
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800683 .unwrap_or(-1);
Janis Danisevskis1af91262020-08-10 14:58:08 -0700684
685 let masking_key = masking_key.unwrap_or(ZERO_BLOB_32);
686
Stephen Crane221bbb52020-12-16 15:52:10 -0800687 let km_dev: Strong<dyn IKeyMintDevice> = self.keymint.get_interface()?;
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800688 let (creation_result, _) = self
689 .upgrade_keyblob_if_required_with(
690 &*km_dev,
691 Some(wrapping_key_id_guard),
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000692 &(&wrapping_key_blob, &wrapping_blob_metadata),
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800693 &[],
694 |wrapping_blob| {
695 let creation_result = map_km_error(km_dev.importWrappedKey(
696 wrapped_data,
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800697 wrapping_blob,
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800698 masking_key,
699 &params,
700 pw_sid,
701 fp_sid,
702 ))?;
703 Ok(creation_result)
704 },
705 )
706 .context("In import_wrapped_key.")?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700707
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000708 self.store_new_key(key, creation_result, user_id, None)
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800709 .context("In import_wrapped_key: Trying to store the new key.")
Janis Danisevskis1af91262020-08-10 14:58:08 -0700710 }
Janis Danisevskisaec14592020-11-12 09:41:49 -0800711
712 fn upgrade_keyblob_if_required_with<T, F>(
713 &self,
714 km_dev: &dyn IKeyMintDevice,
715 key_id_guard: Option<KeyIdGuard>,
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000716 blob_info: &(&KeyBlob, &BlobMetaData),
Janis Danisevskisaec14592020-11-12 09:41:49 -0800717 params: &[KeyParameter],
718 f: F,
719 ) -> Result<(T, Option<Vec<u8>>)>
720 where
721 F: Fn(&[u8]) -> Result<T, Error>,
722 {
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800723 match f(blob_info.0) {
Janis Danisevskisaec14592020-11-12 09:41:49 -0800724 Err(Error::Km(ErrorCode::KEY_REQUIRES_UPGRADE)) => {
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800725 let upgraded_blob = map_km_error(km_dev.upgradeKey(blob_info.0, params))
Janis Danisevskisaec14592020-11-12 09:41:49 -0800726 .context("In upgrade_keyblob_if_required_with: Upgrade failed.")?;
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000727
728 let (upgraded_blob_to_be_stored, blob_metadata) =
729 SuperKeyManager::reencrypt_on_upgrade_if_required(blob_info.0, &upgraded_blob)
730 .context(
731 "In upgrade_keyblob_if_required_with: Failed to handle super encryption.",
732 )?;
733
734 let mut blob_metadata = blob_metadata.unwrap_or_else(BlobMetaData::new);
735 if let Some(uuid) = blob_info.1.km_uuid() {
736 blob_metadata.add(BlobMetaEntry::KmUuid(*uuid));
737 }
738
Janis Danisevskisaec14592020-11-12 09:41:49 -0800739 key_id_guard.map_or(Ok(()), |key_id_guard| {
740 DB.with(|db| {
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000741 let mut db = db.borrow_mut();
742 db.set_blob(
Janis Danisevskisaec14592020-11-12 09:41:49 -0800743 &key_id_guard,
Janis Danisevskisb42fc182020-12-15 08:41:27 -0800744 SubComponentType::KEY_BLOB,
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000745 Some(&upgraded_blob_to_be_stored),
746 Some(&blob_metadata),
Janis Danisevskisaec14592020-11-12 09:41:49 -0800747 )
748 })
749 .context(concat!(
750 "In upgrade_keyblob_if_required_with: ",
751 "Failed to insert upgraded blob into the database.",
752 ))
753 })?;
754 match f(&upgraded_blob) {
755 Ok(v) => Ok((v, Some(upgraded_blob))),
756 Err(e) => Err(e).context(concat!(
757 "In upgrade_keyblob_if_required_with: ",
758 "Failed to perform operation on second try."
759 )),
760 }
761 }
762 Err(e) => {
763 Err(e).context("In upgrade_keyblob_if_required_with: Failed perform operation.")
764 }
765 Ok(v) => Ok((v, None)),
766 }
767 }
Janis Danisevskis1af91262020-08-10 14:58:08 -0700768}
769
770impl binder::Interface for KeystoreSecurityLevel {}
771
772impl IKeystoreSecurityLevel for KeystoreSecurityLevel {
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700773 fn createOperation(
Janis Danisevskis1af91262020-08-10 14:58:08 -0700774 &self,
775 key: &KeyDescriptor,
776 operation_parameters: &[KeyParameter],
777 forced: bool,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700778 ) -> binder::public_api::Result<CreateOperationResponse> {
779 map_or_log_err(self.create_operation(key, operation_parameters, forced), Ok)
Janis Danisevskis1af91262020-08-10 14:58:08 -0700780 }
781 fn generateKey(
782 &self,
783 key: &KeyDescriptor,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700784 attestation_key: Option<&KeyDescriptor>,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700785 params: &[KeyParameter],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700786 flags: i32,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700787 entropy: &[u8],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700788 ) -> binder::public_api::Result<KeyMetadata> {
789 map_or_log_err(self.generate_key(key, attestation_key, params, flags, entropy), Ok)
Janis Danisevskis1af91262020-08-10 14:58:08 -0700790 }
791 fn importKey(
792 &self,
793 key: &KeyDescriptor,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700794 attestation_key: Option<&KeyDescriptor>,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700795 params: &[KeyParameter],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700796 flags: i32,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700797 key_data: &[u8],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700798 ) -> binder::public_api::Result<KeyMetadata> {
799 map_or_log_err(self.import_key(key, attestation_key, params, flags, key_data), Ok)
Janis Danisevskis1af91262020-08-10 14:58:08 -0700800 }
801 fn importWrappedKey(
802 &self,
803 key: &KeyDescriptor,
804 wrapping_key: &KeyDescriptor,
805 masking_key: Option<&[u8]>,
806 params: &[KeyParameter],
807 authenticators: &[AuthenticatorSpec],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700808 ) -> binder::public_api::Result<KeyMetadata> {
Janis Danisevskis1af91262020-08-10 14:58:08 -0700809 map_or_log_err(
810 self.import_wrapped_key(key, wrapping_key, masking_key, params, authenticators),
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700811 Ok,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700812 )
813 }
814}