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