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