Max Bires | 33aac2d | 2018-02-23 10:53:10 -0800 | [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 "operation_proto_handler.h" |
| 19 | |
| 20 | #include <android/os/DropBoxManager.h> |
| 21 | #include <google/protobuf/message_lite.h> |
| 22 | #include <keymasterV4_0/Keymaster.h> |
| 23 | #include <keystore/keymaster_types.h> |
| 24 | #include <keystore/keystore_hidl_support.h> |
| 25 | #include <utils/String16.h> |
| 26 | |
| 27 | #include "operation_config.pb.h" |
| 28 | |
| 29 | namespace keystore { |
| 30 | |
| 31 | void determinePurpose(KeyPurpose purpose, OperationConfig* operationConfig) { |
| 32 | switch (purpose) { |
| 33 | case KeyPurpose::VERIFY: |
| 34 | operationConfig->set_purpose("verify"); |
| 35 | break; |
| 36 | case KeyPurpose::ENCRYPT: |
| 37 | operationConfig->set_purpose("encrypt"); |
| 38 | break; |
| 39 | case KeyPurpose::SIGN: |
| 40 | operationConfig->set_purpose("sign"); |
| 41 | break; |
| 42 | case KeyPurpose::DECRYPT: |
| 43 | operationConfig->set_purpose("decrypt"); |
| 44 | break; |
| 45 | case KeyPurpose::WRAP_KEY: |
| 46 | operationConfig->set_purpose("wrap"); |
| 47 | break; |
| 48 | default: |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void checkKeyCharacteristics(const hidl_vec<KeyParameter>& characteristics, |
| 54 | OperationConfig* operationConfig) { |
| 55 | for (auto& opParam : characteristics) { |
| 56 | switch (opParam.tag) { |
| 57 | case Tag::ALGORITHM: |
| 58 | operationConfig->set_algorithm(toString(accessTagValue(TAG_ALGORITHM, opParam))); |
| 59 | break; |
| 60 | case Tag::KEY_SIZE: |
| 61 | operationConfig->set_key_size(accessTagValue(TAG_KEY_SIZE, opParam)); |
| 62 | break; |
| 63 | case Tag::EC_CURVE: |
| 64 | operationConfig->set_ec_curve(toString(accessTagValue(TAG_EC_CURVE, opParam))); |
| 65 | break; |
| 66 | case Tag::AUTH_TIMEOUT: |
| 67 | operationConfig->set_user_auth_key_timeout(accessTagValue(TAG_AUTH_TIMEOUT, opParam)); |
| 68 | break; |
| 69 | case Tag::ORIGIN: |
| 70 | operationConfig->set_origin(toString(accessTagValue(TAG_ORIGIN, opParam))); |
| 71 | break; |
| 72 | case Tag::BLOB_USAGE_REQUIREMENTS: |
| 73 | operationConfig->set_key_blob_usage_reqs( |
| 74 | toString(accessTagValue(TAG_BLOB_USAGE_REQUIREMENTS, opParam))); |
| 75 | break; |
| 76 | case Tag::USER_AUTH_TYPE: |
| 77 | operationConfig->set_user_auth_type( |
| 78 | toString(accessTagValue(TAG_USER_AUTH_TYPE, opParam))); |
| 79 | break; |
| 80 | default: |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void checkOpCharacteristics(const hidl_vec<KeyParameter>& characteristics, |
| 87 | OperationConfig* operationConfig) { |
| 88 | for (auto& opParam : characteristics) { |
| 89 | switch (opParam.tag) { |
| 90 | case Tag::BLOCK_MODE: |
| 91 | operationConfig->set_block_mode(toString(accessTagValue(TAG_BLOCK_MODE, opParam))); |
| 92 | break; |
| 93 | case Tag::PADDING: |
| 94 | operationConfig->set_padding(toString(accessTagValue(TAG_PADDING, opParam))); |
| 95 | break; |
| 96 | case Tag::DIGEST: |
| 97 | operationConfig->set_digest(toString(accessTagValue(TAG_DIGEST, opParam))); |
| 98 | break; |
| 99 | default: |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void uploadOpAsProto(Operation& op, bool wasOpSuccessful) { |
| 106 | OperationConfig operationConfig; |
| 107 | determinePurpose(op.purpose, &operationConfig); |
| 108 | checkKeyCharacteristics(op.characteristics.softwareEnforced, &operationConfig); |
| 109 | checkKeyCharacteristics(op.characteristics.hardwareEnforced, &operationConfig); |
| 110 | checkOpCharacteristics(op.params, &operationConfig); |
| 111 | auto dropbox = std::make_unique<android::os::DropBoxManager>(); |
| 112 | operationConfig.set_was_op_successful(wasOpSuccessful); |
| 113 | |
| 114 | size_t size = operationConfig.ByteSize(); |
| 115 | auto data = std::make_unique<uint8_t[]>(size); |
| 116 | operationConfig.SerializeWithCachedSizesToArray(data.get()); |
| 117 | dropbox->addData(android::String16("keymaster"), data.get(), size, 0); |
| 118 | } |
| 119 | |
| 120 | } // namespace keystore |