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 | |
| 109 | #include <stdio.h> |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 110 | #include <stdlib.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 111 | #include <assert.h> |
| 112 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 113 | #include <openssl/bytestring.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 114 | #include <openssl/evp.h> |
| 115 | #include <openssl/hmac.h> |
| 116 | #include <openssl/mem.h> |
| 117 | #include <openssl/obj.h> |
| 118 | #include <openssl/rand.h> |
| 119 | |
| 120 | #include "ssl_locl.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 121 | static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen, |
| 122 | const unsigned char *sess_id, int sesslen, |
| 123 | SSL_SESSION **psess); |
| 124 | static int ssl_check_clienthello_tlsext_early(SSL *s); |
| 125 | int ssl_check_serverhello_tlsext(SSL *s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 126 | |
| 127 | SSL3_ENC_METHOD TLSv1_enc_data={ |
| 128 | tls1_enc, |
| 129 | tls1_mac, |
| 130 | tls1_setup_key_block, |
| 131 | tls1_generate_master_secret, |
| 132 | tls1_change_cipher_state, |
| 133 | tls1_final_finish_mac, |
| 134 | TLS1_FINISH_MAC_LENGTH, |
| 135 | tls1_cert_verify_mac, |
| 136 | TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 137 | TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 138 | tls1_alert_code, |
| 139 | tls1_export_keying_material, |
| 140 | 0, |
| 141 | SSL3_HM_HEADER_LENGTH, |
| 142 | ssl3_set_handshake_header, |
| 143 | ssl3_handshake_write |
| 144 | }; |
| 145 | |
| 146 | SSL3_ENC_METHOD TLSv1_1_enc_data={ |
| 147 | tls1_enc, |
| 148 | tls1_mac, |
| 149 | tls1_setup_key_block, |
| 150 | tls1_generate_master_secret, |
| 151 | tls1_change_cipher_state, |
| 152 | tls1_final_finish_mac, |
| 153 | TLS1_FINISH_MAC_LENGTH, |
| 154 | tls1_cert_verify_mac, |
| 155 | TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 156 | TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 157 | tls1_alert_code, |
| 158 | tls1_export_keying_material, |
| 159 | SSL_ENC_FLAG_EXPLICIT_IV, |
| 160 | SSL3_HM_HEADER_LENGTH, |
| 161 | ssl3_set_handshake_header, |
| 162 | ssl3_handshake_write |
| 163 | }; |
| 164 | |
| 165 | SSL3_ENC_METHOD TLSv1_2_enc_data={ |
| 166 | tls1_enc, |
| 167 | tls1_mac, |
| 168 | tls1_setup_key_block, |
| 169 | tls1_generate_master_secret, |
| 170 | tls1_change_cipher_state, |
| 171 | tls1_final_finish_mac, |
| 172 | TLS1_FINISH_MAC_LENGTH, |
| 173 | tls1_cert_verify_mac, |
| 174 | TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 175 | TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 176 | tls1_alert_code, |
| 177 | tls1_export_keying_material, |
| 178 | SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS|SSL_ENC_FLAG_SHA256_PRF |
| 179 | |SSL_ENC_FLAG_TLS1_2_CIPHERS, |
| 180 | SSL3_HM_HEADER_LENGTH, |
| 181 | ssl3_set_handshake_header, |
| 182 | ssl3_handshake_write |
| 183 | }; |
| 184 | |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 185 | static int compare_uint16_t(const void *p1, const void *p2) |
| 186 | { |
| 187 | uint16_t u1 = *((const uint16_t*)p1); |
| 188 | uint16_t u2 = *((const uint16_t*)p2); |
| 189 | if (u1 < u2) |
| 190 | { |
| 191 | return -1; |
| 192 | } |
| 193 | else if (u1 > u2) |
| 194 | { |
| 195 | return 1; |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | return 0; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be more |
| 204 | * than one extension of the same type in a ClientHello or ServerHello. This |
| 205 | * function does an initial scan over the extensions block to filter those |
| 206 | * out. */ |
| 207 | static int tls1_check_duplicate_extensions(const CBS *cbs) |
| 208 | { |
| 209 | CBS extensions = *cbs; |
| 210 | size_t num_extensions = 0, i = 0; |
| 211 | uint16_t *extension_types = NULL; |
| 212 | int ret = 0; |
| 213 | |
| 214 | /* First pass: count the extensions. */ |
| 215 | while (CBS_len(&extensions) > 0) |
| 216 | { |
| 217 | uint16_t type; |
| 218 | CBS extension; |
| 219 | |
| 220 | if (!CBS_get_u16(&extensions, &type) || |
| 221 | !CBS_get_u16_length_prefixed(&extensions, &extension)) |
| 222 | { |
| 223 | goto done; |
| 224 | } |
| 225 | |
| 226 | num_extensions++; |
| 227 | } |
| 228 | |
David Benjamin | 9a37359 | 2014-07-25 04:27:53 -0400 | [diff] [blame] | 229 | if (num_extensions == 0) |
| 230 | { |
| 231 | return 1; |
| 232 | } |
| 233 | |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 234 | extension_types = (uint16_t*)OPENSSL_malloc(sizeof(uint16_t) * num_extensions); |
| 235 | if (extension_types == NULL) |
| 236 | { |
| 237 | OPENSSL_PUT_ERROR(SSL, tls1_check_duplicate_extensions, ERR_R_MALLOC_FAILURE); |
| 238 | goto done; |
| 239 | } |
| 240 | |
| 241 | /* Second pass: gather the extension types. */ |
| 242 | extensions = *cbs; |
| 243 | for (i = 0; i < num_extensions; i++) |
| 244 | { |
| 245 | CBS extension; |
| 246 | |
| 247 | if (!CBS_get_u16(&extensions, &extension_types[i]) || |
| 248 | !CBS_get_u16_length_prefixed(&extensions, &extension)) |
| 249 | { |
| 250 | /* This should not happen. */ |
| 251 | goto done; |
| 252 | } |
| 253 | } |
| 254 | assert(CBS_len(&extensions) == 0); |
| 255 | |
| 256 | /* Sort the extensions and make sure there are no duplicates. */ |
| 257 | qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t); |
| 258 | for (i = 1; i < num_extensions; i++) |
| 259 | { |
| 260 | if (extension_types[i-1] == extension_types[i]) |
| 261 | { |
| 262 | goto done; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | ret = 1; |
| 267 | done: |
| 268 | if (extension_types) |
| 269 | OPENSSL_free(extension_types); |
| 270 | return ret; |
| 271 | } |
| 272 | |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 273 | char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) |
| 274 | { |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 275 | CBS client_hello, session_id, cipher_suites, compression_methods, extensions; |
| 276 | |
| 277 | CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 278 | |
| 279 | /* Skip client version. */ |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 280 | if (!CBS_skip(&client_hello, 2)) |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 281 | return 0; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 282 | |
| 283 | /* Skip client nonce. */ |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 284 | if (!CBS_skip(&client_hello, 32)) |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 285 | return 0; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 286 | |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 287 | /* Extract session_id. */ |
| 288 | if (!CBS_get_u8_length_prefixed(&client_hello, &session_id)) |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 289 | return 0; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 290 | ctx->session_id = CBS_data(&session_id); |
| 291 | ctx->session_id_len = CBS_len(&session_id); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 292 | |
| 293 | /* Skip past DTLS cookie */ |
David Benjamin | 09bd58d | 2014-08-12 21:22:28 -0400 | [diff] [blame] | 294 | if (SSL_IS_DTLS(ctx->ssl)) |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 295 | { |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 296 | CBS cookie; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 297 | |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 298 | if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 299 | return 0; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 300 | } |
| 301 | |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 302 | /* Extract cipher_suites. */ |
| 303 | if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) || |
| 304 | CBS_len(&cipher_suites) < 2 || |
| 305 | (CBS_len(&cipher_suites) & 1) != 0) |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 306 | return 0; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 307 | ctx->cipher_suites = CBS_data(&cipher_suites); |
| 308 | ctx->cipher_suites_len = CBS_len(&cipher_suites); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 309 | |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 310 | /* Extract compression_methods. */ |
| 311 | if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) || |
| 312 | CBS_len(&compression_methods) < 1) |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 313 | return 0; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 314 | ctx->compression_methods = CBS_data(&compression_methods); |
| 315 | ctx->compression_methods_len = CBS_len(&compression_methods); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 316 | |
| 317 | /* If the ClientHello ends here then it's valid, but doesn't have any |
| 318 | * extensions. (E.g. SSLv3.) */ |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 319 | if (CBS_len(&client_hello) == 0) |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 320 | { |
| 321 | ctx->extensions = NULL; |
| 322 | ctx->extensions_len = 0; |
| 323 | return 1; |
| 324 | } |
| 325 | |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 326 | /* Extract extensions and check it is valid. */ |
| 327 | if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) || |
| 328 | !tls1_check_duplicate_extensions(&extensions) || |
| 329 | CBS_len(&client_hello) != 0) |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 330 | return 0; |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 331 | ctx->extensions = CBS_data(&extensions); |
| 332 | ctx->extensions_len = CBS_len(&extensions); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 333 | |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 334 | return 1; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | char |
| 338 | SSL_early_callback_ctx_extension_get(const struct ssl_early_callback_ctx *ctx, |
| 339 | uint16_t extension_type, |
| 340 | const unsigned char **out_data, |
| 341 | size_t *out_len) |
| 342 | { |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 343 | CBS extensions; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 344 | |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 345 | CBS_init(&extensions, ctx->extensions, ctx->extensions_len); |
| 346 | |
| 347 | while (CBS_len(&extensions) != 0) |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 348 | { |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 349 | uint16_t type; |
| 350 | CBS extension; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 351 | |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 352 | /* Decode the next extension. */ |
| 353 | if (!CBS_get_u16(&extensions, &type) || |
| 354 | !CBS_get_u16_length_prefixed(&extensions, &extension)) |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 355 | return 0; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 356 | |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 357 | if (type == extension_type) |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 358 | { |
David Benjamin | 8f2c20e | 2014-07-09 09:30:38 -0400 | [diff] [blame] | 359 | *out_data = CBS_data(&extension); |
| 360 | *out_len = CBS_len(&extension); |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 361 | return 1; |
| 362 | } |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 368 | #ifndef OPENSSL_NO_EC |
| 369 | |
David Benjamin | cff6472 | 2014-08-19 19:54:46 -0400 | [diff] [blame] | 370 | static const int nid_list[] = |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 371 | { |
| 372 | NID_sect163k1, /* sect163k1 (1) */ |
| 373 | NID_sect163r1, /* sect163r1 (2) */ |
| 374 | NID_sect163r2, /* sect163r2 (3) */ |
| 375 | NID_sect193r1, /* sect193r1 (4) */ |
| 376 | NID_sect193r2, /* sect193r2 (5) */ |
| 377 | NID_sect233k1, /* sect233k1 (6) */ |
| 378 | NID_sect233r1, /* sect233r1 (7) */ |
| 379 | NID_sect239k1, /* sect239k1 (8) */ |
| 380 | NID_sect283k1, /* sect283k1 (9) */ |
| 381 | NID_sect283r1, /* sect283r1 (10) */ |
| 382 | NID_sect409k1, /* sect409k1 (11) */ |
| 383 | NID_sect409r1, /* sect409r1 (12) */ |
| 384 | NID_sect571k1, /* sect571k1 (13) */ |
| 385 | NID_sect571r1, /* sect571r1 (14) */ |
| 386 | NID_secp160k1, /* secp160k1 (15) */ |
| 387 | NID_secp160r1, /* secp160r1 (16) */ |
| 388 | NID_secp160r2, /* secp160r2 (17) */ |
| 389 | NID_secp192k1, /* secp192k1 (18) */ |
| 390 | NID_X9_62_prime192v1, /* secp192r1 (19) */ |
| 391 | NID_secp224k1, /* secp224k1 (20) */ |
| 392 | NID_secp224r1, /* secp224r1 (21) */ |
| 393 | NID_secp256k1, /* secp256k1 (22) */ |
| 394 | NID_X9_62_prime256v1, /* secp256r1 (23) */ |
| 395 | NID_secp384r1, /* secp384r1 (24) */ |
| 396 | NID_secp521r1, /* secp521r1 (25) */ |
| 397 | NID_brainpoolP256r1, /* brainpoolP256r1 (26) */ |
| 398 | NID_brainpoolP384r1, /* brainpoolP384r1 (27) */ |
| 399 | NID_brainpoolP512r1 /* brainpool512r1 (28) */ |
| 400 | }; |
| 401 | |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 402 | static const uint8_t ecformats_default[] = |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 403 | { |
| 404 | TLSEXT_ECPOINTFORMAT_uncompressed, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 405 | }; |
| 406 | |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 407 | static const uint16_t eccurves_default[] = |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 408 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 409 | 23, /* secp256r1 (23) */ |
| 410 | 24, /* secp384r1 (24) */ |
| 411 | 25, /* secp521r1 (25) */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 412 | }; |
| 413 | |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 414 | int tls1_ec_curve_id2nid(uint16_t curve_id) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 415 | { |
| 416 | /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */ |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 417 | if (curve_id < 1 || curve_id > sizeof(nid_list)/sizeof(nid_list[0])) |
| 418 | return OBJ_undef; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 419 | return nid_list[curve_id-1]; |
| 420 | } |
| 421 | |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 422 | uint16_t tls1_ec_nid2curve_id(int nid) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 423 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 424 | size_t i; |
| 425 | for (i = 0; i < sizeof(nid_list)/sizeof(nid_list[0]); i++) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 426 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 427 | /* nid_list[i] stores the NID corresponding to curve ID i+1. */ |
| 428 | if (nid == nid_list[i]) |
| 429 | return i + 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 430 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 431 | /* Use 0 for non-existent curve ID. Note: this assumes that curve ID 0 |
| 432 | * will never be allocated. */ |
| 433 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 434 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 435 | |
| 436 | /* tls1_get_curvelist sets |*out_curve_ids| and |*out_curve_ids_len| to the list |
| 437 | * of allowed curve IDs. If |get_client_curves| is non-zero, return the client |
| 438 | * curve list. Otherwise, return the preferred list. */ |
| 439 | static void tls1_get_curvelist(SSL *s, int get_client_curves, |
| 440 | const uint16_t **out_curve_ids, size_t *out_curve_ids_len) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 441 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 442 | if (get_client_curves) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 443 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 444 | *out_curve_ids = s->session->tlsext_ellipticcurvelist; |
| 445 | *out_curve_ids_len = s->session->tlsext_ellipticcurvelist_length; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 446 | return; |
| 447 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 448 | |
David Benjamin | 335d10d | 2014-08-06 19:56:33 -0400 | [diff] [blame] | 449 | *out_curve_ids = s->tlsext_ellipticcurvelist; |
| 450 | *out_curve_ids_len = s->tlsext_ellipticcurvelist_length; |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 451 | if (!*out_curve_ids) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 452 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 453 | *out_curve_ids = eccurves_default; |
David Benjamin | 0eb5a2d | 2014-07-25 02:40:43 -0400 | [diff] [blame] | 454 | *out_curve_ids_len = sizeof(eccurves_default) / sizeof(eccurves_default[0]); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 455 | } |
| 456 | } |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 457 | |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 458 | int tls1_check_curve(SSL *s, CBS *cbs, uint16_t *out_curve_id) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 459 | { |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 460 | uint8_t curve_type; |
| 461 | uint16_t curve_id; |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 462 | const uint16_t *curves; |
| 463 | size_t curves_len, i; |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 464 | |
| 465 | /* Only support named curves. */ |
| 466 | if (!CBS_get_u8(cbs, &curve_type) || |
| 467 | curve_type != NAMED_CURVE_TYPE || |
| 468 | !CBS_get_u16(cbs, &curve_id)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 469 | return 0; |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 470 | |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 471 | tls1_get_curvelist(s, 0, &curves, &curves_len); |
| 472 | for (i = 0; i < curves_len; i++) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 473 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 474 | if (curve_id == curves[i]) |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 475 | { |
| 476 | *out_curve_id = curve_id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 477 | return 1; |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 478 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 479 | } |
| 480 | return 0; |
| 481 | } |
| 482 | |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 483 | int tls1_get_shared_curve(SSL *s) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 484 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 485 | const uint16_t *pref, *supp; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 486 | size_t preflen, supplen, i, j; |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 487 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 488 | /* Can't do anything on client side */ |
| 489 | if (s->server == 0) |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 490 | return NID_undef; |
| 491 | |
David Benjamin | 335d10d | 2014-08-06 19:56:33 -0400 | [diff] [blame] | 492 | /* Return first preference shared curve */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 493 | tls1_get_curvelist(s, !!(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), |
| 494 | &supp, &supplen); |
| 495 | tls1_get_curvelist(s, !(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE), |
| 496 | &pref, &preflen); |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 497 | for (i = 0; i < preflen; i++) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 498 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 499 | for (j = 0; j < supplen; j++) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 500 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 501 | if (pref[i] == supp[j]) |
| 502 | return tls1_ec_curve_id2nid(pref[i]); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 505 | return NID_undef; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 506 | } |
| 507 | |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 508 | /* NOTE: tls1_ec_curve_id2nid and tls1_set_curves assume that |
| 509 | * |
| 510 | * (a) 0 is not a valid curve ID. |
| 511 | * |
| 512 | * (b) The largest curve ID is 31. |
| 513 | * |
| 514 | * Those implementations must be revised before adding support for curve IDs |
| 515 | * that break these assumptions. */ |
| 516 | OPENSSL_COMPILE_ASSERT( |
| 517 | (sizeof(nid_list) / sizeof(nid_list[0])) < 32, small_curve_ids); |
| 518 | |
| 519 | int tls1_set_curves(uint16_t **out_curve_ids, size_t *out_curve_ids_len, |
| 520 | const int *curves, size_t ncurves) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 521 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 522 | uint16_t *curve_ids; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 523 | size_t i; |
| 524 | /* Bitmap of curves included to detect duplicates: only works |
| 525 | * while curve ids < 32 |
| 526 | */ |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 527 | uint32_t dup_list = 0; |
| 528 | curve_ids = (uint16_t*)OPENSSL_malloc(ncurves * sizeof(uint16_t)); |
| 529 | if (!curve_ids) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 530 | return 0; |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 531 | for (i = 0; i < ncurves; i++) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 532 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 533 | uint32_t idmask; |
| 534 | uint16_t id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 535 | id = tls1_ec_nid2curve_id(curves[i]); |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 536 | idmask = ((uint32_t)1) << id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 537 | if (!id || (dup_list & idmask)) |
| 538 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 539 | OPENSSL_free(curve_ids); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 540 | return 0; |
| 541 | } |
| 542 | dup_list |= idmask; |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 543 | curve_ids[i] = id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 544 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 545 | if (*out_curve_ids) |
| 546 | OPENSSL_free(*out_curve_ids); |
| 547 | *out_curve_ids = curve_ids; |
| 548 | *out_curve_ids_len = ncurves; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 549 | return 1; |
| 550 | } |
| 551 | |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 552 | /* tls1_curve_params_from_ec_key sets |*out_curve_id| and |*out_comp_id| to the |
| 553 | * TLS curve ID and point format, respectively, for |ec|. It returns one on |
| 554 | * success and zero on failure. */ |
| 555 | static int tls1_curve_params_from_ec_key(uint16_t *out_curve_id, uint8_t *out_comp_id, EC_KEY *ec) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 556 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 557 | int nid; |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 558 | uint16_t id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 559 | const EC_GROUP *grp; |
| 560 | if (!ec) |
| 561 | return 0; |
| 562 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 563 | grp = EC_KEY_get0_group(ec); |
| 564 | if (!grp) |
| 565 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 566 | |
| 567 | /* Determine curve ID */ |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 568 | nid = EC_GROUP_get_curve_name(grp); |
| 569 | id = tls1_ec_nid2curve_id(nid); |
| 570 | if (!id) |
| 571 | return 0; |
| 572 | |
| 573 | /* Set the named curve ID. Arbitrary explicit curves are not |
| 574 | * supported. */ |
| 575 | *out_curve_id = id; |
| 576 | |
| 577 | if (out_comp_id) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 578 | { |
| 579 | if (EC_KEY_get0_public_key(ec) == NULL) |
| 580 | return 0; |
| 581 | if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 582 | *out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 583 | else |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 584 | *out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 585 | } |
| 586 | return 1; |
| 587 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 588 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 589 | /* Check an EC key is compatible with extensions */ |
| 590 | static int tls1_check_ec_key(SSL *s, |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 591 | const uint16_t *curve_id, const uint8_t *comp_id) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 592 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 593 | const uint16_t *curves; |
| 594 | size_t curves_len, i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 595 | int j; |
| 596 | /* If point formats extension present check it, otherwise everything |
| 597 | * is supported (see RFC4492). |
| 598 | */ |
| 599 | if (comp_id && s->session->tlsext_ecpointformatlist) |
| 600 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 601 | uint8_t *p = s->session->tlsext_ecpointformatlist; |
| 602 | size_t plen = s->session->tlsext_ecpointformatlist_length; |
| 603 | for (i = 0; i < plen; i++) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 604 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 605 | if (*comp_id == p[i]) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 606 | break; |
| 607 | } |
| 608 | if (i == plen) |
| 609 | return 0; |
| 610 | } |
| 611 | if (!curve_id) |
| 612 | return 1; |
| 613 | /* Check curve is consistent with client and server preferences */ |
| 614 | for (j = 0; j <= 1; j++) |
| 615 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 616 | tls1_get_curvelist(s, j, &curves, &curves_len); |
| 617 | for (i = 0; i < curves_len; i++) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 618 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 619 | if (curves[i] == *curve_id) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 620 | break; |
| 621 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 622 | if (i == curves_len) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 623 | return 0; |
| 624 | /* For clients can only check sent curve list */ |
| 625 | if (!s->server) |
| 626 | return 1; |
| 627 | } |
| 628 | return 1; |
| 629 | } |
| 630 | |
| 631 | static void tls1_get_formatlist(SSL *s, const unsigned char **pformats, |
| 632 | size_t *pformatslen) |
| 633 | { |
| 634 | /* If we have a custom point format list use it otherwise |
| 635 | * use default */ |
| 636 | if (s->tlsext_ecpointformatlist) |
| 637 | { |
| 638 | *pformats = s->tlsext_ecpointformatlist; |
| 639 | *pformatslen = s->tlsext_ecpointformatlist_length; |
| 640 | } |
| 641 | else |
| 642 | { |
| 643 | *pformats = ecformats_default; |
David Benjamin | 335d10d | 2014-08-06 19:56:33 -0400 | [diff] [blame] | 644 | *pformatslen = sizeof(ecformats_default); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
| 648 | /* Check cert parameters compatible with extensions: currently just checks |
| 649 | * EC certificates have compatible curves and compression. |
| 650 | */ |
| 651 | static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md) |
| 652 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 653 | uint8_t comp_id; |
| 654 | uint16_t curve_id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 655 | EVP_PKEY *pkey; |
| 656 | int rv; |
| 657 | pkey = X509_get_pubkey(x); |
| 658 | if (!pkey) |
| 659 | return 0; |
| 660 | /* If not EC nothing to do */ |
| 661 | if (pkey->type != EVP_PKEY_EC) |
| 662 | { |
| 663 | EVP_PKEY_free(pkey); |
| 664 | return 1; |
| 665 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 666 | rv = tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 667 | EVP_PKEY_free(pkey); |
| 668 | if (!rv) |
| 669 | return 0; |
| 670 | /* Can't check curve_id for client certs as we don't have a |
| 671 | * supported curves extension. |
| 672 | */ |
David Benjamin | 335d10d | 2014-08-06 19:56:33 -0400 | [diff] [blame] | 673 | return tls1_check_ec_key(s, s->server ? &curve_id : NULL, &comp_id); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 674 | } |
| 675 | /* Check EC temporary key is compatible with client extensions */ |
| 676 | int tls1_check_ec_tmp_key(SSL *s, unsigned long cid) |
| 677 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 678 | uint16_t curve_id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 679 | EC_KEY *ec = s->cert->ecdh_tmp; |
| 680 | #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL |
| 681 | /* Allow any curve: not just those peer supports */ |
| 682 | if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) |
| 683 | return 1; |
| 684 | #endif |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 685 | if (s->cert->ecdh_tmp_auto) |
| 686 | { |
| 687 | /* Need a shared curve */ |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 688 | return tls1_get_shared_curve(s) != NID_undef; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 689 | } |
| 690 | if (!ec) |
| 691 | { |
| 692 | if (s->cert->ecdh_tmp_cb) |
| 693 | return 1; |
| 694 | else |
| 695 | return 0; |
| 696 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 697 | if (!tls1_curve_params_from_ec_key(&curve_id, NULL, ec)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 698 | return 0; |
| 699 | /* Set this to allow use of invalid curves for testing */ |
| 700 | #if 0 |
| 701 | return 1; |
| 702 | #else |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 703 | return tls1_check_ec_key(s, &curve_id, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 704 | #endif |
| 705 | } |
| 706 | |
| 707 | #else |
| 708 | |
| 709 | static int tls1_check_cert_param(SSL *s, X509 *x, int set_ee_md) |
| 710 | { |
| 711 | return 1; |
| 712 | } |
| 713 | |
| 714 | #endif /* OPENSSL_NO_EC */ |
| 715 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 716 | |
| 717 | /* List of supported signature algorithms and hashes. Should make this |
| 718 | * customisable at some point, for now include everything we support. |
| 719 | */ |
| 720 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 721 | #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 722 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 723 | #ifdef OPENSSL_NO_ECDSA |
| 724 | #define tlsext_sigalg_ecdsa(md) /* */ |
| 725 | #else |
| 726 | #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa, |
| 727 | #endif |
| 728 | |
| 729 | #define tlsext_sigalg(md) \ |
| 730 | tlsext_sigalg_rsa(md) \ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 731 | tlsext_sigalg_ecdsa(md) |
| 732 | |
David Benjamin | cff6472 | 2014-08-19 19:54:46 -0400 | [diff] [blame] | 733 | static const uint8_t tls12_sigalgs[] = { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 734 | tlsext_sigalg(TLSEXT_hash_sha512) |
| 735 | tlsext_sigalg(TLSEXT_hash_sha384) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 736 | tlsext_sigalg(TLSEXT_hash_sha256) |
| 737 | tlsext_sigalg(TLSEXT_hash_sha224) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 738 | #ifndef OPENSSL_NO_SHA |
| 739 | tlsext_sigalg(TLSEXT_hash_sha1) |
| 740 | #endif |
| 741 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 742 | size_t tls12_get_psigalgs(SSL *s, const unsigned char **psigs) |
| 743 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 744 | /* If server use client authentication sigalgs if not NULL */ |
| 745 | if (s->server && s->cert->client_sigalgs) |
| 746 | { |
| 747 | *psigs = s->cert->client_sigalgs; |
| 748 | return s->cert->client_sigalgslen; |
| 749 | } |
| 750 | else if (s->cert->conf_sigalgs) |
| 751 | { |
| 752 | *psigs = s->cert->conf_sigalgs; |
| 753 | return s->cert->conf_sigalgslen; |
| 754 | } |
| 755 | else |
| 756 | { |
| 757 | *psigs = tls12_sigalgs; |
| 758 | return sizeof(tls12_sigalgs); |
| 759 | } |
| 760 | } |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 761 | |
| 762 | /* tls12_check_peer_sigalg parses a SignatureAndHashAlgorithm out of |
| 763 | * |cbs|. It checks it is consistent with |s|'s sent supported |
| 764 | * signature algorithms and, if so, writes the relevant digest into |
| 765 | * |*out_md| and returns 1. Otherwise it returns 0 and writes an alert |
| 766 | * into |*out_alert|. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 767 | */ |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 768 | int tls12_check_peer_sigalg(const EVP_MD **out_md, int *out_alert, |
| 769 | SSL *s, CBS *cbs, EVP_PKEY *pkey) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 770 | { |
| 771 | const unsigned char *sent_sigs; |
| 772 | size_t sent_sigslen, i; |
| 773 | int sigalg = tls12_get_sigid(pkey); |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 774 | uint8_t hash, signature; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 775 | /* Should never happen */ |
| 776 | if (sigalg == -1) |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 777 | { |
| 778 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, ERR_R_INTERNAL_ERROR); |
| 779 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 780 | return 0; |
| 781 | } |
| 782 | if (!CBS_get_u8(cbs, &hash) || |
| 783 | !CBS_get_u8(cbs, &signature)) |
| 784 | { |
| 785 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_DECODE_ERROR); |
| 786 | *out_alert = SSL_AD_DECODE_ERROR; |
| 787 | return 0; |
| 788 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 789 | /* Check key type is consistent with signature */ |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 790 | if (sigalg != signature) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 791 | { |
| 792 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE); |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 793 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 794 | return 0; |
| 795 | } |
| 796 | #ifndef OPENSSL_NO_EC |
| 797 | if (pkey->type == EVP_PKEY_EC) |
| 798 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 799 | uint16_t curve_id; |
| 800 | uint8_t comp_id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 801 | /* Check compression and curve matches extensions */ |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 802 | if (!tls1_curve_params_from_ec_key(&curve_id, &comp_id, pkey->pkey.ec)) |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 803 | { |
| 804 | *out_alert = SSL_AD_INTERNAL_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 805 | return 0; |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 806 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 807 | if (!s->server && !tls1_check_ec_key(s, &curve_id, &comp_id)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 808 | { |
| 809 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_CURVE); |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 810 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 811 | return 0; |
| 812 | } |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 813 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 814 | #endif |
| 815 | |
| 816 | /* Check signature matches a type we sent */ |
| 817 | sent_sigslen = tls12_get_psigalgs(s, &sent_sigs); |
| 818 | for (i = 0; i < sent_sigslen; i+=2, sent_sigs+=2) |
| 819 | { |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 820 | if (hash == sent_sigs[0] && signature == sent_sigs[1]) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 821 | break; |
| 822 | } |
| 823 | /* Allow fallback to SHA1 if not strict mode */ |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 824 | if (i == sent_sigslen && (hash != TLSEXT_hash_sha1 || s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 825 | { |
| 826 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_WRONG_SIGNATURE_TYPE); |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 827 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 828 | return 0; |
| 829 | } |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 830 | *out_md = tls12_get_hash(hash); |
| 831 | if (*out_md == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 832 | { |
| 833 | OPENSSL_PUT_ERROR(SSL, tls12_check_peer_sigalg, SSL_R_UNKNOWN_DIGEST); |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 834 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 835 | return 0; |
| 836 | } |
| 837 | /* Store the digest used so applications can retrieve it if they |
| 838 | * wish. |
| 839 | */ |
| 840 | if (s->session && s->session->sess_cert) |
David Benjamin | 05da6e1 | 2014-07-12 20:42:55 -0400 | [diff] [blame] | 841 | s->session->sess_cert->peer_key->digest = *out_md; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 842 | return 1; |
| 843 | } |
| 844 | /* Get a mask of disabled algorithms: an algorithm is disabled |
| 845 | * if it isn't supported or doesn't appear in supported signature |
| 846 | * algorithms. Unlike ssl_cipher_get_disabled this applies to a specific |
| 847 | * session and not global settings. |
| 848 | * |
| 849 | */ |
| 850 | void ssl_set_client_disabled(SSL *s) |
| 851 | { |
| 852 | CERT *c = s->cert; |
| 853 | const unsigned char *sigalgs; |
| 854 | size_t i, sigalgslen; |
David Benjamin | ef2116d | 2014-08-19 20:21:56 -0400 | [diff] [blame] | 855 | int have_rsa = 0, have_ecdsa = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 856 | c->mask_a = 0; |
| 857 | c->mask_k = 0; |
| 858 | /* Don't allow TLS 1.2 only ciphers if we don't suppport them */ |
| 859 | if (!SSL_CLIENT_USE_TLS1_2_CIPHERS(s)) |
| 860 | c->mask_ssl = SSL_TLSV1_2; |
| 861 | else |
| 862 | c->mask_ssl = 0; |
| 863 | /* Now go through all signature algorithms seeing if we support |
| 864 | * any for RSA, DSA, ECDSA. Do this for all versions not just |
| 865 | * TLS 1.2. |
| 866 | */ |
| 867 | sigalgslen = tls12_get_psigalgs(s, &sigalgs); |
| 868 | for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) |
| 869 | { |
| 870 | switch(sigalgs[1]) |
| 871 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 872 | case TLSEXT_signature_rsa: |
| 873 | have_rsa = 1; |
| 874 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 875 | #ifndef OPENSSL_NO_ECDSA |
| 876 | case TLSEXT_signature_ecdsa: |
| 877 | have_ecdsa = 1; |
| 878 | break; |
| 879 | #endif |
| 880 | } |
| 881 | } |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 882 | /* Disable auth if we don't include any appropriate signature |
| 883 | * algorithms. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 884 | */ |
| 885 | if (!have_rsa) |
| 886 | { |
| 887 | c->mask_a |= SSL_aRSA; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 888 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 889 | if (!have_ecdsa) |
| 890 | { |
| 891 | c->mask_a |= SSL_aECDSA; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 892 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 893 | /* with PSK there must be client callback set */ |
| 894 | if (!s->psk_client_callback) |
| 895 | { |
| 896 | c->mask_a |= SSL_aPSK; |
| 897 | c->mask_k |= SSL_kPSK; |
| 898 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 899 | c->valid = 1; |
| 900 | } |
| 901 | |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 902 | /* header_len is the length of the ClientHello header written so far, used to |
| 903 | * compute padding. It does not include the record header. Pass 0 if no padding |
| 904 | * is to be done. */ |
| 905 | unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit, size_t header_len) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 906 | { |
| 907 | int extdatalen=0; |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 908 | unsigned char *ret = buf; |
| 909 | unsigned char *orig = buf; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 910 | #ifndef OPENSSL_NO_EC |
| 911 | /* See if we support any ECC ciphersuites */ |
| 912 | int using_ecc = 0; |
| 913 | if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) |
| 914 | { |
| 915 | int i; |
| 916 | unsigned long alg_k, alg_a; |
| 917 | STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s); |
| 918 | |
| 919 | for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) |
| 920 | { |
David Benjamin | 6f26001 | 2014-08-15 13:49:12 -0400 | [diff] [blame] | 921 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 922 | |
| 923 | alg_k = c->algorithm_mkey; |
| 924 | alg_a = c->algorithm_auth; |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 925 | if ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 926 | { |
| 927 | using_ecc = 1; |
| 928 | break; |
| 929 | } |
| 930 | } |
| 931 | } |
| 932 | #endif |
| 933 | |
| 934 | /* don't add extensions for SSLv3 unless doing secure renegotiation */ |
| 935 | if (s->client_version == SSL3_VERSION |
| 936 | && !s->s3->send_connection_binding) |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 937 | return orig; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 938 | |
| 939 | ret+=2; |
| 940 | |
| 941 | if (ret>=limit) return NULL; /* this really never occurs, but ... */ |
| 942 | |
| 943 | if (s->tlsext_hostname != NULL) |
| 944 | { |
| 945 | /* Add TLS extension servername to the Client Hello message */ |
| 946 | unsigned long size_str; |
| 947 | long lenmax; |
| 948 | |
| 949 | /* check for enough space. |
| 950 | 4 for the servername type and entension length |
| 951 | 2 for servernamelist length |
| 952 | 1 for the hostname type |
| 953 | 2 for hostname length |
| 954 | + hostname length |
| 955 | */ |
| 956 | |
| 957 | if ((lenmax = limit - ret - 9) < 0 |
| 958 | || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax) |
| 959 | return NULL; |
| 960 | |
| 961 | /* extension type and length */ |
| 962 | s2n(TLSEXT_TYPE_server_name,ret); |
| 963 | s2n(size_str+5,ret); |
| 964 | |
| 965 | /* length of servername list */ |
| 966 | s2n(size_str+3,ret); |
| 967 | |
| 968 | /* hostname type, length and hostname */ |
| 969 | *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name; |
| 970 | s2n(size_str,ret); |
| 971 | memcpy(ret, s->tlsext_hostname, size_str); |
| 972 | ret+=size_str; |
| 973 | } |
| 974 | |
| 975 | /* Add RI if renegotiating */ |
| 976 | if (s->renegotiate) |
| 977 | { |
| 978 | int el; |
| 979 | |
| 980 | if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) |
| 981 | { |
| 982 | OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR); |
| 983 | return NULL; |
| 984 | } |
| 985 | |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 986 | if((limit - ret - 4 - el) < 0) return NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 987 | |
| 988 | s2n(TLSEXT_TYPE_renegotiate,ret); |
| 989 | s2n(el,ret); |
| 990 | |
| 991 | if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) |
| 992 | { |
| 993 | OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR); |
| 994 | return NULL; |
| 995 | } |
| 996 | |
| 997 | ret += el; |
| 998 | } |
| 999 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1000 | if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) |
| 1001 | { |
| 1002 | int ticklen; |
| 1003 | if (!s->new_session && s->session && s->session->tlsext_tick) |
| 1004 | ticklen = s->session->tlsext_ticklen; |
| 1005 | else if (s->session && s->tlsext_session_ticket && |
| 1006 | s->tlsext_session_ticket->data) |
| 1007 | { |
David Benjamin | 072c953 | 2014-07-26 11:44:25 -0400 | [diff] [blame] | 1008 | s->session->tlsext_tick = BUF_memdup( |
| 1009 | s->tlsext_session_ticket->data, |
| 1010 | s->tlsext_session_ticket->length); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1011 | if (!s->session->tlsext_tick) |
| 1012 | return NULL; |
David Benjamin | 072c953 | 2014-07-26 11:44:25 -0400 | [diff] [blame] | 1013 | ticklen = s->tlsext_session_ticket->length; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1014 | s->session->tlsext_ticklen = ticklen; |
| 1015 | } |
| 1016 | else |
| 1017 | ticklen = 0; |
| 1018 | if (ticklen == 0 && s->tlsext_session_ticket && |
| 1019 | s->tlsext_session_ticket->data == NULL) |
| 1020 | goto skip_ext; |
| 1021 | /* Check for enough room 2 for extension type, 2 for len |
| 1022 | * rest for ticket |
| 1023 | */ |
| 1024 | if ((long)(limit - ret - 4 - ticklen) < 0) return NULL; |
| 1025 | s2n(TLSEXT_TYPE_session_ticket,ret); |
| 1026 | s2n(ticklen,ret); |
| 1027 | if (ticklen) |
| 1028 | { |
| 1029 | memcpy(ret, s->session->tlsext_tick, ticklen); |
| 1030 | ret += ticklen; |
| 1031 | } |
| 1032 | } |
| 1033 | skip_ext: |
| 1034 | |
| 1035 | if (SSL_USE_SIGALGS(s)) |
| 1036 | { |
| 1037 | size_t salglen; |
| 1038 | const unsigned char *salg; |
| 1039 | salglen = tls12_get_psigalgs(s, &salg); |
| 1040 | if ((size_t)(limit - ret) < salglen + 6) |
| 1041 | return NULL; |
| 1042 | s2n(TLSEXT_TYPE_signature_algorithms,ret); |
| 1043 | s2n(salglen + 2, ret); |
| 1044 | s2n(salglen, ret); |
| 1045 | memcpy(ret, salg, salglen); |
| 1046 | ret += salglen; |
| 1047 | } |
| 1048 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1049 | /* TODO(fork): we probably want OCSP stapling, but it currently pulls in a lot of code. */ |
| 1050 | #if 0 |
| 1051 | if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) |
| 1052 | { |
| 1053 | int i; |
| 1054 | long extlen, idlen, itmp; |
| 1055 | OCSP_RESPID *id; |
| 1056 | |
| 1057 | idlen = 0; |
| 1058 | for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) |
| 1059 | { |
| 1060 | id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i); |
| 1061 | itmp = i2d_OCSP_RESPID(id, NULL); |
| 1062 | if (itmp <= 0) |
| 1063 | return NULL; |
| 1064 | idlen += itmp + 2; |
| 1065 | } |
| 1066 | |
| 1067 | if (s->tlsext_ocsp_exts) |
| 1068 | { |
| 1069 | extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL); |
| 1070 | if (extlen < 0) |
| 1071 | return NULL; |
| 1072 | } |
| 1073 | else |
| 1074 | extlen = 0; |
| 1075 | |
| 1076 | if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL; |
| 1077 | s2n(TLSEXT_TYPE_status_request, ret); |
| 1078 | if (extlen + idlen > 0xFFF0) |
| 1079 | return NULL; |
| 1080 | s2n(extlen + idlen + 5, ret); |
| 1081 | *(ret++) = TLSEXT_STATUSTYPE_ocsp; |
| 1082 | s2n(idlen, ret); |
| 1083 | for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) |
| 1084 | { |
| 1085 | /* save position of id len */ |
| 1086 | unsigned char *q = ret; |
| 1087 | id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i); |
| 1088 | /* skip over id len */ |
| 1089 | ret += 2; |
| 1090 | itmp = i2d_OCSP_RESPID(id, &ret); |
| 1091 | /* write id len */ |
| 1092 | s2n(itmp, q); |
| 1093 | } |
| 1094 | s2n(extlen, ret); |
| 1095 | if (extlen > 0) |
| 1096 | i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret); |
| 1097 | } |
| 1098 | #endif |
| 1099 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1100 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 1101 | if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) |
| 1102 | { |
| 1103 | /* The client advertises an emtpy extension to indicate its |
| 1104 | * support for Next Protocol Negotiation */ |
| 1105 | if (limit - ret - 4 < 0) |
| 1106 | return NULL; |
| 1107 | s2n(TLSEXT_TYPE_next_proto_neg,ret); |
| 1108 | s2n(0,ret); |
| 1109 | } |
| 1110 | #endif |
| 1111 | |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame^] | 1112 | if (s->signed_cert_timestamps_enabled && !s->s3->tmp.finish_md_len) |
| 1113 | { |
| 1114 | /* The client advertises an empty extension to indicate its support for |
| 1115 | * certificate timestamps. */ |
| 1116 | if (limit - ret - 4 < 0) |
| 1117 | return NULL; |
| 1118 | s2n(TLSEXT_TYPE_certificate_timestamp,ret); |
| 1119 | s2n(0,ret); |
| 1120 | } |
| 1121 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1122 | if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) |
| 1123 | { |
| 1124 | if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) |
| 1125 | return NULL; |
| 1126 | s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret); |
| 1127 | s2n(2 + s->alpn_client_proto_list_len,ret); |
| 1128 | s2n(s->alpn_client_proto_list_len,ret); |
| 1129 | memcpy(ret, s->alpn_client_proto_list, |
| 1130 | s->alpn_client_proto_list_len); |
| 1131 | ret += s->alpn_client_proto_list_len; |
| 1132 | } |
| 1133 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1134 | if (s->tlsext_channel_id_enabled) |
| 1135 | { |
| 1136 | /* The client advertises an emtpy extension to indicate its |
| 1137 | * support for Channel ID. */ |
| 1138 | if (limit - ret - 4 < 0) |
| 1139 | return NULL; |
| 1140 | if (s->ctx->tlsext_channel_id_enabled_new) |
| 1141 | s2n(TLSEXT_TYPE_channel_id_new,ret); |
| 1142 | else |
| 1143 | s2n(TLSEXT_TYPE_channel_id,ret); |
| 1144 | s2n(0,ret); |
| 1145 | } |
| 1146 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1147 | if(SSL_get_srtp_profiles(s)) |
| 1148 | { |
| 1149 | int el; |
| 1150 | |
| 1151 | ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0); |
| 1152 | |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1153 | if((limit - ret - 4 - el) < 0) return NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1154 | |
| 1155 | s2n(TLSEXT_TYPE_use_srtp,ret); |
| 1156 | s2n(el,ret); |
| 1157 | |
| 1158 | if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) |
| 1159 | { |
| 1160 | OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1161 | return NULL; |
| 1162 | } |
| 1163 | ret += el; |
| 1164 | } |
| 1165 | |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1166 | #ifndef OPENSSL_NO_EC |
| 1167 | if (using_ecc) |
| 1168 | { |
| 1169 | /* Add TLS extension ECPointFormats to the ClientHello message */ |
| 1170 | long lenmax; |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 1171 | const uint8_t *formats; |
| 1172 | const uint16_t *curves; |
| 1173 | size_t formats_len, curves_len, i; |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1174 | |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 1175 | tls1_get_formatlist(s, &formats, &formats_len); |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1176 | |
| 1177 | if ((lenmax = limit - ret - 5) < 0) return NULL; |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 1178 | if (formats_len > (size_t)lenmax) return NULL; |
| 1179 | if (formats_len > 255) |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1180 | { |
| 1181 | OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1182 | return NULL; |
| 1183 | } |
| 1184 | |
| 1185 | s2n(TLSEXT_TYPE_ec_point_formats,ret); |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 1186 | s2n(formats_len + 1,ret); |
| 1187 | *(ret++) = (unsigned char)formats_len; |
| 1188 | memcpy(ret, formats, formats_len); |
| 1189 | ret+=formats_len; |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1190 | |
| 1191 | /* Add TLS extension EllipticCurves to the ClientHello message */ |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 1192 | tls1_get_curvelist(s, 0, &curves, &curves_len); |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1193 | |
| 1194 | if ((lenmax = limit - ret - 6) < 0) return NULL; |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 1195 | if ((curves_len * 2) > (size_t)lenmax) return NULL; |
| 1196 | if ((curves_len * 2) > 65532) |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1197 | { |
| 1198 | OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1199 | return NULL; |
| 1200 | } |
| 1201 | |
| 1202 | s2n(TLSEXT_TYPE_elliptic_curves,ret); |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 1203 | s2n((curves_len * 2) + 2, ret); |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1204 | |
| 1205 | /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for |
| 1206 | * elliptic_curve_list, but the examples use two bytes. |
| 1207 | * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html |
| 1208 | * resolves this to two bytes. |
| 1209 | */ |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 1210 | s2n(curves_len * 2, ret); |
| 1211 | for (i = 0; i < curves_len; i++) |
| 1212 | { |
| 1213 | s2n(curves[i], ret); |
| 1214 | } |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1215 | } |
| 1216 | #endif /* OPENSSL_NO_EC */ |
| 1217 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1218 | #ifdef TLSEXT_TYPE_padding |
| 1219 | /* Add padding to workaround bugs in F5 terminators. |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1220 | * See https://tools.ietf.org/html/draft-agl-tls-padding-03 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1221 | * |
| 1222 | * NB: because this code works out the length of all existing |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1223 | * extensions it MUST always appear last. */ |
| 1224 | if (header_len > 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1225 | { |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1226 | header_len += ret - orig; |
| 1227 | if (header_len > 0xff && header_len < 0x200) |
| 1228 | { |
| 1229 | size_t padding_len = 0x200 - header_len; |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1230 | /* Extensions take at least four bytes to encode. Always |
| 1231 | * include least one byte of data if including the |
| 1232 | * extension. WebSphere Application Server 7.0 is |
| 1233 | * intolerant to the last extension being zero-length. */ |
| 1234 | if (padding_len >= 4 + 1) |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1235 | padding_len -= 4; |
| 1236 | else |
Adam Langley | c3174b7 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1237 | padding_len = 1; |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1238 | if (limit - ret - 4 - (long)padding_len < 0) |
| 1239 | return NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1240 | |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1241 | s2n(TLSEXT_TYPE_padding, ret); |
| 1242 | s2n(padding_len, ret); |
| 1243 | memset(ret, 0, padding_len); |
| 1244 | ret += padding_len; |
| 1245 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1246 | } |
| 1247 | #endif |
| 1248 | |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1249 | if ((extdatalen = ret-orig-2)== 0) |
| 1250 | return orig; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1251 | |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1252 | s2n(extdatalen, orig); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1253 | return ret; |
| 1254 | } |
| 1255 | |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1256 | unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *buf, unsigned char *limit) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1257 | { |
| 1258 | int extdatalen=0; |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1259 | unsigned char *orig = buf; |
| 1260 | unsigned char *ret = buf; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1261 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 1262 | int next_proto_neg_seen; |
| 1263 | #endif |
| 1264 | #ifndef OPENSSL_NO_EC |
| 1265 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 1266 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 1267 | int using_ecc = (alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1268 | using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL); |
| 1269 | #endif |
| 1270 | /* don't add extensions for SSLv3, unless doing secure renegotiation */ |
| 1271 | if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1272 | return orig; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1273 | |
| 1274 | ret+=2; |
| 1275 | if (ret>=limit) return NULL; /* this really never occurs, but ... */ |
| 1276 | |
| 1277 | if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL) |
| 1278 | { |
| 1279 | if ((long)(limit - ret - 4) < 0) return NULL; |
| 1280 | |
| 1281 | s2n(TLSEXT_TYPE_server_name,ret); |
| 1282 | s2n(0,ret); |
| 1283 | } |
| 1284 | |
| 1285 | if(s->s3->send_connection_binding) |
| 1286 | { |
| 1287 | int el; |
| 1288 | |
| 1289 | if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) |
| 1290 | { |
| 1291 | OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1292 | return NULL; |
| 1293 | } |
| 1294 | |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1295 | if((limit - ret - 4 - el) < 0) return NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1296 | |
| 1297 | s2n(TLSEXT_TYPE_renegotiate,ret); |
| 1298 | s2n(el,ret); |
| 1299 | |
| 1300 | if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) |
| 1301 | { |
| 1302 | OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1303 | return NULL; |
| 1304 | } |
| 1305 | |
| 1306 | ret += el; |
| 1307 | } |
| 1308 | |
| 1309 | #ifndef OPENSSL_NO_EC |
| 1310 | if (using_ecc) |
| 1311 | { |
| 1312 | const unsigned char *plist; |
| 1313 | size_t plistlen; |
| 1314 | /* Add TLS extension ECPointFormats to the ServerHello message */ |
| 1315 | long lenmax; |
| 1316 | |
| 1317 | tls1_get_formatlist(s, &plist, &plistlen); |
| 1318 | |
| 1319 | if ((lenmax = limit - ret - 5) < 0) return NULL; |
| 1320 | if (plistlen > (size_t)lenmax) return NULL; |
| 1321 | if (plistlen > 255) |
| 1322 | { |
| 1323 | OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1324 | return NULL; |
| 1325 | } |
| 1326 | |
| 1327 | s2n(TLSEXT_TYPE_ec_point_formats,ret); |
| 1328 | s2n(plistlen + 1,ret); |
| 1329 | *(ret++) = (unsigned char) plistlen; |
| 1330 | memcpy(ret, plist, plistlen); |
| 1331 | ret+=plistlen; |
| 1332 | |
| 1333 | } |
| 1334 | /* Currently the server should not respond with a SupportedCurves extension */ |
| 1335 | #endif /* OPENSSL_NO_EC */ |
| 1336 | |
| 1337 | if (s->tlsext_ticket_expected |
| 1338 | && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) |
| 1339 | { |
| 1340 | if ((long)(limit - ret - 4) < 0) return NULL; |
| 1341 | s2n(TLSEXT_TYPE_session_ticket,ret); |
| 1342 | s2n(0,ret); |
| 1343 | } |
| 1344 | |
| 1345 | if (s->tlsext_status_expected) |
| 1346 | { |
| 1347 | if ((long)(limit - ret - 4) < 0) return NULL; |
| 1348 | s2n(TLSEXT_TYPE_status_request,ret); |
| 1349 | s2n(0,ret); |
| 1350 | } |
| 1351 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1352 | if(s->srtp_profile) |
| 1353 | { |
| 1354 | int el; |
| 1355 | |
| 1356 | ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0); |
| 1357 | |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1358 | if((limit - ret - 4 - el) < 0) return NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1359 | |
| 1360 | s2n(TLSEXT_TYPE_use_srtp,ret); |
| 1361 | s2n(el,ret); |
| 1362 | |
| 1363 | if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) |
| 1364 | { |
| 1365 | OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR); |
| 1366 | return NULL; |
| 1367 | } |
| 1368 | ret+=el; |
| 1369 | } |
| 1370 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1371 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 1372 | next_proto_neg_seen = s->s3->next_proto_neg_seen; |
| 1373 | s->s3->next_proto_neg_seen = 0; |
| 1374 | if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) |
| 1375 | { |
| 1376 | const unsigned char *npa; |
| 1377 | unsigned int npalen; |
| 1378 | int r; |
| 1379 | |
| 1380 | r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg); |
| 1381 | if (r == SSL_TLSEXT_ERR_OK) |
| 1382 | { |
| 1383 | if ((long)(limit - ret - 4 - npalen) < 0) return NULL; |
| 1384 | s2n(TLSEXT_TYPE_next_proto_neg,ret); |
| 1385 | s2n(npalen,ret); |
| 1386 | memcpy(ret, npa, npalen); |
| 1387 | ret += npalen; |
| 1388 | s->s3->next_proto_neg_seen = 1; |
| 1389 | } |
| 1390 | } |
| 1391 | #endif |
| 1392 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1393 | if (s->s3->alpn_selected) |
| 1394 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 1395 | const uint8_t *selected = s->s3->alpn_selected; |
| 1396 | size_t len = s->s3->alpn_selected_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1397 | |
| 1398 | if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) |
| 1399 | return NULL; |
| 1400 | s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret); |
| 1401 | s2n(3 + len,ret); |
| 1402 | s2n(1 + len,ret); |
| 1403 | *ret++ = len; |
| 1404 | memcpy(ret, selected, len); |
| 1405 | ret += len; |
| 1406 | } |
| 1407 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1408 | /* If the client advertised support for Channel ID, and we have it |
| 1409 | * enabled, then we want to echo it back. */ |
| 1410 | if (s->s3->tlsext_channel_id_valid) |
| 1411 | { |
| 1412 | if (limit - ret - 4 < 0) |
| 1413 | return NULL; |
| 1414 | if (s->s3->tlsext_channel_id_new) |
| 1415 | s2n(TLSEXT_TYPE_channel_id_new,ret); |
| 1416 | else |
| 1417 | s2n(TLSEXT_TYPE_channel_id,ret); |
| 1418 | s2n(0,ret); |
| 1419 | } |
| 1420 | |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1421 | if ((extdatalen = ret-orig-2) == 0) |
| 1422 | return orig; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1423 | |
Adam Langley | b0c235e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1424 | s2n(extdatalen, orig); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1425 | return ret; |
| 1426 | } |
| 1427 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1428 | /* tls1_alpn_handle_client_hello is called to process the ALPN extension in a |
| 1429 | * ClientHello. |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1430 | * cbs: the contents of the extension, not including the type and length. |
| 1431 | * 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] | 1432 | * return. |
| 1433 | * |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1434 | * returns: 1 on success. */ |
| 1435 | static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1436 | { |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 1437 | CBS protocol_name_list, protocol_name_list_copy; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1438 | const unsigned char *selected; |
| 1439 | unsigned char selected_len; |
| 1440 | int r; |
| 1441 | |
| 1442 | if (s->ctx->alpn_select_cb == NULL) |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1443 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1444 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1445 | if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) || |
| 1446 | CBS_len(cbs) != 0 || |
| 1447 | CBS_len(&protocol_name_list) < 2) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1448 | goto parse_error; |
| 1449 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1450 | /* Validate the protocol list. */ |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 1451 | protocol_name_list_copy = protocol_name_list; |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1452 | while (CBS_len(&protocol_name_list_copy) > 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1453 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1454 | CBS protocol_name; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1455 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1456 | if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1457 | goto parse_error; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1460 | r = s->ctx->alpn_select_cb(s, &selected, &selected_len, |
| 1461 | CBS_data(&protocol_name_list), CBS_len(&protocol_name_list), |
| 1462 | s->ctx->alpn_select_cb_arg); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1463 | if (r == SSL_TLSEXT_ERR_OK) { |
| 1464 | if (s->s3->alpn_selected) |
| 1465 | OPENSSL_free(s->s3->alpn_selected); |
David Benjamin | 072c953 | 2014-07-26 11:44:25 -0400 | [diff] [blame] | 1466 | s->s3->alpn_selected = BUF_memdup(selected, selected_len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1467 | if (!s->s3->alpn_selected) |
| 1468 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1469 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1470 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1471 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1472 | s->s3->alpn_selected_len = selected_len; |
| 1473 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1474 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1475 | |
| 1476 | parse_error: |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1477 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1478 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1479 | } |
| 1480 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1481 | static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1482 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1483 | int renegotiate_seen = 0; |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1484 | CBS extensions; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1485 | size_t i; |
| 1486 | |
| 1487 | s->servername_done = 0; |
| 1488 | s->tlsext_status_type = -1; |
| 1489 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 1490 | s->s3->next_proto_neg_seen = 0; |
| 1491 | #endif |
| 1492 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1493 | if (s->s3->alpn_selected) |
| 1494 | { |
| 1495 | OPENSSL_free(s->s3->alpn_selected); |
| 1496 | s->s3->alpn_selected = NULL; |
| 1497 | } |
| 1498 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1499 | /* Clear any signature algorithms extension received */ |
| 1500 | if (s->cert->peer_sigalgs) |
| 1501 | { |
| 1502 | OPENSSL_free(s->cert->peer_sigalgs); |
| 1503 | s->cert->peer_sigalgs = NULL; |
| 1504 | } |
| 1505 | /* Clear any shared sigtnature algorithms */ |
| 1506 | if (s->cert->shared_sigalgs) |
| 1507 | { |
| 1508 | OPENSSL_free(s->cert->shared_sigalgs); |
| 1509 | s->cert->shared_sigalgs = NULL; |
| 1510 | } |
| 1511 | /* Clear certificate digests and validity flags */ |
| 1512 | for (i = 0; i < SSL_PKEY_NUM; i++) |
| 1513 | { |
| 1514 | s->cert->pkeys[i].digest = NULL; |
| 1515 | s->cert->pkeys[i].valid_flags = 0; |
| 1516 | } |
| 1517 | |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 1518 | /* TODO(fork): we probably want OCSP stapling support, but this pulls in |
| 1519 | * a lot of code. */ |
| 1520 | #if 0 |
| 1521 | /* Clear OCSP state. */ |
| 1522 | s->tlsext_status_type = -1; |
| 1523 | if (s->tlsext_ocsp_ids) |
| 1524 | { |
| 1525 | sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free); |
| 1526 | s->tlsext_ocsp_ids = NULL; |
| 1527 | } |
| 1528 | if (s->tlsext_ocsp_exts) |
| 1529 | { |
| 1530 | sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, X509_EXTENSION_free); |
| 1531 | s->tlsext_ocsp_exts = NULL; |
| 1532 | } |
| 1533 | #endif |
| 1534 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1535 | /* There may be no extensions. */ |
| 1536 | if (CBS_len(cbs) == 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1537 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1538 | goto ri_check; |
| 1539 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1540 | |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 1541 | /* Decode the extensions block and check it is valid. */ |
| 1542 | if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 1543 | !tls1_check_duplicate_extensions(&extensions)) |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1544 | { |
| 1545 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1546 | return 0; |
| 1547 | } |
| 1548 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1549 | while (CBS_len(&extensions) != 0) |
| 1550 | { |
| 1551 | uint16_t type; |
| 1552 | CBS extension; |
| 1553 | |
| 1554 | /* Decode the next extension. */ |
| 1555 | if (!CBS_get_u16(&extensions, &type) || |
| 1556 | !CBS_get_u16_length_prefixed(&extensions, &extension)) |
| 1557 | { |
| 1558 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1559 | return 0; |
| 1560 | } |
| 1561 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1562 | if (s->tlsext_debug_cb) |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1563 | { |
| 1564 | s->tlsext_debug_cb(s, 0, type, (unsigned char*)CBS_data(&extension), |
| 1565 | CBS_len(&extension), s->tlsext_debug_arg); |
| 1566 | } |
| 1567 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1568 | /* The servername extension is treated as follows: |
| 1569 | |
| 1570 | - Only the hostname type is supported with a maximum length of 255. |
| 1571 | - The servername is rejected if too long or if it contains zeros, |
| 1572 | in which case an fatal alert is generated. |
| 1573 | - The servername field is maintained together with the session cache. |
| 1574 | - When a session is resumed, the servername call back invoked in order |
| 1575 | to allow the application to position itself to the right context. |
| 1576 | - The servername is acknowledged if it is new for a session or when |
| 1577 | it is identical to a previously used for the same session. |
| 1578 | Applications can control the behaviour. They can at any time |
| 1579 | set a 'desirable' servername for a new SSL object. This can be the |
| 1580 | case for example with HTTPS when a Host: header field is received and |
| 1581 | a renegotiation is requested. In this case, a possible servername |
| 1582 | presented in the new client hello is only acknowledged if it matches |
| 1583 | the value of the Host: field. |
| 1584 | - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
| 1585 | if they provide for changing an explicit servername context for the session, |
| 1586 | i.e. when the session has been established with a servername extension. |
| 1587 | - On session reconnect, the servername extension may be absent. |
| 1588 | |
| 1589 | */ |
| 1590 | |
| 1591 | if (type == TLSEXT_TYPE_server_name) |
| 1592 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1593 | CBS server_name_list; |
| 1594 | |
| 1595 | if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) || |
| 1596 | CBS_len(&server_name_list) < 1 || |
| 1597 | CBS_len(&extension) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1598 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1599 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1600 | return 0; |
| 1601 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1602 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1603 | /* Decode each ServerName in the extension. */ |
| 1604 | while (CBS_len(&server_name_list) > 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1605 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1606 | uint8_t name_type; |
| 1607 | CBS host_name; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1608 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1609 | /* Decode the NameType. */ |
| 1610 | if (!CBS_get_u8(&server_name_list, &name_type)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1611 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1612 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1613 | return 0; |
| 1614 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1615 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1616 | /* Only host_name is supported. */ |
| 1617 | if (name_type != TLSEXT_NAMETYPE_host_name) |
| 1618 | continue; |
| 1619 | |
| 1620 | if (!s->hit) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1621 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1622 | if (s->session->tlsext_hostname) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1623 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1624 | /* The ServerNameList MUST NOT |
| 1625 | contain more than one name of |
| 1626 | the same name_type. */ |
| 1627 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1628 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1629 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1630 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1631 | if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) || |
| 1632 | CBS_len(&host_name) < 1) |
| 1633 | { |
| 1634 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1635 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1636 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1637 | |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1638 | if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name || |
| 1639 | CBS_contains_zero_byte(&host_name)) |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1640 | { |
| 1641 | *out_alert = SSL_AD_UNRECOGNIZED_NAME; |
| 1642 | return 0; |
| 1643 | } |
| 1644 | |
| 1645 | /* Copy the hostname as a string. */ |
David Benjamin | ed43958 | 2014-07-14 19:13:02 -0400 | [diff] [blame] | 1646 | if (!CBS_strdup(&host_name, &s->session->tlsext_hostname)) |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1647 | { |
| 1648 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1649 | return 0; |
| 1650 | } |
| 1651 | s->servername_done = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1652 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1653 | else |
| 1654 | { |
| 1655 | s->servername_done = s->session->tlsext_hostname |
| 1656 | && strlen(s->session->tlsext_hostname) == CBS_len(&host_name) |
| 1657 | && strncmp(s->session->tlsext_hostname, |
| 1658 | (char *)CBS_data(&host_name), CBS_len(&host_name)) == 0; |
| 1659 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1660 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | #ifndef OPENSSL_NO_EC |
| 1664 | else if (type == TLSEXT_TYPE_ec_point_formats) |
| 1665 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1666 | CBS ec_point_format_list; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1667 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1668 | if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) || |
| 1669 | CBS_len(&extension) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1670 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1671 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1672 | return 0; |
| 1673 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1674 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1675 | if (!s->hit) |
| 1676 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1677 | if (!CBS_stow(&ec_point_format_list, |
| 1678 | &s->session->tlsext_ecpointformatlist, |
| 1679 | &s->session->tlsext_ecpointformatlist_length)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1680 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1681 | *out_alert = SSL_AD_INTERNAL_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1682 | return 0; |
| 1683 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1684 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1685 | } |
| 1686 | else if (type == TLSEXT_TYPE_elliptic_curves) |
| 1687 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1688 | CBS elliptic_curve_list; |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 1689 | size_t i, num_curves; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1690 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1691 | if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) || |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 1692 | CBS_len(&elliptic_curve_list) == 0 || |
| 1693 | (CBS_len(&elliptic_curve_list) & 1) != 0 || |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1694 | CBS_len(&extension) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1695 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1696 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1697 | return 0; |
| 1698 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1699 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1700 | if (!s->hit) |
| 1701 | { |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 1702 | if (s->session->tlsext_ellipticcurvelist) |
| 1703 | { |
| 1704 | OPENSSL_free(s->session->tlsext_ellipticcurvelist); |
| 1705 | s->session->tlsext_ellipticcurvelist_length = 0; |
| 1706 | } |
| 1707 | s->session->tlsext_ellipticcurvelist = |
| 1708 | (uint16_t*)OPENSSL_malloc(CBS_len(&elliptic_curve_list)); |
| 1709 | if (s->session->tlsext_ellipticcurvelist == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1710 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1711 | *out_alert = SSL_AD_INTERNAL_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1712 | return 0; |
| 1713 | } |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 1714 | num_curves = CBS_len(&elliptic_curve_list) / 2; |
| 1715 | for (i = 0; i < num_curves; i++) |
| 1716 | { |
| 1717 | if (!CBS_get_u16(&elliptic_curve_list, |
| 1718 | &s->session->tlsext_ellipticcurvelist[i])) |
| 1719 | { |
| 1720 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1721 | return 0; |
| 1722 | } |
| 1723 | } |
| 1724 | if (CBS_len(&elliptic_curve_list) != 0) |
| 1725 | { |
| 1726 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1727 | return 0; |
| 1728 | } |
| 1729 | s->session->tlsext_ellipticcurvelist_length = num_curves; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1730 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1731 | } |
| 1732 | #endif /* OPENSSL_NO_EC */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1733 | else if (type == TLSEXT_TYPE_session_ticket) |
| 1734 | { |
| 1735 | if (s->tls_session_ticket_ext_cb && |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1736 | !s->tls_session_ticket_ext_cb(s, CBS_data(&extension), CBS_len(&extension), s->tls_session_ticket_ext_cb_arg)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1737 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1738 | *out_alert = SSL_AD_INTERNAL_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1739 | return 0; |
| 1740 | } |
| 1741 | } |
| 1742 | else if (type == TLSEXT_TYPE_renegotiate) |
| 1743 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1744 | if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1745 | return 0; |
| 1746 | renegotiate_seen = 1; |
| 1747 | } |
| 1748 | else if (type == TLSEXT_TYPE_signature_algorithms) |
| 1749 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1750 | CBS supported_signature_algorithms; |
| 1751 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1752 | if (!CBS_get_u16_length_prefixed(&extension, &supported_signature_algorithms) || |
| 1753 | CBS_len(&extension) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1754 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1755 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1756 | return 0; |
| 1757 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1758 | |
| 1759 | /* Ensure the signature algorithms are non-empty. It |
| 1760 | * contains a list of SignatureAndHashAlgorithms |
| 1761 | * which are two bytes each. */ |
| 1762 | if (CBS_len(&supported_signature_algorithms) == 0 || |
| 1763 | (CBS_len(&supported_signature_algorithms) % 2) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1764 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1765 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1766 | return 0; |
| 1767 | } |
| 1768 | |
David Benjamin | cd99694 | 2014-07-20 16:23:51 -0400 | [diff] [blame] | 1769 | if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1770 | { |
| 1771 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1772 | return 0; |
| 1773 | } |
| 1774 | /* If sigalgs received and no shared algorithms fatal |
| 1775 | * error. |
| 1776 | */ |
| 1777 | if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs) |
| 1778 | { |
| 1779 | OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_NO_SHARED_SIGATURE_ALGORITHMS); |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1780 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1781 | return 0; |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | /* TODO(fork): we probably want OCSP stapling support, but this pulls in a lot of code. */ |
| 1786 | #if 0 |
| 1787 | else if (type == TLSEXT_TYPE_status_request) |
| 1788 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1789 | uint8_t status_type; |
| 1790 | CBS responder_id_list; |
| 1791 | CBS request_extensions; |
| 1792 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1793 | if (!CBS_get_u8(&extension, &status_type)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1794 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1795 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1796 | return 0; |
| 1797 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1798 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1799 | /* Only OCSP is supported. */ |
| 1800 | if (status_type != TLSEXT_STATUSTYPE_ocsp) |
| 1801 | continue; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1802 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1803 | s->tlsext_status_type = status_type; |
| 1804 | |
| 1805 | /* Extension consists of a responder_id_list and |
| 1806 | * request_extensions. */ |
| 1807 | if (!CBS_get_u16_length_prefixed(&extension, &responder_id_list) || |
David Benjamin | 8f3234b | 2014-07-16 13:09:22 -0400 | [diff] [blame] | 1808 | !CBS_get_u16_length_prefixed(&extension, &request_extensions) || |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1809 | CBS_len(&extension) != 0) |
| 1810 | { |
| 1811 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1812 | return 0; |
| 1813 | } |
| 1814 | |
| 1815 | if (CBS_len(&responder_id_list) > 0) |
| 1816 | { |
| 1817 | s->tlsext_ocsp_ids = sk_OCSP_RESPID_new_null(); |
| 1818 | if (s->tlsext_ocsp_ids == NULL) |
| 1819 | { |
| 1820 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1821 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1822 | } |
| 1823 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1824 | |
| 1825 | /* Parse out the responder IDs. */ |
| 1826 | while (CBS_len(&responder_id_list) > 0) |
| 1827 | { |
| 1828 | CBS responder_id; |
| 1829 | OCSP_RESPID *id; |
| 1830 | const uint8_t *data; |
| 1831 | |
| 1832 | /* Each ResponderID must have size at least 1. */ |
| 1833 | if (!CBS_get_u16_length_prefixed(&responder_id_list, &responder_id) || |
| 1834 | CBS_len(&responder_id) < 1) |
| 1835 | { |
| 1836 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1837 | return 0; |
| 1838 | } |
| 1839 | |
| 1840 | /* TODO(fork): Add CBS versions of d2i_FOO_BAR. */ |
| 1841 | data = CBS_data(&responder_id); |
| 1842 | id = d2i_OCSP_RESPID(NULL, &data, CBS_len(&responder_id)); |
| 1843 | if (!id) |
| 1844 | { |
| 1845 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1846 | return 0; |
| 1847 | } |
| 1848 | if (!CBS_skip(&responder_id, data - CBS_data(&responder_id))) |
| 1849 | { |
| 1850 | /* This should never happen. */ |
| 1851 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1852 | OCSP_RESPID_free(id); |
| 1853 | return 0; |
| 1854 | } |
| 1855 | if (CBS_len(&responder_id) != 0) |
| 1856 | { |
| 1857 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1858 | OCSP_RESPID_free(id); |
| 1859 | return 0; |
| 1860 | } |
| 1861 | |
| 1862 | if (!sk_OCSP_RESPID_push(s->tlsext_ocsp_ids, id)) |
| 1863 | { |
| 1864 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1865 | OCSP_RESPID_free(id); |
| 1866 | return 0; |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | /* Parse out request_extensions. */ |
| 1871 | if (CBS_len(&request_extensions) > 0) |
| 1872 | { |
| 1873 | const uint8_t *data; |
| 1874 | |
| 1875 | data = CBS_data(&request_extensions); |
| 1876 | s->tlsext_ocsp_exts = d2i_X509_EXTENSIONS(NULL, |
| 1877 | &data, CBS_len(&request_extensions)); |
| 1878 | if (s->tlsext_ocsp_exts == NULL) |
| 1879 | { |
| 1880 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1881 | return 0; |
| 1882 | } |
| 1883 | if (!CBS_skip(&request_extensions, data - CBS_data(&request_extensions))) |
| 1884 | { |
| 1885 | /* This should never happen. */ |
| 1886 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 1887 | return 0; |
| 1888 | } |
| 1889 | if (CBS_len(&request_extensions) != 0) |
| 1890 | { |
| 1891 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1892 | return 0; |
| 1893 | } |
| 1894 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1895 | } |
| 1896 | #endif |
| 1897 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1898 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 1899 | else if (type == TLSEXT_TYPE_next_proto_neg && |
| 1900 | s->s3->tmp.finish_md_len == 0 && |
| 1901 | s->s3->alpn_selected == NULL) |
| 1902 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1903 | /* The extension must be empty. */ |
| 1904 | if (CBS_len(&extension) != 0) |
| 1905 | { |
| 1906 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1907 | return 0; |
| 1908 | } |
| 1909 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1910 | /* We shouldn't accept this extension on a |
| 1911 | * renegotiation. |
| 1912 | * |
| 1913 | * s->new_session will be set on renegotiation, but we |
| 1914 | * probably shouldn't rely that it couldn't be set on |
| 1915 | * the initial renegotation too in certain cases (when |
| 1916 | * there's some other reason to disallow resuming an |
| 1917 | * earlier session -- the current code won't be doing |
| 1918 | * anything like that, but this might change). |
| 1919 | |
| 1920 | * A valid sign that there's been a previous handshake |
| 1921 | * in this connection is if s->s3->tmp.finish_md_len > |
| 1922 | * 0. (We are talking about a check that will happen |
| 1923 | * in the Hello protocol round, well before a new |
| 1924 | * Finished message could have been computed.) */ |
| 1925 | s->s3->next_proto_neg_seen = 1; |
| 1926 | } |
| 1927 | #endif |
| 1928 | |
| 1929 | else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation && |
| 1930 | s->ctx->alpn_select_cb && |
| 1931 | s->s3->tmp.finish_md_len == 0) |
| 1932 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1933 | if (!tls1_alpn_handle_client_hello(s, &extension, out_alert)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1934 | return 0; |
| 1935 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 1936 | /* ALPN takes precedence over NPN. */ |
| 1937 | s->s3->next_proto_neg_seen = 0; |
| 1938 | #endif |
| 1939 | } |
| 1940 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1941 | else if (type == TLSEXT_TYPE_channel_id && |
| 1942 | s->tlsext_channel_id_enabled) |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1943 | { |
| 1944 | /* The extension must be empty. */ |
| 1945 | if (CBS_len(&extension) != 0) |
| 1946 | { |
| 1947 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1948 | return 0; |
| 1949 | } |
| 1950 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1951 | s->s3->tlsext_channel_id_valid = 1; |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1952 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1953 | |
| 1954 | else if (type == TLSEXT_TYPE_channel_id_new && |
| 1955 | s->tlsext_channel_id_enabled) |
| 1956 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1957 | /* The extension must be empty. */ |
| 1958 | if (CBS_len(&extension) != 0) |
| 1959 | { |
| 1960 | *out_alert = SSL_AD_DECODE_ERROR; |
| 1961 | return 0; |
| 1962 | } |
| 1963 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1964 | s->s3->tlsext_channel_id_valid = 1; |
| 1965 | s->s3->tlsext_channel_id_new = 1; |
| 1966 | } |
| 1967 | |
| 1968 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1969 | /* session ticket processed earlier */ |
| 1970 | else if (type == TLSEXT_TYPE_use_srtp) |
| 1971 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1972 | if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1973 | return 0; |
| 1974 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1975 | } |
| 1976 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1977 | ri_check: |
| 1978 | |
| 1979 | /* Need RI if renegotiating */ |
| 1980 | |
| 1981 | if (!renegotiate_seen && s->renegotiate && |
| 1982 | !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) |
| 1983 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1984 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1985 | OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); |
| 1986 | return 0; |
| 1987 | } |
| 1988 | /* If no signature algorithms extension set default values */ |
| 1989 | if (!s->cert->peer_sigalgs) |
| 1990 | ssl_cert_set_default_md(s->cert); |
| 1991 | |
| 1992 | return 1; |
| 1993 | } |
| 1994 | |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1995 | int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1996 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1997 | int alert = -1; |
| 1998 | if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1999 | { |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 2000 | ssl3_send_alert(s, SSL3_AL_FATAL, alert); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2001 | return 0; |
| 2002 | } |
| 2003 | |
| 2004 | if (ssl_check_clienthello_tlsext_early(s) <= 0) |
| 2005 | { |
David Benjamin | 9d28c75 | 2014-07-05 00:43:48 -0400 | [diff] [blame] | 2006 | OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext, SSL_R_CLIENTHELLO_TLSEXT); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2007 | return 0; |
| 2008 | } |
| 2009 | return 1; |
David Benjamin | 072334d | 2014-07-13 16:24:27 -0400 | [diff] [blame] | 2010 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2011 | |
| 2012 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 2013 | /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No |
| 2014 | * elements of zero length are allowed and the set of elements must exactly fill |
| 2015 | * the length of the block. */ |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2016 | static char ssl_next_proto_validate(const CBS *cbs) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2017 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2018 | CBS copy = *cbs; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2019 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2020 | while (CBS_len(©) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2021 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2022 | CBS proto; |
| 2023 | if (!CBS_get_u8_length_prefixed(©, &proto) || |
| 2024 | CBS_len(&proto) == 0) |
| 2025 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2026 | return 0; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2027 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2028 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2029 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2030 | } |
| 2031 | #endif |
| 2032 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2033 | static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2034 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2035 | int tlsext_servername = 0; |
| 2036 | int renegotiate_seen = 0; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2037 | CBS extensions; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2038 | |
| 2039 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 2040 | s->s3->next_proto_neg_seen = 0; |
| 2041 | #endif |
| 2042 | |
David Benjamin | 6444287 | 2014-07-21 17:43:45 -0400 | [diff] [blame] | 2043 | s->tlsext_ticket_expected = 0; |
| 2044 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2045 | if (s->s3->alpn_selected) |
| 2046 | { |
| 2047 | OPENSSL_free(s->s3->alpn_selected); |
| 2048 | s->s3->alpn_selected = NULL; |
| 2049 | } |
| 2050 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2051 | /* There may be no extensions. */ |
| 2052 | if (CBS_len(cbs) == 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2053 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2054 | goto ri_check; |
| 2055 | } |
| 2056 | |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 2057 | /* Decode the extensions block and check it is valid. */ |
| 2058 | if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 2059 | !tls1_check_duplicate_extensions(&extensions)) |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2060 | { |
| 2061 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2062 | return 0; |
| 2063 | } |
| 2064 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2065 | while (CBS_len(&extensions) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2066 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2067 | uint16_t type; |
| 2068 | CBS extension; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2069 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2070 | /* Decode the next extension. */ |
| 2071 | if (!CBS_get_u16(&extensions, &type) || |
| 2072 | !CBS_get_u16_length_prefixed(&extensions, &extension)) |
| 2073 | { |
| 2074 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2075 | return 0; |
| 2076 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2077 | |
| 2078 | if (s->tlsext_debug_cb) |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2079 | { |
| 2080 | s->tlsext_debug_cb(s, 1, type, (unsigned char*)CBS_data(&extension), |
| 2081 | CBS_len(&extension), s->tlsext_debug_arg); |
| 2082 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2083 | |
| 2084 | if (type == TLSEXT_TYPE_server_name) |
| 2085 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2086 | /* The extension must be empty. */ |
| 2087 | if (CBS_len(&extension) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2088 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2089 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2090 | return 0; |
| 2091 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2092 | /* We must have sent it in ClientHello. */ |
| 2093 | if (s->tlsext_hostname == NULL) |
| 2094 | { |
| 2095 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2096 | return 0; |
| 2097 | } |
| 2098 | tlsext_servername = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2099 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2100 | #ifndef OPENSSL_NO_EC |
| 2101 | else if (type == TLSEXT_TYPE_ec_point_formats) |
| 2102 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2103 | CBS ec_point_format_list; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2104 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2105 | if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) || |
| 2106 | CBS_len(&extension) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2107 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2108 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2109 | return 0; |
| 2110 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2111 | |
Adam Langley | 5ba06a7 | 2014-08-06 17:27:31 -0700 | [diff] [blame] | 2112 | if (!s->hit) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2113 | { |
Adam Langley | 5ba06a7 | 2014-08-06 17:27:31 -0700 | [diff] [blame] | 2114 | if (!CBS_stow(&ec_point_format_list, |
| 2115 | &s->session->tlsext_ecpointformatlist, |
| 2116 | &s->session->tlsext_ecpointformatlist_length)) |
| 2117 | { |
| 2118 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2119 | return 0; |
| 2120 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2121 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2122 | } |
| 2123 | #endif /* OPENSSL_NO_EC */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2124 | else if (type == TLSEXT_TYPE_session_ticket) |
| 2125 | { |
| 2126 | if (s->tls_session_ticket_ext_cb && |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2127 | !s->tls_session_ticket_ext_cb(s, CBS_data(&extension), CBS_len(&extension), |
| 2128 | s->tls_session_ticket_ext_cb_arg)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2129 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2130 | *out_alert = SSL_AD_INTERNAL_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2131 | return 0; |
| 2132 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2133 | |
| 2134 | if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2135 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2136 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2137 | return 0; |
| 2138 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2139 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2140 | s->tlsext_ticket_expected = 1; |
| 2141 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2142 | else if (type == TLSEXT_TYPE_status_request) |
| 2143 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2144 | /* The extension MUST be empty and may only sent if |
| 2145 | * we've requested a status request message. */ |
| 2146 | if (CBS_len(&extension) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2147 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2148 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2149 | return 0; |
| 2150 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2151 | if (s->tlsext_status_type == -1) |
| 2152 | { |
| 2153 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2154 | return 0; |
| 2155 | } |
| 2156 | /* Set a flag to expect a CertificateStatus message */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2157 | s->tlsext_status_expected = 1; |
| 2158 | } |
| 2159 | #ifndef OPENSSL_NO_NEXTPROTONEG |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2160 | else if (type == TLSEXT_TYPE_next_proto_neg && s->s3->tmp.finish_md_len == 0) { |
| 2161 | unsigned char *selected; |
| 2162 | unsigned char selected_len; |
| 2163 | |
| 2164 | /* We must have requested it. */ |
| 2165 | if (s->ctx->next_proto_select_cb == NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2166 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2167 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 2168 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2169 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2170 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2171 | /* The data must be valid. */ |
| 2172 | if (!ssl_next_proto_validate(&extension)) |
| 2173 | { |
| 2174 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2175 | return 0; |
| 2176 | } |
| 2177 | |
| 2178 | if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, |
| 2179 | CBS_data(&extension), CBS_len(&extension), |
| 2180 | s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) |
| 2181 | { |
| 2182 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2183 | return 0; |
| 2184 | } |
| 2185 | |
| 2186 | s->next_proto_negotiated = BUF_memdup(selected, selected_len); |
| 2187 | if (s->next_proto_negotiated == NULL) |
| 2188 | { |
| 2189 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2190 | return 0; |
| 2191 | } |
| 2192 | s->next_proto_negotiated_len = selected_len; |
| 2193 | s->s3->next_proto_neg_seen = 1; |
| 2194 | } |
| 2195 | #endif |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2196 | else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) |
| 2197 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2198 | CBS protocol_name_list, protocol_name; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2199 | |
| 2200 | /* We must have requested it. */ |
| 2201 | if (s->alpn_client_proto_list == NULL) |
| 2202 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2203 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2204 | return 0; |
| 2205 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2206 | |
| 2207 | /* The extension data consists of a ProtocolNameList |
| 2208 | * which must have exactly one ProtocolName. Each of |
| 2209 | * these is length-prefixed. */ |
| 2210 | if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) || |
| 2211 | CBS_len(&extension) != 0 || |
| 2212 | !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) || |
| 2213 | CBS_len(&protocol_name_list) != 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2214 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2215 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2216 | return 0; |
| 2217 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2218 | |
| 2219 | if (!CBS_stow(&protocol_name, |
| 2220 | &s->s3->alpn_selected, |
| 2221 | &s->s3->alpn_selected_len)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2222 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2223 | *out_alert = SSL_AD_INTERNAL_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2224 | return 0; |
| 2225 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2226 | } |
| 2227 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2228 | else if (type == TLSEXT_TYPE_channel_id) |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2229 | { |
| 2230 | if (CBS_len(&extension) != 0) |
| 2231 | { |
| 2232 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2233 | return 0; |
| 2234 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2235 | s->s3->tlsext_channel_id_valid = 1; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2236 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2237 | else if (type == TLSEXT_TYPE_channel_id_new) |
| 2238 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2239 | if (CBS_len(&extension) != 0) |
| 2240 | { |
| 2241 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2242 | return 0; |
| 2243 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2244 | s->s3->tlsext_channel_id_valid = 1; |
| 2245 | s->s3->tlsext_channel_id_new = 1; |
| 2246 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame^] | 2247 | else if (type == TLSEXT_TYPE_certificate_timestamp) |
| 2248 | { |
| 2249 | if (CBS_len(&extension) == 0) |
| 2250 | { |
| 2251 | *out_alert = SSL_AD_DECODE_ERROR; |
| 2252 | return 0; |
| 2253 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2254 | |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame^] | 2255 | /* Session resumption uses the original session information. */ |
| 2256 | if (!s->hit) |
| 2257 | { |
| 2258 | if (!CBS_stow(&extension, |
| 2259 | &s->session->tlsext_signed_cert_timestamp_list, |
| 2260 | &s->session->tlsext_signed_cert_timestamp_list_length)) |
| 2261 | { |
| 2262 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 2263 | return 0; |
| 2264 | } |
| 2265 | } |
| 2266 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2267 | else if (type == TLSEXT_TYPE_renegotiate) |
| 2268 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2269 | if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2270 | return 0; |
| 2271 | renegotiate_seen = 1; |
| 2272 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2273 | else if (type == TLSEXT_TYPE_use_srtp) |
| 2274 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2275 | if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2276 | return 0; |
| 2277 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | if (!s->hit && tlsext_servername == 1) |
| 2281 | { |
| 2282 | if (s->tlsext_hostname) |
| 2283 | { |
| 2284 | if (s->session->tlsext_hostname == NULL) |
| 2285 | { |
| 2286 | s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname); |
| 2287 | if (!s->session->tlsext_hostname) |
| 2288 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2289 | *out_alert = SSL_AD_UNRECOGNIZED_NAME; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2290 | return 0; |
| 2291 | } |
| 2292 | } |
| 2293 | else |
| 2294 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2295 | *out_alert = SSL_AD_DECODE_ERROR; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2296 | return 0; |
| 2297 | } |
| 2298 | } |
| 2299 | } |
| 2300 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2301 | ri_check: |
| 2302 | |
| 2303 | /* Determine if we need to see RI. Strictly speaking if we want to |
| 2304 | * avoid an attack we should *always* see RI even on initial server |
| 2305 | * hello because the client doesn't see any renegotiation during an |
| 2306 | * attack. However this would mean we could not connect to any server |
| 2307 | * which doesn't support RI so for the immediate future tolerate RI |
| 2308 | * absence on initial connect only. |
| 2309 | */ |
| 2310 | if (!renegotiate_seen |
| 2311 | && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) |
| 2312 | && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) |
| 2313 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2314 | *out_alert = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 9d28c75 | 2014-07-05 00:43:48 -0400 | [diff] [blame] | 2315 | OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2316 | return 0; |
| 2317 | } |
| 2318 | |
| 2319 | return 1; |
| 2320 | } |
| 2321 | |
| 2322 | |
| 2323 | int ssl_prepare_clienthello_tlsext(SSL *s) |
| 2324 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2325 | return 1; |
| 2326 | } |
| 2327 | |
| 2328 | int ssl_prepare_serverhello_tlsext(SSL *s) |
| 2329 | { |
| 2330 | return 1; |
| 2331 | } |
| 2332 | |
| 2333 | static int ssl_check_clienthello_tlsext_early(SSL *s) |
| 2334 | { |
| 2335 | int ret=SSL_TLSEXT_ERR_NOACK; |
| 2336 | int al = SSL_AD_UNRECOGNIZED_NAME; |
| 2337 | |
| 2338 | #ifndef OPENSSL_NO_EC |
| 2339 | /* The handling of the ECPointFormats extension is done elsewhere, namely in |
| 2340 | * ssl3_choose_cipher in s3_lib.c. |
| 2341 | */ |
| 2342 | /* The handling of the EllipticCurves extension is done elsewhere, namely in |
| 2343 | * ssl3_choose_cipher in s3_lib.c. |
| 2344 | */ |
| 2345 | #endif |
| 2346 | |
| 2347 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) |
| 2348 | ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg); |
| 2349 | else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0) |
| 2350 | ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg); |
| 2351 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2352 | switch (ret) |
| 2353 | { |
| 2354 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2355 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2356 | return -1; |
| 2357 | |
| 2358 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 2359 | ssl3_send_alert(s,SSL3_AL_WARNING,al); |
| 2360 | return 1; |
| 2361 | |
| 2362 | case SSL_TLSEXT_ERR_NOACK: |
| 2363 | s->servername_done=0; |
| 2364 | default: |
| 2365 | return 1; |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | int ssl_check_clienthello_tlsext_late(SSL *s) |
| 2370 | { |
| 2371 | int ret = SSL_TLSEXT_ERR_OK; |
| 2372 | int al; |
| 2373 | |
| 2374 | /* If status request then ask callback what to do. |
| 2375 | * Note: this must be called after servername callbacks in case |
| 2376 | * the certificate has changed, and must be called after the cipher |
| 2377 | * has been chosen because this may influence which certificate is sent |
| 2378 | */ |
| 2379 | if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) |
| 2380 | { |
| 2381 | int r; |
| 2382 | CERT_PKEY *certpkey; |
| 2383 | certpkey = ssl_get_server_send_pkey(s); |
| 2384 | /* If no certificate can't return certificate status */ |
| 2385 | if (certpkey == NULL) |
| 2386 | { |
| 2387 | s->tlsext_status_expected = 0; |
| 2388 | return 1; |
| 2389 | } |
| 2390 | /* Set current certificate to one we will use so |
| 2391 | * SSL_get_certificate et al can pick it up. |
| 2392 | */ |
| 2393 | s->cert->key = certpkey; |
| 2394 | r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); |
| 2395 | switch (r) |
| 2396 | { |
| 2397 | /* We don't want to send a status request response */ |
| 2398 | case SSL_TLSEXT_ERR_NOACK: |
| 2399 | s->tlsext_status_expected = 0; |
| 2400 | break; |
| 2401 | /* status request response should be sent */ |
| 2402 | case SSL_TLSEXT_ERR_OK: |
| 2403 | if (s->tlsext_ocsp_resp) |
| 2404 | s->tlsext_status_expected = 1; |
| 2405 | else |
| 2406 | s->tlsext_status_expected = 0; |
| 2407 | break; |
| 2408 | /* something bad happened */ |
| 2409 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2410 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2411 | al = SSL_AD_INTERNAL_ERROR; |
| 2412 | goto err; |
| 2413 | } |
| 2414 | } |
| 2415 | else |
| 2416 | s->tlsext_status_expected = 0; |
| 2417 | |
| 2418 | err: |
| 2419 | switch (ret) |
| 2420 | { |
| 2421 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2422 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2423 | return -1; |
| 2424 | |
| 2425 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 2426 | ssl3_send_alert(s, SSL3_AL_WARNING, al); |
| 2427 | return 1; |
| 2428 | |
| 2429 | default: |
| 2430 | return 1; |
| 2431 | } |
| 2432 | } |
| 2433 | |
| 2434 | int ssl_check_serverhello_tlsext(SSL *s) |
| 2435 | { |
| 2436 | int ret=SSL_TLSEXT_ERR_NOACK; |
| 2437 | int al = SSL_AD_UNRECOGNIZED_NAME; |
| 2438 | |
| 2439 | #ifndef OPENSSL_NO_EC |
| 2440 | /* If we are client and using an elliptic curve cryptography cipher |
| 2441 | * suite, then if server returns an EC point formats lists extension |
| 2442 | * it must contain uncompressed. |
| 2443 | */ |
| 2444 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 2445 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 2446 | if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) && |
| 2447 | (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) && |
David Benjamin | 0da0e18 | 2014-08-19 16:20:28 -0400 | [diff] [blame] | 2448 | ((alg_k & SSL_kEECDH) || (alg_a & SSL_aECDSA))) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2449 | { |
| 2450 | /* we are using an ECC cipher */ |
| 2451 | size_t i; |
| 2452 | unsigned char *list; |
| 2453 | int found_uncompressed = 0; |
| 2454 | list = s->session->tlsext_ecpointformatlist; |
| 2455 | for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) |
| 2456 | { |
| 2457 | if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) |
| 2458 | { |
| 2459 | found_uncompressed = 1; |
| 2460 | break; |
| 2461 | } |
| 2462 | } |
| 2463 | if (!found_uncompressed) |
| 2464 | { |
| 2465 | OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST); |
| 2466 | return -1; |
| 2467 | } |
| 2468 | } |
| 2469 | ret = SSL_TLSEXT_ERR_OK; |
| 2470 | #endif /* OPENSSL_NO_EC */ |
| 2471 | |
| 2472 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) |
| 2473 | ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg); |
| 2474 | else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0) |
| 2475 | ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg); |
| 2476 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2477 | /* If we've requested certificate status and we wont get one |
| 2478 | * tell the callback |
| 2479 | */ |
| 2480 | if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected) |
| 2481 | && s->ctx && s->ctx->tlsext_status_cb) |
| 2482 | { |
| 2483 | int r; |
| 2484 | /* Set resp to NULL, resplen to -1 so callback knows |
| 2485 | * there is no response. |
| 2486 | */ |
| 2487 | if (s->tlsext_ocsp_resp) |
| 2488 | { |
| 2489 | OPENSSL_free(s->tlsext_ocsp_resp); |
| 2490 | s->tlsext_ocsp_resp = NULL; |
| 2491 | } |
| 2492 | s->tlsext_ocsp_resplen = -1; |
| 2493 | r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); |
| 2494 | if (r == 0) |
| 2495 | { |
| 2496 | al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; |
| 2497 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2498 | } |
| 2499 | if (r < 0) |
| 2500 | { |
| 2501 | al = SSL_AD_INTERNAL_ERROR; |
| 2502 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | switch (ret) |
| 2507 | { |
| 2508 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2509 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2510 | return -1; |
| 2511 | |
| 2512 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 2513 | ssl3_send_alert(s,SSL3_AL_WARNING,al); |
| 2514 | return 1; |
| 2515 | |
| 2516 | case SSL_TLSEXT_ERR_NOACK: |
| 2517 | s->servername_done=0; |
| 2518 | default: |
| 2519 | return 1; |
| 2520 | } |
| 2521 | } |
| 2522 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2523 | int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2524 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2525 | int alert = -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2526 | if (s->version < SSL3_VERSION) |
| 2527 | return 1; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2528 | |
| 2529 | if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2530 | { |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2531 | ssl3_send_alert(s, SSL3_AL_FATAL, alert); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2532 | return 0; |
| 2533 | } |
| 2534 | |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2535 | if (ssl_check_serverhello_tlsext(s) <= 0) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2536 | { |
David Benjamin | 9d28c75 | 2014-07-05 00:43:48 -0400 | [diff] [blame] | 2537 | OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext, SSL_R_SERVERHELLO_TLSEXT); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2538 | return 0; |
| 2539 | } |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2540 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2541 | return 1; |
David Benjamin | 0397309 | 2014-06-24 23:27:17 -0400 | [diff] [blame] | 2542 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2543 | |
| 2544 | /* Since the server cache lookup is done early on in the processing of the |
| 2545 | * ClientHello, and other operations depend on the result, we need to handle |
| 2546 | * any TLS session ticket extension at the same time. |
| 2547 | * |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2548 | * ctx: contains the early callback context, which is the result of a |
| 2549 | * shallow parse of the ClientHello. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2550 | * ret: (output) on return, if a ticket was decrypted, then this is set to |
| 2551 | * point to the resulting session. |
| 2552 | * |
| 2553 | * If s->tls_session_secret_cb is set then we are expecting a pre-shared key |
| 2554 | * ciphersuite, in which case we have no use for session tickets and one will |
| 2555 | * never be decrypted, nor will s->tlsext_ticket_expected be set to 1. |
| 2556 | * |
| 2557 | * Returns: |
| 2558 | * -1: fatal error, either from parsing or decrypting the ticket. |
| 2559 | * 0: no ticket was found (or was ignored, based on settings). |
| 2560 | * 1: a zero length extension was found, indicating that the client supports |
| 2561 | * session tickets but doesn't currently have one to offer. |
| 2562 | * 2: either s->tls_session_secret_cb was set, or a ticket was offered but |
| 2563 | * couldn't be decrypted because of a non-fatal error. |
| 2564 | * 3: a ticket was successfully decrypted and *ret was set. |
| 2565 | * |
| 2566 | * Side effects: |
| 2567 | * Sets s->tlsext_ticket_expected to 1 if the server will have to issue |
| 2568 | * a new session ticket to the client because the client indicated support |
| 2569 | * (and s->tls_session_secret_cb is NULL) but the client either doesn't have |
| 2570 | * a session ticket or we couldn't use the one it gave us, or if |
| 2571 | * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket. |
| 2572 | * Otherwise, s->tlsext_ticket_expected is set to 0. |
| 2573 | */ |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2574 | int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx, |
| 2575 | SSL_SESSION **ret) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2576 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2577 | *ret = NULL; |
| 2578 | s->tlsext_ticket_expected = 0; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2579 | const unsigned char *data; |
| 2580 | size_t len; |
| 2581 | int r; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2582 | |
| 2583 | /* If tickets disabled behave as if no ticket present |
| 2584 | * to permit stateful resumption. |
| 2585 | */ |
| 2586 | if (SSL_get_options(s) & SSL_OP_NO_TICKET) |
| 2587 | return 0; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2588 | if ((s->version <= SSL3_VERSION) && !ctx->extensions) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2589 | return 0; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2590 | if (!SSL_early_callback_ctx_extension_get( |
| 2591 | ctx, TLSEXT_TYPE_session_ticket, &data, &len)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2592 | { |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2593 | return 0; |
| 2594 | } |
| 2595 | if (len == 0) |
| 2596 | { |
| 2597 | /* The client will accept a ticket but doesn't |
| 2598 | * currently have one. */ |
| 2599 | s->tlsext_ticket_expected = 1; |
| 2600 | return 1; |
| 2601 | } |
| 2602 | if (s->tls_session_secret_cb) |
| 2603 | { |
| 2604 | /* Indicate that the ticket couldn't be |
| 2605 | * decrypted rather than generating the session |
| 2606 | * from ticket now, trigger abbreviated |
| 2607 | * handshake based on external mechanism to |
| 2608 | * calculate the master secret later. */ |
| 2609 | return 2; |
| 2610 | } |
| 2611 | r = tls_decrypt_ticket(s, data, len, ctx->session_id, |
| 2612 | ctx->session_id_len, ret); |
| 2613 | switch (r) |
| 2614 | { |
| 2615 | case 2: /* ticket couldn't be decrypted */ |
| 2616 | s->tlsext_ticket_expected = 1; |
| 2617 | return 2; |
| 2618 | case 3: /* ticket was decrypted */ |
| 2619 | return r; |
| 2620 | case 4: /* ticket decrypted but need to renew */ |
| 2621 | s->tlsext_ticket_expected = 1; |
| 2622 | return 3; |
| 2623 | default: /* fatal error */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2624 | return -1; |
| 2625 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2626 | } |
| 2627 | |
| 2628 | /* tls_decrypt_ticket attempts to decrypt a session ticket. |
| 2629 | * |
| 2630 | * etick: points to the body of the session ticket extension. |
| 2631 | * eticklen: the length of the session tickets extenion. |
| 2632 | * sess_id: points at the session ID. |
| 2633 | * sesslen: the length of the session ID. |
| 2634 | * psess: (output) on return, if a ticket was decrypted, then this is set to |
| 2635 | * point to the resulting session. |
| 2636 | * |
| 2637 | * Returns: |
| 2638 | * -1: fatal error, either from parsing or decrypting the ticket. |
| 2639 | * 2: the ticket couldn't be decrypted. |
| 2640 | * 3: a ticket was successfully decrypted and *psess was set. |
| 2641 | * 4: same as 3, but the ticket needs to be renewed. |
| 2642 | */ |
| 2643 | static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen, |
| 2644 | const unsigned char *sess_id, int sesslen, |
| 2645 | SSL_SESSION **psess) |
| 2646 | { |
| 2647 | SSL_SESSION *sess; |
| 2648 | unsigned char *sdec; |
| 2649 | const unsigned char *p; |
| 2650 | int slen, mlen, renew_ticket = 0; |
| 2651 | unsigned char tick_hmac[EVP_MAX_MD_SIZE]; |
| 2652 | HMAC_CTX hctx; |
| 2653 | EVP_CIPHER_CTX ctx; |
| 2654 | SSL_CTX *tctx = s->initial_ctx; |
| 2655 | /* Need at least keyname + iv + some encrypted data */ |
| 2656 | if (eticklen < 48) |
| 2657 | return 2; |
| 2658 | /* Initialize session ticket encryption and HMAC contexts */ |
| 2659 | HMAC_CTX_init(&hctx); |
| 2660 | EVP_CIPHER_CTX_init(&ctx); |
| 2661 | if (tctx->tlsext_ticket_key_cb) |
| 2662 | { |
| 2663 | unsigned char *nctick = (unsigned char *)etick; |
| 2664 | int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, |
| 2665 | &ctx, &hctx, 0); |
| 2666 | if (rv < 0) |
| 2667 | return -1; |
| 2668 | if (rv == 0) |
| 2669 | return 2; |
| 2670 | if (rv == 2) |
| 2671 | renew_ticket = 1; |
| 2672 | } |
| 2673 | else |
| 2674 | { |
| 2675 | /* Check key name matches */ |
| 2676 | if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) |
| 2677 | return 2; |
| 2678 | HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, |
| 2679 | tlsext_tick_md(), NULL); |
| 2680 | EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, |
| 2681 | tctx->tlsext_tick_aes_key, etick + 16); |
| 2682 | } |
| 2683 | /* Attempt to process session ticket, first conduct sanity and |
| 2684 | * integrity checks on ticket. |
| 2685 | */ |
| 2686 | mlen = HMAC_size(&hctx); |
| 2687 | if (mlen < 0) |
| 2688 | { |
| 2689 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2690 | return -1; |
| 2691 | } |
| 2692 | eticklen -= mlen; |
| 2693 | /* Check HMAC of encrypted ticket */ |
| 2694 | HMAC_Update(&hctx, etick, eticklen); |
| 2695 | HMAC_Final(&hctx, tick_hmac, NULL); |
| 2696 | HMAC_CTX_cleanup(&hctx); |
| 2697 | if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) |
| 2698 | return 2; |
| 2699 | /* Attempt to decrypt session data */ |
| 2700 | /* Move p after IV to start of encrypted ticket, update length */ |
| 2701 | p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx); |
| 2702 | eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx); |
| 2703 | sdec = OPENSSL_malloc(eticklen); |
| 2704 | if (!sdec) |
| 2705 | { |
| 2706 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2707 | return -1; |
| 2708 | } |
| 2709 | EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen); |
| 2710 | if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0) |
Adam Langley | 3e14885 | 2014-07-24 17:34:02 -0700 | [diff] [blame] | 2711 | { |
| 2712 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2713 | OPENSSL_free(sdec); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2714 | return 2; |
Adam Langley | 3e14885 | 2014-07-24 17:34:02 -0700 | [diff] [blame] | 2715 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2716 | slen += mlen; |
| 2717 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2718 | p = sdec; |
| 2719 | |
| 2720 | sess = d2i_SSL_SESSION(NULL, &p, slen); |
| 2721 | OPENSSL_free(sdec); |
| 2722 | if (sess) |
| 2723 | { |
| 2724 | /* The session ID, if non-empty, is used by some clients to |
| 2725 | * detect that the ticket has been accepted. So we copy it to |
| 2726 | * the session structure. If it is empty set length to zero |
| 2727 | * as required by standard. |
| 2728 | */ |
| 2729 | if (sesslen) |
| 2730 | memcpy(sess->session_id, sess_id, sesslen); |
| 2731 | sess->session_id_length = sesslen; |
| 2732 | *psess = sess; |
| 2733 | if (renew_ticket) |
| 2734 | return 4; |
| 2735 | else |
| 2736 | return 3; |
| 2737 | } |
| 2738 | ERR_clear_error(); |
| 2739 | /* For session parse failure, indicate that we need to send a new |
| 2740 | * ticket. */ |
| 2741 | return 2; |
| 2742 | } |
| 2743 | |
| 2744 | /* Tables to translate from NIDs to TLS v1.2 ids */ |
| 2745 | |
| 2746 | typedef struct |
| 2747 | { |
| 2748 | int nid; |
| 2749 | int id; |
| 2750 | } tls12_lookup; |
| 2751 | |
David Benjamin | cff6472 | 2014-08-19 19:54:46 -0400 | [diff] [blame] | 2752 | static const tls12_lookup tls12_md[] = { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2753 | {NID_md5, TLSEXT_hash_md5}, |
| 2754 | {NID_sha1, TLSEXT_hash_sha1}, |
| 2755 | {NID_sha224, TLSEXT_hash_sha224}, |
| 2756 | {NID_sha256, TLSEXT_hash_sha256}, |
| 2757 | {NID_sha384, TLSEXT_hash_sha384}, |
| 2758 | {NID_sha512, TLSEXT_hash_sha512} |
| 2759 | }; |
| 2760 | |
David Benjamin | cff6472 | 2014-08-19 19:54:46 -0400 | [diff] [blame] | 2761 | static const tls12_lookup tls12_sig[] = { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2762 | {EVP_PKEY_RSA, TLSEXT_signature_rsa}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2763 | {EVP_PKEY_EC, TLSEXT_signature_ecdsa} |
| 2764 | }; |
| 2765 | |
David Benjamin | cff6472 | 2014-08-19 19:54:46 -0400 | [diff] [blame] | 2766 | static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2767 | { |
| 2768 | size_t i; |
| 2769 | for (i = 0; i < tlen; i++) |
| 2770 | { |
| 2771 | if (table[i].nid == nid) |
| 2772 | return table[i].id; |
| 2773 | } |
| 2774 | return -1; |
| 2775 | } |
| 2776 | |
David Benjamin | cff6472 | 2014-08-19 19:54:46 -0400 | [diff] [blame] | 2777 | static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2778 | { |
| 2779 | size_t i; |
| 2780 | for (i = 0; i < tlen; i++) |
| 2781 | { |
| 2782 | if ((table[i].id) == id) |
| 2783 | return table[i].nid; |
| 2784 | } |
| 2785 | return NID_undef; |
| 2786 | } |
| 2787 | |
| 2788 | int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md) |
| 2789 | { |
| 2790 | int sig_id, md_id; |
| 2791 | if (!md) |
| 2792 | return 0; |
| 2793 | md_id = tls12_find_id(EVP_MD_type(md), tls12_md, |
| 2794 | sizeof(tls12_md)/sizeof(tls12_lookup)); |
| 2795 | if (md_id == -1) |
| 2796 | return 0; |
| 2797 | sig_id = tls12_get_sigid(pk); |
| 2798 | if (sig_id == -1) |
| 2799 | return 0; |
| 2800 | p[0] = (unsigned char)md_id; |
| 2801 | p[1] = (unsigned char)sig_id; |
| 2802 | return 1; |
| 2803 | } |
| 2804 | |
| 2805 | int tls12_get_sigid(const EVP_PKEY *pk) |
| 2806 | { |
| 2807 | return tls12_find_id(pk->type, tls12_sig, |
| 2808 | sizeof(tls12_sig)/sizeof(tls12_lookup)); |
| 2809 | } |
| 2810 | |
| 2811 | const EVP_MD *tls12_get_hash(unsigned char hash_alg) |
| 2812 | { |
| 2813 | switch(hash_alg) |
| 2814 | { |
| 2815 | #ifndef OPENSSL_NO_MD5 |
| 2816 | case TLSEXT_hash_md5: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2817 | return EVP_md5(); |
| 2818 | #endif |
| 2819 | #ifndef OPENSSL_NO_SHA |
| 2820 | case TLSEXT_hash_sha1: |
| 2821 | return EVP_sha1(); |
| 2822 | #endif |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2823 | case TLSEXT_hash_sha224: |
| 2824 | return EVP_sha224(); |
| 2825 | |
| 2826 | case TLSEXT_hash_sha256: |
| 2827 | return EVP_sha256(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2828 | case TLSEXT_hash_sha384: |
| 2829 | return EVP_sha384(); |
| 2830 | |
| 2831 | case TLSEXT_hash_sha512: |
| 2832 | return EVP_sha512(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2833 | default: |
| 2834 | return NULL; |
| 2835 | |
| 2836 | } |
| 2837 | } |
| 2838 | |
| 2839 | static int tls12_get_pkey_idx(unsigned char sig_alg) |
| 2840 | { |
| 2841 | switch(sig_alg) |
| 2842 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2843 | case TLSEXT_signature_rsa: |
| 2844 | return SSL_PKEY_RSA_SIGN; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2845 | #ifndef OPENSSL_NO_ECDSA |
| 2846 | case TLSEXT_signature_ecdsa: |
| 2847 | return SSL_PKEY_ECC; |
| 2848 | #endif |
| 2849 | } |
| 2850 | return -1; |
| 2851 | } |
| 2852 | |
| 2853 | /* Convert TLS 1.2 signature algorithm extension values into NIDs */ |
| 2854 | static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid, |
| 2855 | int *psignhash_nid, const unsigned char *data) |
| 2856 | { |
| 2857 | int sign_nid = 0, hash_nid = 0; |
| 2858 | if (!phash_nid && !psign_nid && !psignhash_nid) |
| 2859 | return; |
| 2860 | if (phash_nid || psignhash_nid) |
| 2861 | { |
| 2862 | hash_nid = tls12_find_nid(data[0], tls12_md, |
| 2863 | sizeof(tls12_md)/sizeof(tls12_lookup)); |
| 2864 | if (phash_nid) |
| 2865 | *phash_nid = hash_nid; |
| 2866 | } |
| 2867 | if (psign_nid || psignhash_nid) |
| 2868 | { |
| 2869 | sign_nid = tls12_find_nid(data[1], tls12_sig, |
| 2870 | sizeof(tls12_sig)/sizeof(tls12_lookup)); |
| 2871 | if (psign_nid) |
| 2872 | *psign_nid = sign_nid; |
| 2873 | } |
| 2874 | if (psignhash_nid) |
| 2875 | { |
| 2876 | if (sign_nid && hash_nid) |
| 2877 | OBJ_find_sigid_by_algs(psignhash_nid, |
| 2878 | hash_nid, sign_nid); |
| 2879 | else |
| 2880 | *psignhash_nid = NID_undef; |
| 2881 | } |
| 2882 | } |
| 2883 | /* Given preference and allowed sigalgs set shared sigalgs */ |
| 2884 | static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, |
| 2885 | const unsigned char *pref, size_t preflen, |
| 2886 | const unsigned char *allow, size_t allowlen) |
| 2887 | { |
| 2888 | const unsigned char *ptmp, *atmp; |
| 2889 | size_t i, j, nmatch = 0; |
| 2890 | for (i = 0, ptmp = pref; i < preflen; i+=2, ptmp+=2) |
| 2891 | { |
| 2892 | /* Skip disabled hashes or signature algorithms */ |
| 2893 | if (tls12_get_hash(ptmp[0]) == NULL) |
| 2894 | continue; |
| 2895 | if (tls12_get_pkey_idx(ptmp[1]) == -1) |
| 2896 | continue; |
| 2897 | for (j = 0, atmp = allow; j < allowlen; j+=2, atmp+=2) |
| 2898 | { |
| 2899 | if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) |
| 2900 | { |
| 2901 | nmatch++; |
| 2902 | if (shsig) |
| 2903 | { |
| 2904 | shsig->rhash = ptmp[0]; |
| 2905 | shsig->rsign = ptmp[1]; |
| 2906 | tls1_lookup_sigalg(&shsig->hash_nid, |
| 2907 | &shsig->sign_nid, |
| 2908 | &shsig->signandhash_nid, |
| 2909 | ptmp); |
| 2910 | shsig++; |
| 2911 | } |
| 2912 | break; |
| 2913 | } |
| 2914 | } |
| 2915 | } |
| 2916 | return nmatch; |
| 2917 | } |
| 2918 | |
| 2919 | /* Set shared signature algorithms for SSL structures */ |
| 2920 | static int tls1_set_shared_sigalgs(SSL *s) |
| 2921 | { |
| 2922 | const unsigned char *pref, *allow, *conf; |
| 2923 | size_t preflen, allowlen, conflen; |
| 2924 | size_t nmatch; |
| 2925 | TLS_SIGALGS *salgs = NULL; |
| 2926 | CERT *c = s->cert; |
Adam Langley | db4f952 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2927 | if (c->shared_sigalgs) |
| 2928 | { |
| 2929 | OPENSSL_free(c->shared_sigalgs); |
| 2930 | c->shared_sigalgs = NULL; |
| 2931 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2932 | /* If client use client signature algorithms if not NULL */ |
David Benjamin | 335d10d | 2014-08-06 19:56:33 -0400 | [diff] [blame] | 2933 | if (!s->server && c->client_sigalgs) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2934 | { |
| 2935 | conf = c->client_sigalgs; |
| 2936 | conflen = c->client_sigalgslen; |
| 2937 | } |
David Benjamin | 335d10d | 2014-08-06 19:56:33 -0400 | [diff] [blame] | 2938 | else if (c->conf_sigalgs) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2939 | { |
| 2940 | conf = c->conf_sigalgs; |
| 2941 | conflen = c->conf_sigalgslen; |
| 2942 | } |
| 2943 | else |
| 2944 | conflen = tls12_get_psigalgs(s, &conf); |
David Benjamin | 335d10d | 2014-08-06 19:56:33 -0400 | [diff] [blame] | 2945 | if(s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2946 | { |
| 2947 | pref = conf; |
| 2948 | preflen = conflen; |
| 2949 | allow = c->peer_sigalgs; |
| 2950 | allowlen = c->peer_sigalgslen; |
| 2951 | } |
| 2952 | else |
| 2953 | { |
| 2954 | allow = conf; |
| 2955 | allowlen = conflen; |
| 2956 | pref = c->peer_sigalgs; |
| 2957 | preflen = c->peer_sigalgslen; |
| 2958 | } |
| 2959 | nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen); |
| 2960 | if (!nmatch) |
| 2961 | return 1; |
| 2962 | salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS)); |
| 2963 | if (!salgs) |
| 2964 | return 0; |
| 2965 | nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen); |
| 2966 | c->shared_sigalgs = salgs; |
| 2967 | c->shared_sigalgslen = nmatch; |
| 2968 | return 1; |
| 2969 | } |
| 2970 | |
| 2971 | |
| 2972 | /* Set preferred digest for each key type */ |
| 2973 | |
David Benjamin | cd99694 | 2014-07-20 16:23:51 -0400 | [diff] [blame] | 2974 | int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2975 | { |
| 2976 | int idx; |
| 2977 | size_t i; |
| 2978 | const EVP_MD *md; |
| 2979 | CERT *c = s->cert; |
| 2980 | TLS_SIGALGS *sigptr; |
David Benjamin | cd99694 | 2014-07-20 16:23:51 -0400 | [diff] [blame] | 2981 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2982 | /* Extension ignored for inappropriate versions */ |
| 2983 | if (!SSL_USE_SIGALGS(s)) |
| 2984 | return 1; |
Alex Chernyakhovsky | 31955f9 | 2014-07-05 01:12:34 -0400 | [diff] [blame] | 2985 | /* Length must be even */ |
David Benjamin | cd99694 | 2014-07-20 16:23:51 -0400 | [diff] [blame] | 2986 | if (CBS_len(sigalgs) % 2 != 0) |
Alex Chernyakhovsky | 31955f9 | 2014-07-05 01:12:34 -0400 | [diff] [blame] | 2987 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2988 | /* Should never happen */ |
| 2989 | if (!c) |
| 2990 | return 0; |
| 2991 | |
David Benjamin | cd99694 | 2014-07-20 16:23:51 -0400 | [diff] [blame] | 2992 | if (!CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2993 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2994 | |
| 2995 | tls1_set_shared_sigalgs(s); |
| 2996 | |
| 2997 | #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL |
| 2998 | if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) |
| 2999 | { |
| 3000 | /* Use first set signature preference to force message |
| 3001 | * digest, ignoring any peer preferences. |
| 3002 | */ |
| 3003 | const unsigned char *sigs = NULL; |
| 3004 | if (s->server) |
| 3005 | sigs = c->conf_sigalgs; |
| 3006 | else |
| 3007 | sigs = c->client_sigalgs; |
| 3008 | if (sigs) |
| 3009 | { |
| 3010 | idx = tls12_get_pkey_idx(sigs[1]); |
| 3011 | md = tls12_get_hash(sigs[0]); |
| 3012 | c->pkeys[idx].digest = md; |
| 3013 | c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN; |
| 3014 | if (idx == SSL_PKEY_RSA_SIGN) |
| 3015 | { |
| 3016 | c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN; |
| 3017 | c->pkeys[SSL_PKEY_RSA_ENC].digest = md; |
| 3018 | } |
| 3019 | } |
| 3020 | } |
| 3021 | #endif |
| 3022 | |
| 3023 | for (i = 0, sigptr = c->shared_sigalgs; |
| 3024 | i < c->shared_sigalgslen; i++, sigptr++) |
| 3025 | { |
| 3026 | idx = tls12_get_pkey_idx(sigptr->rsign); |
| 3027 | if (idx > 0 && c->pkeys[idx].digest == NULL) |
| 3028 | { |
| 3029 | md = tls12_get_hash(sigptr->rhash); |
| 3030 | c->pkeys[idx].digest = md; |
| 3031 | c->pkeys[idx].valid_flags = CERT_PKEY_EXPLICIT_SIGN; |
| 3032 | if (idx == SSL_PKEY_RSA_SIGN) |
| 3033 | { |
| 3034 | c->pkeys[SSL_PKEY_RSA_ENC].valid_flags = CERT_PKEY_EXPLICIT_SIGN; |
| 3035 | c->pkeys[SSL_PKEY_RSA_ENC].digest = md; |
| 3036 | } |
| 3037 | } |
| 3038 | |
| 3039 | } |
| 3040 | /* In strict mode leave unset digests as NULL to indicate we can't |
| 3041 | * use the certificate for signing. |
| 3042 | */ |
| 3043 | if (!(s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT)) |
| 3044 | { |
| 3045 | /* Set any remaining keys to default values. NOTE: if alg is |
| 3046 | * not supported it stays as NULL. |
| 3047 | */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3048 | if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest) |
| 3049 | { |
| 3050 | c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1(); |
| 3051 | c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1(); |
| 3052 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3053 | #ifndef OPENSSL_NO_ECDSA |
| 3054 | if (!c->pkeys[SSL_PKEY_ECC].digest) |
| 3055 | c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1(); |
| 3056 | #endif |
| 3057 | } |
| 3058 | return 1; |
| 3059 | } |
| 3060 | |
| 3061 | |
| 3062 | int SSL_get_sigalgs(SSL *s, int idx, |
| 3063 | int *psign, int *phash, int *psignhash, |
| 3064 | unsigned char *rsig, unsigned char *rhash) |
| 3065 | { |
| 3066 | const unsigned char *psig = s->cert->peer_sigalgs; |
| 3067 | if (psig == NULL) |
| 3068 | return 0; |
| 3069 | if (idx >= 0) |
| 3070 | { |
| 3071 | idx <<= 1; |
| 3072 | if (idx >= (int)s->cert->peer_sigalgslen) |
| 3073 | return 0; |
| 3074 | psig += idx; |
| 3075 | if (rhash) |
| 3076 | *rhash = psig[0]; |
| 3077 | if (rsig) |
| 3078 | *rsig = psig[1]; |
| 3079 | tls1_lookup_sigalg(phash, psign, psignhash, psig); |
| 3080 | } |
| 3081 | return s->cert->peer_sigalgslen / 2; |
| 3082 | } |
| 3083 | |
| 3084 | int SSL_get_shared_sigalgs(SSL *s, int idx, |
| 3085 | int *psign, int *phash, int *psignhash, |
| 3086 | unsigned char *rsig, unsigned char *rhash) |
| 3087 | { |
| 3088 | TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs; |
| 3089 | if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen) |
| 3090 | return 0; |
| 3091 | shsigalgs += idx; |
| 3092 | if (phash) |
| 3093 | *phash = shsigalgs->hash_nid; |
| 3094 | if (psign) |
| 3095 | *psign = shsigalgs->sign_nid; |
| 3096 | if (psignhash) |
| 3097 | *psignhash = shsigalgs->signandhash_nid; |
| 3098 | if (rsig) |
| 3099 | *rsig = shsigalgs->rsign; |
| 3100 | if (rhash) |
| 3101 | *rhash = shsigalgs->rhash; |
| 3102 | return s->cert->shared_sigalgslen; |
| 3103 | } |
| 3104 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3105 | /* tls1_channel_id_hash calculates the signed data for a Channel ID on the given |
| 3106 | * SSL connection and writes it to |md|. */ |
| 3107 | int |
| 3108 | tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) |
| 3109 | { |
| 3110 | EVP_MD_CTX ctx; |
| 3111 | unsigned char temp_digest[EVP_MAX_MD_SIZE]; |
| 3112 | unsigned temp_digest_len; |
| 3113 | int i; |
| 3114 | static const char kClientIDMagic[] = "TLS Channel ID signature"; |
| 3115 | |
| 3116 | if (s->s3->handshake_buffer) |
| 3117 | if (!ssl3_digest_cached_records(s)) |
| 3118 | return 0; |
| 3119 | |
| 3120 | EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic)); |
| 3121 | |
| 3122 | if (s->hit && s->s3->tlsext_channel_id_new) |
| 3123 | { |
| 3124 | static const char kResumptionMagic[] = "Resumption"; |
| 3125 | EVP_DigestUpdate(md, kResumptionMagic, |
| 3126 | sizeof(kResumptionMagic)); |
| 3127 | if (s->session->original_handshake_hash_len == 0) |
| 3128 | return 0; |
| 3129 | EVP_DigestUpdate(md, s->session->original_handshake_hash, |
| 3130 | s->session->original_handshake_hash_len); |
| 3131 | } |
| 3132 | |
| 3133 | EVP_MD_CTX_init(&ctx); |
| 3134 | for (i = 0; i < SSL_MAX_DIGEST; i++) |
| 3135 | { |
| 3136 | if (s->s3->handshake_dgst[i] == NULL) |
| 3137 | continue; |
| 3138 | EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]); |
| 3139 | EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len); |
| 3140 | EVP_DigestUpdate(md, temp_digest, temp_digest_len); |
| 3141 | } |
| 3142 | EVP_MD_CTX_cleanup(&ctx); |
| 3143 | |
| 3144 | return 1; |
| 3145 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3146 | |
| 3147 | /* tls1_record_handshake_hashes_for_channel_id records the current handshake |
| 3148 | * hashes in |s->session| so that Channel ID resumptions can sign that data. */ |
| 3149 | int tls1_record_handshake_hashes_for_channel_id(SSL *s) |
| 3150 | { |
| 3151 | int digest_len; |
| 3152 | /* This function should never be called for a resumed session because |
| 3153 | * the handshake hashes that we wish to record are for the original, |
| 3154 | * full handshake. */ |
| 3155 | if (s->hit) |
| 3156 | return -1; |
| 3157 | /* It only makes sense to call this function if Channel IDs have been |
| 3158 | * negotiated. */ |
| 3159 | if (!s->s3->tlsext_channel_id_new) |
| 3160 | return -1; |
| 3161 | |
| 3162 | digest_len = tls1_handshake_digest( |
| 3163 | s, s->session->original_handshake_hash, |
| 3164 | sizeof(s->session->original_handshake_hash)); |
| 3165 | if (digest_len < 0) |
| 3166 | return -1; |
| 3167 | |
| 3168 | s->session->original_handshake_hash_len = digest_len; |
| 3169 | |
| 3170 | return 1; |
| 3171 | } |
| 3172 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3173 | int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen, int client) |
| 3174 | { |
| 3175 | unsigned char *sigalgs, *sptr; |
| 3176 | int rhash, rsign; |
| 3177 | size_t i; |
| 3178 | if (salglen & 1) |
| 3179 | return 0; |
| 3180 | sigalgs = OPENSSL_malloc(salglen); |
| 3181 | if (sigalgs == NULL) |
| 3182 | return 0; |
| 3183 | for (i = 0, sptr = sigalgs; i < salglen; i+=2) |
| 3184 | { |
| 3185 | rhash = tls12_find_id(*psig_nids++, tls12_md, |
| 3186 | sizeof(tls12_md)/sizeof(tls12_lookup)); |
| 3187 | rsign = tls12_find_id(*psig_nids++, tls12_sig, |
| 3188 | sizeof(tls12_sig)/sizeof(tls12_lookup)); |
| 3189 | |
| 3190 | if (rhash == -1 || rsign == -1) |
| 3191 | goto err; |
| 3192 | *sptr++ = rhash; |
| 3193 | *sptr++ = rsign; |
| 3194 | } |
| 3195 | |
| 3196 | if (client) |
| 3197 | { |
| 3198 | if (c->client_sigalgs) |
| 3199 | OPENSSL_free(c->client_sigalgs); |
| 3200 | c->client_sigalgs = sigalgs; |
| 3201 | c->client_sigalgslen = salglen; |
| 3202 | } |
| 3203 | else |
| 3204 | { |
| 3205 | if (c->conf_sigalgs) |
| 3206 | OPENSSL_free(c->conf_sigalgs); |
| 3207 | c->conf_sigalgs = sigalgs; |
| 3208 | c->conf_sigalgslen = salglen; |
| 3209 | } |
| 3210 | |
| 3211 | return 1; |
| 3212 | |
| 3213 | err: |
| 3214 | OPENSSL_free(sigalgs); |
| 3215 | return 0; |
| 3216 | } |
| 3217 | |
| 3218 | static int tls1_check_sig_alg(CERT *c, X509 *x, int default_nid) |
| 3219 | { |
| 3220 | int sig_nid; |
| 3221 | size_t i; |
| 3222 | if (default_nid == -1) |
| 3223 | return 1; |
| 3224 | sig_nid = X509_get_signature_nid(x); |
| 3225 | if (default_nid) |
| 3226 | return sig_nid == default_nid ? 1 : 0; |
| 3227 | for (i = 0; i < c->shared_sigalgslen; i++) |
| 3228 | if (sig_nid == c->shared_sigalgs[i].signandhash_nid) |
| 3229 | return 1; |
| 3230 | return 0; |
| 3231 | } |
| 3232 | /* Check to see if a certificate issuer name matches list of CA names */ |
| 3233 | static int ssl_check_ca_name(STACK_OF(X509_NAME) *names, X509 *x) |
| 3234 | { |
| 3235 | X509_NAME *nm; |
| 3236 | int i; |
| 3237 | nm = X509_get_issuer_name(x); |
| 3238 | for (i = 0; i < sk_X509_NAME_num(names); i++) |
| 3239 | { |
| 3240 | if(!X509_NAME_cmp(nm, sk_X509_NAME_value(names, i))) |
| 3241 | return 1; |
| 3242 | } |
| 3243 | return 0; |
| 3244 | } |
| 3245 | |
| 3246 | /* Check certificate chain is consistent with TLS extensions and is |
| 3247 | * usable by server. This servers two purposes: it allows users to |
| 3248 | * check chains before passing them to the server and it allows the |
| 3249 | * server to check chains before attempting to use them. |
| 3250 | */ |
| 3251 | |
| 3252 | /* Flags which need to be set for a certificate when stict mode not set */ |
| 3253 | |
| 3254 | #define CERT_PKEY_VALID_FLAGS \ |
| 3255 | (CERT_PKEY_EE_SIGNATURE|CERT_PKEY_EE_PARAM) |
| 3256 | /* Strict mode flags */ |
| 3257 | #define CERT_PKEY_STRICT_FLAGS \ |
| 3258 | (CERT_PKEY_VALID_FLAGS|CERT_PKEY_CA_SIGNATURE|CERT_PKEY_CA_PARAM \ |
| 3259 | | CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE) |
| 3260 | |
| 3261 | int tls1_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain, |
| 3262 | int idx) |
| 3263 | { |
| 3264 | int i; |
| 3265 | int rv = 0; |
| 3266 | int check_flags = 0, strict_mode; |
| 3267 | CERT_PKEY *cpk = NULL; |
| 3268 | CERT *c = s->cert; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3269 | /* idx == -1 means checking server chains */ |
| 3270 | if (idx != -1) |
| 3271 | { |
| 3272 | /* idx == -2 means checking client certificate chains */ |
| 3273 | if (idx == -2) |
| 3274 | { |
| 3275 | cpk = c->key; |
| 3276 | idx = cpk - c->pkeys; |
| 3277 | } |
| 3278 | else |
| 3279 | cpk = c->pkeys + idx; |
| 3280 | x = cpk->x509; |
| 3281 | pk = cpk->privatekey; |
| 3282 | chain = cpk->chain; |
| 3283 | strict_mode = c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT; |
| 3284 | /* If no cert or key, forget it */ |
| 3285 | if (!x || !pk) |
| 3286 | goto end; |
| 3287 | #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL |
| 3288 | /* Allow any certificate to pass test */ |
| 3289 | if (s->cert->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) |
| 3290 | { |
| 3291 | rv = CERT_PKEY_STRICT_FLAGS|CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_VALID|CERT_PKEY_SIGN; |
| 3292 | cpk->valid_flags = rv; |
| 3293 | return rv; |
| 3294 | } |
| 3295 | #endif |
| 3296 | } |
| 3297 | else |
| 3298 | { |
| 3299 | if (!x || !pk) |
| 3300 | goto end; |
| 3301 | idx = ssl_cert_type(x, pk); |
| 3302 | if (idx == -1) |
| 3303 | goto end; |
| 3304 | cpk = c->pkeys + idx; |
| 3305 | if (c->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT) |
| 3306 | check_flags = CERT_PKEY_STRICT_FLAGS; |
| 3307 | else |
| 3308 | check_flags = CERT_PKEY_VALID_FLAGS; |
| 3309 | strict_mode = 1; |
| 3310 | } |
| 3311 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3312 | /* Check all signature algorithms are consistent with |
| 3313 | * signature algorithms extension if TLS 1.2 or later |
| 3314 | * and strict mode. |
| 3315 | */ |
| 3316 | if (TLS1_get_version(s) >= TLS1_2_VERSION && strict_mode) |
| 3317 | { |
| 3318 | int default_nid; |
| 3319 | unsigned char rsign = 0; |
| 3320 | if (c->peer_sigalgs) |
| 3321 | default_nid = 0; |
| 3322 | /* If no sigalgs extension use defaults from RFC5246 */ |
| 3323 | else |
| 3324 | { |
| 3325 | switch(idx) |
| 3326 | { |
| 3327 | case SSL_PKEY_RSA_ENC: |
| 3328 | case SSL_PKEY_RSA_SIGN: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3329 | rsign = TLSEXT_signature_rsa; |
| 3330 | default_nid = NID_sha1WithRSAEncryption; |
| 3331 | break; |
| 3332 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3333 | case SSL_PKEY_ECC: |
| 3334 | rsign = TLSEXT_signature_ecdsa; |
| 3335 | default_nid = NID_ecdsa_with_SHA1; |
| 3336 | break; |
| 3337 | |
| 3338 | default: |
| 3339 | default_nid = -1; |
| 3340 | break; |
| 3341 | } |
| 3342 | } |
| 3343 | /* If peer sent no signature algorithms extension and we |
| 3344 | * have set preferred signature algorithms check we support |
| 3345 | * sha1. |
| 3346 | */ |
| 3347 | if (default_nid > 0 && c->conf_sigalgs) |
| 3348 | { |
| 3349 | size_t j; |
| 3350 | const unsigned char *p = c->conf_sigalgs; |
| 3351 | for (j = 0; j < c->conf_sigalgslen; j += 2, p += 2) |
| 3352 | { |
| 3353 | if (p[0] == TLSEXT_hash_sha1 && p[1] == rsign) |
| 3354 | break; |
| 3355 | } |
| 3356 | if (j == c->conf_sigalgslen) |
| 3357 | { |
| 3358 | if (check_flags) |
| 3359 | goto skip_sigs; |
| 3360 | else |
| 3361 | goto end; |
| 3362 | } |
| 3363 | } |
| 3364 | /* Check signature algorithm of each cert in chain */ |
| 3365 | if (!tls1_check_sig_alg(c, x, default_nid)) |
| 3366 | { |
| 3367 | if (!check_flags) goto end; |
| 3368 | } |
| 3369 | else |
| 3370 | rv |= CERT_PKEY_EE_SIGNATURE; |
| 3371 | rv |= CERT_PKEY_CA_SIGNATURE; |
| 3372 | for (i = 0; i < sk_X509_num(chain); i++) |
| 3373 | { |
| 3374 | if (!tls1_check_sig_alg(c, sk_X509_value(chain, i), |
| 3375 | default_nid)) |
| 3376 | { |
| 3377 | if (check_flags) |
| 3378 | { |
| 3379 | rv &= ~CERT_PKEY_CA_SIGNATURE; |
| 3380 | break; |
| 3381 | } |
| 3382 | else |
| 3383 | goto end; |
| 3384 | } |
| 3385 | } |
| 3386 | } |
| 3387 | /* Else not TLS 1.2, so mark EE and CA signing algorithms OK */ |
| 3388 | else if(check_flags) |
| 3389 | rv |= CERT_PKEY_EE_SIGNATURE|CERT_PKEY_CA_SIGNATURE; |
| 3390 | skip_sigs: |
| 3391 | /* Check cert parameters are consistent */ |
| 3392 | if (tls1_check_cert_param(s, x, check_flags ? 1 : 2)) |
| 3393 | rv |= CERT_PKEY_EE_PARAM; |
| 3394 | else if (!check_flags) |
| 3395 | goto end; |
| 3396 | if (!s->server) |
| 3397 | rv |= CERT_PKEY_CA_PARAM; |
| 3398 | /* In strict mode check rest of chain too */ |
| 3399 | else if (strict_mode) |
| 3400 | { |
| 3401 | rv |= CERT_PKEY_CA_PARAM; |
| 3402 | for (i = 0; i < sk_X509_num(chain); i++) |
| 3403 | { |
| 3404 | X509 *ca = sk_X509_value(chain, i); |
| 3405 | if (!tls1_check_cert_param(s, ca, 0)) |
| 3406 | { |
| 3407 | if (check_flags) |
| 3408 | { |
| 3409 | rv &= ~CERT_PKEY_CA_PARAM; |
| 3410 | break; |
| 3411 | } |
| 3412 | else |
| 3413 | goto end; |
| 3414 | } |
| 3415 | } |
| 3416 | } |
| 3417 | if (!s->server && strict_mode) |
| 3418 | { |
| 3419 | STACK_OF(X509_NAME) *ca_dn; |
David Benjamin | 676d1e7 | 2014-07-08 14:34:10 -0400 | [diff] [blame] | 3420 | uint8_t check_type = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3421 | switch (pk->type) |
| 3422 | { |
| 3423 | case EVP_PKEY_RSA: |
| 3424 | check_type = TLS_CT_RSA_SIGN; |
| 3425 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3426 | case EVP_PKEY_EC: |
| 3427 | check_type = TLS_CT_ECDSA_SIGN; |
| 3428 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3429 | } |
| 3430 | if (check_type) |
| 3431 | { |
David Benjamin | 676d1e7 | 2014-07-08 14:34:10 -0400 | [diff] [blame] | 3432 | if (s->s3->tmp.certificate_types && |
| 3433 | memchr(s->s3->tmp.certificate_types, check_type, s->s3->tmp.num_certificate_types)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3434 | { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3435 | rv |= CERT_PKEY_CERT_TYPE; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3436 | } |
| 3437 | if (!(rv & CERT_PKEY_CERT_TYPE) && !check_flags) |
| 3438 | goto end; |
| 3439 | } |
| 3440 | else |
| 3441 | rv |= CERT_PKEY_CERT_TYPE; |
| 3442 | |
| 3443 | |
| 3444 | ca_dn = s->s3->tmp.ca_names; |
| 3445 | |
| 3446 | if (!sk_X509_NAME_num(ca_dn)) |
| 3447 | rv |= CERT_PKEY_ISSUER_NAME; |
| 3448 | |
| 3449 | if (!(rv & CERT_PKEY_ISSUER_NAME)) |
| 3450 | { |
| 3451 | if (ssl_check_ca_name(ca_dn, x)) |
| 3452 | rv |= CERT_PKEY_ISSUER_NAME; |
| 3453 | } |
| 3454 | if (!(rv & CERT_PKEY_ISSUER_NAME)) |
| 3455 | { |
| 3456 | for (i = 0; i < sk_X509_num(chain); i++) |
| 3457 | { |
| 3458 | X509 *xtmp = sk_X509_value(chain, i); |
| 3459 | if (ssl_check_ca_name(ca_dn, xtmp)) |
| 3460 | { |
| 3461 | rv |= CERT_PKEY_ISSUER_NAME; |
| 3462 | break; |
| 3463 | } |
| 3464 | } |
| 3465 | } |
| 3466 | if (!check_flags && !(rv & CERT_PKEY_ISSUER_NAME)) |
| 3467 | goto end; |
| 3468 | } |
| 3469 | else |
| 3470 | rv |= CERT_PKEY_ISSUER_NAME|CERT_PKEY_CERT_TYPE; |
| 3471 | |
| 3472 | if (!check_flags || (rv & check_flags) == check_flags) |
| 3473 | rv |= CERT_PKEY_VALID; |
| 3474 | |
| 3475 | end: |
| 3476 | |
| 3477 | if (TLS1_get_version(s) >= TLS1_2_VERSION) |
| 3478 | { |
| 3479 | if (cpk->valid_flags & CERT_PKEY_EXPLICIT_SIGN) |
| 3480 | rv |= CERT_PKEY_EXPLICIT_SIGN|CERT_PKEY_SIGN; |
| 3481 | else if (cpk->digest) |
| 3482 | rv |= CERT_PKEY_SIGN; |
| 3483 | } |
| 3484 | else |
| 3485 | rv |= CERT_PKEY_SIGN|CERT_PKEY_EXPLICIT_SIGN; |
| 3486 | |
| 3487 | /* When checking a CERT_PKEY structure all flags are irrelevant |
| 3488 | * if the chain is invalid. |
| 3489 | */ |
| 3490 | if (!check_flags) |
| 3491 | { |
| 3492 | if (rv & CERT_PKEY_VALID) |
| 3493 | cpk->valid_flags = rv; |
| 3494 | else |
| 3495 | { |
| 3496 | /* Preserve explicit sign flag, clear rest */ |
| 3497 | cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN; |
| 3498 | return 0; |
| 3499 | } |
| 3500 | } |
| 3501 | return rv; |
| 3502 | } |
| 3503 | |
| 3504 | /* Set validity of certificates in an SSL structure */ |
| 3505 | void tls1_set_cert_validity(SSL *s) |
| 3506 | { |
| 3507 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_ENC); |
| 3508 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_RSA_SIGN); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3509 | tls1_check_chain(s, NULL, NULL, NULL, SSL_PKEY_ECC); |
| 3510 | } |
| 3511 | /* User level utiity function to check a chain is suitable */ |
| 3512 | int SSL_check_chain(SSL *s, X509 *x, EVP_PKEY *pk, STACK_OF(X509) *chain) |
| 3513 | { |
| 3514 | return tls1_check_chain(s, x, pk, chain, -1); |
| 3515 | } |
| 3516 | |