0001-ibrdtnd-added-openssl-compatibility.patch 5.8 KB

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