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