You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.4 KiB

  1. From c842faae63b562acc7d989a9cdc815def9ee2ed6 Mon Sep 17 00:00:00 2001
  2. From: Sven-Haegar Koch <haegar@sdinet.de>
  3. Date: Wed, 2 Nov 2016 23:08:24 +0100
  4. Subject: [PATCH] OpenSSL 1.1.0 compile fix.
  5. ---
  6. crypto.c | 53 +++++++++++++++++++++++++++++++++++------------------
  7. 1 file changed, 35 insertions(+), 18 deletions(-)
  8. diff --git a/crypto.c b/crypto.c
  9. index e476611..e8b72d3 100644
  10. --- a/crypto.c
  11. +++ b/crypto.c
  12. @@ -46,6 +46,10 @@ openssl dgst \
  13. */
  14. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  15. +#define EVP_PKEY_get0_RSA(a) ((a)->pkey.rsa)
  16. +#endif
  17. +
  18. EVP_PKEY *
  19. crypto_load_key(const char *key, const bool is_private)
  20. {
  21. @@ -80,7 +84,7 @@ crypto_rsa_verify_signature(struct string *databuffer, struct string *signature,
  22. {
  23. int err;
  24. bool retval;
  25. - EVP_MD_CTX md_ctx;
  26. + EVP_MD_CTX *md_ctx;
  27. EVP_PKEY *pkey;
  28. /* load public key into openssl structure */
  29. @@ -89,15 +93,22 @@ crypto_rsa_verify_signature(struct string *databuffer, struct string *signature,
  30. log_err("crypto_verify_signature: key loading failed\n");
  31. return false;
  32. }
  33. -
  34. +
  35. + md_ctx = EVP_MD_CTX_create();
  36. + if (!md_ctx) {
  37. + log_err("crypto_verify_signature: md_ctx alloc failed\n");
  38. + return false;
  39. + }
  40. +
  41. /* Verify the signature */
  42. - if (EVP_VerifyInit(&md_ctx, EVP_sha512()) != 1) {
  43. + if (EVP_VerifyInit(md_ctx, EVP_sha512()) != 1) {
  44. log_err("crypto_verify_signature: libcrypto verify init failed\n");
  45. + EVP_MD_CTX_destroy(md_ctx);
  46. EVP_PKEY_free(pkey);
  47. return false;
  48. }
  49. - EVP_VerifyUpdate(&md_ctx, string_get(databuffer), string_length(databuffer));
  50. - err = EVP_VerifyFinal(&md_ctx, (unsigned char*)string_get(signature), string_length(signature), pkey);
  51. + EVP_VerifyUpdate(md_ctx, string_get(databuffer), string_length(databuffer));
  52. + err = EVP_VerifyFinal(md_ctx, (unsigned char*)string_get(signature), string_length(signature), pkey);
  53. EVP_PKEY_free(pkey);
  54. if (err != 1) {
  55. @@ -110,7 +121,7 @@ crypto_rsa_verify_signature(struct string *databuffer, struct string *signature,
  56. retval = true;
  57. bailout_ctx_cleanup:
  58. - EVP_MD_CTX_cleanup(&md_ctx);
  59. + EVP_MD_CTX_destroy(md_ctx);
  60. //log_info("Signature Verified Ok.\n");
  61. return retval;
  62. @@ -146,7 +157,7 @@ crypto_rsa_decrypt(struct string *ciphertext, const char *privkey, struct string
  63. len = RSA_private_decrypt(string_length(ciphertext),
  64. (unsigned char*)string_get(ciphertext),
  65. (unsigned char*)string_get(decrypted),
  66. - pkey->pkey.rsa,
  67. + EVP_PKEY_get0_RSA(pkey),
  68. RSA_PKCS1_OAEP_PADDING);
  69. if (len >= 0) {
  70. /* TODO: need cleaner way: */
  71. @@ -167,28 +178,33 @@ bool
  72. crypto_aes_decrypt(struct string *ciphertext, struct string *aes_key, struct string *aes_iv, struct string *decrypted)
  73. {
  74. bool retval = false;
  75. - EVP_CIPHER_CTX ctx;
  76. + EVP_CIPHER_CTX *ctx;
  77. int decryptspace;
  78. int decryptdone;
  79. - EVP_CIPHER_CTX_init(&ctx);
  80. - if (!EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL,
  81. + ctx = EVP_CIPHER_CTX_new();
  82. + if (!ctx) {
  83. + log_err("crypto_aes_decrypt: ctx alloc failed\n");
  84. + goto bail_out;
  85. + }
  86. +
  87. + if (!EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL,
  88. (unsigned char *)string_get(aes_key),
  89. (unsigned char *)string_get(aes_iv))) {
  90. log_err("crypto_aes_decrypt: init failed\n");
  91. ERR_print_errors_fp(stderr);
  92. goto bail_out;
  93. }
  94. - EVP_CIPHER_CTX_set_padding(&ctx, 1);
  95. + EVP_CIPHER_CTX_set_padding(ctx, 1);
  96. - if (string_length(aes_key) != EVP_CIPHER_CTX_key_length(&ctx)) {
  97. + if (string_length(aes_key) != EVP_CIPHER_CTX_key_length(ctx)) {
  98. log_err("crypto_aes_decrypt: invalid key size (%" PRIuPTR " vs expected %d)\n",
  99. - string_length(aes_key), EVP_CIPHER_CTX_key_length(&ctx));
  100. + string_length(aes_key), EVP_CIPHER_CTX_key_length(ctx));
  101. goto bail_out;
  102. }
  103. - if (string_length(aes_iv) != EVP_CIPHER_CTX_iv_length(&ctx)) {
  104. + if (string_length(aes_iv) != EVP_CIPHER_CTX_iv_length(ctx)) {
  105. log_err("crypto_aes_decrypt: invalid iv size (%" PRIuPTR " vs expected %d)\n",
  106. - string_length(aes_iv), EVP_CIPHER_CTX_iv_length(&ctx));
  107. + string_length(aes_iv), EVP_CIPHER_CTX_iv_length(ctx));
  108. goto bail_out;
  109. }
  110. @@ -201,7 +217,7 @@ crypto_aes_decrypt(struct string *ciphertext, struct string *aes_key, struct str
  111. goto bail_out;
  112. }
  113. - if (EVP_DecryptUpdate(&ctx, (unsigned char*)string_get(decrypted),
  114. + if (EVP_DecryptUpdate(ctx, (unsigned char*)string_get(decrypted),
  115. &decryptdone, (unsigned char*)string_get(ciphertext),
  116. string_length(ciphertext))) {
  117. /* TODO: need cleaner way: */
  118. @@ -212,7 +228,7 @@ crypto_aes_decrypt(struct string *ciphertext, struct string *aes_key, struct str
  119. goto bail_out;
  120. }
  121. - if (EVP_DecryptFinal_ex(&ctx,
  122. + if (EVP_DecryptFinal_ex(ctx,
  123. (unsigned char*)string_get(decrypted)+string_length(decrypted),
  124. &decryptdone)) {
  125. /* TODO: need cleaner way: */
  126. @@ -226,7 +242,8 @@ crypto_aes_decrypt(struct string *ciphertext, struct string *aes_key, struct str
  127. retval = true;
  128. bail_out:
  129. - EVP_CIPHER_CTX_cleanup(&ctx);
  130. + if (ctx)
  131. + EVP_CIPHER_CTX_free(ctx);
  132. return retval;
  133. }