Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 1 | /* 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 Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 23 | #define LOG_TAG "keystore-engine" |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 24 | #include <UniquePtr.h> |
| 25 | |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 26 | #include <pthread.h> |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 27 | #include <sys/socket.h> |
| 28 | #include <stdarg.h> |
| 29 | #include <string.h> |
| 30 | #include <unistd.h> |
| 31 | |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 32 | #include <cutils/log.h> |
| 33 | |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 34 | #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 Stewart | 657356c | 2017-03-09 00:00:23 -0800 | [diff] [blame^] | 43 | #ifndef BACKEND_WIFI_HIDL |
| 44 | #include "keystore_backend_binder.h" |
| 45 | #else |
| 46 | #include "keystore_backend_hidl.h" |
| 47 | #endif |
| 48 | |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 49 | namespace { |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 50 | extern const RSA_METHOD keystore_rsa_method; |
| 51 | extern const ECDSA_METHOD keystore_ecdsa_method; |
| 52 | |
| 53 | /* key_id_dup is called when one of the RSA or EC_KEY objects is duplicated. */ |
Kenny Root | dcca051 | 2015-04-18 11:21:48 -0700 | [diff] [blame] | 54 | int key_id_dup(CRYPTO_EX_DATA* /* to */, |
| 55 | const CRYPTO_EX_DATA* /* from */, |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 56 | void** from_d, |
Kenny Root | dcca051 | 2015-04-18 11:21:48 -0700 | [diff] [blame] | 57 | int /* index */, |
| 58 | long /* argl */, |
| 59 | void* /* argp */) { |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 60 | 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 Root | dcca051 | 2015-04-18 11:21:48 -0700 | [diff] [blame] | 68 | void key_id_free(void* /* parent */, |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 69 | void* ptr, |
Kenny Root | dcca051 | 2015-04-18 11:21:48 -0700 | [diff] [blame] | 70 | CRYPTO_EX_DATA* /* ad */, |
| 71 | int /* index */, |
| 72 | long /* argl */, |
| 73 | void* /* argp */) { |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 74 | char *key_id = reinterpret_cast<char *>(ptr); |
| 75 | free(key_id); |
| 76 | } |
| 77 | |
| 78 | /* KeystoreEngine is a BoringSSL ENGINE that implements RSA and ECDSA by |
| 79 | * forwarding the requested operations to Keystore. */ |
| 80 | class KeystoreEngine { |
| 81 | public: |
| 82 | KeystoreEngine() |
| 83 | : rsa_index_(RSA_get_ex_new_index(0 /* argl */, |
| 84 | NULL /* argp */, |
| 85 | NULL /* new_func */, |
| 86 | key_id_dup, |
| 87 | key_id_free)), |
| 88 | ec_key_index_(EC_KEY_get_ex_new_index(0 /* argl */, |
| 89 | NULL /* argp */, |
| 90 | NULL /* new_func */, |
| 91 | key_id_dup, |
| 92 | key_id_free)), |
| 93 | engine_(ENGINE_new()) { |
| 94 | ENGINE_set_RSA_method( |
| 95 | engine_, &keystore_rsa_method, sizeof(keystore_rsa_method)); |
| 96 | ENGINE_set_ECDSA_method( |
| 97 | engine_, &keystore_ecdsa_method, sizeof(keystore_ecdsa_method)); |
| 98 | } |
| 99 | |
| 100 | int rsa_ex_index() const { return rsa_index_; } |
| 101 | int ec_key_ex_index() const { return ec_key_index_; } |
| 102 | |
| 103 | const ENGINE* engine() const { return engine_; } |
| 104 | |
| 105 | private: |
| 106 | const int rsa_index_; |
| 107 | const int ec_key_index_; |
| 108 | ENGINE* const engine_; |
| 109 | }; |
| 110 | |
| 111 | pthread_once_t g_keystore_engine_once = PTHREAD_ONCE_INIT; |
| 112 | KeystoreEngine *g_keystore_engine; |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 113 | KeystoreBackend *g_keystore_backend; |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 114 | |
| 115 | /* init_keystore_engine is called to initialize |g_keystore_engine|. This |
| 116 | * should only be called by |pthread_once|. */ |
| 117 | void init_keystore_engine() { |
| 118 | g_keystore_engine = new KeystoreEngine; |
Paul Stewart | 657356c | 2017-03-09 00:00:23 -0800 | [diff] [blame^] | 119 | #ifndef BACKEND_WIFI_HIDL |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 120 | g_keystore_backend = new KeystoreBackendBinder; |
Paul Stewart | 657356c | 2017-03-09 00:00:23 -0800 | [diff] [blame^] | 121 | #else |
| 122 | g_keystore_backend = new KeystoreBackendHidl; |
| 123 | #endif |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /* ensure_keystore_engine ensures that |g_keystore_engine| is pointing to a |
| 127 | * valid |KeystoreEngine| object and creates one if not. */ |
| 128 | void ensure_keystore_engine() { |
| 129 | pthread_once(&g_keystore_engine_once, init_keystore_engine); |
| 130 | } |
| 131 | |
| 132 | /* Many OpenSSL APIs take ownership of an argument on success but don't free |
| 133 | * the argument on failure. This means we need to tell our scoped pointers when |
| 134 | * we've transferred ownership, without triggering a warning by not using the |
| 135 | * result of release(). */ |
| 136 | #define OWNERSHIP_TRANSFERRED(obj) \ |
Chih-Hung Hsieh | 26275ad | 2016-05-11 14:26:35 -0700 | [diff] [blame] | 137 | typeof ((obj).release()) _dummy __attribute__((unused)) = (obj).release() |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 138 | |
| 139 | const char* rsa_get_key_id(const RSA* rsa) { |
| 140 | return reinterpret_cast<char*>( |
| 141 | RSA_get_ex_data(rsa, g_keystore_engine->rsa_ex_index())); |
| 142 | } |
| 143 | |
| 144 | /* rsa_private_transform takes a big-endian integer from |in|, calculates the |
| 145 | * d'th power of it, modulo the RSA modulus, and writes the result as a |
| 146 | * big-endian integer to |out|. Both |in| and |out| are |len| bytes long. It |
| 147 | * returns one on success and zero otherwise. */ |
| 148 | int rsa_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in, size_t len) { |
| 149 | ALOGV("rsa_private_transform(%p, %p, %p, %u)", rsa, out, in, (unsigned) len); |
| 150 | |
| 151 | const char *key_id = rsa_get_key_id(rsa); |
| 152 | if (key_id == NULL) { |
| 153 | ALOGE("key had no key_id!"); |
| 154 | return 0; |
| 155 | } |
| 156 | |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 157 | uint8_t* reply = NULL; |
| 158 | size_t reply_len; |
| 159 | int32_t ret = g_keystore_backend->sign(key_id, in, len, &reply, &reply_len); |
| 160 | if (ret < 0) { |
| 161 | ALOGW("There was an error during rsa_decrypt: could not connect"); |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 162 | return 0; |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 163 | } else if (ret != 0) { |
| 164 | ALOGW("Error during sign from keystore: %d", ret); |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 165 | return 0; |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 166 | } else if (reply_len == 0 || reply == NULL) { |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 167 | ALOGW("No valid signature returned"); |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 171 | if (reply_len > len) { |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 172 | /* The result of the RSA operation can never be larger than the size of |
| 173 | * the modulus so we assume that the result has extra zeros on the |
| 174 | * left. This provides attackers with an oracle, but there's nothing |
| 175 | * that we can do about it here. */ |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 176 | ALOGW("Reply len %zu greater than expected %zu", reply_len, len); |
| 177 | memcpy(out, &reply[reply_len - len], len); |
| 178 | } else if (reply_len < len) { |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 179 | /* If the Keystore implementation returns a short value we assume that |
| 180 | * it's because it removed leading zeros from the left side. This is |
| 181 | * bad because it provides attackers with an oracle but we cannot do |
| 182 | * anything about a broken Keystore implementation here. */ |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 183 | ALOGW("Reply len %zu lesser than expected %zu", reply_len, len); |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 184 | memset(out, 0, len); |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 185 | memcpy(out + len - reply_len, &reply[0], reply_len); |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 186 | } else { |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 187 | memcpy(out, &reply[0], len); |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 190 | ALOGV("rsa=%p keystore_rsa_priv_dec successful", rsa); |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | const struct rsa_meth_st keystore_rsa_method = { |
| 195 | { |
| 196 | 0 /* references */, |
| 197 | 1 /* is_static */, |
| 198 | }, |
| 199 | NULL /* app_data */, |
| 200 | |
| 201 | NULL /* init */, |
| 202 | NULL /* finish */, |
| 203 | |
| 204 | NULL /* size */, |
| 205 | |
| 206 | NULL /* sign */, |
| 207 | NULL /* verify */, |
| 208 | |
| 209 | NULL /* encrypt */, |
| 210 | NULL /* sign_raw */, |
| 211 | NULL /* decrypt */, |
| 212 | NULL /* verify_raw */, |
| 213 | |
| 214 | rsa_private_transform, |
| 215 | |
| 216 | NULL /* mod_exp */, |
| 217 | NULL /* bn_mod_exp */, |
| 218 | |
David Benjamin | 30c7752 | 2016-03-28 18:00:17 -0400 | [diff] [blame] | 219 | RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_OPAQUE, |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 220 | |
| 221 | NULL /* keygen */, |
Adam Langley | 9eb9295 | 2015-09-02 15:28:03 -0700 | [diff] [blame] | 222 | NULL /* multi_prime_keygen */, |
Adam Langley | b2747fe | 2014-12-11 17:19:31 -0800 | [diff] [blame] | 223 | NULL /* supports_digest */, |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | const char* ecdsa_get_key_id(const EC_KEY* ec_key) { |
| 227 | return reinterpret_cast<char*>( |
| 228 | EC_KEY_get_ex_data(ec_key, g_keystore_engine->ec_key_ex_index())); |
| 229 | } |
| 230 | |
| 231 | /* ecdsa_sign signs |digest_len| bytes from |digest| with |ec_key| and writes |
| 232 | * the resulting signature (an ASN.1 encoded blob) to |sig|. It returns one on |
| 233 | * success and zero otherwise. */ |
| 234 | static int ecdsa_sign(const uint8_t* digest, size_t digest_len, uint8_t* sig, |
| 235 | unsigned int* sig_len, EC_KEY* ec_key) { |
| 236 | ALOGV("ecdsa_sign(%p, %u, %p)", digest, (unsigned) digest_len, ec_key); |
| 237 | |
| 238 | const char *key_id = ecdsa_get_key_id(ec_key); |
| 239 | if (key_id == NULL) { |
| 240 | ALOGE("key had no key_id!"); |
| 241 | return 0; |
| 242 | } |
| 243 | |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 244 | size_t ecdsa_size = ECDSA_size(ec_key); |
| 245 | |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 246 | uint8_t* reply = NULL; |
| 247 | size_t reply_len; |
| 248 | int32_t ret = g_keystore_backend->sign( |
| 249 | key_id, digest, digest_len, &reply, &reply_len); |
| 250 | if (ret < 0) { |
| 251 | ALOGW("There was an error during ecdsa_sign: could not connect"); |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 252 | return 0; |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 253 | } else if (reply_len == 0 || reply == NULL) { |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 254 | ALOGW("No valid signature returned"); |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 255 | return 0; |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 256 | } else if (reply_len > ecdsa_size) { |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 257 | ALOGW("Signature is too large"); |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 258 | return 0; |
| 259 | } |
| 260 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 261 | // Reviewer: should't sig_len be checked here? Or is it just assumed that it is at least ecdsa_size? |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 262 | memcpy(sig, &reply[0], reply_len); |
| 263 | *sig_len = reply_len; |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 264 | |
| 265 | ALOGV("ecdsa_sign(%p, %u, %p) => success", digest, (unsigned)digest_len, |
| 266 | ec_key); |
| 267 | return 1; |
| 268 | } |
| 269 | |
| 270 | const ECDSA_METHOD keystore_ecdsa_method = { |
| 271 | { |
| 272 | 0 /* references */, |
| 273 | 1 /* is_static */ |
| 274 | } /* common */, |
| 275 | NULL /* app_data */, |
| 276 | |
| 277 | NULL /* init */, |
| 278 | NULL /* finish */, |
| 279 | NULL /* group_order_size */, |
| 280 | ecdsa_sign, |
| 281 | NULL /* verify */, |
| 282 | ECDSA_FLAG_OPAQUE, |
| 283 | }; |
| 284 | |
| 285 | struct EVP_PKEY_Delete { |
| 286 | void operator()(EVP_PKEY* p) const { |
| 287 | EVP_PKEY_free(p); |
| 288 | } |
| 289 | }; |
| 290 | typedef UniquePtr<EVP_PKEY, EVP_PKEY_Delete> Unique_EVP_PKEY; |
| 291 | |
| 292 | struct RSA_Delete { |
| 293 | void operator()(RSA* p) const { |
| 294 | RSA_free(p); |
| 295 | } |
| 296 | }; |
| 297 | typedef UniquePtr<RSA, RSA_Delete> Unique_RSA; |
| 298 | |
| 299 | struct EC_KEY_Delete { |
| 300 | void operator()(EC_KEY* ec) const { |
| 301 | EC_KEY_free(ec); |
| 302 | } |
| 303 | }; |
| 304 | typedef UniquePtr<EC_KEY, EC_KEY_Delete> Unique_EC_KEY; |
| 305 | |
| 306 | /* wrap_rsa returns an |EVP_PKEY| that contains an RSA key where the public |
| 307 | * part is taken from |public_rsa| and the private operations are forwarded to |
| 308 | * KeyStore and operate on the key named |key_id|. */ |
| 309 | static EVP_PKEY *wrap_rsa(const char *key_id, const RSA *public_rsa) { |
| 310 | Unique_RSA rsa(RSA_new_method(g_keystore_engine->engine())); |
| 311 | if (rsa.get() == NULL) { |
| 312 | return NULL; |
| 313 | } |
| 314 | |
| 315 | char *key_id_copy = strdup(key_id); |
| 316 | if (key_id_copy == NULL) { |
| 317 | return NULL; |
| 318 | } |
| 319 | |
| 320 | if (!RSA_set_ex_data(rsa.get(), g_keystore_engine->rsa_ex_index(), |
| 321 | key_id_copy)) { |
| 322 | free(key_id_copy); |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | rsa->n = BN_dup(public_rsa->n); |
| 327 | rsa->e = BN_dup(public_rsa->e); |
| 328 | if (rsa->n == NULL || rsa->e == NULL) { |
| 329 | return NULL; |
| 330 | } |
| 331 | |
| 332 | Unique_EVP_PKEY result(EVP_PKEY_new()); |
| 333 | if (result.get() == NULL || |
| 334 | !EVP_PKEY_assign_RSA(result.get(), rsa.get())) { |
| 335 | return NULL; |
| 336 | } |
| 337 | OWNERSHIP_TRANSFERRED(rsa); |
| 338 | |
| 339 | return result.release(); |
| 340 | } |
| 341 | |
| 342 | /* wrap_ecdsa returns an |EVP_PKEY| that contains an ECDSA key where the public |
| 343 | * part is taken from |public_rsa| and the private operations are forwarded to |
| 344 | * KeyStore and operate on the key named |key_id|. */ |
| 345 | static EVP_PKEY *wrap_ecdsa(const char *key_id, const EC_KEY *public_ecdsa) { |
| 346 | Unique_EC_KEY ec(EC_KEY_new_method(g_keystore_engine->engine())); |
| 347 | if (ec.get() == NULL) { |
| 348 | return NULL; |
| 349 | } |
| 350 | |
| 351 | if (!EC_KEY_set_group(ec.get(), EC_KEY_get0_group(public_ecdsa)) || |
| 352 | !EC_KEY_set_public_key(ec.get(), EC_KEY_get0_public_key(public_ecdsa))) { |
| 353 | return NULL; |
| 354 | } |
| 355 | |
| 356 | char *key_id_copy = strdup(key_id); |
| 357 | if (key_id_copy == NULL) { |
| 358 | return NULL; |
| 359 | } |
| 360 | |
| 361 | if (!EC_KEY_set_ex_data(ec.get(), g_keystore_engine->ec_key_ex_index(), |
| 362 | key_id_copy)) { |
| 363 | free(key_id_copy); |
| 364 | return NULL; |
| 365 | } |
| 366 | |
| 367 | Unique_EVP_PKEY result(EVP_PKEY_new()); |
| 368 | if (result.get() == NULL || |
| 369 | !EVP_PKEY_assign_EC_KEY(result.get(), ec.get())) { |
| 370 | return NULL; |
| 371 | } |
| 372 | OWNERSHIP_TRANSFERRED(ec); |
| 373 | |
| 374 | return result.release(); |
| 375 | } |
| 376 | |
| 377 | } /* anonymous namespace */ |
| 378 | |
| 379 | extern "C" { |
| 380 | |
| 381 | EVP_PKEY* EVP_PKEY_from_keystore(const char* key_id) __attribute__((visibility("default"))); |
| 382 | |
| 383 | /* EVP_PKEY_from_keystore returns an |EVP_PKEY| that contains either an RSA or |
| 384 | * ECDSA key where the public part of the key reflects the value of the key |
| 385 | * named |key_id| in Keystore and the private operations are forwarded onto |
| 386 | * KeyStore. */ |
| 387 | EVP_PKEY* EVP_PKEY_from_keystore(const char* key_id) { |
| 388 | ALOGV("EVP_PKEY_from_keystore(\"%s\")", key_id); |
| 389 | |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 390 | uint8_t *pubkey = NULL; |
| 391 | size_t pubkey_len; |
| 392 | int32_t ret = g_keystore_backend->get_pubkey(key_id, &pubkey, &pubkey_len); |
| 393 | if (ret < 0) { |
| 394 | ALOGW("could not contact keystore"); |
| 395 | return NULL; |
| 396 | } else if (ret != 0) { |
| 397 | ALOGW("keystore reports error: %d", ret); |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 398 | return NULL; |
| 399 | } |
| 400 | |
Janis Danisevskis | c7a9fa2 | 2016-10-13 18:43:45 +0100 | [diff] [blame] | 401 | const uint8_t *inp = &pubkey[0]; |
Paul Stewart | ac0ffbf | 2017-03-03 16:43:33 -0800 | [diff] [blame] | 402 | Unique_EVP_PKEY pkey(d2i_PUBKEY(NULL, &inp, pubkey_len)); |
Adam Langley | 1fb0583 | 2014-09-23 17:42:36 -0700 | [diff] [blame] | 403 | if (pkey.get() == NULL) { |
| 404 | ALOGW("Cannot convert pubkey"); |
| 405 | return NULL; |
| 406 | } |
| 407 | |
| 408 | ensure_keystore_engine(); |
| 409 | |
| 410 | EVP_PKEY *result; |
| 411 | switch (EVP_PKEY_type(pkey->type)) { |
| 412 | case EVP_PKEY_RSA: { |
| 413 | Unique_RSA public_rsa(EVP_PKEY_get1_RSA(pkey.get())); |
| 414 | result = wrap_rsa(key_id, public_rsa.get()); |
| 415 | break; |
| 416 | } |
| 417 | case EVP_PKEY_EC: { |
| 418 | Unique_EC_KEY public_ecdsa(EVP_PKEY_get1_EC_KEY(pkey.get())); |
| 419 | result = wrap_ecdsa(key_id, public_ecdsa.get()); |
| 420 | break; |
| 421 | } |
| 422 | default: |
| 423 | ALOGE("Unsupported key type %d", EVP_PKEY_type(pkey->type)); |
| 424 | result = NULL; |
| 425 | } |
| 426 | |
| 427 | return result; |
| 428 | } |
| 429 | |
| 430 | } // extern "C" |