The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1 | /* ssl/t1_lib.c */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This package is an SSL implementation written |
| 6 | * by Eric Young (eay@cryptsoft.com). |
| 7 | * The implementation was written so as to conform with Netscapes SSL. |
| 8 | * |
| 9 | * This library is free for commercial and non-commercial use as long as |
| 10 | * the following conditions are aheared to. The following conditions |
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 13 | * included with this distribution is covered by the same copyright terms |
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 15 | * |
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 17 | * the code are not to be removed. |
| 18 | * If this package is used in a product, Eric Young should be given attribution |
| 19 | * as the author of the parts of the library used. |
| 20 | * This can be in the form of a textual message at program startup or |
| 21 | * in documentation (online or textual) provided with the package. |
| 22 | * |
| 23 | * Redistribution and use in source and binary forms, with or without |
| 24 | * modification, are permitted provided that the following conditions |
| 25 | * are met: |
| 26 | * 1. Redistributions of source code must retain the copyright |
| 27 | * notice, this list of conditions and the following disclaimer. |
| 28 | * 2. Redistributions in binary form must reproduce the above copyright |
| 29 | * notice, this list of conditions and the following disclaimer in the |
| 30 | * documentation and/or other materials provided with the distribution. |
| 31 | * 3. All advertising materials mentioning features or use of this software |
| 32 | * must display the following acknowledgement: |
| 33 | * "This product includes cryptographic software written by |
| 34 | * Eric Young (eay@cryptsoft.com)" |
| 35 | * The word 'cryptographic' can be left out if the rouines from the library |
| 36 | * being used are not cryptographic related :-). |
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 38 | * the apps directory (application code) you must include an acknowledgement: |
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 40 | * |
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 51 | * SUCH DAMAGE. |
| 52 | * |
| 53 | * The licence and distribution terms for any publically available version or |
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 55 | * copied and put under another distribution licence |
| 56 | * [including the GNU Public Licence.] |
| 57 | */ |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 58 | /* ==================================================================== |
| 59 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 60 | * |
| 61 | * Redistribution and use in source and binary forms, with or without |
| 62 | * modification, are permitted provided that the following conditions |
| 63 | * are met: |
| 64 | * |
| 65 | * 1. Redistributions of source code must retain the above copyright |
| 66 | * notice, this list of conditions and the following disclaimer. |
| 67 | * |
| 68 | * 2. Redistributions in binary form must reproduce the above copyright |
| 69 | * notice, this list of conditions and the following disclaimer in |
| 70 | * the documentation and/or other materials provided with the |
| 71 | * distribution. |
| 72 | * |
| 73 | * 3. All advertising materials mentioning features or use of this |
| 74 | * software must display the following acknowledgment: |
| 75 | * "This product includes software developed by the OpenSSL Project |
| 76 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 77 | * |
| 78 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 79 | * endorse or promote products derived from this software without |
| 80 | * prior written permission. For written permission, please contact |
| 81 | * openssl-core@openssl.org. |
| 82 | * |
| 83 | * 5. Products derived from this software may not be called "OpenSSL" |
| 84 | * nor may "OpenSSL" appear in their names without prior written |
| 85 | * permission of the OpenSSL Project. |
| 86 | * |
| 87 | * 6. Redistributions of any form whatsoever must retain the following |
| 88 | * acknowledgment: |
| 89 | * "This product includes software developed by the OpenSSL Project |
| 90 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 91 | * |
| 92 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 93 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 94 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 95 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 96 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 97 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 98 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 99 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 100 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 101 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 102 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 103 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 104 | * ==================================================================== |
| 105 | * |
| 106 | * This product includes cryptographic software written by Eric Young |
| 107 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 108 | * Hudson (tjh@cryptsoft.com). |
| 109 | * |
| 110 | */ |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 111 | |
| 112 | #include <stdio.h> |
| 113 | #include <openssl/objects.h> |
| 114 | #include <openssl/evp.h> |
| 115 | #include <openssl/hmac.h> |
| 116 | #include <openssl/ocsp.h> |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 117 | #include <openssl/rand.h> |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 118 | #include "ssl_locl.h" |
| 119 | |
| 120 | const char tls1_version_str[]="TLSv1" OPENSSL_VERSION_PTEXT; |
| 121 | |
| 122 | #ifndef OPENSSL_NO_TLSEXT |
| 123 | static int tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen, |
| 124 | const unsigned char *sess_id, int sesslen, |
| 125 | SSL_SESSION **psess); |
| 126 | #endif |
| 127 | |
| 128 | SSL3_ENC_METHOD TLSv1_enc_data={ |
| 129 | tls1_enc, |
| 130 | tls1_mac, |
| 131 | tls1_setup_key_block, |
| 132 | tls1_generate_master_secret, |
| 133 | tls1_change_cipher_state, |
| 134 | tls1_final_finish_mac, |
| 135 | TLS1_FINISH_MAC_LENGTH, |
| 136 | tls1_cert_verify_mac, |
| 137 | TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 138 | TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 139 | tls1_alert_code, |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 140 | tls1_export_keying_material, |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | long tls1_default_timeout(void) |
| 144 | { |
| 145 | /* 2 hours, the 24 hours mentioned in the TLSv1 spec |
| 146 | * is way too long for http, the cache would over fill */ |
| 147 | return(60*60*2); |
| 148 | } |
| 149 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 150 | int tls1_new(SSL *s) |
| 151 | { |
| 152 | if (!ssl3_new(s)) return(0); |
| 153 | s->method->ssl_clear(s); |
| 154 | return(1); |
| 155 | } |
| 156 | |
| 157 | void tls1_free(SSL *s) |
| 158 | { |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 159 | #ifndef OPENSSL_NO_TLSEXT |
| 160 | if (s->tlsext_session_ticket) |
| 161 | { |
| 162 | OPENSSL_free(s->tlsext_session_ticket); |
| 163 | } |
| 164 | #endif /* OPENSSL_NO_TLSEXT */ |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 165 | ssl3_free(s); |
| 166 | } |
| 167 | |
| 168 | void tls1_clear(SSL *s) |
| 169 | { |
| 170 | ssl3_clear(s); |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 171 | s->version = s->method->version; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 174 | #ifndef OPENSSL_NO_EC |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 175 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 176 | static int nid_list[] = |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 177 | { |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 178 | NID_sect163k1, /* sect163k1 (1) */ |
| 179 | NID_sect163r1, /* sect163r1 (2) */ |
| 180 | NID_sect163r2, /* sect163r2 (3) */ |
| 181 | NID_sect193r1, /* sect193r1 (4) */ |
| 182 | NID_sect193r2, /* sect193r2 (5) */ |
| 183 | NID_sect233k1, /* sect233k1 (6) */ |
| 184 | NID_sect233r1, /* sect233r1 (7) */ |
| 185 | NID_sect239k1, /* sect239k1 (8) */ |
| 186 | NID_sect283k1, /* sect283k1 (9) */ |
| 187 | NID_sect283r1, /* sect283r1 (10) */ |
| 188 | NID_sect409k1, /* sect409k1 (11) */ |
| 189 | NID_sect409r1, /* sect409r1 (12) */ |
| 190 | NID_sect571k1, /* sect571k1 (13) */ |
| 191 | NID_sect571r1, /* sect571r1 (14) */ |
| 192 | NID_secp160k1, /* secp160k1 (15) */ |
| 193 | NID_secp160r1, /* secp160r1 (16) */ |
| 194 | NID_secp160r2, /* secp160r2 (17) */ |
| 195 | NID_secp192k1, /* secp192k1 (18) */ |
| 196 | NID_X9_62_prime192v1, /* secp192r1 (19) */ |
| 197 | NID_secp224k1, /* secp224k1 (20) */ |
| 198 | NID_secp224r1, /* secp224r1 (21) */ |
| 199 | NID_secp256k1, /* secp256k1 (22) */ |
| 200 | NID_X9_62_prime256v1, /* secp256r1 (23) */ |
| 201 | NID_secp384r1, /* secp384r1 (24) */ |
| 202 | NID_secp521r1 /* secp521r1 (25) */ |
| 203 | }; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 204 | |
| 205 | static int pref_list[] = |
| 206 | { |
| 207 | NID_sect571r1, /* sect571r1 (14) */ |
| 208 | NID_sect571k1, /* sect571k1 (13) */ |
| 209 | NID_secp521r1, /* secp521r1 (25) */ |
| 210 | NID_sect409k1, /* sect409k1 (11) */ |
| 211 | NID_sect409r1, /* sect409r1 (12) */ |
| 212 | NID_secp384r1, /* secp384r1 (24) */ |
| 213 | NID_sect283k1, /* sect283k1 (9) */ |
| 214 | NID_sect283r1, /* sect283r1 (10) */ |
| 215 | NID_secp256k1, /* secp256k1 (22) */ |
| 216 | NID_X9_62_prime256v1, /* secp256r1 (23) */ |
| 217 | NID_sect239k1, /* sect239k1 (8) */ |
| 218 | NID_sect233k1, /* sect233k1 (6) */ |
| 219 | NID_sect233r1, /* sect233r1 (7) */ |
| 220 | NID_secp224k1, /* secp224k1 (20) */ |
| 221 | NID_secp224r1, /* secp224r1 (21) */ |
| 222 | NID_sect193r1, /* sect193r1 (4) */ |
| 223 | NID_sect193r2, /* sect193r2 (5) */ |
| 224 | NID_secp192k1, /* secp192k1 (18) */ |
| 225 | NID_X9_62_prime192v1, /* secp192r1 (19) */ |
| 226 | NID_sect163k1, /* sect163k1 (1) */ |
| 227 | NID_sect163r1, /* sect163r1 (2) */ |
| 228 | NID_sect163r2, /* sect163r2 (3) */ |
| 229 | NID_secp160k1, /* secp160k1 (15) */ |
| 230 | NID_secp160r1, /* secp160r1 (16) */ |
| 231 | NID_secp160r2, /* secp160r2 (17) */ |
| 232 | }; |
| 233 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 234 | int tls1_ec_curve_id2nid(int curve_id) |
| 235 | { |
| 236 | /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */ |
| 237 | if ((curve_id < 1) || ((unsigned int)curve_id > |
| 238 | sizeof(nid_list)/sizeof(nid_list[0]))) |
| 239 | return 0; |
| 240 | return nid_list[curve_id-1]; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 243 | int tls1_ec_nid2curve_id(int nid) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 244 | { |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 245 | /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */ |
| 246 | switch (nid) |
| 247 | { |
| 248 | case NID_sect163k1: /* sect163k1 (1) */ |
| 249 | return 1; |
| 250 | case NID_sect163r1: /* sect163r1 (2) */ |
| 251 | return 2; |
| 252 | case NID_sect163r2: /* sect163r2 (3) */ |
| 253 | return 3; |
| 254 | case NID_sect193r1: /* sect193r1 (4) */ |
| 255 | return 4; |
| 256 | case NID_sect193r2: /* sect193r2 (5) */ |
| 257 | return 5; |
| 258 | case NID_sect233k1: /* sect233k1 (6) */ |
| 259 | return 6; |
| 260 | case NID_sect233r1: /* sect233r1 (7) */ |
| 261 | return 7; |
| 262 | case NID_sect239k1: /* sect239k1 (8) */ |
| 263 | return 8; |
| 264 | case NID_sect283k1: /* sect283k1 (9) */ |
| 265 | return 9; |
| 266 | case NID_sect283r1: /* sect283r1 (10) */ |
| 267 | return 10; |
| 268 | case NID_sect409k1: /* sect409k1 (11) */ |
| 269 | return 11; |
| 270 | case NID_sect409r1: /* sect409r1 (12) */ |
| 271 | return 12; |
| 272 | case NID_sect571k1: /* sect571k1 (13) */ |
| 273 | return 13; |
| 274 | case NID_sect571r1: /* sect571r1 (14) */ |
| 275 | return 14; |
| 276 | case NID_secp160k1: /* secp160k1 (15) */ |
| 277 | return 15; |
| 278 | case NID_secp160r1: /* secp160r1 (16) */ |
| 279 | return 16; |
| 280 | case NID_secp160r2: /* secp160r2 (17) */ |
| 281 | return 17; |
| 282 | case NID_secp192k1: /* secp192k1 (18) */ |
| 283 | return 18; |
| 284 | case NID_X9_62_prime192v1: /* secp192r1 (19) */ |
| 285 | return 19; |
| 286 | case NID_secp224k1: /* secp224k1 (20) */ |
| 287 | return 20; |
| 288 | case NID_secp224r1: /* secp224r1 (21) */ |
| 289 | return 21; |
| 290 | case NID_secp256k1: /* secp256k1 (22) */ |
| 291 | return 22; |
| 292 | case NID_X9_62_prime256v1: /* secp256r1 (23) */ |
| 293 | return 23; |
| 294 | case NID_secp384r1: /* secp384r1 (24) */ |
| 295 | return 24; |
| 296 | case NID_secp521r1: /* secp521r1 (25) */ |
| 297 | return 25; |
| 298 | default: |
| 299 | return 0; |
| 300 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 301 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 302 | #endif /* OPENSSL_NO_EC */ |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 303 | |
| 304 | #ifndef OPENSSL_NO_TLSEXT |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 305 | |
| 306 | /* List of supported signature algorithms and hashes. Should make this |
| 307 | * customisable at some point, for now include everything we support. |
| 308 | */ |
| 309 | |
| 310 | #ifdef OPENSSL_NO_RSA |
| 311 | #define tlsext_sigalg_rsa(md) /* */ |
| 312 | #else |
| 313 | #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa, |
| 314 | #endif |
| 315 | |
| 316 | #ifdef OPENSSL_NO_DSA |
| 317 | #define tlsext_sigalg_dsa(md) /* */ |
| 318 | #else |
| 319 | #define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa, |
| 320 | #endif |
| 321 | |
| 322 | #ifdef OPENSSL_NO_ECDSA |
| 323 | #define tlsext_sigalg_ecdsa(md) /* */ |
| 324 | #else |
| 325 | #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa, |
| 326 | #endif |
| 327 | |
| 328 | #define tlsext_sigalg(md) \ |
| 329 | tlsext_sigalg_rsa(md) \ |
| 330 | tlsext_sigalg_dsa(md) \ |
| 331 | tlsext_sigalg_ecdsa(md) |
| 332 | |
| 333 | static unsigned char tls12_sigalgs[] = { |
| 334 | #ifndef OPENSSL_NO_SHA512 |
| 335 | tlsext_sigalg(TLSEXT_hash_sha512) |
| 336 | tlsext_sigalg(TLSEXT_hash_sha384) |
| 337 | #endif |
| 338 | #ifndef OPENSSL_NO_SHA256 |
| 339 | tlsext_sigalg(TLSEXT_hash_sha256) |
| 340 | tlsext_sigalg(TLSEXT_hash_sha224) |
| 341 | #endif |
| 342 | #ifndef OPENSSL_NO_SHA |
| 343 | tlsext_sigalg(TLSEXT_hash_sha1) |
| 344 | #endif |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 345 | }; |
| 346 | |
| 347 | int tls12_get_req_sig_algs(SSL *s, unsigned char *p) |
| 348 | { |
| 349 | size_t slen = sizeof(tls12_sigalgs); |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 350 | if (p) |
| 351 | memcpy(p, tls12_sigalgs, slen); |
| 352 | return (int)slen; |
| 353 | } |
| 354 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 355 | unsigned char *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit) |
| 356 | { |
| 357 | int extdatalen=0; |
| 358 | unsigned char *ret = p; |
| 359 | |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 360 | /* don't add extensions for SSLv3 unless doing secure renegotiation */ |
| 361 | if (s->client_version == SSL3_VERSION |
| 362 | && !s->s3->send_connection_binding) |
| 363 | return p; |
| 364 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 365 | ret+=2; |
| 366 | |
| 367 | if (ret>=limit) return NULL; /* this really never occurs, but ... */ |
| 368 | |
| 369 | if (s->tlsext_hostname != NULL) |
| 370 | { |
| 371 | /* Add TLS extension servername to the Client Hello message */ |
| 372 | unsigned long size_str; |
| 373 | long lenmax; |
| 374 | |
| 375 | /* check for enough space. |
| 376 | 4 for the servername type and entension length |
| 377 | 2 for servernamelist length |
| 378 | 1 for the hostname type |
| 379 | 2 for hostname length |
| 380 | + hostname length |
| 381 | */ |
| 382 | |
| 383 | if ((lenmax = limit - ret - 9) < 0 |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 384 | || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 385 | return NULL; |
| 386 | |
| 387 | /* extension type and length */ |
| 388 | s2n(TLSEXT_TYPE_server_name,ret); |
| 389 | s2n(size_str+5,ret); |
| 390 | |
| 391 | /* length of servername list */ |
| 392 | s2n(size_str+3,ret); |
| 393 | |
| 394 | /* hostname type, length and hostname */ |
| 395 | *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name; |
| 396 | s2n(size_str,ret); |
| 397 | memcpy(ret, s->tlsext_hostname, size_str); |
| 398 | ret+=size_str; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 399 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 400 | |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 401 | /* Add RI if renegotiating */ |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 402 | if (s->renegotiate) |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 403 | { |
| 404 | int el; |
| 405 | |
| 406 | if(!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) |
| 407 | { |
| 408 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 409 | return NULL; |
| 410 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 411 | |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 412 | if((limit - p - 4 - el) < 0) return NULL; |
| 413 | |
| 414 | s2n(TLSEXT_TYPE_renegotiate,ret); |
| 415 | s2n(el,ret); |
| 416 | |
| 417 | if(!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) |
| 418 | { |
| 419 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | ret += el; |
| 424 | } |
| 425 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 426 | #ifndef OPENSSL_NO_SRP |
| 427 | /* Add SRP username if there is one */ |
| 428 | if (s->srp_ctx.login != NULL) |
| 429 | { /* Add TLS extension SRP username to the Client Hello message */ |
| 430 | |
| 431 | int login_len = strlen(s->srp_ctx.login); |
| 432 | if (login_len > 255 || login_len == 0) |
| 433 | { |
| 434 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 435 | return NULL; |
| 436 | } |
| 437 | |
| 438 | /* check for enough space. |
| 439 | 4 for the srp type type and entension length |
| 440 | 1 for the srp user identity |
| 441 | + srp user identity length |
| 442 | */ |
| 443 | if ((limit - ret - 5 - login_len) < 0) return NULL; |
| 444 | |
| 445 | /* fill in the extension */ |
| 446 | s2n(TLSEXT_TYPE_srp,ret); |
| 447 | s2n(login_len+1,ret); |
| 448 | (*ret++) = (unsigned char) login_len; |
| 449 | memcpy(ret, s->srp_ctx.login, login_len); |
| 450 | ret+=login_len; |
| 451 | } |
| 452 | #endif |
| 453 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 454 | #ifndef OPENSSL_NO_EC |
| 455 | if (s->tlsext_ecpointformatlist != NULL && |
| 456 | s->version != DTLS1_VERSION) |
| 457 | { |
| 458 | /* Add TLS extension ECPointFormats to the ClientHello message */ |
| 459 | long lenmax; |
| 460 | |
| 461 | if ((lenmax = limit - ret - 5) < 0) return NULL; |
| 462 | if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL; |
| 463 | if (s->tlsext_ecpointformatlist_length > 255) |
| 464 | { |
| 465 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 466 | return NULL; |
| 467 | } |
| 468 | |
| 469 | s2n(TLSEXT_TYPE_ec_point_formats,ret); |
| 470 | s2n(s->tlsext_ecpointformatlist_length + 1,ret); |
| 471 | *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length; |
| 472 | memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length); |
| 473 | ret+=s->tlsext_ecpointformatlist_length; |
| 474 | } |
| 475 | if (s->tlsext_ellipticcurvelist != NULL && |
| 476 | s->version != DTLS1_VERSION) |
| 477 | { |
| 478 | /* Add TLS extension EllipticCurves to the ClientHello message */ |
| 479 | long lenmax; |
| 480 | |
| 481 | if ((lenmax = limit - ret - 6) < 0) return NULL; |
| 482 | if (s->tlsext_ellipticcurvelist_length > (unsigned long)lenmax) return NULL; |
| 483 | if (s->tlsext_ellipticcurvelist_length > 65532) |
| 484 | { |
| 485 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 486 | return NULL; |
| 487 | } |
| 488 | |
| 489 | s2n(TLSEXT_TYPE_elliptic_curves,ret); |
| 490 | s2n(s->tlsext_ellipticcurvelist_length + 2, ret); |
| 491 | |
| 492 | /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for |
| 493 | * elliptic_curve_list, but the examples use two bytes. |
| 494 | * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html |
| 495 | * resolves this to two bytes. |
| 496 | */ |
| 497 | s2n(s->tlsext_ellipticcurvelist_length, ret); |
| 498 | memcpy(ret, s->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist_length); |
| 499 | ret+=s->tlsext_ellipticcurvelist_length; |
| 500 | } |
| 501 | #endif /* OPENSSL_NO_EC */ |
| 502 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 503 | if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) |
| 504 | { |
| 505 | int ticklen; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 506 | if (!s->new_session && s->session && s->session->tlsext_tick) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 507 | ticklen = s->session->tlsext_ticklen; |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 508 | else if (s->session && s->tlsext_session_ticket && |
| 509 | s->tlsext_session_ticket->data) |
| 510 | { |
| 511 | ticklen = s->tlsext_session_ticket->length; |
| 512 | s->session->tlsext_tick = OPENSSL_malloc(ticklen); |
| 513 | if (!s->session->tlsext_tick) |
| 514 | return NULL; |
| 515 | memcpy(s->session->tlsext_tick, |
| 516 | s->tlsext_session_ticket->data, |
| 517 | ticklen); |
| 518 | s->session->tlsext_ticklen = ticklen; |
| 519 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 520 | else |
| 521 | ticklen = 0; |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 522 | if (ticklen == 0 && s->tlsext_session_ticket && |
| 523 | s->tlsext_session_ticket->data == NULL) |
| 524 | goto skip_ext; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 525 | /* Check for enough room 2 for extension type, 2 for len |
| 526 | * rest for ticket |
| 527 | */ |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 528 | if ((long)(limit - ret - 4 - ticklen) < 0) return NULL; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 529 | s2n(TLSEXT_TYPE_session_ticket,ret); |
| 530 | s2n(ticklen,ret); |
| 531 | if (ticklen) |
| 532 | { |
| 533 | memcpy(ret, s->session->tlsext_tick, ticklen); |
| 534 | ret += ticklen; |
| 535 | } |
| 536 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 537 | skip_ext: |
| 538 | |
Brian Carlstrom | ed2ff7a | 2012-03-28 00:55:55 -0700 | [diff] [blame] | 539 | if (TLS1_get_client_version(s) >= TLS1_2_VERSION) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 540 | { |
| 541 | if ((size_t)(limit - ret) < sizeof(tls12_sigalgs) + 6) |
| 542 | return NULL; |
| 543 | s2n(TLSEXT_TYPE_signature_algorithms,ret); |
| 544 | s2n(sizeof(tls12_sigalgs) + 2, ret); |
| 545 | s2n(sizeof(tls12_sigalgs), ret); |
| 546 | memcpy(ret, tls12_sigalgs, sizeof(tls12_sigalgs)); |
| 547 | ret += sizeof(tls12_sigalgs); |
| 548 | } |
| 549 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 550 | #ifdef TLSEXT_TYPE_opaque_prf_input |
| 551 | if (s->s3->client_opaque_prf_input != NULL && |
| 552 | s->version != DTLS1_VERSION) |
| 553 | { |
| 554 | size_t col = s->s3->client_opaque_prf_input_len; |
| 555 | |
| 556 | if ((long)(limit - ret - 6 - col < 0)) |
| 557 | return NULL; |
| 558 | if (col > 0xFFFD) /* can't happen */ |
| 559 | return NULL; |
| 560 | |
| 561 | s2n(TLSEXT_TYPE_opaque_prf_input, ret); |
| 562 | s2n(col + 2, ret); |
| 563 | s2n(col, ret); |
| 564 | memcpy(ret, s->s3->client_opaque_prf_input, col); |
| 565 | ret += col; |
| 566 | } |
| 567 | #endif |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 568 | |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 569 | if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp && |
| 570 | s->version != DTLS1_VERSION) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 571 | { |
| 572 | int i; |
| 573 | long extlen, idlen, itmp; |
| 574 | OCSP_RESPID *id; |
| 575 | |
| 576 | idlen = 0; |
| 577 | for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) |
| 578 | { |
| 579 | id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i); |
| 580 | itmp = i2d_OCSP_RESPID(id, NULL); |
| 581 | if (itmp <= 0) |
| 582 | return NULL; |
| 583 | idlen += itmp + 2; |
| 584 | } |
| 585 | |
| 586 | if (s->tlsext_ocsp_exts) |
| 587 | { |
| 588 | extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL); |
| 589 | if (extlen < 0) |
| 590 | return NULL; |
| 591 | } |
| 592 | else |
| 593 | extlen = 0; |
| 594 | |
| 595 | if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL; |
| 596 | s2n(TLSEXT_TYPE_status_request, ret); |
| 597 | if (extlen + idlen > 0xFFF0) |
| 598 | return NULL; |
| 599 | s2n(extlen + idlen + 5, ret); |
| 600 | *(ret++) = TLSEXT_STATUSTYPE_ocsp; |
| 601 | s2n(idlen, ret); |
| 602 | for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) |
| 603 | { |
| 604 | /* save position of id len */ |
| 605 | unsigned char *q = ret; |
| 606 | id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i); |
| 607 | /* skip over id len */ |
| 608 | ret += 2; |
| 609 | itmp = i2d_OCSP_RESPID(id, &ret); |
| 610 | /* write id len */ |
| 611 | s2n(itmp, q); |
| 612 | } |
| 613 | s2n(extlen, ret); |
| 614 | if (extlen > 0) |
| 615 | i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, &ret); |
| 616 | } |
| 617 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 618 | #ifndef OPENSSL_NO_HEARTBEATS |
| 619 | /* Add Heartbeat extension */ |
Kenny Root | 77c6be7 | 2014-06-12 10:08:29 -0700 | [diff] [blame^] | 620 | if ((limit - ret - 4 - 1) < 0) |
| 621 | return NULL; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 622 | s2n(TLSEXT_TYPE_heartbeat,ret); |
| 623 | s2n(1,ret); |
| 624 | /* Set mode: |
| 625 | * 1: peer may send requests |
| 626 | * 2: peer not allowed to send requests |
| 627 | */ |
| 628 | if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS) |
| 629 | *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS; |
| 630 | else |
| 631 | *(ret++) = SSL_TLSEXT_HB_ENABLED; |
| 632 | #endif |
| 633 | |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 634 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 635 | if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) |
| 636 | { |
| 637 | /* The client advertises an emtpy extension to indicate its |
| 638 | * support for Next Protocol Negotiation */ |
| 639 | if (limit - ret - 4 < 0) |
| 640 | return NULL; |
| 641 | s2n(TLSEXT_TYPE_next_proto_neg,ret); |
| 642 | s2n(0,ret); |
| 643 | } |
| 644 | #endif |
| 645 | |
Adam Langley | 45bcfbc | 2013-01-16 13:43:53 -0800 | [diff] [blame] | 646 | if (s->tlsext_channel_id_enabled) |
| 647 | { |
| 648 | /* The client advertises an emtpy extension to indicate its |
| 649 | * support for Channel ID. */ |
| 650 | if (limit - ret - 4 < 0) |
| 651 | return NULL; |
Kenny Root | 77c6be7 | 2014-06-12 10:08:29 -0700 | [diff] [blame^] | 652 | if (s->ctx->tlsext_channel_id_enabled_new) |
| 653 | s2n(TLSEXT_TYPE_channel_id_new,ret); |
| 654 | else |
| 655 | s2n(TLSEXT_TYPE_channel_id,ret); |
Adam Langley | 45bcfbc | 2013-01-16 13:43:53 -0800 | [diff] [blame] | 656 | s2n(0,ret); |
| 657 | } |
| 658 | |
Kenny Root | ee53ab1 | 2013-06-24 12:07:13 -0700 | [diff] [blame] | 659 | if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) |
| 660 | { |
| 661 | if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) |
| 662 | return NULL; |
| 663 | s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret); |
| 664 | s2n(2 + s->alpn_client_proto_list_len,ret); |
| 665 | s2n(s->alpn_client_proto_list_len,ret); |
| 666 | memcpy(ret, s->alpn_client_proto_list, |
| 667 | s->alpn_client_proto_list_len); |
| 668 | ret += s->alpn_client_proto_list_len; |
| 669 | } |
| 670 | |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 671 | #ifndef OPENSSL_NO_SRTP |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 672 | if(SSL_get_srtp_profiles(s)) |
| 673 | { |
| 674 | int el; |
| 675 | |
| 676 | ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0); |
| 677 | |
| 678 | if((limit - p - 4 - el) < 0) return NULL; |
| 679 | |
| 680 | s2n(TLSEXT_TYPE_use_srtp,ret); |
| 681 | s2n(el,ret); |
| 682 | |
| 683 | if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) |
| 684 | { |
| 685 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 686 | return NULL; |
| 687 | } |
| 688 | ret += el; |
| 689 | } |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 690 | #endif |
Adam Langley | 5df7c8e | 2014-01-23 09:40:22 -0800 | [diff] [blame] | 691 | /* Add padding to workaround bugs in F5 terminators. |
Kenny Root | 7f7ea2d | 2014-04-07 13:17:43 -0700 | [diff] [blame] | 692 | * See https://tools.ietf.org/html/draft-agl-tls-padding-03 |
| 693 | * |
| 694 | * NB: because this code works out the length of all existing |
| 695 | * extensions it MUST always appear last. |
| 696 | */ |
Kenny Root | 77c6be7 | 2014-06-12 10:08:29 -0700 | [diff] [blame^] | 697 | if (s->options & SSL_OP_TLSEXT_PADDING) |
Adam Langley | 5df7c8e | 2014-01-23 09:40:22 -0800 | [diff] [blame] | 698 | { |
Kenny Root | 77c6be7 | 2014-06-12 10:08:29 -0700 | [diff] [blame^] | 699 | int hlen = ret - (unsigned char *)s->init_buf->data; |
| 700 | /* The code in s23_clnt.c to build ClientHello messages |
| 701 | * includes the 5-byte record header in the buffer, while |
| 702 | * the code in s3_clnt.c does not. |
| 703 | */ |
| 704 | if (s->state == SSL23_ST_CW_CLNT_HELLO_A) |
| 705 | hlen -= 5; |
| 706 | if (hlen > 0xff && hlen < 0x200) |
| 707 | { |
| 708 | hlen = 0x200 - hlen; |
| 709 | if (hlen >= 4) |
| 710 | hlen -= 4; |
| 711 | else |
| 712 | hlen = 0; |
Adam Langley | 5df7c8e | 2014-01-23 09:40:22 -0800 | [diff] [blame] | 713 | |
Kenny Root | 77c6be7 | 2014-06-12 10:08:29 -0700 | [diff] [blame^] | 714 | s2n(TLSEXT_TYPE_padding, ret); |
| 715 | s2n(hlen, ret); |
| 716 | memset(ret, 0, hlen); |
| 717 | ret += hlen; |
| 718 | } |
Adam Langley | 5df7c8e | 2014-01-23 09:40:22 -0800 | [diff] [blame] | 719 | } |
Adam Langley | 5df7c8e | 2014-01-23 09:40:22 -0800 | [diff] [blame] | 720 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 721 | if ((extdatalen = ret-p-2)== 0) |
| 722 | return p; |
| 723 | |
| 724 | s2n(extdatalen,p); |
| 725 | return ret; |
| 726 | } |
| 727 | |
| 728 | unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit) |
| 729 | { |
| 730 | int extdatalen=0; |
| 731 | unsigned char *ret = p; |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 732 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 733 | int next_proto_neg_seen; |
| 734 | #endif |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 735 | |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 736 | /* don't add extensions for SSLv3, unless doing secure renegotiation */ |
| 737 | if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) |
| 738 | return p; |
| 739 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 740 | ret+=2; |
| 741 | if (ret>=limit) return NULL; /* this really never occurs, but ... */ |
| 742 | |
| 743 | if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL) |
| 744 | { |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 745 | if ((long)(limit - ret - 4) < 0) return NULL; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 746 | |
| 747 | s2n(TLSEXT_TYPE_server_name,ret); |
| 748 | s2n(0,ret); |
| 749 | } |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 750 | |
| 751 | if(s->s3->send_connection_binding) |
| 752 | { |
| 753 | int el; |
| 754 | |
| 755 | if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) |
| 756 | { |
| 757 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 758 | return NULL; |
| 759 | } |
| 760 | |
| 761 | if((limit - p - 4 - el) < 0) return NULL; |
| 762 | |
| 763 | s2n(TLSEXT_TYPE_renegotiate,ret); |
| 764 | s2n(el,ret); |
| 765 | |
| 766 | if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) |
| 767 | { |
| 768 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 769 | return NULL; |
| 770 | } |
| 771 | |
| 772 | ret += el; |
| 773 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 774 | |
| 775 | #ifndef OPENSSL_NO_EC |
| 776 | if (s->tlsext_ecpointformatlist != NULL && |
| 777 | s->version != DTLS1_VERSION) |
| 778 | { |
| 779 | /* Add TLS extension ECPointFormats to the ServerHello message */ |
| 780 | long lenmax; |
| 781 | |
| 782 | if ((lenmax = limit - ret - 5) < 0) return NULL; |
| 783 | if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL; |
| 784 | if (s->tlsext_ecpointformatlist_length > 255) |
| 785 | { |
| 786 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 787 | return NULL; |
| 788 | } |
| 789 | |
| 790 | s2n(TLSEXT_TYPE_ec_point_formats,ret); |
| 791 | s2n(s->tlsext_ecpointformatlist_length + 1,ret); |
| 792 | *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length; |
| 793 | memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length); |
| 794 | ret+=s->tlsext_ecpointformatlist_length; |
| 795 | |
| 796 | } |
| 797 | /* Currently the server should not respond with a SupportedCurves extension */ |
| 798 | #endif /* OPENSSL_NO_EC */ |
| 799 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 800 | if (s->tlsext_ticket_expected |
| 801 | && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) |
| 802 | { |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 803 | if ((long)(limit - ret - 4) < 0) return NULL; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 804 | s2n(TLSEXT_TYPE_session_ticket,ret); |
| 805 | s2n(0,ret); |
| 806 | } |
| 807 | |
| 808 | if (s->tlsext_status_expected) |
| 809 | { |
| 810 | if ((long)(limit - ret - 4) < 0) return NULL; |
| 811 | s2n(TLSEXT_TYPE_status_request,ret); |
| 812 | s2n(0,ret); |
| 813 | } |
| 814 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 815 | #ifdef TLSEXT_TYPE_opaque_prf_input |
| 816 | if (s->s3->server_opaque_prf_input != NULL && |
| 817 | s->version != DTLS1_VERSION) |
| 818 | { |
| 819 | size_t sol = s->s3->server_opaque_prf_input_len; |
| 820 | |
| 821 | if ((long)(limit - ret - 6 - sol) < 0) |
| 822 | return NULL; |
| 823 | if (sol > 0xFFFD) /* can't happen */ |
| 824 | return NULL; |
| 825 | |
| 826 | s2n(TLSEXT_TYPE_opaque_prf_input, ret); |
| 827 | s2n(sol + 2, ret); |
| 828 | s2n(sol, ret); |
| 829 | memcpy(ret, s->s3->server_opaque_prf_input, sol); |
| 830 | ret += sol; |
| 831 | } |
| 832 | #endif |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 833 | |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 834 | #ifndef OPENSSL_NO_SRTP |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 835 | if(s->srtp_profile) |
| 836 | { |
| 837 | int el; |
| 838 | |
| 839 | ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0); |
| 840 | |
| 841 | if((limit - p - 4 - el) < 0) return NULL; |
| 842 | |
| 843 | s2n(TLSEXT_TYPE_use_srtp,ret); |
| 844 | s2n(el,ret); |
| 845 | |
| 846 | if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) |
| 847 | { |
| 848 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 849 | return NULL; |
| 850 | } |
| 851 | ret+=el; |
| 852 | } |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 853 | #endif |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 854 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 855 | if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81) |
| 856 | && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) |
| 857 | { const unsigned char cryptopro_ext[36] = { |
| 858 | 0xfd, 0xe8, /*65000*/ |
| 859 | 0x00, 0x20, /*32 bytes length*/ |
| 860 | 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85, |
| 861 | 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06, |
| 862 | 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08, |
| 863 | 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17}; |
| 864 | if (limit-ret<36) return NULL; |
| 865 | memcpy(ret,cryptopro_ext,36); |
| 866 | ret+=36; |
| 867 | |
| 868 | } |
| 869 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 870 | #ifndef OPENSSL_NO_HEARTBEATS |
| 871 | /* Add Heartbeat extension if we've received one */ |
| 872 | if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) |
| 873 | { |
Kenny Root | 77c6be7 | 2014-06-12 10:08:29 -0700 | [diff] [blame^] | 874 | if ((limit - ret - 4 - 1) < 0) |
| 875 | return NULL; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 876 | s2n(TLSEXT_TYPE_heartbeat,ret); |
| 877 | s2n(1,ret); |
| 878 | /* Set mode: |
| 879 | * 1: peer may send requests |
| 880 | * 2: peer not allowed to send requests |
| 881 | */ |
| 882 | if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS) |
| 883 | *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS; |
| 884 | else |
| 885 | *(ret++) = SSL_TLSEXT_HB_ENABLED; |
| 886 | |
| 887 | } |
| 888 | #endif |
| 889 | |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 890 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 891 | next_proto_neg_seen = s->s3->next_proto_neg_seen; |
| 892 | s->s3->next_proto_neg_seen = 0; |
| 893 | if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) |
| 894 | { |
| 895 | const unsigned char *npa; |
| 896 | unsigned int npalen; |
| 897 | int r; |
| 898 | |
| 899 | r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg); |
| 900 | if (r == SSL_TLSEXT_ERR_OK) |
| 901 | { |
| 902 | if ((long)(limit - ret - 4 - npalen) < 0) return NULL; |
| 903 | s2n(TLSEXT_TYPE_next_proto_neg,ret); |
| 904 | s2n(npalen,ret); |
| 905 | memcpy(ret, npa, npalen); |
| 906 | ret += npalen; |
| 907 | s->s3->next_proto_neg_seen = 1; |
| 908 | } |
| 909 | } |
| 910 | #endif |
| 911 | |
Adam Langley | 45bcfbc | 2013-01-16 13:43:53 -0800 | [diff] [blame] | 912 | /* If the client advertised support for Channel ID, and we have it |
| 913 | * enabled, then we want to echo it back. */ |
| 914 | if (s->s3->tlsext_channel_id_valid) |
| 915 | { |
| 916 | if (limit - ret - 4 < 0) |
| 917 | return NULL; |
Kenny Root | 77c6be7 | 2014-06-12 10:08:29 -0700 | [diff] [blame^] | 918 | if (s->s3->tlsext_channel_id_new) |
| 919 | s2n(TLSEXT_TYPE_channel_id_new,ret); |
| 920 | else |
| 921 | s2n(TLSEXT_TYPE_channel_id,ret); |
Adam Langley | 45bcfbc | 2013-01-16 13:43:53 -0800 | [diff] [blame] | 922 | s2n(0,ret); |
| 923 | } |
| 924 | |
Kenny Root | ee53ab1 | 2013-06-24 12:07:13 -0700 | [diff] [blame] | 925 | if (s->s3->alpn_selected) |
| 926 | { |
| 927 | const unsigned char *selected = s->s3->alpn_selected; |
| 928 | unsigned len = s->s3->alpn_selected_len; |
| 929 | |
| 930 | if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) |
| 931 | return NULL; |
| 932 | s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret); |
| 933 | s2n(3 + len,ret); |
| 934 | s2n(1 + len,ret); |
| 935 | *ret++ = len; |
| 936 | memcpy(ret, selected, len); |
| 937 | ret += len; |
| 938 | } |
| 939 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 940 | if ((extdatalen = ret-p-2)== 0) |
| 941 | return p; |
| 942 | |
| 943 | s2n(extdatalen,p); |
| 944 | return ret; |
| 945 | } |
| 946 | |
Kenny Root | ff41a4b | 2014-01-07 10:19:10 -0800 | [diff] [blame] | 947 | #ifndef OPENSSL_NO_EC |
| 948 | /* ssl_check_for_safari attempts to fingerprint Safari using OS X |
| 949 | * SecureTransport using the TLS extension block in |d|, of length |n|. |
| 950 | * Safari, since 10.6, sends exactly these extensions, in this order: |
| 951 | * SNI, |
| 952 | * elliptic_curves |
| 953 | * ec_point_formats |
| 954 | * |
| 955 | * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8, |
| 956 | * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them. |
| 957 | * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from |
| 958 | * 10.8..10.8.3 (which don't work). |
| 959 | */ |
| 960 | static void ssl_check_for_safari(SSL *s, const unsigned char *data, const unsigned char *d, int n) { |
| 961 | unsigned short type, size; |
| 962 | static const unsigned char kSafariExtensionsBlock[] = { |
| 963 | 0x00, 0x0a, /* elliptic_curves extension */ |
| 964 | 0x00, 0x08, /* 8 bytes */ |
| 965 | 0x00, 0x06, /* 6 bytes of curve ids */ |
| 966 | 0x00, 0x17, /* P-256 */ |
| 967 | 0x00, 0x18, /* P-384 */ |
| 968 | 0x00, 0x19, /* P-521 */ |
| 969 | |
| 970 | 0x00, 0x0b, /* ec_point_formats */ |
| 971 | 0x00, 0x02, /* 2 bytes */ |
| 972 | 0x01, /* 1 point format */ |
| 973 | 0x00, /* uncompressed */ |
| 974 | }; |
| 975 | |
| 976 | /* The following is only present in TLS 1.2 */ |
| 977 | static const unsigned char kSafariTLS12ExtensionsBlock[] = { |
| 978 | 0x00, 0x0d, /* signature_algorithms */ |
| 979 | 0x00, 0x0c, /* 12 bytes */ |
| 980 | 0x00, 0x0a, /* 10 bytes */ |
| 981 | 0x05, 0x01, /* SHA-384/RSA */ |
| 982 | 0x04, 0x01, /* SHA-256/RSA */ |
| 983 | 0x02, 0x01, /* SHA-1/RSA */ |
| 984 | 0x04, 0x03, /* SHA-256/ECDSA */ |
| 985 | 0x02, 0x03, /* SHA-1/ECDSA */ |
| 986 | }; |
| 987 | |
| 988 | if (data >= (d+n-2)) |
| 989 | return; |
| 990 | data += 2; |
| 991 | |
| 992 | if (data > (d+n-4)) |
| 993 | return; |
| 994 | n2s(data,type); |
| 995 | n2s(data,size); |
| 996 | |
| 997 | if (type != TLSEXT_TYPE_server_name) |
| 998 | return; |
| 999 | |
| 1000 | if (data+size > d+n) |
| 1001 | return; |
| 1002 | data += size; |
| 1003 | |
| 1004 | if (TLS1_get_client_version(s) >= TLS1_2_VERSION) |
| 1005 | { |
| 1006 | const size_t len1 = sizeof(kSafariExtensionsBlock); |
| 1007 | const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock); |
| 1008 | |
| 1009 | if (data + len1 + len2 != d+n) |
| 1010 | return; |
| 1011 | if (memcmp(data, kSafariExtensionsBlock, len1) != 0) |
| 1012 | return; |
| 1013 | if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0) |
| 1014 | return; |
| 1015 | } |
| 1016 | else |
| 1017 | { |
| 1018 | const size_t len = sizeof(kSafariExtensionsBlock); |
| 1019 | |
| 1020 | if (data + len != d+n) |
| 1021 | return; |
| 1022 | if (memcmp(data, kSafariExtensionsBlock, len) != 0) |
| 1023 | return; |
| 1024 | } |
| 1025 | |
| 1026 | s->s3->is_probably_safari = 1; |
| 1027 | } |
| 1028 | #endif /* !OPENSSL_NO_EC */ |
| 1029 | |
Kenny Root | ee53ab1 | 2013-06-24 12:07:13 -0700 | [diff] [blame] | 1030 | /* tls1_alpn_handle_client_hello is called to process the ALPN extension in a |
| 1031 | * ClientHello. |
| 1032 | * data: the contents of the extension, not including the type and length. |
| 1033 | * data_len: the number of bytes in |data| |
| 1034 | * al: a pointer to the alert value to send in the event of a non-zero |
| 1035 | * return. |
| 1036 | * |
| 1037 | * returns: 0 on success. */ |
| 1038 | static int tls1_alpn_handle_client_hello(SSL *s, const unsigned char *data, |
| 1039 | unsigned data_len, int *al) |
| 1040 | { |
| 1041 | unsigned i; |
| 1042 | unsigned proto_len; |
| 1043 | const unsigned char *selected; |
| 1044 | unsigned char selected_len; |
| 1045 | int r; |
| 1046 | |
| 1047 | if (s->ctx->alpn_select_cb == NULL) |
| 1048 | return 0; |
| 1049 | |
| 1050 | if (data_len < 2) |
| 1051 | goto parse_error; |
| 1052 | |
| 1053 | /* data should contain a uint16 length followed by a series of 8-bit, |
| 1054 | * length-prefixed strings. */ |
| 1055 | i = ((unsigned) data[0]) << 8 | |
| 1056 | ((unsigned) data[1]); |
| 1057 | data_len -= 2; |
| 1058 | data += 2; |
| 1059 | if (data_len != i) |
| 1060 | goto parse_error; |
| 1061 | |
| 1062 | if (data_len < 2) |
| 1063 | goto parse_error; |
| 1064 | |
| 1065 | for (i = 0; i < data_len;) |
| 1066 | { |
| 1067 | proto_len = data[i]; |
| 1068 | i++; |
| 1069 | |
| 1070 | if (proto_len == 0) |
| 1071 | goto parse_error; |
| 1072 | |
| 1073 | if (i + proto_len < i || i + proto_len > data_len) |
| 1074 | goto parse_error; |
| 1075 | |
| 1076 | i += proto_len; |
| 1077 | } |
| 1078 | |
| 1079 | r = s->ctx->alpn_select_cb(s, &selected, &selected_len, data, data_len, |
| 1080 | s->ctx->alpn_select_cb_arg); |
| 1081 | if (r == SSL_TLSEXT_ERR_OK) { |
| 1082 | if (s->s3->alpn_selected) |
| 1083 | OPENSSL_free(s->s3->alpn_selected); |
| 1084 | s->s3->alpn_selected = OPENSSL_malloc(selected_len); |
| 1085 | if (!s->s3->alpn_selected) |
| 1086 | { |
| 1087 | *al = SSL_AD_INTERNAL_ERROR; |
| 1088 | return -1; |
| 1089 | } |
| 1090 | memcpy(s->s3->alpn_selected, selected, selected_len); |
| 1091 | s->s3->alpn_selected_len = selected_len; |
| 1092 | } |
| 1093 | return 0; |
| 1094 | |
| 1095 | parse_error: |
| 1096 | *al = SSL_AD_DECODE_ERROR; |
| 1097 | return -1; |
| 1098 | } |
| 1099 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1100 | int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al) |
| 1101 | { |
| 1102 | unsigned short type; |
| 1103 | unsigned short size; |
| 1104 | unsigned short len; |
| 1105 | unsigned char *data = *p; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1106 | int renegotiate_seen = 0; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1107 | int sigalg_seen = 0; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1108 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1109 | s->servername_done = 0; |
| 1110 | s->tlsext_status_type = -1; |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 1111 | |
| 1112 | /* Reset TLS 1.2 digest functions to defaults because they don't carry |
| 1113 | * over to a renegotiation. */ |
| 1114 | s->s3->digest_rsa = NULL; |
| 1115 | s->s3->digest_dsa = NULL; |
| 1116 | s->s3->digest_ecdsa = NULL; |
| 1117 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1118 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 1119 | s->s3->next_proto_neg_seen = 0; |
| 1120 | #endif |
| 1121 | |
Kenny Root | ee53ab1 | 2013-06-24 12:07:13 -0700 | [diff] [blame] | 1122 | if (s->s3->alpn_selected) |
| 1123 | { |
| 1124 | OPENSSL_free(s->s3->alpn_selected); |
| 1125 | s->s3->alpn_selected = NULL; |
| 1126 | } |
| 1127 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1128 | #ifndef OPENSSL_NO_HEARTBEATS |
| 1129 | s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED | |
| 1130 | SSL_TLSEXT_HB_DONT_SEND_REQUESTS); |
| 1131 | #endif |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1132 | |
Kenny Root | ff41a4b | 2014-01-07 10:19:10 -0800 | [diff] [blame] | 1133 | #ifndef OPENSSL_NO_EC |
| 1134 | if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG) |
| 1135 | ssl_check_for_safari(s, data, d, n); |
| 1136 | #endif /* !OPENSSL_NO_EC */ |
| 1137 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1138 | if (data >= (d+n-2)) |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1139 | goto ri_check; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1140 | n2s(data,len); |
| 1141 | |
| 1142 | if (data > (d+n-len)) |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1143 | goto ri_check; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1144 | |
| 1145 | while (data <= (d+n-4)) |
| 1146 | { |
| 1147 | n2s(data,type); |
| 1148 | n2s(data,size); |
| 1149 | |
| 1150 | if (data+size > (d+n)) |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1151 | goto ri_check; |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1152 | #if 0 |
| 1153 | fprintf(stderr,"Received extension type %d size %d\n",type,size); |
| 1154 | #endif |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1155 | if (s->tlsext_debug_cb) |
| 1156 | s->tlsext_debug_cb(s, 0, type, data, size, |
| 1157 | s->tlsext_debug_arg); |
| 1158 | /* The servername extension is treated as follows: |
| 1159 | |
| 1160 | - Only the hostname type is supported with a maximum length of 255. |
| 1161 | - The servername is rejected if too long or if it contains zeros, |
| 1162 | in which case an fatal alert is generated. |
| 1163 | - The servername field is maintained together with the session cache. |
| 1164 | - When a session is resumed, the servername call back invoked in order |
| 1165 | to allow the application to position itself to the right context. |
| 1166 | - The servername is acknowledged if it is new for a session or when |
| 1167 | it is identical to a previously used for the same session. |
| 1168 | Applications can control the behaviour. They can at any time |
| 1169 | set a 'desirable' servername for a new SSL object. This can be the |
| 1170 | case for example with HTTPS when a Host: header field is received and |
| 1171 | a renegotiation is requested. In this case, a possible servername |
| 1172 | presented in the new client hello is only acknowledged if it matches |
| 1173 | the value of the Host: field. |
| 1174 | - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
| 1175 | if they provide for changing an explicit servername context for the session, |
| 1176 | i.e. when the session has been established with a servername extension. |
| 1177 | - On session reconnect, the servername extension may be absent. |
| 1178 | |
| 1179 | */ |
| 1180 | |
| 1181 | if (type == TLSEXT_TYPE_server_name) |
| 1182 | { |
| 1183 | unsigned char *sdata; |
| 1184 | int servname_type; |
| 1185 | int dsize; |
| 1186 | |
| 1187 | if (size < 2) |
| 1188 | { |
| 1189 | *al = SSL_AD_DECODE_ERROR; |
| 1190 | return 0; |
| 1191 | } |
| 1192 | n2s(data,dsize); |
| 1193 | size -= 2; |
| 1194 | if (dsize > size ) |
| 1195 | { |
| 1196 | *al = SSL_AD_DECODE_ERROR; |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
| 1200 | sdata = data; |
| 1201 | while (dsize > 3) |
| 1202 | { |
| 1203 | servname_type = *(sdata++); |
| 1204 | n2s(sdata,len); |
| 1205 | dsize -= 3; |
| 1206 | |
| 1207 | if (len > dsize) |
| 1208 | { |
| 1209 | *al = SSL_AD_DECODE_ERROR; |
| 1210 | return 0; |
| 1211 | } |
| 1212 | if (s->servername_done == 0) |
| 1213 | switch (servname_type) |
| 1214 | { |
| 1215 | case TLSEXT_NAMETYPE_host_name: |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1216 | if (!s->hit) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1217 | { |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1218 | if(s->session->tlsext_hostname) |
| 1219 | { |
| 1220 | *al = SSL_AD_DECODE_ERROR; |
| 1221 | return 0; |
| 1222 | } |
| 1223 | if (len > TLSEXT_MAXLEN_host_name) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1224 | { |
| 1225 | *al = TLS1_AD_UNRECOGNIZED_NAME; |
| 1226 | return 0; |
| 1227 | } |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1228 | if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL) |
| 1229 | { |
| 1230 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1231 | return 0; |
| 1232 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1233 | memcpy(s->session->tlsext_hostname, sdata, len); |
| 1234 | s->session->tlsext_hostname[len]='\0'; |
| 1235 | if (strlen(s->session->tlsext_hostname) != len) { |
| 1236 | OPENSSL_free(s->session->tlsext_hostname); |
| 1237 | s->session->tlsext_hostname = NULL; |
| 1238 | *al = TLS1_AD_UNRECOGNIZED_NAME; |
| 1239 | return 0; |
| 1240 | } |
| 1241 | s->servername_done = 1; |
| 1242 | |
| 1243 | } |
| 1244 | else |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1245 | s->servername_done = s->session->tlsext_hostname |
| 1246 | && strlen(s->session->tlsext_hostname) == len |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1247 | && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0; |
| 1248 | |
| 1249 | break; |
| 1250 | |
| 1251 | default: |
| 1252 | break; |
| 1253 | } |
| 1254 | |
| 1255 | dsize -= len; |
| 1256 | } |
| 1257 | if (dsize != 0) |
| 1258 | { |
| 1259 | *al = SSL_AD_DECODE_ERROR; |
| 1260 | return 0; |
| 1261 | } |
| 1262 | |
| 1263 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1264 | #ifndef OPENSSL_NO_SRP |
| 1265 | else if (type == TLSEXT_TYPE_srp) |
| 1266 | { |
| 1267 | if (size <= 0 || ((len = data[0])) != (size -1)) |
| 1268 | { |
| 1269 | *al = SSL_AD_DECODE_ERROR; |
| 1270 | return 0; |
| 1271 | } |
| 1272 | if (s->srp_ctx.login != NULL) |
| 1273 | { |
| 1274 | *al = SSL_AD_DECODE_ERROR; |
| 1275 | return 0; |
| 1276 | } |
| 1277 | if ((s->srp_ctx.login = OPENSSL_malloc(len+1)) == NULL) |
| 1278 | return -1; |
| 1279 | memcpy(s->srp_ctx.login, &data[1], len); |
| 1280 | s->srp_ctx.login[len]='\0'; |
| 1281 | |
| 1282 | if (strlen(s->srp_ctx.login) != len) |
| 1283 | { |
| 1284 | *al = SSL_AD_DECODE_ERROR; |
| 1285 | return 0; |
| 1286 | } |
| 1287 | } |
| 1288 | #endif |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1289 | |
| 1290 | #ifndef OPENSSL_NO_EC |
| 1291 | else if (type == TLSEXT_TYPE_ec_point_formats && |
| 1292 | s->version != DTLS1_VERSION) |
| 1293 | { |
| 1294 | unsigned char *sdata = data; |
| 1295 | int ecpointformatlist_length = *(sdata++); |
| 1296 | |
| 1297 | if (ecpointformatlist_length != size - 1) |
| 1298 | { |
| 1299 | *al = TLS1_AD_DECODE_ERROR; |
| 1300 | return 0; |
| 1301 | } |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1302 | if (!s->hit) |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1303 | { |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1304 | if(s->session->tlsext_ecpointformatlist) |
| 1305 | { |
Brian Carlstrom | 976a034 | 2010-12-03 16:11:54 -0800 | [diff] [blame] | 1306 | OPENSSL_free(s->session->tlsext_ecpointformatlist); |
| 1307 | s->session->tlsext_ecpointformatlist = NULL; |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1308 | } |
| 1309 | s->session->tlsext_ecpointformatlist_length = 0; |
| 1310 | if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL) |
| 1311 | { |
| 1312 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1313 | return 0; |
| 1314 | } |
| 1315 | s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length; |
| 1316 | memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length); |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1317 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1318 | #if 0 |
| 1319 | fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length); |
| 1320 | sdata = s->session->tlsext_ecpointformatlist; |
| 1321 | for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) |
| 1322 | fprintf(stderr,"%i ",*(sdata++)); |
| 1323 | fprintf(stderr,"\n"); |
| 1324 | #endif |
| 1325 | } |
| 1326 | else if (type == TLSEXT_TYPE_elliptic_curves && |
| 1327 | s->version != DTLS1_VERSION) |
| 1328 | { |
| 1329 | unsigned char *sdata = data; |
| 1330 | int ellipticcurvelist_length = (*(sdata++) << 8); |
| 1331 | ellipticcurvelist_length += (*(sdata++)); |
| 1332 | |
Brian Carlstrom | 04ef91b | 2013-02-05 09:20:38 -0800 | [diff] [blame] | 1333 | if (ellipticcurvelist_length != size - 2 || |
| 1334 | ellipticcurvelist_length < 1) |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1335 | { |
| 1336 | *al = TLS1_AD_DECODE_ERROR; |
| 1337 | return 0; |
| 1338 | } |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1339 | if (!s->hit) |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1340 | { |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1341 | if(s->session->tlsext_ellipticcurvelist) |
| 1342 | { |
| 1343 | *al = TLS1_AD_DECODE_ERROR; |
| 1344 | return 0; |
| 1345 | } |
| 1346 | s->session->tlsext_ellipticcurvelist_length = 0; |
| 1347 | if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL) |
| 1348 | { |
| 1349 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1350 | return 0; |
| 1351 | } |
| 1352 | s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length; |
| 1353 | memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length); |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1354 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1355 | #if 0 |
| 1356 | fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length); |
| 1357 | sdata = s->session->tlsext_ellipticcurvelist; |
| 1358 | for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++) |
| 1359 | fprintf(stderr,"%i ",*(sdata++)); |
| 1360 | fprintf(stderr,"\n"); |
| 1361 | #endif |
| 1362 | } |
| 1363 | #endif /* OPENSSL_NO_EC */ |
| 1364 | #ifdef TLSEXT_TYPE_opaque_prf_input |
| 1365 | else if (type == TLSEXT_TYPE_opaque_prf_input && |
| 1366 | s->version != DTLS1_VERSION) |
| 1367 | { |
| 1368 | unsigned char *sdata = data; |
| 1369 | |
| 1370 | if (size < 2) |
| 1371 | { |
| 1372 | *al = SSL_AD_DECODE_ERROR; |
| 1373 | return 0; |
| 1374 | } |
| 1375 | n2s(sdata, s->s3->client_opaque_prf_input_len); |
| 1376 | if (s->s3->client_opaque_prf_input_len != size - 2) |
| 1377 | { |
| 1378 | *al = SSL_AD_DECODE_ERROR; |
| 1379 | return 0; |
| 1380 | } |
| 1381 | |
| 1382 | if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */ |
| 1383 | OPENSSL_free(s->s3->client_opaque_prf_input); |
| 1384 | if (s->s3->client_opaque_prf_input_len == 0) |
| 1385 | s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */ |
| 1386 | else |
| 1387 | s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len); |
| 1388 | if (s->s3->client_opaque_prf_input == NULL) |
| 1389 | { |
| 1390 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1391 | return 0; |
| 1392 | } |
| 1393 | } |
| 1394 | #endif |
| 1395 | else if (type == TLSEXT_TYPE_session_ticket) |
| 1396 | { |
| 1397 | if (s->tls_session_ticket_ext_cb && |
| 1398 | !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg)) |
| 1399 | { |
| 1400 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1401 | return 0; |
| 1402 | } |
| 1403 | } |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1404 | else if (type == TLSEXT_TYPE_renegotiate) |
| 1405 | { |
| 1406 | if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al)) |
| 1407 | return 0; |
| 1408 | renegotiate_seen = 1; |
| 1409 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1410 | else if (type == TLSEXT_TYPE_signature_algorithms) |
| 1411 | { |
| 1412 | int dsize; |
| 1413 | if (sigalg_seen || size < 2) |
| 1414 | { |
| 1415 | *al = SSL_AD_DECODE_ERROR; |
| 1416 | return 0; |
| 1417 | } |
| 1418 | sigalg_seen = 1; |
| 1419 | n2s(data,dsize); |
| 1420 | size -= 2; |
| 1421 | if (dsize != size || dsize & 1) |
| 1422 | { |
| 1423 | *al = SSL_AD_DECODE_ERROR; |
| 1424 | return 0; |
| 1425 | } |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 1426 | tls1_process_sigalgs(s, data, dsize); |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1427 | } |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1428 | else if (type == TLSEXT_TYPE_status_request && |
Kenny Root | 7f7ea2d | 2014-04-07 13:17:43 -0700 | [diff] [blame] | 1429 | s->version != DTLS1_VERSION) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1430 | { |
| 1431 | |
| 1432 | if (size < 5) |
| 1433 | { |
| 1434 | *al = SSL_AD_DECODE_ERROR; |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
| 1438 | s->tlsext_status_type = *data++; |
| 1439 | size--; |
| 1440 | if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) |
| 1441 | { |
| 1442 | const unsigned char *sdata; |
| 1443 | int dsize; |
| 1444 | /* Read in responder_id_list */ |
| 1445 | n2s(data,dsize); |
| 1446 | size -= 2; |
| 1447 | if (dsize > size ) |
| 1448 | { |
| 1449 | *al = SSL_AD_DECODE_ERROR; |
| 1450 | return 0; |
| 1451 | } |
| 1452 | while (dsize > 0) |
| 1453 | { |
| 1454 | OCSP_RESPID *id; |
| 1455 | int idsize; |
| 1456 | if (dsize < 4) |
| 1457 | { |
| 1458 | *al = SSL_AD_DECODE_ERROR; |
| 1459 | return 0; |
| 1460 | } |
| 1461 | n2s(data, idsize); |
| 1462 | dsize -= 2 + idsize; |
Brian Carlstrom | 81c4de7 | 2011-02-08 14:54:39 -0800 | [diff] [blame] | 1463 | size -= 2 + idsize; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1464 | if (dsize < 0) |
| 1465 | { |
| 1466 | *al = SSL_AD_DECODE_ERROR; |
| 1467 | return 0; |
| 1468 | } |
| 1469 | sdata = data; |
| 1470 | data += idsize; |
| 1471 | id = d2i_OCSP_RESPID(NULL, |
| 1472 | &sdata, idsize); |
| 1473 | if (!id) |
| 1474 | { |
| 1475 | *al = SSL_AD_DECODE_ERROR; |
| 1476 | return 0; |
| 1477 | } |
| 1478 | if (data != sdata) |
| 1479 | { |
| 1480 | OCSP_RESPID_free(id); |
| 1481 | *al = SSL_AD_DECODE_ERROR; |
| 1482 | return 0; |
| 1483 | } |
| 1484 | if (!s->tlsext_ocsp_ids |
| 1485 | && !(s->tlsext_ocsp_ids = |
| 1486 | sk_OCSP_RESPID_new_null())) |
| 1487 | { |
| 1488 | OCSP_RESPID_free(id); |
| 1489 | *al = SSL_AD_INTERNAL_ERROR; |
| 1490 | return 0; |
| 1491 | } |
| 1492 | if (!sk_OCSP_RESPID_push( |
| 1493 | s->tlsext_ocsp_ids, id)) |
| 1494 | { |
| 1495 | OCSP_RESPID_free(id); |
| 1496 | *al = SSL_AD_INTERNAL_ERROR; |
| 1497 | return 0; |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | /* Read in request_extensions */ |
Brian Carlstrom | 81c4de7 | 2011-02-08 14:54:39 -0800 | [diff] [blame] | 1502 | if (size < 2) |
| 1503 | { |
| 1504 | *al = SSL_AD_DECODE_ERROR; |
| 1505 | return 0; |
| 1506 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1507 | n2s(data,dsize); |
| 1508 | size -= 2; |
Brian Carlstrom | 81c4de7 | 2011-02-08 14:54:39 -0800 | [diff] [blame] | 1509 | if (dsize != size) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1510 | { |
| 1511 | *al = SSL_AD_DECODE_ERROR; |
| 1512 | return 0; |
| 1513 | } |
| 1514 | sdata = data; |
| 1515 | if (dsize > 0) |
| 1516 | { |
Brian Carlstrom | 7b476c4 | 2012-01-04 13:22:32 -0800 | [diff] [blame] | 1517 | if (s->tlsext_ocsp_exts) |
| 1518 | { |
| 1519 | sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, |
| 1520 | X509_EXTENSION_free); |
| 1521 | } |
| 1522 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1523 | s->tlsext_ocsp_exts = |
| 1524 | d2i_X509_EXTENSIONS(NULL, |
| 1525 | &sdata, dsize); |
| 1526 | if (!s->tlsext_ocsp_exts |
| 1527 | || (data + dsize != sdata)) |
| 1528 | { |
| 1529 | *al = SSL_AD_DECODE_ERROR; |
| 1530 | return 0; |
| 1531 | } |
| 1532 | } |
| 1533 | } |
| 1534 | /* We don't know what to do with any other type |
| 1535 | * so ignore it. |
| 1536 | */ |
| 1537 | else |
| 1538 | s->tlsext_status_type = -1; |
| 1539 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1540 | #ifndef OPENSSL_NO_HEARTBEATS |
| 1541 | else if (type == TLSEXT_TYPE_heartbeat) |
| 1542 | { |
| 1543 | switch(data[0]) |
| 1544 | { |
| 1545 | case 0x01: /* Client allows us to send HB requests */ |
| 1546 | s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED; |
| 1547 | break; |
| 1548 | case 0x02: /* Client doesn't accept HB requests */ |
| 1549 | s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED; |
| 1550 | s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS; |
| 1551 | break; |
| 1552 | default: *al = SSL_AD_ILLEGAL_PARAMETER; |
| 1553 | return 0; |
| 1554 | } |
| 1555 | } |
| 1556 | #endif |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 1557 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 1558 | else if (type == TLSEXT_TYPE_next_proto_neg && |
Kenny Root | ee53ab1 | 2013-06-24 12:07:13 -0700 | [diff] [blame] | 1559 | s->s3->tmp.finish_md_len == 0 && |
| 1560 | s->s3->alpn_selected == NULL) |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 1561 | { |
| 1562 | /* We shouldn't accept this extension on a |
| 1563 | * renegotiation. |
| 1564 | * |
| 1565 | * s->new_session will be set on renegotiation, but we |
| 1566 | * probably shouldn't rely that it couldn't be set on |
| 1567 | * the initial renegotation too in certain cases (when |
| 1568 | * there's some other reason to disallow resuming an |
| 1569 | * earlier session -- the current code won't be doing |
| 1570 | * anything like that, but this might change). |
| 1571 | |
| 1572 | * A valid sign that there's been a previous handshake |
| 1573 | * in this connection is if s->s3->tmp.finish_md_len > |
| 1574 | * 0. (We are talking about a check that will happen |
| 1575 | * in the Hello protocol round, well before a new |
| 1576 | * Finished message could have been computed.) */ |
| 1577 | s->s3->next_proto_neg_seen = 1; |
| 1578 | } |
| 1579 | #endif |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1580 | |
Adam Langley | 45bcfbc | 2013-01-16 13:43:53 -0800 | [diff] [blame] | 1581 | else if (type == TLSEXT_TYPE_channel_id && s->tlsext_channel_id_enabled) |
| 1582 | s->s3->tlsext_channel_id_valid = 1; |
| 1583 | |
Kenny Root | 77c6be7 | 2014-06-12 10:08:29 -0700 | [diff] [blame^] | 1584 | else if (type == TLSEXT_TYPE_channel_id_new && |
| 1585 | s->tlsext_channel_id_enabled) |
| 1586 | { |
| 1587 | s->s3->tlsext_channel_id_valid = 1; |
| 1588 | s->s3->tlsext_channel_id_new = 1; |
| 1589 | } |
| 1590 | |
Kenny Root | ee53ab1 | 2013-06-24 12:07:13 -0700 | [diff] [blame] | 1591 | else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation && |
| 1592 | s->ctx->alpn_select_cb && |
| 1593 | s->s3->tmp.finish_md_len == 0) |
| 1594 | { |
| 1595 | if (tls1_alpn_handle_client_hello(s, data, size, al) != 0) |
| 1596 | return 0; |
| 1597 | /* ALPN takes precedence over NPN. */ |
| 1598 | s->s3->next_proto_neg_seen = 0; |
| 1599 | } |
| 1600 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1601 | /* session ticket processed earlier */ |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 1602 | #ifndef OPENSSL_NO_SRTP |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1603 | else if (type == TLSEXT_TYPE_use_srtp) |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 1604 | { |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1605 | if(ssl_parse_clienthello_use_srtp_ext(s, data, size, |
| 1606 | al)) |
| 1607 | return 0; |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 1608 | } |
| 1609 | #endif |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1610 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1611 | data+=size; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1612 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1613 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1614 | *p = data; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1615 | |
| 1616 | ri_check: |
| 1617 | |
| 1618 | /* Need RI if renegotiating */ |
| 1619 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1620 | if (!renegotiate_seen && s->renegotiate && |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1621 | !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) |
| 1622 | { |
| 1623 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 1624 | SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, |
| 1625 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); |
| 1626 | return 0; |
| 1627 | } |
| 1628 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1629 | return 1; |
| 1630 | } |
| 1631 | |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 1632 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 1633 | /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No |
| 1634 | * elements of zero length are allowed and the set of elements must exactly fill |
| 1635 | * the length of the block. */ |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1636 | static char ssl_next_proto_validate(unsigned char *d, unsigned len) |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 1637 | { |
| 1638 | unsigned int off = 0; |
| 1639 | |
| 1640 | while (off < len) |
| 1641 | { |
| 1642 | if (d[off] == 0) |
| 1643 | return 0; |
| 1644 | off += d[off]; |
| 1645 | off++; |
| 1646 | } |
| 1647 | |
| 1648 | return off == len; |
| 1649 | } |
| 1650 | #endif |
| 1651 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1652 | int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al) |
| 1653 | { |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1654 | unsigned short length; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1655 | unsigned short type; |
| 1656 | unsigned short size; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1657 | unsigned char *data = *p; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1658 | int tlsext_servername = 0; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1659 | int renegotiate_seen = 0; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1660 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1661 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 1662 | s->s3->next_proto_neg_seen = 0; |
| 1663 | #endif |
| 1664 | |
Kenny Root | ee53ab1 | 2013-06-24 12:07:13 -0700 | [diff] [blame] | 1665 | if (s->s3->alpn_selected) |
| 1666 | { |
| 1667 | OPENSSL_free(s->s3->alpn_selected); |
| 1668 | s->s3->alpn_selected = NULL; |
| 1669 | } |
| 1670 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1671 | #ifndef OPENSSL_NO_HEARTBEATS |
| 1672 | s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED | |
| 1673 | SSL_TLSEXT_HB_DONT_SEND_REQUESTS); |
| 1674 | #endif |
| 1675 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1676 | if (data >= (d+n-2)) |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1677 | goto ri_check; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1678 | |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1679 | n2s(data,length); |
| 1680 | if (data+length != d+n) |
| 1681 | { |
| 1682 | *al = SSL_AD_DECODE_ERROR; |
| 1683 | return 0; |
| 1684 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1685 | |
| 1686 | while(data <= (d+n-4)) |
| 1687 | { |
| 1688 | n2s(data,type); |
| 1689 | n2s(data,size); |
| 1690 | |
| 1691 | if (data+size > (d+n)) |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1692 | goto ri_check; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1693 | |
| 1694 | if (s->tlsext_debug_cb) |
| 1695 | s->tlsext_debug_cb(s, 1, type, data, size, |
| 1696 | s->tlsext_debug_arg); |
| 1697 | |
| 1698 | if (type == TLSEXT_TYPE_server_name) |
| 1699 | { |
| 1700 | if (s->tlsext_hostname == NULL || size > 0) |
| 1701 | { |
| 1702 | *al = TLS1_AD_UNRECOGNIZED_NAME; |
| 1703 | return 0; |
| 1704 | } |
| 1705 | tlsext_servername = 1; |
| 1706 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1707 | |
| 1708 | #ifndef OPENSSL_NO_EC |
| 1709 | else if (type == TLSEXT_TYPE_ec_point_formats && |
| 1710 | s->version != DTLS1_VERSION) |
| 1711 | { |
| 1712 | unsigned char *sdata = data; |
| 1713 | int ecpointformatlist_length = *(sdata++); |
| 1714 | |
Brian Carlstrom | 04ef91b | 2013-02-05 09:20:38 -0800 | [diff] [blame] | 1715 | if (ecpointformatlist_length != size - 1 || |
| 1716 | ecpointformatlist_length < 1) |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1717 | { |
| 1718 | *al = TLS1_AD_DECODE_ERROR; |
| 1719 | return 0; |
| 1720 | } |
| 1721 | s->session->tlsext_ecpointformatlist_length = 0; |
| 1722 | if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist); |
| 1723 | if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL) |
| 1724 | { |
| 1725 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1726 | return 0; |
| 1727 | } |
| 1728 | s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length; |
| 1729 | memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length); |
| 1730 | #if 0 |
| 1731 | fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist "); |
| 1732 | sdata = s->session->tlsext_ecpointformatlist; |
| 1733 | for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) |
| 1734 | fprintf(stderr,"%i ",*(sdata++)); |
| 1735 | fprintf(stderr,"\n"); |
| 1736 | #endif |
| 1737 | } |
| 1738 | #endif /* OPENSSL_NO_EC */ |
| 1739 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1740 | else if (type == TLSEXT_TYPE_session_ticket) |
| 1741 | { |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1742 | if (s->tls_session_ticket_ext_cb && |
| 1743 | !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg)) |
| 1744 | { |
| 1745 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1746 | return 0; |
| 1747 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1748 | if ((SSL_get_options(s) & SSL_OP_NO_TICKET) |
| 1749 | || (size > 0)) |
| 1750 | { |
| 1751 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; |
| 1752 | return 0; |
| 1753 | } |
| 1754 | s->tlsext_ticket_expected = 1; |
| 1755 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1756 | #ifdef TLSEXT_TYPE_opaque_prf_input |
| 1757 | else if (type == TLSEXT_TYPE_opaque_prf_input && |
| 1758 | s->version != DTLS1_VERSION) |
| 1759 | { |
| 1760 | unsigned char *sdata = data; |
| 1761 | |
| 1762 | if (size < 2) |
| 1763 | { |
| 1764 | *al = SSL_AD_DECODE_ERROR; |
| 1765 | return 0; |
| 1766 | } |
| 1767 | n2s(sdata, s->s3->server_opaque_prf_input_len); |
| 1768 | if (s->s3->server_opaque_prf_input_len != size - 2) |
| 1769 | { |
| 1770 | *al = SSL_AD_DECODE_ERROR; |
| 1771 | return 0; |
| 1772 | } |
| 1773 | |
| 1774 | if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */ |
| 1775 | OPENSSL_free(s->s3->server_opaque_prf_input); |
| 1776 | if (s->s3->server_opaque_prf_input_len == 0) |
| 1777 | s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */ |
| 1778 | else |
| 1779 | s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len); |
| 1780 | |
| 1781 | if (s->s3->server_opaque_prf_input == NULL) |
| 1782 | { |
| 1783 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1784 | return 0; |
| 1785 | } |
| 1786 | } |
| 1787 | #endif |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1788 | else if (type == TLSEXT_TYPE_status_request && |
| 1789 | s->version != DTLS1_VERSION) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1790 | { |
| 1791 | /* MUST be empty and only sent if we've requested |
| 1792 | * a status request message. |
| 1793 | */ |
| 1794 | if ((s->tlsext_status_type == -1) || (size > 0)) |
| 1795 | { |
| 1796 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; |
| 1797 | return 0; |
| 1798 | } |
| 1799 | /* Set flag to expect CertificateStatus message */ |
| 1800 | s->tlsext_status_expected = 1; |
| 1801 | } |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 1802 | #ifndef OPENSSL_NO_NEXTPROTONEG |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1803 | else if (type == TLSEXT_TYPE_next_proto_neg && |
| 1804 | s->s3->tmp.finish_md_len == 0) |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 1805 | { |
| 1806 | unsigned char *selected; |
| 1807 | unsigned char selected_len; |
| 1808 | |
| 1809 | /* We must have requested it. */ |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 1810 | if (s->ctx->next_proto_select_cb == NULL) |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 1811 | { |
| 1812 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; |
| 1813 | return 0; |
| 1814 | } |
| 1815 | /* The data must be valid */ |
| 1816 | if (!ssl_next_proto_validate(data, size)) |
| 1817 | { |
| 1818 | *al = TLS1_AD_DECODE_ERROR; |
| 1819 | return 0; |
| 1820 | } |
| 1821 | if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) |
| 1822 | { |
| 1823 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1824 | return 0; |
| 1825 | } |
| 1826 | s->next_proto_negotiated = OPENSSL_malloc(selected_len); |
| 1827 | if (!s->next_proto_negotiated) |
| 1828 | { |
| 1829 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1830 | return 0; |
| 1831 | } |
| 1832 | memcpy(s->next_proto_negotiated, selected, selected_len); |
| 1833 | s->next_proto_negotiated_len = selected_len; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1834 | s->s3->next_proto_neg_seen = 1; |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 1835 | } |
| 1836 | #endif |
Adam Langley | 45bcfbc | 2013-01-16 13:43:53 -0800 | [diff] [blame] | 1837 | else if (type == TLSEXT_TYPE_channel_id) |
| 1838 | s->s3->tlsext_channel_id_valid = 1; |
| 1839 | |
Kenny Root | 77c6be7 | 2014-06-12 10:08:29 -0700 | [diff] [blame^] | 1840 | else if (type == TLSEXT_TYPE_channel_id_new) |
| 1841 | { |
| 1842 | s->s3->tlsext_channel_id_valid = 1; |
| 1843 | s->s3->tlsext_channel_id_new = 1; |
| 1844 | } |
| 1845 | |
Kenny Root | ee53ab1 | 2013-06-24 12:07:13 -0700 | [diff] [blame] | 1846 | else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) |
| 1847 | { |
| 1848 | unsigned len; |
| 1849 | |
| 1850 | /* We must have requested it. */ |
| 1851 | if (s->alpn_client_proto_list == NULL) |
| 1852 | { |
| 1853 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; |
| 1854 | return 0; |
| 1855 | } |
| 1856 | if (size < 4) |
| 1857 | { |
| 1858 | *al = TLS1_AD_DECODE_ERROR; |
| 1859 | return 0; |
| 1860 | } |
| 1861 | /* The extension data consists of: |
| 1862 | * uint16 list_length |
| 1863 | * uint8 proto_length; |
| 1864 | * uint8 proto[proto_length]; */ |
| 1865 | len = data[0]; |
| 1866 | len <<= 8; |
| 1867 | len |= data[1]; |
| 1868 | if (len != (unsigned) size - 2) |
| 1869 | { |
| 1870 | *al = TLS1_AD_DECODE_ERROR; |
| 1871 | return 0; |
| 1872 | } |
| 1873 | len = data[2]; |
| 1874 | if (len != (unsigned) size - 3) |
| 1875 | { |
| 1876 | *al = TLS1_AD_DECODE_ERROR; |
| 1877 | return 0; |
| 1878 | } |
| 1879 | if (s->s3->alpn_selected) |
| 1880 | OPENSSL_free(s->s3->alpn_selected); |
| 1881 | s->s3->alpn_selected = OPENSSL_malloc(len); |
| 1882 | if (!s->s3->alpn_selected) |
| 1883 | { |
| 1884 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1885 | return 0; |
| 1886 | } |
| 1887 | memcpy(s->s3->alpn_selected, data + 3, len); |
| 1888 | s->s3->alpn_selected_len = len; |
| 1889 | } |
| 1890 | |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1891 | else if (type == TLSEXT_TYPE_renegotiate) |
| 1892 | { |
| 1893 | if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al)) |
| 1894 | return 0; |
| 1895 | renegotiate_seen = 1; |
| 1896 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1897 | #ifndef OPENSSL_NO_HEARTBEATS |
| 1898 | else if (type == TLSEXT_TYPE_heartbeat) |
| 1899 | { |
| 1900 | switch(data[0]) |
| 1901 | { |
| 1902 | case 0x01: /* Server allows us to send HB requests */ |
| 1903 | s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED; |
| 1904 | break; |
| 1905 | case 0x02: /* Server doesn't accept HB requests */ |
| 1906 | s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED; |
| 1907 | s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS; |
| 1908 | break; |
| 1909 | default: *al = SSL_AD_ILLEGAL_PARAMETER; |
| 1910 | return 0; |
| 1911 | } |
| 1912 | } |
| 1913 | #endif |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 1914 | #ifndef OPENSSL_NO_SRTP |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1915 | else if (type == TLSEXT_TYPE_use_srtp) |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 1916 | { |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1917 | if(ssl_parse_serverhello_use_srtp_ext(s, data, size, |
| 1918 | al)) |
| 1919 | return 0; |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 1920 | } |
| 1921 | #endif |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1922 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1923 | data+=size; |
| 1924 | } |
| 1925 | |
| 1926 | if (data != d+n) |
| 1927 | { |
| 1928 | *al = SSL_AD_DECODE_ERROR; |
| 1929 | return 0; |
| 1930 | } |
| 1931 | |
| 1932 | if (!s->hit && tlsext_servername == 1) |
| 1933 | { |
| 1934 | if (s->tlsext_hostname) |
| 1935 | { |
| 1936 | if (s->session->tlsext_hostname == NULL) |
| 1937 | { |
| 1938 | s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname); |
| 1939 | if (!s->session->tlsext_hostname) |
| 1940 | { |
| 1941 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 1942 | return 0; |
| 1943 | } |
| 1944 | } |
| 1945 | else |
| 1946 | { |
| 1947 | *al = SSL_AD_DECODE_ERROR; |
| 1948 | return 0; |
| 1949 | } |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | *p = data; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1954 | |
| 1955 | ri_check: |
| 1956 | |
| 1957 | /* Determine if we need to see RI. Strictly speaking if we want to |
| 1958 | * avoid an attack we should *always* see RI even on initial server |
| 1959 | * hello because the client doesn't see any renegotiation during an |
| 1960 | * attack. However this would mean we could not connect to any server |
| 1961 | * which doesn't support RI so for the immediate future tolerate RI |
| 1962 | * absence on initial connect only. |
| 1963 | */ |
| 1964 | if (!renegotiate_seen |
| 1965 | && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) |
| 1966 | && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) |
| 1967 | { |
| 1968 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 1969 | SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, |
| 1970 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); |
| 1971 | return 0; |
| 1972 | } |
| 1973 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1974 | return 1; |
| 1975 | } |
| 1976 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1977 | |
| 1978 | int ssl_prepare_clienthello_tlsext(SSL *s) |
| 1979 | { |
| 1980 | #ifndef OPENSSL_NO_EC |
| 1981 | /* If we are client and using an elliptic curve cryptography cipher suite, send the point formats |
| 1982 | * and elliptic curves we support. |
| 1983 | */ |
| 1984 | int using_ecc = 0; |
| 1985 | int i; |
| 1986 | unsigned char *j; |
| 1987 | unsigned long alg_k, alg_a; |
| 1988 | STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s); |
| 1989 | |
| 1990 | for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) |
| 1991 | { |
| 1992 | SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i); |
| 1993 | |
| 1994 | alg_k = c->algorithm_mkey; |
| 1995 | alg_a = c->algorithm_auth; |
| 1996 | if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe) || (alg_a & SSL_aECDSA))) |
| 1997 | { |
| 1998 | using_ecc = 1; |
| 1999 | break; |
| 2000 | } |
| 2001 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2002 | using_ecc = using_ecc && (s->version >= TLS1_VERSION); |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2003 | if (using_ecc) |
| 2004 | { |
| 2005 | if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist); |
| 2006 | if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL) |
| 2007 | { |
| 2008 | SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE); |
| 2009 | return -1; |
| 2010 | } |
| 2011 | s->tlsext_ecpointformatlist_length = 3; |
| 2012 | s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed; |
| 2013 | s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
| 2014 | s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; |
| 2015 | |
| 2016 | /* we support all named elliptic curves in draft-ietf-tls-ecc-12 */ |
| 2017 | if (s->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->tlsext_ellipticcurvelist); |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2018 | s->tlsext_ellipticcurvelist_length = sizeof(pref_list)/sizeof(pref_list[0]) * 2; |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2019 | if ((s->tlsext_ellipticcurvelist = OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == NULL) |
| 2020 | { |
| 2021 | s->tlsext_ellipticcurvelist_length = 0; |
| 2022 | SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE); |
| 2023 | return -1; |
| 2024 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2025 | for (i = 0, j = s->tlsext_ellipticcurvelist; (unsigned int)i < |
| 2026 | sizeof(pref_list)/sizeof(pref_list[0]); i++) |
| 2027 | { |
| 2028 | int id = tls1_ec_nid2curve_id(pref_list[i]); |
| 2029 | s2n(id,j); |
| 2030 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2031 | } |
| 2032 | #endif /* OPENSSL_NO_EC */ |
| 2033 | |
| 2034 | #ifdef TLSEXT_TYPE_opaque_prf_input |
| 2035 | { |
| 2036 | int r = 1; |
| 2037 | |
| 2038 | if (s->ctx->tlsext_opaque_prf_input_callback != 0) |
| 2039 | { |
| 2040 | r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg); |
| 2041 | if (!r) |
| 2042 | return -1; |
| 2043 | } |
| 2044 | |
| 2045 | if (s->tlsext_opaque_prf_input != NULL) |
| 2046 | { |
| 2047 | if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */ |
| 2048 | OPENSSL_free(s->s3->client_opaque_prf_input); |
| 2049 | |
| 2050 | if (s->tlsext_opaque_prf_input_len == 0) |
| 2051 | s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */ |
| 2052 | else |
| 2053 | s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len); |
| 2054 | if (s->s3->client_opaque_prf_input == NULL) |
| 2055 | { |
| 2056 | SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE); |
| 2057 | return -1; |
| 2058 | } |
| 2059 | s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len; |
| 2060 | } |
| 2061 | |
| 2062 | if (r == 2) |
| 2063 | /* at callback's request, insist on receiving an appropriate server opaque PRF input */ |
| 2064 | s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len; |
| 2065 | } |
| 2066 | #endif |
| 2067 | |
| 2068 | return 1; |
| 2069 | } |
| 2070 | |
| 2071 | int ssl_prepare_serverhello_tlsext(SSL *s) |
| 2072 | { |
| 2073 | #ifndef OPENSSL_NO_EC |
| 2074 | /* If we are server and using an ECC cipher suite, send the point formats we support |
| 2075 | * if the client sent us an ECPointsFormat extension. Note that the server is not |
| 2076 | * supposed to send an EllipticCurves extension. |
| 2077 | */ |
| 2078 | |
| 2079 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 2080 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 2081 | int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA); |
| 2082 | using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL); |
| 2083 | |
| 2084 | if (using_ecc) |
| 2085 | { |
| 2086 | if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist); |
| 2087 | if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL) |
| 2088 | { |
| 2089 | SSLerr(SSL_F_SSL_PREPARE_SERVERHELLO_TLSEXT,ERR_R_MALLOC_FAILURE); |
| 2090 | return -1; |
| 2091 | } |
| 2092 | s->tlsext_ecpointformatlist_length = 3; |
| 2093 | s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed; |
| 2094 | s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
| 2095 | s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; |
| 2096 | } |
| 2097 | #endif /* OPENSSL_NO_EC */ |
| 2098 | |
| 2099 | return 1; |
| 2100 | } |
| 2101 | |
Brian Carlstrom | 04ef91b | 2013-02-05 09:20:38 -0800 | [diff] [blame] | 2102 | int ssl_check_clienthello_tlsext_early(SSL *s) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2103 | { |
| 2104 | int ret=SSL_TLSEXT_ERR_NOACK; |
| 2105 | int al = SSL_AD_UNRECOGNIZED_NAME; |
| 2106 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2107 | #ifndef OPENSSL_NO_EC |
| 2108 | /* The handling of the ECPointFormats extension is done elsewhere, namely in |
| 2109 | * ssl3_choose_cipher in s3_lib.c. |
| 2110 | */ |
| 2111 | /* The handling of the EllipticCurves extension is done elsewhere, namely in |
| 2112 | * ssl3_choose_cipher in s3_lib.c. |
| 2113 | */ |
| 2114 | #endif |
| 2115 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2116 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) |
| 2117 | ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg); |
| 2118 | else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0) |
| 2119 | ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg); |
| 2120 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2121 | #ifdef TLSEXT_TYPE_opaque_prf_input |
| 2122 | { |
| 2123 | /* This sort of belongs into ssl_prepare_serverhello_tlsext(), |
| 2124 | * but we might be sending an alert in response to the client hello, |
Brian Carlstrom | 04ef91b | 2013-02-05 09:20:38 -0800 | [diff] [blame] | 2125 | * so this has to happen here in |
| 2126 | * ssl_check_clienthello_tlsext_early(). */ |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2127 | |
| 2128 | int r = 1; |
| 2129 | |
| 2130 | if (s->ctx->tlsext_opaque_prf_input_callback != 0) |
| 2131 | { |
| 2132 | r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg); |
| 2133 | if (!r) |
| 2134 | { |
| 2135 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2136 | al = SSL_AD_INTERNAL_ERROR; |
| 2137 | goto err; |
| 2138 | } |
| 2139 | } |
| 2140 | |
| 2141 | if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */ |
| 2142 | OPENSSL_free(s->s3->server_opaque_prf_input); |
| 2143 | s->s3->server_opaque_prf_input = NULL; |
| 2144 | |
| 2145 | if (s->tlsext_opaque_prf_input != NULL) |
| 2146 | { |
| 2147 | if (s->s3->client_opaque_prf_input != NULL && |
| 2148 | s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len) |
| 2149 | { |
| 2150 | /* can only use this extension if we have a server opaque PRF input |
| 2151 | * of the same length as the client opaque PRF input! */ |
| 2152 | |
| 2153 | if (s->tlsext_opaque_prf_input_len == 0) |
| 2154 | s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */ |
| 2155 | else |
| 2156 | s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len); |
| 2157 | if (s->s3->server_opaque_prf_input == NULL) |
| 2158 | { |
| 2159 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2160 | al = SSL_AD_INTERNAL_ERROR; |
| 2161 | goto err; |
| 2162 | } |
| 2163 | s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len; |
| 2164 | } |
| 2165 | } |
| 2166 | |
| 2167 | if (r == 2 && s->s3->server_opaque_prf_input == NULL) |
| 2168 | { |
| 2169 | /* The callback wants to enforce use of the extension, |
| 2170 | * but we can't do that with the client opaque PRF input; |
| 2171 | * abort the handshake. |
| 2172 | */ |
| 2173 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2174 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 2175 | } |
| 2176 | } |
| 2177 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2178 | err: |
Brian Carlstrom | 04ef91b | 2013-02-05 09:20:38 -0800 | [diff] [blame] | 2179 | #endif |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2180 | switch (ret) |
| 2181 | { |
| 2182 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2183 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2184 | return -1; |
| 2185 | |
| 2186 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 2187 | ssl3_send_alert(s,SSL3_AL_WARNING,al); |
| 2188 | return 1; |
| 2189 | |
| 2190 | case SSL_TLSEXT_ERR_NOACK: |
| 2191 | s->servername_done=0; |
| 2192 | default: |
| 2193 | return 1; |
| 2194 | } |
| 2195 | } |
| 2196 | |
Brian Carlstrom | 04ef91b | 2013-02-05 09:20:38 -0800 | [diff] [blame] | 2197 | int ssl_check_clienthello_tlsext_late(SSL *s) |
| 2198 | { |
| 2199 | int ret = SSL_TLSEXT_ERR_OK; |
| 2200 | int al; |
| 2201 | |
| 2202 | /* If status request then ask callback what to do. |
| 2203 | * Note: this must be called after servername callbacks in case |
| 2204 | * the certificate has changed, and must be called after the cipher |
| 2205 | * has been chosen because this may influence which certificate is sent |
| 2206 | */ |
| 2207 | if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) |
| 2208 | { |
| 2209 | int r; |
| 2210 | CERT_PKEY *certpkey; |
| 2211 | certpkey = ssl_get_server_send_pkey(s); |
| 2212 | /* If no certificate can't return certificate status */ |
| 2213 | if (certpkey == NULL) |
| 2214 | { |
| 2215 | s->tlsext_status_expected = 0; |
| 2216 | return 1; |
| 2217 | } |
| 2218 | /* Set current certificate to one we will use so |
| 2219 | * SSL_get_certificate et al can pick it up. |
| 2220 | */ |
| 2221 | s->cert->key = certpkey; |
| 2222 | r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); |
| 2223 | switch (r) |
| 2224 | { |
| 2225 | /* We don't want to send a status request response */ |
| 2226 | case SSL_TLSEXT_ERR_NOACK: |
| 2227 | s->tlsext_status_expected = 0; |
| 2228 | break; |
| 2229 | /* status request response should be sent */ |
| 2230 | case SSL_TLSEXT_ERR_OK: |
| 2231 | if (s->tlsext_ocsp_resp) |
| 2232 | s->tlsext_status_expected = 1; |
| 2233 | else |
| 2234 | s->tlsext_status_expected = 0; |
| 2235 | break; |
| 2236 | /* something bad happened */ |
| 2237 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2238 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2239 | al = SSL_AD_INTERNAL_ERROR; |
| 2240 | goto err; |
| 2241 | } |
| 2242 | } |
| 2243 | else |
| 2244 | s->tlsext_status_expected = 0; |
| 2245 | |
| 2246 | err: |
| 2247 | switch (ret) |
| 2248 | { |
| 2249 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2250 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2251 | return -1; |
| 2252 | |
| 2253 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 2254 | ssl3_send_alert(s,SSL3_AL_WARNING,al); |
| 2255 | return 1; |
| 2256 | |
| 2257 | default: |
| 2258 | return 1; |
| 2259 | } |
| 2260 | } |
| 2261 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2262 | int ssl_check_serverhello_tlsext(SSL *s) |
| 2263 | { |
| 2264 | int ret=SSL_TLSEXT_ERR_NOACK; |
| 2265 | int al = SSL_AD_UNRECOGNIZED_NAME; |
| 2266 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2267 | #ifndef OPENSSL_NO_EC |
Brian Carlstrom | 976a034 | 2010-12-03 16:11:54 -0800 | [diff] [blame] | 2268 | /* If we are client and using an elliptic curve cryptography cipher |
| 2269 | * suite, then if server returns an EC point formats lists extension |
| 2270 | * it must contain uncompressed. |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2271 | */ |
| 2272 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 2273 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 2274 | if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) && |
Brian Carlstrom | 976a034 | 2010-12-03 16:11:54 -0800 | [diff] [blame] | 2275 | (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) && |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2276 | ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA))) |
| 2277 | { |
| 2278 | /* we are using an ECC cipher */ |
| 2279 | size_t i; |
| 2280 | unsigned char *list; |
| 2281 | int found_uncompressed = 0; |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2282 | list = s->session->tlsext_ecpointformatlist; |
| 2283 | for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) |
| 2284 | { |
| 2285 | if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) |
| 2286 | { |
| 2287 | found_uncompressed = 1; |
| 2288 | break; |
| 2289 | } |
| 2290 | } |
| 2291 | if (!found_uncompressed) |
| 2292 | { |
| 2293 | SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST); |
| 2294 | return -1; |
| 2295 | } |
| 2296 | } |
| 2297 | ret = SSL_TLSEXT_ERR_OK; |
| 2298 | #endif /* OPENSSL_NO_EC */ |
| 2299 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2300 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) |
| 2301 | ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg); |
| 2302 | else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0) |
| 2303 | ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg); |
| 2304 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2305 | #ifdef TLSEXT_TYPE_opaque_prf_input |
| 2306 | if (s->s3->server_opaque_prf_input_len > 0) |
| 2307 | { |
| 2308 | /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs. |
| 2309 | * So first verify that we really have a value from the server too. */ |
| 2310 | |
| 2311 | if (s->s3->server_opaque_prf_input == NULL) |
| 2312 | { |
| 2313 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2314 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 2315 | } |
| 2316 | |
| 2317 | /* Anytime the server *has* sent an opaque PRF input, we need to check |
| 2318 | * that we have a client opaque PRF input of the same size. */ |
| 2319 | if (s->s3->client_opaque_prf_input == NULL || |
| 2320 | s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len) |
| 2321 | { |
| 2322 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2323 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 2324 | } |
| 2325 | } |
| 2326 | #endif |
| 2327 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2328 | /* If we've requested certificate status and we wont get one |
| 2329 | * tell the callback |
| 2330 | */ |
| 2331 | if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected) |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2332 | && s->ctx && s->ctx->tlsext_status_cb) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2333 | { |
| 2334 | int r; |
| 2335 | /* Set resp to NULL, resplen to -1 so callback knows |
| 2336 | * there is no response. |
| 2337 | */ |
| 2338 | if (s->tlsext_ocsp_resp) |
| 2339 | { |
| 2340 | OPENSSL_free(s->tlsext_ocsp_resp); |
| 2341 | s->tlsext_ocsp_resp = NULL; |
| 2342 | } |
| 2343 | s->tlsext_ocsp_resplen = -1; |
| 2344 | r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); |
| 2345 | if (r == 0) |
| 2346 | { |
| 2347 | al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; |
| 2348 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2349 | } |
| 2350 | if (r < 0) |
| 2351 | { |
| 2352 | al = SSL_AD_INTERNAL_ERROR; |
| 2353 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2354 | } |
| 2355 | } |
| 2356 | |
| 2357 | switch (ret) |
| 2358 | { |
| 2359 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2360 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2361 | return -1; |
| 2362 | |
| 2363 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 2364 | ssl3_send_alert(s,SSL3_AL_WARNING,al); |
| 2365 | return 1; |
| 2366 | |
| 2367 | case SSL_TLSEXT_ERR_NOACK: |
| 2368 | s->servername_done=0; |
| 2369 | default: |
| 2370 | return 1; |
| 2371 | } |
| 2372 | } |
| 2373 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2374 | /* Since the server cache lookup is done early on in the processing of the |
| 2375 | * ClientHello, and other operations depend on the result, we need to handle |
| 2376 | * any TLS session ticket extension at the same time. |
| 2377 | * |
| 2378 | * session_id: points at the session ID in the ClientHello. This code will |
| 2379 | * read past the end of this in order to parse out the session ticket |
| 2380 | * extension, if any. |
| 2381 | * len: the length of the session ID. |
| 2382 | * limit: a pointer to the first byte after the ClientHello. |
| 2383 | * ret: (output) on return, if a ticket was decrypted, then this is set to |
| 2384 | * point to the resulting session. |
| 2385 | * |
| 2386 | * If s->tls_session_secret_cb is set then we are expecting a pre-shared key |
| 2387 | * ciphersuite, in which case we have no use for session tickets and one will |
| 2388 | * never be decrypted, nor will s->tlsext_ticket_expected be set to 1. |
| 2389 | * |
| 2390 | * Returns: |
| 2391 | * -1: fatal error, either from parsing or decrypting the ticket. |
| 2392 | * 0: no ticket was found (or was ignored, based on settings). |
| 2393 | * 1: a zero length extension was found, indicating that the client supports |
| 2394 | * session tickets but doesn't currently have one to offer. |
| 2395 | * 2: either s->tls_session_secret_cb was set, or a ticket was offered but |
| 2396 | * couldn't be decrypted because of a non-fatal error. |
| 2397 | * 3: a ticket was successfully decrypted and *ret was set. |
| 2398 | * |
| 2399 | * Side effects: |
| 2400 | * Sets s->tlsext_ticket_expected to 1 if the server will have to issue |
| 2401 | * a new session ticket to the client because the client indicated support |
| 2402 | * (and s->tls_session_secret_cb is NULL) but the client either doesn't have |
| 2403 | * a session ticket or we couldn't use the one it gave us, or if |
| 2404 | * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket. |
| 2405 | * Otherwise, s->tlsext_ticket_expected is set to 0. |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2406 | */ |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2407 | int tls1_process_ticket(SSL *s, unsigned char *session_id, int len, |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2408 | const unsigned char *limit, SSL_SESSION **ret) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2409 | { |
| 2410 | /* Point after session ID in client hello */ |
| 2411 | const unsigned char *p = session_id + len; |
| 2412 | unsigned short i; |
Nagendra Modadugu | e45f106 | 2009-09-30 11:36:48 -0700 | [diff] [blame] | 2413 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2414 | *ret = NULL; |
| 2415 | s->tlsext_ticket_expected = 0; |
Nagendra Modadugu | e45f106 | 2009-09-30 11:36:48 -0700 | [diff] [blame] | 2416 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2417 | /* If tickets disabled behave as if no ticket present |
| 2418 | * to permit stateful resumption. |
| 2419 | */ |
| 2420 | if (SSL_get_options(s) & SSL_OP_NO_TICKET) |
| 2421 | return 0; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2422 | if ((s->version <= SSL3_VERSION) || !limit) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2423 | return 0; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2424 | if (p >= limit) |
| 2425 | return -1; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 2426 | /* Skip past DTLS cookie */ |
| 2427 | if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) |
| 2428 | { |
| 2429 | i = *(p++); |
| 2430 | p+= i; |
| 2431 | if (p >= limit) |
| 2432 | return -1; |
| 2433 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2434 | /* Skip past cipher list */ |
| 2435 | n2s(p, i); |
| 2436 | p+= i; |
| 2437 | if (p >= limit) |
| 2438 | return -1; |
| 2439 | /* Skip past compression algorithm list */ |
| 2440 | i = *(p++); |
| 2441 | p += i; |
| 2442 | if (p > limit) |
| 2443 | return -1; |
| 2444 | /* Now at start of extensions */ |
| 2445 | if ((p + 2) >= limit) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2446 | return 0; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2447 | n2s(p, i); |
| 2448 | while ((p + 4) <= limit) |
| 2449 | { |
| 2450 | unsigned short type, size; |
| 2451 | n2s(p, type); |
| 2452 | n2s(p, size); |
| 2453 | if (p + size > limit) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2454 | return 0; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2455 | if (type == TLSEXT_TYPE_session_ticket) |
| 2456 | { |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2457 | int r; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2458 | if (size == 0) |
| 2459 | { |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2460 | /* The client will accept a ticket but doesn't |
| 2461 | * currently have one. */ |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2462 | s->tlsext_ticket_expected = 1; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2463 | return 1; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2464 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2465 | if (s->tls_session_secret_cb) |
| 2466 | { |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2467 | /* Indicate that the ticket couldn't be |
| 2468 | * decrypted rather than generating the session |
| 2469 | * from ticket now, trigger abbreviated |
| 2470 | * handshake based on external mechanism to |
| 2471 | * calculate the master secret later. */ |
| 2472 | return 2; |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2473 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2474 | r = tls_decrypt_ticket(s, p, size, session_id, len, ret); |
| 2475 | switch (r) |
| 2476 | { |
| 2477 | case 2: /* ticket couldn't be decrypted */ |
| 2478 | s->tlsext_ticket_expected = 1; |
| 2479 | return 2; |
| 2480 | case 3: /* ticket was decrypted */ |
| 2481 | return r; |
| 2482 | case 4: /* ticket decrypted but need to renew */ |
| 2483 | s->tlsext_ticket_expected = 1; |
| 2484 | return 3; |
| 2485 | default: /* fatal error */ |
| 2486 | return -1; |
| 2487 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2488 | } |
| 2489 | p += size; |
| 2490 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2491 | return 0; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2492 | } |
| 2493 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2494 | /* tls_decrypt_ticket attempts to decrypt a session ticket. |
| 2495 | * |
| 2496 | * etick: points to the body of the session ticket extension. |
| 2497 | * eticklen: the length of the session tickets extenion. |
| 2498 | * sess_id: points at the session ID. |
| 2499 | * sesslen: the length of the session ID. |
| 2500 | * psess: (output) on return, if a ticket was decrypted, then this is set to |
| 2501 | * point to the resulting session. |
| 2502 | * |
| 2503 | * Returns: |
| 2504 | * -1: fatal error, either from parsing or decrypting the ticket. |
| 2505 | * 2: the ticket couldn't be decrypted. |
| 2506 | * 3: a ticket was successfully decrypted and *psess was set. |
| 2507 | * 4: same as 3, but the ticket needs to be renewed. |
| 2508 | */ |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2509 | static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen, |
| 2510 | const unsigned char *sess_id, int sesslen, |
| 2511 | SSL_SESSION **psess) |
| 2512 | { |
| 2513 | SSL_SESSION *sess; |
| 2514 | unsigned char *sdec; |
| 2515 | const unsigned char *p; |
| 2516 | int slen, mlen, renew_ticket = 0; |
| 2517 | unsigned char tick_hmac[EVP_MAX_MD_SIZE]; |
| 2518 | HMAC_CTX hctx; |
| 2519 | EVP_CIPHER_CTX ctx; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 2520 | SSL_CTX *tctx = s->initial_ctx; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2521 | /* Need at least keyname + iv + some encrypted data */ |
| 2522 | if (eticklen < 48) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2523 | return 2; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2524 | /* Initialize session ticket encryption and HMAC contexts */ |
| 2525 | HMAC_CTX_init(&hctx); |
| 2526 | EVP_CIPHER_CTX_init(&ctx); |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 2527 | if (tctx->tlsext_ticket_key_cb) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2528 | { |
| 2529 | unsigned char *nctick = (unsigned char *)etick; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 2530 | int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2531 | &ctx, &hctx, 0); |
| 2532 | if (rv < 0) |
| 2533 | return -1; |
| 2534 | if (rv == 0) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2535 | return 2; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2536 | if (rv == 2) |
| 2537 | renew_ticket = 1; |
| 2538 | } |
| 2539 | else |
| 2540 | { |
| 2541 | /* Check key name matches */ |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 2542 | if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2543 | return 2; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 2544 | HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2545 | tlsext_tick_md(), NULL); |
| 2546 | EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 2547 | tctx->tlsext_tick_aes_key, etick + 16); |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2548 | } |
| 2549 | /* Attempt to process session ticket, first conduct sanity and |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2550 | * integrity checks on ticket. |
| 2551 | */ |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2552 | mlen = HMAC_size(&hctx); |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2553 | if (mlen < 0) |
| 2554 | { |
| 2555 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2556 | return -1; |
| 2557 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2558 | eticklen -= mlen; |
| 2559 | /* Check HMAC of encrypted ticket */ |
| 2560 | HMAC_Update(&hctx, etick, eticklen); |
| 2561 | HMAC_Final(&hctx, tick_hmac, NULL); |
| 2562 | HMAC_CTX_cleanup(&hctx); |
Brian Carlstrom | fa75fdb | 2013-02-01 23:44:05 -0800 | [diff] [blame] | 2563 | if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2564 | return 2; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2565 | /* Attempt to decrypt session data */ |
| 2566 | /* Move p after IV to start of encrypted ticket, update length */ |
| 2567 | p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx); |
| 2568 | eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx); |
| 2569 | sdec = OPENSSL_malloc(eticklen); |
| 2570 | if (!sdec) |
| 2571 | { |
| 2572 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2573 | return -1; |
| 2574 | } |
| 2575 | EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen); |
| 2576 | if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2577 | return 2; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2578 | slen += mlen; |
| 2579 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2580 | p = sdec; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2581 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2582 | sess = d2i_SSL_SESSION(NULL, &p, slen); |
| 2583 | OPENSSL_free(sdec); |
| 2584 | if (sess) |
| 2585 | { |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2586 | /* The session ID, if non-empty, is used by some clients to |
| 2587 | * detect that the ticket has been accepted. So we copy it to |
| 2588 | * the session structure. If it is empty set length to zero |
| 2589 | * as required by standard. |
| 2590 | */ |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2591 | if (sesslen) |
| 2592 | memcpy(sess->session_id, sess_id, sesslen); |
| 2593 | sess->session_id_length = sesslen; |
| 2594 | *psess = sess; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2595 | if (renew_ticket) |
| 2596 | return 4; |
| 2597 | else |
| 2598 | return 3; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2599 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2600 | ERR_clear_error(); |
| 2601 | /* For session parse failure, indicate that we need to send a new |
| 2602 | * ticket. */ |
| 2603 | return 2; |
| 2604 | } |
| 2605 | |
| 2606 | /* Tables to translate from NIDs to TLS v1.2 ids */ |
| 2607 | |
| 2608 | typedef struct |
| 2609 | { |
| 2610 | int nid; |
| 2611 | int id; |
| 2612 | } tls12_lookup; |
| 2613 | |
| 2614 | static tls12_lookup tls12_md[] = { |
| 2615 | #ifndef OPENSSL_NO_MD5 |
| 2616 | {NID_md5, TLSEXT_hash_md5}, |
| 2617 | #endif |
| 2618 | #ifndef OPENSSL_NO_SHA |
| 2619 | {NID_sha1, TLSEXT_hash_sha1}, |
| 2620 | #endif |
| 2621 | #ifndef OPENSSL_NO_SHA256 |
| 2622 | {NID_sha224, TLSEXT_hash_sha224}, |
| 2623 | {NID_sha256, TLSEXT_hash_sha256}, |
| 2624 | #endif |
| 2625 | #ifndef OPENSSL_NO_SHA512 |
| 2626 | {NID_sha384, TLSEXT_hash_sha384}, |
| 2627 | {NID_sha512, TLSEXT_hash_sha512} |
| 2628 | #endif |
| 2629 | }; |
| 2630 | |
| 2631 | static tls12_lookup tls12_sig[] = { |
| 2632 | #ifndef OPENSSL_NO_RSA |
| 2633 | {EVP_PKEY_RSA, TLSEXT_signature_rsa}, |
| 2634 | #endif |
| 2635 | #ifndef OPENSSL_NO_DSA |
| 2636 | {EVP_PKEY_DSA, TLSEXT_signature_dsa}, |
| 2637 | #endif |
| 2638 | #ifndef OPENSSL_NO_ECDSA |
| 2639 | {EVP_PKEY_EC, TLSEXT_signature_ecdsa} |
| 2640 | #endif |
| 2641 | }; |
| 2642 | |
| 2643 | static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen) |
| 2644 | { |
| 2645 | size_t i; |
| 2646 | for (i = 0; i < tlen; i++) |
| 2647 | { |
| 2648 | if (table[i].nid == nid) |
| 2649 | return table[i].id; |
| 2650 | } |
| 2651 | return -1; |
| 2652 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2653 | |
| 2654 | int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md) |
| 2655 | { |
| 2656 | int sig_id, md_id; |
| 2657 | if (!md) |
| 2658 | return 0; |
| 2659 | md_id = tls12_find_id(EVP_MD_type(md), tls12_md, |
| 2660 | sizeof(tls12_md)/sizeof(tls12_lookup)); |
| 2661 | if (md_id == -1) |
| 2662 | return 0; |
| 2663 | sig_id = tls12_get_sigid(pk); |
| 2664 | if (sig_id == -1) |
| 2665 | return 0; |
| 2666 | p[0] = (unsigned char)md_id; |
| 2667 | p[1] = (unsigned char)sig_id; |
| 2668 | return 1; |
| 2669 | } |
| 2670 | |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2671 | /* tls12_get_sigid returns the TLS 1.2 SignatureAlgorithm value corresponding |
| 2672 | * to the given public key, or -1 if not known. */ |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2673 | int tls12_get_sigid(const EVP_PKEY *pk) |
| 2674 | { |
| 2675 | return tls12_find_id(pk->type, tls12_sig, |
| 2676 | sizeof(tls12_sig)/sizeof(tls12_lookup)); |
| 2677 | } |
| 2678 | |
| 2679 | const EVP_MD *tls12_get_hash(unsigned char hash_alg) |
| 2680 | { |
| 2681 | switch(hash_alg) |
| 2682 | { |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2683 | #ifndef OPENSSL_NO_SHA |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2684 | case TLSEXT_hash_sha1: |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2685 | return EVP_sha1(); |
| 2686 | #endif |
| 2687 | #ifndef OPENSSL_NO_SHA256 |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2688 | case TLSEXT_hash_sha224: |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2689 | return EVP_sha224(); |
| 2690 | |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2691 | case TLSEXT_hash_sha256: |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2692 | return EVP_sha256(); |
| 2693 | #endif |
| 2694 | #ifndef OPENSSL_NO_SHA512 |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2695 | case TLSEXT_hash_sha384: |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2696 | return EVP_sha384(); |
| 2697 | |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2698 | case TLSEXT_hash_sha512: |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2699 | return EVP_sha512(); |
| 2700 | #endif |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2701 | default: |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2702 | return NULL; |
| 2703 | |
| 2704 | } |
| 2705 | } |
| 2706 | |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2707 | /* tls1_process_sigalgs processes a signature_algorithms extension and sets the |
| 2708 | * digest functions accordingly for each key type. |
| 2709 | * |
| 2710 | * See RFC 5246, section 7.4.1.4.1. |
| 2711 | * |
| 2712 | * data: points to the content of the extension, not including type and length |
| 2713 | * headers. |
| 2714 | * dsize: the number of bytes of |data|. Must be even. |
| 2715 | */ |
| 2716 | void tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2717 | { |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2718 | int i; |
| 2719 | const EVP_MD *md, **digest_ptr; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2720 | /* Extension ignored for TLS versions below 1.2 */ |
| 2721 | if (TLS1_get_version(s) < TLS1_2_VERSION) |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2722 | return; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2723 | |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2724 | s->s3->digest_rsa = NULL; |
| 2725 | s->s3->digest_dsa = NULL; |
| 2726 | s->s3->digest_ecdsa = NULL; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2727 | |
| 2728 | for (i = 0; i < dsize; i += 2) |
| 2729 | { |
| 2730 | unsigned char hash_alg = data[i], sig_alg = data[i+1]; |
| 2731 | |
| 2732 | switch(sig_alg) |
| 2733 | { |
| 2734 | #ifndef OPENSSL_NO_RSA |
| 2735 | case TLSEXT_signature_rsa: |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2736 | digest_ptr = &s->s3->digest_rsa; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2737 | break; |
| 2738 | #endif |
| 2739 | #ifndef OPENSSL_NO_DSA |
| 2740 | case TLSEXT_signature_dsa: |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2741 | digest_ptr = &s->s3->digest_dsa; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2742 | break; |
| 2743 | #endif |
| 2744 | #ifndef OPENSSL_NO_ECDSA |
| 2745 | case TLSEXT_signature_ecdsa: |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2746 | digest_ptr = &s->s3->digest_ecdsa; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2747 | break; |
| 2748 | #endif |
| 2749 | default: |
| 2750 | continue; |
| 2751 | } |
| 2752 | |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2753 | if (*digest_ptr == NULL) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2754 | { |
| 2755 | md = tls12_get_hash(hash_alg); |
| 2756 | if (md) |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2757 | *digest_ptr = md; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2761 | } |
| 2762 | |
| 2763 | #endif |
| 2764 | |
| 2765 | #ifndef OPENSSL_NO_HEARTBEATS |
| 2766 | int |
| 2767 | tls1_process_heartbeat(SSL *s) |
| 2768 | { |
| 2769 | unsigned char *p = &s->s3->rrec.data[0], *pl; |
| 2770 | unsigned short hbtype; |
| 2771 | unsigned int payload; |
| 2772 | unsigned int padding = 16; /* Use minimum padding */ |
| 2773 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2774 | if (s->msg_callback) |
| 2775 | s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT, |
| 2776 | &s->s3->rrec.data[0], s->s3->rrec.length, |
| 2777 | s, s->msg_callback_arg); |
| 2778 | |
Kenny Root | 7f7ea2d | 2014-04-07 13:17:43 -0700 | [diff] [blame] | 2779 | /* Read type and payload length first */ |
| 2780 | if (1 + 2 + 16 > s->s3->rrec.length) |
| 2781 | return 0; /* silently discard */ |
| 2782 | hbtype = *p++; |
| 2783 | n2s(p, payload); |
| 2784 | if (1 + 2 + payload + 16 > s->s3->rrec.length) |
| 2785 | return 0; /* silently discard per RFC 6520 sec. 4 */ |
| 2786 | pl = p; |
| 2787 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2788 | if (hbtype == TLS1_HB_REQUEST) |
| 2789 | { |
| 2790 | unsigned char *buffer, *bp; |
| 2791 | int r; |
| 2792 | |
| 2793 | /* Allocate memory for the response, size is 1 bytes |
| 2794 | * message type, plus 2 bytes payload length, plus |
| 2795 | * payload, plus padding |
| 2796 | */ |
| 2797 | buffer = OPENSSL_malloc(1 + 2 + payload + padding); |
| 2798 | bp = buffer; |
| 2799 | |
| 2800 | /* Enter response type, length and copy payload */ |
| 2801 | *bp++ = TLS1_HB_RESPONSE; |
| 2802 | s2n(payload, bp); |
| 2803 | memcpy(bp, pl, payload); |
| 2804 | bp += payload; |
| 2805 | /* Random padding */ |
| 2806 | RAND_pseudo_bytes(bp, padding); |
| 2807 | |
| 2808 | r = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buffer, 3 + payload + padding); |
| 2809 | |
| 2810 | if (r >= 0 && s->msg_callback) |
| 2811 | s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT, |
| 2812 | buffer, 3 + payload + padding, |
| 2813 | s, s->msg_callback_arg); |
| 2814 | |
| 2815 | OPENSSL_free(buffer); |
| 2816 | |
| 2817 | if (r < 0) |
| 2818 | return r; |
| 2819 | } |
| 2820 | else if (hbtype == TLS1_HB_RESPONSE) |
| 2821 | { |
| 2822 | unsigned int seq; |
| 2823 | |
| 2824 | /* We only send sequence numbers (2 bytes unsigned int), |
| 2825 | * and 16 random bytes, so we just try to read the |
| 2826 | * sequence number */ |
| 2827 | n2s(pl, seq); |
| 2828 | |
| 2829 | if (payload == 18 && seq == s->tlsext_hb_seq) |
| 2830 | { |
| 2831 | s->tlsext_hb_seq++; |
| 2832 | s->tlsext_hb_pending = 0; |
| 2833 | } |
| 2834 | } |
| 2835 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2836 | return 0; |
| 2837 | } |
| 2838 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2839 | int |
| 2840 | tls1_heartbeat(SSL *s) |
| 2841 | { |
| 2842 | unsigned char *buf, *p; |
| 2843 | int ret; |
| 2844 | unsigned int payload = 18; /* Sequence number + random bytes */ |
| 2845 | unsigned int padding = 16; /* Use minimum padding */ |
| 2846 | |
| 2847 | /* Only send if peer supports and accepts HB requests... */ |
| 2848 | if (!(s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) || |
| 2849 | s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_SEND_REQUESTS) |
| 2850 | { |
| 2851 | SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT); |
| 2852 | return -1; |
| 2853 | } |
| 2854 | |
| 2855 | /* ...and there is none in flight yet... */ |
| 2856 | if (s->tlsext_hb_pending) |
| 2857 | { |
| 2858 | SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_TLS_HEARTBEAT_PENDING); |
| 2859 | return -1; |
| 2860 | } |
| 2861 | |
| 2862 | /* ...and no handshake in progress. */ |
| 2863 | if (SSL_in_init(s) || s->in_handshake) |
| 2864 | { |
| 2865 | SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_UNEXPECTED_MESSAGE); |
| 2866 | return -1; |
| 2867 | } |
| 2868 | |
| 2869 | /* Check if padding is too long, payload and padding |
| 2870 | * must not exceed 2^14 - 3 = 16381 bytes in total. |
| 2871 | */ |
| 2872 | OPENSSL_assert(payload + padding <= 16381); |
| 2873 | |
| 2874 | /* Create HeartBeat message, we just use a sequence number |
| 2875 | * as payload to distuingish different messages and add |
| 2876 | * some random stuff. |
| 2877 | * - Message Type, 1 byte |
| 2878 | * - Payload Length, 2 bytes (unsigned int) |
| 2879 | * - Payload, the sequence number (2 bytes uint) |
| 2880 | * - Payload, random bytes (16 bytes uint) |
| 2881 | * - Padding |
| 2882 | */ |
| 2883 | buf = OPENSSL_malloc(1 + 2 + payload + padding); |
| 2884 | p = buf; |
| 2885 | /* Message Type */ |
| 2886 | *p++ = TLS1_HB_REQUEST; |
| 2887 | /* Payload length (18 bytes here) */ |
| 2888 | s2n(payload, p); |
| 2889 | /* Sequence number */ |
| 2890 | s2n(s->tlsext_hb_seq, p); |
| 2891 | /* 16 random bytes */ |
| 2892 | RAND_pseudo_bytes(p, 16); |
| 2893 | p += 16; |
| 2894 | /* Random padding */ |
| 2895 | RAND_pseudo_bytes(p, padding); |
| 2896 | |
| 2897 | ret = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buf, 3 + payload + padding); |
| 2898 | if (ret >= 0) |
| 2899 | { |
| 2900 | if (s->msg_callback) |
| 2901 | s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT, |
| 2902 | buf, 3 + payload + padding, |
| 2903 | s, s->msg_callback_arg); |
| 2904 | |
| 2905 | s->tlsext_hb_pending = 1; |
| 2906 | } |
| 2907 | |
| 2908 | OPENSSL_free(buf); |
| 2909 | |
| 2910 | return ret; |
| 2911 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2912 | #endif |
Adam Langley | 45bcfbc | 2013-01-16 13:43:53 -0800 | [diff] [blame] | 2913 | |
| 2914 | #if !defined(OPENSSL_NO_TLSEXT) |
| 2915 | /* tls1_channel_id_hash calculates the signed data for a Channel ID on the given |
| 2916 | * SSL connection and writes it to |md|. |
| 2917 | */ |
| 2918 | int |
| 2919 | tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) |
| 2920 | { |
| 2921 | EVP_MD_CTX ctx; |
| 2922 | unsigned char temp_digest[EVP_MAX_MD_SIZE]; |
| 2923 | unsigned temp_digest_len; |
| 2924 | int i; |
| 2925 | static const char kClientIDMagic[] = "TLS Channel ID signature"; |
| 2926 | |
| 2927 | if (s->s3->handshake_buffer) |
| 2928 | if (!ssl3_digest_cached_records(s)) |
| 2929 | return 0; |
| 2930 | |
| 2931 | EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic)); |
| 2932 | |
Kenny Root | 77c6be7 | 2014-06-12 10:08:29 -0700 | [diff] [blame^] | 2933 | if (s->hit && s->s3->tlsext_channel_id_new) |
| 2934 | { |
| 2935 | static const char kResumptionMagic[] = "Resumption"; |
| 2936 | EVP_DigestUpdate(md, kResumptionMagic, |
| 2937 | sizeof(kResumptionMagic)); |
| 2938 | if (s->session->original_handshake_hash_len == 0) |
| 2939 | return 0; |
| 2940 | EVP_DigestUpdate(md, s->session->original_handshake_hash, |
| 2941 | s->session->original_handshake_hash_len); |
| 2942 | } |
| 2943 | |
Adam Langley | 45bcfbc | 2013-01-16 13:43:53 -0800 | [diff] [blame] | 2944 | EVP_MD_CTX_init(&ctx); |
| 2945 | for (i = 0; i < SSL_MAX_DIGEST; i++) |
| 2946 | { |
| 2947 | if (s->s3->handshake_dgst[i] == NULL) |
| 2948 | continue; |
| 2949 | EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]); |
| 2950 | EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len); |
| 2951 | EVP_DigestUpdate(md, temp_digest, temp_digest_len); |
| 2952 | } |
| 2953 | EVP_MD_CTX_cleanup(&ctx); |
| 2954 | |
| 2955 | return 1; |
| 2956 | } |
| 2957 | #endif |
Kenny Root | 77c6be7 | 2014-06-12 10:08:29 -0700 | [diff] [blame^] | 2958 | |
| 2959 | /* tls1_record_handshake_hashes_for_channel_id records the current handshake |
| 2960 | * hashes in |s->session| so that Channel ID resumptions can sign that data. */ |
| 2961 | int tls1_record_handshake_hashes_for_channel_id(SSL *s) |
| 2962 | { |
| 2963 | int digest_len; |
| 2964 | /* This function should never be called for a resumed session because |
| 2965 | * the handshake hashes that we wish to record are for the original, |
| 2966 | * full handshake. */ |
| 2967 | if (s->hit) |
| 2968 | return -1; |
| 2969 | /* It only makes sense to call this function if Channel IDs have been |
| 2970 | * negotiated. */ |
| 2971 | if (!s->s3->tlsext_channel_id_new) |
| 2972 | return -1; |
| 2973 | |
| 2974 | digest_len = tls1_handshake_digest( |
| 2975 | s, s->session->original_handshake_hash, |
| 2976 | sizeof(s->session->original_handshake_hash)); |
| 2977 | if (digest_len < 0) |
| 2978 | return -1; |
| 2979 | |
| 2980 | s->session->original_handshake_hash_len = digest_len; |
| 2981 | |
| 2982 | return 1; |
| 2983 | } |