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