View | Details | Raw Unified | Return to bug 226903 | Differences between
and this patch

Collapse All | Expand All

(-)dns/bind912/files/patch-libressl2.7 (+386 lines)
Added Link Here
1
From 1e64b869b5b33e2deda7059e4348d9870f86d315 Mon Sep 17 00:00:00 2001
2
From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= <ondrej@sury.org>
3
Date: Thu, 3 May 2018 13:59:04 +0200
4
Subject: [PATCH 1/3] Add support for LibreSSL 2.7
5
6
(cherry picked from commit 29ff62a1492ce3dc702a887e864d00bf1949aed3)
7
---
8
 config.h.in                 | 12 +++++++
9
 configure                   | 13 +++++++
10
 configure.in                |  2 ++
11
 lib/dns/openssldh_link.c    | 69 +++++++++++++++++++++++--------------
12
 lib/dns/openssldsa_link.c   |  2 +-
13
 lib/dns/opensslecdsa_link.c | 11 +++---
14
 lib/dns/opensslrsa_link.c   | 36 ++++++++++++-------
15
 7 files changed, 103 insertions(+), 42 deletions(-)
16
17
diff --git config.h.in config.h.in
18
index 0cc04c5dd9..65ee20eeb5 100644
19
--- config.h.in
20
+++ config.h.in
21
@@ -206,6 +206,9 @@ int sigwait(const unsigned int *set, int *sig);
22
 /* Define to 1 if you have the <devpoll.h> header file. */
23
 #undef HAVE_DEVPOLL_H
24
 
25
+/* Define to 1 if you have the `DH_get0_key' function. */
26
+#undef HAVE_DH_GET0_KEY
27
+
28
 /* Define to 1 if you have the `dlclose' function. */
29
 #undef HAVE_DLCLOSE
30
 
31
@@ -221,6 +224,12 @@ int sigwait(const unsigned int *set, int *sig);
32
 /* Define to 1 to enable dnstap support */
33
 #undef HAVE_DNSTAP
34
 
35
+/* Define to 1 if you have the `DSA_get0_pqg' function. */
36
+#undef HAVE_DSA_GET0_PQG
37
+
38
+/* Define to 1 if you have the `ECDSA_SIG_get0' function. */
39
+#undef HAVE_ECDSA_SIG_GET0
40
+
41
 /* Define to 1 if you have the <editline/readline.h> header file. */
42
 #undef HAVE_EDITLINE_READLINE_H
43
 
44
@@ -431,6 +440,9 @@ int sigwait(const unsigned int *set, int *sig);
45
 /* Define to 1 if you have the <regex.h> header file. */
46
 #undef HAVE_REGEX_H
47
 
48
+/* Define to 1 if you have the `RSA_set0_key' function. */
49
+#undef HAVE_RSA_SET0_KEY
50
+
51
 /* Define to 1 if you have the <sched.h> header file. */
52
 #undef HAVE_SCHED_H
53
 
54
diff --git configure configure
55
index fc9256fa8d..2dde1a681d 100755
56
--- configure
57
+++ configure
58
@@ -16724,6 +16724,19 @@ if eval test \"x\$"$as_ac_var"\" = x"yes"; then :
59
 #define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1
60
 _ACEOF
61
 
62
+fi
63
+done
64
+
65
+
66
+	for ac_func in DH_get0_key ECDSA_SIG_get0 RSA_set0_key DSA_get0_pqg
67
+do :
68
+  as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
69
+ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
70
+if eval test \"x\$"$as_ac_var"\" = x"yes"; then :
71
+  cat >>confdefs.h <<_ACEOF
72
+#define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1
73
+_ACEOF
74
+
75
 fi
76
 done
77
 
78
diff --git configure.in configure.in
79
index 99139ba5ac..193562c783 100644
80
--- configure.in
81
+++ configure.in
82
@@ -1781,6 +1781,8 @@ DSO_METHOD_dlfcn();
83
 
84
 	AC_CHECK_FUNCS(EVP_sha256 EVP_sha384 EVP_sha512)
85
 
86
+	AC_CHECK_FUNCS([DH_get0_key ECDSA_SIG_get0 RSA_set0_key DSA_get0_pqg])
87
+
88
 	AC_MSG_CHECKING(for OpenSSL ECDSA support)
89
 	have_ecdsa=""
