blob: deaa61532e7c819411a99a5af421067c5b9758f1 [file] [log] [blame]
Darren Krahn69a3dbc2015-09-22 16:21:04 -07001// Copyright 2015 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "keystore/keystore_client_impl.h"
16
17#include <string>
18#include <vector>
19
20#include "binder/IBinder.h"
21#include "binder/IInterface.h"
22#include "binder/IServiceManager.h"
23#include "keystore/IKeystoreService.h"
24#include "keystore/keystore.h"
25#include "utils/String16.h"
26#include "utils/String8.h"
27
28using android::ExportResult;
29using android::KeyCharacteristics;
30using android::KeymasterArguments;
31using android::OperationResult;
32using android::String16;
33using keymaster::AuthorizationSet;
34
35namespace {
36
37// Use the UID of the current process.
38const int kDefaultUID = -1;
39
40const uint8_t* StringAsByteArray(const std::string& s) {
41 return reinterpret_cast<const uint8_t*>(s.data());
42}
43
44std::string ByteArrayAsString(const uint8_t* data, size_t data_size) {
45 return std::string(reinterpret_cast<const char*>(data), data_size);
46}
47
48} // namespace
49
50namespace keystore {
51
52KeystoreClientImpl::KeystoreClientImpl() {
53 service_manager_ = android::defaultServiceManager();
54 keystore_binder_ = service_manager_->getService(String16("android.security.keystore"));
55 keystore_ = android::interface_cast<android::IKeystoreService>(keystore_binder_);
56}
57
58int32_t KeystoreClientImpl::addRandomNumberGeneratorEntropy(const std::string& entropy) {
59 return mapKeystoreError(keystore_->addRngEntropy(StringAsByteArray(entropy), entropy.size()));
60}
61
62int32_t KeystoreClientImpl::generateKey(const std::string& key_name,
63 const AuthorizationSet& key_parameters,
64 AuthorizationSet* hardware_enforced_characteristics,
65 AuthorizationSet* software_enforced_characteristics) {
66 String16 key_name16(key_name.data(), key_name.size());
67 KeymasterArguments key_arguments;
68 key_arguments.params.assign(key_parameters.begin(), key_parameters.end());
69 KeyCharacteristics characteristics;
70 int32_t result =
71 keystore_->generateKey(key_name16, key_arguments, NULL /*entropy*/, 0 /*entropyLength*/,
72 kDefaultUID, KEYSTORE_FLAG_NONE, &characteristics);
73 hardware_enforced_characteristics->Reinitialize(characteristics.characteristics.hw_enforced);
74 software_enforced_characteristics->Reinitialize(characteristics.characteristics.sw_enforced);
75 return mapKeystoreError(result);
76}
77
78int32_t
79KeystoreClientImpl::getKeyCharacteristics(const std::string& key_name,
80 AuthorizationSet* hardware_enforced_characteristics,
81 AuthorizationSet* software_enforced_characteristics) {
82 String16 key_name16(key_name.data(), key_name.size());
83 keymaster_blob_t client_id_blob = {nullptr, 0};
84 keymaster_blob_t app_data_blob = {nullptr, 0};
85 KeyCharacteristics characteristics;
86 int32_t result = keystore_->getKeyCharacteristics(key_name16, &client_id_blob, &app_data_blob,
87 &characteristics);
88 hardware_enforced_characteristics->Reinitialize(characteristics.characteristics.hw_enforced);
89 software_enforced_characteristics->Reinitialize(characteristics.characteristics.sw_enforced);
90 return mapKeystoreError(result);
91}
92
93int32_t KeystoreClientImpl::importKey(const std::string& key_name,
94 const AuthorizationSet& key_parameters,
95 keymaster_key_format_t key_format,
96 const std::string& key_data,
97 AuthorizationSet* hardware_enforced_characteristics,
98 AuthorizationSet* software_enforced_characteristics) {
99 String16 key_name16(key_name.data(), key_name.size());
100 KeymasterArguments key_arguments;
101 key_arguments.params.assign(key_parameters.begin(), key_parameters.end());
102 KeyCharacteristics characteristics;
103 int32_t result =
104 keystore_->importKey(key_name16, key_arguments, key_format, StringAsByteArray(key_data),
105 key_data.size(), kDefaultUID, KEYSTORE_FLAG_NONE, &characteristics);
106 hardware_enforced_characteristics->Reinitialize(characteristics.characteristics.hw_enforced);
107 software_enforced_characteristics->Reinitialize(characteristics.characteristics.sw_enforced);
108 return mapKeystoreError(result);
109}
110
111int32_t KeystoreClientImpl::exportKey(keymaster_key_format_t export_format,
112 const std::string& key_name, std::string* export_data) {
113 String16 key_name16(key_name.data(), key_name.size());
114 keymaster_blob_t client_id_blob = {nullptr, 0};
115 keymaster_blob_t app_data_blob = {nullptr, 0};
116 ExportResult export_result;
117 keystore_->exportKey(key_name16, export_format, &client_id_blob, &app_data_blob,
118 &export_result);
119 *export_data = ByteArrayAsString(export_result.exportData.get(), export_result.dataLength);
120 return mapKeystoreError(export_result.resultCode);
121}
122
123int32_t KeystoreClientImpl::deleteKey(const std::string& key_name) {
124 String16 key_name16(key_name.data(), key_name.size());
125 return mapKeystoreError(keystore_->del(key_name16, kDefaultUID));
126}
127
128int32_t KeystoreClientImpl::deleteAllKeys() {
129 return mapKeystoreError(keystore_->clear_uid(kDefaultUID));
130}
131
132int32_t KeystoreClientImpl::beginOperation(keymaster_purpose_t purpose, const std::string& key_name,
133 const AuthorizationSet& input_parameters,
134 AuthorizationSet* output_parameters,
135 keymaster_operation_handle_t* handle) {
136 android::sp<android::IBinder> token(new android::BBinder);
137 String16 key_name16(key_name.data(), key_name.size());
138 KeymasterArguments input_arguments;
139 input_arguments.params.assign(input_parameters.begin(), input_parameters.end());
140 OperationResult result;
141 keystore_->begin(token, key_name16, purpose, true /*pruneable*/, input_arguments,
142 NULL /*entropy*/, 0 /*entropyLength*/, &result);
143 int32_t error_code = mapKeystoreError(result.resultCode);
144 if (error_code == KM_ERROR_OK) {
145 *handle = getNextVirtualHandle();
146 active_operations_[*handle] = result.token;
147 if (!result.outParams.params.empty()) {
148 output_parameters->Reinitialize(&*result.outParams.params.begin(),
149 result.outParams.params.size());
150 }
151 }
152 return error_code;
153}
154
155int32_t KeystoreClientImpl::updateOperation(keymaster_operation_handle_t handle,
156 const AuthorizationSet& input_parameters,
157 const std::string& input_data,
158 size_t* num_input_bytes_consumed,
159 AuthorizationSet* output_parameters,
160 std::string* output_data) {
161 if (active_operations_.count(handle) == 0) {
162 return KM_ERROR_INVALID_OPERATION_HANDLE;
163 }
164 KeymasterArguments input_arguments;
165 input_arguments.params.assign(input_parameters.begin(), input_parameters.end());
166 OperationResult result;
167 keystore_->update(active_operations_[handle], input_arguments, StringAsByteArray(input_data),
168 input_data.size(), &result);
169 int32_t error_code = mapKeystoreError(result.resultCode);
170 if (error_code == KM_ERROR_OK) {
171 *num_input_bytes_consumed = result.inputConsumed;
172 if (!result.outParams.params.empty()) {
173 output_parameters->Reinitialize(&*result.outParams.params.begin(),
174 result.outParams.params.size());
175 }
176 *output_data = ByteArrayAsString(result.data.get(), result.dataLength);
177 }
178 return error_code;
179}
180
181int32_t KeystoreClientImpl::finishOperation(keymaster_operation_handle_t handle,
182 const AuthorizationSet& input_parameters,
183 const std::string& signature_to_verify,
184 AuthorizationSet* output_parameters,
185 std::string* output_data) {
186 if (active_operations_.count(handle) == 0) {
187 return KM_ERROR_INVALID_OPERATION_HANDLE;
188 }
189 KeymasterArguments input_arguments;
190 input_arguments.params.assign(input_parameters.begin(), input_parameters.end());
191 OperationResult result;
192 keystore_->finish(active_operations_[handle], input_arguments,
193 StringAsByteArray(signature_to_verify), signature_to_verify.size(),
194 NULL /*entropy*/, 0 /*entropyLength*/, &result);
195 int32_t error_code = mapKeystoreError(result.resultCode);
196 if (error_code == KM_ERROR_OK) {
197 if (!result.outParams.params.empty()) {
198 output_parameters->Reinitialize(&*result.outParams.params.begin(),
199 result.outParams.params.size());
200 }
201 *output_data = ByteArrayAsString(result.data.get(), result.dataLength);
202 active_operations_.erase(handle);
203 }
204 return error_code;
205}
206
207int32_t KeystoreClientImpl::abortOperation(keymaster_operation_handle_t handle) {
208 if (active_operations_.count(handle) == 0) {
209 return KM_ERROR_INVALID_OPERATION_HANDLE;
210 }
211 int32_t error_code = mapKeystoreError(keystore_->abort(active_operations_[handle]));
212 if (error_code == KM_ERROR_OK) {
213 active_operations_.erase(handle);
214 }
215 return error_code;
216}
217
218bool KeystoreClientImpl::doesKeyExist(const std::string& key_name) {
219 String16 key_name16(key_name.data(), key_name.size());
220 int32_t error_code = mapKeystoreError(keystore_->exist(key_name16, kDefaultUID));
221 return (error_code == KM_ERROR_OK);
222}
223
224bool KeystoreClientImpl::listKeys(const std::string& prefix,
225 std::vector<std::string>* key_name_list) {
226 String16 prefix16(prefix.data(), prefix.size());
227 android::Vector<String16> matches;
228 int32_t error_code = mapKeystoreError(keystore_->list(prefix16, kDefaultUID, &matches));
229 if (error_code == KM_ERROR_OK) {
230 for (const auto& match : matches) {
231 android::String8 key_name(match);
232 key_name_list->push_back(prefix + std::string(key_name.string(), key_name.size()));
233 }
234 return true;
235 }
236 return false;
237}
238
239keymaster_operation_handle_t KeystoreClientImpl::getNextVirtualHandle() {
240 return next_virtual_handle_++;
241}
242
243int32_t KeystoreClientImpl::mapKeystoreError(int32_t keystore_error) {
244 // See notes in keystore_client.h for rationale.
245 if (keystore_error == ::NO_ERROR) {
246 return KM_ERROR_OK;
247 }
248 return keystore_error;
249}
250
251} // namespace keystore