summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorjsing <>2019-01-21 13:45:57 +0000
committerjsing <>2019-01-21 13:45:57 +0000
commitc06f6f3e478fe1e9e0a1f1601f983e3d55479ed3 (patch)
tree14969b4304a48b9fdbf41d756aec5076c5ac5d69 /src/lib
parentbde3ac13e78ee3960e9e0340d4af51a79ada0aa6 (diff)
downloadopenbsd-c06f6f3e478fe1e9e0a1f1601f983e3d55479ed3.tar.gz
openbsd-c06f6f3e478fe1e9e0a1f1601f983e3d55479ed3.tar.bz2
openbsd-c06f6f3e478fe1e9e0a1f1601f983e3d55479ed3.zip
Provide the initial TLSv1.3 client implementation.
Move tls13_connect() to a new tls13_client.c file and provide a legacy wrapper to it, which allocates a struct tls_ctx if necessary. Also move tls13_client_hello_send() to tls13_client.c and actual implement the building of a client hello. ok tb@
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libssl/Makefile3
-rw-r--r--src/lib/libssl/tls13_client.c139
-rw-r--r--src/lib/libssl/tls13_handshake.c27
-rw-r--r--src/lib/libssl/tls13_internal.h9
-rw-r--r--src/lib/libssl/tls13_lib.c39
5 files changed, 192 insertions, 25 deletions
diff --git a/src/lib/libssl/Makefile b/src/lib/libssl/Makefile
index b51d7168c0..de8d66c653 100644
--- a/src/lib/libssl/Makefile
+++ b/src/lib/libssl/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.52 2019/01/21 10:28:52 tb Exp $ 1# $OpenBSD: Makefile,v 1.53 2019/01/21 13:45:57 jsing Exp $
2 2
3.include <bsd.own.mk> 3.include <bsd.own.mk>
4.ifndef NOMAN 4.ifndef NOMAN
@@ -63,6 +63,7 @@ SRCS= \
63 t1_hash.c \ 63 t1_hash.c \
64 t1_lib.c \ 64 t1_lib.c \
65 tls13_buffer.c \ 65 tls13_buffer.c \
66 tls13_client.c \
66 tls13_handshake.c \ 67 tls13_handshake.c \
67 tls13_handshake_msg.c \ 68 tls13_handshake_msg.c \
68 tls13_key_schedule.c \ 69 tls13_key_schedule.c \
diff --git a/src/lib/libssl/tls13_client.c b/src/lib/libssl/tls13_client.c
new file mode 100644
index 0000000000..1a2529be6a
--- /dev/null
+++ b/src/lib/libssl/tls13_client.c
@@ -0,0 +1,139 @@
1/* $OpenBSD: tls13_client.c,v 1.1 2019/01/21 13:45:57 jsing Exp $ */
2/*
3 * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include "ssl_locl.h"
19
20#include <openssl/curve25519.h>
21#include <openssl/ssl3.h>
22
23#include "bytestring.h"
24#include "ssl_tlsext.h"
25#include "tls13_internal.h"
26
27int
28tls13_connect(struct tls13_ctx *ctx)
29{
30 if (ctx->mode != TLS13_HS_CLIENT)
31 return TLS13_IO_FAILURE;
32
33 return tls13_handshake_perform(ctx);
34}
35
36static int
37tls13_client_init(SSL *s)
38{
39 if (!ssl_supported_version_range(s, &S3I(s)->hs_tls13.min_version,
40 &S3I(s)->hs_tls13.max_version)) {
41 SSLerror(s, SSL_R_NO_PROTOCOLS_AVAILABLE);
42 return 0;
43 }
44 s->client_version = s->version = S3I(s)->hs_tls13.max_version;
45
46 if (!ssl_get_new_session(s, 0)) /* XXX */
47 return 0;
48
49 if (!tls1_transcript_init(s))
50 return 0;
51
52 arc4random_buf(s->s3->client_random, SSL3_RANDOM_SIZE);
53
54 return 1;
55}
56
57int
58tls13_legacy_connect(SSL *ssl)
59{
60 struct tls13_ctx *ctx = ssl->internal->tls13;
61 int ret;
62
63 if (ctx == NULL) {
64 if ((ctx = tls13_ctx_new(TLS13_HS_CLIENT)) == NULL) {
65 SSLerror(ssl, ERR_R_INTERNAL_ERROR); /* XXX */
66 return -1;
67 }
68 ssl->internal->tls13 = ctx;
69 ctx->ssl = ssl;
70
71 if (!tls13_client_init(ssl)) {
72 if (ERR_peek_error() == 0)
73 SSLerror(ssl, ERR_R_INTERNAL_ERROR); /* XXX */
74 return -1;
75 }
76 }
77
78 ret = tls13_connect(ctx);
79
80 return tls13_legacy_return_code(ssl, ret);
81}
82
83static int
84tls13_client_hello_build(SSL *s, CBB *cbb)
85{
86 CBB cipher_suites, compression_methods, session_id;
87 uint8_t *sid;
88
89 if (!CBB_add_u16(cbb, TLS1_2_VERSION))
90 goto err;
91 if (!CBB_add_bytes(cbb, s->s3->client_random, SSL3_RANDOM_SIZE))
92 goto err;
93
94 /* Either 32-random bytes or zero length... */
95 /* XXX - session resumption for TLSv1.2? */
96 if (!CBB_add_u8_length_prefixed(cbb, &session_id))
97 goto err;
98 if (!CBB_add_space(&session_id, &sid, 32))
99 goto err;
100 arc4random_buf(sid, 32);
101
102 if (!CBB_add_u16_length_prefixed(cbb, &cipher_suites))
103 goto err;
104 if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &cipher_suites)) {
105 SSLerror(s, SSL_R_NO_CIPHERS_AVAILABLE);
106 goto err;
107 }
108
109 if (!CBB_add_u8_length_prefixed(cbb, &compression_methods))
110 goto err;
111 if (!CBB_add_u8(&compression_methods, 0))
112 goto err;
113
114 if (!tlsext_client_build(s, cbb, SSL_TLSEXT_MSG_CH))
115 goto err;
116
117 if (!CBB_flush(cbb))
118 goto err;
119
120 return 1;
121
122 err:
123 return 0;
124}
125
126int
127tls13_client_hello_send(struct tls13_ctx *ctx)
128{
129 CBB body;
130
131 if (!tls13_handshake_msg_start(ctx->hs_msg, &body, TLS13_MT_CLIENT_HELLO))
132 return 0;
133 if (!tls13_client_hello_build(ctx->ssl, &body))
134 return 0;
135 if (!tls13_handshake_msg_finish(ctx->hs_msg))
136 return 0;
137
138 return 1;
139}
diff --git a/src/lib/libssl/tls13_handshake.c b/src/lib/libssl/tls13_handshake.c
index 78f5611b70..160202421c 100644
--- a/src/lib/libssl/tls13_handshake.c
+++ b/src/lib/libssl/tls13_handshake.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls13_handshake.c,v 1.17 2019/01/21 13:13:46 jsing Exp $ */ 1/* $OpenBSD: tls13_handshake.c,v 1.18 2019/01/21 13:45:57 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2018-2019 Theo Buehler <tb@openbsd.org> 3 * Copyright (c) 2018-2019 Theo Buehler <tb@openbsd.org>
4 * Copyright (c) 2019 Joel Sing <jsing@openbsd.org> 4 * Copyright (c) 2019 Joel Sing <jsing@openbsd.org>
@@ -31,11 +31,7 @@
31struct tls13_handshake_action { 31struct tls13_handshake_action {
32 uint8_t record_type; 32 uint8_t record_type;
33 uint8_t handshake_type; 33 uint8_t handshake_type;
34
35 uint8_t sender; 34 uint8_t sender;
36#define TLS13_HS_CLIENT 1
37#define TLS13_HS_SERVER 2
38
39 uint8_t handshake_complete; 35 uint8_t handshake_complete;
40 36
41 int (*send)(struct tls13_ctx *ctx); 37 int (*send)(struct tls13_ctx *ctx);
@@ -44,7 +40,6 @@ struct tls13_handshake_action {
44 40
45enum tls13_message_type tls13_handshake_active_state(struct tls13_ctx *ctx); 41enum tls13_message_type tls13_handshake_active_state(struct tls13_ctx *ctx);
46 42
47int tls13_connect(struct tls13_ctx *ctx);
48int tls13_accept(struct tls13_ctx *ctx); 43int tls13_accept(struct tls13_ctx *ctx);
49 44
50struct tls13_handshake_action * 45struct tls13_handshake_action *
@@ -313,14 +308,6 @@ tls13_handshake_perform(struct tls13_ctx *ctx)
313} 308}
314 309
315int 310int
316tls13_connect(struct tls13_ctx *ctx)
317{
318 ctx->mode = TLS13_HS_CLIENT;
319
320 return tls13_handshake_perform(ctx);
321}
322
323int
324tls13_accept(struct tls13_ctx *ctx) 311tls13_accept(struct tls13_ctx *ctx)
325{ 312{
326 ctx->mode = TLS13_HS_SERVER; 313 ctx->mode = TLS13_HS_SERVER;
@@ -391,13 +378,13 @@ tls13_handshake_recv_action(struct tls13_ctx *ctx,
391 return TLS13_IO_FAILURE; 378 return TLS13_IO_FAILURE;
392 } 379 }
393 380
394 return action->recv(ctx); 381 /* XXX provide CBS and check all consumed. */
395} 382 ret = action->recv(ctx);
396 383
397int 384 tls13_handshake_msg_free(ctx->hs_msg);
398tls13_client_hello_send(struct tls13_ctx *ctx) 385 ctx->hs_msg = NULL;
399{ 386
400 return 0; 387 return ret;
401} 388}
402 389
403int 390int
diff --git a/src/lib/libssl/tls13_internal.h b/src/lib/libssl/tls13_internal.h
index 6ddce37ca3..2738c40c4c 100644
--- a/src/lib/libssl/tls13_internal.h
+++ b/src/lib/libssl/tls13_internal.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls13_internal.h,v 1.14 2019/01/21 13:13:46 jsing Exp $ */ 1/* $OpenBSD: tls13_internal.h,v 1.15 2019/01/21 13:45:57 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2018 Bob Beck <beck@openbsd.org> 3 * Copyright (c) 2018 Bob Beck <beck@openbsd.org>
4 * Copyright (c) 2018 Theo Buehler <tb@openbsd.org> 4 * Copyright (c) 2018 Theo Buehler <tb@openbsd.org>
@@ -26,6 +26,9 @@
26 26
27__BEGIN_HIDDEN_DECLS 27__BEGIN_HIDDEN_DECLS
28 28
29#define TLS13_HS_CLIENT 1
30#define TLS13_HS_SERVER 2
31
29#define TLS13_IO_SUCCESS 1 32#define TLS13_IO_SUCCESS 1
30#define TLS13_IO_EOF 0 33#define TLS13_IO_EOF 0
31#define TLS13_IO_FAILURE -1 34#define TLS13_IO_FAILURE -1
@@ -152,9 +155,13 @@ struct tls13_ctx {
152 struct tls13_handshake_msg *hs_msg; 155 struct tls13_handshake_msg *hs_msg;
153}; 156};
154 157
158struct tls13_ctx *tls13_ctx_new(int mode);
159void tls13_ctx_free(struct tls13_ctx *ctx);
160
155/* 161/*
156 * Legacy interfaces. 162 * Legacy interfaces.
157 */ 163 */
164int tls13_legacy_return_code(SSL *ssl, ssize_t ret);
158ssize_t tls13_legacy_wire_read_cb(void *buf, size_t n, void *arg); 165ssize_t tls13_legacy_wire_read_cb(void *buf, size_t n, void *arg);
159ssize_t tls13_legacy_wire_write_cb(const void *buf, size_t n, void *arg); 166ssize_t tls13_legacy_wire_write_cb(const void *buf, size_t n, void *arg);
160int tls13_legacy_read_bytes(SSL *ssl, int type, unsigned char *buf, int len, 167int tls13_legacy_read_bytes(SSL *ssl, int type, unsigned char *buf, int len,
diff --git a/src/lib/libssl/tls13_lib.c b/src/lib/libssl/tls13_lib.c
index c4cce26ca5..3860ddefef 100644
--- a/src/lib/libssl/tls13_lib.c
+++ b/src/lib/libssl/tls13_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls13_lib.c,v 1.2 2019/01/21 10:24:25 jsing Exp $ */ 1/* $OpenBSD: tls13_lib.c,v 1.3 2019/01/21 13:45:57 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -61,6 +61,39 @@ tls13_cipher_hash(const SSL_CIPHER *cipher)
61 return NULL; 61 return NULL;
62} 62}
63 63
64struct tls13_ctx *
65tls13_ctx_new(int mode)
66{
67 struct tls13_ctx *ctx = NULL;
68
69 if ((ctx = calloc(sizeof(struct tls13_ctx), 1)) == NULL)
70 goto err;
71
72 ctx->mode = mode;
73
74 if ((ctx->rl = tls13_record_layer_new(tls13_legacy_wire_read_cb,
75 tls13_legacy_wire_write_cb, NULL, NULL, ctx)) == NULL)
76 goto err;
77
78 return ctx;
79
80 err:
81 tls13_ctx_free(ctx);
82
83 return NULL;
84}
85
86void
87tls13_ctx_free(struct tls13_ctx *ctx)
88{
89 if (ctx == NULL)
90 return;
91
92 tls13_record_layer_free(ctx->rl);
93
94 freezero(ctx, sizeof(struct tls13_ctx));
95}
96
64static ssize_t 97static ssize_t
65tls13_legacy_wire_read(SSL *ssl, uint8_t *buf, size_t len) 98tls13_legacy_wire_read(SSL *ssl, uint8_t *buf, size_t len)
66{ 99{
@@ -131,7 +164,7 @@ tls13_legacy_wire_write_cb(const void *buf, size_t n, void *arg)
131 return tls13_legacy_wire_write(ctx->ssl, buf, n); 164 return tls13_legacy_wire_write(ctx->ssl, buf, n);
132} 165}
133 166
134static int 167int
135tls13_legacy_return_code(SSL *ssl, ssize_t ret) 168tls13_legacy_return_code(SSL *ssl, ssize_t ret)
136{ 169{
137 if (ret > INT_MAX) { 170 if (ret > INT_MAX) {
@@ -139,7 +172,7 @@ tls13_legacy_return_code(SSL *ssl, ssize_t ret)
139 return -1; 172 return -1;
140 } 173 }
141 174
142 /* A successful read or write. */ 175 /* A successful read, write or other operation. */
143 if (ret > 0) 176 if (ret > 0)
144 return ret; 177 return ret;
145 178