90
 	AC_TRY_RUN([
91
diff --git lib/dns/openssldh_link.c lib/dns/openssldh_link.c
92
index e74bee2e2d..0db673dd31 100644
93
--- lib/dns/openssldh_link.c
94
+++ lib/dns/openssldh_link.c
95
@@ -71,62 +71,81 @@ static isc_result_t openssldh_todns(const dst_key_t *key, isc_buffer_t *data);
96
 
97
 static BIGNUM *bn2, *bn768, *bn1024, *bn1536;
98
 
99
-#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
100
+#if !defined(HAVE_DH_GET0_KEY)
101
 /*
102
  * DH_get0_key, DH_set0_key, DH_get0_pqg and DH_set0_pqg
103
  * are from OpenSSL 1.1.0.
104
  */
105
 static void
106
 DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key) {
107
-	if (pub_key != NULL)
108
+	if (pub_key != NULL) {
109
 		*pub_key = dh->pub_key;
110
-	if (priv_key != NULL)
111
+	}
112
+	if (priv_key != NULL) {
113
 		*priv_key = dh->priv_key;
114
+	}
115
 }
116
 
117
 static int
118
 DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) {
119
-	/* Note that it is valid for priv_key to be NULL */
120
-	if (pub_key == NULL)
121
-		return 0;
122
+	if (pub_key != NULL) {
123
+		BN_free(dh->pub_key);
124
+		dh->pub_key = pub_key;
125
+	}
126
 
127
-	BN_free(dh->pub_key);
128
-	BN_free(dh->priv_key);
129
-	dh->pub_key = pub_key;
130
-	dh->priv_key = priv_key;
131
+	if (priv_key != NULL) {
132
+		BN_free(dh->priv_key);
133
+		dh->priv_key = priv_key;
134
+	}
135
 
136
-	return 1;
137
+	return (1);
138
 }
139
 
140
 static void
141
 DH_get0_pqg(const DH *dh,
142
 	    const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
143
 {
144
-	if (p != NULL)
145
+	if (p != NULL) {
146
 		*p = dh->p;
147
-	if (q != NULL)
148
+	}
149
+	if (q != NULL) {
150
 		*q = dh->q;
151
-	if (g != NULL)
152
+	}
153
+	if (g != NULL) {
154
 		*g = dh->g;
155
+	}
156
 }
157
 
158
 static int
159
-DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) {
160
-	/* q is optional */
161
-	if (p == NULL || g == NULL)
162
-		return(0);
163
-	BN_free(dh->p);
164
-	BN_free(dh->q);
165
-	BN_free(dh->g);
166
-	dh->p = p;
167
-	dh->q = q;
168
-	dh->g = g;
169
+DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
170
+{
171
+	/* If the fields p and g in d are NULL, the corresponding input
172
+	 * parameters MUST be non-NULL.  q may remain NULL.
173
+	 */
174
+	if ((dh->p == NULL && p == NULL)
175
+	    || (dh->g == NULL && g == NULL))
176
+	{
177
+		return 0;
178
+	}
179
+
180
+	if (p != NULL) {
181
+		BN_free(dh->p);
182
+		dh->p = p;
183
+	}
184
+	if (q != NULL) {
185
+		BN_free(dh->q);
186
+		dh->q = q;
187
+	}
188
+	if (g != NULL) {
189
+		BN_free(dh->g);
190
+		dh->g = g;
191
+	}
192
 
193
 	if (q != NULL) {
194
 		dh->length = BN_num_bits(q);
195
 	}
196
 
197
-	return(1);
198
+	return (1);
199
 }
200
 
201
 #define DH_clear_flags(d, f) (d)->flags &= ~(f)
202
diff --git lib/dns/openssldsa_link.c lib/dns/openssldsa_link.c
203
index 1c541ae73a..dfbd484247 100644
204
--- lib/dns/openssldsa_link.c
205
+++ lib/dns/openssldsa_link.c
206
@@ -52,7 +52,7 @@
207
 
208
 static isc_result_t openssldsa_todns(const dst_key_t *key, isc_buffer_t *data);
209
 
210
-#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
211
+#if !defined(HAVE_DSA_GET0_PQG)
212
 static void
213
 DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q,
214
 	     const BIGNUM **g)
215
diff --git lib/dns/opensslecdsa_link.c lib/dns/opensslecdsa_link.c
216
index a8941a808a..2e47459249 100644
217
--- lib/dns/opensslecdsa_link.c
218
+++ lib/dns/opensslecdsa_link.c
219
@@ -45,20 +45,23 @@
220
 
221
 #define DST_RET(a) {ret = a; goto err;}
222
 
223
-#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
224
+#if !defined(HAVE_ECDSA_SIG_GET0)
225
 /* From OpenSSL 1.1 */
226
 static void
227
 ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) {
228
-	if (pr != NULL)
229
+	if (pr != NULL) {
230
 		*pr = sig->r;
231
-	if (ps != NULL)
232
+	}
233
+	if (ps != NULL) {
234
 		*ps = sig->s;
235
+	}
236
 }
237
 
238
 static int
239
 ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) {
240
-	if (r == NULL || s == NULL)
241
+	if (r == NULL || s == NULL) {
242
 		return 0;
243
+	}
244
 
245
 	BN_clear_free(sig->r);
246
 	BN_clear_free(sig->s);
247
diff --git lib/dns/opensslrsa_link.c lib/dns/opensslrsa_link.c
248
index bdb0a3931d..43f6d317bc 100644
249
--- lib/dns/opensslrsa_link.c
250
+++ lib/dns/opensslrsa_link.c
251
@@ -123,7 +123,7 @@
252
 #endif
253
 #define DST_RET(a) {ret = a; goto err;}
254
 
255
-#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
256
+#if !defined(HAVE_RSA_SET0_KEY)
257
 /* From OpenSSL 1.1.0 */
258
 static int
259
 RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) {
260
@@ -133,8 +133,9 @@ RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) {
261
 	 * parameters MUST be non-NULL for n and e.  d may be
262
 	 * left NULL (in case only the public key is used).
263
 	 */
264
-	if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
265
+	if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL)) {
266
 		return 0;
267
+	}
268
 
