blob: 94f87a9d32958db46fe4918cd215d91e776d73e2 [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
Shawn Willden0cb69422015-05-26 08:31:37 -060019#include <keymaster/keymaster_context.h>
20
Shawn Willden567a4a02014-12-31 12:14:46 -070021#include "openssl_err.h"
22#include "openssl_utils.h"
Shawn Willden06298102015-05-25 23:12:48 -060023#include "rsa_operation.h"
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060024
Shawn Willdend79791b2015-05-09 12:48:36 +000025#if defined(OPENSSL_IS_BORINGSSL)
26typedef size_t openssl_size_t;
27#else
28typedef int openssl_size_t;
29#endif
30
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060031namespace keymaster {
32
Shawn Willden06298102015-05-25 23:12:48 -060033static RsaSigningOperationFactory sign_factory;
34static RsaVerificationOperationFactory verify_factory;
35static RsaEncryptionOperationFactory encrypt_factory;
36static RsaDecryptionOperationFactory decrypt_factory;
37
38OperationFactory* RsaKeyFactory::GetOperationFactory(keymaster_purpose_t purpose) const {
39 switch (purpose) {
40 case KM_PURPOSE_SIGN:
41 return &sign_factory;
42 case KM_PURPOSE_VERIFY:
43 return &verify_factory;
44 case KM_PURPOSE_ENCRYPT:
45 return &encrypt_factory;
46 case KM_PURPOSE_DECRYPT:
47 return &decrypt_factory;
48 default:
49 return nullptr;
50 }
51}
52
Shawn Willden0cb69422015-05-26 08:31:37 -060053keymaster_error_t RsaKeyFactory::GenerateKey(const AuthorizationSet& key_description,
54 KeymasterKeyBlob* key_blob,
55 AuthorizationSet* hw_enforced,
Shawn Willden06298102015-05-25 23:12:48 -060056 AuthorizationSet* sw_enforced) const {
Shawn Willden0cb69422015-05-26 08:31:37 -060057 if (!key_blob || !hw_enforced || !sw_enforced)
58 return KM_ERROR_OUTPUT_PARAMETER_NULL;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060059
60 AuthorizationSet authorizations(key_description);
61
Shawn Willden3b4e1652015-02-27 13:33:01 -070062 uint64_t public_exponent;
63 if (!authorizations.GetTagValue(TAG_RSA_PUBLIC_EXPONENT, &public_exponent)) {
64 LOG_E("%s", "No public exponent specified for RSA key generation");
Shawn Willden0cb69422015-05-26 08:31:37 -060065 return KM_ERROR_INVALID_ARGUMENT;
Shawn Willden3b4e1652015-02-27 13:33:01 -070066 }
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060067
Shawn Willden3b4e1652015-02-27 13:33:01 -070068 uint32_t key_size;
69 if (!authorizations.GetTagValue(TAG_KEY_SIZE, &key_size)) {
70 LOG_E("%s", "No key size specified for RSA key generation");
Shawn Willden0cb69422015-05-26 08:31:37 -060071 return KM_ERROR_UNSUPPORTED_KEY_SIZE;
Shawn Willden3b4e1652015-02-27 13:33:01 -070072 }
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060073
74 UniquePtr<BIGNUM, BIGNUM_Delete> exponent(BN_new());
Shawn Willdena278f612014-12-23 11:22:21 -070075 UniquePtr<RSA, RsaKey::RSA_Delete> rsa_key(RSA_new());
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060076 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new());
Shawn Willden0cb69422015-05-26 08:31:37 -060077 if (exponent.get() == NULL || rsa_key.get() == NULL || pkey.get() == NULL)
78 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060079
80 if (!BN_set_word(exponent.get(), public_exponent) ||
Shawn Willden0cb69422015-05-26 08:31:37 -060081 !RSA_generate_key_ex(rsa_key.get(), key_size, exponent.get(), NULL /* callback */))
82 return TranslateLastOpenSslError();
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060083
Shawn Willden0cb69422015-05-26 08:31:37 -060084 if (EVP_PKEY_set1_RSA(pkey.get(), rsa_key.get()) != 1)
85 return TranslateLastOpenSslError();
86
87 KeymasterKeyBlob key_material;
88 keymaster_error_t error = EvpKeyToKeyMaterial(pkey.get(), &key_material);
89 if (error != KM_ERROR_OK)
90 return error;
91
92 return context_->CreateKeyBlob(authorizations, KM_ORIGIN_GENERATED, key_material, key_blob,
93 hw_enforced, sw_enforced);
Shawn Willden2c8dd3e2014-09-18 15:16:31 -060094}
95
Shawn Willden0cb69422015-05-26 08:31:37 -060096keymaster_error_t RsaKeyFactory::ImportKey(const AuthorizationSet& key_description,
97 keymaster_key_format_t input_key_material_format,
98 const KeymasterKeyBlob& input_key_material,
99 KeymasterKeyBlob* output_key_blob,
100 AuthorizationSet* hw_enforced,
Shawn Willden06298102015-05-25 23:12:48 -0600101 AuthorizationSet* sw_enforced) const {
Shawn Willden0cb69422015-05-26 08:31:37 -0600102 if (!output_key_blob || !hw_enforced || !sw_enforced)
103 return KM_ERROR_OUTPUT_PARAMETER_NULL;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600104
Shawn Willden2beb6282015-05-20 16:36:24 -0600105 AuthorizationSet authorizations;
106 uint64_t public_exponent;
107 uint32_t key_size;
Shawn Willden0cb69422015-05-26 08:31:37 -0600108 keymaster_error_t error =
Shawn Willden2beb6282015-05-20 16:36:24 -0600109 UpdateImportKeyDescription(key_description, input_key_material_format, input_key_material,
110 &authorizations, &public_exponent, &key_size);
111 if (error != KM_ERROR_OK)
112 return error;
113 return context_->CreateKeyBlob(authorizations, KM_ORIGIN_IMPORTED, input_key_material,
114 output_key_blob, hw_enforced, sw_enforced);
115}
116
117keymaster_error_t RsaKeyFactory::UpdateImportKeyDescription(const AuthorizationSet& key_description,
118 keymaster_key_format_t key_format,
119 const KeymasterKeyBlob& key_material,
120 AuthorizationSet* updated_description,
121 uint64_t* public_exponent,
Shawn Willden06298102015-05-25 23:12:48 -0600122 uint32_t* key_size) const {
Shawn Willden2beb6282015-05-20 16:36:24 -0600123 if (!updated_description || !public_exponent || !key_size)
124 return KM_ERROR_OUTPUT_PARAMETER_NULL;
125
126 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey;
127 keymaster_error_t error = KeyMaterialToEvpKey(key_format, key_material, &pkey);
Shawn Willden0cb69422015-05-26 08:31:37 -0600128 if (error != KM_ERROR_OK)
129 return error;
Shawn Willdena278f612014-12-23 11:22:21 -0700130
131 UniquePtr<RSA, RsaKey::RSA_Delete> rsa_key(EVP_PKEY_get1_RSA(pkey.get()));
Shawn Willden0cb69422015-05-26 08:31:37 -0600132 if (!rsa_key.get())
133 return TranslateLastOpenSslError();
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600134
Shawn Willden2beb6282015-05-20 16:36:24 -0600135 updated_description->Reinitialize(key_description);
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600136
Shawn Willden2beb6282015-05-20 16:36:24 -0600137 *public_exponent = BN_get_word(rsa_key->e);
138 if (*public_exponent == 0xffffffffL)
139 return KM_ERROR_INVALID_KEY_BLOB;
140 if (!updated_description->GetTagValue(TAG_RSA_PUBLIC_EXPONENT, public_exponent))
141 updated_description->push_back(TAG_RSA_PUBLIC_EXPONENT, *public_exponent);
142 if (*public_exponent != BN_get_word(rsa_key->e))
143 return KM_ERROR_IMPORT_PARAMETER_MISMATCH;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600144
Shawn Willden2beb6282015-05-20 16:36:24 -0600145 *key_size = RSA_size(rsa_key.get()) * 8;
146 if (!updated_description->GetTagValue(TAG_KEY_SIZE, key_size))
147 updated_description->push_back(TAG_KEY_SIZE, *key_size);
148 if (RSA_size(rsa_key.get()) * 8 != (openssl_size_t)*key_size)
149 return KM_ERROR_IMPORT_PARAMETER_MISMATCH;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600150
Shawn Willden2beb6282015-05-20 16:36:24 -0600151 keymaster_algorithm_t algorithm = KM_ALGORITHM_RSA;
152 if (!updated_description->GetTagValue(TAG_ALGORITHM, &algorithm))
153 updated_description->push_back(TAG_ALGORITHM, KM_ALGORITHM_RSA);
154 if (algorithm != KM_ALGORITHM_RSA)
155 return KM_ERROR_IMPORT_PARAMETER_MISMATCH;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600156
Shawn Willden2beb6282015-05-20 16:36:24 -0600157 return KM_ERROR_OK;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600158}
159
Shawn Willden0cb69422015-05-26 08:31:37 -0600160keymaster_error_t RsaKeyFactory::CreateEmptyKey(const AuthorizationSet& hw_enforced,
161 const AuthorizationSet& sw_enforced,
Shawn Willden06298102015-05-25 23:12:48 -0600162 UniquePtr<AsymmetricKey>* key) const {
Shawn Willden0cb69422015-05-26 08:31:37 -0600163 keymaster_error_t error;
164 key->reset(new RsaKey(hw_enforced, sw_enforced, &error));
165 if (!key->get())
166 error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
167 return error;
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600168}
169
170bool 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
175bool RsaKey::InternalToEvp(EVP_PKEY* pkey) const {
176 return EVP_PKEY_set1_RSA(pkey, rsa_key_.get()) == 1;
177}
178
Shawn Willden4200f212014-12-02 07:01:21 -0700179bool RsaKey::SupportedMode(keymaster_purpose_t purpose, keymaster_padding_t padding) {
180 switch (purpose) {
181 case KM_PURPOSE_SIGN:
182 case KM_PURPOSE_VERIFY:
Shawn Willdenf90f2352014-12-18 23:01:15 -0700183 return padding == KM_PAD_NONE || padding == KM_PAD_RSA_PSS ||
184 padding == KM_PAD_RSA_PKCS1_1_5_SIGN;
Shawn Willden4200f212014-12-02 07:01:21 -0700185 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
194bool RsaKey::SupportedMode(keymaster_purpose_t purpose, keymaster_digest_t digest) {
195 switch (purpose) {
196 case KM_PURPOSE_SIGN:
197 case KM_PURPOSE_VERIFY:
Shawn Willden61902362014-12-18 10:33:24 -0700198 return digest == KM_DIGEST_NONE || digest == KM_DIGEST_SHA_2_256;
Shawn Willden4200f212014-12-02 07:01:21 -0700199 break;
200 case KM_PURPOSE_ENCRYPT:
201 case KM_PURPOSE_DECRYPT:
202 /* Don't care */
203 break;
204 };
205 return true;
206}
207
Shawn Willden2c8dd3e2014-09-18 15:16:31 -0600208} // namespace keymaster