0012-tools-kwbimage-fix-build-with-OpenSSL-1.1.x.patch 2.5 KB

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