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