-
Notifications
You must be signed in to change notification settings - Fork 157
/
tls.c
492 lines (456 loc) · 15.9 KB
/
tls.c
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
Copyright (c) 2018 Martin Sustrik
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include <errno.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <stdint.h>
#include <stdlib.h>
#define DILL_DISABLE_RAW_NAMES
#include "libdillimpl.h"
#include "utils.h"
#define DILL_TLS_BUFSIZE 2048
dill_unique_id(dill_tls_type);
struct dill_tls_sock {
struct dill_hvfs hvfs;
struct dill_bsock_vfs bvfs;
SSL_CTX *ctx;
SSL *ssl;
int u;
int64_t deadline;
unsigned int indone : 1;
unsigned int outdone: 1;
unsigned int inerr : 1;
unsigned int outerr : 1;
unsigned int mem : 1;
};
DILL_CHECK_STORAGE(dill_tls_sock, dill_tls_storage)
static void dill_tls_init(void);
static BIO *dill_tls_new_cbio(void *mem);
static int dill_tls_followup(struct dill_tls_sock *self, int rc);
static void *dill_tls_hquery(struct dill_hvfs *hvfs, const void *type);
static void dill_tls_hclose(struct dill_hvfs *hvfs);
static int dill_tls_bsendl(struct dill_bsock_vfs *bvfs,
struct dill_iolist *first, struct dill_iolist *last, int64_t deadline);
static int dill_tls_brecvl(struct dill_bsock_vfs *bvfs,
struct dill_iolist *first, struct dill_iolist *last, int64_t deadline);
static void *dill_tls_hquery(struct dill_hvfs *hvfs, const void *type) {
struct dill_tls_sock *self = (struct dill_tls_sock*)hvfs;
if(type == dill_bsock_type) return &self->bvfs;
if(type == dill_tls_type) return self;
errno = ENOTSUP;
return NULL;
}
int dill_tls_attach_client_mem(int s, struct dill_tls_storage *mem,
int64_t deadline) {
int err;
if(dill_slow(!mem)) {err = EINVAL; goto error1;}
/* Check whether underlying socket is a bytestream. */
void *q = dill_hquery(s, dill_bsock_type);
if(dill_slow(!q && errno == ENOTSUP)) {err = EPROTO; goto error1;}
if(dill_slow(!q)) {err = errno; goto error1;}
/* Create OpenSSL connection context. */
dill_tls_init();
const SSL_METHOD *method = SSLv23_method();
if(dill_slow(!method)) {err = EFAULT; goto error1;}
SSL_CTX *ctx = SSL_CTX_new(method);
if(dill_slow(!ctx)) {err = EFAULT; goto error1;}
/* Create OpenSSL connection object. */
SSL *ssl = SSL_new(ctx);
if(dill_slow(!ssl)) {err = EFAULT; goto error2;}
SSL_set_connect_state(ssl);
/* Create a BIO and attach it to the connection. */
BIO *bio = dill_tls_new_cbio(mem);
if(dill_slow(!bio)) {err = errno; goto error3;}
SSL_set_bio(ssl, bio, bio);
/* Take ownership of the underlying socket. */
s = dill_hown(s);
if(dill_slow(s < 0)) {err = errno; goto error1;}
/* Create the object. */
struct dill_tls_sock *self = (struct dill_tls_sock*)mem;
self->hvfs.query = dill_tls_hquery;
self->hvfs.close = dill_tls_hclose;
self->bvfs.bsendl = dill_tls_bsendl;
self->bvfs.brecvl = dill_tls_brecvl;
self->ctx = ctx;
self->ssl = ssl;
self->u = s;
self->deadline = -1;
self->indone = 0;
self->outdone = 0;
self->inerr = 0;
self->outerr = 0;
self->mem = 1;
/* Initial handshaking. */
while(1) {
ERR_clear_error();
int rc = SSL_connect(ssl);
if(dill_tls_followup(self, rc)) break;
if(dill_slow(errno != 0)) {err = errno; goto error4;}
}
/* Create the handle. */
int h = dill_hmake(&self->hvfs);
if(dill_slow(h < 0)) {err = errno; goto error4;}
return h;
error4:
BIO_vfree(bio);
error3:
SSL_free(ssl);
error2:
SSL_CTX_free(ctx);
error1:
if(s >= 0) dill_hclose(s);
errno = err;
return -1;
}
int dill_tls_attach_client(int s, int64_t deadline) {
int err;
struct dill_tls_sock *obj = malloc(sizeof(struct dill_tls_sock));
if(dill_slow(!obj)) {err = ENOMEM; goto error1;}
s = dill_tls_attach_client_mem(s, (struct dill_tls_storage*)obj,
deadline);
if(dill_slow(s < 0)) {err = errno; goto error2;}
obj->mem = 0;
return s;
error2:
free(obj);
error1:
if(s >= 0) dill_hclose(s);
errno = err;
return -1;
}
int dill_tls_attach_server_mem(int s, const char *cert, const char *pkey,
struct dill_tls_storage *mem, int64_t deadline) {
int err;
if(dill_slow(!mem)) {err = EINVAL; goto error1;}
/* Check whether underlying socket is a bytestream. */
void *q = dill_hquery(s, dill_bsock_type);
if(dill_slow(!q && errno == ENOTSUP)) {err = EPROTO; goto error1;}
if(dill_slow(!q)) {err = errno; goto error1;}
/* Create OpenSSL connection context. */
dill_tls_init();
const SSL_METHOD *method = SSLv23_server_method();
if(dill_slow(!method)) {err = EFAULT; goto error1;}
SSL_CTX *ctx = SSL_CTX_new(method);
if(dill_slow(!ctx)) {err = EFAULT; goto error1;}
int rc = SSL_CTX_set_ecdh_auto(ctx, 1);
if(dill_slow(rc != 1)) {err = EFAULT; goto error2;}
rc = SSL_CTX_use_certificate_file(ctx, cert, SSL_FILETYPE_PEM);
if(dill_slow(rc <= 0)) {err = EFAULT; goto error2;}
rc = SSL_CTX_use_PrivateKey_file(ctx, pkey, SSL_FILETYPE_PEM);
if(dill_slow(rc <= 0 )) {err = EFAULT; goto error2;}
/* Create OpenSSL connection object. */
SSL *ssl = SSL_new(ctx);
if(dill_slow(!ssl)) {err = EFAULT; goto error2;}
SSL_set_accept_state(ssl);
/* Create a BIO and attach it to the connection. */
BIO *bio = dill_tls_new_cbio(mem);
if(dill_slow(!bio)) {err = errno; goto error3;}
SSL_set_bio(ssl, bio, bio);
/* Take ownership of the underlying socket. */
s = dill_hown(s);
if(dill_slow(s < 0)) {err = errno; goto error1;}
/* Create the object. */
struct dill_tls_sock *self = (struct dill_tls_sock*)mem;
self->hvfs.query = dill_tls_hquery;
self->hvfs.close = dill_tls_hclose;
self->bvfs.bsendl = dill_tls_bsendl;
self->bvfs.brecvl = dill_tls_brecvl;
self->ctx = ctx;
self->ssl = ssl;
self->u = s;
self->deadline = -1;
self->indone = 0;
self->outdone = 0;
self->inerr = 0;
self->outerr = 0;
self->mem = 1;
/* Initial handshaking. */
while(1) {
ERR_clear_error();
rc = SSL_accept(ssl);
if(dill_tls_followup(self, rc)) break;
if(dill_slow(errno != 0)) {err = errno; goto error4;}
}
/* Create the handle. */
int h = dill_hmake(&self->hvfs);
if(dill_slow(h < 0)) {err = errno; goto error4;}
return h;
error4:
BIO_vfree(bio);
error3:
SSL_free(ssl);
error2:
SSL_CTX_free(ctx);
error1:
if(s >= 0) dill_hclose(s);
errno = err;
return -1;
}
int dill_tls_attach_server(int s, const char *cert, const char *pkey,
int64_t deadline) {
int err;
struct dill_tls_sock *obj = malloc(sizeof(struct dill_tls_sock));
if(dill_slow(!obj)) {err = ENOMEM; goto error1;}
s = dill_tls_attach_server_mem(s, cert, pkey,
(struct dill_tls_storage*)obj, deadline);
if(dill_slow(s < 0)) {err = errno; goto error2;}
obj->mem = 0;
return s;
error2:
free(obj);
error1:
if(s >= 0) dill_hclose(s);
errno = err;
return -1;
}
int dill_tls_done(int s, int64_t deadline) {
struct dill_tls_sock *self = dill_hquery(s, dill_tls_type);
if(dill_slow(!self)) return -1;
if(dill_slow(self->outerr)) {errno = ECONNRESET; return -1;}
if(dill_slow(self->outdone)) {errno = EPIPE; return -1;}
while(1) {
ERR_clear_error();
int rc = SSL_shutdown(self->ssl);
if(rc == 0) {self->outdone = 1; return 0;}
if(rc == 1) {self->outdone = 1; self->indone = 1; return 0;}
if(dill_tls_followup(self, rc)) break;
}
dill_assert(errno != 0);
self->outerr = 1;
return -1;
}
int dill_tls_detach(int s, int64_t deadline) {
int err;
struct dill_tls_sock *self = dill_hquery(s, dill_tls_type);
if(dill_slow(!self)) return -1;
if(dill_slow(self->inerr || self->outerr)) {err = ECONNRESET; goto error;}
/* Start terminal TLS handshake. */
if(!self->outdone) {
int rc = dill_tls_done(s, deadline);
if(dill_slow(rc < 0)) {err = errno; goto error;}
}
/* Wait for the handshake acknowledgement from the peer. */
if(!self->indone) {
while(1) {
ERR_clear_error();
int rc = SSL_shutdown(self->ssl);
if(rc == 1) break;
if(dill_tls_followup(self, rc)) {err = errno; goto error;}
}
}
int u = self->u;
self->u = -1;
dill_tls_hclose(&self->hvfs);
return u;
error:
dill_tls_hclose(&self->hvfs);
errno = err;
return -1;
}
static int dill_tls_bsendl(struct dill_bsock_vfs *bvfs,
struct dill_iolist *first, struct dill_iolist *last, int64_t deadline) {
struct dill_tls_sock *self = dill_cont(bvfs, struct dill_tls_sock, bvfs);
if(dill_slow(self->outdone)) {errno = EPIPE; return -1;}
if(dill_slow(self->outerr)) {errno = ECONNRESET; return -1;}
self->deadline = deadline;
struct dill_iolist *it = first;
while(1) {
uint8_t *base = it->iol_base;
size_t len = it->iol_len;
while(len > 0) {
ERR_clear_error();
int rc = SSL_write(self->ssl, base, len);
if(dill_tls_followup(self, rc)) {
if(dill_slow(errno != 0)) {
self->outerr = 1;
return -1;
}
base += rc;
len -= rc;
}
}
if(it == last) break;
it = it->iol_next;
}
return 0;
}
static int dill_tls_brecvl(struct dill_bsock_vfs *bvfs,
struct dill_iolist *first, struct dill_iolist *last, int64_t deadline) {
struct dill_tls_sock *self = dill_cont(bvfs, struct dill_tls_sock, bvfs);
if(dill_slow(self->indone)) {errno = EPIPE; return -1;}
if(dill_slow(self->inerr)) {errno = ECONNRESET; return -1;}
self->deadline = deadline;
struct dill_iolist *it = first;
while(1) {
uint8_t *base = it->iol_base;
size_t len = it->iol_len;
while(1) {
ERR_clear_error();
int rc = SSL_read(self->ssl, base, len);
if(dill_tls_followup(self, rc)) {
if(dill_slow(errno != 0)) {
if(errno == EPIPE) self->indone = 1;
else self->inerr = 1;
return -1;
}
if(rc == len) break;
base += rc;
len -= rc;
}
}
if(it == last) break;
it = it->iol_next;
}
return 0;
}
static void dill_tls_hclose(struct dill_hvfs *hvfs) {
struct dill_tls_sock *self = (struct dill_tls_sock*)hvfs;
SSL_free(self->ssl);
SSL_CTX_free(self->ctx);
if(dill_fast(self->u >= 0)) {
int rc = dill_hclose(self->u);
dill_assert(rc == 0);
}
if(!self->mem) free(self);
}
/******************************************************************************/
/* Helpers. */
/******************************************************************************/
/* OpenSSL has huge amount of underspecified errors. It's hard to deal
with them in a consistent manner. Morever, you can get multiple of
them at the same time. There's nothing better to do than to print them to
the stderr and return generic EFAULT error instead. */
static void dill_tls_process_errors(void) {
char errstr[512];
while(1) {
int err = ERR_get_error();
if(err == 0) break;
ERR_error_string_n(err, errstr, sizeof(errstr));
fprintf(stderr, "SSL error: %s\n", errstr);
}
errno = EFAULT;
}
/* Do the follow up work after calling a SSL function.
Returns 0 if the SSL function has to be restarted, 1 is we are done.
In the latter case, error code is in errno.
In the case of success errno is set to zero. */
static int dill_tls_followup(struct dill_tls_sock *self, int rc) {
int err;
char errstr[120];
int code = SSL_get_error(self->ssl, rc);
switch(code) {
case SSL_ERROR_NONE:
/* Operation finished. */
errno = 0;
return 1;
case SSL_ERROR_ZERO_RETURN:
/* Connection terminated by peer. */
errno = EPIPE;
return 1;
case SSL_ERROR_SYSCALL:
/* Error from our custom BIO. */
dill_assert(rc == -1);
if(errno == 0) return 0;
return 1;
case SSL_ERROR_SSL:
dill_tls_process_errors();
return 1;
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
/* These two should never happen -- our custom BIO is blocking. */
dill_assert(0);
default:
/* Unexpected error. Let's at least print out current error queue
for debugging purposes. */
fprintf(stderr, "SSL error code: %d\n", code);
dill_tls_process_errors();
dill_assert(0);
}
}
/******************************************************************************/
/* Custom BIO on top of a bsock. */
/******************************************************************************/
static BIO_METHOD *dill_tls_cbio = NULL;
static BIO *dill_tls_new_cbio(void *mem) {
BIO *bio = BIO_new(dill_tls_cbio);
if(dill_slow(!bio)) {errno = EFAULT; return NULL;}
BIO_set_data(bio, mem);
BIO_set_init(bio, 1);
return bio;
}
static int dill_tls_cbio_create(BIO *bio) {
return 1;
}
static int dill_tls_cbio_destroy(BIO *bio) {
return 1;
}
static int dill_tls_cbio_write(BIO *bio, const char *buf, int len) {
struct dill_tls_sock *self = (struct dill_tls_sock*)BIO_get_data(bio);
int rc = dill_bsend(self->u, buf, len, self->deadline);
if(dill_slow(rc < 0)) return -1;
return len;
}
static int dill_tls_cbio_read(BIO *bio, char *buf, int len) {
struct dill_tls_sock *self = (struct dill_tls_sock*)BIO_get_data(bio);
int rc = dill_brecv(self->u, buf, len, self->deadline);
if(dill_slow(rc < 0)) return -1;
return len;
}
static long dill_tls_cbio_ctrl(BIO *bio, int cmd, long larg, void *parg) {
switch(cmd) {
case BIO_CTRL_FLUSH:
return 1;
case BIO_CTRL_PUSH:
case BIO_CTRL_POP:
return 0;
default:
dill_assert(0);
}
}
static void dill_tls_term(void) {
BIO_meth_free(dill_tls_cbio);
ERR_free_strings();
EVP_cleanup();
}
static void dill_tls_init(void) {
static int init = 0;
if(dill_slow(!init)) {
SSL_library_init();
SSL_load_error_strings();
/* Create our own custom BIO type. */
int idx = BIO_get_new_index();
dill_tls_cbio = BIO_meth_new(idx, "bsock");
dill_assert(dill_tls_cbio);
int rc = BIO_meth_set_create(dill_tls_cbio, dill_tls_cbio_create);
dill_assert(rc == 1);
rc = BIO_meth_set_destroy(dill_tls_cbio, dill_tls_cbio_destroy);
dill_assert(rc == 1);
rc = BIO_meth_set_write(dill_tls_cbio, dill_tls_cbio_write);
dill_assert(rc == 1);
rc = BIO_meth_set_read(dill_tls_cbio, dill_tls_cbio_read);
dill_assert(rc == 1);
rc = BIO_meth_set_ctrl(dill_tls_cbio, dill_tls_cbio_ctrl);
dill_assert(rc == 1);
/* Deallocate the method once the process exits. */
rc = atexit(dill_tls_term);
dill_assert(rc == 0);
init = 1;
}
}