| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#ifndef _WIN32 |
| 18 |
|
| 19 |
#if defined(__FreeBSD__) || defined(__APPLE__) |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#undef _POSIX_C_SOURCE |
| 28 |
#endif |
| 29 |
#include <sys/socket.h> |
| 30 |
|
| 31 |
#ifdef S2N_LINUX_SENDFILE |
| 32 |
#include <sys/sendfile.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
#include "error/s2n_errno.h" |
| 36 |
#include "tls/s2n_ktls.h" |
| 37 |
#include "tls/s2n_tls.h" |
| 38 |
#include "utils/s2n_io.h" |
| 39 |
#include "utils/s2n_result.h" |
| 40 |
#include "utils/s2n_safety.h" |
| 41 |
#include "utils/s2n_socket.h" |
| 42 |
|
| 43 |
|
| 44 |
#define S2N_KTLS_RECORD_TYPE_SIZE (sizeof(uint8_t)) |
| 45 |
#define S2N_KTLS_CONTROL_BUFFER_SIZE (CMSG_SPACE(S2N_KTLS_RECORD_TYPE_SIZE)) |
| 46 |
|
| 47 |
#define S2N_MAX_STACK_IOVECS 16 |
| 48 |
#define S2N_MAX_STACK_IOVECS_MEM (S2N_MAX_STACK_IOVECS * sizeof(struct iovec)) |
| 49 |
|
| 50 |
|
| 51 |
static ssize_t s2n_ktls_default_sendmsg(void *io_context, const struct msghdr *msg); |
| 52 |
static ssize_t s2n_ktls_default_recvmsg(void *io_context, struct msghdr *msg); |
| 53 |
s2n_ktls_sendmsg_fn s2n_sendmsg_fn = s2n_ktls_default_sendmsg; |
| 54 |
s2n_ktls_recvmsg_fn s2n_recvmsg_fn = s2n_ktls_default_recvmsg; |
| 55 |
|
| 56 |
S2N_RESULT s2n_ktls_set_sendmsg_cb(struct s2n_connection *conn, s2n_ktls_sendmsg_fn send_cb, |
| 57 |
void *send_ctx) |
| 58 |
{ |
| 59 |
RESULT_ENSURE_REF(conn); |
| 60 |
RESULT_ENSURE_REF(send_ctx); |
| 61 |
RESULT_ENSURE(s2n_in_test(), S2N_ERR_NOT_IN_TEST); |
| 62 |
conn->send_io_context = send_ctx; |
| 63 |
s2n_sendmsg_fn = send_cb; |
| 64 |
return S2N_RESULT_OK; |
| 65 |
} |
| 66 |
|
| 67 |
S2N_RESULT s2n_ktls_set_recvmsg_cb(struct s2n_connection *conn, s2n_ktls_recvmsg_fn recv_cb, |
| 68 |
void *recv_ctx) |
| 69 |
{ |
| 70 |
RESULT_ENSURE_REF(conn); |
| 71 |
RESULT_ENSURE_REF(recv_ctx); |
| 72 |
RESULT_ENSURE(s2n_in_test(), S2N_ERR_NOT_IN_TEST); |
| 73 |
conn->recv_io_context = recv_ctx; |
| 74 |
s2n_recvmsg_fn = recv_cb; |
| 75 |
return S2N_RESULT_OK; |
| 76 |
} |
| 77 |
|
| 78 |
static ssize_t s2n_ktls_default_recvmsg(void *io_context, struct msghdr *msg) |
| 79 |
{ |
| 80 |
POSIX_ENSURE_REF(io_context); |
| 81 |
POSIX_ENSURE_REF(msg); |
| 82 |
|
| 83 |
const struct s2n_socket_read_io_context *peer_socket_ctx = io_context; |
| 84 |
POSIX_ENSURE_REF(peer_socket_ctx); |
| 85 |
int fd = peer_socket_ctx->fd; |
| 86 |
|
| 87 |
return recvmsg(fd, msg, 0); |
| 88 |
} |
| 89 |
|
| 90 |
static ssize_t s2n_ktls_default_sendmsg(void *io_context, const struct msghdr *msg) |
| 91 |
{ |
| 92 |
POSIX_ENSURE_REF(io_context); |
| 93 |
POSIX_ENSURE_REF(msg); |
| 94 |
|
| 95 |
const struct s2n_socket_write_io_context *peer_socket_ctx = io_context; |
| 96 |
POSIX_ENSURE_REF(peer_socket_ctx); |
| 97 |
int fd = peer_socket_ctx->fd; |
| 98 |
|
| 99 |
return sendmsg(fd, msg, 0); |
| 100 |
} |
| 101 |
|
| 102 |
S2N_RESULT s2n_ktls_set_control_data(struct msghdr *msg, char *buf, size_t buf_size, |
| 103 |
int cmsg_type, uint8_t record_type) |
| 104 |
{ |
| 105 |
RESULT_ENSURE_REF(msg); |
| 106 |
RESULT_ENSURE_REF(buf); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
msg->msg_control = buf; |
| 115 |
msg->msg_controllen = buf_size; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
struct cmsghdr *hdr = CMSG_FIRSTHDR(msg); |
| 123 |
RESULT_ENSURE_REF(hdr); |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
hdr->cmsg_len = CMSG_LEN(S2N_KTLS_RECORD_TYPE_SIZE); |
| 132 |
hdr->cmsg_level = S2N_SOL_TLS; |
| 133 |
hdr->cmsg_type = cmsg_type; |
| 134 |
*CMSG_DATA(hdr) = record_type; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
RESULT_ENSURE_GTE(msg->msg_controllen, CMSG_SPACE(S2N_KTLS_RECORD_TYPE_SIZE)); |
| 143 |
msg->msg_controllen = CMSG_SPACE(S2N_KTLS_RECORD_TYPE_SIZE); |
| 144 |
|
| 145 |
return S2N_RESULT_OK; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
S2N_RESULT s2n_ktls_get_control_data(struct msghdr *msg, int cmsg_type, uint8_t *record_type) |
| 157 |
{ |
| 158 |
RESULT_ENSURE_REF(msg); |
| 159 |
RESULT_ENSURE_REF(record_type); |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
if (msg->msg_flags & MSG_CTRUNC) { |
| 165 |
RESULT_BAIL(S2N_ERR_KTLS_BAD_CMSG); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
RESULT_ENSURE(msg->msg_control, S2N_ERR_SAFETY); |
| 175 |
RESULT_ENSURE(msg->msg_controllen >= CMSG_SPACE(S2N_KTLS_RECORD_TYPE_SIZE), S2N_ERR_SAFETY); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
struct cmsghdr *hdr = CMSG_FIRSTHDR(msg); |
| 182 |
RESULT_ENSURE(hdr, S2N_ERR_KTLS_BAD_CMSG); |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
RESULT_ENSURE(hdr->cmsg_level == S2N_SOL_TLS, S2N_ERR_KTLS_BAD_CMSG); |
| 191 |
RESULT_ENSURE(hdr->cmsg_type == cmsg_type, S2N_ERR_KTLS_BAD_CMSG); |
| 192 |
RESULT_ENSURE(hdr->cmsg_len == CMSG_LEN(S2N_KTLS_RECORD_TYPE_SIZE), S2N_ERR_KTLS_BAD_CMSG); |
| 193 |
*record_type = *CMSG_DATA(hdr); |
| 194 |
|
| 195 |
return S2N_RESULT_OK; |
| 196 |
} |
| 197 |
|
| 198 |
S2N_RESULT s2n_ktls_sendmsg(void *io_context, uint8_t record_type, const struct iovec *msg_iov, |
| 199 |
size_t msg_iovlen, s2n_blocked_status *blocked, size_t *bytes_written) |
| 200 |
{ |
| 201 |
RESULT_ENSURE_REF(bytes_written); |
| 202 |
RESULT_ENSURE_REF(blocked); |
| 203 |
RESULT_ENSURE(msg_iov != NULL || msg_iovlen == 0, S2N_ERR_NULL); |
| 204 |
|
| 205 |
*blocked = S2N_BLOCKED_ON_WRITE; |
| 206 |
*bytes_written = 0; |
| 207 |
|
| 208 |
struct msghdr msg = { |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
.msg_iov = (struct iovec *) (uintptr_t) msg_iov, |
| 213 |
.msg_iovlen = msg_iovlen, |
| 214 |
}; |
| 215 |
|
| 216 |
char control_data[S2N_KTLS_CONTROL_BUFFER_SIZE] = { 0 }; |
| 217 |
RESULT_GUARD(s2n_ktls_set_control_data(&msg, control_data, sizeof(control_data), |
| 218 |
S2N_TLS_SET_RECORD_TYPE, record_type)); |
| 219 |
|
| 220 |
ssize_t result = 0; |
| 221 |
S2N_IO_RETRY_EINTR(result, s2n_sendmsg_fn(io_context, &msg)); |
| 222 |
RESULT_GUARD(s2n_io_check_write_result(result)); |
| 223 |
|
| 224 |
*blocked = S2N_NOT_BLOCKED; |
| 225 |
*bytes_written = result; |
| 226 |
return S2N_RESULT_OK; |
| 227 |
} |
| 228 |
|
| 229 |
S2N_RESULT s2n_ktls_recvmsg(void *io_context, uint8_t *record_type, uint8_t *buf, |
| 230 |
size_t buf_len, s2n_blocked_status *blocked, size_t *bytes_read) |
| 231 |
{ |
| 232 |
RESULT_ENSURE_REF(record_type); |
| 233 |
RESULT_ENSURE_REF(bytes_read); |
| 234 |
RESULT_ENSURE_REF(blocked); |
| 235 |
RESULT_ENSURE_REF(buf); |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
RESULT_ENSURE_GT(buf_len, 0); |
| 240 |
|
| 241 |
*blocked = S2N_BLOCKED_ON_READ; |
| 242 |
*record_type = 0; |
| 243 |
*bytes_read = 0; |
| 244 |
struct iovec msg_iov = { |
| 245 |
.iov_base = buf, |
| 246 |
.iov_len = buf_len |
| 247 |
}; |
| 248 |
struct msghdr msg = { |
| 249 |
.msg_iov = &msg_iov, |
| 250 |
.msg_iovlen = 1, |
| 251 |
}; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
char control_data[S2N_KTLS_CONTROL_BUFFER_SIZE] = { 0 }; |
| 260 |
msg.msg_controllen = sizeof(control_data); |
| 261 |
msg.msg_control = control_data; |
| 262 |
|
| 263 |
ssize_t result = 0; |
| 264 |
S2N_IO_RETRY_EINTR(result, s2n_recvmsg_fn(io_context, &msg)); |
| 265 |
RESULT_GUARD(s2n_io_check_read_result(result)); |
| 266 |
|
| 267 |
RESULT_GUARD(s2n_ktls_get_control_data(&msg, S2N_TLS_GET_RECORD_TYPE, record_type)); |
| 268 |
|
| 269 |
*blocked = S2N_NOT_BLOCKED; |
| 270 |
*bytes_read = result; |
| 271 |
return S2N_RESULT_OK; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
static S2N_RESULT s2n_ktls_update_bufs_with_offset(const struct iovec **bufs, size_t *count, |
| 293 |
size_t offs, struct s2n_blob *mem) |
| 294 |
{ |
| 295 |
RESULT_ENSURE_REF(bufs); |
| 296 |
RESULT_ENSURE_REF(count); |
| 297 |
RESULT_ENSURE(*bufs != NULL || *count == 0, S2N_ERR_NULL); |
| 298 |
RESULT_ENSURE_REF(mem); |
| 299 |
|
| 300 |
size_t skipped = 0; |
| 301 |
while (offs > 0) { |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
RESULT_ENSURE(skipped < *count, S2N_ERR_INVALID_ARGUMENT); |
| 306 |
|
| 307 |
size_t iov_len = (*bufs)[skipped].iov_len; |
| 308 |
|
| 309 |
|
| 310 |
if (offs < iov_len) { |
| 311 |
break; |
| 312 |
} |
| 313 |
|
| 314 |
offs -= iov_len; |
| 315 |
skipped++; |
| 316 |
} |
| 317 |
|
| 318 |
*count = (*count) - skipped; |
| 319 |
if (*count == 0) { |
| 320 |
return S2N_RESULT_OK; |
| 321 |
} |
| 322 |
|
| 323 |
*bufs = &(*bufs)[skipped]; |
| 324 |
if (offs == 0) { |
| 325 |
return S2N_RESULT_OK; |
| 326 |
} |
| 327 |
|
| 328 |
size_t size = (*count) * (sizeof(struct iovec)); |
| 329 |
|
| 330 |
|
| 331 |
if (size > mem->size) { |
| 332 |
RESULT_GUARD_POSIX(s2n_alloc(mem, size)); |
| 333 |
} |
| 334 |
|
| 335 |
struct iovec *new_bufs = (struct iovec *) (void *) mem->data; |
| 336 |
RESULT_CHECKED_MEMCPY(new_bufs, *bufs, size); |
| 337 |
new_bufs[0].iov_base = (uint8_t *) new_bufs[0].iov_base + offs; |
| 338 |
new_bufs[0].iov_len = new_bufs[0].iov_len - offs; |
| 339 |
*bufs = new_bufs; |
| 340 |
|
| 341 |
return S2N_RESULT_OK; |
| 342 |
} |
| 343 |
|
| 344 |
ssize_t s2n_ktls_sendv_with_offset(struct s2n_connection *conn, const struct iovec *bufs, |
| 345 |
ssize_t count_in, ssize_t offs_in, s2n_blocked_status *blocked) |
| 346 |
{ |
| 347 |
POSIX_ENSURE_REF(conn); |
| 348 |
POSIX_ENSURE(count_in >= 0, S2N_ERR_INVALID_ARGUMENT); |
| 349 |
size_t count = count_in; |
| 350 |
POSIX_ENSURE(offs_in >= 0, S2N_ERR_INVALID_ARGUMENT); |
| 351 |
size_t offs = offs_in; |
| 352 |
|
| 353 |
ssize_t total_bytes = 0; |
| 354 |
POSIX_GUARD_RESULT(s2n_sendv_with_offset_total_size(bufs, count_in, offs_in, &total_bytes)); |
| 355 |
POSIX_GUARD_RESULT(s2n_ktls_key_update_send(conn, total_bytes)); |
| 356 |
|
| 357 |
|
| 358 |
uint8_t new_bufs_mem[S2N_MAX_STACK_IOVECS_MEM] = { 0 }; |
| 359 |
DEFER_CLEANUP(struct s2n_blob new_bufs = { 0 }, s2n_free_or_wipe); |
| 360 |
POSIX_GUARD(s2n_blob_init(&new_bufs, new_bufs_mem, sizeof(new_bufs_mem))); |
| 361 |
if (offs > 0) { |
| 362 |
POSIX_GUARD_RESULT(s2n_ktls_update_bufs_with_offset(&bufs, &count, offs, &new_bufs)); |
| 363 |
} |
| 364 |
|
| 365 |
size_t bytes_written = 0; |
| 366 |
POSIX_GUARD_RESULT(s2n_ktls_sendmsg(conn->send_io_context, TLS_APPLICATION_DATA, |
| 367 |
bufs, count, blocked, &bytes_written)); |
| 368 |
|
| 369 |
POSIX_GUARD_RESULT(s2n_ktls_set_estimated_sequence_number(conn, bytes_written)); |
| 370 |
return bytes_written; |
| 371 |
} |
| 372 |
|
| 373 |
int s2n_ktls_send_cb(void *io_context, const uint8_t *buf, uint32_t len) |
| 374 |
{ |
| 375 |
POSIX_ENSURE_REF(io_context); |
| 376 |
POSIX_ENSURE_REF(buf); |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
const uint8_t record_type = TLS_ALERT; |
| 382 |
|
| 383 |
const struct iovec iov = { |
| 384 |
.iov_base = (void *) (uintptr_t) buf, |
| 385 |
.iov_len = len, |
| 386 |
}; |
| 387 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 388 |
size_t bytes_written = 0; |
| 389 |
|
| 390 |
POSIX_GUARD_RESULT(s2n_ktls_sendmsg(io_context, record_type, &iov, 1, |
| 391 |
&blocked, &bytes_written)); |
| 392 |
|
| 393 |
POSIX_ENSURE_LTE(bytes_written, len); |
| 394 |
return bytes_written; |
| 395 |
} |
| 396 |
|
| 397 |
int s2n_ktls_record_writev(struct s2n_connection *conn, uint8_t content_type, |
| 398 |
const struct iovec *in, int in_count, size_t offs, size_t to_write) |
| 399 |
{ |
| 400 |
POSIX_ENSURE_REF(conn); |
| 401 |
POSIX_ENSURE(in_count > 0, S2N_ERR_INVALID_ARGUMENT); |
| 402 |
size_t count = in_count; |
| 403 |
POSIX_ENSURE_REF(in); |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
POSIX_ENSURE(content_type == TLS_ALERT, S2N_ERR_UNIMPLEMENTED); |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
POSIX_GUARD(s2n_stuffer_resize_if_empty(&conn->out, to_write)); |
| 419 |
|
| 420 |
POSIX_GUARD(s2n_stuffer_writev_bytes(&conn->out, in, count, offs, to_write)); |
| 421 |
return to_write; |
| 422 |
} |
| 423 |
|
| 424 |
int s2n_sendfile(struct s2n_connection *conn, int in_fd, off_t offset, size_t count, |
| 425 |
size_t *bytes_written, s2n_blocked_status *blocked) |
| 426 |
{ |
| 427 |
POSIX_ENSURE_REF(blocked); |
| 428 |
*blocked = S2N_BLOCKED_ON_WRITE; |
| 429 |
POSIX_ENSURE_REF(bytes_written); |
| 430 |
*bytes_written = 0; |
| 431 |
POSIX_ENSURE_REF(conn); |
| 432 |
POSIX_ENSURE(conn->ktls_send_enabled, S2N_ERR_KTLS_UNSUPPORTED_CONN); |
| 433 |
POSIX_GUARD_RESULT(s2n_ktls_key_update_send(conn, count)); |
| 434 |
|
| 435 |
int out_fd = 0; |
| 436 |
POSIX_GUARD_RESULT(s2n_ktls_get_file_descriptor(conn, S2N_KTLS_MODE_SEND, &out_fd)); |
| 437 |
|
| 438 |
#ifdef S2N_LINUX_SENDFILE |
| 439 |
|
| 440 |
ssize_t result = 0; |
| 441 |
S2N_IO_RETRY_EINTR(result, sendfile(out_fd, in_fd, &offset, count)); |
| 442 |
POSIX_GUARD_RESULT(s2n_io_check_write_result(result)); |
| 443 |
*bytes_written = result; |
| 444 |
#else |
| 445 |
POSIX_BAIL(S2N_ERR_UNIMPLEMENTED); |
| 446 |
#endif |
| 447 |
|
| 448 |
POSIX_GUARD_RESULT(s2n_ktls_set_estimated_sequence_number(conn, *bytes_written)); |
| 449 |
*blocked = S2N_NOT_BLOCKED; |
| 450 |
return S2N_SUCCESS; |
| 451 |
} |
| 452 |
|
| 453 |
int s2n_ktls_read_full_record(struct s2n_connection *conn, uint8_t *record_type) |
| 454 |
{ |
| 455 |
POSIX_ENSURE_REF(conn); |
| 456 |
POSIX_ENSURE_REF(record_type); |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
if (s2n_stuffer_data_available(&conn->in)) { |
| 462 |
*record_type = TLS_APPLICATION_DATA; |
| 463 |
return S2N_SUCCESS; |
| 464 |
} |
| 465 |
|
| 466 |
POSIX_GUARD(s2n_stuffer_resize_if_empty(&conn->buffer_in, S2N_DEFAULT_FRAGMENT_LENGTH)); |
| 467 |
|
| 468 |
struct s2n_stuffer record_stuffer = conn->buffer_in; |
| 469 |
size_t len = s2n_stuffer_space_remaining(&record_stuffer); |
| 470 |
uint8_t *buf = s2n_stuffer_raw_write(&record_stuffer, len); |
| 471 |
POSIX_ENSURE_REF(buf); |
| 472 |
|
| 473 |
s2n_blocked_status blocked = S2N_NOT_BLOCKED; |
| 474 |
size_t bytes_read = 0; |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
s2n_result result = s2n_ktls_recvmsg(conn->recv_io_context, record_type, |
| 480 |
buf, len, &blocked, &bytes_read); |
| 481 |
WITH_ERROR_BLINDING(conn, POSIX_GUARD_RESULT(result)); |
| 482 |
|
| 483 |
POSIX_GUARD(s2n_stuffer_skip_write(&conn->buffer_in, bytes_read)); |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
POSIX_GUARD_RESULT(s2n_recv_in_init(conn, bytes_read, bytes_read)); |
| 490 |
return S2N_SUCCESS; |
| 491 |
} |
| 492 |
|
| 493 |
#endif |
| 494 |
|
| 495 |
|
| 496 |
#pragma clang diagnostic ignored "-Wempty-translation-unit" |