Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -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 | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 17 | #include "rsa_key.h" |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 18 | |
Shawn Willden | 8ba2a04 | 2015-05-18 08:35:28 -0600 | [diff] [blame^] | 19 | #include <keymaster/keymaster_context.h> |
| 20 | |
Shawn Willden | 567a4a0 | 2014-12-31 12:14:46 -0700 | [diff] [blame] | 21 | #include "openssl_err.h" |
| 22 | #include "openssl_utils.h" |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 23 | |
Shawn Willden | d79791b | 2015-05-09 12:48:36 +0000 | [diff] [blame] | 24 | #if defined(OPENSSL_IS_BORINGSSL) |
| 25 | typedef size_t openssl_size_t; |
| 26 | #else |
| 27 | typedef int openssl_size_t; |
| 28 | #endif |
| 29 | |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 30 | namespace keymaster { |
| 31 | |
Shawn Willden | 8ba2a04 | 2015-05-18 08:35:28 -0600 | [diff] [blame^] | 32 | keymaster_error_t RsaKeyFactory::GenerateKey(const AuthorizationSet& key_description, |
| 33 | KeymasterKeyBlob* key_blob, |
| 34 | AuthorizationSet* hw_enforced, |
| 35 | AuthorizationSet* sw_enforced) { |
| 36 | if (!key_blob || !hw_enforced || !sw_enforced) |
| 37 | return KM_ERROR_OUTPUT_PARAMETER_NULL; |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 38 | |
| 39 | AuthorizationSet authorizations(key_description); |
| 40 | |
Shawn Willden | 3b4e165 | 2015-02-27 13:33:01 -0700 | [diff] [blame] | 41 | uint64_t public_exponent; |
| 42 | if (!authorizations.GetTagValue(TAG_RSA_PUBLIC_EXPONENT, &public_exponent)) { |
| 43 | LOG_E("%s", "No public exponent specified for RSA key generation"); |
Shawn Willden | 8ba2a04 | 2015-05-18 08:35:28 -0600 | [diff] [blame^] | 44 | return KM_ERROR_INVALID_ARGUMENT; |
Shawn Willden | 3b4e165 | 2015-02-27 13:33:01 -0700 | [diff] [blame] | 45 | } |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 46 | |
Shawn Willden | 3b4e165 | 2015-02-27 13:33:01 -0700 | [diff] [blame] | 47 | uint32_t key_size; |
| 48 | if (!authorizations.GetTagValue(TAG_KEY_SIZE, &key_size)) { |
| 49 | LOG_E("%s", "No key size specified for RSA key generation"); |
Shawn Willden | 8ba2a04 | 2015-05-18 08:35:28 -0600 | [diff] [blame^] | 50 | return KM_ERROR_UNSUPPORTED_KEY_SIZE; |
Shawn Willden | 3b4e165 | 2015-02-27 13:33:01 -0700 | [diff] [blame] | 51 | } |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 52 | |
| 53 | UniquePtr<BIGNUM, BIGNUM_Delete> exponent(BN_new()); |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame] | 54 | UniquePtr<RSA, RsaKey::RSA_Delete> rsa_key(RSA_new()); |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 55 | UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new()); |
Shawn Willden | 8ba2a04 | 2015-05-18 08:35:28 -0600 | [diff] [blame^] | 56 | if (exponent.get() == NULL || rsa_key.get() == NULL || pkey.get() == NULL) |
| 57 | return KM_ERROR_MEMORY_ALLOCATION_FAILED; |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 58 | |
| 59 | if (!BN_set_word(exponent.get(), public_exponent) || |
Shawn Willden | 8ba2a04 | 2015-05-18 08:35:28 -0600 | [diff] [blame^] | 60 | !RSA_generate_key_ex(rsa_key.get(), key_size, exponent.get(), NULL /* callback */)) |
| 61 | return TranslateLastOpenSslError(); |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 62 | |
Shawn Willden | 8ba2a04 | 2015-05-18 08:35:28 -0600 | [diff] [blame^] | 63 | if (EVP_PKEY_set1_RSA(pkey.get(), rsa_key.get()) != 1) |
| 64 | return TranslateLastOpenSslError(); |
| 65 | |
| 66 | KeymasterKeyBlob key_material; |
| 67 | keymaster_error_t error = EvpKeyToKeyMaterial(pkey.get(), &key_material); |
| 68 | if (error != KM_ERROR_OK) |
| 69 | return error; |
| 70 | |
| 71 | return context_->CreateKeyBlob(authorizations, KM_ORIGIN_GENERATED, key_material, key_blob, |
| 72 | hw_enforced, sw_enforced); |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 73 | } |
| 74 | |
Shawn Willden | 8ba2a04 | 2015-05-18 08:35:28 -0600 | [diff] [blame^] | 75 | keymaster_error_t RsaKeyFactory::ImportKey(const AuthorizationSet& key_description, |
| 76 | keymaster_key_format_t input_key_material_format, |
| 77 | const KeymasterKeyBlob& input_key_material, |
| 78 | KeymasterKeyBlob* output_key_blob, |
| 79 | AuthorizationSet* hw_enforced, |
| 80 | AuthorizationSet* sw_enforced) { |
| 81 | if (!output_key_blob || !hw_enforced || !sw_enforced) |
| 82 | return KM_ERROR_OUTPUT_PARAMETER_NULL; |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 83 | |
Shawn Willden | 8ba2a04 | 2015-05-18 08:35:28 -0600 | [diff] [blame^] | 84 | UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey; |
| 85 | keymaster_error_t error = |
| 86 | KeyMaterialToEvpKey(input_key_material_format, input_key_material, &pkey); |
| 87 | if (error != KM_ERROR_OK) |
| 88 | return error; |
Shawn Willden | a278f61 | 2014-12-23 11:22:21 -0700 | [diff] [blame] | 89 | |
| 90 | UniquePtr<RSA, RsaKey::RSA_Delete> rsa_key(EVP_PKEY_get1_RSA(pkey.get())); |
Shawn Willden | 8ba2a04 | 2015-05-18 08:35:28 -0600 | [diff] [blame^] | 91 | if (!rsa_key.get()) |
| 92 | return TranslateLastOpenSslError(); |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 93 | |
| 94 | AuthorizationSet authorizations(key_description); |
| 95 | |
| 96 | uint64_t public_exponent; |
| 97 | if (authorizations.GetTagValue(TAG_RSA_PUBLIC_EXPONENT, &public_exponent)) { |
| 98 | // public_exponent specified, make sure it matches the key |
| 99 | UniquePtr<BIGNUM, BIGNUM_Delete> public_exponent_bn(BN_new()); |
| 100 | if (!BN_set_word(public_exponent_bn.get(), public_exponent)) |
Shawn Willden | 8ba2a04 | 2015-05-18 08:35:28 -0600 | [diff] [blame^] | 101 | return KM_ERROR_UNKNOWN_ERROR; |
| 102 | if (BN_cmp(public_exponent_bn.get(), rsa_key->e) != 0) |
| 103 | return KM_ERROR_IMPORT_PARAMETER_MISMATCH; |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 104 | } else { |
| 105 | // public_exponent not specified, use the one from the key. |
| 106 | public_exponent = BN_get_word(rsa_key->e); |
Shawn Willden | 8ba2a04 | 2015-05-18 08:35:28 -0600 | [diff] [blame^] | 107 | if (public_exponent == 0xffffffffL) |
| 108 | return KM_ERROR_IMPORT_PARAMETER_MISMATCH; |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 109 | authorizations.push_back(TAG_RSA_PUBLIC_EXPONENT, public_exponent); |
| 110 | } |
| 111 | |
| 112 | uint32_t key_size; |
| 113 | if (authorizations.GetTagValue(TAG_KEY_SIZE, &key_size)) { |
| 114 | // key_size specified, make sure it matches the key. |
Shawn Willden | 8ba2a04 | 2015-05-18 08:35:28 -0600 | [diff] [blame^] | 115 | if (RSA_size(rsa_key.get()) * 8 != (openssl_size_t)key_size) |
| 116 | return KM_ERROR_IMPORT_PARAMETER_MISMATCH; |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 117 | } else { |
| 118 | key_size = RSA_size(rsa_key.get()) * 8; |
| 119 | authorizations.push_back(TAG_KEY_SIZE, key_size); |
| 120 | } |
| 121 | |
| 122 | keymaster_algorithm_t algorithm; |
| 123 | if (authorizations.GetTagValue(TAG_ALGORITHM, &algorithm)) { |
Shawn Willden | 8ba2a04 | 2015-05-18 08:35:28 -0600 | [diff] [blame^] | 124 | if (algorithm != KM_ALGORITHM_RSA) |
| 125 | return KM_ERROR_IMPORT_PARAMETER_MISMATCH; |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 126 | } else { |
| 127 | authorizations.push_back(TAG_ALGORITHM, KM_ALGORITHM_RSA); |
| 128 | } |
| 129 | |
Shawn Willden | 8ba2a04 | 2015-05-18 08:35:28 -0600 | [diff] [blame^] | 130 | return context_->CreateKeyBlob(authorizations, KM_ORIGIN_IMPORTED, input_key_material, |
| 131 | output_key_blob, hw_enforced, sw_enforced); |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 132 | } |
| 133 | |
Shawn Willden | 8ba2a04 | 2015-05-18 08:35:28 -0600 | [diff] [blame^] | 134 | keymaster_error_t RsaKeyFactory::CreateEmptyKey(const AuthorizationSet& hw_enforced, |
| 135 | const AuthorizationSet& sw_enforced, |
| 136 | UniquePtr<AsymmetricKey>* key) { |
| 137 | keymaster_error_t error; |
| 138 | key->reset(new RsaKey(hw_enforced, sw_enforced, &error)); |
| 139 | if (!key->get()) |
| 140 | error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 141 | return error; |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | bool RsaKey::EvpToInternal(const EVP_PKEY* pkey) { |
| 145 | rsa_key_.reset(EVP_PKEY_get1_RSA(const_cast<EVP_PKEY*>(pkey))); |
| 146 | return rsa_key_.get() != NULL; |
| 147 | } |
| 148 | |
| 149 | bool RsaKey::InternalToEvp(EVP_PKEY* pkey) const { |
| 150 | return EVP_PKEY_set1_RSA(pkey, rsa_key_.get()) == 1; |
| 151 | } |
| 152 | |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 153 | bool RsaKey::SupportedMode(keymaster_purpose_t purpose, keymaster_padding_t padding) { |
| 154 | switch (purpose) { |
| 155 | case KM_PURPOSE_SIGN: |
| 156 | case KM_PURPOSE_VERIFY: |
Shawn Willden | f90f235 | 2014-12-18 23:01:15 -0700 | [diff] [blame] | 157 | return padding == KM_PAD_NONE || padding == KM_PAD_RSA_PSS || |
| 158 | padding == KM_PAD_RSA_PKCS1_1_5_SIGN; |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 159 | break; |
| 160 | case KM_PURPOSE_ENCRYPT: |
| 161 | case KM_PURPOSE_DECRYPT: |
| 162 | return padding == KM_PAD_RSA_OAEP || padding == KM_PAD_RSA_PKCS1_1_5_ENCRYPT; |
| 163 | break; |
| 164 | }; |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | bool RsaKey::SupportedMode(keymaster_purpose_t purpose, keymaster_digest_t digest) { |
| 169 | switch (purpose) { |
| 170 | case KM_PURPOSE_SIGN: |
| 171 | case KM_PURPOSE_VERIFY: |
Shawn Willden | 6190236 | 2014-12-18 10:33:24 -0700 | [diff] [blame] | 172 | return digest == KM_DIGEST_NONE || digest == KM_DIGEST_SHA_2_256; |
Shawn Willden | 4200f21 | 2014-12-02 07:01:21 -0700 | [diff] [blame] | 173 | break; |
| 174 | case KM_PURPOSE_ENCRYPT: |
| 175 | case KM_PURPOSE_DECRYPT: |
| 176 | /* Don't care */ |
| 177 | break; |
| 178 | }; |
| 179 | return true; |
| 180 | } |
| 181 | |
Shawn Willden | 2c8dd3e | 2014-09-18 15:16:31 -0600 | [diff] [blame] | 182 | } // namespace keymaster |