diff options
Diffstat (limited to 'src/lib/libssl/t1_lib.c')
| -rw-r--r-- | src/lib/libssl/t1_lib.c | 2383 |
1 files changed, 2383 insertions, 0 deletions
diff --git a/src/lib/libssl/t1_lib.c b/src/lib/libssl/t1_lib.c new file mode 100644 index 0000000000..8796651806 --- /dev/null +++ b/src/lib/libssl/t1_lib.c | |||
| @@ -0,0 +1,2383 @@ | |||
| 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 | */ | ||
| 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 | */ | ||
| 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> | ||
| 117 | #include <openssl/rand.h> | ||
| 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 | ||
| 124 | tls_decrypt_ticket(SSL *s, const unsigned char *tick, int ticklen, | ||
| 125 | const unsigned char *sess_id, int sesslen, | ||
| 126 | SSL_SESSION **psess); | ||
| 127 | #endif | ||
| 128 | |||
| 129 | SSL3_ENC_METHOD TLSv1_enc_data = { | ||
| 130 | tls1_enc, | ||
| 131 | tls1_mac, | ||
| 132 | tls1_setup_key_block, | ||
| 133 | tls1_generate_master_secret, | ||
| 134 | tls1_change_cipher_state, | ||
| 135 | tls1_final_finish_mac, | ||
| 136 | TLS1_FINISH_MAC_LENGTH, | ||
| 137 | tls1_cert_verify_mac, | ||
| 138 | TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, | ||
| 139 | TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, | ||
| 140 | tls1_alert_code, | ||
| 141 | tls1_export_keying_material, | ||
| 142 | }; | ||
| 143 | |||
| 144 | long | ||
| 145 | tls1_default_timeout(void) | ||
| 146 | { | ||
| 147 | /* 2 hours, the 24 hours mentioned in the TLSv1 spec | ||
| 148 | * is way too long for http, the cache would over fill */ | ||
| 149 | return (60*60*2); | ||
| 150 | } | ||
| 151 | |||
| 152 | int | ||
| 153 | tls1_new(SSL *s) | ||
| 154 | { | ||
| 155 | if (!ssl3_new(s)) return (0); | ||
| 156 | s->method->ssl_clear(s); | ||
| 157 | return (1); | ||
| 158 | } | ||
| 159 | |||
| 160 | void | ||
| 161 | tls1_free(SSL *s) | ||
| 162 | { | ||
| 163 | #ifndef OPENSSL_NO_TLSEXT | ||
| 164 | if (s->tlsext_session_ticket) { | ||
| 165 | OPENSSL_free(s->tlsext_session_ticket); | ||
| 166 | } | ||
| 167 | #endif /* OPENSSL_NO_TLSEXT */ | ||
| 168 | ssl3_free(s); | ||
| 169 | } | ||
| 170 | |||
| 171 | void | ||
| 172 | tls1_clear(SSL *s) | ||
| 173 | { | ||
| 174 | ssl3_clear(s); | ||
| 175 | s->version = s->method->version; | ||
| 176 | } | ||
| 177 | |||
| 178 | #ifndef OPENSSL_NO_EC | ||
| 179 | |||
| 180 | static int nid_list[] = { | ||
| 181 | NID_sect163k1, /* sect163k1 (1) */ | ||
| 182 | NID_sect163r1, /* sect163r1 (2) */ | ||
| 183 | NID_sect163r2, /* sect163r2 (3) */ | ||
| 184 | NID_sect193r1, /* sect193r1 (4) */ | ||
| 185 | NID_sect193r2, /* sect193r2 (5) */ | ||
| 186 | NID_sect233k1, /* sect233k1 (6) */ | ||
| 187 | NID_sect233r1, /* sect233r1 (7) */ | ||
| 188 | NID_sect239k1, /* sect239k1 (8) */ | ||
| 189 | NID_sect283k1, /* sect283k1 (9) */ | ||
| 190 | NID_sect283r1, /* sect283r1 (10) */ | ||
| 191 | NID_sect409k1, /* sect409k1 (11) */ | ||
| 192 | NID_sect409r1, /* sect409r1 (12) */ | ||
| 193 | NID_sect571k1, /* sect571k1 (13) */ | ||
| 194 | NID_sect571r1, /* sect571r1 (14) */ | ||
| 195 | NID_secp160k1, /* secp160k1 (15) */ | ||
| 196 | NID_secp160r1, /* secp160r1 (16) */ | ||
| 197 | NID_secp160r2, /* secp160r2 (17) */ | ||
| 198 | NID_secp192k1, /* secp192k1 (18) */ | ||
| 199 | NID_X9_62_prime192v1, /* secp192r1 (19) */ | ||
| 200 | NID_secp224k1, /* secp224k1 (20) */ | ||
| 201 | NID_secp224r1, /* secp224r1 (21) */ | ||
| 202 | NID_secp256k1, /* secp256k1 (22) */ | ||
| 203 | NID_X9_62_prime256v1, /* secp256r1 (23) */ | ||
| 204 | NID_secp384r1, /* secp384r1 (24) */ | ||
| 205 | NID_secp521r1 /* secp521r1 (25) */ | ||
| 206 | }; | ||
| 207 | |||
| 208 | static int pref_list[] = { | ||
| 209 | NID_sect571r1, /* sect571r1 (14) */ | ||
| 210 | NID_sect571k1, /* sect571k1 (13) */ | ||
| 211 | NID_secp521r1, /* secp521r1 (25) */ | ||
| 212 | NID_sect409k1, /* sect409k1 (11) */ | ||
| 213 | NID_sect409r1, /* sect409r1 (12) */ | ||
| 214 | NID_secp384r1, /* secp384r1 (24) */ | ||
| 215 | NID_sect283k1, /* sect283k1 (9) */ | ||
| 216 | NID_sect283r1, /* sect283r1 (10) */ | ||
| 217 | NID_secp256k1, /* secp256k1 (22) */ | ||
| 218 | NID_X9_62_prime256v1, /* secp256r1 (23) */ | ||
| 219 | NID_sect239k1, /* sect239k1 (8) */ | ||
| 220 | NID_sect233k1, /* sect233k1 (6) */ | ||
| 221 | NID_sect233r1, /* sect233r1 (7) */ | ||
| 222 | NID_secp224k1, /* secp224k1 (20) */ | ||
| 223 | NID_secp224r1, /* secp224r1 (21) */ | ||
| 224 | NID_sect193r1, /* sect193r1 (4) */ | ||
| 225 | NID_sect193r2, /* sect193r2 (5) */ | ||
| 226 | NID_secp192k1, /* secp192k1 (18) */ | ||
| 227 | NID_X9_62_prime192v1, /* secp192r1 (19) */ | ||
| 228 | NID_sect163k1, /* sect163k1 (1) */ | ||
| 229 | NID_sect163r1, /* sect163r1 (2) */ | ||
| 230 | NID_sect163r2, /* sect163r2 (3) */ | ||
| 231 | NID_secp160k1, /* secp160k1 (15) */ | ||
| 232 | NID_secp160r1, /* secp160r1 (16) */ | ||
| 233 | NID_secp160r2, /* secp160r2 (17) */ | ||
| 234 | }; | ||
| 235 | |||
| 236 | int | ||
| 237 | tls1_ec_curve_id2nid(int curve_id) | ||
| 238 | { | ||
| 239 | /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */ | ||
| 240 | if ((curve_id < 1) || ((unsigned int)curve_id > | ||
| 241 | sizeof(nid_list)/sizeof(nid_list[0]))) | ||
| 242 | return 0; | ||
| 243 | return nid_list[curve_id - 1]; | ||
| 244 | } | ||
| 245 | |||
| 246 | int | ||
| 247 | tls1_ec_nid2curve_id(int nid) | ||
| 248 | { | ||
| 249 | /* ECC curves from draft-ietf-tls-ecc-12.txt (Oct. 17, 2005) */ | ||
| 250 | switch (nid) { | ||
| 251 | case NID_sect163k1: /* sect163k1 (1) */ | ||
| 252 | return 1; | ||
| 253 | case NID_sect163r1: /* sect163r1 (2) */ | ||
| 254 | return 2; | ||
| 255 | case NID_sect163r2: /* sect163r2 (3) */ | ||
| 256 | return 3; | ||
| 257 | case NID_sect193r1: /* sect193r1 (4) */ | ||
| 258 | return 4; | ||
| 259 | case NID_sect193r2: /* sect193r2 (5) */ | ||
| 260 | return 5; | ||
| 261 | case NID_sect233k1: /* sect233k1 (6) */ | ||
| 262 | return 6; | ||
| 263 | case NID_sect233r1: /* sect233r1 (7) */ | ||
| 264 | return 7; | ||
| 265 | case NID_sect239k1: /* sect239k1 (8) */ | ||
| 266 | return 8; | ||
| 267 | case NID_sect283k1: /* sect283k1 (9) */ | ||
| 268 | return 9; | ||
| 269 | case NID_sect283r1: /* sect283r1 (10) */ | ||
| 270 | return 10; | ||
| 271 | case NID_sect409k1: /* sect409k1 (11) */ | ||
| 272 | return 11; | ||
| 273 | case NID_sect409r1: /* sect409r1 (12) */ | ||
| 274 | return 12; | ||
| 275 | case NID_sect571k1: /* sect571k1 (13) */ | ||
| 276 | return 13; | ||
| 277 | case NID_sect571r1: /* sect571r1 (14) */ | ||
| 278 | return 14; | ||
| 279 | case NID_secp160k1: /* secp160k1 (15) */ | ||
| 280 | return 15; | ||
| 281 | case NID_secp160r1: /* secp160r1 (16) */ | ||
| 282 | return 16; | ||
| 283 | case NID_secp160r2: /* secp160r2 (17) */ | ||
| 284 | return 17; | ||
| 285 | case NID_secp192k1: /* secp192k1 (18) */ | ||
| 286 | return 18; | ||
| 287 | case NID_X9_62_prime192v1: /* secp192r1 (19) */ | ||
| 288 | return 19; | ||
| 289 | case NID_secp224k1: /* secp224k1 (20) */ | ||
| 290 | return 20; | ||
| 291 | case NID_secp224r1: /* secp224r1 (21) */ | ||
| 292 | return 21; | ||
| 293 | case NID_secp256k1: /* secp256k1 (22) */ | ||
| 294 | return 22; | ||
| 295 | case NID_X9_62_prime256v1: /* secp256r1 (23) */ | ||
| 296 | return 23; | ||
| 297 | case NID_secp384r1: /* secp384r1 (24) */ | ||
| 298 | return 24; | ||
| 299 | case NID_secp521r1: /* secp521r1 (25) */ | ||
| 300 | return 25; | ||
| 301 | default: | ||
| 302 | return 0; | ||
| 303 | } | ||
| 304 | } | ||
| 305 | #endif /* OPENSSL_NO_EC */ | ||
| 306 | |||
| 307 | #ifndef OPENSSL_NO_TLSEXT | ||
| 308 | |||
| 309 | /* List of supported signature algorithms and hashes. Should make this | ||
| 310 | * customisable at some point, for now include everything we support. | ||
| 311 | */ | ||
| 312 | |||
| 313 | #ifdef OPENSSL_NO_RSA | ||
| 314 | #define tlsext_sigalg_rsa(md) /* */ | ||
| 315 | #else | ||
| 316 | #define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa, | ||
| 317 | #endif | ||
| 318 | |||
| 319 | #ifdef OPENSSL_NO_DSA | ||
| 320 | #define tlsext_sigalg_dsa(md) /* */ | ||
| 321 | #else | ||
| 322 | #define tlsext_sigalg_dsa(md) md, TLSEXT_signature_dsa, | ||
| 323 | #endif | ||
| 324 | |||
| 325 | #ifdef OPENSSL_NO_ECDSA | ||
| 326 | #define tlsext_sigalg_ecdsa(md) /* */ | ||
| 327 | #else | ||
| 328 | #define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa, | ||
| 329 | #endif | ||
| 330 | |||
| 331 | #define tlsext_sigalg(md) \ | ||
| 332 | tlsext_sigalg_rsa(md) \ | ||
| 333 | tlsext_sigalg_dsa(md) \ | ||
| 334 | tlsext_sigalg_ecdsa(md) | ||
| 335 | |||
| 336 | static unsigned char tls12_sigalgs[] = { | ||
| 337 | #ifndef OPENSSL_NO_SHA512 | ||
| 338 | tlsext_sigalg(TLSEXT_hash_sha512) | ||
| 339 | tlsext_sigalg(TLSEXT_hash_sha384) | ||
| 340 | #endif | ||
| 341 | #ifndef OPENSSL_NO_SHA256 | ||
| 342 | tlsext_sigalg(TLSEXT_hash_sha256) | ||
| 343 | tlsext_sigalg(TLSEXT_hash_sha224) | ||
| 344 | #endif | ||
| 345 | #ifndef OPENSSL_NO_SHA | ||
| 346 | tlsext_sigalg(TLSEXT_hash_sha1) | ||
| 347 | #endif | ||
| 348 | }; | ||
| 349 | |||
| 350 | int | ||
| 351 | tls12_get_req_sig_algs(SSL *s, unsigned char *p) | ||
| 352 | { | ||
| 353 | size_t slen = sizeof(tls12_sigalgs); | ||
| 354 | if (p) | ||
| 355 | memcpy(p, tls12_sigalgs, slen); | ||
| 356 | return (int)slen; | ||
| 357 | } | ||
| 358 | |||
| 359 | unsigned char | ||
| 360 | *ssl_add_clienthello_tlsext(SSL *s, unsigned char *p, unsigned char *limit) | ||
| 361 | { | ||
| 362 | int extdatalen = 0; | ||
| 363 | unsigned char *ret = p; | ||
| 364 | |||
| 365 | /* don't add extensions for SSLv3 unless doing secure renegotiation */ | ||
| 366 | if (s->client_version == SSL3_VERSION | ||
| 367 | && !s->s3->send_connection_binding) | ||
| 368 | return p; | ||
| 369 | |||
| 370 | ret += 2; | ||
| 371 | |||
| 372 | if (ret>=limit) return NULL; /* this really never occurs, but ... */ | ||
| 373 | |||
| 374 | if (s->tlsext_hostname != NULL) { | ||
| 375 | /* Add TLS extension servername to the Client Hello message */ | ||
| 376 | unsigned long size_str; | ||
| 377 | long lenmax; | ||
| 378 | |||
| 379 | |||
| 380 | /* check for enough space. | ||
| 381 | 4 for the servername type and entension length | ||
| 382 | 2 for servernamelist length | ||
| 383 | 1 for the hostname type | ||
| 384 | 2 for hostname length | ||
| 385 | + hostname length | ||
| 386 | */ | ||
| 387 | |||
| 388 | if ((lenmax = limit - ret - 9) < 0 | ||
| 389 | || (size_str = strlen(s->tlsext_hostname)) > (unsigned long)lenmax) | ||
| 390 | return NULL; | ||
| 391 | |||
| 392 | /* extension type and length */ | ||
| 393 | s2n(TLSEXT_TYPE_server_name, ret); | ||
| 394 | |||
| 395 | s2n(size_str + 5, ret); | ||
| 396 | |||
| 397 | /* length of servername list */ | ||
| 398 | s2n(size_str + 3, ret); | ||
| 399 | |||
| 400 | /* hostname type, length and hostname */ | ||
| 401 | *(ret++) = (unsigned char) TLSEXT_NAMETYPE_host_name; | ||
| 402 | s2n(size_str, ret); | ||
| 403 | memcpy(ret, s->tlsext_hostname, size_str); | ||
| 404 | ret += size_str; | ||
| 405 | } | ||
| 406 | |||
| 407 | /* Add RI if renegotiating */ | ||
| 408 | if (s->renegotiate) { | ||
| 409 | int el; | ||
| 410 | |||
| 411 | if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) { | ||
| 412 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | ||
| 413 | return NULL; | ||
| 414 | } | ||
| 415 | |||
| 416 | if ((limit - p - 4 - el) | ||
| 417 | < 0) return NULL; | ||
| 418 | |||
| 419 | s2n(TLSEXT_TYPE_renegotiate, ret); | ||
| 420 | s2n(el, ret); | ||
| 421 | |||
| 422 | if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) { | ||
| 423 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | ||
| 424 | return NULL; | ||
| 425 | } | ||
| 426 | |||
| 427 | ret += el; | ||
| 428 | } | ||
| 429 | |||
| 430 | #ifndef OPENSSL_NO_SRP | ||
| 431 | /* Add SRP username if there is one */ | ||
| 432 | if (s->srp_ctx.login != NULL) | ||
| 433 | { /* Add TLS extension SRP username to the Client Hello message */ | ||
| 434 | |||
| 435 | int login_len = strlen(s->srp_ctx.login); | ||
| 436 | |||
| 437 | if (login_len > 255 || login_len == 0) { | ||
| 438 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | ||
| 439 | return NULL; | ||
| 440 | } | ||
| 441 | |||
| 442 | /* check for enough space. | ||
| 443 | 4 for the srp type type and entension length | ||
| 444 | 1 for the srp user identity | ||
| 445 | + srp user identity length | ||
| 446 | */ | ||
| 447 | if ((limit - ret - 5 - login_len) | ||
| 448 | < 0) return NULL; | ||
| 449 | |||
| 450 | |||
| 451 | /* fill in the extension */ | ||
| 452 | s2n(TLSEXT_TYPE_srp, ret); | ||
| 453 | s2n(login_len + 1, ret); | ||
| 454 | (*ret++) = (unsigned char) login_len; | ||
| 455 | memcpy(ret, s->srp_ctx.login, login_len); | ||
| 456 | ret += login_len; | ||
| 457 | } | ||
| 458 | #endif | ||
| 459 | |||
| 460 | #ifndef OPENSSL_NO_EC | ||
| 461 | if (s->tlsext_ecpointformatlist != NULL && | ||
| 462 | s->version != DTLS1_VERSION) { | ||
| 463 | /* Add TLS extension ECPointFormats to the ClientHello message */ | ||
| 464 | long lenmax; | ||
| 465 | |||
| 466 | |||
| 467 | if ((lenmax = limit - ret - 5) | ||
| 468 | < 0) return NULL; | ||
| 469 | |||
| 470 | if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL; | ||
| 471 | if (s->tlsext_ecpointformatlist_length > 255) { | ||
| 472 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | ||
| 473 | return NULL; | ||
| 474 | } | ||
| 475 | |||
| 476 | s2n(TLSEXT_TYPE_ec_point_formats, ret); | ||
| 477 | s2n(s->tlsext_ecpointformatlist_length + 1, ret); | ||
| 478 | *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length; | ||
| 479 | memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length); | ||
| 480 | ret += s->tlsext_ecpointformatlist_length; | ||
| 481 | } | ||
| 482 | if (s->tlsext_ellipticcurvelist != NULL && | ||
| 483 | s->version != DTLS1_VERSION) { | ||
| 484 | /* Add TLS extension EllipticCurves to the ClientHello message */ | ||
| 485 | long lenmax; | ||
| 486 | |||
| 487 | |||
| 488 | if ((lenmax = limit - ret - 6) | ||
| 489 | < 0) return NULL; | ||
| 490 | |||
| 491 | if (s->tlsext_ellipticcurvelist_length > (unsigned long)lenmax) return NULL; | ||
| 492 | if (s->tlsext_ellipticcurvelist_length > 65532) { | ||
| 493 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | ||
| 494 | return NULL; | ||
| 495 | } | ||
| 496 | |||
| 497 | s2n(TLSEXT_TYPE_elliptic_curves, ret); | ||
| 498 | s2n(s->tlsext_ellipticcurvelist_length + 2, ret); | ||
| 499 | |||
| 500 | /* NB: draft-ietf-tls-ecc-12.txt uses a one-byte prefix for | ||
| 501 | * elliptic_curve_list, but the examples use two bytes. | ||
| 502 | * http://www1.ietf.org/mail-archive/web/tls/current/msg00538.html | ||
| 503 | * resolves this to two bytes. | ||
| 504 | */ | ||
| 505 | s2n(s->tlsext_ellipticcurvelist_length, ret); | ||
| 506 | memcpy(ret, s->tlsext_ellipticcurvelist, s->tlsext_ellipticcurvelist_length); | ||
| 507 | ret += s->tlsext_ellipticcurvelist_length; | ||
| 508 | } | ||
| 509 | #endif /* OPENSSL_NO_EC */ | ||
| 510 | |||
| 511 | if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) { | ||
| 512 | int ticklen; | ||
| 513 | if (!s->new_session && s->session && s->session->tlsext_tick) | ||
| 514 | ticklen = s->session->tlsext_ticklen; | ||
| 515 | else if (s->session && s->tlsext_session_ticket && | ||
| 516 | s->tlsext_session_ticket->data) { | ||
| 517 | ticklen = s->tlsext_session_ticket->length; | ||
| 518 | s->session->tlsext_tick = OPENSSL_malloc(ticklen); | ||
| 519 | if (!s->session->tlsext_tick) | ||
| 520 | return NULL; | ||
| 521 | memcpy(s->session->tlsext_tick, | ||
| 522 | s->tlsext_session_ticket->data, | ||
| 523 | ticklen); | ||
| 524 | s->session->tlsext_ticklen = ticklen; | ||
| 525 | } else | ||
| 526 | ticklen = 0; | ||
| 527 | if (ticklen == 0 && s->tlsext_session_ticket && | ||
| 528 | s->tlsext_session_ticket->data == NULL) | ||
| 529 | goto skip_ext; | ||
| 530 | /* Check for enough room 2 for extension type, 2 for len | ||
| 531 | * rest for ticket | ||
| 532 | */ | ||
| 533 | if ((long)(limit - ret - 4 - ticklen) < 0) return NULL; | ||
| 534 | s2n(TLSEXT_TYPE_session_ticket, ret); | ||
| 535 | |||
| 536 | s2n(ticklen, ret); | ||
| 537 | if (ticklen) { | ||
| 538 | memcpy(ret, s->session->tlsext_tick, ticklen); | ||
| 539 | ret += ticklen; | ||
| 540 | } | ||
| 541 | } | ||
| 542 | skip_ext: | ||
| 543 | |||
| 544 | if (TLS1_get_client_version(s) >= TLS1_2_VERSION) { | ||
| 545 | if ((size_t)(limit - ret) < sizeof(tls12_sigalgs) + 6) | ||
| 546 | return NULL; | ||
| 547 | |||
| 548 | s2n(TLSEXT_TYPE_signature_algorithms, ret); | ||
| 549 | s2n(sizeof(tls12_sigalgs) + 2, ret); | ||
| 550 | s2n(sizeof(tls12_sigalgs), ret); | ||
| 551 | memcpy(ret, tls12_sigalgs, sizeof(tls12_sigalgs)); | ||
| 552 | ret += sizeof(tls12_sigalgs); | ||
| 553 | } | ||
| 554 | |||
| 555 | #ifdef TLSEXT_TYPE_opaque_prf_input | ||
| 556 | if (s->s3->client_opaque_prf_input != NULL && | ||
| 557 | s->version != DTLS1_VERSION) { | ||
| 558 | size_t col = s->s3->client_opaque_prf_input_len; | ||
| 559 | |||
| 560 | if ((long)(limit - ret - 6 - col < 0)) | ||
| 561 | return NULL; | ||
| 562 | if (col > 0xFFFD) /* can't happen */ | ||
| 563 | return NULL; | ||
| 564 | |||
| 565 | s2n(TLSEXT_TYPE_opaque_prf_input, ret); | ||
| 566 | |||
| 567 | s2n(col + 2, ret); | ||
| 568 | s2n(col, ret); | ||
| 569 | memcpy(ret, s->s3->client_opaque_prf_input, col); | ||
| 570 | ret += col; | ||
| 571 | } | ||
| 572 | #endif | ||
| 573 | |||
| 574 | if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp && | ||
| 575 | s->version != DTLS1_VERSION) { | ||
| 576 | int i; | ||
| 577 | long extlen, idlen, itmp; | ||
| 578 | OCSP_RESPID *id; | ||
| 579 | |||
| 580 | idlen = 0; | ||
| 581 | for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) { | ||
| 582 | id = sk_OCSP_RESPID_value(s->tlsext_ocsp_ids, i); | ||
| 583 | itmp = i2d_OCSP_RESPID(id, NULL); | ||
| 584 | if (itmp <= 0) | ||
| 585 | return NULL; | ||
| 586 | idlen += itmp + 2; | ||
| 587 | } | ||
| 588 | |||
| 589 | if (s->tlsext_ocsp_exts) { | ||
| 590 | extlen = i2d_X509_EXTENSIONS(s->tlsext_ocsp_exts, NULL); | ||
| 591 | if (extlen < 0) | ||
| 592 | return NULL; | ||
| 593 | } else | ||
| 594 | extlen = 0; | ||
| 595 | |||
| 596 | if ((long)(limit - ret - 7 - extlen - idlen) < 0) return NULL; | ||
| 597 | s2n(TLSEXT_TYPE_status_request, ret); | ||
| 598 | if (extlen + idlen > 0xFFF0) | ||
| 599 | return NULL; | ||
| 600 | s2n(extlen + idlen + 5, ret); | ||
| 601 | *(ret++) = TLSEXT_STATUSTYPE_ocsp; | ||
| 602 | s2n(idlen, ret); | ||
| 603 | for (i = 0; i < sk_OCSP_RESPID_num(s->tlsext_ocsp_ids); i++) { | ||
| 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 | |||
| 618 | #ifndef OPENSSL_NO_NEXTPROTONEG | ||
| 619 | if (s->ctx->next_proto_select_cb && !s->s3->tmp.finish_md_len) { | ||
| 620 | /* The client advertises an emtpy extension to indicate its | ||
| 621 | * support for Next Protocol Negotiation */ | ||
| 622 | if (limit - ret - 4 < 0) | ||
| 623 | return NULL; | ||
| 624 | s2n(TLSEXT_TYPE_next_proto_neg, ret); | ||
| 625 | s2n(0, ret); | ||
| 626 | } | ||
| 627 | #endif | ||
| 628 | |||
| 629 | #ifndef OPENSSL_NO_SRTP | ||
| 630 | if (SSL_get_srtp_profiles(s)) { | ||
| 631 | int el; | ||
| 632 | |||
| 633 | ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0); | ||
| 634 | |||
| 635 | if ((limit - p - 4 - el) | ||
| 636 | < 0) return NULL; | ||
| 637 | |||
| 638 | s2n(TLSEXT_TYPE_use_srtp, ret); | ||
| 639 | s2n(el, ret); | ||
| 640 | |||
| 641 | if (ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) { | ||
| 642 | SSLerr(SSL_F_SSL_ADD_CLIENTHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | ||
| 643 | return NULL; | ||
| 644 | } | ||
| 645 | ret += el; | ||
| 646 | } | ||
| 647 | #endif | ||
| 648 | |||
| 649 | #ifdef TLSEXT_TYPE_padding | ||
| 650 | /* Add padding to workaround bugs in F5 terminators. | ||
| 651 | * See https://tools.ietf.org/html/draft-agl-tls-padding-03 | ||
| 652 | * | ||
| 653 | * NB: because this code works out the length of all existing | ||
| 654 | * extensions it MUST always appear last. | ||
| 655 | */ | ||
| 656 | { | ||
| 657 | int hlen = ret - (unsigned char *)s->init_buf->data; | ||
| 658 | /* The code in s23_clnt.c to build ClientHello messages includes the | ||
| 659 | * 5-byte record header in the buffer, while the code in s3_clnt.c does | ||
| 660 | * not. */ | ||
| 661 | if (s->state == SSL23_ST_CW_CLNT_HELLO_A) | ||
| 662 | hlen -= 5; | ||
| 663 | if (hlen > 0xff && hlen < 0x200) { | ||
| 664 | hlen = 0x200 - hlen; | ||
| 665 | if (hlen >= 4) | ||
| 666 | hlen -= 4; | ||
| 667 | else | ||
| 668 | hlen = 0; | ||
| 669 | |||
| 670 | s2n(TLSEXT_TYPE_padding, ret); | ||
| 671 | s2n(hlen, ret); | ||
| 672 | memset(ret, 0, hlen); | ||
| 673 | ret += hlen; | ||
| 674 | } | ||
| 675 | } | ||
| 676 | #endif | ||
| 677 | |||
| 678 | if ((extdatalen = ret - p - 2) == 0) | ||
| 679 | return p; | ||
| 680 | |||
| 681 | s2n(extdatalen, p); | ||
| 682 | return ret; | ||
| 683 | } | ||
| 684 | |||
| 685 | unsigned char | ||
| 686 | *ssl_add_serverhello_tlsext(SSL *s, unsigned char *p, unsigned char *limit) | ||
| 687 | { | ||
| 688 | int extdatalen = 0; | ||
| 689 | unsigned char *ret = p; | ||
| 690 | #ifndef OPENSSL_NO_NEXTPROTONEG | ||
| 691 | int next_proto_neg_seen; | ||
| 692 | #endif | ||
| 693 | |||
| 694 | /* don't add extensions for SSLv3, unless doing secure renegotiation */ | ||
| 695 | if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) | ||
| 696 | return p; | ||
| 697 | |||
| 698 | ret += 2; | ||
| 699 | if (ret>=limit) return NULL; /* this really never occurs, but ... */ | ||
| 700 | |||
| 701 | if (!s->hit && s->servername_done == 1 && s->session->tlsext_hostname != NULL) { | ||
| 702 | if ((long)(limit - ret - 4) < 0) return NULL; | ||
| 703 | |||
| 704 | |||
| 705 | s2n(TLSEXT_TYPE_server_name, ret); | ||
| 706 | s2n(0, ret); | ||
| 707 | } | ||
| 708 | |||
| 709 | if (s->s3->send_connection_binding) { | ||
| 710 | int el; | ||
| 711 | |||
| 712 | if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) { | ||
| 713 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | ||
| 714 | return NULL; | ||
| 715 | } | ||
| 716 | |||
| 717 | if ((limit - p - 4 - el) | ||
| 718 | < 0) return NULL; | ||
| 719 | |||
| 720 | s2n(TLSEXT_TYPE_renegotiate, ret); | ||
| 721 | s2n(el, ret); | ||
| 722 | |||
| 723 | if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) { | ||
| 724 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | ||
| 725 | return NULL; | ||
| 726 | } | ||
| 727 | |||
| 728 | ret += el; | ||
| 729 | } | ||
| 730 | |||
| 731 | #ifndef OPENSSL_NO_EC | ||
| 732 | if (s->tlsext_ecpointformatlist != NULL && | ||
| 733 | s->version != DTLS1_VERSION) { | ||
| 734 | /* Add TLS extension ECPointFormats to the ServerHello message */ | ||
| 735 | long lenmax; | ||
| 736 | |||
| 737 | |||
| 738 | if ((lenmax = limit - ret - 5) | ||
| 739 | < 0) return NULL; | ||
| 740 | |||
| 741 | if (s->tlsext_ecpointformatlist_length > (unsigned long)lenmax) return NULL; | ||
| 742 | if (s->tlsext_ecpointformatlist_length > 255) { | ||
| 743 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | ||
| 744 | return NULL; | ||
| 745 | } | ||
| 746 | |||
| 747 | s2n(TLSEXT_TYPE_ec_point_formats, ret); | ||
| 748 | s2n(s->tlsext_ecpointformatlist_length + 1, ret); | ||
| 749 | *(ret++) = (unsigned char) s->tlsext_ecpointformatlist_length; | ||
| 750 | memcpy(ret, s->tlsext_ecpointformatlist, s->tlsext_ecpointformatlist_length); | ||
| 751 | ret += s->tlsext_ecpointformatlist_length; | ||
| 752 | |||
| 753 | } | ||
| 754 | /* Currently the server should not respond with a SupportedCurves extension */ | ||
| 755 | #endif /* OPENSSL_NO_EC */ | ||
| 756 | |||
| 757 | if (s->tlsext_ticket_expected | ||
| 758 | && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) { | ||
| 759 | if ((long)(limit - ret - 4) < 0) return NULL; | ||
| 760 | |||
| 761 | s2n(TLSEXT_TYPE_session_ticket, ret); | ||
| 762 | s2n(0, ret); | ||
| 763 | } | ||
| 764 | |||
| 765 | if (s->tlsext_status_expected) { | ||
| 766 | if ((long)(limit - ret - 4) < 0) return NULL; | ||
| 767 | |||
| 768 | s2n(TLSEXT_TYPE_status_request, ret); | ||
| 769 | s2n(0, ret); | ||
| 770 | } | ||
| 771 | |||
| 772 | #ifdef TLSEXT_TYPE_opaque_prf_input | ||
| 773 | if (s->s3->server_opaque_prf_input != NULL && | ||
| 774 | s->version != DTLS1_VERSION) { | ||
| 775 | size_t sol = s->s3->server_opaque_prf_input_len; | ||
| 776 | |||
| 777 | if ((long)(limit - ret - 6 - sol) < 0) | ||
| 778 | return NULL; | ||
| 779 | if (sol > 0xFFFD) /* can't happen */ | ||
| 780 | return NULL; | ||
| 781 | |||
| 782 | s2n(TLSEXT_TYPE_opaque_prf_input, ret); | ||
| 783 | |||
| 784 | s2n(sol + 2, ret); | ||
| 785 | s2n(sol, ret); | ||
| 786 | memcpy(ret, s->s3->server_opaque_prf_input, sol); | ||
| 787 | ret += sol; | ||
| 788 | } | ||
| 789 | #endif | ||
| 790 | |||
| 791 | #ifndef OPENSSL_NO_SRTP | ||
| 792 | if (s->srtp_profile) { | ||
| 793 | int el; | ||
| 794 | |||
| 795 | ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0); | ||
| 796 | |||
| 797 | if ((limit - p - 4 - el) < 0) | ||
| 798 | return NULL; | ||
| 799 | |||
| 800 | s2n(TLSEXT_TYPE_use_srtp, ret); | ||
| 801 | s2n(el, ret); | ||
| 802 | |||
| 803 | if (ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) { | ||
| 804 | SSLerr(SSL_F_SSL_ADD_SERVERHELLO_TLSEXT, ERR_R_INTERNAL_ERROR); | ||
| 805 | return NULL; | ||
| 806 | } | ||
| 807 | ret += el; | ||
| 808 | } | ||
| 809 | #endif | ||
| 810 | |||
| 811 | if (((s->s3->tmp.new_cipher->id & 0xFFFF) == 0x80 || (s->s3->tmp.new_cipher->id & 0xFFFF) == 0x81) | ||
| 812 | && (SSL_get_options(s) & SSL_OP_CRYPTOPRO_TLSEXT_BUG)) { | ||
| 813 | const unsigned char cryptopro_ext[36] = { | ||
| 814 | 0xfd, 0xe8, /*65000*/ | ||
| 815 | 0x00, 0x20, /*32 bytes length*/ | ||
| 816 | 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85, | ||
| 817 | 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06, | ||
| 818 | 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08, | ||
| 819 | 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17 | ||
| 820 | }; | ||
| 821 | if (limit - ret < 36) | ||
| 822 | return NULL; | ||
| 823 | memcpy(ret, cryptopro_ext, 36); | ||
| 824 | ret += 36; | ||
| 825 | } | ||
| 826 | |||
| 827 | #ifndef OPENSSL_NO_NEXTPROTONEG | ||
| 828 | next_proto_neg_seen = s->s3->next_proto_neg_seen; | ||
| 829 | s->s3->next_proto_neg_seen = 0; | ||
| 830 | if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) { | ||
| 831 | const unsigned char *npa; | ||
| 832 | unsigned int npalen; | ||
| 833 | int r; | ||
| 834 | |||
| 835 | r = s->ctx->next_protos_advertised_cb(s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg); | ||
| 836 | if (r == SSL_TLSEXT_ERR_OK) { | ||
| 837 | if ((long)(limit - ret - 4 - npalen) < 0) return NULL; | ||
| 838 | s2n(TLSEXT_TYPE_next_proto_neg, ret); | ||
| 839 | s2n(npalen, ret); | ||
| 840 | memcpy(ret, npa, npalen); | ||
| 841 | ret += npalen; | ||
| 842 | s->s3->next_proto_neg_seen = 1; | ||
| 843 | } | ||
| 844 | } | ||
| 845 | #endif | ||
| 846 | |||
| 847 | if ((extdatalen = ret - p - 2) == 0) | ||
| 848 | return p; | ||
| 849 | |||
| 850 | s2n(extdatalen, p); | ||
| 851 | return ret; | ||
| 852 | } | ||
| 853 | |||
| 854 | #ifndef OPENSSL_NO_EC | ||
| 855 | /* ssl_check_for_safari attempts to fingerprint Safari using OS X | ||
| 856 | * SecureTransport using the TLS extension block in |d|, of length |n|. | ||
| 857 | * Safari, since 10.6, sends exactly these extensions, in this order: | ||
| 858 | * SNI, | ||
| 859 | * elliptic_curves | ||
| 860 | * ec_point_formats | ||
| 861 | * | ||
| 862 | * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8, | ||
| 863 | * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them. | ||
| 864 | * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from | ||
| 865 | * 10.8..10.8.3 (which don't work). | ||
| 866 | */ | ||
| 867 | static void | ||
| 868 | ssl_check_for_safari(SSL *s, const unsigned char *data, const unsigned char *d, int n) { | ||
| 869 | unsigned short type, size; | ||
| 870 | static const unsigned char kSafariExtensionsBlock[] = { | ||
| 871 | 0x00, 0x0a, /* elliptic_curves extension */ | ||
| 872 | 0x00, 0x08, /* 8 bytes */ | ||
| 873 | 0x00, 0x06, /* 6 bytes of curve ids */ | ||
| 874 | 0x00, 0x17, /* P-256 */ | ||
| 875 | 0x00, 0x18, /* P-384 */ | ||
| 876 | 0x00, 0x19, /* P-521 */ | ||
| 877 | |||
| 878 | 0x00, 0x0b, /* ec_point_formats */ | ||
| 879 | 0x00, 0x02, /* 2 bytes */ | ||
| 880 | 0x01, /* 1 point format */ | ||
| 881 | 0x00, /* uncompressed */ | ||
| 882 | }; | ||
| 883 | |||
| 884 | /* The following is only present in TLS 1.2 */ | ||
| 885 | static const unsigned char kSafariTLS12ExtensionsBlock[] = { | ||
| 886 | 0x00, 0x0d, /* signature_algorithms */ | ||
| 887 | 0x00, 0x0c, /* 12 bytes */ | ||
| 888 | 0x00, 0x0a, /* 10 bytes */ | ||
| 889 | 0x05, 0x01, /* SHA-384/RSA */ | ||
| 890 | 0x04, 0x01, /* SHA-256/RSA */ | ||
| 891 | 0x02, 0x01, /* SHA-1/RSA */ | ||
| 892 | 0x04, 0x03, /* SHA-256/ECDSA */ | ||
| 893 | 0x02, 0x03, /* SHA-1/ECDSA */ | ||
| 894 | }; | ||
| 895 | |||
| 896 | if (data >= (d + n - 2)) | ||
| 897 | return; | ||
| 898 | data += 2; | ||
| 899 | |||
| 900 | if (data > (d + n - 4)) | ||
| 901 | return; | ||
| 902 | n2s(data, type); | ||
| 903 | n2s(data, size); | ||
| 904 | |||
| 905 | if (type != TLSEXT_TYPE_server_name) | ||
| 906 | return; | ||
| 907 | |||
| 908 | if (data + size > d + n) | ||
| 909 | return; | ||
| 910 | data += size; | ||
| 911 | |||
| 912 | if (TLS1_get_client_version(s) >= TLS1_2_VERSION) { | ||
| 913 | const size_t len1 = sizeof(kSafariExtensionsBlock); | ||
| 914 | const size_t len2 = sizeof(kSafariTLS12ExtensionsBlock); | ||
| 915 | |||
| 916 | if (data + len1 + len2 != d + n) | ||
| 917 | return; | ||
| 918 | if (memcmp(data, kSafariExtensionsBlock, len1) != 0) | ||
| 919 | return; | ||
| 920 | if (memcmp(data + len1, kSafariTLS12ExtensionsBlock, len2) != 0) | ||
| 921 | return; | ||
| 922 | } else { | ||
| 923 | const size_t len = sizeof(kSafariExtensionsBlock); | ||
| 924 | |||
| 925 | if (data + len != d + n) | ||
| 926 | return; | ||
| 927 | if (memcmp(data, kSafariExtensionsBlock, len) != 0) | ||
| 928 | return; | ||
| 929 | } | ||
| 930 | |||
| 931 | s->s3->is_probably_safari = 1; | ||
| 932 | } | ||
| 933 | #endif /* !OPENSSL_NO_EC */ | ||
| 934 | |||
| 935 | int | ||
| 936 | ssl_parse_clienthello_tlsext(SSL *s, unsigned char **p, unsigned char *d, | ||
| 937 | int n, int *al) | ||
| 938 | { | ||
| 939 | unsigned short type; | ||
| 940 | unsigned short size; | ||
| 941 | unsigned short len; | ||
| 942 | unsigned char *data = *p; | ||
| 943 | int renegotiate_seen = 0; | ||
| 944 | int sigalg_seen = 0; | ||
| 945 | |||
| 946 | s->servername_done = 0; | ||
| 947 | s->tlsext_status_type = -1; | ||
| 948 | #ifndef OPENSSL_NO_NEXTPROTONEG | ||
| 949 | s->s3->next_proto_neg_seen = 0; | ||
| 950 | #endif | ||
| 951 | |||
| 952 | #ifndef OPENSSL_NO_EC | ||
| 953 | if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG) | ||
| 954 | ssl_check_for_safari(s, data, d, n); | ||
| 955 | #endif /* !OPENSSL_NO_EC */ | ||
| 956 | |||
| 957 | if (data >= (d + n - 2)) | ||
| 958 | goto ri_check; | ||
| 959 | n2s(data, len); | ||
| 960 | |||
| 961 | if (data > (d + n - len)) | ||
| 962 | goto ri_check; | ||
| 963 | |||
| 964 | while (data <= (d + n - 4)) { | ||
| 965 | n2s(data, type); | ||
| 966 | n2s(data, size); | ||
| 967 | |||
| 968 | if (data + size > (d + n)) | ||
| 969 | goto ri_check; | ||
| 970 | #if 0 | ||
| 971 | fprintf(stderr, "Received extension type %d size %d\n", type, size); | ||
| 972 | #endif | ||
| 973 | if (s->tlsext_debug_cb) | ||
| 974 | s->tlsext_debug_cb(s, 0, type, data, size, | ||
| 975 | s->tlsext_debug_arg); | ||
| 976 | /* The servername extension is treated as follows: | ||
| 977 | |||
| 978 | - Only the hostname type is supported with a maximum length of 255. | ||
| 979 | - The servername is rejected if too long or if it contains zeros, | ||
| 980 | in which case an fatal alert is generated. | ||
| 981 | - The servername field is maintained together with the session cache. | ||
| 982 | - When a session is resumed, the servername call back invoked in order | ||
| 983 | to allow the application to position itself to the right context. | ||
| 984 | - The servername is acknowledged if it is new for a session or when | ||
| 985 | it is identical to a previously used for the same session. | ||
| 986 | Applications can control the behaviour. They can at any time | ||
| 987 | set a 'desirable' servername for a new SSL object. This can be the | ||
| 988 | case for example with HTTPS when a Host: header field is received and | ||
| 989 | a renegotiation is requested. In this case, a possible servername | ||
| 990 | presented in the new client hello is only acknowledged if it matches | ||
| 991 | the value of the Host: field. | ||
| 992 | - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | ||
| 993 | if they provide for changing an explicit servername context for the session, | ||
| 994 | i.e. when the session has been established with a servername extension. | ||
| 995 | - On session reconnect, the servername extension may be absent. | ||
| 996 | |||
| 997 | */ | ||
| 998 | |||
| 999 | if (type == TLSEXT_TYPE_server_name) { | ||
| 1000 | unsigned char *sdata; | ||
| 1001 | int servname_type; | ||
| 1002 | int dsize; | ||
| 1003 | |||
| 1004 | |||
| 1005 | if (size < 2) { | ||
| 1006 | *al = SSL_AD_DECODE_ERROR; | ||
| 1007 | return 0; | ||
| 1008 | } | ||
| 1009 | n2s(data, dsize); | ||
| 1010 | |||
| 1011 | size -= 2; | ||
| 1012 | if (dsize > size) { | ||
| 1013 | *al = SSL_AD_DECODE_ERROR; | ||
| 1014 | return 0; | ||
| 1015 | } | ||
| 1016 | |||
| 1017 | sdata = data; | ||
| 1018 | while (dsize > 3) { | ||
| 1019 | servname_type = *(sdata++); | ||
| 1020 | |||
| 1021 | n2s(sdata, len); | ||
| 1022 | dsize -= 3; | ||
| 1023 | |||
| 1024 | if (len > dsize) { | ||
| 1025 | *al = SSL_AD_DECODE_ERROR; | ||
| 1026 | return 0; | ||
| 1027 | } | ||
| 1028 | if (s->servername_done == 0) | ||
| 1029 | switch (servname_type) { | ||
| 1030 | case TLSEXT_NAMETYPE_host_name: | ||
| 1031 | if (!s->hit) { | ||
| 1032 | if (s->session->tlsext_hostname) { | ||
| 1033 | *al = SSL_AD_DECODE_ERROR; | ||
| 1034 | return 0; | ||
| 1035 | } | ||
| 1036 | if (len > TLSEXT_MAXLEN_host_name) { | ||
| 1037 | *al = TLS1_AD_UNRECOGNIZED_NAME; | ||
| 1038 | return 0; | ||
| 1039 | } | ||
| 1040 | if ((s->session->tlsext_hostname = OPENSSL_malloc(len + 1)) == NULL) { | ||
| 1041 | *al = TLS1_AD_INTERNAL_ERROR; | ||
| 1042 | return 0; | ||
| 1043 | } | ||
| 1044 | memcpy(s->session->tlsext_hostname, sdata, len); | ||
| 1045 | s->session->tlsext_hostname[len] = '\0'; | ||
| 1046 | if (strlen(s->session->tlsext_hostname) != len) { | ||
| 1047 | OPENSSL_free(s->session->tlsext_hostname); | ||
| 1048 | s->session->tlsext_hostname = NULL; | ||
| 1049 | *al = TLS1_AD_UNRECOGNIZED_NAME; | ||
| 1050 | return 0; | ||
| 1051 | } | ||
| 1052 | s->servername_done = 1; | ||
| 1053 | |||
| 1054 | |||
| 1055 | } else | ||
| 1056 | s->servername_done = s->session->tlsext_hostname | ||
| 1057 | && strlen(s->session->tlsext_hostname) == len | ||
| 1058 | && strncmp(s->session->tlsext_hostname, (char *)sdata, len) == 0; | ||
| 1059 | |||
| 1060 | break; | ||
| 1061 | |||
| 1062 | default: | ||
| 1063 | break; | ||
| 1064 | } | ||
| 1065 | |||
| 1066 | dsize -= len; | ||
| 1067 | } | ||
| 1068 | if (dsize != 0) { | ||
| 1069 | *al = SSL_AD_DECODE_ERROR; | ||
| 1070 | return 0; | ||
| 1071 | } | ||
| 1072 | |||
| 1073 | } | ||
| 1074 | #ifndef OPENSSL_NO_SRP | ||
| 1075 | else if (type == TLSEXT_TYPE_srp) { | ||
| 1076 | if (size <= 0 || ((len = data[0])) != (size - 1)) { | ||
| 1077 | *al = SSL_AD_DECODE_ERROR; | ||
| 1078 | return 0; | ||
| 1079 | } | ||
| 1080 | if (s->srp_ctx.login != NULL) { | ||
| 1081 | *al = SSL_AD_DECODE_ERROR; | ||
| 1082 | return 0; | ||
| 1083 | } | ||
| 1084 | if ((s->srp_ctx.login = OPENSSL_malloc(len + 1)) == NULL) | ||
| 1085 | return -1; | ||
| 1086 | memcpy(s->srp_ctx.login, &data[1], len); | ||
| 1087 | s->srp_ctx.login[len] = '\0'; | ||
| 1088 | |||
| 1089 | if (strlen(s->srp_ctx.login) != len) { | ||
| 1090 | *al = SSL_AD_DECODE_ERROR; | ||
| 1091 | return 0; | ||
| 1092 | } | ||
| 1093 | } | ||
| 1094 | #endif | ||
| 1095 | |||
| 1096 | #ifndef OPENSSL_NO_EC | ||
| 1097 | else if (type == TLSEXT_TYPE_ec_point_formats && | ||
| 1098 | s->version != DTLS1_VERSION) { | ||
| 1099 | unsigned char *sdata = data; | ||
| 1100 | int ecpointformatlist_length = *(sdata++); | ||
| 1101 | |||
| 1102 | if (ecpointformatlist_length != size - 1) { | ||
| 1103 | *al = TLS1_AD_DECODE_ERROR; | ||
| 1104 | return 0; | ||
| 1105 | } | ||
| 1106 | if (!s->hit) { | ||
| 1107 | if (s->session->tlsext_ecpointformatlist) { | ||
| 1108 | OPENSSL_free(s->session->tlsext_ecpointformatlist); | ||
| 1109 | s->session->tlsext_ecpointformatlist = NULL; | ||
| 1110 | } | ||
| 1111 | s->session->tlsext_ecpointformatlist_length = 0; | ||
| 1112 | if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL) { | ||
| 1113 | *al = TLS1_AD_INTERNAL_ERROR; | ||
| 1114 | return 0; | ||
| 1115 | } | ||
| 1116 | s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length; | ||
| 1117 | memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length); | ||
| 1118 | } | ||
| 1119 | #if 0 | ||
| 1120 | fprintf(stderr, "ssl_parse_clienthello_tlsext s->session->tlsext_ecpointformatlist (length=%i) ", s->session->tlsext_ecpointformatlist_length); | ||
| 1121 | sdata = s->session->tlsext_ecpointformatlist; | ||
| 1122 | for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) | ||
| 1123 | fprintf(stderr, "%i ", *(sdata++)); | ||
| 1124 | fprintf(stderr, "\n"); | ||
| 1125 | #endif | ||
| 1126 | } else if (type == TLSEXT_TYPE_elliptic_curves && | ||
| 1127 | s->version != DTLS1_VERSION) { | ||
| 1128 | unsigned char *sdata = data; | ||
| 1129 | int ellipticcurvelist_length = (*(sdata++) << 8); | ||
| 1130 | ellipticcurvelist_length += (*(sdata++)); | ||
| 1131 | |||
| 1132 | if (ellipticcurvelist_length != size - 2 || | ||
| 1133 | ellipticcurvelist_length < 1) { | ||
| 1134 | *al = TLS1_AD_DECODE_ERROR; | ||
| 1135 | return 0; | ||
| 1136 | } | ||
| 1137 | if (!s->hit) { | ||
| 1138 | if (s->session->tlsext_ellipticcurvelist) { | ||
| 1139 | *al = TLS1_AD_DECODE_ERROR; | ||
| 1140 | return 0; | ||
| 1141 | } | ||
| 1142 | s->session->tlsext_ellipticcurvelist_length = 0; | ||
| 1143 | if ((s->session->tlsext_ellipticcurvelist = OPENSSL_malloc(ellipticcurvelist_length)) == NULL) { | ||
| 1144 | *al = TLS1_AD_INTERNAL_ERROR; | ||
| 1145 | return 0; | ||
| 1146 | } | ||
| 1147 | s->session->tlsext_ellipticcurvelist_length = ellipticcurvelist_length; | ||
| 1148 | memcpy(s->session->tlsext_ellipticcurvelist, sdata, ellipticcurvelist_length); | ||
| 1149 | } | ||
| 1150 | #if 0 | ||
| 1151 | fprintf(stderr, "ssl_parse_clienthello_tlsext s->session->tlsext_ellipticcurvelist (length=%i) ", s->session->tlsext_ellipticcurvelist_length); | ||
| 1152 | sdata = s->session->tlsext_ellipticcurvelist; | ||
| 1153 | for (i = 0; i < s->session->tlsext_ellipticcurvelist_length; i++) | ||
| 1154 | fprintf(stderr, "%i ", *(sdata++)); | ||
| 1155 | fprintf(stderr, "\n"); | ||
| 1156 | #endif | ||
| 1157 | } | ||
| 1158 | #endif /* OPENSSL_NO_EC */ | ||
| 1159 | #ifdef TLSEXT_TYPE_opaque_prf_input | ||
| 1160 | else if (type == TLSEXT_TYPE_opaque_prf_input && | ||
| 1161 | s->version != DTLS1_VERSION) { | ||
| 1162 | unsigned char *sdata = data; | ||
| 1163 | |||
| 1164 | if (size < 2) { | ||
| 1165 | *al = SSL_AD_DECODE_ERROR; | ||
| 1166 | return 0; | ||
| 1167 | } | ||
| 1168 | n2s(sdata, s->s3->client_opaque_prf_input_len); | ||
| 1169 | if (s->s3->client_opaque_prf_input_len != size - 2) { | ||
| 1170 | *al = SSL_AD_DECODE_ERROR; | ||
| 1171 | return 0; | ||
| 1172 | } | ||
| 1173 | |||
| 1174 | if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */ | ||
| 1175 | OPENSSL_free(s->s3->client_opaque_prf_input); | ||
| 1176 | if (s->s3->client_opaque_prf_input_len == 0) | ||
| 1177 | s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */ | ||
| 1178 | else | ||
| 1179 | s->s3->client_opaque_prf_input = BUF_memdup(sdata, s->s3->client_opaque_prf_input_len); | ||
| 1180 | if (s->s3->client_opaque_prf_input == NULL) { | ||
| 1181 | *al = TLS1_AD_INTERNAL_ERROR; | ||
| 1182 | return 0; | ||
| 1183 | } | ||
| 1184 | } | ||
| 1185 | #endif | ||
| 1186 | else if (type == TLSEXT_TYPE_session_ticket) { | ||
| 1187 | if (s->tls_session_ticket_ext_cb && | ||
| 1188 | !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg)) { | ||
| 1189 | *al = TLS1_AD_INTERNAL_ERROR; | ||
| 1190 | return 0; | ||
| 1191 | } | ||
| 1192 | } else if (type == TLSEXT_TYPE_renegotiate) { | ||
| 1193 | if (!ssl_parse_clienthello_renegotiate_ext(s, data, size, al)) | ||
| 1194 | return 0; | ||
| 1195 | renegotiate_seen = 1; | ||
| 1196 | } else if (type == TLSEXT_TYPE_signature_algorithms) { | ||
| 1197 | int dsize; | ||
| 1198 | if (sigalg_seen || size < 2) { | ||
| 1199 | *al = SSL_AD_DECODE_ERROR; | ||
| 1200 | return 0; | ||
| 1201 | } | ||
| 1202 | sigalg_seen = 1; | ||
| 1203 | n2s(data, dsize); | ||
| 1204 | size -= 2; | ||
| 1205 | if (dsize != size || dsize & 1) { | ||
| 1206 | *al = SSL_AD_DECODE_ERROR; | ||
| 1207 | return 0; | ||
| 1208 | } | ||
| 1209 | if (!tls1_process_sigalgs(s, data, dsize)) { | ||
| 1210 | *al = SSL_AD_DECODE_ERROR; | ||
| 1211 | return 0; | ||
| 1212 | } | ||
| 1213 | } else if (type == TLSEXT_TYPE_status_request && | ||
| 1214 | s->version != DTLS1_VERSION) { | ||
| 1215 | |||
| 1216 | if (size < 5) { | ||
| 1217 | *al = SSL_AD_DECODE_ERROR; | ||
| 1218 | return 0; | ||
| 1219 | } | ||
| 1220 | |||
| 1221 | s->tlsext_status_type = *data++; | ||
| 1222 | size--; | ||
| 1223 | if (s->tlsext_status_type == TLSEXT_STATUSTYPE_ocsp) { | ||
| 1224 | const unsigned char *sdata; | ||
| 1225 | int dsize; | ||
| 1226 | /* Read in responder_id_list */ | ||
| 1227 | n2s(data, dsize); | ||
| 1228 | size -= 2; | ||
| 1229 | if (dsize > size ) { | ||
| 1230 | *al = SSL_AD_DECODE_ERROR; | ||
| 1231 | return 0; | ||
| 1232 | } | ||
| 1233 | while (dsize > 0) { | ||
| 1234 | OCSP_RESPID *id; | ||
| 1235 | int idsize; | ||
| 1236 | if (dsize < 4) { | ||
| 1237 | *al = SSL_AD_DECODE_ERROR; | ||
| 1238 | return 0; | ||
| 1239 | } | ||
| 1240 | n2s(data, idsize); | ||
| 1241 | dsize -= 2 + idsize; | ||
| 1242 | size -= 2 + idsize; | ||
| 1243 | if (dsize < 0) { | ||
| 1244 | *al = SSL_AD_DECODE_ERROR; | ||
| 1245 | return 0; | ||
| 1246 | } | ||
| 1247 | sdata = data; | ||
| 1248 | data += idsize; | ||
| 1249 | id = d2i_OCSP_RESPID(NULL, | ||
| 1250 | &sdata, idsize); | ||
| 1251 | if (!id) { | ||
| 1252 | *al = SSL_AD_DECODE_ERROR; | ||
| 1253 | return 0; | ||
| 1254 | } | ||
| 1255 | if (data != sdata) { | ||
| 1256 | OCSP_RESPID_free(id); | ||
| 1257 | *al = SSL_AD_DECODE_ERROR; | ||
| 1258 | return 0; | ||
| 1259 | } | ||
| 1260 | if (!s->tlsext_ocsp_ids | ||
| 1261 | && !(s->tlsext_ocsp_ids = | ||
| 1262 | sk_OCSP_RESPID_new_null())) { | ||
| 1263 | OCSP_RESPID_free(id); | ||
| 1264 | *al = SSL_AD_INTERNAL_ERROR; | ||
| 1265 | return 0; | ||
| 1266 | } | ||
| 1267 | if (!sk_OCSP_RESPID_push( | ||
| 1268 | s->tlsext_ocsp_ids, id)) { | ||
| 1269 | OCSP_RESPID_free(id); | ||
| 1270 | *al = SSL_AD_INTERNAL_ERROR; | ||
| 1271 | return 0; | ||
| 1272 | } | ||
| 1273 | } | ||
| 1274 | |||
| 1275 | /* Read in request_extensions */ | ||
| 1276 | if (size < 2) { | ||
| 1277 | *al = SSL_AD_DECODE_ERROR; | ||
| 1278 | return 0; | ||
| 1279 | } | ||
| 1280 | n2s(data, dsize); | ||
| 1281 | size -= 2; | ||
| 1282 | if (dsize != size) { | ||
| 1283 | *al = SSL_AD_DECODE_ERROR; | ||
| 1284 | return 0; | ||
| 1285 | } | ||
| 1286 | sdata = data; | ||
| 1287 | if (dsize > 0) { | ||
| 1288 | if (s->tlsext_ocsp_exts) { | ||
| 1289 | sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, | ||
| 1290 | X509_EXTENSION_free); | ||
| 1291 | } | ||
| 1292 | |||
| 1293 | s->tlsext_ocsp_exts = | ||
| 1294 | d2i_X509_EXTENSIONS(NULL, | ||
| 1295 | &sdata, dsize); | ||
| 1296 | if (!s->tlsext_ocsp_exts | ||
| 1297 | || (data + dsize != sdata)) { | ||
| 1298 | *al = SSL_AD_DECODE_ERROR; | ||
| 1299 | return 0; | ||
| 1300 | } | ||
| 1301 | } | ||
| 1302 | } | ||
| 1303 | /* We don't know what to do with any other type | ||
| 1304 | * so ignore it. | ||
| 1305 | */ | ||
| 1306 | else | ||
| 1307 | s->tlsext_status_type = -1; | ||
| 1308 | } | ||
| 1309 | #ifndef OPENSSL_NO_NEXTPROTONEG | ||
| 1310 | else if (type == TLSEXT_TYPE_next_proto_neg && | ||
| 1311 | s->s3->tmp.finish_md_len == 0) { | ||
| 1312 | /* We shouldn't accept this extension on a | ||
| 1313 | * renegotiation. | ||
| 1314 | * | ||
| 1315 | * s->new_session will be set on renegotiation, but we | ||
| 1316 | * probably shouldn't rely that it couldn't be set on | ||
| 1317 | * the initial renegotation too in certain cases (when | ||
| 1318 | * there's some other reason to disallow resuming an | ||
| 1319 | * earlier session -- the current code won't be doing | ||
| 1320 | * anything like that, but this might change). | ||
| 1321 | |||
| 1322 | * A valid sign that there's been a previous handshake | ||
| 1323 | * in this connection is if s->s3->tmp.finish_md_len > | ||
| 1324 | * 0. (We are talking about a check that will happen | ||
| 1325 | * in the Hello protocol round, well before a new | ||
| 1326 | * Finished message could have been computed.) */ | ||
| 1327 | s->s3->next_proto_neg_seen = 1; | ||
| 1328 | } | ||
| 1329 | #endif | ||
| 1330 | |||
| 1331 | /* session ticket processed earlier */ | ||
| 1332 | #ifndef OPENSSL_NO_SRTP | ||
| 1333 | else if (type == TLSEXT_TYPE_use_srtp) { | ||
| 1334 | if (ssl_parse_clienthello_use_srtp_ext(s, data, size, | ||
| 1335 | al)) | ||
| 1336 | return 0; | ||
| 1337 | } | ||
| 1338 | #endif | ||
| 1339 | |||
| 1340 | data += size; | ||
| 1341 | } | ||
| 1342 | |||
| 1343 | *p = data; | ||
| 1344 | |||
| 1345 | ri_check: | ||
| 1346 | |||
| 1347 | /* Need RI if renegotiating */ | ||
| 1348 | |||
| 1349 | if (!renegotiate_seen && s->renegotiate && | ||
| 1350 | !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) { | ||
| 1351 | *al = SSL_AD_HANDSHAKE_FAILURE; | ||
| 1352 | SSLerr(SSL_F_SSL_PARSE_CLIENTHELLO_TLSEXT, | ||
| 1353 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); | ||
| 1354 | return 0; | ||
| 1355 | } | ||
| 1356 | |||
| 1357 | return 1; | ||
| 1358 | } | ||
| 1359 | |||
| 1360 | #ifndef OPENSSL_NO_NEXTPROTONEG | ||
| 1361 | /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No | ||
| 1362 | * elements of zero length are allowed and the set of elements must exactly fill | ||
| 1363 | * the length of the block. */ | ||
| 1364 | static char | ||
| 1365 | ssl_next_proto_validate(unsigned char *d, unsigned len) | ||
| 1366 | { | ||
| 1367 | unsigned int off = 0; | ||
| 1368 | |||
| 1369 | while (off < len) { | ||
| 1370 | if (d[off] == 0) | ||
| 1371 | return 0; | ||
| 1372 | off += d[off]; | ||
| 1373 | off++; | ||
| 1374 | } | ||
| 1375 | |||
| 1376 | return off == len; | ||
| 1377 | } | ||
| 1378 | #endif | ||
| 1379 | |||
| 1380 | int | ||
| 1381 | ssl_parse_serverhello_tlsext(SSL *s, unsigned char **p, unsigned char *d, int n, int *al) | ||
| 1382 | { | ||
| 1383 | unsigned short length; | ||
| 1384 | unsigned short type; | ||
| 1385 | unsigned short size; | ||
| 1386 | unsigned char *data = *p; | ||
| 1387 | int tlsext_servername = 0; | ||
| 1388 | int renegotiate_seen = 0; | ||
| 1389 | |||
| 1390 | #ifndef OPENSSL_NO_NEXTPROTONEG | ||
| 1391 | s->s3->next_proto_neg_seen = 0; | ||
| 1392 | #endif | ||
| 1393 | |||
| 1394 | if (data >= (d + n - 2)) | ||
| 1395 | goto ri_check; | ||
| 1396 | |||
| 1397 | n2s(data, length); | ||
| 1398 | if (data + length != d + n) { | ||
| 1399 | *al = SSL_AD_DECODE_ERROR; | ||
| 1400 | return 0; | ||
| 1401 | } | ||
| 1402 | |||
| 1403 | while (data <= (d + n - 4)) { | ||
| 1404 | n2s(data, type); | ||
| 1405 | n2s(data, size); | ||
| 1406 | |||
| 1407 | if (data + size > (d + n)) | ||
| 1408 | goto ri_check; | ||
| 1409 | |||
| 1410 | if (s->tlsext_debug_cb) | ||
| 1411 | s->tlsext_debug_cb(s, 1, type, data, size, | ||
| 1412 | s->tlsext_debug_arg); | ||
| 1413 | |||
| 1414 | if (type == TLSEXT_TYPE_server_name) { | ||
| 1415 | if (s->tlsext_hostname == NULL || size > 0) { | ||
| 1416 | *al = TLS1_AD_UNRECOGNIZED_NAME; | ||
| 1417 | return 0; | ||
| 1418 | } | ||
| 1419 | tlsext_servername = 1; | ||
| 1420 | |||
| 1421 | } | ||
| 1422 | |||
| 1423 | #ifndef OPENSSL_NO_EC | ||
| 1424 | else if (type == TLSEXT_TYPE_ec_point_formats && | ||
| 1425 | s->version != DTLS1_VERSION) { | ||
| 1426 | unsigned char *sdata = data; | ||
| 1427 | int ecpointformatlist_length = *(sdata++); | ||
| 1428 | |||
| 1429 | if (ecpointformatlist_length != size - 1 || | ||
| 1430 | ecpointformatlist_length < 1) { | ||
| 1431 | *al = TLS1_AD_DECODE_ERROR; | ||
| 1432 | return 0; | ||
| 1433 | } | ||
| 1434 | s->session->tlsext_ecpointformatlist_length = 0; | ||
| 1435 | if (s->session->tlsext_ecpointformatlist != NULL) | ||
| 1436 | OPENSSL_free(s->session->tlsext_ecpointformatlist); | ||
| 1437 | if ((s->session->tlsext_ecpointformatlist = OPENSSL_malloc(ecpointformatlist_length)) == NULL) { | ||
| 1438 | *al = TLS1_AD_INTERNAL_ERROR; | ||
| 1439 | return 0; | ||
| 1440 | } | ||
| 1441 | s->session->tlsext_ecpointformatlist_length = ecpointformatlist_length; | ||
| 1442 | memcpy(s->session->tlsext_ecpointformatlist, sdata, ecpointformatlist_length); | ||
| 1443 | #if 0 | ||
| 1444 | fprintf(stderr, "ssl_parse_serverhello_tlsext s->session->tlsext_ecpointformatlist "); | ||
| 1445 | sdata = s->session->tlsext_ecpointformatlist; | ||
| 1446 | for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) | ||
| 1447 | fprintf(stderr, "%i ", *(sdata++)); | ||
| 1448 | fprintf(stderr, "\n"); | ||
| 1449 | #endif | ||
| 1450 | } | ||
| 1451 | #endif /* OPENSSL_NO_EC */ | ||
| 1452 | |||
| 1453 | else if (type == TLSEXT_TYPE_session_ticket) { | ||
| 1454 | if (s->tls_session_ticket_ext_cb && | ||
| 1455 | !s->tls_session_ticket_ext_cb(s, data, size, s->tls_session_ticket_ext_cb_arg)) { | ||
| 1456 | *al = TLS1_AD_INTERNAL_ERROR; | ||
| 1457 | return 0; | ||
| 1458 | } | ||
| 1459 | if ((SSL_get_options(s) & SSL_OP_NO_TICKET) | ||
| 1460 | || (size > 0)) { | ||
| 1461 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; | ||
| 1462 | return 0; | ||
| 1463 | } | ||
| 1464 | s->tlsext_ticket_expected = 1; | ||
| 1465 | } | ||
| 1466 | #ifdef TLSEXT_TYPE_opaque_prf_input | ||
| 1467 | else if (type == TLSEXT_TYPE_opaque_prf_input && | ||
| 1468 | s->version != DTLS1_VERSION) { | ||
| 1469 | unsigned char *sdata = data; | ||
| 1470 | |||
| 1471 | if (size < 2) { | ||
| 1472 | *al = SSL_AD_DECODE_ERROR; | ||
| 1473 | return 0; | ||
| 1474 | } | ||
| 1475 | n2s(sdata, s->s3->server_opaque_prf_input_len); | ||
| 1476 | if (s->s3->server_opaque_prf_input_len != size - 2) { | ||
| 1477 | *al = SSL_AD_DECODE_ERROR; | ||
| 1478 | return 0; | ||
| 1479 | } | ||
| 1480 | |||
| 1481 | if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */ | ||
| 1482 | OPENSSL_free(s->s3->server_opaque_prf_input); | ||
| 1483 | if (s->s3->server_opaque_prf_input_len == 0) | ||
| 1484 | s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */ | ||
| 1485 | else | ||
| 1486 | s->s3->server_opaque_prf_input = BUF_memdup(sdata, s->s3->server_opaque_prf_input_len); | ||
| 1487 | |||
| 1488 | if (s->s3->server_opaque_prf_input == NULL) { | ||
| 1489 | *al = TLS1_AD_INTERNAL_ERROR; | ||
| 1490 | return 0; | ||
| 1491 | } | ||
| 1492 | } | ||
| 1493 | #endif | ||
| 1494 | else if (type == TLSEXT_TYPE_status_request && | ||
| 1495 | s->version != DTLS1_VERSION) { | ||
| 1496 | /* MUST be empty and only sent if we've requested | ||
| 1497 | * a status request message. | ||
| 1498 | */ | ||
| 1499 | if ((s->tlsext_status_type == -1) || (size > 0)) { | ||
| 1500 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; | ||
| 1501 | return 0; | ||
| 1502 | } | ||
| 1503 | /* Set flag to expect CertificateStatus message */ | ||
| 1504 | s->tlsext_status_expected = 1; | ||
| 1505 | } | ||
| 1506 | #ifndef OPENSSL_NO_NEXTPROTONEG | ||
| 1507 | else if (type == TLSEXT_TYPE_next_proto_neg && | ||
| 1508 | s->s3->tmp.finish_md_len == 0) { | ||
| 1509 | unsigned char *selected; | ||
| 1510 | unsigned char selected_len; | ||
| 1511 | |||
| 1512 | /* We must have requested it. */ | ||
| 1513 | if (s->ctx->next_proto_select_cb == NULL) { | ||
| 1514 | *al = TLS1_AD_UNSUPPORTED_EXTENSION; | ||
| 1515 | return 0; | ||
| 1516 | } | ||
| 1517 | /* The data must be valid */ | ||
| 1518 | if (!ssl_next_proto_validate(data, size)) { | ||
| 1519 | *al = TLS1_AD_DECODE_ERROR; | ||
| 1520 | return 0; | ||
| 1521 | } | ||
| 1522 | if (s->ctx->next_proto_select_cb(s, &selected, &selected_len, data, size, s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) { | ||
| 1523 | *al = TLS1_AD_INTERNAL_ERROR; | ||
| 1524 | return 0; | ||
| 1525 | } | ||
| 1526 | s->next_proto_negotiated = OPENSSL_malloc(selected_len); | ||
| 1527 | if (!s->next_proto_negotiated) { | ||
| 1528 | *al = TLS1_AD_INTERNAL_ERROR; | ||
| 1529 | return 0; | ||
| 1530 | } | ||
| 1531 | memcpy(s->next_proto_negotiated, selected, selected_len); | ||
| 1532 | s->next_proto_negotiated_len = selected_len; | ||
| 1533 | s->s3->next_proto_neg_seen = 1; | ||
| 1534 | } | ||
| 1535 | #endif | ||
| 1536 | else if (type == TLSEXT_TYPE_renegotiate) { | ||
| 1537 | if (!ssl_parse_serverhello_renegotiate_ext(s, data, size, al)) | ||
| 1538 | return 0; | ||
| 1539 | renegotiate_seen = 1; | ||
| 1540 | } | ||
| 1541 | #ifndef OPENSSL_NO_SRTP | ||
| 1542 | else if (type == TLSEXT_TYPE_use_srtp) { | ||
| 1543 | if (ssl_parse_serverhello_use_srtp_ext(s, data, size, | ||
| 1544 | al)) | ||
| 1545 | return 0; | ||
| 1546 | } | ||
| 1547 | #endif | ||
| 1548 | |||
| 1549 | data += size; | ||
| 1550 | |||
| 1551 | } | ||
| 1552 | |||
| 1553 | if (data != d + n) { | ||
| 1554 | *al = SSL_AD_DECODE_ERROR; | ||
| 1555 | return 0; | ||
| 1556 | } | ||
| 1557 | |||
| 1558 | if (!s->hit && tlsext_servername == 1) { | ||
| 1559 | if (s->tlsext_hostname) { | ||
| 1560 | if (s->session->tlsext_hostname == NULL) { | ||
| 1561 | s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname); | ||
| 1562 | |||
| 1563 | if (!s->session->tlsext_hostname) { | ||
| 1564 | *al = SSL_AD_UNRECOGNIZED_NAME; | ||
| 1565 | return 0; | ||
| 1566 | } | ||
| 1567 | } else { | ||
| 1568 | *al = SSL_AD_DECODE_ERROR; | ||
| 1569 | return 0; | ||
| 1570 | } | ||
| 1571 | } | ||
| 1572 | } | ||
| 1573 | |||
| 1574 | *p = data; | ||
| 1575 | |||
| 1576 | ri_check: | ||
| 1577 | |||
| 1578 | /* Determine if we need to see RI. Strictly speaking if we want to | ||
| 1579 | * avoid an attack we should *always* see RI even on initial server | ||
| 1580 | * hello because the client doesn't see any renegotiation during an | ||
| 1581 | * attack. However this would mean we could not connect to any server | ||
| 1582 | * which doesn't support RI so for the immediate future tolerate RI | ||
| 1583 | * absence on initial connect only. | ||
| 1584 | */ | ||
| 1585 | if (!renegotiate_seen | ||
| 1586 | && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) | ||
| 1587 | && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) { | ||
| 1588 | *al = SSL_AD_HANDSHAKE_FAILURE; | ||
| 1589 | SSLerr(SSL_F_SSL_PARSE_SERVERHELLO_TLSEXT, | ||
| 1590 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); | ||
| 1591 | return 0; | ||
| 1592 | } | ||
| 1593 | |||
| 1594 | return 1; | ||
| 1595 | } | ||
| 1596 | |||
| 1597 | |||
| 1598 | int | ||
| 1599 | ssl_prepare_clienthello_tlsext(SSL *s) | ||
| 1600 | { | ||
| 1601 | #ifndef OPENSSL_NO_EC | ||
| 1602 | /* If we are client and using an elliptic curve cryptography cipher suite, send the point formats | ||
| 1603 | * and elliptic curves we support. | ||
| 1604 | */ | ||
| 1605 | int using_ecc = 0; | ||
| 1606 | int i; | ||
| 1607 | unsigned char *j; | ||
| 1608 | unsigned long alg_k, alg_a; | ||
| 1609 | STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s); | ||
| 1610 | |||
| 1611 | for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) { | ||
| 1612 | SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i); | ||
| 1613 | |||
| 1614 | alg_k = c->algorithm_mkey; | ||
| 1615 | alg_a = c->algorithm_auth; | ||
| 1616 | if ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe) || (alg_a & SSL_aECDSA))) { | ||
| 1617 | using_ecc = 1; | ||
| 1618 | break; | ||
| 1619 | } | ||
| 1620 | } | ||
| 1621 | using_ecc = using_ecc && (s->version >= TLS1_VERSION); | ||
| 1622 | if (using_ecc) { | ||
| 1623 | if (s->tlsext_ecpointformatlist != NULL) | ||
| 1624 | OPENSSL_free(s->tlsext_ecpointformatlist); | ||
| 1625 | if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL) { | ||
| 1626 | SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT, ERR_R_MALLOC_FAILURE); | ||
| 1627 | return -1; | ||
| 1628 | } | ||
| 1629 | s->tlsext_ecpointformatlist_length = 3; | ||
| 1630 | s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed; | ||
| 1631 | s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; | ||
| 1632 | s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; | ||
| 1633 | |||
| 1634 | /* we support all named elliptic curves in draft-ietf-tls-ecc-12 */ | ||
| 1635 | if (s->tlsext_ellipticcurvelist != NULL) | ||
| 1636 | OPENSSL_free(s->tlsext_ellipticcurvelist); | ||
| 1637 | s->tlsext_ellipticcurvelist_length = sizeof(pref_list)/sizeof(pref_list[0]) * 2; | ||
| 1638 | if ((s->tlsext_ellipticcurvelist = OPENSSL_malloc(s->tlsext_ellipticcurvelist_length)) == NULL) { | ||
| 1639 | s->tlsext_ellipticcurvelist_length = 0; | ||
| 1640 | SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT, ERR_R_MALLOC_FAILURE); | ||
| 1641 | return -1; | ||
| 1642 | } | ||
| 1643 | for (i = 0, j = s->tlsext_ellipticcurvelist; | ||
| 1644 | (unsigned int)i < | ||
| 1645 | sizeof(pref_list)/sizeof(pref_list[0]); | ||
| 1646 | i++) { | ||
| 1647 | int id = tls1_ec_nid2curve_id(pref_list[i]); | ||
| 1648 | s2n(id, j); | ||
| 1649 | } | ||
| 1650 | } | ||
| 1651 | #endif /* OPENSSL_NO_EC */ | ||
| 1652 | |||
| 1653 | #ifdef TLSEXT_TYPE_opaque_prf_input | ||
| 1654 | { | ||
| 1655 | int r = 1; | ||
| 1656 | |||
| 1657 | if (s->ctx->tlsext_opaque_prf_input_callback != 0) { | ||
| 1658 | r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg); | ||
| 1659 | if (!r) | ||
| 1660 | return -1; | ||
| 1661 | } | ||
| 1662 | |||
| 1663 | if (s->tlsext_opaque_prf_input != NULL) { | ||
| 1664 | if (s->s3->client_opaque_prf_input != NULL) /* shouldn't really happen */ | ||
| 1665 | OPENSSL_free(s->s3->client_opaque_prf_input); | ||
| 1666 | |||
| 1667 | if (s->tlsext_opaque_prf_input_len == 0) | ||
| 1668 | s->s3->client_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */ | ||
| 1669 | else | ||
| 1670 | s->s3->client_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len); | ||
| 1671 | if (s->s3->client_opaque_prf_input == NULL) { | ||
| 1672 | SSLerr(SSL_F_SSL_PREPARE_CLIENTHELLO_TLSEXT, ERR_R_MALLOC_FAILURE); | ||
| 1673 | return -1; | ||
| 1674 | } | ||
| 1675 | s->s3->client_opaque_prf_input_len = s->tlsext_opaque_prf_input_len; | ||
| 1676 | } | ||
| 1677 | |||
| 1678 | if (r == 2) | ||
| 1679 | /* at callback's request, insist on receiving an appropriate server opaque PRF input */ | ||
| 1680 | s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len; | ||
| 1681 | } | ||
| 1682 | #endif | ||
| 1683 | |||
| 1684 | return 1; | ||
| 1685 | } | ||
| 1686 | |||
| 1687 | int | ||
| 1688 | ssl_prepare_serverhello_tlsext(SSL *s) | ||
| 1689 | { | ||
| 1690 | #ifndef OPENSSL_NO_EC | ||
| 1691 | /* If we are server and using an ECC cipher suite, send the point formats we support | ||
| 1692 | * if the client sent us an ECPointsFormat extension. Note that the server is not | ||
| 1693 | * supposed to send an EllipticCurves extension. | ||
| 1694 | */ | ||
| 1695 | |||
| 1696 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | ||
| 1697 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; | ||
| 1698 | int using_ecc = (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA); | ||
| 1699 | using_ecc = using_ecc && (s->session->tlsext_ecpointformatlist != NULL); | ||
| 1700 | |||
| 1701 | if (using_ecc) { | ||
| 1702 | if (s->tlsext_ecpointformatlist != NULL) | ||
| 1703 | OPENSSL_free(s->tlsext_ecpointformatlist); | ||
| 1704 | if ((s->tlsext_ecpointformatlist = OPENSSL_malloc(3)) == NULL) { | ||
| 1705 | SSLerr(SSL_F_SSL_PREPARE_SERVERHELLO_TLSEXT, ERR_R_MALLOC_FAILURE); | ||
| 1706 | return -1; | ||
| 1707 | } | ||
| 1708 | s->tlsext_ecpointformatlist_length = 3; | ||
| 1709 | s->tlsext_ecpointformatlist[0] = TLSEXT_ECPOINTFORMAT_uncompressed; | ||
| 1710 | s->tlsext_ecpointformatlist[1] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; | ||
| 1711 | s->tlsext_ecpointformatlist[2] = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_char2; | ||
| 1712 | } | ||
| 1713 | #endif /* OPENSSL_NO_EC */ | ||
| 1714 | |||
| 1715 | return 1; | ||
| 1716 | } | ||
| 1717 | |||
| 1718 | int | ||
| 1719 | ssl_check_clienthello_tlsext_early(SSL *s) | ||
| 1720 | { | ||
| 1721 | int ret = SSL_TLSEXT_ERR_NOACK; | ||
| 1722 | int al = SSL_AD_UNRECOGNIZED_NAME; | ||
| 1723 | |||
| 1724 | #ifndef OPENSSL_NO_EC | ||
| 1725 | /* The handling of the ECPointFormats extension is done elsewhere, namely in | ||
| 1726 | * ssl3_choose_cipher in s3_lib.c. | ||
| 1727 | */ | ||
| 1728 | /* The handling of the EllipticCurves extension is done elsewhere, namely in | ||
| 1729 | * ssl3_choose_cipher in s3_lib.c. | ||
| 1730 | */ | ||
| 1731 | #endif | ||
| 1732 | |||
| 1733 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) | ||
| 1734 | ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg); | ||
| 1735 | else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0) | ||
| 1736 | ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg); | ||
| 1737 | |||
| 1738 | #ifdef TLSEXT_TYPE_opaque_prf_input | ||
| 1739 | { | ||
| 1740 | /* This sort of belongs into ssl_prepare_serverhello_tlsext(), | ||
| 1741 | * but we might be sending an alert in response to the client hello, | ||
| 1742 | * so this has to happen here in | ||
| 1743 | * ssl_check_clienthello_tlsext_early(). */ | ||
| 1744 | |||
| 1745 | int r = 1; | ||
| 1746 | |||
| 1747 | if (s->ctx->tlsext_opaque_prf_input_callback != 0) { | ||
| 1748 | r = s->ctx->tlsext_opaque_prf_input_callback(s, NULL, 0, s->ctx->tlsext_opaque_prf_input_callback_arg); | ||
| 1749 | if (!r) { | ||
| 1750 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; | ||
| 1751 | al = SSL_AD_INTERNAL_ERROR; | ||
| 1752 | goto err; | ||
| 1753 | } | ||
| 1754 | } | ||
| 1755 | |||
| 1756 | if (s->s3->server_opaque_prf_input != NULL) /* shouldn't really happen */ | ||
| 1757 | OPENSSL_free(s->s3->server_opaque_prf_input); | ||
| 1758 | s->s3->server_opaque_prf_input = NULL; | ||
| 1759 | |||
| 1760 | if (s->tlsext_opaque_prf_input != NULL) { | ||
| 1761 | if (s->s3->client_opaque_prf_input != NULL && | ||
| 1762 | s->s3->client_opaque_prf_input_len == s->tlsext_opaque_prf_input_len) { | ||
| 1763 | /* can only use this extension if we have a server opaque PRF input | ||
| 1764 | * of the same length as the client opaque PRF input! */ | ||
| 1765 | |||
| 1766 | if (s->tlsext_opaque_prf_input_len == 0) | ||
| 1767 | s->s3->server_opaque_prf_input = OPENSSL_malloc(1); /* dummy byte just to get non-NULL */ | ||
| 1768 | else | ||
| 1769 | s->s3->server_opaque_prf_input = BUF_memdup(s->tlsext_opaque_prf_input, s->tlsext_opaque_prf_input_len); | ||
| 1770 | if (s->s3->server_opaque_prf_input == NULL) { | ||
| 1771 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; | ||
| 1772 | al = SSL_AD_INTERNAL_ERROR; | ||
| 1773 | goto err; | ||
| 1774 | } | ||
| 1775 | s->s3->server_opaque_prf_input_len = s->tlsext_opaque_prf_input_len; | ||
| 1776 | } | ||
| 1777 | } | ||
| 1778 | |||
| 1779 | if (r == 2 && s->s3->server_opaque_prf_input == NULL) { | ||
| 1780 | /* The callback wants to enforce use of the extension, | ||
| 1781 | * but we can't do that with the client opaque PRF input; | ||
| 1782 | * abort the handshake. | ||
| 1783 | */ | ||
| 1784 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; | ||
| 1785 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
| 1786 | } | ||
| 1787 | } | ||
| 1788 | |||
| 1789 | err: | ||
| 1790 | #endif | ||
| 1791 | switch (ret) { | ||
| 1792 | case SSL_TLSEXT_ERR_ALERT_FATAL: | ||
| 1793 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
| 1794 | |||
| 1795 | return -1; | ||
| 1796 | |||
| 1797 | case SSL_TLSEXT_ERR_ALERT_WARNING: | ||
| 1798 | ssl3_send_alert(s, SSL3_AL_WARNING, al); | ||
| 1799 | return 1; | ||
| 1800 | |||
| 1801 | |||
| 1802 | case SSL_TLSEXT_ERR_NOACK: | ||
| 1803 | s->servername_done = 0; | ||
| 1804 | default: | ||
| 1805 | return 1; | ||
| 1806 | } | ||
| 1807 | } | ||
| 1808 | |||
| 1809 | int | ||
| 1810 | ssl_check_clienthello_tlsext_late(SSL *s) | ||
| 1811 | { | ||
| 1812 | int ret = SSL_TLSEXT_ERR_OK; | ||
| 1813 | int al; | ||
| 1814 | |||
| 1815 | /* If status request then ask callback what to do. | ||
| 1816 | * Note: this must be called after servername callbacks in case | ||
| 1817 | * the certificate has changed, and must be called after the cipher | ||
| 1818 | * has been chosen because this may influence which certificate is sent | ||
| 1819 | */ | ||
| 1820 | if ((s->tlsext_status_type != -1) && s->ctx && s->ctx->tlsext_status_cb) { | ||
| 1821 | int r; | ||
| 1822 | CERT_PKEY *certpkey; | ||
| 1823 | certpkey = ssl_get_server_send_pkey(s); | ||
| 1824 | /* If no certificate can't return certificate status */ | ||
| 1825 | if (certpkey == NULL) { | ||
| 1826 | s->tlsext_status_expected = 0; | ||
| 1827 | return 1; | ||
| 1828 | } | ||
| 1829 | /* Set current certificate to one we will use so | ||
| 1830 | * SSL_get_certificate et al can pick it up. | ||
| 1831 | */ | ||
| 1832 | s->cert->key = certpkey; | ||
| 1833 | r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); | ||
| 1834 | switch (r) { | ||
| 1835 | /* We don't want to send a status request response */ | ||
| 1836 | case SSL_TLSEXT_ERR_NOACK: | ||
| 1837 | s->tlsext_status_expected = 0; | ||
| 1838 | break; | ||
| 1839 | /* status request response should be sent */ | ||
| 1840 | case SSL_TLSEXT_ERR_OK: | ||
| 1841 | if (s->tlsext_ocsp_resp) | ||
| 1842 | s->tlsext_status_expected = 1; | ||
| 1843 | else | ||
| 1844 | s->tlsext_status_expected = 0; | ||
| 1845 | break; | ||
| 1846 | /* something bad happened */ | ||
| 1847 | case SSL_TLSEXT_ERR_ALERT_FATAL: | ||
| 1848 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; | ||
| 1849 | al = SSL_AD_INTERNAL_ERROR; | ||
| 1850 | goto err; | ||
| 1851 | } | ||
| 1852 | } else | ||
| 1853 | s->tlsext_status_expected = 0; | ||
| 1854 | |||
| 1855 | err: | ||
| 1856 | switch (ret) { | ||
| 1857 | case SSL_TLSEXT_ERR_ALERT_FATAL: | ||
| 1858 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
| 1859 | |||
| 1860 | return -1; | ||
| 1861 | |||
| 1862 | case SSL_TLSEXT_ERR_ALERT_WARNING: | ||
| 1863 | ssl3_send_alert(s, SSL3_AL_WARNING, al); | ||
| 1864 | return 1; | ||
| 1865 | |||
| 1866 | |||
| 1867 | default: | ||
| 1868 | return 1; | ||
| 1869 | } | ||
| 1870 | } | ||
| 1871 | |||
| 1872 | int | ||
| 1873 | ssl_check_serverhello_tlsext(SSL *s) | ||
| 1874 | { | ||
| 1875 | int ret = SSL_TLSEXT_ERR_NOACK; | ||
| 1876 | int al = SSL_AD_UNRECOGNIZED_NAME; | ||
| 1877 | |||
| 1878 | #ifndef OPENSSL_NO_EC | ||
| 1879 | /* If we are client and using an elliptic curve cryptography cipher | ||
| 1880 | * suite, then if server returns an EC point formats lists extension | ||
| 1881 | * it must contain uncompressed. | ||
| 1882 | */ | ||
| 1883 | unsigned long alg_k = s->s3->tmp.new_cipher->algorithm_mkey; | ||
| 1884 | unsigned long alg_a = s->s3->tmp.new_cipher->algorithm_auth; | ||
| 1885 | if ((s->tlsext_ecpointformatlist != NULL) && (s->tlsext_ecpointformatlist_length > 0) && | ||
| 1886 | (s->session->tlsext_ecpointformatlist != NULL) && (s->session->tlsext_ecpointformatlist_length > 0) && | ||
| 1887 | ((alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) || (alg_a & SSL_aECDSA))) { | ||
| 1888 | /* we are using an ECC cipher */ | ||
| 1889 | size_t i; | ||
| 1890 | unsigned char *list; | ||
| 1891 | int found_uncompressed = 0; | ||
| 1892 | list = s->session->tlsext_ecpointformatlist; | ||
| 1893 | for (i = 0; i < s->session->tlsext_ecpointformatlist_length; i++) { | ||
| 1894 | if (*(list++) == TLSEXT_ECPOINTFORMAT_uncompressed) { | ||
| 1895 | found_uncompressed = 1; | ||
| 1896 | break; | ||
| 1897 | } | ||
| 1898 | } | ||
| 1899 | if (!found_uncompressed) { | ||
| 1900 | SSLerr(SSL_F_SSL_CHECK_SERVERHELLO_TLSEXT, SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST); | ||
| 1901 | return -1; | ||
| 1902 | } | ||
| 1903 | } | ||
| 1904 | ret = SSL_TLSEXT_ERR_OK; | ||
| 1905 | #endif /* OPENSSL_NO_EC */ | ||
| 1906 | |||
| 1907 | if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) | ||
| 1908 | ret = s->ctx->tlsext_servername_callback(s, &al, s->ctx->tlsext_servername_arg); | ||
| 1909 | else if (s->initial_ctx != NULL && s->initial_ctx->tlsext_servername_callback != 0) | ||
| 1910 | ret = s->initial_ctx->tlsext_servername_callback(s, &al, s->initial_ctx->tlsext_servername_arg); | ||
| 1911 | |||
| 1912 | #ifdef TLSEXT_TYPE_opaque_prf_input | ||
| 1913 | if (s->s3->server_opaque_prf_input_len > 0) { | ||
| 1914 | /* This case may indicate that we, as a client, want to insist on using opaque PRF inputs. | ||
| 1915 | * So first verify that we really have a value from the server too. */ | ||
| 1916 | |||
| 1917 | if (s->s3->server_opaque_prf_input == NULL) { | ||
| 1918 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; | ||
| 1919 | al = SSL_AD_HANDSHAKE_FAILURE; | ||
| 1920 | } | ||
| 1921 | |||
| 1922 | /* Anytime the server *has* sent an opaque PRF input, we need to check | ||
| 1923 | * that we have a client opaque PRF input of the same size. */ | ||
| 1924 | if (s->s3->client_opaque_prf_input == NULL || | ||
| 1925 | s->s3->client_opaque_prf_input_len != s->s3->server_opaque_prf_input_len) { | ||
| 1926 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; | ||
| 1927 | al = SSL_AD_ILLEGAL_PARAMETER; | ||
| 1928 | } | ||
| 1929 | } | ||
| 1930 | #endif | ||
| 1931 | |||
| 1932 | /* If we've requested certificate status and we wont get one | ||
| 1933 | * tell the callback | ||
| 1934 | */ | ||
| 1935 | if ((s->tlsext_status_type != -1) && !(s->tlsext_status_expected) | ||
| 1936 | && s->ctx && s->ctx->tlsext_status_cb) { | ||
| 1937 | int r; | ||
| 1938 | /* Set resp to NULL, resplen to -1 so callback knows | ||
| 1939 | * there is no response. | ||
| 1940 | */ | ||
| 1941 | if (s->tlsext_ocsp_resp) { | ||
| 1942 | OPENSSL_free(s->tlsext_ocsp_resp); | ||
| 1943 | s->tlsext_ocsp_resp = NULL; | ||
| 1944 | } | ||
| 1945 | s->tlsext_ocsp_resplen = -1; | ||
| 1946 | r = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); | ||
| 1947 | if (r == 0) { | ||
| 1948 | al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; | ||
| 1949 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; | ||
| 1950 | } | ||
| 1951 | if (r < 0) { | ||
| 1952 | al = SSL_AD_INTERNAL_ERROR; | ||
| 1953 | ret = SSL_TLSEXT_ERR_ALERT_FATAL; | ||
| 1954 | } | ||
| 1955 | } | ||
| 1956 | |||
| 1957 | switch (ret) { | ||
| 1958 | case SSL_TLSEXT_ERR_ALERT_FATAL: | ||
| 1959 | ssl3_send_alert(s, SSL3_AL_FATAL, al); | ||
| 1960 | |||
| 1961 | return -1; | ||
| 1962 | |||
| 1963 | case SSL_TLSEXT_ERR_ALERT_WARNING: | ||
| 1964 | ssl3_send_alert(s, SSL3_AL_WARNING, al); | ||
| 1965 | return 1; | ||
| 1966 | |||
| 1967 | |||
| 1968 | case SSL_TLSEXT_ERR_NOACK: | ||
| 1969 | s->servername_done = 0; | ||
| 1970 | default: | ||
| 1971 | return 1; | ||
| 1972 | } | ||
| 1973 | } | ||
| 1974 | |||
| 1975 | /* Since the server cache lookup is done early on in the processing of the | ||
| 1976 | * ClientHello, and other operations depend on the result, we need to handle | ||
| 1977 | * any TLS session ticket extension at the same time. | ||
| 1978 | * | ||
| 1979 | * session_id: points at the session ID in the ClientHello. This code will | ||
| 1980 | * read past the end of this in order to parse out the session ticket | ||
| 1981 | * extension, if any. | ||
| 1982 | * len: the length of the session ID. | ||
| 1983 | * limit: a pointer to the first byte after the ClientHello. | ||
| 1984 | * ret: (output) on return, if a ticket was decrypted, then this is set to | ||
| 1985 | * point to the resulting session. | ||
| 1986 | * | ||
| 1987 | * If s->tls_session_secret_cb is set then we are expecting a pre-shared key | ||
| 1988 | * ciphersuite, in which case we have no use for session tickets and one will | ||
| 1989 | * never be decrypted, nor will s->tlsext_ticket_expected be set to 1. | ||
| 1990 | * | ||
| 1991 | * Returns: | ||
| 1992 | * -1: fatal error, either from parsing or decrypting the ticket. | ||
| 1993 | * 0: no ticket was found (or was ignored, based on settings). | ||
| 1994 | * 1: a zero length extension was found, indicating that the client supports | ||
| 1995 | * session tickets but doesn't currently have one to offer. | ||
| 1996 | * 2: either s->tls_session_secret_cb was set, or a ticket was offered but | ||
| 1997 | * couldn't be decrypted because of a non-fatal error. | ||
| 1998 | * 3: a ticket was successfully decrypted and *ret was set. | ||
| 1999 | * | ||
| 2000 | * Side effects: | ||
| 2001 | * Sets s->tlsext_ticket_expected to 1 if the server will have to issue | ||
| 2002 | * a new session ticket to the client because the client indicated support | ||
| 2003 | * (and s->tls_session_secret_cb is NULL) but the client either doesn't have | ||
| 2004 | * a session ticket or we couldn't use the one it gave us, or if | ||
| 2005 | * s->ctx->tlsext_ticket_key_cb asked to renew the client's ticket. | ||
| 2006 | * Otherwise, s->tlsext_ticket_expected is set to 0. | ||
| 2007 | */ | ||
| 2008 | int | ||
| 2009 | tls1_process_ticket(SSL *s, unsigned char *session_id, int len, | ||
| 2010 | const unsigned char *limit, SSL_SESSION **ret) | ||
| 2011 | { | ||
| 2012 | /* Point after session ID in client hello */ | ||
| 2013 | const unsigned char *p = session_id + len; | ||
| 2014 | unsigned short i; | ||
| 2015 | |||
| 2016 | *ret = NULL; | ||
| 2017 | s->tlsext_ticket_expected = 0; | ||
| 2018 | |||
| 2019 | /* If tickets disabled behave as if no ticket present | ||
| 2020 | * to permit stateful resumption. | ||
| 2021 | */ | ||
| 2022 | if (SSL_get_options(s) & SSL_OP_NO_TICKET) | ||
| 2023 | return 0; | ||
| 2024 | if ((s->version <= SSL3_VERSION) || !limit) | ||
| 2025 | return 0; | ||
| 2026 | if (p >= limit) | ||
| 2027 | return -1; | ||
| 2028 | /* Skip past DTLS cookie */ | ||
| 2029 | if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) { | ||
| 2030 | i = *(p++); | ||
| 2031 | p += i; | ||
| 2032 | if (p >= limit) | ||
| 2033 | return -1; | ||
| 2034 | } | ||
| 2035 | /* Skip past cipher list */ | ||
| 2036 | n2s(p, i); | ||
| 2037 | p += i; | ||
| 2038 | if (p >= limit) | ||
| 2039 | return -1; | ||
| 2040 | /* Skip past compression algorithm list */ | ||
| 2041 | i = *(p++); | ||
| 2042 | p += i; | ||
| 2043 | if (p > limit) | ||
| 2044 | return -1; | ||
| 2045 | /* Now at start of extensions */ | ||
| 2046 | if ((p + 2) >= limit) | ||
| 2047 | return 0; | ||
| 2048 | n2s(p, i); | ||
| 2049 | while ((p + 4) <= limit) { | ||
| 2050 | unsigned short type, size; | ||
| 2051 | n2s(p, type); | ||
| 2052 | n2s(p, size); | ||
| 2053 | if (p + size > limit) | ||
| 2054 | return 0; | ||
| 2055 | if (type == TLSEXT_TYPE_session_ticket) { | ||
| 2056 | int r; | ||
| 2057 | if (size == 0) { | ||
| 2058 | /* The client will accept a ticket but doesn't | ||
| 2059 | * currently have one. */ | ||
| 2060 | s->tlsext_ticket_expected = 1; | ||
| 2061 | return 1; | ||
| 2062 | } | ||
| 2063 | if (s->tls_session_secret_cb) { | ||
| 2064 | /* Indicate that the ticket couldn't be | ||
| 2065 | * decrypted rather than generating the session | ||
| 2066 | * from ticket now, trigger abbreviated | ||
| 2067 | * handshake based on external mechanism to | ||
| 2068 | * calculate the master secret later. */ | ||
| 2069 | return 2; | ||
| 2070 | } | ||
| 2071 | r = tls_decrypt_ticket(s, p, size, session_id, len, ret); | ||
| 2072 | switch (r) { | ||
| 2073 | case 2: /* ticket couldn't be decrypted */ | ||
| 2074 | s->tlsext_ticket_expected = 1; | ||
| 2075 | return 2; | ||
| 2076 | case 3: /* ticket was decrypted */ | ||
| 2077 | return r; | ||
| 2078 | case 4: /* ticket decrypted but need to renew */ | ||
| 2079 | s->tlsext_ticket_expected = 1; | ||
| 2080 | return 3; | ||
| 2081 | default: /* fatal error */ | ||
| 2082 | return -1; | ||
| 2083 | } | ||
| 2084 | } | ||
| 2085 | p += size; | ||
| 2086 | } | ||
| 2087 | return 0; | ||
| 2088 | } | ||
| 2089 | |||
| 2090 | /* tls_decrypt_ticket attempts to decrypt a session ticket. | ||
| 2091 | * | ||
| 2092 | * etick: points to the body of the session ticket extension. | ||
| 2093 | * eticklen: the length of the session tickets extenion. | ||
| 2094 | * sess_id: points at the session ID. | ||
| 2095 | * sesslen: the length of the session ID. | ||
| 2096 | * psess: (output) on return, if a ticket was decrypted, then this is set to | ||
| 2097 | * point to the resulting session. | ||
| 2098 | * | ||
| 2099 | * Returns: | ||
| 2100 | * -1: fatal error, either from parsing or decrypting the ticket. | ||
| 2101 | * 2: the ticket couldn't be decrypted. | ||
| 2102 | * 3: a ticket was successfully decrypted and *psess was set. | ||
| 2103 | * 4: same as 3, but the ticket needs to be renewed. | ||
| 2104 | */ | ||
| 2105 | static int | ||
| 2106 | tls_decrypt_ticket(SSL *s, const unsigned char *etick, int eticklen, | ||
| 2107 | const unsigned char *sess_id, int sesslen, SSL_SESSION **psess) | ||
| 2108 | { | ||
| 2109 | SSL_SESSION *sess; | ||
| 2110 | unsigned char *sdec; | ||
| 2111 | const unsigned char *p; | ||
| 2112 | int slen, mlen, renew_ticket = 0; | ||
| 2113 | unsigned char tick_hmac[EVP_MAX_MD_SIZE]; | ||
| 2114 | HMAC_CTX hctx; | ||
| 2115 | EVP_CIPHER_CTX ctx; | ||
| 2116 | SSL_CTX *tctx = s->initial_ctx; | ||
| 2117 | /* Need at least keyname + iv + some encrypted data */ | ||
| 2118 | if (eticklen < 48) | ||
| 2119 | return 2; | ||
| 2120 | /* Initialize session ticket encryption and HMAC contexts */ | ||
| 2121 | HMAC_CTX_init(&hctx); | ||
| 2122 | EVP_CIPHER_CTX_init(&ctx); | ||
| 2123 | if (tctx->tlsext_ticket_key_cb) { | ||
| 2124 | unsigned char *nctick = (unsigned char *)etick; | ||
| 2125 | int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, | ||
| 2126 | &ctx, &hctx, 0); | ||
| 2127 | if (rv < 0) | ||
| 2128 | return -1; | ||
| 2129 | if (rv == 0) | ||
| 2130 | return 2; | ||
| 2131 | if (rv == 2) | ||
| 2132 | renew_ticket = 1; | ||
| 2133 | } else { | ||
| 2134 | /* Check key name matches */ | ||
| 2135 | if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) | ||
| 2136 | return 2; | ||
| 2137 | HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, | ||
| 2138 | tlsext_tick_md(), NULL); | ||
| 2139 | EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, | ||
| 2140 | tctx->tlsext_tick_aes_key, etick + 16); | ||
| 2141 | } | ||
| 2142 | /* Attempt to process session ticket, first conduct sanity and | ||
| 2143 | * integrity checks on ticket. | ||
| 2144 | */ | ||
| 2145 | mlen = HMAC_size(&hctx); | ||
| 2146 | if (mlen < 0) { | ||
| 2147 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
| 2148 | return -1; | ||
| 2149 | } | ||
| 2150 | eticklen -= mlen; | ||
| 2151 | /* Check HMAC of encrypted ticket */ | ||
| 2152 | HMAC_Update(&hctx, etick, eticklen); | ||
| 2153 | HMAC_Final(&hctx, tick_hmac, NULL); | ||
| 2154 | HMAC_CTX_cleanup(&hctx); | ||
| 2155 | if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) | ||
| 2156 | return 2; | ||
| 2157 | /* Attempt to decrypt session data */ | ||
| 2158 | /* Move p after IV to start of encrypted ticket, update length */ | ||
| 2159 | p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx); | ||
| 2160 | eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx); | ||
| 2161 | sdec = OPENSSL_malloc(eticklen); | ||
| 2162 | if (!sdec) { | ||
| 2163 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
| 2164 | return -1; | ||
| 2165 | } | ||
| 2166 | EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen); | ||
| 2167 | if (EVP_DecryptFinal(&ctx, sdec + slen, &mlen) <= 0) | ||
| 2168 | return 2; | ||
| 2169 | slen += mlen; | ||
| 2170 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
| 2171 | p = sdec; | ||
| 2172 | |||
| 2173 | sess = d2i_SSL_SESSION(NULL, &p, slen); | ||
| 2174 | OPENSSL_free(sdec); | ||
| 2175 | if (sess) { | ||
| 2176 | /* The session ID, if non-empty, is used by some clients to | ||
| 2177 | * detect that the ticket has been accepted. So we copy it to | ||
| 2178 | * the session structure. If it is empty set length to zero | ||
| 2179 | * as required by standard. | ||
| 2180 | */ | ||
| 2181 | if (sesslen) | ||
| 2182 | memcpy(sess->session_id, sess_id, sesslen); | ||
| 2183 | sess->session_id_length = sesslen; | ||
| 2184 | *psess = sess; | ||
| 2185 | if (renew_ticket) | ||
| 2186 | return 4; | ||
| 2187 | else | ||
| 2188 | return 3; | ||
| 2189 | } | ||
| 2190 | ERR_clear_error(); | ||
| 2191 | /* For session parse failure, indicate that we need to send a new | ||
| 2192 | * ticket. */ | ||
| 2193 | return 2; | ||
| 2194 | } | ||
| 2195 | |||
| 2196 | /* Tables to translate from NIDs to TLS v1.2 ids */ | ||
| 2197 | |||
| 2198 | typedef struct { | ||
| 2199 | int nid; | ||
| 2200 | int id; | ||
| 2201 | } tls12_lookup; | ||
| 2202 | |||
| 2203 | static tls12_lookup tls12_md[] = { | ||
| 2204 | #ifndef OPENSSL_NO_MD5 | ||
| 2205 | {NID_md5, TLSEXT_hash_md5}, | ||
| 2206 | #endif | ||
| 2207 | #ifndef OPENSSL_NO_SHA | ||
| 2208 | {NID_sha1, TLSEXT_hash_sha1}, | ||
| 2209 | #endif | ||
| 2210 | #ifndef OPENSSL_NO_SHA256 | ||
| 2211 | {NID_sha224, TLSEXT_hash_sha224}, | ||
| 2212 | {NID_sha256, TLSEXT_hash_sha256}, | ||
| 2213 | #endif | ||
| 2214 | #ifndef OPENSSL_NO_SHA512 | ||
| 2215 | {NID_sha384, TLSEXT_hash_sha384}, | ||
| 2216 | {NID_sha512, TLSEXT_hash_sha512} | ||
| 2217 | #endif | ||
| 2218 | }; | ||
| 2219 | |||
| 2220 | static tls12_lookup tls12_sig[] = { | ||
| 2221 | #ifndef OPENSSL_NO_RSA | ||
| 2222 | {EVP_PKEY_RSA, TLSEXT_signature_rsa}, | ||
| 2223 | #endif | ||
| 2224 | #ifndef OPENSSL_NO_DSA | ||
| 2225 | {EVP_PKEY_DSA, TLSEXT_signature_dsa}, | ||
| 2226 | #endif | ||
| 2227 | #ifndef OPENSSL_NO_ECDSA | ||
| 2228 | {EVP_PKEY_EC, TLSEXT_signature_ecdsa} | ||
| 2229 | #endif | ||
| 2230 | }; | ||
| 2231 | |||
| 2232 | static int | ||
| 2233 | tls12_find_id(int nid, tls12_lookup *table, size_t tlen) | ||
| 2234 | { | ||
| 2235 | size_t i; | ||
| 2236 | for (i = 0; i < tlen; i++) { | ||
| 2237 | if (table[i].nid == nid) | ||
| 2238 | return table[i].id; | ||
| 2239 | } | ||
| 2240 | return -1; | ||
| 2241 | } | ||
| 2242 | #if 0 | ||
| 2243 | static int | ||
| 2244 | tls12_find_nid(int id, tls12_lookup *table, size_t tlen) | ||
| 2245 | { | ||
| 2246 | size_t i; | ||
| 2247 | for (i = 0; i < tlen; i++) { | ||
| 2248 | if (table[i].id == id) | ||
| 2249 | return table[i].nid; | ||
| 2250 | } | ||
| 2251 | return -1; | ||
| 2252 | } | ||
| 2253 | #endif | ||
| 2254 | |||
| 2255 | int | ||
| 2256 | tls12_get_sigandhash(unsigned char *p, const EVP_PKEY *pk, const EVP_MD *md) | ||
| 2257 | { | ||
| 2258 | int sig_id, md_id; | ||
| 2259 | if (!md) | ||
| 2260 | return 0; | ||
| 2261 | md_id = tls12_find_id(EVP_MD_type(md), tls12_md, | ||
| 2262 | sizeof(tls12_md)/sizeof(tls12_lookup)); | ||
| 2263 | if (md_id == -1) | ||
| 2264 | return 0; | ||
| 2265 | sig_id = tls12_get_sigid(pk); | ||
| 2266 | if (sig_id == -1) | ||
| 2267 | return 0; | ||
| 2268 | p[0] = (unsigned char)md_id; | ||
| 2269 | p[1] = (unsigned char)sig_id; | ||
| 2270 | return 1; | ||
| 2271 | } | ||
| 2272 | |||
| 2273 | int | ||
| 2274 | tls12_get_sigid(const EVP_PKEY *pk) | ||
| 2275 | { | ||
| 2276 | return tls12_find_id(pk->type, tls12_sig, | ||
| 2277 | sizeof(tls12_sig)/sizeof(tls12_lookup)); | ||
| 2278 | } | ||
| 2279 | |||
| 2280 | const EVP_MD | ||
| 2281 | *tls12_get_hash(unsigned char hash_alg) | ||
| 2282 | { | ||
| 2283 | switch (hash_alg) { | ||
| 2284 | #ifndef OPENSSL_NO_SHA | ||
| 2285 | case TLSEXT_hash_sha1: | ||
| 2286 | return EVP_sha1(); | ||
| 2287 | #endif | ||
| 2288 | #ifndef OPENSSL_NO_SHA256 | ||
| 2289 | case TLSEXT_hash_sha224: | ||
| 2290 | return EVP_sha224(); | ||
| 2291 | |||
| 2292 | case TLSEXT_hash_sha256: | ||
| 2293 | return EVP_sha256(); | ||
| 2294 | #endif | ||
| 2295 | #ifndef OPENSSL_NO_SHA512 | ||
| 2296 | case TLSEXT_hash_sha384: | ||
| 2297 | return EVP_sha384(); | ||
| 2298 | |||
| 2299 | case TLSEXT_hash_sha512: | ||
| 2300 | return EVP_sha512(); | ||
| 2301 | #endif | ||
| 2302 | default: | ||
| 2303 | return NULL; | ||
| 2304 | |||
| 2305 | } | ||
| 2306 | } | ||
| 2307 | |||
| 2308 | /* Set preferred digest for each key type */ | ||
| 2309 | |||
| 2310 | int | ||
| 2311 | tls1_process_sigalgs(SSL *s, const unsigned char *data, int dsize) | ||
| 2312 | { | ||
| 2313 | int i, idx; | ||
| 2314 | const EVP_MD *md; | ||
| 2315 | CERT *c = s->cert; | ||
| 2316 | /* Extension ignored for TLS versions below 1.2 */ | ||
| 2317 | if (TLS1_get_version(s) < TLS1_2_VERSION) | ||
| 2318 | return 1; | ||
| 2319 | /* Should never happen */ | ||
| 2320 | if (!c) | ||
| 2321 | return 0; | ||
| 2322 | |||
| 2323 | c->pkeys[SSL_PKEY_DSA_SIGN].digest = NULL; | ||
| 2324 | c->pkeys[SSL_PKEY_RSA_SIGN].digest = NULL; | ||
| 2325 | c->pkeys[SSL_PKEY_RSA_ENC].digest = NULL; | ||
| 2326 | c->pkeys[SSL_PKEY_ECC].digest = NULL; | ||
| 2327 | |||
| 2328 | for (i = 0; i < dsize; i += 2) { | ||
| 2329 | unsigned char hash_alg = data[i], sig_alg = data[i + 1]; | ||
| 2330 | |||
| 2331 | switch (sig_alg) { | ||
| 2332 | #ifndef OPENSSL_NO_RSA | ||
| 2333 | case TLSEXT_signature_rsa: | ||
| 2334 | idx = SSL_PKEY_RSA_SIGN; | ||
| 2335 | break; | ||
| 2336 | #endif | ||
| 2337 | #ifndef OPENSSL_NO_DSA | ||
| 2338 | case TLSEXT_signature_dsa: | ||
| 2339 | idx = SSL_PKEY_DSA_SIGN; | ||
| 2340 | break; | ||
| 2341 | #endif | ||
| 2342 | #ifndef OPENSSL_NO_ECDSA | ||
| 2343 | case TLSEXT_signature_ecdsa: | ||
| 2344 | idx = SSL_PKEY_ECC; | ||
| 2345 | break; | ||
| 2346 | #endif | ||
| 2347 | default: | ||
| 2348 | continue; | ||
| 2349 | } | ||
| 2350 | |||
| 2351 | if (c->pkeys[idx].digest == NULL) { | ||
| 2352 | md = tls12_get_hash(hash_alg); | ||
| 2353 | if (md) { | ||
| 2354 | c->pkeys[idx].digest = md; | ||
| 2355 | if (idx == SSL_PKEY_RSA_SIGN) | ||
| 2356 | c->pkeys[SSL_PKEY_RSA_ENC].digest = md; | ||
| 2357 | } | ||
| 2358 | } | ||
| 2359 | |||
| 2360 | } | ||
| 2361 | |||
| 2362 | |||
| 2363 | /* Set any remaining keys to default values. NOTE: if alg is not | ||
| 2364 | * supported it stays as NULL. | ||
| 2365 | */ | ||
| 2366 | #ifndef OPENSSL_NO_DSA | ||
| 2367 | if (!c->pkeys[SSL_PKEY_DSA_SIGN].digest) | ||
| 2368 | c->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1(); | ||
| 2369 | #endif | ||
| 2370 | #ifndef OPENSSL_NO_RSA | ||
| 2371 | if (!c->pkeys[SSL_PKEY_RSA_SIGN].digest) { | ||
| 2372 | c->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1(); | ||
| 2373 | c->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1(); | ||
| 2374 | } | ||
| 2375 | #endif | ||
| 2376 | #ifndef OPENSSL_NO_ECDSA | ||
| 2377 | if (!c->pkeys[SSL_PKEY_ECC].digest) | ||
| 2378 | c->pkeys[SSL_PKEY_ECC].digest = EVP_sha1(); | ||
| 2379 | #endif | ||
| 2380 | return 1; | ||
| 2381 | } | ||
| 2382 | |||
| 2383 | #endif | ||
