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 */ |
| 620 | s2n(TLSEXT_TYPE_heartbeat,ret); |
| 621 | s2n(1,ret); |
| 622 | /* Set mode: |
| 623 | * 1: peer may send requests |
| 624 | * 2: peer not allowed to send requests |
| 625 | */ |
| 626 | if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS) |
| 627 | *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS; |
| 628 | else |
| 629 | *(ret++) = SSL_TLSEXT_HB_ENABLED; |
| 630 | #endif |
| 631 | |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 632 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 633 | if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) |
| 634 | { |
| 635 | /* The client advertises an emtpy extension to indicate its |
| 636 | * support for Next Protocol Negotiation */ |
| 637 | if (limit - ret - 4 < 0) |
| 638 | return NULL; |
| 639 | s2n(TLSEXT_TYPE_next_proto_neg,ret); |
| 640 | s2n(0,ret); |
| 641 | } |
| 642 | #endif |
| 643 | |
Adam Langley | 45bcfbc | 2013-01-16 13:43:53 -0800 | [diff] [blame] | 644 | if (s->tlsext_channel_id_enabled) |
| 645 | { |
| 646 | /* The client advertises an emtpy extension to indicate its |
| 647 | * support for Channel ID. */ |
| 648 | if (limit - ret - 4 < 0) |
| 649 | return NULL; |
| 650 | s2n(TLSEXT_TYPE_channel_id,ret); |
| 651 | s2n(0,ret); |
| 652 | } |
| 653 | |
Kenny Root | ee53ab1 | 2013-06-24 12:07:13 -0700 | [diff] [blame] | 654 | if (s->alpn_client_proto_list && !s->s3->tmp.finish_md_len) |
| 655 | { |
| 656 | if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) |
| 657 | return NULL; |
| 658 | s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret); |
| 659 | s2n(2 + s->alpn_client_proto_list_len,ret); |
| 660 | s2n(s->alpn_client_proto_list_len,ret); |
| 661 | memcpy(ret, s->alpn_client_proto_list, |
| 662 | s->alpn_client_proto_list_len); |
| 663 | ret += s->alpn_client_proto_list_len; |
| 664 | } |
| 665 | |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 666 | #ifndef OPENSSL_NO_SRTP |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 667 | if(SSL_get_srtp_profiles(s)) |
| 668 | { |
| 669 | int el; |
| 670 | |
| 671 | ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0); |
| 672 | |
| 673 | if((limit - p - 4 - el) < 0) return NULL; |
| 674 | |
| 675 | s2n(TLSEXT_TYPE_use_srtp,ret); |
| 676 | s2n(el,ret); |
| 677 | |
| 678 | if(ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) |
| 679 | { |
| 680 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 681 | return NULL; |
| 682 | } |
| 683 | ret += el; |
| 684 | } |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 685 | #endif |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 686 | |
Adam Langley | 5df7c8e | 2014-01-23 09:40:22 -0800 | [diff] [blame^] | 687 | /* Add padding to workaround bugs in F5 terminators. |
| 688 | * See https://tools.ietf.org/html/draft-agl-tls-padding-02 */ |
| 689 | { |
| 690 | int hlen = ret - (unsigned char *)s->init_buf->data; |
| 691 | /* The code in s23_clnt.c to build ClientHello messages includes the |
| 692 | * 5-byte record header in the buffer, while the code in s3_clnt.c does |
| 693 | * not. */ |
| 694 | if (s->state == SSL23_ST_CW_CLNT_HELLO_A) |
| 695 | hlen -= 5; |
| 696 | if (hlen > 0xff && hlen < 0x200) |
| 697 | { |
| 698 | hlen = 0x200 - hlen; |
| 699 | if (hlen >= 4) |
| 700 | hlen -= 4; |
| 701 | else |
| 702 | hlen = 0; |
| 703 | |
| 704 | s2n(TLSEXT_TYPE_padding, ret); |
| 705 | s2n(hlen, ret); |
| 706 | memset(ret, 0, hlen); |
| 707 | ret += hlen; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 712 | if ((extdatalen = ret-p-2)== 0) |
| 713 | return p; |
| 714 | |
| 715 | s2n(extdatalen,p); |
| 716 | return ret; |
| 717 | } |
| 718 | |
| 719 | unsigned char *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit) |
| 720 | { |
| 721 | int extdatalen=0; |
| 722 | unsigned char *ret = p; |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 723 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 724 | int next_proto_neg_seen; |
| 725 | #endif |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 726 | |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 727 | /* don't add extensions for SSLv3, unless doing secure renegotiation */ |
| 728 | if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) |
| 729 | return p; |
| 730 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 731 | ret+=2; |
| 732 | if (ret>=limit) return NULL; /* this really never occurs, but ... */ |
| 733 | |
| 734 | if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL) |
| 735 | { |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 736 | if ((long)(limit - ret - 4) < 0) return NULL; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 737 | |
| 738 | s2n(TLSEXT_TYPE_server_name,ret); |
| 739 | s2n(0,ret); |
| 740 | } |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 741 | |
| 742 | if(s->s3->send_connection_binding) |
| 743 | { |
| 744 | int el; |
| 745 | |
| 746 | if(!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) |
| 747 | { |
| 748 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 749 | return NULL; |
| 750 | } |
| 751 | |
| 752 | if((limit - p - 4 - el) < 0) return NULL; |
| 753 | |
| 754 | s2n(TLSEXT_TYPE_renegotiate,ret); |
| 755 | s2n(el,ret); |
| 756 | |
| 757 | if(!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) |
| 758 | { |
| 759 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 760 | return NULL; |
| 761 | } |
| 762 | |
| 763 | ret += el; |
| 764 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 765 | |
| 766 | #ifndef OPENSSL_NO_EC |
| 767 | if (s->tlsext_ecpointformatlist != NULL && |
| 768 | s->version != DTLS1_VERSION) |
| 769 | { |
| 770 | /* Add TLS extension ECPointFormats to the ServerHello message */ |
| 771 | long lenmax; |
| 772 | |
| 773 | if ((lenmax = limit - ret - 5) < 0) return NULL; |
| 774 | if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL; |
| 775 | if (s->tlsext_ecpointformatlist_length > 255) |
| 776 | { |
| 777 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 778 | return NULL; |
| 779 | } |
| 780 | |
| 781 | s2n(TLSEXT_TYPE_ec_point_formats,ret); |
| 782 | s2n(s->tlsext_ecpointformatlist_length + 1,ret); |
| 783 | *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length; |
| 784 | memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length); |
| 785 | ret+=s->tlsext_ecpointformatlist_length; |
| 786 | |
| 787 | } |
| 788 | /* Currently the server should not respond with a SupportedCurves extension */ |
| 789 | #endif /* OPENSSL_NO_EC */ |
| 790 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 791 | if (s->tlsext_ticket_expected |
| 792 | && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) |
| 793 | { |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 794 | if ((long)(limit - ret - 4) < 0) return NULL; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 795 | s2n(TLSEXT_TYPE_session_ticket,ret); |
| 796 | s2n(0,ret); |
| 797 | } |
| 798 | |
| 799 | if (s->tlsext_status_expected) |
| 800 | { |
| 801 | if ((long)(limit - ret - 4) < 0) return NULL; |
| 802 | s2n(TLSEXT_TYPE_status_request,ret); |
| 803 | s2n(0,ret); |
| 804 | } |
| 805 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 806 | #ifdef TLSEXT_TYPE_opaque_prf_input |
| 807 | if (s->s3->server_opaque_prf_input != NULL && |
| 808 | s->version != DTLS1_VERSION) |
| 809 | { |
| 810 | size_t sol = s->s3->server_opaque_prf_input_len; |
| 811 | |
| 812 | if ((long)(limit - ret - 6 - sol) < 0) |
| 813 | return NULL; |
| 814 | if (sol > 0xFFFD) /* can't happen */ |
| 815 | return NULL; |
| 816 | |
| 817 | s2n(TLSEXT_TYPE_opaque_prf_input, ret); |
| 818 | s2n(sol + 2, ret); |
| 819 | s2n(sol, ret); |
| 820 | memcpy(ret, s->s3->server_opaque_prf_input, sol); |
| 821 | ret += sol; |
| 822 | } |
| 823 | #endif |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 824 | |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 825 | #ifndef OPENSSL_NO_SRTP |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 826 | if(s->srtp_profile) |
| 827 | { |
| 828 | int el; |
| 829 | |
| 830 | ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0); |
| 831 | |
| 832 | if((limit - p - 4 - el) < 0) return NULL; |
| 833 | |
| 834 | s2n(TLSEXT_TYPE_use_srtp,ret); |
| 835 | s2n(el,ret); |
| 836 | |
| 837 | if(ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) |
| 838 | { |
| 839 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); |
| 840 | return NULL; |
| 841 | } |
| 842 | ret+=el; |
| 843 | } |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 844 | #endif |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 845 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 846 | if (((s->s3->tmp.new_cipher->id & 0xFFFF)==0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF)==0x81) |
| 847 | && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) |
| 848 | { const unsigned char cryptopro_ext[36] = { |
| 849 | 0xfd, 0xe8, /*65000*/ |
| 850 | 0x00, 0x20, /*32 bytes length*/ |
| 851 | 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85, |
| 852 | 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06, |
| 853 | 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08, |
| 854 | 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17}; |
| 855 | if (limit-ret<36) return NULL; |
| 856 | memcpy(ret,cryptopro_ext,36); |
| 857 | ret+=36; |
| 858 | |
| 859 | } |
| 860 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 861 | #ifndef OPENSSL_NO_HEARTBEATS |
| 862 | /* Add Heartbeat extension if we've received one */ |
| 863 | if (s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) |
| 864 | { |
| 865 | s2n(TLSEXT_TYPE_heartbeat,ret); |
| 866 | s2n(1,ret); |
| 867 | /* Set mode: |
| 868 | * 1: peer may send requests |
| 869 | * 2: peer not allowed to send requests |
| 870 | */ |
| 871 | if (s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_RECV_REQUESTS) |
| 872 | *(ret++) = SSL_TLSEXT_HB_DONT_SEND_REQUESTS; |
| 873 | else |
| 874 | *(ret++) = SSL_TLSEXT_HB_ENABLED; |
| 875 | |
| 876 | } |
| 877 | #endif |
| 878 | |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 879 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 880 | next_proto_neg_seen = s->s3->next_proto_neg_seen; |
| 881 | s->s3->next_proto_neg_seen = 0; |
| 882 | if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) |
| 883 | { |
| 884 | const unsigned char *npa; |
| 885 | unsigned int npalen; |
| 886 | int r; |
| 887 | |
| 888 | r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg); |
| 889 | if (r == SSL_TLSEXT_ERR_OK) |
| 890 | { |
| 891 | if ((long)(limit - ret - 4 - npalen) < 0) return NULL; |
| 892 | s2n(TLSEXT_TYPE_next_proto_neg,ret); |
| 893 | s2n(npalen,ret); |
| 894 | memcpy(ret, npa, npalen); |
| 895 | ret += npalen; |
| 896 | s->s3->next_proto_neg_seen = 1; |
| 897 | } |
| 898 | } |
| 899 | #endif |
| 900 | |
Adam Langley | 45bcfbc | 2013-01-16 13:43:53 -0800 | [diff] [blame] | 901 | /* If the client advertised support for Channel ID, and we have it |
| 902 | * enabled, then we want to echo it back. */ |
| 903 | if (s->s3->tlsext_channel_id_valid) |
| 904 | { |
| 905 | if (limit - ret - 4 < 0) |
| 906 | return NULL; |
| 907 | s2n(TLSEXT_TYPE_channel_id,ret); |
| 908 | s2n(0,ret); |
| 909 | } |
| 910 | |
Kenny Root | ee53ab1 | 2013-06-24 12:07:13 -0700 | [diff] [blame] | 911 | if (s->s3->alpn_selected) |
| 912 | { |
| 913 | const unsigned char *selected = s->s3->alpn_selected; |
| 914 | unsigned len = s->s3->alpn_selected_len; |
| 915 | |
| 916 | if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) |
| 917 | return NULL; |
| 918 | s2n(TLSEXT_TYPE_application_layer_protocol_negotiation,ret); |
| 919 | s2n(3 + len,ret); |
| 920 | s2n(1 + len,ret); |
| 921 | *ret++ = len; |
| 922 | memcpy(ret, selected, len); |
| 923 | ret += len; |
| 924 | } |
| 925 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 926 | if ((extdatalen = ret-p-2)== 0) |
| 927 | return p; |
| 928 | |
| 929 | s2n(extdatalen,p); |
| 930 | return ret; |
| 931 | } |
| 932 | |
Kenny Root | ff41a4b | 2014-01-07 10:19:10 -0800 | [diff] [blame] | 933 | #ifndef OPENSSL_NO_EC |
| 934 | /* ssl_check_for_safari attempts to fingerprint Safari using OS X |
| 935 | * SecureTransport using the TLS extension block in |d|, of length |n|. |
| 936 | * Safari, since 10.6, sends exactly these extensions, in this order: |
| 937 | * SNI, |
| 938 | * elliptic_curves |
| 939 | * ec_point_formats |
| 940 | * |
| 941 | * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8, |
| 942 | * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them. |
| 943 | * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from |
| 944 | * 10.8..10.8.3 (which don't work). |
| 945 | */ |
| 946 | static void ssl_check_for_safari(SSL *s, const unsigned char *data, const unsigned char *d, int n) { |
| 947 | unsigned short type, size; |
| 948 | static const unsigned char kSafariExtensionsBlock[] = { |
| 949 | 0x00, 0x0a, /* elliptic_curves extension */ |
| 950 | 0x00, 0x08, /* 8 bytes */ |
| 951 | 0x00, 0x06, /* 6 bytes of curve ids */ |
| 952 | 0x00, 0x17, /* P-256 */ |
| 953 | 0x00, 0x18, /* P-384 */ |
| 954 | 0x00, 0x19, /* P-521 */ |
| 955 | |
| 956 | 0x00, 0x0b, /* ec_point_formats */ |
| 957 | 0x00, 0x02, /* 2 bytes */ |
| 958 | 0x01, /* 1 point format */ |
| 959 | 0x00, /* uncompressed */ |
| 960 | }; |
| 961 | |
| 962 | /* The following is only present in TLS 1.2 */ |
| 963 | static const unsigned char kSafariTLS12ExtensionsBlock[] = { |
| 964 | 0x00, 0x0d, /* signature_algorithms */ |
| 965 | 0x00, 0x0c, /* 12 bytes */ |
| 966 | 0x00, 0x0a, /* 10 bytes */ |
| 967 | 0x05, 0x01, /* SHA-384/RSA */ |
| 968 | 0x04, 0x01, /* SHA-256/RSA */ |
| 969 | 0x02, 0x01, /* SHA-1/RSA */ |
| 970 | 0x04, 0x03, /* SHA-256/ECDSA */ |
| 971 | 0x02, 0x03, /* SHA-1/ECDSA */ |
| 972 | }; |
| 973 | |
| 974 | if (data >= (d+n-2)) |
| 975 | return; |
| 976 | data += 2; |
| 977 | |
| 978 | if (data > (d+n-4)) |
| 979 | return; |
| 980 | n2s(data,type); |
| 981 | n2s(data,size); |
| 982 | |
| 983 | if (type != TLSEXT_TYPE_server_name) |
| 984 | return; |
| 985 | |
| 986 | if (data+size > d+n) |
| 987 | return; |
| 988 | data += size; |
| 989 | |
| 990 | if (TLS1_get_client_version(s) >= TLS1_2_VERSION) |
| 991 | { |
| 992 | const size_t len1 = sizeof(kSafariExtensionsBlock); |
| 993 | const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock); |
| 994 | |
| 995 | if (data + len1 + len2 != d+n) |
| 996 | return; |
| 997 | if (memcmp(data, kSafariExtensionsBlock, len1) != 0) |
| 998 | return; |
| 999 | if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0) |
| 1000 | return; |
| 1001 | } |
| 1002 | else |
| 1003 | { |
| 1004 | const size_t len = sizeof(kSafariExtensionsBlock); |
| 1005 | |
| 1006 | if (data + len != d+n) |
| 1007 | return; |
| 1008 | if (memcmp(data, kSafariExtensionsBlock, len) != 0) |
| 1009 | return; |
| 1010 | } |
| 1011 | |
| 1012 | s->s3->is_probably_safari = 1; |
| 1013 | } |
| 1014 | #endif /* !OPENSSL_NO_EC */ |
| 1015 | |
Kenny Root | ee53ab1 | 2013-06-24 12:07:13 -0700 | [diff] [blame] | 1016 | /* tls1_alpn_handle_client_hello is called to process the ALPN extension in a |
| 1017 | * ClientHello. |
| 1018 | * data: the contents of the extension, not including the type and length. |
| 1019 | * data_len: the number of bytes in |data| |
| 1020 | * al: a pointer to the alert value to send in the event of a non-zero |
| 1021 | * return. |
| 1022 | * |
| 1023 | * returns: 0 on success. */ |
| 1024 | static int tls1_alpn_handle_client_hello(SSL *s, const unsigned char *data, |
| 1025 | unsigned data_len, int *al) |
| 1026 | { |
| 1027 | unsigned i; |
| 1028 | unsigned proto_len; |
| 1029 | const unsigned char *selected; |
| 1030 | unsigned char selected_len; |
| 1031 | int r; |
| 1032 | |
| 1033 | if (s->ctx->alpn_select_cb == NULL) |
| 1034 | return 0; |
| 1035 | |
| 1036 | if (data_len < 2) |
| 1037 | goto parse_error; |
| 1038 | |
| 1039 | /* data should contain a uint16 length followed by a series of 8-bit, |
| 1040 | * length-prefixed strings. */ |
| 1041 | i = ((unsigned) data[0]) << 8 | |
| 1042 | ((unsigned) data[1]); |
| 1043 | data_len -= 2; |
| 1044 | data += 2; |
| 1045 | if (data_len != i) |
| 1046 | goto parse_error; |
| 1047 | |
| 1048 | if (data_len < 2) |
| 1049 | goto parse_error; |
| 1050 | |
| 1051 | for (i = 0; i < data_len;) |
| 1052 | { |
| 1053 | proto_len = data[i]; |
| 1054 | i++; |
| 1055 | |
| 1056 | if (proto_len == 0) |
| 1057 | goto parse_error; |
| 1058 | |
| 1059 | if (i + proto_len < i || i + proto_len > data_len) |
| 1060 | goto parse_error; |
| 1061 | |
| 1062 | i += proto_len; |
| 1063 | } |
| 1064 | |
| 1065 | r = s->ctx->alpn_select_cb(s, &selected, &selected_len, data, data_len, |
| 1066 | s->ctx->alpn_select_cb_arg); |
| 1067 | if (r == SSL_TLSEXT_ERR_OK) { |
| 1068 | if (s->s3->alpn_selected) |
| 1069 | OPENSSL_free(s->s3->alpn_selected); |
| 1070 | s->s3->alpn_selected = OPENSSL_malloc(selected_len); |
| 1071 | if (!s->s3->alpn_selected) |
| 1072 | { |
| 1073 | *al = SSL_AD_INTERNAL_ERROR; |
| 1074 | return -1; |
| 1075 | } |
| 1076 | memcpy(s->s3->alpn_selected, selected, selected_len); |
| 1077 | s->s3->alpn_selected_len = selected_len; |
| 1078 | } |
| 1079 | return 0; |
| 1080 | |
| 1081 | parse_error: |
| 1082 | *al = SSL_AD_DECODE_ERROR; |
| 1083 | return -1; |
| 1084 | } |
| 1085 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1086 | int ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al) |
| 1087 | { |
| 1088 | unsigned short type; |
| 1089 | unsigned short size; |
| 1090 | unsigned short len; |
| 1091 | unsigned char *data = *p; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1092 | int renegotiate_seen = 0; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1093 | int sigalg_seen = 0; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1094 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1095 | s->servername_done = 0; |
| 1096 | s->tlsext_status_type = -1; |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 1097 | |
| 1098 | /* Reset TLS 1.2 digest functions to defaults because they don't carry |
| 1099 | * over to a renegotiation. */ |
| 1100 | s->s3->digest_rsa = NULL; |
| 1101 | s->s3->digest_dsa = NULL; |
| 1102 | s->s3->digest_ecdsa = NULL; |
| 1103 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1104 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 1105 | s->s3->next_proto_neg_seen = 0; |
| 1106 | #endif |
| 1107 | |
Kenny Root | ee53ab1 | 2013-06-24 12:07:13 -0700 | [diff] [blame] | 1108 | if (s->s3->alpn_selected) |
| 1109 | { |
| 1110 | OPENSSL_free(s->s3->alpn_selected); |
| 1111 | s->s3->alpn_selected = NULL; |
| 1112 | } |
| 1113 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1114 | #ifndef OPENSSL_NO_HEARTBEATS |
| 1115 | s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED | |
| 1116 | SSL_TLSEXT_HB_DONT_SEND_REQUESTS); |
| 1117 | #endif |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1118 | |
Kenny Root | ff41a4b | 2014-01-07 10:19:10 -0800 | [diff] [blame] | 1119 | #ifndef OPENSSL_NO_EC |
| 1120 | if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG) |
| 1121 | ssl_check_for_safari(s, data, d, n); |
| 1122 | #endif /* !OPENSSL_NO_EC */ |
| 1123 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1124 | if (data >= (d+n-2)) |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1125 | goto ri_check; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1126 | n2s(data,len); |
| 1127 | |
| 1128 | if (data > (d+n-len)) |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1129 | goto ri_check; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1130 | |
| 1131 | while (data <= (d+n-4)) |
| 1132 | { |
| 1133 | n2s(data,type); |
| 1134 | n2s(data,size); |
| 1135 | |
| 1136 | if (data+size > (d+n)) |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1137 | goto ri_check; |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1138 | #if 0 |
| 1139 | fprintf(stderr,"Received extension type %d size %d\n",type,size); |
| 1140 | #endif |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1141 | if (s->tlsext_debug_cb) |
| 1142 | s->tlsext_debug_cb(s, 0, type, data, size, |
| 1143 | s->tlsext_debug_arg); |
| 1144 | /* The servername extension is treated as follows: |
| 1145 | |
| 1146 | - Only the hostname type is supported with a maximum length of 255. |
| 1147 | - The servername is rejected if too long or if it contains zeros, |
| 1148 | in which case an fatal alert is generated. |
| 1149 | - The servername field is maintained together with the session cache. |
| 1150 | - When a session is resumed, the servername call back invoked in order |
| 1151 | to allow the application to position itself to the right context. |
| 1152 | - The servername is acknowledged if it is new for a session or when |
| 1153 | it is identical to a previously used for the same session. |
| 1154 | Applications can control the behaviour. They can at any time |
| 1155 | set a 'desirable' servername for a new SSL object. This can be the |
| 1156 | case for example with HTTPS when a Host: header field is received and |
| 1157 | a renegotiation is requested. In this case, a possible servername |
| 1158 | presented in the new client hello is only acknowledged if it matches |
| 1159 | the value of the Host: field. |
| 1160 | - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
| 1161 | if they provide for changing an explicit servername context for the session, |
| 1162 | i.e. when the session has been established with a servername extension. |
| 1163 | - On session reconnect, the servername extension may be absent. |
| 1164 | |
| 1165 | */ |
| 1166 | |
| 1167 | if (type == TLSEXT_TYPE_server_name) |
| 1168 | { |
| 1169 | unsigned char *sdata; |
| 1170 | int servname_type; |
| 1171 | int dsize; |
| 1172 | |
| 1173 | if (size < 2) |
| 1174 | { |
| 1175 | *al = SSL_AD_DECODE_ERROR; |
| 1176 | return 0; |
| 1177 | } |
| 1178 | n2s(data,dsize); |
| 1179 | size -= 2; |
| 1180 | if (dsize > size ) |
| 1181 | { |
| 1182 | *al = SSL_AD_DECODE_ERROR; |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | sdata = data; |
| 1187 | while (dsize > 3) |
| 1188 | { |
| 1189 | servname_type = *(sdata++); |
| 1190 | n2s(sdata,len); |
| 1191 | dsize -= 3; |
| 1192 | |
| 1193 | if (len > dsize) |
| 1194 | { |
| 1195 | *al = SSL_AD_DECODE_ERROR; |
| 1196 | return 0; |
| 1197 | } |
| 1198 | if (s->servername_done == 0) |
| 1199 | switch (servname_type) |
| 1200 | { |
| 1201 | case TLSEXT_NAMETYPE_host_name: |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1202 | if (!s->hit) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1203 | { |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1204 | if(s->session->tlsext_hostname) |
| 1205 | { |
| 1206 | *al = SSL_AD_DECODE_ERROR; |
| 1207 | return 0; |
| 1208 | } |
| 1209 | if (len > TLSEXT_MAXLEN_host_name) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1210 | { |
| 1211 | *al = TLS1_AD_UNRECOGNIZED_NAME; |
| 1212 | return 0; |
| 1213 | } |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1214 | if ((s->session->tlsext_hostname = OPENSSL_malloc(len+1)) == NULL) |
| 1215 | { |
| 1216 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1217 | return 0; |
| 1218 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1219 | memcpy(s->session->tlsext_hostname, sdata, len); |
| 1220 | s->session->tlsext_hostname[len]='\0'; |
| 1221 | if (strlen(s->session->tlsext_hostname) != len) { |
| 1222 | OPENSSL_free(s->session->tlsext_hostname); |
| 1223 | s->session->tlsext_hostname = NULL; |
| 1224 | *al = TLS1_AD_UNRECOGNIZED_NAME; |
| 1225 | return 0; |
| 1226 | } |
| 1227 | s->servername_done = 1; |
| 1228 | |
| 1229 | } |
| 1230 | else |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1231 | s->servername_done = s->session->tlsext_hostname |
| 1232 | && strlen(s->session->tlsext_hostname) == len |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1233 | && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0; |
| 1234 | |
| 1235 | break; |
| 1236 | |
| 1237 | default: |
| 1238 | break; |
| 1239 | } |
| 1240 | |
| 1241 | dsize -= len; |
| 1242 | } |
| 1243 | if (dsize != 0) |
| 1244 | { |
| 1245 | *al = SSL_AD_DECODE_ERROR; |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1250 | #ifndef OPENSSL_NO_SRP |
| 1251 | else if (type == TLSEXT_TYPE_srp) |
| 1252 | { |
| 1253 | if (size <= 0 || ((len = data[0])) != (size -1)) |
| 1254 | { |
| 1255 | *al = SSL_AD_DECODE_ERROR; |
| 1256 | return 0; |
| 1257 | } |
| 1258 | if (s->srp_ctx.login != NULL) |
| 1259 | { |
| 1260 | *al = SSL_AD_DECODE_ERROR; |
| 1261 | return 0; |
| 1262 | } |
| 1263 | if ((s->srp_ctx.login = OPENSSL_malloc(len+1)) == NULL) |
| 1264 | return -1; |
| 1265 | memcpy(s->srp_ctx.login, &data[1], len); |
| 1266 | s->srp_ctx.login[len]='\0'; |
| 1267 | |
| 1268 | if (strlen(s->srp_ctx.login) != len) |
| 1269 | { |
| 1270 | *al = SSL_AD_DECODE_ERROR; |
| 1271 | return 0; |
| 1272 | } |
| 1273 | } |
| 1274 | #endif |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1275 | |
| 1276 | #ifndef OPENSSL_NO_EC |
| 1277 | else if (type == TLSEXT_TYPE_ec_point_formats && |
| 1278 | s->version != DTLS1_VERSION) |
| 1279 | { |
| 1280 | unsigned char *sdata = data; |
| 1281 | int ecpointformatlist_length = *(sdata++); |
| 1282 | |
| 1283 | if (ecpointformatlist_length != size - 1) |
| 1284 | { |
| 1285 | *al = TLS1_AD_DECODE_ERROR; |
| 1286 | return 0; |
| 1287 | } |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1288 | if (!s->hit) |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1289 | { |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1290 | if(s->session->tlsext_ecpointformatlist) |
| 1291 | { |
Brian Carlstrom | 976a034 | 2010-12-03 16:11:54 -0800 | [diff] [blame] | 1292 | OPENSSL_free(s->session->tlsext_ecpointformatlist); |
| 1293 | s->session->tlsext_ecpointformatlist = NULL; |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1294 | } |
| 1295 | s->session->tlsext_ecpointformatlist_length = 0; |
| 1296 | if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL) |
| 1297 | { |
| 1298 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1299 | return 0; |
| 1300 | } |
| 1301 | s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length; |
| 1302 | memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length); |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1303 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1304 | #if 0 |
| 1305 | fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length); |
| 1306 | sdata = s->session->tlsext_ecpointformatlist; |
| 1307 | for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) |
| 1308 | fprintf(stderr,"%i ",*(sdata++)); |
| 1309 | fprintf(stderr,"\n"); |
| 1310 | #endif |
| 1311 | } |
| 1312 | else if (type == TLSEXT_TYPE_elliptic_curves && |
| 1313 | s->version != DTLS1_VERSION) |
| 1314 | { |
| 1315 | unsigned char *sdata = data; |
| 1316 | int ellipticcurvelist_length = (*(sdata++) << 8); |
| 1317 | ellipticcurvelist_length += (*(sdata++)); |
| 1318 | |
Brian Carlstrom | 04ef91b | 2013-02-05 09:20:38 -0800 | [diff] [blame] | 1319 | if (ellipticcurvelist_length != size - 2 || |
| 1320 | ellipticcurvelist_length < 1) |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1321 | { |
| 1322 | *al = TLS1_AD_DECODE_ERROR; |
| 1323 | return 0; |
| 1324 | } |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1325 | if (!s->hit) |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1326 | { |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1327 | if(s->session->tlsext_ellipticcurvelist) |
| 1328 | { |
| 1329 | *al = TLS1_AD_DECODE_ERROR; |
| 1330 | return 0; |
| 1331 | } |
| 1332 | s->session->tlsext_ellipticcurvelist_length = 0; |
| 1333 | if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL) |
| 1334 | { |
| 1335 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1336 | return 0; |
| 1337 | } |
| 1338 | s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length; |
| 1339 | memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length); |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1340 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1341 | #if 0 |
| 1342 | fprintf(stderr,"ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length); |
| 1343 | sdata = s->session->tlsext_ellipticcurvelist; |
| 1344 | for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++) |
| 1345 | fprintf(stderr,"%i ",*(sdata++)); |
| 1346 | fprintf(stderr,"\n"); |
| 1347 | #endif |
| 1348 | } |
| 1349 | #endif /* OPENSSL_NO_EC */ |
| 1350 | #ifdef TLSEXT_TYPE_opaque_prf_input |
| 1351 | else if (type == TLSEXT_TYPE_opaque_prf_input && |
| 1352 | s->version != DTLS1_VERSION) |
| 1353 | { |
| 1354 | unsigned char *sdata = data; |
| 1355 | |
| 1356 | if (size < 2) |
| 1357 | { |
| 1358 | *al = SSL_AD_DECODE_ERROR; |
| 1359 | return 0; |
| 1360 | } |
| 1361 | n2s(sdata, s->s3->client_opaque_prf_input_len); |
| 1362 | if (s->s3->client_opaque_prf_input_len != size - 2) |
| 1363 | { |
| 1364 | *al = SSL_AD_DECODE_ERROR; |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
| 1368 | if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */ |
| 1369 | OPENSSL_free(s->s3->client_opaque_prf_input); |
| 1370 | if (s->s3->client_opaque_prf_input_len == 0) |
| 1371 | s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */ |
| 1372 | else |
| 1373 | s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len); |
| 1374 | if (s->s3->client_opaque_prf_input == NULL) |
| 1375 | { |
| 1376 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1377 | return 0; |
| 1378 | } |
| 1379 | } |
| 1380 | #endif |
| 1381 | else if (type == TLSEXT_TYPE_session_ticket) |
| 1382 | { |
| 1383 | if (s->tls_session_ticket_ext_cb && |
| 1384 | !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg)) |
| 1385 | { |
| 1386 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1387 | return 0; |
| 1388 | } |
| 1389 | } |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1390 | else if (type == TLSEXT_TYPE_renegotiate) |
| 1391 | { |
| 1392 | if(!ssl_parse_clienthello_renegotiate_ext(s, data, size, al)) |
| 1393 | return 0; |
| 1394 | renegotiate_seen = 1; |
| 1395 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1396 | else if (type == TLSEXT_TYPE_signature_algorithms) |
| 1397 | { |
| 1398 | int dsize; |
| 1399 | if (sigalg_seen || size < 2) |
| 1400 | { |
| 1401 | *al = SSL_AD_DECODE_ERROR; |
| 1402 | return 0; |
| 1403 | } |
| 1404 | sigalg_seen = 1; |
| 1405 | n2s(data,dsize); |
| 1406 | size -= 2; |
| 1407 | if (dsize != size || dsize & 1) |
| 1408 | { |
| 1409 | *al = SSL_AD_DECODE_ERROR; |
| 1410 | return 0; |
| 1411 | } |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 1412 | tls1_process_sigalgs(s, data, dsize); |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1413 | } |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1414 | else if (type == TLSEXT_TYPE_status_request && |
| 1415 | s->version != DTLS1_VERSION && s->ctx->tlsext_status_cb) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1416 | { |
| 1417 | |
| 1418 | if (size < 5) |
| 1419 | { |
| 1420 | *al = SSL_AD_DECODE_ERROR; |
| 1421 | return 0; |
| 1422 | } |
| 1423 | |
| 1424 | s->tlsext_status_type = *data++; |
| 1425 | size--; |
| 1426 | if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) |
| 1427 | { |
| 1428 | const unsigned char *sdata; |
| 1429 | int dsize; |
| 1430 | /* Read in responder_id_list */ |
| 1431 | n2s(data,dsize); |
| 1432 | size -= 2; |
| 1433 | if (dsize > size ) |
| 1434 | { |
| 1435 | *al = SSL_AD_DECODE_ERROR; |
| 1436 | return 0; |
| 1437 | } |
| 1438 | while (dsize > 0) |
| 1439 | { |
| 1440 | OCSP_RESPID *id; |
| 1441 | int idsize; |
| 1442 | if (dsize < 4) |
| 1443 | { |
| 1444 | *al = SSL_AD_DECODE_ERROR; |
| 1445 | return 0; |
| 1446 | } |
| 1447 | n2s(data, idsize); |
| 1448 | dsize -= 2 + idsize; |
Brian Carlstrom | 81c4de7 | 2011-02-08 14:54:39 -0800 | [diff] [blame] | 1449 | size -= 2 + idsize; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1450 | if (dsize < 0) |
| 1451 | { |
| 1452 | *al = SSL_AD_DECODE_ERROR; |
| 1453 | return 0; |
| 1454 | } |
| 1455 | sdata = data; |
| 1456 | data += idsize; |
| 1457 | id = d2i_OCSP_RESPID(NULL, |
| 1458 | &sdata, idsize); |
| 1459 | if (!id) |
| 1460 | { |
| 1461 | *al = SSL_AD_DECODE_ERROR; |
| 1462 | return 0; |
| 1463 | } |
| 1464 | if (data != sdata) |
| 1465 | { |
| 1466 | OCSP_RESPID_free(id); |
| 1467 | *al = SSL_AD_DECODE_ERROR; |
| 1468 | return 0; |
| 1469 | } |
| 1470 | if (!s->tlsext_ocsp_ids |
| 1471 | && !(s->tlsext_ocsp_ids = |
| 1472 | sk_OCSP_RESPID_new_null())) |
| 1473 | { |
| 1474 | OCSP_RESPID_free(id); |
| 1475 | *al = SSL_AD_INTERNAL_ERROR; |
| 1476 | return 0; |
| 1477 | } |
| 1478 | if (!sk_OCSP_RESPID_push( |
| 1479 | s->tlsext_ocsp_ids, id)) |
| 1480 | { |
| 1481 | OCSP_RESPID_free(id); |
| 1482 | *al = SSL_AD_INTERNAL_ERROR; |
| 1483 | return 0; |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | /* Read in request_extensions */ |
Brian Carlstrom | 81c4de7 | 2011-02-08 14:54:39 -0800 | [diff] [blame] | 1488 | if (size < 2) |
| 1489 | { |
| 1490 | *al = SSL_AD_DECODE_ERROR; |
| 1491 | return 0; |
| 1492 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1493 | n2s(data,dsize); |
| 1494 | size -= 2; |
Brian Carlstrom | 81c4de7 | 2011-02-08 14:54:39 -0800 | [diff] [blame] | 1495 | if (dsize != size) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1496 | { |
| 1497 | *al = SSL_AD_DECODE_ERROR; |
| 1498 | return 0; |
| 1499 | } |
| 1500 | sdata = data; |
| 1501 | if (dsize > 0) |
| 1502 | { |
Brian Carlstrom | 7b476c4 | 2012-01-04 13:22:32 -0800 | [diff] [blame] | 1503 | if (s->tlsext_ocsp_exts) |
| 1504 | { |
| 1505 | sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, |
| 1506 | X509_EXTENSION_free); |
| 1507 | } |
| 1508 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1509 | s->tlsext_ocsp_exts = |
| 1510 | d2i_X509_EXTENSIONS(NULL, |
| 1511 | &sdata, dsize); |
| 1512 | if (!s->tlsext_ocsp_exts |
| 1513 | || (data + dsize != sdata)) |
| 1514 | { |
| 1515 | *al = SSL_AD_DECODE_ERROR; |
| 1516 | return 0; |
| 1517 | } |
| 1518 | } |
| 1519 | } |
| 1520 | /* We don't know what to do with any other type |
| 1521 | * so ignore it. |
| 1522 | */ |
| 1523 | else |
| 1524 | s->tlsext_status_type = -1; |
| 1525 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1526 | #ifndef OPENSSL_NO_HEARTBEATS |
| 1527 | else if (type == TLSEXT_TYPE_heartbeat) |
| 1528 | { |
| 1529 | switch(data[0]) |
| 1530 | { |
| 1531 | case 0x01: /* Client allows us to send HB requests */ |
| 1532 | s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED; |
| 1533 | break; |
| 1534 | case 0x02: /* Client doesn't accept HB requests */ |
| 1535 | s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED; |
| 1536 | s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS; |
| 1537 | break; |
| 1538 | default: *al = SSL_AD_ILLEGAL_PARAMETER; |
| 1539 | return 0; |
| 1540 | } |
| 1541 | } |
| 1542 | #endif |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 1543 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 1544 | else if (type == TLSEXT_TYPE_next_proto_neg && |
Kenny Root | ee53ab1 | 2013-06-24 12:07:13 -0700 | [diff] [blame] | 1545 | s->s3->tmp.finish_md_len == 0 && |
| 1546 | s->s3->alpn_selected == NULL) |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 1547 | { |
| 1548 | /* We shouldn't accept this extension on a |
| 1549 | * renegotiation. |
| 1550 | * |
| 1551 | * s->new_session will be set on renegotiation, but we |
| 1552 | * probably shouldn't rely that it couldn't be set on |
| 1553 | * the initial renegotation too in certain cases (when |
| 1554 | * there's some other reason to disallow resuming an |
| 1555 | * earlier session -- the current code won't be doing |
| 1556 | * anything like that, but this might change). |
| 1557 | |
| 1558 | * A valid sign that there's been a previous handshake |
| 1559 | * in this connection is if s->s3->tmp.finish_md_len > |
| 1560 | * 0. (We are talking about a check that will happen |
| 1561 | * in the Hello protocol round, well before a new |
| 1562 | * Finished message could have been computed.) */ |
| 1563 | s->s3->next_proto_neg_seen = 1; |
| 1564 | } |
| 1565 | #endif |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1566 | |
Adam Langley | 45bcfbc | 2013-01-16 13:43:53 -0800 | [diff] [blame] | 1567 | else if (type == TLSEXT_TYPE_channel_id && s->tlsext_channel_id_enabled) |
| 1568 | s->s3->tlsext_channel_id_valid = 1; |
| 1569 | |
Kenny Root | ee53ab1 | 2013-06-24 12:07:13 -0700 | [diff] [blame] | 1570 | else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation && |
| 1571 | s->ctx->alpn_select_cb && |
| 1572 | s->s3->tmp.finish_md_len == 0) |
| 1573 | { |
| 1574 | if (tls1_alpn_handle_client_hello(s, data, size, al) != 0) |
| 1575 | return 0; |
| 1576 | /* ALPN takes precedence over NPN. */ |
| 1577 | s->s3->next_proto_neg_seen = 0; |
| 1578 | } |
| 1579 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1580 | /* session ticket processed earlier */ |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 1581 | #ifndef OPENSSL_NO_SRTP |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1582 | else if (type == TLSEXT_TYPE_use_srtp) |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 1583 | { |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1584 | if(ssl_parse_clienthello_use_srtp_ext(s, data, size, |
| 1585 | al)) |
| 1586 | return 0; |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 1587 | } |
| 1588 | #endif |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1589 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1590 | data+=size; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1591 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1592 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1593 | *p = data; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1594 | |
| 1595 | ri_check: |
| 1596 | |
| 1597 | /* Need RI if renegotiating */ |
| 1598 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1599 | if (!renegotiate_seen && s->renegotiate && |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1600 | !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) |
| 1601 | { |
| 1602 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 1603 | SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, |
| 1604 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); |
| 1605 | return 0; |
| 1606 | } |
| 1607 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1608 | return 1; |
| 1609 | } |
| 1610 | |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 1611 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 1612 | /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No |
| 1613 | * elements of zero length are allowed and the set of elements must exactly fill |
| 1614 | * the length of the block. */ |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1615 | static char ssl_next_proto_validate(unsigned char *d, unsigned len) |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 1616 | { |
| 1617 | unsigned int off = 0; |
| 1618 | |
| 1619 | while (off < len) |
| 1620 | { |
| 1621 | if (d[off] == 0) |
| 1622 | return 0; |
| 1623 | off += d[off]; |
| 1624 | off++; |
| 1625 | } |
| 1626 | |
| 1627 | return off == len; |
| 1628 | } |
| 1629 | #endif |
| 1630 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1631 | int ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al) |
| 1632 | { |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1633 | unsigned short length; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1634 | unsigned short type; |
| 1635 | unsigned short size; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1636 | unsigned char *data = *p; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1637 | int tlsext_servername = 0; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1638 | int renegotiate_seen = 0; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1639 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1640 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 1641 | s->s3->next_proto_neg_seen = 0; |
| 1642 | #endif |
| 1643 | |
Kenny Root | ee53ab1 | 2013-06-24 12:07:13 -0700 | [diff] [blame] | 1644 | if (s->s3->alpn_selected) |
| 1645 | { |
| 1646 | OPENSSL_free(s->s3->alpn_selected); |
| 1647 | s->s3->alpn_selected = NULL; |
| 1648 | } |
| 1649 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1650 | #ifndef OPENSSL_NO_HEARTBEATS |
| 1651 | s->tlsext_heartbeat &= ~(SSL_TLSEXT_HB_ENABLED | |
| 1652 | SSL_TLSEXT_HB_DONT_SEND_REQUESTS); |
| 1653 | #endif |
| 1654 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1655 | if (data >= (d+n-2)) |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1656 | goto ri_check; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1657 | |
Brian Carlstrom | 43c12e3 | 2010-11-16 11:19:35 -0800 | [diff] [blame] | 1658 | n2s(data,length); |
| 1659 | if (data+length != d+n) |
| 1660 | { |
| 1661 | *al = SSL_AD_DECODE_ERROR; |
| 1662 | return 0; |
| 1663 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1664 | |
| 1665 | while(data <= (d+n-4)) |
| 1666 | { |
| 1667 | n2s(data,type); |
| 1668 | n2s(data,size); |
| 1669 | |
| 1670 | if (data+size > (d+n)) |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1671 | goto ri_check; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1672 | |
| 1673 | if (s->tlsext_debug_cb) |
| 1674 | s->tlsext_debug_cb(s, 1, type, data, size, |
| 1675 | s->tlsext_debug_arg); |
| 1676 | |
| 1677 | if (type == TLSEXT_TYPE_server_name) |
| 1678 | { |
| 1679 | if (s->tlsext_hostname == NULL || size > 0) |
| 1680 | { |
| 1681 | *al = TLS1_AD_UNRECOGNIZED_NAME; |
| 1682 | return 0; |
| 1683 | } |
| 1684 | tlsext_servername = 1; |
| 1685 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1686 | |
| 1687 | #ifndef OPENSSL_NO_EC |
| 1688 | else if (type == TLSEXT_TYPE_ec_point_formats && |
| 1689 | s->version != DTLS1_VERSION) |
| 1690 | { |
| 1691 | unsigned char *sdata = data; |
| 1692 | int ecpointformatlist_length = *(sdata++); |
| 1693 | |
Brian Carlstrom | 04ef91b | 2013-02-05 09:20:38 -0800 | [diff] [blame] | 1694 | if (ecpointformatlist_length != size - 1 || |
| 1695 | ecpointformatlist_length < 1) |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1696 | { |
| 1697 | *al = TLS1_AD_DECODE_ERROR; |
| 1698 | return 0; |
| 1699 | } |
| 1700 | s->session->tlsext_ecpointformatlist_length = 0; |
| 1701 | if (s->session->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->session->tlsext_ecpointformatlist); |
| 1702 | if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL) |
| 1703 | { |
| 1704 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1705 | return 0; |
| 1706 | } |
| 1707 | s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length; |
| 1708 | memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length); |
| 1709 | #if 0 |
| 1710 | fprintf(stderr,"ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist "); |
| 1711 | sdata = s->session->tlsext_ecpointformatlist; |
| 1712 | for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) |
| 1713 | fprintf(stderr,"%i ",*(sdata++)); |
| 1714 | fprintf(stderr,"\n"); |
| 1715 | #endif |
| 1716 | } |
| 1717 | #endif /* OPENSSL_NO_EC */ |
| 1718 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1719 | else if (type == TLSEXT_TYPE_session_ticket) |
| 1720 | { |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1721 | if (s->tls_session_ticket_ext_cb && |
| 1722 | !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg)) |
| 1723 | { |
| 1724 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1725 | return 0; |
| 1726 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1727 | if ((SSL_get_options(s) & SSL_OP_NO_TICKET) |
| 1728 | || (size > 0)) |
| 1729 | { |
| 1730 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; |
| 1731 | return 0; |
| 1732 | } |
| 1733 | s->tlsext_ticket_expected = 1; |
| 1734 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1735 | #ifdef TLSEXT_TYPE_opaque_prf_input |
| 1736 | else if (type == TLSEXT_TYPE_opaque_prf_input && |
| 1737 | s->version != DTLS1_VERSION) |
| 1738 | { |
| 1739 | unsigned char *sdata = data; |
| 1740 | |
| 1741 | if (size < 2) |
| 1742 | { |
| 1743 | *al = SSL_AD_DECODE_ERROR; |
| 1744 | return 0; |
| 1745 | } |
| 1746 | n2s(sdata, s->s3->server_opaque_prf_input_len); |
| 1747 | if (s->s3->server_opaque_prf_input_len != size - 2) |
| 1748 | { |
| 1749 | *al = SSL_AD_DECODE_ERROR; |
| 1750 | return 0; |
| 1751 | } |
| 1752 | |
| 1753 | if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */ |
| 1754 | OPENSSL_free(s->s3->server_opaque_prf_input); |
| 1755 | if (s->s3->server_opaque_prf_input_len == 0) |
| 1756 | s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */ |
| 1757 | else |
| 1758 | s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len); |
| 1759 | |
| 1760 | if (s->s3->server_opaque_prf_input == NULL) |
| 1761 | { |
| 1762 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1763 | return 0; |
| 1764 | } |
| 1765 | } |
| 1766 | #endif |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1767 | else if (type == TLSEXT_TYPE_status_request && |
| 1768 | s->version != DTLS1_VERSION) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1769 | { |
| 1770 | /* MUST be empty and only sent if we've requested |
| 1771 | * a status request message. |
| 1772 | */ |
| 1773 | if ((s->tlsext_status_type == -1) || (size > 0)) |
| 1774 | { |
| 1775 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; |
| 1776 | return 0; |
| 1777 | } |
| 1778 | /* Set flag to expect CertificateStatus message */ |
| 1779 | s->tlsext_status_expected = 1; |
| 1780 | } |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 1781 | #ifndef OPENSSL_NO_NEXTPROTONEG |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1782 | else if (type == TLSEXT_TYPE_next_proto_neg && |
| 1783 | s->s3->tmp.finish_md_len == 0) |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 1784 | { |
| 1785 | unsigned char *selected; |
| 1786 | unsigned char selected_len; |
| 1787 | |
| 1788 | /* We must have requested it. */ |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 1789 | if (s->ctx->next_proto_select_cb == NULL) |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 1790 | { |
| 1791 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; |
| 1792 | return 0; |
| 1793 | } |
| 1794 | /* The data must be valid */ |
| 1795 | if (!ssl_next_proto_validate(data, size)) |
| 1796 | { |
| 1797 | *al = TLS1_AD_DECODE_ERROR; |
| 1798 | return 0; |
| 1799 | } |
| 1800 | if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) |
| 1801 | { |
| 1802 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1803 | return 0; |
| 1804 | } |
| 1805 | s->next_proto_negotiated = OPENSSL_malloc(selected_len); |
| 1806 | if (!s->next_proto_negotiated) |
| 1807 | { |
| 1808 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1809 | return 0; |
| 1810 | } |
| 1811 | memcpy(s->next_proto_negotiated, selected, selected_len); |
| 1812 | s->next_proto_negotiated_len = selected_len; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1813 | s->s3->next_proto_neg_seen = 1; |
Kristian Monsen | bf9ac26 | 2010-11-29 16:05:31 +0000 | [diff] [blame] | 1814 | } |
| 1815 | #endif |
Adam Langley | 45bcfbc | 2013-01-16 13:43:53 -0800 | [diff] [blame] | 1816 | else if (type == TLSEXT_TYPE_channel_id) |
| 1817 | s->s3->tlsext_channel_id_valid = 1; |
| 1818 | |
Kenny Root | ee53ab1 | 2013-06-24 12:07:13 -0700 | [diff] [blame] | 1819 | else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation) |
| 1820 | { |
| 1821 | unsigned len; |
| 1822 | |
| 1823 | /* We must have requested it. */ |
| 1824 | if (s->alpn_client_proto_list == NULL) |
| 1825 | { |
| 1826 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; |
| 1827 | return 0; |
| 1828 | } |
| 1829 | if (size < 4) |
| 1830 | { |
| 1831 | *al = TLS1_AD_DECODE_ERROR; |
| 1832 | return 0; |
| 1833 | } |
| 1834 | /* The extension data consists of: |
| 1835 | * uint16 list_length |
| 1836 | * uint8 proto_length; |
| 1837 | * uint8 proto[proto_length]; */ |
| 1838 | len = data[0]; |
| 1839 | len <<= 8; |
| 1840 | len |= data[1]; |
| 1841 | if (len != (unsigned) size - 2) |
| 1842 | { |
| 1843 | *al = TLS1_AD_DECODE_ERROR; |
| 1844 | return 0; |
| 1845 | } |
| 1846 | len = data[2]; |
| 1847 | if (len != (unsigned) size - 3) |
| 1848 | { |
| 1849 | *al = TLS1_AD_DECODE_ERROR; |
| 1850 | return 0; |
| 1851 | } |
| 1852 | if (s->s3->alpn_selected) |
| 1853 | OPENSSL_free(s->s3->alpn_selected); |
| 1854 | s->s3->alpn_selected = OPENSSL_malloc(len); |
| 1855 | if (!s->s3->alpn_selected) |
| 1856 | { |
| 1857 | *al = TLS1_AD_INTERNAL_ERROR; |
| 1858 | return 0; |
| 1859 | } |
| 1860 | memcpy(s->s3->alpn_selected, data + 3, len); |
| 1861 | s->s3->alpn_selected_len = len; |
| 1862 | } |
| 1863 | |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1864 | else if (type == TLSEXT_TYPE_renegotiate) |
| 1865 | { |
| 1866 | if(!ssl_parse_serverhello_renegotiate_ext(s, data, size, al)) |
| 1867 | return 0; |
| 1868 | renegotiate_seen = 1; |
| 1869 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1870 | #ifndef OPENSSL_NO_HEARTBEATS |
| 1871 | else if (type == TLSEXT_TYPE_heartbeat) |
| 1872 | { |
| 1873 | switch(data[0]) |
| 1874 | { |
| 1875 | case 0x01: /* Server allows us to send HB requests */ |
| 1876 | s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED; |
| 1877 | break; |
| 1878 | case 0x02: /* Server doesn't accept HB requests */ |
| 1879 | s->tlsext_heartbeat |= SSL_TLSEXT_HB_ENABLED; |
| 1880 | s->tlsext_heartbeat |= SSL_TLSEXT_HB_DONT_SEND_REQUESTS; |
| 1881 | break; |
| 1882 | default: *al = SSL_AD_ILLEGAL_PARAMETER; |
| 1883 | return 0; |
| 1884 | } |
| 1885 | } |
| 1886 | #endif |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 1887 | #ifndef OPENSSL_NO_SRTP |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1888 | else if (type == TLSEXT_TYPE_use_srtp) |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 1889 | { |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1890 | if(ssl_parse_serverhello_use_srtp_ext(s, data, size, |
| 1891 | al)) |
| 1892 | return 0; |
Brian Carlstrom | eefface | 2013-02-11 09:54:43 -0800 | [diff] [blame] | 1893 | } |
| 1894 | #endif |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1895 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1896 | data+=size; |
| 1897 | } |
| 1898 | |
| 1899 | if (data != d+n) |
| 1900 | { |
| 1901 | *al = SSL_AD_DECODE_ERROR; |
| 1902 | return 0; |
| 1903 | } |
| 1904 | |
| 1905 | if (!s->hit && tlsext_servername == 1) |
| 1906 | { |
| 1907 | if (s->tlsext_hostname) |
| 1908 | { |
| 1909 | if (s->session->tlsext_hostname == NULL) |
| 1910 | { |
| 1911 | s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname); |
| 1912 | if (!s->session->tlsext_hostname) |
| 1913 | { |
| 1914 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 1915 | return 0; |
| 1916 | } |
| 1917 | } |
| 1918 | else |
| 1919 | { |
| 1920 | *al = SSL_AD_DECODE_ERROR; |
| 1921 | return 0; |
| 1922 | } |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | *p = data; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 1927 | |
| 1928 | ri_check: |
| 1929 | |
| 1930 | /* Determine if we need to see RI. Strictly speaking if we want to |
| 1931 | * avoid an attack we should *always* see RI even on initial server |
| 1932 | * hello because the client doesn't see any renegotiation during an |
| 1933 | * attack. However this would mean we could not connect to any server |
| 1934 | * which doesn't support RI so for the immediate future tolerate RI |
| 1935 | * absence on initial connect only. |
| 1936 | */ |
| 1937 | if (!renegotiate_seen |
| 1938 | && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) |
| 1939 | && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) |
| 1940 | { |
| 1941 | *al = SSL_AD_HANDSHAKE_FAILURE; |
| 1942 | SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, |
| 1943 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); |
| 1944 | return 0; |
| 1945 | } |
| 1946 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 1947 | return 1; |
| 1948 | } |
| 1949 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1950 | |
| 1951 | int ssl_prepare_clienthello_tlsext(SSL *s) |
| 1952 | { |
| 1953 | #ifndef OPENSSL_NO_EC |
| 1954 | /* If we are client and using an elliptic curve cryptography cipher suite, send the point formats |
| 1955 | * and elliptic curves we support. |
| 1956 | */ |
| 1957 | int using_ecc = 0; |
| 1958 | int i; |
| 1959 | unsigned char *j; |
| 1960 | unsigned long alg_k, alg_a; |
| 1961 | STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s); |
| 1962 | |
| 1963 | for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) |
| 1964 | { |
| 1965 | SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i); |
| 1966 | |
| 1967 | alg_k = c->algorithm_mkey; |
| 1968 | alg_a = c->algorithm_auth; |
| 1969 | if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe) || (alg_a & SSL_aECDSA))) |
| 1970 | { |
| 1971 | using_ecc = 1; |
| 1972 | break; |
| 1973 | } |
| 1974 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1975 | using_ecc = using_ecc && (s->version >= TLS1_VERSION); |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1976 | if (using_ecc) |
| 1977 | { |
| 1978 | if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist); |
| 1979 | if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL) |
| 1980 | { |
| 1981 | SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE); |
| 1982 | return -1; |
| 1983 | } |
| 1984 | s->tlsext_ecpointformatlist_length = 3; |
| 1985 | s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed; |
| 1986 | s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
| 1987 | s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; |
| 1988 | |
| 1989 | /* we support all named elliptic curves in draft-ietf-tls-ecc-12 */ |
| 1990 | if (s->tlsext_ellipticcurvelist != NULL) OPENSSL_free(s->tlsext_ellipticcurvelist); |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1991 | s->tlsext_ellipticcurvelist_length = sizeof(pref_list)/sizeof(pref_list[0]) * 2; |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 1992 | if ((s->tlsext_ellipticcurvelist = OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == NULL) |
| 1993 | { |
| 1994 | s->tlsext_ellipticcurvelist_length = 0; |
| 1995 | SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE); |
| 1996 | return -1; |
| 1997 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 1998 | for (i = 0, j = s->tlsext_ellipticcurvelist; (unsigned int)i < |
| 1999 | sizeof(pref_list)/sizeof(pref_list[0]); i++) |
| 2000 | { |
| 2001 | int id = tls1_ec_nid2curve_id(pref_list[i]); |
| 2002 | s2n(id,j); |
| 2003 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2004 | } |
| 2005 | #endif /* OPENSSL_NO_EC */ |
| 2006 | |
| 2007 | #ifdef TLSEXT_TYPE_opaque_prf_input |
| 2008 | { |
| 2009 | int r = 1; |
| 2010 | |
| 2011 | if (s->ctx->tlsext_opaque_prf_input_callback != 0) |
| 2012 | { |
| 2013 | r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg); |
| 2014 | if (!r) |
| 2015 | return -1; |
| 2016 | } |
| 2017 | |
| 2018 | if (s->tlsext_opaque_prf_input != NULL) |
| 2019 | { |
| 2020 | if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */ |
| 2021 | OPENSSL_free(s->s3->client_opaque_prf_input); |
| 2022 | |
| 2023 | if (s->tlsext_opaque_prf_input_len == 0) |
| 2024 | s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */ |
| 2025 | else |
| 2026 | s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len); |
| 2027 | if (s->s3->client_opaque_prf_input == NULL) |
| 2028 | { |
| 2029 | SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT,ERR_R_MALLOC_FAILURE); |
| 2030 | return -1; |
| 2031 | } |
| 2032 | s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len; |
| 2033 | } |
| 2034 | |
| 2035 | if (r == 2) |
| 2036 | /* at callback's request, insist on receiving an appropriate server opaque PRF input */ |
| 2037 | s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len; |
| 2038 | } |
| 2039 | #endif |
| 2040 | |
| 2041 | return 1; |
| 2042 | } |
| 2043 | |
| 2044 | int ssl_prepare_serverhello_tlsext(SSL *s) |
| 2045 | { |
| 2046 | #ifndef OPENSSL_NO_EC |
| 2047 | /* If we are server and using an ECC cipher suite, send the point formats we support |
| 2048 | * if the client sent us an ECPointsFormat extension. Note that the server is not |
| 2049 | * supposed to send an EllipticCurves extension. |
| 2050 | */ |
| 2051 | |
| 2052 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 2053 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 2054 | int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA); |
| 2055 | using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL); |
| 2056 | |
| 2057 | if (using_ecc) |
| 2058 | { |
| 2059 | if (s->tlsext_ecpointformatlist != NULL) OPENSSL_free(s->tlsext_ecpointformatlist); |
| 2060 | if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL) |
| 2061 | { |
| 2062 | SSLerr(SSL_F_SSL_PREPARE_SERVERHELLO_TLSEXT,ERR_R_MALLOC_FAILURE); |
| 2063 | return -1; |
| 2064 | } |
| 2065 | s->tlsext_ecpointformatlist_length = 3; |
| 2066 | s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed; |
| 2067 | s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
| 2068 | s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; |
| 2069 | } |
| 2070 | #endif /* OPENSSL_NO_EC */ |
| 2071 | |
| 2072 | return 1; |
| 2073 | } |
| 2074 | |
Brian Carlstrom | 04ef91b | 2013-02-05 09:20:38 -0800 | [diff] [blame] | 2075 | int ssl_check_clienthello_tlsext_early(SSL *s) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2076 | { |
| 2077 | int ret=SSL_TLSEXT_ERR_NOACK; |
| 2078 | int al = SSL_AD_UNRECOGNIZED_NAME; |
| 2079 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2080 | #ifndef OPENSSL_NO_EC |
| 2081 | /* The handling of the ECPointFormats extension is done elsewhere, namely in |
| 2082 | * ssl3_choose_cipher in s3_lib.c. |
| 2083 | */ |
| 2084 | /* The handling of the EllipticCurves extension is done elsewhere, namely in |
| 2085 | * ssl3_choose_cipher in s3_lib.c. |
| 2086 | */ |
| 2087 | #endif |
| 2088 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2089 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) |
| 2090 | ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg); |
| 2091 | else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0) |
| 2092 | ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg); |
| 2093 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2094 | #ifdef TLSEXT_TYPE_opaque_prf_input |
| 2095 | { |
| 2096 | /* This sort of belongs into ssl_prepare_serverhello_tlsext(), |
| 2097 | * 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] | 2098 | * so this has to happen here in |
| 2099 | * ssl_check_clienthello_tlsext_early(). */ |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2100 | |
| 2101 | int r = 1; |
| 2102 | |
| 2103 | if (s->ctx->tlsext_opaque_prf_input_callback != 0) |
| 2104 | { |
| 2105 | r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg); |
| 2106 | if (!r) |
| 2107 | { |
| 2108 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2109 | al = SSL_AD_INTERNAL_ERROR; |
| 2110 | goto err; |
| 2111 | } |
| 2112 | } |
| 2113 | |
| 2114 | if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */ |
| 2115 | OPENSSL_free(s->s3->server_opaque_prf_input); |
| 2116 | s->s3->server_opaque_prf_input = NULL; |
| 2117 | |
| 2118 | if (s->tlsext_opaque_prf_input != NULL) |
| 2119 | { |
| 2120 | if (s->s3->client_opaque_prf_input != NULL && |
| 2121 | s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len) |
| 2122 | { |
| 2123 | /* can only use this extension if we have a server opaque PRF input |
| 2124 | * of the same length as the client opaque PRF input! */ |
| 2125 | |
| 2126 | if (s->tlsext_opaque_prf_input_len == 0) |
| 2127 | s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */ |
| 2128 | else |
| 2129 | s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len); |
| 2130 | if (s->s3->server_opaque_prf_input == NULL) |
| 2131 | { |
| 2132 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2133 | al = SSL_AD_INTERNAL_ERROR; |
| 2134 | goto err; |
| 2135 | } |
| 2136 | s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len; |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | if (r == 2 && s->s3->server_opaque_prf_input == NULL) |
| 2141 | { |
| 2142 | /* The callback wants to enforce use of the extension, |
| 2143 | * but we can't do that with the client opaque PRF input; |
| 2144 | * abort the handshake. |
| 2145 | */ |
| 2146 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2147 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 2148 | } |
| 2149 | } |
| 2150 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2151 | err: |
Brian Carlstrom | 04ef91b | 2013-02-05 09:20:38 -0800 | [diff] [blame] | 2152 | #endif |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2153 | switch (ret) |
| 2154 | { |
| 2155 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2156 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2157 | return -1; |
| 2158 | |
| 2159 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 2160 | ssl3_send_alert(s,SSL3_AL_WARNING,al); |
| 2161 | return 1; |
| 2162 | |
| 2163 | case SSL_TLSEXT_ERR_NOACK: |
| 2164 | s->servername_done=0; |
| 2165 | default: |
| 2166 | return 1; |
| 2167 | } |
| 2168 | } |
| 2169 | |
Brian Carlstrom | 04ef91b | 2013-02-05 09:20:38 -0800 | [diff] [blame] | 2170 | int ssl_check_clienthello_tlsext_late(SSL *s) |
| 2171 | { |
| 2172 | int ret = SSL_TLSEXT_ERR_OK; |
| 2173 | int al; |
| 2174 | |
| 2175 | /* If status request then ask callback what to do. |
| 2176 | * Note: this must be called after servername callbacks in case |
| 2177 | * the certificate has changed, and must be called after the cipher |
| 2178 | * has been chosen because this may influence which certificate is sent |
| 2179 | */ |
| 2180 | if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) |
| 2181 | { |
| 2182 | int r; |
| 2183 | CERT_PKEY *certpkey; |
| 2184 | certpkey = ssl_get_server_send_pkey(s); |
| 2185 | /* If no certificate can't return certificate status */ |
| 2186 | if (certpkey == NULL) |
| 2187 | { |
| 2188 | s->tlsext_status_expected = 0; |
| 2189 | return 1; |
| 2190 | } |
| 2191 | /* Set current certificate to one we will use so |
| 2192 | * SSL_get_certificate et al can pick it up. |
| 2193 | */ |
| 2194 | s->cert->key = certpkey; |
| 2195 | r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); |
| 2196 | switch (r) |
| 2197 | { |
| 2198 | /* We don't want to send a status request response */ |
| 2199 | case SSL_TLSEXT_ERR_NOACK: |
| 2200 | s->tlsext_status_expected = 0; |
| 2201 | break; |
| 2202 | /* status request response should be sent */ |
| 2203 | case SSL_TLSEXT_ERR_OK: |
| 2204 | if (s->tlsext_ocsp_resp) |
| 2205 | s->tlsext_status_expected = 1; |
| 2206 | else |
| 2207 | s->tlsext_status_expected = 0; |
| 2208 | break; |
| 2209 | /* something bad happened */ |
| 2210 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2211 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2212 | al = SSL_AD_INTERNAL_ERROR; |
| 2213 | goto err; |
| 2214 | } |
| 2215 | } |
| 2216 | else |
| 2217 | s->tlsext_status_expected = 0; |
| 2218 | |
| 2219 | err: |
| 2220 | switch (ret) |
| 2221 | { |
| 2222 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2223 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2224 | return -1; |
| 2225 | |
| 2226 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 2227 | ssl3_send_alert(s,SSL3_AL_WARNING,al); |
| 2228 | return 1; |
| 2229 | |
| 2230 | default: |
| 2231 | return 1; |
| 2232 | } |
| 2233 | } |
| 2234 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2235 | int ssl_check_serverhello_tlsext(SSL *s) |
| 2236 | { |
| 2237 | int ret=SSL_TLSEXT_ERR_NOACK; |
| 2238 | int al = SSL_AD_UNRECOGNIZED_NAME; |
| 2239 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2240 | #ifndef OPENSSL_NO_EC |
Brian Carlstrom | 976a034 | 2010-12-03 16:11:54 -0800 | [diff] [blame] | 2241 | /* If we are client and using an elliptic curve cryptography cipher |
| 2242 | * suite, then if server returns an EC point formats lists extension |
| 2243 | * it must contain uncompressed. |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2244 | */ |
| 2245 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 2246 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 2247 | if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) && |
Brian Carlstrom | 976a034 | 2010-12-03 16:11:54 -0800 | [diff] [blame] | 2248 | (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) && |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2249 | ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA))) |
| 2250 | { |
| 2251 | /* we are using an ECC cipher */ |
| 2252 | size_t i; |
| 2253 | unsigned char *list; |
| 2254 | int found_uncompressed = 0; |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2255 | list = s->session->tlsext_ecpointformatlist; |
| 2256 | for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) |
| 2257 | { |
| 2258 | if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) |
| 2259 | { |
| 2260 | found_uncompressed = 1; |
| 2261 | break; |
| 2262 | } |
| 2263 | } |
| 2264 | if (!found_uncompressed) |
| 2265 | { |
| 2266 | SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT,SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST); |
| 2267 | return -1; |
| 2268 | } |
| 2269 | } |
| 2270 | ret = SSL_TLSEXT_ERR_OK; |
| 2271 | #endif /* OPENSSL_NO_EC */ |
| 2272 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2273 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) |
| 2274 | ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg); |
| 2275 | else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0) |
| 2276 | ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg); |
| 2277 | |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2278 | #ifdef TLSEXT_TYPE_opaque_prf_input |
| 2279 | if (s->s3->server_opaque_prf_input_len > 0) |
| 2280 | { |
| 2281 | /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs. |
| 2282 | * So first verify that we really have a value from the server too. */ |
| 2283 | |
| 2284 | if (s->s3->server_opaque_prf_input == NULL) |
| 2285 | { |
| 2286 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2287 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 2288 | } |
| 2289 | |
| 2290 | /* Anytime the server *has* sent an opaque PRF input, we need to check |
| 2291 | * that we have a client opaque PRF input of the same size. */ |
| 2292 | if (s->s3->client_opaque_prf_input == NULL || |
| 2293 | s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len) |
| 2294 | { |
| 2295 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2296 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 2297 | } |
| 2298 | } |
| 2299 | #endif |
| 2300 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2301 | /* If we've requested certificate status and we wont get one |
| 2302 | * tell the callback |
| 2303 | */ |
| 2304 | if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected) |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2305 | && s->ctx && s->ctx->tlsext_status_cb) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2306 | { |
| 2307 | int r; |
| 2308 | /* Set resp to NULL, resplen to -1 so callback knows |
| 2309 | * there is no response. |
| 2310 | */ |
| 2311 | if (s->tlsext_ocsp_resp) |
| 2312 | { |
| 2313 | OPENSSL_free(s->tlsext_ocsp_resp); |
| 2314 | s->tlsext_ocsp_resp = NULL; |
| 2315 | } |
| 2316 | s->tlsext_ocsp_resplen = -1; |
| 2317 | r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); |
| 2318 | if (r == 0) |
| 2319 | { |
| 2320 | al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; |
| 2321 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2322 | } |
| 2323 | if (r < 0) |
| 2324 | { |
| 2325 | al = SSL_AD_INTERNAL_ERROR; |
| 2326 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2327 | } |
| 2328 | } |
| 2329 | |
| 2330 | switch (ret) |
| 2331 | { |
| 2332 | case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2333 | ssl3_send_alert(s,SSL3_AL_FATAL,al); |
| 2334 | return -1; |
| 2335 | |
| 2336 | case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 2337 | ssl3_send_alert(s,SSL3_AL_WARNING,al); |
| 2338 | return 1; |
| 2339 | |
| 2340 | case SSL_TLSEXT_ERR_NOACK: |
| 2341 | s->servername_done=0; |
| 2342 | default: |
| 2343 | return 1; |
| 2344 | } |
| 2345 | } |
| 2346 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2347 | /* Since the server cache lookup is done early on in the processing of the |
| 2348 | * ClientHello, and other operations depend on the result, we need to handle |
| 2349 | * any TLS session ticket extension at the same time. |
| 2350 | * |
| 2351 | * session_id: points at the session ID in the ClientHello. This code will |
| 2352 | * read past the end of this in order to parse out the session ticket |
| 2353 | * extension, if any. |
| 2354 | * len: the length of the session ID. |
| 2355 | * limit: a pointer to the first byte after the ClientHello. |
| 2356 | * ret: (output) on return, if a ticket was decrypted, then this is set to |
| 2357 | * point to the resulting session. |
| 2358 | * |
| 2359 | * If s->tls_session_secret_cb is set then we are expecting a pre-shared key |
| 2360 | * ciphersuite, in which case we have no use for session tickets and one will |
| 2361 | * never be decrypted, nor will s->tlsext_ticket_expected be set to 1. |
| 2362 | * |
| 2363 | * Returns: |
| 2364 | * -1: fatal error, either from parsing or decrypting the ticket. |
| 2365 | * 0: no ticket was found (or was ignored, based on settings). |
| 2366 | * 1: a zero length extension was found, indicating that the client supports |
| 2367 | * session tickets but doesn't currently have one to offer. |
| 2368 | * 2: either s->tls_session_secret_cb was set, or a ticket was offered but |
| 2369 | * couldn't be decrypted because of a non-fatal error. |
| 2370 | * 3: a ticket was successfully decrypted and *ret was set. |
| 2371 | * |
| 2372 | * Side effects: |
| 2373 | * Sets s->tlsext_ticket_expected to 1 if the server will have to issue |
| 2374 | * a new session ticket to the client because the client indicated support |
| 2375 | * (and s->tls_session_secret_cb is NULL) but the client either doesn't have |
| 2376 | * a session ticket or we couldn't use the one it gave us, or if |
| 2377 | * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket. |
| 2378 | * Otherwise, s->tlsext_ticket_expected is set to 0. |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2379 | */ |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2380 | int tls1_process_ticket(SSL *s, unsigned char *session_id, int len, |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2381 | const unsigned char *limit, SSL_SESSION **ret) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2382 | { |
| 2383 | /* Point after session ID in client hello */ |
| 2384 | const unsigned char *p = session_id + len; |
| 2385 | unsigned short i; |
Nagendra Modadugu | e45f106 | 2009-09-30 11:36:48 -0700 | [diff] [blame] | 2386 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2387 | *ret = NULL; |
| 2388 | s->tlsext_ticket_expected = 0; |
Nagendra Modadugu | e45f106 | 2009-09-30 11:36:48 -0700 | [diff] [blame] | 2389 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2390 | /* If tickets disabled behave as if no ticket present |
| 2391 | * to permit stateful resumption. |
| 2392 | */ |
| 2393 | if (SSL_get_options(s) & SSL_OP_NO_TICKET) |
| 2394 | return 0; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2395 | if ((s->version <= SSL3_VERSION) || !limit) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2396 | return 0; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2397 | if (p >= limit) |
| 2398 | return -1; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 2399 | /* Skip past DTLS cookie */ |
| 2400 | if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) |
| 2401 | { |
| 2402 | i = *(p++); |
| 2403 | p+= i; |
| 2404 | if (p >= limit) |
| 2405 | return -1; |
| 2406 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2407 | /* Skip past cipher list */ |
| 2408 | n2s(p, i); |
| 2409 | p+= i; |
| 2410 | if (p >= limit) |
| 2411 | return -1; |
| 2412 | /* Skip past compression algorithm list */ |
| 2413 | i = *(p++); |
| 2414 | p += i; |
| 2415 | if (p > limit) |
| 2416 | return -1; |
| 2417 | /* Now at start of extensions */ |
| 2418 | if ((p + 2) >= limit) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2419 | return 0; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2420 | n2s(p, i); |
| 2421 | while ((p + 4) <= limit) |
| 2422 | { |
| 2423 | unsigned short type, size; |
| 2424 | n2s(p, type); |
| 2425 | n2s(p, size); |
| 2426 | if (p + size > limit) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2427 | return 0; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2428 | if (type == TLSEXT_TYPE_session_ticket) |
| 2429 | { |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2430 | int r; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2431 | if (size == 0) |
| 2432 | { |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2433 | /* The client will accept a ticket but doesn't |
| 2434 | * currently have one. */ |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2435 | s->tlsext_ticket_expected = 1; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2436 | return 1; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2437 | } |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2438 | if (s->tls_session_secret_cb) |
| 2439 | { |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2440 | /* Indicate that the ticket couldn't be |
| 2441 | * decrypted rather than generating the session |
| 2442 | * from ticket now, trigger abbreviated |
| 2443 | * handshake based on external mechanism to |
| 2444 | * calculate the master secret later. */ |
| 2445 | return 2; |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2446 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2447 | r = tls_decrypt_ticket(s, p, size, session_id, len, ret); |
| 2448 | switch (r) |
| 2449 | { |
| 2450 | case 2: /* ticket couldn't be decrypted */ |
| 2451 | s->tlsext_ticket_expected = 1; |
| 2452 | return 2; |
| 2453 | case 3: /* ticket was decrypted */ |
| 2454 | return r; |
| 2455 | case 4: /* ticket decrypted but need to renew */ |
| 2456 | s->tlsext_ticket_expected = 1; |
| 2457 | return 3; |
| 2458 | default: /* fatal error */ |
| 2459 | return -1; |
| 2460 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2461 | } |
| 2462 | p += size; |
| 2463 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2464 | return 0; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2465 | } |
| 2466 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2467 | /* tls_decrypt_ticket attempts to decrypt a session ticket. |
| 2468 | * |
| 2469 | * etick: points to the body of the session ticket extension. |
| 2470 | * eticklen: the length of the session tickets extenion. |
| 2471 | * sess_id: points at the session ID. |
| 2472 | * sesslen: the length of the session ID. |
| 2473 | * psess: (output) on return, if a ticket was decrypted, then this is set to |
| 2474 | * point to the resulting session. |
| 2475 | * |
| 2476 | * Returns: |
| 2477 | * -1: fatal error, either from parsing or decrypting the ticket. |
| 2478 | * 2: the ticket couldn't be decrypted. |
| 2479 | * 3: a ticket was successfully decrypted and *psess was set. |
| 2480 | * 4: same as 3, but the ticket needs to be renewed. |
| 2481 | */ |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2482 | static int tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen, |
| 2483 | const unsigned char *sess_id, int sesslen, |
| 2484 | SSL_SESSION **psess) |
| 2485 | { |
| 2486 | SSL_SESSION *sess; |
| 2487 | unsigned char *sdec; |
| 2488 | const unsigned char *p; |
| 2489 | int slen, mlen, renew_ticket = 0; |
| 2490 | unsigned char tick_hmac[EVP_MAX_MD_SIZE]; |
| 2491 | HMAC_CTX hctx; |
| 2492 | EVP_CIPHER_CTX ctx; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 2493 | SSL_CTX *tctx = s->initial_ctx; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2494 | /* Need at least keyname + iv + some encrypted data */ |
| 2495 | if (eticklen < 48) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2496 | return 2; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2497 | /* Initialize session ticket encryption and HMAC contexts */ |
| 2498 | HMAC_CTX_init(&hctx); |
| 2499 | EVP_CIPHER_CTX_init(&ctx); |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 2500 | if (tctx->tlsext_ticket_key_cb) |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2501 | { |
| 2502 | unsigned char *nctick = (unsigned char *)etick; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 2503 | 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] | 2504 | &ctx, &hctx, 0); |
| 2505 | if (rv < 0) |
| 2506 | return -1; |
| 2507 | if (rv == 0) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2508 | return 2; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2509 | if (rv == 2) |
| 2510 | renew_ticket = 1; |
| 2511 | } |
| 2512 | else |
| 2513 | { |
| 2514 | /* Check key name matches */ |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 2515 | if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2516 | return 2; |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 2517 | 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] | 2518 | tlsext_tick_md(), NULL); |
| 2519 | EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, |
Brian Carlstrom | 98d58bb | 2010-03-09 09:56:55 -0800 | [diff] [blame] | 2520 | tctx->tlsext_tick_aes_key, etick + 16); |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2521 | } |
| 2522 | /* Attempt to process session ticket, first conduct sanity and |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2523 | * integrity checks on ticket. |
| 2524 | */ |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2525 | mlen = HMAC_size(&hctx); |
Brian Carlstrom | 221304e | 2010-04-15 14:22:36 -0700 | [diff] [blame] | 2526 | if (mlen < 0) |
| 2527 | { |
| 2528 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2529 | return -1; |
| 2530 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2531 | eticklen -= mlen; |
| 2532 | /* Check HMAC of encrypted ticket */ |
| 2533 | HMAC_Update(&hctx, etick, eticklen); |
| 2534 | HMAC_Final(&hctx, tick_hmac, NULL); |
| 2535 | HMAC_CTX_cleanup(&hctx); |
Brian Carlstrom | fa75fdb | 2013-02-01 23:44:05 -0800 | [diff] [blame] | 2536 | if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2537 | return 2; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2538 | /* Attempt to decrypt session data */ |
| 2539 | /* Move p after IV to start of encrypted ticket, update length */ |
| 2540 | p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx); |
| 2541 | eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx); |
| 2542 | sdec = OPENSSL_malloc(eticklen); |
| 2543 | if (!sdec) |
| 2544 | { |
| 2545 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2546 | return -1; |
| 2547 | } |
| 2548 | EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen); |
| 2549 | if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2550 | return 2; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2551 | slen += mlen; |
| 2552 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2553 | p = sdec; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2554 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2555 | sess = d2i_SSL_SESSION(NULL, &p, slen); |
| 2556 | OPENSSL_free(sdec); |
| 2557 | if (sess) |
| 2558 | { |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2559 | /* The session ID, if non-empty, is used by some clients to |
| 2560 | * detect that the ticket has been accepted. So we copy it to |
| 2561 | * the session structure. If it is empty set length to zero |
| 2562 | * as required by standard. |
| 2563 | */ |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2564 | if (sesslen) |
| 2565 | memcpy(sess->session_id, sess_id, sesslen); |
| 2566 | sess->session_id_length = sesslen; |
| 2567 | *psess = sess; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2568 | if (renew_ticket) |
| 2569 | return 4; |
| 2570 | else |
| 2571 | return 3; |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2572 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2573 | ERR_clear_error(); |
| 2574 | /* For session parse failure, indicate that we need to send a new |
| 2575 | * ticket. */ |
| 2576 | return 2; |
| 2577 | } |
| 2578 | |
| 2579 | /* Tables to translate from NIDs to TLS v1.2 ids */ |
| 2580 | |
| 2581 | typedef struct |
| 2582 | { |
| 2583 | int nid; |
| 2584 | int id; |
| 2585 | } tls12_lookup; |
| 2586 | |
| 2587 | static tls12_lookup tls12_md[] = { |
| 2588 | #ifndef OPENSSL_NO_MD5 |
| 2589 | {NID_md5, TLSEXT_hash_md5}, |
| 2590 | #endif |
| 2591 | #ifndef OPENSSL_NO_SHA |
| 2592 | {NID_sha1, TLSEXT_hash_sha1}, |
| 2593 | #endif |
| 2594 | #ifndef OPENSSL_NO_SHA256 |
| 2595 | {NID_sha224, TLSEXT_hash_sha224}, |
| 2596 | {NID_sha256, TLSEXT_hash_sha256}, |
| 2597 | #endif |
| 2598 | #ifndef OPENSSL_NO_SHA512 |
| 2599 | {NID_sha384, TLSEXT_hash_sha384}, |
| 2600 | {NID_sha512, TLSEXT_hash_sha512} |
| 2601 | #endif |
| 2602 | }; |
| 2603 | |
| 2604 | static tls12_lookup tls12_sig[] = { |
| 2605 | #ifndef OPENSSL_NO_RSA |
| 2606 | {EVP_PKEY_RSA, TLSEXT_signature_rsa}, |
| 2607 | #endif |
| 2608 | #ifndef OPENSSL_NO_DSA |
| 2609 | {EVP_PKEY_DSA, TLSEXT_signature_dsa}, |
| 2610 | #endif |
| 2611 | #ifndef OPENSSL_NO_ECDSA |
| 2612 | {EVP_PKEY_EC, TLSEXT_signature_ecdsa} |
| 2613 | #endif |
| 2614 | }; |
| 2615 | |
| 2616 | static int tls12_find_id(int nid, tls12_lookup *table, size_t tlen) |
| 2617 | { |
| 2618 | size_t i; |
| 2619 | for (i = 0; i < tlen; i++) |
| 2620 | { |
| 2621 | if (table[i].nid == nid) |
| 2622 | return table[i].id; |
| 2623 | } |
| 2624 | return -1; |
| 2625 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2626 | |
| 2627 | int tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md) |
| 2628 | { |
| 2629 | int sig_id, md_id; |
| 2630 | if (!md) |
| 2631 | return 0; |
| 2632 | md_id = tls12_find_id(EVP_MD_type(md), tls12_md, |
| 2633 | sizeof(tls12_md)/sizeof(tls12_lookup)); |
| 2634 | if (md_id == -1) |
| 2635 | return 0; |
| 2636 | sig_id = tls12_get_sigid(pk); |
| 2637 | if (sig_id == -1) |
| 2638 | return 0; |
| 2639 | p[0] = (unsigned char)md_id; |
| 2640 | p[1] = (unsigned char)sig_id; |
| 2641 | return 1; |
| 2642 | } |
| 2643 | |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2644 | /* tls12_get_sigid returns the TLS 1.2 SignatureAlgorithm value corresponding |
| 2645 | * to the given public key, or -1 if not known. */ |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2646 | int tls12_get_sigid(const EVP_PKEY *pk) |
| 2647 | { |
| 2648 | return tls12_find_id(pk->type, tls12_sig, |
| 2649 | sizeof(tls12_sig)/sizeof(tls12_lookup)); |
| 2650 | } |
| 2651 | |
| 2652 | const EVP_MD *tls12_get_hash(unsigned char hash_alg) |
| 2653 | { |
| 2654 | switch(hash_alg) |
| 2655 | { |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2656 | #ifndef OPENSSL_NO_SHA |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2657 | case TLSEXT_hash_sha1: |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2658 | return EVP_sha1(); |
| 2659 | #endif |
| 2660 | #ifndef OPENSSL_NO_SHA256 |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2661 | case TLSEXT_hash_sha224: |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2662 | return EVP_sha224(); |
| 2663 | |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2664 | case TLSEXT_hash_sha256: |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2665 | return EVP_sha256(); |
| 2666 | #endif |
| 2667 | #ifndef OPENSSL_NO_SHA512 |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2668 | case TLSEXT_hash_sha384: |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2669 | return EVP_sha384(); |
| 2670 | |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2671 | case TLSEXT_hash_sha512: |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2672 | return EVP_sha512(); |
| 2673 | #endif |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2674 | default: |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2675 | return NULL; |
| 2676 | |
| 2677 | } |
| 2678 | } |
| 2679 | |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2680 | /* tls1_process_sigalgs processes a signature_algorithms extension and sets the |
| 2681 | * digest functions accordingly for each key type. |
| 2682 | * |
| 2683 | * See RFC 5246, section 7.4.1.4.1. |
| 2684 | * |
| 2685 | * data: points to the content of the extension, not including type and length |
| 2686 | * headers. |
| 2687 | * dsize: the number of bytes of |data|. Must be even. |
| 2688 | */ |
| 2689 | void tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2690 | { |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2691 | int i; |
| 2692 | const EVP_MD *md, **digest_ptr; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2693 | /* Extension ignored for TLS versions below 1.2 */ |
| 2694 | if (TLS1_get_version(s) < TLS1_2_VERSION) |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2695 | return; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2696 | |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2697 | s->s3->digest_rsa = NULL; |
| 2698 | s->s3->digest_dsa = NULL; |
| 2699 | s->s3->digest_ecdsa = NULL; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2700 | |
| 2701 | for (i = 0; i < dsize; i += 2) |
| 2702 | { |
| 2703 | unsigned char hash_alg = data[i], sig_alg = data[i+1]; |
| 2704 | |
| 2705 | switch(sig_alg) |
| 2706 | { |
| 2707 | #ifndef OPENSSL_NO_RSA |
| 2708 | case TLSEXT_signature_rsa: |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2709 | digest_ptr = &s->s3->digest_rsa; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2710 | break; |
| 2711 | #endif |
| 2712 | #ifndef OPENSSL_NO_DSA |
| 2713 | case TLSEXT_signature_dsa: |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2714 | digest_ptr = &s->s3->digest_dsa; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2715 | break; |
| 2716 | #endif |
| 2717 | #ifndef OPENSSL_NO_ECDSA |
| 2718 | case TLSEXT_signature_ecdsa: |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2719 | digest_ptr = &s->s3->digest_ecdsa; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2720 | break; |
| 2721 | #endif |
| 2722 | default: |
| 2723 | continue; |
| 2724 | } |
| 2725 | |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2726 | if (*digest_ptr == NULL) |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2727 | { |
| 2728 | md = tls12_get_hash(hash_alg); |
| 2729 | if (md) |
Kenny Root | 9296b4b | 2013-06-24 12:03:19 -0700 | [diff] [blame] | 2730 | *digest_ptr = md; |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | } |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | #endif |
| 2737 | |
| 2738 | #ifndef OPENSSL_NO_HEARTBEATS |
| 2739 | int |
| 2740 | tls1_process_heartbeat(SSL *s) |
| 2741 | { |
| 2742 | unsigned char *p = &s->s3->rrec.data[0], *pl; |
| 2743 | unsigned short hbtype; |
| 2744 | unsigned int payload; |
| 2745 | unsigned int padding = 16; /* Use minimum padding */ |
| 2746 | |
| 2747 | /* Read type and payload length first */ |
| 2748 | hbtype = *p++; |
| 2749 | n2s(p, payload); |
| 2750 | pl = p; |
| 2751 | |
| 2752 | if (s->msg_callback) |
| 2753 | s->msg_callback(0, s->version, TLS1_RT_HEARTBEAT, |
| 2754 | &s->s3->rrec.data[0], s->s3->rrec.length, |
| 2755 | s, s->msg_callback_arg); |
| 2756 | |
| 2757 | if (hbtype == TLS1_HB_REQUEST) |
| 2758 | { |
| 2759 | unsigned char *buffer, *bp; |
| 2760 | int r; |
| 2761 | |
| 2762 | /* Allocate memory for the response, size is 1 bytes |
| 2763 | * message type, plus 2 bytes payload length, plus |
| 2764 | * payload, plus padding |
| 2765 | */ |
| 2766 | buffer = OPENSSL_malloc(1 + 2 + payload + padding); |
| 2767 | bp = buffer; |
| 2768 | |
| 2769 | /* Enter response type, length and copy payload */ |
| 2770 | *bp++ = TLS1_HB_RESPONSE; |
| 2771 | s2n(payload, bp); |
| 2772 | memcpy(bp, pl, payload); |
| 2773 | bp += payload; |
| 2774 | /* Random padding */ |
| 2775 | RAND_pseudo_bytes(bp, padding); |
| 2776 | |
| 2777 | r = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buffer, 3 + payload + padding); |
| 2778 | |
| 2779 | if (r >= 0 && s->msg_callback) |
| 2780 | s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT, |
| 2781 | buffer, 3 + payload + padding, |
| 2782 | s, s->msg_callback_arg); |
| 2783 | |
| 2784 | OPENSSL_free(buffer); |
| 2785 | |
| 2786 | if (r < 0) |
| 2787 | return r; |
| 2788 | } |
| 2789 | else if (hbtype == TLS1_HB_RESPONSE) |
| 2790 | { |
| 2791 | unsigned int seq; |
| 2792 | |
| 2793 | /* We only send sequence numbers (2 bytes unsigned int), |
| 2794 | * and 16 random bytes, so we just try to read the |
| 2795 | * sequence number */ |
| 2796 | n2s(pl, seq); |
| 2797 | |
| 2798 | if (payload == 18 && seq == s->tlsext_hb_seq) |
| 2799 | { |
| 2800 | s->tlsext_hb_seq++; |
| 2801 | s->tlsext_hb_pending = 0; |
| 2802 | } |
| 2803 | } |
| 2804 | |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2805 | return 0; |
| 2806 | } |
| 2807 | |
Brian Carlstrom | 392aa7c | 2012-03-15 16:03:43 -0700 | [diff] [blame] | 2808 | int |
| 2809 | tls1_heartbeat(SSL *s) |
| 2810 | { |
| 2811 | unsigned char *buf, *p; |
| 2812 | int ret; |
| 2813 | unsigned int payload = 18; /* Sequence number + random bytes */ |
| 2814 | unsigned int padding = 16; /* Use minimum padding */ |
| 2815 | |
| 2816 | /* Only send if peer supports and accepts HB requests... */ |
| 2817 | if (!(s->tlsext_heartbeat & SSL_TLSEXT_HB_ENABLED) || |
| 2818 | s->tlsext_heartbeat & SSL_TLSEXT_HB_DONT_SEND_REQUESTS) |
| 2819 | { |
| 2820 | SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_TLS_HEARTBEAT_PEER_DOESNT_ACCEPT); |
| 2821 | return -1; |
| 2822 | } |
| 2823 | |
| 2824 | /* ...and there is none in flight yet... */ |
| 2825 | if (s->tlsext_hb_pending) |
| 2826 | { |
| 2827 | SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_TLS_HEARTBEAT_PENDING); |
| 2828 | return -1; |
| 2829 | } |
| 2830 | |
| 2831 | /* ...and no handshake in progress. */ |
| 2832 | if (SSL_in_init(s) || s->in_handshake) |
| 2833 | { |
| 2834 | SSLerr(SSL_F_TLS1_HEARTBEAT,SSL_R_UNEXPECTED_MESSAGE); |
| 2835 | return -1; |
| 2836 | } |
| 2837 | |
| 2838 | /* Check if padding is too long, payload and padding |
| 2839 | * must not exceed 2^14 - 3 = 16381 bytes in total. |
| 2840 | */ |
| 2841 | OPENSSL_assert(payload + padding <= 16381); |
| 2842 | |
| 2843 | /* Create HeartBeat message, we just use a sequence number |
| 2844 | * as payload to distuingish different messages and add |
| 2845 | * some random stuff. |
| 2846 | * - Message Type, 1 byte |
| 2847 | * - Payload Length, 2 bytes (unsigned int) |
| 2848 | * - Payload, the sequence number (2 bytes uint) |
| 2849 | * - Payload, random bytes (16 bytes uint) |
| 2850 | * - Padding |
| 2851 | */ |
| 2852 | buf = OPENSSL_malloc(1 + 2 + payload + padding); |
| 2853 | p = buf; |
| 2854 | /* Message Type */ |
| 2855 | *p++ = TLS1_HB_REQUEST; |
| 2856 | /* Payload length (18 bytes here) */ |
| 2857 | s2n(payload, p); |
| 2858 | /* Sequence number */ |
| 2859 | s2n(s->tlsext_hb_seq, p); |
| 2860 | /* 16 random bytes */ |
| 2861 | RAND_pseudo_bytes(p, 16); |
| 2862 | p += 16; |
| 2863 | /* Random padding */ |
| 2864 | RAND_pseudo_bytes(p, padding); |
| 2865 | |
| 2866 | ret = ssl3_write_bytes(s, TLS1_RT_HEARTBEAT, buf, 3 + payload + padding); |
| 2867 | if (ret >= 0) |
| 2868 | { |
| 2869 | if (s->msg_callback) |
| 2870 | s->msg_callback(1, s->version, TLS1_RT_HEARTBEAT, |
| 2871 | buf, 3 + payload + padding, |
| 2872 | s, s->msg_callback_arg); |
| 2873 | |
| 2874 | s->tlsext_hb_pending = 1; |
| 2875 | } |
| 2876 | |
| 2877 | OPENSSL_free(buf); |
| 2878 | |
| 2879 | return ret; |
| 2880 | } |
The Android Open Source Project | 656d9c7 | 2009-03-03 19:30:25 -0800 | [diff] [blame] | 2881 | #endif |
Adam Langley | 45bcfbc | 2013-01-16 13:43:53 -0800 | [diff] [blame] | 2882 | |
| 2883 | #if !defined(OPENSSL_NO_TLSEXT) |
| 2884 | /* tls1_channel_id_hash calculates the signed data for a Channel ID on the given |
| 2885 | * SSL connection and writes it to |md|. |
| 2886 | */ |
| 2887 | int |
| 2888 | tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) |
| 2889 | { |
| 2890 | EVP_MD_CTX ctx; |
| 2891 | unsigned char temp_digest[EVP_MAX_MD_SIZE]; |
| 2892 | unsigned temp_digest_len; |
| 2893 | int i; |
| 2894 | static const char kClientIDMagic[] = "TLS Channel ID signature"; |
| 2895 | |
| 2896 | if (s->s3->handshake_buffer) |
| 2897 | if (!ssl3_digest_cached_records(s)) |
| 2898 | return 0; |
| 2899 | |
| 2900 | EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic)); |
| 2901 | |
| 2902 | EVP_MD_CTX_init(&ctx); |
| 2903 | for (i = 0; i < SSL_MAX_DIGEST; i++) |
| 2904 | { |
| 2905 | if (s->s3->handshake_dgst[i] == NULL) |
| 2906 | continue; |
| 2907 | EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i]); |
| 2908 | EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len); |
| 2909 | EVP_DigestUpdate(md, temp_digest, temp_digest_len); |
| 2910 | } |
| 2911 | EVP_MD_CTX_cleanup(&ctx); |
| 2912 | |
| 2913 | return 1; |
| 2914 | } |
| 2915 | #endif |