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