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 | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 17 | #include <openssl/x509.h> |
| 18 | |
Shawn Willden | b9d584d | 2015-01-22 16:35:00 -0700 | [diff] [blame] | 19 | #include <hardware/keymaster_defs.h> |
Shawn Willden | 98d9b92 | 2014-08-26 08:14:10 -0600 | [diff] [blame] | 20 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 21 | #include "asymmetric_key.h" |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 22 | #include "openssl_utils.h" |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame] | 23 | #include "unencrypted_key_blob.h" |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 24 | |
| 25 | namespace keymaster { |
| 26 | |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame] | 27 | keymaster_error_t AsymmetricKey::LoadKey(const UnencryptedKeyBlob& blob) { |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 28 | UniquePtr<EVP_PKEY, EVP_PKEY_Delete> evp_key(EVP_PKEY_new()); |
| 29 | if (evp_key.get() == NULL) |
| 30 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 31 | |
| 32 | EVP_PKEY* tmp_pkey = evp_key.get(); |
Shawn Willden | 72014ad | 2014-09-17 13:04:10 -0600 | [diff] [blame] | 33 | const uint8_t* key_material = blob.unencrypted_key_material(); |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 34 | if (d2i_PrivateKey(evp_key_type(), &tmp_pkey, &key_material, blob.key_material_length()) == |
| 35 | NULL) { |
| 36 | return KM_ERROR_INVALID_KEY_BLOB; |
| 37 | } |
| 38 | if (!EvpToInternal(evp_key.get())) |
| 39 | return KM_ERROR_UNKNOWN_ERROR; |
| 40 | |
| 41 | return KM_ERROR_OK; |
| 42 | } |
| 43 | |
| 44 | keymaster_error_t AsymmetricKey::key_material(UniquePtr<uint8_t[]>* material, size_t* size) const { |
| 45 | if (material == NULL || size == NULL) |
| 46 | return KM_ERROR_OUTPUT_PARAMETER_NULL; |
| 47 | |
| 48 | UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new()); |
| 49 | if (pkey.get() == NULL) |
| 50 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 51 | |
| 52 | if (!InternalToEvp(pkey.get())) |
| 53 | return KM_ERROR_UNKNOWN_ERROR; |
| 54 | |
| 55 | *size = i2d_PrivateKey(pkey.get(), NULL /* key_data*/); |
| 56 | if (*size <= 0) |
| 57 | return KM_ERROR_UNKNOWN_ERROR; |
| 58 | |
| 59 | material->reset(new uint8_t[*size]); |
| 60 | uint8_t* tmp = material->get(); |
| 61 | i2d_PrivateKey(pkey.get(), &tmp); |
| 62 | |
| 63 | return KM_ERROR_OK; |
| 64 | } |
| 65 | |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 66 | keymaster_error_t AsymmetricKey::formatted_key_material(keymaster_key_format_t format, |
| 67 | UniquePtr<uint8_t[]>* material, |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 68 | size_t* size) const { |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 69 | if (format != KM_KEY_FORMAT_X509) |
| 70 | return KM_ERROR_UNSUPPORTED_KEY_FORMAT; |
| 71 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 72 | if (material == NULL || size == NULL) |
| 73 | return KM_ERROR_OUTPUT_PARAMETER_NULL; |
| 74 | |
Shawn Willden | f268d74 | 2014-08-19 15:36:26 -0600 | [diff] [blame] | 75 | UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new()); |
| 76 | if (!InternalToEvp(pkey.get())) |
| 77 | return KM_ERROR_UNKNOWN_ERROR; |
| 78 | |
| 79 | int key_data_length = i2d_PUBKEY(pkey.get(), NULL); |
| 80 | if (key_data_length <= 0) |
| 81 | return KM_ERROR_UNKNOWN_ERROR; |
| 82 | |
| 83 | material->reset(new uint8_t[key_data_length]); |
| 84 | if (material->get() == NULL) |
| 85 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 86 | |
| 87 | uint8_t* tmp = material->get(); |
| 88 | if (i2d_PUBKEY(pkey.get(), &tmp) != key_data_length) { |
| 89 | material->reset(); |
| 90 | return KM_ERROR_UNKNOWN_ERROR; |
| 91 | } |
| 92 | |
| 93 | *size = key_data_length; |
| 94 | return KM_ERROR_OK; |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 95 | } |
| 96 | |
Shawn Willden | d67afae | 2014-08-19 12:36:27 -0600 | [diff] [blame] | 97 | } // namespace keymaster |