Shawn Willden | d67afae | 2014-08-19 12:36:27 -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 | |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame] | 17 | #include "asymmetric_key.h" |
| 18 | |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 19 | #include <openssl/x509.h> |
| 20 | |
Shawn Willden | b9d584d | 2015-01-22 16:35:00 -0700 | [diff] [blame] | 21 | #include <hardware/keymaster_defs.h> |
Shawn Willden | 98d9b92 | 2014-08-26 08:14:10 -0600 | [diff] [blame] | 22 | |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame] | 23 | #include "ecdsa_key.h" |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 24 | #include "openssl_utils.h" |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame] | 25 | #include "rsa_key.h" |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame] | 26 | #include "unencrypted_key_blob.h" |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 27 | |
| 28 | namespace keymaster { |
| 29 | |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame] | 30 | struct PKCS8_PRIV_KEY_INFO_Delete { |
| 31 | void operator()(PKCS8_PRIV_KEY_INFO* p) const { PKCS8_PRIV_KEY_INFO_free(p); } |
| 32 | }; |
| 33 | |
| 34 | EVP_PKEY* AsymmetricKeyFactory::ExtractEvpKey(keymaster_key_format_t key_format, |
| 35 | keymaster_algorithm_t expected_algorithm, |
| 36 | const uint8_t* key_data, size_t key_data_length, |
| 37 | keymaster_error_t* error) { |
| 38 | *error = KM_ERROR_OK; |
| 39 | |
| 40 | if (key_data == NULL || key_data_length <= 0) { |
| 41 | *error = KM_ERROR_INVALID_KEY_BLOB; |
| 42 | return NULL; |
| 43 | } |
| 44 | |
| 45 | if (key_format != KM_KEY_FORMAT_PKCS8) { |
| 46 | *error = KM_ERROR_UNSUPPORTED_KEY_FORMAT; |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | UniquePtr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_Delete> pkcs8( |
| 51 | d2i_PKCS8_PRIV_KEY_INFO(NULL, &key_data, key_data_length)); |
| 52 | if (pkcs8.get() == NULL) { |
| 53 | *error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKCS82PKEY(pkcs8.get())); |
| 58 | if (pkey.get() == NULL || EVP_PKEY_type(pkey->type) != convert_to_evp(expected_algorithm)) { |
| 59 | *error = KM_ERROR_INVALID_KEY_BLOB; |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | return pkey.release(); |
| 64 | } |
| 65 | |
| 66 | static const keymaster_key_format_t supported_import_formats[] = {KM_KEY_FORMAT_PKCS8}; |
| 67 | const keymaster_key_format_t* AsymmetricKeyFactory::SupportedImportFormats(size_t* format_count) { |
| 68 | *format_count = array_length(supported_import_formats); |
| 69 | return supported_import_formats; |
| 70 | } |
| 71 | |
| 72 | static const keymaster_key_format_t supported_export_formats[] = {KM_KEY_FORMAT_X509}; |
| 73 | const keymaster_key_format_t* AsymmetricKeyFactory::SupportedExportFormats(size_t* format_count) { |
| 74 | *format_count = array_length(supported_export_formats); |
| 75 | return supported_export_formats; |
| 76 | } |
| 77 | |
| 78 | /* static */ |
| 79 | int AsymmetricKeyFactory::convert_to_evp(keymaster_algorithm_t algorithm) { |
| 80 | switch (algorithm) { |
| 81 | case KM_ALGORITHM_RSA: |
| 82 | return EVP_PKEY_RSA; |
| 83 | case KM_ALGORITHM_ECDSA: |
| 84 | return EVP_PKEY_EC; |
| 85 | default: |
| 86 | return -1; |
| 87 | }; |
| 88 | } |
| 89 | |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame] | 90 | keymaster_error_t AsymmetricKey::LoadKey(const UnencryptedKeyBlob& blob) { |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 91 | UniquePtr<EVP_PKEY, EVP_PKEY_Delete> evp_key(EVP_PKEY_new()); |
| 92 | if (evp_key.get() == NULL) |
| 93 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 94 | |
| 95 | EVP_PKEY* tmp_pkey = evp_key.get(); |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame] | 96 | const uint8_t* key_material = blob.unencrypted_key_material(); |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 97 | if (d2i_PrivateKey(evp_key_type(), &tmp_pkey, &key_material, blob.key_material_length()) == |
| 98 | NULL) { |
| 99 | return KM_ERROR_INVALID_KEY_BLOB; |
| 100 | } |
| 101 | if (!EvpToInternal(evp_key.get())) |
| 102 | return KM_ERROR_UNKNOWN_ERROR; |
| 103 | |
| 104 | return KM_ERROR_OK; |
| 105 | } |
| 106 | |
| 107 | keymaster_error_t AsymmetricKey::key_material(UniquePtr<uint8_t[]>* material, size_t* size) const { |
| 108 | if (material == NULL || size == NULL) |
| 109 | return KM_ERROR_OUTPUT_PARAMETER_NULL; |
| 110 | |
| 111 | UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new()); |
| 112 | if (pkey.get() == NULL) |
| 113 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 114 | |
| 115 | if (!InternalToEvp(pkey.get())) |
| 116 | return KM_ERROR_UNKNOWN_ERROR; |
| 117 | |
| 118 | *size = i2d_PrivateKey(pkey.get(), NULL /* key_data*/); |
| 119 | if (*size <= 0) |
| 120 | return KM_ERROR_UNKNOWN_ERROR; |
| 121 | |
| 122 | material->reset(new uint8_t[*size]); |
| 123 | uint8_t* tmp = material->get(); |
| 124 | i2d_PrivateKey(pkey.get(), &tmp); |
| 125 | |
| 126 | return KM_ERROR_OK; |
| 127 | } |
| 128 | |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 129 | keymaster_error_t AsymmetricKey::formatted_key_material(keymaster_key_format_t format, |
| 130 | UniquePtr<uint8_t[]>* material, |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 131 | size_t* size) const { |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 132 | if (format != KM_KEY_FORMAT_X509) |
| 133 | return KM_ERROR_UNSUPPORTED_KEY_FORMAT; |
| 134 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 135 | if (material == NULL || size == NULL) |
| 136 | return KM_ERROR_OUTPUT_PARAMETER_NULL; |
| 137 | |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 138 | UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new()); |
| 139 | if (!InternalToEvp(pkey.get())) |
| 140 | return KM_ERROR_UNKNOWN_ERROR; |
| 141 | |
| 142 | int key_data_length = i2d_PUBKEY(pkey.get(), NULL); |
| 143 | if (key_data_length <= 0) |
| 144 | return KM_ERROR_UNKNOWN_ERROR; |
| 145 | |
| 146 | material->reset(new uint8_t[key_data_length]); |
| 147 | if (material->get() == NULL) |
| 148 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 149 | |
| 150 | uint8_t* tmp = material->get(); |
| 151 | if (i2d_PUBKEY(pkey.get(), &tmp) != key_data_length) { |
| 152 | material->reset(); |
| 153 | return KM_ERROR_UNKNOWN_ERROR; |
| 154 | } |
| 155 | |
| 156 | *size = key_data_length; |
| 157 | return KM_ERROR_OK; |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 158 | } |
| 159 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 160 | } // namespace keymaster |