summaryrefslogtreecommitdiff
path: root/regress/lib/libssl/api/apitest.c
blob: b5a5c544e2f1c46114086f1af8fcadeb2cedb974 (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
/* $OpenBSD: apitest.c,v 1.1 2022/01/05 09:59:39 jsing Exp $ */
/*
 * Copyright (c) 2020, 2021 Joel Sing <jsing@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.
 */

#include <err.h>

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

const char *certs_path;

int debug = 0;

static int
ssl_ctx_use_ca_file(SSL_CTX *ssl_ctx, const char *ca_file)
{
	char *ca_path = NULL;
	int ret = 0;

	if (asprintf(&ca_path, "%s/%s", certs_path, ca_file) == -1)
		goto err;
	if (!SSL_CTX_load_verify_locations(ssl_ctx, ca_path, NULL)) {
		fprintf(stderr, "load_verify_locations(%s) failed\n", ca_path);
		goto err;
	}

	ret = 1;

 err:
	free(ca_path);

	return ret;
}

static int
ssl_ctx_use_keypair(SSL_CTX *ssl_ctx, const char *chain_file,
    const char *key_file)
{
	char *chain_path = NULL, *key_path = NULL;
	int ret = 0;

	if (asprintf(&chain_path, "%s/%s", certs_path, chain_file) == -1)
		goto err;
	if (SSL_CTX_use_certificate_chain_file(ssl_ctx, chain_path) != 1) {
		fprintf(stderr, "FAIL: Failed to load certificates\n");
		goto err;
	}
	if (asprintf(&key_path, "%s/%s", certs_path, key_file) == -1)
		goto err;
	if (SSL_CTX_use_PrivateKey_file(ssl_ctx, key_path,
	    SSL_FILETYPE_PEM) != 1) {
		fprintf(stderr, "FAIL: Failed to load private key\n");
		goto err;
	}

	ret = 1;

 err:
	free(chain_path);
	free(key_path);

	return ret;
}

static SSL *
tls_client(BIO *rbio, BIO *wbio)
{
	SSL_CTX *ssl_ctx = NULL;
	SSL *ssl = NULL;

	if ((ssl_ctx = SSL_CTX_new(TLS_method())) == NULL)
		errx(1, "client context");

	SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL);

	if (!ssl_ctx_use_ca_file(ssl_ctx, "ca-root-rsa.pem"))
		goto failure;
	if (!ssl_ctx_use_keypair(ssl_ctx, "client1-rsa-chain.pem",
	    "client1-rsa.pem"))
		goto failure;

	if ((ssl = SSL_new(ssl_ctx)) == NULL)
		errx(1, "client ssl");

	BIO_up_ref(rbio);
	BIO_up_ref(wbio);

	SSL_set_bio(ssl, rbio, wbio);

 failure:
	SSL_CTX_free(ssl_ctx);

	return ssl;
}

static SSL *
tls_server(BIO *rbio, BIO *wbio)
{
	SSL_CTX *ssl_ctx = NULL;
	SSL *ssl = NULL;

	if ((ssl_ctx = SSL_CTX_new(TLS_method())) == NULL)
		errx(1, "server context");

	SSL_CTX_set_dh_auto(ssl_ctx, 2);

	SSL_CTX_set_verify(ssl_ctx,
	    SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT, NULL);

	if (!ssl_ctx_use_ca_file(ssl_ctx, "ca-root-rsa.pem"))
		goto failure;
	if (!ssl_ctx_use_keypair(ssl_ctx, "server1-rsa-chain.pem",
	    "server1-rsa.pem"))
		goto failure;

	if ((ssl = SSL_new(ssl_ctx)) == NULL)
		errx(1, "server ssl");

	BIO_up_ref(rbio);
	BIO_up_ref(wbio);

	SSL_set_bio(ssl, rbio, wbio);

 failure:
	SSL_CTX_free(ssl_ctx);

	return ssl;
}

static int
ssl_error(SSL *ssl, const char *name, const char *desc, int ssl_ret)
{
	int ssl_err;

	ssl_err = SSL_get_error(ssl, ssl_ret);

	if (ssl_err == SSL_ERROR_WANT_READ) {
		return 1;
	} else if (ssl_err == SSL_ERROR_WANT_WRITE) {
		return 1;
	} else if (ssl_err == SSL_ERROR_SYSCALL && errno == 0) {
		/* Yup, this is apparently a thing... */
	} else {
		fprintf(stderr, "FAIL: %s %s failed - ssl err = %d, errno = %d\n",
		    name, desc, ssl_err, errno);
		ERR_print_errors_fp(stderr);
		return 0;
	}

	return 1;
}

static int
do_connect(SSL *ssl, const char *name, int *done)
{
	int ssl_ret;

	if ((ssl_ret = SSL_connect(ssl)) == 1) {
		fprintf(stderr, "INFO: %s connect done\n", name);
		*done = 1;
		return 1;
	}

	return ssl_error(ssl, name, "connect", ssl_ret);
}

static int
do_accept(SSL *ssl, const char *name, int *done)
{
	int ssl_ret;

	if ((ssl_ret = SSL_accept(ssl)) == 1) {
		fprintf(stderr, "INFO: %s accept done\n", name);
		*done = 1;
		return 1;
	}

	return ssl_error(ssl, name, "accept", ssl_ret);
}

typedef int (*ssl_func)(SSL *ssl, const char *name, int *done);

