blob: b7b81992298ff18a80a71357302008ef555fbe6e [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 {
Adam Langley1fb05832014-09-23 17:42:36 -070051extern const RSA_METHOD keystore_rsa_method;
52extern const ECDSA_METHOD keystore_ecdsa_method;
53
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);
62 if (key_id != NULL) {
63 *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
79/* KeystoreEngine is a BoringSSL ENGINE that implements RSA and ECDSA by
80 * forwarding the requested operations to Keystore. */
81class KeystoreEngine {
82 public:
83 KeystoreEngine()
84 : rsa_index_(RSA_get_ex_new_index(0 /* argl */,
85 NULL /* argp */,
86 NULL /* new_func */,
87 key_id_dup,
88 key_id_free)),
89 ec_key_index_(EC_KEY_get_ex_new_index(0 /* argl */,
90 NULL /* argp */,
91 NULL /* new_func */,
92 key_id_dup,
93 key_id_free)),
94 engine_(ENGINE_new()) {
95 ENGINE_set_RSA_method(
96 engine_, &keystore_rsa_method, sizeof(keystore_rsa_method));
97 ENGINE_set_ECDSA_method(
98 engine_, &keystore_ecdsa_method, sizeof(keystore_ecdsa_method));
99 }
100
101 int rsa_ex_index() const { return rsa_index_; }
102 int ec_key_ex_index() const { return ec_key_index_; }
103
104 const ENGINE* engine() const { return engine_; }
105
106 private:
107 const int rsa_index_;
108 const int ec_key_index_;
109 ENGINE* const engine_;
110};
111
112pthread_once_t g_keystore_engine_once = PTHREAD_ONCE_INIT;
113KeystoreEngine *g_keystore_engine;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800114KeystoreBackend *g_keystore_backend;
Adam Langley1fb05832014-09-23 17:42:36 -0700115
116/* init_keystore_engine is called to initialize |g_keystore_engine|. This
117 * should only be called by |pthread_once|. */
118void init_keystore_engine() {
119 g_keystore_engine = new KeystoreEngine;
Paul Stewart657356c2017-03-09 00:00:23 -0800120#ifndef BACKEND_WIFI_HIDL
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800121 g_keystore_backend = new KeystoreBackendBinder;
Paul Stewart657356c2017-03-09 00:00:23 -0800122#else
123 g_keystore_backend = new KeystoreBackendHidl;
124#endif
Adam Langley1fb05832014-09-23 17:42:36 -0700125}
126
127/* ensure_keystore_engine ensures that |g_keystore_engine| is pointing to a
128 * valid |KeystoreEngine| object and creates one if not. */
129void ensure_keystore_engine() {
130 pthread_once(&g_keystore_engine_once, init_keystore_engine);
131}
132
133/* Many OpenSSL APIs take ownership of an argument on success but don't free
134 * the argument on failure. This means we need to tell our scoped pointers when
135 * we've transferred ownership, without triggering a warning by not using the
136 * result of release(). */
137#define OWNERSHIP_TRANSFERRED(obj) \
Chih-Hung Hsieh26275ad2016-05-11 14:26:35 -0700138 typeof ((obj).release()) _dummy __attribute__((unused)) = (obj).release()
Adam Langley1fb05832014-09-23 17:42:36 -0700139
140const char* rsa_get_key_id(const RSA* rsa) {
141 return reinterpret_cast<char*>(
142 RSA_get_ex_data(rsa, g_keystore_engine->rsa_ex_index()));
143}
144
145/* rsa_private_transform takes a big-endian integer from |in|, calculates the
146 * d'th power of it, modulo the RSA modulus, and writes the result as a
147 * big-endian integer to |out|. Both |in| and |out| are |len| bytes long. It
148 * returns one on success and zero otherwise. */
149int rsa_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in, size_t len) {
150 ALOGV("rsa_private_transform(%p, %p, %p, %u)", rsa, out, in, (unsigned) len);
151
Roshan Pius30b220e2017-03-31 16:47:04 -0700152 ensure_keystore_engine();
153
Adam Langley1fb05832014-09-23 17:42:36 -0700154 const char *key_id = rsa_get_key_id(rsa);
155 if (key_id == NULL) {
156 ALOGE("key had no key_id!");
157 return 0;
158 }
159
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800160 uint8_t* reply = NULL;
161 size_t reply_len;
162 int32_t ret = g_keystore_backend->sign(key_id, in, len, &reply, &reply_len);
163 if (ret < 0) {
164 ALOGW("There was an error during rsa_decrypt: could not connect");
Adam Langley1fb05832014-09-23 17:42:36 -0700165 return 0;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800166 } else if (ret != 0) {
167 ALOGW("Error during sign from keystore: %d", ret);
Adam Langley1fb05832014-09-23 17:42:36 -0700168 return 0;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800169 } else if (reply_len == 0 || reply == NULL) {
Adam Langley1fb05832014-09-23 17:42:36 -0700170 ALOGW("No valid signature returned");
Adam Langley1fb05832014-09-23 17:42:36 -0700171 return 0;
172 }
173
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800174 if (reply_len > len) {
Adam Langley1fb05832014-09-23 17:42:36 -0700175 /* The result of the RSA operation can never be larger than the size of
176 * the modulus so we assume that the result has extra zeros on the
177 * left. This provides attackers with an oracle, but there's nothing
178 * that we can do about it here. */
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800179 ALOGW("Reply len %zu greater than expected %zu", reply_len, len);
180 memcpy(out, &reply[reply_len - len], len);
181 } else if (reply_len < len) {
Adam Langley1fb05832014-09-23 17:42:36 -0700182 /* If the Keystore implementation returns a short value we assume that
183 * it's because it removed leading zeros from the left side. This is
184 * bad because it provides attackers with an oracle but we cannot do
185 * anything about a broken Keystore implementation here. */
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800186 ALOGW("Reply len %zu lesser than expected %zu", reply_len, len);
Adam Langley1fb05832014-09-23 17:42:36 -0700187 memset(out, 0, len);
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800188 memcpy(out + len - reply_len, &reply[0], reply_len);
Adam Langley1fb05832014-09-23 17:42:36 -0700189 } else {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100190 memcpy(out, &reply[0], len);
Adam Langley1fb05832014-09-23 17:42:36 -0700191 }
192
Adam Langley1fb05832014-09-23 17:42:36 -0700193 ALOGV("rsa=%p keystore_rsa_priv_dec successful", rsa);
194 return 1;
195}
196
197const struct rsa_meth_st keystore_rsa_method = {
198 {
199 0 /* references */,
200 1 /* is_static */,
201 },
202 NULL /* app_data */,
203
204 NULL /* init */,
205 NULL /* finish */,
206
207 NULL /* size */,
208
209 NULL /* sign */,
210 NULL /* verify */,
211
212 NULL /* encrypt */,
213 NULL /* sign_raw */,
214 NULL /* decrypt */,
215 NULL /* verify_raw */,
216
217 rsa_private_transform,
218
219 NULL /* mod_exp */,
220 NULL /* bn_mod_exp */,
221
David Benjamin30c77522016-03-28 18:00:17 -0400222 RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_OPAQUE,
Adam Langley1fb05832014-09-23 17:42:36 -0700223
224 NULL /* keygen */,
Adam Langley9eb92952015-09-02 15:28:03 -0700225 NULL /* multi_prime_keygen */,
Adam Langleyb2747fe2014-12-11 17:19:31 -0800226 NULL /* supports_digest */,
Adam Langley1fb05832014-09-23 17:42:36 -0700227};
228
229const char* ecdsa_get_key_id(const EC_KEY* ec_key) {
230 return reinterpret_cast<char*>(
231 EC_KEY_get_ex_data(ec_key, g_keystore_engine->ec_key_ex_index()));
232}
233
234/* ecdsa_sign signs |digest_len| bytes from |digest| with |ec_key| and writes
235 * the resulting signature (an ASN.1 encoded blob) to |sig|. It returns one on
236 * success and zero otherwise. */
237static int ecdsa_sign(const uint8_t* digest, size_t digest_len, uint8_t* sig,
238 unsigned int* sig_len, EC_KEY* ec_key) {
239 ALOGV("ecdsa_sign(%p, %u, %p)", digest, (unsigned) digest_len, ec_key);
240
Roshan Pius30b220e2017-03-31 16:47:04 -0700241 ensure_keystore_engine();
242
Adam Langley1fb05832014-09-23 17:42:36 -0700243 const char *key_id = ecdsa_get_key_id(ec_key);
244 if (key_id == NULL) {
245 ALOGE("key had no key_id!");
246 return 0;
247 }
248
Adam Langley1fb05832014-09-23 17:42:36 -0700249 size_t ecdsa_size = ECDSA_size(ec_key);
250
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800251 uint8_t* reply = NULL;
252 size_t reply_len;
253 int32_t ret = g_keystore_backend->sign(
254 key_id, digest, digest_len, &reply, &reply_len);
255 if (ret < 0) {
256 ALOGW("There was an error during ecdsa_sign: could not connect");
Adam Langley1fb05832014-09-23 17:42:36 -0700257 return 0;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800258 } else if (reply_len == 0 || reply == NULL) {
Adam Langley1fb05832014-09-23 17:42:36 -0700259 ALOGW("No valid signature returned");
Adam Langley1fb05832014-09-23 17:42:36 -0700260 return 0;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800261 } else if (reply_len > ecdsa_size) {
Adam Langley1fb05832014-09-23 17:42:36 -0700262 ALOGW("Signature is too large");
Adam Langley1fb05832014-09-23 17:42:36 -0700263 return 0;
264 }
265
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100266 // 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 -0800267 memcpy(sig, &reply[0], reply_len);
268 *sig_len = reply_len;
Adam Langley1fb05832014-09-23 17:42:36 -0700269
270 ALOGV("ecdsa_sign(%p, %u, %p) => success", digest, (unsigned)digest_len,
271 ec_key);
272 return 1;
273}
274
275const ECDSA_METHOD keystore_ecdsa_method = {
276 {
277 0 /* references */,
278 1 /* is_static */
279 } /* common */,
280 NULL /* app_data */,
281
282 NULL /* init */,
283 NULL /* finish */,
284 NULL /* group_order_size */,
285 ecdsa_sign,
286 NULL /* verify */,
287 ECDSA_FLAG_OPAQUE,
288};
289
290struct EVP_PKEY_Delete {
291 void operator()(EVP_PKEY* p) const {
292 EVP_PKEY_free(p);
293 }
294};
Janis Danisevskisccfff102017-05-01 11:02:51 -0700295typedef std::unique_ptr<EVP_PKEY, EVP_PKEY_Delete> Unique_EVP_PKEY;
Adam Langley1fb05832014-09-23 17:42:36 -0700296
297struct RSA_Delete {
298 void operator()(RSA* p) const {
299 RSA_free(p);
300 }
301};
Janis Danisevskisccfff102017-05-01 11:02:51 -0700302typedef std::unique_ptr<RSA, RSA_Delete> Unique_RSA;
Adam Langley1fb05832014-09-23 17:42:36 -0700303
304struct EC_KEY_Delete {
305 void operator()(EC_KEY* ec) const {
306 EC_KEY_free(ec);
307 }
308};
Janis Danisevskisccfff102017-05-01 11:02:51 -0700309typedef std::unique_ptr<EC_KEY, EC_KEY_Delete> Unique_EC_KEY;
Adam Langley1fb05832014-09-23 17:42:36 -0700310
311/* wrap_rsa returns an |EVP_PKEY| that contains an RSA key where the public
312 * part is taken from |public_rsa| and the private operations are forwarded to
313 * KeyStore and operate on the key named |key_id|. */
314static EVP_PKEY *wrap_rsa(const char *key_id, const RSA *public_rsa) {
315 Unique_RSA rsa(RSA_new_method(g_keystore_engine->engine()));
316 if (rsa.get() == NULL) {
317 return NULL;
318 }
319
320 char *key_id_copy = strdup(key_id);
321 if (key_id_copy == NULL) {
322 return NULL;
323 }
324
325 if (!RSA_set_ex_data(rsa.get(), g_keystore_engine->rsa_ex_index(),
326 key_id_copy)) {
327 free(key_id_copy);
328 return NULL;
329 }
330
331 rsa->n = BN_dup(public_rsa->n);
332 rsa->e = BN_dup(public_rsa->e);
333 if (rsa->n == NULL || rsa->e == NULL) {
334 return NULL;
335 }
336
337 Unique_EVP_PKEY result(EVP_PKEY_new());
338 if (result.get() == NULL ||
339 !EVP_PKEY_assign_RSA(result.get(), rsa.get())) {
340 return NULL;
341 }
342 OWNERSHIP_TRANSFERRED(rsa);
343
344 return result.release();
345}
346
347/* wrap_ecdsa returns an |EVP_PKEY| that contains an ECDSA key where the public
348 * part is taken from |public_rsa| and the private operations are forwarded to
349 * KeyStore and operate on the key named |key_id|. */
350static EVP_PKEY *wrap_ecdsa(const char *key_id, const EC_KEY *public_ecdsa) {
351 Unique_EC_KEY ec(EC_KEY_new_method(g_keystore_engine->engine()));
352 if (ec.get() == NULL) {
353 return NULL;
354 }
355
356 if (!EC_KEY_set_group(ec.get(), EC_KEY_get0_group(public_ecdsa)) ||
357 !EC_KEY_set_public_key(ec.get(), EC_KEY_get0_public_key(public_ecdsa))) {
358 return NULL;
359 }
360
361 char *key_id_copy = strdup(key_id);
362 if (key_id_copy == NULL) {
363 return NULL;
364 }
365
366 if (!EC_KEY_set_ex_data(ec.get(), g_keystore_engine->ec_key_ex_index(),
367 key_id_copy)) {
368 free(key_id_copy);
369 return NULL;
370 }
371
372 Unique_EVP_PKEY result(EVP_PKEY_new());
373 if (result.get() == NULL ||
374 !EVP_PKEY_assign_EC_KEY(result.get(), ec.get())) {
375 return NULL;
376 }
377 OWNERSHIP_TRANSFERRED(ec);
378
379 return result.release();
380}
381
382} /* anonymous namespace */
383
384extern "C" {
385
386EVP_PKEY* EVP_PKEY_from_keystore(const char* key_id) __attribute__((visibility("default")));
387
388/* EVP_PKEY_from_keystore returns an |EVP_PKEY| that contains either an RSA or
389 * ECDSA key where the public part of the key reflects the value of the key
390 * named |key_id| in Keystore and the private operations are forwarded onto
391 * KeyStore. */
392EVP_PKEY* EVP_PKEY_from_keystore(const char* key_id) {
393 ALOGV("EVP_PKEY_from_keystore(\"%s\")", key_id);
394
Roshan Pius30b220e2017-03-31 16:47:04 -0700395 ensure_keystore_engine();
396
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800397 uint8_t *pubkey = NULL;
398 size_t pubkey_len;
399 int32_t ret = g_keystore_backend->get_pubkey(key_id, &pubkey, &pubkey_len);
400 if (ret < 0) {
401 ALOGW("could not contact keystore");
402 return NULL;
Roshan Pius30b220e2017-03-31 16:47:04 -0700403 } else if (ret != 0 || pubkey == NULL) {
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800404 ALOGW("keystore reports error: %d", ret);
Adam Langley1fb05832014-09-23 17:42:36 -0700405 return NULL;
406 }
407
Roshan Pius30b220e2017-03-31 16:47:04 -0700408 const uint8_t *inp = pubkey;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800409 Unique_EVP_PKEY pkey(d2i_PUBKEY(NULL, &inp, pubkey_len));
Adam Langley1fb05832014-09-23 17:42:36 -0700410 if (pkey.get() == NULL) {
411 ALOGW("Cannot convert pubkey");
412 return NULL;
413 }
414
Adam Langley1fb05832014-09-23 17:42:36 -0700415 EVP_PKEY *result;
416 switch (EVP_PKEY_type(pkey->type)) {
417 case EVP_PKEY_RSA: {
418 Unique_RSA public_rsa(EVP_PKEY_get1_RSA(pkey.get()));
419 result = wrap_rsa(key_id, public_rsa.get());
420 break;
421 }
422 case EVP_PKEY_EC: {
423 Unique_EC_KEY public_ecdsa(EVP_PKEY_get1_EC_KEY(pkey.get()));
424 result = wrap_ecdsa(key_id, public_ecdsa.get());
425 break;
426 }
427 default:
428 ALOGE("Unsupported key type %d", EVP_PKEY_type(pkey->type));
429 result = NULL;
430 }
431
432 return result;
433}
434
435} // extern "C"