blob: ce9a58c81f054523fab95504f1fe5cdbf3c87cfe [file] [log] [blame]
Shawn Willden1615f2e2014-08-13 10:37:40 -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
17#include <openssl/rsa.h>
18#include <openssl/evp.h>
19
20#include "rsa_operation.h"
21
22namespace keymaster {
23
24struct EVP_PKEY_Delete {
Shawn Willden802bb292014-08-18 10:46:29 -060025 void operator()(EVP_PKEY* p) const { EVP_PKEY_free(p); }
Shawn Willden1615f2e2014-08-13 10:37:40 -060026};
Shawn Willden802bb292014-08-18 10:46:29 -060027struct BIGNUM_Delete {
28 void operator()(BIGNUM* p) const { BN_free(p); }
29};
30
31struct RSA_Delete {
32 void operator()(RSA* p) const { RSA_free(p); }
33};
34
35/**
36 * Many OpenSSL APIs take ownership of an argument on success but don't free the argument on
37 * failure. This means we need to tell our scoped pointers when we've transferred ownership, without
38 * triggering a warning by not using the result of release().
39 */
40template <typename T, typename Delete_T>
41inline void release_because_ownership_transferred(UniquePtr<T, Delete_T>& p) {
42 T* val __attribute__((unused)) = p.release();
43}
44
45/* static */
46keymaster_error_t RsaOperation::Generate(uint64_t public_exponent, uint32_t key_size,
47 UniquePtr<uint8_t[]>* key_data, size_t* key_data_size) {
48 if (key_data == NULL || key_data_size == NULL)
49 return KM_ERROR_OUTPUT_PARAMETER_NULL;
50
51 UniquePtr<BIGNUM, BIGNUM_Delete> exponent(BN_new());
52 UniquePtr<RSA, RSA_Delete> rsa_key(RSA_new());
53 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> pkey(EVP_PKEY_new());
54 if (rsa_key.get() == NULL || pkey.get() == NULL)
55 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
56
57 if (!BN_set_word(exponent.get(), public_exponent) ||
58 !RSA_generate_key_ex(rsa_key.get(), key_size, exponent.get(), NULL /* callback */))
59 return KM_ERROR_UNKNOWN_ERROR;
60
61 if (!EVP_PKEY_assign_RSA(pkey.get(), rsa_key.get()))
62 return KM_ERROR_UNKNOWN_ERROR;
63 else
64 release_because_ownership_transferred(rsa_key);
65
66 *key_data_size = i2d_PrivateKey(pkey.get(), NULL);
67 if (*key_data_size <= 0)
68 return KM_ERROR_UNKNOWN_ERROR;
69
70 key_data->reset(new uint8_t[*key_data_size]);
71 if (key_data->get() == NULL)
72 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
73
74 uint8_t* tmp = key_data->get();
75 i2d_PrivateKey(pkey.get(), &tmp);
76
77 return KM_ERROR_OK;
78}
Shawn Willden1615f2e2014-08-13 10:37:40 -060079
80RsaOperation::RsaOperation(keymaster_purpose_t purpose, const KeyBlob& key)
81 : Operation(purpose), rsa_key_(NULL) {
82 assert(key.algorithm() == KM_ALGORITHM_RSA);
83
84 if ((!key.enforced().GetTagValue(TAG_DIGEST, &digest_) &&
85 !key.unenforced().GetTagValue(TAG_DIGEST, &digest_)) ||
86 digest_ != KM_DIGEST_NONE) {
87 error_ = KM_ERROR_UNSUPPORTED_DIGEST;
88 return;
89 }
90
91 if ((!key.enforced().GetTagValue(TAG_PADDING, &padding_) &&
92 !key.unenforced().GetTagValue(TAG_PADDING, &padding_)) ||
93 padding_ != KM_PAD_NONE) {
94 error_ = KM_ERROR_UNSUPPORTED_PADDING_MODE;
95 return;
96 }
97
98 UniquePtr<EVP_PKEY, EVP_PKEY_Delete> evp_key(EVP_PKEY_new());
99 if (evp_key.get() == NULL) {
100 error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED;
101 return;
102 }
103
104 EVP_PKEY* tmp_pkey = evp_key.get();
105 const uint8_t* key_material = key.key_material();
106 if (d2i_PrivateKey(EVP_PKEY_RSA, &tmp_pkey, &key_material, key.key_material_length()) == NULL) {
107 error_ = KM_ERROR_INVALID_KEY_BLOB;
108 return;
109 }
110
111 rsa_key_ = EVP_PKEY_get1_RSA(evp_key.get());
112 if (rsa_key_ == NULL) {
113 error_ = KM_ERROR_UNKNOWN_ERROR;
114 return;
115 }
116
117 // Since we're not using a digest function, we just need to store the text, up to the key
118 // size, until Finish is called, so we allocate a place to put it.
Shawn Willden43e999e2014-08-13 13:29:50 -0600119 if (!data_.Reinitialize(RSA_size(rsa_key_))) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600120 error_ = KM_ERROR_MEMORY_ALLOCATION_FAILED;
121 return;
122 }
123 error_ = KM_ERROR_OK;
124}
125
126RsaOperation::~RsaOperation() {
127 if (rsa_key_ != NULL)
128 RSA_free(rsa_key_);
129}
130
Shawn Willden43e999e2014-08-13 13:29:50 -0600131keymaster_error_t RsaOperation::Update(const Buffer& input, Buffer* /* output */) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600132 switch (purpose()) {
133 default:
134 return KM_ERROR_UNIMPLEMENTED;
135 case KM_PURPOSE_SIGN:
Shawn Willden43e999e2014-08-13 13:29:50 -0600136 case KM_PURPOSE_VERIFY:
137 return StoreData(input);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600138 }
139}
140
Shawn Willden43e999e2014-08-13 13:29:50 -0600141keymaster_error_t RsaOperation::StoreData(const Buffer& input) {
142 if (!data_.write(input.peek_read(), input.available_read()))
Shawn Willden1615f2e2014-08-13 10:37:40 -0600143 return KM_ERROR_INVALID_INPUT_LENGTH;
144 return KM_ERROR_OK;
145}
146
Shawn Willden43e999e2014-08-13 13:29:50 -0600147keymaster_error_t RsaOperation::Finish(const Buffer& signature, Buffer* output) {
Shawn Willden1615f2e2014-08-13 10:37:40 -0600148 switch (purpose()) {
149 case KM_PURPOSE_SIGN: {
Shawn Willden43e999e2014-08-13 13:29:50 -0600150 output->Reinitialize(RSA_size(rsa_key_));
151 if (data_.available_read() != output->buffer_size())
Shawn Willden1615f2e2014-08-13 10:37:40 -0600152 return KM_ERROR_INVALID_INPUT_LENGTH;
153
Shawn Willden43e999e2014-08-13 13:29:50 -0600154 int bytes_encrypted = RSA_private_encrypt(data_.available_read(), data_.peek_read(),
155 output->peek_write(), rsa_key_, RSA_NO_PADDING);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600156 if (bytes_encrypted < 0)
157 return KM_ERROR_UNKNOWN_ERROR;
158 assert(bytes_encrypted == RSA_size(rsa_key_));
Shawn Willden43e999e2014-08-13 13:29:50 -0600159 output->advance_write(bytes_encrypted);
Shawn Willden1615f2e2014-08-13 10:37:40 -0600160 return KM_ERROR_OK;
161 }
Shawn Willden43e999e2014-08-13 13:29:50 -0600162 case KM_PURPOSE_VERIFY: {
163 if ((int)data_.available_read() != RSA_size(rsa_key_))
164 return KM_ERROR_INVALID_INPUT_LENGTH;
165 if (data_.available_read() != signature.available_read())
166 return KM_ERROR_VERIFICATION_FAILED;
167
168 UniquePtr<uint8_t[]> decrypted_data(new uint8_t[RSA_size(rsa_key_)]);
169 int bytes_decrypted = RSA_public_decrypt(signature.available_read(), signature.peek_read(),
170 decrypted_data.get(), rsa_key_, RSA_NO_PADDING);
171 if (bytes_decrypted < 0)
172 return KM_ERROR_UNKNOWN_ERROR;
173 assert(bytes_decrypted == RSA_size(rsa_key_));
174
175 if (memcmp_s(decrypted_data.get(), data_.peek_read(), data_.available_read()) == 0)
176 return KM_ERROR_OK;
177 return KM_ERROR_VERIFICATION_FAILED;
178 }
Shawn Willden1615f2e2014-08-13 10:37:40 -0600179 default:
180 return KM_ERROR_UNIMPLEMENTED;
181 }
182}
183
184} // namespace keymaster