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