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