Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame^] | 1 | // Copyright 2021, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! This is the metrics store module of keystore. It does the following tasks: |
| 16 | //! 1. Processes the data about keystore events asynchronously, and |
| 17 | //! stores them in an in-memory store. |
| 18 | //! 2. Returns the collected metrics when requested by the statsd proxy. |
| 19 | |
| 20 | use crate::error::get_error_code; |
| 21 | use crate::globals::DB; |
| 22 | use crate::key_parameter::KeyParameterValue as KsKeyParamValue; |
| 23 | use crate::operation::Outcome; |
| 24 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
| 25 | Algorithm::Algorithm, BlockMode::BlockMode, Digest::Digest, EcCurve::EcCurve, |
| 26 | HardwareAuthenticatorType::HardwareAuthenticatorType, KeyOrigin::KeyOrigin, |
| 27 | KeyParameter::KeyParameter, KeyPurpose::KeyPurpose, PaddingMode::PaddingMode, |
| 28 | SecurityLevel::SecurityLevel, |
| 29 | }; |
| 30 | use android_security_metrics::aidl::android::security::metrics::{ |
| 31 | Algorithm::Algorithm as MetricsAlgorithm, AtomID::AtomID, EcCurve::EcCurve as MetricsEcCurve, |
| 32 | HardwareAuthenticatorType::HardwareAuthenticatorType as MetricsHardwareAuthenticatorType, |
| 33 | KeyCreationWithAuthInfo::KeyCreationWithAuthInfo, |
| 34 | KeyCreationWithGeneralInfo::KeyCreationWithGeneralInfo, |
| 35 | KeyCreationWithPurposeAndModesInfo::KeyCreationWithPurposeAndModesInfo, |
| 36 | KeyOperationWithGeneralInfo::KeyOperationWithGeneralInfo, |
| 37 | KeyOperationWithPurposeAndModesInfo::KeyOperationWithPurposeAndModesInfo, |
| 38 | KeyOrigin::KeyOrigin as MetricsKeyOrigin, Keystore2AtomWithOverflow::Keystore2AtomWithOverflow, |
| 39 | KeystoreAtom::KeystoreAtom, KeystoreAtomPayload::KeystoreAtomPayload, |
| 40 | Outcome::Outcome as MetricsOutcome, Purpose::Purpose as MetricsPurpose, |
| 41 | SecurityLevel::SecurityLevel as MetricsSecurityLevel, Storage::Storage as MetricsStorage, |
| 42 | }; |
| 43 | use anyhow::Result; |
| 44 | use lazy_static::lazy_static; |
| 45 | use std::collections::HashMap; |
| 46 | use std::sync::Mutex; |
| 47 | |
| 48 | lazy_static! { |
| 49 | /// Singleton for MetricsStore. |
| 50 | pub static ref METRICS_STORE: MetricsStore = Default::default(); |
| 51 | } |
| 52 | |
| 53 | /// MetricsStore stores the <atom object, count> as <key, value> in the inner hash map, |
| 54 | /// indexed by the atom id, in the outer hash map. |
| 55 | /// There can be different atom objects with the same atom id based on the values assigned to the |
| 56 | /// fields of the atom objects. When an atom object with a particular combination of field values is |
| 57 | /// inserted, we first check if that atom object is in the inner hash map. If one exists, count |
| 58 | /// is inceremented. Otherwise, the atom object is inserted with count = 1. Note that count field |
| 59 | /// of the atom object itself is set to 0 while the object is stored in the hash map. When the atom |
| 60 | /// objects are queried by the atom id, the corresponding atom objects are retrieved, cloned, and |
| 61 | /// the count field of the cloned objects is set to the corresponding value field in the inner hash |
| 62 | /// map before the query result is returned. |
| 63 | #[derive(Default)] |
| 64 | pub struct MetricsStore { |
| 65 | metrics_store: Mutex<HashMap<AtomID, HashMap<KeystoreAtomPayload, i32>>>, |
| 66 | } |
| 67 | |
| 68 | impl MetricsStore { |
| 69 | /// There are some atoms whose maximum cardinality exceeds the cardinality limits tolerated |
| 70 | /// by statsd. Statsd tolerates cardinality between 200-300. Therefore, the in-memory storage |
| 71 | /// limit for a single atom is set to 250. If the number of atom objects created for a |
| 72 | /// particular atom exceeds this limit, an overflow atom object is created to track the ID of |
| 73 | /// such atoms. |
| 74 | const SINGLE_ATOM_STORE_MAX_SIZE: usize = 250; |
| 75 | |
| 76 | /// Return a vector of atom objects with the given atom ID, if one exists in the metrics_store. |
| 77 | /// If any atom object does not exist in the metrics_store for the given atom ID, return an |
| 78 | /// empty vector. |
| 79 | pub fn get_atoms(&self, atom_id: AtomID) -> Result<Vec<KeystoreAtom>> { |
| 80 | // StorageStats is an original pulled atom (i.e. not a pushed atom converted to a |
| 81 | // pulledd atom). Therefore, it is handled separately. |
| 82 | if AtomID::STORAGE_STATS == atom_id { |
| 83 | return pull_storage_stats(); |
| 84 | } |
| 85 | |
| 86 | // TODO (b/184301651): process and return RKP pool stats. |
| 87 | |
| 88 | // It is safe to call unwrap here since the lock can not be poisoned based on its usage |
| 89 | // in this module and the lock is not acquired in the same thread before. |
| 90 | let metrics_store_guard = self.metrics_store.lock().unwrap(); |
| 91 | metrics_store_guard.get(&atom_id).map_or(Ok(Vec::<KeystoreAtom>::new()), |atom_count_map| { |
| 92 | Ok(atom_count_map |
| 93 | .iter() |
| 94 | .map(|(atom, count)| KeystoreAtom { payload: atom.clone(), count: *count }) |
| 95 | .collect()) |
| 96 | }) |
| 97 | } |
| 98 | |
| 99 | /// Insert an atom object to the metrics_store indexed by the atom ID. |
| 100 | fn insert_atom(&self, atom_id: AtomID, atom: KeystoreAtomPayload) { |
| 101 | // It is ok to unwrap here since the mutex cannot be poisoned according to the way it is |
| 102 | // used in this module. And the lock is not acquired by this thread before. |
| 103 | let mut metrics_store_guard = self.metrics_store.lock().unwrap(); |
| 104 | let atom_count_map = metrics_store_guard.entry(atom_id).or_insert_with(HashMap::new); |
| 105 | if atom_count_map.len() < MetricsStore::SINGLE_ATOM_STORE_MAX_SIZE { |
| 106 | let atom_count = atom_count_map.entry(atom).or_insert(0); |
| 107 | *atom_count += 1; |
| 108 | } else { |
| 109 | // Insert an overflow atom |
| 110 | let overflow_atom_count_map = metrics_store_guard |
| 111 | .entry(AtomID::KEYSTORE2_ATOM_WITH_OVERFLOW) |
| 112 | .or_insert_with(HashMap::new); |
| 113 | |
| 114 | if overflow_atom_count_map.len() < MetricsStore::SINGLE_ATOM_STORE_MAX_SIZE { |
| 115 | let overflow_atom = Keystore2AtomWithOverflow { atom_id }; |
| 116 | let atom_count = overflow_atom_count_map |
| 117 | .entry(KeystoreAtomPayload::Keystore2AtomWithOverflow(overflow_atom)) |
| 118 | .or_insert(0); |
| 119 | *atom_count += 1; |
| 120 | } else { |
| 121 | // This is a rare case, if at all. |
| 122 | log::error!("In insert_atom: Maximum storage limit reached for overflow atom.") |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /// Log key creation events to be sent to statsd. |
| 129 | pub fn log_key_creation_event_stats<U>( |
| 130 | sec_level: SecurityLevel, |
| 131 | key_params: &[KeyParameter], |
| 132 | result: &Result<U>, |
| 133 | ) { |
| 134 | let ( |
| 135 | key_creation_with_general_info, |
| 136 | key_creation_with_auth_info, |
| 137 | key_creation_with_purpose_and_modes_info, |
| 138 | ) = process_key_creation_event_stats(sec_level, key_params, result); |
| 139 | |
| 140 | METRICS_STORE |
| 141 | .insert_atom(AtomID::KEY_CREATION_WITH_GENERAL_INFO, key_creation_with_general_info); |
| 142 | METRICS_STORE.insert_atom(AtomID::KEY_CREATION_WITH_AUTH_INFO, key_creation_with_auth_info); |
| 143 | METRICS_STORE.insert_atom( |
| 144 | AtomID::KEY_CREATION_WITH_PURPOSE_AND_MODES_INFO, |
| 145 | key_creation_with_purpose_and_modes_info, |
| 146 | ); |
| 147 | } |
| 148 | |
| 149 | // Process the statistics related to key creations and return the three atom objects related to key |
| 150 | // creations: i) KeyCreationWithGeneralInfo ii) KeyCreationWithAuthInfo |
| 151 | // iii) KeyCreationWithPurposeAndModesInfo |
| 152 | fn process_key_creation_event_stats<U>( |
| 153 | sec_level: SecurityLevel, |
| 154 | key_params: &[KeyParameter], |
| 155 | result: &Result<U>, |
| 156 | ) -> (KeystoreAtomPayload, KeystoreAtomPayload, KeystoreAtomPayload) { |
| 157 | // In the default atom objects, fields represented by bitmaps and i32 fields |
| 158 | // will take 0, except error_code which defaults to 1 indicating NO_ERROR and key_size, |
| 159 | // and auth_time_out which defaults to -1. |
| 160 | // The boolean fields are set to false by default. |
| 161 | // Some keymint enums do have 0 as an enum variant value. In such cases, the corresponding |
| 162 | // enum variant value in atoms.proto is incremented by 1, in order to have 0 as the reserved |
| 163 | // value for unspecified fields. |
| 164 | let mut key_creation_with_general_info = KeyCreationWithGeneralInfo { |
| 165 | algorithm: MetricsAlgorithm::ALGORITHM_UNSPECIFIED, |
| 166 | key_size: -1, |
| 167 | ec_curve: MetricsEcCurve::EC_CURVE_UNSPECIFIED, |
| 168 | key_origin: MetricsKeyOrigin::ORIGIN_UNSPECIFIED, |
| 169 | error_code: 1, |
| 170 | // Default for bool is false (for attestation_requested field). |
| 171 | ..Default::default() |
| 172 | }; |
| 173 | |
| 174 | let mut key_creation_with_auth_info = KeyCreationWithAuthInfo { |
| 175 | user_auth_type: MetricsHardwareAuthenticatorType::AUTH_TYPE_UNSPECIFIED, |
| 176 | log10_auth_key_timeout_seconds: -1, |
| 177 | security_level: MetricsSecurityLevel::SECURITY_LEVEL_UNSPECIFIED, |
| 178 | }; |
| 179 | |
| 180 | let mut key_creation_with_purpose_and_modes_info = KeyCreationWithPurposeAndModesInfo { |
| 181 | algorithm: MetricsAlgorithm::ALGORITHM_UNSPECIFIED, |
| 182 | // Default for i32 is 0 (for the remaining bitmap fields). |
| 183 | ..Default::default() |
| 184 | }; |
| 185 | |
| 186 | if let Err(ref e) = result { |
| 187 | key_creation_with_general_info.error_code = get_error_code(e); |
| 188 | } |
| 189 | |
| 190 | key_creation_with_auth_info.security_level = process_security_level(sec_level); |
| 191 | |
| 192 | for key_param in key_params.iter().map(KsKeyParamValue::from) { |
| 193 | match key_param { |
| 194 | KsKeyParamValue::Algorithm(a) => { |
| 195 | let algorithm = match a { |
| 196 | Algorithm::RSA => MetricsAlgorithm::RSA, |
| 197 | Algorithm::EC => MetricsAlgorithm::EC, |
| 198 | Algorithm::AES => MetricsAlgorithm::AES, |
| 199 | Algorithm::TRIPLE_DES => MetricsAlgorithm::TRIPLE_DES, |
| 200 | Algorithm::HMAC => MetricsAlgorithm::HMAC, |
| 201 | _ => MetricsAlgorithm::ALGORITHM_UNSPECIFIED, |
| 202 | }; |
| 203 | key_creation_with_general_info.algorithm = algorithm; |
| 204 | key_creation_with_purpose_and_modes_info.algorithm = algorithm; |
| 205 | } |
| 206 | KsKeyParamValue::KeySize(s) => { |
| 207 | key_creation_with_general_info.key_size = s; |
| 208 | } |
| 209 | KsKeyParamValue::KeyOrigin(o) => { |
| 210 | key_creation_with_general_info.key_origin = match o { |
| 211 | KeyOrigin::GENERATED => MetricsKeyOrigin::GENERATED, |
| 212 | KeyOrigin::DERIVED => MetricsKeyOrigin::DERIVED, |
| 213 | KeyOrigin::IMPORTED => MetricsKeyOrigin::IMPORTED, |
| 214 | KeyOrigin::RESERVED => MetricsKeyOrigin::RESERVED, |
| 215 | KeyOrigin::SECURELY_IMPORTED => MetricsKeyOrigin::SECURELY_IMPORTED, |
| 216 | _ => MetricsKeyOrigin::ORIGIN_UNSPECIFIED, |
| 217 | } |
| 218 | } |
| 219 | KsKeyParamValue::HardwareAuthenticatorType(a) => { |
| 220 | key_creation_with_auth_info.user_auth_type = match a { |
| 221 | HardwareAuthenticatorType::NONE => MetricsHardwareAuthenticatorType::NONE, |
| 222 | HardwareAuthenticatorType::PASSWORD => { |
| 223 | MetricsHardwareAuthenticatorType::PASSWORD |
| 224 | } |
| 225 | HardwareAuthenticatorType::FINGERPRINT => { |
| 226 | MetricsHardwareAuthenticatorType::FINGERPRINT |
| 227 | } |
| 228 | HardwareAuthenticatorType::ANY => MetricsHardwareAuthenticatorType::ANY, |
| 229 | _ => MetricsHardwareAuthenticatorType::AUTH_TYPE_UNSPECIFIED, |
| 230 | } |
| 231 | } |
| 232 | KsKeyParamValue::AuthTimeout(t) => { |
| 233 | key_creation_with_auth_info.log10_auth_key_timeout_seconds = |
| 234 | f32::log10(t as f32) as i32; |
| 235 | } |
| 236 | KsKeyParamValue::PaddingMode(p) => { |
| 237 | compute_padding_mode_bitmap( |
| 238 | &mut key_creation_with_purpose_and_modes_info.padding_mode_bitmap, |
| 239 | p, |
| 240 | ); |
| 241 | } |
| 242 | KsKeyParamValue::Digest(d) => { |
| 243 | // key_creation_with_purpose_and_modes_info.digest_bitmap = |
| 244 | compute_digest_bitmap( |
| 245 | &mut key_creation_with_purpose_and_modes_info.digest_bitmap, |
| 246 | d, |
| 247 | ); |
| 248 | } |
| 249 | KsKeyParamValue::BlockMode(b) => { |
| 250 | compute_block_mode_bitmap( |
| 251 | &mut key_creation_with_purpose_and_modes_info.block_mode_bitmap, |
| 252 | b, |
| 253 | ); |
| 254 | } |
| 255 | KsKeyParamValue::KeyPurpose(k) => { |
| 256 | compute_purpose_bitmap( |
| 257 | &mut key_creation_with_purpose_and_modes_info.purpose_bitmap, |
| 258 | k, |
| 259 | ); |
| 260 | } |
| 261 | KsKeyParamValue::EcCurve(e) => { |
| 262 | key_creation_with_general_info.ec_curve = match e { |
| 263 | EcCurve::P_224 => MetricsEcCurve::P_224, |
| 264 | EcCurve::P_256 => MetricsEcCurve::P_256, |
| 265 | EcCurve::P_384 => MetricsEcCurve::P_384, |
| 266 | EcCurve::P_521 => MetricsEcCurve::P_521, |
| 267 | _ => MetricsEcCurve::EC_CURVE_UNSPECIFIED, |
| 268 | } |
| 269 | } |
| 270 | KsKeyParamValue::AttestationChallenge(_) => { |
| 271 | key_creation_with_general_info.attestation_requested = true; |
| 272 | } |
| 273 | _ => {} |
| 274 | } |
| 275 | } |
| 276 | if key_creation_with_general_info.algorithm == MetricsAlgorithm::EC { |
| 277 | // Do not record key sizes if Algorithm = EC, in order to reduce cardinality. |
| 278 | key_creation_with_general_info.key_size = -1; |
| 279 | } |
| 280 | |
| 281 | ( |
| 282 | KeystoreAtomPayload::KeyCreationWithGeneralInfo(key_creation_with_general_info), |
| 283 | KeystoreAtomPayload::KeyCreationWithAuthInfo(key_creation_with_auth_info), |
| 284 | KeystoreAtomPayload::KeyCreationWithPurposeAndModesInfo( |
| 285 | key_creation_with_purpose_and_modes_info, |
| 286 | ), |
| 287 | ) |
| 288 | } |
| 289 | |
| 290 | /// Log key operation events to be sent to statsd. |
| 291 | pub fn log_key_operation_event_stats( |
| 292 | sec_level: SecurityLevel, |
| 293 | key_purpose: KeyPurpose, |
| 294 | op_params: &[KeyParameter], |
| 295 | op_outcome: &Outcome, |
| 296 | key_upgraded: bool, |
| 297 | ) { |
| 298 | let (key_operation_with_general_info, key_operation_with_purpose_and_modes_info) = |
| 299 | process_key_operation_event_stats( |
| 300 | sec_level, |
| 301 | key_purpose, |
| 302 | op_params, |
| 303 | op_outcome, |
| 304 | key_upgraded, |
| 305 | ); |
| 306 | METRICS_STORE |
| 307 | .insert_atom(AtomID::KEY_OPERATION_WITH_GENERAL_INFO, key_operation_with_general_info); |
| 308 | METRICS_STORE.insert_atom( |
| 309 | AtomID::KEY_OPERATION_WITH_PURPOSE_AND_MODES_INFO, |
| 310 | key_operation_with_purpose_and_modes_info, |
| 311 | ); |
| 312 | } |
| 313 | |
| 314 | // Process the statistics related to key operations and return the two atom objects related to key |
| 315 | // operations: i) KeyOperationWithGeneralInfo ii) KeyOperationWithPurposeAndModesInfo |
| 316 | fn process_key_operation_event_stats( |
| 317 | sec_level: SecurityLevel, |
| 318 | key_purpose: KeyPurpose, |
| 319 | op_params: &[KeyParameter], |
| 320 | op_outcome: &Outcome, |
| 321 | key_upgraded: bool, |
| 322 | ) -> (KeystoreAtomPayload, KeystoreAtomPayload) { |
| 323 | let mut key_operation_with_general_info = KeyOperationWithGeneralInfo { |
| 324 | outcome: MetricsOutcome::OUTCOME_UNSPECIFIED, |
| 325 | error_code: 1, |
| 326 | security_level: MetricsSecurityLevel::SECURITY_LEVEL_UNSPECIFIED, |
| 327 | // Default for bool is false (for key_upgraded field). |
| 328 | ..Default::default() |
| 329 | }; |
| 330 | |
| 331 | let mut key_operation_with_purpose_and_modes_info = KeyOperationWithPurposeAndModesInfo { |
| 332 | purpose: MetricsPurpose::KEY_PURPOSE_UNSPECIFIED, |
| 333 | // Default for i32 is 0 (for the remaining bitmap fields). |
| 334 | ..Default::default() |
| 335 | }; |
| 336 | |
| 337 | key_operation_with_general_info.security_level = process_security_level(sec_level); |
| 338 | |
| 339 | key_operation_with_general_info.key_upgraded = key_upgraded; |
| 340 | |
| 341 | key_operation_with_purpose_and_modes_info.purpose = match key_purpose { |
| 342 | KeyPurpose::ENCRYPT => MetricsPurpose::ENCRYPT, |
| 343 | KeyPurpose::DECRYPT => MetricsPurpose::DECRYPT, |
| 344 | KeyPurpose::SIGN => MetricsPurpose::SIGN, |
| 345 | KeyPurpose::VERIFY => MetricsPurpose::VERIFY, |
| 346 | KeyPurpose::WRAP_KEY => MetricsPurpose::WRAP_KEY, |
| 347 | KeyPurpose::AGREE_KEY => MetricsPurpose::AGREE_KEY, |
| 348 | KeyPurpose::ATTEST_KEY => MetricsPurpose::ATTEST_KEY, |
| 349 | _ => MetricsPurpose::KEY_PURPOSE_UNSPECIFIED, |
| 350 | }; |
| 351 | |
| 352 | key_operation_with_general_info.outcome = match op_outcome { |
| 353 | Outcome::Unknown | Outcome::Dropped => MetricsOutcome::DROPPED, |
| 354 | Outcome::Success => MetricsOutcome::SUCCESS, |
| 355 | Outcome::Abort => MetricsOutcome::ABORT, |
| 356 | Outcome::Pruned => MetricsOutcome::PRUNED, |
| 357 | Outcome::ErrorCode(e) => { |
| 358 | key_operation_with_general_info.error_code = e.0; |
| 359 | MetricsOutcome::ERROR |
| 360 | } |
| 361 | }; |
| 362 | |
| 363 | for key_param in op_params.iter().map(KsKeyParamValue::from) { |
| 364 | match key_param { |
| 365 | KsKeyParamValue::PaddingMode(p) => { |
| 366 | compute_padding_mode_bitmap( |
| 367 | &mut key_operation_with_purpose_and_modes_info.padding_mode_bitmap, |
| 368 | p, |
| 369 | ); |
| 370 | } |
| 371 | KsKeyParamValue::Digest(d) => { |
| 372 | compute_digest_bitmap( |
| 373 | &mut key_operation_with_purpose_and_modes_info.digest_bitmap, |
| 374 | d, |
| 375 | ); |
| 376 | } |
| 377 | KsKeyParamValue::BlockMode(b) => { |
| 378 | compute_block_mode_bitmap( |
| 379 | &mut key_operation_with_purpose_and_modes_info.block_mode_bitmap, |
| 380 | b, |
| 381 | ); |
| 382 | } |
| 383 | _ => {} |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | ( |
| 388 | KeystoreAtomPayload::KeyOperationWithGeneralInfo(key_operation_with_general_info), |
| 389 | KeystoreAtomPayload::KeyOperationWithPurposeAndModesInfo( |
| 390 | key_operation_with_purpose_and_modes_info, |
| 391 | ), |
| 392 | ) |
| 393 | } |
| 394 | |
| 395 | fn process_security_level(sec_level: SecurityLevel) -> MetricsSecurityLevel { |
| 396 | match sec_level { |
| 397 | SecurityLevel::SOFTWARE => MetricsSecurityLevel::SECURITY_LEVEL_SOFTWARE, |
| 398 | SecurityLevel::TRUSTED_ENVIRONMENT => { |
| 399 | MetricsSecurityLevel::SECURITY_LEVEL_TRUSTED_ENVIRONMENT |
| 400 | } |
| 401 | SecurityLevel::STRONGBOX => MetricsSecurityLevel::SECURITY_LEVEL_STRONGBOX, |
| 402 | SecurityLevel::KEYSTORE => MetricsSecurityLevel::SECURITY_LEVEL_KEYSTORE, |
| 403 | _ => MetricsSecurityLevel::SECURITY_LEVEL_UNSPECIFIED, |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | fn compute_padding_mode_bitmap(padding_mode_bitmap: &mut i32, padding_mode: PaddingMode) { |
| 408 | match padding_mode { |
| 409 | PaddingMode::NONE => { |
| 410 | *padding_mode_bitmap |= 1 << PaddingModeBitPosition::NONE_BIT_POSITION as i32; |
| 411 | } |
| 412 | PaddingMode::RSA_OAEP => { |
| 413 | *padding_mode_bitmap |= 1 << PaddingModeBitPosition::RSA_OAEP_BIT_POS as i32; |
| 414 | } |
| 415 | PaddingMode::RSA_PSS => { |
| 416 | *padding_mode_bitmap |= 1 << PaddingModeBitPosition::RSA_PSS_BIT_POS as i32; |
| 417 | } |
| 418 | PaddingMode::RSA_PKCS1_1_5_ENCRYPT => { |
| 419 | *padding_mode_bitmap |= |
| 420 | 1 << PaddingModeBitPosition::RSA_PKCS1_1_5_ENCRYPT_BIT_POS as i32; |
| 421 | } |
| 422 | PaddingMode::RSA_PKCS1_1_5_SIGN => { |
| 423 | *padding_mode_bitmap |= 1 << PaddingModeBitPosition::RSA_PKCS1_1_5_SIGN_BIT_POS as i32; |
| 424 | } |
| 425 | PaddingMode::PKCS7 => { |
| 426 | *padding_mode_bitmap |= 1 << PaddingModeBitPosition::PKCS7_BIT_POS as i32; |
| 427 | } |
| 428 | _ => {} |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | fn compute_digest_bitmap(digest_bitmap: &mut i32, digest: Digest) { |
| 433 | match digest { |
| 434 | Digest::NONE => { |
| 435 | *digest_bitmap |= 1 << DigestBitPosition::NONE_BIT_POSITION as i32; |
| 436 | } |
| 437 | Digest::MD5 => { |
| 438 | *digest_bitmap |= 1 << DigestBitPosition::MD5_BIT_POS as i32; |
| 439 | } |
| 440 | Digest::SHA1 => { |
| 441 | *digest_bitmap |= 1 << DigestBitPosition::SHA_1_BIT_POS as i32; |
| 442 | } |
| 443 | Digest::SHA_2_224 => { |
| 444 | *digest_bitmap |= 1 << DigestBitPosition::SHA_2_224_BIT_POS as i32; |
| 445 | } |
| 446 | Digest::SHA_2_256 => { |
| 447 | *digest_bitmap |= 1 << DigestBitPosition::SHA_2_256_BIT_POS as i32; |
| 448 | } |
| 449 | Digest::SHA_2_384 => { |
| 450 | *digest_bitmap |= 1 << DigestBitPosition::SHA_2_384_BIT_POS as i32; |
| 451 | } |
| 452 | Digest::SHA_2_512 => { |
| 453 | *digest_bitmap |= 1 << DigestBitPosition::SHA_2_512_BIT_POS as i32; |
| 454 | } |
| 455 | _ => {} |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | fn compute_block_mode_bitmap(block_mode_bitmap: &mut i32, block_mode: BlockMode) { |
| 460 | match block_mode { |
| 461 | BlockMode::ECB => { |
| 462 | *block_mode_bitmap |= 1 << BlockModeBitPosition::ECB_BIT_POS as i32; |
| 463 | } |
| 464 | BlockMode::CBC => { |
| 465 | *block_mode_bitmap |= 1 << BlockModeBitPosition::CBC_BIT_POS as i32; |
| 466 | } |
| 467 | BlockMode::CTR => { |
| 468 | *block_mode_bitmap |= 1 << BlockModeBitPosition::CTR_BIT_POS as i32; |
| 469 | } |
| 470 | BlockMode::GCM => { |
| 471 | *block_mode_bitmap |= 1 << BlockModeBitPosition::GCM_BIT_POS as i32; |
| 472 | } |
| 473 | _ => {} |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | fn compute_purpose_bitmap(purpose_bitmap: &mut i32, purpose: KeyPurpose) { |
| 478 | match purpose { |
| 479 | KeyPurpose::ENCRYPT => { |
| 480 | *purpose_bitmap |= 1 << KeyPurposeBitPosition::ENCRYPT_BIT_POS as i32; |
| 481 | } |
| 482 | KeyPurpose::DECRYPT => { |
| 483 | *purpose_bitmap |= 1 << KeyPurposeBitPosition::DECRYPT_BIT_POS as i32; |
| 484 | } |
| 485 | KeyPurpose::SIGN => { |
| 486 | *purpose_bitmap |= 1 << KeyPurposeBitPosition::SIGN_BIT_POS as i32; |
| 487 | } |
| 488 | KeyPurpose::VERIFY => { |
| 489 | *purpose_bitmap |= 1 << KeyPurposeBitPosition::VERIFY_BIT_POS as i32; |
| 490 | } |
| 491 | KeyPurpose::WRAP_KEY => { |
| 492 | *purpose_bitmap |= 1 << KeyPurposeBitPosition::WRAP_KEY_BIT_POS as i32; |
| 493 | } |
| 494 | KeyPurpose::AGREE_KEY => { |
| 495 | *purpose_bitmap |= 1 << KeyPurposeBitPosition::AGREE_KEY_BIT_POS as i32; |
| 496 | } |
| 497 | KeyPurpose::ATTEST_KEY => { |
| 498 | *purpose_bitmap |= 1 << KeyPurposeBitPosition::ATTEST_KEY_BIT_POS as i32; |
| 499 | } |
| 500 | _ => {} |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | fn pull_storage_stats() -> Result<Vec<KeystoreAtom>> { |
| 505 | let mut atom_vec: Vec<KeystoreAtom> = Vec::new(); |
| 506 | let mut append = |stat| { |
| 507 | match stat { |
| 508 | Ok(s) => atom_vec.push(KeystoreAtom { |
| 509 | payload: KeystoreAtomPayload::StorageStats(s), |
| 510 | ..Default::default() |
| 511 | }), |
| 512 | Err(error) => { |
| 513 | log::error!("pull_metrics_callback: Error getting storage stat: {}", error) |
| 514 | } |
| 515 | }; |
| 516 | }; |
| 517 | DB.with(|db| { |
| 518 | let mut db = db.borrow_mut(); |
| 519 | append(db.get_storage_stat(MetricsStorage::DATABASE)); |
| 520 | append(db.get_storage_stat(MetricsStorage::KEY_ENTRY)); |
| 521 | append(db.get_storage_stat(MetricsStorage::KEY_ENTRY_ID_INDEX)); |
| 522 | append(db.get_storage_stat(MetricsStorage::KEY_ENTRY_DOMAIN_NAMESPACE_INDEX)); |
| 523 | append(db.get_storage_stat(MetricsStorage::BLOB_ENTRY)); |
| 524 | append(db.get_storage_stat(MetricsStorage::BLOB_ENTRY_KEY_ENTRY_ID_INDEX)); |
| 525 | append(db.get_storage_stat(MetricsStorage::KEY_PARAMETER)); |
| 526 | append(db.get_storage_stat(MetricsStorage::KEY_PARAMETER_KEY_ENTRY_ID_INDEX)); |
| 527 | append(db.get_storage_stat(MetricsStorage::KEY_METADATA)); |
| 528 | append(db.get_storage_stat(MetricsStorage::KEY_METADATA_KEY_ENTRY_ID_INDEX)); |
| 529 | append(db.get_storage_stat(MetricsStorage::GRANT)); |
| 530 | append(db.get_storage_stat(MetricsStorage::AUTH_TOKEN)); |
| 531 | append(db.get_storage_stat(MetricsStorage::BLOB_METADATA)); |
| 532 | append(db.get_storage_stat(MetricsStorage::BLOB_METADATA_BLOB_ENTRY_ID_INDEX)); |
| 533 | }); |
| 534 | Ok(atom_vec) |
| 535 | } |
| 536 | |
| 537 | /// Enum defining the bit position for each padding mode. Since padding mode can be repeatable, it |
| 538 | /// is represented using a bitmap. |
| 539 | #[allow(non_camel_case_types)] |
| 540 | #[repr(i32)] |
| 541 | enum PaddingModeBitPosition { |
| 542 | ///Bit position in the PaddingMode bitmap for NONE. |
| 543 | NONE_BIT_POSITION = 0, |
| 544 | ///Bit position in the PaddingMode bitmap for RSA_OAEP. |
| 545 | RSA_OAEP_BIT_POS = 1, |
| 546 | ///Bit position in the PaddingMode bitmap for RSA_PSS. |
| 547 | RSA_PSS_BIT_POS = 2, |
| 548 | ///Bit position in the PaddingMode bitmap for RSA_PKCS1_1_5_ENCRYPT. |
| 549 | RSA_PKCS1_1_5_ENCRYPT_BIT_POS = 3, |
| 550 | ///Bit position in the PaddingMode bitmap for RSA_PKCS1_1_5_SIGN. |
| 551 | RSA_PKCS1_1_5_SIGN_BIT_POS = 4, |
| 552 | ///Bit position in the PaddingMode bitmap for RSA_PKCS7. |
| 553 | PKCS7_BIT_POS = 5, |
| 554 | } |
| 555 | |
| 556 | /// Enum defining the bit position for each digest type. Since digest can be repeatable in |
| 557 | /// key parameters, it is represented using a bitmap. |
| 558 | #[allow(non_camel_case_types)] |
| 559 | #[repr(i32)] |
| 560 | enum DigestBitPosition { |
| 561 | ///Bit position in the Digest bitmap for NONE. |
| 562 | NONE_BIT_POSITION = 0, |
| 563 | ///Bit position in the Digest bitmap for MD5. |
| 564 | MD5_BIT_POS = 1, |
| 565 | ///Bit position in the Digest bitmap for SHA1. |
| 566 | SHA_1_BIT_POS = 2, |
| 567 | ///Bit position in the Digest bitmap for SHA_2_224. |
| 568 | SHA_2_224_BIT_POS = 3, |
| 569 | ///Bit position in the Digest bitmap for SHA_2_256. |
| 570 | SHA_2_256_BIT_POS = 4, |
| 571 | ///Bit position in the Digest bitmap for SHA_2_384. |
| 572 | SHA_2_384_BIT_POS = 5, |
| 573 | ///Bit position in the Digest bitmap for SHA_2_512. |
| 574 | SHA_2_512_BIT_POS = 6, |
| 575 | } |
| 576 | |
| 577 | /// Enum defining the bit position for each block mode type. Since block mode can be repeatable in |
| 578 | /// key parameters, it is represented using a bitmap. |
| 579 | #[allow(non_camel_case_types)] |
| 580 | #[repr(i32)] |
| 581 | enum BlockModeBitPosition { |
| 582 | ///Bit position in the BlockMode bitmap for ECB. |
| 583 | ECB_BIT_POS = 1, |
| 584 | ///Bit position in the BlockMode bitmap for CBC. |
| 585 | CBC_BIT_POS = 2, |
| 586 | ///Bit position in the BlockMode bitmap for CTR. |
| 587 | CTR_BIT_POS = 3, |
| 588 | ///Bit position in the BlockMode bitmap for GCM. |
| 589 | GCM_BIT_POS = 4, |
| 590 | } |
| 591 | |
| 592 | /// Enum defining the bit position for each key purpose. Since key purpose can be repeatable in |
| 593 | /// key parameters, it is represented using a bitmap. |
| 594 | #[allow(non_camel_case_types)] |
| 595 | #[repr(i32)] |
| 596 | enum KeyPurposeBitPosition { |
| 597 | ///Bit position in the KeyPurpose bitmap for Encrypt. |
| 598 | ENCRYPT_BIT_POS = 1, |
| 599 | ///Bit position in the KeyPurpose bitmap for Decrypt. |
| 600 | DECRYPT_BIT_POS = 2, |
| 601 | ///Bit position in the KeyPurpose bitmap for Sign. |
| 602 | SIGN_BIT_POS = 3, |
| 603 | ///Bit position in the KeyPurpose bitmap for Verify. |
| 604 | VERIFY_BIT_POS = 4, |
| 605 | ///Bit position in the KeyPurpose bitmap for Wrap Key. |
| 606 | WRAP_KEY_BIT_POS = 5, |
| 607 | ///Bit position in the KeyPurpose bitmap for Agree Key. |
| 608 | AGREE_KEY_BIT_POS = 6, |
| 609 | ///Bit position in the KeyPurpose bitmap for Attest Key. |
| 610 | ATTEST_KEY_BIT_POS = 7, |
| 611 | } |