blob: efb1d083c2895dfd874b692e9083851eb4b27f3d [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#include <UniquePtr.h>
25
Paul Stewartac0ffbf2017-03-03 16:43:33 -080026#include <pthread.h>
Adam Langley1fb05832014-09-23 17:42:36 -070027#include <sys/socket.h>
28#include <stdarg.h>
29#include <string.h>
30#include <unistd.h>
31
Paul Stewartac0ffbf2017-03-03 16:43:33 -080032#include <cutils/log.h>
33
Adam Langley1fb05832014-09-23 17:42:36 -070034#include <openssl/bn.h>
35#include <openssl/ec.h>
36#include <openssl/ec_key.h>
37#include <openssl/ecdsa.h>
38#include <openssl/engine.h>
39#include <openssl/evp.h>
40#include <openssl/rsa.h>
41#include <openssl/x509.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);
61 if (key_id != NULL) {
62 *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(). */
82#define OWNERSHIP_TRANSFERRED(obj) \
Chih-Hung Hsieh26275ad2016-05-11 14:26:35 -070083 typeof ((obj).release()) _dummy __attribute__((unused)) = (obj).release()
Adam Langley1fb05832014-09-23 17:42:36 -070084
Robert Sloan29f72ec2017-07-14 12:21:26 -070085const char* rsa_get_key_id(const RSA* rsa);
Adam Langley1fb05832014-09-23 17:42:36 -070086
87/* rsa_private_transform takes a big-endian integer from |in|, calculates the
88 * d'th power of it, modulo the RSA modulus, and writes the result as a
89 * big-endian integer to |out|. Both |in| and |out| are |len| bytes long. It
90 * returns one on success and zero otherwise. */
91int rsa_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in, size_t len) {
92 ALOGV("rsa_private_transform(%p, %p, %p, %u)", rsa, out, in, (unsigned) len);
93
Roshan Pius30b220e2017-03-31 16:47:04 -070094 ensure_keystore_engine();
95
Adam Langley1fb05832014-09-23 17:42:36 -070096 const char *key_id = rsa_get_key_id(rsa);
97 if (key_id == NULL) {
98 ALOGE("key had no key_id!");
99 return 0;
100 }
101
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800102 uint8_t* reply = NULL;
103 size_t reply_len;
104 int32_t ret = g_keystore_backend->sign(key_id, in, len, &reply, &reply_len);
105 if (ret < 0) {
106 ALOGW("There was an error during rsa_decrypt: could not connect");
Adam Langley1fb05832014-09-23 17:42:36 -0700107 return 0;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800108 } else if (ret != 0) {
109 ALOGW("Error during sign from keystore: %d", ret);
Adam Langley1fb05832014-09-23 17:42:36 -0700110 return 0;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800111 } else if (reply_len == 0 || reply == NULL) {
Adam Langley1fb05832014-09-23 17:42:36 -0700112 ALOGW("No valid signature returned");
Adam Langley1fb05832014-09-23 17:42:36 -0700113 return 0;
114 }
115
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800116 if (reply_len > len) {
Adam Langley1fb05832014-09-23 17:42:36 -0700117 /* The result of the RSA operation can never be larger than the size of
118 * the modulus so we assume that the result has extra zeros on the
119 * left. This provides attackers with an oracle, but there's nothing
120 * that we can do about it here. */
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800121 ALOGW("Reply len %zu greater than expected %zu", reply_len, len);
122 memcpy(out, &reply[reply_len - len], len);
123 } else if (reply_len < len) {
Adam Langley1fb05832014-09-23 17:42:36 -0700124 /* If the Keystore implementation returns a short value we assume that
125 * it's because it removed leading zeros from the left side. This is
126 * bad because it provides attackers with an oracle but we cannot do
127 * anything about a broken Keystore implementation here. */
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800128 ALOGW("Reply len %zu lesser than expected %zu", reply_len, len);
Adam Langley1fb05832014-09-23 17:42:36 -0700129 memset(out, 0, len);
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800130 memcpy(out + len - reply_len, &reply[0], reply_len);
Adam Langley1fb05832014-09-23 17:42:36 -0700131 } else {
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100132 memcpy(out, &reply[0], len);
Adam Langley1fb05832014-09-23 17:42:36 -0700133 }
134
Adam Langley1fb05832014-09-23 17:42:36 -0700135 ALOGV("rsa=%p keystore_rsa_priv_dec successful", rsa);
136 return 1;
137}
138
Robert Sloan29f72ec2017-07-14 12:21:26 -0700139const char* ecdsa_get_key_id(const EC_KEY* ec_key);
Adam Langley1fb05832014-09-23 17:42:36 -0700140
141/* ecdsa_sign signs |digest_len| bytes from |digest| with |ec_key| and writes
142 * the resulting signature (an ASN.1 encoded blob) to |sig|. It returns one on
143 * success and zero otherwise. */
144static int ecdsa_sign(const uint8_t* digest, size_t digest_len, uint8_t* sig,
145 unsigned int* sig_len, EC_KEY* ec_key) {
146 ALOGV("ecdsa_sign(%p, %u, %p)", digest, (unsigned) digest_len, ec_key);
147
Roshan Pius30b220e2017-03-31 16:47:04 -0700148 ensure_keystore_engine();
149
Adam Langley1fb05832014-09-23 17:42:36 -0700150 const char *key_id = ecdsa_get_key_id(ec_key);
151 if (key_id == NULL) {
152 ALOGE("key had no key_id!");
153 return 0;
154 }
155
Adam Langley1fb05832014-09-23 17:42:36 -0700156 size_t ecdsa_size = ECDSA_size(ec_key);
157
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800158 uint8_t* reply = NULL;
159 size_t reply_len;
160 int32_t ret = g_keystore_backend->sign(
161 key_id, digest, digest_len, &reply, &reply_len);
162 if (ret < 0) {
163 ALOGW("There was an error during ecdsa_sign: could not connect");
Adam Langley1fb05832014-09-23 17:42:36 -0700164 return 0;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800165 } else if (reply_len == 0 || reply == NULL) {
Adam Langley1fb05832014-09-23 17:42:36 -0700166 ALOGW("No valid signature returned");
Adam Langley1fb05832014-09-23 17:42:36 -0700167 return 0;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800168 } else if (reply_len > ecdsa_size) {
Adam Langley1fb05832014-09-23 17:42:36 -0700169 ALOGW("Signature is too large");
Adam Langley1fb05832014-09-23 17:42:36 -0700170 return 0;
171 }
172
Janis Danisevskisc7a9fa22016-10-13 18:43:45 +0100173 // 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 -0800174 memcpy(sig, &reply[0], reply_len);
175 *sig_len = reply_len;
Adam Langley1fb05832014-09-23 17:42:36 -0700176
177 ALOGV("ecdsa_sign(%p, %u, %p) => success", digest, (unsigned)digest_len,
178 ec_key);
179 return 1;
180}
181
Robert Sloan29f72ec2017-07-14 12:21:26 -0700182/* KeystoreEngine is a BoringSSL ENGINE that implements RSA and ECDSA by
183 * forwarding the requested operations to Keystore. */
184class KeystoreEngine {
185 public:
186 KeystoreEngine()
187 : rsa_index_(RSA_get_ex_new_index(0 /* argl */,
188 NULL /* argp */,
189 NULL /* new_func */,
190 key_id_dup,
191 key_id_free)),
192 ec_key_index_(EC_KEY_get_ex_new_index(0 /* argl */,
193 NULL /* argp */,
194 NULL /* new_func */,
195 key_id_dup,
196 key_id_free)),
197 engine_(ENGINE_new()) {
198 memset(&rsa_method_, 0, sizeof(rsa_method_));
199 rsa_method_.common.is_static = 1;
200 rsa_method_.private_transform = rsa_private_transform;
201 rsa_method_.flags = RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_OPAQUE;
202 ENGINE_set_RSA_method(engine_, &rsa_method_, sizeof(rsa_method_));
Adam Langley1fb05832014-09-23 17:42:36 -0700203
Robert Sloan29f72ec2017-07-14 12:21:26 -0700204 memset(&ecdsa_method_, 0, sizeof(ecdsa_method_));
205 ecdsa_method_.common.is_static = 1;
206 ecdsa_method_.sign = ecdsa_sign;
207 ecdsa_method_.flags = ECDSA_FLAG_OPAQUE;
208 ENGINE_set_ECDSA_method(engine_, &ecdsa_method_, sizeof(ecdsa_method_));
209 }
210
211 int rsa_ex_index() const { return rsa_index_; }
212 int ec_key_ex_index() const { return ec_key_index_; }
213
214 const ENGINE* engine() const { return engine_; }
215
216 private:
217 const int rsa_index_;
218 const int ec_key_index_;
219 RSA_METHOD rsa_method_;
220 ECDSA_METHOD ecdsa_method_;
221 ENGINE* const engine_;
Adam Langley1fb05832014-09-23 17:42:36 -0700222};
223
Robert Sloan29f72ec2017-07-14 12:21:26 -0700224pthread_once_t g_keystore_engine_once = PTHREAD_ONCE_INIT;
225KeystoreEngine *g_keystore_engine;
226
227/* init_keystore_engine is called to initialize |g_keystore_engine|. This
228 * should only be called by |pthread_once|. */
229void init_keystore_engine() {
230 g_keystore_engine = new KeystoreEngine;
231#ifndef BACKEND_WIFI_HIDL
232 g_keystore_backend = new KeystoreBackendBinder;
233#else
234 g_keystore_backend = new KeystoreBackendHidl;
235#endif
236}
237
238/* ensure_keystore_engine ensures that |g_keystore_engine| is pointing to a
239 * valid |KeystoreEngine| object and creates one if not. */
240void ensure_keystore_engine() {
241 pthread_once(&g_keystore_engine_once, init_keystore_engine);
242}
243
244const char* rsa_get_key_id(const RSA* rsa) {
245 return reinterpret_cast<char*>(
246 RSA_get_ex_data(rsa, g_keystore_engine->rsa_ex_index()));
247}
248
249const char* ecdsa_get_key_id(const EC_KEY* ec_key) {
250 return reinterpret_cast<char*>(
251 EC_KEY_get_ex_data(ec_key, g_keystore_engine->ec_key_ex_index()));
252}
253
Adam Langley1fb05832014-09-23 17:42:36 -0700254struct EVP_PKEY_Delete {
255 void operator()(EVP_PKEY* p) const {
256 EVP_PKEY_free(p);
257 }
258};
259typedef UniquePtr<EVP_PKEY, EVP_PKEY_Delete> Unique_EVP_PKEY;
260
261struct RSA_Delete {
262 void operator()(RSA* p) const {
263 RSA_free(p);
264 }
265};
266typedef UniquePtr<RSA, RSA_Delete> Unique_RSA;
267
268struct EC_KEY_Delete {
269 void operator()(EC_KEY* ec) const {
270 EC_KEY_free(ec);
271 }
272};
273typedef UniquePtr<EC_KEY, EC_KEY_Delete> Unique_EC_KEY;
274
275/* wrap_rsa returns an |EVP_PKEY| that contains an RSA key where the public
276 * part is taken from |public_rsa| and the private operations are forwarded to
277 * KeyStore and operate on the key named |key_id|. */
278static EVP_PKEY *wrap_rsa(const char *key_id, const RSA *public_rsa) {
279 Unique_RSA rsa(RSA_new_method(g_keystore_engine->engine()));
280 if (rsa.get() == NULL) {
281 return NULL;
282 }
283
284 char *key_id_copy = strdup(key_id);
285 if (key_id_copy == NULL) {
286 return NULL;
287 }
288
289 if (!RSA_set_ex_data(rsa.get(), g_keystore_engine->rsa_ex_index(),
290 key_id_copy)) {
291 free(key_id_copy);
292 return NULL;
293 }
294
295 rsa->n = BN_dup(public_rsa->n);
296 rsa->e = BN_dup(public_rsa->e);
297 if (rsa->n == NULL || rsa->e == NULL) {
298 return NULL;
299 }
300
301 Unique_EVP_PKEY result(EVP_PKEY_new());
302 if (result.get() == NULL ||
303 !EVP_PKEY_assign_RSA(result.get(), rsa.get())) {
304 return NULL;
305 }
306 OWNERSHIP_TRANSFERRED(rsa);
307
308 return result.release();
309}
310
311/* wrap_ecdsa returns an |EVP_PKEY| that contains an ECDSA 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_ecdsa(const char *key_id, const EC_KEY *public_ecdsa) {
315 Unique_EC_KEY ec(EC_KEY_new_method(g_keystore_engine->engine()));
316 if (ec.get() == NULL) {
317 return NULL;
318 }
319
320 if (!EC_KEY_set_group(ec.get(), EC_KEY_get0_group(public_ecdsa)) ||
321 !EC_KEY_set_public_key(ec.get(), EC_KEY_get0_public_key(public_ecdsa))) {
322 return NULL;
323 }
324
325 char *key_id_copy = strdup(key_id);
326 if (key_id_copy == NULL) {
327 return NULL;
328 }
329
330 if (!EC_KEY_set_ex_data(ec.get(), g_keystore_engine->ec_key_ex_index(),
331 key_id_copy)) {
332 free(key_id_copy);
333 return NULL;
334 }
335
336 Unique_EVP_PKEY result(EVP_PKEY_new());
337 if (result.get() == NULL ||
338 !EVP_PKEY_assign_EC_KEY(result.get(), ec.get())) {
339 return NULL;
340 }
341 OWNERSHIP_TRANSFERRED(ec);
342
343 return result.release();
344}
345
346} /* anonymous namespace */
347
348extern "C" {
349
350EVP_PKEY* EVP_PKEY_from_keystore(const char* key_id) __attribute__((visibility("default")));
351
352/* EVP_PKEY_from_keystore returns an |EVP_PKEY| that contains either an RSA or
353 * ECDSA key where the public part of the key reflects the value of the key
354 * named |key_id| in Keystore and the private operations are forwarded onto
355 * KeyStore. */
356EVP_PKEY* EVP_PKEY_from_keystore(const char* key_id) {
357 ALOGV("EVP_PKEY_from_keystore(\"%s\")", key_id);
358
Roshan Pius30b220e2017-03-31 16:47:04 -0700359 ensure_keystore_engine();
360
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800361 uint8_t *pubkey = NULL;
362 size_t pubkey_len;
363 int32_t ret = g_keystore_backend->get_pubkey(key_id, &pubkey, &pubkey_len);
364 if (ret < 0) {
365 ALOGW("could not contact keystore");
366 return NULL;
Roshan Pius30b220e2017-03-31 16:47:04 -0700367 } else if (ret != 0 || pubkey == NULL) {
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800368 ALOGW("keystore reports error: %d", ret);
Adam Langley1fb05832014-09-23 17:42:36 -0700369 return NULL;
370 }
371
Roshan Pius30b220e2017-03-31 16:47:04 -0700372 const uint8_t *inp = pubkey;
Paul Stewartac0ffbf2017-03-03 16:43:33 -0800373 Unique_EVP_PKEY pkey(d2i_PUBKEY(NULL, &inp, pubkey_len));
Adam Langley1fb05832014-09-23 17:42:36 -0700374 if (pkey.get() == NULL) {
375 ALOGW("Cannot convert pubkey");
376 return NULL;
377 }
378
Adam Langley1fb05832014-09-23 17:42:36 -0700379 EVP_PKEY *result;
380 switch (EVP_PKEY_type(pkey->type)) {
381 case EVP_PKEY_RSA: {
382 Unique_RSA public_rsa(EVP_PKEY_get1_RSA(pkey.get()));
383 result = wrap_rsa(key_id, public_rsa.get());
384 break;
385 }
386 case EVP_PKEY_EC: {
387 Unique_EC_KEY public_ecdsa(EVP_PKEY_get1_EC_KEY(pkey.get()));
388 result = wrap_ecdsa(key_id, public_ecdsa.get());
389 break;
390 }
391 default:
392 ALOGE("Unsupported key type %d", EVP_PKEY_type(pkey->type));
393 result = NULL;
394 }
395
396 return result;
397}
398
399} // extern "C"