blob: dfe2dddec4cf29db0590f10d67805f64042f3c78 [file] [log] [blame]
Shawn Willden398c1582015-05-28 00:04:06 -06001/*
2 * Copyright 2015 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 <keymaster/rsa_key_factory.h>
18
Shawn Willden0f906ec2015-06-20 09:16:30 -060019#include <new>
20
Shawn Willden398c1582015-05-28 00:04:06 -060021#include <keymaster/keymaster_context.h>
22
23#include "openssl_err.h"
24#include "openssl_utils.h"
25#include "rsa_key.h"
26#include "rsa_operation.h"
27
28#if defined(OPENSSL_IS_BORINGSSL)
29typedef size_t openssl_size_t;
30#else
31typedef int openssl_size_t;
32#endif
33
34namespace keymaster {
35
36static RsaSigningOperationFactory sign_factory;
37static RsaVerificationOperationFactory verify_factory;
38static RsaEncryptionOperationFactory encrypt_factory;
39static RsaDecryptionOperationFactory decrypt_factory;
40
41OperationFactory* RsaKeyFactory::GetOperationFactory(keymaster_purpose_t purpose) const {
42 switch (purpose) {
43 case KM_PURPOSE_SIGN:
44 return &sign_factory;
45 case KM_PURPOSE_VERIFY:
46 return &verify_factory;
47 case KM_PURPOSE_ENCRYPT:
48 return &encrypt_factory;
49 case KM_PURPOSE_DECRYPT:
50 return &decrypt_factory;
51 default:
52 return nullptr;
53 }
54}
55
56keymaster_error_t RsaKeyFactory::GenerateKey(const AuthorizationSet& key_description,
57 KeymasterKeyBlob* key_blob,
58 AuthorizationSet* hw_enforced,
59 AuthorizationSet* sw_enforced) const {
60 if (!key_blob || !hw_enforced || !sw_enforced)
61 return KM_ERROR_OUTPUT_PARAMETER_NULL;
62
63 AuthorizationSet authorizations(key_description);
64
65 uint64_t public_exponent;
66 if (!authorizations.GetTagValue(TAG_RSA_PUBLIC_EXPONENT, &public_exponent)) {
67 LOG_E("%s", "No public exponent specified for RSA key generation");
68 return KM_ERROR_INVALID_ARGUMENT;
69 }
70
71 uint32_t key_size;
72 if (!authorizations.GetTagValue(TAG_KEY_SIZE, &key_size)) {
73 LOG_E("%s", "No key size specified for RSA key generation");
74 return KM_ERROR_UNSUPPORTED_KEY_SIZE;
75 }
76
77 UniquePtr<BIGNUM, BIGNUM_Delete> exponent(BN_new());
78 UniquePtr<RSA, RsaKey::RSA_Delete> rsa_key(RSA_new());
79 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new());
80 if (exponent.get() == NULL || rsa_key.get() == NULL || pkey.get() == NULL)
81 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
82
83 if (!BN_set_word(exponent.get(), public_exponent) ||
84 !RSA_generate_key_ex(rsa_key.get(), key_size, exponent.get(), NULL /* callback */))
85 return TranslateLastOpenSslError();
86
87 if (EVP_PKEY_set1_RSA(pkey.get(), rsa_key.get()) != 1)
88 return TranslateLastOpenSslError();
89
90 KeymasterKeyBlob key_material;
91 keymaster_error_t error = EvpKeyToKeyMaterial(pkey.get(), &key_material);
92 if (error != KM_ERROR_OK)
93 return error;
94
95 return context_->CreateKeyBlob(authorizations, KM_ORIGIN_GENERATED, key_material, key_blob,
96 hw_enforced, sw_enforced);
97}
98
99keymaster_error_t RsaKeyFactory::ImportKey(const AuthorizationSet& key_description,
100 keymaster_key_format_t input_key_material_format,
101 const KeymasterKeyBlob& input_key_material,
102 KeymasterKeyBlob* output_key_blob,
103 AuthorizationSet* hw_enforced,
104 AuthorizationSet* sw_enforced) const {
105 if (!output_key_blob || !hw_enforced || !sw_enforced)
106 return KM_ERROR_OUTPUT_PARAMETER_NULL;
107
108 AuthorizationSet authorizations;
109 uint64_t public_exponent;
110 uint32_t key_size;
111 keymaster_error_t error =
112 UpdateImportKeyDescription(key_description, input_key_material_format, input_key_material,
113 &authorizations, &public_exponent, &key_size);
114 if (error != KM_ERROR_OK)
115 return error;
116 return context_->CreateKeyBlob(authorizations, KM_ORIGIN_IMPORTED, input_key_material,
117 output_key_blob, hw_enforced, sw_enforced);
118}
119
120keymaster_error_t RsaKeyFactory::UpdateImportKeyDescription(const AuthorizationSet& key_description,
121 keymaster_key_format_t key_format,
122 const KeymasterKeyBlob& key_material,
123 AuthorizationSet* updated_description,
124 uint64_t* public_exponent,
125 uint32_t* key_size) const {
126 if (!updated_description || !public_exponent || !key_size)
127 return KM_ERROR_OUTPUT_PARAMETER_NULL;
128
129 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey;
130 keymaster_error_t error =
131 KeyMaterialToEvpKey(key_format, key_material, keymaster_key_type(), &pkey);
132 if (error != KM_ERROR_OK)
133 return error;
134
135 UniquePtr<RSA, RsaKey::RSA_Delete> rsa_key(EVP_PKEY_get1_RSA(pkey.get()));
136 if (!rsa_key.get())
137 return TranslateLastOpenSslError();
138
139 updated_description->Reinitialize(key_description);
140
141 *public_exponent = BN_get_word(rsa_key->e);
142 if (*public_exponent == 0xffffffffL)
143 return KM_ERROR_INVALID_KEY_BLOB;
144 if (!updated_description->GetTagValue(TAG_RSA_PUBLIC_EXPONENT, public_exponent))
145 updated_description->push_back(TAG_RSA_PUBLIC_EXPONENT, *public_exponent);
146 if (*public_exponent != BN_get_word(rsa_key->e))
147 return KM_ERROR_IMPORT_PARAMETER_MISMATCH;
148
149 *key_size = RSA_size(rsa_key.get()) * 8;
150 if (!updated_description->GetTagValue(TAG_KEY_SIZE, key_size))
151 updated_description->push_back(TAG_KEY_SIZE, *key_size);
152 if (RSA_size(rsa_key.get()) * 8 != (openssl_size_t)*key_size)
153 return KM_ERROR_IMPORT_PARAMETER_MISMATCH;
154
155 keymaster_algorithm_t algorithm = KM_ALGORITHM_RSA;
156 if (!updated_description->GetTagValue(TAG_ALGORITHM, &algorithm))
157 updated_description->push_back(TAG_ALGORITHM, KM_ALGORITHM_RSA);
158 if (algorithm != KM_ALGORITHM_RSA)
159 return KM_ERROR_IMPORT_PARAMETER_MISMATCH;
160
161 return KM_ERROR_OK;
162}
163
164keymaster_error_t RsaKeyFactory::CreateEmptyKey(const AuthorizationSet& hw_enforced,
165 const AuthorizationSet& sw_enforced,
166 UniquePtr<AsymmetricKey>* key) const {
167 keymaster_error_t error;
Shawn Willden0f906ec2015-06-20 09:16:30 -0600168 key->reset(new (std::nothrow) RsaKey(hw_enforced, sw_enforced, &error));
Shawn Willden398c1582015-05-28 00:04:06 -0600169 if (!key->get())
170 error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
171 return error;
172}
173
174} // namespace keymaster