Browse code

tls: add a level of libressl compatibility

libressl is the openbsd openssl fork that is getting more popular.
It defines OPENSSL_VERSION_NUMBER 0x20000000L and introduces a new
LIBRESSL_VERSION_NUMBER that reflects the true libressl version.

As openssl made major changes between 1.0.1 and 1.1.0, and libressl
being closer to 1.0.1 series, it needs to be specifically detected
in certain cases.

This commit updates the compat logic to implement missing api
functions as inline function where sensible needed in order to
minimize the #ifdef clutter and make the code more readable and
less prone to bugs.

Timo Teräs authored on 28/04/2017 07:06:01
Showing 2 changed files
... ...
@@ -63,7 +63,7 @@ static int tls_bio_mbuf_puts(BIO* b, const char* s);
63 63
 static long tls_bio_mbuf_ctrl(BIO* b, int cmd, long arg1, void* arg2);
64 64
 
65 65
 
66
-#if OPENSSL_VERSION_NUMBER < 0x010100000L
66
+#if OPENSSL_VERSION_NUMBER < 0x010100000L || defined(LIBRESSL_VERSION_NUMBER)
67 67
 static BIO_METHOD tls_mbuf_method = {
68 68
 	BIO_TYPE_TLS_MBUF,	/* type */
69 69
 	"sr_tls_mbuf",		/* name */
... ...
@@ -76,6 +76,27 @@ static BIO_METHOD tls_mbuf_method = {
76 76
 	tls_bio_mbuf_free,	/* destroy(free) function */
77 77
 	0					/* ctrl callback */
78 78
 };
79
+
80
+static void *CRYPTO_zalloc(size_t num, const char *file, int line)
81
+{
82
+	void *ret = CRYPTO_malloc(num, file, line);
83
+	if (ret != NULL)
84
+		memset(ret, 0, num);
85
+	return ret;
86
+}
87
+# define OPENSSL_zalloc(num) CRYPTO_zalloc(num, __FILE__, __LINE__)
88
+static void *BIO_get_data(BIO *b)
89
+{
90
+	return b->ptr;
91
+}
92
+static void BIO_set_data(BIO *b, void *ptr)
93
+{
94
+	b->ptr = ptr;
95
+}
96
+static void BIO_set_init(BIO *b, int init)
97
+{
98
+	b->init = init;
99
+}
79 100
 #else
80 101
 static BIO_METHOD *tls_mbuf_method = NULL;
81 102
 #endif
... ...
@@ -84,7 +105,7 @@ static BIO_METHOD *tls_mbuf_method = NULL;
84 105
 /** returns a custom tls_mbuf BIO. */
85 106
 BIO_METHOD* tls_BIO_mbuf(void)
86 107
 {
87
-#if OPENSSL_VERSION_NUMBER < 0x010100000L
108
+#if OPENSSL_VERSION_NUMBER < 0x010100000L || defined(LIBRESSL_VERSION_NUMBER)
88 109
 	return &tls_mbuf_method;
89 110
 #else
90 111
 	if(tls_mbuf_method != NULL) {
... ...
@@ -137,22 +158,14 @@ int tls_BIO_mbuf_set(BIO* b, struct tls_mbuf* rd, struct tls_mbuf* wr)
137 158
 	struct tls_bio_mbuf_data* d;
138 159
 
139 160
 	TLS_BIO_DBG("tls_BIO_mbuf_set called (%p => %p, %p)\n", b, rd, wr);
140
-#if OPENSSL_VERSION_NUMBER < 0x010100000L
141
-	d = b->ptr;
142
-#else
143 161
 	d = BIO_get_data(b);
144
-#endif
145 162
 	if (unlikely(d == 0)){
146 163
 		BUG("null BIO ptr data\n");
147 164
 		return 0;
148 165
 	}
149 166
 	d->rd = rd;
150 167
 	d->wr = wr;
151
-#if OPENSSL_VERSION_NUMBER < 0x010100000L
152
-	b->init = 1;
153
-#else
154 168
 	BIO_set_init(b, 1);
155
-#endif
156 169
 	return 1;
157 170
 }
158 171
 
... ...
@@ -167,25 +180,12 @@ static int tls_bio_mbuf_new(BIO* b)
167 180
 	struct tls_bio_mbuf_data* d;
168 181
 
169 182
 	TLS_BIO_DBG("tls_bio_mbuf_new called (%p)\n", b);
170
-#if OPENSSL_VERSION_NUMBER < 0x010100000L
171
-	b->init = 0; /* not initialized yet */
172
-	b->num = 0;
173
-	b->ptr = 0;
174
-	b->flags = 0;
175
-	d = OPENSSL_malloc(sizeof(*d));
176
-	if (unlikely(d == 0))
177
-		return 0;
178
-	d->rd = 0;
179
-	d->wr = 0;
180
-	b->ptr = d;
181
-#else
182 183
 	BIO_set_init(b, 0);
183 184
 	BIO_set_data(b, NULL);
184 185
 	d = OPENSSL_zalloc(sizeof(*d));
185 186
 	if (unlikely(d == 0))
186 187
 		return 0;
187 188
 	BIO_set_data(b, d);
188
-#endif
189 189
 	return 1;
190 190
 }
191 191
 
... ...
@@ -200,13 +200,6 @@ static int tls_bio_mbuf_free(BIO* b)
200 200
 	TLS_BIO_DBG("tls_bio_mbuf_free called (%p)\n", b);
201 201
 	if (unlikely( b == 0))
202 202
 			return 0;
203
-#if OPENSSL_VERSION_NUMBER < 0x010100000L
204
-	if (likely(b->ptr)){
205
-		OPENSSL_free(b->ptr);
206
-		b->ptr = 0;
207
-		b->init = 0;
208
-	}
209
-#else
210 203
 	do {
211 204
 		struct tls_bio_mbuf_data* d;
212 205
 		d = BIO_get_data(b);
... ...
@@ -216,7 +209,6 @@ static int tls_bio_mbuf_free(BIO* b)
216 209
 			BIO_set_init(b, 0);
217 210
 		}
218 211
 	} while(0);
