blob: 3bf8c0604c982b44c1e33891769b879c66f7d037 [file] [log] [blame]
Max Biresd6d89522018-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 "key_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 <utils/String16.h>
25
26#include "key_config.pb.h"
27
28namespace keystore {
29
30void checkEnforcedCharacteristics(const hidl_vec<KeyParameter>& keyParams, KeyConfig* keyConfig) {
31 for (auto& keyParam : keyParams) {
32 switch (keyParam.tag) {
33 case Tag::PURPOSE:
34 keyConfig->add_purpose(toString(accessTagValue(TAG_PURPOSE, keyParam)));
35 break;
36 case Tag::ALGORITHM:
37 keyConfig->set_algorithm(toString(accessTagValue(TAG_ALGORITHM, keyParam)));
38 break;
39 case Tag::KEY_SIZE:
40 keyConfig->set_key_size(accessTagValue(TAG_KEY_SIZE, keyParam));
41 break;
42 case Tag::BLOCK_MODE:
43 keyConfig->add_block_mode(toString(accessTagValue(TAG_BLOCK_MODE, keyParam)));
44 break;
45 case Tag::PADDING:
46 keyConfig->add_padding(toString(accessTagValue(TAG_PADDING, keyParam)));
47 break;
48 case Tag::DIGEST:
49 keyConfig->add_digest(toString(accessTagValue(TAG_DIGEST, keyParam)));
50 break;
51 case Tag::EC_CURVE:
52 keyConfig->set_ec_curve(toString(accessTagValue(TAG_EC_CURVE, keyParam)));
53 break;
54 case Tag::AUTH_TIMEOUT:
55 keyConfig->set_user_auth_key_timeout(accessTagValue(TAG_AUTH_TIMEOUT, keyParam));
56 break;
57 case Tag::ORIGIN:
58 keyConfig->set_origin(toString(accessTagValue(TAG_ORIGIN, keyParam)));
59 break;
60 case Tag::BLOB_USAGE_REQUIREMENTS:
61 keyConfig->set_key_blob_usage_reqs(
62 toString(accessTagValue(TAG_BLOB_USAGE_REQUIREMENTS, keyParam)));
63 break;
64 case Tag::USER_AUTH_TYPE:
65 keyConfig->set_user_auth_type(toString(accessTagValue(TAG_USER_AUTH_TYPE, keyParam)));
66 break;
67 default:
68 break;
69 }
70 }
71}
72
73void uploadKeyCharacteristicsAsProto(const hidl_vec<KeyParameter>& keyParams,
74 bool wasCreationSuccessful) {
75 KeyConfig keyConfig;
76 checkEnforcedCharacteristics(keyParams, &keyConfig);
77 auto dropbox = std::make_unique<android::os::DropBoxManager>();
78 keyConfig.set_was_creation_successful(wasCreationSuccessful);
79
80 size_t size = keyConfig.ByteSize();
81 auto data = std::make_unique<uint8_t[]>(size);
82 keyConfig.SerializeWithCachedSizesToArray(data.get());
83 dropbox->addData(android::String16("keymaster"), data.get(), size, 0);
84}
85
86} // namespace keystore