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