aboutsummaryrefslogtreecommitdiff
path: root/net/ibrdtnd/patches/0001-ibrdtnd-added-openssl-compatibility.patch
blob: b0ee81e3e0bc4d0a6550f633b84aedcfa3c96b4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
From c794bbd16d2f39c656478608eb1314055e877370 Mon Sep 17 00:00:00 2001
From: Eneas U de Queiroz <cote2004-github@yahoo.com>
Date: Sat, 26 May 2018 23:44:54 -0300
Subject: [PATCH] ibrdtnd: added openssl compatibility

This patch adds compatibility with openssl 1.1.0 to ibrdtnd.

Signed-off-by: Eneas U de Queiroz <cote2004-github@yahoo.com>
---
 ibrdtn/daemon/src/security/exchange/DHProtocol.cpp | 36 ++++++++++---
 ibrdtn/daemon/src/security/exchange/Makefile.am    |  2 +
 .../src/security/exchange/openssl_compat.cpp       | 62 ++++++++++++++++++++++
 .../daemon/src/security/exchange/openssl_compat.h  | 13 +++++
 4 files changed, 107 insertions(+), 6 deletions(-)
 create mode 100644 ibrdtn/daemon/src/security/exchange/openssl_compat.cpp
 create mode 100644 ibrdtn/daemon/src/security/exchange/openssl_compat.h

--- a/src/security/exchange/DHProtocol.cpp
+++ b/src/security/exchange/DHProtocol.cpp
@@ -30,6 +30,7 @@
 
 #include <openssl/rand.h>
 #include <openssl/pem.h>
+#include "openssl_compat.h"
 
 #define DH_KEY_LENGTH 1024
 
@@ -132,6 +133,7 @@ namespace dtn
 
 		void DHProtocol::begin(KeyExchangeSession &session, KeyExchangeData &data)
 		{
+			const BIGNUM *pub_key, *p, *g;
 			// get session state
 			DHState &state = session.getState<DHState>();
 
@@ -159,9 +161,12 @@ namespace dtn
 			// prepare request
 			KeyExchangeData request(KeyExchangeData::REQUEST, session);
 
-			write(request, state.dh->pub_key);
-			write(request, state.dh->p);
-			write(request, state.dh->g);
+			DH_get0_pqg(state.dh, &p, NULL, &g);
+			DH_get0_key(state.dh, &pub_key, NULL);
+
+			write(request, pub_key);
+			write(request, p);
+			write(request, g);
 
 			manager.submit(session, request);
 		}
@@ -177,6 +182,15 @@ namespace dtn
 				{
 					if (data.getAction() == KeyExchangeData::REQUEST)
 					{
+						BIGNUM *p = BN_new();
+						BIGNUM *g = BN_new();
+						if (p == NULL || g == NULL)
+						{
+							BN_free(p);
+							BN_free(g);
+							throw ibrcommon::Exception("Error while allocating space for DH parameters");
+						}
+
 						BIGNUM* pub_key = BN_new();
 						read(data, &pub_key);
 
@@ -184,8 +198,16 @@ namespace dtn
 						state.dh = DH_new();
 
 						// read p and g paramter from message
-						read(data, &state.dh->p);
-						read(data, &state.dh->g);
+						read(data, &p);
+						read(data, &g);
+
+						if (DH_set0_pqg(state.dh, p, NULL, g))
+						{
+							BN_free(p);
+							BN_free(g);
+							BN_free(pub_key);
+							throw ibrcommon::Exception("Error while setting DH parameters");
+						}
 
 						int codes;
 						if (!DH_check(state.dh, &codes))
@@ -213,7 +235,9 @@ namespace dtn
 						state.secret.assign((const char*)secret, length);
 
 						KeyExchangeData response(KeyExchangeData::RESPONSE, session);
-						write(response, state.dh->pub_key);
+						const BIGNUM *state_dh_pub_key;
+						DH_get0_key(state.dh, &state_dh_pub_key, NULL);
+						write(response, state_dh_pub_key);
 
 						manager.submit(session, response);
 
--- a/src/security/exchange/Makefile.am
+++ b/src/security/exchange/Makefile.am
@@ -22,6 +22,8 @@ exchange_SOURCES += \
 	NFCProtocol.cpp \
 	NoneProtocol.h \
 	NoneProtocol.cpp \
+	openssl_compat.h \
+	openssl_compat.cpp \
 	QRCodeProtocol.h \
 	QRCodeProtocol.cpp
 	
--- /dev/null
+++ b/src/security/exchange/openssl_compat.cpp
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the OpenSSL license (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include "openssl_compat.h"
+
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+
+void DH_get0_pqg(const DH *dh,
+                 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
+{
+    if (p != NULL)
+        *p = dh->p;
+    if (q != NULL)
+        *q = dh->q;
+    if (g != NULL)
+        *g = dh->g;
+}
+
+int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
+{
+    /* If the fields p and g in d are NULL, the corresponding input
+     * parameters MUST be non-NULL.  q may remain NULL.
+     */
+    if ((dh->p == NULL && p == NULL)
+        || (dh->g == NULL && g == NULL))
+        return 0;
+
+    if (p != NULL) {
+        BN_free(dh->p);
+        dh->p = p;
+    }
+    if (q != NULL) {
+        BN_free(dh->q);
+        dh->q = q;
+    }
+    if (g != NULL) {
+        BN_free(dh->g);
+        dh->g = g;
+    }
+
+    if (q != NULL) {
+        dh->length = BN_num_bits(q);
+    }
+
+    return 1;
+}
+
+void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
+{
+    if (pub_key != NULL)
+        *pub_key = dh->pub_key;
+    if (priv_key != NULL)
+        *priv_key = dh->priv_key;
+}
+
+#endif /* OPENSSL_VERSION_NUMBER */
--- /dev/null
+++ b/src/security/exchange/openssl_compat.h
@@ -0,0 +1,13 @@
+#ifndef LIBCRYPTO_COMPAT_H
+#define LIBCRYPTO_COMPAT_H
+
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+
+#include <openssl/dh.h>
+
+void DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g);
+int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g);
+void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key);
+
+#endif /* OPENSSL_VERSION_NUMBER */
+#endif /* LIBCRYPTO_COMPAT_H */