blob: c64deac31f4868912a7086c22cb776db375efd2e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass19c402a2013-06-13 15:10:02 -07002/*
3 * Copyright (c) 2013, Google Inc.
Simon Glass19c402a2013-06-13 15:10:02 -07004 */
5
6#include "mkimage.h"
Jonathan Gray0ff042d2020-05-15 22:20:34 +10007#include <stdlib.h>
Simon Glass19c402a2013-06-13 15:10:02 -07008#include <stdio.h>
9#include <string.h>
Simon Glass19c402a2013-06-13 15:10:02 -070010#include <image.h>
11#include <time.h>
Alexandru Gagniuc4c17e5f2021-02-19 12:45:11 -060012#include <u-boot/fdt-libcrypto.h>
Jelle van der Waac3b43282017-05-08 21:31:19 +020013#include <openssl/bn.h>
Chan, Donaldfbc77742021-04-01 22:42:36 +000014#include <openssl/ec.h>
Simon Glass19c402a2013-06-13 15:10:02 -070015#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 McCollisterf1ca1fd2017-01-06 13:14:17 -060020#include <openssl/engine.h>
Simon Glass19c402a2013-06-13 15:10:02 -070021
22#if OPENSSL_VERSION_NUMBER >= 0x10000000L
23#define HAVE_ERR_REMOVE_THREAD_STATE
24#endif
25
Caliph Nomble7ac1a432018-07-25 22:13:03 -040026#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
27 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
Jelle van der Waac3b43282017-05-08 21:31:19 +020028static 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 Glass19c402a2013-06-13 15:10:02 -070040static 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 McCollisterf1ca1fd2017-01-06 13:14:17 -060052 * rsa_pem_get_pub_key() - read a public key from a .crt file
Simon Glass19c402a2013-06-13 15:10:02 -070053 *
54 * @keydir: Directory containins the key
55 * @name Name of key file (will have a .crt extension)
Chan, Donaldfbc77742021-04-01 22:42:36 +000056 * @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 Glass19c402a2013-06-13 15:10:02 -070058 */
Chan, Donaldfbc77742021-04-01 22:42:36 +000059static int rsa_pem_get_pub_key(const char *keydir, const char *name, EVP_PKEY **evpp)
Simon Glass19c402a2013-06-13 15:10:02 -070060{
61 char path[1024];
Chan, Donaldfbc77742021-04-01 22:42:36 +000062 EVP_PKEY *key = NULL;
Simon Glass19c402a2013-06-13 15:10:02 -070063 X509 *cert;
Simon Glass19c402a2013-06-13 15:10:02 -070064 FILE *f;
65 int ret;
66
Chan, Donaldfbc77742021-04-01 22:42:36 +000067 if (!evpp)
68 return -EINVAL;
69
70 *evpp = NULL;
Simon Glass19c402a2013-06-13 15:10:02 -070071 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 Glass19c402a2013-06-13 15:10:02 -070095 fclose(f);
Chan, Donaldfbc77742021-04-01 22:42:36 +000096 *evpp = key;
Simon Glass19c402a2013-06-13 15:10:02 -070097 X509_free(cert);
Simon Glass19c402a2013-06-13 15:10:02 -070098
99 return 0;
100
Simon Glass19c402a2013-06-13 15:10:02 -0700101err_pubkey:
102 X509_free(cert);
103err_cert:
104 fclose(f);
105 return ret;
106}
107
108/**
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600109 * rsa_engine_get_pub_key() - read a public key from given engine
Simon Glass19c402a2013-06-13 15:10:02 -0700110 *
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600111 * @keydir: Key prefix
112 * @name Name of key
113 * @engine Engine to use
Chan, Donaldfbc77742021-04-01 22:42:36 +0000114 * @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 McCollisterf1ca1fd2017-01-06 13:14:17 -0600116 */
117static int rsa_engine_get_pub_key(const char *keydir, const char *name,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000118 ENGINE *engine, EVP_PKEY **evpp)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600119{
120 const char *engine_id;
121 char key_id[1024];
Chan, Donaldfbc77742021-04-01 22:42:36 +0000122 EVP_PKEY *key = NULL;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600123
Chan, Donaldfbc77742021-04-01 22:42:36 +0000124 if (!evpp)
125 return -EINVAL;
126
127 *evpp = NULL;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600128
129 engine_id = ENGINE_get_id(engine);
130
131 if (engine_id && !strcmp(engine_id, "pkcs11")) {
132 if (keydir)
Jan Luebbe24bf6e82020-05-13 12:26:24 +0200133 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 McCollisterf1ca1fd2017-01-06 13:14:17 -0600141 else
142 snprintf(key_id, sizeof(key_id),
143 "pkcs11:object=%s;type=public",
144 name);
Vesa Jääskeläinen5b123e02019-06-16 20:53:38 +0300145 } 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 McCollisterf1ca1fd2017-01-06 13:14:17 -0600154 } 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, Donaldfbc77742021-04-01 22:42:36 +0000163 *evpp = key;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600164
165 return 0;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600166}
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, Donaldfbc77742021-04-01 22:42:36 +0000174 * @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 McCollisterf1ca1fd2017-01-06 13:14:17 -0600176 */
177static int rsa_get_pub_key(const char *keydir, const char *name,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000178 ENGINE *engine, EVP_PKEY **evpp)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600179{
180 if (engine)
Chan, Donaldfbc77742021-04-01 22:42:36 +0000181 return rsa_engine_get_pub_key(keydir, name, engine, evpp);
182 return rsa_pem_get_pub_key(keydir, name, evpp);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600183}
184
185/**
186 * rsa_pem_get_priv_key() - read a private key from a .key file
187 *
188 * @keydir: Directory containing the key
Simon Glass19c402a2013-06-13 15:10:02 -0700189 * @name Name of key file (will have a .key extension)
Chan, Donaldfbc77742021-04-01 22:42:36 +0000190 * @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 Glass19c402a2013-06-13 15:10:02 -0700192 */
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600193static int rsa_pem_get_priv_key(const char *keydir, const char *name,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000194 const char *keyfile, EVP_PKEY **evpp)
Simon Glass19c402a2013-06-13 15:10:02 -0700195{
Chan, Donaldfbc77742021-04-01 22:42:36 +0000196 char path[1024] = {0};
197 FILE *f = NULL;
Simon Glass19c402a2013-06-13 15:10:02 -0700198
Chan, Donaldfbc77742021-04-01 22:42:36 +0000199 if (!evpp)
200 return -EINVAL;
201
202 *evpp = NULL;
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600203 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 Glass19c402a2013-06-13 15:10:02 -0700210 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, Donaldfbc77742021-04-01 22:42:36 +0000217 if (!PEM_read_PrivateKey(f, evpp, NULL, path)) {
Simon Glass19c402a2013-06-13 15:10:02 -0700218 rsa_err("Failure reading private key");
219 fclose(f);
220 return -EPROTO;
221 }
222 fclose(f);
Simon Glass19c402a2013-06-13 15:10:02 -0700223
224 return 0;
225}
226
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600227/**
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, Donaldfbc77742021-04-01 22:42:36 +0000233 * @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 McCollisterf1ca1fd2017-01-06 13:14:17 -0600235 */
236static int rsa_engine_get_priv_key(const char *keydir, const char *name,
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600237 const char *keyfile,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000238 ENGINE *engine, EVP_PKEY **evpp)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600239{
240 const char *engine_id;
241 char key_id[1024];
Chan, Donaldfbc77742021-04-01 22:42:36 +0000242 EVP_PKEY *key = NULL;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600243
Chan, Donaldfbc77742021-04-01 22:42:36 +0000244 if (!evpp)
245 return -EINVAL;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600246
247 engine_id = ENGINE_get_id(engine);
248
249 if (engine_id && !strcmp(engine_id, "pkcs11")) {
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600250 if (!keydir && !name) {
251 fprintf(stderr, "Please use 'keydir' with PKCS11\n");
252 return -EINVAL;
253 }
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600254 if (keydir)
Jan Luebbe24bf6e82020-05-13 12:26:24 +0200255 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 McCollisterf1ca1fd2017-01-06 13:14:17 -0600263 else
264 snprintf(key_id, sizeof(key_id),
265 "pkcs11:object=%s;type=private",
266 name);
Vesa Jääskeläinen5b123e02019-06-16 20:53:38 +0300267 } else if (engine_id) {
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600268 if (keydir && name)
Vesa Jääskeläinen5b123e02019-06-16 20:53:38 +0300269 snprintf(key_id, sizeof(key_id),
270 "%s%s",
271 keydir, name);
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600272 else if (keydir)
Vesa Jääskeläinen5b123e02019-06-16 20:53:38 +0300273 snprintf(key_id, sizeof(key_id),
274 "%s",
275 name);
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600276 else if (keyfile)
277 snprintf(key_id, sizeof(key_id), "%s", keyfile);
278 else
279 return -EINVAL;
280
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600281 } 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, Donaldfbc77742021-04-01 22:42:36 +0000290 *evpp = key;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600291
292 return 0;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600293}
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, Donaldfbc77742021-04-01 22:42:36 +0000301 * @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 McCollisterf1ca1fd2017-01-06 13:14:17 -0600303 */
304static int rsa_get_priv_key(const char *keydir, const char *name,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000305 const char *keyfile, ENGINE *engine, EVP_PKEY **evpp)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600306{
307 if (engine)
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600308 return rsa_engine_get_priv_key(keydir, name, keyfile, engine,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000309 evpp);
310 return rsa_pem_get_priv_key(keydir, name, keyfile, evpp);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600311}
312
Simon Glass19c402a2013-06-13 15:10:02 -0700313static int rsa_init(void)
314{
315 int ret;
316
Caliph Nomble7ac1a432018-07-25 22:13:03 -0400317#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
318 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
Simon Glass19c402a2013-06-13 15:10:02 -0700319 ret = SSL_library_init();
Jelle van der Waac3b43282017-05-08 21:31:19 +0200320#else
321 ret = OPENSSL_init_ssl(0, NULL);
322#endif
Simon Glass19c402a2013-06-13 15:10:02 -0700323 if (!ret) {
324 fprintf(stderr, "Failure to init SSL library\n");
325 return -1;
326 }
Caliph Nomble7ac1a432018-07-25 22:13:03 -0400327#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
328 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
Simon Glass19c402a2013-06-13 15:10:02 -0700329 SSL_load_error_strings();
330
331 OpenSSL_add_all_algorithms();
332 OpenSSL_add_all_digests();
333 OpenSSL_add_all_ciphers();
Jelle van der Waac3b43282017-05-08 21:31:19 +0200334#endif
Simon Glass19c402a2013-06-13 15:10:02 -0700335
336 return 0;
337}
338
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600339static int rsa_engine_init(const char *engine_id, ENGINE **pe)
340{
341 ENGINE *e;
342 int ret;
343
344 ENGINE_load_builtin_engines();
345
346 e = ENGINE_by_id(engine_id);
347 if (!e) {
348 fprintf(stderr, "Engine isn't available\n");
349 ret = -1;
350 goto err_engine_by_id;
351 }
352
353 if (!ENGINE_init(e)) {
354 fprintf(stderr, "Couldn't initialize engine\n");
355 ret = -1;
356 goto err_engine_init;
357 }
358
359 if (!ENGINE_set_default_RSA(e)) {
360 fprintf(stderr, "Couldn't set engine as default for RSA\n");
361 ret = -1;
362 goto err_set_rsa;
363 }
364
365 *pe = e;
366
367 return 0;
368
369err_set_rsa:
370 ENGINE_finish(e);
371err_engine_init:
372 ENGINE_free(e);
373err_engine_by_id:
Caliph Nomble7ac1a432018-07-25 22:13:03 -0400374#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
375 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600376 ENGINE_cleanup();
Jelle van der Waac3b43282017-05-08 21:31:19 +0200377#endif
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600378 return ret;
379}
380
Simon Glass19c402a2013-06-13 15:10:02 -0700381static void rsa_remove(void)
382{
Caliph Nomble7ac1a432018-07-25 22:13:03 -0400383#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
384 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
Simon Glass19c402a2013-06-13 15:10:02 -0700385 CRYPTO_cleanup_all_ex_data();
386 ERR_free_strings();
387#ifdef HAVE_ERR_REMOVE_THREAD_STATE
388 ERR_remove_thread_state(NULL);
389#else
390 ERR_remove_state(0);
391#endif
392 EVP_cleanup();
Jelle van der Waac3b43282017-05-08 21:31:19 +0200393#endif
Simon Glass19c402a2013-06-13 15:10:02 -0700394}
395
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600396static void rsa_engine_remove(ENGINE *e)
397{
398 if (e) {
399 ENGINE_finish(e);
400 ENGINE_free(e);
401 }
402}
403
Chan, Donaldfbc77742021-04-01 22:42:36 +0000404static int rsa_sign_with_key(EVP_PKEY *pkey, struct padding_algo *padding_algo,
Philippe Reynes20031562018-11-14 13:51:00 +0100405 struct checksum_algo *checksum_algo,
Heiko Schocher646257d2014-03-03 12:19:26 +0100406 const struct image_region region[], int region_count,
407 uint8_t **sigp, uint *sig_size)
Simon Glass19c402a2013-06-13 15:10:02 -0700408{
Philippe Reynes20031562018-11-14 13:51:00 +0100409 EVP_PKEY_CTX *ckey;
Simon Glass19c402a2013-06-13 15:10:02 -0700410 EVP_MD_CTX *context;
Philippe Reynes3b5d6972018-11-14 13:50:59 +0100411 int ret = 0;
412 size_t size;
Simon Glass19c402a2013-06-13 15:10:02 -0700413 uint8_t *sig;
414 int i;
415
Chan, Donaldfbc77742021-04-01 22:42:36 +0000416 size = EVP_PKEY_size(pkey);
Simon Glass19c402a2013-06-13 15:10:02 -0700417 sig = malloc(size);
418 if (!sig) {
Philippe Reynes3b5d6972018-11-14 13:50:59 +0100419 fprintf(stderr, "Out of memory for signature (%zu bytes)\n",
Simon Glass19c402a2013-06-13 15:10:02 -0700420 size);
421 ret = -ENOMEM;
422 goto err_alloc;
423 }
424
425 context = EVP_MD_CTX_create();
426 if (!context) {
427 ret = rsa_err("EVP context creation failed");
428 goto err_create;
429 }
430 EVP_MD_CTX_init(context);
Philippe Reynes20031562018-11-14 13:51:00 +0100431
Chan, Donaldfbc77742021-04-01 22:42:36 +0000432 ckey = EVP_PKEY_CTX_new(pkey, NULL);
Philippe Reynes20031562018-11-14 13:51:00 +0100433 if (!ckey) {
434 ret = rsa_err("EVP key context creation failed");
435 goto err_create;
436 }
437
438 if (EVP_DigestSignInit(context, &ckey,
Philippe Reynes3b5d6972018-11-14 13:50:59 +0100439 checksum_algo->calculate_sign(),
Chan, Donaldfbc77742021-04-01 22:42:36 +0000440 NULL, pkey) <= 0) {
Simon Glass19c402a2013-06-13 15:10:02 -0700441 ret = rsa_err("Signer setup failed");
442 goto err_sign;
443 }
444
Simon Glassad74aed2021-07-14 17:05:31 -0500445#ifdef CONFIG_FIT_RSASSA_PSS
Philippe Reynes061daa02018-11-14 13:51:01 +0100446 if (padding_algo && !strcmp(padding_algo->name, "pss")) {
447 if (EVP_PKEY_CTX_set_rsa_padding(ckey,
448 RSA_PKCS1_PSS_PADDING) <= 0) {
449 ret = rsa_err("Signer padding setup failed");
450 goto err_sign;
451 }
452 }
Simon Glassad74aed2021-07-14 17:05:31 -0500453#endif /* CONFIG_FIT_RSASSA_PSS */
Philippe Reynes061daa02018-11-14 13:51:01 +0100454
Simon Glass19c402a2013-06-13 15:10:02 -0700455 for (i = 0; i < region_count; i++) {
Philippe Reynes3b5d6972018-11-14 13:50:59 +0100456 if (!EVP_DigestSignUpdate(context, region[i].data,
457 region[i].size)) {
Simon Glass19c402a2013-06-13 15:10:02 -0700458 ret = rsa_err("Signing data failed");
459 goto err_sign;
460 }
461 }
462
Philippe Reynes3b5d6972018-11-14 13:50:59 +0100463 if (!EVP_DigestSignFinal(context, sig, &size)) {
Simon Glass19c402a2013-06-13 15:10:02 -0700464 ret = rsa_err("Could not obtain signature");
465 goto err_sign;
466 }
Philippe Reynes3b5d6972018-11-14 13:50:59 +0100467
Caliph Nomble7ac1a432018-07-25 22:13:03 -0400468 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
469 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
Jelle van der Waac3b43282017-05-08 21:31:19 +0200470 EVP_MD_CTX_cleanup(context);
471 #else
472 EVP_MD_CTX_reset(context);
473 #endif
Simon Glass19c402a2013-06-13 15:10:02 -0700474 EVP_MD_CTX_destroy(context);
Simon Glass19c402a2013-06-13 15:10:02 -0700475
Chan, Donald6d59ace2021-07-19 09:18:54 -0700476 debug("Got signature: %zu bytes, expected %d\n", size, EVP_PKEY_size(pkey));
Simon Glass19c402a2013-06-13 15:10:02 -0700477 *sigp = sig;
478 *sig_size = size;
479
480 return 0;
481
482err_sign:
483 EVP_MD_CTX_destroy(context);
484err_create:
485 free(sig);
486err_alloc:
Simon Glass19c402a2013-06-13 15:10:02 -0700487 return ret;
488}
489
490int rsa_sign(struct image_sign_info *info,
491 const struct image_region region[], int region_count,
492 uint8_t **sigp, uint *sig_len)
493{
Chan, Donaldfbc77742021-04-01 22:42:36 +0000494 EVP_PKEY *pkey = NULL;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600495 ENGINE *e = NULL;
Simon Glass19c402a2013-06-13 15:10:02 -0700496 int ret;
497
498 ret = rsa_init();
499 if (ret)
500 return ret;
501
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600502 if (info->engine_id) {
503 ret = rsa_engine_init(info->engine_id, &e);
504 if (ret)
505 goto err_engine;
506 }
507
Alexandru Gagniuc824ee742021-02-19 12:45:18 -0600508 ret = rsa_get_priv_key(info->keydir, info->keyname, info->keyfile,
Chan, Donaldfbc77742021-04-01 22:42:36 +0000509 e, &pkey);
Simon Glass19c402a2013-06-13 15:10:02 -0700510 if (ret)
511 goto err_priv;
Chan, Donaldfbc77742021-04-01 22:42:36 +0000512 ret = rsa_sign_with_key(pkey, info->padding, info->checksum, region,
Heiko Schocher646257d2014-03-03 12:19:26 +0100513 region_count, sigp, sig_len);
Simon Glass19c402a2013-06-13 15:10:02 -0700514 if (ret)
515 goto err_sign;
516
Chan, Donaldfbc77742021-04-01 22:42:36 +0000517 EVP_PKEY_free(pkey);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600518 if (info->engine_id)
519 rsa_engine_remove(e);
Simon Glass19c402a2013-06-13 15:10:02 -0700520 rsa_remove();
521
522 return ret;
523
524err_sign:
Chan, Donaldfbc77742021-04-01 22:42:36 +0000525 EVP_PKEY_free(pkey);
Simon Glass19c402a2013-06-13 15:10:02 -0700526err_priv:
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600527 if (info->engine_id)
528 rsa_engine_remove(e);
529err_engine:
Simon Glass19c402a2013-06-13 15:10:02 -0700530 rsa_remove();
531 return ret;
532}
533
534/*
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200535 * rsa_get_exponent(): - Get the public exponent from an RSA key
536 */
537static int rsa_get_exponent(RSA *key, uint64_t *e)
538{
539 int ret;
540 BIGNUM *bn_te;
Jelle van der Waac3b43282017-05-08 21:31:19 +0200541 const BIGNUM *key_e;
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200542 uint64_t te;
543
544 ret = -EINVAL;
545 bn_te = NULL;
546
547 if (!e)
548 goto cleanup;
549
Jelle van der Waac3b43282017-05-08 21:31:19 +0200550 RSA_get0_key(key, NULL, &key_e, NULL);
551 if (BN_num_bits(key_e) > 64)
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200552 goto cleanup;
553
Jelle van der Waac3b43282017-05-08 21:31:19 +0200554 *e = BN_get_word(key_e);
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200555
Jelle van der Waac3b43282017-05-08 21:31:19 +0200556 if (BN_num_bits(key_e) < 33) {
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200557 ret = 0;
558 goto cleanup;
559 }
560
Jelle van der Waac3b43282017-05-08 21:31:19 +0200561 bn_te = BN_dup(key_e);
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200562 if (!bn_te)
563 goto cleanup;
564
565 if (!BN_rshift(bn_te, bn_te, 32))
566 goto cleanup;
567
568 if (!BN_mask_bits(bn_te, 32))
569 goto cleanup;
570
571 te = BN_get_word(bn_te);
572 te <<= 32;
573 *e |= te;
574 ret = 0;
575
576cleanup:
577 if (bn_te)
578 BN_free(bn_te);
579
580 return ret;
581}
582
583/*
Simon Glass19c402a2013-06-13 15:10:02 -0700584 * rsa_get_params(): - Get the important parameters of an RSA public key
585 */
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200586int rsa_get_params(RSA *key, uint64_t *exponent, uint32_t *n0_invp,
587 BIGNUM **modulusp, BIGNUM **r_squaredp)
Simon Glass19c402a2013-06-13 15:10:02 -0700588{
589 BIGNUM *big1, *big2, *big32, *big2_32;
590 BIGNUM *n, *r, *r_squared, *tmp;
Jelle van der Waac3b43282017-05-08 21:31:19 +0200591 const BIGNUM *key_n;
Simon Glass19c402a2013-06-13 15:10:02 -0700592 BN_CTX *bn_ctx = BN_CTX_new();
593 int ret = 0;
594
595 /* Initialize BIGNUMs */
596 big1 = BN_new();
597 big2 = BN_new();
598 big32 = BN_new();
599 r = BN_new();
600 r_squared = BN_new();
601 tmp = BN_new();
602 big2_32 = BN_new();
603 n = BN_new();
604 if (!big1 || !big2 || !big32 || !r || !r_squared || !tmp || !big2_32 ||
605 !n) {
606 fprintf(stderr, "Out of memory (bignum)\n");
607 return -ENOMEM;
608 }
609
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200610 if (0 != rsa_get_exponent(key, exponent))
611 ret = -1;
612
Jelle van der Waac3b43282017-05-08 21:31:19 +0200613 RSA_get0_key(key, &key_n, NULL, NULL);
614 if (!BN_copy(n, key_n) || !BN_set_word(big1, 1L) ||
Simon Glass19c402a2013-06-13 15:10:02 -0700615 !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L))
616 ret = -1;
617
618 /* big2_32 = 2^32 */
619 if (!BN_exp(big2_32, big2, big32, bn_ctx))
620 ret = -1;
621
622 /* Calculate n0_inv = -1 / n[0] mod 2^32 */
623 if (!BN_mod_inverse(tmp, n, big2_32, bn_ctx) ||
624 !BN_sub(tmp, big2_32, tmp))
625 ret = -1;
626 *n0_invp = BN_get_word(tmp);
627
628 /* Calculate R = 2^(# of key bits) */
629 if (!BN_set_word(tmp, BN_num_bits(n)) ||
630 !BN_exp(r, big2, tmp, bn_ctx))
631 ret = -1;
632
633 /* Calculate r_squared = R^2 mod n */
634 if (!BN_copy(r_squared, r) ||
635 !BN_mul(tmp, r_squared, r, bn_ctx) ||
636 !BN_mod(r_squared, tmp, n, bn_ctx))
637 ret = -1;
638
639 *modulusp = n;
640 *r_squaredp = r_squared;
641
642 BN_free(big1);
643 BN_free(big2);
644 BN_free(big32);
645 BN_free(r);
646 BN_free(tmp);
647 BN_free(big2_32);
648 if (ret) {
649 fprintf(stderr, "Bignum operations failed\n");
650 return -ENOMEM;
651 }
652
653 return ret;
654}
655
Simon Glass19c402a2013-06-13 15:10:02 -0700656int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
657{
658 BIGNUM *modulus, *r_squared;
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200659 uint64_t exponent;
Simon Glass19c402a2013-06-13 15:10:02 -0700660 uint32_t n0_inv;
661 int parent, node;
662 char name[100];
663 int ret;
664 int bits;
665 RSA *rsa;
Chan, Donaldfbc77742021-04-01 22:42:36 +0000666 EVP_PKEY *pkey = NULL;
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600667 ENGINE *e = NULL;
Simon Glass19c402a2013-06-13 15:10:02 -0700668
669 debug("%s: Getting verification data\n", __func__);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600670 if (info->engine_id) {
671 ret = rsa_engine_init(info->engine_id, &e);
672 if (ret)
673 return ret;
674 }
Chan, Donaldfbc77742021-04-01 22:42:36 +0000675 ret = rsa_get_pub_key(info->keydir, info->keyname, e, &pkey);
Simon Glass19c402a2013-06-13 15:10:02 -0700676 if (ret)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600677 goto err_get_pub_key;
Chan, Donaldfbc77742021-04-01 22:42:36 +0000678#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
679 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
680 rsa = EVP_PKEY_get1_RSA(pkey);
681#else
682 rsa = EVP_PKEY_get0_RSA(pkey);
683#endif
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200684 ret = rsa_get_params(rsa, &exponent, &n0_inv, &modulus, &r_squared);
Simon Glass19c402a2013-06-13 15:10:02 -0700685 if (ret)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600686 goto err_get_params;
Simon Glass19c402a2013-06-13 15:10:02 -0700687 bits = BN_num_bits(modulus);
688 parent = fdt_subnode_offset(keydest, 0, FIT_SIG_NODENAME);
689 if (parent == -FDT_ERR_NOTFOUND) {
690 parent = fdt_add_subnode(keydest, 0, FIT_SIG_NODENAME);
691 if (parent < 0) {
Simon Glass597a8b22014-06-12 07:24:42 -0600692 ret = parent;
693 if (ret != -FDT_ERR_NOSPACE) {
694 fprintf(stderr, "Couldn't create signature node: %s\n",
695 fdt_strerror(parent));
696 }
Simon Glass19c402a2013-06-13 15:10:02 -0700697 }
698 }
Simon Glass597a8b22014-06-12 07:24:42 -0600699 if (ret)
700 goto done;
Simon Glass19c402a2013-06-13 15:10:02 -0700701
702 /* Either create or overwrite the named key node */
703 snprintf(name, sizeof(name), "key-%s", info->keyname);
704 node = fdt_subnode_offset(keydest, parent, name);
705 if (node == -FDT_ERR_NOTFOUND) {
706 node = fdt_add_subnode(keydest, parent, name);
707 if (node < 0) {
Simon Glass597a8b22014-06-12 07:24:42 -0600708 ret = node;
709 if (ret != -FDT_ERR_NOSPACE) {
710 fprintf(stderr, "Could not create key subnode: %s\n",
711 fdt_strerror(node));
712 }
Simon Glass19c402a2013-06-13 15:10:02 -0700713 }
714 } else if (node < 0) {
715 fprintf(stderr, "Cannot select keys parent: %s\n",
716 fdt_strerror(node));
Simon Glass597a8b22014-06-12 07:24:42 -0600717 ret = node;
Simon Glass19c402a2013-06-13 15:10:02 -0700718 }
719
Simon Glass597a8b22014-06-12 07:24:42 -0600720 if (!ret) {
Simon Glass72188f52020-03-18 11:44:06 -0600721 ret = fdt_setprop_string(keydest, node, FIT_KEY_HINT,
722 info->keyname);
Simon Glass597a8b22014-06-12 07:24:42 -0600723 }
Simon Glass4f427a42014-06-02 22:04:51 -0600724 if (!ret)
725 ret = fdt_setprop_u32(keydest, node, "rsa,num-bits", bits);
726 if (!ret)
727 ret = fdt_setprop_u32(keydest, node, "rsa,n0-inverse", n0_inv);
728 if (!ret) {
Michael van der Westhuizene0f2f152014-07-02 10:17:26 +0200729 ret = fdt_setprop_u64(keydest, node, "rsa,exponent", exponent);
730 }
731 if (!ret) {
Simon Glass4f427a42014-06-02 22:04:51 -0600732 ret = fdt_add_bignum(keydest, node, "rsa,modulus", modulus,
733 bits);
734 }
735 if (!ret) {
736 ret = fdt_add_bignum(keydest, node, "rsa,r-squared", r_squared,
737 bits);
738 }
739 if (!ret) {
740 ret = fdt_setprop_string(keydest, node, FIT_ALGO_PROP,
Andrew Duda83dd98e2016-11-08 18:53:41 +0000741 info->name);
Simon Glass4f427a42014-06-02 22:04:51 -0600742 }
mario.six@gdsys.cc2b9ec762016-07-19 11:07:07 +0200743 if (!ret && info->require_keys) {
Simon Glass72188f52020-03-18 11:44:06 -0600744 ret = fdt_setprop_string(keydest, node, FIT_KEY_REQUIRED,
Simon Glass4f427a42014-06-02 22:04:51 -0600745 info->require_keys);
Simon Glass19c402a2013-06-13 15:10:02 -0700746 }
Simon Glass597a8b22014-06-12 07:24:42 -0600747done:
Simon Glass19c402a2013-06-13 15:10:02 -0700748 BN_free(modulus);
749 BN_free(r_squared);
750 if (ret)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600751 ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
752err_get_params:
Chan, Donaldfbc77742021-04-01 22:42:36 +0000753#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
754 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x02070000fL)
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600755 RSA_free(rsa);
Chan, Donaldfbc77742021-04-01 22:42:36 +0000756#endif
757 EVP_PKEY_free(pkey);
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600758err_get_pub_key:
759 if (info->engine_id)
760 rsa_engine_remove(e);
Simon Glass19c402a2013-06-13 15:10:02 -0700761
George McCollisterf1ca1fd2017-01-06 13:14:17 -0600762 return ret;
Simon Glass19c402a2013-06-13 15:10:02 -0700763}