uboot-mvebu: add OpenSSL compat patches
[openwrt/staging/lynxis.git] / package / boot / uboot-mvebu / patches / 0012-tools-kwbimage-fix-build-with-OpenSSL-1.1.x.patch
1 From 65030804dc57f3488e4ffe21e72fc65cd245cb98 Mon Sep 17 00:00:00 2001
2 From: Jelle van der Waa <jelle@vdwaa.nl>
3 Date: Mon, 8 May 2017 21:31:20 +0200
4 Subject: [PATCH 2/2] tools: kwbimage fix build with OpenSSL 1.1.x
5
6 The rsa_st struct has been made opaque in 1.1.x, add forward compatible
7 code to access the n, e, d members of rsa_struct.
8
9 EVP_MD_CTX_cleanup has been removed in 1.1.x and EVP_MD_CTX_reset should be
10 called to reinitialise an already created structure.
11
12 Signed-off-by: Jelle van der Waa <jelle@vdwaa.nl>
13 ---
14 tools/kwbimage.c | 36 ++++++++++++++++++++++++++++++------
15 1 file changed, 30 insertions(+), 6 deletions(-)
16
17 --- a/tools/kwbimage.c
18 +++ b/tools/kwbimage.c
19 @@ -18,10 +18,30 @@
20 #include "kwbimage.h"
21
22 #ifdef CONFIG_KWB_SECURE
23 +#include <openssl/bn.h>
24 #include <openssl/rsa.h>
25 #include <openssl/pem.h>
26 #include <openssl/err.h>
27 #include <openssl/evp.h>
28 +
29 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
30 +static void RSA_get0_key(const RSA *r,
31 + const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
32 +{
33 + if (n != NULL)
34 + *n = r->n;
35 + if (e != NULL)
36 + *e = r->e;
37 + if (d != NULL)
38 + *d = r->d;
39 +}
40 +
41 +#else
42 +void EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
43 +{
44 + EVP_MD_CTX_reset(ctx);
45 +}
46 +#endif
47 #endif
48
49 static struct image_cfg_element *image_cfg;
50 @@ -470,12 +490,16 @@ static int kwb_export_pubkey(RSA *key, s
51 char *keyname)
52 {
53 int size_exp, size_mod, size_seq;
54 + const BIGNUM *key_e, *key_n;
55 uint8_t *cur;
56 char *errmsg = "Failed to encode %s\n";
57
58 - if (!key || !key->e || !key->n || !dst) {
59 + RSA_get0_key(key, NULL, &key_e, NULL);
60 + RSA_get0_key(key, &key_n, NULL, NULL);
61 +
62 + if (!key || !key_e || !key_n || !dst) {
63 fprintf(stderr, "export pk failed: (%p, %p, %p, %p)",
64 - key, key->e, key->n, dst);
65 + key, key_e, key_n, dst);
66 fprintf(stderr, errmsg, keyname);
67 return -EINVAL;
68 }
69 @@ -490,8 +514,8 @@ static int kwb_export_pubkey(RSA *key, s
70 * do the encoding manually.
71 */
72
73 - size_exp = BN_num_bytes(key->e);
74 - size_mod = BN_num_bytes(key->n);
75 + size_exp = BN_num_bytes(key_e);
76 + size_mod = BN_num_bytes(key_n);
77 size_seq = 4 + size_mod + 4 + size_exp;
78
79 if (size_mod > 256) {
80 @@ -520,14 +544,14 @@ static int kwb_export_pubkey(RSA *key, s
81 *cur++ = 0x82;
82 *cur++ = (size_mod >> 8) & 0xFF;
83 *cur++ = size_mod & 0xFF;
84 - BN_bn2bin(key->n, cur);
85 + BN_bn2bin(key_n, cur);
86 cur += size_mod;
87 /* Exponent */
88 *cur++ = 0x02; /* INTEGER */
89 *cur++ = 0x82;
90 *cur++ = (size_exp >> 8) & 0xFF;
91 *cur++ = size_exp & 0xFF;
92 - BN_bn2bin(key->e, cur);
93 + BN_bn2bin(key_e, cur);
94
95 if (hashf) {
96 struct hash_v1 pk_hash;