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.

337 lines
9.8 KiB

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