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