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