0011-rsa-Fix-build-with-OpenSSL-1.1.x.patch 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. From 59be82ef7e7ec4be6e1597d8aef65dd3d8c3a0d9 Mon Sep 17 00:00:00 2001
  2. From: Jelle van der Waa <[email protected]>
  3. Date: Mon, 8 May 2017 21:31:19 +0200
  4. Subject: [PATCH 1/2] rsa: 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. ---
  10. lib/rsa/rsa-sign.c | 44 ++++++++++++++++++++++++++++++++++++++------
  11. 1 file changed, 38 insertions(+), 6 deletions(-)
  12. --- a/lib/rsa/rsa-sign.c
  13. +++ b/lib/rsa/rsa-sign.c
  14. @@ -9,6 +9,7 @@
  15. #include <string.h>
  16. #include <image.h>
  17. #include <time.h>
  18. +#include <openssl/bn.h>
  19. #include <openssl/rsa.h>
  20. #include <openssl/pem.h>
  21. #include <openssl/err.h>
  22. @@ -20,6 +21,19 @@
  23. #define HAVE_ERR_REMOVE_THREAD_STATE
  24. #endif
  25. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  26. +static void RSA_get0_key(const RSA *r,
  27. + const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
  28. +{
  29. + if (n != NULL)
  30. + *n = r->n;
  31. + if (e != NULL)
  32. + *e = r->e;
  33. + if (d != NULL)
  34. + *d = r->d;
  35. +}
  36. +#endif
  37. +
  38. static int rsa_err(const char *msg)
  39. {
  40. unsigned long sslErr = ERR_get_error();
  41. @@ -286,16 +300,22 @@ static int rsa_init(void)
  42. {
  43. int ret;
  44. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  45. ret = SSL_library_init();
  46. +#else
  47. + ret = OPENSSL_init_ssl(0, NULL);
  48. +#endif
  49. if (!ret) {
  50. fprintf(stderr, "Failure to init SSL library\n");
  51. return -1;
  52. }
  53. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  54. SSL_load_error_strings();
  55. OpenSSL_add_all_algorithms();
  56. OpenSSL_add_all_digests();
  57. OpenSSL_add_all_ciphers();
  58. +#endif
  59. return 0;
  60. }
  61. @@ -335,12 +355,15 @@ err_set_rsa:
  62. err_engine_init:
  63. ENGINE_free(e);
  64. err_engine_by_id:
  65. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  66. ENGINE_cleanup();
  67. +#endif
  68. return ret;
  69. }
  70. static void rsa_remove(void)
  71. {
  72. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  73. CRYPTO_cleanup_all_ex_data();
  74. ERR_free_strings();
  75. #ifdef HAVE_ERR_REMOVE_THREAD_STATE
  76. @@ -349,6 +372,7 @@ static void rsa_remove(void)
  77. ERR_remove_state(0);
  78. #endif
  79. EVP_cleanup();
  80. +#endif
  81. }
  82. static void rsa_engine_remove(ENGINE *e)
  83. @@ -409,7 +433,11 @@ static int rsa_sign_with_key(RSA *rsa, s
  84. ret = rsa_err("Could not obtain signature");
  85. goto err_sign;
  86. }
  87. - EVP_MD_CTX_cleanup(context);
  88. + #if OPENSSL_VERSION_NUMBER < 0x10100000L
  89. + EVP_MD_CTX_cleanup(context);
  90. + #else
  91. + EVP_MD_CTX_reset(context);
  92. + #endif
  93. EVP_MD_CTX_destroy(context);
  94. EVP_PKEY_free(key);
  95. @@ -479,6 +507,7 @@ static int rsa_get_exponent(RSA *key, ui
  96. {
  97. int ret;
  98. BIGNUM *bn_te;
  99. + const BIGNUM *key_e;
  100. uint64_t te;
  101. ret = -EINVAL;
  102. @@ -487,17 +516,18 @@ static int rsa_get_exponent(RSA *key, ui
  103. if (!e)
  104. goto cleanup;
  105. - if (BN_num_bits(key->e) > 64)
  106. + RSA_get0_key(key, NULL, &key_e, NULL);
  107. + if (BN_num_bits(key_e) > 64)
  108. goto cleanup;
  109. - *e = BN_get_word(key->e);
  110. + *e = BN_get_word(key_e);
  111. - if (BN_num_bits(key->e) < 33) {
  112. + if (BN_num_bits(key_e) < 33) {
  113. ret = 0;
  114. goto cleanup;
  115. }
  116. - bn_te = BN_dup(key->e);
  117. + bn_te = BN_dup(key_e);
  118. if (!bn_te)
  119. goto cleanup;
  120. @@ -527,6 +557,7 @@ int rsa_get_params(RSA *key, uint64_t *e
  121. {
  122. BIGNUM *big1, *big2, *big32, *big2_32;
  123. BIGNUM *n, *r, *r_squared, *tmp;
  124. + const BIGNUM *key_n;
  125. BN_CTX *bn_ctx = BN_CTX_new();
  126. int ret = 0;
  127. @@ -548,7 +579,8 @@ int rsa_get_params(RSA *key, uint64_t *e
  128. if (0 != rsa_get_exponent(key, exponent))
  129. ret = -1;
  130. - if (!BN_copy(n, key->n) || !BN_set_word(big1, 1L) ||
  131. + RSA_get0_key(key, &key_n, NULL, NULL);
  132. + if (!BN_copy(n, key_n) || !BN_set_word(big1, 1L) ||
  133. !BN_set_word(big2, 2L) || !BN_set_word(big32, 32L))
  134. ret = -1;