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 | } |
Loic Poulain | ec596ed | 2024-10-31 10:15:31 +0100 | [diff] [blame] | 431 | |
| 432 | /* Per RFC 3447 (and convention) the Typical salt length is the |
| 433 | * length of the output of the digest algorithm. |
| 434 | */ |
| 435 | if (EVP_PKEY_CTX_set_rsa_pss_saltlen(ckey, |
| 436 | checksum_algo->checksum_len) <= 0) { |
| 437 | ret = rsa_err("Signer salt length setup failed"); |
| 438 | goto err_sign; |
| 439 | } |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 440 | } |
Philippe Reynes | 47d73f0 | 2018-11-14 13:51:01 +0100 | [diff] [blame] | 441 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 442 | for (i = 0; i < region_count; i++) { |
Philippe Reynes | 8161ff8 | 2018-11-14 13:50:59 +0100 | [diff] [blame] | 443 | if (!EVP_DigestSignUpdate(context, region[i].data, |
| 444 | region[i].size)) { |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 445 | ret = rsa_err("Signing data failed"); |
| 446 | goto err_sign; |
| 447 | } |
| 448 | } |
| 449 | |
Philippe Reynes | 8161ff8 | 2018-11-14 13:50:59 +0100 | [diff] [blame] | 450 | if (!EVP_DigestSignFinal(context, sig, &size)) { |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 451 | ret = rsa_err("Could not obtain signature"); |
| 452 | goto err_sign; |
| 453 | } |
Philippe Reynes | 8161ff8 | 2018-11-14 13:50:59 +0100 | [diff] [blame] | 454 | |
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 | |
Chan, Donald | 9ccd605 | 2021-07-19 09:18:54 -0700 | [diff] [blame] | 457 | 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] | 458 | *sigp = sig; |
| 459 | *sig_size = size; |
| 460 | |
| 461 | return 0; |
| 462 | |
| 463 | err_sign: |
Yann Droneaud | 68ca280 | 2022-03-01 16:12:34 +0100 | [diff] [blame] | 464 | EVP_MD_CTX_free(context); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 465 | err_create: |
| 466 | free(sig); |
| 467 | err_alloc: |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | int rsa_sign(struct image_sign_info *info, |
| 472 | const struct image_region region[], int region_count, |
| 473 | uint8_t **sigp, uint *sig_len) |
| 474 | { |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 475 | EVP_PKEY *pkey = NULL; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 476 | ENGINE *e = NULL; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 477 | int ret; |
| 478 | |
| 479 | ret = rsa_init(); |
| 480 | if (ret) |
| 481 | return ret; |
| 482 | |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 483 | if (info->engine_id) { |
| 484 | ret = rsa_engine_init(info->engine_id, &e); |
| 485 | if (ret) |
Alexandru Gagniuc | b63dc08 | 2021-07-29 13:31:21 -0500 | [diff] [blame] | 486 | return ret; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 487 | } |
| 488 | |
Alexandru Gagniuc | a35da20 | 2021-02-19 12:45:18 -0600 | [diff] [blame] | 489 | ret = rsa_get_priv_key(info->keydir, info->keyname, info->keyfile, |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 490 | e, &pkey); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 491 | if (ret) |
| 492 | goto err_priv; |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 493 | ret = rsa_sign_with_key(pkey, info->padding, info->checksum, region, |
Heiko Schocher | edaf9b1 | 2014-03-03 12:19:26 +0100 | [diff] [blame] | 494 | region_count, sigp, sig_len); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 495 | if (ret) |
| 496 | goto err_sign; |
| 497 | |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 498 | EVP_PKEY_free(pkey); |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 499 | if (info->engine_id) |
| 500 | rsa_engine_remove(e); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 501 | |
| 502 | return ret; |
| 503 | |
| 504 | err_sign: |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 505 | EVP_PKEY_free(pkey); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 506 | err_priv: |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 507 | if (info->engine_id) |
| 508 | rsa_engine_remove(e); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | /* |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 513 | * rsa_get_exponent(): - Get the public exponent from an RSA key |
| 514 | */ |
| 515 | static int rsa_get_exponent(RSA *key, uint64_t *e) |
| 516 | { |
| 517 | int ret; |
| 518 | BIGNUM *bn_te; |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 519 | const BIGNUM *key_e; |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 520 | uint64_t te; |
| 521 | |
| 522 | ret = -EINVAL; |
| 523 | bn_te = NULL; |
| 524 | |
| 525 | if (!e) |
| 526 | goto cleanup; |
| 527 | |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 528 | RSA_get0_key(key, NULL, &key_e, NULL); |
| 529 | if (BN_num_bits(key_e) > 64) |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 530 | goto cleanup; |
| 531 | |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 532 | *e = BN_get_word(key_e); |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 533 | |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 534 | if (BN_num_bits(key_e) < 33) { |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 535 | ret = 0; |
| 536 | goto cleanup; |
| 537 | } |
| 538 | |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 539 | bn_te = BN_dup(key_e); |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 540 | if (!bn_te) |
| 541 | goto cleanup; |
| 542 | |
| 543 | if (!BN_rshift(bn_te, bn_te, 32)) |
| 544 | goto cleanup; |
| 545 | |
| 546 | if (!BN_mask_bits(bn_te, 32)) |
| 547 | goto cleanup; |
| 548 | |
| 549 | te = BN_get_word(bn_te); |
| 550 | te <<= 32; |
| 551 | *e |= te; |
| 552 | ret = 0; |
| 553 | |
| 554 | cleanup: |
| 555 | if (bn_te) |
| 556 | BN_free(bn_te); |
| 557 | |
| 558 | return ret; |
| 559 | } |
| 560 | |
| 561 | /* |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 562 | * rsa_get_params(): - Get the important parameters of an RSA public key |
| 563 | */ |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 564 | int rsa_get_params(RSA *key, uint64_t *exponent, uint32_t *n0_invp, |
| 565 | BIGNUM **modulusp, BIGNUM **r_squaredp) |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 566 | { |
| 567 | BIGNUM *big1, *big2, *big32, *big2_32; |
| 568 | BIGNUM *n, *r, *r_squared, *tmp; |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 569 | const BIGNUM *key_n; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 570 | BN_CTX *bn_ctx = BN_CTX_new(); |
| 571 | int ret = 0; |
| 572 | |
| 573 | /* Initialize BIGNUMs */ |
| 574 | big1 = BN_new(); |
| 575 | big2 = BN_new(); |
| 576 | big32 = BN_new(); |
| 577 | r = BN_new(); |
| 578 | r_squared = BN_new(); |
| 579 | tmp = BN_new(); |
| 580 | big2_32 = BN_new(); |
| 581 | n = BN_new(); |
| 582 | if (!big1 || !big2 || !big32 || !r || !r_squared || !tmp || !big2_32 || |
| 583 | !n) { |
| 584 | fprintf(stderr, "Out of memory (bignum)\n"); |
| 585 | return -ENOMEM; |
| 586 | } |
| 587 | |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 588 | if (0 != rsa_get_exponent(key, exponent)) |
| 589 | ret = -1; |
| 590 | |
Jelle van der Waa | e980590 | 2017-05-08 21:31:19 +0200 | [diff] [blame] | 591 | RSA_get0_key(key, &key_n, NULL, NULL); |
| 592 | if (!BN_copy(n, key_n) || !BN_set_word(big1, 1L) || |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 593 | !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L)) |
| 594 | ret = -1; |
| 595 | |
| 596 | /* big2_32 = 2^32 */ |
| 597 | if (!BN_exp(big2_32, big2, big32, bn_ctx)) |
| 598 | ret = -1; |
| 599 | |
| 600 | /* Calculate n0_inv = -1 / n[0] mod 2^32 */ |
| 601 | if (!BN_mod_inverse(tmp, n, big2_32, bn_ctx) || |
| 602 | !BN_sub(tmp, big2_32, tmp)) |
| 603 | ret = -1; |
| 604 | *n0_invp = BN_get_word(tmp); |
| 605 | |
| 606 | /* Calculate R = 2^(# of key bits) */ |
| 607 | if (!BN_set_word(tmp, BN_num_bits(n)) || |
| 608 | !BN_exp(r, big2, tmp, bn_ctx)) |
| 609 | ret = -1; |
| 610 | |
| 611 | /* Calculate r_squared = R^2 mod n */ |
| 612 | if (!BN_copy(r_squared, r) || |
| 613 | !BN_mul(tmp, r_squared, r, bn_ctx) || |
| 614 | !BN_mod(r_squared, tmp, n, bn_ctx)) |
| 615 | ret = -1; |
| 616 | |
| 617 | *modulusp = n; |
| 618 | *r_squaredp = r_squared; |
| 619 | |
| 620 | BN_free(big1); |
| 621 | BN_free(big2); |
| 622 | BN_free(big32); |
| 623 | BN_free(r); |
| 624 | BN_free(tmp); |
| 625 | BN_free(big2_32); |
| 626 | if (ret) { |
| 627 | fprintf(stderr, "Bignum operations failed\n"); |
| 628 | return -ENOMEM; |
| 629 | } |
| 630 | |
| 631 | return ret; |
| 632 | } |
| 633 | |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 634 | int rsa_add_verify_data(struct image_sign_info *info, void *keydest) |
| 635 | { |
| 636 | BIGNUM *modulus, *r_squared; |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 637 | uint64_t exponent; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 638 | uint32_t n0_inv; |
Haijun Qin | 811863c | 2022-12-06 15:41:37 +0800 | [diff] [blame] | 639 | int parent, node = -FDT_ERR_NOTFOUND; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 640 | char name[100]; |
| 641 | int ret; |
| 642 | int bits; |
| 643 | RSA *rsa; |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 644 | EVP_PKEY *pkey = NULL; |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 645 | ENGINE *e = NULL; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 646 | |
| 647 | debug("%s: Getting verification data\n", __func__); |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 648 | if (info->engine_id) { |
| 649 | ret = rsa_engine_init(info->engine_id, &e); |
| 650 | if (ret) |
| 651 | return ret; |
| 652 | } |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 653 | ret = rsa_get_pub_key(info->keydir, info->keyname, e, &pkey); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 654 | if (ret) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 655 | goto err_get_pub_key; |
Alexandru Gagniuc | b63dc08 | 2021-07-29 13:31:21 -0500 | [diff] [blame] | 656 | |
Heinrich Schuchardt | dccc9da | 2022-01-09 15:39:40 +0100 | [diff] [blame] | 657 | rsa = (RSA *)EVP_PKEY_get0_RSA(pkey); |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 658 | ret = rsa_get_params(rsa, &exponent, &n0_inv, &modulus, &r_squared); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 659 | if (ret) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 660 | goto err_get_params; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 661 | bits = BN_num_bits(modulus); |
| 662 | parent = fdt_subnode_offset(keydest, 0, FIT_SIG_NODENAME); |
| 663 | if (parent == -FDT_ERR_NOTFOUND) { |
| 664 | parent = fdt_add_subnode(keydest, 0, FIT_SIG_NODENAME); |
| 665 | if (parent < 0) { |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 666 | ret = parent; |
| 667 | if (ret != -FDT_ERR_NOSPACE) { |
| 668 | fprintf(stderr, "Couldn't create signature node: %s\n", |
| 669 | fdt_strerror(parent)); |
| 670 | } |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 671 | } |
| 672 | } |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 673 | if (ret) |
| 674 | goto done; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 675 | |
| 676 | /* Either create or overwrite the named key node */ |
| 677 | snprintf(name, sizeof(name), "key-%s", info->keyname); |
| 678 | node = fdt_subnode_offset(keydest, parent, name); |
| 679 | if (node == -FDT_ERR_NOTFOUND) { |
| 680 | node = fdt_add_subnode(keydest, parent, name); |
| 681 | if (node < 0) { |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 682 | ret = node; |
| 683 | if (ret != -FDT_ERR_NOSPACE) { |
| 684 | fprintf(stderr, "Could not create key subnode: %s\n", |
| 685 | fdt_strerror(node)); |
| 686 | } |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 687 | } |
| 688 | } else if (node < 0) { |
| 689 | fprintf(stderr, "Cannot select keys parent: %s\n", |
| 690 | fdt_strerror(node)); |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 691 | ret = node; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 694 | if (!ret) { |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 695 | ret = fdt_setprop_string(keydest, node, FIT_KEY_HINT, |
| 696 | info->keyname); |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 697 | } |
Simon Glass | af2f9d5 | 2014-06-02 22:04:51 -0600 | [diff] [blame] | 698 | if (!ret) |
| 699 | ret = fdt_setprop_u32(keydest, node, "rsa,num-bits", bits); |
| 700 | if (!ret) |
| 701 | ret = fdt_setprop_u32(keydest, node, "rsa,n0-inverse", n0_inv); |
| 702 | if (!ret) { |
Michael van der Westhuizen | 89f4ed1 | 2014-07-02 10:17:26 +0200 | [diff] [blame] | 703 | ret = fdt_setprop_u64(keydest, node, "rsa,exponent", exponent); |
| 704 | } |
| 705 | if (!ret) { |
Simon Glass | af2f9d5 | 2014-06-02 22:04:51 -0600 | [diff] [blame] | 706 | ret = fdt_add_bignum(keydest, node, "rsa,modulus", modulus, |
| 707 | bits); |
| 708 | } |
| 709 | if (!ret) { |
| 710 | ret = fdt_add_bignum(keydest, node, "rsa,r-squared", r_squared, |
| 711 | bits); |
| 712 | } |
| 713 | if (!ret) { |
| 714 | ret = fdt_setprop_string(keydest, node, FIT_ALGO_PROP, |
Andrew Duda | 6616c82 | 2016-11-08 18:53:41 +0000 | [diff] [blame] | 715 | info->name); |
Simon Glass | af2f9d5 | 2014-06-02 22:04:51 -0600 | [diff] [blame] | 716 | } |
mario.six@gdsys.cc | 99fc6ea | 2016-07-19 11:07:07 +0200 | [diff] [blame] | 717 | if (!ret && info->require_keys) { |
Simon Glass | d7aabcc | 2020-03-18 11:44:06 -0600 | [diff] [blame] | 718 | ret = fdt_setprop_string(keydest, node, FIT_KEY_REQUIRED, |
Simon Glass | af2f9d5 | 2014-06-02 22:04:51 -0600 | [diff] [blame] | 719 | info->require_keys); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 720 | } |
Simon Glass | 9423bad | 2014-06-12 07:24:42 -0600 | [diff] [blame] | 721 | done: |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 722 | BN_free(modulus); |
| 723 | BN_free(r_squared); |
| 724 | if (ret) |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 725 | ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO; |
| 726 | err_get_params: |
Chan, Donald | 0bdd4ee | 2021-04-01 22:42:36 +0000 | [diff] [blame] | 727 | EVP_PKEY_free(pkey); |
George McCollister | 23d1489 | 2017-01-06 13:14:17 -0600 | [diff] [blame] | 728 | err_get_pub_key: |
| 729 | if (info->engine_id) |
| 730 | rsa_engine_remove(e); |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 731 | |
Simon Glass | 94336dc | 2021-11-12 12:28:11 -0700 | [diff] [blame] | 732 | if (ret) |
| 733 | return ret; |
| 734 | |
| 735 | return node; |
Simon Glass | 35191a3 | 2013-06-13 15:10:02 -0700 | [diff] [blame] | 736 | } |