Hasini Gunasinghe | b714297 | 2021-02-20 03:11: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 module provides convenience functions for keystore2 logging. |
| 16 | use crate::error::get_error_code; |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 17 | use crate::globals::DB; |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 18 | use crate::key_parameter::KeyParameterValue as KsKeyParamValue; |
Hasini Gunasinghe | 0aba68a | 2021-03-19 00:43:52 +0000 | [diff] [blame] | 19 | use crate::operation::Outcome; |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 20 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
| 21 | Algorithm::Algorithm, BlockMode::BlockMode, Digest::Digest, EcCurve::EcCurve, |
| 22 | HardwareAuthenticatorType::HardwareAuthenticatorType, KeyOrigin::KeyOrigin, |
| 23 | KeyParameter::KeyParameter, KeyPurpose::KeyPurpose, PaddingMode::PaddingMode, |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 24 | SecurityLevel::SecurityLevel, |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 25 | }; |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 26 | use anyhow::Result; |
| 27 | use keystore2_system_property::PropertyWatcher; |
| 28 | use statslog_rust::{ |
| 29 | keystore2_key_creation_event_reported::{ |
| 30 | Algorithm as StatsdAlgorithm, EcCurve as StatsdEcCurve, KeyOrigin as StatsdKeyOrigin, |
| 31 | Keystore2KeyCreationEventReported, SecurityLevel as StatsdKeyCreationSecurityLevel, |
| 32 | UserAuthType as StatsdUserAuthType, |
| 33 | }, |
| 34 | keystore2_key_operation_event_reported::{ |
| 35 | Keystore2KeyOperationEventReported, Outcome as StatsdOutcome, Purpose as StatsdKeyPurpose, |
| 36 | SecurityLevel as StatsdKeyOperationSecurityLevel, |
| 37 | }, |
| 38 | keystore2_storage_stats::StorageType as StatsdStorageType, |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 39 | }; |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 40 | use statslog_rust_header::Atoms; |
| 41 | use statspull_rust::{set_pull_atom_callback, StatsPullResult}; |
Hasini Gunasinghe | 0aba68a | 2021-03-19 00:43:52 +0000 | [diff] [blame] | 42 | |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 43 | fn create_default_key_creation_atom() -> Keystore2KeyCreationEventReported { |
| 44 | // If a value is not present, fields represented by bitmaps and i32 fields |
| 45 | // will take 0, except error_code which defaults to 1 indicating NO_ERROR and key_size, |
| 46 | // and auth_time_out which default to -1. |
| 47 | // The boolean fields are set to false by default. |
| 48 | // Some keymint enums do have 0 as an enum variant value. In such cases, the corresponding |
| 49 | // enum variant value in atoms.proto is incremented by 1, in order to have 0 as the reserved |
| 50 | // value for unspecified fields. |
| 51 | Keystore2KeyCreationEventReported { |
| 52 | algorithm: StatsdAlgorithm::AlgorithmUnspecified, |
| 53 | key_size: -1, |
| 54 | key_origin: StatsdKeyOrigin::OriginUnspecified, |
| 55 | user_auth_type: StatsdUserAuthType::AuthTypeUnspecified, |
| 56 | user_auth_key_timeout_seconds: -1, |
| 57 | padding_mode_bitmap: 0, |
| 58 | digest_bitmap: 0, |
| 59 | block_mode_bitmap: 0, |
| 60 | purpose_bitmap: 0, |
| 61 | ec_curve: StatsdEcCurve::EcCurveUnspecified, |
| 62 | // as per keystore2/ResponseCode.aidl, 1 is reserved for NO_ERROR |
| 63 | error_code: 1, |
| 64 | attestation_requested: false, |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 65 | security_level: StatsdKeyCreationSecurityLevel::SecurityLevelUnspecified, |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Hasini Gunasinghe | 0aba68a | 2021-03-19 00:43:52 +0000 | [diff] [blame] | 69 | fn create_default_key_operation_atom() -> Keystore2KeyOperationEventReported { |
| 70 | Keystore2KeyOperationEventReported { |
| 71 | purpose: StatsdKeyPurpose::KeyPurposeUnspecified, |
| 72 | padding_mode_bitmap: 0, |
| 73 | digest_bitmap: 0, |
| 74 | block_mode_bitmap: 0, |
| 75 | outcome: StatsdOutcome::OutcomeUnspecified, |
| 76 | error_code: 1, |
| 77 | key_upgraded: false, |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 78 | security_level: StatsdKeyOperationSecurityLevel::SecurityLevelUnspecified, |
Hasini Gunasinghe | 0aba68a | 2021-03-19 00:43:52 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | /// Log key creation events via statsd API. |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 83 | pub fn log_key_creation_event_stats<U>( |
| 84 | sec_level: SecurityLevel, |
| 85 | key_params: &[KeyParameter], |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 86 | result: &Result<U>, |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 87 | ) { |
| 88 | let key_creation_event_stats = |
| 89 | construct_key_creation_event_stats(sec_level, key_params, result); |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 90 | |
| 91 | let logging_result = key_creation_event_stats.stats_write(); |
| 92 | |
| 93 | if let Err(e) = logging_result { |
Hasini Gunasinghe | 0aba68a | 2021-03-19 00:43:52 +0000 | [diff] [blame] | 94 | log::error!( |
| 95 | "In log_key_creation_event_stats. Error in logging key creation event. {:?}", |
| 96 | e |
| 97 | ); |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Hasini Gunasinghe | 0aba68a | 2021-03-19 00:43:52 +0000 | [diff] [blame] | 101 | /// Log key operation events via statsd API. |
| 102 | pub fn log_key_operation_event_stats( |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 103 | sec_level: SecurityLevel, |
Hasini Gunasinghe | 0aba68a | 2021-03-19 00:43:52 +0000 | [diff] [blame] | 104 | key_purpose: KeyPurpose, |
| 105 | op_params: &[KeyParameter], |
| 106 | op_outcome: &Outcome, |
| 107 | key_upgraded: bool, |
| 108 | ) { |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 109 | let key_operation_event_stats = construct_key_operation_event_stats( |
| 110 | sec_level, |
| 111 | key_purpose, |
| 112 | op_params, |
| 113 | op_outcome, |
| 114 | key_upgraded, |
| 115 | ); |
Hasini Gunasinghe | 0aba68a | 2021-03-19 00:43:52 +0000 | [diff] [blame] | 116 | |
| 117 | let logging_result = key_operation_event_stats.stats_write(); |
| 118 | |
| 119 | if let Err(e) = logging_result { |
| 120 | log::error!( |
| 121 | "In log_key_operation_event_stats. Error in logging key operation event. {:?}", |
| 122 | e |
| 123 | ); |
| 124 | } |
| 125 | } |
| 126 | |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 127 | fn construct_key_creation_event_stats<U>( |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 128 | sec_level: SecurityLevel, |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 129 | key_params: &[KeyParameter], |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 130 | result: &Result<U>, |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 131 | ) -> Keystore2KeyCreationEventReported { |
| 132 | let mut key_creation_event_atom = create_default_key_creation_atom(); |
| 133 | |
| 134 | if let Err(ref e) = result { |
| 135 | key_creation_event_atom.error_code = get_error_code(e); |
| 136 | } |
| 137 | |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 138 | key_creation_event_atom.security_level = match sec_level { |
| 139 | SecurityLevel::SOFTWARE => StatsdKeyCreationSecurityLevel::SecurityLevelSoftware, |
| 140 | SecurityLevel::TRUSTED_ENVIRONMENT => { |
| 141 | StatsdKeyCreationSecurityLevel::SecurityLevelTrustedEnvironment |
| 142 | } |
| 143 | SecurityLevel::STRONGBOX => StatsdKeyCreationSecurityLevel::SecurityLevelStrongbox, |
| 144 | //KEYSTORE is not a valid variant here |
| 145 | _ => StatsdKeyCreationSecurityLevel::SecurityLevelUnspecified, |
| 146 | }; |
| 147 | |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 148 | for key_param in key_params.iter().map(KsKeyParamValue::from) { |
| 149 | match key_param { |
| 150 | KsKeyParamValue::Algorithm(a) => { |
| 151 | key_creation_event_atom.algorithm = match a { |
| 152 | Algorithm::RSA => StatsdAlgorithm::Rsa, |
| 153 | Algorithm::EC => StatsdAlgorithm::Ec, |
| 154 | Algorithm::AES => StatsdAlgorithm::Aes, |
| 155 | Algorithm::TRIPLE_DES => StatsdAlgorithm::TripleDes, |
| 156 | Algorithm::HMAC => StatsdAlgorithm::Hmac, |
| 157 | _ => StatsdAlgorithm::AlgorithmUnspecified, |
| 158 | } |
| 159 | } |
| 160 | KsKeyParamValue::KeySize(s) => { |
| 161 | key_creation_event_atom.key_size = s; |
| 162 | } |
| 163 | KsKeyParamValue::KeyOrigin(o) => { |
| 164 | key_creation_event_atom.key_origin = match o { |
| 165 | KeyOrigin::GENERATED => StatsdKeyOrigin::Generated, |
| 166 | KeyOrigin::DERIVED => StatsdKeyOrigin::Derived, |
| 167 | KeyOrigin::IMPORTED => StatsdKeyOrigin::Imported, |
| 168 | KeyOrigin::RESERVED => StatsdKeyOrigin::Reserved, |
| 169 | KeyOrigin::SECURELY_IMPORTED => StatsdKeyOrigin::SecurelyImported, |
| 170 | _ => StatsdKeyOrigin::OriginUnspecified, |
| 171 | } |
| 172 | } |
| 173 | KsKeyParamValue::HardwareAuthenticatorType(a) => { |
| 174 | key_creation_event_atom.user_auth_type = match a { |
| 175 | HardwareAuthenticatorType::NONE => StatsdUserAuthType::None, |
| 176 | HardwareAuthenticatorType::PASSWORD => StatsdUserAuthType::Password, |
| 177 | HardwareAuthenticatorType::FINGERPRINT => StatsdUserAuthType::Fingerprint, |
| 178 | HardwareAuthenticatorType::ANY => StatsdUserAuthType::Any, |
| 179 | _ => StatsdUserAuthType::AuthTypeUnspecified, |
| 180 | } |
| 181 | } |
| 182 | KsKeyParamValue::AuthTimeout(t) => { |
| 183 | key_creation_event_atom.user_auth_key_timeout_seconds = t; |
| 184 | } |
| 185 | KsKeyParamValue::PaddingMode(p) => { |
| 186 | key_creation_event_atom.padding_mode_bitmap = |
| 187 | compute_padding_mode_bitmap(&key_creation_event_atom.padding_mode_bitmap, p); |
| 188 | } |
| 189 | KsKeyParamValue::Digest(d) => { |
| 190 | key_creation_event_atom.digest_bitmap = |
| 191 | compute_digest_bitmap(&key_creation_event_atom.digest_bitmap, d); |
| 192 | } |
| 193 | KsKeyParamValue::BlockMode(b) => { |
| 194 | key_creation_event_atom.block_mode_bitmap = |
| 195 | compute_block_mode_bitmap(&key_creation_event_atom.block_mode_bitmap, b); |
| 196 | } |
| 197 | KsKeyParamValue::KeyPurpose(k) => { |
| 198 | key_creation_event_atom.purpose_bitmap = |
| 199 | compute_purpose_bitmap(&key_creation_event_atom.purpose_bitmap, k); |
| 200 | } |
| 201 | KsKeyParamValue::EcCurve(e) => { |
| 202 | key_creation_event_atom.ec_curve = match e { |
| 203 | EcCurve::P_224 => StatsdEcCurve::P224, |
| 204 | EcCurve::P_256 => StatsdEcCurve::P256, |
| 205 | EcCurve::P_384 => StatsdEcCurve::P384, |
| 206 | EcCurve::P_521 => StatsdEcCurve::P521, |
| 207 | _ => StatsdEcCurve::EcCurveUnspecified, |
| 208 | } |
| 209 | } |
| 210 | KsKeyParamValue::AttestationChallenge(_) => { |
| 211 | key_creation_event_atom.attestation_requested = true; |
| 212 | } |
| 213 | _ => {} |
| 214 | } |
| 215 | } |
| 216 | key_creation_event_atom |
| 217 | } |
| 218 | |
Hasini Gunasinghe | 0aba68a | 2021-03-19 00:43:52 +0000 | [diff] [blame] | 219 | fn construct_key_operation_event_stats( |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 220 | sec_level: SecurityLevel, |
Hasini Gunasinghe | 0aba68a | 2021-03-19 00:43:52 +0000 | [diff] [blame] | 221 | key_purpose: KeyPurpose, |
| 222 | op_params: &[KeyParameter], |
| 223 | op_outcome: &Outcome, |
| 224 | key_upgraded: bool, |
| 225 | ) -> Keystore2KeyOperationEventReported { |
| 226 | let mut key_operation_event_atom = create_default_key_operation_atom(); |
| 227 | |
Hasini Gunasinghe | 9617fd9 | 2021-04-01 22:27:07 +0000 | [diff] [blame] | 228 | key_operation_event_atom.security_level = match sec_level { |
| 229 | SecurityLevel::SOFTWARE => StatsdKeyOperationSecurityLevel::SecurityLevelSoftware, |
| 230 | SecurityLevel::TRUSTED_ENVIRONMENT => { |
| 231 | StatsdKeyOperationSecurityLevel::SecurityLevelTrustedEnvironment |
| 232 | } |
| 233 | SecurityLevel::STRONGBOX => StatsdKeyOperationSecurityLevel::SecurityLevelStrongbox, |
| 234 | //KEYSTORE is not a valid variant here |
| 235 | _ => StatsdKeyOperationSecurityLevel::SecurityLevelUnspecified, |
| 236 | }; |
| 237 | |
Hasini Gunasinghe | 0aba68a | 2021-03-19 00:43:52 +0000 | [diff] [blame] | 238 | key_operation_event_atom.key_upgraded = key_upgraded; |
| 239 | |
| 240 | key_operation_event_atom.purpose = match key_purpose { |
| 241 | KeyPurpose::ENCRYPT => StatsdKeyPurpose::Encrypt, |
| 242 | KeyPurpose::DECRYPT => StatsdKeyPurpose::Decrypt, |
| 243 | KeyPurpose::SIGN => StatsdKeyPurpose::Sign, |
| 244 | KeyPurpose::VERIFY => StatsdKeyPurpose::Verify, |
| 245 | KeyPurpose::WRAP_KEY => StatsdKeyPurpose::WrapKey, |
| 246 | KeyPurpose::AGREE_KEY => StatsdKeyPurpose::AgreeKey, |
| 247 | KeyPurpose::ATTEST_KEY => StatsdKeyPurpose::AttestKey, |
| 248 | _ => StatsdKeyPurpose::KeyPurposeUnspecified, |
| 249 | }; |
| 250 | |
| 251 | key_operation_event_atom.outcome = match op_outcome { |
| 252 | Outcome::Unknown | Outcome::Dropped => StatsdOutcome::Dropped, |
| 253 | Outcome::Success => StatsdOutcome::Success, |
| 254 | Outcome::Abort => StatsdOutcome::Abort, |
| 255 | Outcome::Pruned => StatsdOutcome::Pruned, |
| 256 | Outcome::ErrorCode(e) => { |
| 257 | key_operation_event_atom.error_code = e.0; |
| 258 | StatsdOutcome::Error |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | for key_param in op_params.iter().map(KsKeyParamValue::from) { |
| 263 | match key_param { |
| 264 | KsKeyParamValue::PaddingMode(p) => { |
| 265 | key_operation_event_atom.padding_mode_bitmap = |
| 266 | compute_padding_mode_bitmap(&key_operation_event_atom.padding_mode_bitmap, p); |
| 267 | } |
| 268 | KsKeyParamValue::Digest(d) => { |
| 269 | key_operation_event_atom.digest_bitmap = |
| 270 | compute_digest_bitmap(&key_operation_event_atom.digest_bitmap, d); |
| 271 | } |
| 272 | KsKeyParamValue::BlockMode(b) => { |
| 273 | key_operation_event_atom.block_mode_bitmap = |
| 274 | compute_block_mode_bitmap(&key_operation_event_atom.block_mode_bitmap, b); |
| 275 | } |
| 276 | _ => {} |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | key_operation_event_atom |
| 281 | } |
| 282 | |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 283 | fn compute_purpose_bitmap(purpose_bitmap: &i32, purpose: KeyPurpose) -> i32 { |
| 284 | let mut bitmap = *purpose_bitmap; |
| 285 | match purpose { |
| 286 | KeyPurpose::ENCRYPT => { |
| 287 | bitmap |= 1 << KeyPurposeBitPosition::ENCRYPT_BIT_POS as i32; |
| 288 | } |
| 289 | KeyPurpose::DECRYPT => { |
| 290 | bitmap |= 1 << KeyPurposeBitPosition::DECRYPT_BIT_POS as i32; |
| 291 | } |
| 292 | KeyPurpose::SIGN => { |
| 293 | bitmap |= 1 << KeyPurposeBitPosition::SIGN_BIT_POS as i32; |
| 294 | } |
| 295 | KeyPurpose::VERIFY => { |
| 296 | bitmap |= 1 << KeyPurposeBitPosition::VERIFY_BIT_POS as i32; |
| 297 | } |
| 298 | KeyPurpose::WRAP_KEY => { |
| 299 | bitmap |= 1 << KeyPurposeBitPosition::WRAP_KEY_BIT_POS as i32; |
| 300 | } |
| 301 | KeyPurpose::AGREE_KEY => { |
| 302 | bitmap |= 1 << KeyPurposeBitPosition::AGREE_KEY_BIT_POS as i32; |
| 303 | } |
| 304 | KeyPurpose::ATTEST_KEY => { |
| 305 | bitmap |= 1 << KeyPurposeBitPosition::ATTEST_KEY_BIT_POS as i32; |
| 306 | } |
| 307 | _ => {} |
| 308 | } |
| 309 | bitmap |
| 310 | } |
| 311 | |
| 312 | fn compute_padding_mode_bitmap(padding_mode_bitmap: &i32, padding_mode: PaddingMode) -> i32 { |
| 313 | let mut bitmap = *padding_mode_bitmap; |
| 314 | match padding_mode { |
| 315 | PaddingMode::NONE => { |
| 316 | bitmap |= 1 << PaddingModeBitPosition::NONE_BIT_POSITION as i32; |
| 317 | } |
| 318 | PaddingMode::RSA_OAEP => { |
| 319 | bitmap |= 1 << PaddingModeBitPosition::RSA_OAEP_BIT_POS as i32; |
| 320 | } |
| 321 | PaddingMode::RSA_PSS => { |
| 322 | bitmap |= 1 << PaddingModeBitPosition::RSA_PSS_BIT_POS as i32; |
| 323 | } |
| 324 | PaddingMode::RSA_PKCS1_1_5_ENCRYPT => { |
| 325 | bitmap |= 1 << PaddingModeBitPosition::RSA_PKCS1_1_5_ENCRYPT_BIT_POS as i32; |
| 326 | } |
| 327 | PaddingMode::RSA_PKCS1_1_5_SIGN => { |
| 328 | bitmap |= 1 << PaddingModeBitPosition::RSA_PKCS1_1_5_SIGN_BIT_POS as i32; |
| 329 | } |
| 330 | PaddingMode::PKCS7 => { |
| 331 | bitmap |= 1 << PaddingModeBitPosition::PKCS7_BIT_POS as i32; |
| 332 | } |
| 333 | _ => {} |
| 334 | } |
| 335 | bitmap |
| 336 | } |
| 337 | |
| 338 | fn compute_digest_bitmap(digest_bitmap: &i32, digest: Digest) -> i32 { |
| 339 | let mut bitmap = *digest_bitmap; |
| 340 | match digest { |
| 341 | Digest::NONE => { |
| 342 | bitmap |= 1 << DigestBitPosition::NONE_BIT_POSITION as i32; |
| 343 | } |
| 344 | Digest::MD5 => { |
| 345 | bitmap |= 1 << DigestBitPosition::MD5_BIT_POS as i32; |
| 346 | } |
| 347 | Digest::SHA1 => { |
| 348 | bitmap |= 1 << DigestBitPosition::SHA_1_BIT_POS as i32; |
| 349 | } |
| 350 | Digest::SHA_2_224 => { |
| 351 | bitmap |= 1 << DigestBitPosition::SHA_2_224_BIT_POS as i32; |
| 352 | } |
| 353 | Digest::SHA_2_256 => { |
| 354 | bitmap |= 1 << DigestBitPosition::SHA_2_256_BIT_POS as i32; |
| 355 | } |
| 356 | Digest::SHA_2_384 => { |
| 357 | bitmap |= 1 << DigestBitPosition::SHA_2_384_BIT_POS as i32; |
| 358 | } |
| 359 | Digest::SHA_2_512 => { |
| 360 | bitmap |= 1 << DigestBitPosition::SHA_2_512_BIT_POS as i32; |
| 361 | } |
| 362 | _ => {} |
| 363 | } |
| 364 | bitmap |
| 365 | } |
| 366 | |
| 367 | fn compute_block_mode_bitmap(block_mode_bitmap: &i32, block_mode: BlockMode) -> i32 { |
| 368 | let mut bitmap = *block_mode_bitmap; |
| 369 | match block_mode { |
| 370 | BlockMode::ECB => { |
| 371 | bitmap |= 1 << BlockModeBitPosition::ECB_BIT_POS as i32; |
| 372 | } |
| 373 | BlockMode::CBC => { |
| 374 | bitmap |= 1 << BlockModeBitPosition::CBC_BIT_POS as i32; |
| 375 | } |
| 376 | BlockMode::CTR => { |
| 377 | bitmap |= 1 << BlockModeBitPosition::CTR_BIT_POS as i32; |
| 378 | } |
| 379 | BlockMode::GCM => { |
| 380 | bitmap |= 1 << BlockModeBitPosition::GCM_BIT_POS as i32; |
| 381 | } |
| 382 | _ => {} |
| 383 | } |
| 384 | bitmap |
| 385 | } |
Seth Moore | 78c091f | 2021-04-09 21:38:30 +0000 | [diff] [blame] | 386 | |
| 387 | /// Registers pull metrics callbacks |
| 388 | pub fn register_pull_metrics_callbacks() -> Result<()> { |
| 389 | // Before registering the callbacks with statsd, we have to wait for the system to finish |
| 390 | // booting up. This avoids possible races that may occur at startup. For example, statsd |
| 391 | // depends on a companion service, and if registration happens too soon it will fail since |
| 392 | // the companion service isn't up yet. |
| 393 | let mut watcher = PropertyWatcher::new("sys.boot_completed")?; |
| 394 | loop { |
| 395 | watcher.wait()?; |
| 396 | let value = watcher.read(|_name, value| Ok(value.trim().to_string())); |
| 397 | if value? == "1" { |
| 398 | set_pull_atom_callback(Atoms::Keystore2StorageStats, None, pull_metrics_callback); |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | Ok(()) |
| 403 | } |
| 404 | |
| 405 | fn pull_metrics_callback() -> StatsPullResult { |
| 406 | let mut result = StatsPullResult::new(); |
| 407 | let mut append = |stat| { |
| 408 | match stat { |
| 409 | Ok(s) => result.push(Box::new(s)), |
| 410 | Err(error) => { |
| 411 | log::error!("pull_metrics_callback: Error getting storage stat: {}", error) |
| 412 | } |
| 413 | }; |
| 414 | }; |
| 415 | DB.with(|db| { |
| 416 | let mut db = db.borrow_mut(); |
| 417 | append(db.get_storage_stat(StatsdStorageType::Database)); |
| 418 | append(db.get_storage_stat(StatsdStorageType::KeyEntry)); |
| 419 | append(db.get_storage_stat(StatsdStorageType::KeyEntryIdIndex)); |
| 420 | append(db.get_storage_stat(StatsdStorageType::KeyEntryDomainNamespaceIndex)); |
| 421 | append(db.get_storage_stat(StatsdStorageType::BlobEntry)); |
| 422 | append(db.get_storage_stat(StatsdStorageType::BlobEntryKeyEntryIdIndex)); |
| 423 | append(db.get_storage_stat(StatsdStorageType::KeyParameter)); |
| 424 | append(db.get_storage_stat(StatsdStorageType::KeyParameterKeyEntryIdIndex)); |
| 425 | append(db.get_storage_stat(StatsdStorageType::KeyMetadata)); |
| 426 | append(db.get_storage_stat(StatsdStorageType::KeyMetadataKeyEntryIdIndex)); |
| 427 | append(db.get_storage_stat(StatsdStorageType::Grant)); |
| 428 | append(db.get_storage_stat(StatsdStorageType::AuthToken)); |
| 429 | append(db.get_storage_stat(StatsdStorageType::BlobMetadata)); |
| 430 | append(db.get_storage_stat(StatsdStorageType::BlobMetadataBlobEntryIdIndex)); |
| 431 | }); |
| 432 | result |
| 433 | } |
| 434 | |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 435 | /// Enum defining the bit position for each padding mode. Since padding mode can be repeatable, it |
| 436 | /// is represented using a bitmap. |
| 437 | #[allow(non_camel_case_types)] |
| 438 | #[repr(i32)] |
| 439 | pub enum PaddingModeBitPosition { |
| 440 | ///Bit position in the PaddingMode bitmap for NONE. |
| 441 | NONE_BIT_POSITION = 0, |
| 442 | ///Bit position in the PaddingMode bitmap for RSA_OAEP. |
| 443 | RSA_OAEP_BIT_POS = 1, |
| 444 | ///Bit position in the PaddingMode bitmap for RSA_PSS. |
| 445 | RSA_PSS_BIT_POS = 2, |
| 446 | ///Bit position in the PaddingMode bitmap for RSA_PKCS1_1_5_ENCRYPT. |
| 447 | RSA_PKCS1_1_5_ENCRYPT_BIT_POS = 3, |
| 448 | ///Bit position in the PaddingMode bitmap for RSA_PKCS1_1_5_SIGN. |
| 449 | RSA_PKCS1_1_5_SIGN_BIT_POS = 4, |
| 450 | ///Bit position in the PaddingMode bitmap for RSA_PKCS7. |
| 451 | PKCS7_BIT_POS = 5, |
| 452 | } |
| 453 | |
| 454 | /// Enum defining the bit position for each digest type. Since digest can be repeatable in |
| 455 | /// key parameters, it is represented using a bitmap. |
| 456 | #[allow(non_camel_case_types)] |
| 457 | #[repr(i32)] |
| 458 | pub enum DigestBitPosition { |
| 459 | ///Bit position in the Digest bitmap for NONE. |
| 460 | NONE_BIT_POSITION = 0, |
| 461 | ///Bit position in the Digest bitmap for MD5. |
| 462 | MD5_BIT_POS = 1, |
| 463 | ///Bit position in the Digest bitmap for SHA1. |
| 464 | SHA_1_BIT_POS = 2, |
| 465 | ///Bit position in the Digest bitmap for SHA_2_224. |
| 466 | SHA_2_224_BIT_POS = 3, |
| 467 | ///Bit position in the Digest bitmap for SHA_2_256. |
| 468 | SHA_2_256_BIT_POS = 4, |
| 469 | ///Bit position in the Digest bitmap for SHA_2_384. |
| 470 | SHA_2_384_BIT_POS = 5, |
| 471 | ///Bit position in the Digest bitmap for SHA_2_512. |
| 472 | SHA_2_512_BIT_POS = 6, |
| 473 | } |
| 474 | |
| 475 | /// Enum defining the bit position for each block mode type. Since block mode can be repeatable in |
| 476 | /// key parameters, it is represented using a bitmap. |
| 477 | #[allow(non_camel_case_types)] |
| 478 | #[repr(i32)] |
| 479 | enum BlockModeBitPosition { |
| 480 | ///Bit position in the BlockMode bitmap for ECB. |
| 481 | ECB_BIT_POS = 1, |
| 482 | ///Bit position in the BlockMode bitmap for CBC. |
| 483 | CBC_BIT_POS = 2, |
| 484 | ///Bit position in the BlockMode bitmap for CTR. |
| 485 | CTR_BIT_POS = 3, |
| 486 | ///Bit position in the BlockMode bitmap for GCM. |
| 487 | GCM_BIT_POS = 4, |
| 488 | } |
| 489 | |
| 490 | /// Enum defining the bit position for each key purpose. Since key purpose can be repeatable in |
| 491 | /// key parameters, it is represented using a bitmap. |
| 492 | #[allow(non_camel_case_types)] |
| 493 | #[repr(i32)] |
| 494 | enum KeyPurposeBitPosition { |
| 495 | ///Bit position in the KeyPurpose bitmap for Encrypt. |
| 496 | ENCRYPT_BIT_POS = 1, |
| 497 | ///Bit position in the KeyPurpose bitmap for Decrypt. |
| 498 | DECRYPT_BIT_POS = 2, |
| 499 | ///Bit position in the KeyPurpose bitmap for Sign. |
| 500 | SIGN_BIT_POS = 3, |
| 501 | ///Bit position in the KeyPurpose bitmap for Verify. |
| 502 | VERIFY_BIT_POS = 4, |
| 503 | ///Bit position in the KeyPurpose bitmap for Wrap Key. |
| 504 | WRAP_KEY_BIT_POS = 5, |
| 505 | ///Bit position in the KeyPurpose bitmap for Agree Key. |
| 506 | AGREE_KEY_BIT_POS = 6, |
| 507 | ///Bit position in the KeyPurpose bitmap for Attest Key. |
| 508 | ATTEST_KEY_BIT_POS = 7, |
| 509 | } |