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