Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 1 | /* |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 2 | * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved. |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 3 | * |
Rich Salz | 846e33c | 2016-05-17 14:18:30 -0400 | [diff] [blame] | 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
Dr. Stephen Henson | 9289f21 | 2009-05-16 11:15:42 +0000 | [diff] [blame] | 11 | #define USE_SOCKETS |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 12 | #include <openssl/objects.h> |
Matt Caswell | 8ba708e | 2015-09-11 10:48:59 +0100 | [diff] [blame] | 13 | #include <openssl/rand.h> |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 14 | #include "ssl_locl.h" |
| 15 | |
Richard Levitte | 14097b6 | 2017-02-28 14:07:08 +0100 | [diff] [blame] | 16 | #if defined(OPENSSL_SYS_VXWORKS) |
Matt Caswell | fb45690 | 2015-04-30 09:40:55 +0100 | [diff] [blame] | 17 | # include <sys/times.h> |
| 18 | #elif !defined(OPENSSL_SYS_WIN32) |
| 19 | # include <sys/time.h> |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 20 | #endif |
| 21 | |
| 22 | static void get_current_time(struct timeval *t); |
Dr. Stephen Henson | 173e72e | 2013-03-11 15:34:28 +0000 | [diff] [blame] | 23 | static int dtls1_handshake_write(SSL *s); |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 24 | static size_t dtls1_link_min_mtu(void); |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 25 | |
Matt Caswell | 8ba708e | 2015-09-11 10:48:59 +0100 | [diff] [blame] | 26 | /* XDTLS: figure out the right values */ |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 27 | static const size_t g_probable_mtu[] = { 1500, 512, 256 }; |
Matt Caswell | 8ba708e | 2015-09-11 10:48:59 +0100 | [diff] [blame] | 28 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 29 | const SSL3_ENC_METHOD DTLSv1_enc_data = { |
| 30 | tls1_enc, |
| 31 | tls1_mac, |
| 32 | tls1_setup_key_block, |
| 33 | tls1_generate_master_secret, |
| 34 | tls1_change_cipher_state, |
| 35 | tls1_final_finish_mac, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 36 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 37 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 38 | tls1_alert_code, |
| 39 | tls1_export_keying_material, |
| 40 | SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV, |
Matt Caswell | a29fa98 | 2016-09-29 22:40:15 +0100 | [diff] [blame] | 41 | dtls1_set_handshake_header, |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 42 | dtls1_close_construct_packet, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 43 | dtls1_handshake_write |
| 44 | }; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 45 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 46 | const SSL3_ENC_METHOD DTLSv1_2_enc_data = { |
| 47 | tls1_enc, |
| 48 | tls1_mac, |
| 49 | tls1_setup_key_block, |
| 50 | tls1_generate_master_secret, |
| 51 | tls1_change_cipher_state, |
| 52 | tls1_final_finish_mac, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 53 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 54 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 55 | tls1_alert_code, |
| 56 | tls1_export_keying_material, |
| 57 | SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS |
| 58 | | SSL_ENC_FLAG_SHA256_PRF | SSL_ENC_FLAG_TLS1_2_CIPHERS, |
Matt Caswell | a29fa98 | 2016-09-29 22:40:15 +0100 | [diff] [blame] | 59 | dtls1_set_handshake_header, |
Matt Caswell | 2c7b4db | 2016-08-03 20:57:52 +0100 | [diff] [blame] | 60 | dtls1_close_construct_packet, |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 61 | dtls1_handshake_write |
| 62 | }; |
Dr. Stephen Henson | c3b344e | 2013-03-20 15:49:14 +0000 | [diff] [blame] | 63 | |
Dr. Stephen Henson | f3b656b | 2005-08-05 23:56:11 +0000 | [diff] [blame] | 64 | long dtls1_default_timeout(void) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 65 | { |
| 66 | /* |
| 67 | * 2 hours, the 24 hours mentioned in the DTLSv1 spec is way too long for |
| 68 | * http, the cache would over fill |
| 69 | */ |
| 70 | return (60 * 60 * 2); |
| 71 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 72 | |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 73 | int dtls1_new(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 74 | { |
| 75 | DTLS1_STATE *d1; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 76 | |
Viktor Dukhovni | 61986d3 | 2015-04-16 01:50:03 -0400 | [diff] [blame] | 77 | if (!DTLS_RECORD_LAYER_new(&s->rlayer)) { |
Matt Caswell | 5fb6f80 | 2015-02-03 16:11:49 +0000 | [diff] [blame] | 78 | return 0; |
| 79 | } |
FdaSilvaYY | 0485d54 | 2016-06-28 22:51:27 +0200 | [diff] [blame] | 80 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 81 | if (!ssl3_new(s)) |
| 82 | return (0); |
Rich Salz | b51bce9 | 2015-08-25 13:25:58 -0400 | [diff] [blame] | 83 | if ((d1 = OPENSSL_zalloc(sizeof(*d1))) == NULL) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 84 | ssl3_free(s); |
| 85 | return (0); |
| 86 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 87 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 88 | d1->buffered_messages = pqueue_new(); |
| 89 | d1->sent_messages = pqueue_new(); |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 90 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 91 | if (s->server) { |
| 92 | d1->cookie_len = sizeof(s->d1->cookie); |
| 93 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 94 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 95 | d1->link_mtu = 0; |
| 96 | d1->mtu = 0; |
Matt Caswell | 59669b6 | 2014-12-01 23:58:05 +0000 | [diff] [blame] | 97 | |
Matt Caswell | a71edf3 | 2015-10-30 10:05:53 +0000 | [diff] [blame] | 98 | if (d1->buffered_messages == NULL || d1->sent_messages == NULL) { |
Rich Salz | 25aaa98 | 2015-05-01 14:37:16 -0400 | [diff] [blame] | 99 | pqueue_free(d1->buffered_messages); |
| 100 | pqueue_free(d1->sent_messages); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 101 | OPENSSL_free(d1); |
| 102 | ssl3_free(s); |
| 103 | return (0); |
| 104 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 105 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 106 | s->d1 = d1; |
| 107 | s->method->ssl_clear(s); |
| 108 | return (1); |
| 109 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 110 | |
Dr. Stephen Henson | 7832d6a | 2011-05-25 12:28:06 +0000 | [diff] [blame] | 111 | static void dtls1_clear_queues(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 112 | { |
Matt Caswell | f5c7f5d | 2016-06-30 13:17:08 +0100 | [diff] [blame] | 113 | dtls1_clear_received_buffer(s); |
| 114 | dtls1_clear_sent_buffer(s); |
| 115 | } |
| 116 | |
| 117 | void dtls1_clear_received_buffer(SSL *s) |
| 118 | { |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 119 | pitem *item = NULL; |
| 120 | hm_fragment *frag = NULL; |
Dr. Stephen Henson | 006b54a | 2011-05-25 14:30:20 +0000 | [diff] [blame] | 121 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 122 | while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) { |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 123 | frag = (hm_fragment *)item->data; |
Matt Caswell | 8a35dbb | 2014-11-25 13:36:00 +0000 | [diff] [blame] | 124 | dtls1_hm_fragment_free(frag); |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 125 | pitem_free(item); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 126 | } |
Matt Caswell | f5c7f5d | 2016-06-30 13:17:08 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void dtls1_clear_sent_buffer(SSL *s) |
| 130 | { |
| 131 | pitem *item = NULL; |
| 132 | hm_fragment *frag = NULL; |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 133 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 134 | while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) { |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 135 | frag = (hm_fragment *)item->data; |
Matt Caswell | 8a35dbb | 2014-11-25 13:36:00 +0000 | [diff] [blame] | 136 | dtls1_hm_fragment_free(frag); |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 137 | pitem_free(item); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 138 | } |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 139 | } |
Dr. Stephen Henson | 7832d6a | 2011-05-25 12:28:06 +0000 | [diff] [blame] | 140 | |
Matt Caswell | f5c7f5d | 2016-06-30 13:17:08 +0100 | [diff] [blame] | 141 | |
Dr. Stephen Henson | 7832d6a | 2011-05-25 12:28:06 +0000 | [diff] [blame] | 142 | void dtls1_free(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 143 | { |
Matt Caswell | 40f3718 | 2015-02-03 14:54:13 +0000 | [diff] [blame] | 144 | DTLS_RECORD_LAYER_free(&s->rlayer); |
| 145 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 146 | ssl3_free(s); |
Dr. Stephen Henson | 7832d6a | 2011-05-25 12:28:06 +0000 | [diff] [blame] | 147 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 148 | dtls1_clear_queues(s); |
Dr. Stephen Henson | 7832d6a | 2011-05-25 12:28:06 +0000 | [diff] [blame] | 149 | |
Dr. Stephen Henson | 7832d6a | 2011-05-25 12:28:06 +0000 | [diff] [blame] | 150 | pqueue_free(s->d1->buffered_messages); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 151 | pqueue_free(s->d1->sent_messages); |
Dr. Stephen Henson | e5fa864 | 2009-04-15 15:27:03 +0000 | [diff] [blame] | 152 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 153 | OPENSSL_free(s->d1); |
| 154 | s->d1 = NULL; |
| 155 | } |
Ben Laurie | 36d16f8 | 2005-04-26 16:02:40 +0000 | [diff] [blame] | 156 | |
| 157 | void dtls1_clear(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 158 | { |
Rich Salz | cf2cede | 2016-01-22 14:54:01 -0500 | [diff] [blame] | 159 | pqueue *buffered_messages; |
| 160 | pqueue *sent_messages; |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 161 | size_t mtu; |
| 162 | size_t link_mtu; |
Dr. Stephen Henson | 1d7392f | 2011-09-23 13:34:48 +0000 | [diff] [blame] | 163 | |
Matt Caswell | 40f3718 | 2015-02-03 14:54:13 +0000 | [diff] [blame] | 164 | DTLS_RECORD_LAYER_clear(&s->rlayer); |
| 165 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 166 | if (s->d1) { |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 167 | buffered_messages = s->d1->buffered_messages; |
| 168 | sent_messages = s->d1->sent_messages; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 169 | mtu = s->d1->mtu; |
| 170 | link_mtu = s->d1->link_mtu; |
Dr. Stephen Henson | 7832d6a | 2011-05-25 12:28:06 +0000 | [diff] [blame] | 171 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 172 | dtls1_clear_queues(s); |
Dr. Stephen Henson | 7832d6a | 2011-05-25 12:28:06 +0000 | [diff] [blame] | 173 | |
Rich Salz | 16f8d4e | 2015-05-04 18:00:15 -0400 | [diff] [blame] | 174 | memset(s->d1, 0, sizeof(*s->d1)); |
Dr. Stephen Henson | 7832d6a | 2011-05-25 12:28:06 +0000 | [diff] [blame] | 175 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 176 | if (s->server) { |
| 177 | s->d1->cookie_len = sizeof(s->d1->cookie); |
| 178 | } |
Dr. Stephen Henson | 7832d6a | 2011-05-25 12:28:06 +0000 | [diff] [blame] | 179 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 180 | if (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU) { |
| 181 | s->d1->mtu = mtu; |
| 182 | s->d1->link_mtu = link_mtu; |
| 183 | } |
Dr. Stephen Henson | 1d7392f | 2011-09-23 13:34:48 +0000 | [diff] [blame] | 184 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 185 | s->d1->buffered_messages = buffered_messages; |
| 186 | s->d1->sent_messages = sent_messages; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 187 | } |
Dr. Stephen Henson | 7832d6a | 2011-05-25 12:28:06 +0000 | [diff] [blame] | 188 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 189 | ssl3_clear(s); |
David Woodhouse | 032924c | 2016-07-25 18:03:27 +0100 | [diff] [blame] | 190 | |
| 191 | if (s->method->version == DTLS_ANY_VERSION) |
Viktor Dukhovni | 4fa5214 | 2015-12-29 03:24:17 -0500 | [diff] [blame] | 192 | s->version = DTLS_MAX_VERSION; |
David Woodhouse | 032924c | 2016-07-25 18:03:27 +0100 | [diff] [blame] | 193 | #ifndef OPENSSL_NO_DTLS1_METHOD |
| 194 | else if (s->options & SSL_OP_CISCO_ANYCONNECT) |
| 195 | s->client_version = s->version = DTLS1_BAD_VER; |
| 196 | #endif |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 197 | else |
| 198 | s->version = s->method->version; |
| 199 | } |
Andy Polyakov | 5d58f1b | 2007-10-05 21:04:56 +0000 | [diff] [blame] | 200 | |
Dr. Stephen Henson | b972fba | 2009-08-12 13:19:54 +0000 | [diff] [blame] | 201 | long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 202 | { |
| 203 | int ret = 0; |
Dr. Stephen Henson | b972fba | 2009-08-12 13:19:54 +0000 | [diff] [blame] | 204 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 205 | switch (cmd) { |
| 206 | case DTLS_CTRL_GET_TIMEOUT: |
| 207 | if (dtls1_get_timeout(s, (struct timeval *)parg) != NULL) { |
| 208 | ret = 1; |
| 209 | } |
| 210 | break; |
| 211 | case DTLS_CTRL_HANDLE_TIMEOUT: |
| 212 | ret = dtls1_handle_timeout(s); |
| 213 | break; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 214 | case DTLS_CTRL_SET_LINK_MTU: |
| 215 | if (larg < (long)dtls1_link_min_mtu()) |
| 216 | return 0; |
| 217 | s->d1->link_mtu = larg; |
| 218 | return 1; |
| 219 | case DTLS_CTRL_GET_LINK_MIN_MTU: |
| 220 | return (long)dtls1_link_min_mtu(); |
| 221 | case SSL_CTRL_SET_MTU: |
| 222 | /* |
| 223 | * We may not have a BIO set yet so can't call dtls1_min_mtu() |
| 224 | * We'll have to make do with dtls1_link_min_mtu() and max overhead |
| 225 | */ |
| 226 | if (larg < (long)dtls1_link_min_mtu() - DTLS1_MAX_MTU_OVERHEAD) |
| 227 | return 0; |
| 228 | s->d1->mtu = larg; |
| 229 | return larg; |
| 230 | default: |
| 231 | ret = ssl3_ctrl(s, cmd, larg, parg); |
| 232 | break; |
| 233 | } |
| 234 | return (ret); |
| 235 | } |
Dr. Stephen Henson | b972fba | 2009-08-12 13:19:54 +0000 | [diff] [blame] | 236 | |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 237 | void dtls1_start_timer(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 238 | { |
Dr. Stephen Henson | 7e159e0 | 2011-12-25 14:45:15 +0000 | [diff] [blame] | 239 | #ifndef OPENSSL_NO_SCTP |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 240 | /* Disable timer for SCTP */ |
| 241 | if (BIO_dgram_is_sctp(SSL_get_wbio(s))) { |
Rich Salz | 16f8d4e | 2015-05-04 18:00:15 -0400 | [diff] [blame] | 242 | memset(&s->d1->next_timeout, 0, sizeof(s->d1->next_timeout)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 243 | return; |
| 244 | } |
Dr. Stephen Henson | 7e159e0 | 2011-12-25 14:45:15 +0000 | [diff] [blame] | 245 | #endif |
| 246 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 247 | /* If timer is not set, initialize duration with 1 second */ |
| 248 | if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) { |
| 249 | s->d1->timeout_duration = 1; |
| 250 | } |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 251 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 252 | /* Set timeout to current time */ |
| 253 | get_current_time(&(s->d1->next_timeout)); |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 254 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 255 | /* Add duration to current time */ |
| 256 | s->d1->next_timeout.tv_sec += s->d1->timeout_duration; |
| 257 | BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, |
| 258 | &(s->d1->next_timeout)); |
| 259 | } |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 260 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 261 | struct timeval *dtls1_get_timeout(SSL *s, struct timeval *timeleft) |
| 262 | { |
| 263 | struct timeval timenow; |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 264 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 265 | /* If no timeout is set, just return NULL */ |
| 266 | if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) { |
| 267 | return NULL; |
| 268 | } |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 269 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 270 | /* Get current time */ |
| 271 | get_current_time(&timenow); |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 272 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 273 | /* If timer already expired, set remaining time to 0 */ |
| 274 | if (s->d1->next_timeout.tv_sec < timenow.tv_sec || |
| 275 | (s->d1->next_timeout.tv_sec == timenow.tv_sec && |
| 276 | s->d1->next_timeout.tv_usec <= timenow.tv_usec)) { |
Rich Salz | 16f8d4e | 2015-05-04 18:00:15 -0400 | [diff] [blame] | 277 | memset(timeleft, 0, sizeof(*timeleft)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 278 | return timeleft; |
| 279 | } |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 280 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 281 | /* Calculate time left until timer expires */ |
| 282 | memcpy(timeleft, &(s->d1->next_timeout), sizeof(struct timeval)); |
| 283 | timeleft->tv_sec -= timenow.tv_sec; |
| 284 | timeleft->tv_usec -= timenow.tv_usec; |
| 285 | if (timeleft->tv_usec < 0) { |
| 286 | timeleft->tv_sec--; |
| 287 | timeleft->tv_usec += 1000000; |
| 288 | } |
Dr. Stephen Henson | 87a37cb | 2010-04-06 12:29:31 +0000 | [diff] [blame] | 289 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 290 | /* |
| 291 | * If remaining time is less than 15 ms, set it to 0 to prevent issues |
FdaSilvaYY | f430ba3 | 2016-06-19 19:39:01 +0200 | [diff] [blame] | 292 | * because of small divergences with socket timeouts. |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 293 | */ |
| 294 | if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) { |
Rich Salz | 16f8d4e | 2015-05-04 18:00:15 -0400 | [diff] [blame] | 295 | memset(timeleft, 0, sizeof(*timeleft)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | return timeleft; |
| 299 | } |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 300 | |
| 301 | int dtls1_is_timer_expired(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 302 | { |
| 303 | struct timeval timeleft; |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 304 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 305 | /* Get time left until timeout, return false if no timer running */ |
| 306 | if (dtls1_get_timeout(s, &timeleft) == NULL) { |
| 307 | return 0; |
| 308 | } |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 309 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 310 | /* Return false if timer is not expired yet */ |
| 311 | if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) { |
| 312 | return 0; |
| 313 | } |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 314 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 315 | /* Timer expired, so return true */ |
| 316 | return 1; |
| 317 | } |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 318 | |
| 319 | void dtls1_double_timeout(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 320 | { |
| 321 | s->d1->timeout_duration *= 2; |
| 322 | if (s->d1->timeout_duration > 60) |
| 323 | s->d1->timeout_duration = 60; |
| 324 | dtls1_start_timer(s); |
| 325 | } |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 326 | |
| 327 | void dtls1_stop_timer(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 328 | { |
| 329 | /* Reset everything */ |
Rich Salz | 16f8d4e | 2015-05-04 18:00:15 -0400 | [diff] [blame] | 330 | memset(&s->d1->timeout, 0, sizeof(s->d1->timeout)); |
| 331 | memset(&s->d1->next_timeout, 0, sizeof(s->d1->next_timeout)); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 332 | s->d1->timeout_duration = 1; |
| 333 | BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, |
| 334 | &(s->d1->next_timeout)); |
| 335 | /* Clear retransmission buffer */ |
Matt Caswell | f5c7f5d | 2016-06-30 13:17:08 +0100 | [diff] [blame] | 336 | dtls1_clear_sent_buffer(s); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 337 | } |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 338 | |
Dr. Stephen Henson | ea6e386 | 2012-03-09 15:52:33 +0000 | [diff] [blame] | 339 | int dtls1_check_timeout_num(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 340 | { |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 341 | size_t mtu; |
Matt Caswell | 047f215 | 2014-12-02 11:16:35 +0000 | [diff] [blame] | 342 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 343 | s->d1->timeout.num_alerts++; |
Dr. Stephen Henson | ea6e386 | 2012-03-09 15:52:33 +0000 | [diff] [blame] | 344 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 345 | /* Reduce MTU after 2 unsuccessful retransmissions */ |
| 346 | if (s->d1->timeout.num_alerts > 2 |
| 347 | && !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) { |
| 348 | mtu = |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 349 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 350 | if (mtu < s->d1->mtu) |
| 351 | s->d1->mtu = mtu; |
| 352 | } |
Dr. Stephen Henson | ea6e386 | 2012-03-09 15:52:33 +0000 | [diff] [blame] | 353 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 354 | if (s->d1->timeout.num_alerts > DTLS1_TMO_ALERT_COUNT) { |
| 355 | /* fail the connection, enough alerts have been sent */ |
| 356 | SSLerr(SSL_F_DTLS1_CHECK_TIMEOUT_NUM, SSL_R_READ_TIMEOUT_EXPIRED); |
| 357 | return -1; |
| 358 | } |
Dr. Stephen Henson | ea6e386 | 2012-03-09 15:52:33 +0000 | [diff] [blame] | 359 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 360 | return 0; |
| 361 | } |
Dr. Stephen Henson | ea6e386 | 2012-03-09 15:52:33 +0000 | [diff] [blame] | 362 | |
Dr. Stephen Henson | b972fba | 2009-08-12 13:19:54 +0000 | [diff] [blame] | 363 | int dtls1_handle_timeout(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 364 | { |
| 365 | /* if no timer is expired, don't do anything */ |
| 366 | if (!dtls1_is_timer_expired(s)) { |
| 367 | return 0; |
| 368 | } |
Dr. Stephen Henson | b972fba | 2009-08-12 13:19:54 +0000 | [diff] [blame] | 369 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 370 | dtls1_double_timeout(s); |
Dr. Stephen Henson | ea6e386 | 2012-03-09 15:52:33 +0000 | [diff] [blame] | 371 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 372 | if (dtls1_check_timeout_num(s) < 0) |
| 373 | return -1; |
Dr. Stephen Henson | b972fba | 2009-08-12 13:19:54 +0000 | [diff] [blame] | 374 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 375 | s->d1->timeout.read_timeouts++; |
| 376 | if (s->d1->timeout.read_timeouts > DTLS1_TMO_READ_COUNT) { |
| 377 | s->d1->timeout.read_timeouts = 1; |
| 378 | } |
Dr. Stephen Henson | 4817504 | 2011-12-31 22:59:57 +0000 | [diff] [blame] | 379 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 380 | dtls1_start_timer(s); |
| 381 | return dtls1_retransmit_buffered_messages(s); |
| 382 | } |
Dr. Stephen Henson | b972fba | 2009-08-12 13:19:54 +0000 | [diff] [blame] | 383 | |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 384 | static void get_current_time(struct timeval *t) |
| 385 | { |
Andy Polyakov | a006fef | 2013-01-19 21:23:13 +0100 | [diff] [blame] | 386 | #if defined(_WIN32) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 387 | SYSTEMTIME st; |
| 388 | union { |
| 389 | unsigned __int64 ul; |
| 390 | FILETIME ft; |
| 391 | } now; |
Andy Polyakov | f469880 | 2012-12-16 19:02:59 +0000 | [diff] [blame] | 392 | |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 393 | GetSystemTime(&st); |
| 394 | SystemTimeToFileTime(&st, &now.ft); |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 395 | /* re-bias to 1/1/1970 */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 396 | # ifdef __MINGW32__ |
| 397 | now.ul -= 116444736000000000ULL; |
| 398 | # else |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 399 | /* *INDENT-OFF* */ |
| 400 | now.ul -= 116444736000000000UI64; |
| 401 | /* *INDENT-ON* */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 402 | # endif |
| 403 | t->tv_sec = (long)(now.ul / 10000000); |
| 404 | t->tv_usec = ((int)(now.ul % 10000000)) / 10; |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 405 | #else |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 406 | gettimeofday(t, NULL); |
Dr. Stephen Henson | eb38b26 | 2009-05-15 22:58:40 +0000 | [diff] [blame] | 407 | #endif |
| 408 | } |
Dr. Stephen Henson | 1fc3ac8 | 2009-09-09 17:05:18 +0000 | [diff] [blame] | 409 | |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 410 | #define LISTEN_SUCCESS 2 |
| 411 | #define LISTEN_SEND_VERIFY_REQUEST 1 |
| 412 | |
Matt Caswell | f9e5503 | 2016-03-21 15:32:40 +0000 | [diff] [blame] | 413 | #ifndef OPENSSL_NO_SOCK |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 414 | int DTLSv1_listen(SSL *s, BIO_ADDR *client) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 415 | { |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 416 | int next, n, ret = 0, clearpkt = 0; |
| 417 | unsigned char cookie[DTLS1_COOKIE_LENGTH]; |
| 418 | unsigned char seq[SEQ_NUM_SIZE]; |
Emilia Kasper | b698174 | 2016-02-01 15:26:18 +0100 | [diff] [blame] | 419 | const unsigned char *data; |
Matt Caswell | c536b6b | 2016-09-21 11:26:47 +0100 | [diff] [blame] | 420 | unsigned char *buf; |
Matt Caswell | 8b0e934 | 2016-10-06 19:17:54 +0100 | [diff] [blame] | 421 | size_t fragoff, fraglen, msglen; |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 422 | unsigned int rectype, versmajor, msgseq, msgtype, clientvers, cookielen; |
| 423 | BIO *rbio, *wbio; |
| 424 | BUF_MEM *bufm; |
Richard Levitte | d858c87 | 2016-02-03 00:27:44 +0100 | [diff] [blame] | 425 | BIO_ADDR *tmpclient = NULL; |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 426 | PACKET pkt, msgpkt, msgpayload, session, cookiepkt; |
Dr. Stephen Henson | 1fc3ac8 | 2009-09-09 17:05:18 +0000 | [diff] [blame] | 427 | |
Matt Caswell | 5bdcd36 | 2016-11-23 23:03:13 +0000 | [diff] [blame] | 428 | if (s->handshake_func == NULL) { |
| 429 | /* Not properly initialized yet */ |
| 430 | SSL_set_accept_state(s); |
| 431 | } |
| 432 | |
Matt Caswell | e83ee04 | 2015-03-09 16:09:04 +0000 | [diff] [blame] | 433 | /* Ensure there is no state left over from a previous invocation */ |
Viktor Dukhovni | 61986d3 | 2015-04-16 01:50:03 -0400 | [diff] [blame] | 434 | if (!SSL_clear(s)) |
Matt Caswell | c7f5b5d | 2015-03-23 15:27:40 +0000 | [diff] [blame] | 435 | return -1; |
Matt Caswell | e83ee04 | 2015-03-09 16:09:04 +0000 | [diff] [blame] | 436 | |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 437 | ERR_clear_error(); |
| 438 | |
| 439 | rbio = SSL_get_rbio(s); |
| 440 | wbio = SSL_get_wbio(s); |
| 441 | |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 442 | if (!rbio || !wbio) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 443 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_BIO_NOT_SET); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 444 | return -1; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * We only peek at incoming ClientHello's until we're sure we are going to |
| 449 | * to respond with a HelloVerifyRequest. If its a ClientHello with a valid |
Viktor Dukhovni | aea145e | 2016-01-09 17:18:02 -0500 | [diff] [blame] | 450 | * cookie then we leave it in the BIO for accept to handle. |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 451 | */ |
| 452 | BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 1, NULL); |
| 453 | |
| 454 | /* |
| 455 | * Note: This check deliberately excludes DTLS1_BAD_VER because that version |
| 456 | * requires the MAC to be calculated *including* the first ClientHello |
| 457 | * (without the cookie). Since DTLSv1_listen is stateless that cannot be |
| 458 | * supported. DTLS1_BAD_VER must use cookies in a stateful manner (e.g. via |
| 459 | * SSL_accept) |
| 460 | */ |
| 461 | if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 462 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNSUPPORTED_SSL_VERSION); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 463 | return -1; |
| 464 | } |
| 465 | |
| 466 | if (s->init_buf == NULL) { |
| 467 | if ((bufm = BUF_MEM_new()) == NULL) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 468 | SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 469 | return -1; |
| 470 | } |
| 471 | |
| 472 | if (!BUF_MEM_grow(bufm, SSL3_RT_MAX_PLAIN_LENGTH)) { |
| 473 | BUF_MEM_free(bufm); |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 474 | SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 475 | return -1; |
| 476 | } |
| 477 | s->init_buf = bufm; |
| 478 | } |
| 479 | buf = (unsigned char *)s->init_buf->data; |
| 480 | |
| 481 | do { |
| 482 | /* Get a packet */ |
| 483 | |
| 484 | clear_sys_error(); |
| 485 | /* |
| 486 | * Technically a ClientHello could be SSL3_RT_MAX_PLAIN_LENGTH |
| 487 | * + DTLS1_RT_HEADER_LENGTH bytes long. Normally init_buf does not store |
| 488 | * the record header as well, but we do here. We've set up init_buf to |
| 489 | * be the standard size for simplicity. In practice we shouldn't ever |
| 490 | * receive a ClientHello as long as this. If we do it will get dropped |
| 491 | * in the record length check below. |
| 492 | */ |
| 493 | n = BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH); |
| 494 | |
| 495 | if (n <= 0) { |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 496 | if (BIO_should_retry(rbio)) { |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 497 | /* Non-blocking IO */ |
| 498 | goto end; |
| 499 | } |
| 500 | return -1; |
| 501 | } |
| 502 | |
| 503 | /* If we hit any problems we need to clear this packet from the BIO */ |
| 504 | clearpkt = 1; |
| 505 | |
| 506 | if (!PACKET_buf_init(&pkt, buf, n)) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 507 | SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 508 | return -1; |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * Parse the received record. If there are any problems with it we just |
| 513 | * dump it - with no alert. RFC6347 says this "Unlike TLS, DTLS is |
| 514 | * resilient in the face of invalid records (e.g., invalid formatting, |
| 515 | * length, MAC, etc.). In general, invalid records SHOULD be silently |
| 516 | * discarded, thus preserving the association; however, an error MAY be |
| 517 | * logged for diagnostic purposes." |
| 518 | */ |
| 519 | |
| 520 | /* this packet contained a partial record, dump it */ |
| 521 | if (n < DTLS1_RT_HEADER_LENGTH) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 522 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_RECORD_TOO_SMALL); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 523 | goto end; |
| 524 | } |
| 525 | |
| 526 | if (s->msg_callback) |
| 527 | s->msg_callback(0, 0, SSL3_RT_HEADER, buf, |
| 528 | DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg); |
| 529 | |
| 530 | /* Get the record header */ |
| 531 | if (!PACKET_get_1(&pkt, &rectype) |
| 532 | || !PACKET_get_1(&pkt, &versmajor)) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 533 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 534 | goto end; |
| 535 | } |
| 536 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 537 | if (rectype != SSL3_RT_HANDSHAKE) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 538 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 539 | goto end; |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * Check record version number. We only check that the major version is |
| 544 | * the same. |
| 545 | */ |
| 546 | if (versmajor != DTLS1_VERSION_MAJOR) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 547 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_BAD_PROTOCOL_VERSION_NUMBER); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 548 | goto end; |
| 549 | } |
| 550 | |
| 551 | if (!PACKET_forward(&pkt, 1) |
| 552 | /* Save the sequence number: 64 bits, with top 2 bytes = epoch */ |
| 553 | || !PACKET_copy_bytes(&pkt, seq, SEQ_NUM_SIZE) |
Matt Caswell | 4b1043e | 2015-11-04 13:53:57 +0000 | [diff] [blame] | 554 | || !PACKET_get_length_prefixed_2(&pkt, &msgpkt)) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 555 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 556 | goto end; |
| 557 | } |
Matt Caswell | 4b1043e | 2015-11-04 13:53:57 +0000 | [diff] [blame] | 558 | /* |
| 559 | * We allow data remaining at the end of the packet because there could |
| 560 | * be a second record (but we ignore it) |
| 561 | */ |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 562 | |
| 563 | /* This is an initial ClientHello so the epoch has to be 0 */ |
| 564 | if (seq[0] != 0 || seq[1] != 0) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 565 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 566 | goto end; |
| 567 | } |
| 568 | |
| 569 | /* Get a pointer to the raw message for the later callback */ |
| 570 | data = PACKET_data(&msgpkt); |
| 571 | |
| 572 | /* Finished processing the record header, now process the message */ |
| 573 | if (!PACKET_get_1(&msgpkt, &msgtype) |
Matt Caswell | 153703d | 2016-10-19 14:39:39 +0100 | [diff] [blame] | 574 | || !PACKET_get_net_3_len(&msgpkt, &msglen) |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 575 | || !PACKET_get_net_2(&msgpkt, &msgseq) |
Matt Caswell | 153703d | 2016-10-19 14:39:39 +0100 | [diff] [blame] | 576 | || !PACKET_get_net_3_len(&msgpkt, &fragoff) |
| 577 | || !PACKET_get_net_3_len(&msgpkt, &fraglen) |
Matt Caswell | 4b1043e | 2015-11-04 13:53:57 +0000 | [diff] [blame] | 578 | || !PACKET_get_sub_packet(&msgpkt, &msgpayload, fraglen) |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 579 | || PACKET_remaining(&msgpkt) != 0) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 580 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 581 | goto end; |
| 582 | } |
| 583 | |
| 584 | if (msgtype != SSL3_MT_CLIENT_HELLO) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 585 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 586 | goto end; |
| 587 | } |
| 588 | |
| 589 | /* Message sequence number can only be 0 or 1 */ |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 590 | if (msgseq > 2) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 591 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_INVALID_SEQUENCE_NUMBER); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 592 | goto end; |
| 593 | } |
| 594 | |
Matt Caswell | 4b1043e | 2015-11-04 13:53:57 +0000 | [diff] [blame] | 595 | /* |
| 596 | * We don't support fragment reassembly for ClientHellos whilst |
| 597 | * listening because that would require server side state (which is |
| 598 | * against the whole point of the ClientHello/HelloVerifyRequest |
| 599 | * mechanism). Instead we only look at the first ClientHello fragment |
| 600 | * and require that the cookie must be contained within it. |
| 601 | */ |
| 602 | if (fragoff != 0 || fraglen > msglen) { |
| 603 | /* Non initial ClientHello fragment (or bad fragment) */ |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 604 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_FRAGMENTED_CLIENT_HELLO); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 605 | goto end; |
| 606 | } |
| 607 | |
| 608 | if (s->msg_callback) |
| 609 | s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, data, |
Matt Caswell | 4b1043e | 2015-11-04 13:53:57 +0000 | [diff] [blame] | 610 | fraglen + DTLS1_HM_HEADER_LENGTH, s, |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 611 | s->msg_callback_arg); |
| 612 | |
| 613 | if (!PACKET_get_net_2(&msgpayload, &clientvers)) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 614 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 615 | goto end; |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * Verify client version is supported |
| 620 | */ |
Viktor Dukhovni | 4fa5214 | 2015-12-29 03:24:17 -0500 | [diff] [blame] | 621 | if (DTLS_VERSION_LT(clientvers, (unsigned int)s->method->version) && |
| 622 | s->method->version != DTLS_ANY_VERSION) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 623 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_WRONG_VERSION_NUMBER); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 624 | goto end; |
| 625 | } |
| 626 | |
| 627 | if (!PACKET_forward(&msgpayload, SSL3_RANDOM_SIZE) |
| 628 | || !PACKET_get_length_prefixed_1(&msgpayload, &session) |
| 629 | || !PACKET_get_length_prefixed_1(&msgpayload, &cookiepkt)) { |
Matt Caswell | 4b1043e | 2015-11-04 13:53:57 +0000 | [diff] [blame] | 630 | /* |
| 631 | * Could be malformed or the cookie does not fit within the initial |
| 632 | * ClientHello fragment. Either way we can't handle it. |
| 633 | */ |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 634 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 635 | goto end; |
| 636 | } |
| 637 | |
| 638 | /* |
| 639 | * Check if we have a cookie or not. If not we need to send a |
| 640 | * HelloVerifyRequest. |
| 641 | */ |
| 642 | if (PACKET_remaining(&cookiepkt) == 0) { |
| 643 | next = LISTEN_SEND_VERIFY_REQUEST; |
| 644 | } else { |
| 645 | /* |
| 646 | * We have a cookie, so lets check it. |
| 647 | */ |
| 648 | if (s->ctx->app_verify_cookie_cb == NULL) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 649 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_NO_VERIFY_COOKIE_CALLBACK); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 650 | /* This is fatal */ |
| 651 | return -1; |
| 652 | } |
Emilia Kasper | 3101154 | 2015-10-06 17:20:32 +0200 | [diff] [blame] | 653 | if (s->ctx->app_verify_cookie_cb(s, PACKET_data(&cookiepkt), |
Matt Caswell | 8b0e934 | 2016-10-06 19:17:54 +0100 | [diff] [blame] | 654 | (unsigned int)PACKET_remaining(&cookiepkt)) == 0) { |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 655 | /* |
| 656 | * We treat invalid cookies in the same was as no cookie as |
| 657 | * per RFC6347 |
| 658 | */ |
| 659 | next = LISTEN_SEND_VERIFY_REQUEST; |
| 660 | } else { |
| 661 | /* Cookie verification succeeded */ |
| 662 | next = LISTEN_SUCCESS; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | if (next == LISTEN_SEND_VERIFY_REQUEST) { |
Matt Caswell | c536b6b | 2016-09-21 11:26:47 +0100 | [diff] [blame] | 667 | WPACKET wpkt; |
| 668 | unsigned int version; |
| 669 | size_t wreclen; |
| 670 | |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 671 | /* |
| 672 | * There was no cookie in the ClientHello so we need to send a |
| 673 | * HelloVerifyRequest. If this fails we do not worry about trying |
| 674 | * to resend, we just drop it. |
| 675 | */ |
| 676 | |
| 677 | /* |
| 678 | * Dump the read packet, we don't need it any more. Ignore return |
| 679 | * value |
| 680 | */ |
| 681 | BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 0, NULL); |
| 682 | BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH); |
| 683 | BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 1, NULL); |
| 684 | |
| 685 | /* Generate the cookie */ |
| 686 | if (s->ctx->app_gen_cookie_cb == NULL || |
Matt Caswell | 373dc6e | 2015-09-23 12:57:34 +0100 | [diff] [blame] | 687 | s->ctx->app_gen_cookie_cb(s, cookie, &cookielen) == 0 || |
| 688 | cookielen > 255) { |
Matt Caswell | 3edeb62 | 2016-02-05 10:59:42 +0000 | [diff] [blame] | 689 | SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_COOKIE_GEN_CALLBACK_FAILURE); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 690 | /* This is fatal */ |
| 691 | return -1; |
| 692 | } |
| 693 | |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 694 | /* |
| 695 | * Special case: for hello verify request, client version 1.0 and we |
| 696 | * haven't decided which version to use yet send back using version |
| 697 | * 1.0 header: otherwise some clients will ignore it. |
| 698 | */ |
Matt Caswell | c536b6b | 2016-09-21 11:26:47 +0100 | [diff] [blame] | 699 | version = (s->method->version == DTLS_ANY_VERSION) ? DTLS1_VERSION |
| 700 | : s->version; |
| 701 | |
| 702 | /* Construct the record and message headers */ |
| 703 | if (!WPACKET_init(&wpkt, s->init_buf) |
| 704 | || !WPACKET_put_bytes_u8(&wpkt, SSL3_RT_HANDSHAKE) |
| 705 | || !WPACKET_put_bytes_u16(&wpkt, version) |
| 706 | /* |
| 707 | * Record sequence number is always the same as in the |
| 708 | * received ClientHello |
| 709 | */ |
| 710 | || !WPACKET_memcpy(&wpkt, seq, SEQ_NUM_SIZE) |
| 711 | /* End of record, start sub packet for message */ |
| 712 | || !WPACKET_start_sub_packet_u16(&wpkt) |
| 713 | /* Message type */ |
| 714 | || !WPACKET_put_bytes_u8(&wpkt, |
| 715 | DTLS1_MT_HELLO_VERIFY_REQUEST) |
| 716 | /* |
| 717 | * Message length - doesn't follow normal TLS convention: |
| 718 | * the length isn't the last thing in the message header. |
| 719 | * We'll need to fill this in later when we know the |
| 720 | * length. Set it to zero for now |
| 721 | */ |
| 722 | || !WPACKET_put_bytes_u24(&wpkt, 0) |
| 723 | /* |
| 724 | * Message sequence number is always 0 for a |
| 725 | * HelloVerifyRequest |
| 726 | */ |
| 727 | || !WPACKET_put_bytes_u16(&wpkt, 0) |
| 728 | /* |
| 729 | * We never fragment a HelloVerifyRequest, so fragment |
| 730 | * offset is 0 |
| 731 | */ |
| 732 | || !WPACKET_put_bytes_u24(&wpkt, 0) |
| 733 | /* |
| 734 | * Fragment length is the same as message length, but |
| 735 | * this *is* the last thing in the message header so we |
| 736 | * can just start a sub-packet. No need to come back |
| 737 | * later for this one. |
| 738 | */ |
| 739 | || !WPACKET_start_sub_packet_u24(&wpkt) |
| 740 | /* Create the actual HelloVerifyRequest body */ |
| 741 | || !dtls_raw_hello_verify_request(&wpkt, cookie, cookielen) |
| 742 | /* Close message body */ |
| 743 | || !WPACKET_close(&wpkt) |
| 744 | /* Close record body */ |
| 745 | || !WPACKET_close(&wpkt) |
| 746 | || !WPACKET_get_total_written(&wpkt, &wreclen) |
| 747 | || !WPACKET_finish(&wpkt)) { |
| 748 | SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR); |
| 749 | WPACKET_cleanup(&wpkt); |
| 750 | /* This is fatal */ |
| 751 | return -1; |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /* |
Matt Caswell | c536b6b | 2016-09-21 11:26:47 +0100 | [diff] [blame] | 755 | * Fix up the message len in the message header. Its the same as the |
| 756 | * fragment len which has been filled in by WPACKET, so just copy |
| 757 | * that. Destination for the message len is after the record header |
| 758 | * plus one byte for the message content type. The source is the |
| 759 | * last 3 bytes of the message header |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 760 | */ |
Matt Caswell | c536b6b | 2016-09-21 11:26:47 +0100 | [diff] [blame] | 761 | memcpy(&buf[DTLS1_RT_HEADER_LENGTH + 1], |
| 762 | &buf[DTLS1_RT_HEADER_LENGTH + DTLS1_HM_HEADER_LENGTH - 3], |
| 763 | 3); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 764 | |
| 765 | if (s->msg_callback) |
| 766 | s->msg_callback(1, 0, SSL3_RT_HEADER, buf, |
| 767 | DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg); |
| 768 | |
Matt Caswell | ce0865d | 2016-01-21 12:22:58 +0000 | [diff] [blame] | 769 | if ((tmpclient = BIO_ADDR_new()) == NULL) { |
| 770 | SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE); |
| 771 | goto end; |
| 772 | } |
| 773 | |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 774 | /* |
FdaSilvaYY | 8483a00 | 2016-03-10 21:34:48 +0100 | [diff] [blame] | 775 | * This is unnecessary if rbio and wbio are one and the same - but |
Matt Caswell | ce0865d | 2016-01-21 12:22:58 +0000 | [diff] [blame] | 776 | * maybe they're not. We ignore errors here - some BIOs do not |
| 777 | * support this. |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 778 | */ |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 779 | if (BIO_dgram_get_peer(rbio, tmpclient) > 0) { |
Matt Caswell | ce0865d | 2016-01-21 12:22:58 +0000 | [diff] [blame] | 780 | (void)BIO_dgram_set_peer(wbio, tmpclient); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 781 | } |
Richard Levitte | d858c87 | 2016-02-03 00:27:44 +0100 | [diff] [blame] | 782 | BIO_ADDR_free(tmpclient); |
| 783 | tmpclient = NULL; |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 784 | |
Matt Caswell | 8b0e934 | 2016-10-06 19:17:54 +0100 | [diff] [blame] | 785 | /* TODO(size_t): convert this call */ |
Matt Caswell | c536b6b | 2016-09-21 11:26:47 +0100 | [diff] [blame] | 786 | if (BIO_write(wbio, buf, wreclen) < (int)wreclen) { |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 787 | if (BIO_should_retry(wbio)) { |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 788 | /* |
| 789 | * Non-blocking IO...but we're stateless, so we're just |
| 790 | * going to drop this packet. |
| 791 | */ |
| 792 | goto end; |
| 793 | } |
| 794 | return -1; |
| 795 | } |
| 796 | |
| 797 | if (BIO_flush(wbio) <= 0) { |
FdaSilvaYY | e8aa8b6 | 2016-06-29 00:18:50 +0200 | [diff] [blame] | 798 | if (BIO_should_retry(wbio)) { |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 799 | /* |
| 800 | * Non-blocking IO...but we're stateless, so we're just |
| 801 | * going to drop this packet. |
| 802 | */ |
| 803 | goto end; |
| 804 | } |
| 805 | return -1; |
| 806 | } |
| 807 | } |
| 808 | } while (next != LISTEN_SUCCESS); |
| 809 | |
| 810 | /* |
| 811 | * Set expected sequence numbers to continue the handshake. |
| 812 | */ |
| 813 | s->d1->handshake_read_seq = 1; |
| 814 | s->d1->handshake_write_seq = 1; |
| 815 | s->d1->next_handshake_write_seq = 1; |
| 816 | DTLS_RECORD_LAYER_set_write_sequence(&s->rlayer, seq); |
| 817 | |
| 818 | /* |
| 819 | * We are doing cookie exchange, so make sure we set that option in the |
| 820 | * SSL object |
| 821 | */ |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 822 | SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE); |
Dr. Stephen Henson | 1fc3ac8 | 2009-09-09 17:05:18 +0000 | [diff] [blame] | 823 | |
Matt Caswell | 31fd10e | 2015-10-22 12:18:45 +0100 | [diff] [blame] | 824 | /* |
| 825 | * Tell the state machine that we've done the initial hello verify |
| 826 | * exchange |
| 827 | */ |
| 828 | ossl_statem_set_hello_verify_done(s); |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 829 | |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 830 | /* |
| 831 | * Some BIOs may not support this. If we fail we clear the client address |
| 832 | */ |
Matt Caswell | ce0865d | 2016-01-21 12:22:58 +0000 | [diff] [blame] | 833 | if (BIO_dgram_get_peer(rbio, client) <= 0) |
| 834 | BIO_ADDR_clear(client); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 835 | |
| 836 | ret = 1; |
| 837 | clearpkt = 0; |
Emilia Kasper | a230b26 | 2016-08-05 19:03:17 +0200 | [diff] [blame] | 838 | end: |
Richard Levitte | d858c87 | 2016-02-03 00:27:44 +0100 | [diff] [blame] | 839 | BIO_ADDR_free(tmpclient); |
Matt Caswell | e3d0dae | 2015-09-14 22:49:35 +0100 | [diff] [blame] | 840 | BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 0, NULL); |
| 841 | if (clearpkt) { |
| 842 | /* Dump this packet. Ignore return value */ |
| 843 | BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH); |
| 844 | } |
| 845 | return ret; |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 846 | } |
Matt Caswell | f9e5503 | 2016-03-21 15:32:40 +0000 | [diff] [blame] | 847 | #endif |
Dr. Stephen Henson | 173e72e | 2013-03-11 15:34:28 +0000 | [diff] [blame] | 848 | |
Dr. Stephen Henson | 173e72e | 2013-03-11 15:34:28 +0000 | [diff] [blame] | 849 | static int dtls1_handshake_write(SSL *s) |
Matt Caswell | 0f113f3 | 2015-01-22 03:40:55 +0000 | [diff] [blame] | 850 | { |
| 851 | return dtls1_do_write(s, SSL3_RT_HANDSHAKE); |
| 852 | } |
Matt Caswell | 8ba708e | 2015-09-11 10:48:59 +0100 | [diff] [blame] | 853 | |
Matt Caswell | 8ba708e | 2015-09-11 10:48:59 +0100 | [diff] [blame] | 854 | int dtls1_shutdown(SSL *s) |
| 855 | { |
| 856 | int ret; |
| 857 | #ifndef OPENSSL_NO_SCTP |
| 858 | BIO *wbio; |
| 859 | |
| 860 | wbio = SSL_get_wbio(s); |
| 861 | if (wbio != NULL && BIO_dgram_is_sctp(wbio) && |
| 862 | !(s->shutdown & SSL_SENT_SHUTDOWN)) { |
| 863 | ret = BIO_dgram_sctp_wait_for_dry(wbio); |
| 864 | if (ret < 0) |
| 865 | return -1; |
| 866 | |
| 867 | if (ret == 0) |
| 868 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN, 1, |
| 869 | NULL); |
| 870 | } |
| 871 | #endif |
| 872 | ret = ssl3_shutdown(s); |
| 873 | #ifndef OPENSSL_NO_SCTP |
| 874 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN, 0, NULL); |
| 875 | #endif |
| 876 | return ret; |
| 877 | } |
| 878 | |
| 879 | int dtls1_query_mtu(SSL *s) |
| 880 | { |
| 881 | if (s->d1->link_mtu) { |
| 882 | s->d1->mtu = |
| 883 | s->d1->link_mtu - BIO_dgram_get_mtu_overhead(SSL_get_wbio(s)); |
| 884 | s->d1->link_mtu = 0; |
| 885 | } |
| 886 | |
| 887 | /* AHA! Figure out the MTU, and stick to the right size */ |
| 888 | if (s->d1->mtu < dtls1_min_mtu(s)) { |
| 889 | if (!(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) { |
| 890 | s->d1->mtu = |
| 891 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL); |
| 892 | |
| 893 | /* |
| 894 | * I've seen the kernel return bogus numbers when it doesn't know |
| 895 | * (initial write), so just make sure we have a reasonable number |
| 896 | */ |
| 897 | if (s->d1->mtu < dtls1_min_mtu(s)) { |
| 898 | /* Set to min mtu */ |
| 899 | s->d1->mtu = dtls1_min_mtu(s); |
| 900 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU, |
Matt Caswell | 8b0e934 | 2016-10-06 19:17:54 +0100 | [diff] [blame] | 901 | (long)s->d1->mtu, NULL); |
Matt Caswell | 8ba708e | 2015-09-11 10:48:59 +0100 | [diff] [blame] | 902 | } |
| 903 | } else |
| 904 | return 0; |
| 905 | } |
| 906 | return 1; |
| 907 | } |
| 908 | |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 909 | static size_t dtls1_link_min_mtu(void) |
Matt Caswell | 8ba708e | 2015-09-11 10:48:59 +0100 | [diff] [blame] | 910 | { |
| 911 | return (g_probable_mtu[(sizeof(g_probable_mtu) / |
| 912 | sizeof(g_probable_mtu[0])) - 1]); |
| 913 | } |
| 914 | |
Matt Caswell | 7ee8627 | 2016-09-07 11:34:39 +0100 | [diff] [blame] | 915 | size_t dtls1_min_mtu(SSL *s) |
Matt Caswell | 8ba708e | 2015-09-11 10:48:59 +0100 | [diff] [blame] | 916 | { |
| 917 | return dtls1_link_min_mtu() - BIO_dgram_get_mtu_overhead(SSL_get_wbio(s)); |
| 918 | } |
David Woodhouse | 045bd04 | 2016-10-06 00:44:59 +0100 | [diff] [blame] | 919 | |
| 920 | size_t DTLS_get_data_mtu(const SSL *s) |
| 921 | { |
| 922 | size_t mac_overhead, int_overhead, blocksize, ext_overhead; |
| 923 | const SSL_CIPHER *ciph = SSL_get_current_cipher(s); |
| 924 | size_t mtu = s->d1->mtu; |
| 925 | |
| 926 | if (ciph == NULL) |
| 927 | return 0; |
| 928 | |
| 929 | if (!ssl_cipher_get_overhead(ciph, &mac_overhead, &int_overhead, |
| 930 | &blocksize, &ext_overhead)) |
| 931 | return 0; |
| 932 | |
Matt Caswell | 28a31a0 | 2017-02-03 14:06:20 +0000 | [diff] [blame] | 933 | if (SSL_READ_ETM(s)) |
David Woodhouse | 045bd04 | 2016-10-06 00:44:59 +0100 | [diff] [blame] | 934 | ext_overhead += mac_overhead; |
| 935 | else |
| 936 | int_overhead += mac_overhead; |
| 937 | |
| 938 | /* Subtract external overhead (e.g. IV/nonce, separate MAC) */ |
| 939 | if (ext_overhead + DTLS1_RT_HEADER_LENGTH >= mtu) |
| 940 | return 0; |
| 941 | mtu -= ext_overhead + DTLS1_RT_HEADER_LENGTH; |
| 942 | |
| 943 | /* Round encrypted payload down to cipher block size (for CBC etc.) |
| 944 | * No check for overflow since 'mtu % blocksize' cannot exceed mtu. */ |
| 945 | if (blocksize) |
| 946 | mtu -= (mtu % blocksize); |
| 947 | |
| 948 | /* Subtract internal overhead (e.g. CBC padding len byte) */ |
| 949 | if (int_overhead >= mtu) |
| 950 | return 0; |
| 951 | mtu -= int_overhead; |
| 952 | |
| 953 | return mtu; |
| 954 | } |