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