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