Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 1 | // Copyright 2020, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #![allow(dead_code)] |
| 16 | |
| 17 | use crate::{ |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 18 | database::BlobMetaData, database::BlobMetaEntry, database::EncryptedBy, database::KeyEntry, |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 19 | database::KeyType, database::KeystoreDB, enforcements::Enforcements, error::Error, |
| 20 | error::ResponseCode, key_parameter::KeyParameter, legacy_blob::LegacyBlobLoader, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 21 | legacy_migrator::LegacyMigrator, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 22 | }; |
| 23 | use android_system_keystore2::aidl::android::system::keystore2::Domain::Domain; |
| 24 | use anyhow::{Context, Result}; |
| 25 | use keystore2_crypto::{ |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame^] | 26 | aes_gcm_decrypt, aes_gcm_encrypt, generate_aes256_key, generate_salt, Password, ZVec, |
| 27 | AES_256_KEY_LENGTH, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 28 | }; |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 29 | use std::ops::Deref; |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 30 | use std::{ |
| 31 | collections::HashMap, |
| 32 | sync::Arc, |
| 33 | sync::{Mutex, Weak}, |
| 34 | }; |
| 35 | |
| 36 | type UserId = u32; |
| 37 | |
| 38 | #[derive(Default)] |
| 39 | struct UserSuperKeys { |
| 40 | /// The per boot key is used for LSKF binding of authentication bound keys. There is one |
| 41 | /// key per android user. The key is stored on flash encrypted with a key derived from a |
| 42 | /// secret, that is itself derived from the user's lock screen knowledge factor (LSKF). |
| 43 | /// When the user unlocks the device for the first time, this key is unlocked, i.e., decrypted, |
| 44 | /// and stays memory resident until the device reboots. |
Hasini Gunasinghe | 0e16145 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 45 | per_boot: Option<SuperKey>, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 46 | /// The screen lock key works like the per boot key with the distinction that it is cleared |
| 47 | /// from memory when the screen lock is engaged. |
| 48 | /// TODO the life cycle is not fully implemented at this time. |
| 49 | screen_lock: Option<Arc<ZVec>>, |
| 50 | } |
| 51 | |
Hasini Gunasinghe | 0e16145 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 52 | #[derive(Default, Clone)] |
| 53 | pub struct SuperKey { |
| 54 | key: Arc<ZVec>, |
| 55 | // id of the super key in the database. |
| 56 | id: i64, |
| 57 | } |
| 58 | |
| 59 | impl SuperKey { |
| 60 | pub fn get_key(&self) -> &Arc<ZVec> { |
| 61 | &self.key |
| 62 | } |
| 63 | |
| 64 | pub fn get_id(&self) -> i64 { |
| 65 | self.id |
| 66 | } |
| 67 | } |
| 68 | |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 69 | #[derive(Default)] |
| 70 | struct SkmState { |
| 71 | user_keys: HashMap<UserId, UserSuperKeys>, |
| 72 | key_index: HashMap<i64, Weak<ZVec>>, |
| 73 | } |
| 74 | |
| 75 | #[derive(Default)] |
| 76 | pub struct SuperKeyManager { |
| 77 | data: Mutex<SkmState>, |
| 78 | } |
| 79 | |
| 80 | impl SuperKeyManager { |
| 81 | pub fn new() -> Self { |
| 82 | Self { data: Mutex::new(Default::default()) } |
| 83 | } |
| 84 | |
| 85 | pub fn forget_screen_lock_key_for_user(&self, user: UserId) { |
| 86 | let mut data = self.data.lock().unwrap(); |
| 87 | if let Some(usk) = data.user_keys.get_mut(&user) { |
| 88 | usk.screen_lock = None; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | pub fn forget_screen_lock_keys(&self) { |
| 93 | let mut data = self.data.lock().unwrap(); |
| 94 | for (_, usk) in data.user_keys.iter_mut() { |
| 95 | usk.screen_lock = None; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | pub fn forget_all_keys_for_user(&self, user: UserId) { |
| 100 | let mut data = self.data.lock().unwrap(); |
| 101 | data.user_keys.remove(&user); |
| 102 | } |
| 103 | |
| 104 | pub fn forget_all_keys(&self) { |
| 105 | let mut data = self.data.lock().unwrap(); |
| 106 | data.user_keys.clear(); |
| 107 | data.key_index.clear(); |
| 108 | } |
| 109 | |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 110 | fn install_per_boot_key_for_user(&self, user: UserId, super_key: SuperKey) { |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 111 | let mut data = self.data.lock().unwrap(); |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 112 | data.key_index.insert(super_key.id, Arc::downgrade(&(super_key.key))); |
| 113 | data.user_keys.entry(user).or_default().per_boot = Some(super_key); |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | fn get_key(&self, key_id: &i64) -> Option<Arc<ZVec>> { |
| 117 | self.data.lock().unwrap().key_index.get(key_id).and_then(|k| k.upgrade()) |
| 118 | } |
| 119 | |
Hasini Gunasinghe | 0e16145 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 120 | pub fn get_per_boot_key_by_user_id(&self, user_id: u32) -> Option<SuperKey> { |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 121 | let data = self.data.lock().unwrap(); |
| 122 | data.user_keys.get(&user_id).map(|e| e.per_boot.clone()).flatten() |
| 123 | } |
| 124 | |
| 125 | /// This function unlocks the super keys for a given user. |
| 126 | /// This means the key is loaded from the database, decrypted and placed in the |
| 127 | /// super key cache. If there is no such key a new key is created, encrypted with |
| 128 | /// a key derived from the given password and stored in the database. |
Janis Danisevskis | a51ccbc | 2020-11-25 21:04:24 -0800 | [diff] [blame] | 129 | pub fn unlock_user_key( |
| 130 | &self, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 131 | db: &mut KeystoreDB, |
Janis Danisevskis | a51ccbc | 2020-11-25 21:04:24 -0800 | [diff] [blame] | 132 | user: UserId, |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame^] | 133 | pw: &Password, |
Janis Danisevskis | a51ccbc | 2020-11-25 21:04:24 -0800 | [diff] [blame] | 134 | legacy_blob_loader: &LegacyBlobLoader, |
| 135 | ) -> Result<()> { |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 136 | let (_, entry) = db |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 137 | .get_or_create_key_with( |
| 138 | Domain::APP, |
| 139 | user as u64 as i64, |
Hasini Gunasinghe | 0e16145 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 140 | KeystoreDB::USER_SUPER_KEY_ALIAS, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 141 | crate::database::KEYSTORE_UUID, |
| 142 | || { |
| 143 | // For backward compatibility we need to check if there is a super key present. |
| 144 | let super_key = legacy_blob_loader |
| 145 | .load_super_key(user, pw) |
| 146 | .context("In create_new_key: Failed to load legacy key blob.")?; |
| 147 | let super_key = match super_key { |
| 148 | None => { |
| 149 | // No legacy file was found. So we generate a new key. |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 150 | generate_aes256_key() |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 151 | .context("In create_new_key: Failed to generate AES 256 key.")? |
| 152 | } |
| 153 | Some(key) => key, |
| 154 | }; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 155 | // Regardless of whether we loaded an old AES128 key or generated a new AES256 |
| 156 | // key as the super key, we derive a AES256 key from the password and re-encrypt |
| 157 | // the super key before we insert it in the database. The length of the key is |
| 158 | // preserved by the encryption so we don't need any extra flags to inform us |
| 159 | // which algorithm to use it with. |
| 160 | Self::encrypt_with_password(&super_key, pw).context("In create_new_key.") |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 161 | }, |
| 162 | ) |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 163 | .context("In unlock_user_key: Failed to get key id.")?; |
| 164 | |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 165 | self.populate_cache_from_super_key_blob(user, entry, pw).context("In unlock_user_key.")?; |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 166 | Ok(()) |
| 167 | } |
| 168 | |
| 169 | /// Unwraps an encrypted key blob given metadata identifying the encryption key. |
| 170 | /// The function queries `metadata.encrypted_by()` to determine the encryption key. |
| 171 | /// It then check if the required key is memory resident, and if so decrypts the |
| 172 | /// blob. |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 173 | pub fn unwrap_key<'a>(&self, blob: &'a [u8], metadata: &BlobMetaData) -> Result<KeyBlob<'a>> { |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 174 | match metadata.encrypted_by() { |
| 175 | Some(EncryptedBy::KeyId(key_id)) => match self.get_key(key_id) { |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 176 | Some(key) => Ok(KeyBlob::Sensitive( |
| 177 | Self::unwrap_key_with_key(blob, metadata, &key).context("In unwrap_key.")?, |
| 178 | SuperKey { key: key.clone(), id: *key_id }, |
| 179 | )), |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 180 | None => Err(Error::Rc(ResponseCode::LOCKED)) |
| 181 | .context("In unwrap_key: Key is not usable until the user entered their LSKF."), |
| 182 | }, |
| 183 | _ => Err(Error::Rc(ResponseCode::VALUE_CORRUPTED)) |
| 184 | .context("In unwrap_key: Cannot determined wrapping key."), |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /// Unwraps an encrypted key blob given an encryption key. |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 189 | fn unwrap_key_with_key(blob: &[u8], metadata: &BlobMetaData, key: &[u8]) -> Result<ZVec> { |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 190 | match (metadata.iv(), metadata.aead_tag()) { |
| 191 | (Some(iv), Some(tag)) => aes_gcm_decrypt(blob, iv, tag, key) |
| 192 | .context("In unwrap_key_with_key: Failed to decrypt the key blob."), |
| 193 | (iv, tag) => Err(Error::Rc(ResponseCode::VALUE_CORRUPTED)).context(format!( |
| 194 | concat!( |
| 195 | "In unwrap_key_with_key: Key has incomplete metadata.", |
| 196 | "Present: iv: {}, aead_tag: {}." |
| 197 | ), |
| 198 | iv.is_some(), |
| 199 | tag.is_some(), |
| 200 | )), |
| 201 | } |
| 202 | } |
Hasini Gunasinghe | 0e16145 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 203 | |
| 204 | /// Checks if user has setup LSKF, even when super key cache is empty for the user. |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 205 | pub fn super_key_exists_in_db_for_user( |
| 206 | db: &mut KeystoreDB, |
| 207 | legacy_migrator: &LegacyMigrator, |
| 208 | user_id: u32, |
| 209 | ) -> Result<bool> { |
Hasini Gunasinghe | 0e16145 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 210 | let key_in_db = db |
| 211 | .key_exists( |
| 212 | Domain::APP, |
| 213 | user_id as u64 as i64, |
| 214 | KeystoreDB::USER_SUPER_KEY_ALIAS, |
| 215 | KeyType::Super, |
| 216 | ) |
| 217 | .context("In super_key_exists_in_db_for_user.")?; |
| 218 | |
| 219 | if key_in_db { |
| 220 | Ok(key_in_db) |
| 221 | } else { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 222 | legacy_migrator |
| 223 | .has_super_key(user_id) |
| 224 | .context("In super_key_exists_in_db_for_user: Trying to query legacy db.") |
Hasini Gunasinghe | 0e16145 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 227 | |
| 228 | /// Checks if user has already setup LSKF (i.e. a super key is persisted in the database or the |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 229 | /// legacy database). If not, return Uninitialized state. |
| 230 | /// Otherwise, decrypt the super key from the password and return LskfUnlocked state. |
| 231 | pub fn check_and_unlock_super_key( |
| 232 | &self, |
| 233 | db: &mut KeystoreDB, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 234 | legacy_migrator: &LegacyMigrator, |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 235 | user_id: u32, |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame^] | 236 | pw: &Password, |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 237 | ) -> Result<UserState> { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 238 | let result = legacy_migrator |
| 239 | .with_try_migrate_super_key(user_id, pw, || db.load_super_key(user_id)) |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 240 | .context("In check_and_unlock_super_key. Failed to load super key")?; |
| 241 | |
| 242 | match result { |
| 243 | Some((_, entry)) => { |
| 244 | let super_key = self |
| 245 | .populate_cache_from_super_key_blob(user_id, entry, pw) |
| 246 | .context("In check_and_unlock_super_key.")?; |
| 247 | Ok(UserState::LskfUnlocked(super_key)) |
| 248 | } |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 249 | None => Ok(UserState::Uninitialized), |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | /// Checks if user has already setup LSKF (i.e. a super key is persisted in the database or the |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 254 | /// legacy database). If so, return LskfLocked state. |
| 255 | /// If the password is provided, generate a new super key, encrypt with the password, |
| 256 | /// store in the database and populate the super key cache for the new user |
| 257 | /// and return LskfUnlocked state. |
| 258 | /// If the password is not provided, return Uninitialized state. |
| 259 | pub fn check_and_initialize_super_key( |
| 260 | &self, |
| 261 | db: &mut KeystoreDB, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 262 | legacy_migrator: &LegacyMigrator, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 263 | user_id: u32, |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame^] | 264 | pw: Option<&Password>, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 265 | ) -> Result<UserState> { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 266 | let super_key_exists_in_db = |
| 267 | Self::super_key_exists_in_db_for_user(db, legacy_migrator, user_id).context( |
| 268 | "In check_and_initialize_super_key. Failed to check if super key exists.", |
| 269 | )?; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 270 | if super_key_exists_in_db { |
| 271 | Ok(UserState::LskfLocked) |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 272 | } else if let Some(pw) = pw { |
| 273 | //generate a new super key. |
| 274 | let super_key = generate_aes256_key() |
| 275 | .context("In check_and_initialize_super_key: Failed to generate AES 256 key.")?; |
| 276 | //derive an AES256 key from the password and re-encrypt the super key |
| 277 | //before we insert it in the database. |
| 278 | let (encrypted_super_key, blob_metadata) = Self::encrypt_with_password(&super_key, pw) |
| 279 | .context("In check_and_initialize_super_key.")?; |
| 280 | |
| 281 | let key_entry = db |
| 282 | .store_super_key(user_id, &(&encrypted_super_key, &blob_metadata)) |
| 283 | .context("In check_and_initialize_super_key. Failed to store super key.")?; |
| 284 | |
| 285 | let super_key = self |
| 286 | .populate_cache_from_super_key_blob(user_id, key_entry, pw) |
| 287 | .context("In check_and_initialize_super_key.")?; |
| 288 | Ok(UserState::LskfUnlocked(super_key)) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 289 | } else { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 290 | Ok(UserState::Uninitialized) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | //helper function to populate super key cache from the super key blob loaded from the database |
| 295 | fn populate_cache_from_super_key_blob( |
| 296 | &self, |
| 297 | user_id: u32, |
| 298 | entry: KeyEntry, |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame^] | 299 | pw: &Password, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 300 | ) -> Result<SuperKey> { |
| 301 | let super_key = Self::extract_super_key_from_key_entry(entry, pw).context( |
| 302 | "In populate_cache_from_super_key_blob. Failed to extract super key from key entry", |
| 303 | )?; |
| 304 | self.install_per_boot_key_for_user(user_id, super_key.clone()); |
| 305 | Ok(super_key) |
| 306 | } |
| 307 | |
| 308 | /// Extracts super key from the entry loaded from the database |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame^] | 309 | pub fn extract_super_key_from_key_entry(entry: KeyEntry, pw: &Password) -> Result<SuperKey> { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 310 | if let Some((blob, metadata)) = entry.key_blob_info() { |
| 311 | let key = match ( |
| 312 | metadata.encrypted_by(), |
| 313 | metadata.salt(), |
| 314 | metadata.iv(), |
| 315 | metadata.aead_tag(), |
| 316 | ) { |
| 317 | (Some(&EncryptedBy::Password), Some(salt), Some(iv), Some(tag)) => { |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame^] | 318 | let key = pw.derive_key(Some(salt), AES_256_KEY_LENGTH).context( |
| 319 | "In extract_super_key_from_key_entry: Failed to generate key from password.", |
| 320 | )?; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 321 | |
| 322 | aes_gcm_decrypt(blob, iv, tag, &key).context( |
| 323 | "In extract_super_key_from_key_entry: Failed to decrypt key blob.", |
| 324 | )? |
| 325 | } |
| 326 | (enc_by, salt, iv, tag) => { |
| 327 | return Err(Error::Rc(ResponseCode::VALUE_CORRUPTED)).context(format!( |
| 328 | concat!( |
| 329 | "In extract_super_key_from_key_entry: Super key has incomplete metadata.", |
| 330 | "Present: encrypted_by: {}, salt: {}, iv: {}, aead_tag: {}." |
| 331 | ), |
| 332 | enc_by.is_some(), |
| 333 | salt.is_some(), |
| 334 | iv.is_some(), |
| 335 | tag.is_some() |
| 336 | )); |
| 337 | } |
| 338 | }; |
| 339 | Ok(SuperKey { key: Arc::new(key), id: entry.id() }) |
| 340 | } else { |
| 341 | Err(Error::Rc(ResponseCode::VALUE_CORRUPTED)) |
| 342 | .context("In extract_super_key_from_key_entry: No key blob info.") |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /// Encrypts the super key from a key derived from the password, before storing in the database. |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame^] | 347 | pub fn encrypt_with_password( |
| 348 | super_key: &[u8], |
| 349 | pw: &Password, |
| 350 | ) -> Result<(Vec<u8>, BlobMetaData)> { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 351 | let salt = generate_salt().context("In encrypt_with_password: Failed to generate salt.")?; |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame^] | 352 | let derived_key = pw |
| 353 | .derive_key(Some(&salt), AES_256_KEY_LENGTH) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 354 | .context("In encrypt_with_password: Failed to derive password.")?; |
| 355 | let mut metadata = BlobMetaData::new(); |
| 356 | metadata.add(BlobMetaEntry::EncryptedBy(EncryptedBy::Password)); |
| 357 | metadata.add(BlobMetaEntry::Salt(salt)); |
| 358 | let (encrypted_key, iv, tag) = aes_gcm_encrypt(super_key, &derived_key) |
| 359 | .context("In encrypt_with_password: Failed to encrypt new super key.")?; |
| 360 | metadata.add(BlobMetaEntry::Iv(iv)); |
| 361 | metadata.add(BlobMetaEntry::AeadTag(tag)); |
| 362 | Ok((encrypted_key, metadata)) |
| 363 | } |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 364 | |
| 365 | // Encrypt the given key blob with the user's super key, if the super key exists and the device |
| 366 | // is unlocked. If the super key exists and the device is locked, or LSKF is not setup, |
| 367 | // return error. Note that it is out of the scope of this function to check if super encryption |
| 368 | // is required. Such check should be performed before calling this function. |
| 369 | fn super_encrypt_on_key_init( |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 370 | &self, |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 371 | db: &mut KeystoreDB, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 372 | legacy_migrator: &LegacyMigrator, |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 373 | user_id: u32, |
| 374 | key_blob: &[u8], |
| 375 | ) -> Result<(Vec<u8>, BlobMetaData)> { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 376 | match UserState::get(db, legacy_migrator, self, user_id) |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 377 | .context("In super_encrypt. Failed to get user state.")? |
| 378 | { |
| 379 | UserState::LskfUnlocked(super_key) => { |
| 380 | Self::encrypt_with_super_key(key_blob, &super_key) |
| 381 | .context("In super_encrypt_on_key_init. Failed to encrypt the key.") |
| 382 | } |
| 383 | UserState::LskfLocked => { |
| 384 | Err(Error::Rc(ResponseCode::LOCKED)).context("In super_encrypt. Device is locked.") |
| 385 | } |
| 386 | UserState::Uninitialized => Err(Error::Rc(ResponseCode::UNINITIALIZED)) |
| 387 | .context("In super_encrypt. LSKF is not setup for the user."), |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | //Helper function to encrypt a key with the given super key. Callers should select which super |
| 392 | //key to be used. This is called when a key is super encrypted at its creation as well as at its |
| 393 | //upgrade. |
| 394 | fn encrypt_with_super_key( |
| 395 | key_blob: &[u8], |
| 396 | super_key: &SuperKey, |
| 397 | ) -> Result<(Vec<u8>, BlobMetaData)> { |
| 398 | let mut metadata = BlobMetaData::new(); |
| 399 | let (encrypted_key, iv, tag) = aes_gcm_encrypt(key_blob, &(super_key.key)) |
| 400 | .context("In encrypt_with_super_key: Failed to encrypt new super key.")?; |
| 401 | metadata.add(BlobMetaEntry::Iv(iv)); |
| 402 | metadata.add(BlobMetaEntry::AeadTag(tag)); |
| 403 | metadata.add(BlobMetaEntry::EncryptedBy(EncryptedBy::KeyId(super_key.id))); |
| 404 | Ok((encrypted_key, metadata)) |
| 405 | } |
| 406 | |
| 407 | /// Check if super encryption is required and if so, super-encrypt the key to be stored in |
| 408 | /// the database. |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 409 | #[allow(clippy::clippy::too_many_arguments)] |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 410 | pub fn handle_super_encryption_on_key_init( |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 411 | &self, |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 412 | db: &mut KeystoreDB, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 413 | legacy_migrator: &LegacyMigrator, |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 414 | domain: &Domain, |
| 415 | key_parameters: &[KeyParameter], |
| 416 | flags: Option<i32>, |
| 417 | user_id: u32, |
| 418 | key_blob: &[u8], |
| 419 | ) -> Result<(Vec<u8>, BlobMetaData)> { |
| 420 | match (*domain, Enforcements::super_encryption_required(key_parameters, flags)) { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 421 | (Domain::APP, true) => { |
| 422 | self.super_encrypt_on_key_init(db, legacy_migrator, user_id, &key_blob).context( |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 423 | "In handle_super_encryption_on_key_init. |
| 424 | Failed to super encrypt the key.", |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 425 | ) |
| 426 | } |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 427 | _ => Ok((key_blob.to_vec(), BlobMetaData::new())), |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /// Check if a given key is super-encrypted, from its metadata. If so, unwrap the key using |
| 432 | /// the relevant super key. |
| 433 | pub fn unwrap_key_if_required<'a>( |
| 434 | &self, |
| 435 | metadata: &BlobMetaData, |
| 436 | key_blob: &'a [u8], |
| 437 | ) -> Result<KeyBlob<'a>> { |
| 438 | if Self::key_super_encrypted(&metadata) { |
| 439 | let unwrapped_key = self |
| 440 | .unwrap_key(key_blob, metadata) |
| 441 | .context("In unwrap_key_if_required. Error in unwrapping the key.")?; |
| 442 | Ok(unwrapped_key) |
| 443 | } else { |
| 444 | Ok(KeyBlob::Ref(key_blob)) |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | /// Check if a given key needs re-super-encryption, from its KeyBlob type. |
| 449 | /// If so, re-super-encrypt the key and return a new set of metadata, |
| 450 | /// containing the new super encryption information. |
| 451 | pub fn reencrypt_on_upgrade_if_required<'a>( |
| 452 | key_blob_before_upgrade: &KeyBlob, |
| 453 | key_after_upgrade: &'a [u8], |
| 454 | ) -> Result<(KeyBlob<'a>, Option<BlobMetaData>)> { |
| 455 | match key_blob_before_upgrade { |
| 456 | KeyBlob::Sensitive(_, super_key) => { |
| 457 | let (key, metadata) = Self::encrypt_with_super_key(key_after_upgrade, super_key) |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 458 | .context(concat!( |
| 459 | "In reencrypt_on_upgrade_if_required. ", |
| 460 | "Failed to re-super-encrypt key on key upgrade." |
| 461 | ))?; |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 462 | Ok((KeyBlob::NonSensitive(key), Some(metadata))) |
| 463 | } |
| 464 | _ => Ok((KeyBlob::Ref(key_after_upgrade), None)), |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | // Helper function to decide if a key is super encrypted, given metadata. |
| 469 | fn key_super_encrypted(metadata: &BlobMetaData) -> bool { |
| 470 | if let Some(&EncryptedBy::KeyId(_)) = metadata.encrypted_by() { |
| 471 | return true; |
| 472 | } |
| 473 | false |
| 474 | } |
Hasini Gunasinghe | 0e16145 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /// This enum represents different states of the user's life cycle in the device. |
| 478 | /// For now, only three states are defined. More states may be added later. |
| 479 | pub enum UserState { |
| 480 | // The user has registered LSKF and has unlocked the device by entering PIN/Password, |
| 481 | // and hence the per-boot super key is available in the cache. |
| 482 | LskfUnlocked(SuperKey), |
| 483 | // The user has registered LSKF, but has not unlocked the device using password, after reboot. |
| 484 | // Hence the per-boot super-key(s) is not available in the cache. |
| 485 | // However, the encrypted super key is available in the database. |
| 486 | LskfLocked, |
| 487 | // There's no user in the device for the given user id, or the user with the user id has not |
| 488 | // setup LSKF. |
| 489 | Uninitialized, |
| 490 | } |
| 491 | |
| 492 | impl UserState { |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 493 | pub fn get( |
| 494 | db: &mut KeystoreDB, |
| 495 | legacy_migrator: &LegacyMigrator, |
| 496 | skm: &SuperKeyManager, |
| 497 | user_id: u32, |
| 498 | ) -> Result<UserState> { |
Hasini Gunasinghe | 0e16145 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 499 | match skm.get_per_boot_key_by_user_id(user_id) { |
| 500 | Some(super_key) => Ok(UserState::LskfUnlocked(super_key)), |
| 501 | None => { |
| 502 | //Check if a super key exists in the database or legacy database. |
| 503 | //If so, return locked user state. |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 504 | if SuperKeyManager::super_key_exists_in_db_for_user(db, legacy_migrator, user_id) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 505 | .context("In get.")? |
Hasini Gunasinghe | 0e16145 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 506 | { |
| 507 | Ok(UserState::LskfLocked) |
| 508 | } else { |
| 509 | Ok(UserState::Uninitialized) |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 514 | |
| 515 | /// Queries user state when serving password change requests. |
| 516 | pub fn get_with_password_changed( |
| 517 | db: &mut KeystoreDB, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 518 | legacy_migrator: &LegacyMigrator, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 519 | skm: &SuperKeyManager, |
| 520 | user_id: u32, |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame^] | 521 | password: Option<&Password>, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 522 | ) -> Result<UserState> { |
| 523 | match skm.get_per_boot_key_by_user_id(user_id) { |
| 524 | Some(super_key) => { |
| 525 | if password.is_none() { |
| 526 | //transitioning to swiping, delete only the super key in database and cache, and |
| 527 | //super-encrypted keys in database (and in KM) |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 528 | Self::reset_user(db, skm, legacy_migrator, user_id, true).context( |
| 529 | "In get_with_password_changed: Trying to delete keys from the db.", |
| 530 | )?; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 531 | //Lskf is now removed in Keystore |
| 532 | Ok(UserState::Uninitialized) |
| 533 | } else { |
| 534 | //Keystore won't be notified when changing to a new password when LSKF is |
| 535 | //already setup. Therefore, ideally this path wouldn't be reached. |
| 536 | Ok(UserState::LskfUnlocked(super_key)) |
| 537 | } |
| 538 | } |
| 539 | None => { |
| 540 | //Check if a super key exists in the database or legacy database. |
| 541 | //If so, return LskfLocked state. |
| 542 | //Otherwise, i) if the password is provided, initialize the super key and return |
| 543 | //LskfUnlocked state ii) if password is not provided, return Uninitialized state. |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 544 | skm.check_and_initialize_super_key(db, legacy_migrator, user_id, password) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 549 | /// Queries user state when serving password unlock requests. |
| 550 | pub fn get_with_password_unlock( |
| 551 | db: &mut KeystoreDB, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 552 | legacy_migrator: &LegacyMigrator, |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 553 | skm: &SuperKeyManager, |
| 554 | user_id: u32, |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame^] | 555 | password: &Password, |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 556 | ) -> Result<UserState> { |
| 557 | match skm.get_per_boot_key_by_user_id(user_id) { |
| 558 | Some(super_key) => { |
| 559 | log::info!("In get_with_password_unlock. Trying to unlock when already unlocked."); |
| 560 | Ok(UserState::LskfUnlocked(super_key)) |
| 561 | } |
| 562 | None => { |
| 563 | //Check if a super key exists in the database or legacy database. |
| 564 | //If not, return Uninitialized state. |
| 565 | //Otherwise, try to unlock the super key and if successful, |
| 566 | //return LskfUnlocked state |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 567 | skm.check_and_unlock_super_key(db, legacy_migrator, user_id, password) |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 568 | .context("In get_with_password_unlock. Failed to unlock super key.") |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 573 | /// Delete all the keys created on behalf of the user. |
| 574 | /// If 'keep_non_super_encrypted_keys' is set to true, delete only the super key and super |
| 575 | /// encrypted keys. |
| 576 | pub fn reset_user( |
| 577 | db: &mut KeystoreDB, |
| 578 | skm: &SuperKeyManager, |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 579 | legacy_migrator: &LegacyMigrator, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 580 | user_id: u32, |
| 581 | keep_non_super_encrypted_keys: bool, |
| 582 | ) -> Result<()> { |
| 583 | // mark keys created on behalf of the user as unreferenced. |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 584 | legacy_migrator |
| 585 | .bulk_delete_user(user_id, keep_non_super_encrypted_keys) |
| 586 | .context("In reset_user: Trying to delete legacy keys.")?; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 587 | db.unbind_keys_for_user(user_id as u32, keep_non_super_encrypted_keys) |
| 588 | .context("In reset user. Error in unbinding keys.")?; |
| 589 | |
| 590 | //delete super key in cache, if exists |
| 591 | skm.forget_all_keys_for_user(user_id as u32); |
| 592 | Ok(()) |
| 593 | } |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 594 | } |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 595 | |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 596 | /// This enum represents three states a KeyMint Blob can be in, w.r.t super encryption. |
| 597 | /// `Sensitive` holds the non encrypted key and a reference to its super key. |
| 598 | /// `NonSensitive` holds a non encrypted key that is never supposed to be encrypted. |
| 599 | /// `Ref` holds a reference to a key blob when it does not need to be modified if its |
| 600 | /// life time allows it. |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 601 | pub enum KeyBlob<'a> { |
| 602 | Sensitive(ZVec, SuperKey), |
| 603 | NonSensitive(Vec<u8>), |
| 604 | Ref(&'a [u8]), |
| 605 | } |
| 606 | |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 607 | /// Deref returns a reference to the key material in any variant. |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 608 | impl<'a> Deref for KeyBlob<'a> { |
| 609 | type Target = [u8]; |
| 610 | |
| 611 | fn deref(&self) -> &Self::Target { |
| 612 | match self { |
| 613 | Self::Sensitive(key, _) => &key, |
| 614 | Self::NonSensitive(key) => &key, |
| 615 | Self::Ref(key) => key, |
| 616 | } |
| 617 | } |
| 618 | } |