blob: 684f69c8883585da5a1a25585a015c947db09c9e [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 <sys/socket.h>
27#include <stdarg.h>
28#include <string.h>
29#include <unistd.h>
30
Paul Stewartac0ffbf2017-03-03 16:43:33 -080031#include <cutils/log.h>
32
Adam Langley1fb05832014-09-23 17:42:36 -070033#include <openssl/bn.h>
34#include <openssl/ec.h>
35#include <openssl/ec_key.h>
36#include <openssl/ecdsa.h>
37#include <openssl/engine.h>
38#include <openssl/evp.h>
39#include <openssl/rsa.h>
40#include <openssl/x509.h>
41
Janis Danisevskisccfff102017-05-01 11:02:51 -070042#include <memory>
43
Paul Stewart657356c2017-03-09 00:00:23 -080044#ifndef BACKEND_WIFI_HIDL
45#include "keystore_backend_binder.h"
46#else
47#include "keystore_backend_hidl.h"
48#endif
49
Adam Langley1fb05832014-09-23 17:42:36 -070050namespace {
Robert Sloan29f72ec2017-07-14 12:21:26 -070051KeystoreBackend *g_keystore_backend;
52void ensure_keystore_engine();
Adam Langley1fb05832014-09-23 17:42:36 -070053
54/* key_id_dup is called when one of the RSA or EC_KEY objects is duplicated. */
Kenny Rootdcca0512015-04-18 11:21:48 -070055int key_id_dup(CRYPTO_EX_DATA* /* to */,
56 const CRYPTO_EX_DATA* /* from */,
Adam Langley1fb05832014-09-23 17:42:36 -070057 void** from_d,
Kenny Rootdcca0512015-04-18 11:21:48 -070058 int /* index */,
59 long /* argl */,
60 void* /* argp */) {
Adam Langley1fb05832014-09-23 17:42:36 -070061 char *key_id = reinterpret_cast<char *>(*from_d);
Yi Kongd2916752018-07-26 17:44:27 -070062 if (key_id != nullptr) {
Adam Langley1fb05832014-09-23 17:42:36 -070063 *from_d = strdup(key_id);
64 }
65 return 1;
66}
67
68/* key_id_free is called when one of the RSA, DSA or EC_KEY object is freed. */
Kenny Rootdcca0512015-04-18 11:21:48 -070069void key_id_free(void* /* parent */,
Adam Langley1fb05832014-09-23 17:42:36 -070070 void* ptr,
Kenny Rootdcca0512015-04-18 11:21:48 -070071 CRYPTO_EX_DATA* /* ad */,
72 int /* index */,
73 long /* argl */,
74 void* /* argp */) {
Adam Langley1fb05832014-09-23 17:42:36 -070075 char *key_id = reinterpret_cast<char *>(ptr);
76 free(key_id);
77}
78
Adam Langley1fb05832014-09-23 17:42:36 -070079/* Many OpenSSL APIs take ownership of an argument on success but don't free
80 * the argument on failure. This means we need to tell our scoped pointers when
81 * we've transferred ownership, without triggering a warning by not using the
82 * result of release(). */
83#define OWNERSHIP_TRANSFERRED(obj) \
Chih-Hung Hsieh26275ad2016-05-11 14:26:35 -070084 typeof ((obj).release()) _dummy __attribute__((unused)) = (obj).release()
Adam Langley1fb05832014-09-23 17:42:36 -070085
Robert Sloan29f72ec2017-07-14 12:21:26 -070086const char* rsa_get_key_id(const RSA* rsa);
Adam Langley1fb05832014-09-23 17:42:36 -070087
88/* rsa_private_transform takes a big-endian integer from |in|, calculates the
89 * d'th power of it, modulo the RSA modulus, and writes the result as a
90 * big-endian integer to |out|. Both |in| and |out| are |len| bytes long. It
91 * returns one on success and zero otherwise. */
92int rsa_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in, size_t len) {
93 ALOGV("rsa_private_transform(%p, %p, %p, %u)", rsa, out, in, (unsigned) len);
94
Roshan Pius30b220e2017-03-31 16:47:04 -070095 ensure_keystore_engine();
96
Adam Langley1fb05832014-09-23 17:42:36 -070097 const char *key_id = rsa_get_key_id(rsa);
Yi Kongd2916752018-07-26 17:44:27 -070098 if (key_id == nullptr) {
Adam Langley1fb05832014-09-23 17:42:36 -070099 ALOGE("key had no key_id!");
100 return 0;
101 }
102
Yi Kongd2916752018-07-26 17:44:27 -0700103 uint8_t* reply = nullptr;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800104 size_t reply_len;
105 int32_t ret = g_keystore_backend->sign(key_id, in, len, &reply, &reply_len);
106 if (ret < 0) {
107 ALOGW("There was an error during rsa_decrypt: could not connect");
Adam Langley1fb05832014-09-23 17:42:36 -0700108 return 0;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800109 } else if (ret != 0) {
110 ALOGW("Error during sign from keystore: %d", ret);
Adam Langley1fb05832014-09-23 17:42:36 -0700111 return 0;
Yi Kongd2916752018-07-26 17:44:27 -0700112 } else if (reply_len == 0 || reply == nullptr) {
Adam Langley1fb05832014-09-23 17:42:36 -0700113 ALOGW("No valid signature returned");
Adam Langley1fb05832014-09-23 17:42:36 -0700114 return 0;
115 }
116
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800117 if (reply_len > len) {
Adam Langley1fb05832014-09-23 17:42:36 -0700118 /* The result of the RSA operation can never be larger than the size of
119 * the modulus so we assume that the result has extra zeros on the
120 * left. This provides attackers with an oracle, but there's nothing
121 * that we can do about it here. */
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800122 ALOGW("Reply len %zu greater than expected %zu", reply_len, len);
123 memcpy(out, &reply[reply_len - len], len);
124 } else if (reply_len < len) {
Adam Langley1fb05832014-09-23 17:42:36 -0700125 /* If the Keystore implementation returns a short value we assume that
126 * it's because it removed leading zeros from the left side. This is
127 * bad because it provides attackers with an oracle but we cannot do
128 * anything about a broken Keystore implementation here. */
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800129 ALOGW("Reply len %zu lesser than expected %zu", reply_len, len);
Adam Langley1fb05832014-09-23 17:42:36 -0700130 memset(out, 0, len);
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800131 memcpy(out + len - reply_len, &reply[0], reply_len);
Adam Langley1fb05832014-09-23 17:42:36 -0700132 } else {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100133 memcpy(out, &reply[0], len);
Adam Langley1fb05832014-09-23 17:42:36 -0700134 }
135
Adam Langley1fb05832014-09-23 17:42:36 -0700136 ALOGV("rsa=%p keystore_rsa_priv_dec successful", rsa);
137 return 1;
138}
139
Robert Sloan29f72ec2017-07-14 12:21:26 -0700140const char* ecdsa_get_key_id(const EC_KEY* ec_key);
Adam Langley1fb05832014-09-23 17:42:36 -0700141
142/* ecdsa_sign signs |digest_len| bytes from |digest| with |ec_key| and writes
143 * the resulting signature (an ASN.1 encoded blob) to |sig|. It returns one on
144 * success and zero otherwise. */
145static int ecdsa_sign(const uint8_t* digest, size_t digest_len, uint8_t* sig,
146 unsigned int* sig_len, EC_KEY* ec_key) {
147 ALOGV("ecdsa_sign(%p, %u, %p)", digest, (unsigned) digest_len, ec_key);
148
Roshan Pius30b220e2017-03-31 16:47:04 -0700149 ensure_keystore_engine();
150
Adam Langley1fb05832014-09-23 17:42:36 -0700151 const char *key_id = ecdsa_get_key_id(ec_key);
Yi Kongd2916752018-07-26 17:44:27 -0700152 if (key_id == nullptr) {
Adam Langley1fb05832014-09-23 17:42:36 -0700153 ALOGE("key had no key_id!");
154 return 0;
155 }
156
Adam Langley1fb05832014-09-23 17:42:36 -0700157 size_t ecdsa_size = ECDSA_size(ec_key);
158
Yi Kongd2916752018-07-26 17:44:27 -0700159 uint8_t* reply = nullptr;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800160 size_t reply_len;
161 int32_t ret = g_keystore_backend->sign(
162 key_id, digest, digest_len, &reply, &reply_len);
163 if (ret < 0) {
164 ALOGW("There was an error during ecdsa_sign: could not connect");
Adam Langley1fb05832014-09-23 17:42:36 -0700165 return 0;
Yi Kongd2916752018-07-26 17:44:27 -0700166 } else if (reply_len == 0 || reply == nullptr) {
Adam Langley1fb05832014-09-23 17:42:36 -0700167 ALOGW("No valid signature returned");
Adam Langley1fb05832014-09-23 17:42:36 -0700168 return 0;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800169 } else if (reply_len > ecdsa_size) {
Adam Langley1fb05832014-09-23 17:42:36 -0700170 ALOGW("Signature is too large");
Adam Langley1fb05832014-09-23 17:42:36 -0700171 return 0;
172 }
173
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100174 // 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 -0800175 memcpy(sig, &reply[0], reply_len);
176 *sig_len = reply_len;
Adam Langley1fb05832014-09-23 17:42:36 -0700177
178 ALOGV("ecdsa_sign(%p, %u, %p) => success", digest, (unsigned)digest_len,
179 ec_key);
180 return 1;
181}
182
Robert Sloan29f72ec2017-07-14 12:21:26 -0700183/* KeystoreEngine is a BoringSSL ENGINE that implements RSA and ECDSA by
184 * forwarding the requested operations to Keystore. */
185class KeystoreEngine {
186 public:
187 KeystoreEngine()
188 : rsa_index_(RSA_get_ex_new_index(0 /* argl */,
Yi Kongd2916752018-07-26 17:44:27 -0700189 nullptr /* argp */,
190 nullptr /* new_func */,
Robert Sloan29f72ec2017-07-14 12:21:26 -0700191 key_id_dup,
192 key_id_free)),
193 ec_key_index_(EC_KEY_get_ex_new_index(0 /* argl */,
Yi Kongd2916752018-07-26 17:44:27 -0700194 nullptr /* argp */,
195 nullptr /* new_func */,
Robert Sloan29f72ec2017-07-14 12:21:26 -0700196 key_id_dup,
197 key_id_free)),
198 engine_(ENGINE_new()) {
199 memset(&rsa_method_, 0, sizeof(rsa_method_));
200 rsa_method_.common.is_static = 1;
201 rsa_method_.private_transform = rsa_private_transform;
David Benjamin48d2ea92017-12-15 18:35:49 -0500202 rsa_method_.flags = RSA_FLAG_OPAQUE;
Robert Sloan29f72ec2017-07-14 12:21:26 -0700203 ENGINE_set_RSA_method(engine_, &rsa_method_, sizeof(rsa_method_));
Adam Langley1fb05832014-09-23 17:42:36 -0700204
Robert Sloan29f72ec2017-07-14 12:21:26 -0700205 memset(&ecdsa_method_, 0, sizeof(ecdsa_method_));
206 ecdsa_method_.common.is_static = 1;
207 ecdsa_method_.sign = ecdsa_sign;
208 ecdsa_method_.flags = ECDSA_FLAG_OPAQUE;
209 ENGINE_set_ECDSA_method(engine_, &ecdsa_method_, sizeof(ecdsa_method_));
210 }
211
212 int rsa_ex_index() const { return rsa_index_; }
213 int ec_key_ex_index() const { return ec_key_index_; }
214
215 const ENGINE* engine() const { return engine_; }
216
217 private:
218 const int rsa_index_;
219 const int ec_key_index_;
220 RSA_METHOD rsa_method_;
221 ECDSA_METHOD ecdsa_method_;
222 ENGINE* const engine_;
Adam Langley1fb05832014-09-23 17:42:36 -0700223};
224
Robert Sloan29f72ec2017-07-14 12:21:26 -0700225pthread_once_t g_keystore_engine_once = PTHREAD_ONCE_INIT;
226KeystoreEngine *g_keystore_engine;
227
228/* init_keystore_engine is called to initialize |g_keystore_engine|. This
229 * should only be called by |pthread_once|. */
230void init_keystore_engine() {
231 g_keystore_engine = new KeystoreEngine;
232#ifndef BACKEND_WIFI_HIDL
233 g_keystore_backend = new KeystoreBackendBinder;
234#else
235 g_keystore_backend = new KeystoreBackendHidl;
236#endif
237}
238
239/* ensure_keystore_engine ensures that |g_keystore_engine| is pointing to a
240 * valid |KeystoreEngine| object and creates one if not. */
241void ensure_keystore_engine() {
242 pthread_once(&g_keystore_engine_once, init_keystore_engine);
243}
244
245const char* rsa_get_key_id(const RSA* rsa) {
246 return reinterpret_cast<char*>(
247 RSA_get_ex_data(rsa, g_keystore_engine->rsa_ex_index()));
248}
249
250const char* ecdsa_get_key_id(const EC_KEY* ec_key) {
251 return reinterpret_cast<char*>(
252 EC_KEY_get_ex_data(ec_key, g_keystore_engine->ec_key_ex_index()));
253}
254
Adam Langley1fb05832014-09-23 17:42:36 -0700255struct EVP_PKEY_Delete {
256 void operator()(EVP_PKEY* p) const {
257 EVP_PKEY_free(p);
258 }
259};
Janis Danisevskisccfff102017-05-01 11:02:51 -0700260typedef std::unique_ptr<EVP_PKEY, EVP_PKEY_Delete> Unique_EVP_PKEY;
Adam Langley1fb05832014-09-23 17:42:36 -0700261
262struct RSA_Delete {
263 void operator()(RSA* p) const {
264 RSA_free(p);
265 }
266};
Janis Danisevskisccfff102017-05-01 11:02:51 -0700267typedef std::unique_ptr<RSA, RSA_Delete> Unique_RSA;
Adam Langley1fb05832014-09-23 17:42:36 -0700268
269struct EC_KEY_Delete {
270 void operator()(EC_KEY* ec) const {
271 EC_KEY_free(ec);
272 }
273};
Janis Danisevskisccfff102017-05-01 11:02:51 -0700274typedef std::unique_ptr<EC_KEY, EC_KEY_Delete> Unique_EC_KEY;
Adam Langley1fb05832014-09-23 17:42:36 -0700275
276/* wrap_rsa returns an |EVP_PKEY| that contains an RSA key where the public
277 * part is taken from |public_rsa| and the private operations are forwarded to
278 * KeyStore and operate on the key named |key_id|. */
279static EVP_PKEY *wrap_rsa(const char *key_id, const RSA *public_rsa) {
280 Unique_RSA rsa(RSA_new_method(g_keystore_engine->engine()));
Yi Kongd2916752018-07-26 17:44:27 -0700281 if (rsa.get() == nullptr) {
282 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700283 }
284
285 char *key_id_copy = strdup(key_id);
Yi Kongd2916752018-07-26 17:44:27 -0700286 if (key_id_copy == nullptr) {
287 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700288 }
289
290 if (!RSA_set_ex_data(rsa.get(), g_keystore_engine->rsa_ex_index(),
291 key_id_copy)) {
292 free(key_id_copy);
Yi Kongd2916752018-07-26 17:44:27 -0700293 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700294 }
295
296 rsa->n = BN_dup(public_rsa->n);
297 rsa->e = BN_dup(public_rsa->e);
Yi Kongd2916752018-07-26 17:44:27 -0700298 if (rsa->n == nullptr || rsa->e == nullptr) {
299 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700300 }
301
302 Unique_EVP_PKEY result(EVP_PKEY_new());
Yi Kongd2916752018-07-26 17:44:27 -0700303 if (result.get() == nullptr ||
Adam Langley1fb05832014-09-23 17:42:36 -0700304 !EVP_PKEY_assign_RSA(result.get(), rsa.get())) {
Yi Kongd2916752018-07-26 17:44:27 -0700305 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700306 }
307 OWNERSHIP_TRANSFERRED(rsa);
308
309 return result.release();
310}
311
312/* wrap_ecdsa returns an |EVP_PKEY| that contains an ECDSA key where the public
313 * part is taken from |public_rsa| and the private operations are forwarded to
314 * KeyStore and operate on the key named |key_id|. */
315static EVP_PKEY *wrap_ecdsa(const char *key_id, const EC_KEY *public_ecdsa) {
316 Unique_EC_KEY ec(EC_KEY_new_method(g_keystore_engine->engine()));
Yi Kongd2916752018-07-26 17:44:27 -0700317 if (ec.get() == nullptr) {
318 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700319 }
320
321 if (!EC_KEY_set_group(ec.get(), EC_KEY_get0_group(public_ecdsa)) ||
322 !EC_KEY_set_public_key(ec.get(), EC_KEY_get0_public_key(public_ecdsa))) {
Yi Kongd2916752018-07-26 17:44:27 -0700323 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700324 }
325
326 char *key_id_copy = strdup(key_id);
Yi Kongd2916752018-07-26 17:44:27 -0700327 if (key_id_copy == nullptr) {
328 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700329 }
330
331 if (!EC_KEY_set_ex_data(ec.get(), g_keystore_engine->ec_key_ex_index(),
332 key_id_copy)) {
333 free(key_id_copy);
Yi Kongd2916752018-07-26 17:44:27 -0700334 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700335 }
336
337 Unique_EVP_PKEY result(EVP_PKEY_new());
Yi Kongd2916752018-07-26 17:44:27 -0700338 if (result.get() == nullptr ||
Adam Langley1fb05832014-09-23 17:42:36 -0700339 !EVP_PKEY_assign_EC_KEY(result.get(), ec.get())) {
Yi Kongd2916752018-07-26 17:44:27 -0700340 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700341 }
342 OWNERSHIP_TRANSFERRED(ec);
343
344 return result.release();
345}
346
347} /* anonymous namespace */
348
349extern "C" {
350
351EVP_PKEY* EVP_PKEY_from_keystore(const char* key_id) __attribute__((visibility("default")));
352
353/* EVP_PKEY_from_keystore returns an |EVP_PKEY| that contains either an RSA or
354 * ECDSA key where the public part of the key reflects the value of the key
355 * named |key_id| in Keystore and the private operations are forwarded onto
356 * KeyStore. */
357EVP_PKEY* EVP_PKEY_from_keystore(const char* key_id) {
358 ALOGV("EVP_PKEY_from_keystore(\"%s\")", key_id);
359
Roshan Pius30b220e2017-03-31 16:47:04 -0700360 ensure_keystore_engine();
361
Yi Kongd2916752018-07-26 17:44:27 -0700362 uint8_t *pubkey = nullptr;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800363 size_t pubkey_len;
364 int32_t ret = g_keystore_backend->get_pubkey(key_id, &pubkey, &pubkey_len);
365 if (ret < 0) {
366 ALOGW("could not contact keystore");
Yi Kongd2916752018-07-26 17:44:27 -0700367 return nullptr;
368 } else if (ret != 0 || pubkey == nullptr) {
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800369 ALOGW("keystore reports error: %d", ret);
Yi Kongd2916752018-07-26 17:44:27 -0700370 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700371 }
372
Roshan Pius30b220e2017-03-31 16:47:04 -0700373 const uint8_t *inp = pubkey;
Yi Kongd2916752018-07-26 17:44:27 -0700374 Unique_EVP_PKEY pkey(d2i_PUBKEY(nullptr, &inp, pubkey_len));
375 if (pkey.get() == nullptr) {
Adam Langley1fb05832014-09-23 17:42:36 -0700376 ALOGW("Cannot convert pubkey");
Yi Kongd2916752018-07-26 17:44:27 -0700377 return nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700378 }
379
Adam Langley1fb05832014-09-23 17:42:36 -0700380 EVP_PKEY *result;
381 switch (EVP_PKEY_type(pkey->type)) {
382 case EVP_PKEY_RSA: {
383 Unique_RSA public_rsa(EVP_PKEY_get1_RSA(pkey.get()));
384 result = wrap_rsa(key_id, public_rsa.get());
385 break;
386 }
387 case EVP_PKEY_EC: {
388 Unique_EC_KEY public_ecdsa(EVP_PKEY_get1_EC_KEY(pkey.get()));
389 result = wrap_ecdsa(key_id, public_ecdsa.get());
390 break;
391 }
392 default:
393 ALOGE("Unsupported key type %d", EVP_PKEY_type(pkey->type));
Yi Kongd2916752018-07-26 17:44:27 -0700394 result = nullptr;
Adam Langley1fb05832014-09-23 17:42:36 -0700395 }
396
397 return result;
398}
399
400} // extern "C"