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