summaryrefslogtreecommitdiff
path: root/src/regress/lib/libssl/verify/verify.c
blob: 8784396a79219e46c66d19dd3b9b0e58ad0725ae (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*	$OpenBSD: verify.c,v 1.1.1.1 2021/08/30 17:27:45 tb Exp $ */
/*
 * Copyright (c) 2021 Theo Buehler <tb@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* Based on https://github.com/noxxi/libressl-tests */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <openssl/bio.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/x509_vfy.h>
#include <openssl/ssl.h>

struct peer_config {
	const char *name;
	int server;
	const char *cert;
	const char *key;
	const char *ca_file;
};

struct ssl_wildcard_test_data {
	const char *description;
	struct peer_config client_config;
	struct peer_config server_config;
	long verify_result;
};

static const struct ssl_wildcard_test_data ssl_wildcard_tests[] = {
	{
		.description = "unusual wildcard cert, no CA given to client",
		.client_config = {
			.name = "client",
			.server = 0,
			.cert = NULL,
			.ca_file = NULL,
		},
		.server_config = {
			.name = "server",
			.server = 1,
			.cert = "server-unusual-wildcard.pem",
			.key = "server-unusual-wildcard.pem",
		},
		/* OpenSSL returns X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE */
		.verify_result = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY,
	},

	{
		.description = "unusual wildcard cert, CA given to client",
		.client_config = {
			.name = "client",
			.server = 0,
			.cert = NULL,
			.ca_file = "caR.pem",
		},
		.server_config = {
			.name = "server",
			.server = 1,
			.cert = "server-unusual-wildcard.pem",
			.key = "server-unusual-wildcard.pem",
		},
		.verify_result = X509_V_OK,
	},

	{
		.description = "common wildcard cert, no CA given to client",
		.client_config = {
			.name = "client",
			.server = 0,
			.cert = NULL,
			.ca_file = NULL,
		},
		.server_config = {
			.name = "server",
			.server = 1,
			.cert = "server-common-wildcard.pem",
			.key = "server-common-wildcard.pem",
		},
		/* OpenSSL returns X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE */
		.verify_result = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY,
	},

	{
		.description = "common wildcard cert, CA given to client",
		.client_config = {
			.name = "client",
			.server = 0,
			.cert = NULL,
			.ca_file = "caR.pem",
		},
		.server_config = {
			.name = "server",
			.server = 1,
			.cert = "server-common-wildcard.pem",
			.key = "server-common-wildcard.pem",
		},
		.verify_result = X509_V_OK,
	},

	{
		.description = "server sends all chain certificates",
		.client_config = {
			.name = "client",
			.server = 0,
			.cert = NULL,
			.ca_file = "caR.pem",
		},
		.server_config = {
			.name = "server",
			.server = 1,
			.cert = "server-subca-chainS.pem",
			.key = "server-subca-chainS.pem",
			.ca_file = "subcaR.pem"
		},
		.verify_result = X509_V_OK,
	},
};

static const size_t N_SSL_WILDCARD_TESTS =
    sizeof(ssl_wildcard_tests) / sizeof(ssl_wildcard_tests[0]);

static SSL_CTX *
peer_config_to_ssl_ctx(const struct peer_config *config)
{
	SSL_CTX *ctx;

	if ((ctx = SSL_CTX_new(TLS_method())) == NULL) {
		fprintf(stderr, "SSL_CTX_new(%s) failed\n", config->name);
		goto err;
	}

	if (config->server) {
		if (!SSL_CTX_use_certificate_file(ctx, config->cert,
		    SSL_FILETYPE_PEM)) {
			fprintf(stderr, "use_certificate_file(%s) failed\n",
			    config->name);
			goto err;
		}
		if (config->key != NULL && !SSL_CTX_use_PrivateKey_file(ctx,
		    config->key, SSL_FILETYPE_PEM)) {
			fprintf(stderr, "use_PrivateKey_file(%s) failed\n",
			    config->name);
			goto err;
		}
	}

	if (config->ca_file != NULL) {
		if (!SSL_CTX_load_verify_locations(ctx, config->ca_file, NULL)) {
			fprintf(stderr, "load_verify_locations(%s) failed\n",
			    config->name);
			goto err;
		}
	}

	return ctx;

 err:
	SSL_CTX_free(ctx);
	return NULL;
}

/* Connect client and server via a pair of "nonblocking" memory BIOs. */
static int
connect_peers(SSL *client_ssl, SSL *server_ssl, const char *description)
{
	BIO *client_wbio = NULL, *server_wbio = NULL;
	int ret = 0;

	if ((client_wbio = BIO_new(BIO_s_mem())) == NULL) {
		fprintf(stderr, "%s: failed to create client BIO\n",
		    description);
		goto err;
	}
	if ((server_wbio = BIO_new(BIO_s_mem())) == NULL) {
		fprintf(stderr, "%s: failed to create server BIO\n",
		    description);
		goto err;
	}
	if (BIO_set_mem_eof_return(client_wbio, -1) <= 0) {
		fprintf(stderr, "%s: failed to set client eof return\n",
		    description);
		goto err;
	}
	if (BIO_set_mem_eof_return(server_wbio, -1) <= 0) {
		fprintf(stderr, "%s: failed to set server eof return\n",
		    description);
		goto err;
	}

	/* Avoid double free. SSL_set_bio() takes ownership of the BIOs. */
	BIO_up_ref(client_wbio);
	BIO_up_ref(server_wbio);

	SSL_set_bio(client_ssl, server_wbio, client_wbio);
	SSL_set_bio(server_ssl, client_wbio, server_wbio);
	client_wbio = NULL;
	server_wbio = NULL;

	ret = 1;

 err:
	BIO_free(client_wbio);
	BIO_free(server_wbio);

	return ret;
}

static int
push_data_to_peer(SSL *ssl, int *ret, int (*func)(SSL *), const char *func_name,
    const char *description)
{
	int ssl_err = 0;

	if (*ret == 1)
		return 1;

	/*
	 * Do SSL_connect/SSL_accept/SSL_shutdown once and loop while hitting
	 * WANT_WRITE.  If done or on WANT_READ hand off to peer.
	 */

	do {
		if ((*ret = func(ssl)) <= 0)
			ssl_err = SSL_get_error(ssl, *ret);
	} while (*ret <= 0 && ssl_err == SSL_ERROR_WANT_WRITE);

	/* Ignore erroneous error - see SSL_shutdown(3)... */
	if (func == SSL_shutdown && ssl_err == SSL_ERROR_SYSCALL)
		return 1;

	if (*ret <= 0 && ssl_err != SSL_ERROR_WANT_READ) {
		fprintf(stderr, "%s: %s failed\n", description, func_name);
		ERR_print_errors_fp(stderr);
		return 0;
	}

	return 1;
}

/*
 * Alternate between loops of SSL_connect() and SSL_accept() as long as only
 * WANT_READ and WANT_WRITE situations are encountered. A function is repeated
 * until WANT_READ is returned or it succeeds, then it's the other function's
 * turn to make progress. Succeeds if SSL_connect() and SSL_accept() return 1.
 */
static int
handshake(SSL *client_ssl, SSL *server_ssl, const char *description)
{
	int loops = 0, client_ret = 0, server_ret = 0;

	while (loops++ < 10 && (client_ret <= 0 || server_ret <= 0)) {
		if (!push_data_to_peer(client_ssl, &client_ret, SSL_connect,
		    "SSL_connect", description))
			return 0;

		if (!push_data_to_peer(server_ssl, &server_ret, SSL_accept,
		    "SSL_accept", description))
			return 0;
	}

	if (client_ret != 1 || server_ret != 1) {
		fprintf(stderr, "%s: failed\n", __func__);
		return 0;
	}

	return 1;
}

static int
shutdown_peers(SSL *client_ssl, SSL *server_ssl, const char *description)
{
	int loops = 0, client_ret = 0, server_ret = 0;

	while (loops++ < 10 && (client_ret <= 0 || server_ret <= 0)) {
		if (!push_data_to_peer(client_ssl, &client_ret, SSL_shutdown,
		    "client shutdown", description))
			return 0;

		if (!push_data_to_peer(server_ssl, &server_ret, SSL_shutdown,
		    "server shutdown", description))
			return 0;
	}

	if (client_ret != 1 || server_ret != 1) {
		fprintf(stderr, "%s: failed\n", __func__);
		return 0;
	}

	return 1;
}

static int
test_ssl_wildcards(const struct ssl_wildcard_test_data *test)
{
	SSL_CTX *client_ctx = NULL, *server_ctx = NULL;
	SSL *client_ssl = NULL, *server_ssl = NULL;
	long verify_result;
	int failed = 1;

	if ((client_ctx = peer_config_to_ssl_ctx(&test->client_config)) == NULL)
		goto err;
	if ((server_ctx = peer_config_to_ssl_ctx(&test->server_config)) == NULL)
		goto err;

	if ((client_ssl = SSL_new(client_ctx)) == NULL) {
		fprintf(stderr, "%s: failed to create client SSL\n",
		    test->description);
		goto err;
	}
	if ((server_ssl = SSL_new(server_ctx)) == NULL) {
		fprintf(stderr, "%s: failed to create server SSL\n",
		    test->description);
		goto err;
	}

	if (!connect_peers(client_ssl, server_ssl, test->description))
		goto err;

	if (!handshake(client_ssl, server_ssl, test->description))
		goto err;

	verify_result = SSL_get_verify_result(client_ssl);

	if (test->verify_result == verify_result) {
		failed = 0;
		fprintf(stderr, "%s: ok\n", test->description);
	} else
		fprintf(stderr, "%s: verify_result: want %ld, got %ld\n",
		    test->description, test->verify_result, verify_result);

	if (!shutdown_peers(client_ssl, server_ssl, test->description))
		goto err;

 err:
	SSL_CTX_free(client_ctx);
	SSL_CTX_free(server_ctx);
	SSL_free(client_ssl);
	SSL_free(server_ssl);

	return failed;
}

int
main(int argc, char **argv)
{
	size_t i;
	int failed = 0;

	for (i = 0; i < N_SSL_WILDCARD_TESTS; i++)
		failed |= test_ssl_wildcards(&ssl_wildcard_tests[i]);

	if (failed == 0)
		printf("PASS %s\n", __FILE__);

	return failed;
}