blob: 588152354c4289f4ed11c0b2729f84c163189eb6 [file] [log] [blame]
Adam Langley1fb05832014-09-23 17:42:36 -07001/* Copyright 2014 The Android Open Source Project
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
5 * are met:
6 * 1. Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 *
12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
13 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
16 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
22
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +010023#define LOG_TAG "keystore-engine"
Adam Langley1fb05832014-09-23 17:42:36 -070024
Paul Stewartac0ffbf2017-03-03 16:43:33 -080025#include <pthread.h>
Adam Langley1fb05832014-09-23 17:42:36 -070026#include <string.h>
Adam Langley1fb05832014-09-23 17:42:36 -070027
Logan Chiencdc813f2018-04-23 13:52:28 +080028#include <log/log.h>
Paul Stewartac0ffbf2017-03-03 16:43:33 -080029
Adam Langley1fb05832014-09-23 17:42:36 -070030#include <openssl/bn.h>
31#include <openssl/ec.h>
32#include <openssl/ec_key.h>
33#include <openssl/ecdsa.h>
34#include <openssl/engine.h>
35#include <openssl/evp.h>
36#include <openssl/rsa.h>
37#include <openssl/x509.h>
38
Janis Danisevskisccfff102017-05-01 11:02:51 -070039#include <memory>
40
Janis Danisevskis670122f2021-01-17 22:20:11 -080041#include "keystore2_engine.h"
42
Paul Stewart657356c2017-03-09 00:00:23 -080043#ifndef BACKEND_WIFI_HIDL
44#include "keystore_backend_binder.h"
45#else
46#include "keystore_backend_hidl.h"
47#endif
48
Adam Langley1fb05832014-09-23 17:42:36 -070049namespace {
Robert Sloan29f72ec2017-07-14 12:21:26 -070050KeystoreBackend *g_keystore_backend;
51void ensure_keystore_engine();
Adam Langley1fb05832014-09-23 17:42:36 -070052
53/* key_id_dup is called when one of the RSA or EC_KEY objects is duplicated. */
Kenny Rootdcca0512015-04-18 11:21:48 -070054int key_id_dup(CRYPTO_EX_DATA* /* to */,
55 const CRYPTO_EX_DATA* /* from */,
Adam Langley1fb05832014-09-23 17:42:36 -070056 void** from_d,
Kenny Rootdcca0512015-04-18 11:21:48 -070057 int /* index */,
58 long /* argl */,
59 void* /* argp */) {
Adam Langley1fb05832014-09-23 17:42:36 -070060 char *key_id = reinterpret_cast<char *>(*from_d);
Yi Kongd2916752018-07-26 17:44:27 -070061 if (key_id != nullptr) {
Adam Langley1fb05832014-09-23 17:42:36 -070062 *from_d = strdup(key_id);
63 }
64 return 1;
65}
66
67/* key_id_free is called when one of the RSA, DSA or EC_KEY object is freed. */
Kenny Rootdcca0512015-04-18 11:21:48 -070068void key_id_free(void* /* parent */,
Adam Langley1fb05832014-09-23 17:42:36 -070069 void* ptr,
Kenny Rootdcca0512015-04-18 11:21:48 -070070 CRYPTO_EX_DATA* /* ad */,
71 int /* index */,
72 long /* argl */,
73 void* /* argp */) {
Adam Langley1fb05832014-09-23 17:42:36 -070074 char *key_id = reinterpret_cast<char *>(ptr);
75 free(key_id);
76}
77
Adam Langley1fb05832014-09-23 17:42:36 -070078/* Many OpenSSL APIs take ownership of an argument on success but don't free
79 * the argument on failure. This means we need to tell our scoped pointers when
80 * we've transferred ownership, without triggering a warning by not using the
81 * result of release(). */
Rob Barnesbb6cabd2018-10-04 17:10:37 -060082#define OWNERSHIP_TRANSFERRED(obj) auto _dummy __attribute__((unused)) = (obj).release()
Adam Langley1fb05832014-09-23 17:42:36 -070083
Robert Sloan29f72ec2017-07-14 12:21:26 -070084const char* rsa_get_key_id(const RSA* rsa);
Adam Langley1fb05832014-09-23 17:42:36 -070085
86/* rsa_private_transform takes a big-endian integer from |in|, calculates the
87 * d'th power of it, modulo the RSA modulus, and writes the result as a
88 * big-endian integer to |out|. Both |in| and |out| are |len| bytes long. It
89 * returns one on success and zero otherwise. */
90int rsa_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in, size_t len) {
91 ALOGV("rsa_private_transform(%p, %p, %p, %u)", rsa, out, in, (unsigned) len);
92
Roshan Pius30b220e2017-03-31 16:47:04 -070093 ensure_keystore_engine();
94
Adam Langley1fb05832014-09-23 17:42:36 -070095 const char *key_id = rsa_get_key_id(rsa);
Yi Kongd2916752018-07-26 17:44:27 -070096 if (key_id == nullptr) {
Adam Langley1fb05832014-09-23 17:42:36 -070097 ALOGE("key had no key_id!");
98 return 0;
99 }
100
Yi Kongd2916752018-07-26 17:44:27 -0700101 uint8_t* reply = nullptr;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800102 size_t reply_len;
103 int32_t ret = g_keystore_backend->sign(key_id, in, len, &reply, &reply_len);
104 if (ret < 0) {
105 ALOGW("There was an error during rsa_decrypt: could not connect");
Adam Langley1fb05832014-09-23 17:42:36 -0700106 return 0;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800107 } else if (ret != 0) {
108 ALOGW("Error during sign from keystore: %d", ret);
Adam Langley1fb05832014-09-23 17:42:36 -0700109 return 0;
Yi Kongd2916752018-07-26 17:44:27 -0700110 } else if (reply_len == 0 || reply == nullptr) {
Adam Langley1fb05832014-09-23 17:42:36 -0700111 ALOGW("No valid signature returned");
Adam Langley1fb05832014-09-23 17:42:36 -0700112 return 0;
113 }
114
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800115 if (reply_len > len) {
Adam Langley1fb05832014-09-23 17:42:36 -0700116 /* The result of the RSA operation can never be larger than the size of
117 * the modulus so we assume that the result has extra zeros on the
118 * left. This provides attackers with an oracle, but there's nothing
119 * that we can do about it here. */
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800120 ALOGW("Reply len %zu greater than expected %zu", reply_len, len);
121 memcpy(out, &reply[reply_len - len], len);
122 } else if (reply_len < len) {
Adam Langley1fb05832014-09-23 17:42:36 -0700123 /* If the Keystore implementation returns a short value we assume that
124 * it's because it removed leading zeros from the left side. This is
125 * bad because it provides attackers with an oracle but we cannot do
126 * anything about a broken Keystore implementation here. */
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800127 ALOGW("Reply len %zu lesser than expected %zu", reply_len, len);
Adam Langley1fb05832014-09-23 17:42:36 -0700128 memset(out, 0, len);
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800129 memcpy(out + len - reply_len, &reply[0], reply_len);
Adam Langley1fb05832014-09-23 17:42:36 -0700130 } else {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100131 memcpy(out, &reply[0], len);
Adam Langley1fb05832014-09-23 17:42:36 -0700132 }
133
Adam Langley1fb05832014-09-23 17:42:36 -0700134 ALOGV("rsa=%p keystore_rsa_priv_dec successful", rsa);
135 return 1;
136}
137
Robert Sloan29f72ec2017-07-14 12:21:26 -0700138const char* ecdsa_get_key_id(const EC_KEY* ec_key);
Adam Langley1fb05832014-09-23 17:42:36 -0700139
140/* ecdsa_sign signs |digest_len| bytes from |digest| with |ec_key| and writes
141 * the resulting signature (an ASN.1 encoded blob) to |sig|. It returns one on
142 * success and zero otherwise. */
143static int ecdsa_sign(const uint8_t* digest, size_t digest_len, uint8_t* sig,
144 unsigned int* sig_len, EC_KEY* ec_key) {
145 ALOGV("ecdsa_sign(%p, %u, %p)", digest, (unsigned) digest_len, ec_key);
146
Roshan Pius30b220e2017-03-31 16:47:04 -0700147 ensure_keystore_engine();
148
Adam Langley1fb05832014-09-23 17:42:36 -0700149 const char *key_id = ecdsa_get_key_id(ec_key);
Yi Kongd2916752018-07-26 17:44:27 -0700150 if (key_id == nullptr) {
Adam Langley1fb05832014-09-23 17:42:36 -0700151 ALOGE("key had no key_id!");
152 return 0;
153 }
154
Adam Langley1fb05832014-09-23 17:42:36 -0700155 size_t ecdsa_size = ECDSA_size(ec_key);
156
Yi Kongd2916752018-07-26 17:44:27 -0700157 uint8_t* reply = nullptr;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800158 size_t reply_len;
159 int32_t ret = g_keystore_backend->sign(
160 key_id, digest, digest_len, &reply, &reply_len);
161 if (ret < 0) {
162 ALOGW("There was an error during ecdsa_sign: could not connect");
Adam Langley1fb05832014-09-23 17:42:36 -0700163 return 0;
Yi Kongd2916752018-07-26 17:44:27 -0700164 } else if (reply_len == 0 || reply == nullptr) {
Adam Langley1fb05832014-09-23 17:42:36 -0700165 ALOGW("No valid signature returned");
Adam Langley1fb05832014-09-23 17:42:36 -0700166 return 0;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800167 } else if (reply_len > ecdsa_size) {
Adam Langley1fb05832014-09-23 17:42:36 -0700168 ALOGW("Signature is too large");
Adam Langley1fb05832014-09-23 17:42:36 -0700169 return 0;
170 }
171
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100172 // Reviewer: should't sig_len be checked here? Or is it just assumed that it is at least ecdsa_size?
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800173 memcpy(sig, &reply[0], reply_len);
174 *sig_len = reply_len;
Adam Langley1fb05832014-09-23 17:42:36 -0700175
176 ALOGV("ecdsa_sign(%p, %u, %p) => success", digest, (unsigned)digest_len,
177 ec_key);
178 return 1;
179}
180
Robert Sloan29f72ec2017-07-14 12:21:26 -0700181/* KeystoreEngine is a BoringSSL ENGINE that implements RSA and ECDSA by
182 * forwarding the requested operations to Keystore. */
183class KeystoreEngine {
184 public:
185 KeystoreEngine()
186 : rsa_index_(RSA_get_ex_new_index(0 /* argl */,
Yi Kongd2916752018-07-26 17:44:27 -0700187 nullptr /* argp */,
188 nullptr /* new_func */,
Robert Sloan29f72ec2017-07-14 12:21:26 -0700189 key_id_dup,
190 key_id_free)),
191 ec_key_index_(EC_KEY_get_ex_new_index(0 /* argl */,
Yi Kongd2916752018-07-26 17:44:27 -0700192 nullptr /* argp */,
193 nullptr /* new_func */,
Robert Sloan29f72ec2017-07-14 12:21:26 -0700194 key_id_dup,
195 key_id_free)),
196 engine_(ENGINE_new()) {
197 memset(&rsa_method_, 0, sizeof(rsa_method_));
198 rsa_method_.common.is_static = 1;
199 rsa_method_.private_transform = rsa_private_transform;
David Benjamin48d2ea92017-12-15 18:35:49 -0500200 rsa_method_.flags = RSA_FLAG_OPAQUE;
Robert Sloan29f72ec2017-07-14 12:21:26 -0700201 ENGINE_set_RSA_method(engine_, &rsa_method_, sizeof(rsa_method_));
Adam Langley1fb05832014-09-23 17:42:36 -0700202
Robert Sloan29f72ec2017-07-14 12:21:26 -0700203 memset(&ecdsa_method_, 0, sizeof(ecdsa_method_));
204 ecdsa_method_.common.is_static = 1;
205 ecdsa_method_.sign = ecdsa_sign;
206 ecdsa_method_.flags = ECDSA_FLAG_OPAQUE;
207 ENGINE_set_ECDSA_method(engine_, &ecdsa_method_, sizeof(ecdsa_method_));
208 }
209
210 int rsa_ex_index() const { return rsa_index_; }
211 int ec_key_ex_index() const { return ec_key_index_; }
212
213 const ENGINE* engine() const { return engine_; }
214
215 private:
216 const int rsa_index_;
217 const int ec_key_index_;
218 RSA_METHOD rsa_method_;
219 ECDSA_METHOD ecdsa_method_;
220 ENGINE* const engine_;
Adam Langley1fb05832014-09-23 17:42:36 -0700221};
222
Robert Sloan29f72ec2017-07-14 12:21:26 -0700223pthread_once_t g_keystore_engine_once = PTHREAD_ONCE_INIT;
224KeystoreEngine *g_keystore_engine;
225
226/* init_keystore_engine is called to initialize |g_keystore_engine|. This
227 * should only be called by |pthread_once|. */
228void init_keystore_engine() {
229 g_keystore_engine = new KeystoreEngine;
230#ifndef BACKEND_WIFI_HIDL
231 g_keystore_backend = new KeystoreBackendBinder;
232#else
233 g_keystore_backend = new KeystoreBackendHidl;
234#endif
235}
236
237/* ensure_keystore_engine ensures that |g_keystore_engine| is pointing to a
238 * valid |KeystoreEngine| object and creates one if not. */
239void ensure_keystore_engine() {
240 pthread_once(&g_keystore_engine_once, init_keystore_engine);
241}
242
243const char* rsa_get_key_id(const RSA* rsa) {
244 return reinterpret_cast<char*>(
245 RSA_get_ex_data(rsa, g_keystore_engine->rsa_ex_index()));
246}
247
248const char* ecdsa_get_key_id(const EC_KEY* ec_key) {
249 return reinterpret_cast<char*>(
250 EC_KEY_get_ex_data(ec_key, g_keystore_engine->ec_key_ex_index()));
251}
252
Adam Langley1fb05832014-09-23 17:42:36 -0700253/* wrap_rsa returns an |EVP_PKEY| that contains an RSA key where the public
254 * part is taken from |public_rsa| and the private operations are forwarded to
255 * KeyStore and operate on the key named |key_id|. */
256static EVP_PKEY *wrap_rsa(const char *key_id, const RSA *public_rsa) {
David Benjamindc4d1422019-08-08 12:50:38 -0400257 bssl::UniquePtr<RSA> rsa(RSA_new_method(g_keystore_engine->engine()));
Yi Kongd2916752018-07-26 17:44:27 -0700258 if (rsa.get() == nullptr) {
259 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700260 }
261
262 char *key_id_copy = strdup(key_id);
Yi Kongd2916752018-07-26 17:44:27 -0700263 if (key_id_copy == nullptr) {
264 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700265 }
266
267 if (!RSA_set_ex_data(rsa.get(), g_keystore_engine->rsa_ex_index(),
268 key_id_copy)) {
269 free(key_id_copy);
Yi Kongd2916752018-07-26 17:44:27 -0700270 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700271 }
272
273 rsa->n = BN_dup(public_rsa->n);
274 rsa->e = BN_dup(public_rsa->e);
Yi Kongd2916752018-07-26 17:44:27 -0700275 if (rsa->n == nullptr || rsa->e == nullptr) {
276 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700277 }
278
David Benjamindc4d1422019-08-08 12:50:38 -0400279 bssl::UniquePtr<EVP_PKEY> result(EVP_PKEY_new());
Yi Kongd2916752018-07-26 17:44:27 -0700280 if (result.get() == nullptr ||
Adam Langley1fb05832014-09-23 17:42:36 -0700281 !EVP_PKEY_assign_RSA(result.get(), rsa.get())) {
Yi Kongd2916752018-07-26 17:44:27 -0700282 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700283 }
284 OWNERSHIP_TRANSFERRED(rsa);
285
286 return result.release();
287}
288
289/* wrap_ecdsa returns an |EVP_PKEY| that contains an ECDSA key where the public
290 * part is taken from |public_rsa| and the private operations are forwarded to
291 * KeyStore and operate on the key named |key_id|. */
292static EVP_PKEY *wrap_ecdsa(const char *key_id, const EC_KEY *public_ecdsa) {
David Benjamindc4d1422019-08-08 12:50:38 -0400293 bssl::UniquePtr<EC_KEY> ec(EC_KEY_new_method(g_keystore_engine->engine()));
Yi Kongd2916752018-07-26 17:44:27 -0700294 if (ec.get() == nullptr) {
295 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700296 }
297
298 if (!EC_KEY_set_group(ec.get(), EC_KEY_get0_group(public_ecdsa)) ||
299 !EC_KEY_set_public_key(ec.get(), EC_KEY_get0_public_key(public_ecdsa))) {
Yi Kongd2916752018-07-26 17:44:27 -0700300 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700301 }
302
303 char *key_id_copy = strdup(key_id);
Yi Kongd2916752018-07-26 17:44:27 -0700304 if (key_id_copy == nullptr) {
305 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700306 }
307
308 if (!EC_KEY_set_ex_data(ec.get(), g_keystore_engine->ec_key_ex_index(),
309 key_id_copy)) {
310 free(key_id_copy);
Yi Kongd2916752018-07-26 17:44:27 -0700311 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700312 }
313
David Benjamindc4d1422019-08-08 12:50:38 -0400314 bssl::UniquePtr<EVP_PKEY> result(EVP_PKEY_new());
Yi Kongd2916752018-07-26 17:44:27 -0700315 if (result.get() == nullptr ||
Adam Langley1fb05832014-09-23 17:42:36 -0700316 !EVP_PKEY_assign_EC_KEY(result.get(), ec.get())) {
Yi Kongd2916752018-07-26 17:44:27 -0700317 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700318 }
319 OWNERSHIP_TRANSFERRED(ec);
320
321 return result.release();
322}
323
324} /* anonymous namespace */
325
326extern "C" {
327
328EVP_PKEY* EVP_PKEY_from_keystore(const char* key_id) __attribute__((visibility("default")));
329
330/* EVP_PKEY_from_keystore returns an |EVP_PKEY| that contains either an RSA or
331 * ECDSA key where the public part of the key reflects the value of the key
332 * named |key_id| in Keystore and the private operations are forwarded onto
333 * KeyStore. */
334EVP_PKEY* EVP_PKEY_from_keystore(const char* key_id) {
335 ALOGV("EVP_PKEY_from_keystore(\"%s\")", key_id);
336
Janis Danisevskis670122f2021-01-17 22:20:11 -0800337 if (auto ks2_key = EVP_PKEY_from_keystore2(key_id)) {
338 return ks2_key;
339 }
340
Roshan Pius30b220e2017-03-31 16:47:04 -0700341 ensure_keystore_engine();
342
Yi Kongd2916752018-07-26 17:44:27 -0700343 uint8_t *pubkey = nullptr;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800344 size_t pubkey_len;
345 int32_t ret = g_keystore_backend->get_pubkey(key_id, &pubkey, &pubkey_len);
346 if (ret < 0) {
347 ALOGW("could not contact keystore");
Yi Kongd2916752018-07-26 17:44:27 -0700348 return nullptr;
349 } else if (ret != 0 || pubkey == nullptr) {
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800350 ALOGW("keystore reports error: %d", ret);
Yi Kongd2916752018-07-26 17:44:27 -0700351 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700352 }
353
Roshan Pius30b220e2017-03-31 16:47:04 -0700354 const uint8_t *inp = pubkey;
David Benjamindc4d1422019-08-08 12:50:38 -0400355 bssl::UniquePtr<EVP_PKEY> pkey(d2i_PUBKEY(nullptr, &inp, pubkey_len));
Yi Kongd2916752018-07-26 17:44:27 -0700356 if (pkey.get() == nullptr) {
Adam Langley1fb05832014-09-23 17:42:36 -0700357 ALOGW("Cannot convert pubkey");
Yi Kongd2916752018-07-26 17:44:27 -0700358 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700359 }
360
Adam Langley1fb05832014-09-23 17:42:36 -0700361 EVP_PKEY *result;
362 switch (EVP_PKEY_type(pkey->type)) {
363 case EVP_PKEY_RSA: {
David Benjamindc4d1422019-08-08 12:50:38 -0400364 bssl::UniquePtr<RSA> public_rsa(EVP_PKEY_get1_RSA(pkey.get()));
Adam Langley1fb05832014-09-23 17:42:36 -0700365 result = wrap_rsa(key_id, public_rsa.get());
366 break;
367 }
368 case EVP_PKEY_EC: {
David Benjamindc4d1422019-08-08 12:50:38 -0400369 bssl::UniquePtr<EC_KEY> public_ecdsa(EVP_PKEY_get1_EC_KEY(pkey.get()));
Adam Langley1fb05832014-09-23 17:42:36 -0700370 result = wrap_ecdsa(key_id, public_ecdsa.get());
371 break;
372 }
373 default:
374 ALOGE("Unsupported key type %d", EVP_PKEY_type(pkey->type));
Yi Kongd2916752018-07-26 17:44:27 -0700375 result = nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700376 }
377
378 return result;
379}
380
381} // extern "C"