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