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