Hasini Gunasinghe | 0dab3eb | 2020-06-15 16:40:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #define LOG_TAG "KeystoreOperation" |
| 17 | |
| 18 | #include "key_operation_log_handler.h" |
| 19 | #include "key_creation_log_handler.h" |
| 20 | |
| 21 | #include <keystore/keystore_hidl_support.h> |
| 22 | #include <statslog.h> |
| 23 | |
| 24 | namespace keystore { |
| 25 | |
| 26 | template <typename Tag> |
| 27 | int32_t getOptionalEnumTagValue(const AuthorizationSet& authorization_set, Tag tag) { |
| 28 | auto tagValue = authorization_set.GetTagValue(tag); |
| 29 | if (tagValue.isOk()) { |
| 30 | static_assert(sizeof(decltype(tagValue.value())) <= sizeof(int32_t), |
| 31 | "Tag type value will be truncated, if cast to int32_t"); |
| 32 | return static_cast<int32_t>(tagValue.value()); |
| 33 | } |
| 34 | //-1 is an invalid value for all enum types. |
| 35 | return -1; |
| 36 | } |
| 37 | |
| 38 | int32_t generateBitMapForPaddingModeValue(const AuthorizationSet& authorization_set) { |
| 39 | auto tagValue = authorization_set.GetTagValue(TAG_PADDING); |
| 40 | if (tagValue.isOk()) { |
| 41 | auto value = tagValue.value(); |
| 42 | switch (value) { |
| 43 | case PaddingMode::NONE: |
| 44 | return (1 << NONE_BIT_POS); |
| 45 | case PaddingMode::RSA_OAEP: |
| 46 | return (1 << PaddingModeBitPosition::RSA_OAEP_BIT_POS); |
| 47 | case PaddingMode::RSA_PSS: |
| 48 | return (1 << PaddingModeBitPosition::RSA_PSS_BIT_POS); |
| 49 | case PaddingMode::RSA_PKCS1_1_5_ENCRYPT: |
| 50 | return (1 << PaddingModeBitPosition::RSA_PKCS1_1_5_ENCRYPT_BIT_POS); |
| 51 | case PaddingMode::RSA_PKCS1_1_5_SIGN: |
| 52 | return (1 << PaddingModeBitPosition::RSA_PKCS1_1_5_SIGN_BIT_POS); |
| 53 | case PaddingMode::PKCS7: |
| 54 | return (1 << PaddingModeBitPosition::PKCS7_BIT_POS); |
| 55 | default: |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | // unlike in the single enum fields, if no value is provided, |
| 60 | // 0 is set for the bitmap |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | int32_t generateBitMapForDigestValue(const AuthorizationSet& authorization_set) { |
| 65 | auto tagValue = authorization_set.GetTagValue(TAG_DIGEST); |
| 66 | if (tagValue.isOk()) { |
| 67 | auto value = tagValue.value(); |
| 68 | switch (value) { |
| 69 | case Digest::NONE: |
| 70 | return (1 << NONE_BIT_POS); |
| 71 | case Digest::MD5: |
| 72 | return (1 << DigestBitPosition::MD5_BIT_POS); |
| 73 | case Digest::SHA1: |
| 74 | return (1 << DigestBitPosition::SHA1_BIT_POS); |
| 75 | case Digest::SHA_2_224: |
| 76 | return (1 << DigestBitPosition::SHA_2_224_BIT_POS); |
| 77 | case Digest::SHA_2_256: |
| 78 | return (1 << DigestBitPosition::SHA_2_256_BIT_POS); |
| 79 | case Digest::SHA_2_384: |
| 80 | return (1 << DigestBitPosition::SHA_2_384_BIT_POS); |
| 81 | case Digest::SHA_2_512: |
| 82 | return (1 << DigestBitPosition::SHA_2_512_BIT_POS); |
| 83 | default: |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | // unlike in the single enum fields, if no value is provided, |
| 88 | // 0 is set for the bitmap |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | int32_t generateBitMapForBlockModeValue(const AuthorizationSet& authorization_set) { |
| 93 | auto tagValue = authorization_set.GetTagValue(TAG_BLOCK_MODE); |
| 94 | if (tagValue.isOk()) { |
| 95 | auto value = tagValue.value(); |
| 96 | switch (value) { |
| 97 | case BlockMode::ECB: |
| 98 | return (1 << BlockModeBitPosition::ECB_BIT_POS); |
| 99 | case BlockMode::CBC: |
| 100 | return (1 << BlockModeBitPosition::CBC_BIT_POS); |
| 101 | case BlockMode::CTR: |
| 102 | return (1 << BlockModeBitPosition::CTR_BIT_POS); |
| 103 | case BlockMode::GCM: |
| 104 | return (1 << BlockModeBitPosition::GCM_BIT_POS); |
| 105 | default: |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | // unlike in the single enum fields, if no value is provided, |
| 110 | // 0 is set for the bitmap |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | void logKeystoreKeyOperationEvent(const Operation& op, bool wasOperationSuccessful, |
| 115 | int32_t responseCode) { |
| 116 | AuthorizationSet authorization_set(op.characteristics.softwareEnforced); |
| 117 | authorization_set.Union(op.characteristics.hardwareEnforced); |
| 118 | AuthorizationSet operation_params(op.params); |
| 119 | |
| 120 | android::util::stats_write( |
| 121 | android::util::KEYSTORE_KEY_EVENT_REPORTED, |
| 122 | getOptionalEnumTagValue(authorization_set, TAG_ALGORITHM), |
| 123 | getOptionalEnumTagValue(authorization_set, TAG_KEY_SIZE), |
| 124 | getOptionalEnumTagValue(authorization_set, TAG_ORIGIN), |
| 125 | getOptionalEnumTagValue(authorization_set, TAG_USER_AUTH_TYPE), |
| 126 | getOptionalEnumTagValue(authorization_set, TAG_AUTH_TIMEOUT), |
| 127 | generateBitMapForPaddingModeValue(operation_params), |
| 128 | generateBitMapForDigestValue(operation_params), |
| 129 | generateBitMapForBlockModeValue(operation_params), static_cast<int32_t>(op.purpose), |
| 130 | getOptionalEnumTagValue(authorization_set, TAG_EC_CURVE), |
| 131 | getOptionalEnumTagValue(authorization_set, TAG_BLOB_USAGE_REQUIREMENTS), |
| 132 | android::util::KEYSTORE_KEY_EVENT_REPORTED__TYPE__KEY_OPERATION, wasOperationSuccessful, |
| 133 | responseCode); |
| 134 | } |
| 135 | |
| 136 | } // namespace keystore |