269
 	if (n != NULL) {
270
 		BN_free(r->n);
271
@@ -159,8 +160,9 @@ RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q) {
272
 	 * If the fields p and q in r are NULL, the corresponding input
273
 	 * parameters MUST be non-NULL.
274
 	 */
275
-	if ((r->p == NULL && p == NULL) || (r->q == NULL && q == NULL))
276
+	if ((r->p == NULL && p == NULL) || (r->q == NULL && q == NULL)) {
277
 		return 0;
278
+	}
279
 
280
 	if (p != NULL) {
281
 		BN_free(r->p);
282
@@ -183,7 +185,9 @@ RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) {
283
 	if ((r->dmp1 == NULL && dmp1 == NULL) ||
284
 	    (r->dmq1 == NULL && dmq1 == NULL) ||
285
 	    (r->iqmp == NULL && iqmp == NULL))
286
+	{
287
 		return 0;
288
+	}
289
 
290
 	if (dmp1 != NULL) {
291
 		BN_free(r->dmp1);
292
@@ -205,32 +209,40 @@ static void
293
 RSA_get0_key(const RSA *r,
294
 	     const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
295
 {
296
-	if (n != NULL)
297
+	if (n != NULL) {
298
 		*n = r->n;
299
-	if (e != NULL)
300
+	}
301
+	if (e != NULL) {
302
 		*e = r->e;
303
-	if (d != NULL)
304
+	}
305
+	if (d != NULL) {
306
 		*d = r->d;
307
+	}
308
 }
309
 
310
 static void
311
 RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q) {
312
-	if (p != NULL)
313
+	if (p != NULL) {
314
 		*p = r->p;
315
-	if (q != NULL)
316
-	*q = r->q;
317
+	}
318
+	if (q != NULL) {
319
+		*q = r->q;
320
+	}
321
 }
322
 
323
 static void
324
 RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
325
 		    const BIGNUM **iqmp)
326
 {
327
-	if (dmp1 != NULL)
328
+	if (dmp1 != NULL) {
329
 		*dmp1 = r->dmp1;
330
-	if (dmq1 != NULL)
331
+	}
332
+	if (dmq1 != NULL) {
333
 		*dmq1 = r->dmq1;
334
-	if (iqmp != NULL)
335
+	}
336
+	if (iqmp != NULL) {
337
 		*iqmp = r->iqmp;
338
+	}
339
 }
340
 
341
 static int
342
-- 
343
2.17.1
344
345
From e3a318e8d3e050677cfe603b25eaa9607c202276 Mon Sep 17 00:00:00 2001
346
From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= <ondrej@sury.org>
347
Date: Wed, 2 May 2018 14:18:06 +0200
348
Subject: [PATCH 2/3] Workaround LibreSSL 2.7.0-2.7.2 quirk in DH_set0_key
349
350
(cherry picked from commit 6b9e3b7b069509e79c59f89403a91761c300bdee)
351
---
352
 lib/dns/openssldh_link.c | 10 ++++++++++
353
 1 file changed, 10 insertions(+)
354
355
diff --git lib/dns/openssldh_link.c lib/dns/openssldh_link.c
356
index 0db673dd31..8dfda0d2fa 100644
357
--- lib/dns/openssldh_link.c
358
+++ lib/dns/openssldh_link.c
359
@@ -44,6 +44,8 @@
360
 
361
 #include <dst/result.h>
362
 
363
+#include <openssl/opensslv.h>
364
+
365
 #include "dst_internal.h"
366
 #include "dst_openssl.h"
367
 #include "dst_parse.h"
368
@@ -564,7 +566,15 @@ openssldh_fromdns(dst_key_t *key, isc_buffer_t *data) {
369
 		DH_free(dh);
370
 		return (dst__openssl_toresult(ISC_R_NOMEMORY));
371
 	}
372
+#if (LIBRESSL_VERSION_NUMBER >= 0x2070000fL) && (LIBRESSL_VERSION_NUMBER <= 0x2070200fL)
373
+	/*
374
+	 * LibreSSL << 2.7.3 DH_get0_key requires priv_key to be set when
375
+	 * DH structure is empty, hence we cannot use DH_get0_key().
376
+	 */
377
+	dh->pub_key = pub_key;
378
+#else /* LIBRESSL_VERSION_NUMBER */
379
 	DH_set0_key(dh, pub_key, NULL);
380
+#endif /* LIBRESSL_VERSION_NUMBER */
381
 	isc_region_consume(&r, publen);
382
 
383
 	key->key_size = BN_num_bits(p);
384
-- 
385
2.17.1
386

Return to bug 226903