blob: 91e1499d691207d737612f26fb5587efa6c8e628 [file] [log] [blame]
Shawn Willden2c8dd3e2014-09-18 15:16:31 -06001/*
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 Willden2c8dd3e2014-09-18 15:16:31 -060017#include "rsa_key.h"
Shawn Willden567a4a02014-12-31 12:14:46 -070018
19#include "openssl_err.h"
20#include "openssl_utils.h"
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060021#include "rsa_operation.h"
22#include "unencrypted_key_blob.h"
23
Adam Langleya550fba2015-02-13 14:44:14 -080024#if defined(OPENSSL_IS_BORINGSSL)
25typedef size_t openssl_size_t;
26#else
27typedef int openssl_size_t;
28#endif
29
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060030namespace keymaster {
31
32const uint32_t RSA_DEFAULT_KEY_SIZE = 2048;
33const uint64_t RSA_DEFAULT_EXPONENT = 65537;
34
Shawn Willdena278f612014-12-23 11:22:21 -070035class RsaKeyFactory : public AsymmetricKeyFactory {
36 public:
37 virtual keymaster_algorithm_t registry_key() const { return KM_ALGORITHM_RSA; }
Shawn Willden567a4a02014-12-31 12:14:46 -070038 virtual Key* GenerateKey(const AuthorizationSet& key_description, keymaster_error_t* error);
Shawn Willdena278f612014-12-23 11:22:21 -070039 virtual Key* ImportKey(const AuthorizationSet& key_description,
40 keymaster_key_format_t key_format, const uint8_t* key_data,
Shawn Willden567a4a02014-12-31 12:14:46 -070041 size_t key_data_length, keymaster_error_t* error);
42 virtual Key* LoadKey(const UnencryptedKeyBlob& blob, keymaster_error_t* error) {
43 return new RsaKey(blob, error);
Shawn Willdena278f612014-12-23 11:22:21 -070044 }
Shawn Willden5fad7852015-01-26 16:10:56 -070045 virtual Key* RescopeKey(const UnencryptedKeyBlob& blob,
46 const AuthorizationSet& new_authorizations, keymaster_error_t* error);
Shawn Willdena278f612014-12-23 11:22:21 -070047};
48static KeyFactoryRegistry::Registration<RsaKeyFactory> registration;
49
Shawn Willden567a4a02014-12-31 12:14:46 -070050Key* RsaKeyFactory::GenerateKey(const AuthorizationSet& key_description, keymaster_error_t* error) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060051 if (!error)
52 return NULL;
53
54 AuthorizationSet authorizations(key_description);
55
56 uint64_t public_exponent = RSA_DEFAULT_EXPONENT;
57 if (!authorizations.GetTagValue(TAG_RSA_PUBLIC_EXPONENT, &public_exponent))
58 authorizations.push_back(Authorization(TAG_RSA_PUBLIC_EXPONENT, public_exponent));
59
60 uint32_t key_size = RSA_DEFAULT_KEY_SIZE;
61 if (!authorizations.GetTagValue(TAG_KEY_SIZE, &key_size))
62 authorizations.push_back(Authorization(TAG_KEY_SIZE, key_size));
63
64 UniquePtr<BIGNUM, BIGNUM_Delete> exponent(BN_new());
Shawn Willdena278f612014-12-23 11:22:21 -070065 UniquePtr<RSA, RsaKey::RSA_Delete> rsa_key(RSA_new());
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060066 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new());
67 if (rsa_key.get() == NULL || pkey.get() == NULL) {
68 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
69 return NULL;
70 }
71
72 if (!BN_set_word(exponent.get(), public_exponent) ||
73 !RSA_generate_key_ex(rsa_key.get(), key_size, exponent.get(), NULL /* callback */)) {
Shawn Willden567a4a02014-12-31 12:14:46 -070074 *error = TranslateLastOpenSslError();
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060075 return NULL;
76 }
77
Shawn Willden567a4a02014-12-31 12:14:46 -070078 RsaKey* new_key = new RsaKey(rsa_key.release(), authorizations);
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060079 *error = new_key ? KM_ERROR_OK : KM_ERROR_MEMORY_ALLOCATION_FAILED;
80 return new_key;
81}
82
Shawn Willdena278f612014-12-23 11:22:21 -070083Key* RsaKeyFactory::ImportKey(const AuthorizationSet& key_description,
84 keymaster_key_format_t key_format, const uint8_t* key_data,
Shawn Willden567a4a02014-12-31 12:14:46 -070085 size_t key_data_length, keymaster_error_t* error) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060086 if (!error)
87 return NULL;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060088
Shawn Willdena278f612014-12-23 11:22:21 -070089 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(
90 ExtractEvpKey(key_format, KM_ALGORITHM_RSA, key_data, key_data_length, error));
91 if (*error != KM_ERROR_OK)
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060092 return NULL;
Shawn Willdena278f612014-12-23 11:22:21 -070093 assert(pkey.get());
94
95 UniquePtr<RSA, RsaKey::RSA_Delete> rsa_key(EVP_PKEY_get1_RSA(pkey.get()));
96 if (!rsa_key.get()) {
Shawn Willden567a4a02014-12-31 12:14:46 -070097 *error = TranslateLastOpenSslError();
Shawn Willdena278f612014-12-23 11:22:21 -070098 return NULL;
99 }
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600100
101 AuthorizationSet authorizations(key_description);
102
103 uint64_t public_exponent;
104 if (authorizations.GetTagValue(TAG_RSA_PUBLIC_EXPONENT, &public_exponent)) {
105 // public_exponent specified, make sure it matches the key
106 UniquePtr<BIGNUM, BIGNUM_Delete> public_exponent_bn(BN_new());
107 if (!BN_set_word(public_exponent_bn.get(), public_exponent))
108 return NULL;
109 if (BN_cmp(public_exponent_bn.get(), rsa_key->e) != 0) {
110 *error = KM_ERROR_IMPORT_PARAMETER_MISMATCH;
111 return NULL;
112 }
113 } else {
114 // public_exponent not specified, use the one from the key.
115 public_exponent = BN_get_word(rsa_key->e);
116 if (public_exponent == 0xffffffffL) {
117 *error = KM_ERROR_IMPORT_PARAMETER_MISMATCH;
118 return NULL;
119 }
120 authorizations.push_back(TAG_RSA_PUBLIC_EXPONENT, public_exponent);
121 }
122
123 uint32_t key_size;
124 if (authorizations.GetTagValue(TAG_KEY_SIZE, &key_size)) {
125 // key_size specified, make sure it matches the key.
Shawn Willden2c242002015-02-27 07:01:02 -0700126 if (RSA_size(rsa_key.get()) * 8 != (openssl_size_t)key_size) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600127 *error = KM_ERROR_IMPORT_PARAMETER_MISMATCH;
128 return NULL;
129 }
130 } else {
131 key_size = RSA_size(rsa_key.get()) * 8;
132 authorizations.push_back(TAG_KEY_SIZE, key_size);
133 }
134
135 keymaster_algorithm_t algorithm;
136 if (authorizations.GetTagValue(TAG_ALGORITHM, &algorithm)) {
137 if (algorithm != KM_ALGORITHM_RSA) {
138 *error = KM_ERROR_IMPORT_PARAMETER_MISMATCH;
139 return NULL;
140 }
141 } else {
142 authorizations.push_back(TAG_ALGORITHM, KM_ALGORITHM_RSA);
143 }
144
145 // Don't bother with the other parameters. If the necessary padding, digest, purpose, etc. are
146 // missing, the error will be diagnosed when the key is used (when auth checking is
147 // implemented).
148 *error = KM_ERROR_OK;
Shawn Willden567a4a02014-12-31 12:14:46 -0700149 return new RsaKey(rsa_key.release(), authorizations);
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600150}
151
Shawn Willden5fad7852015-01-26 16:10:56 -0700152Key* RsaKeyFactory::RescopeKey(const UnencryptedKeyBlob& blob,
153 const AuthorizationSet& new_authorizations,
154 keymaster_error_t* error) {
155 if (!error)
156 return NULL;
157
158 RsaKey original_key(blob, error);
159 if (*error != KM_ERROR_OK)
160 return NULL;
161
162 RsaKey* new_key = new RsaKey(original_key.rsa_key_.release(), new_authorizations);
163 *error = new_key ? KM_ERROR_OK : KM_ERROR_MEMORY_ALLOCATION_FAILED;
164 return new_key;
165}
166
Shawn Willden567a4a02014-12-31 12:14:46 -0700167RsaKey::RsaKey(const UnencryptedKeyBlob& blob, keymaster_error_t* error) : AsymmetricKey(blob) {
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600168 if (error)
169 *error = LoadKey(blob);
170}
171
Shawn Willden63ac0432014-12-29 14:07:08 -0700172RSA* RsaKey::key() const {
173 return rsa_key_.get();
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600174}
175
176bool RsaKey::EvpToInternal(const EVP_PKEY* pkey) {
177 rsa_key_.reset(EVP_PKEY_get1_RSA(const_cast<EVP_PKEY*>(pkey)));
178 return rsa_key_.get() != NULL;
179}
180
181bool RsaKey::InternalToEvp(EVP_PKEY* pkey) const {
182 return EVP_PKEY_set1_RSA(pkey, rsa_key_.get()) == 1;
183}
184
Shawn Willden4200f212014-12-02 07:01:21 -0700185bool RsaKey::SupportedMode(keymaster_purpose_t purpose, keymaster_padding_t padding) {
186 switch (purpose) {
187 case KM_PURPOSE_SIGN:
188 case KM_PURPOSE_VERIFY:
Shawn Willdenf90f2352014-12-18 23:01:15 -0700189 return padding == KM_PAD_NONE || padding == KM_PAD_RSA_PSS ||
190 padding == KM_PAD_RSA_PKCS1_1_5_SIGN;
Shawn Willden4200f212014-12-02 07:01:21 -0700191 break;
192 case KM_PURPOSE_ENCRYPT:
193 case KM_PURPOSE_DECRYPT:
194 return padding == KM_PAD_RSA_OAEP || padding == KM_PAD_RSA_PKCS1_1_5_ENCRYPT;
195 break;
196 };
197 return false;
198}
199
200bool RsaKey::SupportedMode(keymaster_purpose_t purpose, keymaster_digest_t digest) {
201 switch (purpose) {
202 case KM_PURPOSE_SIGN:
203 case KM_PURPOSE_VERIFY:
Shawn Willden61902362014-12-18 10:33:24 -0700204 return digest == KM_DIGEST_NONE || digest == KM_DIGEST_SHA_2_256;
Shawn Willden4200f212014-12-02 07:01:21 -0700205 break;
206 case KM_PURPOSE_ENCRYPT:
207 case KM_PURPOSE_DECRYPT:
208 /* Don't care */
209 break;
210 };
211 return true;
212}
213
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600214} // namespace keymaster