Joel Galenson | 26f4d01 | 2020-07-17 14:57:21 -0700 | [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 | |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 15 | //! This is the Keystore 2.0 database module. |
| 16 | //! The database module provides a connection to the backing SQLite store. |
| 17 | //! We have two databases one for persistent key blob storage and one for |
| 18 | //! items that have a per boot life cycle. |
| 19 | //! |
| 20 | //! ## Persistent database |
| 21 | //! The persistent database has tables for key blobs. They are organized |
| 22 | //! as follows: |
| 23 | //! The `keyentry` table is the primary table for key entries. It is |
| 24 | //! accompanied by two tables for blobs and parameters. |
| 25 | //! Each key entry occupies exactly one row in the `keyentry` table and |
| 26 | //! zero or more rows in the tables `blobentry` and `keyparameter`. |
| 27 | //! |
| 28 | //! ## Per boot database |
| 29 | //! The per boot database stores items with a per boot lifecycle. |
| 30 | //! Currently, there is only the `grant` table in this database. |
| 31 | //! Grants are references to a key that can be used to access a key by |
| 32 | //! clients that don't own that key. Grants can only be created by the |
| 33 | //! owner of a key. And only certain components can create grants. |
| 34 | //! This is governed by SEPolicy. |
| 35 | //! |
| 36 | //! ## Access control |
| 37 | //! Some database functions that load keys or create grants perform |
| 38 | //! access control. This is because in some cases access control |
| 39 | //! can only be performed after some information about the designated |
| 40 | //! key was loaded from the database. To decouple the permission checks |
| 41 | //! from the database module these functions take permission check |
| 42 | //! callbacks. |
Joel Galenson | 26f4d01 | 2020-07-17 14:57:21 -0700 | [diff] [blame] | 43 | |
Matthew Maurer | d7815ca | 2021-05-06 21:58:45 -0700 | [diff] [blame] | 44 | mod perboot; |
Janis Danisevskis | 030ba02 | 2021-05-26 11:15:30 -0700 | [diff] [blame] | 45 | pub(crate) mod utils; |
Janis Danisevskis | cfaf919 | 2021-05-26 16:31:02 -0700 | [diff] [blame] | 46 | mod versioning; |
Matthew Maurer | d7815ca | 2021-05-06 21:58:45 -0700 | [diff] [blame] | 47 | |
David Drysdale | 2566fb3 | 2024-07-09 14:46:37 +0100 | [diff] [blame] | 48 | #[cfg(test)] |
| 49 | pub mod tests; |
| 50 | |
Janis Danisevskis | 11bd259 | 2022-01-04 19:59:26 -0800 | [diff] [blame] | 51 | use crate::gc::Gc; |
David Drysdale | f1ba381 | 2024-05-08 17:50:13 +0100 | [diff] [blame] | 52 | use crate::impl_metadata; // This is in database/utils.rs |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 53 | use crate::key_parameter::{KeyParameter, KeyParameterValue, Tag}; |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 54 | use crate::ks_err; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 55 | use crate::permission::KeyPermSet; |
Hasini Gunasinghe | 66a2460 | 2021-05-12 19:03:12 +0000 | [diff] [blame] | 56 | use crate::utils::{get_current_time_in_milliseconds, watchdog as wd, AID_USER_OFFSET}; |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 57 | use crate::{ |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 58 | error::{Error as KsError, ErrorCode, ResponseCode}, |
| 59 | super_key::SuperKeyType, |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 60 | }; |
Hasini Gunasinghe | 52333ba | 2020-11-06 01:24:16 +0000 | [diff] [blame] | 61 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
Tri Vo | a1634bb | 2022-12-01 15:54:19 -0800 | [diff] [blame] | 62 | HardwareAuthToken::HardwareAuthToken, HardwareAuthenticatorType::HardwareAuthenticatorType, |
| 63 | SecurityLevel::SecurityLevel, |
| 64 | }; |
| 65 | use android_security_metrics::aidl::android::security::metrics::{ |
Tri Vo | 0346bbe | 2023-05-12 14:16:31 -0400 | [diff] [blame] | 66 | Storage::Storage as MetricsStorage, StorageStats::StorageStats, |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 67 | }; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 68 | use android_system_keystore2::aidl::android::system::keystore2::{ |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 69 | Domain::Domain, KeyDescriptor::KeyDescriptor, |
Janis Danisevskis | 60400fe | 2020-08-26 15:24:42 -0700 | [diff] [blame] | 70 | }; |
Shaquille Johnson | 7f5a815 | 2023-09-27 18:46:27 +0100 | [diff] [blame] | 71 | use anyhow::{anyhow, Context, Result}; |
| 72 | use keystore2_flags; |
Andrew Walbran | a4bc1a9 | 2024-09-03 13:08:17 +0100 | [diff] [blame] | 73 | use std::{convert::TryFrom, convert::TryInto, ops::Deref, sync::LazyLock, time::SystemTimeError}; |
Shaquille Johnson | 7f5a815 | 2023-09-27 18:46:27 +0100 | [diff] [blame] | 74 | use utils as db_utils; |
| 75 | use utils::SqlField; |
Max Bires | 2b2e656 | 2020-09-22 11:22:36 -0700 | [diff] [blame] | 76 | |
| 77 | use keystore2_crypto::ZVec; |
Hasini Gunasinghe | f70cf8e | 2020-11-11 01:02:41 +0000 | [diff] [blame] | 78 | use log::error; |
Joel Galenson | 0891bc1 | 2020-07-20 10:37:03 -0700 | [diff] [blame] | 79 | #[cfg(not(test))] |
| 80 | use rand::prelude::random; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 81 | use rusqlite::{ |
Joel Galenson | ff79e36 | 2021-05-25 16:30:17 -0700 | [diff] [blame] | 82 | params, params_from_iter, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 83 | types::FromSql, |
| 84 | types::FromSqlResult, |
| 85 | types::ToSqlOutput, |
| 86 | types::{FromSqlError, Value, ValueRef}, |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 87 | Connection, OptionalExtension, ToSql, Transaction, |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 88 | }; |
Max Bires | 2b2e656 | 2020-09-22 11:22:36 -0700 | [diff] [blame] | 89 | |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 90 | use std::{ |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 91 | collections::{HashMap, HashSet}, |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 92 | path::Path, |
Janis Danisevskis | 3395f86 | 2021-05-06 10:54:17 -0700 | [diff] [blame] | 93 | sync::{Arc, Condvar, Mutex}, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 94 | time::{Duration, SystemTime}, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 95 | }; |
Max Bires | 2b2e656 | 2020-09-22 11:22:36 -0700 | [diff] [blame] | 96 | |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 97 | use TransactionBehavior::Immediate; |
| 98 | |
Joel Galenson | 0891bc1 | 2020-07-20 10:37:03 -0700 | [diff] [blame] | 99 | #[cfg(test)] |
| 100 | use tests::random; |
Joel Galenson | 26f4d01 | 2020-07-17 14:57:21 -0700 | [diff] [blame] | 101 | |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 102 | /// Wrapper for `rusqlite::TransactionBehavior` which includes information about the transaction |
| 103 | /// being performed. |
| 104 | #[derive(Clone, Copy)] |
| 105 | enum TransactionBehavior { |
| 106 | Deferred, |
| 107 | Immediate(&'static str), |
| 108 | } |
| 109 | |
| 110 | impl From<TransactionBehavior> for rusqlite::TransactionBehavior { |
| 111 | fn from(val: TransactionBehavior) -> Self { |
| 112 | match val { |
| 113 | TransactionBehavior::Deferred => rusqlite::TransactionBehavior::Deferred, |
| 114 | TransactionBehavior::Immediate(_) => rusqlite::TransactionBehavior::Immediate, |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | impl TransactionBehavior { |
| 120 | fn name(&self) -> Option<&'static str> { |
| 121 | match self { |
| 122 | TransactionBehavior::Deferred => None, |
| 123 | TransactionBehavior::Immediate(v) => Some(v), |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 128 | /// Access information for a key. |
| 129 | #[derive(Debug)] |
| 130 | struct KeyAccessInfo { |
| 131 | key_id: i64, |
| 132 | descriptor: KeyDescriptor, |
| 133 | vector: Option<KeyPermSet>, |
| 134 | } |
| 135 | |
David Drysdale | 115c472 | 2024-04-15 14:11:52 +0100 | [diff] [blame] | 136 | /// If the database returns a busy error code, retry after this interval. |
| 137 | const DB_BUSY_RETRY_INTERVAL: Duration = Duration::from_micros(500); |
David Drysdale | 115c472 | 2024-04-15 14:11:52 +0100 | [diff] [blame] | 138 | |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 139 | impl_metadata!( |
| 140 | /// A set of metadata for key entries. |
| 141 | #[derive(Debug, Default, Eq, PartialEq)] |
| 142 | pub struct KeyMetaData; |
| 143 | /// A metadata entry for key entries. |
| 144 | #[derive(Debug, Eq, PartialEq, Ord, PartialOrd)] |
| 145 | pub enum KeyMetaEntry { |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 146 | /// Date of the creation of the key entry. |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 147 | CreationDate(DateTime) with accessor creation_date, |
| 148 | /// Expiration date for attestation keys. |
| 149 | AttestationExpirationDate(DateTime) with accessor attestation_expiration_date, |
Max Bires | 2b2e656 | 2020-09-22 11:22:36 -0700 | [diff] [blame] | 150 | /// CBOR Blob that represents a COSE_Key and associated metadata needed for remote |
| 151 | /// provisioning |
| 152 | AttestationMacedPublicKey(Vec<u8>) with accessor attestation_maced_public_key, |
| 153 | /// Vector representing the raw public key so results from the server can be matched |
| 154 | /// to the right entry |
| 155 | AttestationRawPubKey(Vec<u8>) with accessor attestation_raw_pub_key, |
Paul Crowley | 8d5b253 | 2021-03-19 10:53:07 -0700 | [diff] [blame] | 156 | /// SEC1 public key for ECDH encryption |
| 157 | Sec1PublicKey(Vec<u8>) with accessor sec1_public_key, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 158 | // --- ADD NEW META DATA FIELDS HERE --- |
| 159 | // For backwards compatibility add new entries only to |
| 160 | // end of this list and above this comment. |
| 161 | }; |
| 162 | ); |
| 163 | |
| 164 | impl KeyMetaData { |
| 165 | fn load_from_db(key_id: i64, tx: &Transaction) -> Result<Self> { |
| 166 | let mut stmt = tx |
| 167 | .prepare( |
| 168 | "SELECT tag, data from persistent.keymetadata |
| 169 | WHERE keyentryid = ?;", |
| 170 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 171 | .context(ks_err!("KeyMetaData::load_from_db: prepare statement failed."))?; |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 172 | |
| 173 | let mut metadata: HashMap<i64, KeyMetaEntry> = Default::default(); |
| 174 | |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 175 | let mut rows = stmt |
| 176 | .query(params![key_id]) |
| 177 | .context(ks_err!("KeyMetaData::load_from_db: query failed."))?; |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 178 | db_utils::with_rows_extract_all(&mut rows, |row| { |
| 179 | let db_tag: i64 = row.get(0).context("Failed to read tag.")?; |
| 180 | metadata.insert( |
| 181 | db_tag, |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 182 | KeyMetaEntry::new_from_sql(db_tag, &SqlField::new(1, row)) |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 183 | .context("Failed to read KeyMetaEntry.")?, |
| 184 | ); |
| 185 | Ok(()) |
| 186 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 187 | .context(ks_err!("KeyMetaData::load_from_db."))?; |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 188 | |
| 189 | Ok(Self { data: metadata }) |
| 190 | } |
| 191 | |
| 192 | fn store_in_db(&self, key_id: i64, tx: &Transaction) -> Result<()> { |
| 193 | let mut stmt = tx |
| 194 | .prepare( |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 195 | "INSERT or REPLACE INTO persistent.keymetadata (keyentryid, tag, data) |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 196 | VALUES (?, ?, ?);", |
| 197 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 198 | .context(ks_err!("KeyMetaData::store_in_db: Failed to prepare statement."))?; |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 199 | |
| 200 | let iter = self.data.iter(); |
| 201 | for (tag, entry) in iter { |
| 202 | stmt.insert(params![key_id, tag, entry,]).with_context(|| { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 203 | ks_err!("KeyMetaData::store_in_db: Failed to insert {:?}", entry) |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 204 | })?; |
| 205 | } |
| 206 | Ok(()) |
| 207 | } |
| 208 | } |
| 209 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 210 | impl_metadata!( |
| 211 | /// A set of metadata for key blobs. |
| 212 | #[derive(Debug, Default, Eq, PartialEq)] |
| 213 | pub struct BlobMetaData; |
| 214 | /// A metadata entry for key blobs. |
| 215 | #[derive(Debug, Eq, PartialEq, Ord, PartialOrd)] |
| 216 | pub enum BlobMetaEntry { |
| 217 | /// If present, indicates that the blob is encrypted with another key or a key derived |
| 218 | /// from a password. |
| 219 | EncryptedBy(EncryptedBy) with accessor encrypted_by, |
| 220 | /// If the blob is password encrypted this field is set to the |
| 221 | /// salt used for the key derivation. |
| 222 | Salt(Vec<u8>) with accessor salt, |
| 223 | /// If the blob is encrypted, this field is set to the initialization vector. |
| 224 | Iv(Vec<u8>) with accessor iv, |
| 225 | /// If the blob is encrypted, this field holds the AEAD TAG. |
| 226 | AeadTag(Vec<u8>) with accessor aead_tag, |
| 227 | /// The uuid of the owning KeyMint instance. |
| 228 | KmUuid(Uuid) with accessor km_uuid, |
Paul Crowley | 8d5b253 | 2021-03-19 10:53:07 -0700 | [diff] [blame] | 229 | /// If the key is ECDH encrypted, this is the ephemeral public key |
| 230 | PublicKey(Vec<u8>) with accessor public_key, |
Paul Crowley | 44c02da | 2021-04-08 17:04:43 +0000 | [diff] [blame] | 231 | /// If the key is encrypted with a MaxBootLevel key, this is the boot level |
| 232 | /// of that key |
| 233 | MaxBootLevel(i32) with accessor max_boot_level, |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 234 | // --- ADD NEW META DATA FIELDS HERE --- |
| 235 | // For backwards compatibility add new entries only to |
| 236 | // end of this list and above this comment. |
| 237 | }; |
| 238 | ); |
| 239 | |
| 240 | impl BlobMetaData { |
| 241 | fn load_from_db(blob_id: i64, tx: &Transaction) -> Result<Self> { |
| 242 | let mut stmt = tx |
| 243 | .prepare( |
| 244 | "SELECT tag, data from persistent.blobmetadata |
| 245 | WHERE blobentryid = ?;", |
| 246 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 247 | .context(ks_err!("BlobMetaData::load_from_db: prepare statement failed."))?; |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 248 | |
| 249 | let mut metadata: HashMap<i64, BlobMetaEntry> = Default::default(); |
| 250 | |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 251 | let mut rows = stmt.query(params![blob_id]).context(ks_err!("query failed."))?; |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 252 | db_utils::with_rows_extract_all(&mut rows, |row| { |
| 253 | let db_tag: i64 = row.get(0).context("Failed to read tag.")?; |
| 254 | metadata.insert( |
| 255 | db_tag, |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 256 | BlobMetaEntry::new_from_sql(db_tag, &SqlField::new(1, row)) |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 257 | .context("Failed to read BlobMetaEntry.")?, |
| 258 | ); |
| 259 | Ok(()) |
| 260 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 261 | .context(ks_err!("BlobMetaData::load_from_db"))?; |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 262 | |
| 263 | Ok(Self { data: metadata }) |
| 264 | } |
| 265 | |
| 266 | fn store_in_db(&self, blob_id: i64, tx: &Transaction) -> Result<()> { |
| 267 | let mut stmt = tx |
| 268 | .prepare( |
| 269 | "INSERT or REPLACE INTO persistent.blobmetadata (blobentryid, tag, data) |
| 270 | VALUES (?, ?, ?);", |
| 271 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 272 | .context(ks_err!("BlobMetaData::store_in_db: Failed to prepare statement.",))?; |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 273 | |
| 274 | let iter = self.data.iter(); |
| 275 | for (tag, entry) in iter { |
| 276 | stmt.insert(params![blob_id, tag, entry,]).with_context(|| { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 277 | ks_err!("BlobMetaData::store_in_db: Failed to insert {:?}", entry) |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 278 | })?; |
| 279 | } |
| 280 | Ok(()) |
| 281 | } |
| 282 | } |
| 283 | |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 284 | /// Indicates the type of the keyentry. |
| 285 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] |
| 286 | pub enum KeyType { |
| 287 | /// This is a client key type. These keys are created or imported through the Keystore 2.0 |
| 288 | /// AIDL interface android.system.keystore2. |
| 289 | Client, |
| 290 | /// This is a super key type. These keys are created by keystore itself and used to encrypt |
| 291 | /// other key blobs to provide LSKF binding. |
| 292 | Super, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | impl ToSql for KeyType { |
| 296 | fn to_sql(&self) -> rusqlite::Result<ToSqlOutput> { |
| 297 | Ok(ToSqlOutput::Owned(Value::Integer(match self { |
| 298 | KeyType::Client => 0, |
| 299 | KeyType::Super => 1, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 300 | }))) |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | impl FromSql for KeyType { |
| 305 | fn column_result(value: ValueRef) -> FromSqlResult<Self> { |
| 306 | match i64::column_result(value)? { |
| 307 | 0 => Ok(KeyType::Client), |
| 308 | 1 => Ok(KeyType::Super), |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 309 | v => Err(FromSqlError::OutOfRange(v)), |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 314 | /// Uuid representation that can be stored in the database. |
| 315 | /// Right now it can only be initialized from SecurityLevel. |
| 316 | /// Once KeyMint provides a UUID type a corresponding From impl shall be added. |
| 317 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 318 | pub struct Uuid([u8; 16]); |
| 319 | |
| 320 | impl Deref for Uuid { |
| 321 | type Target = [u8; 16]; |
| 322 | |
| 323 | fn deref(&self) -> &Self::Target { |
| 324 | &self.0 |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | impl From<SecurityLevel> for Uuid { |
| 329 | fn from(sec_level: SecurityLevel) -> Self { |
| 330 | Self((sec_level.0 as u128).to_be_bytes()) |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | impl ToSql for Uuid { |
| 335 | fn to_sql(&self) -> rusqlite::Result<ToSqlOutput> { |
| 336 | self.0.to_sql() |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | impl FromSql for Uuid { |
| 341 | fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> { |
| 342 | let blob = Vec::<u8>::column_result(value)?; |
| 343 | if blob.len() != 16 { |
| 344 | return Err(FromSqlError::OutOfRange(blob.len() as i64)); |
| 345 | } |
| 346 | let mut arr = [0u8; 16]; |
| 347 | arr.copy_from_slice(&blob); |
| 348 | Ok(Self(arr)) |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /// Key entries that are not associated with any KeyMint instance, such as pure certificate |
| 353 | /// entries are associated with this UUID. |
| 354 | pub static KEYSTORE_UUID: Uuid = Uuid([ |
| 355 | 0x41, 0xe3, 0xb9, 0xce, 0x27, 0x58, 0x4e, 0x91, 0xbc, 0xfd, 0xa5, 0x5d, 0x91, 0x85, 0xab, 0x11, |
| 356 | ]); |
| 357 | |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 358 | /// Indicates how the sensitive part of this key blob is encrypted. |
| 359 | #[derive(Debug, Eq, PartialEq, Ord, PartialOrd)] |
| 360 | pub enum EncryptedBy { |
| 361 | /// The keyblob is encrypted by a user password. |
| 362 | /// In the database this variant is represented as NULL. |
| 363 | Password, |
| 364 | /// The keyblob is encrypted by another key with wrapped key id. |
| 365 | /// In the database this variant is represented as non NULL value |
| 366 | /// that is convertible to i64, typically NUMERIC. |
| 367 | KeyId(i64), |
| 368 | } |
| 369 | |
| 370 | impl ToSql for EncryptedBy { |
| 371 | fn to_sql(&self) -> rusqlite::Result<ToSqlOutput> { |
| 372 | match self { |
| 373 | Self::Password => Ok(ToSqlOutput::Owned(Value::Null)), |
| 374 | Self::KeyId(id) => id.to_sql(), |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | impl FromSql for EncryptedBy { |
| 380 | fn column_result(value: ValueRef) -> FromSqlResult<Self> { |
| 381 | match value { |
| 382 | ValueRef::Null => Ok(Self::Password), |
| 383 | _ => Ok(Self::KeyId(i64::column_result(value)?)), |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /// A database representation of wall clock time. DateTime stores unix epoch time as |
| 389 | /// i64 in milliseconds. |
| 390 | #[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd)] |
| 391 | pub struct DateTime(i64); |
| 392 | |
| 393 | /// Error type returned when creating DateTime or converting it from and to |
| 394 | /// SystemTime. |
| 395 | #[derive(thiserror::Error, Debug)] |
| 396 | pub enum DateTimeError { |
| 397 | /// This is returned when SystemTime and Duration computations fail. |
| 398 | #[error(transparent)] |
| 399 | SystemTimeError(#[from] SystemTimeError), |
| 400 | |
| 401 | /// This is returned when type conversions fail. |
| 402 | #[error(transparent)] |
| 403 | TypeConversion(#[from] std::num::TryFromIntError), |
| 404 | |
| 405 | /// This is returned when checked time arithmetic failed. |
| 406 | #[error("Time arithmetic failed.")] |
| 407 | TimeArithmetic, |
| 408 | } |
| 409 | |
| 410 | impl DateTime { |
| 411 | /// Constructs a new DateTime object denoting the current time. This may fail during |
| 412 | /// conversion to unix epoch time and during conversion to the internal i64 representation. |
| 413 | pub fn now() -> Result<Self, DateTimeError> { |
| 414 | Ok(Self(SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?.as_millis().try_into()?)) |
| 415 | } |
| 416 | |
| 417 | /// Constructs a new DateTime object from milliseconds. |
| 418 | pub fn from_millis_epoch(millis: i64) -> Self { |
| 419 | Self(millis) |
| 420 | } |
| 421 | |
| 422 | /// Returns unix epoch time in milliseconds. |
Chris Wailes | 3877f29 | 2021-07-26 19:24:18 -0700 | [diff] [blame] | 423 | pub fn to_millis_epoch(self) -> i64 { |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 424 | self.0 |
| 425 | } |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | impl ToSql for DateTime { |
| 429 | fn to_sql(&self) -> rusqlite::Result<ToSqlOutput> { |
| 430 | Ok(ToSqlOutput::Owned(Value::Integer(self.0))) |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | impl FromSql for DateTime { |
| 435 | fn column_result(value: ValueRef) -> FromSqlResult<Self> { |
| 436 | Ok(Self(i64::column_result(value)?)) |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | impl TryInto<SystemTime> for DateTime { |
| 441 | type Error = DateTimeError; |
| 442 | |
| 443 | fn try_into(self) -> Result<SystemTime, Self::Error> { |
| 444 | // We want to construct a SystemTime representation equivalent to self, denoting |
| 445 | // a point in time THEN, but we cannot set the time directly. We can only construct |
| 446 | // a SystemTime denoting NOW, and we can get the duration between EPOCH and NOW, |
| 447 | // and between EPOCH and THEN. With this common reference we can construct the |
| 448 | // duration between NOW and THEN which we can add to our SystemTime representation |
| 449 | // of NOW to get a SystemTime representation of THEN. |
| 450 | // Durations can only be positive, thus the if statement below. |
| 451 | let now = SystemTime::now(); |
| 452 | let now_epoch = now.duration_since(SystemTime::UNIX_EPOCH)?; |
| 453 | let then_epoch = Duration::from_millis(self.0.try_into()?); |
| 454 | Ok(if now_epoch > then_epoch { |
| 455 | // then = now - (now_epoch - then_epoch) |
| 456 | now_epoch |
| 457 | .checked_sub(then_epoch) |
| 458 | .and_then(|d| now.checked_sub(d)) |
| 459 | .ok_or(DateTimeError::TimeArithmetic)? |
| 460 | } else { |
| 461 | // then = now + (then_epoch - now_epoch) |
| 462 | then_epoch |
| 463 | .checked_sub(now_epoch) |
| 464 | .and_then(|d| now.checked_add(d)) |
| 465 | .ok_or(DateTimeError::TimeArithmetic)? |
| 466 | }) |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | impl TryFrom<SystemTime> for DateTime { |
| 471 | type Error = DateTimeError; |
| 472 | |
| 473 | fn try_from(t: SystemTime) -> Result<Self, Self::Error> { |
| 474 | Ok(Self(t.duration_since(SystemTime::UNIX_EPOCH)?.as_millis().try_into()?)) |
| 475 | } |
| 476 | } |
| 477 | |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 478 | #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)] |
| 479 | enum KeyLifeCycle { |
| 480 | /// Existing keys have a key ID but are not fully populated yet. |
| 481 | /// This is a transient state. If Keystore finds any such keys when it starts up, it must move |
| 482 | /// them to Unreferenced for garbage collection. |
| 483 | Existing, |
| 484 | /// A live key is fully populated and usable by clients. |
| 485 | Live, |
| 486 | /// An unreferenced key is scheduled for garbage collection. |
| 487 | Unreferenced, |
| 488 | } |
| 489 | |
| 490 | impl ToSql for KeyLifeCycle { |
| 491 | fn to_sql(&self) -> rusqlite::Result<ToSqlOutput> { |
| 492 | match self { |
| 493 | Self::Existing => Ok(ToSqlOutput::Owned(Value::Integer(0))), |
| 494 | Self::Live => Ok(ToSqlOutput::Owned(Value::Integer(1))), |
| 495 | Self::Unreferenced => Ok(ToSqlOutput::Owned(Value::Integer(2))), |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | impl FromSql for KeyLifeCycle { |
| 501 | fn column_result(value: ValueRef) -> FromSqlResult<Self> { |
| 502 | match i64::column_result(value)? { |
| 503 | 0 => Ok(KeyLifeCycle::Existing), |
| 504 | 1 => Ok(KeyLifeCycle::Live), |
| 505 | 2 => Ok(KeyLifeCycle::Unreferenced), |
| 506 | v => Err(FromSqlError::OutOfRange(v)), |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
David Drysdale | 8da288c | 2024-07-29 15:57:01 +0100 | [diff] [blame] | 511 | /// Current state of a `blobentry` row. |
| 512 | #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone, Default)] |
| 513 | enum BlobState { |
| 514 | #[default] |
| 515 | /// Current blobentry (of its `subcomponent_type`) for the associated key. |
| 516 | Current, |
| 517 | /// Blobentry that is no longer the current blob (of its `subcomponent_type`) for the associated |
| 518 | /// key. |
| 519 | Superseded, |
| 520 | /// Blobentry for a key that no longer exists. |
| 521 | Orphaned, |
| 522 | } |
| 523 | |
| 524 | impl ToSql for BlobState { |
| 525 | fn to_sql(&self) -> rusqlite::Result<ToSqlOutput> { |
| 526 | match self { |
| 527 | Self::Current => Ok(ToSqlOutput::Owned(Value::Integer(0))), |
| 528 | Self::Superseded => Ok(ToSqlOutput::Owned(Value::Integer(1))), |
| 529 | Self::Orphaned => Ok(ToSqlOutput::Owned(Value::Integer(2))), |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | impl FromSql for BlobState { |
| 535 | fn column_result(value: ValueRef) -> FromSqlResult<Self> { |
| 536 | match i64::column_result(value)? { |
| 537 | 0 => Ok(Self::Current), |
| 538 | 1 => Ok(Self::Superseded), |
| 539 | 2 => Ok(Self::Orphaned), |
| 540 | v => Err(FromSqlError::OutOfRange(v)), |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 545 | /// Keys have a KeyMint blob component and optional public certificate and |
| 546 | /// certificate chain components. |
| 547 | /// KeyEntryLoadBits is a bitmap that indicates to `KeystoreDB::load_key_entry` |
| 548 | /// which components shall be loaded from the database if present. |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 549 | #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)] |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 550 | pub struct KeyEntryLoadBits(u32); |
| 551 | |
| 552 | impl KeyEntryLoadBits { |
| 553 | /// Indicate to `KeystoreDB::load_key_entry` that no component shall be loaded. |
| 554 | pub const NONE: KeyEntryLoadBits = Self(0); |
| 555 | /// Indicate to `KeystoreDB::load_key_entry` that the KeyMint component shall be loaded. |
| 556 | pub const KM: KeyEntryLoadBits = Self(1); |
| 557 | /// Indicate to `KeystoreDB::load_key_entry` that the Public components shall be loaded. |
| 558 | pub const PUBLIC: KeyEntryLoadBits = Self(2); |
| 559 | /// Indicate to `KeystoreDB::load_key_entry` that both components shall be loaded. |
| 560 | pub const BOTH: KeyEntryLoadBits = Self(3); |
| 561 | |
| 562 | /// Returns true if this object indicates that the public components shall be loaded. |
| 563 | pub const fn load_public(&self) -> bool { |
| 564 | self.0 & Self::PUBLIC.0 != 0 |
| 565 | } |
| 566 | |
| 567 | /// Returns true if the object indicates that the KeyMint component shall be loaded. |
| 568 | pub const fn load_km(&self) -> bool { |
| 569 | self.0 & Self::KM.0 != 0 |
| 570 | } |
| 571 | } |
| 572 | |
Andrew Walbran | a4bc1a9 | 2024-09-03 13:08:17 +0100 | [diff] [blame] | 573 | static KEY_ID_LOCK: LazyLock<KeyIdLockDb> = LazyLock::new(KeyIdLockDb::new); |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 574 | |
| 575 | struct KeyIdLockDb { |
| 576 | locked_keys: Mutex<HashSet<i64>>, |
| 577 | cond_var: Condvar, |
| 578 | } |
| 579 | |
| 580 | /// A locked key. While a guard exists for a given key id, the same key cannot be loaded |
| 581 | /// from the database a second time. Most functions manipulating the key blob database |
| 582 | /// require a KeyIdGuard. |
| 583 | #[derive(Debug)] |
| 584 | pub struct KeyIdGuard(i64); |
| 585 | |
| 586 | impl KeyIdLockDb { |
| 587 | fn new() -> Self { |
| 588 | Self { locked_keys: Mutex::new(HashSet::new()), cond_var: Condvar::new() } |
| 589 | } |
| 590 | |
| 591 | /// This function blocks until an exclusive lock for the given key entry id can |
| 592 | /// be acquired. It returns a guard object, that represents the lifecycle of the |
| 593 | /// acquired lock. |
David Drysdale | 8c4c4f3 | 2023-10-31 12:14:11 +0000 | [diff] [blame] | 594 | fn get(&self, key_id: i64) -> KeyIdGuard { |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 595 | let mut locked_keys = self.locked_keys.lock().unwrap(); |
| 596 | while locked_keys.contains(&key_id) { |
| 597 | locked_keys = self.cond_var.wait(locked_keys).unwrap(); |
| 598 | } |
| 599 | locked_keys.insert(key_id); |
| 600 | KeyIdGuard(key_id) |
| 601 | } |
| 602 | |
| 603 | /// This function attempts to acquire an exclusive lock on a given key id. If the |
| 604 | /// given key id is already taken the function returns None immediately. If a lock |
| 605 | /// can be acquired this function returns a guard object, that represents the |
| 606 | /// lifecycle of the acquired lock. |
David Drysdale | 8c4c4f3 | 2023-10-31 12:14:11 +0000 | [diff] [blame] | 607 | fn try_get(&self, key_id: i64) -> Option<KeyIdGuard> { |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 608 | let mut locked_keys = self.locked_keys.lock().unwrap(); |
| 609 | if locked_keys.insert(key_id) { |
| 610 | Some(KeyIdGuard(key_id)) |
| 611 | } else { |
| 612 | None |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | impl KeyIdGuard { |
| 618 | /// Get the numeric key id of the locked key. |
| 619 | pub fn id(&self) -> i64 { |
| 620 | self.0 |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | impl Drop for KeyIdGuard { |
| 625 | fn drop(&mut self) { |
| 626 | let mut locked_keys = KEY_ID_LOCK.locked_keys.lock().unwrap(); |
| 627 | locked_keys.remove(&self.0); |
Janis Danisevskis | 7fd5358 | 2020-11-23 13:40:34 -0800 | [diff] [blame] | 628 | drop(locked_keys); |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 629 | KEY_ID_LOCK.cond_var.notify_all(); |
| 630 | } |
| 631 | } |
| 632 | |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 633 | /// This type represents a certificate and certificate chain entry for a key. |
Max Bires | 2b2e656 | 2020-09-22 11:22:36 -0700 | [diff] [blame] | 634 | #[derive(Debug, Default)] |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 635 | pub struct CertificateInfo { |
| 636 | cert: Option<Vec<u8>>, |
| 637 | cert_chain: Option<Vec<u8>>, |
| 638 | } |
| 639 | |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 640 | /// This type represents a Blob with its metadata and an optional superseded blob. |
| 641 | #[derive(Debug)] |
| 642 | pub struct BlobInfo<'a> { |
| 643 | blob: &'a [u8], |
| 644 | metadata: &'a BlobMetaData, |
| 645 | /// Superseded blobs are an artifact of legacy import. In some rare occasions |
| 646 | /// the key blob needs to be upgraded during import. In that case two |
| 647 | /// blob are imported, the superseded one will have to be imported first, |
| 648 | /// so that the garbage collector can reap it. |
| 649 | superseded_blob: Option<(&'a [u8], &'a BlobMetaData)>, |
| 650 | } |
| 651 | |
| 652 | impl<'a> BlobInfo<'a> { |
| 653 | /// Create a new instance of blob info with blob and corresponding metadata |
| 654 | /// and no superseded blob info. |
| 655 | pub fn new(blob: &'a [u8], metadata: &'a BlobMetaData) -> Self { |
| 656 | Self { blob, metadata, superseded_blob: None } |
| 657 | } |
| 658 | |
| 659 | /// Create a new instance of blob info with blob and corresponding metadata |
| 660 | /// as well as superseded blob info. |
| 661 | pub fn new_with_superseded( |
| 662 | blob: &'a [u8], |
| 663 | metadata: &'a BlobMetaData, |
| 664 | superseded_blob: Option<(&'a [u8], &'a BlobMetaData)>, |
| 665 | ) -> Self { |
| 666 | Self { blob, metadata, superseded_blob } |
| 667 | } |
| 668 | } |
| 669 | |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 670 | impl CertificateInfo { |
| 671 | /// Constructs a new CertificateInfo object from `cert` and `cert_chain` |
| 672 | pub fn new(cert: Option<Vec<u8>>, cert_chain: Option<Vec<u8>>) -> Self { |
| 673 | Self { cert, cert_chain } |
| 674 | } |
| 675 | |
| 676 | /// Take the cert |
| 677 | pub fn take_cert(&mut self) -> Option<Vec<u8>> { |
| 678 | self.cert.take() |
| 679 | } |
| 680 | |
| 681 | /// Take the cert chain |
| 682 | pub fn take_cert_chain(&mut self) -> Option<Vec<u8>> { |
| 683 | self.cert_chain.take() |
| 684 | } |
| 685 | } |
| 686 | |
Max Bires | 2b2e656 | 2020-09-22 11:22:36 -0700 | [diff] [blame] | 687 | /// This type represents a certificate chain with a private key corresponding to the leaf |
| 688 | /// certificate. TODO(jbires): This will be used in a follow-on CL, for now it's used in the tests. |
Max Bires | 2b2e656 | 2020-09-22 11:22:36 -0700 | [diff] [blame] | 689 | pub struct CertificateChain { |
Max Bires | 97f9681 | 2021-02-23 23:44:57 -0800 | [diff] [blame] | 690 | /// A KM key blob |
| 691 | pub private_key: ZVec, |
| 692 | /// A batch cert for private_key |
| 693 | pub batch_cert: Vec<u8>, |
| 694 | /// A full certificate chain from root signing authority to private_key, including batch_cert |
| 695 | /// for convenience. |
| 696 | pub cert_chain: Vec<u8>, |
Max Bires | 2b2e656 | 2020-09-22 11:22:36 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 699 | /// This type represents a Keystore 2.0 key entry. |
| 700 | /// An entry has a unique `id` by which it can be found in the database. |
| 701 | /// It has a security level field, key parameters, and three optional fields |
| 702 | /// for the KeyMint blob, public certificate and a public certificate chain. |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 703 | #[derive(Debug, Default, Eq, PartialEq)] |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 704 | pub struct KeyEntry { |
| 705 | id: i64, |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 706 | key_blob_info: Option<(Vec<u8>, BlobMetaData)>, |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 707 | cert: Option<Vec<u8>>, |
| 708 | cert_chain: Option<Vec<u8>>, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 709 | km_uuid: Uuid, |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 710 | parameters: Vec<KeyParameter>, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 711 | metadata: KeyMetaData, |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 712 | pure_cert: bool, |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | impl KeyEntry { |
| 716 | /// Returns the unique id of the Key entry. |
| 717 | pub fn id(&self) -> i64 { |
| 718 | self.id |
| 719 | } |
| 720 | /// Exposes the optional KeyMint blob. |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 721 | pub fn key_blob_info(&self) -> &Option<(Vec<u8>, BlobMetaData)> { |
| 722 | &self.key_blob_info |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 723 | } |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 724 | /// Extracts the Optional KeyMint blob including its metadata. |
| 725 | pub fn take_key_blob_info(&mut self) -> Option<(Vec<u8>, BlobMetaData)> { |
| 726 | self.key_blob_info.take() |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 727 | } |
| 728 | /// Exposes the optional public certificate. |
| 729 | pub fn cert(&self) -> &Option<Vec<u8>> { |
| 730 | &self.cert |
| 731 | } |
| 732 | /// Extracts the optional public certificate. |
| 733 | pub fn take_cert(&mut self) -> Option<Vec<u8>> { |
| 734 | self.cert.take() |
| 735 | } |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 736 | /// Extracts the optional public certificate_chain. |
| 737 | pub fn take_cert_chain(&mut self) -> Option<Vec<u8>> { |
| 738 | self.cert_chain.take() |
| 739 | } |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 740 | /// Returns the uuid of the owning KeyMint instance. |
| 741 | pub fn km_uuid(&self) -> &Uuid { |
| 742 | &self.km_uuid |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 743 | } |
Janis Danisevskis | 04b0283 | 2020-10-26 09:21:40 -0700 | [diff] [blame] | 744 | /// Consumes this key entry and extracts the keyparameters from it. |
| 745 | pub fn into_key_parameters(self) -> Vec<KeyParameter> { |
| 746 | self.parameters |
| 747 | } |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 748 | /// Exposes the key metadata of this key entry. |
| 749 | pub fn metadata(&self) -> &KeyMetaData { |
| 750 | &self.metadata |
| 751 | } |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 752 | /// This returns true if the entry is a pure certificate entry with no |
| 753 | /// private key component. |
| 754 | pub fn pure_cert(&self) -> bool { |
| 755 | self.pure_cert |
| 756 | } |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /// Indicates the sub component of a key entry for persistent storage. |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 760 | #[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)] |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 761 | pub struct SubComponentType(u32); |
| 762 | impl SubComponentType { |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 763 | /// Persistent identifier for a key blob. |
| 764 | pub const KEY_BLOB: SubComponentType = Self(0); |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 765 | /// Persistent identifier for a certificate blob. |
| 766 | pub const CERT: SubComponentType = Self(1); |
| 767 | /// Persistent identifier for a certificate chain blob. |
| 768 | pub const CERT_CHAIN: SubComponentType = Self(2); |
| 769 | } |
| 770 | |
| 771 | impl ToSql for SubComponentType { |
| 772 | fn to_sql(&self) -> rusqlite::Result<ToSqlOutput> { |
| 773 | self.0.to_sql() |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | impl FromSql for SubComponentType { |
| 778 | fn column_result(value: ValueRef) -> FromSqlResult<Self> { |
| 779 | Ok(Self(u32::column_result(value)?)) |
| 780 | } |
| 781 | } |
| 782 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 783 | /// This trait is private to the database module. It is used to convey whether or not the garbage |
| 784 | /// collector shall be invoked after a database access. All closures passed to |
| 785 | /// `KeystoreDB::with_transaction` return a tuple (bool, T) where the bool indicates if the |
| 786 | /// gc needs to be triggered. This convenience function allows to turn any anyhow::Result<T> |
| 787 | /// into anyhow::Result<(bool, T)> by simply appending one of `.do_gc(bool)`, `.no_gc()`, or |
| 788 | /// `.need_gc()`. |
| 789 | trait DoGc<T> { |
| 790 | fn do_gc(self, need_gc: bool) -> Result<(bool, T)>; |
| 791 | |
| 792 | fn no_gc(self) -> Result<(bool, T)>; |
| 793 | |
| 794 | fn need_gc(self) -> Result<(bool, T)>; |
| 795 | } |
| 796 | |
| 797 | impl<T> DoGc<T> for Result<T> { |
| 798 | fn do_gc(self, need_gc: bool) -> Result<(bool, T)> { |
| 799 | self.map(|r| (need_gc, r)) |
| 800 | } |
| 801 | |
| 802 | fn no_gc(self) -> Result<(bool, T)> { |
| 803 | self.do_gc(false) |
| 804 | } |
| 805 | |
| 806 | fn need_gc(self) -> Result<(bool, T)> { |
| 807 | self.do_gc(true) |
| 808 | } |
| 809 | } |
| 810 | |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 811 | /// KeystoreDB wraps a connection to an SQLite database and tracks its |
| 812 | /// ownership. It also implements all of Keystore 2.0's database functionality. |
Joel Galenson | 26f4d01 | 2020-07-17 14:57:21 -0700 | [diff] [blame] | 813 | pub struct KeystoreDB { |
Joel Galenson | 26f4d01 | 2020-07-17 14:57:21 -0700 | [diff] [blame] | 814 | conn: Connection, |
Janis Danisevskis | 3395f86 | 2021-05-06 10:54:17 -0700 | [diff] [blame] | 815 | gc: Option<Arc<Gc>>, |
Matthew Maurer | d7815ca | 2021-05-06 21:58:45 -0700 | [diff] [blame] | 816 | perboot: Arc<perboot::PerbootDB>, |
Joel Galenson | 26f4d01 | 2020-07-17 14:57:21 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Hasini Gunasinghe | 557b103 | 2020-11-10 01:35:30 +0000 | [diff] [blame] | 819 | /// Database representation of the monotonic time retrieved from the system call clock_gettime with |
Eric Biggers | 19b3b0d | 2024-01-31 22:46:47 +0000 | [diff] [blame] | 820 | /// CLOCK_BOOTTIME. Stores monotonic time as i64 in milliseconds. |
Hasini Gunasinghe | 557b103 | 2020-11-10 01:35:30 +0000 | [diff] [blame] | 821 | #[derive(Debug, Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd)] |
Eric Biggers | 19b3b0d | 2024-01-31 22:46:47 +0000 | [diff] [blame] | 822 | pub struct BootTime(i64); |
Hasini Gunasinghe | 557b103 | 2020-11-10 01:35:30 +0000 | [diff] [blame] | 823 | |
Eric Biggers | 19b3b0d | 2024-01-31 22:46:47 +0000 | [diff] [blame] | 824 | impl BootTime { |
| 825 | /// Constructs a new BootTime |
Hasini Gunasinghe | 557b103 | 2020-11-10 01:35:30 +0000 | [diff] [blame] | 826 | pub fn now() -> Self { |
Hasini Gunasinghe | 66a2460 | 2021-05-12 19:03:12 +0000 | [diff] [blame] | 827 | Self(get_current_time_in_milliseconds()) |
Hasini Gunasinghe | 557b103 | 2020-11-10 01:35:30 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Eric Biggers | 19b3b0d | 2024-01-31 22:46:47 +0000 | [diff] [blame] | 830 | /// Returns the value of BootTime in milliseconds as i64 |
Hasini Gunasinghe | 66a2460 | 2021-05-12 19:03:12 +0000 | [diff] [blame] | 831 | pub fn milliseconds(&self) -> i64 { |
| 832 | self.0 |
David Drysdale | 0e45a61 | 2021-02-25 17:24:36 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Eric Biggers | 19b3b0d | 2024-01-31 22:46:47 +0000 | [diff] [blame] | 835 | /// Returns the integer value of BootTime as i64 |
Hasini Gunasinghe | 557b103 | 2020-11-10 01:35:30 +0000 | [diff] [blame] | 836 | pub fn seconds(&self) -> i64 { |
Hasini Gunasinghe | 66a2460 | 2021-05-12 19:03:12 +0000 | [diff] [blame] | 837 | self.0 / 1000 |
Hasini Gunasinghe | b3715fb | 2021-02-26 20:34:45 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 840 | /// Like i64::checked_sub. |
| 841 | pub fn checked_sub(&self, other: &Self) -> Option<Self> { |
| 842 | self.0.checked_sub(other.0).map(Self) |
| 843 | } |
Hasini Gunasinghe | 557b103 | 2020-11-10 01:35:30 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Eric Biggers | 19b3b0d | 2024-01-31 22:46:47 +0000 | [diff] [blame] | 846 | impl ToSql for BootTime { |
Hasini Gunasinghe | 557b103 | 2020-11-10 01:35:30 +0000 | [diff] [blame] | 847 | fn to_sql(&self) -> rusqlite::Result<ToSqlOutput> { |
| 848 | Ok(ToSqlOutput::Owned(Value::Integer(self.0))) |
| 849 | } |
| 850 | } |
| 851 | |
Eric Biggers | 19b3b0d | 2024-01-31 22:46:47 +0000 | [diff] [blame] | 852 | impl FromSql for BootTime { |
Hasini Gunasinghe | 557b103 | 2020-11-10 01:35:30 +0000 | [diff] [blame] | 853 | fn column_result(value: ValueRef) -> FromSqlResult<Self> { |
| 854 | Ok(Self(i64::column_result(value)?)) |
| 855 | } |
| 856 | } |
| 857 | |
Hasini Gunasinghe | 52333ba | 2020-11-06 01:24:16 +0000 | [diff] [blame] | 858 | /// This struct encapsulates the information to be stored in the database about the auth tokens |
| 859 | /// received by keystore. |
Matthew Maurer | d7815ca | 2021-05-06 21:58:45 -0700 | [diff] [blame] | 860 | #[derive(Clone)] |
Hasini Gunasinghe | 52333ba | 2020-11-06 01:24:16 +0000 | [diff] [blame] | 861 | pub struct AuthTokenEntry { |
| 862 | auth_token: HardwareAuthToken, |
Hasini Gunasinghe | 66a2460 | 2021-05-12 19:03:12 +0000 | [diff] [blame] | 863 | // Time received in milliseconds |
Eric Biggers | 19b3b0d | 2024-01-31 22:46:47 +0000 | [diff] [blame] | 864 | time_received: BootTime, |
Hasini Gunasinghe | 52333ba | 2020-11-06 01:24:16 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | impl AuthTokenEntry { |
Eric Biggers | 19b3b0d | 2024-01-31 22:46:47 +0000 | [diff] [blame] | 868 | fn new(auth_token: HardwareAuthToken, time_received: BootTime) -> Self { |
Hasini Gunasinghe | 52333ba | 2020-11-06 01:24:16 +0000 | [diff] [blame] | 869 | AuthTokenEntry { auth_token, time_received } |
| 870 | } |
| 871 | |
| 872 | /// Checks if this auth token satisfies the given authentication information. |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 873 | pub fn satisfies(&self, user_secure_ids: &[i64], auth_type: HardwareAuthenticatorType) -> bool { |
Hasini Gunasinghe | 52333ba | 2020-11-06 01:24:16 +0000 | [diff] [blame] | 874 | user_secure_ids.iter().any(|&sid| { |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 875 | (sid == self.auth_token.userId || sid == self.auth_token.authenticatorId) |
Charisee | 03e0084 | 2023-01-25 01:41:23 +0000 | [diff] [blame] | 876 | && ((auth_type.0 & self.auth_token.authenticatorType.0) != 0) |
Hasini Gunasinghe | 52333ba | 2020-11-06 01:24:16 +0000 | [diff] [blame] | 877 | }) |
| 878 | } |
| 879 | |
Hasini Gunasinghe | 52333ba | 2020-11-06 01:24:16 +0000 | [diff] [blame] | 880 | /// Returns the auth token wrapped by the AuthTokenEntry |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 881 | pub fn auth_token(&self) -> &HardwareAuthToken { |
| 882 | &self.auth_token |
| 883 | } |
| 884 | |
| 885 | /// Returns the auth token wrapped by the AuthTokenEntry |
| 886 | pub fn take_auth_token(self) -> HardwareAuthToken { |
Hasini Gunasinghe | 52333ba | 2020-11-06 01:24:16 +0000 | [diff] [blame] | 887 | self.auth_token |
| 888 | } |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 889 | |
| 890 | /// Returns the time that this auth token was received. |
Eric Biggers | 19b3b0d | 2024-01-31 22:46:47 +0000 | [diff] [blame] | 891 | pub fn time_received(&self) -> BootTime { |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 892 | self.time_received |
| 893 | } |
Hasini Gunasinghe | b3715fb | 2021-02-26 20:34:45 +0000 | [diff] [blame] | 894 | |
| 895 | /// Returns the challenge value of the auth token. |
| 896 | pub fn challenge(&self) -> i64 { |
| 897 | self.auth_token.challenge |
| 898 | } |
Hasini Gunasinghe | 52333ba | 2020-11-06 01:24:16 +0000 | [diff] [blame] | 899 | } |
| 900 | |
David Drysdale | f1ba381 | 2024-05-08 17:50:13 +0100 | [diff] [blame] | 901 | /// Information about a superseded blob (a blob that is no longer the |
| 902 | /// most recent blob of that type for a given key, due to upgrade or |
| 903 | /// replacement). |
| 904 | pub struct SupersededBlob { |
| 905 | /// ID |
| 906 | pub blob_id: i64, |
| 907 | /// Contents. |
| 908 | pub blob: Vec<u8>, |
| 909 | /// Metadata. |
| 910 | pub metadata: BlobMetaData, |
| 911 | } |
| 912 | |
Joel Galenson | 26f4d01 | 2020-07-17 14:57:21 -0700 | [diff] [blame] | 913 | impl KeystoreDB { |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 914 | const UNASSIGNED_KEY_ID: i64 = -1i64; |
David Drysdale | 8da288c | 2024-07-29 15:57:01 +0100 | [diff] [blame] | 915 | const CURRENT_DB_VERSION: u32 = 2; |
| 916 | const UPGRADERS: &'static [fn(&Transaction) -> Result<u32>] = |
| 917 | &[Self::from_0_to_1, Self::from_1_to_2]; |
Janis Danisevskis | b00ebd0 | 2021-02-02 21:52:24 -0800 | [diff] [blame] | 918 | |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 919 | /// Name of the file that holds the cross-boot persistent database. |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 920 | pub const PERSISTENT_DB_FILENAME: &'static str = "persistent.sqlite"; |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 921 | |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 922 | /// This will create a new database connection connecting the two |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 923 | /// files persistent.sqlite and perboot.sqlite in the given directory. |
| 924 | /// It also attempts to initialize all of the tables. |
| 925 | /// KeystoreDB cannot be used by multiple threads. |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 926 | /// Each thread should open their own connection using `thread_local!`. |
Janis Danisevskis | 3395f86 | 2021-05-06 10:54:17 -0700 | [diff] [blame] | 927 | pub fn new(db_root: &Path, gc: Option<Arc<Gc>>) -> Result<Self> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 928 | let _wp = wd::watch("KeystoreDB::new"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 929 | |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 930 | let persistent_path = Self::make_persistent_path(db_root)?; |
Seth Moore | 472fcbb | 2021-05-12 10:07:51 -0700 | [diff] [blame] | 931 | let conn = Self::make_connection(&persistent_path)?; |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 932 | |
Matthew Maurer | d7815ca | 2021-05-06 21:58:45 -0700 | [diff] [blame] | 933 | let mut db = Self { conn, gc, perboot: perboot::PERBOOT_DB.clone() }; |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 934 | db.with_transaction(Immediate("TX_new"), |tx| { |
Janis Danisevskis | cfaf919 | 2021-05-26 16:31:02 -0700 | [diff] [blame] | 935 | versioning::upgrade_database(tx, Self::CURRENT_DB_VERSION, Self::UPGRADERS) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 936 | .context(ks_err!("KeystoreDB::new: trying to upgrade database."))?; |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 937 | Self::init_tables(tx).context("Trying to initialize tables.").no_gc() |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 938 | })?; |
| 939 | Ok(db) |
Joel Galenson | 2aab443 | 2020-07-22 15:27:57 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Janis Danisevskis | cfaf919 | 2021-05-26 16:31:02 -0700 | [diff] [blame] | 942 | // This upgrade function deletes all MAX_BOOT_LEVEL keys, that were generated before |
| 943 | // cryptographic binding to the boot level keys was implemented. |
| 944 | fn from_0_to_1(tx: &Transaction) -> Result<u32> { |
| 945 | tx.execute( |
| 946 | "UPDATE persistent.keyentry SET state = ? |
| 947 | WHERE |
| 948 | id IN (SELECT keyentryid FROM persistent.keyparameter WHERE tag = ?) |
| 949 | AND |
| 950 | id NOT IN ( |
| 951 | SELECT keyentryid FROM persistent.blobentry |
| 952 | WHERE id IN ( |
| 953 | SELECT blobentryid FROM persistent.blobmetadata WHERE tag = ? |
| 954 | ) |
| 955 | );", |
| 956 | params![KeyLifeCycle::Unreferenced, Tag::MAX_BOOT_LEVEL.0, BlobMetaData::MaxBootLevel], |
| 957 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 958 | .context(ks_err!("Failed to delete logical boot level keys."))?; |
David Drysdale | 8da288c | 2024-07-29 15:57:01 +0100 | [diff] [blame] | 959 | |
| 960 | // DB version is now 1. |
Janis Danisevskis | cfaf919 | 2021-05-26 16:31:02 -0700 | [diff] [blame] | 961 | Ok(1) |
| 962 | } |
| 963 | |
David Drysdale | 8da288c | 2024-07-29 15:57:01 +0100 | [diff] [blame] | 964 | // This upgrade function adds an additional `state INTEGER` column to the blobentry |
| 965 | // table, and populates it based on whether each blob is the most recent of its type for |
| 966 | // the corresponding key. |
| 967 | fn from_1_to_2(tx: &Transaction) -> Result<u32> { |
| 968 | tx.execute( |
| 969 | "ALTER TABLE persistent.blobentry ADD COLUMN state INTEGER DEFAULT 0;", |
| 970 | params![], |
| 971 | ) |
| 972 | .context(ks_err!("Failed to add state column"))?; |
| 973 | |
| 974 | // Mark keyblobs that are not the most recent for their corresponding key. |
| 975 | // This may take a while if there are excessive numbers of keys in the database. |
| 976 | let _wp = wd::watch("KeystoreDB::from_1_to_2 mark all non-current keyblobs"); |
| 977 | let sc_key_blob = SubComponentType::KEY_BLOB; |
| 978 | let mut stmt = tx |
| 979 | .prepare( |
| 980 | "UPDATE persistent.blobentry SET state=? |
| 981 | WHERE subcomponent_type = ? |
| 982 | AND id NOT IN ( |
| 983 | SELECT MAX(id) FROM persistent.blobentry |
| 984 | WHERE subcomponent_type = ? |
| 985 | GROUP BY keyentryid, subcomponent_type |
| 986 | );", |
| 987 | ) |
| 988 | .context("Trying to prepare query to mark superseded keyblobs")?; |
| 989 | stmt.execute(params![BlobState::Superseded, sc_key_blob, sc_key_blob]) |
| 990 | .context(ks_err!("Failed to set state=superseded state for keyblobs"))?; |
| 991 | log::info!("marked non-current blobentry rows for keyblobs as superseded"); |
| 992 | |
| 993 | // Mark keyblobs that don't have a corresponding key. |
| 994 | // This may take a while if there are excessive numbers of keys in the database. |
| 995 | let _wp = wd::watch("KeystoreDB::from_1_to_2 mark all orphaned keyblobs"); |
| 996 | let mut stmt = tx |
| 997 | .prepare( |
| 998 | "UPDATE persistent.blobentry SET state=? |
| 999 | WHERE subcomponent_type = ? |
| 1000 | AND NOT EXISTS (SELECT id FROM persistent.keyentry |
| 1001 | WHERE id = keyentryid);", |
| 1002 | ) |
| 1003 | .context("Trying to prepare query to mark orphaned keyblobs")?; |
| 1004 | stmt.execute(params![BlobState::Orphaned, sc_key_blob]) |
| 1005 | .context(ks_err!("Failed to set state=orphaned for keyblobs"))?; |
| 1006 | log::info!("marked orphaned blobentry rows for keyblobs"); |
| 1007 | |
| 1008 | // Add an index to make it fast to find out of date blobentry rows. |
| 1009 | let _wp = wd::watch("KeystoreDB::from_1_to_2 add blobentry index"); |
| 1010 | tx.execute( |
| 1011 | "CREATE INDEX IF NOT EXISTS persistent.blobentry_state_index |
| 1012 | ON blobentry(subcomponent_type, state);", |
| 1013 | [], |
| 1014 | ) |
| 1015 | .context("Failed to create index blobentry_state_index.")?; |
| 1016 | |
| 1017 | // Add an index to make it fast to find unreferenced keyentry rows. |
| 1018 | let _wp = wd::watch("KeystoreDB::from_1_to_2 add keyentry state index"); |
| 1019 | tx.execute( |
| 1020 | "CREATE INDEX IF NOT EXISTS persistent.keyentry_state_index |
| 1021 | ON keyentry(state);", |
| 1022 | [], |
| 1023 | ) |
| 1024 | .context("Failed to create index keyentry_state_index.")?; |
| 1025 | |
| 1026 | // DB version is now 2. |
| 1027 | Ok(2) |
| 1028 | } |
| 1029 | |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1030 | fn init_tables(tx: &Transaction) -> Result<()> { |
| 1031 | tx.execute( |
Janis Danisevskis | 4df44f4 | 2020-08-26 14:40:03 -0700 | [diff] [blame] | 1032 | "CREATE TABLE IF NOT EXISTS persistent.keyentry ( |
Joel Galenson | 0891bc1 | 2020-07-20 10:37:03 -0700 | [diff] [blame] | 1033 | id INTEGER UNIQUE, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 1034 | key_type INTEGER, |
Joel Galenson | 0891bc1 | 2020-07-20 10:37:03 -0700 | [diff] [blame] | 1035 | domain INTEGER, |
| 1036 | namespace INTEGER, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1037 | alias BLOB, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 1038 | state INTEGER, |
| 1039 | km_uuid BLOB);", |
Andrew Walbran | 78abb1e | 2023-05-30 16:20:56 +0000 | [diff] [blame] | 1040 | [], |
Janis Danisevskis | 4df44f4 | 2020-08-26 14:40:03 -0700 | [diff] [blame] | 1041 | ) |
| 1042 | .context("Failed to initialize \"keyentry\" table.")?; |
| 1043 | |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1044 | tx.execute( |
Janis Danisevskis | a543818 | 2021-02-02 14:22:59 -0800 | [diff] [blame] | 1045 | "CREATE INDEX IF NOT EXISTS persistent.keyentry_id_index |
| 1046 | ON keyentry(id);", |
Andrew Walbran | 78abb1e | 2023-05-30 16:20:56 +0000 | [diff] [blame] | 1047 | [], |
Janis Danisevskis | a543818 | 2021-02-02 14:22:59 -0800 | [diff] [blame] | 1048 | ) |
| 1049 | .context("Failed to create index keyentry_id_index.")?; |
| 1050 | |
| 1051 | tx.execute( |
| 1052 | "CREATE INDEX IF NOT EXISTS persistent.keyentry_domain_namespace_index |
| 1053 | ON keyentry(domain, namespace, alias);", |
Andrew Walbran | 78abb1e | 2023-05-30 16:20:56 +0000 | [diff] [blame] | 1054 | [], |
Janis Danisevskis | a543818 | 2021-02-02 14:22:59 -0800 | [diff] [blame] | 1055 | ) |
| 1056 | .context("Failed to create index keyentry_domain_namespace_index.")?; |
| 1057 | |
David Drysdale | 8da288c | 2024-07-29 15:57:01 +0100 | [diff] [blame] | 1058 | // Index added in v2 of database schema. |
| 1059 | tx.execute( |
| 1060 | "CREATE INDEX IF NOT EXISTS persistent.keyentry_state_index |
| 1061 | ON keyentry(state);", |
| 1062 | [], |
| 1063 | ) |
| 1064 | .context("Failed to create index keyentry_state_index.")?; |
| 1065 | |
Janis Danisevskis | a543818 | 2021-02-02 14:22:59 -0800 | [diff] [blame] | 1066 | tx.execute( |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 1067 | "CREATE TABLE IF NOT EXISTS persistent.blobentry ( |
| 1068 | id INTEGER PRIMARY KEY, |
| 1069 | subcomponent_type INTEGER, |
| 1070 | keyentryid INTEGER, |
David Drysdale | 8da288c | 2024-07-29 15:57:01 +0100 | [diff] [blame] | 1071 | blob BLOB, |
| 1072 | state INTEGER DEFAULT 0);", // `state` added in v2 of schema |
Andrew Walbran | 78abb1e | 2023-05-30 16:20:56 +0000 | [diff] [blame] | 1073 | [], |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 1074 | ) |
| 1075 | .context("Failed to initialize \"blobentry\" table.")?; |
| 1076 | |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1077 | tx.execute( |
Janis Danisevskis | a543818 | 2021-02-02 14:22:59 -0800 | [diff] [blame] | 1078 | "CREATE INDEX IF NOT EXISTS persistent.blobentry_keyentryid_index |
| 1079 | ON blobentry(keyentryid);", |
Andrew Walbran | 78abb1e | 2023-05-30 16:20:56 +0000 | [diff] [blame] | 1080 | [], |
Janis Danisevskis | a543818 | 2021-02-02 14:22:59 -0800 | [diff] [blame] | 1081 | ) |
| 1082 | .context("Failed to create index blobentry_keyentryid_index.")?; |
| 1083 | |
David Drysdale | 8da288c | 2024-07-29 15:57:01 +0100 | [diff] [blame] | 1084 | // Index added in v2 of database schema. |
| 1085 | tx.execute( |
| 1086 | "CREATE INDEX IF NOT EXISTS persistent.blobentry_state_index |
| 1087 | ON blobentry(subcomponent_type, state);", |
| 1088 | [], |
| 1089 | ) |
| 1090 | .context("Failed to create index blobentry_state_index.")?; |
| 1091 | |
Janis Danisevskis | a543818 | 2021-02-02 14:22:59 -0800 | [diff] [blame] | 1092 | tx.execute( |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1093 | "CREATE TABLE IF NOT EXISTS persistent.blobmetadata ( |
| 1094 | id INTEGER PRIMARY KEY, |
| 1095 | blobentryid INTEGER, |
| 1096 | tag INTEGER, |
| 1097 | data ANY, |
| 1098 | UNIQUE (blobentryid, tag));", |
Andrew Walbran | 78abb1e | 2023-05-30 16:20:56 +0000 | [diff] [blame] | 1099 | [], |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1100 | ) |
| 1101 | .context("Failed to initialize \"blobmetadata\" table.")?; |
| 1102 | |
| 1103 | tx.execute( |
| 1104 | "CREATE INDEX IF NOT EXISTS persistent.blobmetadata_blobentryid_index |
| 1105 | ON blobmetadata(blobentryid);", |
Andrew Walbran | 78abb1e | 2023-05-30 16:20:56 +0000 | [diff] [blame] | 1106 | [], |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1107 | ) |
| 1108 | .context("Failed to create index blobmetadata_blobentryid_index.")?; |
| 1109 | |
| 1110 | tx.execute( |
Janis Danisevskis | 4df44f4 | 2020-08-26 14:40:03 -0700 | [diff] [blame] | 1111 | "CREATE TABLE IF NOT EXISTS persistent.keyparameter ( |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 1112 | keyentryid INTEGER, |
| 1113 | tag INTEGER, |
| 1114 | data ANY, |
| 1115 | security_level INTEGER);", |
Andrew Walbran | 78abb1e | 2023-05-30 16:20:56 +0000 | [diff] [blame] | 1116 | [], |
Janis Danisevskis | 4df44f4 | 2020-08-26 14:40:03 -0700 | [diff] [blame] | 1117 | ) |
| 1118 | .context("Failed to initialize \"keyparameter\" table.")?; |
| 1119 | |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1120 | tx.execute( |
Janis Danisevskis | a543818 | 2021-02-02 14:22:59 -0800 | [diff] [blame] | 1121 | "CREATE INDEX IF NOT EXISTS persistent.keyparameter_keyentryid_index |
| 1122 | ON keyparameter(keyentryid);", |
Andrew Walbran | 78abb1e | 2023-05-30 16:20:56 +0000 | [diff] [blame] | 1123 | [], |
Janis Danisevskis | a543818 | 2021-02-02 14:22:59 -0800 | [diff] [blame] | 1124 | ) |
| 1125 | .context("Failed to create index keyparameter_keyentryid_index.")?; |
| 1126 | |
| 1127 | tx.execute( |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 1128 | "CREATE TABLE IF NOT EXISTS persistent.keymetadata ( |
| 1129 | keyentryid INTEGER, |
| 1130 | tag INTEGER, |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 1131 | data ANY, |
| 1132 | UNIQUE (keyentryid, tag));", |
Andrew Walbran | 78abb1e | 2023-05-30 16:20:56 +0000 | [diff] [blame] | 1133 | [], |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 1134 | ) |
| 1135 | .context("Failed to initialize \"keymetadata\" table.")?; |
| 1136 | |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1137 | tx.execute( |
Janis Danisevskis | a543818 | 2021-02-02 14:22:59 -0800 | [diff] [blame] | 1138 | "CREATE INDEX IF NOT EXISTS persistent.keymetadata_keyentryid_index |
| 1139 | ON keymetadata(keyentryid);", |
Andrew Walbran | 78abb1e | 2023-05-30 16:20:56 +0000 | [diff] [blame] | 1140 | [], |
Janis Danisevskis | a543818 | 2021-02-02 14:22:59 -0800 | [diff] [blame] | 1141 | ) |
| 1142 | .context("Failed to create index keymetadata_keyentryid_index.")?; |
| 1143 | |
| 1144 | tx.execute( |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 1145 | "CREATE TABLE IF NOT EXISTS persistent.grant ( |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 1146 | id INTEGER UNIQUE, |
| 1147 | grantee INTEGER, |
| 1148 | keyentryid INTEGER, |
| 1149 | access_vector INTEGER);", |
Andrew Walbran | 78abb1e | 2023-05-30 16:20:56 +0000 | [diff] [blame] | 1150 | [], |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 1151 | ) |
| 1152 | .context("Failed to initialize \"grant\" table.")?; |
| 1153 | |
Joel Galenson | 0891bc1 | 2020-07-20 10:37:03 -0700 | [diff] [blame] | 1154 | Ok(()) |
| 1155 | } |
| 1156 | |
Seth Moore | 472fcbb | 2021-05-12 10:07:51 -0700 | [diff] [blame] | 1157 | fn make_persistent_path(db_root: &Path) -> Result<String> { |
| 1158 | // Build the path to the sqlite file. |
| 1159 | let mut persistent_path = db_root.to_path_buf(); |
| 1160 | persistent_path.push(Self::PERSISTENT_DB_FILENAME); |
| 1161 | |
| 1162 | // Now convert them to strings prefixed with "file:" |
| 1163 | let mut persistent_path_str = "file:".to_owned(); |
| 1164 | persistent_path_str.push_str(&persistent_path.to_string_lossy()); |
| 1165 | |
Shaquille Johnson | 52b8c93 | 2023-12-19 19:45:32 +0000 | [diff] [blame] | 1166 | // Connect to database in specific mode |
| 1167 | let persistent_path_mode = if keystore2_flags::wal_db_journalmode_v3() { |
| 1168 | "?journal_mode=WAL".to_owned() |
| 1169 | } else { |
| 1170 | "?journal_mode=DELETE".to_owned() |
| 1171 | }; |
| 1172 | persistent_path_str.push_str(&persistent_path_mode); |
| 1173 | |
Seth Moore | 472fcbb | 2021-05-12 10:07:51 -0700 | [diff] [blame] | 1174 | Ok(persistent_path_str) |
| 1175 | } |
| 1176 | |
Matthew Maurer | d7815ca | 2021-05-06 21:58:45 -0700 | [diff] [blame] | 1177 | fn make_connection(persistent_file: &str) -> Result<Connection> { |
Janis Danisevskis | 4df44f4 | 2020-08-26 14:40:03 -0700 | [diff] [blame] | 1178 | let conn = |
| 1179 | Connection::open_in_memory().context("Failed to initialize SQLite connection.")?; |
| 1180 | |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1181 | loop { |
| 1182 | if let Err(e) = conn |
| 1183 | .execute("ATTACH DATABASE ? as persistent;", params![persistent_file]) |
| 1184 | .context("Failed to attach database persistent.") |
| 1185 | { |
| 1186 | if Self::is_locked_error(&e) { |
David Drysdale | 115c472 | 2024-04-15 14:11:52 +0100 | [diff] [blame] | 1187 | std::thread::sleep(DB_BUSY_RETRY_INTERVAL); |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1188 | continue; |
| 1189 | } else { |
| 1190 | return Err(e); |
| 1191 | } |
| 1192 | } |
| 1193 | break; |
| 1194 | } |
Janis Danisevskis | 4df44f4 | 2020-08-26 14:40:03 -0700 | [diff] [blame] | 1195 | |
Matthew Maurer | 4fb1911 | 2021-05-06 15:40:44 -0700 | [diff] [blame] | 1196 | // Drop the cache size from default (2M) to 0.5M |
| 1197 | conn.execute("PRAGMA persistent.cache_size = -500;", params![]) |
| 1198 | .context("Failed to decrease cache size for persistent db")?; |
Matthew Maurer | 4fb1911 | 2021-05-06 15:40:44 -0700 | [diff] [blame] | 1199 | |
Janis Danisevskis | 4df44f4 | 2020-08-26 14:40:03 -0700 | [diff] [blame] | 1200 | Ok(conn) |
| 1201 | } |
| 1202 | |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1203 | fn do_table_size_query( |
| 1204 | &mut self, |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1205 | storage_type: MetricsStorage, |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1206 | query: &str, |
| 1207 | params: &[&str], |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1208 | ) -> Result<StorageStats> { |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1209 | let (total, unused) = self.with_transaction(TransactionBehavior::Deferred, |tx| { |
Joel Galenson | ff79e36 | 2021-05-25 16:30:17 -0700 | [diff] [blame] | 1210 | tx.query_row(query, params_from_iter(params), |row| Ok((row.get(0)?, row.get(1)?))) |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1211 | .with_context(|| { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1212 | ks_err!("get_storage_stat: Error size of storage type {}", storage_type.0) |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1213 | }) |
| 1214 | .no_gc() |
| 1215 | })?; |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1216 | Ok(StorageStats { storage_type, size: total, unused_size: unused }) |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1219 | fn get_total_size(&mut self) -> Result<StorageStats> { |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1220 | self.do_table_size_query( |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1221 | MetricsStorage::DATABASE, |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1222 | "SELECT page_count * page_size, freelist_count * page_size |
| 1223 | FROM pragma_page_count('persistent'), |
| 1224 | pragma_page_size('persistent'), |
| 1225 | persistent.pragma_freelist_count();", |
| 1226 | &[], |
| 1227 | ) |
| 1228 | } |
| 1229 | |
| 1230 | fn get_table_size( |
| 1231 | &mut self, |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1232 | storage_type: MetricsStorage, |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1233 | schema: &str, |
| 1234 | table: &str, |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1235 | ) -> Result<StorageStats> { |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1236 | self.do_table_size_query( |
| 1237 | storage_type, |
| 1238 | "SELECT pgsize,unused FROM dbstat(?1) |
| 1239 | WHERE name=?2 AND aggregate=TRUE;", |
| 1240 | &[schema, table], |
| 1241 | ) |
| 1242 | } |
| 1243 | |
David Drysdale | 0fefae3 | 2024-09-16 13:32:27 +0100 | [diff] [blame] | 1244 | /// Fetches a storage statistics atom for a given storage type. For storage |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1245 | /// types that map to a table, information about the table's storage is |
| 1246 | /// returned. Requests for storage types that are not DB tables return None. |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1247 | pub fn get_storage_stat(&mut self, storage_type: MetricsStorage) -> Result<StorageStats> { |
David Drysdale | 703bcc1 | 2024-11-26 14:15:03 +0000 | [diff] [blame] | 1248 | let _wp = wd::watch_millis_with("KeystoreDB::get_storage_stat", 500, storage_type); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 1249 | |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1250 | match storage_type { |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1251 | MetricsStorage::DATABASE => self.get_total_size(), |
| 1252 | MetricsStorage::KEY_ENTRY => { |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1253 | self.get_table_size(storage_type, "persistent", "keyentry") |
| 1254 | } |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1255 | MetricsStorage::KEY_ENTRY_ID_INDEX => { |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1256 | self.get_table_size(storage_type, "persistent", "keyentry_id_index") |
| 1257 | } |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1258 | MetricsStorage::KEY_ENTRY_DOMAIN_NAMESPACE_INDEX => { |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1259 | self.get_table_size(storage_type, "persistent", "keyentry_domain_namespace_index") |
| 1260 | } |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1261 | MetricsStorage::BLOB_ENTRY => { |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1262 | self.get_table_size(storage_type, "persistent", "blobentry") |
| 1263 | } |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1264 | MetricsStorage::BLOB_ENTRY_KEY_ENTRY_ID_INDEX => { |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1265 | self.get_table_size(storage_type, "persistent", "blobentry_keyentryid_index") |
| 1266 | } |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1267 | MetricsStorage::KEY_PARAMETER => { |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1268 | self.get_table_size(storage_type, "persistent", "keyparameter") |
| 1269 | } |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1270 | MetricsStorage::KEY_PARAMETER_KEY_ENTRY_ID_INDEX => { |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1271 | self.get_table_size(storage_type, "persistent", "keyparameter_keyentryid_index") |
| 1272 | } |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1273 | MetricsStorage::KEY_METADATA => { |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1274 | self.get_table_size(storage_type, "persistent", "keymetadata") |
| 1275 | } |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1276 | MetricsStorage::KEY_METADATA_KEY_ENTRY_ID_INDEX => { |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1277 | self.get_table_size(storage_type, "persistent", "keymetadata_keyentryid_index") |
| 1278 | } |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1279 | MetricsStorage::GRANT => self.get_table_size(storage_type, "persistent", "grant"), |
| 1280 | MetricsStorage::AUTH_TOKEN => { |
Matthew Maurer | d7815ca | 2021-05-06 21:58:45 -0700 | [diff] [blame] | 1281 | // Since the table is actually a BTreeMap now, unused_size is not meaningfully |
| 1282 | // reportable |
| 1283 | // Size provided is only an approximation |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1284 | Ok(StorageStats { |
Matthew Maurer | d7815ca | 2021-05-06 21:58:45 -0700 | [diff] [blame] | 1285 | storage_type, |
| 1286 | size: (self.perboot.auth_tokens_len() * std::mem::size_of::<AuthTokenEntry>()) |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1287 | as i32, |
Matthew Maurer | d7815ca | 2021-05-06 21:58:45 -0700 | [diff] [blame] | 1288 | unused_size: 0, |
| 1289 | }) |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1290 | } |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1291 | MetricsStorage::BLOB_METADATA => { |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1292 | self.get_table_size(storage_type, "persistent", "blobmetadata") |
| 1293 | } |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1294 | MetricsStorage::BLOB_METADATA_BLOB_ENTRY_ID_INDEX => { |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1295 | self.get_table_size(storage_type, "persistent", "blobmetadata_blobentryid_index") |
| 1296 | } |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 1297 | _ => Err(anyhow::Error::msg(format!("Unsupported storage type: {}", storage_type.0))), |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 1298 | } |
| 1299 | } |
| 1300 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1301 | /// This function is intended to be used by the garbage collector. |
Janis Danisevskis | 3395f86 | 2021-05-06 10:54:17 -0700 | [diff] [blame] | 1302 | /// It deletes the blobs given by `blob_ids_to_delete`. It then tries to find up to `max_blobs` |
| 1303 | /// superseded key blobs that might need special handling by the garbage collector. |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1304 | /// If no further superseded blobs can be found it deletes all other superseded blobs that don't |
| 1305 | /// need special handling and returns None. |
Janis Danisevskis | 3395f86 | 2021-05-06 10:54:17 -0700 | [diff] [blame] | 1306 | pub fn handle_next_superseded_blobs( |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1307 | &mut self, |
Janis Danisevskis | 3395f86 | 2021-05-06 10:54:17 -0700 | [diff] [blame] | 1308 | blob_ids_to_delete: &[i64], |
| 1309 | max_blobs: usize, |
David Drysdale | f1ba381 | 2024-05-08 17:50:13 +0100 | [diff] [blame] | 1310 | ) -> Result<Vec<SupersededBlob>> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1311 | let _wp = wd::watch("KeystoreDB::handle_next_superseded_blob"); |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 1312 | self.with_transaction(Immediate("TX_handle_next_superseded_blob"), |tx| { |
Janis Danisevskis | 3395f86 | 2021-05-06 10:54:17 -0700 | [diff] [blame] | 1313 | // Delete the given blobs. |
| 1314 | for blob_id in blob_ids_to_delete { |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1315 | tx.execute( |
| 1316 | "DELETE FROM persistent.blobmetadata WHERE blobentryid = ?;", |
Janis Danisevskis | 3395f86 | 2021-05-06 10:54:17 -0700 | [diff] [blame] | 1317 | params![blob_id], |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1318 | ) |
Shaquille Johnson | f23fc94 | 2024-02-13 17:01:29 +0000 | [diff] [blame] | 1319 | .context(ks_err!("Trying to delete blob metadata: {:?}", blob_id))?; |
Janis Danisevskis | 3395f86 | 2021-05-06 10:54:17 -0700 | [diff] [blame] | 1320 | tx.execute("DELETE FROM persistent.blobentry WHERE id = ?;", params![blob_id]) |
Shaquille Johnson | f23fc94 | 2024-02-13 17:01:29 +0000 | [diff] [blame] | 1321 | .context(ks_err!("Trying to delete blob: {:?}", blob_id))?; |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1322 | } |
Janis Danisevskis | 3cba10d | 2021-05-06 17:02:19 -0700 | [diff] [blame] | 1323 | |
| 1324 | Self::cleanup_unreferenced(tx).context("Trying to cleanup unreferenced.")?; |
| 1325 | |
David Drysdale | 8da288c | 2024-07-29 15:57:01 +0100 | [diff] [blame] | 1326 | // Find up to `max_blobs` more out-of-date key blobs, load their metadata and return it. |
| 1327 | let result: Vec<(i64, Vec<u8>)> = if keystore2_flags::use_blob_state_column() { |
| 1328 | let _wp = wd::watch("KeystoreDB::handle_next_superseded_blob find_next v2"); |
| 1329 | let mut stmt = tx |
| 1330 | .prepare( |
| 1331 | "SELECT id, blob FROM persistent.blobentry |
| 1332 | WHERE subcomponent_type = ? AND state != ? |
| 1333 | LIMIT ?;", |
| 1334 | ) |
| 1335 | .context("Trying to prepare query for superseded blobs.")?; |
| 1336 | |
| 1337 | let rows = stmt |
| 1338 | .query_map( |
| 1339 | params![SubComponentType::KEY_BLOB, BlobState::Current, max_blobs as i64], |
| 1340 | |row| Ok((row.get(0)?, row.get(1)?)), |
| 1341 | ) |
| 1342 | .context("Trying to query superseded blob.")?; |
| 1343 | |
| 1344 | rows.collect::<Result<Vec<(i64, Vec<u8>)>, rusqlite::Error>>() |
| 1345 | .context("Trying to extract superseded blobs.")? |
| 1346 | } else { |
| 1347 | let _wp = wd::watch("KeystoreDB::handle_next_superseded_blob find_next v1"); |
Janis Danisevskis | 3395f86 | 2021-05-06 10:54:17 -0700 | [diff] [blame] | 1348 | let mut stmt = tx |
| 1349 | .prepare( |
| 1350 | "SELECT id, blob FROM persistent.blobentry |
| 1351 | WHERE subcomponent_type = ? |
| 1352 | AND ( |
| 1353 | id NOT IN ( |
| 1354 | SELECT MAX(id) FROM persistent.blobentry |
| 1355 | WHERE subcomponent_type = ? |
| 1356 | GROUP BY keyentryid, subcomponent_type |
| 1357 | ) |
| 1358 | OR keyentryid NOT IN (SELECT id FROM persistent.keyentry) |
| 1359 | ) LIMIT ?;", |
| 1360 | ) |
| 1361 | .context("Trying to prepare query for superseded blobs.")?; |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1362 | |
Janis Danisevskis | 3395f86 | 2021-05-06 10:54:17 -0700 | [diff] [blame] | 1363 | let rows = stmt |
| 1364 | .query_map( |
| 1365 | params![ |
| 1366 | SubComponentType::KEY_BLOB, |
| 1367 | SubComponentType::KEY_BLOB, |
| 1368 | max_blobs as i64, |
| 1369 | ], |
| 1370 | |row| Ok((row.get(0)?, row.get(1)?)), |
| 1371 | ) |
| 1372 | .context("Trying to query superseded blob.")?; |
| 1373 | |
| 1374 | rows.collect::<Result<Vec<(i64, Vec<u8>)>, rusqlite::Error>>() |
| 1375 | .context("Trying to extract superseded blobs.")? |
| 1376 | }; |
| 1377 | |
David Drysdale | c652f6c | 2024-07-18 13:01:23 +0100 | [diff] [blame] | 1378 | let _wp = wd::watch("KeystoreDB::handle_next_superseded_blob load_metadata"); |
Janis Danisevskis | 3395f86 | 2021-05-06 10:54:17 -0700 | [diff] [blame] | 1379 | let result = result |
| 1380 | .into_iter() |
| 1381 | .map(|(blob_id, blob)| { |
David Drysdale | f1ba381 | 2024-05-08 17:50:13 +0100 | [diff] [blame] | 1382 | Ok(SupersededBlob { |
| 1383 | blob_id, |
| 1384 | blob, |
| 1385 | metadata: BlobMetaData::load_from_db(blob_id, tx)?, |
| 1386 | }) |
Janis Danisevskis | 3395f86 | 2021-05-06 10:54:17 -0700 | [diff] [blame] | 1387 | }) |
David Drysdale | f1ba381 | 2024-05-08 17:50:13 +0100 | [diff] [blame] | 1388 | .collect::<Result<Vec<_>>>() |
Janis Danisevskis | 3395f86 | 2021-05-06 10:54:17 -0700 | [diff] [blame] | 1389 | .context("Trying to load blob metadata.")?; |
| 1390 | if !result.is_empty() { |
| 1391 | return Ok(result).no_gc(); |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1392 | } |
| 1393 | |
David Drysdale | 8da288c | 2024-07-29 15:57:01 +0100 | [diff] [blame] | 1394 | // We did not find any out-of-date key blobs, so let's remove other types of superseded |
| 1395 | // blob in one transaction. |
| 1396 | if keystore2_flags::use_blob_state_column() { |
| 1397 | let _wp = wd::watch("KeystoreDB::handle_next_superseded_blob delete v2"); |
| 1398 | tx.execute( |
| 1399 | "DELETE FROM persistent.blobentry |
| 1400 | WHERE subcomponent_type != ? AND state != ?;", |
| 1401 | params![SubComponentType::KEY_BLOB, BlobState::Current], |
| 1402 | ) |
| 1403 | .context("Trying to purge out-of-date blobs (other than keyblobs)")?; |
| 1404 | } else { |
| 1405 | let _wp = wd::watch("KeystoreDB::handle_next_superseded_blob delete v1"); |
| 1406 | tx.execute( |
| 1407 | "DELETE FROM persistent.blobentry |
| 1408 | WHERE NOT subcomponent_type = ? |
| 1409 | AND ( |
| 1410 | id NOT IN ( |
| 1411 | SELECT MAX(id) FROM persistent.blobentry |
| 1412 | WHERE NOT subcomponent_type = ? |
| 1413 | GROUP BY keyentryid, subcomponent_type |
| 1414 | ) OR keyentryid NOT IN (SELECT id FROM persistent.keyentry) |
| 1415 | );", |
| 1416 | params![SubComponentType::KEY_BLOB, SubComponentType::KEY_BLOB], |
| 1417 | ) |
| 1418 | .context("Trying to purge superseded blobs.")?; |
| 1419 | } |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1420 | |
Janis Danisevskis | 3395f86 | 2021-05-06 10:54:17 -0700 | [diff] [blame] | 1421 | Ok(vec![]).no_gc() |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1422 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1423 | .context(ks_err!()) |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | /// This maintenance function should be called only once before the database is used for the |
| 1427 | /// first time. It restores the invariant that `KeyLifeCycle::Existing` is a transient state. |
| 1428 | /// The function transitions all key entries from Existing to Unreferenced unconditionally and |
| 1429 | /// returns the number of rows affected. If this returns a value greater than 0, it means that |
| 1430 | /// Keystore crashed at some point during key generation. Callers may want to log such |
| 1431 | /// occurrences. |
| 1432 | /// Unlike with `mark_unreferenced`, we don't need to purge grants, because only keys that made |
| 1433 | /// it to `KeyLifeCycle::Live` may have grants. |
| 1434 | pub fn cleanup_leftovers(&mut self) -> Result<usize> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1435 | let _wp = wd::watch("KeystoreDB::cleanup_leftovers"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 1436 | |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 1437 | self.with_transaction(Immediate("TX_cleanup_leftovers"), |tx| { |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1438 | tx.execute( |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1439 | "UPDATE persistent.keyentry SET state = ? WHERE state = ?;", |
| 1440 | params![KeyLifeCycle::Unreferenced, KeyLifeCycle::Existing], |
| 1441 | ) |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1442 | .context("Failed to execute query.") |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1443 | .need_gc() |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1444 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1445 | .context(ks_err!()) |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1446 | } |
| 1447 | |
Hasini Gunasinghe | 0e16145 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 1448 | /// Checks if a key exists with given key type and key descriptor properties. |
| 1449 | pub fn key_exists( |
| 1450 | &mut self, |
| 1451 | domain: Domain, |
| 1452 | nspace: i64, |
| 1453 | alias: &str, |
| 1454 | key_type: KeyType, |
| 1455 | ) -> Result<bool> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1456 | let _wp = wd::watch("KeystoreDB::key_exists"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 1457 | |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 1458 | self.with_transaction(Immediate("TX_key_exists"), |tx| { |
Hasini Gunasinghe | 0e16145 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 1459 | let key_descriptor = |
| 1460 | KeyDescriptor { domain, nspace, alias: Some(alias.to_string()), blob: None }; |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 1461 | let result = Self::load_key_entry_id(tx, &key_descriptor, key_type); |
Hasini Gunasinghe | 0e16145 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 1462 | match result { |
| 1463 | Ok(_) => Ok(true), |
| 1464 | Err(error) => match error.root_cause().downcast_ref::<KsError>() { |
| 1465 | Some(KsError::Rc(ResponseCode::KEY_NOT_FOUND)) => Ok(false), |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1466 | _ => Err(error).context(ks_err!("Failed to find if the key exists.")), |
Hasini Gunasinghe | 0e16145 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 1467 | }, |
| 1468 | } |
| 1469 | .no_gc() |
| 1470 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1471 | .context(ks_err!()) |
Hasini Gunasinghe | 0e16145 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 1474 | /// Stores a super key in the database. |
| 1475 | pub fn store_super_key( |
| 1476 | &mut self, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 1477 | user_id: u32, |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 1478 | key_type: &SuperKeyType, |
| 1479 | blob: &[u8], |
| 1480 | blob_metadata: &BlobMetaData, |
Paul Crowley | 8d5b253 | 2021-03-19 10:53:07 -0700 | [diff] [blame] | 1481 | key_metadata: &KeyMetaData, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 1482 | ) -> Result<KeyEntry> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1483 | let _wp = wd::watch("KeystoreDB::store_super_key"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 1484 | |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 1485 | self.with_transaction(Immediate("TX_store_super_key"), |tx| { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 1486 | let key_id = Self::insert_with_retry(|id| { |
| 1487 | tx.execute( |
| 1488 | "INSERT into persistent.keyentry |
| 1489 | (id, key_type, domain, namespace, alias, state, km_uuid) |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 1490 | VALUES(?, ?, ?, ?, ?, ?, ?);", |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 1491 | params![ |
| 1492 | id, |
| 1493 | KeyType::Super, |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 1494 | Domain::APP.0, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 1495 | user_id as i64, |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 1496 | key_type.alias, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 1497 | KeyLifeCycle::Live, |
| 1498 | &KEYSTORE_UUID, |
| 1499 | ], |
| 1500 | ) |
| 1501 | }) |
| 1502 | .context("Failed to insert into keyentry table.")?; |
| 1503 | |
Paul Crowley | 8d5b253 | 2021-03-19 10:53:07 -0700 | [diff] [blame] | 1504 | key_metadata.store_in_db(key_id, tx).context("KeyMetaData::store_in_db failed")?; |
| 1505 | |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 1506 | Self::set_blob_internal( |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 1507 | tx, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 1508 | key_id, |
| 1509 | SubComponentType::KEY_BLOB, |
| 1510 | Some(blob), |
| 1511 | Some(blob_metadata), |
| 1512 | ) |
| 1513 | .context("Failed to store key blob.")?; |
| 1514 | |
| 1515 | Self::load_key_components(tx, KeyEntryLoadBits::KM, key_id) |
| 1516 | .context("Trying to load key components.") |
| 1517 | .no_gc() |
| 1518 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1519 | .context(ks_err!()) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 1522 | /// Loads super key of a given user, if exists |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 1523 | pub fn load_super_key( |
| 1524 | &mut self, |
| 1525 | key_type: &SuperKeyType, |
| 1526 | user_id: u32, |
| 1527 | ) -> Result<Option<(KeyIdGuard, KeyEntry)>> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1528 | let _wp = wd::watch("KeystoreDB::load_super_key"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 1529 | |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 1530 | self.with_transaction(Immediate("TX_load_super_key"), |tx| { |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 1531 | let key_descriptor = KeyDescriptor { |
| 1532 | domain: Domain::APP, |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 1533 | nspace: user_id as i64, |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 1534 | alias: Some(key_type.alias.into()), |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 1535 | blob: None, |
| 1536 | }; |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 1537 | let id = Self::load_key_entry_id(tx, &key_descriptor, KeyType::Super); |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 1538 | match id { |
| 1539 | Ok(id) => { |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 1540 | let key_entry = Self::load_key_components(tx, KeyEntryLoadBits::KM, id) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1541 | .context(ks_err!("Failed to load key entry."))?; |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 1542 | Ok(Some((KEY_ID_LOCK.get(id), key_entry))) |
| 1543 | } |
| 1544 | Err(error) => match error.root_cause().downcast_ref::<KsError>() { |
| 1545 | Some(KsError::Rc(ResponseCode::KEY_NOT_FOUND)) => Ok(None), |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1546 | _ => Err(error).context(ks_err!()), |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 1547 | }, |
| 1548 | } |
| 1549 | .no_gc() |
| 1550 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1551 | .context(ks_err!()) |
Hasini Gunasinghe | 731e3c8 | 2021-02-06 00:56:28 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1554 | /// Creates a transaction with the given behavior and executes f with the new transaction. |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1555 | /// The transaction is committed only if f returns Ok and retried if DatabaseBusy |
| 1556 | /// or DatabaseLocked is encountered. |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1557 | fn with_transaction<T, F>(&mut self, behavior: TransactionBehavior, f: F) -> Result<T> |
| 1558 | where |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1559 | F: Fn(&Transaction) -> Result<(bool, T)>, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1560 | { |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 1561 | let name = behavior.name(); |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1562 | loop { |
James Farrell | efe1a2f | 2024-02-28 21:36:47 +0000 | [diff] [blame] | 1563 | let result = self |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1564 | .conn |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 1565 | .transaction_with_behavior(behavior.into()) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1566 | .context(ks_err!()) |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 1567 | .and_then(|tx| { |
| 1568 | let _wp = name.map(wd::watch); |
| 1569 | f(&tx).map(|result| (result, tx)) |
| 1570 | }) |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1571 | .and_then(|(result, tx)| { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1572 | tx.commit().context(ks_err!("Failed to commit transaction."))?; |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1573 | Ok(result) |
James Farrell | efe1a2f | 2024-02-28 21:36:47 +0000 | [diff] [blame] | 1574 | }); |
| 1575 | match result { |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1576 | Ok(result) => break Ok(result), |
| 1577 | Err(e) => { |
| 1578 | if Self::is_locked_error(&e) { |
David Drysdale | 115c472 | 2024-04-15 14:11:52 +0100 | [diff] [blame] | 1579 | std::thread::sleep(DB_BUSY_RETRY_INTERVAL); |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1580 | continue; |
| 1581 | } else { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1582 | return Err(e).context(ks_err!()); |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1583 | } |
| 1584 | } |
| 1585 | } |
| 1586 | } |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1587 | .map(|(need_gc, result)| { |
| 1588 | if need_gc { |
| 1589 | if let Some(ref gc) = self.gc { |
| 1590 | gc.notify_gc(); |
| 1591 | } |
| 1592 | } |
| 1593 | result |
| 1594 | }) |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | fn is_locked_error(e: &anyhow::Error) -> bool { |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame] | 1598 | matches!( |
| 1599 | e.root_cause().downcast_ref::<rusqlite::ffi::Error>(), |
| 1600 | Some(rusqlite::ffi::Error { code: rusqlite::ErrorCode::DatabaseBusy, .. }) |
| 1601 | | Some(rusqlite::ffi::Error { code: rusqlite::ErrorCode::DatabaseLocked, .. }) |
| 1602 | ) |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1603 | } |
| 1604 | |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1605 | fn create_key_entry_internal( |
| 1606 | tx: &Transaction, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1607 | domain: &Domain, |
| 1608 | namespace: &i64, |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 1609 | key_type: KeyType, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 1610 | km_uuid: &Uuid, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1611 | ) -> Result<KeyIdGuard> { |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1612 | match *domain { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 1613 | Domain::APP | Domain::SELINUX => {} |
Joel Galenson | 0891bc1 | 2020-07-20 10:37:03 -0700 | [diff] [blame] | 1614 | _ => { |
| 1615 | return Err(KsError::sys()) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1616 | .context(ks_err!("Domain {:?} must be either App or SELinux.", domain)); |
Joel Galenson | 0891bc1 | 2020-07-20 10:37:03 -0700 | [diff] [blame] | 1617 | } |
| 1618 | } |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 1619 | Ok(KEY_ID_LOCK.get( |
| 1620 | Self::insert_with_retry(|id| { |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1621 | tx.execute( |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 1622 | "INSERT into persistent.keyentry |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 1623 | (id, key_type, domain, namespace, alias, state, km_uuid) |
| 1624 | VALUES(?, ?, ?, ?, NULL, ?, ?);", |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1625 | params![ |
| 1626 | id, |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 1627 | key_type, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1628 | domain.0 as u32, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1629 | *namespace, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 1630 | KeyLifeCycle::Existing, |
| 1631 | km_uuid, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1632 | ], |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 1633 | ) |
| 1634 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1635 | .context(ks_err!())?, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 1636 | )) |
Joel Galenson | 26f4d01 | 2020-07-17 14:57:21 -0700 | [diff] [blame] | 1637 | } |
Joel Galenson | 33c04ad | 2020-08-03 11:04:38 -0700 | [diff] [blame] | 1638 | |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 1639 | /// Set a new blob and associates it with the given key id. Each blob |
| 1640 | /// has a sub component type. |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 1641 | /// Each key can have one of each sub component type associated. If more |
| 1642 | /// are added only the most recent can be retrieved, and superseded blobs |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 1643 | /// will get garbage collected. |
| 1644 | /// Components SubComponentType::CERT and SubComponentType::CERT_CHAIN can be |
| 1645 | /// removed by setting blob to None. |
| 1646 | pub fn set_blob( |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 1647 | &mut self, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 1648 | key_id: &KeyIdGuard, |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 1649 | sc_type: SubComponentType, |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 1650 | blob: Option<&[u8]>, |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1651 | blob_metadata: Option<&BlobMetaData>, |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 1652 | ) -> Result<()> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1653 | let _wp = wd::watch("KeystoreDB::set_blob"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 1654 | |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 1655 | self.with_transaction(Immediate("TX_set_blob"), |tx| { |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 1656 | Self::set_blob_internal(tx, key_id.0, sc_type, blob, blob_metadata).need_gc() |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1657 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1658 | .context(ks_err!()) |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 1659 | } |
| 1660 | |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 1661 | /// Why would we insert a deleted blob? This weird function is for the purpose of legacy |
| 1662 | /// key migration in the case where we bulk delete all the keys of an app or even a user. |
| 1663 | /// We use this to insert key blobs into the database which can then be garbage collected |
| 1664 | /// lazily by the key garbage collector. |
| 1665 | pub fn set_deleted_blob(&mut self, blob: &[u8], blob_metadata: &BlobMetaData) -> Result<()> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1666 | let _wp = wd::watch("KeystoreDB::set_deleted_blob"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 1667 | |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 1668 | self.with_transaction(Immediate("TX_set_deleted_blob"), |tx| { |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 1669 | Self::set_blob_internal( |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 1670 | tx, |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 1671 | Self::UNASSIGNED_KEY_ID, |
| 1672 | SubComponentType::KEY_BLOB, |
| 1673 | Some(blob), |
| 1674 | Some(blob_metadata), |
| 1675 | ) |
| 1676 | .need_gc() |
| 1677 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1678 | .context(ks_err!()) |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 1679 | } |
| 1680 | |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 1681 | fn set_blob_internal( |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 1682 | tx: &Transaction, |
| 1683 | key_id: i64, |
| 1684 | sc_type: SubComponentType, |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 1685 | blob: Option<&[u8]>, |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1686 | blob_metadata: Option<&BlobMetaData>, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 1687 | ) -> Result<()> { |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 1688 | match (blob, sc_type) { |
| 1689 | (Some(blob), _) => { |
David Drysdale | 8da288c | 2024-07-29 15:57:01 +0100 | [diff] [blame] | 1690 | // Mark any previous blobentry(s) of the same type for the same key as superseded. |
| 1691 | tx.execute( |
| 1692 | "UPDATE persistent.blobentry SET state = ? |
| 1693 | WHERE keyentryid = ? AND subcomponent_type = ?", |
| 1694 | params![BlobState::Superseded, key_id, sc_type], |
| 1695 | ) |
| 1696 | .context(ks_err!( |
| 1697 | "Failed to mark prior {sc_type:?} blobentrys for {key_id} as superseded" |
| 1698 | ))?; |
| 1699 | |
| 1700 | // Now insert the new, un-superseded, blob. (If this fails, the marking of |
| 1701 | // old blobs as superseded will be rolled back, because we're inside a |
| 1702 | // transaction.) |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 1703 | tx.execute( |
| 1704 | "INSERT INTO persistent.blobentry |
| 1705 | (subcomponent_type, keyentryid, blob) VALUES (?, ?, ?);", |
| 1706 | params![sc_type, key_id, blob], |
| 1707 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1708 | .context(ks_err!("Failed to insert blob."))?; |
David Drysdale | 8da288c | 2024-07-29 15:57:01 +0100 | [diff] [blame] | 1709 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1710 | if let Some(blob_metadata) = blob_metadata { |
| 1711 | let blob_id = tx |
Andrew Walbran | 78abb1e | 2023-05-30 16:20:56 +0000 | [diff] [blame] | 1712 | .query_row("SELECT MAX(id) FROM persistent.blobentry;", [], |row| { |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1713 | row.get(0) |
| 1714 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1715 | .context(ks_err!("Failed to get new blob id."))?; |
David Drysdale | 8da288c | 2024-07-29 15:57:01 +0100 | [diff] [blame] | 1716 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1717 | blob_metadata |
| 1718 | .store_in_db(blob_id, tx) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1719 | .context(ks_err!("Trying to store blob metadata."))?; |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1720 | } |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 1721 | } |
| 1722 | (None, SubComponentType::CERT) | (None, SubComponentType::CERT_CHAIN) => { |
| 1723 | tx.execute( |
| 1724 | "DELETE FROM persistent.blobentry |
| 1725 | WHERE subcomponent_type = ? AND keyentryid = ?;", |
| 1726 | params![sc_type, key_id], |
| 1727 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1728 | .context(ks_err!("Failed to delete blob."))?; |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 1729 | } |
| 1730 | (None, _) => { |
| 1731 | return Err(KsError::sys()) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1732 | .context(ks_err!("Other blobs cannot be deleted in this way.")); |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 1733 | } |
| 1734 | } |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 1735 | Ok(()) |
| 1736 | } |
| 1737 | |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 1738 | /// Inserts a collection of key parameters into the `persistent.keyparameter` table |
| 1739 | /// and associates them with the given `key_id`. |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1740 | #[cfg(test)] |
| 1741 | fn insert_keyparameter(&mut self, key_id: &KeyIdGuard, params: &[KeyParameter]) -> Result<()> { |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 1742 | self.with_transaction(Immediate("TX_insert_keyparameter"), |tx| { |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1743 | Self::insert_keyparameter_internal(tx, key_id, params).no_gc() |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1744 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1745 | .context(ks_err!()) |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1746 | } |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 1747 | |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1748 | fn insert_keyparameter_internal( |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1749 | tx: &Transaction, |
| 1750 | key_id: &KeyIdGuard, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1751 | params: &[KeyParameter], |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1752 | ) -> Result<()> { |
| 1753 | let mut stmt = tx |
| 1754 | .prepare( |
| 1755 | "INSERT into persistent.keyparameter (keyentryid, tag, data, security_level) |
| 1756 | VALUES (?, ?, ?, ?);", |
| 1757 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1758 | .context(ks_err!("Failed to prepare statement."))?; |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1759 | |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1760 | for p in params.iter() { |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1761 | stmt.insert(params![ |
| 1762 | key_id.0, |
| 1763 | p.get_tag().0, |
| 1764 | p.key_parameter_value(), |
| 1765 | p.security_level().0 |
| 1766 | ]) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1767 | .with_context(|| ks_err!("Failed to insert {:?}", p))?; |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 1768 | } |
| 1769 | Ok(()) |
| 1770 | } |
| 1771 | |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 1772 | /// Insert a set of key entry specific metadata into the database. |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1773 | #[cfg(test)] |
| 1774 | fn insert_key_metadata(&mut self, key_id: &KeyIdGuard, metadata: &KeyMetaData) -> Result<()> { |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 1775 | self.with_transaction(Immediate("TX_insert_key_metadata"), |tx| { |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 1776 | metadata.store_in_db(key_id.0, tx).no_gc() |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1777 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1778 | .context(ks_err!()) |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1779 | } |
| 1780 | |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 1781 | /// Updates the alias column of the given key id `newid` with the given alias, |
| 1782 | /// and atomically, removes the alias, domain, and namespace from another row |
| 1783 | /// with the same alias-domain-namespace tuple if such row exits. |
Janis Danisevskis | 4507f3b | 2021-01-13 16:34:39 -0800 | [diff] [blame] | 1784 | /// Returns Ok(true) if an old key was marked unreferenced as a hint to the garbage |
| 1785 | /// collector. |
| 1786 | fn rebind_alias( |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1787 | tx: &Transaction, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 1788 | newid: &KeyIdGuard, |
Joel Galenson | 33c04ad | 2020-08-03 11:04:38 -0700 | [diff] [blame] | 1789 | alias: &str, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1790 | domain: &Domain, |
| 1791 | namespace: &i64, |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 1792 | key_type: KeyType, |
Janis Danisevskis | 4507f3b | 2021-01-13 16:34:39 -0800 | [diff] [blame] | 1793 | ) -> Result<bool> { |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1794 | match *domain { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 1795 | Domain::APP | Domain::SELINUX => {} |
Joel Galenson | 33c04ad | 2020-08-03 11:04:38 -0700 | [diff] [blame] | 1796 | _ => { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1797 | return Err(KsError::sys()) |
| 1798 | .context(ks_err!("Domain {:?} must be either App or SELinux.", domain)); |
Joel Galenson | 33c04ad | 2020-08-03 11:04:38 -0700 | [diff] [blame] | 1799 | } |
| 1800 | } |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1801 | let updated = tx |
| 1802 | .execute( |
| 1803 | "UPDATE persistent.keyentry |
| 1804 | SET alias = NULL, domain = NULL, namespace = NULL, state = ? |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 1805 | WHERE alias = ? AND domain = ? AND namespace = ? AND key_type = ?;", |
| 1806 | params![KeyLifeCycle::Unreferenced, alias, domain.0 as u32, namespace, key_type], |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1807 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1808 | .context(ks_err!("Failed to rebind existing entry."))?; |
Joel Galenson | 33c04ad | 2020-08-03 11:04:38 -0700 | [diff] [blame] | 1809 | let result = tx |
| 1810 | .execute( |
| 1811 | "UPDATE persistent.keyentry |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1812 | SET alias = ?, state = ? |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 1813 | WHERE id = ? AND domain = ? AND namespace = ? AND state = ? AND key_type = ?;", |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1814 | params![ |
| 1815 | alias, |
| 1816 | KeyLifeCycle::Live, |
| 1817 | newid.0, |
| 1818 | domain.0 as u32, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1819 | *namespace, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 1820 | KeyLifeCycle::Existing, |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 1821 | key_type, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1822 | ], |
Joel Galenson | 33c04ad | 2020-08-03 11:04:38 -0700 | [diff] [blame] | 1823 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1824 | .context(ks_err!("Failed to set alias."))?; |
Joel Galenson | 33c04ad | 2020-08-03 11:04:38 -0700 | [diff] [blame] | 1825 | if result != 1 { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1826 | return Err(KsError::sys()).context(ks_err!( |
| 1827 | "Expected to update a single entry but instead updated {}.", |
Joel Galenson | 33c04ad | 2020-08-03 11:04:38 -0700 | [diff] [blame] | 1828 | result |
| 1829 | )); |
| 1830 | } |
Janis Danisevskis | 4507f3b | 2021-01-13 16:34:39 -0800 | [diff] [blame] | 1831 | Ok(updated != 0) |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1832 | } |
| 1833 | |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 1834 | /// Moves the key given by KeyIdGuard to the new location at `destination`. If the destination |
| 1835 | /// is already occupied by a key, this function fails with `ResponseCode::INVALID_ARGUMENT`. |
| 1836 | pub fn migrate_key_namespace( |
| 1837 | &mut self, |
| 1838 | key_id_guard: KeyIdGuard, |
| 1839 | destination: &KeyDescriptor, |
| 1840 | caller_uid: u32, |
| 1841 | check_permission: impl Fn(&KeyDescriptor) -> Result<()>, |
| 1842 | ) -> Result<()> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1843 | let _wp = wd::watch("KeystoreDB::migrate_key_namespace"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 1844 | |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 1845 | let destination = match destination.domain { |
| 1846 | Domain::APP => KeyDescriptor { nspace: caller_uid as i64, ..(*destination).clone() }, |
| 1847 | Domain::SELINUX => (*destination).clone(), |
| 1848 | domain => { |
| 1849 | return Err(KsError::Rc(ResponseCode::INVALID_ARGUMENT)) |
| 1850 | .context(format!("Domain {:?} must be either APP or SELINUX.", domain)); |
| 1851 | } |
| 1852 | }; |
| 1853 | |
| 1854 | // Security critical: Must return immediately on failure. Do not remove the '?'; |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1855 | check_permission(&destination).context(ks_err!("Trying to check permission."))?; |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 1856 | |
| 1857 | let alias = destination |
| 1858 | .alias |
| 1859 | .as_ref() |
| 1860 | .ok_or(KsError::Rc(ResponseCode::INVALID_ARGUMENT)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1861 | .context(ks_err!("Alias must be specified."))?; |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 1862 | |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 1863 | self.with_transaction(Immediate("TX_migrate_key_namespace"), |tx| { |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 1864 | // Query the destination location. If there is a key, the migration request fails. |
| 1865 | if tx |
| 1866 | .query_row( |
| 1867 | "SELECT id FROM persistent.keyentry |
| 1868 | WHERE alias = ? AND domain = ? AND namespace = ?;", |
| 1869 | params![alias, destination.domain.0, destination.nspace], |
| 1870 | |_| Ok(()), |
| 1871 | ) |
| 1872 | .optional() |
| 1873 | .context("Failed to query destination.")? |
| 1874 | .is_some() |
| 1875 | { |
| 1876 | return Err(KsError::Rc(ResponseCode::INVALID_ARGUMENT)) |
| 1877 | .context("Target already exists."); |
| 1878 | } |
| 1879 | |
| 1880 | let updated = tx |
| 1881 | .execute( |
| 1882 | "UPDATE persistent.keyentry |
| 1883 | SET alias = ?, domain = ?, namespace = ? |
| 1884 | WHERE id = ?;", |
| 1885 | params![alias, destination.domain.0, destination.nspace, key_id_guard.id()], |
| 1886 | ) |
| 1887 | .context("Failed to update key entry.")?; |
| 1888 | |
| 1889 | if updated != 1 { |
| 1890 | return Err(KsError::sys()) |
| 1891 | .context(format!("Update succeeded, but {} rows were updated.", updated)); |
| 1892 | } |
| 1893 | Ok(()).no_gc() |
| 1894 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1895 | .context(ks_err!()) |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 1896 | } |
| 1897 | |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1898 | /// Store a new key in a single transaction. |
| 1899 | /// The function creates a new key entry, populates the blob, key parameter, and metadata |
| 1900 | /// fields, and rebinds the given alias to the new key. |
Janis Danisevskis | 4507f3b | 2021-01-13 16:34:39 -0800 | [diff] [blame] | 1901 | /// The boolean returned is a hint for the garbage collector. If true, a key was replaced, |
| 1902 | /// is now unreferenced and needs to be collected. |
Chris Wailes | 3877f29 | 2021-07-26 19:24:18 -0700 | [diff] [blame] | 1903 | #[allow(clippy::too_many_arguments)] |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1904 | pub fn store_new_key( |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1905 | &mut self, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1906 | key: &KeyDescriptor, |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 1907 | key_type: KeyType, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1908 | params: &[KeyParameter], |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 1909 | blob_info: &BlobInfo, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 1910 | cert_info: &CertificateInfo, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1911 | metadata: &KeyMetaData, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 1912 | km_uuid: &Uuid, |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1913 | ) -> Result<KeyIdGuard> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1914 | let _wp = wd::watch("KeystoreDB::store_new_key"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 1915 | |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1916 | let (alias, domain, namespace) = match key { |
| 1917 | KeyDescriptor { alias: Some(alias), domain: Domain::APP, nspace, blob: None } |
| 1918 | | KeyDescriptor { alias: Some(alias), domain: Domain::SELINUX, nspace, blob: None } => { |
| 1919 | (alias, key.domain, nspace) |
| 1920 | } |
| 1921 | _ => { |
| 1922 | return Err(KsError::Rc(ResponseCode::INVALID_ARGUMENT)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1923 | .context(ks_err!("Need alias and domain must be APP or SELINUX.")); |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1924 | } |
| 1925 | }; |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 1926 | self.with_transaction(Immediate("TX_store_new_key"), |tx| { |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 1927 | let key_id = Self::create_key_entry_internal(tx, &domain, namespace, key_type, km_uuid) |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1928 | .context("Trying to create new key entry.")?; |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 1929 | let BlobInfo { blob, metadata: blob_metadata, superseded_blob } = *blob_info; |
| 1930 | |
| 1931 | // In some occasions the key blob is already upgraded during the import. |
| 1932 | // In order to make sure it gets properly deleted it is inserted into the |
| 1933 | // database here and then immediately replaced by the superseding blob. |
| 1934 | // The garbage collector will then subject the blob to deleteKey of the |
| 1935 | // KM back end to permanently invalidate the key. |
| 1936 | let need_gc = if let Some((blob, blob_metadata)) = superseded_blob { |
| 1937 | Self::set_blob_internal( |
| 1938 | tx, |
| 1939 | key_id.id(), |
| 1940 | SubComponentType::KEY_BLOB, |
| 1941 | Some(blob), |
| 1942 | Some(blob_metadata), |
| 1943 | ) |
| 1944 | .context("Trying to insert superseded key blob.")?; |
| 1945 | true |
| 1946 | } else { |
| 1947 | false |
| 1948 | }; |
| 1949 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1950 | Self::set_blob_internal( |
| 1951 | tx, |
| 1952 | key_id.id(), |
| 1953 | SubComponentType::KEY_BLOB, |
| 1954 | Some(blob), |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 1955 | Some(blob_metadata), |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1956 | ) |
| 1957 | .context("Trying to insert the key blob.")?; |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 1958 | if let Some(cert) = &cert_info.cert { |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 1959 | Self::set_blob_internal(tx, key_id.id(), SubComponentType::CERT, Some(cert), None) |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1960 | .context("Trying to insert the certificate.")?; |
| 1961 | } |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 1962 | if let Some(cert_chain) = &cert_info.cert_chain { |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 1963 | Self::set_blob_internal( |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1964 | tx, |
| 1965 | key_id.id(), |
| 1966 | SubComponentType::CERT_CHAIN, |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 1967 | Some(cert_chain), |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1968 | None, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1969 | ) |
| 1970 | .context("Trying to insert the certificate chain.")?; |
| 1971 | } |
| 1972 | Self::insert_keyparameter_internal(tx, &key_id, params) |
| 1973 | .context("Trying to insert key parameters.")?; |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 1974 | metadata.store_in_db(key_id.id(), tx).context("Trying to insert key metadata.")?; |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 1975 | let need_gc = Self::rebind_alias(tx, &key_id, alias, &domain, namespace, key_type) |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 1976 | .context("Trying to rebind alias.")? |
| 1977 | || need_gc; |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 1978 | Ok(key_id).do_gc(need_gc) |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 1979 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 1980 | .context(ks_err!()) |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 1981 | } |
| 1982 | |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 1983 | /// Store a new certificate |
| 1984 | /// The function creates a new key entry, populates the blob field and metadata, and rebinds |
| 1985 | /// the given alias to the new cert. |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 1986 | pub fn store_new_certificate( |
| 1987 | &mut self, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 1988 | key: &KeyDescriptor, |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 1989 | key_type: KeyType, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 1990 | cert: &[u8], |
| 1991 | km_uuid: &Uuid, |
| 1992 | ) -> Result<KeyIdGuard> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 1993 | let _wp = wd::watch("KeystoreDB::store_new_certificate"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 1994 | |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 1995 | let (alias, domain, namespace) = match key { |
| 1996 | KeyDescriptor { alias: Some(alias), domain: Domain::APP, nspace, blob: None } |
| 1997 | | KeyDescriptor { alias: Some(alias), domain: Domain::SELINUX, nspace, blob: None } => { |
| 1998 | (alias, key.domain, nspace) |
| 1999 | } |
| 2000 | _ => { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2001 | return Err(KsError::Rc(ResponseCode::INVALID_ARGUMENT)) |
| 2002 | .context(ks_err!("Need alias and domain must be APP or SELINUX.")); |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 2003 | } |
| 2004 | }; |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 2005 | self.with_transaction(Immediate("TX_store_new_certificate"), |tx| { |
Janis Danisevskis | 0cabd71 | 2021-05-25 11:07:10 -0700 | [diff] [blame] | 2006 | let key_id = Self::create_key_entry_internal(tx, &domain, namespace, key_type, km_uuid) |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 2007 | .context("Trying to create new key entry.")?; |
| 2008 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2009 | Self::set_blob_internal( |
| 2010 | tx, |
| 2011 | key_id.id(), |
| 2012 | SubComponentType::CERT_CHAIN, |
| 2013 | Some(cert), |
| 2014 | None, |
| 2015 | ) |
| 2016 | .context("Trying to insert certificate.")?; |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 2017 | |
| 2018 | let mut metadata = KeyMetaData::new(); |
| 2019 | metadata.add(KeyMetaEntry::CreationDate( |
| 2020 | DateTime::now().context("Trying to make creation time.")?, |
| 2021 | )); |
| 2022 | |
| 2023 | metadata.store_in_db(key_id.id(), tx).context("Trying to insert key metadata.")?; |
| 2024 | |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 2025 | let need_gc = Self::rebind_alias(tx, &key_id, alias, &domain, namespace, key_type) |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 2026 | .context("Trying to rebind alias.")?; |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2027 | Ok(key_id).do_gc(need_gc) |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 2028 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2029 | .context(ks_err!()) |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 2030 | } |
| 2031 | |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2032 | // Helper function loading the key_id given the key descriptor |
| 2033 | // tuple comprising domain, namespace, and alias. |
| 2034 | // Requires a valid transaction. |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 2035 | fn load_key_entry_id(tx: &Transaction, key: &KeyDescriptor, key_type: KeyType) -> Result<i64> { |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2036 | let alias = key |
| 2037 | .alias |
| 2038 | .as_ref() |
| 2039 | .map_or_else(|| Err(KsError::sys()), Ok) |
| 2040 | .context("In load_key_entry_id: Alias must be specified.")?; |
| 2041 | let mut stmt = tx |
| 2042 | .prepare( |
| 2043 | "SELECT id FROM persistent.keyentry |
| 2044 | WHERE |
Hasini Gunasinghe | deab85d | 2021-02-01 21:10:02 +0000 | [diff] [blame] | 2045 | key_type = ? |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 2046 | AND domain = ? |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2047 | AND namespace = ? |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2048 | AND alias = ? |
| 2049 | AND state = ?;", |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2050 | ) |
| 2051 | .context("In load_key_entry_id: Failed to select from keyentry table.")?; |
| 2052 | let mut rows = stmt |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2053 | .query(params![key_type, key.domain.0 as u32, key.nspace, alias, KeyLifeCycle::Live]) |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2054 | .context("In load_key_entry_id: Failed to read from keyentry table.")?; |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 2055 | db_utils::with_rows_extract_one(&mut rows, |row| { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2056 | row.map_or_else(|| Err(KsError::Rc(ResponseCode::KEY_NOT_FOUND)), Ok)? |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2057 | .get(0) |
| 2058 | .context("Failed to unpack id.") |
| 2059 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2060 | .context(ks_err!()) |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | /// This helper function completes the access tuple of a key, which is required |
| 2064 | /// to perform access control. The strategy depends on the `domain` field in the |
| 2065 | /// key descriptor. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2066 | /// * Domain::SELINUX: The access tuple is complete and this function only loads |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2067 | /// the key_id for further processing. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2068 | /// * Domain::APP: Like Domain::SELINUX, but the tuple is completed by `caller_uid` |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2069 | /// which serves as the namespace. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2070 | /// * Domain::GRANT: The grant table is queried for the `key_id` and the |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2071 | /// `access_vector`. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2072 | /// * Domain::KEY_ID: The keyentry table is queried for the owning `domain` and |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2073 | /// `namespace`. |
Chris Wailes | 1806f97 | 2024-08-19 16:37:40 -0700 | [diff] [blame] | 2074 | /// |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2075 | /// In each case the information returned is sufficient to perform the access |
| 2076 | /// check and the key id can be used to load further key artifacts. |
| 2077 | fn load_access_tuple( |
| 2078 | tx: &Transaction, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2079 | key: &KeyDescriptor, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 2080 | key_type: KeyType, |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2081 | caller_uid: u32, |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2082 | ) -> Result<KeyAccessInfo> { |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2083 | match key.domain { |
| 2084 | // Domain App or SELinux. In this case we load the key_id from |
| 2085 | // the keyentry database for further loading of key components. |
| 2086 | // We already have the full access tuple to perform access control. |
| 2087 | // The only distinction is that we use the caller_uid instead |
| 2088 | // of the caller supplied namespace if the domain field is |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2089 | // Domain::APP. |
| 2090 | Domain::APP | Domain::SELINUX => { |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2091 | let mut access_key = key.clone(); |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2092 | if access_key.domain == Domain::APP { |
| 2093 | access_key.nspace = caller_uid as i64; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2094 | } |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 2095 | let key_id = Self::load_key_entry_id(tx, &access_key, key_type) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2096 | .with_context(|| format!("With key.domain = {:?}.", access_key.domain))?; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2097 | |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2098 | Ok(KeyAccessInfo { key_id, descriptor: access_key, vector: None }) |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2099 | } |
| 2100 | |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2101 | // Domain::GRANT. In this case we load the key_id and the access_vector |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2102 | // from the grant table. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2103 | Domain::GRANT => { |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2104 | let mut stmt = tx |
| 2105 | .prepare( |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 2106 | "SELECT keyentryid, access_vector FROM persistent.grant |
Hasini Gunasinghe | e70a0ec | 2021-05-10 21:12:34 +0000 | [diff] [blame] | 2107 | WHERE grantee = ? AND id = ? AND |
| 2108 | (SELECT state FROM persistent.keyentry WHERE id = keyentryid) = ?;", |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2109 | ) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2110 | .context("Domain::GRANT prepare statement failed")?; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2111 | let mut rows = stmt |
Hasini Gunasinghe | e70a0ec | 2021-05-10 21:12:34 +0000 | [diff] [blame] | 2112 | .query(params![caller_uid as i64, key.nspace, KeyLifeCycle::Live]) |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2113 | .context("Domain:Grant: query failed.")?; |
| 2114 | let (key_id, access_vector): (i64, i32) = |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 2115 | db_utils::with_rows_extract_one(&mut rows, |row| { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2116 | let r = |
| 2117 | row.map_or_else(|| Err(KsError::Rc(ResponseCode::KEY_NOT_FOUND)), Ok)?; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2118 | Ok(( |
| 2119 | r.get(0).context("Failed to unpack key_id.")?, |
| 2120 | r.get(1).context("Failed to unpack access_vector.")?, |
| 2121 | )) |
| 2122 | }) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2123 | .context("Domain::GRANT.")?; |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2124 | Ok(KeyAccessInfo { |
| 2125 | key_id, |
| 2126 | descriptor: key.clone(), |
| 2127 | vector: Some(access_vector.into()), |
| 2128 | }) |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2129 | } |
| 2130 | |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2131 | // Domain::KEY_ID. In this case we load the domain and namespace from the |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2132 | // keyentry database because we need them for access control. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2133 | Domain::KEY_ID => { |
Janis Danisevskis | 4576002 | 2021-01-19 16:34:10 -0800 | [diff] [blame] | 2134 | let (domain, namespace): (Domain, i64) = { |
| 2135 | let mut stmt = tx |
| 2136 | .prepare( |
| 2137 | "SELECT domain, namespace FROM persistent.keyentry |
| 2138 | WHERE |
| 2139 | id = ? |
| 2140 | AND state = ?;", |
| 2141 | ) |
| 2142 | .context("Domain::KEY_ID: prepare statement failed")?; |
| 2143 | let mut rows = stmt |
| 2144 | .query(params![key.nspace, KeyLifeCycle::Live]) |
| 2145 | .context("Domain::KEY_ID: query failed.")?; |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 2146 | db_utils::with_rows_extract_one(&mut rows, |row| { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2147 | let r = |
| 2148 | row.map_or_else(|| Err(KsError::Rc(ResponseCode::KEY_NOT_FOUND)), Ok)?; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2149 | Ok(( |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2150 | Domain(r.get(0).context("Failed to unpack domain.")?), |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2151 | r.get(1).context("Failed to unpack namespace.")?, |
| 2152 | )) |
| 2153 | }) |
Janis Danisevskis | 4576002 | 2021-01-19 16:34:10 -0800 | [diff] [blame] | 2154 | .context("Domain::KEY_ID.")? |
| 2155 | }; |
| 2156 | |
| 2157 | // We may use a key by id after loading it by grant. |
| 2158 | // In this case we have to check if the caller has a grant for this particular |
| 2159 | // key. We can skip this if we already know that the caller is the owner. |
| 2160 | // But we cannot know this if domain is anything but App. E.g. in the case |
| 2161 | // of Domain::SELINUX we have to speculatively check for grants because we have to |
| 2162 | // consult the SEPolicy before we know if the caller is the owner. |
| 2163 | let access_vector: Option<KeyPermSet> = |
| 2164 | if domain != Domain::APP || namespace != caller_uid as i64 { |
| 2165 | let access_vector: Option<i32> = tx |
| 2166 | .query_row( |
| 2167 | "SELECT access_vector FROM persistent.grant |
| 2168 | WHERE grantee = ? AND keyentryid = ?;", |
| 2169 | params![caller_uid as i64, key.nspace], |
| 2170 | |row| row.get(0), |
| 2171 | ) |
| 2172 | .optional() |
| 2173 | .context("Domain::KEY_ID: query grant failed.")?; |
| 2174 | access_vector.map(|p| p.into()) |
| 2175 | } else { |
| 2176 | None |
| 2177 | }; |
| 2178 | |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2179 | let key_id = key.nspace; |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2180 | let mut access_key: KeyDescriptor = key.clone(); |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2181 | access_key.domain = domain; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2182 | access_key.nspace = namespace; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2183 | |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2184 | Ok(KeyAccessInfo { key_id, descriptor: access_key, vector: access_vector }) |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2185 | } |
Rajesh Nyamagoud | 625e589 | 2022-05-18 01:31:26 +0000 | [diff] [blame] | 2186 | _ => Err(anyhow!(KsError::Rc(ResponseCode::INVALID_ARGUMENT))), |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2187 | } |
| 2188 | } |
| 2189 | |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2190 | fn load_blob_components( |
| 2191 | key_id: i64, |
| 2192 | load_bits: KeyEntryLoadBits, |
| 2193 | tx: &Transaction, |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2194 | ) -> Result<(bool, Option<(Vec<u8>, BlobMetaData)>, Option<Vec<u8>>, Option<Vec<u8>>)> { |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2195 | let mut stmt = tx |
| 2196 | .prepare( |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2197 | "SELECT MAX(id), subcomponent_type, blob FROM persistent.blobentry |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2198 | WHERE keyentryid = ? GROUP BY subcomponent_type;", |
| 2199 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2200 | .context(ks_err!("prepare statement failed."))?; |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2201 | |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2202 | let mut rows = stmt.query(params![key_id]).context(ks_err!("query failed."))?; |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2203 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2204 | let mut key_blob: Option<(i64, Vec<u8>)> = None; |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2205 | let mut cert_blob: Option<Vec<u8>> = None; |
| 2206 | let mut cert_chain_blob: Option<Vec<u8>> = None; |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 2207 | let mut has_km_blob: bool = false; |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 2208 | db_utils::with_rows_extract_all(&mut rows, |row| { |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2209 | let sub_type: SubComponentType = |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2210 | row.get(1).context("Failed to extract subcomponent_type.")?; |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 2211 | has_km_blob = has_km_blob || sub_type == SubComponentType::KEY_BLOB; |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2212 | match (sub_type, load_bits.load_public(), load_bits.load_km()) { |
| 2213 | (SubComponentType::KEY_BLOB, _, true) => { |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2214 | key_blob = Some(( |
| 2215 | row.get(0).context("Failed to extract key blob id.")?, |
| 2216 | row.get(2).context("Failed to extract key blob.")?, |
| 2217 | )); |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2218 | } |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2219 | (SubComponentType::CERT, true, _) => { |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2220 | cert_blob = |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2221 | Some(row.get(2).context("Failed to extract public certificate blob.")?); |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2222 | } |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2223 | (SubComponentType::CERT_CHAIN, true, _) => { |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2224 | cert_chain_blob = |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2225 | Some(row.get(2).context("Failed to extract certificate chain blob.")?); |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2226 | } |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2227 | (SubComponentType::CERT, _, _) |
| 2228 | | (SubComponentType::CERT_CHAIN, _, _) |
| 2229 | | (SubComponentType::KEY_BLOB, _, _) => {} |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2230 | _ => Err(KsError::sys()).context("Unknown subcomponent type.")?, |
| 2231 | } |
| 2232 | Ok(()) |
| 2233 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2234 | .context(ks_err!())?; |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2235 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2236 | let blob_info = key_blob.map_or::<Result<_>, _>(Ok(None), |(blob_id, blob)| { |
| 2237 | Ok(Some(( |
| 2238 | blob, |
| 2239 | BlobMetaData::load_from_db(blob_id, tx) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2240 | .context(ks_err!("Trying to load blob_metadata."))?, |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2241 | ))) |
| 2242 | })?; |
| 2243 | |
| 2244 | Ok((has_km_blob, blob_info, cert_blob, cert_chain_blob)) |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | fn load_key_parameters(key_id: i64, tx: &Transaction) -> Result<Vec<KeyParameter>> { |
| 2248 | let mut stmt = tx |
| 2249 | .prepare( |
| 2250 | "SELECT tag, data, security_level from persistent.keyparameter |
| 2251 | WHERE keyentryid = ?;", |
| 2252 | ) |
| 2253 | .context("In load_key_parameters: prepare statement failed.")?; |
| 2254 | |
| 2255 | let mut parameters: Vec<KeyParameter> = Vec::new(); |
| 2256 | |
| 2257 | let mut rows = |
| 2258 | stmt.query(params![key_id]).context("In load_key_parameters: query failed.")?; |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 2259 | db_utils::with_rows_extract_all(&mut rows, |row| { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 2260 | let tag = Tag(row.get(0).context("Failed to read tag.")?); |
| 2261 | let sec_level = SecurityLevel(row.get(2).context("Failed to read sec_level.")?); |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2262 | parameters.push( |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 2263 | KeyParameter::new_from_sql(tag, &SqlField::new(1, row), sec_level) |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2264 | .context("Failed to read KeyParameter.")?, |
| 2265 | ); |
| 2266 | Ok(()) |
| 2267 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2268 | .context(ks_err!())?; |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2269 | |
| 2270 | Ok(parameters) |
| 2271 | } |
| 2272 | |
Qi Wu | b9433b5 | 2020-12-01 14:52:46 +0800 | [diff] [blame] | 2273 | /// Decrements the usage count of a limited use key. This function first checks whether the |
| 2274 | /// usage has been exhausted, if not, decreases the usage count. If the usage count reaches |
| 2275 | /// zero, the key also gets marked unreferenced and scheduled for deletion. |
| 2276 | /// Returns Ok(true) if the key was marked unreferenced as a hint to the garbage collector. |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2277 | pub fn check_and_update_key_usage_count(&mut self, key_id: i64) -> Result<()> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 2278 | let _wp = wd::watch("KeystoreDB::check_and_update_key_usage_count"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 2279 | |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 2280 | self.with_transaction(Immediate("TX_check_and_update_key_usage_count"), |tx| { |
Qi Wu | b9433b5 | 2020-12-01 14:52:46 +0800 | [diff] [blame] | 2281 | let limit: Option<i32> = tx |
| 2282 | .query_row( |
| 2283 | "SELECT data FROM persistent.keyparameter WHERE keyentryid = ? AND tag = ?;", |
| 2284 | params![key_id, Tag::USAGE_COUNT_LIMIT.0], |
| 2285 | |row| row.get(0), |
| 2286 | ) |
| 2287 | .optional() |
| 2288 | .context("Trying to load usage count")?; |
| 2289 | |
| 2290 | let limit = limit |
| 2291 | .ok_or(KsError::Km(ErrorCode::INVALID_KEY_BLOB)) |
| 2292 | .context("The Key no longer exists. Key is exhausted.")?; |
| 2293 | |
| 2294 | tx.execute( |
| 2295 | "UPDATE persistent.keyparameter |
| 2296 | SET data = data - 1 |
| 2297 | WHERE keyentryid = ? AND tag = ? AND data > 0;", |
| 2298 | params![key_id, Tag::USAGE_COUNT_LIMIT.0], |
| 2299 | ) |
| 2300 | .context("Failed to update key usage count.")?; |
| 2301 | |
| 2302 | match limit { |
| 2303 | 1 => Self::mark_unreferenced(tx, key_id) |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2304 | .map(|need_gc| (need_gc, ())) |
Qi Wu | b9433b5 | 2020-12-01 14:52:46 +0800 | [diff] [blame] | 2305 | .context("Trying to mark limited use key for deletion."), |
| 2306 | 0 => Err(KsError::Km(ErrorCode::INVALID_KEY_BLOB)).context("Key is exhausted."), |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2307 | _ => Ok(()).no_gc(), |
Qi Wu | b9433b5 | 2020-12-01 14:52:46 +0800 | [diff] [blame] | 2308 | } |
| 2309 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2310 | .context(ks_err!()) |
Qi Wu | b9433b5 | 2020-12-01 14:52:46 +0800 | [diff] [blame] | 2311 | } |
| 2312 | |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2313 | /// Load a key entry by the given key descriptor. |
| 2314 | /// It uses the `check_permission` callback to verify if the access is allowed |
| 2315 | /// given the key access tuple read from the database using `load_access_tuple`. |
| 2316 | /// With `load_bits` the caller may specify which blobs shall be loaded from |
| 2317 | /// the blob database. |
| 2318 | pub fn load_key_entry( |
| 2319 | &mut self, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2320 | key: &KeyDescriptor, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 2321 | key_type: KeyType, |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2322 | load_bits: KeyEntryLoadBits, |
| 2323 | caller_uid: u32, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2324 | check_permission: impl Fn(&KeyDescriptor, Option<KeyPermSet>) -> Result<()>, |
| 2325 | ) -> Result<(KeyIdGuard, KeyEntry)> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 2326 | let _wp = wd::watch("KeystoreDB::load_key_entry"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 2327 | |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2328 | loop { |
| 2329 | match self.load_key_entry_internal( |
| 2330 | key, |
| 2331 | key_type, |
| 2332 | load_bits, |
| 2333 | caller_uid, |
| 2334 | &check_permission, |
| 2335 | ) { |
| 2336 | Ok(result) => break Ok(result), |
| 2337 | Err(e) => { |
| 2338 | if Self::is_locked_error(&e) { |
David Drysdale | 115c472 | 2024-04-15 14:11:52 +0100 | [diff] [blame] | 2339 | std::thread::sleep(DB_BUSY_RETRY_INTERVAL); |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2340 | continue; |
| 2341 | } else { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2342 | return Err(e).context(ks_err!()); |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2343 | } |
| 2344 | } |
| 2345 | } |
| 2346 | } |
| 2347 | } |
| 2348 | |
| 2349 | fn load_key_entry_internal( |
| 2350 | &mut self, |
| 2351 | key: &KeyDescriptor, |
| 2352 | key_type: KeyType, |
| 2353 | load_bits: KeyEntryLoadBits, |
| 2354 | caller_uid: u32, |
| 2355 | check_permission: &impl Fn(&KeyDescriptor, Option<KeyPermSet>) -> Result<()>, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 2356 | ) -> Result<(KeyIdGuard, KeyEntry)> { |
| 2357 | // KEY ID LOCK 1/2 |
| 2358 | // If we got a key descriptor with a key id we can get the lock right away. |
| 2359 | // Otherwise we have to defer it until we know the key id. |
| 2360 | let key_id_guard = match key.domain { |
| 2361 | Domain::KEY_ID => Some(KEY_ID_LOCK.get(key.nspace)), |
| 2362 | _ => None, |
| 2363 | }; |
| 2364 | |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2365 | let tx = self |
| 2366 | .conn |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 2367 | .unchecked_transaction() |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2368 | .context(ks_err!("Failed to initialize transaction."))?; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2369 | |
| 2370 | // Load the key_id and complete the access control tuple. |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2371 | let access = Self::load_access_tuple(&tx, key, key_type, caller_uid).context(ks_err!())?; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2372 | |
| 2373 | // Perform access control. It is vital that we return here if the permission is denied. |
| 2374 | // So do not touch that '?' at the end. |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2375 | check_permission(&access.descriptor, access.vector).context(ks_err!())?; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2376 | |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 2377 | // KEY ID LOCK 2/2 |
| 2378 | // If we did not get a key id lock by now, it was because we got a key descriptor |
| 2379 | // without a key id. At this point we got the key id, so we can try and get a lock. |
| 2380 | // However, we cannot block here, because we are in the middle of the transaction. |
| 2381 | // So first we try to get the lock non blocking. If that fails, we roll back the |
| 2382 | // transaction and block until we get the lock. After we successfully got the lock, |
| 2383 | // we start a new transaction and load the access tuple again. |
| 2384 | // |
| 2385 | // We don't need to perform access control again, because we already established |
| 2386 | // that the caller had access to the given key. But we need to make sure that the |
| 2387 | // key id still exists. So we have to load the key entry by key id this time. |
| 2388 | let (key_id_guard, tx) = match key_id_guard { |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2389 | None => match KEY_ID_LOCK.try_get(access.key_id) { |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 2390 | None => { |
| 2391 | // Roll back the transaction. |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2392 | tx.rollback().context(ks_err!("Failed to roll back transaction."))?; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2393 | |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 2394 | // Block until we have a key id lock. |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2395 | let key_id_guard = KEY_ID_LOCK.get(access.key_id); |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 2396 | |
| 2397 | // Create a new transaction. |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2398 | let tx = self |
| 2399 | .conn |
| 2400 | .unchecked_transaction() |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2401 | .context(ks_err!("Failed to initialize transaction."))?; |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 2402 | |
| 2403 | Self::load_access_tuple( |
| 2404 | &tx, |
| 2405 | // This time we have to load the key by the retrieved key id, because the |
| 2406 | // alias may have been rebound after we rolled back the transaction. |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2407 | &KeyDescriptor { |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 2408 | domain: Domain::KEY_ID, |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2409 | nspace: access.key_id, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 2410 | ..Default::default() |
| 2411 | }, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 2412 | key_type, |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 2413 | caller_uid, |
| 2414 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2415 | .context(ks_err!("(deferred key lock)"))?; |
Janis Danisevskis | aec1459 | 2020-11-12 09:41:49 -0800 | [diff] [blame] | 2416 | (key_id_guard, tx) |
| 2417 | } |
| 2418 | Some(l) => (l, tx), |
| 2419 | }, |
| 2420 | Some(key_id_guard) => (key_id_guard, tx), |
| 2421 | }; |
| 2422 | |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2423 | let key_entry = |
| 2424 | Self::load_key_components(&tx, load_bits, key_id_guard.id()).context(ks_err!())?; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2425 | |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2426 | tx.commit().context(ks_err!("Failed to commit transaction."))?; |
Janis Danisevskis | 3f322cb | 2020-09-03 14:46:22 -0700 | [diff] [blame] | 2427 | |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 2428 | Ok((key_id_guard, key_entry)) |
| 2429 | } |
| 2430 | |
Janis Danisevskis | 4507f3b | 2021-01-13 16:34:39 -0800 | [diff] [blame] | 2431 | fn mark_unreferenced(tx: &Transaction, key_id: i64) -> Result<bool> { |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2432 | let updated = tx |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2433 | .execute("DELETE FROM persistent.keyentry WHERE id = ?;", params![key_id]) |
| 2434 | .context("Trying to delete keyentry.")?; |
| 2435 | tx.execute("DELETE FROM persistent.keymetadata WHERE keyentryid = ?;", params![key_id]) |
| 2436 | .context("Trying to delete keymetadata.")?; |
| 2437 | tx.execute("DELETE FROM persistent.keyparameter WHERE keyentryid = ?;", params![key_id]) |
| 2438 | .context("Trying to delete keyparameters.")?; |
| 2439 | tx.execute("DELETE FROM persistent.grant WHERE keyentryid = ?;", params![key_id]) |
| 2440 | .context("Trying to delete grants.")?; |
David Drysdale | 8da288c | 2024-07-29 15:57:01 +0100 | [diff] [blame] | 2441 | // The associated blobentry rows are not immediately deleted when the owning keyentry is |
| 2442 | // removed, because a KeyMint `deleteKey()` invocation is needed (specifically for the |
| 2443 | // `KEY_BLOB`). Mark the affected rows with `state=Orphaned` so a subsequent garbage |
| 2444 | // collection can do this. |
| 2445 | tx.execute( |
| 2446 | "UPDATE persistent.blobentry SET state = ? WHERE keyentryid = ?", |
| 2447 | params![BlobState::Orphaned, key_id], |
| 2448 | ) |
| 2449 | .context("Trying to mark blobentrys as superseded")?; |
Janis Danisevskis | 4507f3b | 2021-01-13 16:34:39 -0800 | [diff] [blame] | 2450 | Ok(updated != 0) |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | /// Marks the given key as unreferenced and removes all of the grants to this key. |
Janis Danisevskis | 4507f3b | 2021-01-13 16:34:39 -0800 | [diff] [blame] | 2454 | /// Returns Ok(true) if a key was marked unreferenced as a hint for the garbage collector. |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2455 | pub fn unbind_key( |
| 2456 | &mut self, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2457 | key: &KeyDescriptor, |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2458 | key_type: KeyType, |
| 2459 | caller_uid: u32, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2460 | check_permission: impl Fn(&KeyDescriptor, Option<KeyPermSet>) -> Result<()>, |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2461 | ) -> Result<()> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 2462 | let _wp = wd::watch("KeystoreDB::unbind_key"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 2463 | |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 2464 | self.with_transaction(Immediate("TX_unbind_key"), |tx| { |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2465 | let access = Self::load_access_tuple(tx, key, key_type, caller_uid) |
| 2466 | .context("Trying to get access tuple.")?; |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2467 | |
| 2468 | // Perform access control. It is vital that we return here if the permission is denied. |
| 2469 | // So do not touch that '?' at the end. |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2470 | check_permission(&access.descriptor, access.vector) |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2471 | .context("While checking permission.")?; |
| 2472 | |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2473 | Self::mark_unreferenced(tx, access.key_id) |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2474 | .map(|need_gc| (need_gc, ())) |
| 2475 | .context("Trying to mark the key unreferenced.") |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2476 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2477 | .context(ks_err!()) |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2478 | } |
| 2479 | |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 2480 | fn get_key_km_uuid(tx: &Transaction, key_id: i64) -> Result<Uuid> { |
| 2481 | tx.query_row( |
| 2482 | "SELECT km_uuid FROM persistent.keyentry WHERE id = ?", |
| 2483 | params![key_id], |
| 2484 | |row| row.get(0), |
| 2485 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2486 | .context(ks_err!()) |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 2487 | } |
| 2488 | |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 2489 | /// Delete all artifacts belonging to the namespace given by the domain-namespace tuple. |
| 2490 | /// This leaves all of the blob entries orphaned for subsequent garbage collection. |
| 2491 | pub fn unbind_keys_for_namespace(&mut self, domain: Domain, namespace: i64) -> Result<()> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 2492 | let _wp = wd::watch("KeystoreDB::unbind_keys_for_namespace"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 2493 | |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 2494 | if !(domain == Domain::APP || domain == Domain::SELINUX) { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2495 | return Err(KsError::Rc(ResponseCode::INVALID_ARGUMENT)).context(ks_err!()); |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 2496 | } |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 2497 | self.with_transaction(Immediate("TX_unbind_keys_for_namespace"), |tx| { |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 2498 | tx.execute( |
| 2499 | "DELETE FROM persistent.keymetadata |
| 2500 | WHERE keyentryid IN ( |
| 2501 | SELECT id FROM persistent.keyentry |
Tri Vo | 0346bbe | 2023-05-12 14:16:31 -0400 | [diff] [blame] | 2502 | WHERE domain = ? AND namespace = ? AND key_type = ? |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 2503 | );", |
Tri Vo | 0346bbe | 2023-05-12 14:16:31 -0400 | [diff] [blame] | 2504 | params![domain.0, namespace, KeyType::Client], |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 2505 | ) |
| 2506 | .context("Trying to delete keymetadata.")?; |
| 2507 | tx.execute( |
| 2508 | "DELETE FROM persistent.keyparameter |
| 2509 | WHERE keyentryid IN ( |
| 2510 | SELECT id FROM persistent.keyentry |
Tri Vo | 0346bbe | 2023-05-12 14:16:31 -0400 | [diff] [blame] | 2511 | WHERE domain = ? AND namespace = ? AND key_type = ? |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 2512 | );", |
Tri Vo | 0346bbe | 2023-05-12 14:16:31 -0400 | [diff] [blame] | 2513 | params![domain.0, namespace, KeyType::Client], |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 2514 | ) |
| 2515 | .context("Trying to delete keyparameters.")?; |
| 2516 | tx.execute( |
| 2517 | "DELETE FROM persistent.grant |
| 2518 | WHERE keyentryid IN ( |
| 2519 | SELECT id FROM persistent.keyentry |
Tri Vo | 0346bbe | 2023-05-12 14:16:31 -0400 | [diff] [blame] | 2520 | WHERE domain = ? AND namespace = ? AND key_type = ? |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 2521 | );", |
Tri Vo | 0346bbe | 2023-05-12 14:16:31 -0400 | [diff] [blame] | 2522 | params![domain.0, namespace, KeyType::Client], |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 2523 | ) |
| 2524 | .context("Trying to delete grants.")?; |
| 2525 | tx.execute( |
Janis Danisevskis | b146f31 | 2021-05-06 15:05:45 -0700 | [diff] [blame] | 2526 | "DELETE FROM persistent.keyentry |
Tri Vo | 0346bbe | 2023-05-12 14:16:31 -0400 | [diff] [blame] | 2527 | WHERE domain = ? AND namespace = ? AND key_type = ?;", |
| 2528 | params![domain.0, namespace, KeyType::Client], |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 2529 | ) |
| 2530 | .context("Trying to delete keyentry.")?; |
| 2531 | Ok(()).need_gc() |
| 2532 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2533 | .context(ks_err!()) |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 2534 | } |
| 2535 | |
Janis Danisevskis | 3cba10d | 2021-05-06 17:02:19 -0700 | [diff] [blame] | 2536 | fn cleanup_unreferenced(tx: &Transaction) -> Result<()> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 2537 | let _wp = wd::watch("KeystoreDB::cleanup_unreferenced"); |
Janis Danisevskis | 3cba10d | 2021-05-06 17:02:19 -0700 | [diff] [blame] | 2538 | { |
| 2539 | tx.execute( |
| 2540 | "DELETE FROM persistent.keymetadata |
| 2541 | WHERE keyentryid IN ( |
| 2542 | SELECT id FROM persistent.keyentry |
| 2543 | WHERE state = ? |
| 2544 | );", |
| 2545 | params![KeyLifeCycle::Unreferenced], |
| 2546 | ) |
| 2547 | .context("Trying to delete keymetadata.")?; |
| 2548 | tx.execute( |
| 2549 | "DELETE FROM persistent.keyparameter |
| 2550 | WHERE keyentryid IN ( |
| 2551 | SELECT id FROM persistent.keyentry |
| 2552 | WHERE state = ? |
| 2553 | );", |
| 2554 | params![KeyLifeCycle::Unreferenced], |
| 2555 | ) |
| 2556 | .context("Trying to delete keyparameters.")?; |
| 2557 | tx.execute( |
| 2558 | "DELETE FROM persistent.grant |
| 2559 | WHERE keyentryid IN ( |
| 2560 | SELECT id FROM persistent.keyentry |
| 2561 | WHERE state = ? |
| 2562 | );", |
| 2563 | params![KeyLifeCycle::Unreferenced], |
| 2564 | ) |
| 2565 | .context("Trying to delete grants.")?; |
| 2566 | tx.execute( |
| 2567 | "DELETE FROM persistent.keyentry |
| 2568 | WHERE state = ?;", |
| 2569 | params![KeyLifeCycle::Unreferenced], |
| 2570 | ) |
| 2571 | .context("Trying to delete keyentry.")?; |
| 2572 | Result::<()>::Ok(()) |
| 2573 | } |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2574 | .context(ks_err!()) |
Janis Danisevskis | 3cba10d | 2021-05-06 17:02:19 -0700 | [diff] [blame] | 2575 | } |
| 2576 | |
Eric Biggers | 9f7ebeb | 2024-06-20 14:59:32 +0000 | [diff] [blame] | 2577 | /// Deletes all keys for the given user, including both client keys and super keys. |
| 2578 | pub fn unbind_keys_for_user(&mut self, user_id: u32) -> Result<()> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 2579 | let _wp = wd::watch("KeystoreDB::unbind_keys_for_user"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 2580 | |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 2581 | self.with_transaction(Immediate("TX_unbind_keys_for_user"), |tx| { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 2582 | let mut stmt = tx |
| 2583 | .prepare(&format!( |
| 2584 | "SELECT id from persistent.keyentry |
| 2585 | WHERE ( |
| 2586 | key_type = ? |
| 2587 | AND domain = ? |
| 2588 | AND cast ( (namespace/{aid_user_offset}) as int) = ? |
| 2589 | AND state = ? |
| 2590 | ) OR ( |
| 2591 | key_type = ? |
| 2592 | AND namespace = ? |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 2593 | AND state = ? |
| 2594 | );", |
| 2595 | aid_user_offset = AID_USER_OFFSET |
| 2596 | )) |
| 2597 | .context(concat!( |
| 2598 | "In unbind_keys_for_user. ", |
| 2599 | "Failed to prepare the query to find the keys created by apps." |
| 2600 | ))?; |
| 2601 | |
| 2602 | let mut rows = stmt |
| 2603 | .query(params![ |
| 2604 | // WHERE client key: |
| 2605 | KeyType::Client, |
| 2606 | Domain::APP.0 as u32, |
| 2607 | user_id, |
| 2608 | KeyLifeCycle::Live, |
| 2609 | // OR super key: |
| 2610 | KeyType::Super, |
| 2611 | user_id, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 2612 | KeyLifeCycle::Live |
| 2613 | ]) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2614 | .context(ks_err!("Failed to query the keys created by apps."))?; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 2615 | |
| 2616 | let mut key_ids: Vec<i64> = Vec::new(); |
| 2617 | db_utils::with_rows_extract_all(&mut rows, |row| { |
| 2618 | key_ids |
| 2619 | .push(row.get(0).context("Failed to read key id of a key created by an app.")?); |
| 2620 | Ok(()) |
| 2621 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2622 | .context(ks_err!())?; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 2623 | |
| 2624 | let mut notify_gc = false; |
| 2625 | for key_id in key_ids { |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 2626 | notify_gc = Self::mark_unreferenced(tx, key_id) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 2627 | .context("In unbind_keys_for_user.")? |
| 2628 | || notify_gc; |
| 2629 | } |
| 2630 | Ok(()).do_gc(notify_gc) |
| 2631 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2632 | .context(ks_err!()) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 2635 | /// Deletes all auth-bound keys, i.e. keys that require user authentication, for the given user. |
| 2636 | /// This runs when the user's lock screen is being changed to Swipe or None. |
| 2637 | /// |
| 2638 | /// This intentionally does *not* delete keys that require that the device be unlocked, unless |
| 2639 | /// such keys also require user authentication. Keystore's concept of user authentication is |
| 2640 | /// fairly strong, and it requires that keys that require authentication be deleted as soon as |
| 2641 | /// authentication is no longer possible. In contrast, keys that just require that the device |
| 2642 | /// be unlocked should remain usable when the lock screen is set to Swipe or None, as the device |
| 2643 | /// is always considered "unlocked" in that case. |
| 2644 | pub fn unbind_auth_bound_keys_for_user(&mut self, user_id: u32) -> Result<()> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 2645 | let _wp = wd::watch("KeystoreDB::unbind_auth_bound_keys_for_user"); |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 2646 | |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 2647 | self.with_transaction(Immediate("TX_unbind_auth_bound_keys_for_user"), |tx| { |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 2648 | let mut stmt = tx |
| 2649 | .prepare(&format!( |
| 2650 | "SELECT id from persistent.keyentry |
| 2651 | WHERE key_type = ? |
| 2652 | AND domain = ? |
| 2653 | AND cast ( (namespace/{aid_user_offset}) as int) = ? |
| 2654 | AND state = ?;", |
| 2655 | aid_user_offset = AID_USER_OFFSET |
| 2656 | )) |
| 2657 | .context(concat!( |
| 2658 | "In unbind_auth_bound_keys_for_user. ", |
| 2659 | "Failed to prepare the query to find the keys created by apps." |
| 2660 | ))?; |
| 2661 | |
| 2662 | let mut rows = stmt |
| 2663 | .query(params![KeyType::Client, Domain::APP.0 as u32, user_id, KeyLifeCycle::Live,]) |
| 2664 | .context(ks_err!("Failed to query the keys created by apps."))?; |
| 2665 | |
| 2666 | let mut key_ids: Vec<i64> = Vec::new(); |
| 2667 | db_utils::with_rows_extract_all(&mut rows, |row| { |
| 2668 | key_ids |
| 2669 | .push(row.get(0).context("Failed to read key id of a key created by an app.")?); |
| 2670 | Ok(()) |
| 2671 | }) |
| 2672 | .context(ks_err!())?; |
| 2673 | |
| 2674 | let mut notify_gc = false; |
| 2675 | let mut num_unbound = 0; |
| 2676 | for key_id in key_ids { |
| 2677 | // Load the key parameters and filter out non-auth-bound keys. To identify |
| 2678 | // auth-bound keys, use the presence of UserSecureID. The absence of NoAuthRequired |
| 2679 | // could also be used, but UserSecureID is what Keystore treats as authoritative |
| 2680 | // when actually enforcing the key parameters (it might not matter, though). |
| 2681 | let params = Self::load_key_parameters(key_id, tx) |
| 2682 | .context("Failed to load key parameters.")?; |
| 2683 | let is_auth_bound_key = params.iter().any(|kp| { |
| 2684 | matches!(kp.key_parameter_value(), KeyParameterValue::UserSecureID(_)) |
| 2685 | }); |
| 2686 | if is_auth_bound_key { |
| 2687 | notify_gc = Self::mark_unreferenced(tx, key_id) |
| 2688 | .context("In unbind_auth_bound_keys_for_user.")? |
| 2689 | || notify_gc; |
| 2690 | num_unbound += 1; |
| 2691 | } |
| 2692 | } |
| 2693 | log::info!("Deleting {num_unbound} auth-bound keys for user {user_id}"); |
| 2694 | Ok(()).do_gc(notify_gc) |
| 2695 | }) |
| 2696 | .context(ks_err!()) |
| 2697 | } |
| 2698 | |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 2699 | fn load_key_components( |
| 2700 | tx: &Transaction, |
| 2701 | load_bits: KeyEntryLoadBits, |
| 2702 | key_id: i64, |
| 2703 | ) -> Result<KeyEntry> { |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 2704 | let metadata = KeyMetaData::load_from_db(key_id, tx).context("In load_key_components.")?; |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 2705 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2706 | let (has_km_blob, key_blob_info, cert_blob, cert_chain_blob) = |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 2707 | Self::load_blob_components(key_id, load_bits, tx).context("In load_key_components.")?; |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 2708 | |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 2709 | let parameters = Self::load_key_parameters(key_id, tx) |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 2710 | .context("In load_key_components: Trying to load key parameters.")?; |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 2711 | |
Chris Wailes | d5aaaef | 2021-07-27 16:04:33 -0700 | [diff] [blame] | 2712 | let km_uuid = Self::get_key_km_uuid(tx, key_id) |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 2713 | .context("In load_key_components: Trying to get KM uuid.")?; |
Janis Danisevskis | 93927dd | 2020-12-23 12:23:08 -0800 | [diff] [blame] | 2714 | |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 2715 | Ok(KeyEntry { |
| 2716 | id: key_id, |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2717 | key_blob_info, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 2718 | cert: cert_blob, |
| 2719 | cert_chain: cert_chain_blob, |
Max Bires | 8e93d2b | 2021-01-14 13:17:59 -0800 | [diff] [blame] | 2720 | km_uuid, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 2721 | parameters, |
| 2722 | metadata, |
Janis Danisevskis | 377d100 | 2021-01-27 19:07:48 -0800 | [diff] [blame] | 2723 | pure_cert: !has_km_blob, |
Janis Danisevskis | b42fc18 | 2020-12-15 08:41:27 -0800 | [diff] [blame] | 2724 | }) |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2725 | } |
| 2726 | |
Eran Messeri | 24f3197 | 2023-01-25 17:00:33 +0000 | [diff] [blame] | 2727 | /// Returns a list of KeyDescriptors in the selected domain/namespace whose |
| 2728 | /// aliases are greater than the specified 'start_past_alias'. If no value |
| 2729 | /// is provided, returns all KeyDescriptors. |
Janis Danisevskis | e92a5e6 | 2020-12-02 12:57:41 -0800 | [diff] [blame] | 2730 | /// The key descriptors will have the domain, nspace, and alias field set. |
Eran Messeri | 24f3197 | 2023-01-25 17:00:33 +0000 | [diff] [blame] | 2731 | /// The returned list will be sorted by alias. |
Janis Danisevskis | e92a5e6 | 2020-12-02 12:57:41 -0800 | [diff] [blame] | 2732 | /// Domain must be APP or SELINUX, the caller must make sure of that. |
David Drysdale | 8da288c | 2024-07-29 15:57:01 +0100 | [diff] [blame] | 2733 | /// Number of returned values is limited to 10,000 (which is empirically roughly |
| 2734 | /// what will fit in a Binder message). |
Eran Messeri | 24f3197 | 2023-01-25 17:00:33 +0000 | [diff] [blame] | 2735 | pub fn list_past_alias( |
Janis Danisevskis | 1831383 | 2021-05-17 13:30:32 -0700 | [diff] [blame] | 2736 | &mut self, |
| 2737 | domain: Domain, |
| 2738 | namespace: i64, |
| 2739 | key_type: KeyType, |
Eran Messeri | 24f3197 | 2023-01-25 17:00:33 +0000 | [diff] [blame] | 2740 | start_past_alias: Option<&str>, |
Janis Danisevskis | 1831383 | 2021-05-17 13:30:32 -0700 | [diff] [blame] | 2741 | ) -> Result<Vec<KeyDescriptor>> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 2742 | let _wp = wd::watch("KeystoreDB::list_past_alias"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 2743 | |
Eran Messeri | 24f3197 | 2023-01-25 17:00:33 +0000 | [diff] [blame] | 2744 | let query = format!( |
| 2745 | "SELECT DISTINCT alias FROM persistent.keyentry |
Janis Danisevskis | 1831383 | 2021-05-17 13:30:32 -0700 | [diff] [blame] | 2746 | WHERE domain = ? |
| 2747 | AND namespace = ? |
| 2748 | AND alias IS NOT NULL |
| 2749 | AND state = ? |
Eran Messeri | 24f3197 | 2023-01-25 17:00:33 +0000 | [diff] [blame] | 2750 | AND key_type = ? |
| 2751 | {} |
David Drysdale | 8da288c | 2024-07-29 15:57:01 +0100 | [diff] [blame] | 2752 | ORDER BY alias ASC |
| 2753 | LIMIT 10000;", |
Eran Messeri | 24f3197 | 2023-01-25 17:00:33 +0000 | [diff] [blame] | 2754 | if start_past_alias.is_some() { " AND alias > ?" } else { "" } |
| 2755 | ); |
Janis Danisevskis | e92a5e6 | 2020-12-02 12:57:41 -0800 | [diff] [blame] | 2756 | |
Eran Messeri | 24f3197 | 2023-01-25 17:00:33 +0000 | [diff] [blame] | 2757 | self.with_transaction(TransactionBehavior::Deferred, |tx| { |
| 2758 | let mut stmt = tx.prepare(&query).context(ks_err!("Failed to prepare."))?; |
| 2759 | |
| 2760 | let mut rows = match start_past_alias { |
| 2761 | Some(past_alias) => stmt |
| 2762 | .query(params![ |
| 2763 | domain.0 as u32, |
| 2764 | namespace, |
| 2765 | KeyLifeCycle::Live, |
| 2766 | key_type, |
| 2767 | past_alias |
| 2768 | ]) |
| 2769 | .context(ks_err!("Failed to query."))?, |
| 2770 | None => stmt |
| 2771 | .query(params![domain.0 as u32, namespace, KeyLifeCycle::Live, key_type,]) |
| 2772 | .context(ks_err!("Failed to query."))?, |
| 2773 | }; |
Janis Danisevskis | e92a5e6 | 2020-12-02 12:57:41 -0800 | [diff] [blame] | 2774 | |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2775 | let mut descriptors: Vec<KeyDescriptor> = Vec::new(); |
| 2776 | db_utils::with_rows_extract_all(&mut rows, |row| { |
| 2777 | descriptors.push(KeyDescriptor { |
| 2778 | domain, |
| 2779 | nspace: namespace, |
| 2780 | alias: Some(row.get(0).context("Trying to extract alias.")?), |
| 2781 | blob: None, |
| 2782 | }); |
| 2783 | Ok(()) |
| 2784 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2785 | .context(ks_err!("Failed to extract rows."))?; |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2786 | Ok(descriptors).no_gc() |
Janis Danisevskis | e92a5e6 | 2020-12-02 12:57:41 -0800 | [diff] [blame] | 2787 | }) |
Janis Danisevskis | e92a5e6 | 2020-12-02 12:57:41 -0800 | [diff] [blame] | 2788 | } |
| 2789 | |
Eran Messeri | 24f3197 | 2023-01-25 17:00:33 +0000 | [diff] [blame] | 2790 | /// Returns a number of KeyDescriptors in the selected domain/namespace. |
| 2791 | /// Domain must be APP or SELINUX, the caller must make sure of that. |
| 2792 | pub fn count_keys( |
| 2793 | &mut self, |
| 2794 | domain: Domain, |
| 2795 | namespace: i64, |
| 2796 | key_type: KeyType, |
| 2797 | ) -> Result<usize> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 2798 | let _wp = wd::watch("KeystoreDB::countKeys"); |
Eran Messeri | 24f3197 | 2023-01-25 17:00:33 +0000 | [diff] [blame] | 2799 | |
| 2800 | let num_keys = self.with_transaction(TransactionBehavior::Deferred, |tx| { |
| 2801 | tx.query_row( |
| 2802 | "SELECT COUNT(alias) FROM persistent.keyentry |
| 2803 | WHERE domain = ? |
| 2804 | AND namespace = ? |
| 2805 | AND alias IS NOT NULL |
| 2806 | AND state = ? |
| 2807 | AND key_type = ?;", |
| 2808 | params![domain.0 as u32, namespace, KeyLifeCycle::Live, key_type], |
| 2809 | |row| row.get(0), |
| 2810 | ) |
| 2811 | .context(ks_err!("Failed to count number of keys.")) |
| 2812 | .no_gc() |
| 2813 | })?; |
| 2814 | Ok(num_keys) |
| 2815 | } |
| 2816 | |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2817 | /// Adds a grant to the grant table. |
| 2818 | /// Like `load_key_entry` this function loads the access tuple before |
| 2819 | /// it uses the callback for a permission check. Upon success, |
| 2820 | /// it inserts the `grantee_uid`, `key_id`, and `access_vector` into the |
| 2821 | /// grant table. The new row will have a randomized id, which is used as |
| 2822 | /// grant id in the namespace field of the resulting KeyDescriptor. |
| 2823 | pub fn grant( |
| 2824 | &mut self, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2825 | key: &KeyDescriptor, |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2826 | caller_uid: u32, |
| 2827 | grantee_uid: u32, |
| 2828 | access_vector: KeyPermSet, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2829 | check_permission: impl Fn(&KeyDescriptor, &KeyPermSet) -> Result<()>, |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2830 | ) -> Result<KeyDescriptor> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 2831 | let _wp = wd::watch("KeystoreDB::grant"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 2832 | |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 2833 | self.with_transaction(Immediate("TX_grant"), |tx| { |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2834 | // Load the key_id and complete the access control tuple. |
| 2835 | // We ignore the access vector here because grants cannot be granted. |
| 2836 | // The access vector returned here expresses the permissions the |
| 2837 | // grantee has if key.domain == Domain::GRANT. But this vector |
| 2838 | // cannot include the grant permission by design, so there is no way the |
| 2839 | // subsequent permission check can pass. |
| 2840 | // We could check key.domain == Domain::GRANT and fail early. |
| 2841 | // But even if we load the access tuple by grant here, the permission |
| 2842 | // check denies the attempt to create a grant by grant descriptor. |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2843 | let access = |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2844 | Self::load_access_tuple(tx, key, KeyType::Client, caller_uid).context(ks_err!())?; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2845 | |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2846 | // Perform access control. It is vital that we return here if the permission |
| 2847 | // was denied. So do not touch that '?' at the end of the line. |
| 2848 | // This permission check checks if the caller has the grant permission |
| 2849 | // for the given key and in addition to all of the permissions |
| 2850 | // expressed in `access_vector`. |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2851 | check_permission(&access.descriptor, &access_vector) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2852 | .context(ks_err!("check_permission failed"))?; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2853 | |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2854 | let grant_id = if let Some(grant_id) = tx |
| 2855 | .query_row( |
| 2856 | "SELECT id FROM persistent.grant |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2857 | WHERE keyentryid = ? AND grantee = ?;", |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2858 | params![access.key_id, grantee_uid], |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2859 | |row| row.get(0), |
| 2860 | ) |
| 2861 | .optional() |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2862 | .context(ks_err!("Failed get optional existing grant id."))? |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2863 | { |
| 2864 | tx.execute( |
| 2865 | "UPDATE persistent.grant |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2866 | SET access_vector = ? |
| 2867 | WHERE id = ?;", |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2868 | params![i32::from(access_vector), grant_id], |
Joel Galenson | 845f74b | 2020-09-09 14:11:55 -0700 | [diff] [blame] | 2869 | ) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2870 | .context(ks_err!("Failed to update existing grant."))?; |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2871 | grant_id |
| 2872 | } else { |
| 2873 | Self::insert_with_retry(|id| { |
| 2874 | tx.execute( |
| 2875 | "INSERT INTO persistent.grant (id, grantee, keyentryid, access_vector) |
| 2876 | VALUES (?, ?, ?, ?);", |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2877 | params![id, grantee_uid, access.key_id, i32::from(access_vector)], |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2878 | ) |
| 2879 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2880 | .context(ks_err!())? |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2881 | }; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2882 | |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2883 | Ok(KeyDescriptor { domain: Domain::GRANT, nspace: grant_id, alias: None, blob: None }) |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2884 | .no_gc() |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2885 | }) |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2886 | } |
| 2887 | |
| 2888 | /// This function checks permissions like `grant` and `load_key_entry` |
| 2889 | /// before removing a grant from the grant table. |
| 2890 | pub fn ungrant( |
| 2891 | &mut self, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2892 | key: &KeyDescriptor, |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2893 | caller_uid: u32, |
| 2894 | grantee_uid: u32, |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2895 | check_permission: impl Fn(&KeyDescriptor) -> Result<()>, |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2896 | ) -> Result<()> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 2897 | let _wp = wd::watch("KeystoreDB::ungrant"); |
Janis Danisevskis | 850d486 | 2021-05-05 08:41:14 -0700 | [diff] [blame] | 2898 | |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 2899 | self.with_transaction(Immediate("TX_ungrant"), |tx| { |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2900 | // Load the key_id and complete the access control tuple. |
| 2901 | // We ignore the access vector here because grants cannot be granted. |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2902 | let access = |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2903 | Self::load_access_tuple(tx, key, KeyType::Client, caller_uid).context(ks_err!())?; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2904 | |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2905 | // Perform access control. We must return here if the permission |
| 2906 | // was denied. So do not touch the '?' at the end of this line. |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2907 | check_permission(&access.descriptor).context(ks_err!("check_permission failed."))?; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2908 | |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2909 | tx.execute( |
| 2910 | "DELETE FROM persistent.grant |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2911 | WHERE keyentryid = ? AND grantee = ?;", |
David Drysdale | ef897ec | 2024-10-22 12:55:06 +0100 | [diff] [blame] | 2912 | params![access.key_id, grantee_uid], |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2913 | ) |
| 2914 | .context("Failed to delete grant.")?; |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2915 | |
Janis Danisevskis | 7e8b462 | 2021-02-13 10:01:59 -0800 | [diff] [blame] | 2916 | Ok(()).no_gc() |
Janis Danisevskis | 66784c4 | 2021-01-27 08:40:25 -0800 | [diff] [blame] | 2917 | }) |
Janis Danisevskis | 63f7bc8 | 2020-09-03 10:12:56 -0700 | [diff] [blame] | 2918 | } |
| 2919 | |
Joel Galenson | 845f74b | 2020-09-09 14:11:55 -0700 | [diff] [blame] | 2920 | // Generates a random id and passes it to the given function, which will |
| 2921 | // try to insert it into a database. If that insertion fails, retry; |
| 2922 | // otherwise return the id. |
| 2923 | fn insert_with_retry(inserter: impl Fn(i64) -> rusqlite::Result<usize>) -> Result<i64> { |
| 2924 | loop { |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 2925 | let newid: i64 = match random() { |
| 2926 | Self::UNASSIGNED_KEY_ID => continue, // UNASSIGNED_KEY_ID cannot be assigned. |
| 2927 | i => i, |
| 2928 | }; |
Joel Galenson | 845f74b | 2020-09-09 14:11:55 -0700 | [diff] [blame] | 2929 | match inserter(newid) { |
| 2930 | // If the id already existed, try again. |
| 2931 | Err(rusqlite::Error::SqliteFailure( |
| 2932 | libsqlite3_sys::Error { |
| 2933 | code: libsqlite3_sys::ErrorCode::ConstraintViolation, |
| 2934 | extended_code: libsqlite3_sys::SQLITE_CONSTRAINT_UNIQUE, |
| 2935 | }, |
| 2936 | _, |
| 2937 | )) => (), |
| 2938 | Err(e) => { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2939 | return Err(e).context(ks_err!("failed to insert into database.")); |
Joel Galenson | 845f74b | 2020-09-09 14:11:55 -0700 | [diff] [blame] | 2940 | } |
| 2941 | _ => return Ok(newid), |
| 2942 | } |
| 2943 | } |
| 2944 | } |
Hasini Gunasinghe | 557b103 | 2020-11-10 01:35:30 +0000 | [diff] [blame] | 2945 | |
Matthew Maurer | d7815ca | 2021-05-06 21:58:45 -0700 | [diff] [blame] | 2946 | /// Insert or replace the auth token based on (user_id, auth_id, auth_type) |
| 2947 | pub fn insert_auth_token(&mut self, auth_token: &HardwareAuthToken) { |
Eric Biggers | 19b3b0d | 2024-01-31 22:46:47 +0000 | [diff] [blame] | 2948 | self.perboot |
| 2949 | .insert_auth_token_entry(AuthTokenEntry::new(auth_token.clone(), BootTime::now())) |
Hasini Gunasinghe | 557b103 | 2020-11-10 01:35:30 +0000 | [diff] [blame] | 2950 | } |
Hasini Gunasinghe | f70cf8e | 2020-11-11 01:02:41 +0000 | [diff] [blame] | 2951 | |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 2952 | /// Find the newest auth token matching the given predicate. |
Eric Biggers | b5613da | 2024-03-13 19:31:42 +0000 | [diff] [blame] | 2953 | pub fn find_auth_token_entry<F>(&self, p: F) -> Option<AuthTokenEntry> |
Janis Danisevskis | 5ed8c53 | 2021-01-11 14:19:42 -0800 | [diff] [blame] | 2954 | where |
| 2955 | F: Fn(&AuthTokenEntry) -> bool, |
| 2956 | { |
Eric Biggers | b5613da | 2024-03-13 19:31:42 +0000 | [diff] [blame] | 2957 | self.perboot.find_auth_token_entry(p) |
Hasini Gunasinghe | f70cf8e | 2020-11-11 01:02:41 +0000 | [diff] [blame] | 2958 | } |
Pavel Grafov | f45034a | 2021-05-12 22:35:45 +0100 | [diff] [blame] | 2959 | |
| 2960 | /// Load descriptor of a key by key id |
| 2961 | pub fn load_key_descriptor(&mut self, key_id: i64) -> Result<Option<KeyDescriptor>> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 2962 | let _wp = wd::watch("KeystoreDB::load_key_descriptor"); |
Pavel Grafov | f45034a | 2021-05-12 22:35:45 +0100 | [diff] [blame] | 2963 | |
| 2964 | self.with_transaction(TransactionBehavior::Deferred, |tx| { |
| 2965 | tx.query_row( |
| 2966 | "SELECT domain, namespace, alias FROM persistent.keyentry WHERE id = ?;", |
| 2967 | params![key_id], |
| 2968 | |row| { |
| 2969 | Ok(KeyDescriptor { |
| 2970 | domain: Domain(row.get(0)?), |
| 2971 | nspace: row.get(1)?, |
| 2972 | alias: row.get(2)?, |
| 2973 | blob: None, |
| 2974 | }) |
| 2975 | }, |
| 2976 | ) |
| 2977 | .optional() |
| 2978 | .context("Trying to load key descriptor") |
| 2979 | .no_gc() |
| 2980 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 2981 | .context(ks_err!()) |
Pavel Grafov | f45034a | 2021-05-12 22:35:45 +0100 | [diff] [blame] | 2982 | } |
Eran Messeri | 4dc27b5 | 2024-01-09 12:43:31 +0000 | [diff] [blame] | 2983 | |
| 2984 | /// Returns a list of app UIDs that have keys authenticated by the given secure_user_id |
| 2985 | /// (for the given user_id). |
| 2986 | /// This is helpful for finding out which apps will have their keys invalidated when |
| 2987 | /// the user changes biometrics enrollment or removes their LSKF. |
| 2988 | pub fn get_app_uids_affected_by_sid( |
| 2989 | &mut self, |
| 2990 | user_id: i32, |
| 2991 | secure_user_id: i64, |
| 2992 | ) -> Result<Vec<i64>> { |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 2993 | let _wp = wd::watch("KeystoreDB::get_app_uids_affected_by_sid"); |
Eran Messeri | 4dc27b5 | 2024-01-09 12:43:31 +0000 | [diff] [blame] | 2994 | |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 2995 | let ids = self.with_transaction(Immediate("TX_get_app_uids_affected_by_sid"), |tx| { |
Eran Messeri | 4dc27b5 | 2024-01-09 12:43:31 +0000 | [diff] [blame] | 2996 | let mut stmt = tx |
| 2997 | .prepare(&format!( |
| 2998 | "SELECT id, namespace from persistent.keyentry |
| 2999 | WHERE key_type = ? |
| 3000 | AND domain = ? |
| 3001 | AND cast ( (namespace/{AID_USER_OFFSET}) as int) = ? |
| 3002 | AND state = ?;", |
| 3003 | )) |
| 3004 | .context(concat!( |
| 3005 | "In get_app_uids_affected_by_sid, ", |
| 3006 | "failed to prepare the query to find the keys created by apps." |
| 3007 | ))?; |
| 3008 | |
| 3009 | let mut rows = stmt |
| 3010 | .query(params![KeyType::Client, Domain::APP.0 as u32, user_id, KeyLifeCycle::Live,]) |
| 3011 | .context(ks_err!("Failed to query the keys created by apps."))?; |
| 3012 | |
| 3013 | let mut key_ids_and_app_uids: HashMap<i64, i64> = Default::default(); |
| 3014 | db_utils::with_rows_extract_all(&mut rows, |row| { |
| 3015 | key_ids_and_app_uids.insert( |
| 3016 | row.get(0).context("Failed to read key id of a key created by an app.")?, |
| 3017 | row.get(1).context("Failed to read the app uid")?, |
| 3018 | ); |
| 3019 | Ok(()) |
| 3020 | })?; |
| 3021 | Ok(key_ids_and_app_uids).no_gc() |
| 3022 | })?; |
| 3023 | let mut app_uids_affected_by_sid: HashSet<i64> = Default::default(); |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 3024 | for (key_id, app_uid) in ids { |
Eran Messeri | 4dc27b5 | 2024-01-09 12:43:31 +0000 | [diff] [blame] | 3025 | // Read the key parameters for each key in its own transaction. It is OK to ignore |
| 3026 | // an error to get the properties of a particular key since it might have been deleted |
| 3027 | // under our feet after the previous transaction concluded. If the key was deleted |
| 3028 | // then it is no longer applicable if it was auth-bound or not. |
| 3029 | if let Ok(is_key_bound_to_sid) = |
David Drysdale | 7b9ca23 | 2024-05-23 18:19:46 +0100 | [diff] [blame] | 3030 | self.with_transaction(Immediate("TX_get_app_uids_affects_by_sid 2"), |tx| { |
Eran Messeri | 4dc27b5 | 2024-01-09 12:43:31 +0000 | [diff] [blame] | 3031 | let params = Self::load_key_parameters(key_id, tx) |
| 3032 | .context("Failed to load key parameters.")?; |
| 3033 | // Check if the key is bound to this secure user ID. |
| 3034 | let is_key_bound_to_sid = params.iter().any(|kp| { |
| 3035 | matches!( |
| 3036 | kp.key_parameter_value(), |
| 3037 | KeyParameterValue::UserSecureID(sid) if *sid == secure_user_id |
| 3038 | ) |
| 3039 | }); |
| 3040 | Ok(is_key_bound_to_sid).no_gc() |
| 3041 | }) |
| 3042 | { |
| 3043 | if is_key_bound_to_sid { |
| 3044 | app_uids_affected_by_sid.insert(app_uid); |
| 3045 | } |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | let app_uids_vec: Vec<i64> = app_uids_affected_by_sid.into_iter().collect(); |
| 3050 | Ok(app_uids_vec) |
| 3051 | } |
Joel Galenson | 26f4d01 | 2020-07-17 14:57:21 -0700 | [diff] [blame] | 3052 | } |