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