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
|
/* $OpenBSD: tlstest.c,v 1.1 2017/01/12 15:50:16 jsing Exp $ */
/*
* Copyright (c) 2017 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 <sys/socket.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <tls.h>
#define CIRCULAR_BUFFER_SIZE 512
unsigned char client_buffer[CIRCULAR_BUFFER_SIZE];
unsigned char *client_readptr, *client_writeptr;
unsigned char server_buffer[CIRCULAR_BUFFER_SIZE];
unsigned char *server_readptr, *server_writeptr;
int debug = 0;
static void
circular_init(void)
{
client_readptr = client_writeptr = client_buffer;
server_readptr = server_writeptr = server_buffer;
}
static ssize_t
circular_read(char *name, unsigned char *buf, size_t bufsize,
unsigned char **readptr, unsigned char *writeptr,
unsigned char *outbuf, size_t outlen)
{
unsigned char *nextptr = *readptr;
size_t n = 0;
while (n < outlen) {
if (nextptr == writeptr)
break;
*outbuf++ = *nextptr++;
if ((size_t)(nextptr - buf) >= bufsize)
nextptr = buf;
*readptr = nextptr;
n++;
}
if (debug && n > 0)
fprintf(stderr, "%s buffer: read %zi bytes\n", name, n);
return (n > 0 ? (ssize_t)n : TLS_WANT_POLLIN);
}
static ssize_t
circular_write(char *name, unsigned char *buf, size_t bufsize,
unsigned char *readptr, unsigned char **writeptr,
const unsigned char *inbuf, size_t inlen)
{
unsigned char *nextptr = *writeptr;
unsigned char *prevptr;
size_t n = 0;
while (n < inlen) {
prevptr = nextptr++;
if ((size_t)(nextptr - buf) >= bufsize)
nextptr = buf;
if (nextptr == readptr)
break;
*prevptr = *inbuf++;
*writeptr = nextptr;
n++;
}
if (debug && n > 0)
fprintf(stderr, "%s buffer: wrote %zi bytes\n", name, n);
return (n > 0 ? (ssize_t)n : TLS_WANT_POLLOUT);
}
static ssize_t
client_read(struct tls *ctx, void *buf, size_t buflen, void *cb_arg)
{
return circular_read("client", client_buffer, sizeof(client_buffer),
&client_readptr, client_writeptr, buf, buflen);
}
static ssize_t
client_write(struct tls *ctx, const void *buf, size_t buflen, void *cb_arg)
{
return circular_write("server", server_buffer, sizeof(server_buffer),
server_readptr, &server_writeptr, buf, buflen);
}
static ssize_t
server_read(struct tls *ctx, void *buf, size_t buflen, void *cb_arg)
{
return circular_read("server", server_buffer, sizeof(server_buffer),
&server_readptr, server_writeptr, buf, buflen);
}
static ssize_t
server_write(struct tls *ctx, const void *buf, size_t buflen, void *cb_arg)
{
return circular_write("client", client_buffer, sizeof(client_buffer),
client_readptr, &client_writeptr, buf, buflen);
}
static int
do_tls_handshake(char *name, struct tls *ctx)
{
int rv;
rv = tls_handshake(ctx);
if (rv == 0)
return (1);
if (rv == TLS_WANT_POLLIN || rv == TLS_WANT_POLLOUT)
return (0);
errx(1, "%s handshake failed: %s", name, tls_error(ctx));
}
static int
do_tls_close(char *name, struct tls *ctx)
{
int rv;
rv = tls_close(ctx);
if (rv == 0)
return (1);
if (rv == TLS_WANT_POLLIN || rv == TLS_WANT_POLLOUT)
return (0);
errx(1, "%s close failed: %s", name, tls_error(ctx));
}
static int
do_client_server_test(char *desc, struct tls *client, struct tls *server_cctx)
{
int i, client_done, server_done;
i = client_done = server_done = 0;
do {
if (client_done == 0)
client_done = do_tls_handshake("client", client);
if (server_done == 0)
server_done = do_tls_handshake("server", server_cctx);
} while (i++ < 100 && (client_done == 0 || server_done == 0));
if (client_done == 0 || server_done == 0) {
printf("FAIL: %s TLS handshake did not complete\n", desc);
return (1);
}
printf("INFO: %s TLS handshake completed successfully\n", desc);
/* XXX - Do some reads and writes... */
i = client_done = server_done = 0;
do {
if (client_done == 0)
client_done = do_tls_close("client", client);
if (server_done == 0)
server_done = do_tls_close("server", server_cctx);
} while (i++ < 100 && (client_done == 0 || server_done == 0));
if (client_done == 0 || server_done == 0) {
printf("FAIL: %s TLS close did not complete\n", desc);
return (1);
}
printf("INFO: %s TLS close completed successfully\n", desc);
return (0);
}
static int
test_tls_cbs(struct tls *client, struct tls *server)
{
struct tls *server_cctx;
int failure;
circular_init();
if (tls_accept_cbs(server, &server_cctx, server_read, server_write,
NULL) == -1)
errx(1, "failed to accept: %s", tls_error(server));
if (tls_connect_cbs(client, client_read, client_write, NULL,
"test") == -1)
errx(1, "failed to connect: %s", tls_error(client));
failure = do_client_server_test("callback", client, server_cctx);
tls_free(server_cctx);
return (failure);
}
static int
test_tls_fds(struct tls *client, struct tls *server)
{
struct tls *server_cctx;
int cfds[2], sfds[2];
int failure;
if (pipe2(cfds, O_NONBLOCK) == -1)
err(1, "failed to create pipe");
if (pipe2(sfds, O_NONBLOCK) == -1)
err(1, "failed to create pipe");
if (tls_accept_fds(server, &server_cctx, sfds[0], cfds[1]) == -1)
errx(1, "failed to accept: %s", tls_error(server));
if (tls_connect_fds(client, cfds[0], sfds[1], "test") == -1)
errx(1, "failed to connect: %s", tls_error(client));
failure = do_client_server_test("file descriptor", client, server_cctx);
tls_free(server_cctx);
close(cfds[0]);
close(cfds[1]);
close(sfds[0]);
close(sfds[1]);
return (failure);
}
static int
test_tls_socket(struct tls *client, struct tls *server)
{
struct tls *server_cctx;
int failure;
int sv[2];
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, PF_UNSPEC,
sv) == -1)
err(1, "failed to create socketpair");
if (tls_accept_socket(server, &server_cctx, sv[0]) == -1)
errx(1, "failed to accept: %s", tls_error(server));
if (tls_connect_socket(client, sv[1], "test") == -1)
errx(1, "failed to connect: %s", tls_error(client));
failure = do_client_server_test("socket", client, server_cctx);
tls_free(server_cctx);
close(sv[0]);
close(sv[1]);
return (failure);
}
int
main(int argc, char **argv)
{
struct tls_config *client_cfg, *server_cfg;
struct tls *client, *server;
int failure = 0;
if (argc != 4) {
fprintf(stderr, "usage: %s keyfile certfile cafile\n",
argv[0]);
return (1);
}
if (tls_init() == -1)
errx(1, "failed to initialise tls");
if ((client = tls_client()) == NULL)
errx(1, "failed to create tls client");
if ((client_cfg = tls_config_new()) == NULL)
errx(1, "failed to create tls client config");
tls_config_insecure_noverifyname(client_cfg);
if (tls_config_set_ca_file(client_cfg, argv[3]))
errx(1, "failed to set ca: %s", tls_config_error(client_cfg));
if ((server = tls_server()) == NULL)
errx(1, "failed to create tls server");
if ((server_cfg = tls_config_new()) == NULL)
errx(1, "failed to create tls server config");
if (tls_config_set_keypair_file(server_cfg, argv[1], argv[2]) == -1)
errx(1, "failed to set keypair: %s",
tls_config_error(server_cfg));
tls_reset(client);
if (tls_configure(client, client_cfg) == -1)
errx(1, "failed to configure client: %s", tls_error(client));
tls_reset(server);
if (tls_configure(server, server_cfg) == -1)
errx(1, "failed to configure server: %s", tls_error(server));
failure |= test_tls_cbs(client, server);
tls_reset(client);
if (tls_configure(client, client_cfg) == -1)
errx(1, "failed to configure client: %s", tls_error(client));
tls_reset(server);
if (tls_configure(server, server_cfg) == -1)
errx(1, "failed to configure server: %s", tls_error(server));
failure |= test_tls_fds(client, server);
tls_reset(client);
if (tls_configure(client, client_cfg) == -1)
errx(1, "failed to configure client: %s", tls_error(client));
tls_reset(server);
if (tls_configure(server, server_cfg) == -1)
errx(1, "failed to configure server: %s", tls_error(server));
failure |= test_tls_socket(client, server);
tls_free(client);
tls_free(server);
tls_config_free(client_cfg);
tls_config_free(server_cfg);
return (failure);
}
|