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