summaryrefslogtreecommitdiffstats
path: root/debian/vendor-h2o/deps/picotls/t
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--debian/vendor-h2o/deps/picotls/t/cli.c392
-rw-r--r--debian/vendor-h2o/deps/picotls/t/minicrypto.c165
-rw-r--r--debian/vendor-h2o/deps/picotls/t/openssl.c227
-rw-r--r--debian/vendor-h2o/deps/picotls/t/picotls.c843
-rw-r--r--debian/vendor-h2o/deps/picotls/t/test.h52
-rw-r--r--debian/vendor-h2o/deps/picotls/t/util.h240
6 files changed, 1919 insertions, 0 deletions
diff --git a/debian/vendor-h2o/deps/picotls/t/cli.c b/debian/vendor-h2o/deps/picotls/t/cli.c
new file mode 100644
index 0000000..fc73499
--- /dev/null
+++ b/debian/vendor-h2o/deps/picotls/t/cli.c
@@ -0,0 +1,392 @@
+/*
+ * Copyright (c) 2016 DeNA Co., Ltd., Kazuho Oku
+ *
+ * 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.
+ */
+#ifndef _XOPEN_SOURCE
+#define _XOPEN_SOURCE 700 /* required for glibc to use getaddrinfo, etc. */
+#endif
+
+#include <arpa/inet.h>
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <openssl/err.h>
+#include <openssl/evp.h>
+#include <openssl/engine.h>
+#include <openssl/pem.h>
+#include "picotls.h"
+#include "picotls/openssl.h"
+#include "util.h"
+
+static void shift_buffer(ptls_buffer_t *buf, size_t delta)
+{
+ if (delta != 0) {
+ assert(delta <= buf->off);
+ if (delta != buf->off)
+ memmove(buf->base, buf->base + delta, buf->off - delta);
+ buf->off -= delta;
+ }
+}
+
+static int handle_connection(int sockfd, ptls_context_t *ctx, const char *server_name, const char *input_file,
+ ptls_handshake_properties_t *hsprop)
+{
+ ptls_t *tls = ptls_new(ctx, server_name == NULL);
+ ptls_buffer_t rbuf, encbuf, ptbuf;
+ char bytebuf[16384];
+ enum { IN_HANDSHAKE, IN_1RTT, IN_SHUTDOWN } state = IN_HANDSHAKE;
+ int inputfd = 0, ret = 0;
+ size_t early_bytes_sent = 0;
+ ssize_t ioret;
+
+ ptls_buffer_init(&rbuf, "", 0);
+ ptls_buffer_init(&encbuf, "", 0);
+ ptls_buffer_init(&ptbuf, "", 0);
+
+ fcntl(sockfd, F_SETFL, O_NONBLOCK);
+
+ if (input_file != NULL) {
+ if ((inputfd = open(input_file, O_RDONLY)) == -1) {
+ fprintf(stderr, "failed to open file:%s:%s\n", input_file, strerror(errno));
+ ret = 1;
+ goto Exit;
+ }
+ }
+ if (server_name != NULL) {
+ ptls_set_server_name(tls, server_name, 0);
+ if ((ret = ptls_handshake(tls, &encbuf, NULL, NULL, hsprop)) != PTLS_ERROR_IN_PROGRESS) {
+ fprintf(stderr, "ptls_handshake:%d\n", ret);
+ ret = 1;
+ goto Exit;
+ }
+ }
+
+ while (1) {
+ /* check if data is available */
+ fd_set readfds, writefds, exceptfds;
+ int maxfd = 0;
+ struct timeval timeout;
+ do {
+ FD_ZERO(&readfds);
+ FD_ZERO(&writefds);
+ FD_ZERO(&exceptfds);
+ FD_SET(sockfd, &readfds);
+ if (encbuf.off != 0)
+ FD_SET(sockfd, &writefds);
+ FD_SET(sockfd, &exceptfds);
+ maxfd = sockfd + 1;
+ if (inputfd != -1) {
+ FD_SET(inputfd, &readfds);
+ FD_SET(inputfd, &exceptfds);
+ if (maxfd <= inputfd)
+ maxfd = inputfd + 1;
+ }
+ timeout.tv_sec = encbuf.off != 0 ? 0 : 3600;
+ timeout.tv_usec = 0;
+ } while (select(maxfd, &readfds, &writefds, &exceptfds, &timeout) == -1);
+
+ /* consume incoming messages */
+ if (FD_ISSET(sockfd, &readfds) || FD_ISSET(sockfd, &exceptfds)) {
+ size_t off = 0, leftlen;
+ while ((ioret = read(sockfd, bytebuf, sizeof(bytebuf))) == -1 && errno == EINTR)
+ ;
+ if (ioret == -1 && (errno == EWOULDBLOCK || errno == EAGAIN)) {
+ /* no data */
+ ioret = 0;
+ } else if (ioret <= 0) {
+ goto Exit;
+ }
+ while ((leftlen = ioret - off) != 0) {
+ if (state == IN_HANDSHAKE) {
+ if ((ret = ptls_handshake(tls, &encbuf, bytebuf + off, &leftlen, hsprop)) == 0) {
+ state = IN_1RTT;
+ /* release data sent as early-data, if server accepted it */
+ if (hsprop->client.early_data_accepted_by_peer)
+ shift_buffer(&ptbuf, early_bytes_sent);
+ if (ptbuf.off != 0) {
+ if ((ret = ptls_send(tls, &encbuf, ptbuf.base, ptbuf.off)) != 0) {
+ fprintf(stderr, "ptls_send(1rtt):%d\n", ret);
+ goto Exit;
+ }
+ ptbuf.off = 0;
+ }
+ } else if (ret == PTLS_ERROR_IN_PROGRESS) {
+ /* ok */
+ } else {
+ fprintf(stderr, "ptls_handshake:%d\n", ret);
+ goto Exit;
+ }
+ } else {
+ if ((ret = ptls_receive(tls, &rbuf, bytebuf + off, &leftlen)) == 0) {
+ if (rbuf.off != 0) {
+ write(1, rbuf.base, rbuf.off);
+ rbuf.off = 0;
+ }
+ } else if (ret == PTLS_ERROR_IN_PROGRESS) {
+ /* ok */
+ } else {
+ fprintf(stderr, "ptls_receive:%d\n", ret);
+ goto Exit;
+ }
+ }
+ off += leftlen;
+ }
+ }
+
+ /* read input (and send if possible) */
+ if (inputfd != -1 && (FD_ISSET(inputfd, &readfds) || FD_ISSET(inputfd, &exceptfds))) {
+ while ((ioret = read(inputfd, bytebuf, sizeof(bytebuf))) == -1 && errno == EINTR)
+ ;
+ if (ioret > 0) {
+ ptls_buffer_pushv(&ptbuf, bytebuf, ioret);
+ if (state == IN_HANDSHAKE) {
+ size_t send_amount = 0;
+ if (hsprop->client.max_early_data_size != NULL) {
+ size_t max_can_be_sent = *hsprop->client.max_early_data_size;
+ if (max_can_be_sent > ptbuf.off)
+ max_can_be_sent = ptbuf.off;
+ send_amount = max_can_be_sent - early_bytes_sent;
+ }
+ if (send_amount != 0) {
+ if ((ret = ptls_send(tls, &encbuf, ptbuf.base, send_amount)) != 0) {
+ fprintf(stderr, "ptls_send(early_data):%d\n", ret);
+ goto Exit;
+ }
+ early_bytes_sent += send_amount;
+ }
+ } else {
+ if ((ret = ptls_send(tls, &encbuf, bytebuf, ioret)) != 0) {
+ fprintf(stderr, "ptls_send(1rtt):%d\n", ret);
+ goto Exit;
+ }
+ ptbuf.off = 0;
+ }
+ } else {
+ /* closed */
+ if (input_file != NULL)
+ close(inputfd);
+ inputfd = -1;
+ }
+ }
+
+ /* send any data */
+ if (encbuf.off != 0) {
+ while ((ioret = write(sockfd, encbuf.base, encbuf.off)) == -1 && errno == EINTR)
+ ;
+ if (ioret == -1 && (errno == EWOULDBLOCK || errno == EAGAIN)) {
+ /* no data */
+ } else if (ioret <= 0) {
+ goto Exit;
+ } else {
+ shift_buffer(&encbuf, ioret);
+ }
+ }
+
+ /* close the sender side when necessary */
+ if (state == IN_1RTT && inputfd == -1) {
+ /* FIXME send close_alert */
+ shutdown(sockfd, SHUT_WR);
+ state = IN_SHUTDOWN;
+ }
+ }
+
+Exit:
+ if (sockfd != -1)
+ close(sockfd);
+ if (input_file != NULL && inputfd != -1)
+ close(inputfd);
+ ptls_buffer_dispose(&rbuf);
+ ptls_buffer_dispose(&encbuf);
+ ptls_buffer_dispose(&ptbuf);
+ ptls_free(tls);
+ return ret != 0;
+}
+
+static int run_server(struct sockaddr *sa, socklen_t salen, ptls_context_t *ctx, const char *input_file,
+ ptls_handshake_properties_t *hsprop)
+{
+ int listen_fd, conn_fd, on = 1;
+
+ if ((listen_fd = socket(sa->sa_family, SOCK_STREAM, 0)) == -1) {
+ perror("socket(2) failed");
+ return 1;
+ }
+ if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) {
+ perror("setsockopt(SO_REUSEADDR) failed");
+ return 1;
+ }
+ if (bind(listen_fd, sa, salen) != 0) {
+ perror("bind(2) failed");
+ return 1;
+ }
+ if (listen(listen_fd, SOMAXCONN) != 0) {
+ perror("listen(2) failed");
+ return 1;
+ }
+
+ while (1) {
+ if ((conn_fd = accept(listen_fd, NULL, 0)) != -1)
+ handle_connection(conn_fd, ctx, NULL, input_file, hsprop);
+ }
+
+ return 0;
+}
+
+static int run_client(struct sockaddr *sa, socklen_t salen, ptls_context_t *ctx, const char *server_name, const char *input_file,
+ ptls_handshake_properties_t *hsprop)
+{
+ int fd;
+
+ if ((fd = socket(sa->sa_family, SOCK_STREAM, 0)) == 1) {
+ perror("socket(2) failed");
+ return 1;
+ }
+ if (connect(fd, sa, salen) != 0) {
+ perror("connect(2) failed");
+ return 1;
+ }
+
+ return handle_connection(fd, ctx, server_name, input_file, hsprop);
+}
+
+static void usage(const char *cmd)
+{
+ printf("Usage: %s [options] host port\n"
+ "\n"
+ "Options:\n"
+ " -4 force IPv4\n"
+ " -6 force IPv6\n"
+ " -c certificate-file\n"
+ " -i file a file to read from and send to the peer (default: stdin)\n"
+ " -k key-file specifies the credentials to be used for running the\n"
+ " server. If omitted, the command runs as a client.\n"
+ " -l log-file file to log traffic secrets\n"
+ " -n negotiates the key exchange method (i.e. wait for HRR)\n"
+ " -s session-file file to read/write the session ticket\n"
+ " -S require public key exchange when resuming a session\n"
+ " -e when resuming a session, send first 8,192 bytes of input\n"
+ " as early data\n"
+ " -v verify peer using the default certificates\n"
+ " -h print this help\n"
+ "\n",
+ cmd);
+}
+
+int main(int argc, char **argv)
+{
+ ERR_load_crypto_strings();
+ OpenSSL_add_all_algorithms();
+#if !defined(OPENSSL_NO_ENGINE)
+ /* Load all compiled-in ENGINEs */
+ ENGINE_load_builtin_engines();
+ ENGINE_register_all_ciphers();
+ ENGINE_register_all_digests();
+#endif
+
+ ptls_context_t ctx = {ptls_openssl_random_bytes, &ptls_get_time, ptls_openssl_key_exchanges, ptls_openssl_cipher_suites};
+ ptls_handshake_properties_t hsprop = {{{{NULL}}}};
+ const char *host, *port, *file = NULL;
+ int use_early_data = 0, ch;
+ struct sockaddr_storage sa;
+ socklen_t salen;
+ int family = 0;
+
+ while ((ch = getopt(argc, argv, "46c:i:k:nes:Sl:vh")) != -1) {
+ switch (ch) {
+ case '4':
+ family = AF_INET;
+ break;
+ case '6':
+ family = AF_INET6;
+ break;
+ case 'c':
+ load_certificate_chain(&ctx, optarg);
+ break;
+ case 'i':
+ file = optarg;
+ break;
+ case 'k':
+ load_private_key(&ctx, optarg);
+ break;
+ case 'n':
+ hsprop.client.negotiate_before_key_exchange = 1;
+ break;
+ case 'e':
+ use_early_data = 1;
+ break;
+ case 's':
+ setup_session_file(&ctx, &hsprop, optarg);
+ break;
+ case 'S':
+ ctx.require_dhe_on_psk = 1;
+ break;
+ case 'l':
+ setup_log_secret(&ctx, optarg);
+ break;
+ case 'v':
+ setup_verify_certificate(&ctx);
+ break;
+ default:
+ usage(argv[0]);
+ exit(1);
+ }
+ }
+ argc -= optind;
+ argv += optind;
+ if (ctx.certificates.count != 0 || ctx.sign_certificate != NULL) {
+ /* server */
+ if (ctx.certificates.count == 0 || ctx.sign_certificate == NULL) {
+ fprintf(stderr, "-c and -k options must be used together\n");
+ return 1;
+ }
+ setup_session_cache(&ctx);
+ } else {
+ /* client */
+ if (use_early_data) {
+ static size_t max_early_data_size;
+ hsprop.client.max_early_data_size = &max_early_data_size;
+ }
+ }
+ if (argc != 2) {
+ fprintf(stderr, "missing host and port\n");
+ return 1;
+ }
+ host = (--argc, *argv++);
+ port = (--argc, *argv++);
+
+ if (resolve_address((struct sockaddr *)&sa, &salen, host, port, family, SOCK_STREAM, IPPROTO_TCP) != 0)
+ exit(1);
+
+ if (ctx.certificates.count != 0) {
+ return run_server((struct sockaddr *)&sa, salen, &ctx, file, &hsprop);
+ } else {
+ return run_client((struct sockaddr *)&sa, salen, &ctx, host, file, &hsprop);
+ }
+}
diff --git a/debian/vendor-h2o/deps/picotls/t/minicrypto.c b/debian/vendor-h2o/deps/picotls/t/minicrypto.c
new file mode 100644
index 0000000..0c18a90
--- /dev/null
+++ b/debian/vendor-h2o/deps/picotls/t/minicrypto.c
@@ -0,0 +1,165 @@
+/*
+ * Copyright (c) 2016 DeNA Co., Ltd., Kazuho Oku
+ *
+ * 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.
+ */
+#ifndef _XOPEN_SOURCE
+#define _XOPEN_SOURCE 700 /* required for glibc to use getaddrinfo, etc. */
+#endif
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include "../deps/picotest/picotest.h"
+#include "../lib/cifra.c"
+#include "../lib/uecc.c"
+#include "test.h"
+
+static void test_secp256r1_key_exchange(void)
+{
+ test_key_exchange(&ptls_minicrypto_secp256r1);
+}
+
+static void test_x25519_key_exchange(void)
+{
+ test_key_exchange(&ptls_minicrypto_x25519);
+}
+
+static void test_secp256r1_sign(void)
+{
+ const char *msg = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef";
+ ptls_minicrypto_secp256r1sha256_sign_certificate_t signer = {{secp256r1sha256_sign}};
+ uint8_t pub[SECP256R1_PUBLIC_KEY_SIZE];
+ uint16_t selected;
+ ptls_buffer_t sigbuf;
+ uint32_t sigbuf_small[128];
+
+ uECC_make_key(pub, signer.key, uECC_secp256r1());
+ ptls_buffer_init(&sigbuf, sigbuf_small, sizeof(sigbuf_small));
+
+ ok(secp256r1sha256_sign(&signer.super, NULL, &selected, &sigbuf, ptls_iovec_init(msg, 32),
+ (uint16_t[]){PTLS_SIGNATURE_ECDSA_SECP256R1_SHA256}, 1) == 0);
+ ok(selected == PTLS_SIGNATURE_ECDSA_SECP256R1_SHA256);
+
+ /* FIXME verify sign */
+
+ ptls_buffer_dispose(&sigbuf);
+}
+
+static void test_hrr(void)
+{
+ ptls_key_exchange_algorithm_t *client_keyex[] = {&ptls_minicrypto_x25519, &ptls_minicrypto_secp256r1, NULL};
+ ptls_context_t client_ctx = {ptls_minicrypto_random_bytes, &ptls_get_time, client_keyex, ptls_minicrypto_cipher_suites};
+ ptls_t *client, *server;
+ ptls_buffer_t cbuf, sbuf, decbuf;
+ uint8_t cbuf_small[16384], sbuf_small[16384], decbuf_small[16384];
+ size_t consumed;
+ int ret;
+
+ assert(ctx_peer->key_exchanges[0] != NULL && ctx_peer->key_exchanges[0]->id == PTLS_GROUP_SECP256R1);
+ assert(ctx_peer->key_exchanges[1] == NULL);
+
+ client = ptls_new(&client_ctx, 0);
+ server = ptls_new(ctx_peer, 1);
+ ptls_buffer_init(&cbuf, cbuf_small, sizeof(cbuf_small));
+ ptls_buffer_init(&sbuf, sbuf_small, sizeof(sbuf_small));
+ ptls_buffer_init(&decbuf, decbuf_small, sizeof(decbuf_small));
+
+ ret = ptls_handshake(client, &cbuf, NULL, NULL, NULL);
+ ok(ret == PTLS_ERROR_IN_PROGRESS);
+
+ consumed = cbuf.off;
+ ret = ptls_handshake(server, &sbuf, cbuf.base, &consumed, NULL);
+ ok(ret == PTLS_ERROR_IN_PROGRESS);
+ ok(consumed == cbuf.off);
+ cbuf.off = 0;
+
+ ok(sbuf.off > 5 + 4);
+ ok(sbuf.base[5] == 2 /* PTLS_HANDSHAKE_TYPE_SERVER_HELLO (RETRY_REQUEST) */);
+
+ consumed = sbuf.off;
+ ret = ptls_handshake(client, &cbuf, sbuf.base, &consumed, NULL);
+ ok(ret == PTLS_ERROR_IN_PROGRESS);
+ ok(consumed == sbuf.off);
+ sbuf.off = 0;
+
+ ok(cbuf.off >= 5 + 4);
+ ok(cbuf.base[5] == 1 /* PTLS_HANDSHAKE_TYPE_CLIENT_HELLO */);
+
+ consumed = cbuf.off;
+ ret = ptls_handshake(server, &sbuf, cbuf.base, &consumed, NULL);
+ ok(ret == 0);
+ ok(consumed == cbuf.off);
+ cbuf.off = 0;
+
+ ok(sbuf.off >= 5 + 4);
+ ok(sbuf.base[5] == 2 /* PTLS_HANDSHAKE_TYPE_SERVER_HELLO */);
+
+ consumed = sbuf.off;
+ ret = ptls_handshake(client, &cbuf, sbuf.base, &consumed, NULL);
+ ok(ret == 0);
+ ok(consumed == sbuf.off);
+ sbuf.off = 0;
+
+ ret = ptls_send(client, &cbuf, "hello world", 11);
+ ok(ret == 0);
+
+ consumed = cbuf.off;
+ ret = ptls_receive(server, &decbuf, cbuf.base, &consumed);
+ ok(ret == 0);
+ ok(consumed == cbuf.off);
+ cbuf.off = 0;
+
+ ok(decbuf.off == 11);
+ ok(memcmp(decbuf.base, "hello world", 11) == 0);
+
+ ptls_buffer_dispose(&decbuf);
+ ptls_buffer_dispose(&sbuf);
+ ptls_buffer_dispose(&cbuf);
+ ptls_free(client);
+ ptls_free(server);
+}
+
+int main(int argc, char **argv)
+{
+ subtest("secp256r1", test_secp256r1_key_exchange);
+ subtest("x25519", test_x25519_key_exchange);
+ subtest("secp256r1-sign", test_secp256r1_sign);
+
+ ptls_iovec_t cert = ptls_iovec_init(SECP256R1_CERTIFICATE, sizeof(SECP256R1_CERTIFICATE) - 1);
+
+ ptls_minicrypto_secp256r1sha256_sign_certificate_t sign_certificate;
+ ptls_minicrypto_init_secp256r1sha256_sign_certificate(&sign_certificate,
+ ptls_iovec_init(SECP256R1_PRIVATE_KEY, SECP256R1_PRIVATE_KEY_SIZE));
+
+ ptls_context_t ctxbuf = {ptls_minicrypto_random_bytes,
+ &ptls_get_time,
+ ptls_minicrypto_key_exchanges,
+ ptls_minicrypto_cipher_suites,
+ {&cert, 1},
+ NULL,
+ NULL,
+ &sign_certificate.super};
+ ctx = ctx_peer = &ctxbuf;
+
+ subtest("picotls", test_picotls);
+ subtest("hrr", test_hrr);
+
+ return done_testing();
+ return done_testing();
+}
diff --git a/debian/vendor-h2o/deps/picotls/t/openssl.c b/debian/vendor-h2o/deps/picotls/t/openssl.c
new file mode 100644
index 0000000..9a7fc53
--- /dev/null
+++ b/debian/vendor-h2o/deps/picotls/t/openssl.c
@@ -0,0 +1,227 @@
+/*
+ * Copyright (c) 2016 DeNA Co., Ltd., Kazuho Oku
+ *
+ * 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.
+ */
+#ifdef _WINDOWS
+#include "wincompat.h"
+#endif
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include <openssl/bio.h>
+#include <openssl/pem.h>
+#include <openssl/engine.h>
+#include "picotls.h"
+#include "picotls/minicrypto.h"
+#include "../deps/picotest/picotest.h"
+#include "../lib/openssl.c"
+#include "test.h"
+
+#define RSA_PRIVATE_KEY \
+ "-----BEGIN RSA PRIVATE KEY-----\n" \
+ "MIIEowIBAAKCAQEA5soWzSG7iyawQlHM1yaX2dUAATUkhpbg2WPFOEem7E3zYzc6\n" \
+ "A/Z+bViFlfEgL37cbDUb4pnOAHrrsjGgkyBYh5i9iCTVfCk+H6SOHZJORO1Tq8X9\n" \
+ "C7WcNcshpSdm2Pa8hmv9hsHbLSeoPNeg8NkTPwMVaMZ2GpdmiyAmhzSZ2H9mzNI7\n" \
+ "ntPW/XCchVf+ax2yt9haZ+mQE2NPYwHDjqCtdGkP5ZXXnYhJSBzSEhxfGckIiKDy\n" \
+ "OxiNkLFLvUdT4ERSFBjauP2cSI0XoOUsiBxJNwHH310AU8jZbveSTcXGYgEuu2MI\n" \
+ "uDo7Vhkq5+TCqXsIFNbjy0taOoPRvUbPsbqFlQIDAQABAoIBAQCWcUv1wjR/2+Nw\n" \
+ "B+Swp267R9bt8pdxyK6f5yKrskGErremiFygMrFtVBQYjws9CsRjISehSkN4GqjE\n" \
+ "CweygJZVJeL++YvUmQnvFJSzgCjXU6GEStbOKD/A7T5sa0fmzMhOE907V+kpAT3x\n" \
+ "E1rNRaP/ImJ1X1GjuefVb0rOPiK/dehFQWfsUkOvh+J3PU76wcnexxzJgxhVxdfX\n" \
+ "qNa7UDsWzTImUjcHIfnhXc1K/oSKk6HjImQi/oE4lgoJUCEDaUbq0nXNrM0EmTTv\n" \
+ "OQ5TVP5Lds9p8UDEa55eZllGXam0zKjhDKtkQ/5UfnxsAv2adY5cuH+XN0ExfKD8\n" \
+ "wIZ5qINtAoGBAPRbQGZZkP/HOYA4YZ9HYAUQwFS9IZrQ8Y7C/UbL01Xli13nKalH\n" \
+ "xXdG6Zv6Yv0FCJKA3N945lEof9rwriwhuZbyrA1TcKok/s7HR8Bhcsm2DzRD5OiC\n" \
+ "3HK+Xy+6fBaMebffqBPp3Lfj/lSPNt0w/8DdrKBTw/cAy40g0n1zEu07AoGBAPHJ\n" \
+ "V4IfQBiblCqDh77FfQRUNR4hVbbl00Gviigiw563nk7sxdrOJ1edTyTOUBHtM3zg\n" \
+ "AT9sYz2CUXvsyEPqzMDANWMb9e2R//NcP6aM4k7WQRnwkZkp0WOIH95U2o1MHCYc\n" \
+ "5meAHVf2UMl+64xU2ZfY3rjMmPLjWMt0hKYsOmtvAoGAClIQVkJSLXtsok2/Ucrh\n" \
+ "81TRysJyOOe6TB1QNT1Gn8oiKMUqrUuqu27zTvM0WxtrUUTAD3A7yhG71LN1p8eE\n" \
+ "3ytAuQ9dItKNMI6aKTX0czCNU9fKQ0fDp9UCkDGALDOisHFx1+V4vQuUIl4qIw1+\n" \
+ "v9adA+iFzljqP/uy6DmEAyECgYAyWCgecf9YoFxzlbuYH2rukdIVmf9M/AHG9ZQg\n" \
+ "00xEKhuOd4KjErXiamDmWwcVFHzaDZJ08E6hqhbpZN42Nhe4Ms1q+5FzjCjtNVIT\n" \
+ "jdY5cCdSDWNjru9oeBmao7R2I1jhHrdi6awyeplLu1+0cp50HbYSaJeYS3pbssFE\n" \
+ "EIWBhQKBgG3xleD4Sg9rG2OWQz5IrvLFg/Hy7YWyushVez61kZeLDnt9iM2um76k\n" \
+ "/xFNIW0a+eL2VxRTCbXr9z86hjc/6CeSJHKYFQl4zsSAZkaIJ0+HbrhDNBAYh9b2\n" \
+ "mRdX+OMdZ7Z5J3Glt8ENFRqe8RlESMpAKxjR+dID0bjwAjVr2KCh\n" \
+ "-----END RSA PRIVATE KEY-----\n"
+
+#define RSA_CERTIFICATE \
+ "-----BEGIN CERTIFICATE-----\n" \
+ "MIICqDCCAZACCQDI5jeEvExN+TANBgkqhkiG9w0BAQUFADAWMRQwEgYDVQQDEwtl\n" \
+ "eGFtcGxlLmNvbTAeFw0xNjA5MzAwMzQ0NTFaFw0yNjA5MjgwMzQ0NTFaMBYxFDAS\n" \
+ "BgNVBAMTC2V4YW1wbGUuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC\n" \
+ "AQEA5soWzSG7iyawQlHM1yaX2dUAATUkhpbg2WPFOEem7E3zYzc6A/Z+bViFlfEg\n" \
+ "L37cbDUb4pnOAHrrsjGgkyBYh5i9iCTVfCk+H6SOHZJORO1Tq8X9C7WcNcshpSdm\n" \
+ "2Pa8hmv9hsHbLSeoPNeg8NkTPwMVaMZ2GpdmiyAmhzSZ2H9mzNI7ntPW/XCchVf+\n" \
+ "ax2yt9haZ+mQE2NPYwHDjqCtdGkP5ZXXnYhJSBzSEhxfGckIiKDyOxiNkLFLvUdT\n" \
+ "4ERSFBjauP2cSI0XoOUsiBxJNwHH310AU8jZbveSTcXGYgEuu2MIuDo7Vhkq5+TC\n" \
+ "qXsIFNbjy0taOoPRvUbPsbqFlQIDAQABMA0GCSqGSIb3DQEBBQUAA4IBAQAwZQsG\n" \
+ "E/3DQFBOnmBITFsaIVJVXU0fbfIjy3p1r6O9z2zvrfB1i8AMxOORAVjE5wHstGnK\n" \
+ "3sLMjkMYXqu1XEfQbStQN+Bsi8m+nE/x9MmuLthpzJHXUmPYZ4TKs0KJmFPLTXYi\n" \
+ "j0OrP0a5BNcyGj/B4Z33aaU9N3z0TWBwx4OPjJoK3iInBx80sC1Ig2PE6mDBxLOg\n" \
+ "5Ohm/XU/43MrtH8SgYkxr3OyzXTm8J0RFMWhYlo1uqR+pWV3TgacixNnUq5w5h4m\n" \
+ "sqXcikh+j8ReNXsKnMOAfFo+HbRqyKWNE3DekCIiiQ5ds4A4SfT7pYyGAmBkAxht\n" \
+ "sS919x2o8l97kaYf\n" \
+ "-----END CERTIFICATE-----\n"
+
+static void test_ecdh_key_exchange(void)
+{
+ test_key_exchange(&ptls_openssl_secp256r1);
+}
+
+static void test_rsa_sign(void)
+{
+ ptls_openssl_sign_certificate_t *sc = (ptls_openssl_sign_certificate_t *)ctx->sign_certificate;
+
+ const void *message = "hello world";
+ ptls_buffer_t sigbuf;
+ uint8_t sigbuf_small[1024];
+
+ ptls_buffer_init(&sigbuf, sigbuf_small, sizeof(sigbuf_small));
+ ok(do_sign(sc->key, &sigbuf, ptls_iovec_init(message, strlen(message)), EVP_sha256()) == 0);
+ EVP_PKEY_up_ref(sc->key);
+ ok(verify_sign(sc->key, ptls_iovec_init(message, strlen(message)), ptls_iovec_init(sigbuf.base, sigbuf.off)) == 0);
+
+ ptls_buffer_dispose(&sigbuf);
+}
+
+static void test_ecdsa_sign(void)
+{
+ EVP_PKEY *pkey;
+
+ { /* create pkey */
+ EC_KEY *eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
+ EC_KEY_generate_key(eckey);
+ pkey = EVP_PKEY_new();
+ EVP_PKEY_set1_EC_KEY(pkey, eckey);
+ EC_KEY_free(eckey);
+ }
+
+ const char *message = "hello world";
+ ptls_buffer_t sigbuf;
+ uint8_t sigbuf_small[1024];
+
+ ptls_buffer_init(&sigbuf, sigbuf_small, sizeof(sigbuf_small));
+ ok(do_sign(pkey, &sigbuf, ptls_iovec_init(message, strlen(message)), EVP_sha256()) == 0);
+ EVP_PKEY_up_ref(pkey);
+ ok(verify_sign(pkey, ptls_iovec_init(message, strlen(message)), ptls_iovec_init(sigbuf.base, sigbuf.off)) == 0);
+
+ ptls_buffer_dispose(&sigbuf);
+ EVP_PKEY_free(pkey);
+}
+
+static void setup_certificate(ptls_iovec_t *dst)
+{
+ BIO *bio = BIO_new_mem_buf(RSA_CERTIFICATE, strlen(RSA_CERTIFICATE));
+ X509 *cert = PEM_read_bio_X509(bio, NULL, NULL, NULL);
+ assert(cert != NULL || !!"failed to load certificate");
+ BIO_free(bio);
+
+ dst->base = NULL;
+ dst->len = i2d_X509(cert, &dst->base);
+
+ X509_free(cert);
+}
+
+static void setup_sign_certificate(ptls_openssl_sign_certificate_t *sc)
+{
+ BIO *bio = BIO_new_mem_buf(RSA_PRIVATE_KEY, strlen(RSA_PRIVATE_KEY));
+ EVP_PKEY *pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
+ assert(pkey != NULL || !"failed to load private key");
+ BIO_free(bio);
+
+ ptls_openssl_init_sign_certificate(sc, pkey);
+
+ EVP_PKEY_free(pkey);
+}
+
+int main(int argc, char **argv)
+{
+ ptls_openssl_sign_certificate_t openssl_sign_certificate;
+ ptls_openssl_verify_certificate_t openssl_verify_certificate;
+
+ ERR_load_crypto_strings();
+ OpenSSL_add_all_algorithms();
+#if !defined(OPENSSL_NO_ENGINE)
+ /* Load all compiled-in ENGINEs */
+ ENGINE_load_builtin_engines();
+ ENGINE_register_all_ciphers();
+ ENGINE_register_all_digests();
+#endif
+
+ ptls_iovec_t cert;
+ setup_certificate(&cert);
+ setup_sign_certificate(&openssl_sign_certificate);
+ ptls_openssl_init_verify_certificate(&openssl_verify_certificate, NULL);
+ ptls_context_t openssl_ctx = {ptls_openssl_random_bytes,
+ &ptls_get_time,
+ ptls_openssl_key_exchanges,
+ ptls_openssl_cipher_suites,
+ {&cert, 1},
+ NULL,
+ NULL,
+ &openssl_sign_certificate.super,
+ &openssl_verify_certificate.super};
+ assert(openssl_ctx.cipher_suites[0]->hash->digest_size == 48); /* sha384 */
+ ptls_context_t openssl_ctx_sha256only = openssl_ctx;
+ ++openssl_ctx_sha256only.cipher_suites;
+ assert(openssl_ctx_sha256only.cipher_suites[0]->hash->digest_size == 32); /* sha256 */
+
+ ctx = ctx_peer = &openssl_ctx;
+
+ subtest("ecdh-key-exchange", test_ecdh_key_exchange);
+ subtest("rsa-sign", test_rsa_sign);
+ subtest("ecdsa-sign", test_ecdsa_sign);
+ subtest("picotls", test_picotls);
+
+ ctx = ctx_peer = &openssl_ctx_sha256only;
+ subtest("picotls", test_picotls);
+
+ ctx = &openssl_ctx_sha256only;
+ ctx_peer = &openssl_ctx;
+ subtest("picotls", test_picotls);
+
+ ctx = &openssl_ctx;
+ ctx_peer = &openssl_ctx_sha256only;
+ subtest("picotls", test_picotls);
+
+ ptls_minicrypto_secp256r1sha256_sign_certificate_t minicrypto_sign_certificate;
+ ptls_iovec_t minicrypto_certificate = ptls_iovec_init(SECP256R1_CERTIFICATE, sizeof(SECP256R1_CERTIFICATE) - 1);
+ ptls_minicrypto_init_secp256r1sha256_sign_certificate(
+ &minicrypto_sign_certificate, ptls_iovec_init(SECP256R1_PRIVATE_KEY, sizeof(SECP256R1_PRIVATE_KEY) - 1));
+ ptls_context_t minicrypto_ctx = {ptls_minicrypto_random_bytes,
+ &ptls_get_time,
+ ptls_minicrypto_key_exchanges,
+ ptls_minicrypto_cipher_suites,
+ {&minicrypto_certificate, 1},
+ NULL,
+ NULL,
+ &minicrypto_sign_certificate.super};
+ ctx = &openssl_ctx;
+ ctx_peer = &minicrypto_ctx;
+ subtest("vs. minicrypto", test_picotls);
+
+ ctx = &minicrypto_ctx;
+ ctx_peer = &openssl_ctx;
+ subtest("minicrypto vs.", test_picotls);
+
+ return done_testing();
+}
diff --git a/debian/vendor-h2o/deps/picotls/t/picotls.c b/debian/vendor-h2o/deps/picotls/t/picotls.c
new file mode 100644
index 0000000..bb90c62
--- /dev/null
+++ b/debian/vendor-h2o/deps/picotls/t/picotls.c
@@ -0,0 +1,843 @@
+/*
+ * Copyright (c) 2016 DeNA Co., Ltd., Kazuho Oku
+ *
+ * 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.
+ */
+#ifdef _WINDOWS
+#include "wincompat.h"
+#endif
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+#include "picotls.h"
+#include "picotls/minicrypto.h"
+#include "../deps/picotest/picotest.h"
+#include "../lib/picotls.c"
+#include "test.h"
+
+ptls_context_t *ctx, *ctx_peer;
+
+static ptls_cipher_suite_t *find_cipher(ptls_context_t *ctx, uint16_t id)
+{
+ ptls_cipher_suite_t **cs;
+ for (cs = ctx->cipher_suites; *cs != NULL; ++cs)
+ if ((*cs)->id == id)
+ return *cs;
+ return NULL;
+}
+
+static void test_hash(ptls_hash_algorithm_t *hash)
+{
+ ptls_hash_context_t *hctx = hash->create();
+ uint8_t digest[PTLS_MAX_DIGEST_SIZE];
+
+ hctx->final(hctx, digest, PTLS_HASH_FINAL_MODE_FREE);
+ ok(memcmp(digest, hash->empty_digest, hash->digest_size) == 0);
+}
+
+static void test_sha256(void)
+{
+ test_hash(find_cipher(ctx, PTLS_CIPHER_SUITE_AES_128_GCM_SHA256)->hash);
+}
+
+static void test_sha384(void)
+{
+ ptls_cipher_suite_t *cs = find_cipher(ctx, PTLS_CIPHER_SUITE_AES_256_GCM_SHA384);
+ if (cs != NULL)
+ test_hash(cs->hash);
+}
+
+static void test_hmac_sha256(void)
+{
+ /* test vector from RFC 4231 */
+ const char *secret = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b", *message = "Hi There";
+ uint8_t digest[32];
+
+ ptls_hash_context_t *hctx =
+ ptls_hmac_create(find_cipher(ctx, PTLS_CIPHER_SUITE_AES_128_GCM_SHA256)->hash, secret, strlen(secret));
+ hctx->update(hctx, message, strlen(message));
+ hctx->final(hctx, digest, 0);
+
+ ok(memcmp(digest, "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37"
+ "\x6c\x2e\x32\xcf\xf7",
+ 32) == 0);
+}
+
+static void test_hkdf(void)
+{
+ ptls_hash_algorithm_t *sha256 = find_cipher(ctx, PTLS_CIPHER_SUITE_AES_128_GCM_SHA256)->hash;
+ const char salt[] = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c";
+ const char ikm[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
+ const char info[] = "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9";
+ uint8_t prk[PTLS_MAX_DIGEST_SIZE];
+ uint8_t okm[42];
+
+ ptls_hkdf_extract(sha256, prk, ptls_iovec_init(salt, sizeof(salt) - 1), ptls_iovec_init(ikm, sizeof(ikm) - 1));
+ ok(memcmp(prk, "\x07\x77\x09\x36\x2c\x2e\x32\xdf\x0d\xdc\x3f\x0d\xc4\x7b\xba\x63\x90\xb6\xc7\x3b\xb5\x0f\x9c\x31\x22\xec\x84"
+ "\x4a\xd7\xc2\xb3\xe5",
+ 32) == 0);
+
+ ptls_hkdf_expand(sha256, okm, sizeof(okm), ptls_iovec_init(prk, sha256->digest_size), ptls_iovec_init(info, sizeof(info) - 1));
+ ok(memcmp(okm, "\x3c\xb2\x5f\x25\xfa\xac\xd5\x7a\x90\x43\x4f\x64\xd0\x36\x2f\x2a\x2d\x2d\x0a\x90\xcf\x1a\x5a\x4c\x5d\xb0\x2d"
+ "\x56\xec\xc4\xc5\xbf\x34\x00\x72\x08\xd5\xb8\x87\x18\x58\x65",
+ sizeof(okm)) == 0);
+}
+
+static void test_ciphersuite(ptls_cipher_suite_t *cs1, ptls_cipher_suite_t *cs2)
+{
+ const char *traffic_secret = "01234567890123456789012345678901", *src1 = "hello world", *src2 = "good bye, all";
+ ptls_aead_context_t *c;
+ char enc1[256], enc2[256], dec1[256], dec2[256];
+ size_t enc1len, enc2len, dec1len, dec2len;
+
+ /* encrypt */
+ c = ptls_aead_new(cs1->aead, cs1->hash, 1, traffic_secret, NULL);
+ assert(c != NULL);
+ ptls_aead_encrypt_init(c, 0, NULL, 0);
+ enc1len = ptls_aead_encrypt_update(c, enc1, src1, strlen(src1));
+ enc1len += ptls_aead_encrypt_final(c, enc1 + enc1len);
+ ptls_aead_encrypt_init(c, 1, NULL, 0);
+ enc2len = ptls_aead_encrypt_update(c, enc2, src2, strlen(src2));
+ enc2len += ptls_aead_encrypt_final(c, enc2 + enc2len);
+ ptls_aead_free(c);
+
+ c = ptls_aead_new(cs2->aead, cs2->hash, 0, traffic_secret, NULL);
+ assert(c != NULL);
+
+ /* decrypt and compare */
+ dec1len = ptls_aead_decrypt(c, dec1, enc1, enc1len, 0, NULL, 0);
+ ok(dec1len != SIZE_MAX);
+ dec2len = ptls_aead_decrypt(c, dec2, enc2, enc2len, 1, NULL, 0);
+ ok(dec2len != SIZE_MAX);
+ ok(strlen(src1) == dec1len);
+ ok(memcmp(src1, dec1, dec1len) == 0);
+ ok(strlen(src2) == dec2len);
+ ok(memcmp(src2, dec2, dec2len - 1) == 0);
+
+ /* alter and decrypt to detect failure */
+ enc1[0] ^= 1;
+ dec1len = ptls_aead_decrypt(c, dec1, enc1, enc1len, 0, NULL, 0);
+ ok(dec1len == SIZE_MAX);
+
+ ptls_aead_free(c);
+}
+
+static void test_aad_ciphersuite(ptls_cipher_suite_t *cs1, ptls_cipher_suite_t *cs2)
+{
+ const char *traffic_secret = "01234567890123456789012345678901", *src = "hello world", *aad = "my true aad";
+ ptls_aead_context_t *c;
+ char enc[256], dec[256];
+ size_t enclen, declen;
+
+ /* encrypt */
+ c = ptls_aead_new(cs1->aead, cs1->hash, 1, traffic_secret, NULL);
+ assert(c != NULL);
+ ptls_aead_encrypt_init(c, 123, aad, strlen(aad));
+ enclen = ptls_aead_encrypt_update(c, enc, src, strlen(src));
+ enclen += ptls_aead_encrypt_final(c, enc + enclen);
+ ptls_aead_free(c);
+
+ /* decrypt */
+ c = ptls_aead_new(cs2->aead, cs2->hash, 0, traffic_secret, NULL);
+ assert(c != NULL);
+ declen = ptls_aead_decrypt(c, dec, enc, enclen, 123, aad, strlen(aad));
+ ok(declen == strlen(src));
+ ok(memcmp(src, dec, declen) == 0);
+ declen = ptls_aead_decrypt(c, dec, enc, enclen, 123, "my fake aad", strlen(aad));
+ ok(declen == SIZE_MAX);
+ ptls_aead_free(c);
+}
+
+static void test_ctr(ptls_cipher_suite_t *cs, const uint8_t *key, size_t key_len, const void *iv, size_t iv_len,
+ const void *expected, size_t expected_len)
+{
+ static uint8_t zeroes[64] = {0};
+
+ if (cs == NULL)
+ return;
+
+ ptls_cipher_algorithm_t *algo = cs->aead->ctr_cipher;
+ uint8_t buf[sizeof(zeroes)];
+
+ assert(expected_len <= sizeof(zeroes));
+ ok(algo->key_size == key_len);
+ ok(algo->iv_size == iv_len);
+
+ ptls_cipher_context_t *ctx = ptls_cipher_new(algo, 1, key);
+ assert(ctx != NULL);
+ ptls_cipher_init(ctx, iv);
+ ptls_cipher_encrypt(ctx, buf, zeroes, expected_len);
+ ptls_cipher_free(ctx);
+
+ ok(memcmp(buf, expected, expected_len) == 0);
+}
+
+static void test_aes128ctr(void)
+{
+ static const uint8_t key[] = {0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c},
+ iv[] = {0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a},
+ expected[] = {0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60,
+ 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97};
+
+ test_ctr(find_cipher(ctx, PTLS_CIPHER_SUITE_AES_128_GCM_SHA256), key, sizeof(key), iv, sizeof(iv), expected, sizeof(expected));
+}
+
+static void test_chacha20(void)
+{
+ static const uint8_t key[] = {0, 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},
+ iv[] = {1, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0x4a, 0, 0, 0, 0},
+ expected[] = {0x10, 0xf1, 0xe7, 0xe4, 0xd1, 0x3b, 0x59, 0x15, 0x50, 0x0f, 0xdd,
+ 0x1f, 0xa3, 0x20, 0x71, 0xc4, 0xc7, 0xd1, 0xf4, 0xc7, 0x33, 0xc0,
+ 0x68, 0x03, 0x04, 0x22, 0xaa, 0x9a, 0xc3, 0xd4, 0x6c, 0x4e};
+
+ test_ctr(find_cipher(ctx, PTLS_CIPHER_SUITE_CHACHA20_POLY1305_SHA256), key, sizeof(key), iv, sizeof(iv), expected,
+ sizeof(expected));
+}
+
+static void test_aes128gcm(void)
+{
+ ptls_cipher_suite_t *cs = find_cipher(ctx, PTLS_CIPHER_SUITE_AES_128_GCM_SHA256),
+ *cs_peer = find_cipher(ctx, PTLS_CIPHER_SUITE_AES_128_GCM_SHA256);
+
+ test_ciphersuite(cs, cs_peer);
+ test_aad_ciphersuite(cs, cs_peer);
+}
+
+static void test_aes256gcm(void)
+{
+ ptls_cipher_suite_t *cs = find_cipher(ctx, PTLS_CIPHER_SUITE_AES_256_GCM_SHA384),
+ *cs_peer = find_cipher(ctx, PTLS_CIPHER_SUITE_AES_256_GCM_SHA384);
+
+ if (cs != NULL && cs_peer != NULL) {
+ test_ciphersuite(cs, cs_peer);
+ test_aad_ciphersuite(cs, cs_peer);
+ }
+}
+
+static void test_chacha20poly1305(void)
+{
+ ptls_cipher_suite_t *cs = find_cipher(ctx, PTLS_CIPHER_SUITE_CHACHA20_POLY1305_SHA256),
+ *cs_peer = find_cipher(ctx, PTLS_CIPHER_SUITE_CHACHA20_POLY1305_SHA256);
+
+ if (cs != NULL && cs_peer != NULL) {
+ test_ciphersuite(cs, cs_peer);
+ test_aad_ciphersuite(cs, cs_peer);
+ }
+}
+
+static struct {
+ struct {
+ uint8_t buf[32];
+ size_t len;
+ int is_end_of_record;
+ } vec[16];
+ size_t count;
+} test_fragmented_message_queue = {{{{0}}}};
+
+static int test_fragmented_message_record(ptls_t *tls, ptls_buffer_t *sendbuf, ptls_iovec_t message, int is_end_of_record,
+ ptls_handshake_properties_t *properties)
+{
+ memcpy(test_fragmented_message_queue.vec[test_fragmented_message_queue.count].buf, message.base, message.len);
+ test_fragmented_message_queue.vec[test_fragmented_message_queue.count].len = message.len;
+ test_fragmented_message_queue.vec[test_fragmented_message_queue.count].is_end_of_record = is_end_of_record;
+ ++test_fragmented_message_queue.count;
+
+ return 0;
+}
+
+static void test_fragmented_message(void)
+{
+ ptls_t tls = {NULL};
+ struct st_ptls_record_t rec = {PTLS_CONTENT_TYPE_HANDSHAKE, 0x0301};
+ int ret;
+
+#define SET_RECORD(lit) \
+ do { \
+ rec.length = sizeof(lit) - 1; \
+ rec.fragment = (const uint8_t *)(lit); \
+ } while (0)
+
+ /* not fragmented */
+ test_fragmented_message_queue.count = 0;
+ SET_RECORD("\x01\x00\x00\x03"
+ "abc");
+ ret = handle_handshake_record(&tls, test_fragmented_message_record, NULL, &rec, NULL);
+ ok(ret == 0);
+ ok(test_fragmented_message_queue.count == 1);
+ ok(test_fragmented_message_queue.vec[0].len == rec.length);
+ ok(memcmp(test_fragmented_message_queue.vec[0].buf, rec.fragment, rec.length) == 0);
+ ok(test_fragmented_message_queue.vec[0].is_end_of_record);
+ ok(tls.recvbuf.mess.base == NULL);
+
+ /* fragmented */
+ test_fragmented_message_queue.count = 0;
+ SET_RECORD("\x01\x00\x00\x03"
+ "a");
+ ret = handle_handshake_record(&tls, test_fragmented_message_record, NULL, &rec, NULL);
+ ok(ret == PTLS_ERROR_IN_PROGRESS);
+ ok(tls.recvbuf.mess.base != NULL);
+ ok(test_fragmented_message_queue.count == 0);
+ SET_RECORD("bc\x02\x00\x00\x02"
+ "de"
+ "\x03");
+ ret = handle_handshake_record(&tls, test_fragmented_message_record, NULL, &rec, NULL);
+ ok(ret == PTLS_ERROR_IN_PROGRESS);
+ ok(test_fragmented_message_queue.count == 2);
+ ok(test_fragmented_message_queue.vec[0].len == 7);
+ ok(memcmp(test_fragmented_message_queue.vec[0].buf, "\x01\x00\x00\x03"
+ "abc",
+ 7) == 0);
+ ok(!test_fragmented_message_queue.vec[0].is_end_of_record);
+ ok(test_fragmented_message_queue.vec[1].len == 6);
+ ok(memcmp(test_fragmented_message_queue.vec[1].buf, "\x02\x00\x00\x02"
+ "de",
+ 6) == 0);
+ ok(!test_fragmented_message_queue.vec[1].is_end_of_record);
+ SET_RECORD("\x00\x00\x03"
+ "end");
+ ret = handle_handshake_record(&tls, test_fragmented_message_record, NULL, &rec, NULL);
+ ok(ret == 0);
+ ok(tls.recvbuf.mess.base == NULL);
+ ok(test_fragmented_message_queue.count == 3);
+ ok(test_fragmented_message_queue.vec[2].len == 7);
+ ok(memcmp(test_fragmented_message_queue.vec[2].buf, "\x03\x00\x00\x03"
+ "end",
+ 7) == 0);
+ ok(test_fragmented_message_queue.vec[2].is_end_of_record);
+
+#undef SET_RECORD
+}
+
+static int save_client_hello(ptls_on_client_hello_t *self, ptls_t *tls, ptls_iovec_t server_name, const ptls_iovec_t *protocols,
+ size_t num_protocols, const uint16_t *signature_algorithms, size_t num_signature_algorithms)
+{
+ ptls_set_server_name(tls, (const char *)server_name.base, server_name.len);
+ ptls_set_negotiated_protocol(tls, (const char *)protocols[0].base, protocols[0].len);
+ return 0;
+}
+
+enum { TEST_HANDSHAKE_1RTT, TEST_HANDSHAKE_2RTT, TEST_HANDSHAKE_HRR, TEST_HANDSHAKE_HRR_STATELESS, TEST_HANDSHAKE_EARLY_DATA };
+
+static void test_handshake(ptls_iovec_t ticket, int mode, int expect_ticket, int check_ch)
+{
+ ptls_t *client, *server;
+ ptls_handshake_properties_t client_hs_prop = {{{{NULL}, ticket}}}, server_hs_prop = {{{{NULL}}}};
+ uint8_t cbuf_small[16384], sbuf_small[16384], decbuf_small[16384];
+ ptls_buffer_t cbuf, sbuf, decbuf;
+ size_t consumed, max_early_data_size = 0;
+ int ret;
+ const char *req = "GET / HTTP/1.0\r\n\r\n";
+ const char *resp = "HTTP/1.0 200 OK\r\n\r\nhello world\n";
+
+ client = ptls_new(ctx, 0);
+ server = ptls_new(ctx_peer, 1);
+ ptls_buffer_init(&cbuf, cbuf_small, sizeof(cbuf_small));
+ ptls_buffer_init(&sbuf, sbuf_small, sizeof(sbuf_small));
+ ptls_buffer_init(&decbuf, decbuf_small, sizeof(decbuf_small));
+
+ if (check_ch) {
+ static ptls_on_client_hello_t cb = {save_client_hello};
+ ctx_peer->on_client_hello = &cb;
+ static const ptls_iovec_t protocols[] = {{(uint8_t *)"h2", 2}, {(uint8_t *)"http/1.1", 8}};
+ client_hs_prop.client.negotiated_protocols.list = protocols;
+ client_hs_prop.client.negotiated_protocols.count = sizeof(protocols) / sizeof(protocols[0]);
+ ptls_set_server_name(client, "example.com", 0);
+ }
+
+ switch (mode) {
+ case TEST_HANDSHAKE_HRR:
+ client_hs_prop.client.negotiate_before_key_exchange = 1;
+ break;
+ case TEST_HANDSHAKE_HRR_STATELESS:
+ client_hs_prop.client.negotiate_before_key_exchange = 1;
+ server_hs_prop.server.cookie.key = "0123456789abcdef0123456789abcdef";
+ server_hs_prop.server.retry_uses_cookie = 1;
+ break;
+ case TEST_HANDSHAKE_EARLY_DATA:
+ assert(ctx_peer->max_early_data_size != 0);
+ client_hs_prop.client.max_early_data_size = &max_early_data_size;
+ break;
+ }
+
+ ret = ptls_handshake(client, &cbuf, NULL, NULL, &client_hs_prop);
+ ok(ret == PTLS_ERROR_IN_PROGRESS);
+ ok(cbuf.off != 0);
+
+ switch (mode) {
+ case TEST_HANDSHAKE_2RTT:
+ case TEST_HANDSHAKE_HRR:
+ case TEST_HANDSHAKE_HRR_STATELESS:
+ consumed = cbuf.off;
+ ret = ptls_handshake(server, &sbuf, cbuf.base, &consumed, &server_hs_prop);
+ if (mode == TEST_HANDSHAKE_HRR_STATELESS) {
+ ok(ret == PTLS_ERROR_STATELESS_RETRY);
+ ptls_free(server);
+ server = ptls_new(ctx_peer, 1);
+ } else {
+ ok(ret == PTLS_ERROR_IN_PROGRESS);
+ }
+ ok(cbuf.off == consumed);
+ ok(sbuf.off != 0);
+ cbuf.off = 0;
+ consumed = sbuf.off;
+ ret = ptls_handshake(client, &cbuf, sbuf.base, &consumed, &client_hs_prop);
+ ok(ret == PTLS_ERROR_IN_PROGRESS);
+ ok(sbuf.off == consumed);
+ ok(cbuf.off != 0);
+ sbuf.off = 0;
+ break;
+ case TEST_HANDSHAKE_EARLY_DATA:
+ ok(max_early_data_size == ctx_peer->max_early_data_size);
+ ret = ptls_send(client, &cbuf, req, strlen(req));
+ ok(ret == 0);
+ break;
+ }
+
+ consumed = cbuf.off;
+ ret = ptls_handshake(server, &sbuf, cbuf.base, &consumed, &server_hs_prop);
+ ok(ret == 0);
+ ok(sbuf.off != 0);
+ if (check_ch) {
+ ok(ptls_get_server_name(server) != NULL);
+ ok(strcmp(ptls_get_server_name(server), "example.com") == 0);
+ ok(ptls_get_negotiated_protocol(server) != NULL);
+ ok(strcmp(ptls_get_negotiated_protocol(server), "h2") == 0);
+ } else {
+ ok(ptls_get_server_name(server) == NULL);
+ ok(ptls_get_negotiated_protocol(server) == NULL);
+ }
+
+ if (mode == TEST_HANDSHAKE_EARLY_DATA) {
+ ok(consumed < cbuf.off);
+ memmove(cbuf.base, cbuf.base + consumed, cbuf.off - consumed);
+ cbuf.off -= consumed;
+
+ consumed = cbuf.off;
+ ret = ptls_receive(server, &decbuf, cbuf.base, &consumed);
+ ok(ret == 0);
+ ok(consumed == cbuf.off);
+ ok(decbuf.off == strlen(req));
+ ok(memcmp(decbuf.base, req, decbuf.off) == 0);
+ ok(!ptls_handshake_is_complete(server));
+ cbuf.off = 0;
+ decbuf.off = 0;
+
+ ret = ptls_send(server, &sbuf, resp, strlen(resp));
+ ok(ret == 0);
+ } else {
+ ok(consumed == cbuf.off);
+ cbuf.off = 0;
+ }
+
+ consumed = sbuf.off;
+ ret = ptls_handshake(client, &cbuf, sbuf.base, &consumed, NULL);
+ ok(ret == 0);
+ ok(cbuf.off != 0);
+ if (check_ch) {
+ ok(ptls_get_server_name(client) != NULL);
+ ok(strcmp(ptls_get_server_name(client), "example.com") == 0);
+ ok(ptls_get_negotiated_protocol(client) != NULL);
+ ok(strcmp(ptls_get_negotiated_protocol(client), "h2") == 0);
+ } else {
+ ok(ptls_get_server_name(server) == NULL);
+ ok(ptls_get_negotiated_protocol(server) == NULL);
+ }
+
+ if (expect_ticket) {
+ ok(consumed < sbuf.off);
+ memmove(sbuf.base, sbuf.base + consumed, sbuf.off - consumed);
+ sbuf.off -= consumed;
+ } else {
+ ok(consumed == sbuf.off);
+ sbuf.off = 0;
+ }
+
+ if (mode != TEST_HANDSHAKE_EARLY_DATA) {
+ ret = ptls_send(client, &cbuf, req, strlen(req));
+ ok(ret == 0);
+
+ consumed = cbuf.off;
+ ret = ptls_receive(server, &decbuf, cbuf.base, &consumed);
+ ok(ret == 0);
+ ok(consumed == cbuf.off);
+ ok(decbuf.off == strlen(req));
+ ok(memcmp(decbuf.base, req, strlen(req)) == 0);
+ ok(ptls_handshake_is_complete(server));
+ decbuf.off = 0;
+
+ ret = ptls_send(server, &sbuf, resp, strlen(resp));
+ ok(ret == 0);
+ }
+
+ consumed = sbuf.off;
+ ret = ptls_receive(client, &decbuf, sbuf.base, &consumed);
+ ok(ret == 0);
+ ok(consumed == sbuf.off);
+ ok(decbuf.off == strlen(resp));
+ ok(memcmp(decbuf.base, resp, strlen(resp)) == 0);
+ ok(ptls_handshake_is_complete(client));
+ decbuf.off = 0;
+
+ if (mode == TEST_HANDSHAKE_EARLY_DATA) {
+ consumed = cbuf.off;
+ ret = ptls_receive(server, &decbuf, cbuf.base, &consumed);
+ ok(ret == 0);
+ ok(cbuf.off == consumed);
+ ok(decbuf.off == 0);
+ ok(ptls_handshake_is_complete(client));
+ }
+
+ ptls_buffer_dispose(&cbuf);
+ ptls_buffer_dispose(&sbuf);
+ ptls_buffer_dispose(&decbuf);
+ ptls_free(client);
+ ptls_free(server);
+
+ if (check_ch)
+ ctx_peer->on_client_hello = NULL;
+}
+
+static ptls_sign_certificate_t *sc_orig;
+size_t sc_callcnt;
+
+static int sign_certificate(ptls_sign_certificate_t *self, ptls_t *tls, uint16_t *selected_algorithm, ptls_buffer_t *output,
+ ptls_iovec_t input, const uint16_t *algorithms, size_t num_algorithms)
+{
+ ++sc_callcnt;
+ return sc_orig->cb(sc_orig, tls, selected_algorithm, output, input, algorithms, num_algorithms);
+}
+
+static void test_full_handshake(void)
+{
+ sc_callcnt = 0;
+ test_handshake(ptls_iovec_init(NULL, 0), TEST_HANDSHAKE_1RTT, 0, 0);
+ ok(sc_callcnt == 1);
+ test_handshake(ptls_iovec_init(NULL, 0), TEST_HANDSHAKE_1RTT, 0, 0);
+ ok(sc_callcnt == 2);
+ test_handshake(ptls_iovec_init(NULL, 0), TEST_HANDSHAKE_1RTT, 0, 1);
+ ok(sc_callcnt == 3);
+}
+
+static void test_hrr_handshake(void)
+{
+ sc_callcnt = 0;
+ test_handshake(ptls_iovec_init(NULL, 0), TEST_HANDSHAKE_HRR, 0, 0);
+ ok(sc_callcnt == 1);
+}
+
+static void test_hrr_stateless_handshake(void)
+{
+ sc_callcnt = 0;
+ test_handshake(ptls_iovec_init(NULL, 0), TEST_HANDSHAKE_HRR_STATELESS, 0, 0);
+ ok(sc_callcnt == 1);
+}
+
+static int copy_ticket(ptls_encrypt_ticket_t *self, ptls_t *tls, int is_encrypt, ptls_buffer_t *dst, ptls_iovec_t src)
+{
+ int ret;
+
+ if ((ret = ptls_buffer_reserve(dst, src.len)) != 0)
+ return ret;
+ memcpy(dst->base + dst->off, src.base, src.len);
+ dst->off += src.len;
+
+ return 0;
+}
+
+static ptls_iovec_t saved_ticket = {NULL};
+
+static int save_ticket(ptls_save_ticket_t *self, ptls_t *tls, ptls_iovec_t src)
+{
+ saved_ticket.base = malloc(src.len);
+ memcpy(saved_ticket.base, src.base, src.len);
+ saved_ticket.len = src.len;
+ return 0;
+}
+
+static void do_test_resumption(int different_preferred_key_share)
+{
+ assert(ctx->key_exchanges[0]->id == ctx_peer->key_exchanges[0]->id);
+ assert(ctx->key_exchanges[1] == NULL);
+ assert(ctx_peer->key_exchanges[1] == NULL);
+ assert(ctx->key_exchanges[0]->id != ptls_minicrypto_x25519.id);
+ ptls_key_exchange_algorithm_t *different_key_exchanges[] = {&ptls_minicrypto_x25519, ctx->key_exchanges[0], NULL},
+ **key_exchanges_orig = ctx->key_exchanges;
+
+ if (different_preferred_key_share)
+ ctx->key_exchanges = different_key_exchanges;
+
+ ptls_encrypt_ticket_t et = {copy_ticket};
+ ptls_save_ticket_t st = {save_ticket};
+
+ assert(ctx_peer->ticket_lifetime == 0);
+ assert(ctx_peer->max_early_data_size == 0);
+ assert(ctx_peer->encrypt_ticket == NULL);
+ assert(ctx_peer->save_ticket == NULL);
+ saved_ticket = ptls_iovec_init(NULL, 0);
+
+ ctx_peer->ticket_lifetime = 86400;
+ ctx_peer->max_early_data_size = 8192;
+ ctx_peer->encrypt_ticket = &et;
+ ctx->save_ticket = &st;
+
+ sc_callcnt = 0;
+ test_handshake(saved_ticket, different_preferred_key_share ? TEST_HANDSHAKE_2RTT : TEST_HANDSHAKE_1RTT, 1, 0);
+ ok(sc_callcnt == 1);
+ ok(saved_ticket.base != NULL);
+
+ /* psk using saved ticket */
+ test_handshake(saved_ticket, TEST_HANDSHAKE_1RTT, 1, 0);
+ ok(sc_callcnt == 1);
+
+ /* 0-rtt psk using saved ticket */
+ test_handshake(saved_ticket, TEST_HANDSHAKE_EARLY_DATA, 1, 0);
+ ok(sc_callcnt == 1);
+
+ ctx->require_dhe_on_psk = 1;
+
+ /* psk-dhe using saved ticket */
+ test_handshake(saved_ticket, TEST_HANDSHAKE_1RTT, 1, 0);
+ ok(sc_callcnt == 1);
+
+ /* 0-rtt psk-dhe using saved ticket */
+ test_handshake(saved_ticket, TEST_HANDSHAKE_EARLY_DATA, 1, 0);
+ ok(sc_callcnt == 1);
+
+ ctx->require_dhe_on_psk = 0;
+ ctx_peer->ticket_lifetime = 0;
+ ctx_peer->max_early_data_size = 0;
+ ctx_peer->encrypt_ticket = NULL;
+ ctx->save_ticket = NULL;
+ ctx->key_exchanges = key_exchanges_orig;
+}
+
+static void test_resumption(void)
+{
+ do_test_resumption(0);
+}
+
+static void test_resumption_different_preferred_key_share(void)
+{
+ if (ctx == ctx_peer)
+ return;
+ do_test_resumption(1);
+}
+
+static void test_enforce_retry(int use_cookie)
+{
+ ptls_t *client, *server;
+ ptls_handshake_properties_t server_hs_prop = {{{{NULL}}}};
+ ptls_buffer_t cbuf, sbuf, decbuf;
+ size_t consumed;
+ int ret;
+
+ server_hs_prop.server.cookie.key = "0123456789abcdef0123456789abcdef";
+ server_hs_prop.server.cookie.additional_data = ptls_iovec_init("1.2.3.4:1234", 12);
+ server_hs_prop.server.enforce_retry = 1;
+ server_hs_prop.server.retry_uses_cookie = use_cookie;
+
+ ptls_buffer_init(&cbuf, "", 0);
+ ptls_buffer_init(&sbuf, "", 0);
+ ptls_buffer_init(&decbuf, "", 0);
+
+ client = ptls_new(ctx, 0);
+
+ ret = ptls_handshake(client, &cbuf, NULL, NULL, NULL);
+ ok(ret == PTLS_ERROR_IN_PROGRESS);
+ ok(cbuf.off != 0);
+
+ server = ptls_new(ctx, 1);
+
+ consumed = cbuf.off;
+ ret = ptls_handshake(server, &sbuf, cbuf.base, &consumed, &server_hs_prop);
+ cbuf.off = 0;
+
+ if (use_cookie) {
+ ok(ret == PTLS_ERROR_STATELESS_RETRY);
+ ptls_free(server);
+ server = ptls_new(ctx, 1);
+ } else {
+ ok(ret == PTLS_ERROR_IN_PROGRESS);
+ }
+
+ consumed = sbuf.off;
+ ret = ptls_handshake(client, &cbuf, sbuf.base, &consumed, NULL);
+ ok(ret == PTLS_ERROR_IN_PROGRESS);
+ ok(sbuf.off == consumed);
+ sbuf.off = 0;
+
+ consumed = cbuf.off;
+ ret = ptls_handshake(server, &sbuf, cbuf.base, &consumed, &server_hs_prop);
+ ok(ret == 0);
+ ok(cbuf.off == consumed);
+ cbuf.off = 0;
+
+ consumed = sbuf.off;
+ ret = ptls_handshake(client, &cbuf, sbuf.base, &consumed, NULL);
+ ok(ret == 0);
+ ok(sbuf.off == consumed);
+ sbuf.off = 0;
+
+ ret = ptls_send(client, &cbuf, "hello world", 11);
+ ok(ret == 0);
+
+ consumed = cbuf.off;
+ ret = ptls_receive(server, &decbuf, cbuf.base, &consumed);
+ ok(ret == 0);
+ ok(cbuf.off == consumed);
+ cbuf.off = 0;
+
+ ok(decbuf.off == 11);
+ ok(memcmp(decbuf.base, "hello world", 11) == 0);
+ decbuf.off = 0;
+
+ ptls_free(client);
+ ptls_free(server);
+
+ ptls_buffer_dispose(&cbuf);
+ ptls_buffer_dispose(&sbuf);
+ ptls_buffer_dispose(&decbuf);
+}
+
+static void test_enforce_retry_stateful(void)
+{
+ test_enforce_retry(0);
+}
+
+static void test_enforce_retry_stateless(void)
+{
+ test_enforce_retry(1);
+}
+
+static ptls_t *stateless_hrr_prepare(ptls_buffer_t *sbuf, ptls_handshake_properties_t *server_hs_prop)
+{
+ ptls_t *client = ptls_new(ctx, 0), *server = ptls_new(ctx_peer, 1);
+ ptls_buffer_t cbuf;
+ size_t consumed;
+ int ret;
+
+ ptls_buffer_init(&cbuf, "", 0);
+ ptls_buffer_init(sbuf, "", 0);
+
+ ret = ptls_handshake(client, &cbuf, NULL, NULL, NULL);
+ ok(ret == PTLS_ERROR_IN_PROGRESS);
+
+ consumed = cbuf.off;
+ ret = ptls_handshake(server, sbuf, cbuf.base, &consumed, server_hs_prop);
+ ok(ret == PTLS_ERROR_STATELESS_RETRY);
+
+ ptls_buffer_dispose(&cbuf);
+ ptls_free(server);
+
+ return client;
+}
+
+static void test_stateless_hrr_aad_change(void)
+{
+ ptls_t *client, *server;
+ ptls_handshake_properties_t server_hs_prop = {{{{NULL}}}};
+ ptls_buffer_t cbuf, sbuf;
+ size_t consumed;
+ int ret;
+
+ server_hs_prop.server.cookie.key = "0123456789abcdef0123456789abcdef";
+ server_hs_prop.server.cookie.additional_data = ptls_iovec_init("1.2.3.4:1234", 12);
+ server_hs_prop.server.enforce_retry = 1;
+ server_hs_prop.server.retry_uses_cookie = 1;
+
+ client = stateless_hrr_prepare(&sbuf, &server_hs_prop);
+ ptls_buffer_init(&cbuf, "", 0);
+
+ consumed = sbuf.off;
+ ret = ptls_handshake(client, &cbuf, sbuf.base, &consumed, NULL);
+ ok(ret == PTLS_ERROR_IN_PROGRESS);
+ ok(sbuf.off == consumed);
+ sbuf.off = 0;
+
+ server = ptls_new(ctx_peer, 1);
+ server_hs_prop.server.cookie.additional_data = ptls_iovec_init("1.2.3.4:4321", 12);
+
+ consumed = cbuf.off;
+ ret = ptls_handshake(server, &sbuf, cbuf.base, &consumed, &server_hs_prop);
+ ok(ret == PTLS_ALERT_HANDSHAKE_FAILURE);
+
+ ptls_free(client);
+ ptls_free(server);
+
+ ptls_buffer_dispose(&cbuf);
+ ptls_buffer_dispose(&sbuf);
+}
+
+void test_picotls(void)
+{
+ subtest("sha256", test_sha256);
+ subtest("sha384", test_sha384);
+ subtest("hmac-sha256", test_hmac_sha256);
+ subtest("hkdf", test_hkdf);
+ subtest("aes128gcm", test_aes128gcm);
+ subtest("aes256gcm", test_aes256gcm);
+ subtest("chacha20poly1305", test_chacha20poly1305);
+ subtest("aes128ctr", test_aes128ctr);
+ subtest("chacha20", test_chacha20);
+
+ subtest("fragmented-message", test_fragmented_message);
+
+ ptls_sign_certificate_t sc = {sign_certificate};
+ sc_orig = ctx_peer->sign_certificate;
+ ctx_peer->sign_certificate = &sc;
+
+ subtest("full-handshake", test_full_handshake);
+ subtest("hrr-handshake", test_hrr_handshake);
+ subtest("hrr-stateless-handshake", test_hrr_stateless_handshake);
+ subtest("resumption", test_resumption);
+ subtest("resumption-different-preferred-key-share", test_resumption_different_preferred_key_share);
+
+ subtest("enforce-retry-stateful", test_enforce_retry_stateful);
+ subtest("enforce-retry-stateless", test_enforce_retry_stateless);
+
+ subtest("stateless-hrr-aad-change", test_stateless_hrr_aad_change);
+
+ ctx_peer->sign_certificate = sc_orig;
+}
+
+void test_key_exchange(ptls_key_exchange_algorithm_t *algo)
+{
+ ptls_key_exchange_context_t *ctx;
+ ptls_iovec_t client_pubkey, client_secret, server_pubkey, server_secret;
+ int ret;
+
+ /* fail */
+ ret = algo->exchange(&server_pubkey, &server_secret, (ptls_iovec_t){NULL});
+ ok(ret != 0);
+
+ /* perform ecdh */
+ ret = algo->create(&ctx, &client_pubkey);
+ ok(ret == 0);
+ ret = algo->exchange(&server_pubkey, &server_secret, client_pubkey);
+ ok(ret == 0);
+ ret = ctx->on_exchange(&ctx, &client_secret, server_pubkey);
+ ok(ret == 0);
+ ok(client_secret.len == server_secret.len);
+ ok(memcmp(client_secret.base, server_secret.base, client_secret.len) == 0);
+
+ free(client_secret.base);
+ free(server_pubkey.base);
+ free(server_secret.base);
+}
diff --git a/debian/vendor-h2o/deps/picotls/t/test.h b/debian/vendor-h2o/deps/picotls/t/test.h
new file mode 100644
index 0000000..2f33876
--- /dev/null
+++ b/debian/vendor-h2o/deps/picotls/t/test.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2016 DeNA Co., Ltd., Kazuho Oku
+ *
+ * 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.
+ */
+#ifndef test_h
+#define test_h
+
+#include "picotls.h"
+
+/* raw private key and certificate using secp256v1 */
+#define SECP256R1_PRIVATE_KEY \
+ "\x92\xbe\xc7\x34\x58\xc8\xa7\x1a\x25\x22\xf0\x29\x81\xc8\xca\x33\x84\xa5\xca\x0b\x8f\x0f\x19\x94\x83\xcb\xaf\x3f\x3d\x9f\x19" \
+ "\xa1"
+#define SECP256R1_CERTIFICATE \
+ "\x30\x82\x01\x97\x30\x82\x01\x3f\xa0\x03\x02\x01\x02\x02\x09\x00\xa5\x28\xf1\x53\xe1\x92\xb8\x1c\x30\x09\x06\x07\x2a\x86\x48" \
+ "\xce\x3d\x04\x01\x30\x16\x31\x14\x30\x12\x06\x03\x55\x04\x03\x13\x0b\x65\x78\x61\x6d\x70\x6c\x65\x2e\x63\x6f\x6d\x30\x1e\x17" \
+ "\x0d\x31\x36\x31\x31\x30\x33\x30\x37\x31\x33\x32\x39\x5a\x17\x0d\x32\x36\x31\x31\x30\x31\x30\x37\x31\x33\x32\x39\x5a\x30\x16" \
+ "\x31\x14\x30\x12\x06\x03\x55\x04\x03\x13\x0b\x65\x78\x61\x6d\x70\x6c\x65\x2e\x63\x6f\x6d\x30\x59\x30\x13\x06\x07\x2a\x86\x48" \
+ "\xce\x3d\x02\x01\x06\x08\x2a\x86\x48\xce\x3d\x03\x01\x07\x03\x42\x00\x04\x73\x47\xc4\x07\x56\x9a\x5a\x83\xa2\x49\xba\x34\x73" \
+ "\x66\xd8\xb5\x95\x1e\xd6\xe9\x4e\xaf\x76\x09\x9f\x96\xb6\xb6\xab\xd3\xb9\xf0\x3e\x96\x10\x6f\xb2\xb4\x42\x93\x95\xfc\x30\x61" \
+ "\x3b\xb4\x4b\xa1\x46\x92\xec\xf9\xf1\x0f\x7a\x25\x5c\x87\x29\x3e\x23\x56\x77\x91\xa3\x77\x30\x75\x30\x1d\x06\x03\x55\x1d\x0e" \
+ "\x04\x16\x04\x14\x24\x7a\x07\x7b\x93\xd2\x3a\x60\x5e\xea\xb3\xdf\x21\xdf\x02\x63\x7d\x89\x40\xdd\x30\x46\x06\x03\x55\x1d\x23" \
+ "\x04\x3f\x30\x3d\x80\x14\x24\x7a\x07\x7b\x93\xd2\x3a\x60\x5e\xea\xb3\xdf\x21\xdf\x02\x63\x7d\x89\x40\xdd\xa1\x1a\xa4\x18\x30" \
+ "\x16\x31\x14\x30\x12\x06\x03\x55\x04\x03\x13\x0b\x65\x78\x61\x6d\x70\x6c\x65\x2e\x63\x6f\x6d\x82\x09\x00\xa5\x28\xf1\x53\xe1" \
+ "\x92\xb8\x1c\x30\x0c\x06\x03\x55\x1d\x13\x04\x05\x30\x03\x01\x01\xff\x30\x09\x06\x07\x2a\x86\x48\xce\x3d\x04\x01\x03\x47\x00" \
+ "\x30\x44\x02\x20\x3f\xfc\x14\x45\xa4\xc6\x21\x37\xa9\x4a\x6b\x79\x4d\x86\xea\x48\x2c\xa8\xea\xb8\x18\xd9\xc9\x94\xd0\x15\x38" \
+ "\xa5\xfd\x23\xf1\xb0\x02\x20\x2e\xd4\x93\xfe\x19\xfa\x31\x82\xa0\xfe\xa2\x04\xbd\xf4\x8b\x68\xdb\xee\x7a\xe8\x33\x2c\xe1\x35" \
+ "\x6d\xdc\x08\x37\xfd\x49\x35\x90"
+
+extern ptls_context_t *ctx, *ctx_peer;
+
+void test_key_exchange(ptls_key_exchange_algorithm_t *algo);
+void test_picotls(void);
+
+#endif
diff --git a/debian/vendor-h2o/deps/picotls/t/util.h b/debian/vendor-h2o/deps/picotls/t/util.h
new file mode 100644
index 0000000..78ce87c
--- /dev/null
+++ b/debian/vendor-h2o/deps/picotls/t/util.h
@@ -0,0 +1,240 @@
+/*
+ * Copyright (c) 2016,2017 DeNA Co., Ltd., Kazuho Oku, Fastly
+ *
+ * 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.
+ */
+#ifndef util_h
+#define util_h
+
+#ifndef _XOPEN_SOURCE
+#define _XOPEN_SOURCE 700 /* required for glibc to use getaddrinfo, etc. */
+#endif
+
+#include <errno.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <openssl/pem.h>
+#include "picotls/openssl.h"
+
+static inline void load_certificate_chain(ptls_context_t *ctx, const char *fn)
+{
+ if (ptls_load_certificates(ctx, (char *)fn) != 0) {
+ fprintf(stderr, "failed to load certificate:%s:%s\n", fn, strerror(errno));
+ exit(1);
+ }
+}
+
+static inline void load_private_key(ptls_context_t *ctx, const char *fn)
+{
+ static ptls_openssl_sign_certificate_t sc;
+ FILE *fp;
+ EVP_PKEY *pkey;
+
+ if ((fp = fopen(fn, "rb")) == NULL) {
+ fprintf(stderr, "failed to open file:%s:%s\n", fn, strerror(errno));
+ exit(1);
+ }
+ pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
+ fclose(fp);
+
+ if (pkey == NULL) {
+ fprintf(stderr, "failed to read private key from file:%s\n", fn);
+ exit(1);
+ }
+
+ ptls_openssl_init_sign_certificate(&sc, pkey);
+ EVP_PKEY_free(pkey);
+
+ ctx->sign_certificate = &sc.super;
+}
+
+struct st_util_save_ticket_t {
+ ptls_save_ticket_t super;
+ char fn[MAXPATHLEN];
+};
+
+static int save_ticket_cb(ptls_save_ticket_t *_self, ptls_t *tls, ptls_iovec_t src)
+{
+ struct st_util_save_ticket_t *self = (void *)_self;
+ FILE *fp;
+
+ if ((fp = fopen(self->fn, "wb")) == NULL) {
+ fprintf(stderr, "failed to open file:%s:%s\n", self->fn, strerror(errno));
+ return PTLS_ERROR_LIBRARY;
+ }
+ fwrite(src.base, 1, src.len, fp);
+ fclose(fp);
+
+ return 0;
+}
+
+static inline void setup_session_file(ptls_context_t *ctx, ptls_handshake_properties_t *hsprop, const char *fn)
+{
+ static struct st_util_save_ticket_t st;
+ FILE *fp;
+
+ /* setup save_ticket callback */
+ strcpy(st.fn, fn);
+ st.super.cb = save_ticket_cb;
+ ctx->save_ticket = &st.super;
+
+ /* load session ticket if possible */
+ if ((fp = fopen(fn, "rb")) != NULL) {
+ static uint8_t ticket[16384];
+ size_t ticket_size = fread(ticket, 1, sizeof(ticket), fp);
+ if (ticket_size == 0 || !feof(fp)) {
+ fprintf(stderr, "failed to load ticket from file:%s\n", fn);
+ exit(1);
+ }
+ fclose(fp);
+ hsprop->client.session_ticket = ptls_iovec_init(ticket, ticket_size);
+ }
+}
+
+static inline void setup_verify_certificate(ptls_context_t *ctx)
+{
+ static ptls_openssl_verify_certificate_t vc;
+ ptls_openssl_init_verify_certificate(&vc, NULL);
+ ctx->verify_certificate = &vc.super;
+}
+
+struct st_util_log_secret_t {
+ ptls_log_secret_t super;
+ FILE *fp;
+};
+
+static void fprinthex(FILE *fp, ptls_iovec_t vec)
+{
+ size_t i;
+ for (i = 0; i != vec.len; ++i)
+ fprintf(fp, "%02x", vec.base[i]);
+}
+
+static void log_secret_cb(ptls_log_secret_t *_self, ptls_t *tls, const char *label, ptls_iovec_t secret)
+{
+ struct st_util_log_secret_t *self = (void *)_self;
+
+ fprintf(self->fp, "%s ", label);
+ fprinthex(self->fp, ptls_get_client_random(tls));
+ fprintf(self->fp, " ");
+ fprinthex(self->fp, secret);
+ fprintf(self->fp, "\n");
+ fflush(self->fp);
+}
+
+static inline void setup_log_secret(ptls_context_t *ctx, const char *fn)
+{
+ static struct st_util_log_secret_t ls;
+
+ if ((ls.fp = fopen(fn, "at")) == NULL) {
+ fprintf(stderr, "failed to open file:%s:%s\n", fn, strerror(errno));
+ exit(1);
+ }
+ ls.super.cb = log_secret_cb;
+ ctx->log_secret = &ls.super;
+}
+
+/* single-entry session cache */
+struct st_util_session_cache_t {
+ ptls_encrypt_ticket_t super;
+ uint8_t id[32];
+ ptls_iovec_t data;
+};
+
+static int encrypt_ticket_cb(ptls_encrypt_ticket_t *_self, ptls_t *tls, int is_encrypt, ptls_buffer_t *dst, ptls_iovec_t src)
+{
+ struct st_util_session_cache_t *self = (void *)_self;
+ int ret;
+
+ if (is_encrypt) {
+
+ /* replace the cached entry along with a newly generated session id */
+ free(self->data.base);
+ if ((self->data.base = malloc(src.len)) == NULL)
+ return PTLS_ERROR_NO_MEMORY;
+
+ ptls_get_context(tls)->random_bytes(self->id, sizeof(self->id));
+ memcpy(self->data.base, src.base, src.len);
+ self->data.len = src.len;
+
+ /* store the session id in buffer */
+ if ((ret = ptls_buffer_reserve(dst, sizeof(self->id))) != 0)
+ return ret;
+ memcpy(dst->base + dst->off, self->id, sizeof(self->id));
+ dst->off += sizeof(self->id);
+
+ } else {
+
+ /* check if session id is the one stored in cache */
+ if (src.len != sizeof(self->id))
+ return PTLS_ERROR_SESSION_NOT_FOUND;
+ if (memcmp(self->id, src.base, sizeof(self->id)) != 0)
+ return PTLS_ERROR_SESSION_NOT_FOUND;
+
+ /* return the cached value */
+ if ((ret = ptls_buffer_reserve(dst, self->data.len)) != 0)
+ return ret;
+ memcpy(dst->base + dst->off, self->data.base, self->data.len);
+ dst->off += self->data.len;
+ }
+
+ return 0;
+}
+
+static inline void setup_session_cache(ptls_context_t *ctx)
+{
+ static struct st_util_session_cache_t sc;
+
+ sc.super.cb = encrypt_ticket_cb;
+
+ ctx->ticket_lifetime = 86400;
+ ctx->max_early_data_size = 8192;
+ ctx->encrypt_ticket = &sc.super;
+}
+
+static inline int resolve_address(struct sockaddr *sa, socklen_t *salen, const char *host, const char *port, int family, int type,
+ int proto)
+{
+ struct addrinfo hints, *res;
+ int err;
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = family;
+ hints.ai_socktype = type;
+ hints.ai_protocol = proto;
+ hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV | AI_PASSIVE;
+ if ((err = getaddrinfo(host, port, &hints, &res)) != 0 || res == NULL) {
+ fprintf(stderr, "failed to resolve address:%s:%s:%s\n", host, port,
+ err != 0 ? gai_strerror(err) : "getaddrinfo returned NULL");
+ return -1;
+ }
+
+ memcpy(sa, res->ai_addr, res->ai_addrlen);
+ *salen = res->ai_addrlen;
+
+ freeaddrinfo(res);
+ return 0;
+}
+
+#endif