Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). */ |
| 108 | |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 109 | #include <assert.h> |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 110 | #include <limits.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 111 | #include <stdio.h> |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 112 | #include <stdlib.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 113 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 114 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 115 | #include <openssl/bytestring.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 116 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 117 | #include <openssl/evp.h> |
| 118 | #include <openssl/hmac.h> |
| 119 | #include <openssl/mem.h> |
| 120 | #include <openssl/obj.h> |
| 121 | #include <openssl/rand.h> |
| 122 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 123 | #include "internal.h" |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 124 | |
| 125 | |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 126 | static int ssl_check_clienthello_tlsext(SSL *s); |
| 127 | static int ssl_check_serverhello_tlsext(SSL *s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 128 | |
David Benjamin | 338fcaf | 2014-12-11 01:20:52 -0500 | [diff] [blame] | 129 | const SSL3_ENC_METHOD TLSv1_enc_data = { |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 130 | tls1_prf, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 131 | tls1_setup_key_block, |
| 132 | tls1_generate_master_secret, |
| 133 | tls1_change_cipher_state, |
| 134 | tls1_final_finish_mac, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 135 | tls1_cert_verify_mac, |
| 136 | TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 137 | TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 138 | tls1_alert_code, |
| 139 | tls1_export_keying_material, |
| 140 | 0, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 141 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 142 | |
David Benjamin | 338fcaf | 2014-12-11 01:20:52 -0500 | [diff] [blame] | 143 | const SSL3_ENC_METHOD TLSv1_1_enc_data = { |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 144 | tls1_prf, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 145 | tls1_setup_key_block, |
| 146 | tls1_generate_master_secret, |
| 147 | tls1_change_cipher_state, |
| 148 | tls1_final_finish_mac, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 149 | tls1_cert_verify_mac, |
| 150 | TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 151 | TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 152 | tls1_alert_code, |
| 153 | tls1_export_keying_material, |
| 154 | SSL_ENC_FLAG_EXPLICIT_IV, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 155 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 156 | |
David Benjamin | 338fcaf | 2014-12-11 01:20:52 -0500 | [diff] [blame] | 157 | const SSL3_ENC_METHOD TLSv1_2_enc_data = { |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 158 | tls1_prf, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 159 | tls1_setup_key_block, |
| 160 | tls1_generate_master_secret, |
| 161 | tls1_change_cipher_state, |
| 162 | tls1_final_finish_mac, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 163 | tls1_cert_verify_mac, |
| 164 | TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 165 | TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 166 | tls1_alert_code, |
| 167 | tls1_export_keying_material, |
| 168 | SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF |
| 169 | |SSL_ENC_FLAG_TLS1_2_CIPHERS, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 170 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 171 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 172 | static int compare_uint16_t(const void *p1, const void *p2) { |
| 173 | uint16_t u1 = *((const uint16_t *)p1); |
| 174 | uint16_t u2 = *((const uint16_t *)p2); |
| 175 | if (u1 < u2) { |
| 176 | return -1; |
| 177 | } else if (u1 > u2) { |
| 178 | return 1; |
| 179 | } else { |
| 180 | return 0; |
| 181 | } |
| 182 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 183 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 184 | /* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be |
| 185 | * more than one extension of the same type in a ClientHello or ServerHello. |
| 186 | * This function does an initial scan over the extensions block to filter those |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 187 | * out. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 188 | static int tls1_check_duplicate_extensions(const CBS *cbs) { |
| 189 | CBS extensions = *cbs; |
| 190 | size_t num_extensions = 0, i = 0; |
| 191 | uint16_t *extension_types = NULL; |
| 192 | int ret = 0; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 193 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 194 | /* First pass: count the extensions. */ |
| 195 | while (CBS_len(&extensions) > 0) { |
| 196 | uint16_t type; |
| 197 | CBS extension; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 198 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 199 | if (!CBS_get_u16(&extensions, &type) || |
| 200 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 201 | goto done; |
| 202 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 203 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 204 | num_extensions++; |
| 205 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 206 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 207 | if (num_extensions == 0) { |
| 208 | return 1; |
| 209 | } |
David Benjamin | 9a37359 | 2014-07-25 04:27:53 -0400 | [diff] [blame] | 210 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 211 | extension_types = |
| 212 | (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions); |
| 213 | if (extension_types == NULL) { |
| 214 | OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions, |
| 215 | ERR_R_MALLOC_FAILURE); |
| 216 | goto done; |
| 217 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 218 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 219 | /* Second pass: gather the extension types. */ |
| 220 | extensions = *cbs; |
| 221 | for (i = 0; i < num_extensions; i++) { |
| 222 | CBS extension; |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 223 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 224 | if (!CBS_get_u16(&extensions, &extension_types[i]) || |
| 225 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 226 | /* This should not happen. */ |
| 227 | goto done; |
| 228 | } |
| 229 | } |
| 230 | assert(CBS_len(&extensions) == 0); |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 231 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 232 | /* Sort the extensions and make sure there are no duplicates. */ |
| 233 | qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t); |
| 234 | for (i = 1; i < num_extensions; i++) { |
| 235 | if (extension_types[i - 1] == extension_types[i]) { |
| 236 | goto done; |
| 237 | } |
| 238 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 239 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 240 | ret = 1; |
| 241 | |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 242 | done: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 243 | OPENSSL_free(extension_types); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 244 | return ret; |
| 245 | } |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 246 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 247 | char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) { |
| 248 | CBS client_hello, session_id, cipher_suites, compression_methods, extensions; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 249 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 250 | CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 251 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 252 | if (/* Skip client version. */ |
| 253 | !CBS_skip(&client_hello, 2) || |
| 254 | /* Skip client nonce. */ |
| 255 | !CBS_skip(&client_hello, 32) || |
| 256 | /* Extract session_id. */ |
| 257 | !CBS_get_u8_length_prefixed(&client_hello, &session_id)) { |
| 258 | return 0; |
| 259 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 260 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 261 | ctx->session_id = CBS_data(&session_id); |
| 262 | ctx->session_id_len = CBS_len(&session_id); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 263 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 264 | /* Skip past DTLS cookie */ |
| 265 | if (SSL_IS_DTLS(ctx->ssl)) { |
| 266 | CBS cookie; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 267 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 268 | if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) { |
| 269 | return 0; |
| 270 | } |
| 271 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 272 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 273 | /* Extract cipher_suites. */ |
| 274 | if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) || |
| 275 | CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) { |
| 276 | return 0; |
| 277 | } |
| 278 | ctx->cipher_suites = CBS_data(&cipher_suites); |
| 279 | ctx->cipher_suites_len = CBS_len(&cipher_suites); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 280 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 281 | /* Extract compression_methods. */ |
| 282 | if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) || |
| 283 | CBS_len(&compression_methods) < 1) { |
| 284 | return 0; |
| 285 | } |
| 286 | ctx->compression_methods = CBS_data(&compression_methods); |
| 287 | ctx->compression_methods_len = CBS_len(&compression_methods); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 288 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 289 | /* If the ClientHello ends here then it's valid, but doesn't have any |
| 290 | * extensions. (E.g. SSLv3.) */ |
| 291 | if (CBS_len(&client_hello) == 0) { |
| 292 | ctx->extensions = NULL; |
| 293 | ctx->extensions_len = 0; |
| 294 | return 1; |
| 295 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 296 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 297 | /* Extract extensions and check it is valid. */ |
| 298 | if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) || |
| 299 | !tls1_check_duplicate_extensions(&extensions) || |
| 300 | CBS_len(&client_hello) != 0) { |
| 301 | return 0; |
| 302 | } |
| 303 | ctx->extensions = CBS_data(&extensions); |
| 304 | ctx->extensions_len = CBS_len(&extensions); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 305 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 306 | return 1; |
| 307 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 308 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 309 | char SSL_early_callback_ctx_extension_get( |
| 310 | const struct ssl_early_callback_ctx *ctx, uint16_t extension_type, |
| 311 | const uint8_t **out_data, size_t *out_len) { |
| 312 | CBS extensions; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 313 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 314 | CBS_init(&extensions, ctx->extensions, ctx->extensions_len); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 315 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 316 | while (CBS_len(&extensions) != 0) { |
| 317 | uint16_t type; |
| 318 | CBS extension; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 319 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 320 | /* Decode the next extension. */ |
| 321 | if (!CBS_get_u16(&extensions, &type) || |
| 322 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 323 | return 0; |
| 324 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 325 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 326 | if (type == extension_type) { |
| 327 | *out_data = CBS_data(&extension); |
| 328 | *out_len = CBS_len(&extension); |
| 329 | return 1; |
| 330 | } |
| 331 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 332 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 333 | return 0; |
| 334 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 335 | |
David Benjamin | 52e5bac | 2014-12-27 02:27:35 -0500 | [diff] [blame] | 336 | struct tls_curve { |
| 337 | uint16_t curve_id; |
| 338 | int nid; |
| 339 | }; |
| 340 | |
David Benjamin | 70bd80a | 2014-12-27 03:06:46 -0500 | [diff] [blame] | 341 | /* ECC curves from RFC4492. */ |
David Benjamin | 52e5bac | 2014-12-27 02:27:35 -0500 | [diff] [blame] | 342 | static const struct tls_curve tls_curves[] = { |
| 343 | {21, NID_secp224r1}, |
| 344 | {23, NID_X9_62_prime256v1}, |
| 345 | {24, NID_secp384r1}, |
| 346 | {25, NID_secp521r1}, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 347 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 348 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 349 | static const uint8_t ecformats_default[] = { |
| 350 | TLSEXT_ECPOINTFORMAT_uncompressed, |
| 351 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 352 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 353 | static const uint16_t eccurves_default[] = { |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 354 | 23, /* X9_62_prime256v1 */ |
David Benjamin | 52e5bac | 2014-12-27 02:27:35 -0500 | [diff] [blame] | 355 | 24, /* secp384r1 */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 356 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 357 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 358 | int tls1_ec_curve_id2nid(uint16_t curve_id) { |
David Benjamin | 52e5bac | 2014-12-27 02:27:35 -0500 | [diff] [blame] | 359 | size_t i; |
| 360 | for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) { |
| 361 | if (curve_id == tls_curves[i].curve_id) { |
| 362 | return tls_curves[i].nid; |
| 363 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 364 | } |
David Benjamin | 52e5bac | 2014-12-27 02:27:35 -0500 | [diff] [blame] | 365 | return NID_undef; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 366 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 367 | |
David Benjamin | 70bd80a | 2014-12-27 03:06:46 -0500 | [diff] [blame] | 368 | int tls1_ec_nid2curve_id(uint16_t *out_curve_id, int nid) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 369 | size_t i; |
David Benjamin | 52e5bac | 2014-12-27 02:27:35 -0500 | [diff] [blame] | 370 | for (i = 0; i < sizeof(tls_curves) / sizeof(tls_curves[0]); i++) { |
| 371 | if (nid == tls_curves[i].nid) { |
David Benjamin | 70bd80a | 2014-12-27 03:06:46 -0500 | [diff] [blame] | 372 | *out_curve_id = tls_curves[i].curve_id; |
| 373 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 374 | } |
| 375 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | /* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the |
| 380 | * list of allowed curve IDs. If |get_peer_curves| is non-zero, return the |
| 381 | * peer's curve list. Otherwise, return the preferred list. */ |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 382 | static void tls1_get_curvelist(SSL *s, int get_peer_curves, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 383 | const uint16_t **out_curve_ids, |
| 384 | size_t *out_curve_ids_len) { |
| 385 | if (get_peer_curves) { |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 386 | /* Only clients send a curve list, so this function is only called |
| 387 | * on the server. */ |
| 388 | assert(s->server); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 389 | *out_curve_ids = s->s3->tmp.peer_ellipticcurvelist; |
| 390 | *out_curve_ids_len = s->s3->tmp.peer_ellipticcurvelist_length; |
| 391 | return; |
| 392 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 393 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 394 | *out_curve_ids = s->tlsext_ellipticcurvelist; |
| 395 | *out_curve_ids_len = s->tlsext_ellipticcurvelist_length; |
| 396 | if (!*out_curve_ids) { |
| 397 | *out_curve_ids = eccurves_default; |
| 398 | *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]); |
| 399 | } |
| 400 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 401 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 402 | int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) { |
| 403 | uint8_t curve_type; |
| 404 | uint16_t curve_id; |
| 405 | const uint16_t *curves; |
| 406 | size_t curves_len, i; |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 407 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 408 | /* Only support named curves. */ |
| 409 | if (!CBS_get_u8(cbs, &curve_type) || |
| 410 | curve_type != NAMED_CURVE_TYPE || |
| 411 | !CBS_get_u16(cbs, &curve_id)) { |
| 412 | return 0; |
| 413 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 414 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 415 | tls1_get_curvelist(s, 0, &curves, &curves_len); |
| 416 | for (i = 0; i < curves_len; i++) { |
| 417 | if (curve_id == curves[i]) { |
| 418 | *out_curve_id = curve_id; |
| 419 | return 1; |
| 420 | } |
| 421 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 422 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 423 | return 0; |
| 424 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 425 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 426 | int tls1_get_shared_curve(SSL *s) { |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 427 | const uint16_t *curves, *peer_curves, *pref, *supp; |
| 428 | size_t curves_len, peer_curves_len, pref_len, supp_len, i, j; |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 429 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 430 | /* Can't do anything on client side */ |
| 431 | if (s->server == 0) { |
| 432 | return NID_undef; |
| 433 | } |
| 434 | |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 435 | tls1_get_curvelist(s, 0 /* local curves */, &curves, &curves_len); |
| 436 | tls1_get_curvelist(s, 1 /* peer curves */, &peer_curves, &peer_curves_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 437 | |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 438 | if (peer_curves_len == 0) { |
| 439 | /* Clients are not required to send a supported_curves extension. In this |
| 440 | * case, the server is free to pick any curve it likes. See RFC 4492, |
| 441 | * section 4, paragraph 3. */ |
| 442 | return (curves_len == 0) ? NID_undef : tls1_ec_curve_id2nid(curves[0]); |
| 443 | } |
| 444 | |
| 445 | if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
| 446 | pref = curves; |
| 447 | pref_len = curves_len; |
| 448 | supp = peer_curves; |
| 449 | supp_len = peer_curves_len; |
| 450 | } else { |
| 451 | pref = peer_curves; |
| 452 | pref_len = peer_curves_len; |
| 453 | supp = curves; |
| 454 | supp_len = curves_len; |
| 455 | } |
| 456 | |
| 457 | for (i = 0; i < pref_len; i++) { |
| 458 | for (j = 0; j < supp_len; j++) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 459 | if (pref[i] == supp[j]) { |
| 460 | return tls1_ec_curve_id2nid(pref[i]); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | return NID_undef; |
| 466 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 467 | |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 468 | int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 469 | const int *curves, size_t ncurves) { |
| 470 | uint16_t *curve_ids; |
| 471 | size_t i; |
| 472 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 473 | curve_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t)); |
| 474 | if (curve_ids == NULL) { |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | for (i = 0; i < ncurves; i++) { |
David Benjamin | 70bd80a | 2014-12-27 03:06:46 -0500 | [diff] [blame] | 479 | if (!tls1_ec_nid2curve_id(&curve_ids[i], curves[i])) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 480 | OPENSSL_free(curve_ids); |
| 481 | return 0; |
| 482 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 483 | } |
| 484 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 485 | OPENSSL_free(*out_curve_ids); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 486 | *out_curve_ids = curve_ids; |
| 487 | *out_curve_ids_len = ncurves; |
| 488 | |
| 489 | return 1; |
| 490 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 491 | |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 492 | /* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the |
| 493 | * TLS curve ID and point format, respectively, for |ec|. It returns one on |
| 494 | * success and zero on failure. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 495 | static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id, |
| 496 | uint8_t *out_comp_id, EC_KEY *ec) { |
| 497 | int nid; |
| 498 | uint16_t id; |
| 499 | const EC_GROUP *grp; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 500 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 501 | if (ec == NULL) { |
| 502 | return 0; |
| 503 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 504 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 505 | grp = EC_KEY_get0_group(ec); |
| 506 | if (grp == NULL) { |
| 507 | return 0; |
| 508 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 509 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 510 | /* Determine curve ID */ |
| 511 | nid = EC_GROUP_get_curve_name(grp); |
David Benjamin | 70bd80a | 2014-12-27 03:06:46 -0500 | [diff] [blame] | 512 | if (!tls1_ec_nid2curve_id(&id, nid)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 513 | return 0; |
| 514 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 515 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 516 | /* Set the named curve ID. Arbitrary explicit curves are not supported. */ |
| 517 | *out_curve_id = id; |
| 518 | |
| 519 | if (out_comp_id) { |
| 520 | if (EC_KEY_get0_public_key(ec) == NULL) { |
| 521 | return 0; |
| 522 | } |
| 523 | if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) { |
| 524 | *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
| 525 | } else { |
| 526 | *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | return 1; |
| 531 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 532 | |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 533 | /* tls1_check_point_format returns one if |comp_id| is consistent with the |
| 534 | * peer's point format preferences. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 535 | static int tls1_check_point_format(SSL *s, uint8_t comp_id) { |
| 536 | uint8_t *p = s->s3->tmp.peer_ecpointformatlist; |
| 537 | size_t plen = s->s3->tmp.peer_ecpointformatlist_length; |
| 538 | size_t i; |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 539 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 540 | /* If point formats extension present check it, otherwise everything is |
| 541 | * supported (see RFC4492). */ |
| 542 | if (p == NULL) { |
| 543 | return 1; |
| 544 | } |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 545 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 546 | for (i = 0; i < plen; i++) { |
| 547 | if (comp_id == p[i]) { |
| 548 | return 1; |
| 549 | } |
| 550 | } |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 551 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | /* tls1_check_curve_id returns one if |curve_id| is consistent with both our |
| 556 | * and the peer's curve preferences. Note: if called as the client, only our |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 557 | * preferences are checked; the peer (the server) does not send preferences. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 558 | static int tls1_check_curve_id(SSL *s, uint16_t curve_id) { |
| 559 | const uint16_t *curves; |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 560 | size_t curves_len, i, get_peer_curves; |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 561 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 562 | /* Check against our list, then the peer's list. */ |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 563 | for (get_peer_curves = 0; get_peer_curves <= 1; get_peer_curves++) { |
| 564 | if (get_peer_curves && !s->server) { |
| 565 | /* Servers do not present a preference list so, if we are a client, only |
| 566 | * check our list. */ |
| 567 | continue; |
| 568 | } |
| 569 | |
| 570 | tls1_get_curvelist(s, get_peer_curves, &curves, &curves_len); |
| 571 | if (get_peer_curves && curves_len == 0) { |
| 572 | /* Clients are not required to send a supported_curves extension. In this |
| 573 | * case, the server is free to pick any curve it likes. See RFC 4492, |
| 574 | * section 4, paragraph 3. */ |
| 575 | continue; |
| 576 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 577 | for (i = 0; i < curves_len; i++) { |
| 578 | if (curves[i] == curve_id) { |
| 579 | break; |
| 580 | } |
| 581 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 582 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 583 | if (i == curves_len) { |
| 584 | return 0; |
| 585 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 586 | } |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 587 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 588 | return 1; |
| 589 | } |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 590 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 591 | static void tls1_get_formatlist(SSL *s, const uint8_t **pformats, |
| 592 | size_t *pformatslen) { |
| 593 | /* If we have a custom point format list use it otherwise use default */ |
| 594 | if (s->tlsext_ecpointformatlist) { |
| 595 | *pformats = s->tlsext_ecpointformatlist; |
| 596 | *pformatslen = s->tlsext_ecpointformatlist_length; |
| 597 | } else { |
| 598 | *pformats = ecformats_default; |
| 599 | *pformatslen = sizeof(ecformats_default); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | int tls1_check_ec_cert(SSL *s, X509 *x) { |
| 604 | int ret = 0; |
| 605 | EVP_PKEY *pkey = X509_get_pubkey(x); |
| 606 | uint16_t curve_id; |
| 607 | uint8_t comp_id; |
| 608 | |
| 609 | if (!pkey || |
| 610 | pkey->type != EVP_PKEY_EC || |
| 611 | !tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec) || |
| 612 | !tls1_check_curve_id(s, curve_id) || |
| 613 | !tls1_check_point_format(s, comp_id)) { |
| 614 | goto done; |
| 615 | } |
| 616 | |
| 617 | ret = 1; |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 618 | |
| 619 | done: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 620 | EVP_PKEY_free(pkey); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 621 | return ret; |
| 622 | } |
David Benjamin | 42e9a77 | 2014-09-02 23:18:44 -0400 | [diff] [blame] | 623 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 624 | int tls1_check_ec_tmp_key(SSL *s) { |
David Benjamin | c0f763b | 2015-03-27 02:05:39 -0400 | [diff] [blame] | 625 | if (s->cert->ecdh_nid != NID_undef) { |
David Benjamin | dd97878 | 2015-04-24 15:20:13 -0400 | [diff] [blame] | 626 | /* If the curve is preconfigured, ECDH is acceptable iff the peer supports |
David Benjamin | c0f763b | 2015-03-27 02:05:39 -0400 | [diff] [blame] | 627 | * the curve. */ |
| 628 | uint16_t curve_id; |
| 629 | return tls1_ec_nid2curve_id(&curve_id, s->cert->ecdh_nid) && |
| 630 | tls1_check_curve_id(s, curve_id); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 631 | } |
| 632 | |
David Benjamin | dd97878 | 2015-04-24 15:20:13 -0400 | [diff] [blame] | 633 | if (s->cert->ecdh_tmp_cb != NULL) { |
| 634 | /* Assume the callback will provide an acceptable curve. */ |
| 635 | return 1; |
| 636 | } |
| 637 | |
| 638 | /* Otherwise, the curve gets selected automatically. ECDH is acceptable iff |
| 639 | * there is a shared curve. */ |
| 640 | return tls1_get_shared_curve(s) != NID_undef; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 641 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 642 | |
| 643 | /* List of supported signature algorithms and hashes. Should make this |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 644 | * customisable at some point, for now include everything we support. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 645 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 646 | #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 647 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 648 | #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 649 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 650 | #define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 651 | |
David Benjamin | cff6472 | 2014-08-19 19:54:46 -0400 | [diff] [blame] | 652 | static const uint8_t tls12_sigalgs[] = { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 653 | tlsext_sigalg(TLSEXT_hash_sha512) |
| 654 | tlsext_sigalg(TLSEXT_hash_sha384) |
| 655 | tlsext_sigalg(TLSEXT_hash_sha256) |
| 656 | tlsext_sigalg(TLSEXT_hash_sha224) |
| 657 | tlsext_sigalg(TLSEXT_hash_sha1) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 658 | }; |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 659 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 660 | size_t tls12_get_psigalgs(SSL *s, const uint8_t **psigs) { |
| 661 | /* If server use client authentication sigalgs if not NULL */ |
| 662 | if (s->server && s->cert->client_sigalgs) { |
| 663 | *psigs = s->cert->client_sigalgs; |
| 664 | return s->cert->client_sigalgslen; |
| 665 | } else if (s->cert->conf_sigalgs) { |
| 666 | *psigs = s->cert->conf_sigalgs; |
| 667 | return s->cert->conf_sigalgslen; |
| 668 | } else { |
| 669 | *psigs = tls12_sigalgs; |
| 670 | return sizeof(tls12_sigalgs); |
| 671 | } |
| 672 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 673 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 674 | /* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |cbs|. It |
| 675 | * checks it is consistent with |s|'s sent supported signature algorithms and, |
| 676 | * if so, writes the relevant digest into |*out_md| and returns 1. Otherwise it |
| 677 | * returns 0 and writes an alert into |*out_alert|. */ |
| 678 | int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, SSL *s, |
| 679 | CBS *cbs, EVP_PKEY *pkey) { |
| 680 | const uint8_t *sent_sigs; |
| 681 | size_t sent_sigslen, i; |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 682 | int sigalg = tls12_get_sigid(pkey->type); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 683 | uint8_t hash, signature; |
| 684 | |
| 685 | /* Should never happen */ |
| 686 | if (sigalg == -1) { |
| 687 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR); |
| 688 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | if (!CBS_get_u8(cbs, &hash) || |
| 693 | !CBS_get_u8(cbs, &signature)) { |
| 694 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR); |
| 695 | *out_alert = SSL_AD_DECODE_ERROR; |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /* Check key type is consistent with signature */ |
| 700 | if (sigalg != signature) { |
| 701 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE); |
| 702 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | if (pkey->type == EVP_PKEY_EC) { |
| 707 | uint16_t curve_id; |
| 708 | uint8_t comp_id; |
| 709 | /* Check compression and curve matches extensions */ |
| 710 | if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) { |
| 711 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | if (s->server && (!tls1_check_curve_id(s, curve_id) || |
| 716 | !tls1_check_point_format(s, comp_id))) { |
| 717 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE); |
| 718 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 719 | return 0; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | /* Check signature matches a type we sent */ |
| 724 | sent_sigslen = tls12_get_psigalgs(s, &sent_sigs); |
| 725 | for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) { |
| 726 | if (hash == sent_sigs[0] && signature == sent_sigs[1]) { |
| 727 | break; |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | /* Allow fallback to SHA-1. */ |
| 732 | if (i == sent_sigslen && hash != TLSEXT_hash_sha1) { |
| 733 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE); |
| 734 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | *out_md = tls12_get_hash(hash); |
| 739 | if (*out_md == NULL) { |
| 740 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST); |
| 741 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | return 1; |
| 746 | } |
| 747 | |
| 748 | /* Get a mask of disabled algorithms: an algorithm is disabled if it isn't |
| 749 | * supported or doesn't appear in supported signature algorithms. Unlike |
| 750 | * ssl_cipher_get_disabled this applies to a specific session and not global |
| 751 | * settings. */ |
| 752 | void ssl_set_client_disabled(SSL *s) { |
| 753 | CERT *c = s->cert; |
| 754 | const uint8_t *sigalgs; |
| 755 | size_t i, sigalgslen; |
| 756 | int have_rsa = 0, have_ecdsa = 0; |
| 757 | c->mask_a = 0; |
| 758 | c->mask_k = 0; |
| 759 | |
| 760 | /* Don't allow TLS 1.2 only ciphers if we don't suppport them */ |
| 761 | if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) { |
| 762 | c->mask_ssl = SSL_TLSV1_2; |
| 763 | } else { |
| 764 | c->mask_ssl = 0; |
| 765 | } |
| 766 | |
| 767 | /* Now go through all signature algorithms seeing if we support any for RSA, |
| 768 | * DSA, ECDSA. Do this for all versions not just TLS 1.2. */ |
| 769 | sigalgslen = tls12_get_psigalgs(s, &sigalgs); |
| 770 | for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) { |
| 771 | switch (sigalgs[1]) { |
| 772 | case TLSEXT_signature_rsa: |
| 773 | have_rsa = 1; |
| 774 | break; |
| 775 | |
| 776 | case TLSEXT_signature_ecdsa: |
| 777 | have_ecdsa = 1; |
| 778 | break; |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | /* Disable auth if we don't include any appropriate signature algorithms. */ |
| 783 | if (!have_rsa) { |
| 784 | c->mask_a |= SSL_aRSA; |
| 785 | } |
| 786 | if (!have_ecdsa) { |
| 787 | c->mask_a |= SSL_aECDSA; |
| 788 | } |
| 789 | |
| 790 | /* with PSK there must be client callback set */ |
| 791 | if (!s->psk_client_callback) { |
| 792 | c->mask_a |= SSL_aPSK; |
| 793 | c->mask_k |= SSL_kPSK; |
| 794 | } |
| 795 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 796 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 797 | /* tls_extension represents a TLS extension that is handled internally. The |
| 798 | * |init| function is called for each handshake, before any other functions of |
| 799 | * the extension. Then the add and parse callbacks are called as needed. |
| 800 | * |
| 801 | * The parse callbacks receive a |CBS| that contains the contents of the |
| 802 | * extension (i.e. not including the type and length bytes). If an extension is |
| 803 | * not received then the parse callbacks will be called with a NULL CBS so that |
| 804 | * they can do any processing needed to handle the absence of an extension. |
| 805 | * |
| 806 | * The add callbacks receive a |CBB| to which the extension can be appended but |
| 807 | * the function is responsible for appending the type and length bytes too. |
| 808 | * |
| 809 | * All callbacks return one for success and zero for error. If a parse function |
| 810 | * returns zero then a fatal alert with value |*out_alert| will be sent. If |
| 811 | * |*out_alert| isn't set, then a |decode_error| alert will be sent. */ |
| 812 | struct tls_extension { |
| 813 | uint16_t value; |
| 814 | void (*init)(SSL *ssl); |
| 815 | |
| 816 | int (*add_clienthello)(SSL *ssl, CBB *out); |
| 817 | int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents); |
| 818 | |
| 819 | int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents); |
| 820 | int (*add_serverhello)(SSL *ssl, CBB *out); |
| 821 | }; |
| 822 | |
| 823 | |
| 824 | /* Server name indication (SNI). |
| 825 | * |
| 826 | * https://tools.ietf.org/html/rfc6066#section-3. */ |
| 827 | |
| 828 | static void ext_sni_init(SSL *ssl) { |
| 829 | ssl->s3->tmp.should_ack_sni = 0; |
| 830 | } |
| 831 | |
| 832 | static int ext_sni_add_clienthello(SSL *ssl, CBB *out) { |
| 833 | if (ssl->tlsext_hostname == NULL) { |
| 834 | return 1; |
| 835 | } |
| 836 | |
| 837 | CBB contents, server_name_list, name; |
| 838 | if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 839 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 840 | !CBB_add_u16_length_prefixed(&contents, &server_name_list) || |
| 841 | !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) || |
| 842 | !CBB_add_u16_length_prefixed(&server_name_list, &name) || |
| 843 | !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname, |
| 844 | strlen(ssl->tlsext_hostname)) || |
| 845 | !CBB_flush(out)) { |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | return 1; |
| 850 | } |
| 851 | |
| 852 | static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) { |
| 853 | if (contents == NULL) { |
| 854 | return 1; |
| 855 | } |
| 856 | |
| 857 | if (CBS_len(contents) != 0) { |
| 858 | return 0; |
| 859 | } |
| 860 | |
| 861 | assert(ssl->tlsext_hostname != NULL); |
| 862 | |
| 863 | if (!ssl->hit) { |
| 864 | assert(ssl->session->tlsext_hostname == NULL); |
| 865 | ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname); |
| 866 | if (!ssl->session->tlsext_hostname) { |
| 867 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 868 | return 0; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | return 1; |
| 873 | } |
| 874 | |
| 875 | static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) { |
| 876 | if (contents == NULL) { |
| 877 | return 1; |
| 878 | } |
| 879 | |
| 880 | /* The servername extension is treated as follows: |
| 881 | * |
| 882 | * - Only the hostname type is supported with a maximum length of 255. |
| 883 | * - The servername is rejected if too long or if it contains zeros, in |
| 884 | * which case an fatal alert is generated. |
| 885 | * - The servername field is maintained together with the session cache. |
| 886 | * - When a session is resumed, the servername callback is invoked in order |
| 887 | * to allow the application to position itself to the right context. |
| 888 | * - The servername is acknowledged if it is new for a session or when |
| 889 | * it is identical to a previously used for the same session. |
| 890 | * Applications can control the behaviour. They can at any time |
| 891 | * set a 'desirable' servername for a new SSL object. This can be the |
| 892 | * case for example with HTTPS when a Host: header field is received and |
| 893 | * a renegotiation is requested. In this case, a possible servername |
| 894 | * presented in the new client hello is only acknowledged if it matches |
| 895 | * the value of the Host: field. |
| 896 | * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
| 897 | * if they provide for changing an explicit servername context for the |
| 898 | * session, |
| 899 | * i.e. when the session has been established with a servername extension. |
| 900 | */ |
| 901 | |
| 902 | CBS server_name_list; |
| 903 | char have_seen_host_name = 0; |
| 904 | |
| 905 | if (!CBS_get_u16_length_prefixed(contents, &server_name_list) || |
| 906 | CBS_len(&server_name_list) == 0 || |
| 907 | CBS_len(contents) != 0) { |
| 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | /* Decode each ServerName in the extension. */ |
| 912 | while (CBS_len(&server_name_list) > 0) { |
| 913 | uint8_t name_type; |
| 914 | CBS host_name; |
| 915 | |
| 916 | if (!CBS_get_u8(&server_name_list, &name_type) || |
| 917 | !CBS_get_u16_length_prefixed(&server_name_list, &host_name)) { |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | /* Only host_name is supported. */ |
| 922 | if (name_type != TLSEXT_NAMETYPE_host_name) { |
| 923 | continue; |
| 924 | } |
| 925 | |
| 926 | if (have_seen_host_name) { |
| 927 | /* The ServerNameList MUST NOT contain more than one name of the same |
| 928 | * name_type. */ |
| 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | have_seen_host_name = 1; |
| 933 | |
| 934 | if (CBS_len(&host_name) == 0 || |
| 935 | CBS_len(&host_name) > TLSEXT_MAXLEN_host_name || |
| 936 | CBS_contains_zero_byte(&host_name)) { |
| 937 | *out_alert = SSL_AD_UNRECOGNIZED_NAME; |
| 938 | return 0; |
| 939 | } |
| 940 | |
| 941 | if (!ssl->hit) { |
| 942 | assert(ssl->session->tlsext_hostname == NULL); |
| 943 | if (ssl->session->tlsext_hostname) { |
| 944 | /* This should be impossible. */ |
| 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | /* Copy the hostname as a string. */ |
| 949 | if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) { |
| 950 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 951 | return 0; |
| 952 | } |
| 953 | |
| 954 | ssl->s3->tmp.should_ack_sni = 1; |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | return 1; |
| 959 | } |
| 960 | |
| 961 | static int ext_sni_add_serverhello(SSL *ssl, CBB *out) { |
| 962 | if (ssl->hit || |
| 963 | !ssl->s3->tmp.should_ack_sni || |
| 964 | ssl->session->tlsext_hostname == NULL) { |
| 965 | return 1; |
| 966 | } |
| 967 | |
| 968 | if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 969 | !CBB_add_u16(out, 0 /* length */)) { |
| 970 | return 0; |
| 971 | } |
| 972 | |
| 973 | return 1; |
| 974 | } |
| 975 | |
| 976 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 977 | /* Renegotiation indication. |
| 978 | * |
| 979 | * https://tools.ietf.org/html/rfc5746 */ |
| 980 | |
| 981 | static int ext_ri_add_clienthello(SSL *ssl, CBB *out) { |
| 982 | CBB contents, prev_finished; |
| 983 | if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
| 984 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 985 | !CBB_add_u8_length_prefixed(&contents, &prev_finished) || |
| 986 | !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished, |
| 987 | ssl->s3->previous_client_finished_len) || |
| 988 | !CBB_flush(out)) { |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | return 1; |
| 993 | } |
| 994 | |
| 995 | static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 996 | CBS *contents) { |
| 997 | if (contents == NULL) { |
| 998 | /* No renegotiation extension received. |
| 999 | * |
| 1000 | * Strictly speaking if we want to avoid an attack we should *always* see |
| 1001 | * RI even on initial ServerHello because the client doesn't see any |
| 1002 | * renegotiation during an attack. However this would mean we could not |
| 1003 | * connect to any server which doesn't support RI. |
| 1004 | * |
| 1005 | * A lack of the extension is allowed if SSL_OP_LEGACY_SERVER_CONNECT is |
| 1006 | * defined. */ |
| 1007 | if (ssl->options & SSL_OP_LEGACY_SERVER_CONNECT) { |
| 1008 | return 1; |
| 1009 | } |
| 1010 | |
| 1011 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 1012 | OPENSSL_PUT_ERROR(SSL, ext_ri_parse_serverhello, |
| 1013 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); |
| 1014 | return 0; |
| 1015 | } |
| 1016 | |
| 1017 | const size_t expected_len = ssl->s3->previous_client_finished_len + |
| 1018 | ssl->s3->previous_server_finished_len; |
| 1019 | |
| 1020 | /* Check for logic errors */ |
| 1021 | assert(!expected_len || ssl->s3->previous_client_finished_len); |
| 1022 | assert(!expected_len || ssl->s3->previous_server_finished_len); |
| 1023 | |
| 1024 | /* Parse out the extension contents. */ |
| 1025 | CBS renegotiated_connection; |
| 1026 | if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 1027 | CBS_len(contents) != 0) { |
| 1028 | OPENSSL_PUT_ERROR(SSL, ext_ri_parse_serverhello, |
| 1029 | SSL_R_RENEGOTIATION_ENCODING_ERR); |
| 1030 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
| 1034 | /* Check that the extension matches. */ |
| 1035 | if (CBS_len(&renegotiated_connection) != expected_len) { |
| 1036 | OPENSSL_PUT_ERROR(SSL, ext_ri_parse_serverhello, |
| 1037 | SSL_R_RENEGOTIATION_MISMATCH); |
| 1038 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | const uint8_t *d = CBS_data(&renegotiated_connection); |
| 1043 | if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished, |
| 1044 | ssl->s3->previous_client_finished_len)) { |
| 1045 | OPENSSL_PUT_ERROR(SSL, ext_ri_parse_serverhello, |
| 1046 | SSL_R_RENEGOTIATION_MISMATCH); |
| 1047 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 1048 | return 0; |
| 1049 | } |
| 1050 | d += ssl->s3->previous_client_finished_len; |
| 1051 | |
| 1052 | if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished, |
| 1053 | ssl->s3->previous_server_finished_len)) { |
| 1054 | OPENSSL_PUT_ERROR(SSL, ext_ri_parse_serverhello, |
| 1055 | SSL_R_RENEGOTIATION_MISMATCH); |
| 1056 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1057 | return 0; |
| 1058 | } |
| 1059 | ssl->s3->send_connection_binding = 1; |
| 1060 | |
| 1061 | return 1; |
| 1062 | } |
| 1063 | |
| 1064 | static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1065 | CBS *contents) { |
| 1066 | /* Renegotiation isn't supported as a server so this function should never be |
| 1067 | * called after the initial handshake. */ |
| 1068 | assert(!ssl->s3->initial_handshake_complete); |
| 1069 | |
| 1070 | CBS fake_contents; |
| 1071 | static const uint8_t kFakeExtension[] = {0}; |
| 1072 | |
| 1073 | if (contents == NULL) { |
| 1074 | if (ssl->s3->send_connection_binding) { |
| 1075 | /* The renegotiation SCSV was received so pretend that we received a |
| 1076 | * renegotiation extension. */ |
| 1077 | CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension)); |
| 1078 | contents = &fake_contents; |
| 1079 | /* We require that the renegotiation extension is at index zero of |
| 1080 | * kExtensions. */ |
| 1081 | ssl->s3->tmp.extensions.received |= (1u << 0); |
| 1082 | } else { |
| 1083 | return 1; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | CBS renegotiated_connection; |
| 1088 | |
| 1089 | if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 1090 | CBS_len(contents) != 0) { |
| 1091 | OPENSSL_PUT_ERROR(SSL, ext_ri_parse_clienthello, |
| 1092 | SSL_R_RENEGOTIATION_ENCODING_ERR); |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | /* Check that the extension matches */ |
| 1097 | if (!CBS_mem_equal(&renegotiated_connection, ssl->s3->previous_client_finished, |
| 1098 | ssl->s3->previous_client_finished_len)) { |
| 1099 | OPENSSL_PUT_ERROR(SSL, ext_ri_parse_clienthello, |
| 1100 | SSL_R_RENEGOTIATION_MISMATCH); |
| 1101 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | ssl->s3->send_connection_binding = 1; |
| 1106 | |
| 1107 | return 1; |
| 1108 | } |
| 1109 | |
| 1110 | static int ext_ri_add_serverhello(SSL *ssl, CBB *out) { |
| 1111 | CBB contents, prev_finished; |
| 1112 | if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
| 1113 | !CBB_add_u16_length_prefixed(out, &contents) || |
| 1114 | !CBB_add_u8_length_prefixed(&contents, &prev_finished) || |
| 1115 | !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished, |
| 1116 | ssl->s3->previous_client_finished_len) || |
| 1117 | !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished, |
| 1118 | ssl->s3->previous_server_finished_len) || |
| 1119 | !CBB_flush(out)) { |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
| 1123 | return 1; |
| 1124 | } |
| 1125 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1126 | /* kExtensions contains all the supported extensions. */ |
| 1127 | static const struct tls_extension kExtensions[] = { |
| 1128 | { |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 1129 | /* The renegotiation extension must always be at index zero because the |
| 1130 | * |received| and |sent| bitsets need to be tweaked when the "extension" is |
| 1131 | * sent as an SCSV. */ |
| 1132 | TLSEXT_TYPE_renegotiate, |
| 1133 | NULL, |
| 1134 | ext_ri_add_clienthello, |
| 1135 | ext_ri_parse_serverhello, |
| 1136 | ext_ri_parse_clienthello, |
| 1137 | ext_ri_add_serverhello, |
| 1138 | }, |
| 1139 | { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1140 | TLSEXT_TYPE_server_name, |
| 1141 | ext_sni_init, |
| 1142 | ext_sni_add_clienthello, |
| 1143 | ext_sni_parse_serverhello, |
| 1144 | ext_sni_parse_clienthello, |
| 1145 | ext_sni_add_serverhello, |
| 1146 | }, |
| 1147 | }; |
| 1148 | |
| 1149 | #define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension)) |
| 1150 | |
Adam Langley | 4cfa96b | 2015-07-01 11:56:55 -0700 | [diff] [blame] | 1151 | OPENSSL_COMPILE_ASSERT(kNumExtensions <= |
| 1152 | sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8, |
| 1153 | too_many_extensions_for_bitset); |
| 1154 | OPENSSL_COMPILE_ASSERT(kNumExtensions <= |
| 1155 | sizeof(((SSL *)NULL)->s3->tmp.extensions.received) * |
| 1156 | 8, |
| 1157 | too_many_extensions_for_bitset); |
| 1158 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1159 | static const struct tls_extension *tls_extension_find(uint32_t *out_index, |
| 1160 | uint16_t value) { |
| 1161 | unsigned i; |
| 1162 | for (i = 0; i < kNumExtensions; i++) { |
| 1163 | if (kExtensions[i].value == value) { |
| 1164 | *out_index = i; |
| 1165 | return &kExtensions[i]; |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | return NULL; |
| 1170 | } |
| 1171 | |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1172 | /* header_len is the length of the ClientHello header written so far, used to |
| 1173 | * compute padding. It does not include the record header. Pass 0 if no padding |
| 1174 | * is to be done. */ |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1175 | uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *const buf, |
| 1176 | uint8_t *const limit, size_t header_len) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1177 | int extdatalen = 0; |
| 1178 | uint8_t *ret = buf; |
| 1179 | uint8_t *orig = buf; |
| 1180 | /* See if we support any ECC ciphersuites */ |
| 1181 | int using_ecc = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1182 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1183 | if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) { |
| 1184 | size_t i; |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 1185 | uint32_t alg_k, alg_a; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1186 | STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1187 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1188 | for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) { |
| 1189 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1190 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1191 | alg_k = c->algorithm_mkey; |
| 1192 | alg_a = c->algorithm_auth; |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1193 | if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1194 | using_ecc = 1; |
| 1195 | break; |
| 1196 | } |
| 1197 | } |
| 1198 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1199 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1200 | /* don't add extensions for SSLv3 unless doing secure renegotiation */ |
| 1201 | if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) { |
| 1202 | return orig; |
| 1203 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1204 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1205 | ret += 2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1206 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1207 | if (ret >= limit) { |
| 1208 | return NULL; /* should never occur. */ |
| 1209 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1210 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1211 | s->s3->tmp.extensions.sent = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1212 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1213 | size_t i; |
| 1214 | for (i = 0; i < kNumExtensions; i++) { |
| 1215 | if (kExtensions[i].init != NULL) { |
| 1216 | kExtensions[i].init(s); |
| 1217 | } |
| 1218 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1219 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1220 | CBB cbb; |
| 1221 | if (!CBB_init_fixed(&cbb, ret, limit - ret)) { |
| 1222 | OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1223 | return NULL; |
| 1224 | } |
| 1225 | |
| 1226 | for (i = 0; i < kNumExtensions; i++) { |
| 1227 | const size_t space_before = CBB_len(&cbb); |
| 1228 | if (!kExtensions[i].add_clienthello(s, &cbb)) { |
| 1229 | CBB_cleanup(&cbb); |
| 1230 | OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1231 | return NULL; |
| 1232 | } |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1233 | const size_t space_after = CBB_len(&cbb); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1234 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1235 | if (space_after != space_before) { |
| 1236 | s->s3->tmp.extensions.sent |= (1u << i); |
| 1237 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1238 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1239 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1240 | ret = limit - CBB_len(&cbb); |
| 1241 | CBB_cleanup(&cbb); |
| 1242 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1243 | /* Add extended master secret. */ |
| 1244 | if (s->version != SSL3_VERSION) { |
| 1245 | if (limit - ret - 4 < 0) { |
| 1246 | return NULL; |
| 1247 | } |
| 1248 | s2n(TLSEXT_TYPE_extended_master_secret, ret); |
| 1249 | s2n(0, ret); |
| 1250 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 1251 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1252 | if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) { |
| 1253 | int ticklen = 0; |
David Benjamin | 4b27d9f | 2015-05-12 22:42:52 -0400 | [diff] [blame] | 1254 | /* Renegotiation does not participate in session resumption. However, still |
| 1255 | * advertise the extension to avoid potentially breaking servers which carry |
| 1256 | * over the state from the previous handshake, such as OpenSSL servers |
| 1257 | * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */ |
| 1258 | if (!s->s3->initial_handshake_complete && s->session != NULL && |
| 1259 | s->session->tlsext_tick != NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1260 | ticklen = s->session->tlsext_ticklen; |
| 1261 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1262 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1263 | /* Check for enough room 2 for extension type, 2 for len rest for |
| 1264 | * ticket. */ |
| 1265 | if ((long)(limit - ret - 4 - ticklen) < 0) { |
| 1266 | return NULL; |
| 1267 | } |
| 1268 | s2n(TLSEXT_TYPE_session_ticket, ret); |
| 1269 | s2n(ticklen, ret); |
| 1270 | if (ticklen) { |
| 1271 | memcpy(ret, s->session->tlsext_tick, ticklen); |
| 1272 | ret += ticklen; |
| 1273 | } |
| 1274 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1275 | |
David Benjamin | 6ae7f07 | 2015-01-26 10:22:13 -0500 | [diff] [blame] | 1276 | if (ssl3_version_from_wire(s, s->client_version) >= TLS1_2_VERSION) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1277 | size_t salglen; |
| 1278 | const uint8_t *salg; |
| 1279 | salglen = tls12_get_psigalgs(s, &salg); |
| 1280 | if ((size_t)(limit - ret) < salglen + 6) { |
| 1281 | return NULL; |
| 1282 | } |
| 1283 | s2n(TLSEXT_TYPE_signature_algorithms, ret); |
| 1284 | s2n(salglen + 2, ret); |
| 1285 | s2n(salglen, ret); |
| 1286 | memcpy(ret, salg, salglen); |
| 1287 | ret += salglen; |
| 1288 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1289 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1290 | if (s->ocsp_stapling_enabled) { |
| 1291 | /* The status_request extension is excessively extensible at every layer. |
| 1292 | * On the client, only support requesting OCSP responses with an empty |
| 1293 | * responder_id_list and no extensions. */ |
| 1294 | if (limit - ret - 4 - 1 - 2 - 2 < 0) { |
| 1295 | return NULL; |
| 1296 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1297 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1298 | s2n(TLSEXT_TYPE_status_request, ret); |
| 1299 | s2n(1 + 2 + 2, ret); |
| 1300 | /* status_type */ |
| 1301 | *(ret++) = TLSEXT_STATUSTYPE_ocsp; |
| 1302 | /* responder_id_list - empty */ |
| 1303 | s2n(0, ret); |
| 1304 | /* request_extensions - empty */ |
| 1305 | s2n(0, ret); |
| 1306 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1307 | |
David Benjamin | e6df054 | 2015-05-12 22:02:08 -0400 | [diff] [blame] | 1308 | if (s->ctx->next_proto_select_cb && !s->s3->initial_handshake_complete && |
David Benjamin | 78e6978 | 2014-12-19 04:52:17 -0500 | [diff] [blame] | 1309 | !SSL_IS_DTLS(s)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1310 | /* The client advertises an emtpy extension to indicate its support for |
| 1311 | * Next Protocol Negotiation */ |
| 1312 | if (limit - ret - 4 < 0) { |
| 1313 | return NULL; |
| 1314 | } |
| 1315 | s2n(TLSEXT_TYPE_next_proto_neg, ret); |
| 1316 | s2n(0, ret); |
| 1317 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1318 | |
David Benjamin | e6df054 | 2015-05-12 22:02:08 -0400 | [diff] [blame] | 1319 | if (s->signed_cert_timestamps_enabled) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1320 | /* The client advertises an empty extension to indicate its support for |
| 1321 | * certificate timestamps. */ |
| 1322 | if (limit - ret - 4 < 0) { |
| 1323 | return NULL; |
| 1324 | } |
| 1325 | s2n(TLSEXT_TYPE_certificate_timestamp, ret); |
| 1326 | s2n(0, ret); |
| 1327 | } |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1328 | |
David Benjamin | e6df054 | 2015-05-12 22:02:08 -0400 | [diff] [blame] | 1329 | if (s->alpn_client_proto_list && !s->s3->initial_handshake_complete) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1330 | if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) { |
| 1331 | return NULL; |
| 1332 | } |
| 1333 | s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret); |
| 1334 | s2n(2 + s->alpn_client_proto_list_len, ret); |
| 1335 | s2n(s->alpn_client_proto_list_len, ret); |
| 1336 | memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len); |
| 1337 | ret += s->alpn_client_proto_list_len; |
| 1338 | } |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1339 | |
David Benjamin | 78e6978 | 2014-12-19 04:52:17 -0500 | [diff] [blame] | 1340 | if (s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1341 | /* The client advertises an emtpy extension to indicate its support for |
| 1342 | * Channel ID. */ |
| 1343 | if (limit - ret - 4 < 0) { |
| 1344 | return NULL; |
| 1345 | } |
| 1346 | if (s->ctx->tlsext_channel_id_enabled_new) { |
| 1347 | s2n(TLSEXT_TYPE_channel_id_new, ret); |
| 1348 | } else { |
| 1349 | s2n(TLSEXT_TYPE_channel_id, ret); |
| 1350 | } |
| 1351 | s2n(0, ret); |
| 1352 | } |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1353 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1354 | if (SSL_get_srtp_profiles(s)) { |
| 1355 | int el; |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1356 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1357 | ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0); |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1358 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1359 | if ((limit - ret - 4 - el) < 0) { |
| 1360 | return NULL; |
| 1361 | } |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1362 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1363 | s2n(TLSEXT_TYPE_use_srtp, ret); |
| 1364 | s2n(el, ret); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1365 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1366 | if (!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) { |
| 1367 | OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1368 | return NULL; |
| 1369 | } |
| 1370 | ret += el; |
| 1371 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1372 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1373 | if (using_ecc) { |
| 1374 | /* Add TLS extension ECPointFormats to the ClientHello message */ |
| 1375 | long lenmax; |
| 1376 | const uint8_t *formats; |
| 1377 | const uint16_t *curves; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1378 | size_t formats_len, curves_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1379 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1380 | tls1_get_formatlist(s, &formats, &formats_len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1381 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1382 | lenmax = limit - ret - 5; |
| 1383 | if (lenmax < 0) { |
| 1384 | return NULL; |
| 1385 | } |
| 1386 | if (formats_len > (size_t)lenmax) { |
| 1387 | return NULL; |
| 1388 | } |
| 1389 | if (formats_len > 255) { |
| 1390 | OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1391 | return NULL; |
| 1392 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1393 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1394 | s2n(TLSEXT_TYPE_ec_point_formats, ret); |
| 1395 | s2n(formats_len + 1, ret); |
| 1396 | *(ret++) = (uint8_t)formats_len; |
| 1397 | memcpy(ret, formats, formats_len); |
| 1398 | ret += formats_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1399 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1400 | /* Add TLS extension EllipticCurves to the ClientHello message */ |
| 1401 | tls1_get_curvelist(s, 0, &curves, &curves_len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1402 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1403 | lenmax = limit - ret - 6; |
| 1404 | if (lenmax < 0) { |
| 1405 | return NULL; |
| 1406 | } |
| 1407 | if (curves_len * 2 > (size_t)lenmax) { |
| 1408 | return NULL; |
| 1409 | } |
| 1410 | if (curves_len * 2 > 65532) { |
| 1411 | OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1412 | return NULL; |
| 1413 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1414 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1415 | s2n(TLSEXT_TYPE_elliptic_curves, ret); |
| 1416 | s2n((curves_len * 2) + 2, ret); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1417 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1418 | s2n(curves_len * 2, ret); |
| 1419 | for (i = 0; i < curves_len; i++) { |
| 1420 | s2n(curves[i], ret); |
| 1421 | } |
| 1422 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1423 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1424 | if (header_len > 0) { |
| 1425 | size_t clienthello_minsize = 0; |
| 1426 | header_len += ret - orig; |
| 1427 | if (header_len > 0xff && header_len < 0x200) { |
| 1428 | /* Add padding to workaround bugs in F5 terminators. See |
| 1429 | * https://tools.ietf.org/html/draft-agl-tls-padding-03 |
| 1430 | * |
| 1431 | * NB: because this code works out the length of all existing extensions |
| 1432 | * it MUST always appear last. */ |
| 1433 | clienthello_minsize = 0x200; |
| 1434 | } |
| 1435 | if (s->fastradio_padding) { |
| 1436 | /* Pad the ClientHello record to 1024 bytes to fast forward the radio |
| 1437 | * into DCH (high data rate) state in 3G networks. Note that when |
| 1438 | * fastradio_padding is enabled, even if the header_len is less than 255 |
| 1439 | * bytes, the padding will be applied regardless. This is slightly |
| 1440 | * different from the TLS padding extension suggested in |
| 1441 | * https://tools.ietf.org/html/draft-agl-tls-padding-03 */ |
| 1442 | clienthello_minsize = 0x400; |
| 1443 | } |
| 1444 | if (header_len < clienthello_minsize) { |
| 1445 | size_t padding_len = clienthello_minsize - header_len; |
| 1446 | /* Extensions take at least four bytes to encode. Always include least |
| 1447 | * one byte of data if including the extension. WebSphere Application |
| 1448 | * Server 7.0 is intolerant to the last extension being zero-length. */ |
| 1449 | if (padding_len >= 4 + 1) { |
| 1450 | padding_len -= 4; |
| 1451 | } else { |
| 1452 | padding_len = 1; |
| 1453 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1454 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1455 | if (limit - ret - 4 - (long)padding_len < 0) { |
| 1456 | return NULL; |
| 1457 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1458 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1459 | s2n(TLSEXT_TYPE_padding, ret); |
| 1460 | s2n(padding_len, ret); |
| 1461 | memset(ret, 0, padding_len); |
| 1462 | ret += padding_len; |
| 1463 | } |
| 1464 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1465 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1466 | extdatalen = ret - orig - 2; |
| 1467 | if (extdatalen == 0) { |
| 1468 | return orig; |
| 1469 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1470 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1471 | s2n(extdatalen, orig); |
| 1472 | return ret; |
| 1473 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1474 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1475 | uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *const buf, |
| 1476 | uint8_t *const limit) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1477 | int extdatalen = 0; |
| 1478 | uint8_t *orig = buf; |
| 1479 | uint8_t *ret = buf; |
| 1480 | int next_proto_neg_seen; |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 1481 | uint32_t alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 1482 | uint32_t alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1483 | int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1484 | using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1485 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1486 | /* don't add extensions for SSLv3, unless doing secure renegotiation */ |
| 1487 | if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) { |
| 1488 | return orig; |
| 1489 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1490 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1491 | ret += 2; |
| 1492 | if (ret >= limit) { |
| 1493 | return NULL; /* should never happen. */ |
| 1494 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1495 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1496 | CBB cbb; |
| 1497 | if (!CBB_init_fixed(&cbb, ret, limit - ret)) { |
| 1498 | OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1499 | return NULL; |
| 1500 | } |
| 1501 | |
| 1502 | unsigned i; |
| 1503 | for (i = 0; i < kNumExtensions; i++) { |
| 1504 | if (!(s->s3->tmp.extensions.received & (1u << i))) { |
| 1505 | /* Don't send extensions that were not received. */ |
| 1506 | continue; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1507 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1508 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1509 | if (!kExtensions[i].add_serverhello(s, &cbb)) { |
| 1510 | CBB_cleanup(&cbb); |
| 1511 | OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1512 | return NULL; |
| 1513 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1514 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1515 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1516 | ret = limit - CBB_len(&cbb); |
| 1517 | CBB_cleanup(&cbb); |
| 1518 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1519 | if (s->s3->tmp.extended_master_secret) { |
| 1520 | if ((long)(limit - ret - 4) < 0) { |
| 1521 | return NULL; |
| 1522 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1523 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1524 | s2n(TLSEXT_TYPE_extended_master_secret, ret); |
| 1525 | s2n(0, ret); |
| 1526 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1527 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1528 | if (using_ecc) { |
| 1529 | const uint8_t *plist; |
| 1530 | size_t plistlen; |
| 1531 | /* Add TLS extension ECPointFormats to the ServerHello message */ |
| 1532 | long lenmax; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1533 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1534 | tls1_get_formatlist(s, &plist, &plistlen); |
| 1535 | |
| 1536 | lenmax = limit - ret - 5; |
| 1537 | if (lenmax < 0) { |
| 1538 | return NULL; |
| 1539 | } |
| 1540 | if (plistlen > (size_t)lenmax) { |
| 1541 | return NULL; |
| 1542 | } |
| 1543 | if (plistlen > 255) { |
| 1544 | OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1545 | return NULL; |
| 1546 | } |
| 1547 | |
| 1548 | s2n(TLSEXT_TYPE_ec_point_formats, ret); |
| 1549 | s2n(plistlen + 1, ret); |
| 1550 | *(ret++) = (uint8_t)plistlen; |
| 1551 | memcpy(ret, plist, plistlen); |
| 1552 | ret += plistlen; |
| 1553 | } |
| 1554 | /* Currently the server should not respond with a SupportedCurves extension */ |
| 1555 | |
| 1556 | if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) { |
| 1557 | if ((long)(limit - ret - 4) < 0) { |
| 1558 | return NULL; |
| 1559 | } |
| 1560 | s2n(TLSEXT_TYPE_session_ticket, ret); |
| 1561 | s2n(0, ret); |
| 1562 | } |
| 1563 | |
| 1564 | if (s->s3->tmp.certificate_status_expected) { |
| 1565 | if ((long)(limit - ret - 4) < 0) { |
| 1566 | return NULL; |
| 1567 | } |
| 1568 | s2n(TLSEXT_TYPE_status_request, ret); |
| 1569 | s2n(0, ret); |
| 1570 | } |
| 1571 | |
| 1572 | if (s->srtp_profile) { |
| 1573 | int el; |
| 1574 | |
| 1575 | ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0); |
| 1576 | |
| 1577 | if ((limit - ret - 4 - el) < 0) { |
| 1578 | return NULL; |
| 1579 | } |
| 1580 | |
| 1581 | s2n(TLSEXT_TYPE_use_srtp, ret); |
| 1582 | s2n(el, ret); |
| 1583 | |
| 1584 | if (!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) { |
| 1585 | OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1586 | return NULL; |
| 1587 | } |
| 1588 | ret += el; |
| 1589 | } |
| 1590 | |
| 1591 | next_proto_neg_seen = s->s3->next_proto_neg_seen; |
| 1592 | s->s3->next_proto_neg_seen = 0; |
| 1593 | if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) { |
| 1594 | const uint8_t *npa; |
| 1595 | unsigned int npalen; |
| 1596 | int r; |
| 1597 | |
| 1598 | r = s->ctx->next_protos_advertised_cb( |
| 1599 | s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg); |
| 1600 | if (r == SSL_TLSEXT_ERR_OK) { |
| 1601 | if ((long)(limit - ret - 4 - npalen) < 0) { |
| 1602 | return NULL; |
| 1603 | } |
| 1604 | s2n(TLSEXT_TYPE_next_proto_neg, ret); |
| 1605 | s2n(npalen, ret); |
| 1606 | memcpy(ret, npa, npalen); |
| 1607 | ret += npalen; |
| 1608 | s->s3->next_proto_neg_seen = 1; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | if (s->s3->alpn_selected) { |
| 1613 | const uint8_t *selected = s->s3->alpn_selected; |
| 1614 | size_t len = s->s3->alpn_selected_len; |
| 1615 | |
| 1616 | if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) { |
| 1617 | return NULL; |
| 1618 | } |
| 1619 | s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret); |
| 1620 | s2n(3 + len, ret); |
| 1621 | s2n(1 + len, ret); |
| 1622 | *ret++ = len; |
| 1623 | memcpy(ret, selected, len); |
| 1624 | ret += len; |
| 1625 | } |
| 1626 | |
| 1627 | /* If the client advertised support for Channel ID, and we have it |
| 1628 | * enabled, then we want to echo it back. */ |
| 1629 | if (s->s3->tlsext_channel_id_valid) { |
| 1630 | if (limit - ret - 4 < 0) { |
| 1631 | return NULL; |
| 1632 | } |
| 1633 | if (s->s3->tlsext_channel_id_new) { |
| 1634 | s2n(TLSEXT_TYPE_channel_id_new, ret); |
| 1635 | } else { |
| 1636 | s2n(TLSEXT_TYPE_channel_id, ret); |
| 1637 | } |
| 1638 | s2n(0, ret); |
| 1639 | } |
| 1640 | |
| 1641 | extdatalen = ret - orig - 2; |
| 1642 | if (extdatalen == 0) { |
| 1643 | return orig; |
| 1644 | } |
| 1645 | |
| 1646 | s2n(extdatalen, orig); |
| 1647 | return ret; |
| 1648 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1649 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1650 | /* tls1_alpn_handle_client_hello is called to process the ALPN extension in a |
| 1651 | * ClientHello. |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1652 | * cbs: the contents of the extension, not including the type and length. |
| 1653 | * out_alert: a pointer to the alert value to send in the event of a zero |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1654 | * return. |
| 1655 | * |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1656 | * returns: 1 on success. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1657 | static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert) { |
| 1658 | CBS protocol_name_list, protocol_name_list_copy; |
| 1659 | const uint8_t *selected; |
| 1660 | uint8_t selected_len; |
| 1661 | int r; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1662 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1663 | if (s->ctx->alpn_select_cb == NULL) { |
| 1664 | return 1; |
| 1665 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1666 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1667 | if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) || |
| 1668 | CBS_len(cbs) != 0 || CBS_len(&protocol_name_list) < 2) { |
| 1669 | goto parse_error; |
| 1670 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1671 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1672 | /* Validate the protocol list. */ |
| 1673 | protocol_name_list_copy = protocol_name_list; |
| 1674 | while (CBS_len(&protocol_name_list_copy) > 0) { |
| 1675 | CBS protocol_name; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1676 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1677 | if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name)) { |
| 1678 | goto parse_error; |
| 1679 | } |
| 1680 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1681 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1682 | r = s->ctx->alpn_select_cb( |
| 1683 | s, &selected, &selected_len, CBS_data(&protocol_name_list), |
| 1684 | CBS_len(&protocol_name_list), s->ctx->alpn_select_cb_arg); |
| 1685 | if (r == SSL_TLSEXT_ERR_OK) { |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1686 | OPENSSL_free(s->s3->alpn_selected); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1687 | s->s3->alpn_selected = BUF_memdup(selected, selected_len); |
| 1688 | if (!s->s3->alpn_selected) { |
| 1689 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1690 | return 0; |
| 1691 | } |
| 1692 | s->s3->alpn_selected_len = selected_len; |
| 1693 | } |
| 1694 | |
| 1695 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1696 | |
| 1697 | parse_error: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1698 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1699 | return 0; |
| 1700 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1701 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1702 | static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1703 | CBS extensions; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1704 | |
David Benjamin | d1d7d3d | 2015-01-11 20:30:21 -0500 | [diff] [blame] | 1705 | s->srtp_profile = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1706 | s->s3->next_proto_neg_seen = 0; |
| 1707 | s->s3->tmp.certificate_status_expected = 0; |
| 1708 | s->s3->tmp.extended_master_secret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1709 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1710 | OPENSSL_free(s->s3->alpn_selected); |
| 1711 | s->s3->alpn_selected = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1712 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1713 | /* Clear any signature algorithms extension received */ |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1714 | OPENSSL_free(s->cert->peer_sigalgs); |
| 1715 | s->cert->peer_sigalgs = NULL; |
| 1716 | s->cert->peer_sigalgslen = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1717 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1718 | /* Clear any shared signature algorithms */ |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1719 | OPENSSL_free(s->cert->shared_sigalgs); |
| 1720 | s->cert->shared_sigalgs = NULL; |
| 1721 | s->cert->shared_sigalgslen = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1722 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1723 | /* Clear ECC extensions */ |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1724 | OPENSSL_free(s->s3->tmp.peer_ecpointformatlist); |
| 1725 | s->s3->tmp.peer_ecpointformatlist = NULL; |
| 1726 | s->s3->tmp.peer_ecpointformatlist_length = 0; |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1727 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1728 | OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist); |
| 1729 | s->s3->tmp.peer_ellipticcurvelist = NULL; |
| 1730 | s->s3->tmp.peer_ellipticcurvelist_length = 0; |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1731 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1732 | size_t i; |
| 1733 | for (i = 0; i < kNumExtensions; i++) { |
| 1734 | if (kExtensions[i].init != NULL) { |
| 1735 | kExtensions[i].init(s); |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | s->s3->tmp.extensions.received = 0; |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 1740 | /* The renegotiation extension must always be at index zero because the |
| 1741 | * |received| and |sent| bitsets need to be tweaked when the "extension" is |
| 1742 | * sent as an SCSV. */ |
| 1743 | assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1744 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1745 | /* There may be no extensions. */ |
| 1746 | if (CBS_len(cbs) == 0) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1747 | goto no_extensions; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1748 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1749 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1750 | /* Decode the extensions block and check it is valid. */ |
| 1751 | if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 1752 | !tls1_check_duplicate_extensions(&extensions)) { |
| 1753 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1754 | return 0; |
| 1755 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1756 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1757 | while (CBS_len(&extensions) != 0) { |
| 1758 | uint16_t type; |
| 1759 | CBS extension; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1760 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1761 | /* Decode the next extension. */ |
| 1762 | if (!CBS_get_u16(&extensions, &type) || |
| 1763 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 1764 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1765 | return 0; |
| 1766 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1767 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1768 | unsigned ext_index; |
| 1769 | const struct tls_extension *const ext = |
| 1770 | tls_extension_find(&ext_index, type); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1771 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1772 | if (ext != NULL) { |
| 1773 | s->s3->tmp.extensions.received |= (1u << ext_index); |
| 1774 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 1775 | if (!ext->parse_clienthello(s, &alert, &extension)) { |
| 1776 | *out_alert = alert; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1777 | return 0; |
| 1778 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1779 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1780 | continue; |
| 1781 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1782 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1783 | if (type == TLSEXT_TYPE_ec_point_formats) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1784 | CBS ec_point_format_list; |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1785 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1786 | if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) || |
| 1787 | CBS_len(&extension) != 0) { |
| 1788 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1789 | return 0; |
| 1790 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1791 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1792 | if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist, |
| 1793 | &s->s3->tmp.peer_ecpointformatlist_length)) { |
| 1794 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1795 | return 0; |
| 1796 | } |
| 1797 | } else if (type == TLSEXT_TYPE_elliptic_curves) { |
| 1798 | CBS elliptic_curve_list; |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1799 | size_t num_curves; |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1800 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1801 | if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) || |
| 1802 | CBS_len(&elliptic_curve_list) == 0 || |
| 1803 | (CBS_len(&elliptic_curve_list) & 1) != 0 || |
| 1804 | CBS_len(&extension) != 0) { |
| 1805 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1806 | return 0; |
| 1807 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1808 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1809 | OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist); |
| 1810 | s->s3->tmp.peer_ellipticcurvelist_length = 0; |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1811 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1812 | s->s3->tmp.peer_ellipticcurvelist = |
| 1813 | (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list)); |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1814 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1815 | if (s->s3->tmp.peer_ellipticcurvelist == NULL) { |
| 1816 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1817 | return 0; |
| 1818 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1819 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1820 | num_curves = CBS_len(&elliptic_curve_list) / 2; |
| 1821 | for (i = 0; i < num_curves; i++) { |
| 1822 | if (!CBS_get_u16(&elliptic_curve_list, |
| 1823 | &s->s3->tmp.peer_ellipticcurvelist[i])) { |
| 1824 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1825 | return 0; |
| 1826 | } |
| 1827 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1828 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1829 | if (CBS_len(&elliptic_curve_list) != 0) { |
| 1830 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1831 | return 0; |
| 1832 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1833 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1834 | s->s3->tmp.peer_ellipticcurvelist_length = num_curves; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1835 | } else if (type == TLSEXT_TYPE_signature_algorithms) { |
| 1836 | CBS supported_signature_algorithms; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1837 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1838 | if (!CBS_get_u16_length_prefixed(&extension, |
| 1839 | &supported_signature_algorithms) || |
| 1840 | CBS_len(&extension) != 0) { |
| 1841 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1842 | return 0; |
| 1843 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1844 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1845 | /* Ensure the signature algorithms are non-empty. It contains a list of |
| 1846 | * SignatureAndHashAlgorithms which are two bytes each. */ |
| 1847 | if (CBS_len(&supported_signature_algorithms) == 0 || |
| 1848 | (CBS_len(&supported_signature_algorithms) % 2) != 0) { |
| 1849 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1850 | return 0; |
| 1851 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1852 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1853 | if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) { |
| 1854 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1855 | return 0; |
| 1856 | } |
| 1857 | /* If sigalgs received and no shared algorithms fatal error. */ |
| 1858 | if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs) { |
HÃ¥vard Molland | ab2479a | 2015-03-20 13:15:39 +0100 | [diff] [blame] | 1859 | OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1860 | SSL_R_NO_SHARED_SIGATURE_ALGORITHMS); |
| 1861 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1862 | return 0; |
| 1863 | } |
| 1864 | } else if (type == TLSEXT_TYPE_next_proto_neg && |
David Benjamin | e6df054 | 2015-05-12 22:02:08 -0400 | [diff] [blame] | 1865 | !s->s3->initial_handshake_complete && |
| 1866 | s->s3->alpn_selected == NULL && !SSL_IS_DTLS(s)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1867 | /* The extension must be empty. */ |
| 1868 | if (CBS_len(&extension) != 0) { |
| 1869 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1870 | return 0; |
| 1871 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1872 | s->s3->next_proto_neg_seen = 1; |
| 1873 | } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation && |
David Benjamin | e6df054 | 2015-05-12 22:02:08 -0400 | [diff] [blame] | 1874 | s->ctx->alpn_select_cb && !s->s3->initial_handshake_complete) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1875 | if (!tls1_alpn_handle_client_hello(s, &extension, out_alert)) { |
| 1876 | return 0; |
| 1877 | } |
| 1878 | /* ALPN takes precedence over NPN. */ |
| 1879 | s->s3->next_proto_neg_seen = 0; |
David Benjamin | 78e6978 | 2014-12-19 04:52:17 -0500 | [diff] [blame] | 1880 | } else if (type == TLSEXT_TYPE_channel_id && s->tlsext_channel_id_enabled && |
| 1881 | !SSL_IS_DTLS(s)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1882 | /* The extension must be empty. */ |
| 1883 | if (CBS_len(&extension) != 0) { |
| 1884 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1885 | return 0; |
| 1886 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1887 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1888 | s->s3->tlsext_channel_id_valid = 1; |
| 1889 | } else if (type == TLSEXT_TYPE_channel_id_new && |
David Benjamin | 78e6978 | 2014-12-19 04:52:17 -0500 | [diff] [blame] | 1890 | s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1891 | /* The extension must be empty. */ |
| 1892 | if (CBS_len(&extension) != 0) { |
| 1893 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1894 | return 0; |
| 1895 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1896 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1897 | s->s3->tlsext_channel_id_valid = 1; |
| 1898 | s->s3->tlsext_channel_id_new = 1; |
| 1899 | } else if (type == TLSEXT_TYPE_use_srtp) { |
| 1900 | if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert)) { |
| 1901 | return 0; |
| 1902 | } |
| 1903 | } else if (type == TLSEXT_TYPE_extended_master_secret && |
| 1904 | s->version != SSL3_VERSION) { |
| 1905 | if (CBS_len(&extension) != 0) { |
| 1906 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1907 | return 0; |
| 1908 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1909 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1910 | s->s3->tmp.extended_master_secret = 1; |
| 1911 | } |
| 1912 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 1913 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1914 | no_extensions: |
| 1915 | for (i = 0; i < kNumExtensions; i++) { |
| 1916 | if (!(s->s3->tmp.extensions.received & (1u << i))) { |
| 1917 | /* Extension wasn't observed so call the callback with a NULL |
| 1918 | * parameter. */ |
| 1919 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 1920 | if (!kExtensions[i].parse_clienthello(s, &alert, NULL)) { |
| 1921 | *out_alert = alert; |
| 1922 | return 0; |
| 1923 | } |
| 1924 | } |
| 1925 | } |
| 1926 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1927 | return 1; |
| 1928 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1929 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1930 | int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) { |
| 1931 | int alert = -1; |
| 1932 | if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) { |
| 1933 | ssl3_send_alert(s, SSL3_AL_FATAL, alert); |
| 1934 | return 0; |
| 1935 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1936 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1937 | if (ssl_check_clienthello_tlsext(s) <= 0) { |
| 1938 | OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext, |
| 1939 | SSL_R_CLIENTHELLO_TLSEXT); |
| 1940 | return 0; |
| 1941 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1942 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1943 | return 1; |
| 1944 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1945 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1946 | /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1947 | * elements of zero length are allowed and the set of elements must exactly |
| 1948 | * fill the length of the block. */ |
| 1949 | static char ssl_next_proto_validate(const CBS *cbs) { |
| 1950 | CBS copy = *cbs; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1951 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1952 | while (CBS_len(©) != 0) { |
| 1953 | CBS proto; |
| 1954 | if (!CBS_get_u8_length_prefixed(©, &proto) || CBS_len(&proto) == 0) { |
| 1955 | return 0; |
| 1956 | } |
| 1957 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1958 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1959 | return 1; |
| 1960 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1961 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1962 | static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1963 | CBS extensions; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1964 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1965 | /* TODO(davidben): Move all of these to some per-handshake state that gets |
| 1966 | * systematically reset on a new handshake; perhaps allocate it fresh each |
| 1967 | * time so it's not even kept around post-handshake. */ |
| 1968 | s->s3->next_proto_neg_seen = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1969 | s->tlsext_ticket_expected = 0; |
| 1970 | s->s3->tmp.certificate_status_expected = 0; |
| 1971 | s->s3->tmp.extended_master_secret = 0; |
David Benjamin | d1d7d3d | 2015-01-11 20:30:21 -0500 | [diff] [blame] | 1972 | s->srtp_profile = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1973 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1974 | OPENSSL_free(s->s3->alpn_selected); |
| 1975 | s->s3->alpn_selected = NULL; |
David Benjamin | a19fc25 | 2014-10-19 00:14:36 -0400 | [diff] [blame] | 1976 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1977 | /* Clear ECC extensions */ |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1978 | OPENSSL_free(s->s3->tmp.peer_ecpointformatlist); |
| 1979 | s->s3->tmp.peer_ecpointformatlist = NULL; |
| 1980 | s->s3->tmp.peer_ecpointformatlist_length = 0; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 1981 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1982 | uint32_t received = 0; |
| 1983 | size_t i; |
Adam Langley | 4cfa96b | 2015-07-01 11:56:55 -0700 | [diff] [blame] | 1984 | assert(kNumExtensions <= sizeof(received) * 8); |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1985 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1986 | /* There may be no extensions. */ |
| 1987 | if (CBS_len(cbs) == 0) { |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 1988 | goto no_extensions; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1989 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1990 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1991 | /* Decode the extensions block and check it is valid. */ |
| 1992 | if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 1993 | !tls1_check_duplicate_extensions(&extensions)) { |
| 1994 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1995 | return 0; |
| 1996 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1997 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1998 | while (CBS_len(&extensions) != 0) { |
| 1999 | uint16_t type; |
| 2000 | CBS extension; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2001 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2002 | /* Decode the next extension. */ |
| 2003 | if (!CBS_get_u16(&extensions, &type) || |
| 2004 | !CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 2005 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2006 | return 0; |
| 2007 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2008 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2009 | unsigned ext_index; |
| 2010 | const struct tls_extension *const ext = |
| 2011 | tls_extension_find(&ext_index, type); |
| 2012 | |
| 2013 | /* While we have extensions that don't use tls_extension this conditional |
| 2014 | * needs to be guarded on |ext != NULL|. In the future, ext being NULL will |
| 2015 | * be fatal. */ |
| 2016 | if (ext != NULL) { |
| 2017 | if (!(s->s3->tmp.extensions.sent & (1u << ext_index))) { |
| 2018 | /* Received an extension that was never sent. */ |
| 2019 | OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext, |
| 2020 | SSL_R_UNEXPECTED_EXTENSION); |
| 2021 | ERR_add_error_dataf("ext:%u", (unsigned) type); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2022 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2023 | return 0; |
| 2024 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2025 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2026 | received |= (1u << ext_index); |
| 2027 | |
| 2028 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 2029 | if (!ext->parse_serverhello(s, &alert, &extension)) { |
| 2030 | *out_alert = alert; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2031 | return 0; |
| 2032 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2033 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2034 | continue; |
| 2035 | } |
| 2036 | |
| 2037 | if (type == TLSEXT_TYPE_ec_point_formats) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2038 | CBS ec_point_format_list; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2039 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2040 | if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) || |
| 2041 | CBS_len(&extension) != 0) { |
| 2042 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2043 | return 0; |
| 2044 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2045 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2046 | if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist, |
| 2047 | &s->s3->tmp.peer_ecpointformatlist_length)) { |
| 2048 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2049 | return 0; |
| 2050 | } |
| 2051 | } else if (type == TLSEXT_TYPE_session_ticket) { |
| 2052 | if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0) { |
| 2053 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2054 | return 0; |
| 2055 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2056 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2057 | s->tlsext_ticket_expected = 1; |
| 2058 | } else if (type == TLSEXT_TYPE_status_request) { |
| 2059 | /* The extension MUST be empty and may only sent if we've requested a |
| 2060 | * status request message. */ |
| 2061 | if (CBS_len(&extension) != 0) { |
| 2062 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2063 | return 0; |
| 2064 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2065 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2066 | if (!s->ocsp_stapling_enabled) { |
| 2067 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2068 | return 0; |
| 2069 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2070 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2071 | /* Set a flag to expect a CertificateStatus message */ |
| 2072 | s->s3->tmp.certificate_status_expected = 1; |
| 2073 | } else if (type == TLSEXT_TYPE_next_proto_neg && |
David Benjamin | e6df054 | 2015-05-12 22:02:08 -0400 | [diff] [blame] | 2074 | !s->s3->initial_handshake_complete && !SSL_IS_DTLS(s)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2075 | uint8_t *selected; |
| 2076 | uint8_t selected_len; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2077 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2078 | /* We must have requested it. */ |
| 2079 | if (s->ctx->next_proto_select_cb == NULL) { |
| 2080 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2081 | return 0; |
| 2082 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2083 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2084 | /* The data must be valid. */ |
| 2085 | if (!ssl_next_proto_validate(&extension)) { |
| 2086 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2087 | return 0; |
| 2088 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2089 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2090 | if (s->ctx->next_proto_select_cb( |
| 2091 | s, &selected, &selected_len, CBS_data(&extension), |
| 2092 | CBS_len(&extension), |
| 2093 | s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) { |
| 2094 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2095 | return 0; |
| 2096 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2097 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2098 | s->next_proto_negotiated = BUF_memdup(selected, selected_len); |
| 2099 | if (s->next_proto_negotiated == NULL) { |
| 2100 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2101 | return 0; |
| 2102 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2103 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2104 | s->next_proto_negotiated_len = selected_len; |
| 2105 | s->s3->next_proto_neg_seen = 1; |
David Benjamin | e6df054 | 2015-05-12 22:02:08 -0400 | [diff] [blame] | 2106 | } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation && |
| 2107 | !s->s3->initial_handshake_complete) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2108 | CBS protocol_name_list, protocol_name; |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 2109 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2110 | /* We must have requested it. */ |
| 2111 | if (s->alpn_client_proto_list == NULL) { |
| 2112 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2113 | return 0; |
| 2114 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2115 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2116 | /* The extension data consists of a ProtocolNameList which must have |
| 2117 | * exactly one ProtocolName. Each of these is length-prefixed. */ |
| 2118 | if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) || |
| 2119 | CBS_len(&extension) != 0 || |
| 2120 | !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) || |
| 2121 | CBS_len(&protocol_name_list) != 0) { |
| 2122 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2123 | return 0; |
| 2124 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2125 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2126 | if (!CBS_stow(&protocol_name, &s->s3->alpn_selected, |
| 2127 | &s->s3->alpn_selected_len)) { |
| 2128 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2129 | return 0; |
| 2130 | } |
David Benjamin | 78e6978 | 2014-12-19 04:52:17 -0500 | [diff] [blame] | 2131 | } else if (type == TLSEXT_TYPE_channel_id && !SSL_IS_DTLS(s)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2132 | if (CBS_len(&extension) != 0) { |
| 2133 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2134 | return 0; |
| 2135 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2136 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2137 | s->s3->tlsext_channel_id_valid = 1; |
David Benjamin | 78e6978 | 2014-12-19 04:52:17 -0500 | [diff] [blame] | 2138 | } else if (type == TLSEXT_TYPE_channel_id_new && !SSL_IS_DTLS(s)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2139 | if (CBS_len(&extension) != 0) { |
| 2140 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2141 | return 0; |
| 2142 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2143 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2144 | s->s3->tlsext_channel_id_valid = 1; |
| 2145 | s->s3->tlsext_channel_id_new = 1; |
| 2146 | } else if (type == TLSEXT_TYPE_certificate_timestamp) { |
| 2147 | if (CBS_len(&extension) == 0) { |
| 2148 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2149 | return 0; |
| 2150 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2151 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2152 | /* Session resumption uses the original session information. */ |
| 2153 | if (!s->hit && |
| 2154 | !CBS_stow(&extension, &s->session->tlsext_signed_cert_timestamp_list, |
| 2155 | &s->session->tlsext_signed_cert_timestamp_list_length)) { |
| 2156 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2157 | return 0; |
| 2158 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2159 | } else if (type == TLSEXT_TYPE_use_srtp) { |
| 2160 | if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert)) { |
| 2161 | return 0; |
| 2162 | } |
| 2163 | } else if (type == TLSEXT_TYPE_extended_master_secret) { |
| 2164 | if (/* It is invalid for the server to select EMS and |
| 2165 | SSLv3. */ |
| 2166 | s->version == SSL3_VERSION || CBS_len(&extension) != 0) { |
| 2167 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2168 | return 0; |
| 2169 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2170 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2171 | s->s3->tmp.extended_master_secret = 1; |
| 2172 | } |
| 2173 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2174 | |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2175 | no_extensions: |
| 2176 | for (i = 0; i < kNumExtensions; i++) { |
| 2177 | if (!(received & (1u << i))) { |
| 2178 | /* Extension wasn't observed so call the callback with a NULL |
| 2179 | * parameter. */ |
| 2180 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 2181 | if (!kExtensions[i].parse_serverhello(s, &alert, NULL)) { |
| 2182 | *out_alert = alert; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2183 | return 0; |
| 2184 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2185 | } |
| 2186 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2187 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2188 | return 1; |
| 2189 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2190 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2191 | int ssl_prepare_clienthello_tlsext(SSL *s) { return 1; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2192 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2193 | int ssl_prepare_serverhello_tlsext(SSL *s) { return 1; } |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 2194 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2195 | static int ssl_check_clienthello_tlsext(SSL *s) { |
| 2196 | int ret = SSL_TLSEXT_ERR_NOACK; |
| 2197 | int al = SSL_AD_UNRECOGNIZED_NAME; |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 2198 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2199 | /* The handling of the ECPointFormats extension is done elsewhere, namely in |
| 2200 | * ssl3_choose_cipher in s3_lib.c. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2201 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2202 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) { |
| 2203 | ret = s->ctx->tlsext_servername_callback(s, &al, |
| 2204 | s->ctx->tlsext_servername_arg); |
| 2205 | } else if (s->initial_ctx != NULL && |
| 2206 | s->initial_ctx->tlsext_servername_callback != 0) { |
| 2207 | ret = s->initial_ctx->tlsext_servername_callback( |
| 2208 | s, &al, s->initial_ctx->tlsext_servername_arg); |
| 2209 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2210 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2211 | switch (ret) { |
| 2212 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2213 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2214 | return -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2215 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2216 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 2217 | ssl3_send_alert(s, SSL3_AL_WARNING, al); |
| 2218 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2219 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2220 | case SSL_TLSEXT_ERR_NOACK: |
Adam Langley | 614c66a | 2015-06-12 15:26:58 -0700 | [diff] [blame] | 2221 | s->s3->tmp.should_ack_sni = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2222 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2223 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2224 | default: |
| 2225 | return 1; |
| 2226 | } |
| 2227 | } |
Adam Langley | ed8270a | 2014-09-02 13:52:56 -0700 | [diff] [blame] | 2228 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2229 | static int ssl_check_serverhello_tlsext(SSL *s) { |
| 2230 | int ret = SSL_TLSEXT_ERR_NOACK; |
| 2231 | int al = SSL_AD_UNRECOGNIZED_NAME; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2232 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2233 | /* If we are client and using an elliptic curve cryptography cipher suite, |
| 2234 | * then if server returns an EC point formats lists extension it must contain |
| 2235 | * uncompressed. */ |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 2236 | uint32_t alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 2237 | uint32_t alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 2238 | if (((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) && |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2239 | !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed)) { |
| 2240 | OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext, |
| 2241 | SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST); |
| 2242 | return -1; |
| 2243 | } |
| 2244 | ret = SSL_TLSEXT_ERR_OK; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2245 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2246 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) { |
| 2247 | ret = s->ctx->tlsext_servername_callback(s, &al, |
| 2248 | s->ctx->tlsext_servername_arg); |
| 2249 | } else if (s->initial_ctx != NULL && |
David Benjamin | b18f024 | 2015-03-10 18:30:08 -0400 | [diff] [blame] | 2250 | s->initial_ctx->tlsext_servername_callback != 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2251 | ret = s->initial_ctx->tlsext_servername_callback( |
| 2252 | s, &al, s->initial_ctx->tlsext_servername_arg); |
| 2253 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2254 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2255 | switch (ret) { |
| 2256 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2257 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2258 | return -1; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2259 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2260 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 2261 | ssl3_send_alert(s, SSL3_AL_WARNING, al); |
| 2262 | return 1; |
| 2263 | |
| 2264 | default: |
| 2265 | return 1; |
| 2266 | } |
| 2267 | } |
| 2268 | |
| 2269 | int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) { |
| 2270 | int alert = -1; |
| 2271 | if (s->version < SSL3_VERSION) { |
| 2272 | return 1; |
| 2273 | } |
| 2274 | |
| 2275 | if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) { |
| 2276 | ssl3_send_alert(s, SSL3_AL_FATAL, alert); |
| 2277 | return 0; |
| 2278 | } |
| 2279 | |
| 2280 | if (ssl_check_serverhello_tlsext(s) <= 0) { |
| 2281 | OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext, |
| 2282 | SSL_R_SERVERHELLO_TLSEXT); |
| 2283 | return 0; |
| 2284 | } |
| 2285 | |
| 2286 | return 1; |
| 2287 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2288 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2289 | int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session, |
| 2290 | int *out_send_ticket, const uint8_t *ticket, |
| 2291 | size_t ticket_len, const uint8_t *session_id, |
| 2292 | size_t session_id_len) { |
| 2293 | int ret = 1; /* Most errors are non-fatal. */ |
| 2294 | SSL_CTX *ssl_ctx = ssl->initial_ctx; |
| 2295 | uint8_t *plaintext = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2296 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2297 | HMAC_CTX hmac_ctx; |
| 2298 | HMAC_CTX_init(&hmac_ctx); |
| 2299 | EVP_CIPHER_CTX cipher_ctx; |
| 2300 | EVP_CIPHER_CTX_init(&cipher_ctx); |
| 2301 | |
| 2302 | *out_send_ticket = 0; |
| 2303 | *out_session = NULL; |
| 2304 | |
| 2305 | if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 2306 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2307 | } |
| 2308 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2309 | if (ticket_len == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2310 | /* The client will accept a ticket but doesn't currently have one. */ |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2311 | *out_send_ticket = 1; |
| 2312 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2313 | } |
| 2314 | |
David Benjamin | adcc395 | 2015-04-26 13:07:57 -0400 | [diff] [blame] | 2315 | /* Ensure there is room for the key name and the largest IV |
| 2316 | * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but |
| 2317 | * the maximum IV length should be well under the minimum size for the |
| 2318 | * session material and HMAC. */ |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2319 | if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) { |
| 2320 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2321 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2322 | const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2323 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2324 | if (ssl_ctx->tlsext_ticket_key_cb != NULL) { |
| 2325 | int cb_ret = ssl_ctx->tlsext_ticket_key_cb(ssl, (uint8_t*)ticket /* name */, |
| 2326 | (uint8_t*)iv, &cipher_ctx, &hmac_ctx, |
| 2327 | 0 /* decrypt */); |
| 2328 | if (cb_ret < 0) { |
| 2329 | ret = 0; |
| 2330 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2331 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2332 | if (cb_ret == 0) { |
| 2333 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2334 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2335 | if (cb_ret == 2) { |
| 2336 | *out_send_ticket = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2337 | } |
| 2338 | } else { |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2339 | /* Check the key name matches. */ |
| 2340 | if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name, |
| 2341 | SSL_TICKET_KEY_NAME_LEN) != 0) { |
| 2342 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2343 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2344 | if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key, |
| 2345 | sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2346 | NULL) || |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2347 | !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL, |
| 2348 | ssl_ctx->tlsext_tick_aes_key, iv)) { |
| 2349 | ret = 0; |
| 2350 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2351 | } |
| 2352 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2353 | size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2354 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2355 | /* Check the MAC at the end of the ticket. */ |
| 2356 | uint8_t mac[EVP_MAX_MD_SIZE]; |
| 2357 | size_t mac_len = HMAC_size(&hmac_ctx); |
| 2358 | if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) { |
David Benjamin | adcc395 | 2015-04-26 13:07:57 -0400 | [diff] [blame] | 2359 | /* The ticket must be large enough for key name, IV, data, and MAC. */ |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2360 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2361 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2362 | HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len); |
| 2363 | HMAC_Final(&hmac_ctx, mac, NULL); |
| 2364 | if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) { |
| 2365 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2366 | } |
| 2367 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2368 | /* Decrypt the session data. */ |
| 2369 | const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len; |
| 2370 | size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len - |
| 2371 | mac_len; |
| 2372 | plaintext = OPENSSL_malloc(ciphertext_len); |
| 2373 | if (plaintext == NULL) { |
| 2374 | ret = 0; |
| 2375 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2376 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2377 | if (ciphertext_len >= INT_MAX) { |
| 2378 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2379 | } |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2380 | int len1, len2; |
| 2381 | if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext, |
| 2382 | (int)ciphertext_len) || |
| 2383 | !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) { |
| 2384 | ERR_clear_error(); /* Don't leave an error on the queue. */ |
| 2385 | goto done; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2386 | } |
| 2387 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame^] | 2388 | /* Decode the session. */ |
| 2389 | SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2); |
| 2390 | if (session == NULL) { |
| 2391 | ERR_clear_error(); /* Don't leave an error on the queue. */ |
| 2392 | goto done; |
| 2393 | } |
| 2394 | |
| 2395 | /* Copy the client's session ID into the new session, to denote the ticket has |
| 2396 | * been accepted. */ |
| 2397 | memcpy(session->session_id, session_id, session_id_len); |
| 2398 | session->session_id_length = session_id_len; |
| 2399 | |
| 2400 | *out_session = session; |
| 2401 | |
| 2402 | done: |
| 2403 | OPENSSL_free(plaintext); |
| 2404 | HMAC_CTX_cleanup(&hmac_ctx); |
| 2405 | EVP_CIPHER_CTX_cleanup(&cipher_ctx); |
| 2406 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2407 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2408 | |
| 2409 | /* Tables to translate from NIDs to TLS v1.2 ids */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2410 | typedef struct { |
| 2411 | int nid; |
| 2412 | int id; |
| 2413 | } tls12_lookup; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2414 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2415 | static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5}, |
| 2416 | {NID_sha1, TLSEXT_hash_sha1}, |
| 2417 | {NID_sha224, TLSEXT_hash_sha224}, |
| 2418 | {NID_sha256, TLSEXT_hash_sha256}, |
| 2419 | {NID_sha384, TLSEXT_hash_sha384}, |
| 2420 | {NID_sha512, TLSEXT_hash_sha512}}; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2421 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2422 | static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa}, |
| 2423 | {EVP_PKEY_EC, TLSEXT_signature_ecdsa}}; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2424 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2425 | static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) { |
| 2426 | size_t i; |
| 2427 | for (i = 0; i < tlen; i++) { |
| 2428 | if (table[i].nid == nid) { |
| 2429 | return table[i].id; |
| 2430 | } |
| 2431 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2432 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2433 | return -1; |
| 2434 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2435 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2436 | static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen) { |
| 2437 | size_t i; |
| 2438 | for (i = 0; i < tlen; i++) { |
| 2439 | if (table[i].id == id) { |
| 2440 | return table[i].nid; |
| 2441 | } |
| 2442 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2443 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2444 | return NID_undef; |
| 2445 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2446 | |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 2447 | int tls12_get_sigid(int pkey_type) { |
| 2448 | return tls12_find_id(pkey_type, tls12_sig, |
| 2449 | sizeof(tls12_sig) / sizeof(tls12_lookup)); |
| 2450 | } |
| 2451 | |
| 2452 | int tls12_get_sigandhash(SSL *ssl, uint8_t *p, const EVP_PKEY *pk, |
| 2453 | const EVP_MD *md) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2454 | int sig_id, md_id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2455 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2456 | if (!md) { |
| 2457 | return 0; |
| 2458 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2459 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2460 | md_id = tls12_find_id(EVP_MD_type(md), tls12_md, |
| 2461 | sizeof(tls12_md) / sizeof(tls12_lookup)); |
| 2462 | if (md_id == -1) { |
| 2463 | return 0; |
| 2464 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2465 | |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 2466 | sig_id = tls12_get_sigid(ssl_private_key_type(ssl, pk)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2467 | if (sig_id == -1) { |
| 2468 | return 0; |
| 2469 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2470 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2471 | p[0] = (uint8_t)md_id; |
| 2472 | p[1] = (uint8_t)sig_id; |
| 2473 | return 1; |
| 2474 | } |
| 2475 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2476 | const EVP_MD *tls12_get_hash(uint8_t hash_alg) { |
| 2477 | switch (hash_alg) { |
| 2478 | case TLSEXT_hash_md5: |
| 2479 | return EVP_md5(); |
| 2480 | |
| 2481 | case TLSEXT_hash_sha1: |
| 2482 | return EVP_sha1(); |
| 2483 | |
| 2484 | case TLSEXT_hash_sha224: |
| 2485 | return EVP_sha224(); |
| 2486 | |
| 2487 | case TLSEXT_hash_sha256: |
| 2488 | return EVP_sha256(); |
| 2489 | |
| 2490 | case TLSEXT_hash_sha384: |
| 2491 | return EVP_sha384(); |
| 2492 | |
| 2493 | case TLSEXT_hash_sha512: |
| 2494 | return EVP_sha512(); |
| 2495 | |
| 2496 | default: |
| 2497 | return NULL; |
| 2498 | } |
| 2499 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2500 | |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 2501 | /* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature |
| 2502 | * algorithm |sig_alg|. It returns -1 if the type is unknown. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2503 | static int tls12_get_pkey_type(uint8_t sig_alg) { |
| 2504 | switch (sig_alg) { |
| 2505 | case TLSEXT_signature_rsa: |
| 2506 | return EVP_PKEY_RSA; |
| 2507 | |
| 2508 | case TLSEXT_signature_ecdsa: |
| 2509 | return EVP_PKEY_EC; |
| 2510 | |
| 2511 | default: |
| 2512 | return -1; |
| 2513 | } |
| 2514 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2515 | |
| 2516 | /* Convert TLS 1.2 signature algorithm extension values into NIDs */ |
| 2517 | static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2518 | int *psignhash_nid, const uint8_t *data) { |
| 2519 | int sign_nid = 0, hash_nid = 0; |
| 2520 | if (!phash_nid && !psign_nid && !psignhash_nid) { |
| 2521 | return; |
| 2522 | } |
| 2523 | |
| 2524 | if (phash_nid || psignhash_nid) { |
| 2525 | hash_nid = tls12_find_nid(data[0], tls12_md, |
| 2526 | sizeof(tls12_md) / sizeof(tls12_lookup)); |
| 2527 | if (phash_nid) { |
| 2528 | *phash_nid = hash_nid; |
| 2529 | } |
| 2530 | } |
| 2531 | |
| 2532 | if (psign_nid || psignhash_nid) { |
| 2533 | sign_nid = tls12_find_nid(data[1], tls12_sig, |
| 2534 | sizeof(tls12_sig) / sizeof(tls12_lookup)); |
| 2535 | if (psign_nid) { |
| 2536 | *psign_nid = sign_nid; |
| 2537 | } |
| 2538 | } |
| 2539 | |
| 2540 | if (psignhash_nid) { |
| 2541 | if (sign_nid && hash_nid) { |
| 2542 | OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid); |
| 2543 | } else { |
| 2544 | *psignhash_nid = NID_undef; |
| 2545 | } |
| 2546 | } |
| 2547 | } |
| 2548 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2549 | /* Given preference and allowed sigalgs set shared sigalgs */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2550 | static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref, |
| 2551 | size_t preflen, const uint8_t *allow, |
| 2552 | size_t allowlen) { |
| 2553 | const uint8_t *ptmp, *atmp; |
| 2554 | size_t i, j, nmatch = 0; |
| 2555 | |
| 2556 | for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) { |
| 2557 | /* Skip disabled hashes or signature algorithms */ |
| 2558 | if (tls12_get_hash(ptmp[0]) == NULL || |
| 2559 | tls12_get_pkey_type(ptmp[1]) == -1) { |
| 2560 | continue; |
| 2561 | } |
| 2562 | |
| 2563 | for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) { |
| 2564 | if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) { |
| 2565 | nmatch++; |
| 2566 | if (shsig) { |
| 2567 | shsig->rhash = ptmp[0]; |
| 2568 | shsig->rsign = ptmp[1]; |
| 2569 | tls1_lookup_sigalg(&shsig->hash_nid, &shsig->sign_nid, |
| 2570 | &shsig->signandhash_nid, ptmp); |
| 2571 | shsig++; |
| 2572 | } |
| 2573 | |
| 2574 | break; |
| 2575 | } |
| 2576 | } |
| 2577 | } |
| 2578 | |
| 2579 | return nmatch; |
| 2580 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2581 | |
| 2582 | /* Set shared signature algorithms for SSL structures */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2583 | static int tls1_set_shared_sigalgs(SSL *s) { |
| 2584 | const uint8_t *pref, *allow, *conf; |
| 2585 | size_t preflen, allowlen, conflen; |
| 2586 | size_t nmatch; |
| 2587 | TLS_SIGALGS *salgs = NULL; |
| 2588 | CERT *c = s->cert; |
| 2589 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2590 | OPENSSL_free(c->shared_sigalgs); |
| 2591 | c->shared_sigalgs = NULL; |
| 2592 | c->shared_sigalgslen = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2593 | |
| 2594 | /* If client use client signature algorithms if not NULL */ |
| 2595 | if (!s->server && c->client_sigalgs) { |
| 2596 | conf = c->client_sigalgs; |
| 2597 | conflen = c->client_sigalgslen; |
| 2598 | } else if (c->conf_sigalgs) { |
| 2599 | conf = c->conf_sigalgs; |
| 2600 | conflen = c->conf_sigalgslen; |
| 2601 | } else { |
| 2602 | conflen = tls12_get_psigalgs(s, &conf); |
| 2603 | } |
| 2604 | |
| 2605 | if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
| 2606 | pref = conf; |
| 2607 | preflen = conflen; |
| 2608 | allow = c->peer_sigalgs; |
| 2609 | allowlen = c->peer_sigalgslen; |
| 2610 | } else { |
| 2611 | allow = conf; |
| 2612 | allowlen = conflen; |
| 2613 | pref = c->peer_sigalgs; |
| 2614 | preflen = c->peer_sigalgslen; |
| 2615 | } |
| 2616 | |
| 2617 | nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen); |
| 2618 | if (!nmatch) { |
| 2619 | return 1; |
| 2620 | } |
| 2621 | |
| 2622 | salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS)); |
| 2623 | if (!salgs) { |
| 2624 | return 0; |
| 2625 | } |
| 2626 | |
| 2627 | nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen); |
| 2628 | c->shared_sigalgs = salgs; |
| 2629 | c->shared_sigalgslen = nmatch; |
| 2630 | return 1; |
| 2631 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2632 | |
| 2633 | /* Set preferred digest for each key type */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2634 | int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) { |
| 2635 | CERT *c = s->cert; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2636 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2637 | /* Extension ignored for inappropriate versions */ |
| 2638 | if (!SSL_USE_SIGALGS(s)) { |
| 2639 | return 1; |
| 2640 | } |
David Benjamin | cd99694 | 2014-07-20 16:23:51 -0400 | [diff] [blame] | 2641 | |
David Benjamin | 61c0d4e | 2015-03-19 14:24:37 -0400 | [diff] [blame] | 2642 | if (CBS_len(sigalgs) % 2 != 0 || |
| 2643 | !CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen) || |
| 2644 | !tls1_set_shared_sigalgs(s)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2645 | return 0; |
| 2646 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2647 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2648 | return 1; |
| 2649 | } |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 2650 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2651 | const EVP_MD *tls1_choose_signing_digest(SSL *s, EVP_PKEY *pkey) { |
| 2652 | CERT *c = s->cert; |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 2653 | int type = ssl_private_key_type(s, pkey); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2654 | size_t i; |
David Benjamin | ec2f27d | 2014-11-13 19:17:25 -0500 | [diff] [blame] | 2655 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2656 | /* Select the first shared digest supported by our key. */ |
| 2657 | for (i = 0; i < c->shared_sigalgslen; i++) { |
| 2658 | const EVP_MD *md = tls12_get_hash(c->shared_sigalgs[i].rhash); |
| 2659 | if (md == NULL || |
| 2660 | tls12_get_pkey_type(c->shared_sigalgs[i].rsign) != type || |
David Benjamin | b4d65fd | 2015-05-29 17:11:21 -0400 | [diff] [blame] | 2661 | !ssl_private_key_supports_digest(s, pkey, md)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2662 | continue; |
| 2663 | } |
| 2664 | return md; |
| 2665 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2666 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2667 | /* If no suitable digest may be found, default to SHA-1. */ |
| 2668 | return EVP_sha1(); |
| 2669 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2670 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2671 | int SSL_get_sigalgs(SSL *s, int idx, int *psign, int *phash, int *psignhash, |
| 2672 | uint8_t *rsig, uint8_t *rhash) { |
| 2673 | const uint8_t *psig = s->cert->peer_sigalgs; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2674 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2675 | if (psig == NULL) { |
| 2676 | return 0; |
| 2677 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2678 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2679 | if (idx >= 0) { |
| 2680 | idx <<= 1; |
| 2681 | if (idx >= (int)s->cert->peer_sigalgslen) { |
| 2682 | return 0; |
| 2683 | } |
| 2684 | psig += idx; |
| 2685 | if (rhash) { |
| 2686 | *rhash = psig[0]; |
| 2687 | } |
| 2688 | if (rsig) { |
| 2689 | *rsig = psig[1]; |
| 2690 | } |
| 2691 | tls1_lookup_sigalg(phash, psign, psignhash, psig); |
| 2692 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2693 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2694 | return s->cert->peer_sigalgslen / 2; |
| 2695 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2696 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2697 | int SSL_get_shared_sigalgs(SSL *s, int idx, int *psign, int *phash, |
| 2698 | int *psignhash, uint8_t *rsig, uint8_t *rhash) { |
| 2699 | TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2700 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2701 | if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen) { |
| 2702 | return 0; |
| 2703 | } |
| 2704 | |
| 2705 | shsigalgs += idx; |
| 2706 | if (phash) { |
| 2707 | *phash = shsigalgs->hash_nid; |
| 2708 | } |
| 2709 | if (psign) { |
| 2710 | *psign = shsigalgs->sign_nid; |
| 2711 | } |
| 2712 | if (psignhash) { |
| 2713 | *psignhash = shsigalgs->signandhash_nid; |
| 2714 | } |
| 2715 | if (rsig) { |
| 2716 | *rsig = shsigalgs->rsign; |
| 2717 | } |
| 2718 | if (rhash) { |
| 2719 | *rhash = shsigalgs->rhash; |
| 2720 | } |
| 2721 | |
| 2722 | return s->cert->shared_sigalgslen; |
| 2723 | } |
| 2724 | |
| 2725 | /* tls1_channel_id_hash calculates the signed data for a Channel ID on the |
| 2726 | * given SSL connection and writes it to |md|. */ |
| 2727 | int tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) { |
| 2728 | EVP_MD_CTX ctx; |
| 2729 | uint8_t temp_digest[EVP_MAX_MD_SIZE]; |
| 2730 | unsigned temp_digest_len; |
| 2731 | int i; |
| 2732 | static const char kClientIDMagic[] = "TLS Channel ID signature"; |
| 2733 | |
| 2734 | if (s->s3->handshake_buffer && |
| 2735 | !ssl3_digest_cached_records(s, free_handshake_buffer)) { |
| 2736 | return 0; |
| 2737 | } |
| 2738 | |
| 2739 | EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic)); |
| 2740 | |
| 2741 | if (s->hit && s->s3->tlsext_channel_id_new) { |
| 2742 | static const char kResumptionMagic[] = "Resumption"; |
| 2743 | EVP_DigestUpdate(md, kResumptionMagic, sizeof(kResumptionMagic)); |
| 2744 | if (s->session->original_handshake_hash_len == 0) { |
| 2745 | return 0; |
| 2746 | } |
| 2747 | EVP_DigestUpdate(md, s->session->original_handshake_hash, |
| 2748 | s->session->original_handshake_hash_len); |
| 2749 | } |
| 2750 | |
| 2751 | EVP_MD_CTX_init(&ctx); |
| 2752 | for (i = 0; i < SSL_MAX_DIGEST; i++) { |
| 2753 | if (s->s3->handshake_dgst[i] == NULL) { |
| 2754 | continue; |
| 2755 | } |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 2756 | if (!EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i])) { |
| 2757 | EVP_MD_CTX_cleanup(&ctx); |
| 2758 | return 0; |
| 2759 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2760 | EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len); |
| 2761 | EVP_DigestUpdate(md, temp_digest, temp_digest_len); |
| 2762 | } |
| 2763 | EVP_MD_CTX_cleanup(&ctx); |
| 2764 | |
| 2765 | return 1; |
| 2766 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2767 | |
| 2768 | /* tls1_record_handshake_hashes_for_channel_id records the current handshake |
| 2769 | * hashes in |s->session| so that Channel ID resumptions can sign that data. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2770 | int tls1_record_handshake_hashes_for_channel_id(SSL *s) { |
| 2771 | int digest_len; |
| 2772 | /* This function should never be called for a resumed session because the |
| 2773 | * handshake hashes that we wish to record are for the original, full |
| 2774 | * handshake. */ |
| 2775 | if (s->hit) { |
| 2776 | return -1; |
| 2777 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2778 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2779 | /* It only makes sense to call this function if Channel IDs have been |
| 2780 | * negotiated. */ |
| 2781 | if (!s->s3->tlsext_channel_id_new) { |
| 2782 | return -1; |
| 2783 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2784 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2785 | digest_len = |
| 2786 | tls1_handshake_digest(s, s->session->original_handshake_hash, |
| 2787 | sizeof(s->session->original_handshake_hash)); |
| 2788 | if (digest_len < 0) { |
| 2789 | return -1; |
| 2790 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2791 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2792 | s->session->original_handshake_hash_len = digest_len; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2793 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2794 | return 1; |
| 2795 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2796 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2797 | int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, |
| 2798 | int client) { |
| 2799 | uint8_t *sigalgs, *sptr; |
| 2800 | int rhash, rsign; |
| 2801 | size_t i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2802 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2803 | if (salglen & 1) { |
| 2804 | return 0; |
| 2805 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2806 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2807 | sigalgs = OPENSSL_malloc(salglen); |
| 2808 | if (sigalgs == NULL) { |
| 2809 | return 0; |
| 2810 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2811 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2812 | for (i = 0, sptr = sigalgs; i < salglen; i += 2) { |
| 2813 | rhash = tls12_find_id(*psig_nids++, tls12_md, |
| 2814 | sizeof(tls12_md) / sizeof(tls12_lookup)); |
| 2815 | rsign = tls12_find_id(*psig_nids++, tls12_sig, |
| 2816 | sizeof(tls12_sig) / sizeof(tls12_lookup)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2817 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2818 | if (rhash == -1 || rsign == -1) { |
| 2819 | goto err; |
| 2820 | } |
| 2821 | *sptr++ = rhash; |
| 2822 | *sptr++ = rsign; |
| 2823 | } |
| 2824 | |
| 2825 | if (client) { |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2826 | OPENSSL_free(c->client_sigalgs); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2827 | c->client_sigalgs = sigalgs; |
| 2828 | c->client_sigalgslen = salglen; |
| 2829 | } else { |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2830 | OPENSSL_free(c->conf_sigalgs); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2831 | c->conf_sigalgs = sigalgs; |
| 2832 | c->conf_sigalgslen = salglen; |
| 2833 | } |
| 2834 | |
| 2835 | return 1; |
| 2836 | |
| 2837 | err: |
| 2838 | OPENSSL_free(sigalgs); |
| 2839 | return 0; |
| 2840 | } |