blob: e50155b294f20d8396a77591ff376b3d41f7acd8 [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::{
Janis Danisevskis3541f3e2021-03-20 14:18:52 -070021 Algorithm::Algorithm, AttestationKey::AttestationKey,
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
Janis Danisevskis3541f3e2021-03-20 14:18:52 -070035use crate::attestation_key_utils::{get_attest_key_info, AttestationKeyInfo};
36use crate::database::{CertificateInfo, KeyIdGuard};
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +000037use crate::globals::{DB, ENFORCEMENTS, LEGACY_MIGRATOR, SUPER_KEY};
Hasini Gunasinghe888dd352020-11-17 23:08:39 +000038use crate::key_parameter::KeyParameter as KsKeyParam;
Hasini Gunasinghea020b532021-01-07 21:42:35 +000039use crate::key_parameter::KeyParameterValue as KsKeyParamValue;
Max Bires97f96812021-02-23 23:44:57 -080040use crate::remote_provisioning::RemProvState;
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +000041use crate::super_key::{KeyBlob, SuperKeyManager};
Bram Bonné5d6c5102021-02-24 15:09:18 +010042use crate::utils::{
43 check_device_attestation_permissions, check_key_permission, is_device_id_attestation_tag,
44 uid_to_android_user, Asp,
45};
Max Bires8e93d2b2021-01-14 13:17:59 -080046use crate::{
Janis Danisevskis7e8b4622021-02-13 10:01:59 -080047 database::{
48 BlobMetaData, BlobMetaEntry, DateTime, KeyEntry, KeyEntryLoadBits, KeyMetaData,
49 KeyMetaEntry, KeyType, SubComponentType, Uuid,
50 },
51 operation::KeystoreOperation,
52 operation::OperationDb,
Janis Danisevskisb42fc182020-12-15 08:41:27 -080053 permission::KeyPerm,
54};
55use crate::{
Janis Danisevskis04b02832020-10-26 09:21:40 -070056 error::{self, map_km_error, map_or_log_err, Error, ErrorCode},
57 utils::key_characteristics_to_internal,
58};
Janis Danisevskis212c68b2021-01-14 22:29:28 -080059use anyhow::{anyhow, Context, Result};
Andrew Walbran808e8602021-03-16 13:58:28 +000060use binder::{IBinderInternal, Strong, ThreadState};
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.
Janis Danisevskis3541f3e2021-03-20 14:18:52 -0700432 // Must return on error for security reasons.
Janis Danisevskis1af91262020-08-10 14:58:08 -0700433 check_key_permission(KeyPerm::rebind(), &key, &None).context("In generate_key.")?;
Janis Danisevskis3541f3e2021-03-20 14:18:52 -0700434
435 let attestation_key_info = match (key.domain, attest_key_descriptor) {
436 (Domain::BLOB, _) => None,
Satya Tangiralafdb9c762021-03-09 22:34:22 -0800437 _ => DB
Janis Danisevskis3541f3e2021-03-20 14:18:52 -0700438 .with(|db| {
439 get_attest_key_info(
Satya Tangiralafdb9c762021-03-09 22:34:22 -0800440 &key,
441 caller_uid,
442 attest_key_descriptor,
443 params,
Janis Danisevskis3541f3e2021-03-20 14:18:52 -0700444 &self.rem_prov_state,
Satya Tangiralafdb9c762021-03-09 22:34:22 -0800445 &mut db.borrow_mut(),
446 )
447 })
448 .context("In generate_key: Trying to get an attestation key")?,
449 };
Janis Danisevskise766edc2021-02-06 12:16:26 -0800450 let params = Self::add_certificate_parameters(caller_uid, params, &key)
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800451 .context("In generate_key: Trying to get aaid.")?;
452
Stephen Crane221bbb52020-12-16 15:52:10 -0800453 let km_dev: Strong<dyn IKeyMintDevice> = self.keymint.get_interface()?;
Janis Danisevskis3541f3e2021-03-20 14:18:52 -0700454
455 let creation_result = match attestation_key_info {
456 Some(AttestationKeyInfo::UserGenerated {
457 key_id_guard,
458 blob,
459 blob_metadata,
460 issuer_subject,
461 }) => self
462 .upgrade_keyblob_if_required_with(
463 &*km_dev,
464 Some(key_id_guard),
465 &(&KeyBlob::Ref(&blob), &blob_metadata),
466 &params,
467 |blob| {
468 let attest_key = Some(AttestationKey {
469 keyBlob: blob.to_vec(),
470 attestKeyParams: vec![],
471 issuerSubjectName: issuer_subject.clone(),
472 });
473 map_km_error(km_dev.generateKey(&params, attest_key.as_ref()))
474 },
475 )
476 .context("In generate_key: Using user generated attestation key.")
477 .map(|(result, _)| result),
478 Some(AttestationKeyInfo::RemoteProvisioned { attestation_key, attestation_certs }) => {
479 map_km_error(km_dev.generateKey(&params, Some(&attestation_key)))
480 .context("While generating Key with remote provisioned attestation key.")
481 .map(|mut creation_result| {
482 creation_result.certificateChain.push(attestation_certs);
483 creation_result
484 })
485 }
486 None => map_km_error(km_dev.generateKey(&params, None))
487 .context("While generating Key without explicit attestation key."),
Max Bires97f96812021-02-23 23:44:57 -0800488 }
Janis Danisevskis3541f3e2021-03-20 14:18:52 -0700489 .context("In generate_key.")?;
490
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000491 let user_id = uid_to_android_user(caller_uid);
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000492 self.store_new_key(key, creation_result, user_id, Some(flags)).context("In generate_key.")
Janis Danisevskis1af91262020-08-10 14:58:08 -0700493 }
494
495 fn import_key(
496 &self,
497 key: &KeyDescriptor,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700498 attestation_key: Option<&KeyDescriptor>,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700499 params: &[KeyParameter],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700500 flags: i32,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700501 key_data: &[u8],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700502 ) -> Result<KeyMetadata> {
Janis Danisevskis1af91262020-08-10 14:58:08 -0700503 if key.domain != Domain::BLOB && key.alias.is_none() {
504 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
505 .context("In import_key: Alias must be specified");
506 }
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000507 let caller_uid = ThreadState::get_calling_uid();
Janis Danisevskis1af91262020-08-10 14:58:08 -0700508
509 let key = match key.domain {
510 Domain::APP => KeyDescriptor {
511 domain: key.domain,
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000512 nspace: caller_uid as i64,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700513 alias: key.alias.clone(),
514 blob: None,
515 },
516 _ => key.clone(),
517 };
518
519 // import_key requires the rebind permission.
520 check_key_permission(KeyPerm::rebind(), &key, &None).context("In import_key.")?;
521
Janis Danisevskise766edc2021-02-06 12:16:26 -0800522 let params = Self::add_certificate_parameters(caller_uid, params, &key)
Janis Danisevskis212c68b2021-01-14 22:29:28 -0800523 .context("In import_key: Trying to get aaid.")?;
524
Janis Danisevskis1af91262020-08-10 14:58:08 -0700525 let format = params
526 .iter()
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700527 .find(|p| p.tag == Tag::ALGORITHM)
Janis Danisevskis1af91262020-08-10 14:58:08 -0700528 .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
529 .context("No KeyParameter 'Algorithm'.")
Janis Danisevskis398e6be2020-12-17 09:29:25 -0800530 .and_then(|p| match &p.value {
531 KeyParameterValue::Algorithm(Algorithm::AES)
532 | KeyParameterValue::Algorithm(Algorithm::HMAC)
533 | KeyParameterValue::Algorithm(Algorithm::TRIPLE_DES) => Ok(KeyFormat::RAW),
534 KeyParameterValue::Algorithm(Algorithm::RSA)
535 | KeyParameterValue::Algorithm(Algorithm::EC) => Ok(KeyFormat::PKCS8),
536 v => Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
537 .context(format!("Unknown Algorithm {:?}.", v)),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700538 })
539 .context("In import_key.")?;
540
Stephen Crane221bbb52020-12-16 15:52:10 -0800541 let km_dev: Strong<dyn IKeyMintDevice> =
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800542 self.keymint.get_interface().context("In import_key: Trying to get the KM device")?;
Shawn Willdenc2f3acf2021-02-03 07:07:17 -0700543 let creation_result =
544 map_km_error(km_dev.importKey(&params, format, key_data, None /* attestKey */))
545 .context("In import_key: Trying to call importKey")?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700546
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000547 let user_id = uid_to_android_user(caller_uid);
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000548 self.store_new_key(key, creation_result, user_id, Some(flags)).context("In import_key.")
Janis Danisevskis1af91262020-08-10 14:58:08 -0700549 }
550
551 fn import_wrapped_key(
552 &self,
553 key: &KeyDescriptor,
554 wrapping_key: &KeyDescriptor,
555 masking_key: Option<&[u8]>,
556 params: &[KeyParameter],
557 authenticators: &[AuthenticatorSpec],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700558 ) -> Result<KeyMetadata> {
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800559 let wrapped_data: &[u8] = match key {
560 KeyDescriptor { domain: Domain::APP, blob: Some(ref blob), alias: Some(_), .. }
561 | KeyDescriptor {
562 domain: Domain::SELINUX, blob: Some(ref blob), alias: Some(_), ..
563 } => blob,
564 _ => {
565 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)).context(format!(
566 concat!(
567 "In import_wrapped_key: Alias and blob must be specified ",
568 "and domain must be APP or SELINUX. {:?}"
569 ),
570 key
571 ))
572 }
573 };
Janis Danisevskis1af91262020-08-10 14:58:08 -0700574
Janis Danisevskisaec14592020-11-12 09:41:49 -0800575 if wrapping_key.domain == Domain::BLOB {
576 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)).context(
577 "In import_wrapped_key: Import wrapped key not supported for self managed blobs.",
578 );
579 }
580
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000581 let caller_uid = ThreadState::get_calling_uid();
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000582 let user_id = uid_to_android_user(caller_uid);
583
Janis Danisevskis1af91262020-08-10 14:58:08 -0700584 let key = match key.domain {
585 Domain::APP => KeyDescriptor {
586 domain: key.domain,
Hasini Gunasinghea020b532021-01-07 21:42:35 +0000587 nspace: caller_uid as i64,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700588 alias: key.alias.clone(),
589 blob: None,
590 },
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800591 Domain::SELINUX => KeyDescriptor {
592 domain: Domain::SELINUX,
593 nspace: key.nspace,
594 alias: key.alias.clone(),
595 blob: None,
596 },
597 _ => panic!("Unreachable."),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700598 };
599
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800600 // Import_wrapped_key requires the rebind permission for the new key.
Janis Danisevskis1af91262020-08-10 14:58:08 -0700601 check_key_permission(KeyPerm::rebind(), &key, &None).context("In import_wrapped_key.")?;
602
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000603 let (wrapping_key_id_guard, mut wrapping_key_entry) = DB
Janis Danisevskis1af91262020-08-10 14:58:08 -0700604 .with(|db| {
Hasini Gunasinghe3ed5da72021-02-04 15:18:54 +0000605 LEGACY_MIGRATOR.with_try_migrate(&key, caller_uid, || {
606 db.borrow_mut().load_key_entry(
607 &wrapping_key,
608 KeyType::Client,
609 KeyEntryLoadBits::KM,
610 caller_uid,
611 |k, av| check_key_permission(KeyPerm::use_(), k, &av),
612 )
613 })
Janis Danisevskis1af91262020-08-10 14:58:08 -0700614 })
615 .context("Failed to load wrapping key.")?;
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000616
617 let (wrapping_key_blob, wrapping_blob_metadata) = wrapping_key_entry
618 .take_key_blob_info()
619 .ok_or_else(error::Error::sys)
620 .context("No km_blob after successfully loading key. This should never happen.")?;
621
622 let wrapping_key_blob =
623 SUPER_KEY.unwrap_key_if_required(&wrapping_blob_metadata, &wrapping_key_blob).context(
624 "In import_wrapped_key. Failed to handle super encryption for wrapping key.",
625 )?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700626
Janis Danisevskis1af91262020-08-10 14:58:08 -0700627 // km_dev.importWrappedKey does not return a certificate chain.
628 // TODO Do we assume that all wrapped keys are symmetric?
629 // let certificate_chain: Vec<KmCertificate> = Default::default();
630
631 let pw_sid = authenticators
632 .iter()
633 .find_map(|a| match a.authenticatorType {
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700634 HardwareAuthenticatorType::PASSWORD => Some(a.authenticatorId),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700635 _ => None,
636 })
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800637 .unwrap_or(-1);
Janis Danisevskis1af91262020-08-10 14:58:08 -0700638
639 let fp_sid = authenticators
640 .iter()
641 .find_map(|a| match a.authenticatorType {
Janis Danisevskisa53c9cf2020-10-26 11:52:33 -0700642 HardwareAuthenticatorType::FINGERPRINT => Some(a.authenticatorId),
Janis Danisevskis1af91262020-08-10 14:58:08 -0700643 _ => None,
644 })
Janis Danisevskis32adc7d2021-02-07 14:04:01 -0800645 .unwrap_or(-1);
Janis Danisevskis1af91262020-08-10 14:58:08 -0700646
647 let masking_key = masking_key.unwrap_or(ZERO_BLOB_32);
648
Stephen Crane221bbb52020-12-16 15:52:10 -0800649 let km_dev: Strong<dyn IKeyMintDevice> = self.keymint.get_interface()?;
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800650 let (creation_result, _) = self
651 .upgrade_keyblob_if_required_with(
652 &*km_dev,
653 Some(wrapping_key_id_guard),
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000654 &(&wrapping_key_blob, &wrapping_blob_metadata),
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800655 &[],
656 |wrapping_blob| {
657 let creation_result = map_km_error(km_dev.importWrappedKey(
658 wrapped_data,
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800659 wrapping_blob,
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800660 masking_key,
661 &params,
662 pw_sid,
663 fp_sid,
664 ))?;
665 Ok(creation_result)
666 },
667 )
668 .context("In import_wrapped_key.")?;
Janis Danisevskis1af91262020-08-10 14:58:08 -0700669
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000670 self.store_new_key(key, creation_result, user_id, None)
Janis Danisevskis104d8e42021-01-14 22:49:27 -0800671 .context("In import_wrapped_key: Trying to store the new key.")
Janis Danisevskis1af91262020-08-10 14:58:08 -0700672 }
Janis Danisevskisaec14592020-11-12 09:41:49 -0800673
674 fn upgrade_keyblob_if_required_with<T, F>(
675 &self,
676 km_dev: &dyn IKeyMintDevice,
677 key_id_guard: Option<KeyIdGuard>,
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000678 blob_info: &(&KeyBlob, &BlobMetaData),
Janis Danisevskisaec14592020-11-12 09:41:49 -0800679 params: &[KeyParameter],
680 f: F,
681 ) -> Result<(T, Option<Vec<u8>>)>
682 where
683 F: Fn(&[u8]) -> Result<T, Error>,
684 {
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800685 match f(blob_info.0) {
Janis Danisevskisaec14592020-11-12 09:41:49 -0800686 Err(Error::Km(ErrorCode::KEY_REQUIRES_UPGRADE)) => {
Janis Danisevskis7e8b4622021-02-13 10:01:59 -0800687 let upgraded_blob = map_km_error(km_dev.upgradeKey(blob_info.0, params))
Janis Danisevskisaec14592020-11-12 09:41:49 -0800688 .context("In upgrade_keyblob_if_required_with: Upgrade failed.")?;
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000689
690 let (upgraded_blob_to_be_stored, blob_metadata) =
691 SuperKeyManager::reencrypt_on_upgrade_if_required(blob_info.0, &upgraded_blob)
692 .context(
693 "In upgrade_keyblob_if_required_with: Failed to handle super encryption.",
694 )?;
695
696 let mut blob_metadata = blob_metadata.unwrap_or_else(BlobMetaData::new);
697 if let Some(uuid) = blob_info.1.km_uuid() {
698 blob_metadata.add(BlobMetaEntry::KmUuid(*uuid));
699 }
700
Janis Danisevskisaec14592020-11-12 09:41:49 -0800701 key_id_guard.map_or(Ok(()), |key_id_guard| {
702 DB.with(|db| {
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000703 let mut db = db.borrow_mut();
704 db.set_blob(
Janis Danisevskisaec14592020-11-12 09:41:49 -0800705 &key_id_guard,
Janis Danisevskisb42fc182020-12-15 08:41:27 -0800706 SubComponentType::KEY_BLOB,
Hasini Gunasinghedeab85d2021-02-01 21:10:02 +0000707 Some(&upgraded_blob_to_be_stored),
708 Some(&blob_metadata),
Janis Danisevskisaec14592020-11-12 09:41:49 -0800709 )
710 })
711 .context(concat!(
712 "In upgrade_keyblob_if_required_with: ",
713 "Failed to insert upgraded blob into the database.",
714 ))
715 })?;
716 match f(&upgraded_blob) {
717 Ok(v) => Ok((v, Some(upgraded_blob))),
718 Err(e) => Err(e).context(concat!(
719 "In upgrade_keyblob_if_required_with: ",
720 "Failed to perform operation on second try."
721 )),
722 }
723 }
724 Err(e) => {
725 Err(e).context("In upgrade_keyblob_if_required_with: Failed perform operation.")
726 }
727 Ok(v) => Ok((v, None)),
728 }
729 }
Satya Tangirala3361b612021-03-08 14:36:11 -0800730
731 fn convert_storage_key_to_ephemeral(&self, storage_key: &KeyDescriptor) -> Result<Vec<u8>> {
732 if storage_key.domain != Domain::BLOB {
733 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT)).context(concat!(
734 "In IKeystoreSecurityLevel convert_storage_key_to_ephemeral: ",
735 "Key must be of Domain::BLOB"
736 ));
737 }
738 let key_blob = storage_key
739 .blob
740 .as_ref()
741 .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
742 .context(
743 "In IKeystoreSecurityLevel convert_storage_key_to_ephemeral: No key blob specified",
744 )?;
745
746 // convert_storage_key_to_ephemeral requires the associated permission
747 check_key_permission(KeyPerm::convert_storage_key_to_ephemeral(), storage_key, &None)
748 .context("In convert_storage_key_to_ephemeral: Check permission")?;
749
750 let km_dev: Strong<dyn IKeyMintDevice> = self.keymint.get_interface().context(concat!(
751 "In IKeystoreSecurityLevel convert_storage_key_to_ephemeral: ",
752 "Getting keymint device interface"
753 ))?;
754 map_km_error(km_dev.convertStorageKeyToEphemeral(key_blob))
755 .context("In keymint device convertStorageKeyToEphemeral")
756 }
Satya Tangirala04bca0d2021-03-08 22:27:54 -0800757
758 fn delete_key(&self, key: &KeyDescriptor) -> Result<()> {
759 if key.domain != Domain::BLOB {
760 return Err(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
761 .context("In IKeystoreSecurityLevel delete_key: Key must be of Domain::BLOB");
762 }
763
764 let key_blob = key
765 .blob
766 .as_ref()
767 .ok_or(error::Error::Km(ErrorCode::INVALID_ARGUMENT))
768 .context("In IKeystoreSecurityLevel delete_key: No key blob specified")?;
769
770 check_key_permission(KeyPerm::delete(), key, &None)
771 .context("In IKeystoreSecurityLevel delete_key: Checking delete permissions")?;
772
773 let km_dev: Strong<dyn IKeyMintDevice> = self
774 .keymint
775 .get_interface()
776 .context("In IKeystoreSecurityLevel delete_key: Getting keymint device interface")?;
777 map_km_error(km_dev.deleteKey(&key_blob)).context("In keymint device deleteKey")
778 }
Janis Danisevskis1af91262020-08-10 14:58:08 -0700779}
780
781impl binder::Interface for KeystoreSecurityLevel {}
782
783impl IKeystoreSecurityLevel for KeystoreSecurityLevel {
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700784 fn createOperation(
Janis Danisevskis1af91262020-08-10 14:58:08 -0700785 &self,
786 key: &KeyDescriptor,
787 operation_parameters: &[KeyParameter],
788 forced: bool,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700789 ) -> binder::public_api::Result<CreateOperationResponse> {
790 map_or_log_err(self.create_operation(key, operation_parameters, forced), Ok)
Janis Danisevskis1af91262020-08-10 14:58:08 -0700791 }
792 fn generateKey(
793 &self,
794 key: &KeyDescriptor,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700795 attestation_key: Option<&KeyDescriptor>,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700796 params: &[KeyParameter],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700797 flags: i32,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700798 entropy: &[u8],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700799 ) -> binder::public_api::Result<KeyMetadata> {
800 map_or_log_err(self.generate_key(key, attestation_key, params, flags, entropy), Ok)
Janis Danisevskis1af91262020-08-10 14:58:08 -0700801 }
802 fn importKey(
803 &self,
804 key: &KeyDescriptor,
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700805 attestation_key: Option<&KeyDescriptor>,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700806 params: &[KeyParameter],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700807 flags: i32,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700808 key_data: &[u8],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700809 ) -> binder::public_api::Result<KeyMetadata> {
810 map_or_log_err(self.import_key(key, attestation_key, params, flags, key_data), Ok)
Janis Danisevskis1af91262020-08-10 14:58:08 -0700811 }
812 fn importWrappedKey(
813 &self,
814 key: &KeyDescriptor,
815 wrapping_key: &KeyDescriptor,
816 masking_key: Option<&[u8]>,
817 params: &[KeyParameter],
818 authenticators: &[AuthenticatorSpec],
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700819 ) -> binder::public_api::Result<KeyMetadata> {
Janis Danisevskis1af91262020-08-10 14:58:08 -0700820 map_or_log_err(
821 self.import_wrapped_key(key, wrapping_key, masking_key, params, authenticators),
Janis Danisevskis2c7f9622020-09-30 16:30:31 -0700822 Ok,
Janis Danisevskis1af91262020-08-10 14:58:08 -0700823 )
824 }
Satya Tangirala3361b612021-03-08 14:36:11 -0800825 fn convertStorageKeyToEphemeral(
826 &self,
827 storage_key: &KeyDescriptor,
828 ) -> binder::public_api::Result<Vec<u8>> {
829 map_or_log_err(self.convert_storage_key_to_ephemeral(storage_key), Ok)
830 }
Satya Tangirala04bca0d2021-03-08 22:27:54 -0800831 fn deleteKey(&self, key: &KeyDescriptor) -> binder::public_api::Result<()> {
832 map_or_log_err(self.delete_key(key), Ok)
833 }
Janis Danisevskis1af91262020-08-10 14:58:08 -0700834}