1 // SPDX-License-Identifier: GPL-2.0
14 #include <linux/tls.h>
15 #include <linux/tcp.h>
16 #include <linux/socket.h>
18 #include <sys/epoll.h>
19 #include <sys/types.h>
20 #include <sys/sendfile.h>
21 #include <sys/socket.h>
24 #include "../kselftest_harness.h"
26 #define TLS_PAYLOAD_MAX_LEN 16384
29 static int fips_enabled;
31 struct tls_crypto_info_keys {
33 struct tls_crypto_info crypto_info;
34 struct tls12_crypto_info_aes_gcm_128 aes128;
35 struct tls12_crypto_info_chacha20_poly1305 chacha20;
36 struct tls12_crypto_info_sm4_gcm sm4gcm;
37 struct tls12_crypto_info_sm4_ccm sm4ccm;
38 struct tls12_crypto_info_aes_ccm_128 aesccm128;
39 struct tls12_crypto_info_aes_gcm_256 aesgcm256;
40 struct tls12_crypto_info_aria_gcm_128 ariagcm128;
41 struct tls12_crypto_info_aria_gcm_256 ariagcm256;
46 static void tls_crypto_info_init(uint16_t tls_version, uint16_t cipher_type,
47 struct tls_crypto_info_keys *tls12)
49 memset(tls12, 0, sizeof(*tls12));
51 switch (cipher_type) {
52 case TLS_CIPHER_CHACHA20_POLY1305:
53 tls12->len = sizeof(struct tls12_crypto_info_chacha20_poly1305);
54 tls12->chacha20.info.version = tls_version;
55 tls12->chacha20.info.cipher_type = cipher_type;
57 case TLS_CIPHER_AES_GCM_128:
58 tls12->len = sizeof(struct tls12_crypto_info_aes_gcm_128);
59 tls12->aes128.info.version = tls_version;
60 tls12->aes128.info.cipher_type = cipher_type;
62 case TLS_CIPHER_SM4_GCM:
63 tls12->len = sizeof(struct tls12_crypto_info_sm4_gcm);
64 tls12->sm4gcm.info.version = tls_version;
65 tls12->sm4gcm.info.cipher_type = cipher_type;
67 case TLS_CIPHER_SM4_CCM:
68 tls12->len = sizeof(struct tls12_crypto_info_sm4_ccm);
69 tls12->sm4ccm.info.version = tls_version;
70 tls12->sm4ccm.info.cipher_type = cipher_type;
72 case TLS_CIPHER_AES_CCM_128:
73 tls12->len = sizeof(struct tls12_crypto_info_aes_ccm_128);
74 tls12->aesccm128.info.version = tls_version;
75 tls12->aesccm128.info.cipher_type = cipher_type;
77 case TLS_CIPHER_AES_GCM_256:
78 tls12->len = sizeof(struct tls12_crypto_info_aes_gcm_256);
79 tls12->aesgcm256.info.version = tls_version;
80 tls12->aesgcm256.info.cipher_type = cipher_type;
82 case TLS_CIPHER_ARIA_GCM_128:
83 tls12->len = sizeof(struct tls12_crypto_info_aria_gcm_128);
84 tls12->ariagcm128.info.version = tls_version;
85 tls12->ariagcm128.info.cipher_type = cipher_type;
87 case TLS_CIPHER_ARIA_GCM_256:
88 tls12->len = sizeof(struct tls12_crypto_info_aria_gcm_256);
89 tls12->ariagcm256.info.version = tls_version;
90 tls12->ariagcm256.info.cipher_type = cipher_type;
97 static void memrnd(void *s, size_t n)
102 for (; n >= 4; n -= 4)
104 byte = (void *)dword;
109 static void ulp_sock_pair(struct __test_metadata *_metadata,
110 int *fd, int *cfd, bool *notls)
112 struct sockaddr_in addr;
119 addr.sin_family = AF_INET;
120 addr.sin_addr.s_addr = htonl(INADDR_ANY);
123 *fd = socket(AF_INET, SOCK_STREAM, 0);
124 sfd = socket(AF_INET, SOCK_STREAM, 0);
126 ret = bind(sfd, &addr, sizeof(addr));
128 ret = listen(sfd, 10);
131 ret = getsockname(sfd, &addr, &len);
134 ret = connect(*fd, &addr, sizeof(addr));
137 *cfd = accept(sfd, &addr, &len);
142 ret = setsockopt(*fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
144 ASSERT_EQ(errno, ENOENT);
146 printf("Failure setting TCP_ULP, testing without tls\n");
150 ret = setsockopt(*cfd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
154 /* Produce a basic cmsg */
155 static int tls_send_cmsg(int fd, unsigned char record_type,
156 void *data, size_t len, int flags)
158 char cbuf[CMSG_SPACE(sizeof(char))];
159 int cmsg_len = sizeof(char);
160 struct cmsghdr *cmsg;
166 memset(&msg, 0, sizeof(struct msghdr));
169 msg.msg_control = cbuf;
170 msg.msg_controllen = sizeof(cbuf);
171 cmsg = CMSG_FIRSTHDR(&msg);
172 cmsg->cmsg_level = SOL_TLS;
173 /* test sending non-record types. */
174 cmsg->cmsg_type = TLS_SET_RECORD_TYPE;
175 cmsg->cmsg_len = CMSG_LEN(cmsg_len);
176 *CMSG_DATA(cmsg) = record_type;
177 msg.msg_controllen = cmsg->cmsg_len;
179 return sendmsg(fd, &msg, flags);
182 static int tls_recv_cmsg(struct __test_metadata *_metadata,
183 int fd, unsigned char record_type,
184 void *data, size_t len, int flags)
186 char cbuf[CMSG_SPACE(sizeof(char))];
187 struct cmsghdr *cmsg;
195 memset(&msg, 0, sizeof(struct msghdr));
198 msg.msg_control = cbuf;
199 msg.msg_controllen = sizeof(cbuf);
201 n = recvmsg(fd, &msg, flags);
203 cmsg = CMSG_FIRSTHDR(&msg);
204 EXPECT_NE(cmsg, NULL);
205 EXPECT_EQ(cmsg->cmsg_level, SOL_TLS);
206 EXPECT_EQ(cmsg->cmsg_type, TLS_GET_RECORD_TYPE);
207 ctype = *((unsigned char *)CMSG_DATA(cmsg));
208 EXPECT_EQ(ctype, record_type);
219 FIXTURE_SETUP(tls_basic)
221 ulp_sock_pair(_metadata, &self->fd, &self->cfd, &self->notls);
224 FIXTURE_TEARDOWN(tls_basic)
230 /* Send some data through with ULP but no keys */
231 TEST_F(tls_basic, base_base)
233 char const *test_str = "test_read";
237 ASSERT_EQ(strlen(test_str) + 1, send_len);
239 EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
240 EXPECT_NE(recv(self->cfd, buf, send_len, 0), -1);
241 EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
244 TEST_F(tls_basic, bad_cipher)
246 struct tls_crypto_info_keys tls12;
248 tls12.crypto_info.version = 200;
249 tls12.crypto_info.cipher_type = TLS_CIPHER_AES_GCM_128;
250 EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, sizeof(struct tls12_crypto_info_aes_gcm_128)), -1);
252 tls12.crypto_info.version = TLS_1_2_VERSION;
253 tls12.crypto_info.cipher_type = 50;
254 EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, sizeof(struct tls12_crypto_info_aes_gcm_128)), -1);
256 tls12.crypto_info.version = TLS_1_2_VERSION;
257 tls12.crypto_info.cipher_type = 59;
258 EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, sizeof(struct tls12_crypto_info_aes_gcm_128)), -1);
260 tls12.crypto_info.version = TLS_1_2_VERSION;
261 tls12.crypto_info.cipher_type = 10;
262 EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, sizeof(struct tls12_crypto_info_aes_gcm_128)), -1);
264 tls12.crypto_info.version = TLS_1_2_VERSION;
265 tls12.crypto_info.cipher_type = 70;
266 EXPECT_EQ(setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, sizeof(struct tls12_crypto_info_aes_gcm_128)), -1);
277 uint16_t tls_version;
278 uint16_t cipher_type;
279 bool nopad, fips_non_compliant;
282 FIXTURE_VARIANT_ADD(tls, 12_aes_gcm)
284 .tls_version = TLS_1_2_VERSION,
285 .cipher_type = TLS_CIPHER_AES_GCM_128,
288 FIXTURE_VARIANT_ADD(tls, 13_aes_gcm)
290 .tls_version = TLS_1_3_VERSION,
291 .cipher_type = TLS_CIPHER_AES_GCM_128,
294 FIXTURE_VARIANT_ADD(tls, 12_chacha)
296 .tls_version = TLS_1_2_VERSION,
297 .cipher_type = TLS_CIPHER_CHACHA20_POLY1305,
298 .fips_non_compliant = true,
301 FIXTURE_VARIANT_ADD(tls, 13_chacha)
303 .tls_version = TLS_1_3_VERSION,
304 .cipher_type = TLS_CIPHER_CHACHA20_POLY1305,
305 .fips_non_compliant = true,
308 FIXTURE_VARIANT_ADD(tls, 13_sm4_gcm)
310 .tls_version = TLS_1_3_VERSION,
311 .cipher_type = TLS_CIPHER_SM4_GCM,
312 .fips_non_compliant = true,
315 FIXTURE_VARIANT_ADD(tls, 13_sm4_ccm)
317 .tls_version = TLS_1_3_VERSION,
318 .cipher_type = TLS_CIPHER_SM4_CCM,
319 .fips_non_compliant = true,
322 FIXTURE_VARIANT_ADD(tls, 12_aes_ccm)
324 .tls_version = TLS_1_2_VERSION,
325 .cipher_type = TLS_CIPHER_AES_CCM_128,
328 FIXTURE_VARIANT_ADD(tls, 13_aes_ccm)
330 .tls_version = TLS_1_3_VERSION,
331 .cipher_type = TLS_CIPHER_AES_CCM_128,
334 FIXTURE_VARIANT_ADD(tls, 12_aes_gcm_256)
336 .tls_version = TLS_1_2_VERSION,
337 .cipher_type = TLS_CIPHER_AES_GCM_256,
340 FIXTURE_VARIANT_ADD(tls, 13_aes_gcm_256)
342 .tls_version = TLS_1_3_VERSION,
343 .cipher_type = TLS_CIPHER_AES_GCM_256,
346 FIXTURE_VARIANT_ADD(tls, 13_nopad)
348 .tls_version = TLS_1_3_VERSION,
349 .cipher_type = TLS_CIPHER_AES_GCM_128,
353 FIXTURE_VARIANT_ADD(tls, 12_aria_gcm)
355 .tls_version = TLS_1_2_VERSION,
356 .cipher_type = TLS_CIPHER_ARIA_GCM_128,
359 FIXTURE_VARIANT_ADD(tls, 12_aria_gcm_256)
361 .tls_version = TLS_1_2_VERSION,
362 .cipher_type = TLS_CIPHER_ARIA_GCM_256,
367 struct tls_crypto_info_keys tls12;
371 if (fips_enabled && variant->fips_non_compliant)
372 SKIP(return, "Unsupported cipher in FIPS mode");
374 tls_crypto_info_init(variant->tls_version, variant->cipher_type,
377 ulp_sock_pair(_metadata, &self->fd, &self->cfd, &self->notls);
382 ret = setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, tls12.len);
385 ret = setsockopt(self->cfd, SOL_TLS, TLS_RX, &tls12, tls12.len);
388 if (variant->nopad) {
389 ret = setsockopt(self->cfd, SOL_TLS, TLS_RX_EXPECT_NO_PAD,
390 (void *)&one, sizeof(one));
395 FIXTURE_TEARDOWN(tls)
401 TEST_F(tls, sendfile)
403 int filefd = open("/proc/self/exe", O_RDONLY);
406 EXPECT_GE(filefd, 0);
408 EXPECT_GE(sendfile(self->fd, filefd, 0, st.st_size), 0);
411 TEST_F(tls, send_then_sendfile)
413 int filefd = open("/proc/self/exe", O_RDONLY);
414 char const *test_str = "test_send";
415 int to_send = strlen(test_str) + 1;
420 EXPECT_GE(filefd, 0);
422 buf = (char *)malloc(st.st_size);
424 EXPECT_EQ(send(self->fd, test_str, to_send, 0), to_send);
425 EXPECT_EQ(recv(self->cfd, recv_buf, to_send, MSG_WAITALL), to_send);
426 EXPECT_EQ(memcmp(test_str, recv_buf, to_send), 0);
428 EXPECT_GE(sendfile(self->fd, filefd, 0, st.st_size), 0);
429 EXPECT_EQ(recv(self->cfd, buf, st.st_size, MSG_WAITALL), st.st_size);
432 static void chunked_sendfile(struct __test_metadata *_metadata,
433 struct _test_data_tls *self,
435 uint16_t extra_payload_size)
437 char buf[TLS_PAYLOAD_MAX_LEN];
438 uint16_t test_payload_size;
441 char filename[] = "/tmp/mytemp.XXXXXX";
442 int fd = mkstemp(filename);
447 EXPECT_GE(chunk_size, 1);
448 test_payload_size = chunk_size + extra_payload_size;
449 ASSERT_GE(TLS_PAYLOAD_MAX_LEN, test_payload_size);
450 memset(buf, 1, test_payload_size);
451 size = write(fd, buf, test_payload_size);
452 EXPECT_EQ(size, test_payload_size);
456 ret = sendfile(self->fd, fd, &offset, chunk_size);
461 EXPECT_EQ(recv(self->cfd, buf, test_payload_size, MSG_WAITALL),
467 TEST_F(tls, multi_chunk_sendfile)
469 chunked_sendfile(_metadata, self, 4096, 4096);
470 chunked_sendfile(_metadata, self, 4096, 0);
471 chunked_sendfile(_metadata, self, 4096, 1);
472 chunked_sendfile(_metadata, self, 4096, 2048);
473 chunked_sendfile(_metadata, self, 8192, 2048);
474 chunked_sendfile(_metadata, self, 4096, 8192);
475 chunked_sendfile(_metadata, self, 8192, 4096);
476 chunked_sendfile(_metadata, self, 12288, 1024);
477 chunked_sendfile(_metadata, self, 12288, 2000);
478 chunked_sendfile(_metadata, self, 15360, 100);
479 chunked_sendfile(_metadata, self, 15360, 300);
480 chunked_sendfile(_metadata, self, 1, 4096);
481 chunked_sendfile(_metadata, self, 2048, 4096);
482 chunked_sendfile(_metadata, self, 2048, 8192);
483 chunked_sendfile(_metadata, self, 4096, 8192);
484 chunked_sendfile(_metadata, self, 1024, 12288);
485 chunked_sendfile(_metadata, self, 2000, 12288);
486 chunked_sendfile(_metadata, self, 100, 15360);
487 chunked_sendfile(_metadata, self, 300, 15360);
490 TEST_F(tls, recv_max)
492 unsigned int send_len = TLS_PAYLOAD_MAX_LEN;
493 char recv_mem[TLS_PAYLOAD_MAX_LEN];
494 char buf[TLS_PAYLOAD_MAX_LEN];
496 memrnd(buf, sizeof(buf));
498 EXPECT_GE(send(self->fd, buf, send_len, 0), 0);
499 EXPECT_NE(recv(self->cfd, recv_mem, send_len, 0), -1);
500 EXPECT_EQ(memcmp(buf, recv_mem, send_len), 0);
503 TEST_F(tls, recv_small)
505 char const *test_str = "test_read";
509 send_len = strlen(test_str) + 1;
510 EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
511 EXPECT_NE(recv(self->cfd, buf, send_len, 0), -1);
512 EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
515 TEST_F(tls, msg_more)
517 char const *test_str = "test_read";
521 EXPECT_EQ(send(self->fd, test_str, send_len, MSG_MORE), send_len);
522 EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_DONTWAIT), -1);
523 EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
524 EXPECT_EQ(recv(self->cfd, buf, send_len * 2, MSG_WAITALL),
526 EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
529 TEST_F(tls, msg_more_unsent)
531 char const *test_str = "test_read";
535 EXPECT_EQ(send(self->fd, test_str, send_len, MSG_MORE), send_len);
536 EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_DONTWAIT), -1);
541 char const *test_str = "test_read";
545 EXPECT_EQ(send(self->fd, test_str, send_len, MSG_EOR), send_len);
546 EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_WAITALL), send_len);
547 EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
550 TEST_F(tls, sendmsg_single)
554 char const *test_str = "test_sendmsg";
555 size_t send_len = 13;
559 vec.iov_base = (char *)test_str;
560 vec.iov_len = send_len;
561 memset(&msg, 0, sizeof(struct msghdr));
564 EXPECT_EQ(sendmsg(self->fd, &msg, 0), send_len);
565 EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_WAITALL), send_len);
566 EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
571 TEST_F(tls, sendmsg_fragmented)
573 char const *test_str = "test_sendmsg";
574 char buf[SEND_LEN * MAX_FRAGS];
575 struct iovec vec[MAX_FRAGS];
579 for (frags = 1; frags <= MAX_FRAGS; frags++) {
580 for (i = 0; i < frags; i++) {
581 vec[i].iov_base = (char *)test_str;
582 vec[i].iov_len = SEND_LEN;
585 memset(&msg, 0, sizeof(struct msghdr));
587 msg.msg_iovlen = frags;
589 EXPECT_EQ(sendmsg(self->fd, &msg, 0), SEND_LEN * frags);
590 EXPECT_EQ(recv(self->cfd, buf, SEND_LEN * frags, MSG_WAITALL),
593 for (i = 0; i < frags; i++)
594 EXPECT_EQ(memcmp(buf + SEND_LEN * i,
595 test_str, SEND_LEN), 0);
601 TEST_F(tls, sendmsg_large)
603 void *mem = malloc(16384);
604 size_t send_len = 16384;
610 memset(&msg, 0, sizeof(struct msghdr));
611 while (sent++ < sends) {
612 struct iovec vec = { (void *)mem, send_len };
616 EXPECT_EQ(sendmsg(self->fd, &msg, 0), send_len);
619 while (recvs++ < sends) {
620 EXPECT_NE(recv(self->cfd, mem, send_len, 0), -1);
626 TEST_F(tls, sendmsg_multiple)
628 char const *test_str = "test_sendmsg_multiple";
638 memset(&msg, 0, sizeof(struct msghdr));
639 for (i = 0; i < iov_len; i++) {
640 test_strs[i] = (char *)malloc(strlen(test_str) + 1);
641 snprintf(test_strs[i], strlen(test_str) + 1, "%s", test_str);
642 vec[i].iov_base = (void *)test_strs[i];
643 vec[i].iov_len = strlen(test_strs[i]) + 1;
644 total_len += vec[i].iov_len;
647 msg.msg_iovlen = iov_len;
649 EXPECT_EQ(sendmsg(self->fd, &msg, 0), total_len);
650 buf = malloc(total_len);
651 EXPECT_NE(recv(self->cfd, buf, total_len, 0), -1);
652 for (i = 0; i < iov_len; i++) {
653 EXPECT_EQ(memcmp(test_strs[i], buf + len_cmp,
654 strlen(test_strs[i])),
656 len_cmp += strlen(buf + len_cmp) + 1;
658 for (i = 0; i < iov_len; i++)
663 TEST_F(tls, sendmsg_multiple_stress)
665 char const *test_str = "abcdefghijklmno";
666 struct iovec vec[1024];
667 char *test_strs[1024];
675 memset(&msg, 0, sizeof(struct msghdr));
676 for (i = 0; i < iov_len; i++) {
677 test_strs[i] = (char *)malloc(strlen(test_str) + 1);
678 snprintf(test_strs[i], strlen(test_str) + 1, "%s", test_str);
679 vec[i].iov_base = (void *)test_strs[i];
680 vec[i].iov_len = strlen(test_strs[i]) + 1;
681 total_len += vec[i].iov_len;
684 msg.msg_iovlen = iov_len;
686 EXPECT_EQ(sendmsg(self->fd, &msg, 0), total_len);
687 EXPECT_NE(recv(self->cfd, buf, total_len, 0), -1);
689 for (i = 0; i < iov_len; i++)
690 len_cmp += strlen(buf + len_cmp) + 1;
692 for (i = 0; i < iov_len; i++)
696 TEST_F(tls, splice_from_pipe)
698 int send_len = TLS_PAYLOAD_MAX_LEN;
699 char mem_send[TLS_PAYLOAD_MAX_LEN];
700 char mem_recv[TLS_PAYLOAD_MAX_LEN];
703 ASSERT_GE(pipe(p), 0);
704 EXPECT_GE(write(p[1], mem_send, send_len), 0);
705 EXPECT_GE(splice(p[0], NULL, self->fd, NULL, send_len, 0), 0);
706 EXPECT_EQ(recv(self->cfd, mem_recv, send_len, MSG_WAITALL), send_len);
707 EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
710 TEST_F(tls, splice_from_pipe2)
712 int send_len = 16000;
713 char mem_send[16000];
714 char mem_recv[16000];
718 memrnd(mem_send, sizeof(mem_send));
720 ASSERT_GE(pipe(p), 0);
721 ASSERT_GE(pipe(p2), 0);
722 EXPECT_EQ(write(p[1], mem_send, 8000), 8000);
723 EXPECT_EQ(splice(p[0], NULL, self->fd, NULL, 8000, 0), 8000);
724 EXPECT_EQ(write(p2[1], mem_send + 8000, 8000), 8000);
725 EXPECT_EQ(splice(p2[0], NULL, self->fd, NULL, 8000, 0), 8000);
726 EXPECT_EQ(recv(self->cfd, mem_recv, send_len, MSG_WAITALL), send_len);
727 EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
730 TEST_F(tls, send_and_splice)
732 int send_len = TLS_PAYLOAD_MAX_LEN;
733 char mem_send[TLS_PAYLOAD_MAX_LEN];
734 char mem_recv[TLS_PAYLOAD_MAX_LEN];
735 char const *test_str = "test_read";
740 ASSERT_GE(pipe(p), 0);
741 EXPECT_EQ(send(self->fd, test_str, send_len2, 0), send_len2);
742 EXPECT_EQ(recv(self->cfd, buf, send_len2, MSG_WAITALL), send_len2);
743 EXPECT_EQ(memcmp(test_str, buf, send_len2), 0);
745 EXPECT_GE(write(p[1], mem_send, send_len), send_len);
746 EXPECT_GE(splice(p[0], NULL, self->fd, NULL, send_len, 0), send_len);
748 EXPECT_EQ(recv(self->cfd, mem_recv, send_len, MSG_WAITALL), send_len);
749 EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
752 TEST_F(tls, splice_to_pipe)
754 int send_len = TLS_PAYLOAD_MAX_LEN;
755 char mem_send[TLS_PAYLOAD_MAX_LEN];
756 char mem_recv[TLS_PAYLOAD_MAX_LEN];
759 memrnd(mem_send, sizeof(mem_send));
761 ASSERT_GE(pipe(p), 0);
762 EXPECT_EQ(send(self->fd, mem_send, send_len, 0), send_len);
763 EXPECT_EQ(splice(self->cfd, NULL, p[1], NULL, send_len, 0), send_len);
764 EXPECT_EQ(read(p[0], mem_recv, send_len), send_len);
765 EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
768 TEST_F(tls, splice_cmsg_to_pipe)
770 char *test_str = "test_read";
771 char record_type = 100;
777 SKIP(return, "no TLS support");
779 ASSERT_GE(pipe(p), 0);
780 EXPECT_EQ(tls_send_cmsg(self->fd, 100, test_str, send_len, 0), 10);
781 EXPECT_EQ(splice(self->cfd, NULL, p[1], NULL, send_len, 0), -1);
782 EXPECT_EQ(errno, EINVAL);
783 EXPECT_EQ(recv(self->cfd, buf, send_len, 0), -1);
784 EXPECT_EQ(errno, EIO);
785 EXPECT_EQ(tls_recv_cmsg(_metadata, self->cfd, record_type,
786 buf, sizeof(buf), MSG_WAITALL),
788 EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
791 TEST_F(tls, splice_dec_cmsg_to_pipe)
793 char *test_str = "test_read";
794 char record_type = 100;
800 SKIP(return, "no TLS support");
802 ASSERT_GE(pipe(p), 0);
803 EXPECT_EQ(tls_send_cmsg(self->fd, 100, test_str, send_len, 0), 10);
804 EXPECT_EQ(recv(self->cfd, buf, send_len, 0), -1);
805 EXPECT_EQ(errno, EIO);
806 EXPECT_EQ(splice(self->cfd, NULL, p[1], NULL, send_len, 0), -1);
807 EXPECT_EQ(errno, EINVAL);
808 EXPECT_EQ(tls_recv_cmsg(_metadata, self->cfd, record_type,
809 buf, sizeof(buf), MSG_WAITALL),
811 EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
814 TEST_F(tls, recv_and_splice)
816 int send_len = TLS_PAYLOAD_MAX_LEN;
817 char mem_send[TLS_PAYLOAD_MAX_LEN];
818 char mem_recv[TLS_PAYLOAD_MAX_LEN];
819 int half = send_len / 2;
822 ASSERT_GE(pipe(p), 0);
823 EXPECT_EQ(send(self->fd, mem_send, send_len, 0), send_len);
824 /* Recv hald of the record, splice the other half */
825 EXPECT_EQ(recv(self->cfd, mem_recv, half, MSG_WAITALL), half);
826 EXPECT_EQ(splice(self->cfd, NULL, p[1], NULL, half, SPLICE_F_NONBLOCK),
828 EXPECT_EQ(read(p[0], &mem_recv[half], half), half);
829 EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
832 TEST_F(tls, peek_and_splice)
834 int send_len = TLS_PAYLOAD_MAX_LEN;
835 char mem_send[TLS_PAYLOAD_MAX_LEN];
836 char mem_recv[TLS_PAYLOAD_MAX_LEN];
837 int chunk = TLS_PAYLOAD_MAX_LEN / 4;
840 memrnd(mem_send, sizeof(mem_send));
842 ASSERT_GE(pipe(p), 0);
843 for (i = 0; i < 4; i++)
844 EXPECT_EQ(send(self->fd, &mem_send[chunk * i], chunk, 0),
847 EXPECT_EQ(recv(self->cfd, mem_recv, chunk * 5 / 2,
848 MSG_WAITALL | MSG_PEEK),
850 EXPECT_EQ(memcmp(mem_send, mem_recv, chunk * 5 / 2), 0);
853 while (n < send_len) {
854 i = splice(self->cfd, NULL, p[1], NULL, send_len - n, 0);
858 EXPECT_EQ(n, send_len);
859 EXPECT_EQ(read(p[0], mem_recv, send_len), send_len);
860 EXPECT_EQ(memcmp(mem_send, mem_recv, send_len), 0);
863 TEST_F(tls, recvmsg_single)
865 char const *test_str = "test_recvmsg_single";
866 int send_len = strlen(test_str) + 1;
871 memset(&hdr, 0, sizeof(hdr));
872 EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
873 vec.iov_base = (char *)buf;
874 vec.iov_len = send_len;
877 EXPECT_NE(recvmsg(self->cfd, &hdr, 0), -1);
878 EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
881 TEST_F(tls, recvmsg_single_max)
883 int send_len = TLS_PAYLOAD_MAX_LEN;
884 char send_mem[TLS_PAYLOAD_MAX_LEN];
885 char recv_mem[TLS_PAYLOAD_MAX_LEN];
889 memrnd(send_mem, sizeof(send_mem));
891 EXPECT_EQ(send(self->fd, send_mem, send_len, 0), send_len);
892 vec.iov_base = (char *)recv_mem;
893 vec.iov_len = TLS_PAYLOAD_MAX_LEN;
897 EXPECT_NE(recvmsg(self->cfd, &hdr, 0), -1);
898 EXPECT_EQ(memcmp(send_mem, recv_mem, send_len), 0);
901 TEST_F(tls, recvmsg_multiple)
903 unsigned int msg_iovlen = 1024;
904 struct iovec vec[1024];
905 char *iov_base[1024];
906 unsigned int iov_len = 16;
907 int send_len = 1 << 14;
912 memrnd(buf, sizeof(buf));
914 EXPECT_EQ(send(self->fd, buf, send_len, 0), send_len);
915 for (i = 0; i < msg_iovlen; i++) {
916 iov_base[i] = (char *)malloc(iov_len);
917 vec[i].iov_base = iov_base[i];
918 vec[i].iov_len = iov_len;
921 hdr.msg_iovlen = msg_iovlen;
923 EXPECT_NE(recvmsg(self->cfd, &hdr, 0), -1);
925 for (i = 0; i < msg_iovlen; i++)
929 TEST_F(tls, single_send_multiple_recv)
931 unsigned int total_len = TLS_PAYLOAD_MAX_LEN * 2;
932 unsigned int send_len = TLS_PAYLOAD_MAX_LEN;
933 char send_mem[TLS_PAYLOAD_MAX_LEN * 2];
934 char recv_mem[TLS_PAYLOAD_MAX_LEN * 2];
936 memrnd(send_mem, sizeof(send_mem));
938 EXPECT_GE(send(self->fd, send_mem, total_len, 0), 0);
939 memset(recv_mem, 0, total_len);
941 EXPECT_NE(recv(self->cfd, recv_mem, send_len, 0), -1);
942 EXPECT_NE(recv(self->cfd, recv_mem + send_len, send_len, 0), -1);
943 EXPECT_EQ(memcmp(send_mem, recv_mem, total_len), 0);
946 TEST_F(tls, multiple_send_single_recv)
948 unsigned int total_len = 2 * 10;
949 unsigned int send_len = 10;
950 char recv_mem[2 * 10];
953 memrnd(send_mem, sizeof(send_mem));
955 EXPECT_GE(send(self->fd, send_mem, send_len, 0), 0);
956 EXPECT_GE(send(self->fd, send_mem, send_len, 0), 0);
957 memset(recv_mem, 0, total_len);
958 EXPECT_EQ(recv(self->cfd, recv_mem, total_len, MSG_WAITALL), total_len);
960 EXPECT_EQ(memcmp(send_mem, recv_mem, send_len), 0);
961 EXPECT_EQ(memcmp(send_mem, recv_mem + send_len, send_len), 0);
964 TEST_F(tls, single_send_multiple_recv_non_align)
966 const unsigned int total_len = 15;
967 const unsigned int recv_len = 10;
968 char recv_mem[recv_len * 2];
969 char send_mem[total_len];
971 memrnd(send_mem, sizeof(send_mem));
973 EXPECT_GE(send(self->fd, send_mem, total_len, 0), 0);
974 memset(recv_mem, 0, total_len);
976 EXPECT_EQ(recv(self->cfd, recv_mem, recv_len, 0), recv_len);
977 EXPECT_EQ(recv(self->cfd, recv_mem + recv_len, recv_len, 0), 5);
978 EXPECT_EQ(memcmp(send_mem, recv_mem, total_len), 0);
981 TEST_F(tls, recv_partial)
983 char const *test_str = "test_read_partial";
984 char const *test_str_first = "test_read";
985 char const *test_str_second = "_partial";
986 int send_len = strlen(test_str) + 1;
989 memset(recv_mem, 0, sizeof(recv_mem));
990 EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
991 EXPECT_NE(recv(self->cfd, recv_mem, strlen(test_str_first),
993 EXPECT_EQ(memcmp(test_str_first, recv_mem, strlen(test_str_first)), 0);
994 memset(recv_mem, 0, sizeof(recv_mem));
995 EXPECT_NE(recv(self->cfd, recv_mem, strlen(test_str_second),
997 EXPECT_EQ(memcmp(test_str_second, recv_mem, strlen(test_str_second)),
1001 TEST_F(tls, recv_nonblock)
1006 EXPECT_EQ(recv(self->cfd, buf, sizeof(buf), MSG_DONTWAIT), -1);
1007 err = (errno == EAGAIN || errno == EWOULDBLOCK);
1008 EXPECT_EQ(err, true);
1011 TEST_F(tls, recv_peek)
1013 char const *test_str = "test_read_peek";
1014 int send_len = strlen(test_str) + 1;
1017 EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
1018 EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_PEEK), send_len);
1019 EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
1020 memset(buf, 0, sizeof(buf));
1021 EXPECT_EQ(recv(self->cfd, buf, send_len, 0), send_len);
1022 EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
1025 TEST_F(tls, recv_peek_multiple)
1027 char const *test_str = "test_read_peek";
1028 int send_len = strlen(test_str) + 1;
1029 unsigned int num_peeks = 100;
1033 EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
1034 for (i = 0; i < num_peeks; i++) {
1035 EXPECT_NE(recv(self->cfd, buf, send_len, MSG_PEEK), -1);
1036 EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
1037 memset(buf, 0, sizeof(buf));
1039 EXPECT_NE(recv(self->cfd, buf, send_len, 0), -1);
1040 EXPECT_EQ(memcmp(test_str, buf, send_len), 0);
1043 TEST_F(tls, recv_peek_multiple_records)
1045 char const *test_str = "test_read_peek_mult_recs";
1046 char const *test_str_first = "test_read_peek";
1047 char const *test_str_second = "_mult_recs";
1051 len = strlen(test_str_first);
1052 EXPECT_EQ(send(self->fd, test_str_first, len, 0), len);
1054 len = strlen(test_str_second) + 1;
1055 EXPECT_EQ(send(self->fd, test_str_second, len, 0), len);
1057 len = strlen(test_str_first);
1058 memset(buf, 0, len);
1059 EXPECT_EQ(recv(self->cfd, buf, len, MSG_PEEK | MSG_WAITALL), len);
1061 /* MSG_PEEK can only peek into the current record. */
1062 len = strlen(test_str_first);
1063 EXPECT_EQ(memcmp(test_str_first, buf, len), 0);
1065 len = strlen(test_str) + 1;
1066 memset(buf, 0, len);
1067 EXPECT_EQ(recv(self->cfd, buf, len, MSG_WAITALL), len);
1069 /* Non-MSG_PEEK will advance strparser (and therefore record)
1072 len = strlen(test_str) + 1;
1073 EXPECT_EQ(memcmp(test_str, buf, len), 0);
1075 /* MSG_MORE will hold current record open, so later MSG_PEEK
1076 * will see everything.
1078 len = strlen(test_str_first);
1079 EXPECT_EQ(send(self->fd, test_str_first, len, MSG_MORE), len);
1081 len = strlen(test_str_second) + 1;
1082 EXPECT_EQ(send(self->fd, test_str_second, len, 0), len);
1084 len = strlen(test_str) + 1;
1085 memset(buf, 0, len);
1086 EXPECT_EQ(recv(self->cfd, buf, len, MSG_PEEK | MSG_WAITALL), len);
1088 len = strlen(test_str) + 1;
1089 EXPECT_EQ(memcmp(test_str, buf, len), 0);
1092 TEST_F(tls, recv_peek_large_buf_mult_recs)
1094 char const *test_str = "test_read_peek_mult_recs";
1095 char const *test_str_first = "test_read_peek";
1096 char const *test_str_second = "_mult_recs";
1100 len = strlen(test_str_first);
1101 EXPECT_EQ(send(self->fd, test_str_first, len, 0), len);
1103 len = strlen(test_str_second) + 1;
1104 EXPECT_EQ(send(self->fd, test_str_second, len, 0), len);
1106 len = strlen(test_str) + 1;
1107 memset(buf, 0, len);
1108 EXPECT_NE((len = recv(self->cfd, buf, len,
1109 MSG_PEEK | MSG_WAITALL)), -1);
1110 len = strlen(test_str) + 1;
1111 EXPECT_EQ(memcmp(test_str, buf, len), 0);
1114 TEST_F(tls, recv_lowat)
1116 char send_mem[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
1120 EXPECT_EQ(send(self->fd, send_mem, 10, 0), 10);
1121 EXPECT_EQ(send(self->fd, send_mem, 5, 0), 5);
1123 memset(recv_mem, 0, 20);
1124 EXPECT_EQ(setsockopt(self->cfd, SOL_SOCKET, SO_RCVLOWAT,
1125 &lowat, sizeof(lowat)), 0);
1126 EXPECT_EQ(recv(self->cfd, recv_mem, 1, MSG_WAITALL), 1);
1127 EXPECT_EQ(recv(self->cfd, recv_mem + 1, 6, MSG_WAITALL), 6);
1128 EXPECT_EQ(recv(self->cfd, recv_mem + 7, 10, 0), 8);
1130 EXPECT_EQ(memcmp(send_mem, recv_mem, 10), 0);
1131 EXPECT_EQ(memcmp(send_mem, recv_mem + 10, 5), 0);
1136 char const *test_str = "test_read";
1142 struct tls_crypto_info_keys tls12;
1144 tls_crypto_info_init(variant->tls_version, variant->cipher_type,
1147 ret = setsockopt(self->fd, SOL_TLS, TLS_RX, &tls12,
1151 ret = setsockopt(self->cfd, SOL_TLS, TLS_TX, &tls12,
1156 ASSERT_EQ(strlen(test_str) + 1, send_len);
1158 EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
1159 EXPECT_NE(recv(self->cfd, buf, send_len, 0), -1);
1160 EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
1162 memset(buf, 0, sizeof(buf));
1164 EXPECT_EQ(send(self->cfd, test_str, send_len, 0), send_len);
1165 EXPECT_NE(recv(self->fd, buf, send_len, 0), -1);
1166 EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
1171 char const *test_str = "test_poll";
1172 struct pollfd fd = { 0, 0, 0 };
1176 EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
1180 EXPECT_EQ(poll(&fd, 1, 20), 1);
1181 EXPECT_EQ(fd.revents & POLLIN, 1);
1182 EXPECT_EQ(recv(self->cfd, buf, send_len, MSG_WAITALL), send_len);
1183 /* Test timing out */
1184 EXPECT_EQ(poll(&fd, 1, 20), 0);
1187 TEST_F(tls, poll_wait)
1189 char const *test_str = "test_poll_wait";
1190 int send_len = strlen(test_str) + 1;
1191 struct pollfd fd = { 0, 0, 0 };
1196 EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
1197 /* Set timeout to inf. secs */
1198 EXPECT_EQ(poll(&fd, 1, -1), 1);
1199 EXPECT_EQ(fd.revents & POLLIN, 1);
1200 EXPECT_EQ(recv(self->cfd, recv_mem, send_len, MSG_WAITALL), send_len);
1203 TEST_F(tls, poll_wait_split)
1205 struct pollfd fd = { 0, 0, 0 };
1206 char send_mem[20] = {};
1212 EXPECT_EQ(send(self->fd, send_mem, sizeof(send_mem), 0),
1214 /* Poll with inf. timeout */
1215 EXPECT_EQ(poll(&fd, 1, -1), 1);
1216 EXPECT_EQ(fd.revents & POLLIN, 1);
1217 EXPECT_EQ(recv(self->cfd, recv_mem, sizeof(recv_mem), MSG_WAITALL),
1220 /* Now the remaining 5 bytes of record data are in TLS ULP */
1223 EXPECT_EQ(poll(&fd, 1, -1), 1);
1224 EXPECT_EQ(fd.revents & POLLIN, 1);
1225 EXPECT_EQ(recv(self->cfd, recv_mem, sizeof(recv_mem), 0),
1226 sizeof(send_mem) - sizeof(recv_mem));
1229 TEST_F(tls, blocking)
1231 size_t data = 100000;
1244 int res = send(self->fd, buf,
1245 left > 16384 ? 16384 : left, 0);
1251 pid2 = wait(&status);
1252 EXPECT_EQ(status, 0);
1253 EXPECT_EQ(res, pid2);
1260 int res = recv(self->cfd, buf,
1261 left > 16384 ? 16384 : left, 0);
1269 TEST_F(tls, nonblocking)
1271 size_t data = 100000;
1276 flags = fcntl(self->fd, F_GETFL, 0);
1277 fcntl(self->fd, F_SETFL, flags | O_NONBLOCK);
1278 fcntl(self->cfd, F_SETFL, flags | O_NONBLOCK);
1280 /* Ensure nonblocking behavior by imposing a small send
1283 EXPECT_EQ(setsockopt(self->fd, SOL_SOCKET, SO_SNDBUF,
1284 &sendbuf, sizeof(sendbuf)), 0);
1291 bool eagain = false;
1298 int res = send(self->fd, buf,
1299 left > 16384 ? 16384 : left, 0);
1301 if (res == -1 && errno == EAGAIN) {
1310 EXPECT_TRUE(eagain);
1311 pid2 = wait(&status);
1313 EXPECT_EQ(status, 0);
1314 EXPECT_EQ(res, pid2);
1317 bool eagain = false;
1322 int res = recv(self->cfd, buf,
1323 left > 16384 ? 16384 : left, 0);
1325 if (res == -1 && errno == EAGAIN) {
1333 EXPECT_TRUE(eagain);
1338 test_mutliproc(struct __test_metadata *_metadata, struct _test_data_tls *self,
1339 bool sendpg, unsigned int n_readers, unsigned int n_writers)
1341 const unsigned int n_children = n_readers + n_writers;
1342 const size_t data = 6 * 1000 * 1000;
1343 const size_t file_sz = data / 100;
1344 size_t read_bias, write_bias;
1345 int i, fd, child_id;
1349 /* Only allow multiples for simplicity */
1350 ASSERT_EQ(!(n_readers % n_writers) || !(n_writers % n_readers), true);
1351 read_bias = n_writers / n_readers ?: 1;
1352 write_bias = n_readers / n_writers ?: 1;
1354 /* prep a file to send */
1355 fd = open("/tmp/", O_TMPFILE | O_RDWR, 0600);
1358 memset(buf, 0xac, file_sz);
1359 ASSERT_EQ(write(fd, buf, file_sz), file_sz);
1361 /* spawn children */
1362 for (child_id = 0; child_id < n_children; child_id++) {
1369 /* parent waits for all children */
1371 for (i = 0; i < n_children; i++) {
1375 EXPECT_EQ(status, 0);
1381 /* Split threads for reading and writing */
1382 if (child_id < n_readers) {
1383 size_t left = data * read_bias;
1389 res = recv(self->cfd, rb,
1390 left > sizeof(rb) ? sizeof(rb) : left, 0);
1396 size_t left = data * write_bias;
1401 ASSERT_EQ(lseek(fd, 0, SEEK_SET), 0);
1403 res = sendfile(self->fd, fd, NULL,
1404 left > file_sz ? file_sz : left);
1406 res = send(self->fd, buf,
1407 left > file_sz ? file_sz : left, 0);
1415 TEST_F(tls, mutliproc_even)
1417 test_mutliproc(_metadata, self, false, 6, 6);
1420 TEST_F(tls, mutliproc_readers)
1422 test_mutliproc(_metadata, self, false, 4, 12);
1425 TEST_F(tls, mutliproc_writers)
1427 test_mutliproc(_metadata, self, false, 10, 2);
1430 TEST_F(tls, mutliproc_sendpage_even)
1432 test_mutliproc(_metadata, self, true, 6, 6);
1435 TEST_F(tls, mutliproc_sendpage_readers)
1437 test_mutliproc(_metadata, self, true, 4, 12);
1440 TEST_F(tls, mutliproc_sendpage_writers)
1442 test_mutliproc(_metadata, self, true, 10, 2);
1445 TEST_F(tls, control_msg)
1447 char *test_str = "test_read";
1448 char record_type = 100;
1453 SKIP(return, "no TLS support");
1455 EXPECT_EQ(tls_send_cmsg(self->fd, record_type, test_str, send_len, 0),
1457 /* Should fail because we didn't provide a control message */
1458 EXPECT_EQ(recv(self->cfd, buf, send_len, 0), -1);
1460 EXPECT_EQ(tls_recv_cmsg(_metadata, self->cfd, record_type,
1461 buf, sizeof(buf), MSG_WAITALL | MSG_PEEK),
1463 EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
1465 /* Recv the message again without MSG_PEEK */
1466 memset(buf, 0, sizeof(buf));
1468 EXPECT_EQ(tls_recv_cmsg(_metadata, self->cfd, record_type,
1469 buf, sizeof(buf), MSG_WAITALL),
1471 EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
1474 TEST_F(tls, shutdown)
1476 char const *test_str = "test_read";
1480 ASSERT_EQ(strlen(test_str) + 1, send_len);
1482 EXPECT_EQ(send(self->fd, test_str, send_len, 0), send_len);
1483 EXPECT_NE(recv(self->cfd, buf, send_len, 0), -1);
1484 EXPECT_EQ(memcmp(buf, test_str, send_len), 0);
1486 shutdown(self->fd, SHUT_RDWR);
1487 shutdown(self->cfd, SHUT_RDWR);
1490 TEST_F(tls, shutdown_unsent)
1492 char const *test_str = "test_read";
1495 EXPECT_EQ(send(self->fd, test_str, send_len, MSG_MORE), send_len);
1497 shutdown(self->fd, SHUT_RDWR);
1498 shutdown(self->cfd, SHUT_RDWR);
1501 TEST_F(tls, shutdown_reuse)
1503 struct sockaddr_in addr;
1506 shutdown(self->fd, SHUT_RDWR);
1507 shutdown(self->cfd, SHUT_RDWR);
1510 addr.sin_family = AF_INET;
1511 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1514 ret = bind(self->fd, &addr, sizeof(addr));
1516 ret = listen(self->fd, 10);
1518 EXPECT_EQ(errno, EINVAL);
1520 ret = connect(self->fd, &addr, sizeof(addr));
1522 EXPECT_EQ(errno, EISCONN);
1525 TEST_F(tls, getsockopt)
1527 struct tls_crypto_info_keys expect, get;
1530 /* get only the version/cipher */
1531 len = sizeof(struct tls_crypto_info);
1532 memrnd(&get, sizeof(get));
1533 EXPECT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &len), 0);
1534 EXPECT_EQ(len, sizeof(struct tls_crypto_info));
1535 EXPECT_EQ(get.crypto_info.version, variant->tls_version);
1536 EXPECT_EQ(get.crypto_info.cipher_type, variant->cipher_type);
1538 /* get the full crypto_info */
1539 tls_crypto_info_init(variant->tls_version, variant->cipher_type, &expect);
1541 memrnd(&get, sizeof(get));
1542 EXPECT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &len), 0);
1543 EXPECT_EQ(len, expect.len);
1544 EXPECT_EQ(get.crypto_info.version, variant->tls_version);
1545 EXPECT_EQ(get.crypto_info.cipher_type, variant->cipher_type);
1546 EXPECT_EQ(memcmp(&get, &expect, expect.len), 0);
1548 /* short get should fail */
1549 len = sizeof(struct tls_crypto_info) - 1;
1550 EXPECT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &len), -1);
1551 EXPECT_EQ(errno, EINVAL);
1553 /* partial get of the cipher data should fail */
1554 len = expect.len - 1;
1555 EXPECT_EQ(getsockopt(self->fd, SOL_TLS, TLS_TX, &get, &len), -1);
1556 EXPECT_EQ(errno, EINVAL);
1566 FIXTURE_VARIANT(tls_err)
1568 uint16_t tls_version;
1571 FIXTURE_VARIANT_ADD(tls_err, 12_aes_gcm)
1573 .tls_version = TLS_1_2_VERSION,
1576 FIXTURE_VARIANT_ADD(tls_err, 13_aes_gcm)
1578 .tls_version = TLS_1_3_VERSION,
1581 FIXTURE_SETUP(tls_err)
1583 struct tls_crypto_info_keys tls12;
1586 tls_crypto_info_init(variant->tls_version, TLS_CIPHER_AES_GCM_128,
1589 ulp_sock_pair(_metadata, &self->fd, &self->cfd, &self->notls);
1590 ulp_sock_pair(_metadata, &self->fd2, &self->cfd2, &self->notls);
1594 ret = setsockopt(self->fd, SOL_TLS, TLS_TX, &tls12, tls12.len);
1597 ret = setsockopt(self->cfd2, SOL_TLS, TLS_RX, &tls12, tls12.len);
1601 FIXTURE_TEARDOWN(tls_err)
1609 TEST_F(tls_err, bad_rec)
1614 SKIP(return, "no TLS support");
1616 memset(buf, 0x55, sizeof(buf));
1617 EXPECT_EQ(send(self->fd2, buf, sizeof(buf), 0), sizeof(buf));
1618 EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1619 EXPECT_EQ(errno, EMSGSIZE);
1620 EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), MSG_DONTWAIT), -1);
1621 EXPECT_EQ(errno, EAGAIN);
1624 TEST_F(tls_err, bad_auth)
1630 SKIP(return, "no TLS support");
1632 memrnd(buf, sizeof(buf) / 2);
1633 EXPECT_EQ(send(self->fd, buf, sizeof(buf) / 2, 0), sizeof(buf) / 2);
1634 n = recv(self->cfd, buf, sizeof(buf), 0);
1635 EXPECT_GT(n, sizeof(buf) / 2);
1639 EXPECT_EQ(send(self->fd2, buf, n, 0), n);
1640 EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1641 EXPECT_EQ(errno, EBADMSG);
1642 EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1643 EXPECT_EQ(errno, EBADMSG);
1646 TEST_F(tls_err, bad_in_large_read)
1654 SKIP(return, "no TLS support");
1656 /* Put 3 records in the sockets */
1657 for (i = 0; i < 3; i++) {
1658 memrnd(txt[i], sizeof(txt[i]));
1659 EXPECT_EQ(send(self->fd, txt[i], sizeof(txt[i]), 0),
1661 n = recv(self->cfd, cip[i], sizeof(cip[i]), 0);
1662 EXPECT_GT(n, sizeof(txt[i]));
1663 /* Break the third message */
1666 EXPECT_EQ(send(self->fd2, cip[i], n, 0), n);
1669 /* We should be able to receive the first two messages */
1670 EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), sizeof(txt[0]) * 2);
1671 EXPECT_EQ(memcmp(buf, txt[0], sizeof(txt[0])), 0);
1672 EXPECT_EQ(memcmp(buf + sizeof(txt[0]), txt[1], sizeof(txt[1])), 0);
1673 /* Third mesasge is bad */
1674 EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1675 EXPECT_EQ(errno, EBADMSG);
1676 EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1677 EXPECT_EQ(errno, EBADMSG);
1680 TEST_F(tls_err, bad_cmsg)
1682 char *test_str = "test_read";
1690 SKIP(return, "no TLS support");
1692 /* Queue up one data record */
1693 memrnd(txt, sizeof(txt));
1694 EXPECT_EQ(send(self->fd, txt, sizeof(txt), 0), sizeof(txt));
1695 n = recv(self->cfd, cip, sizeof(cip), 0);
1696 EXPECT_GT(n, sizeof(txt));
1697 EXPECT_EQ(send(self->fd2, cip, n, 0), n);
1699 EXPECT_EQ(tls_send_cmsg(self->fd, 100, test_str, send_len, 0), 10);
1700 n = recv(self->cfd, cip, sizeof(cip), 0);
1701 cip[n - 1]++; /* Break it */
1702 EXPECT_GT(n, send_len);
1703 EXPECT_EQ(send(self->fd2, cip, n, 0), n);
1705 EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), sizeof(txt));
1706 EXPECT_EQ(memcmp(buf, txt, sizeof(txt)), 0);
1707 EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1708 EXPECT_EQ(errno, EBADMSG);
1709 EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1710 EXPECT_EQ(errno, EBADMSG);
1713 TEST_F(tls_err, timeo)
1715 struct timeval tv = { .tv_usec = 10000, };
1720 SKIP(return, "no TLS support");
1722 ret = setsockopt(self->cfd2, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
1729 usleep(1000); /* Give child a head start */
1731 EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1732 EXPECT_EQ(errno, EAGAIN);
1734 EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1735 EXPECT_EQ(errno, EAGAIN);
1739 EXPECT_EQ(recv(self->cfd2, buf, sizeof(buf), 0), -1);
1740 EXPECT_EQ(errno, EAGAIN);
1745 TEST_F(tls_err, poll_partial_rec)
1747 struct pollfd pfd = { };
1753 SKIP(return, "no TLS support");
1755 pfd.fd = self->cfd2;
1756 pfd.events = POLLIN;
1757 EXPECT_EQ(poll(&pfd, 1, 1), 0);
1759 memrnd(buf, sizeof(buf));
1760 EXPECT_EQ(send(self->fd, buf, sizeof(buf), 0), sizeof(buf));
1761 rec_len = recv(self->cfd, rec, sizeof(rec), 0);
1762 EXPECT_GT(rec_len, sizeof(buf));
1764 /* Write 100B, not the full record ... */
1765 EXPECT_EQ(send(self->fd2, rec, 100, 0), 100);
1766 /* ... no full record should mean no POLLIN */
1767 pfd.fd = self->cfd2;
1768 pfd.events = POLLIN;
1769 EXPECT_EQ(poll(&pfd, 1, 1), 0);
1770 /* Now write the rest, and it should all pop out of the other end. */
1771 EXPECT_EQ(send(self->fd2, rec + 100, rec_len - 100, 0), rec_len - 100);
1772 pfd.fd = self->cfd2;
1773 pfd.events = POLLIN;
1774 EXPECT_EQ(poll(&pfd, 1, 1), 1);
1775 EXPECT_EQ(recv(self->cfd2, rec, sizeof(rec), 0), sizeof(buf));
1776 EXPECT_EQ(memcmp(buf, rec, sizeof(buf)), 0);
1779 TEST_F(tls_err, epoll_partial_rec)
1781 struct epoll_event ev, events[10];
1788 SKIP(return, "no TLS support");
1790 epollfd = epoll_create1(0);
1791 ASSERT_GE(epollfd, 0);
1793 memset(&ev, 0, sizeof(ev));
1794 ev.events = EPOLLIN;
1795 ev.data.fd = self->cfd2;
1796 ASSERT_GE(epoll_ctl(epollfd, EPOLL_CTL_ADD, self->cfd2, &ev), 0);
1798 EXPECT_EQ(epoll_wait(epollfd, events, 10, 0), 0);
1800 memrnd(buf, sizeof(buf));
1801 EXPECT_EQ(send(self->fd, buf, sizeof(buf), 0), sizeof(buf));
1802 rec_len = recv(self->cfd, rec, sizeof(rec), 0);
1803 EXPECT_GT(rec_len, sizeof(buf));
1805 /* Write 100B, not the full record ... */
1806 EXPECT_EQ(send(self->fd2, rec, 100, 0), 100);
1807 /* ... no full record should mean no POLLIN */
1808 EXPECT_EQ(epoll_wait(epollfd, events, 10, 0), 0);
1809 /* Now write the rest, and it should all pop out of the other end. */
1810 EXPECT_EQ(send(self->fd2, rec + 100, rec_len - 100, 0), rec_len - 100);
1811 EXPECT_EQ(epoll_wait(epollfd, events, 10, 0), 1);
1812 EXPECT_EQ(recv(self->cfd2, rec, sizeof(rec), 0), sizeof(buf));
1813 EXPECT_EQ(memcmp(buf, rec, sizeof(buf)), 0);
1818 TEST_F(tls_err, poll_partial_rec_async)
1820 struct pollfd pfd = { };
1829 SKIP(return, "no TLS support");
1831 ASSERT_GE(pipe(p), 0);
1833 memrnd(buf, sizeof(buf));
1834 EXPECT_EQ(send(self->fd, buf, sizeof(buf), 0), sizeof(buf));
1835 rec_len = recv(self->cfd, rec, sizeof(rec), 0);
1836 EXPECT_GT(rec_len, sizeof(buf));
1845 usleep(1000); /* Give child a head start */
1847 EXPECT_EQ(send(self->fd2, rec, 100, 0), 100);
1849 EXPECT_EQ(read(p[0], &token, 1), 1); /* Barrier #1 */
1851 EXPECT_EQ(send(self->fd2, rec + 100, rec_len - 100, 0),
1854 pid2 = wait(&status);
1855 EXPECT_EQ(pid2, ret);
1856 EXPECT_EQ(status, 0);
1860 /* Child should sleep in poll(), never get a wake */
1861 pfd.fd = self->cfd2;
1862 pfd.events = POLLIN;
1863 EXPECT_EQ(poll(&pfd, 1, 5), 0);
1865 EXPECT_EQ(write(p[1], &token, 1), 1); /* Barrier #1 */
1867 pfd.fd = self->cfd2;
1868 pfd.events = POLLIN;
1869 EXPECT_EQ(poll(&pfd, 1, 5), 1);
1871 exit(!_metadata->passed);
1875 TEST(non_established) {
1876 struct tls12_crypto_info_aes_gcm_256 tls12;
1877 struct sockaddr_in addr;
1883 memset(&tls12, 0, sizeof(tls12));
1884 tls12.info.version = TLS_1_2_VERSION;
1885 tls12.info.cipher_type = TLS_CIPHER_AES_GCM_256;
1887 addr.sin_family = AF_INET;
1888 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1891 fd = socket(AF_INET, SOCK_STREAM, 0);
1892 sfd = socket(AF_INET, SOCK_STREAM, 0);
1894 ret = bind(sfd, &addr, sizeof(addr));
1896 ret = listen(sfd, 10);
1899 ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
1901 /* TLS ULP not supported */
1902 if (errno == ENOENT)
1904 EXPECT_EQ(errno, ENOTCONN);
1906 ret = setsockopt(sfd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
1908 EXPECT_EQ(errno, ENOTCONN);
1910 ret = getsockname(sfd, &addr, &len);
1913 ret = connect(fd, &addr, sizeof(addr));
1916 ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
1919 ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
1921 EXPECT_EQ(errno, EEXIST);
1928 struct tls12_crypto_info_aes_gcm_256 tls12;
1932 memset(&tls12, 0, sizeof(tls12));
1933 tls12.info.version = TLS_1_2_VERSION;
1934 tls12.info.cipher_type = TLS_CIPHER_AES_GCM_256;
1936 ulp_sock_pair(_metadata, &fd, &cfd, ¬ls);
1939 ret = setsockopt(fd, SOL_TLS, TLS_TX, &tls12,
1943 ret = setsockopt(cfd, SOL_TLS, TLS_RX, &tls12,
1953 struct tls12_crypto_info_aes_gcm_256 tls12;
1954 int ret, fd, cfd, val;
1958 memset(&tls12, 0, sizeof(tls12));
1959 tls12.info.version = TLS_1_3_VERSION;
1960 tls12.info.cipher_type = TLS_CIPHER_AES_GCM_256;
1962 ulp_sock_pair(_metadata, &fd, &cfd, ¬ls);
1967 ret = setsockopt(fd, SOL_TLS, TLS_TX, &tls12, sizeof(tls12));
1970 ret = setsockopt(cfd, SOL_TLS, TLS_RX, &tls12, sizeof(tls12));
1974 ret = setsockopt(cfd, SOL_TLS, TLS_RX_EXPECT_NO_PAD,
1975 (void *)&val, sizeof(val));
1980 ret = getsockopt(cfd, SOL_TLS, TLS_RX_EXPECT_NO_PAD,
1981 (void *)&val, &len);
1987 ret = setsockopt(cfd, SOL_TLS, TLS_RX_EXPECT_NO_PAD,
1988 (void *)&val, sizeof(val));
1993 ret = getsockopt(cfd, SOL_TLS, TLS_RX_EXPECT_NO_PAD,
1994 (void *)&val, &len);
2004 struct tls_crypto_info_keys tls12;
2005 struct sockaddr_in6 addr, addr2;
2007 socklen_t len, len2;
2009 tls_crypto_info_init(TLS_1_2_VERSION, TLS_CIPHER_AES_GCM_128, &tls12);
2011 addr.sin6_family = AF_INET6;
2012 addr.sin6_addr = in6addr_any;
2015 fd = socket(AF_INET6, SOCK_STREAM, 0);
2016 sfd = socket(AF_INET6, SOCK_STREAM, 0);
2018 ret = bind(sfd, &addr, sizeof(addr));
2020 ret = listen(sfd, 10);
2024 ret = getsockname(sfd, &addr, &len);
2027 ret = connect(fd, &addr, sizeof(addr));
2031 ret = getsockname(fd, &addr, &len);
2034 ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
2036 ASSERT_EQ(errno, ENOENT);
2037 SKIP(return, "no TLS support");
2041 ret = setsockopt(fd, SOL_TLS, TLS_TX, &tls12, tls12.len);
2044 ret = setsockopt(fd, SOL_TLS, TLS_RX, &tls12, tls12.len);
2047 len2 = sizeof(addr2);
2048 ret = getsockname(fd, &addr2, &len2);
2051 EXPECT_EQ(len2, len);
2052 EXPECT_EQ(memcmp(&addr, &addr2, len), 0);
2059 struct tls_crypto_info_keys tls12;
2060 char buf[20000], buf2[20000];
2061 struct sockaddr_in addr;
2062 int sfd, cfd, ret, fd;
2066 memrnd(buf, sizeof(buf));
2068 tls_crypto_info_init(TLS_1_2_VERSION, TLS_CIPHER_AES_GCM_256, &tls12);
2070 addr.sin_family = AF_INET;
2071 addr.sin_addr.s_addr = htonl(INADDR_ANY);
2074 fd = socket(AF_INET, SOCK_STREAM, 0);
2075 sfd = socket(AF_INET, SOCK_STREAM, 0);
2077 ASSERT_EQ(bind(sfd, &addr, sizeof(addr)), 0);
2078 ASSERT_EQ(listen(sfd, 10), 0);
2079 ASSERT_EQ(getsockname(sfd, &addr, &len), 0);
2080 ASSERT_EQ(connect(fd, &addr, sizeof(addr)), 0);
2081 ASSERT_GE(cfd = accept(sfd, &addr, &len), 0);
2084 ret = setsockopt(fd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls"));
2086 ASSERT_EQ(errno, ENOENT);
2087 SKIP(return, "no TLS support");
2090 ASSERT_EQ(setsockopt(fd, SOL_TLS, TLS_TX, &tls12, tls12.len), 0);
2091 EXPECT_EQ(send(fd, buf, sizeof(buf), MSG_DONTWAIT), sizeof(buf));
2093 ASSERT_EQ(setsockopt(cfd, IPPROTO_TCP, TCP_ULP, "tls", sizeof("tls")), 0);
2094 ASSERT_EQ(setsockopt(cfd, SOL_TLS, TLS_RX, &tls12, tls12.len), 0);
2095 EXPECT_EQ(recv(cfd, buf2, sizeof(buf2), MSG_WAITALL), sizeof(buf2));
2097 EXPECT_EQ(memcmp(buf, buf2, sizeof(buf)), 0);
2103 static void __attribute__((constructor)) fips_check(void) {
2107 f = fopen("/proc/sys/crypto/fips_enabled", "r");
2109 res = fscanf(f, "%d", &fips_enabled);
2111 ksft_print_msg("ERROR: Couldn't read /proc/sys/crypto/fips_enabled\n");