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