0006-Update-for-openssl-1.1.patch 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. From 2e730b2259c701f16d473dbfb7e58e86a6e71b01 Mon Sep 17 00:00:00 2001
  2. From: Daniel Kurtz <djkurtz@chromium.org>
  3. Date: Fri, 18 Jan 2019 13:04:59 +0200
  4. Subject: [PATCH] Update for openssl 1.1
  5. OpenSSL 1.1 has made significant non-backwards compatible changes to its
  6. API as outlined in:
  7. https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
  8. BRANCH=none
  9. BUG=chromium:738114
  10. TEST=cros_workon --host start vboot_reference
  11. TEST=w/ openssl-1.0.2k: sudo emerge vboot_reference
  12. TEST=w/ openssl-1.1.0e: sudo emerge vboot_reference
  13. => both build ok
  14. $ futility version
  15. => command runs without error
  16. TEST=cros_workon --board=soraka start vboot_reference coreboot
  17. TEST=w/ openssl-1.0.2k: emerge-soraka vboot_reference coreboot
  18. TEST=w/ openssl-1.1.0e: emerge-soraka vboot_reference coreboot
  19. => All build ok
  20. Change-Id: I37cfc8cbb04a092eab7b0b3224f475b82609447c
  21. Reviewed-on: https://chromium-review.googlesource.com/557739
  22. Commit-Ready: Daniel Kurtz <djkurtz@chromium.org>
  23. Tested-by: Daniel Kurtz <djkurtz@chromium.org>
  24. Reviewed-by: Randall Spangler <rspangler@chromium.org>
  25. Reviewed-by: Mike Frysinger <vapier@chromium.org>
  26. (cherry-picked from bce7904376beee2912932433a4634c1c25afe2f5)
  27. Signed-off-by: Vadim Kochan <vadim4j@gmail.com>
  28. ---
  29. futility/cmd_create.c | 5 ++++-
  30. futility/vb2_helper.c | 7 +++++--
  31. host/include/openssl_compat.h | 26 ++++++++++++++++++++++++++
  32. host/lib/util_misc.c | 7 +++++--
  33. host/lib21/host_key.c | 8 +++++++-
  34. utility/dumpRSAPublicKey.c | 19 ++++++++++++++-----
  35. 6 files changed, 61 insertions(+), 11 deletions(-)
  36. create mode 100644 host/include/openssl_compat.h
  37. diff --git a/futility/cmd_create.c b/futility/cmd_create.c
  38. index 143ea9ae..80d3fd90 100644
  39. --- a/futility/cmd_create.c
  40. +++ b/futility/cmd_create.c
  41. @@ -13,6 +13,7 @@
  42. #include "2common.h"
  43. #include "2id.h"
  44. #include "2rsa.h"
  45. +#include "openssl_compat.h"
  46. #include "util_misc.h"
  47. #include "vb2_common.h"
  48. #include "vb2_struct.h"
  49. @@ -170,6 +171,7 @@ static int vb2_make_keypair()
  50. enum vb2_signature_algorithm sig_alg;
  51. uint8_t *pubkey_buf = 0;
  52. int has_priv = 0;
  53. + const BIGNUM *rsa_d;
  54. FILE *fp;
  55. int ret = 1;
  56. @@ -193,7 +195,8 @@ static int vb2_make_keypair()
  57. goto done;
  58. }
  59. /* Public keys doesn't have the private exponent */
  60. - has_priv = !!rsa_key->d;
  61. + RSA_get0_key(rsa_key, NULL, NULL, &rsa_d);
  62. + has_priv = !!rsa_d;
  63. if (!has_priv)
  64. fprintf(stderr, "%s has a public key only.\n", infile);
  65. diff --git a/futility/vb2_helper.c b/futility/vb2_helper.c
  66. index 51a78375..c6cc0fdd 100644
  67. --- a/futility/vb2_helper.c
  68. +++ b/futility/vb2_helper.c
  69. @@ -11,6 +11,7 @@
  70. #include "2common.h"
  71. #include "2id.h"
  72. #include "2rsa.h"
  73. +#include "openssl_compat.h"
  74. #include "util_misc.h"
  75. #include "vb2_common.h"
  76. #include "vb2_struct.h"
  77. @@ -216,6 +217,7 @@ int ft_show_pem(const char *name, uint8_t *buf, uint32_t len, void *data)
  78. uint8_t *keyb, *digest;
  79. uint32_t keyb_len;
  80. int i, bits;
  81. + const BIGNUM *rsa_key_n, *rsa_key_d;
  82. /* We're called only after ft_recognize_pem, so this should work. */
  83. rsa_key = rsa_from_buffer(buf, len);
  84. @@ -223,10 +225,11 @@ int ft_show_pem(const char *name, uint8_t *buf, uint32_t len, void *data)
  85. DIE;
  86. /* Use to presence of the private exponent to decide if it's public */
  87. - printf("%s Key file: %s\n", rsa_key->d ? "Private" : "Public",
  88. + RSA_get0_key(rsa_key, &rsa_key_n, NULL, &rsa_key_d);
  89. + printf("%s Key file: %s\n", rsa_key_d ? "Private" : "Public",
  90. name);
  91. - bits = BN_num_bits(rsa_key->n);
  92. + bits = BN_num_bits(rsa_key_n);
  93. printf(" Key length: %d\n", bits);
  94. if (vb_keyb_from_rsa(rsa_key, &keyb, &keyb_len)) {
  95. diff --git a/host/include/openssl_compat.h b/host/include/openssl_compat.h
  96. new file mode 100644
  97. index 00000000..7771f32a
  98. --- /dev/null
  99. +++ b/host/include/openssl_compat.h
  100. @@ -0,0 +1,26 @@
  101. +/* Copyright 2017 The Chromium OS Authors. All rights reserved.
  102. + * Use of this source code is governed by a BSD-style license that can be
  103. + * found in the LICENSE file.
  104. + */
  105. +
  106. +#ifndef VBOOT_REFERENCE_OPENSSL_COMPAT_H_
  107. +#define VBOOT_REFERENCE_OPENSSL_COMPAT_H_
  108. +
  109. +#include <openssl/rsa.h>
  110. +
  111. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  112. +
  113. +static inline void RSA_get0_key(const RSA *rsa, const BIGNUM **n,
  114. + const BIGNUM **e, const BIGNUM **d)
  115. +{
  116. + if (n != NULL)
  117. + *n = rsa->n;
  118. + if (e != NULL)
  119. + *e = rsa->e;
  120. + if (d != NULL)
  121. + *d = rsa->d;
  122. +}
  123. +
  124. +#endif /* OPENSSL_VERSION_NUMBER < 0x10100000L */
  125. +
  126. +#endif /* VBOOT_REFERENCE_OPENSSL_COMPAT_H_ */
  127. diff --git a/host/lib/util_misc.c b/host/lib/util_misc.c
  128. index 03ec683f..f0a1f7ad 100644
  129. --- a/host/lib/util_misc.c
  130. +++ b/host/lib/util_misc.c
  131. @@ -15,6 +15,7 @@
  132. #include "cryptolib.h"
  133. #include "host_common.h"
  134. +#include "openssl_compat.h"
  135. #include "util_misc.h"
  136. #include "vboot_common.h"
  137. @@ -58,6 +59,7 @@ int vb_keyb_from_rsa(struct rsa_st *rsa_private_key,
  138. BIGNUM *N0inv = NULL, *R = NULL, *RR = NULL;
  139. BIGNUM *RRTemp = NULL, *NnumBits = NULL;
  140. BIGNUM *n = NULL, *rr = NULL;
  141. + const BIGNUM *rsa_private_key_n;
  142. BN_CTX *bn_ctx = BN_CTX_new();
  143. uint32_t n0invout;
  144. uint32_t bufsize;
  145. @@ -65,7 +67,7 @@ int vb_keyb_from_rsa(struct rsa_st *rsa_private_key,
  146. int retval = 1;
  147. /* Size of RSA key in 32-bit words */
  148. - nwords = BN_num_bits(rsa_private_key->n) / 32;
  149. + nwords = RSA_size(rsa_private_key) / 4;
  150. bufsize = (2 + nwords + nwords) * sizeof(uint32_t);
  151. outbuf = malloc(bufsize);
  152. @@ -94,7 +96,8 @@ int vb_keyb_from_rsa(struct rsa_st *rsa_private_key,
  153. NEW_BIGNUM(B);
  154. #undef NEW_BIGNUM
  155. - BN_copy(N, rsa_private_key->n);
  156. + RSA_get0_key(rsa_private_key, &rsa_private_key_n, NULL, NULL);
  157. + BN_copy(N, rsa_private_key_n);
  158. BN_set_word(Big1, 1L);
  159. BN_set_word(Big2, 2L);
  160. BN_set_word(Big32, 32L);
  161. diff --git a/host/lib21/host_key.c b/host/lib21/host_key.c
  162. index f7ea1622..f9419ad3 100644
  163. --- a/host/lib21/host_key.c
  164. +++ b/host/lib21/host_key.c
  165. @@ -17,6 +17,7 @@
  166. #include "host_common.h"
  167. #include "host_key2.h"
  168. #include "host_misc.h"
  169. +#include "openssl_compat.h"
  170. struct vb2_text_vs_enum vb2_text_vs_algorithm[] = {
  171. {"RSA1024 SHA1", VB2_ALG_RSA1024_SHA1},
  172. @@ -544,7 +545,12 @@ int vb2_public_key_hash(struct vb2_public_key *key,
  173. enum vb2_signature_algorithm vb2_rsa_sig_alg(struct rsa_st *rsa)
  174. {
  175. - int bits = BN_num_bits(rsa->n);
  176. + const BIGNUM *e, *n;
  177. + int exp, bits;
  178. +
  179. + RSA_get0_key(rsa, &n, &e, NULL);
  180. + exp = BN_get_word(e);
  181. + bits = BN_num_bits(n);
  182. switch (bits) {
  183. case 1024:
  184. diff --git a/utility/dumpRSAPublicKey.c b/utility/dumpRSAPublicKey.c
  185. index b3b7b96b..a17b159e 100644
  186. --- a/utility/dumpRSAPublicKey.c
  187. +++ b/utility/dumpRSAPublicKey.c
  188. @@ -14,14 +14,20 @@
  189. #include <string.h>
  190. #include <unistd.h>
  191. +#include "openssl_compat.h"
  192. +
  193. /* Command line tool to extract RSA public keys from X.509 certificates
  194. * and output a pre-processed version of keys for use by RSA verification
  195. * routines.
  196. */
  197. int check(RSA* key) {
  198. - int public_exponent = BN_get_word(key->e);
  199. - int modulus = BN_num_bits(key->n);
  200. + const BIGNUM *n, *e;
  201. + int public_exponent, modulus;
  202. +
  203. + RSA_get0_key(key, &n, &e, NULL);
  204. + public_exponent = BN_get_word(e);
  205. + modulus = BN_num_bits(n);
  206. if (public_exponent != 65537) {
  207. fprintf(stderr, "WARNING: Public exponent should be 65537 (but is %d).\n",
  208. @@ -40,7 +46,8 @@ int check(RSA* key) {
  209. */
  210. void output(RSA* key) {
  211. int i, nwords;
  212. - BIGNUM *N = key->n;
  213. + const BIGNUM *key_n;
  214. + BIGNUM *N = NULL;
  215. BIGNUM *Big1 = NULL, *Big2 = NULL, *Big32 = NULL, *BigMinus1 = NULL;
  216. BIGNUM *B = NULL;
  217. BIGNUM *N0inv= NULL, *R = NULL, *RR = NULL, *RRTemp = NULL, *NnumBits = NULL;
  218. @@ -48,14 +55,15 @@ void output(RSA* key) {
  219. BN_CTX *bn_ctx = BN_CTX_new();
  220. uint32_t n0invout;
  221. - N = key->n;
  222. /* Output size of RSA key in 32-bit words */
  223. - nwords = BN_num_bits(N) / 32;
  224. + nwords = RSA_size(key) / 4;
  225. if (-1 == write(1, &nwords, sizeof(nwords)))
  226. goto failure;
  227. /* Initialize BIGNUMs */
  228. + RSA_get0_key(key, &key_n, NULL, NULL);
  229. + N = BN_dup(key_n);
  230. Big1 = BN_new();
  231. Big2 = BN_new();
  232. Big32 = BN_new();
  233. @@ -120,6 +128,7 @@ void output(RSA* key) {
  234. failure:
  235. /* Free BIGNUMs. */
  236. + BN_free(N);
  237. BN_free(Big1);
  238. BN_free(Big2);
  239. BN_free(Big32);
  240. --
  241. 2.14.1