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