0002-ibrcommon-added-openssl-1.1-compatibility-264.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. From a801d10a081e3130e24042256a43190c9eb6c112 Mon Sep 17 00:00:00 2001
  2. From: Eneas Queiroz <35331380+cotequeiroz@users.noreply.github.com>
  3. Date: Wed, 23 May 2018 03:09:02 -0300
  4. Subject: [PATCH] ibrcommon: added openssl 1.1 compatibility (#264)
  5. This patch adds compatibility to openssl 1.1.0.
  6. Backported from master branch:
  7. https://github.com/ibrdtn/ibrdtn/commit/a801d10a081e3130e24042256a43190c9eb6c112
  8. Signed-off-by: Eneas U de Queiroz <cote2004-github@yahoo.com>
  9. Signed-off-by: Bernd Kuhls <bernd.kuhls@t-online.de>
  10. ---
  11. ibrcommon/ibrcommon/ssl/HMacStream.cpp | 11 +++---
  12. ibrcommon/ibrcommon/ssl/HMacStream.h | 2 +-
  13. ibrcommon/ibrcommon/ssl/RSASHA256Stream.cpp | 28 +++++++------
  14. ibrcommon/ibrcommon/ssl/RSASHA256Stream.h | 2 +-
  15. ibrcommon/ibrcommon/ssl/iostreamBIO.cpp | 44 ++++++++++++++++-----
  16. ibrcommon/ibrcommon/ssl/openssl_compat.h | 38 ++++++++++++++++++
  17. 6 files changed, 95 insertions(+), 30 deletions(-)
  18. create mode 100644 ibrcommon/ibrcommon/ssl/openssl_compat.h
  19. diff --git a/ibrcommon/ssl/HMacStream.cpp b/ibrcommon/ssl/HMacStream.cpp
  20. index e5d317e3..66d8ce42 100644
  21. --- a/ibrcommon/ssl/HMacStream.cpp
  22. +++ b/ibrcommon/ssl/HMacStream.cpp
  23. @@ -20,29 +20,30 @@
  24. */
  25. #include "ibrcommon/ssl/HMacStream.h"
  26. +#include "openssl_compat.h"
  27. namespace ibrcommon
  28. {
  29. HMacStream::HMacStream(const unsigned char * const key, const int key_size)
  30. : HashStream(EVP_MAX_MD_SIZE, BUFF_SIZE), key_(key), key_size_(key_size)
  31. {
  32. - HMAC_CTX_init(&ctx_);
  33. - HMAC_Init_ex(&ctx_, key_, key_size_, EVP_sha1(), NULL);
  34. + ctx_ = HMAC_CTX_new();
  35. + HMAC_Init_ex(ctx_, key_, key_size_, EVP_sha1(), NULL);
  36. }
  37. HMacStream::~HMacStream()
  38. {
  39. - HMAC_CTX_cleanup(&ctx_);
  40. + HMAC_CTX_free(ctx_);
  41. }
  42. void HMacStream::update(char *buf, const size_t size)
  43. {
  44. // hashing
  45. - HMAC_Update(&ctx_, (unsigned char*)buf, size);
  46. + HMAC_Update(ctx_, (unsigned char*)buf, size);
  47. }
  48. void HMacStream::finalize(char * hash, unsigned int &size)
  49. {
  50. - HMAC_Final(&ctx_, (unsigned char*)hash, &size);
  51. + HMAC_Final(ctx_, (unsigned char*)hash, &size);
  52. }
  53. }
  54. diff --git a/ibrcommon/ssl/HMacStream.h b/ibrcommon/ssl/HMacStream.h
  55. index 7dcea168..d04bceb8 100644
  56. --- a/ibrcommon/ssl/HMacStream.h
  57. +++ b/ibrcommon/ssl/HMacStream.h
  58. @@ -44,7 +44,7 @@ namespace ibrcommon
  59. const unsigned char * const key_;
  60. const int key_size_;
  61. - HMAC_CTX ctx_;
  62. + HMAC_CTX* ctx_;
  63. };
  64. }
  65. diff --git a/ibrcommon/ssl/RSASHA256Stream.cpp b/ibrcommon/ssl/RSASHA256Stream.cpp
  66. index d94430ed..d25c5d2f 100644
  67. --- a/ibrcommon/ssl/RSASHA256Stream.cpp
  68. +++ b/ibrcommon/ssl/RSASHA256Stream.cpp
  69. @@ -21,6 +21,7 @@
  70. #include "ibrcommon/ssl/RSASHA256Stream.h"
  71. #include "ibrcommon/Logger.h"
  72. +#include "openssl_compat.h"
  73. #include <openssl/err.h>
  74. namespace ibrcommon
  75. @@ -30,11 +31,11 @@ namespace ibrcommon
  76. {
  77. // Initialize get pointer. This should be zero so that underflow is called upon first read.
  78. setp(&out_buf_[0], &out_buf_[BUFF_SIZE - 1]);
  79. - EVP_MD_CTX_init(&_ctx);
  80. + _ctx = EVP_MD_CTX_new();
  81. if (!_verify)
  82. {
  83. - if (!EVP_SignInit_ex(&_ctx, EVP_sha256(), NULL))
  84. + if (!EVP_SignInit_ex(_ctx, EVP_sha256(), NULL))
  85. {
  86. IBRCOMMON_LOGGER_TAG("RSASHA256Stream", critical) << "failed to initialize the signature function" << IBRCOMMON_LOGGER_ENDL;
  87. ERR_print_errors_fp(stderr);
  88. @@ -42,7 +43,7 @@ namespace ibrcommon
  89. }
  90. else
  91. {
  92. - if (!EVP_VerifyInit_ex(&_ctx, EVP_sha256(), NULL))
  93. + if (!EVP_VerifyInit_ex(_ctx, EVP_sha256(), NULL))
  94. {
  95. IBRCOMMON_LOGGER_TAG("RSASHA256Stream", critical) << "failed to initialize the verification function" << IBRCOMMON_LOGGER_ENDL;
  96. ERR_print_errors_fp(stderr);
  97. @@ -52,18 +53,19 @@ namespace ibrcommon
  98. RSASHA256Stream::~RSASHA256Stream()
  99. {
  100. - EVP_MD_CTX_cleanup(&_ctx);
  101. + EVP_MD_CTX_free(_ctx);
  102. }
  103. void RSASHA256Stream::reset()
  104. {
  105. - EVP_MD_CTX_cleanup(&_ctx);
  106. -
  107. - EVP_MD_CTX_init(&_ctx);
  108. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  109. + EVP_MD_CTX_cleanup(_ctx);
  110. +#endif
  111. + EVP_MD_CTX_init(_ctx);
  112. if (!_verify)
  113. {
  114. - if (!EVP_SignInit_ex(&_ctx, EVP_sha256(), NULL))
  115. + if (!EVP_SignInit_ex(_ctx, EVP_sha256(), NULL))
  116. {
  117. IBRCOMMON_LOGGER_TAG("RSASHA256Stream", critical) << "failed to initialize the signature function" << IBRCOMMON_LOGGER_ENDL;
  118. ERR_print_errors_fp(stderr);
  119. @@ -71,7 +73,7 @@ namespace ibrcommon
  120. }
  121. else
  122. {
  123. - if (!EVP_VerifyInit_ex(&_ctx, EVP_sha256(), NULL))
  124. + if (!EVP_VerifyInit_ex(_ctx, EVP_sha256(), NULL))
  125. {
  126. IBRCOMMON_LOGGER_TAG("RSASHA256Stream", critical) << "failed to initialize the verfication function" << IBRCOMMON_LOGGER_ENDL;
  127. ERR_print_errors_fp(stderr);
  128. @@ -91,7 +93,7 @@ namespace ibrcommon
  129. std::vector<unsigned char> sign(EVP_PKEY_size(_pkey));
  130. unsigned int size = EVP_PKEY_size(_pkey);
  131. - _return_code = EVP_SignFinal(&_ctx, &sign[0], &size, _pkey);
  132. + _return_code = EVP_SignFinal(_ctx, &sign[0], &size, _pkey);
  133. _sign = std::string((const char*)&sign[0], size);
  134. @@ -107,7 +109,7 @@ namespace ibrcommon
  135. if (!_sign_valid)
  136. {
  137. sync();
  138. - _return_code = EVP_VerifyFinal(&_ctx, reinterpret_cast<const unsigned char *>(their_sign.c_str()), static_cast<unsigned int>(their_sign.size()), _pkey);
  139. + _return_code = EVP_VerifyFinal(_ctx, reinterpret_cast<const unsigned char *>(their_sign.c_str()), static_cast<unsigned int>(their_sign.size()), _pkey);
  140. _sign_valid = true;
  141. }
  142. return _return_code;
  143. @@ -145,7 +147,7 @@ namespace ibrcommon
  144. if (!_verify)
  145. // hashing
  146. {
  147. - if (!EVP_SignUpdate(&_ctx, &out_buf_[0], iend - ibegin))
  148. + if (!EVP_SignUpdate(_ctx, &out_buf_[0], iend - ibegin))
  149. {
  150. IBRCOMMON_LOGGER_TAG("RSASHA256Stream", critical) << "failed to feed data into the signature function" << IBRCOMMON_LOGGER_ENDL;
  151. ERR_print_errors_fp(stderr);
  152. @@ -153,7 +155,7 @@ namespace ibrcommon
  153. }
  154. else
  155. {
  156. - if (!EVP_VerifyUpdate(&_ctx, &out_buf_[0], iend - ibegin))
  157. + if (!EVP_VerifyUpdate(_ctx, &out_buf_[0], iend - ibegin))
  158. {
  159. IBRCOMMON_LOGGER_TAG("RSASHA256Stream", critical) << "failed to feed data into the verification function" << IBRCOMMON_LOGGER_ENDL;
  160. ERR_print_errors_fp(stderr);
  161. diff --git a/ibrcommon/ssl/RSASHA256Stream.h b/ibrcommon/ssl/RSASHA256Stream.h
  162. index 344f8e10..6f3a1168 100644
  163. --- a/ibrcommon/ssl/RSASHA256Stream.h
  164. +++ b/ibrcommon/ssl/RSASHA256Stream.h
  165. @@ -106,7 +106,7 @@ namespace ibrcommon
  166. /** the context in which the streamed data will be feed into for
  167. calculation of the hash/signature */
  168. - EVP_MD_CTX _ctx;
  169. + EVP_MD_CTX * _ctx;
  170. /** tells if the context needs to be finalized to get a valid signature or
  171. verification */
  172. diff --git a/ibrcommon/ssl/iostreamBIO.cpp b/ibrcommon/ssl/iostreamBIO.cpp
  173. index 18c1b55c..ea6c63eb 100644
  174. --- a/ibrcommon/ssl/iostreamBIO.cpp
  175. +++ b/ibrcommon/ssl/iostreamBIO.cpp
  176. @@ -23,6 +23,7 @@
  177. #include "ibrcommon/Logger.h"
  178. +#include "openssl_compat.h"
  179. #include <openssl/err.h>
  180. namespace ibrcommon
  181. @@ -42,7 +43,20 @@ static int create(BIO *bio);
  182. //static int destroy(BIO *bio);
  183. //static long (*callback_ctrl)(BIO *, int, bio_info_cb *);
  184. -
  185. +#if OPENSSL_VERSION_NUMBER >= 0x10100000L
  186. +BIO_METHOD * BIO_iostream_method()
  187. +{
  188. + static BIO_METHOD *iostream_method = NULL;
  189. + if (iostream_method) {
  190. + iostream_method = BIO_meth_new(iostreamBIO::type, iostreamBIO::name);
  191. + BIO_meth_set_write(iostream_method, bwrite);
  192. + BIO_meth_set_read(iostream_method, bread);
  193. + BIO_meth_set_ctrl(iostream_method, ctrl);
  194. + BIO_meth_set_create(iostream_method, create);
  195. + }
  196. + return iostream_method;
  197. +}
  198. +#else
  199. static BIO_METHOD iostream_method =
  200. {
  201. iostreamBIO::type,
  202. @@ -56,12 +70,17 @@ static BIO_METHOD iostream_method =
  203. NULL,//destroy,
  204. NULL//callback_ctrl
  205. };
  206. +BIO_METHOD * BIO_iostream_method()
  207. +{
  208. + return &iostream_method;
  209. +}
  210. +#endif
  211. iostreamBIO::iostreamBIO(iostream *stream)
  212. : _stream(stream)
  213. {
  214. /* create BIO */
  215. - _bio = BIO_new(&iostream_method);
  216. + _bio = BIO_new(BIO_iostream_method());
  217. if(!_bio){
  218. /* creation failed, throw exception */
  219. char err_buf[ERR_BUF_SIZE];
  220. @@ -72,7 +91,7 @@ iostreamBIO::iostreamBIO(iostream *stream)
  221. }
  222. /* save the iostream in the bio object */
  223. - _bio->ptr = stream;
  224. + BIO_set_data(_bio, (void *) stream);
  225. }
  226. BIO * iostreamBIO::getBIO(){
  227. @@ -81,10 +100,10 @@ BIO * iostreamBIO::getBIO(){
  228. static int create(BIO *bio)
  229. {
  230. - bio->ptr = NULL;
  231. - /* (from openssl memory bio) */
  232. - bio->shutdown=1;
  233. - bio->init=1;
  234. + BIO_set_data(bio, NULL);
  235. + BIO_set_shutdown(bio, 1);
  236. + BIO_set_init(bio, 1);
  237. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  238. /* from bss_mem.c (openssl):
  239. * bio->num is used to hold the value to return on 'empty', if it is
  240. * 0, should_retry is not set
  241. @@ -93,6 +112,7 @@ static int create(BIO *bio)
  242. * it is set to 0 since the underlying stream is blocking
  243. */
  244. bio->num= 0;
  245. +#endif
  246. return 1;
  247. }
  248. @@ -102,7 +122,7 @@ static int create(BIO *bio)
  249. static long ctrl(BIO *bio, int cmd, long num, void *)
  250. {
  251. long ret;
  252. - iostream *stream = reinterpret_cast<iostream*>(bio->ptr);
  253. + iostream *stream = reinterpret_cast<iostream*>(BIO_get_data(bio));
  254. IBRCOMMON_LOGGER_DEBUG_TAG("iostreamBIO", 90) << "ctrl called, cmd: " << cmd << ", num: " << num << "." << IBRCOMMON_LOGGER_ENDL;
  255. @@ -147,8 +167,12 @@ static long ctrl(BIO *bio, int cmd, long num, void *)
  256. static int bread(BIO *bio, char *buf, int len)
  257. {
  258. - iostream *stream = reinterpret_cast<iostream*>(bio->ptr);
  259. + iostream *stream = reinterpret_cast<iostream*>(BIO_get_data(bio));
  260. +#if OPENSSL_VERSION_NUMBER >= 0x10100000L
  261. + int num_bytes = 0;
  262. +#else
  263. int num_bytes = bio->num;
  264. +#endif
  265. try{
  266. /* make sure to read at least 1 byte and then read as much as we can */
  267. @@ -170,7 +194,7 @@ static int bwrite(BIO *bio, const char *buf, int len)
  268. if(len == 0){
  269. return 0;
  270. }
  271. - iostream *stream = reinterpret_cast<iostream*>(bio->ptr);
  272. + iostream *stream = reinterpret_cast<iostream*>(BIO_get_data(bio));
  273. /* write the data */
  274. try{
  275. diff --git a/ibrcommon/ssl/openssl_compat.h b/ibrcommon/ssl/openssl_compat.h
  276. new file mode 100644
  277. index 00000000..e491677f
  278. --- /dev/null
  279. +++ b/ibrcommon/ssl/openssl_compat.h
  280. @@ -0,0 +1,38 @@
  281. +#ifndef OPENSSL_COMPAT_H
  282. +#define OPENSSL_COMPAT_H
  283. +
  284. +#include <openssl/crypto.h>
  285. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  286. +
  287. +#include <openssl/evp.h>
  288. +#include <openssl/hmac.h>
  289. +
  290. +static inline EVP_MD_CTX * EVP_MD_CTX_new()
  291. +{
  292. + EVP_MD_CTX *ctx;
  293. +
  294. + ctx = (EVP_MD_CTX *) OPENSSL_malloc(sizeof(EVP_MD_CTX));
  295. + EVP_MD_CTX_init(ctx);
  296. + return ctx;
  297. +}
  298. +#define EVP_MD_CTX_free(c) if (c != NULL) OPENSSL_free(c)
  299. +
  300. +static inline HMAC_CTX * HMAC_CTX_new()
  301. +{
  302. + HMAC_CTX *ctx;
  303. +
  304. + ctx = (HMAC_CTX *) OPENSSL_malloc(sizeof(HMAC_CTX));
  305. + HMAC_CTX_init(ctx);
  306. + return ctx;
  307. +}
  308. +#define HMAC_CTX_free(c) if (c != NULL) OPENSSL_free(c)
  309. +
  310. +#define BIO_get_data(b) b->ptr
  311. +#define BIO_set_data(b, v) b->ptr=v
  312. +#define BIO_set_shutdown(b, v) b->shutdown=v
  313. +#define BIO_set_init(b, v) b->init=v
  314. +
  315. +#endif /* OPENSSL_VERSION_NUMBER */
  316. +
  317. +#endif /* OPENSSL_COMPAT_H */
  318. +
  319. --
  320. 2.18.0