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