-
Notifications
You must be signed in to change notification settings - Fork 0
/
rustls_tls.c
205 lines (161 loc) · 5.94 KB
/
rustls_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
// SPDX-License-Identifier: MIT
#include "rustls_tls.h"
#include "operations.h"
#include "timespec.h"
#include <errno.h>
#include <limits.h>
#include <rustls.h>
typedef struct iou_rustls_iodata_s {
reactor_t * reactor;
int fd;
} iou_rustls_iodata_t;
static rustls_io_result
iou_rustls_read_callback(void *userdata, uint8_t *buf, size_t n, size_t *out_n) {
iou_rustls_iodata_t *iodata = (iou_rustls_iodata_t*)userdata;
ssize_t result = iou_read(iodata->reactor, iodata->fd, buf, n);
if (result < 0)
return result;
if (out_n)
*out_n = result;
return 0;
}
static rustls_io_result
iou_rustls_tls_in_fd(reactor_t * reactor, int fd, struct rustls_connection * connection) {
size_t n;
iou_rustls_iodata_t iodata = { .reactor = reactor, .fd = fd };
rustls_io_result result = rustls_connection_read_tls(connection, iou_rustls_read_callback, &iodata, &n);
if (result)
return result;
if (RUSTLS_RESULT_OK != rustls_connection_process_new_packets(connection))
return -EINVAL;
return n > 0 ? 0 : -EINVAL;
}
ssize_t
iou_rustls_read(reactor_t * reactor, int fd, struct rustls_connection * connection, uint8_t *buf, size_t len) {
size_t n;
rustls_result result;
rustls_io_result io_result;
do {
result = rustls_connection_read(connection, buf, len, &n);
} while (RUSTLS_RESULT_PLAINTEXT_EMPTY == result && !(io_result = iou_rustls_tls_in_fd(reactor, fd, connection)));
if (RUSTLS_RESULT_PLAINTEXT_EMPTY == result)
return io_result;
if (RUSTLS_RESULT_UNEXPECTED_EOF == result)
return -EPIPE;
if (RUSTLS_RESULT_OK != result)
return -EINVAL;
return n;
}
static rustls_io_result
iou_rustls_write_vectored_callback(void *userdata, const struct rustls_iovec *iov, size_t n, size_t *out_n) {
iou_rustls_iodata_t *iodata = (iou_rustls_iodata_t*)userdata;
ssize_t result = iou_pwritev(iodata->reactor, iodata->fd, (const struct iovec *)iov, n, -1, 0);
if (result < 0)
return -result;
if (out_n)
*out_n = result;
return 0;
}
static rustls_io_result
iou_rustls_tls_out_fd(reactor_t * reactor, int fd, struct rustls_connection * connection) {
size_t n;
iou_rustls_iodata_t iodata = { .reactor = reactor, .fd = fd };
rustls_io_result result = rustls_connection_write_tls_vectored(connection, iou_rustls_write_vectored_callback, &iodata, &n);
if (result)
return result;
return n > 0 ? 0 : -EINVAL;
}
ssize_t
iou_rustls_write(reactor_t * reactor, int fd, struct rustls_connection * connection, const uint8_t *buf, size_t len) {
size_t total = 0;
while (true) {
size_t n;
if (RUSTLS_RESULT_OK != rustls_connection_write(connection, buf + total, len - total, &n))
return total ? total : -EINVAL;
total += n;
if (total == len)
return total;
rustls_io_result result = iou_rustls_tls_out_fd(reactor, fd, connection);
if (result)
return result;
}
}
rustls_io_result
iou_rustls_flush(reactor_t * reactor, int fd, struct rustls_connection * connection) {
while (rustls_connection_wants_write(connection)) {
rustls_io_result result = iou_rustls_tls_out_fd(reactor, fd, connection);
if (result)
return result;
}
return 0;
}
rustls_io_result
iou_rustls_shutdown(reactor_t * reactor, int fd, struct rustls_connection * connection) {
rustls_connection_send_close_notify(connection);
return iou_rustls_flush(reactor, fd, connection);
}
rustls_io_result
iou_rustls_close(reactor_t * reactor, int fd, struct rustls_connection * connection) {
rustls_connection_free(connection);
return iou_close(reactor, fd);
}
static rustls_io_result
iou_rustls_process(reactor_t * reactor, int fd, struct rustls_connection * connection) {
rustls_io_result result;
result = iou_rustls_flush(reactor, fd, connection);
if (result)
return result;
while (rustls_connection_wants_read(connection) && !rustls_connection_wants_write(connection)) {
result = iou_rustls_tls_in_fd(reactor, fd, connection);
if (result)
return result;
}
return 0;
}
static rustls_io_result
iou_rustls_handshake(reactor_t * reactor, int fd, struct rustls_connection * connection) {
while (rustls_connection_is_handshaking(connection)) {
rustls_io_result result = iou_rustls_process(reactor, fd, connection);
if (result)
return result;
}
return 0;
}
static void
iou_rustls_log_callback(void *userdata, const struct rustls_log_params *params) {
reactor_t * reactor = (reactor_t *)userdata;
struct rustls_str level_str = rustls_log_level_str(params->level);
iou_printf(reactor, STDERR_FILENO, "%.*s: %.*s\n"
, level_str.len, level_str.data
, params->message.len, params->message.data
);
}
struct rustls_connection *
iou_rustls_accept(reactor_t * reactor, int fd, const struct rustls_server_config *config) {
struct rustls_connection *connection;
rustls_result result = rustls_server_connection_new(config, &connection);
if (RUSTLS_RESULT_OK != result)
return NULL;
if (iou_rustls_handshake(reactor, fd, connection)) {
iou_close_fast(reactor, fd);
rustls_connection_free(connection);
return NULL;
}
return connection;
}
struct rustls_connection *
iou_rustls_connect(reactor_t * reactor, int fd, struct rustls_client_config_builder *config_builder, const char *host) {
const struct rustls_client_config *config = rustls_client_config_builder_build(config_builder);
struct rustls_connection *connection;
rustls_result result = rustls_client_connection_new(config, host, &connection);
rustls_client_config_free(config);
if (RUSTLS_RESULT_OK != result)
return NULL;
if (iou_rustls_handshake(reactor, fd, connection)) {
iou_close_fast(reactor, fd);
rustls_connection_free(connection);
return NULL;
}
return connection;
}
//