This repository has been archived by the owner on Sep 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.c
292 lines (231 loc) · 6.27 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
/**
* \file
*
* \internal
*/
#include "config.h"
#include <assert.h>
#include <errno.h>
#include <nyanttp/tls.h>
#include <nyanttp/expect.h>
int ny_tls_init(struct ny_tls *restrict tls, struct ny *restrict ny,
char const *prio, uint_least32_t maxsess) {
assert(tls);
assert(ny);
int _;
int ret = -1;
/* Initialise structure */
tls->data = NULL;
tls->ny = ny;
tls->tls_error = NULL;
tls->sess_error = NULL;
tls->sess_destroy = NULL;
tls->sess_readable = NULL;
tls->sess_writable = NULL;
tls->trans_recv = NULL;
tls->trans_send_vec = NULL;
tls->trans_close = NULL;
/* Initialise GnuTLS */
gnutls_global_init();
char const *err_pos;
_ = gnutls_priority_init(&tls->prio_cache,
prio ? prio : NY_TLS_DEFAULT_PRIO, &err_pos);
if (unlikely(_)) {
ny_error_set(&tls->ny->error, NY_ERROR_DOMAIN_GTLS, _);
goto exit;
}
/* Generate Diffie‐Hellman parameters */
unsigned int dh_bits = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH,
GNUTLS_SEC_PARAM_HIGH);
_ = gnutls_dh_params_init(&tls->dh_params);
if (unlikely(_)) {
ny_error_set(&tls->ny->error, NY_ERROR_DOMAIN_GTLS, _);
goto deinit_gtls;
}
_ = gnutls_dh_params_generate2(tls->dh_params, dh_bits);
if (unlikely(_)) {
ny_error_set(&tls->ny->error, NY_ERROR_DOMAIN_GTLS, _);
goto exit;
}
/* Initialise allocator */
_ = ny_alloc_init(&tls->alloc_sess, ny, maxsess,
sizeof (struct ny_tls_sess));
if (unlikely(_))
goto deinit_dh;
ret = 0;
goto exit;
deinit_dh:
/* Deinitialise Diffie‐Hellman parameters */
gnutls_dh_params_deinit(tls->dh_params);
deinit_gtls:
/* Deinitialise GnuTLS */
gnutls_global_deinit();
exit:
return ret;
}
void ny_tls_destroy(struct ny_tls *restrict tls) {
assert(tls);
/* Deinitialise Diffie‐Hellman parameters */
gnutls_dh_params_deinit(tls->dh_params);
/* Deinitialise GnuTLS */
gnutls_global_deinit();
}
static ssize_t gtls_recv(struct ny_tls_sess *restrict sess,
void *restrict buffer, size_t length) {
assert(sess);
ssize_t rlen = sess->tls->trans_recv(sess->trans, buffer, length);
if (unlikely(rlen == 0)) {
rlen = -1;
gnutls_transport_set_errno(sess->session, EAGAIN);
}
else if (unlikely(rlen < 0)) {
gnutls_transport_set_errno(sess->session,
likely(sess->tls->ny->error.domain == NY_ERROR_DOMAIN_ERRNO) ?
sess->tls->ny->error.code : EINVAL );
}
return rlen;
}
static ssize_t gtls_send_vec(struct ny_tls_sess *restrict sess,
struct iovec const *restrict vector, size_t count) {
assert(sess);
assert(vector);
ssize_t wlen = sess->tls->trans_send_vec(sess->trans, vector, count);
if (unlikely(wlen == 0)) {
wlen = -1;
gnutls_transport_set_errno(sess->session, EAGAIN);
}
else if (unlikely(wlen < 0)) {
gnutls_transport_set_errno(sess->session,
likely(sess->tls->ny->error.domain == NY_ERROR_DOMAIN_ERRNO) ?
sess->tls->ny->error.code : EINVAL );
}
return wlen;
}
void ny_tls_connect(struct ny_tls *restrict tls, void *restrict trans) {
assert(tls);
int _;
struct ny_tls_sess *sess = ny_alloc_acquire(&tls->alloc_sess);
if (unlikely(!sess)) {
if (tls->tls_error)
tls->tls_error(tls, &tls->ny->error);
goto close;
}
sess->data = NULL;
sess->trans = trans;
sess->tls = tls;
_ = gnutls_init(&sess->session, GNUTLS_SERVER);
if (unlikely(_)) {
if (tls->tls_error) {
struct ny_error error;
ny_error_set(&error, NY_ERROR_DOMAIN_GTLS, _);
tls->tls_error(tls, &error);
}
goto close;
}
_ = gnutls_priority_set(sess->session, tls->prio_cache);
if (unlikely(_)) {
if (tls->tls_error) {
struct ny_error error;
ny_error_set(&error, NY_ERROR_DOMAIN_GTLS, _);
tls->tls_error(tls, &error);
}
goto deinit;
}
/* TODO: Credentials */
/* Setup transport layer */
gnutls_transport_set_ptr(sess->session, sess);
gnutls_transport_set_pull_function(sess->session,
(gnutls_pull_func) gtls_recv);
gnutls_transport_set_vec_push_function(sess->session,
(gnutls_vec_push_func) gtls_send_vec);
goto exit;
deinit:
gnutls_deinit(sess->session);
close:
tls->trans_close(trans);
release:
ny_alloc_release(&tls->alloc_sess, sess);
exit:
return;
}
static void gtls_handshake(struct ny_tls_sess *restrict sess) {
int _;
if (!sess->handshake) {
_ = gnutls_handshake(sess->session);
if (unlikely(_)) {
if (likely(_ == GNUTLS_E_AGAIN)) {
/* FIXME: Define independent event codes */
sess->tls->trans_event(sess->trans,
gnutls_record_get_direction(sess->session) ?
NY_TCP_WRITABLE : NY_TCP_READABLE);
}
else if (sess->tls->sess_error) {
struct ny_error error;
ny_error_set(&error, NY_ERROR_DOMAIN_GTLS, _);
sess->tls->sess_error(sess, &error);
}
return;
}
/* FIXME: Reset events */
sess->handshake = true;
}
}
void ny_tls_sess_readable(struct ny_tls_sess *restrict sess) {
assert(sess);
gtls_handshake(sess);
if (sess->tls->sess_readable)
sess->tls->sess_readable(sess);
}
void ny_tls_sess_writable(struct ny_tls_sess *restrict sess) {
assert (sess);
gtls_handshake(sess);
if (sess->tls->sess_writable)
sess->tls->sess_writable(sess);
}
void ny_tls_sess_destroy(struct ny_tls_sess *restrict sess) {
assert(sess);
assert(sess->tls->trans_close);
if (sess->tls->sess_destroy)
sess->tls->sess_destroy(sess);
sess->tls->trans_close(sess->trans);
/* Free session structure */
ny_alloc_release(&sess->tls->alloc_sess, sess);
}
ssize_t ny_tls_sess_recv(struct ny_tls_sess *restrict sess,
void *restrict buffer, size_t length) {
assert(sess);
assert(buffer);
ssize_t rlen = gnutls_record_recv(sess->session, buffer, length);
if (unlikely(rlen == 0)) {
rlen = -1;
ny_error_set(&sess->tls->ny->error, NY_ERROR_DOMAIN_NY, NY_ERROR_EOF);
}
else if (unlikely(rlen < 0)) {
if (rlen == GNUTLS_E_AGAIN)
rlen = 0;
else if (rlen == GNUTLS_E_REHANDSHAKE) {
sess->handshake = false;
gtls_handshake(sess);
}
else
ny_error_set(&sess->tls->ny->error, NY_ERROR_DOMAIN_GTLS, rlen);
}
return rlen;
}
ssize_t ny_tls_sess_send(struct ny_tls_sess *restrict sess,
void const *restrict buffer, size_t length) {
assert(sess);
assert(buffer);
ssize_t wlen = gnutls_record_send(sess->session, buffer, length);
if (unlikely(wlen < 0)) {
if (wlen == GNUTLS_E_AGAIN)
wlen = 0;
else if (wlen == GNUTLS_E_REHANDSHAKE) {
sess->handshake = false;
gtls_handshake(sess);
}
else
ny_error_set(&sess->tls->ny->error, NY_ERROR_DOMAIN_GTLS, wlen);
}
return wlen;
}