219
-#endif
220 212
 	return 1;
221 213
 }
222 214
 
... ...
@@ -235,11 +227,7 @@ static int tls_bio_mbuf_read(BIO* b, char* dst, int dst_len)
235 227
 
236 228
 	ret = 0;
237 229
 	if (likely(dst)) {
238
-#if OPENSSL_VERSION_NUMBER < 0x010100000L
239
-		d = b->ptr;
240
-#else
241 230
 		d = BIO_get_data(b);
242
-#endif
243 231
 		BIO_clear_retry_flags(b);
244 232
 		if (unlikely(d == 0 || d->rd->buf == 0)) {
245 233
 			if (d == 0)
... ...
@@ -291,11 +279,7 @@ static int tls_bio_mbuf_write(BIO* b, const char* src, int src_len)
291 279
 	int ret;
292 280
 
293 281
 	ret = 0;
294
-#if OPENSSL_VERSION_NUMBER < 0x010100000L
295
-	d = b->ptr;
296
-#else
297 282
 	d = BIO_get_data(b);
298
-#endif
299 283
 	BIO_clear_retry_flags(b);
300 284
 	if (unlikely(d == 0 || d->wr->buf == 0)) {
301 285
 		if (d == 0)
... ...
@@ -124,7 +124,7 @@ static void setup_dh(SSL_CTX *ctx)
124 124
 		return;
125 125
 	}
126 126
 
127
-#if (OPENSSL_VERSION_NUMBER >= 0x1010000fL)
127
+#if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER)
128 128
 	/* libssl >= v1.1.0 */
129 129
 	DH_set0_pqg(dh, p, NULL, g);
130 130
 #else