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.

202 lines
5.8 KiB

  1. From c794bbd16d2f39c656478608eb1314055e877370 Mon Sep 17 00:00:00 2001
  2. From: Eneas U de Queiroz <cote2004-github@yahoo.com>
  3. Date: Sat, 26 May 2018 23:44:54 -0300
  4. Subject: [PATCH] ibrdtnd: added openssl compatibility
  5. This patch adds compatibility with openssl 1.1.0 to ibrdtnd.
  6. Signed-off-by: Eneas U de Queiroz <cote2004-github@yahoo.com>
  7. ---
  8. ibrdtn/daemon/src/security/exchange/DHProtocol.cpp | 36 ++++++++++---
  9. ibrdtn/daemon/src/security/exchange/Makefile.am | 2 +
  10. .../src/security/exchange/openssl_compat.cpp | 62 ++++++++++++++++++++++
  11. .../daemon/src/security/exchange/openssl_compat.h | 13 +++++
  12. 4 files changed, 107 insertions(+), 6 deletions(-)
  13. create mode 100644 ibrdtn/daemon/src/security/exchange/openssl_compat.cpp
  14. create mode 100644 ibrdtn/daemon/src/security/exchange/openssl_compat.h
  15. diff --git a/src/security/exchange/DHProtocol.cpp b/src/security/exchange/DHProtocol.cpp
  16. index e94c5026..3e0ad717 100644
  17. --- a/src/security/exchange/DHProtocol.cpp
  18. +++ b/src/security/exchange/DHProtocol.cpp
  19. @@ -30,6 +30,7 @@
  20. #include <openssl/rand.h>
  21. #include <openssl/pem.h>
  22. +#include "openssl_compat.h"
  23. #define DH_KEY_LENGTH 1024
  24. @@ -132,6 +133,7 @@ namespace dtn
  25. void DHProtocol::begin(KeyExchangeSession &session, KeyExchangeData &data)
  26. {
  27. + const BIGNUM *pub_key, *p, *g;
  28. // get session state
  29. DHState &state = session.getState<DHState>();
  30. @@ -159,9 +161,12 @@ namespace dtn
  31. // prepare request
  32. KeyExchangeData request(KeyExchangeData::REQUEST, session);
  33. - write(request, state.dh->pub_key);
  34. - write(request, state.dh->p);
  35. - write(request, state.dh->g);
  36. + DH_get0_pqg(state.dh, &p, NULL, &g);
  37. + DH_get0_key(state.dh, &pub_key, NULL);
  38. +
  39. + write(request, pub_key);
  40. + write(request, p);
  41. + write(request, g);
  42. manager.submit(session, request);
  43. }
  44. @@ -177,6 +182,15 @@ namespace dtn
  45. {
  46. if (data.getAction() == KeyExchangeData::REQUEST)
  47. {
  48. + BIGNUM *p = BN_new();
  49. + BIGNUM *g = BN_new();
  50. + if (p == NULL || g == NULL)
  51. + {
  52. + BN_free(p);
  53. + BN_free(g);
  54. + throw ibrcommon::Exception("Error while allocating space for DH parameters");
  55. + }
  56. +
  57. BIGNUM* pub_key = BN_new();
  58. read(data, &pub_key);
  59. @@ -184,8 +198,16 @@ namespace dtn
  60. state.dh = DH_new();
  61. // read p and g paramter from message
  62. - read(data, &state.dh->p);
  63. - read(data, &state.dh->g);
  64. + read(data, &p);
  65. + read(data, &g);
  66. +
  67. + if (DH_set0_pqg(state.dh, p, NULL, g))
  68. + {
  69. + BN_free(p);
  70. + BN_free(g);
  71. + BN_free(pub_key);
  72. + throw ibrcommon::Exception("Error while setting DH parameters");
  73. + }
  74. int codes;
  75. if (!DH_check(state.dh, &codes))
  76. @@ -213,7 +235,9 @@ namespace dtn
  77. state.secret.assign((const char*)secret, length);
  78. KeyExchangeData response(KeyExchangeData::RESPONSE, session);
  79. - write(response, state.dh->pub_key);
  80. + const BIGNUM *state_dh_pub_key;
  81. + DH_get0_key(state.dh, &state_dh_pub_key, NULL);
  82. + write(response, state_dh_pub_key);
  83. manager.submit(session, response);
  84. diff --git a/src/security/exchange/Makefile.am b/src/security/exchange/Makefile.am
  85. index a6b2f832..71ed8365 100644
  86. --- a/src/security/exchange/Makefile.am
  87. +++ b/src/security/exchange/Makefile.am
  88. @@ -22,6 +22,8 @@ exchange_SOURCES += \
  89. NFCProtocol.cpp \
  90. NoneProtocol.h \
  91. NoneProtocol.cpp \
  92. + openssl_compat.h \
  93. + openssl_compat.cpp \
  94. QRCodeProtocol.h \
  95. QRCodeProtocol.cpp
  96. diff --git a/src/security/exchange/openssl_compat.cpp b/src/security/exchange/openssl_compat.cpp
  97. new file mode 100644
  98. index 00000000..e3baba0f
  99. --- /dev/null
  100. +++ b/src/security/exchange/openssl_compat.cpp
  101. @@ -0,0 +1,62 @@
  102. +/*
  103. + * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  104. + *
  105. + * Licensed under the OpenSSL license (the "License"). You may not use
  106. + * this file except in compliance with the License. You can obtain a copy
  107. + * in the file LICENSE in the source distribution or at
  108. + * https://www.openssl.org/source/license.html
  109. + */
  110. +
  111. +#include "openssl_compat.h"
  112. +
  113. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  114. +
  115. +void DH_get0_pqg(const DH *dh,
  116. + const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
  117. +{
  118. + if (p != NULL)
  119. + *p = dh->p;
  120. + if (q != NULL)
  121. + *q = dh->q;
  122. + if (g != NULL)
  123. + *g = dh->g;
  124. +}
  125. +
  126. +int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
  127. +{
  128. + /* If the fields p and g in d are NULL, the corresponding input
  129. + * parameters MUST be non-NULL. q may remain NULL.
  130. + */
  131. + if ((dh->p == NULL && p == NULL)
  132. + || (dh->g == NULL && g == NULL))
  133. + return 0;
  134. +
  135. + if (p != NULL) {
  136. + BN_free(dh->p);
  137. + dh->p = p;
  138. + }
  139. + if (q != NULL) {
  140. + BN_free(dh->q);
  141. + dh->q = q;
  142. + }
  143. + if (g != NULL) {
  144. + BN_free(dh->g);
  145. + dh->g = g;
  146. + }
  147. +
  148. + if (q != NULL) {
  149. + dh->length = BN_num_bits(q);
  150. + }
  151. +
  152. + return 1;
  153. +}
  154. +
  155. +void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
  156. +{
  157. + if (pub_key != NULL)
  158. + *pub_key = dh->pub_key;
  159. + if (priv_key != NULL)
  160. + *priv_key = dh->priv_key;
  161. +}
  162. +
  163. +#endif /* OPENSSL_VERSION_NUMBER */
  164. diff --git a/src/security/exchange/openssl_compat.h b/src/security/exchange/openssl_compat.h
  165. new file mode 100644
  166. index 00000000..29e7d415
  167. --- /dev/null
  168. +++ b/src/security/exchange/openssl_compat.h
  169. @@ -0,0 +1,13 @@
  170. +#ifndef LIBCRYPTO_COMPAT_H
  171. +#define LIBCRYPTO_COMPAT_H
  172. +
  173. +#if OPENSSL_VERSION_NUMBER < 0x10100000L
  174. +
  175. +#include <openssl/dh.h>
  176. +
  177. +void DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g);
  178. +int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
  179. +void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key);
  180. +
  181. +#endif /* OPENSSL_VERSION_NUMBER */
  182. +#endif /* LIBCRYPTO_COMPAT_H */
  183. --
  184. 2.16.1