Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
| 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 | |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 20 | #include <cstddef> |
| 21 | |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 22 | #include <openssl/rand.h> |
Shawn Willden | ffd790c | 2014-08-18 21:20:06 -0600 | [diff] [blame] | 23 | #include <openssl/x509.h> |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 24 | |
| 25 | #include <UniquePtr.h> |
| 26 | |
Shawn Willden | 3879f86 | 2014-08-06 14:40:48 -0600 | [diff] [blame] | 27 | #include "ae.h" |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 28 | #include "google_keymaster.h" |
| 29 | #include "google_keymaster_utils.h" |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 30 | #include "key.h" |
Shawn Willden | 4db3fbd | 2014-08-08 22:13:44 -0600 | [diff] [blame] | 31 | #include "key_blob.h" |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 32 | #include "operation.h" |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 33 | |
| 34 | namespace keymaster { |
| 35 | |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 36 | GoogleKeymaster::GoogleKeymaster(size_t operation_table_size) |
| 37 | : operation_table_(new OpTableEntry[operation_table_size]), |
| 38 | operation_table_size_(operation_table_size) { |
| 39 | if (operation_table_.get() == NULL) |
| 40 | operation_table_size_ = 0; |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 41 | } |
| 42 | GoogleKeymaster::~GoogleKeymaster() { |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 43 | for (size_t i = 0; i < operation_table_size_; ++i) |
| 44 | if (operation_table_[i].operation != NULL) |
| 45 | delete operation_table_[i].operation; |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 46 | } |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 47 | |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 48 | struct AE_CTX_Delete { |
Shawn Willden | 802bb29 | 2014-08-18 10:46:29 -0600 | [diff] [blame] | 49 | void operator()(ae_ctx* ctx) const { ae_free(ctx); } |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 50 | }; |
| 51 | typedef UniquePtr<ae_ctx, AE_CTX_Delete> Unique_ae_ctx; |
| 52 | |
Shawn Willden | ffd790c | 2014-08-18 21:20:06 -0600 | [diff] [blame] | 53 | struct EVP_PKEY_Delete { |
| 54 | void operator()(EVP_PKEY* p) { EVP_PKEY_free(p); } |
| 55 | }; |
| 56 | typedef UniquePtr<EVP_PKEY, EVP_PKEY_Delete> Unique_EVP_PKEY; |
| 57 | |
| 58 | struct PKCS8_PRIV_KEY_INFO_Delete { |
| 59 | void operator()(PKCS8_PRIV_KEY_INFO* p) const { PKCS8_PRIV_KEY_INFO_free(p); } |
| 60 | }; |
| 61 | typedef UniquePtr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_Delete> Unique_PKCS8_PRIV_KEY_INFO; |
| 62 | |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 63 | keymaster_algorithm_t supported_algorithms[] = { |
Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 64 | KM_ALGORITHM_RSA, KM_ALGORITHM_DSA, KM_ALGORITHM_ECDSA, |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | template <typename T> |
| 68 | bool check_supported(keymaster_algorithm_t algorithm, SupportedResponse<T>* response) { |
| 69 | if (!array_contains(supported_algorithms, algorithm)) { |
| 70 | response->error = KM_ERROR_UNSUPPORTED_ALGORITHM; |
| 71 | return false; |
| 72 | } |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | GoogleKeymaster::SupportedAlgorithms(SupportedResponse<keymaster_algorithm_t>* response) const { |
| 78 | if (response == NULL) |
| 79 | return; |
| 80 | response->SetResults(supported_algorithms); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | GoogleKeymaster::SupportedBlockModes(keymaster_algorithm_t algorithm, |
| 85 | SupportedResponse<keymaster_block_mode_t>* response) const { |
| 86 | if (response == NULL || !check_supported(algorithm, response)) |
| 87 | return; |
| 88 | response->error = KM_ERROR_OK; |
| 89 | } |
| 90 | |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 91 | keymaster_padding_t supported_padding[] = {KM_PAD_NONE}; |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 92 | void |
| 93 | GoogleKeymaster::SupportedPaddingModes(keymaster_algorithm_t algorithm, |
| 94 | SupportedResponse<keymaster_padding_t>* response) const { |
| 95 | if (response == NULL || !check_supported(algorithm, response)) |
| 96 | return; |
| 97 | |
| 98 | response->error = KM_ERROR_OK; |
| 99 | switch (algorithm) { |
| 100 | case KM_ALGORITHM_RSA: |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 101 | case KM_ALGORITHM_DSA: |
Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 102 | case KM_ALGORITHM_ECDSA: |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 103 | response->SetResults(supported_padding); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 104 | break; |
| 105 | default: |
| 106 | response->results_length = 0; |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 111 | keymaster_digest_t supported_digests[] = {KM_DIGEST_NONE}; |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 112 | void GoogleKeymaster::SupportedDigests(keymaster_algorithm_t algorithm, |
| 113 | SupportedResponse<keymaster_digest_t>* response) const { |
| 114 | if (response == NULL || !check_supported(algorithm, response)) |
| 115 | return; |
| 116 | |
| 117 | response->error = KM_ERROR_OK; |
| 118 | switch (algorithm) { |
| 119 | case KM_ALGORITHM_RSA: |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 120 | case KM_ALGORITHM_DSA: |
Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 121 | case KM_ALGORITHM_ECDSA: |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 122 | response->SetResults(supported_digests); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 123 | break; |
| 124 | default: |
| 125 | response->results_length = 0; |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 130 | keymaster_key_format_t supported_import_formats[] = {KM_KEY_FORMAT_PKCS8}; |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 131 | void |
| 132 | GoogleKeymaster::SupportedImportFormats(keymaster_algorithm_t algorithm, |
| 133 | SupportedResponse<keymaster_key_format_t>* response) const { |
| 134 | if (response == NULL || !check_supported(algorithm, response)) |
| 135 | return; |
| 136 | |
| 137 | response->error = KM_ERROR_OK; |
| 138 | switch (algorithm) { |
| 139 | case KM_ALGORITHM_RSA: |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 140 | case KM_ALGORITHM_DSA: |
Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 141 | case KM_ALGORITHM_ECDSA: |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 142 | response->SetResults(supported_import_formats); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 143 | break; |
| 144 | default: |
| 145 | response->results_length = 0; |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 150 | keymaster_key_format_t supported_export_formats[] = {KM_KEY_FORMAT_X509}; |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 151 | void |
| 152 | GoogleKeymaster::SupportedExportFormats(keymaster_algorithm_t algorithm, |
| 153 | SupportedResponse<keymaster_key_format_t>* response) const { |
| 154 | if (response == NULL || !check_supported(algorithm, response)) |
| 155 | return; |
| 156 | |
| 157 | response->error = KM_ERROR_OK; |
| 158 | switch (algorithm) { |
| 159 | case KM_ALGORITHM_RSA: |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 160 | case KM_ALGORITHM_DSA: |
Shawn Willden | c3864dd | 2014-08-18 15:20:01 -0600 | [diff] [blame] | 161 | case KM_ALGORITHM_ECDSA: |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 162 | response->SetResults(supported_export_formats); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 163 | break; |
| 164 | default: |
| 165 | response->results_length = 0; |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 170 | void GoogleKeymaster::GenerateKey(const GenerateKeyRequest& request, |
| 171 | GenerateKeyResponse* response) { |
| 172 | if (response == NULL) |
| 173 | return; |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 174 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 175 | UniquePtr<Key> key(Key::GenerateKey(request.key_description, &response->error)); |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 176 | if (response->error != KM_ERROR_OK) |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 177 | return; |
| 178 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 179 | if (!CopyAuthorizations(key->authorizations(), response)) |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 180 | return; |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 181 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 182 | AuthorizationSet hidden_auths; |
| 183 | response->error = BuildHiddenAuthorizations(key->authorizations(), &hidden_auths); |
| 184 | if (response->error != KM_ERROR_OK) |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 185 | return; |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 186 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 187 | UniquePtr<uint8_t[]> key_material; |
| 188 | size_t key_material_size; |
| 189 | response->error = key->key_material(&key_material, &key_material_size); |
| 190 | if (response->error != KM_ERROR_OK) |
| 191 | return; |
| 192 | |
| 193 | response->error = |
| 194 | SerializeKeyToResponse(key_material.get(), key_material_size, hidden_auths, response); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 195 | } |
| 196 | |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 197 | void GoogleKeymaster::GetKeyCharacteristics(const GetKeyCharacteristicsRequest& request, |
| 198 | GetKeyCharacteristicsResponse* response) { |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 199 | if (response == NULL) |
| 200 | return; |
| 201 | response->error = KM_ERROR_UNKNOWN_ERROR; |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 202 | |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 203 | UniquePtr<KeyBlob> blob( |
| 204 | LoadKeyBlob(request.key_blob, request.additional_params, &(response->error))); |
| 205 | if (blob.get() == NULL) |
| 206 | return; |
| 207 | |
| 208 | response->enforced.Reinitialize(blob->enforced()); |
| 209 | response->unenforced.Reinitialize(blob->unenforced()); |
| 210 | response->error = KM_ERROR_OK; |
| 211 | } |
| 212 | |
| 213 | void GoogleKeymaster::BeginOperation(const BeginOperationRequest& request, |
| 214 | BeginOperationResponse* response) { |
| 215 | if (response == NULL) |
| 216 | return; |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 217 | response->op_handle = 0; |
| 218 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 219 | UniquePtr<Key> key(LoadKey(request.key_blob, request.additional_params, &response->error)); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 220 | if (key.get() == NULL) |
| 221 | return; |
| 222 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 223 | UniquePtr<Operation> operation(key->CreateOperation(request.purpose, &response->error)); |
| 224 | if (operation.get() == NULL) |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 225 | return; |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 226 | |
| 227 | response->error = operation->Begin(); |
| 228 | if (response->error != KM_ERROR_OK) |
| 229 | return; |
| 230 | |
| 231 | response->error = AddOperation(operation.release(), &response->op_handle); |
| 232 | } |
| 233 | |
| 234 | void GoogleKeymaster::UpdateOperation(const UpdateOperationRequest& request, |
| 235 | UpdateOperationResponse* response) { |
| 236 | OpTableEntry* entry = FindOperation(request.op_handle); |
| 237 | if (entry == NULL) { |
| 238 | response->error = KM_ERROR_INVALID_OPERATION_HANDLE; |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | response->error = entry->operation->Update(request.input, &response->output); |
| 243 | if (response->error != KM_ERROR_OK) { |
| 244 | // Any error invalidates the operation. |
| 245 | DeleteOperation(entry); |
| 246 | } |
| 247 | } |
| 248 | |
Shawn Willden | 43e999e | 2014-08-13 13:29:50 -0600 | [diff] [blame] | 249 | void GoogleKeymaster::FinishOperation(const FinishOperationRequest& request, |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 250 | FinishOperationResponse* response) { |
Shawn Willden | 43e999e | 2014-08-13 13:29:50 -0600 | [diff] [blame] | 251 | OpTableEntry* entry = FindOperation(request.op_handle); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 252 | if (entry == NULL) { |
| 253 | response->error = KM_ERROR_INVALID_OPERATION_HANDLE; |
| 254 | return; |
| 255 | } |
| 256 | |
Shawn Willden | 43e999e | 2014-08-13 13:29:50 -0600 | [diff] [blame] | 257 | response->error = entry->operation->Finish(request.signature, &response->output); |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 258 | DeleteOperation(entry); |
| 259 | } |
| 260 | |
| 261 | keymaster_error_t GoogleKeymaster::AbortOperation(const keymaster_operation_handle_t op_handle) { |
| 262 | OpTableEntry* entry = FindOperation(op_handle); |
| 263 | if (entry == NULL) |
| 264 | return KM_ERROR_INVALID_OPERATION_HANDLE; |
| 265 | DeleteOperation(entry); |
| 266 | return KM_ERROR_OK; |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 267 | } |
| 268 | |
Shawn Willden | ffd790c | 2014-08-18 21:20:06 -0600 | [diff] [blame] | 269 | bool GoogleKeymaster::is_supported_export_format(keymaster_key_format_t test_format) { |
| 270 | unsigned int index; |
| 271 | for (index = 0; index < array_length(supported_export_formats); index++) { |
| 272 | if (test_format == supported_export_formats[index]) { |
| 273 | return true; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | bool GoogleKeymaster::is_supported_import_format(keymaster_key_format_t test_format) { |
| 281 | unsigned int index; |
| 282 | for (index = 0; index < array_length(supported_import_formats); index++) { |
| 283 | if (test_format == supported_import_formats[index]) { |
| 284 | return true; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | void GoogleKeymaster::ExportKey(const ExportKeyRequest& request, ExportKeyResponse* response) { |
| 292 | if (response == NULL) |
| 293 | return; |
| 294 | |
| 295 | keymaster_error_t blob_error; |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame^] | 296 | UniquePtr<Key> to_export( |
| 297 | LoadKey(request.key_blob, request.additional_params, &response->error)); |
Shawn Willden | ffd790c | 2014-08-18 21:20:06 -0600 | [diff] [blame] | 298 | if (to_export.get() == NULL) |
| 299 | return; |
| 300 | |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame^] | 301 | UniquePtr<uint8_t[]> out_key; |
| 302 | size_t size; |
| 303 | response->error = to_export->formatted_key_material(request.key_format, &out_key, &size); |
| 304 | if (response->error == KM_ERROR_OK) { |
| 305 | response->key_data = out_key.release(); |
| 306 | response->key_data_length = size; |
Shawn Willden | ffd790c | 2014-08-18 21:20:06 -0600 | [diff] [blame] | 307 | } |
Shawn Willden | ffd790c | 2014-08-18 21:20:06 -0600 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | void GoogleKeymaster::ImportKey(const ImportKeyRequest& request, ImportKeyResponse* response) { |
| 311 | |
| 312 | if (request.key_data == NULL || request.key_data_length <= 0) { |
| 313 | response->error = KM_ERROR_INVALID_KEY_BLOB; |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | if (!is_supported_export_format(request.key_format)) { |
| 318 | response->error = KM_ERROR_UNSUPPORTED_KEY_FORMAT; |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | const uint8_t* key_data = request.key_data; |
| 323 | Unique_PKCS8_PRIV_KEY_INFO pkcs8( |
| 324 | d2i_PKCS8_PRIV_KEY_INFO(NULL, &key_data, request.key_data_length)); |
| 325 | if (pkcs8.get() == NULL) { |
| 326 | response->error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | Unique_EVP_PKEY pkey(EVP_PKCS82PKEY(pkcs8.get())); |
| 331 | if (pkey.get() == NULL) { |
| 332 | response->error = KM_ERROR_INVALID_KEY_BLOB; |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | int len = i2d_PrivateKey(pkey.get(), NULL); |
| 337 | if (len <= 0) { |
| 338 | response->error = KM_ERROR_INVALID_KEY_BLOB; |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | UniquePtr<uint8_t[]> der_encoded_key(new uint8_t[len]); |
| 343 | if (der_encoded_key.get() == NULL) { |
| 344 | response->error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | uint8_t* tmp_der = der_encoded_key.get(); |
| 349 | if (i2d_PrivateKey(pkey.get(), &tmp_der) != len) { |
| 350 | response->error = KM_ERROR_INVALID_KEY_BLOB; |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | void* key_material = der_encoded_key.release(); |
| 355 | response->SetKeyMaterial(key_material, len); |
| 356 | |
| 357 | response->error = KM_ERROR_OK; |
| 358 | } |
| 359 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 360 | keymaster_error_t GoogleKeymaster::SerializeKeyToResponse(uint8_t* key_bytes, size_t key_length, |
| 361 | const AuthorizationSet& hidden_auths, |
| 362 | GenerateKeyResponse* response) { |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 363 | uint8_t nonce[KeyBlob::NONCE_LENGTH]; |
| 364 | GenerateNonce(nonce, array_size(nonce)); |
| 365 | |
| 366 | keymaster_key_blob_t key_data = {key_bytes, key_length}; |
| 367 | UniquePtr<KeyBlob> blob(new KeyBlob(response->enforced, response->unenforced, hidden_auths, |
| 368 | key_data, MasterKey(), nonce)); |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 369 | if (blob.get() == NULL) |
| 370 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 371 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 372 | if (blob->error() != KM_ERROR_OK) |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 373 | return blob->error(); |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 374 | |
| 375 | size_t size = blob->SerializedSize(); |
| 376 | UniquePtr<uint8_t[]> blob_bytes(new uint8_t[size]); |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 377 | if (blob_bytes.get() == NULL) |
| 378 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 379 | |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 380 | blob->Serialize(blob_bytes.get(), blob_bytes.get() + size); |
| 381 | response->key_blob.key_material_size = size; |
| 382 | response->key_blob.key_material = blob_bytes.release(); |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 383 | |
| 384 | return KM_ERROR_OK; |
| 385 | } |
| 386 | |
| 387 | Key* GoogleKeymaster::LoadKey(const keymaster_key_blob_t& key, |
| 388 | const AuthorizationSet& client_params, keymaster_error_t* error) { |
| 389 | UniquePtr<KeyBlob> blob(LoadKeyBlob(key, client_params, error)); |
| 390 | if (*error != KM_ERROR_OK) |
| 391 | return NULL; |
| 392 | return Key::CreateKey(*blob, error); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 393 | } |
| 394 | |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 395 | KeyBlob* GoogleKeymaster::LoadKeyBlob(const keymaster_key_blob_t& key, |
| 396 | const AuthorizationSet& client_params, |
| 397 | keymaster_error_t* error) { |
| 398 | AuthorizationSet hidden; |
| 399 | BuildHiddenAuthorizations(client_params, &hidden); |
| 400 | UniquePtr<KeyBlob> blob(new KeyBlob(key, hidden, MasterKey())); |
| 401 | if (blob.get() == NULL) { |
| 402 | *error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 403 | return NULL; |
| 404 | } else if (blob->error() != KM_ERROR_OK) { |
| 405 | *error = blob->error(); |
| 406 | return NULL; |
| 407 | } |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 408 | *error = KM_ERROR_OK; |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 409 | return blob.release(); |
| 410 | } |
| 411 | |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 412 | static keymaster_error_t CheckAuthorizationSet(const AuthorizationSet& set) { |
| 413 | switch (set.is_valid()) { |
Shawn Willden | 58e1a54 | 2014-08-08 21:58:29 -0600 | [diff] [blame] | 414 | case AuthorizationSet::OK: |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 415 | return KM_ERROR_OK; |
| 416 | case AuthorizationSet::ALLOCATION_FAILURE: |
| 417 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 418 | case AuthorizationSet::MALFORMED_DATA: |
| 419 | return KM_ERROR_UNKNOWN_ERROR; |
| 420 | } |
| 421 | return KM_ERROR_OK; |
| 422 | } |
| 423 | |
| 424 | bool GoogleKeymaster::CopyAuthorizations(const AuthorizationSet& key_description, |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 425 | GenerateKeyResponse* response) { |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 426 | for (size_t i = 0; i < key_description.size(); ++i) { |
| 427 | switch (key_description[i].tag) { |
| 428 | case KM_TAG_ROOT_OF_TRUST: |
| 429 | case KM_TAG_CREATION_DATETIME: |
| 430 | case KM_TAG_ORIGIN: |
| 431 | response->error = KM_ERROR_INVALID_TAG; |
| 432 | return false; |
| 433 | case KM_TAG_ROLLBACK_RESISTANT: |
| 434 | response->error = KM_ERROR_UNSUPPORTED_TAG; |
| 435 | return false; |
| 436 | default: |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 437 | if (!AddAuthorization(key_description[i], response)) |
| 438 | return false; |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 439 | break; |
| 440 | } |
| 441 | } |
| 442 | |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 443 | if (!AddAuthorization(Authorization(TAG_CREATION_DATETIME, java_time(time(NULL))), response) || |
| 444 | !AddAuthorization(Authorization(TAG_ORIGIN, origin()), response)) |
| 445 | return false; |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 446 | |
| 447 | response->error = CheckAuthorizationSet(response->enforced); |
| 448 | if (response->error != KM_ERROR_OK) |
| 449 | return false; |
| 450 | response->error = CheckAuthorizationSet(response->unenforced); |
| 451 | if (response->error != KM_ERROR_OK) |
| 452 | return false; |
| 453 | |
| 454 | return true; |
| 455 | } |
| 456 | |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 457 | keymaster_error_t GoogleKeymaster::BuildHiddenAuthorizations(const AuthorizationSet& input_set, |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 458 | AuthorizationSet* hidden) { |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 459 | keymaster_blob_t entry; |
| 460 | if (input_set.GetTagValue(TAG_APPLICATION_ID, &entry)) |
| 461 | hidden->push_back(TAG_APPLICATION_ID, entry.data, entry.data_length); |
| 462 | if (input_set.GetTagValue(TAG_APPLICATION_DATA, &entry)) |
| 463 | hidden->push_back(TAG_APPLICATION_DATA, entry.data, entry.data_length); |
| 464 | hidden->push_back(RootOfTrustTag()); |
| 465 | |
| 466 | return CheckAuthorizationSet(*hidden); |
| 467 | } |
| 468 | |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 469 | bool GoogleKeymaster::AddAuthorization(const keymaster_key_param_t& auth, |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 470 | GenerateKeyResponse* response) { |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 471 | switch (auth.tag) { |
| 472 | case KM_TAG_ROOT_OF_TRUST: |
| 473 | case KM_TAG_APPLICATION_ID: |
| 474 | case KM_TAG_APPLICATION_DATA: |
Shawn Willden | 7636471 | 2014-08-11 17:48:04 -0600 | [diff] [blame] | 475 | // Skip. We handle these tags separately. |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 476 | return true; |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 477 | default: |
| 478 | if (is_enforced(auth.tag)) |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 479 | return response->enforced.push_back(auth); |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 480 | else |
Shawn Willden | 28e4147 | 2014-08-18 13:35:22 -0600 | [diff] [blame] | 481 | return response->unenforced.push_back(auth); |
Shawn Willden | 39b970b | 2014-08-11 09:11:21 -0600 | [diff] [blame] | 482 | } |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 483 | } |
| 484 | |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 485 | keymaster_error_t GoogleKeymaster::AddOperation(Operation* operation, |
| 486 | keymaster_operation_handle_t* op_handle) { |
| 487 | UniquePtr<Operation> op(operation); |
| 488 | if (RAND_bytes(reinterpret_cast<uint8_t*>(op_handle), sizeof(*op_handle)) == 0) |
| 489 | return KM_ERROR_UNKNOWN_ERROR; |
Shawn Willden | 802bb29 | 2014-08-18 10:46:29 -0600 | [diff] [blame] | 490 | if (*op_handle == 0) { |
Shawn Willden | 1615f2e | 2014-08-13 10:37:40 -0600 | [diff] [blame] | 491 | // Statistically this is vanishingly unlikely, which means if it ever happens in practice, |
| 492 | // it indicates a broken RNG. |
| 493 | return KM_ERROR_UNKNOWN_ERROR; |
| 494 | } |
| 495 | for (size_t i = 0; i < operation_table_size_; ++i) { |
| 496 | if (operation_table_[i].operation == NULL) { |
| 497 | operation_table_[i].operation = op.release(); |
| 498 | operation_table_[i].handle = *op_handle; |
| 499 | return KM_ERROR_OK; |
| 500 | } |
| 501 | } |
| 502 | return KM_ERROR_TOO_MANY_OPERATIONS; |
| 503 | } |
| 504 | |
| 505 | GoogleKeymaster::OpTableEntry* |
| 506 | GoogleKeymaster::FindOperation(keymaster_operation_handle_t op_handle) { |
| 507 | if (op_handle == 0) |
| 508 | return NULL; |
| 509 | |
| 510 | for (size_t i = 0; i < operation_table_size_; ++i) { |
| 511 | if (operation_table_[i].handle == op_handle) |
| 512 | return operation_table_.get() + i; |
| 513 | } |
| 514 | return NULL; |
| 515 | } |
| 516 | |
| 517 | void GoogleKeymaster::DeleteOperation(OpTableEntry* entry) { |
| 518 | delete entry->operation; |
| 519 | entry->operation = NULL; |
| 520 | entry->handle = 0; |
| 521 | } |
| 522 | |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 523 | } // namespace keymaster |