static int
do_client_server_loop(SSL *client, ssl_func client_func, SSL *server,
    ssl_func server_func)
{
	int client_done = 0, server_done = 0;
	int i = 0;

	do {
		if (!client_done) {
			if (debug)
				fprintf(stderr, "DEBUG: client loop\n");
			if (!client_func(client, "client", &client_done))
				return 0;
		}
		if (!server_done) {
			if (debug)
				fprintf(stderr, "DEBUG: server loop\n");
			if (!server_func(server, "server", &server_done))
				return 0;
		}
	} while (i++ < 100 && (!client_done || !server_done));

	if (!client_done || !server_done)
		fprintf(stderr, "FAIL: gave up\n");

	return client_done && server_done;
}

static int
ssl_get_peer_cert_chain_test(uint16_t tls_version)
{
	STACK_OF(X509) *peer_chain;
	X509 *peer_cert;
	BIO *client_wbio = NULL, *server_wbio = NULL;
	SSL *client = NULL, *server = NULL;
	int failed = 1;

	if ((client_wbio = BIO_new(BIO_s_mem())) == NULL)
		goto failure;
	if (BIO_set_mem_eof_return(client_wbio, -1) <= 0)
		goto failure;

	if ((server_wbio = BIO_new(BIO_s_mem())) == NULL)
		goto failure;
	if (BIO_set_mem_eof_return(server_wbio, -1) <= 0)
		goto failure;

	if ((client = tls_client(server_wbio, client_wbio)) == NULL)
		goto failure;
	if (tls_version != 0) {
		if (!SSL_set_min_proto_version(client, tls_version))
			goto failure;
		if (!SSL_set_max_proto_version(client, tls_version))
			goto failure;
	}

	if ((server = tls_server(client_wbio, server_wbio)) == NULL)
		goto failure;
	if (tls_version != 0) {
		if (!SSL_set_min_proto_version(server, tls_version))
			goto failure;
		if (!SSL_set_max_proto_version(server, tls_version))
			goto failure;
	}

	if (!do_client_server_loop(client, do_connect, server, do_accept)) {
		fprintf(stderr, "FAIL: client and server handshake failed\n");
		goto failure;
	}

	if (tls_version != 0) {
		if (SSL_version(client) != tls_version) {
			fprintf(stderr, "FAIL: client got TLS version %x, "
			    "want %x\n", SSL_version(client), tls_version);
			goto failure;
		}
		if (SSL_version(server) != tls_version) {
			fprintf(stderr, "FAIL: server got TLS version %x, "
			    "want %x\n", SSL_version(server), tls_version);
			goto failure;
		}
	}

	/*
	 * Due to the wonders of API inconsistency, SSL_get_peer_cert_chain()
	 * includes the peer's leaf certificate when called by the client,
	 * however it does not when called by the server. Futhermore, the
	 * certificate returned by SSL_get_peer_certificate() has already
	 * had its reference count incremented and must be freed, where as
	 * the certificates returned from SSL_get_peer_cert_chain() must
	 * not be freed... *sigh*
	 */
	peer_cert = SSL_get_peer_certificate(client);
	peer_chain = SSL_get_peer_cert_chain(client);
	X509_free(peer_cert);

	if (peer_cert == NULL) {
		fprintf(stderr, "FAIL: client got no peer cert\n");
		goto failure;
	}
	if (sk_X509_num(peer_chain) != 2) {
		fprintf(stderr, "FAIL: client got peer cert chain with %d "
		    "certificates, want 2\n", sk_X509_num(peer_chain));
		goto failure;
	}
	if (X509_cmp(peer_cert, sk_X509_value(peer_chain, 0)) != 0) {
		fprintf(stderr, "FAIL: client got peer cert chain without peer "
		    "certificate\n");
		goto failure;
	}

	peer_cert = SSL_get_peer_certificate(server);
	peer_chain = SSL_get_peer_cert_chain(server);
	X509_free(peer_cert);

	if (peer_cert == NULL) {
		fprintf(stderr, "FAIL: server got no peer cert\n");
		goto failure;
	}
	if (sk_X509_num(peer_chain) != 1) {
		fprintf(stderr, "FAIL: server got peer cert chain with %d "
		    "certificates, want 1\n", sk_X509_num(peer_chain));
		goto failure;
	}
	if (X509_cmp(peer_cert, sk_X509_value(peer_chain, 0)) == 0) {
		fprintf(stderr, "FAIL: server got peer cert chain with peer "
		    "certificate\n");
		goto failure;
	}

	fprintf(stderr, "INFO: Done!\n");

	failed = 0;

 failure:
	BIO_free(client_wbio);
	BIO_free(server_wbio);

	SSL_free(client);
	SSL_free(server);

	return failed;
}

static int
ssl_get_peer_cert_chain_tests(void)
{
	int failed = 0;

	fprintf(stderr, "\n== Testing SSL_get_peer_cert_chain()... ==\n");

	failed |= ssl_get_peer_cert_chain_test(0);
	failed |= ssl_get_peer_cert_chain_test(TLS1_3_VERSION);
	failed |= ssl_get_peer_cert_chain_test(TLS1_2_VERSION);

	return failed;
}

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

	if (argc != 2) {
		fprintf(stderr, "usage: %s certspath\n", argv[0]);
		exit(1);
	}
	certs_path = argv[1];

	failed |= ssl_get_peer_cert_chain_tests();

	return failed;
}