diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libssl/tls13_internal.h | 19 | ||||
| -rw-r--r-- | src/lib/libssl/tls13_lib.c | 150 | ||||
| -rw-r--r-- | src/lib/libssl/tls13_record_layer.c | 68 |
3 files changed, 217 insertions, 20 deletions
diff --git a/src/lib/libssl/tls13_internal.h b/src/lib/libssl/tls13_internal.h index 859030747f..b33e4818af 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.35 2019/11/20 16:21:20 beck Exp $ */ | 1 | /* $OpenBSD: tls13_internal.h,v 1.36 2019/11/26 23:46:18 beck 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> |
| @@ -38,8 +38,8 @@ __BEGIN_HIDDEN_DECLS | |||
| 38 | #define TLS13_IO_USE_LEGACY -4 | 38 | #define TLS13_IO_USE_LEGACY -4 |
| 39 | 39 | ||
| 40 | typedef void (*tls13_alert_cb)(uint8_t _alert_desc, void *_cb_arg); | 40 | typedef void (*tls13_alert_cb)(uint8_t _alert_desc, void *_cb_arg); |
| 41 | typedef int (*tls13_post_handshake_recv_cb)(void *_cb_arg, CBS *cbs); | 41 | typedef ssize_t (*tls13_phh_recv_cb)(void *_cb_arg, CBS *cbs); |
| 42 | typedef int (*tls13_post_handshake_sent_cb)(void *_cb_arg); | 42 | typedef void (*tls13_phh_sent_cb)(void *_cb_arg); |
| 43 | typedef ssize_t (*tls13_read_cb)(void *_buf, size_t _buflen, void *_cb_arg); | 43 | typedef ssize_t (*tls13_read_cb)(void *_buf, size_t _buflen, void *_cb_arg); |
| 44 | typedef ssize_t (*tls13_write_cb)(const void *_buf, size_t _buflen, | 44 | typedef ssize_t (*tls13_write_cb)(const void *_buf, size_t _buflen, |
| 45 | void *_cb_arg); | 45 | void *_cb_arg); |
| @@ -111,8 +111,8 @@ struct tls13_record_layer; | |||
| 111 | 111 | ||
| 112 | struct tls13_record_layer *tls13_record_layer_new(tls13_read_cb wire_read, | 112 | struct tls13_record_layer *tls13_record_layer_new(tls13_read_cb wire_read, |
| 113 | tls13_write_cb wire_write, tls13_alert_cb alert_cb, | 113 | tls13_write_cb wire_write, tls13_alert_cb alert_cb, |
| 114 | tls13_post_handshake_recv_cb post_handshake_recv_cb, | 114 | tls13_phh_recv_cb phh_recv_cb, |
| 115 | tls13_post_handshake_sent_cb post_handshake_sent_cb, void *cb_arg); | 115 | tls13_phh_sent_cb phh_sent_cb, void *cb_arg); |
| 116 | void tls13_record_layer_free(struct tls13_record_layer *rl); | 116 | void tls13_record_layer_free(struct tls13_record_layer *rl); |
| 117 | void tls13_record_layer_set_aead(struct tls13_record_layer *rl, | 117 | void tls13_record_layer_set_aead(struct tls13_record_layer *rl, |
| 118 | const EVP_AEAD *aead); | 118 | const EVP_AEAD *aead); |
| @@ -172,7 +172,16 @@ struct tls13_ctx { | |||
| 172 | 172 | ||
| 173 | struct tls13_record_layer *rl; | 173 | struct tls13_record_layer *rl; |
| 174 | struct tls13_handshake_msg *hs_msg; | 174 | struct tls13_handshake_msg *hs_msg; |
| 175 | uint8_t key_update_request; | ||
| 176 | int phh_count; | ||
| 177 | time_t phh_last_seen; | ||
| 175 | }; | 178 | }; |
| 179 | #ifndef TLS13_PHH_LIMIT_TIME | ||
| 180 | #define TLS13_PHH_LIMIT_TIME 3600 | ||
| 181 | #endif | ||
| 182 | #ifndef TLS13_PHH_LIMIT | ||
| 183 | #define TLS13_PHH_LIMIT 100 | ||
| 184 | #endif | ||
| 176 | 185 | ||
| 177 | struct tls13_ctx *tls13_ctx_new(int mode); | 186 | struct tls13_ctx *tls13_ctx_new(int mode); |
| 178 | void tls13_ctx_free(struct tls13_ctx *ctx); | 187 | void tls13_ctx_free(struct tls13_ctx *ctx); |
diff --git a/src/lib/libssl/tls13_lib.c b/src/lib/libssl/tls13_lib.c index 61ca3d4682..6876528f50 100644 --- a/src/lib/libssl/tls13_lib.c +++ b/src/lib/libssl/tls13_lib.c | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | /* $OpenBSD: tls13_lib.c,v 1.12 2019/11/17 00:10:47 beck Exp $ */ | 1 | /* $OpenBSD: tls13_lib.c,v 1.13 2019/11/26 23:46:18 beck Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org> |
| 4 | * Copyright (c) 2019 Bob Beck <beck@openbsd.org> | ||
| 4 | * | 5 | * |
| 5 | * Permission to use, copy, modify, and distribute this software for any | 6 | * 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 | * purpose with or without fee is hereby granted, provided that the above |
| @@ -90,6 +91,149 @@ tls13_alert_received_cb(uint8_t alert_desc, void *arg) | |||
| 90 | SSL_CTX_remove_session(s->ctx, s->session); | 91 | SSL_CTX_remove_session(s->ctx, s->session); |
| 91 | } | 92 | } |
| 92 | 93 | ||
| 94 | static int | ||
| 95 | tls13_phh_update_local_traffic_secret(struct tls13_ctx *ctx) | ||
| 96 | { | ||
| 97 | struct tls13_secrets *secrets = ctx->hs->secrets; | ||
| 98 | |||
| 99 | if (ctx->mode == TLS13_HS_CLIENT) | ||
| 100 | return (tls13_update_client_traffic_secret(secrets) && | ||
| 101 | tls13_record_layer_set_write_traffic_key(ctx->rl, | ||
| 102 | &secrets->client_application_traffic)); | ||
| 103 | return (tls13_update_server_traffic_secret(secrets) && | ||
| 104 | tls13_record_layer_set_read_traffic_key(ctx->rl, | ||
| 105 | &secrets->server_application_traffic)); | ||
| 106 | } | ||
| 107 | |||
| 108 | static int | ||
| 109 | tls13_phh_update_peer_traffic_secret(struct tls13_ctx *ctx) | ||
| 110 | { | ||
| 111 | struct tls13_secrets *secrets = ctx->hs->secrets; | ||
| 112 | |||
| 113 | if (ctx->mode == TLS13_HS_CLIENT) | ||
| 114 | return (tls13_update_server_traffic_secret(secrets) && | ||
| 115 | tls13_record_layer_set_read_traffic_key(ctx->rl, | ||
| 116 | &secrets->server_application_traffic)); | ||
| 117 | return (tls13_update_client_traffic_secret(secrets) && | ||
| 118 | tls13_record_layer_set_write_traffic_key(ctx->rl, | ||
| 119 | &secrets->client_application_traffic)); | ||
| 120 | } | ||
| 121 | |||
| 122 | /* | ||
| 123 | * XXX arbitrarily chosen limit of 100 post handshake handshake | ||
| 124 | * messages in an hour - to avoid a hostile peer from constantly | ||
| 125 | * requesting certificates or key renegotiaitons, etc. | ||
| 126 | */ | ||
| 127 | static int | ||
| 128 | tls13_phh_limit_check(struct tls13_ctx *ctx) | ||
| 129 | { | ||
| 130 | time_t now = time(NULL); | ||
| 131 | |||
| 132 | if (ctx->phh_last_seen > now - TLS13_PHH_LIMIT_TIME) { | ||
| 133 | if (ctx->phh_count > TLS13_PHH_LIMIT) | ||
| 134 | return 0; | ||
| 135 | } else | ||
| 136 | ctx->phh_count = 0; | ||
| 137 | ctx->phh_count++; | ||
| 138 | ctx->phh_last_seen = now; | ||
| 139 | return 1; | ||
| 140 | } | ||
| 141 | |||
| 142 | static ssize_t | ||
| 143 | tls13_key_update_recv(struct tls13_ctx *ctx, CBS *cbs) | ||
| 144 | { | ||
| 145 | ssize_t ret = TLS13_IO_FAILURE; | ||
| 146 | |||
| 147 | if (!CBS_get_u8(cbs, &ctx->key_update_request)) | ||
| 148 | goto err; | ||
| 149 | if (CBS_len(cbs) != 0) | ||
| 150 | goto err; | ||
| 151 | |||
| 152 | if (!tls13_phh_update_peer_traffic_secret(ctx)) | ||
| 153 | goto err; | ||
| 154 | |||
| 155 | if (ctx->key_update_request) { | ||
| 156 | CBB cbb; | ||
| 157 | CBS cbs; /* XXX */ | ||
| 158 | |||
| 159 | free(ctx->hs_msg); | ||
| 160 | ctx->hs_msg = tls13_handshake_msg_new(); | ||
| 161 | if (!tls13_handshake_msg_start(ctx->hs_msg, &cbb, TLS13_MT_KEY_UPDATE)) | ||
| 162 | goto err; | ||
| 163 | if (!CBB_add_u8(&cbb, 0)) | ||
| 164 | goto err; | ||
| 165 | if (!tls13_handshake_msg_finish(ctx->hs_msg)) | ||
| 166 | goto err; | ||
| 167 | tls13_handshake_msg_data(ctx->hs_msg, &cbs); | ||
| 168 | ret = tls13_record_layer_phh(ctx->rl, &cbs); | ||
| 169 | |||
| 170 | tls13_handshake_msg_free(ctx->hs_msg); | ||
| 171 | ctx->hs_msg = NULL; | ||
| 172 | } else | ||
| 173 | ret = TLS13_IO_SUCCESS; | ||
| 174 | |||
| 175 | return ret; | ||
| 176 | err: | ||
| 177 | ctx->key_update_request = 0; | ||
| 178 | /* XXX alert */ | ||
| 179 | return TLS13_IO_FAILURE; | ||
| 180 | } | ||
| 181 | |||
| 182 | static void | ||
| 183 | tls13_phh_done_cb(void *cb_arg) | ||
| 184 | { | ||
| 185 | struct tls13_ctx *ctx = cb_arg; | ||
| 186 | |||
| 187 | if (ctx->key_update_request) { | ||
| 188 | tls13_phh_update_local_traffic_secret(ctx); | ||
| 189 | ctx->key_update_request = 0; | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | static ssize_t | ||
| 194 | tls13_phh_received_cb(void *cb_arg, CBS *cbs) | ||
| 195 | { | ||
| 196 | ssize_t ret = TLS13_IO_FAILURE; | ||
| 197 | struct tls13_ctx *ctx = cb_arg; | ||
| 198 | CBS phh_cbs; | ||
| 199 | |||
| 200 | if (!tls13_phh_limit_check(ctx)) | ||
| 201 | return tls13_send_alert(ctx->rl, SSL3_AD_UNEXPECTED_MESSAGE); | ||
| 202 | |||
| 203 | if ((ctx->hs_msg == NULL) && | ||
| 204 | ((ctx->hs_msg = tls13_handshake_msg_new()) == NULL)) | ||
| 205 | return TLS13_IO_FAILURE; | ||
| 206 | |||
| 207 | if (!tls13_handshake_msg_set_buffer(ctx->hs_msg, cbs)) | ||
| 208 | return TLS13_IO_FAILURE; | ||
| 209 | |||
| 210 | if ((ret = tls13_handshake_msg_recv(ctx->hs_msg, ctx->rl)) | ||
| 211 | != TLS13_IO_SUCCESS) | ||
| 212 | return ret; | ||
| 213 | |||
| 214 | if (!tls13_handshake_msg_content(ctx->hs_msg, &phh_cbs)) | ||
| 215 | return TLS13_IO_FAILURE; | ||
| 216 | |||
| 217 | switch(tls13_handshake_msg_type(ctx->hs_msg)) { | ||
| 218 | case TLS13_MT_KEY_UPDATE: | ||
| 219 | ret = tls13_key_update_recv(ctx, &phh_cbs); | ||
| 220 | break; | ||
| 221 | case TLS13_MT_NEW_SESSION_TICKET: | ||
| 222 | /* XXX do nothing for now and ignore this */ | ||
| 223 | break; | ||
| 224 | case TLS13_MT_CERTIFICATE_REQUEST: | ||
| 225 | /* XXX add support if we choose to advertise this */ | ||
| 226 | /* FALLTHROUGH */ | ||
| 227 | default: | ||
| 228 | ret = TLS13_IO_FAILURE; /* XXX send alert */ | ||
| 229 | break; | ||
| 230 | } | ||
| 231 | |||
| 232 | tls13_handshake_msg_free(ctx->hs_msg); | ||
| 233 | ctx->hs_msg = NULL; | ||
| 234 | return ret; | ||
| 235 | } | ||
| 236 | |||
| 93 | struct tls13_ctx * | 237 | struct tls13_ctx * |
| 94 | tls13_ctx_new(int mode) | 238 | tls13_ctx_new(int mode) |
| 95 | { | 239 | { |
| @@ -101,8 +245,8 @@ tls13_ctx_new(int mode) | |||
| 101 | ctx->mode = mode; | 245 | ctx->mode = mode; |
| 102 | 246 | ||
| 103 | if ((ctx->rl = tls13_record_layer_new(tls13_legacy_wire_read_cb, | 247 | if ((ctx->rl = tls13_record_layer_new(tls13_legacy_wire_read_cb, |
| 104 | tls13_legacy_wire_write_cb, tls13_alert_received_cb, NULL, NULL, | 248 | tls13_legacy_wire_write_cb, tls13_alert_received_cb, |
| 105 | ctx)) == NULL) | 249 | tls13_phh_received_cb, tls13_phh_done_cb, ctx)) == NULL) |
| 106 | goto err; | 250 | goto err; |
| 107 | 251 | ||
| 108 | return ctx; | 252 | return ctx; |
diff --git a/src/lib/libssl/tls13_record_layer.c b/src/lib/libssl/tls13_record_layer.c index 188f56e0b4..feaca53181 100644 --- a/src/lib/libssl/tls13_record_layer.c +++ b/src/lib/libssl/tls13_record_layer.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: tls13_record_layer.c,v 1.15 2019/11/18 02:44:20 jsing Exp $ */ | 1 | /* $OpenBSD: tls13_record_layer.c,v 1.16 2019/11/26 23:46:18 beck 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 | * |
| @@ -30,6 +30,7 @@ static ssize_t tls13_record_layer_write_record(struct tls13_record_layer *rl, | |||
| 30 | struct tls13_record_layer { | 30 | struct tls13_record_layer { |
| 31 | int change_cipher_spec_seen; | 31 | int change_cipher_spec_seen; |
| 32 | int handshake_completed; | 32 | int handshake_completed; |
| 33 | int phh; | ||
| 33 | 34 | ||
| 34 | /* | 35 | /* |
| 35 | * Read and/or write channels are closed due to an alert being | 36 | * Read and/or write channels are closed due to an alert being |
| @@ -76,8 +77,8 @@ struct tls13_record_layer { | |||
| 76 | 77 | ||
| 77 | /* Record callbacks. */ | 78 | /* Record callbacks. */ |
| 78 | tls13_alert_cb alert_cb; | 79 | tls13_alert_cb alert_cb; |
| 79 | tls13_post_handshake_recv_cb post_handshake_recv_cb; | 80 | tls13_phh_recv_cb phh_recv_cb; |
| 80 | tls13_post_handshake_sent_cb post_handshake_sent_cb; | 81 | tls13_phh_sent_cb phh_sent_cb; |
| 81 | 82 | ||
| 82 | /* Wire read/write callbacks. */ | 83 | /* Wire read/write callbacks. */ |
| 83 | tls13_read_cb wire_read; | 84 | tls13_read_cb wire_read; |
| @@ -112,8 +113,8 @@ tls13_record_layer_wrec_free(struct tls13_record_layer *rl) | |||
| 112 | struct tls13_record_layer * | 113 | struct tls13_record_layer * |
| 113 | tls13_record_layer_new(tls13_read_cb wire_read, tls13_write_cb wire_write, | 114 | tls13_record_layer_new(tls13_read_cb wire_read, tls13_write_cb wire_write, |
| 114 | tls13_alert_cb alert_cb, | 115 | tls13_alert_cb alert_cb, |
| 115 | tls13_post_handshake_recv_cb post_handshake_recv_cb, | 116 | tls13_phh_recv_cb phh_recv_cb, |
| 116 | tls13_post_handshake_sent_cb post_handshake_sent_cb, | 117 | tls13_phh_sent_cb phh_sent_cb, |
| 117 | void *cb_arg) | 118 | void *cb_arg) |
| 118 | { | 119 | { |
| 119 | struct tls13_record_layer *rl; | 120 | struct tls13_record_layer *rl; |
| @@ -124,8 +125,8 @@ tls13_record_layer_new(tls13_read_cb wire_read, tls13_write_cb wire_write, | |||
| 124 | rl->wire_read = wire_read; | 125 | rl->wire_read = wire_read; |
| 125 | rl->wire_write = wire_write; | 126 | rl->wire_write = wire_write; |
| 126 | rl->alert_cb = alert_cb; | 127 | rl->alert_cb = alert_cb; |
| 127 | rl->post_handshake_recv_cb = post_handshake_recv_cb; | 128 | rl->phh_recv_cb = phh_recv_cb; |
| 128 | rl->post_handshake_sent_cb = post_handshake_sent_cb; | 129 | rl->phh_sent_cb = phh_sent_cb; |
| 129 | rl->cb_arg = cb_arg; | 130 | rl->cb_arg = cb_arg; |
| 130 | 131 | ||
| 131 | return rl; | 132 | return rl; |
| @@ -304,6 +305,8 @@ tls13_record_layer_send_phh(struct tls13_record_layer *rl) | |||
| 304 | 305 | ||
| 305 | CBS_init(&rl->phh_cbs, rl->phh_data, rl->phh_len); | 306 | CBS_init(&rl->phh_cbs, rl->phh_data, rl->phh_len); |
| 306 | 307 | ||
| 308 | rl->phh_sent_cb(rl->cb_arg); | ||
| 309 | |||
| 307 | return TLS13_IO_SUCCESS; | 310 | return TLS13_IO_SUCCESS; |
| 308 | } | 311 | } |
| 309 | 312 | ||
| @@ -812,6 +815,16 @@ tls13_record_layer_read(struct tls13_record_layer *rl, uint8_t content_type, | |||
| 812 | 815 | ||
| 813 | /* XXX - need to check record version. */ | 816 | /* XXX - need to check record version. */ |
| 814 | } | 817 | } |
| 818 | |||
| 819 | /* | ||
| 820 | * If we are in post handshake handshake mode, we may not see | ||
| 821 | * any record type that isn't a handshake until we are done. | ||
| 822 | */ | ||
| 823 | if (rl->phh && rl->rbuf_content_type != SSL3_RT_HANDSHAKE) { | ||
| 824 | /* XXX send unexpected message alert */ | ||
| 825 | return TLS13_IO_FAILURE; | ||
| 826 | } | ||
| 827 | |||
| 815 | if (rl->rbuf_content_type != content_type) { | 828 | if (rl->rbuf_content_type != content_type) { |
| 816 | /* | 829 | /* |
| 817 | * Handshake content can appear as post-handshake messages (yup, | 830 | * Handshake content can appear as post-handshake messages (yup, |
| @@ -821,15 +834,46 @@ tls13_record_layer_read(struct tls13_record_layer *rl, uint8_t content_type, | |||
| 821 | */ | 834 | */ |
| 822 | if (rl->rbuf_content_type == SSL3_RT_HANDSHAKE) { | 835 | if (rl->rbuf_content_type == SSL3_RT_HANDSHAKE) { |
| 823 | if (rl->handshake_completed) { | 836 | if (rl->handshake_completed) { |
| 824 | if (rl->post_handshake_recv_cb != NULL) | 837 | rl->phh = 1; |
| 825 | rl->post_handshake_recv_cb( | 838 | ret = TLS13_IO_FAILURE; |
| 839 | |||
| 840 | /* | ||
| 841 | * The post handshake handshake | ||
| 842 | * receive callback is allowed to | ||
| 843 | * return: | ||
| 844 | * | ||
| 845 | * TLS13_IO_WANT_POLLIN -> | ||
| 846 | * I need more handshake data. | ||
| 847 | * | ||
| 848 | * TLS13_IO_WANT_POLLOUT -> I got the | ||
| 849 | * whole handshake message, and have | ||
| 850 | * enqueued a response | ||
| 851 | * | ||
| 852 | * TLS13_IO_SUCCESS -> I got the whole handshake, | ||
| 853 | * nothing more to do | ||
| 854 | * | ||
| 855 | * TLS13_IO_FAILURE -> something broke. | ||
| 856 | */ | ||
| 857 | if (rl->phh_recv_cb != NULL) { | ||
| 858 | ret = rl->phh_recv_cb( | ||
| 826 | rl->cb_arg, &rl->rbuf_cbs); | 859 | rl->cb_arg, &rl->rbuf_cbs); |
| 860 | } | ||
| 861 | |||
| 827 | tls13_record_layer_rbuf_free(rl); | 862 | tls13_record_layer_rbuf_free(rl); |
| 863 | |||
| 864 | if (ret == TLS13_IO_WANT_POLLIN) | ||
| 865 | return ret; | ||
| 866 | |||
| 828 | /* | 867 | /* |
| 829 | * XXX if handshake or alert queued | 868 | * leave post handshake handshake mode |
| 830 | * return POLLOUT | 869 | * if we do not need more handshake data |
| 831 | */ | 870 | */ |
| 832 | return TLS13_IO_WANT_POLLIN; | 871 | rl->phh = 0; |
| 872 | |||
| 873 | if (ret == TLS13_IO_SUCCESS) | ||
| 874 | return TLS13_IO_WANT_POLLIN; | ||
| 875 | |||
| 876 | return ret; | ||
| 833 | } | 877 | } |
| 834 | } | 878 | } |
| 835 | 879 | ||
