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