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 | |
| 17 | #include "openssl_utils.h" |
| 18 | #include "rsa_key.h" |
| 19 | #include "rsa_operation.h" |
| 20 | #include "unencrypted_key_blob.h" |
| 21 | |
| 22 | namespace keymaster { |
| 23 | |
| 24 | const uint32_t RSA_DEFAULT_KEY_SIZE = 2048; |
| 25 | const uint64_t RSA_DEFAULT_EXPONENT = 65537; |
| 26 | |
| 27 | /* static */ |
| 28 | RsaKey* RsaKey::GenerateKey(const AuthorizationSet& key_description, const Logger& logger, |
| 29 | keymaster_error_t* error) { |
| 30 | if (!error) |
| 31 | return NULL; |
| 32 | |
| 33 | AuthorizationSet authorizations(key_description); |
| 34 | |
| 35 | uint64_t public_exponent = RSA_DEFAULT_EXPONENT; |
| 36 | if (!authorizations.GetTagValue(TAG_RSA_PUBLIC_EXPONENT, &public_exponent)) |
| 37 | authorizations.push_back(Authorization(TAG_RSA_PUBLIC_EXPONENT, public_exponent)); |
| 38 | |
| 39 | uint32_t key_size = RSA_DEFAULT_KEY_SIZE; |
| 40 | if (!authorizations.GetTagValue(TAG_KEY_SIZE, &key_size)) |
| 41 | authorizations.push_back(Authorization(TAG_KEY_SIZE, key_size)); |
| 42 | |
| 43 | UniquePtr<BIGNUM, BIGNUM_Delete> exponent(BN_new()); |
| 44 | UniquePtr<RSA, RSA_Delete> rsa_key(RSA_new()); |
| 45 | UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new()); |
| 46 | if (rsa_key.get() == NULL || pkey.get() == NULL) { |
| 47 | *error = KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | if (!BN_set_word(exponent.get(), public_exponent) || |
| 52 | !RSA_generate_key_ex(rsa_key.get(), key_size, exponent.get(), NULL /* callback */)) { |
| 53 | *error = KM_ERROR_UNKNOWN_ERROR; |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | RsaKey* new_key = new RsaKey(rsa_key.release(), authorizations, logger); |
| 58 | *error = new_key ? KM_ERROR_OK : KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 59 | return new_key; |
| 60 | } |
| 61 | |
| 62 | /* static */ |
| 63 | RsaKey* RsaKey::ImportKey(const AuthorizationSet& key_description, EVP_PKEY* pkey, |
| 64 | const Logger& logger, keymaster_error_t* error) { |
| 65 | if (!error) |
| 66 | return NULL; |
| 67 | *error = KM_ERROR_UNKNOWN_ERROR; |
| 68 | |
| 69 | UniquePtr<RSA, RSA_Delete> rsa_key(EVP_PKEY_get1_RSA(pkey)); |
| 70 | if (!rsa_key.get()) |
| 71 | return NULL; |
| 72 | |
| 73 | AuthorizationSet authorizations(key_description); |
| 74 | |
| 75 | uint64_t public_exponent; |
| 76 | if (authorizations.GetTagValue(TAG_RSA_PUBLIC_EXPONENT, &public_exponent)) { |
| 77 | // public_exponent specified, make sure it matches the key |
| 78 | UniquePtr<BIGNUM, BIGNUM_Delete> public_exponent_bn(BN_new()); |
| 79 | if (!BN_set_word(public_exponent_bn.get(), public_exponent)) |
| 80 | return NULL; |
| 81 | if (BN_cmp(public_exponent_bn.get(), rsa_key->e) != 0) { |
| 82 | *error = KM_ERROR_IMPORT_PARAMETER_MISMATCH; |
| 83 | return NULL; |
| 84 | } |
| 85 | } else { |
| 86 | // public_exponent not specified, use the one from the key. |
| 87 | public_exponent = BN_get_word(rsa_key->e); |
| 88 | if (public_exponent == 0xffffffffL) { |
| 89 | *error = KM_ERROR_IMPORT_PARAMETER_MISMATCH; |
| 90 | return NULL; |
| 91 | } |
| 92 | authorizations.push_back(TAG_RSA_PUBLIC_EXPONENT, public_exponent); |
| 93 | } |
| 94 | |
| 95 | uint32_t key_size; |
| 96 | if (authorizations.GetTagValue(TAG_KEY_SIZE, &key_size)) { |
| 97 | // key_size specified, make sure it matches the key. |
| 98 | if (RSA_size(rsa_key.get()) != (int)key_size) { |
| 99 | *error = KM_ERROR_IMPORT_PARAMETER_MISMATCH; |
| 100 | return NULL; |
| 101 | } |
| 102 | } else { |
| 103 | key_size = RSA_size(rsa_key.get()) * 8; |
| 104 | authorizations.push_back(TAG_KEY_SIZE, key_size); |
| 105 | } |
| 106 | |
| 107 | keymaster_algorithm_t algorithm; |
| 108 | if (authorizations.GetTagValue(TAG_ALGORITHM, &algorithm)) { |
| 109 | if (algorithm != KM_ALGORITHM_RSA) { |
| 110 | *error = KM_ERROR_IMPORT_PARAMETER_MISMATCH; |
| 111 | return NULL; |
| 112 | } |
| 113 | } else { |
| 114 | authorizations.push_back(TAG_ALGORITHM, KM_ALGORITHM_RSA); |
| 115 | } |
| 116 | |
| 117 | // Don't bother with the other parameters. If the necessary padding, digest, purpose, etc. are |
| 118 | // missing, the error will be diagnosed when the key is used (when auth checking is |
| 119 | // implemented). |
| 120 | *error = KM_ERROR_OK; |
| 121 | return new RsaKey(rsa_key.release(), authorizations, logger); |
| 122 | } |
| 123 | |
| 124 | RsaKey::RsaKey(const UnencryptedKeyBlob& blob, const Logger& logger, keymaster_error_t* error) |
| 125 | : AsymmetricKey(blob, logger) { |
| 126 | if (error) |
| 127 | *error = LoadKey(blob); |
| 128 | } |
| 129 | |
| 130 | Operation* RsaKey::CreateOperation(keymaster_purpose_t purpose, keymaster_digest_t digest, |
| 131 | keymaster_padding_t padding, keymaster_error_t* error) { |
| 132 | Operation* op; |
| 133 | switch (purpose) { |
| 134 | case KM_PURPOSE_SIGN: |
| 135 | op = new RsaSignOperation(purpose, logger_, digest, padding, rsa_key_.release()); |
| 136 | break; |
| 137 | case KM_PURPOSE_VERIFY: |
| 138 | op = new RsaVerifyOperation(purpose, logger_, digest, padding, rsa_key_.release()); |
| 139 | break; |
| 140 | default: |
| 141 | *error = KM_ERROR_UNIMPLEMENTED; |
| 142 | return NULL; |
| 143 | } |
| 144 | *error = op ? KM_ERROR_OK : KM_ERROR_MEMORY_ALLOCATION_FAILED; |
| 145 | return op; |
| 146 | } |
| 147 | |
| 148 | bool RsaKey::EvpToInternal(const EVP_PKEY* pkey) { |
| 149 | rsa_key_.reset(EVP_PKEY_get1_RSA(const_cast<EVP_PKEY*>(pkey))); |
| 150 | return rsa_key_.get() != NULL; |
| 151 | } |
| 152 | |
| 153 | bool RsaKey::InternalToEvp(EVP_PKEY* pkey) const { |
| 154 | return EVP_PKEY_set1_RSA(pkey, rsa_key_.get()) == 1; |
| 155 | } |
| 156 | |
| 157 | } // namespace keymaster |