Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013, Google Inc. |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include "mkimage.h" |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 9 | #include <image.h> |
| 10 | #include <time.h> |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 11 | #include <openssl/bn.h> |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 12 | #include <openssl/rsa.h> |
| 13 | #include <openssl/pem.h> |
| 14 | #include <openssl/err.h> |
| 15 | #include <openssl/ssl.h> |
| 16 | #include <openssl/evp.h> |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 17 | #include <openssl/engine.h> |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 18 | |
| 19 | #if OPENSSL_VERSION_NUMBER >= 0x10000000L |
| 20 | #define HAVE_ERR_REMOVE_THREAD_STATE |
| 21 | #endif |
| 22 | |
Caliph Nomble | 1a31b02 | 2018-07-25 22:13:03 -0400 | [diff] [blame] | 23 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
| 24 | (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL) |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 25 | static void RSA_get0_key(const RSA *r, |
| 26 | const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) |
| 27 | { |
| 28 | if (n != NULL) |
| 29 | *n = r->n; |
| 30 | if (e != NULL) |
| 31 | *e = r->e; |
| 32 | if (d != NULL) |
| 33 | *d = r->d; |
| 34 | } |
| 35 | #endif |
| 36 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 37 | static int rsa_err(const char *msg) |
| 38 | { |
| 39 | unsigned long sslErr = ERR_get_error(); |
| 40 | |
| 41 | fprintf(stderr, "%s", msg); |
| 42 | fprintf(stderr, ": %s\n", |
| 43 | ERR_error_string(sslErr, 0)); |
| 44 | |
| 45 | return -1; |
| 46 | } |
| 47 | |
| 48 | /** |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 49 | * rsa_pem_get_pub_key() - read a public key from a .crt file |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 50 | * |
| 51 | * @keydir: Directory containins the key |
| 52 | * @name Name of key file (will have a .crt extension) |
| 53 | * @rsap Returns RSA object, or NULL on failure |
| 54 | * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) |
| 55 | */ |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 56 | static int rsa_pem_get_pub_key(const char *keydir, const char *name, RSA **rsap) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 57 | { |
| 58 | char path[1024]; |
| 59 | EVP_PKEY *key; |
| 60 | X509 *cert; |
| 61 | RSA *rsa; |
| 62 | FILE *f; |
| 63 | int ret; |
| 64 | |
| 65 | *rsap = NULL; |
| 66 | snprintf(path, sizeof(path), "%s/%s.crt", keydir, name); |
| 67 | f = fopen(path, "r"); |
| 68 | if (!f) { |
| 69 | fprintf(stderr, "Couldn't open RSA certificate: '%s': %s\n", |
| 70 | path, strerror(errno)); |
| 71 | return -EACCES; |
| 72 | } |
| 73 | |
| 74 | /* Read the certificate */ |
| 75 | cert = NULL; |
| 76 | if (!PEM_read_X509(f, &cert, NULL, NULL)) { |
| 77 | rsa_err("Couldn't read certificate"); |
| 78 | ret = -EINVAL; |
| 79 | goto err_cert; |
| 80 | } |
| 81 | |
| 82 | /* Get the public key from the certificate. */ |
| 83 | key = X509_get_pubkey(cert); |
| 84 | if (!key) { |
| 85 | rsa_err("Couldn't read public key\n"); |
| 86 | ret = -EINVAL; |
| 87 | goto err_pubkey; |
| 88 | } |
| 89 | |
| 90 | /* Convert to a RSA_style key. */ |
| 91 | rsa = EVP_PKEY_get1_RSA(key); |
| 92 | if (!rsa) { |
| 93 | rsa_err("Couldn't convert to a RSA style key"); |
Simon Glass | a436f21 | 2014-07-30 10:00:17 -0600 | [diff] [blame] | 94 | ret = -EINVAL; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 95 | goto err_rsa; |
| 96 | } |
| 97 | fclose(f); |
| 98 | EVP_PKEY_free(key); |
| 99 | X509_free(cert); |
| 100 | *rsap = rsa; |
| 101 | |
| 102 | return 0; |
| 103 | |
| 104 | err_rsa: |
| 105 | EVP_PKEY_free(key); |
| 106 | err_pubkey: |
| 107 | X509_free(cert); |
| 108 | err_cert: |
| 109 | fclose(f); |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | /** |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 114 | * rsa_engine_get_pub_key() - read a public key from given engine |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 115 | * |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 116 | * @keydir: Key prefix |
| 117 | * @name Name of key |
| 118 | * @engine Engine to use |
| 119 | * @rsap Returns RSA object, or NULL on failure |
| 120 | * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) |
| 121 | */ |
| 122 | static int rsa_engine_get_pub_key(const char *keydir, const char *name, |
| 123 | ENGINE *engine, RSA **rsap) |
| 124 | { |
| 125 | const char *engine_id; |
| 126 | char key_id[1024]; |
| 127 | EVP_PKEY *key; |
| 128 | RSA *rsa; |
| 129 | int ret; |
| 130 | |
| 131 | *rsap = NULL; |
| 132 | |
| 133 | engine_id = ENGINE_get_id(engine); |
| 134 | |
| 135 | if (engine_id && !strcmp(engine_id, "pkcs11")) { |
| 136 | if (keydir) |
| 137 | snprintf(key_id, sizeof(key_id), |
| 138 | "pkcs11:%s;object=%s;type=public", |
| 139 | keydir, name); |
| 140 | else |
| 141 | snprintf(key_id, sizeof(key_id), |
| 142 | "pkcs11:object=%s;type=public", |
| 143 | name); |
| 144 | } else { |
| 145 | fprintf(stderr, "Engine not supported\n"); |
| 146 | return -ENOTSUP; |
| 147 | } |
| 148 | |
| 149 | key = ENGINE_load_public_key(engine, key_id, NULL, NULL); |
| 150 | if (!key) |
| 151 | return rsa_err("Failure loading public key from engine"); |
| 152 | |
| 153 | /* Convert to a RSA_style key. */ |
| 154 | rsa = EVP_PKEY_get1_RSA(key); |
| 155 | if (!rsa) { |
| 156 | rsa_err("Couldn't convert to a RSA style key"); |
| 157 | ret = -EINVAL; |
| 158 | goto err_rsa; |
| 159 | } |
| 160 | |
| 161 | EVP_PKEY_free(key); |
| 162 | *rsap = rsa; |
| 163 | |
| 164 | return 0; |
| 165 | |
| 166 | err_rsa: |
| 167 | EVP_PKEY_free(key); |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * rsa_get_pub_key() - read a public key |
| 173 | * |
| 174 | * @keydir: Directory containing the key (PEM file) or key prefix (engine) |
| 175 | * @name Name of key file (will have a .crt extension) |
| 176 | * @engine Engine to use |
| 177 | * @rsap Returns RSA object, or NULL on failure |
| 178 | * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) |
| 179 | */ |
| 180 | static int rsa_get_pub_key(const char *keydir, const char *name, |
| 181 | ENGINE *engine, RSA **rsap) |
| 182 | { |
| 183 | if (engine) |
| 184 | return rsa_engine_get_pub_key(keydir, name, engine, rsap); |
| 185 | return rsa_pem_get_pub_key(keydir, name, rsap); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * rsa_pem_get_priv_key() - read a private key from a .key file |
| 190 | * |
| 191 | * @keydir: Directory containing the key |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 192 | * @name Name of key file (will have a .key extension) |
| 193 | * @rsap Returns RSA object, or NULL on failure |
| 194 | * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) |
| 195 | */ |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 196 | static int rsa_pem_get_priv_key(const char *keydir, const char *name, |
| 197 | RSA **rsap) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 198 | { |
| 199 | char path[1024]; |
| 200 | RSA *rsa; |
| 201 | FILE *f; |
| 202 | |
| 203 | *rsap = NULL; |
| 204 | snprintf(path, sizeof(path), "%s/%s.key", keydir, name); |
| 205 | f = fopen(path, "r"); |
| 206 | if (!f) { |
| 207 | fprintf(stderr, "Couldn't open RSA private key: '%s': %s\n", |
| 208 | path, strerror(errno)); |
| 209 | return -ENOENT; |
| 210 | } |
| 211 | |
| 212 | rsa = PEM_read_RSAPrivateKey(f, 0, NULL, path); |
| 213 | if (!rsa) { |
| 214 | rsa_err("Failure reading private key"); |
| 215 | fclose(f); |
| 216 | return -EPROTO; |
| 217 | } |
| 218 | fclose(f); |
| 219 | *rsap = rsa; |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 224 | /** |
| 225 | * rsa_engine_get_priv_key() - read a private key from given engine |
| 226 | * |
| 227 | * @keydir: Key prefix |
| 228 | * @name Name of key |
| 229 | * @engine Engine to use |
| 230 | * @rsap Returns RSA object, or NULL on failure |
| 231 | * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) |
| 232 | */ |
| 233 | static int rsa_engine_get_priv_key(const char *keydir, const char *name, |
| 234 | ENGINE *engine, RSA **rsap) |
| 235 | { |
| 236 | const char *engine_id; |
| 237 | char key_id[1024]; |
| 238 | EVP_PKEY *key; |
| 239 | RSA *rsa; |
| 240 | int ret; |
| 241 | |
| 242 | *rsap = NULL; |
| 243 | |
| 244 | engine_id = ENGINE_get_id(engine); |
| 245 | |
| 246 | if (engine_id && !strcmp(engine_id, "pkcs11")) { |
| 247 | if (keydir) |
| 248 | snprintf(key_id, sizeof(key_id), |
| 249 | "pkcs11:%s;object=%s;type=private", |
| 250 | keydir, name); |
| 251 | else |
| 252 | snprintf(key_id, sizeof(key_id), |
| 253 | "pkcs11:object=%s;type=private", |
| 254 | name); |
| 255 | } else { |
| 256 | fprintf(stderr, "Engine not supported\n"); |
| 257 | return -ENOTSUP; |
| 258 | } |
| 259 | |
| 260 | key = ENGINE_load_private_key(engine, key_id, NULL, NULL); |
| 261 | if (!key) |
| 262 | return rsa_err("Failure loading private key from engine"); |
| 263 | |
| 264 | /* Convert to a RSA_style key. */ |
| 265 | rsa = EVP_PKEY_get1_RSA(key); |
| 266 | if (!rsa) { |
| 267 | rsa_err("Couldn't convert to a RSA style key"); |
| 268 | ret = -EINVAL; |
| 269 | goto err_rsa; |
| 270 | } |
| 271 | |
| 272 | EVP_PKEY_free(key); |
| 273 | *rsap = rsa; |
| 274 | |
| 275 | return 0; |
| 276 | |
| 277 | err_rsa: |
| 278 | EVP_PKEY_free(key); |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * rsa_get_priv_key() - read a private key |
| 284 | * |
| 285 | * @keydir: Directory containing the key (PEM file) or key prefix (engine) |
| 286 | * @name Name of key |
| 287 | * @engine Engine to use for signing |
| 288 | * @rsap Returns RSA object, or NULL on failure |
| 289 | * @return 0 if ok, -ve on error (in which case *rsap will be set to NULL) |
| 290 | */ |
| 291 | static int rsa_get_priv_key(const char *keydir, const char *name, |
| 292 | ENGINE *engine, RSA **rsap) |
| 293 | { |
| 294 | if (engine) |
| 295 | return rsa_engine_get_priv_key(keydir, name, engine, rsap); |
| 296 | return rsa_pem_get_priv_key(keydir, name, rsap); |
| 297 | } |
| 298 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 299 | static int rsa_init(void) |
| 300 | { |
| 301 | int ret; |
| 302 | |
Caliph Nomble | 1a31b02 | 2018-07-25 22:13:03 -0400 | [diff] [blame] | 303 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
| 304 | (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 305 | ret = SSL_library_init(); |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 306 | #else |
| 307 | ret = OPENSSL_init_ssl(0, NULL); |
| 308 | #endif |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 309 | if (!ret) { |
| 310 | fprintf(stderr, "Failure to init SSL library\n"); |
| 311 | return -1; |
| 312 | } |
Caliph Nomble | 1a31b02 | 2018-07-25 22:13:03 -0400 | [diff] [blame] | 313 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
| 314 | (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 315 | SSL_load_error_strings(); |
| 316 | |
| 317 | OpenSSL_add_all_algorithms(); |
| 318 | OpenSSL_add_all_digests(); |
| 319 | OpenSSL_add_all_ciphers(); |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 320 | #endif |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 325 | static int rsa_engine_init(const char *engine_id, ENGINE **pe) |
| 326 | { |
| 327 | ENGINE *e; |
| 328 | int ret; |
| 329 | |
| 330 | ENGINE_load_builtin_engines(); |
| 331 | |
| 332 | e = ENGINE_by_id(engine_id); |
| 333 | if (!e) { |
| 334 | fprintf(stderr, "Engine isn't available\n"); |
| 335 | ret = -1; |
| 336 | goto err_engine_by_id; |
| 337 | } |
| 338 | |
| 339 | if (!ENGINE_init(e)) { |
| 340 | fprintf(stderr, "Couldn't initialize engine\n"); |
| 341 | ret = -1; |
| 342 | goto err_engine_init; |
| 343 | } |
| 344 | |
| 345 | if (!ENGINE_set_default_RSA(e)) { |
| 346 | fprintf(stderr, "Couldn't set engine as default for RSA\n"); |
| 347 | ret = -1; |
| 348 | goto err_set_rsa; |
| 349 | } |
| 350 | |
| 351 | *pe = e; |
| 352 | |
| 353 | return 0; |
| 354 | |
| 355 | err_set_rsa: |
| 356 | ENGINE_finish(e); |
| 357 | err_engine_init: |
| 358 | ENGINE_free(e); |
| 359 | err_engine_by_id: |
Caliph Nomble | 1a31b02 | 2018-07-25 22:13:03 -0400 | [diff] [blame] | 360 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
| 361 | (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 362 | ENGINE_cleanup(); |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 363 | #endif |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 364 | return ret; |
| 365 | } |
| 366 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 367 | static void rsa_remove(void) |
| 368 | { |
Caliph Nomble | 1a31b02 | 2018-07-25 22:13:03 -0400 | [diff] [blame] | 369 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
| 370 | (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 371 | CRYPTO_cleanup_all_ex_data(); |
| 372 | ERR_free_strings(); |
| 373 | #ifdef HAVE_ERR_REMOVE_THREAD_STATE |
| 374 | ERR_remove_thread_state(NULL); |
| 375 | #else |
| 376 | ERR_remove_state(0); |
| 377 | #endif |
| 378 | EVP_cleanup(); |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 379 | #endif |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 380 | } |
| 381 | |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 382 | static void rsa_engine_remove(ENGINE *e) |
| 383 | { |
| 384 | if (e) { |
| 385 | ENGINE_finish(e); |
| 386 | ENGINE_free(e); |
| 387 | } |
| 388 | } |
| 389 | |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 390 | static int rsa_sign_with_key(RSA *rsa, struct padding_algo *padding_algo, |
| 391 | struct checksum_algo *checksum_algo, |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 392 | const struct image_region region[], int region_count, |
| 393 | uint8_t **sigp, uint *sig_size) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 394 | { |
| 395 | EVP_PKEY *key; |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 396 | EVP_PKEY_CTX *ckey; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 397 | EVP_MD_CTX *context; |
Philippe Reynes | 8161ff8 | 2018-11-14 13:50:59 +0100 | [diff] [blame] | 398 | int ret = 0; |
| 399 | size_t size; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 400 | uint8_t *sig; |
| 401 | int i; |
| 402 | |
| 403 | key = EVP_PKEY_new(); |
| 404 | if (!key) |
| 405 | return rsa_err("EVP_PKEY object creation failed"); |
| 406 | |
| 407 | if (!EVP_PKEY_set1_RSA(key, rsa)) { |
| 408 | ret = rsa_err("EVP key setup failed"); |
| 409 | goto err_set; |
| 410 | } |
| 411 | |
| 412 | size = EVP_PKEY_size(key); |
| 413 | sig = malloc(size); |
| 414 | if (!sig) { |
Philippe Reynes | 8161ff8 | 2018-11-14 13:50:59 +0100 | [diff] [blame] | 415 | fprintf(stderr, "Out of memory for signature (%zu bytes)\n", |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 416 | size); |
| 417 | ret = -ENOMEM; |
| 418 | goto err_alloc; |
| 419 | } |
| 420 | |
| 421 | context = EVP_MD_CTX_create(); |
| 422 | if (!context) { |
| 423 | ret = rsa_err("EVP context creation failed"); |
| 424 | goto err_create; |
| 425 | } |
| 426 | EVP_MD_CTX_init(context); |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 427 | |
| 428 | ckey = EVP_PKEY_CTX_new(key, NULL); |
| 429 | if (!ckey) { |
| 430 | ret = rsa_err("EVP key context creation failed"); |
| 431 | goto err_create; |
| 432 | } |
| 433 | |
| 434 | if (EVP_DigestSignInit(context, &ckey, |
Philippe Reynes | 8161ff8 | 2018-11-14 13:50:59 +0100 | [diff] [blame] | 435 | checksum_algo->calculate_sign(), |
| 436 | NULL, key) <= 0) { |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 437 | ret = rsa_err("Signer setup failed"); |
| 438 | goto err_sign; |
| 439 | } |
| 440 | |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 441 | #ifdef CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT |
| 442 | if (padding_algo && !strcmp(padding_algo->name, "pss")) { |
| 443 | if (EVP_PKEY_CTX_set_rsa_padding(ckey, |
| 444 | RSA_PKCS1_PSS_PADDING) <= 0) { |
| 445 | ret = rsa_err("Signer padding setup failed"); |
| 446 | goto err_sign; |
| 447 | } |
| 448 | } |
| 449 | #endif /* CONFIG_FIT_ENABLE_RSASSA_PSS_SUPPORT */ |
| 450 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 451 | for (i = 0; i < region_count; i++) { |
Philippe Reynes | 8161ff8 | 2018-11-14 13:50:59 +0100 | [diff] [blame] | 452 | if (!EVP_DigestSignUpdate(context, region[i].data, |
| 453 | region[i].size)) { |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 454 | ret = rsa_err("Signing data failed"); |
| 455 | goto err_sign; |
| 456 | } |
| 457 | } |
| 458 | |
Philippe Reynes | 8161ff8 | 2018-11-14 13:50:59 +0100 | [diff] [blame] | 459 | if (!EVP_DigestSignFinal(context, sig, &size)) { |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 460 | ret = rsa_err("Could not obtain signature"); |
| 461 | goto err_sign; |
| 462 | } |
Philippe Reynes | 8161ff8 | 2018-11-14 13:50:59 +0100 | [diff] [blame] | 463 | |
Caliph Nomble | 1a31b02 | 2018-07-25 22:13:03 -0400 | [diff] [blame] | 464 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
| 465 | (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL) |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 466 | EVP_MD_CTX_cleanup(context); |
| 467 | #else |
| 468 | EVP_MD_CTX_reset(context); |
| 469 | #endif |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 470 | EVP_MD_CTX_destroy(context); |
| 471 | EVP_PKEY_free(key); |
| 472 | |
Philippe Reynes | 8161ff8 | 2018-11-14 13:50:59 +0100 | [diff] [blame] | 473 | debug("Got signature: %d bytes, expected %zu\n", *sig_size, size); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 474 | *sigp = sig; |
| 475 | *sig_size = size; |
| 476 | |
| 477 | return 0; |
| 478 | |
| 479 | err_sign: |
| 480 | EVP_MD_CTX_destroy(context); |
| 481 | err_create: |
| 482 | free(sig); |
| 483 | err_alloc: |
| 484 | err_set: |
| 485 | EVP_PKEY_free(key); |
| 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | int rsa_sign(struct image_sign_info *info, |
| 490 | const struct image_region region[], int region_count, |
| 491 | uint8_t **sigp, uint *sig_len) |
| 492 | { |
| 493 | RSA *rsa; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 494 | ENGINE *e = NULL; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 495 | int ret; |
| 496 | |
| 497 | ret = rsa_init(); |
| 498 | if (ret) |
| 499 | return ret; |
| 500 | |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 501 | if (info->engine_id) { |
| 502 | ret = rsa_engine_init(info->engine_id, &e); |
| 503 | if (ret) |
| 504 | goto err_engine; |
| 505 | } |
| 506 | |
| 507 | ret = rsa_get_priv_key(info->keydir, info->keyname, e, &rsa); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 508 | if (ret) |
| 509 | goto err_priv; |
Philippe Reynes | 1246835 | 2018-11-14 13:51:00 +0100 | [diff] [blame] | 510 | ret = rsa_sign_with_key(rsa, info->padding, info->checksum, region, |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 511 | region_count, sigp, sig_len); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 512 | if (ret) |
| 513 | goto err_sign; |
| 514 | |
| 515 | RSA_free(rsa); |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 516 | if (info->engine_id) |
| 517 | rsa_engine_remove(e); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 518 | rsa_remove(); |
| 519 | |
| 520 | return ret; |
| 521 | |
| 522 | err_sign: |
| 523 | RSA_free(rsa); |
| 524 | err_priv: |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 525 | if (info->engine_id) |
| 526 | rsa_engine_remove(e); |
| 527 | err_engine: |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 528 | rsa_remove(); |
| 529 | return ret; |
| 530 | } |
| 531 | |
| 532 | /* |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 533 | * rsa_get_exponent(): - Get the public exponent from an RSA key |
| 534 | */ |
| 535 | static int rsa_get_exponent(RSA *key, uint64_t *e) |
| 536 | { |
| 537 | int ret; |
| 538 | BIGNUM *bn_te; |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 539 | const BIGNUM *key_e; |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 540 | uint64_t te; |
| 541 | |
| 542 | ret = -EINVAL; |
| 543 | bn_te = NULL; |
| 544 | |
| 545 | if (!e) |
| 546 | goto cleanup; |
| 547 | |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 548 | RSA_get0_key(key, NULL, &key_e, NULL); |
| 549 | if (BN_num_bits(key_e) > 64) |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 550 | goto cleanup; |
| 551 | |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 552 | *e = BN_get_word(key_e); |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 553 | |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 554 | if (BN_num_bits(key_e) < 33) { |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 555 | ret = 0; |
| 556 | goto cleanup; |
| 557 | } |
| 558 | |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 559 | bn_te = BN_dup(key_e); |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 560 | if (!bn_te) |
| 561 | goto cleanup; |
| 562 | |
| 563 | if (!BN_rshift(bn_te, bn_te, 32)) |
| 564 | goto cleanup; |
| 565 | |
| 566 | if (!BN_mask_bits(bn_te, 32)) |
| 567 | goto cleanup; |
| 568 | |
| 569 | te = BN_get_word(bn_te); |
| 570 | te <<= 32; |
| 571 | *e |= te; |
| 572 | ret = 0; |
| 573 | |
| 574 | cleanup: |
| 575 | if (bn_te) |
| 576 | BN_free(bn_te); |
| 577 | |
| 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | /* |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 582 | * rsa_get_params(): - Get the important parameters of an RSA public key |
| 583 | */ |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 584 | int rsa_get_params(RSA *key, uint64_t *exponent, uint32_t *n0_invp, |
| 585 | BIGNUM **modulusp, BIGNUM **r_squaredp) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 586 | { |
| 587 | BIGNUM *big1, *big2, *big32, *big2_32; |
| 588 | BIGNUM *n, *r, *r_squared, *tmp; |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 589 | const BIGNUM *key_n; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 590 | BN_CTX *bn_ctx = BN_CTX_new(); |
| 591 | int ret = 0; |
| 592 | |
| 593 | /* Initialize BIGNUMs */ |
| 594 | big1 = BN_new(); |
| 595 | big2 = BN_new(); |
| 596 | big32 = BN_new(); |
| 597 | r = BN_new(); |
| 598 | r_squared = BN_new(); |
| 599 | tmp = BN_new(); |
| 600 | big2_32 = BN_new(); |
| 601 | n = BN_new(); |
| 602 | if (!big1 || !big2 || !big32 || !r || !r_squared || !tmp || !big2_32 || |
| 603 | !n) { |
| 604 | fprintf(stderr, "Out of memory (bignum)\n"); |
| 605 | return -ENOMEM; |
| 606 | } |
| 607 | |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 608 | if (0 != rsa_get_exponent(key, exponent)) |
| 609 | ret = -1; |
| 610 | |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 611 | RSA_get0_key(key, &key_n, NULL, NULL); |
| 612 | if (!BN_copy(n, key_n) || !BN_set_word(big1, 1L) || |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 613 | !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L)) |
| 614 | ret = -1; |
| 615 | |
| 616 | /* big2_32 = 2^32 */ |
| 617 | if (!BN_exp(big2_32, big2, big32, bn_ctx)) |
| 618 | ret = -1; |
| 619 | |
| 620 | /* Calculate n0_inv = -1 / n[0] mod 2^32 */ |
| 621 | if (!BN_mod_inverse(tmp, n, big2_32, bn_ctx) || |
| 622 | !BN_sub(tmp, big2_32, tmp)) |
| 623 | ret = -1; |
| 624 | *n0_invp = BN_get_word(tmp); |
| 625 | |
| 626 | /* Calculate R = 2^(# of key bits) */ |
| 627 | if (!BN_set_word(tmp, BN_num_bits(n)) || |
| 628 | !BN_exp(r, big2, tmp, bn_ctx)) |
| 629 | ret = -1; |
| 630 | |
| 631 | /* Calculate r_squared = R^2 mod n */ |
| 632 | if (!BN_copy(r_squared, r) || |
| 633 | !BN_mul(tmp, r_squared, r, bn_ctx) || |
| 634 | !BN_mod(r_squared, tmp, n, bn_ctx)) |
| 635 | ret = -1; |
| 636 | |
| 637 | *modulusp = n; |
| 638 | *r_squaredp = r_squared; |
| 639 | |
| 640 | BN_free(big1); |
| 641 | BN_free(big2); |
| 642 | BN_free(big32); |
| 643 | BN_free(r); |
| 644 | BN_free(tmp); |
| 645 | BN_free(big2_32); |
| 646 | if (ret) { |
| 647 | fprintf(stderr, "Bignum operations failed\n"); |
| 648 | return -ENOMEM; |
| 649 | } |
| 650 | |
| 651 | return ret; |
| 652 | } |
| 653 | |
| 654 | static int fdt_add_bignum(void *blob, int noffset, const char *prop_name, |
| 655 | BIGNUM *num, int num_bits) |
| 656 | { |
| 657 | int nwords = num_bits / 32; |
| 658 | int size; |
| 659 | uint32_t *buf, *ptr; |
| 660 | BIGNUM *tmp, *big2, *big32, *big2_32; |
| 661 | BN_CTX *ctx; |
| 662 | int ret; |
| 663 | |
| 664 | tmp = BN_new(); |
| 665 | big2 = BN_new(); |
| 666 | big32 = BN_new(); |
| 667 | big2_32 = BN_new(); |
Simon Glass | 198b80c | 2018-06-12 00:05:00 -0600 | [diff] [blame] | 668 | |
| 669 | /* |
| 670 | * Note: This code assumes that all of the above succeed, or all fail. |
| 671 | * In practice memory allocations generally do not fail (unless the |
| 672 | * process is killed), so it does not seem worth handling each of these |
| 673 | * as a separate case. Technicaly this could leak memory on failure, |
| 674 | * but a) it won't happen in practice, and b) it doesn't matter as we |
| 675 | * will immediately exit with a failure code. |
| 676 | */ |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 677 | if (!tmp || !big2 || !big32 || !big2_32) { |
| 678 | fprintf(stderr, "Out of memory (bignum)\n"); |
| 679 | return -ENOMEM; |
| 680 | } |
| 681 | ctx = BN_CTX_new(); |
| 682 | if (!tmp) { |
| 683 | fprintf(stderr, "Out of memory (bignum context)\n"); |
| 684 | return -ENOMEM; |
| 685 | } |
| 686 | BN_set_word(big2, 2L); |
| 687 | BN_set_word(big32, 32L); |
| 688 | BN_exp(big2_32, big2, big32, ctx); /* B = 2^32 */ |
| 689 | |
| 690 | size = nwords * sizeof(uint32_t); |
| 691 | buf = malloc(size); |
| 692 | if (!buf) { |
| 693 | fprintf(stderr, "Out of memory (%d bytes)\n", size); |
| 694 | return -ENOMEM; |
| 695 | } |
| 696 | |
| 697 | /* Write out modulus as big endian array of integers */ |
| 698 | for (ptr = buf + nwords - 1; ptr >= buf; ptr--) { |
| 699 | BN_mod(tmp, num, big2_32, ctx); /* n = N mod B */ |
| 700 | *ptr = cpu_to_fdt32(BN_get_word(tmp)); |
| 701 | BN_rshift(num, num, 32); /* N = N/B */ |
| 702 | } |
| 703 | |
mario.six@gdsys.cc | dc4053a | 2016-07-22 08:58:40 +0200 | [diff] [blame] | 704 | /* |
| 705 | * We try signing with successively increasing size values, so this |
| 706 | * might fail several times |
| 707 | */ |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 708 | ret = fdt_setprop(blob, noffset, prop_name, buf, size); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 709 | free(buf); |
| 710 | BN_free(tmp); |
| 711 | BN_free(big2); |
| 712 | BN_free(big32); |
| 713 | BN_free(big2_32); |
| 714 | |
Simon Glass | 198b80c | 2018-06-12 00:05:00 -0600 | [diff] [blame] | 715 | return ret ? -FDT_ERR_NOSPACE : 0; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | int rsa_add_verify_data(struct image_sign_info *info, void *keydest) |
| 719 | { |
| 720 | BIGNUM *modulus, *r_squared; |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 721 | uint64_t exponent; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 722 | uint32_t n0_inv; |
| 723 | int parent, node; |
| 724 | char name[100]; |
| 725 | int ret; |
| 726 | int bits; |
| 727 | RSA *rsa; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 728 | ENGINE *e = NULL; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 729 | |
| 730 | debug("%s: Getting verification data\n", __func__); |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 731 | if (info->engine_id) { |
| 732 | ret = rsa_engine_init(info->engine_id, &e); |
| 733 | if (ret) |
| 734 | return ret; |
| 735 | } |
| 736 | ret = rsa_get_pub_key(info->keydir, info->keyname, e, &rsa); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 737 | if (ret) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 738 | goto err_get_pub_key; |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 739 | ret = rsa_get_params(rsa, &exponent, &n0_inv, &modulus, &r_squared); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 740 | if (ret) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 741 | goto err_get_params; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 742 | bits = BN_num_bits(modulus); |
| 743 | parent = fdt_subnode_offset(keydest, 0, FIT_SIG_NODENAME); |
| 744 | if (parent == -FDT_ERR_NOTFOUND) { |
| 745 | parent = fdt_add_subnode(keydest, 0, FIT_SIG_NODENAME); |
| 746 | if (parent < 0) { |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 747 | ret = parent; |
| 748 | if (ret != -FDT_ERR_NOSPACE) { |
| 749 | fprintf(stderr, "Couldn't create signature node: %s\n", |
| 750 | fdt_strerror(parent)); |
| 751 | } |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 752 | } |
| 753 | } |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 754 | if (ret) |
| 755 | goto done; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 756 | |
| 757 | /* Either create or overwrite the named key node */ |
| 758 | snprintf(name, sizeof(name), "key-%s", info->keyname); |
| 759 | node = fdt_subnode_offset(keydest, parent, name); |
| 760 | if (node == -FDT_ERR_NOTFOUND) { |
| 761 | node = fdt_add_subnode(keydest, parent, name); |
| 762 | if (node < 0) { |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 763 | ret = node; |
| 764 | if (ret != -FDT_ERR_NOSPACE) { |
| 765 | fprintf(stderr, "Could not create key subnode: %s\n", |
| 766 | fdt_strerror(node)); |
| 767 | } |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 768 | } |
| 769 | } else if (node < 0) { |
| 770 | fprintf(stderr, "Cannot select keys parent: %s\n", |
| 771 | fdt_strerror(node)); |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 772 | ret = node; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 775 | if (!ret) { |
| 776 | ret = fdt_setprop_string(keydest, node, "key-name-hint", |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 777 | info->keyname); |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 778 | } |
Simon Glass | af2f9d5 | 2014-06-02 22:04:51 -0600 | [diff] [blame] | 779 | if (!ret) |
| 780 | ret = fdt_setprop_u32(keydest, node, "rsa,num-bits", bits); |
| 781 | if (!ret) |
| 782 | ret = fdt_setprop_u32(keydest, node, "rsa,n0-inverse", n0_inv); |
| 783 | if (!ret) { |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 784 | ret = fdt_setprop_u64(keydest, node, "rsa,exponent", exponent); |
| 785 | } |
| 786 | if (!ret) { |
Simon Glass | af2f9d5 | 2014-06-02 22:04:51 -0600 | [diff] [blame] | 787 | ret = fdt_add_bignum(keydest, node, "rsa,modulus", modulus, |
| 788 | bits); |
| 789 | } |
| 790 | if (!ret) { |
| 791 | ret = fdt_add_bignum(keydest, node, "rsa,r-squared", r_squared, |
| 792 | bits); |
| 793 | } |
| 794 | if (!ret) { |
| 795 | ret = fdt_setprop_string(keydest, node, FIT_ALGO_PROP, |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 796 | info->name); |
Simon Glass | af2f9d5 | 2014-06-02 22:04:51 -0600 | [diff] [blame] | 797 | } |
mario.six@gdsys.cc | 99fc6ea | 2016-07-19 11:07:07 +0200 | [diff] [blame] | 798 | if (!ret && info->require_keys) { |
Simon Glass | af2f9d5 | 2014-06-02 22:04:51 -0600 | [diff] [blame] | 799 | ret = fdt_setprop_string(keydest, node, "required", |
| 800 | info->require_keys); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 801 | } |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 802 | done: |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 803 | BN_free(modulus); |
| 804 | BN_free(r_squared); |
| 805 | if (ret) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 806 | ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO; |
| 807 | err_get_params: |
| 808 | RSA_free(rsa); |
| 809 | err_get_pub_key: |
| 810 | if (info->engine_id) |
| 811 | rsa_engine_remove(e); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 812 | |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 813 | return ret; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 814 | } |