Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 112 | * ECC cipher suite support in OpenSSL originally developed by |
| 113 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
| 114 | */ |
| 115 | /* ==================================================================== |
| 116 | * Copyright 2005 Nokia. All rights reserved. |
| 117 | * |
| 118 | * The portions of the attached software ("Contribution") is developed by |
| 119 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 120 | * license. |
| 121 | * |
| 122 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 123 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 124 | * support (see RFC 4279) to OpenSSL. |
| 125 | * |
| 126 | * No patent licenses or other rights except those expressly stated in |
| 127 | * the OpenSSL open source license shall be deemed granted or received |
| 128 | * expressly, by implication, estoppel, or otherwise. |
| 129 | * |
| 130 | * No assurances are provided by Nokia that the Contribution does not |
| 131 | * infringe the patent or other intellectual property rights of any third |
| 132 | * party or that the license provides you with all the necessary rights |
| 133 | * to make use of the Contribution. |
| 134 | * |
| 135 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 136 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 137 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 138 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 139 | * OTHERWISE. */ |
| 140 | |
| 141 | #include <stdio.h> |
| 142 | #include <assert.h> |
| 143 | |
| 144 | #include <openssl/dh.h> |
| 145 | #include <openssl/engine.h> |
| 146 | #include <openssl/lhash.h> |
| 147 | #include <openssl/mem.h> |
| 148 | #include <openssl/obj.h> |
| 149 | #include <openssl/rand.h> |
| 150 | #include <openssl/x509v3.h> |
| 151 | |
| 152 | #include "ssl_locl.h" |
| 153 | |
| 154 | SSL3_ENC_METHOD ssl3_undef_enc_method={ |
| 155 | /* evil casts, but these functions are only called if there's a library bug */ |
| 156 | (int (*)(SSL *,int))ssl_undefined_function, |
| 157 | (int (*)(SSL *, unsigned char *, int))ssl_undefined_function, |
| 158 | ssl_undefined_function, |
| 159 | (int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function, |
| 160 | (int (*)(SSL*, int))ssl_undefined_function, |
| 161 | (int (*)(SSL *, const char*, int, unsigned char *))ssl_undefined_function, |
| 162 | 0, /* finish_mac_length */ |
| 163 | (int (*)(SSL *, int, unsigned char *))ssl_undefined_function, |
| 164 | NULL, /* client_finished_label */ |
| 165 | 0, /* client_finished_label_len */ |
| 166 | NULL, /* server_finished_label */ |
| 167 | 0, /* server_finished_label_len */ |
| 168 | (int (*)(int))ssl_undefined_function, |
| 169 | (int (*)(SSL *, unsigned char *, size_t, const char *, |
| 170 | size_t, const unsigned char *, size_t, |
| 171 | int use_context)) ssl_undefined_function, |
| 172 | }; |
| 173 | |
| 174 | int SSL_clear(SSL *s) |
| 175 | { |
| 176 | |
| 177 | if (s->method == NULL) |
| 178 | { |
| 179 | OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED); |
| 180 | return(0); |
| 181 | } |
| 182 | |
| 183 | if (ssl_clear_bad_session(s)) |
| 184 | { |
| 185 | SSL_SESSION_free(s->session); |
| 186 | s->session=NULL; |
| 187 | } |
| 188 | |
| 189 | s->error=0; |
| 190 | s->hit=0; |
| 191 | s->shutdown=0; |
| 192 | |
| 193 | #if 0 /* Disabled since version 1.10 of this file (early return not |
| 194 | * needed because SSL_clear is not called when doing renegotiation) */ |
| 195 | /* This is set if we are doing dynamic renegotiation so keep |
| 196 | * the old cipher. It is sort of a SSL_clear_lite :-) */ |
| 197 | if (s->renegotiate) return(1); |
| 198 | #else |
| 199 | if (s->renegotiate) |
| 200 | { |
| 201 | OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR); |
| 202 | return 0; |
| 203 | } |
| 204 | #endif |
| 205 | |
| 206 | s->type=0; |
| 207 | |
| 208 | s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT); |
| 209 | |
| 210 | s->version=s->method->version; |
| 211 | s->client_version=s->version; |
| 212 | s->rwstate=SSL_NOTHING; |
| 213 | s->rstate=SSL_ST_READ_HEADER; |
| 214 | #if 0 |
| 215 | s->read_ahead=s->ctx->read_ahead; |
| 216 | #endif |
| 217 | |
| 218 | if (s->init_buf != NULL) |
| 219 | { |
| 220 | BUF_MEM_free(s->init_buf); |
| 221 | s->init_buf=NULL; |
| 222 | } |
| 223 | |
| 224 | ssl_clear_cipher_ctx(s); |
| 225 | ssl_clear_hash_ctx(&s->read_hash); |
| 226 | ssl_clear_hash_ctx(&s->write_hash); |
| 227 | |
| 228 | s->first_packet=0; |
| 229 | |
| 230 | #if 1 |
| 231 | /* Check to see if we were changed into a different method, if |
| 232 | * so, revert back if we are not doing session-id reuse. */ |
| 233 | if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method)) |
| 234 | { |
| 235 | s->method->ssl_free(s); |
| 236 | s->method=s->ctx->method; |
| 237 | if (!s->method->ssl_new(s)) |
| 238 | return(0); |
| 239 | } |
| 240 | else |
| 241 | #endif |
| 242 | s->method->ssl_clear(s); |
| 243 | return(1); |
| 244 | } |
| 245 | |
| 246 | /** Used to change an SSL_CTXs default SSL method type */ |
| 247 | int SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth) |
| 248 | { |
| 249 | STACK_OF(SSL_CIPHER) *sk; |
| 250 | |
| 251 | ctx->method=meth; |
| 252 | |
| 253 | sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list), |
| 254 | &(ctx->cipher_list_by_id), |
| 255 | meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST, ctx->cert); |
| 256 | if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) |
| 257 | { |
| 258 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_ssl_version, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS); |
| 259 | return(0); |
| 260 | } |
| 261 | return(1); |
| 262 | } |
| 263 | |
| 264 | SSL *SSL_new(SSL_CTX *ctx) |
| 265 | { |
| 266 | SSL *s; |
| 267 | |
| 268 | if (ctx == NULL) |
| 269 | { |
| 270 | OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX); |
| 271 | return(NULL); |
| 272 | } |
| 273 | if (ctx->method == NULL) |
| 274 | { |
| 275 | OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION); |
| 276 | return(NULL); |
| 277 | } |
| 278 | |
| 279 | s=(SSL *)OPENSSL_malloc(sizeof(SSL)); |
| 280 | if (s == NULL) goto err; |
| 281 | memset(s,0,sizeof(SSL)); |
| 282 | |
| 283 | s->options=ctx->options; |
| 284 | s->mode=ctx->mode; |
| 285 | s->max_cert_list=ctx->max_cert_list; |
| 286 | |
| 287 | if (ctx->cert != NULL) |
| 288 | { |
| 289 | /* Earlier library versions used to copy the pointer to |
| 290 | * the CERT, not its contents; only when setting new |
| 291 | * parameters for the per-SSL copy, ssl_cert_new would be |
| 292 | * called (and the direct reference to the per-SSL_CTX |
| 293 | * settings would be lost, but those still were indirectly |
| 294 | * accessed for various purposes, and for that reason they |
| 295 | * used to be known as s->ctx->default_cert). |
| 296 | * Now we don't look at the SSL_CTX's CERT after having |
| 297 | * duplicated it once. */ |
| 298 | |
| 299 | s->cert = ssl_cert_dup(ctx->cert); |
| 300 | if (s->cert == NULL) |
| 301 | goto err; |
| 302 | } |
| 303 | else |
| 304 | s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */ |
| 305 | |
| 306 | s->read_ahead=ctx->read_ahead; |
| 307 | s->msg_callback=ctx->msg_callback; |
| 308 | s->msg_callback_arg=ctx->msg_callback_arg; |
| 309 | s->verify_mode=ctx->verify_mode; |
| 310 | #if 0 |
| 311 | s->verify_depth=ctx->verify_depth; |
| 312 | #endif |
| 313 | s->sid_ctx_length=ctx->sid_ctx_length; |
| 314 | assert(s->sid_ctx_length <= sizeof s->sid_ctx); |
| 315 | memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx)); |
| 316 | s->verify_callback=ctx->default_verify_callback; |
| 317 | s->generate_session_id=ctx->generate_session_id; |
| 318 | |
| 319 | s->param = X509_VERIFY_PARAM_new(); |
| 320 | if (!s->param) |
| 321 | goto err; |
| 322 | X509_VERIFY_PARAM_inherit(s->param, ctx->param); |
| 323 | #if 0 |
| 324 | s->purpose = ctx->purpose; |
| 325 | s->trust = ctx->trust; |
| 326 | #endif |
| 327 | s->quiet_shutdown=ctx->quiet_shutdown; |
| 328 | s->max_send_fragment = ctx->max_send_fragment; |
| 329 | |
| 330 | CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX); |
| 331 | s->ctx=ctx; |
| 332 | #ifndef OPENSSL_NO_TLSEXT |
| 333 | s->tlsext_debug_cb = 0; |
| 334 | s->tlsext_debug_arg = NULL; |
| 335 | s->tlsext_ticket_expected = 0; |
| 336 | s->tlsext_status_type = -1; |
| 337 | s->tlsext_status_expected = 0; |
| 338 | s->tlsext_ocsp_ids = NULL; |
| 339 | s->tlsext_ocsp_exts = NULL; |
| 340 | s->tlsext_ocsp_resp = NULL; |
| 341 | s->tlsext_ocsp_resplen = -1; |
| 342 | CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX); |
| 343 | s->initial_ctx=ctx; |
| 344 | #ifndef OPENSSL_NO_EC |
| 345 | if (ctx->tlsext_ecpointformatlist) |
| 346 | { |
| 347 | s->tlsext_ecpointformatlist = |
| 348 | BUF_memdup(ctx->tlsext_ecpointformatlist, |
| 349 | ctx->tlsext_ecpointformatlist_length); |
| 350 | if (!s->tlsext_ecpointformatlist) |
| 351 | goto err; |
| 352 | s->tlsext_ecpointformatlist_length = |
| 353 | ctx->tlsext_ecpointformatlist_length; |
| 354 | } |
| 355 | if (ctx->tlsext_ellipticcurvelist) |
| 356 | { |
| 357 | s->tlsext_ellipticcurvelist = |
| 358 | BUF_memdup(ctx->tlsext_ellipticcurvelist, |
| 359 | ctx->tlsext_ellipticcurvelist_length); |
| 360 | if (!s->tlsext_ellipticcurvelist) |
| 361 | goto err; |
| 362 | s->tlsext_ellipticcurvelist_length = |
| 363 | ctx->tlsext_ellipticcurvelist_length; |
| 364 | } |
| 365 | #endif |
| 366 | # ifndef OPENSSL_NO_NEXTPROTONEG |
| 367 | s->next_proto_negotiated = NULL; |
| 368 | # endif |
| 369 | |
| 370 | if (s->ctx->alpn_client_proto_list) |
| 371 | { |
| 372 | s->alpn_client_proto_list = |
| 373 | OPENSSL_malloc(s->ctx->alpn_client_proto_list_len); |
| 374 | if (s->alpn_client_proto_list == NULL) |
| 375 | goto err; |
| 376 | memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list, |
| 377 | s->ctx->alpn_client_proto_list_len); |
| 378 | s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len; |
| 379 | } |
| 380 | #endif |
| 381 | |
| 382 | s->verify_result=X509_V_OK; |
| 383 | |
| 384 | s->method=ctx->method; |
| 385 | |
| 386 | if (!s->method->ssl_new(s)) |
| 387 | goto err; |
| 388 | |
| 389 | s->references=1; |
| 390 | s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1; |
| 391 | |
| 392 | SSL_clear(s); |
| 393 | |
| 394 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); |
| 395 | |
| 396 | #ifndef OPENSSL_NO_PSK |
| 397 | s->psk_client_callback=ctx->psk_client_callback; |
| 398 | s->psk_server_callback=ctx->psk_server_callback; |
| 399 | #endif |
| 400 | |
| 401 | return(s); |
| 402 | err: |
| 403 | if (s != NULL) |
| 404 | { |
| 405 | if (s->cert != NULL) |
| 406 | ssl_cert_free(s->cert); |
| 407 | if (s->ctx != NULL) |
| 408 | SSL_CTX_free(s->ctx); /* decrement reference count */ |
| 409 | OPENSSL_free(s); |
| 410 | } |
| 411 | OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE); |
| 412 | return(NULL); |
| 413 | } |
| 414 | |
| 415 | int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx, |
| 416 | unsigned int sid_ctx_len) |
| 417 | { |
| 418 | if(sid_ctx_len > sizeof ctx->sid_ctx) |
| 419 | { |
| 420 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); |
| 421 | return 0; |
| 422 | } |
| 423 | ctx->sid_ctx_length=sid_ctx_len; |
| 424 | memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len); |
| 425 | |
| 426 | return 1; |
| 427 | } |
| 428 | |
| 429 | int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx, |
| 430 | unsigned int sid_ctx_len) |
| 431 | { |
| 432 | if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) |
| 433 | { |
| 434 | OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); |
| 435 | return 0; |
| 436 | } |
| 437 | ssl->sid_ctx_length=sid_ctx_len; |
| 438 | memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len); |
| 439 | |
| 440 | return 1; |
| 441 | } |
| 442 | |
| 443 | int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) |
| 444 | { |
| 445 | CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); |
| 446 | ctx->generate_session_id = cb; |
| 447 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); |
| 448 | return 1; |
| 449 | } |
| 450 | |
| 451 | int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) |
| 452 | { |
| 453 | CRYPTO_w_lock(CRYPTO_LOCK_SSL); |
| 454 | ssl->generate_session_id = cb; |
| 455 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL); |
| 456 | return 1; |
| 457 | } |
| 458 | |
| 459 | int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id, |
| 460 | unsigned int id_len) |
| 461 | { |
| 462 | /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how |
| 463 | * we can "construct" a session to give us the desired check - ie. to |
| 464 | * find if there's a session in the hash table that would conflict with |
| 465 | * any new session built out of this id/id_len and the ssl_version in |
| 466 | * use by this SSL. */ |
| 467 | SSL_SESSION r, *p; |
| 468 | |
| 469 | if(id_len > sizeof r.session_id) |
| 470 | return 0; |
| 471 | |
| 472 | r.ssl_version = ssl->version; |
| 473 | r.session_id_length = id_len; |
| 474 | memcpy(r.session_id, id, id_len); |
| 475 | /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a |
| 476 | * callback is calling us to check the uniqueness of a shorter ID, it |
| 477 | * must be compared as a padded-out ID because that is what it will be |
| 478 | * converted to when the callback has finished choosing it. */ |
| 479 | if((r.ssl_version == SSL2_VERSION) && |
| 480 | (id_len < SSL2_SSL_SESSION_ID_LENGTH)) |
| 481 | { |
| 482 | memset(r.session_id + id_len, 0, |
| 483 | SSL2_SSL_SESSION_ID_LENGTH - id_len); |
| 484 | r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH; |
| 485 | } |
| 486 | |
| 487 | CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX); |
| 488 | p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r); |
| 489 | CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX); |
| 490 | return (p != NULL); |
| 491 | } |
| 492 | |
| 493 | int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) |
| 494 | { |
| 495 | return X509_VERIFY_PARAM_set_purpose(s->param, purpose); |
| 496 | } |
| 497 | |
| 498 | int SSL_set_purpose(SSL *s, int purpose) |
| 499 | { |
| 500 | return X509_VERIFY_PARAM_set_purpose(s->param, purpose); |
| 501 | } |
| 502 | |
| 503 | int SSL_CTX_set_trust(SSL_CTX *s, int trust) |
| 504 | { |
| 505 | return X509_VERIFY_PARAM_set_trust(s->param, trust); |
| 506 | } |
| 507 | |
| 508 | int SSL_set_trust(SSL *s, int trust) |
| 509 | { |
| 510 | return X509_VERIFY_PARAM_set_trust(s->param, trust); |
| 511 | } |
| 512 | |
| 513 | int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) |
| 514 | { |
| 515 | return X509_VERIFY_PARAM_set1(ctx->param, vpm); |
| 516 | } |
| 517 | |
| 518 | int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) |
| 519 | { |
| 520 | return X509_VERIFY_PARAM_set1(ssl->param, vpm); |
| 521 | } |
| 522 | |
| 523 | X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) |
| 524 | { |
| 525 | return ctx->param; |
| 526 | } |
| 527 | |
| 528 | X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) |
| 529 | { |
| 530 | return ssl->param; |
| 531 | } |
| 532 | |
| 533 | void SSL_certs_clear(SSL *s) |
| 534 | { |
| 535 | ssl_cert_clear_certs(s->cert); |
| 536 | } |
| 537 | |
| 538 | void SSL_free(SSL *s) |
| 539 | { |
| 540 | int i; |
| 541 | |
| 542 | if(s == NULL) |
| 543 | return; |
| 544 | |
| 545 | i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL); |
| 546 | #ifdef REF_PRINT |
| 547 | REF_PRINT("SSL",s); |
| 548 | #endif |
| 549 | if (i > 0) return; |
| 550 | #ifdef REF_CHECK |
| 551 | if (i < 0) |
| 552 | { |
| 553 | fprintf(stderr,"SSL_free, bad reference count\n"); |
| 554 | abort(); /* ok */ |
| 555 | } |
| 556 | #endif |
| 557 | |
| 558 | if (s->param) |
| 559 | X509_VERIFY_PARAM_free(s->param); |
| 560 | |
| 561 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data); |
| 562 | |
| 563 | if (s->bbio != NULL) |
| 564 | { |
| 565 | /* If the buffering BIO is in place, pop it off */ |
| 566 | if (s->bbio == s->wbio) |
| 567 | { |
| 568 | s->wbio=BIO_pop(s->wbio); |
| 569 | } |
| 570 | BIO_free(s->bbio); |
| 571 | s->bbio=NULL; |
| 572 | } |
| 573 | if (s->rbio != NULL) |
| 574 | BIO_free_all(s->rbio); |
| 575 | if ((s->wbio != NULL) && (s->wbio != s->rbio)) |
| 576 | BIO_free_all(s->wbio); |
| 577 | |
| 578 | if (s->init_buf != NULL) BUF_MEM_free(s->init_buf); |
| 579 | |
| 580 | /* add extra stuff */ |
| 581 | if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list); |
| 582 | if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id); |
| 583 | |
| 584 | /* Make the next call work :-) */ |
| 585 | if (s->session != NULL) |
| 586 | { |
| 587 | ssl_clear_bad_session(s); |
| 588 | SSL_SESSION_free(s->session); |
| 589 | } |
| 590 | |
| 591 | ssl_clear_cipher_ctx(s); |
| 592 | ssl_clear_hash_ctx(&s->read_hash); |
| 593 | ssl_clear_hash_ctx(&s->write_hash); |
| 594 | |
| 595 | if (s->cert != NULL) ssl_cert_free(s->cert); |
| 596 | /* Free up if allocated */ |
| 597 | |
| 598 | #ifndef OPENSSL_NO_TLSEXT |
| 599 | if (s->tlsext_hostname) |
| 600 | OPENSSL_free(s->tlsext_hostname); |
| 601 | if (s->initial_ctx) SSL_CTX_free(s->initial_ctx); |
| 602 | #ifndef OPENSSL_NO_EC |
| 603 | if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist); |
| 604 | if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist); |
| 605 | #endif /* OPENSSL_NO_EC */ |
| 606 | if (s->tlsext_opaque_prf_input) OPENSSL_free(s->tlsext_opaque_prf_input); |
| 607 | if (s->tlsext_ocsp_exts) |
| 608 | sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, |
| 609 | X509_EXTENSION_free); |
| 610 | /* TODO(fork): OCSP support */ |
| 611 | #if 0 |
| 612 | if (s->tlsext_ocsp_ids) |
| 613 | sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free); |
| 614 | #endif |
| 615 | if (s->tlsext_ocsp_resp) |
| 616 | OPENSSL_free(s->tlsext_ocsp_resp); |
| 617 | if (s->alpn_client_proto_list) |
| 618 | OPENSSL_free(s->alpn_client_proto_list); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 619 | if (s->tlsext_channel_id_private) |
| 620 | EVP_PKEY_free(s->tlsext_channel_id_private); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 621 | #endif |
| 622 | |
| 623 | if (s->client_CA != NULL) |
| 624 | sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free); |
| 625 | |
| 626 | if (s->method != NULL) s->method->ssl_free(s); |
| 627 | |
| 628 | if (s->ctx) SSL_CTX_free(s->ctx); |
| 629 | |
| 630 | #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 631 | if (s->next_proto_negotiated) |
| 632 | OPENSSL_free(s->next_proto_negotiated); |
| 633 | #endif |
| 634 | |
| 635 | if (s->srtp_profiles) |
| 636 | sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles); |
| 637 | |
| 638 | #ifndef OPENSSL_NO_DANE |
| 639 | if (s->tlsa_record && s->tlsa_record!=(void *)-1) |
| 640 | OPENSSL_free(s->tlsa_record); |
| 641 | #endif |
| 642 | |
| 643 | OPENSSL_free(s); |
| 644 | } |
| 645 | |
| 646 | void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio) |
| 647 | { |
| 648 | /* If the output buffering BIO is still in place, remove it |
| 649 | */ |
| 650 | if (s->bbio != NULL) |
| 651 | { |
| 652 | if (s->wbio == s->bbio) |
| 653 | { |
| 654 | s->wbio=s->wbio->next_bio; |
| 655 | s->bbio->next_bio=NULL; |
| 656 | } |
| 657 | } |
| 658 | if ((s->rbio != NULL) && (s->rbio != rbio)) |
| 659 | BIO_free_all(s->rbio); |
| 660 | if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio)) |
| 661 | BIO_free_all(s->wbio); |
| 662 | s->rbio=rbio; |
| 663 | s->wbio=wbio; |
| 664 | } |
| 665 | |
| 666 | BIO *SSL_get_rbio(const SSL *s) |
| 667 | { return(s->rbio); } |
| 668 | |
| 669 | BIO *SSL_get_wbio(const SSL *s) |
| 670 | { return(s->wbio); } |
| 671 | |
| 672 | int SSL_get_fd(const SSL *s) |
| 673 | { |
| 674 | return(SSL_get_rfd(s)); |
| 675 | } |
| 676 | |
| 677 | int SSL_get_rfd(const SSL *s) |
| 678 | { |
| 679 | int ret= -1; |
| 680 | BIO *b,*r; |
| 681 | |
| 682 | b=SSL_get_rbio(s); |
| 683 | r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR); |
| 684 | if (r != NULL) |
| 685 | BIO_get_fd(r,&ret); |
| 686 | return(ret); |
| 687 | } |
| 688 | |
| 689 | int SSL_get_wfd(const SSL *s) |
| 690 | { |
| 691 | int ret= -1; |
| 692 | BIO *b,*r; |
| 693 | |
| 694 | b=SSL_get_wbio(s); |
| 695 | r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR); |
| 696 | if (r != NULL) |
| 697 | BIO_get_fd(r,&ret); |
| 698 | return(ret); |
| 699 | } |
| 700 | |
| 701 | #ifndef OPENSSL_NO_SOCK |
| 702 | int SSL_set_fd(SSL *s,int fd) |
| 703 | { |
| 704 | int ret=0; |
| 705 | BIO *bio=NULL; |
| 706 | |
| 707 | bio=BIO_new(BIO_s_fd()); |
| 708 | |
| 709 | if (bio == NULL) |
| 710 | { |
| 711 | OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB); |
| 712 | goto err; |
| 713 | } |
| 714 | BIO_set_fd(bio,fd,BIO_NOCLOSE); |
| 715 | SSL_set_bio(s,bio,bio); |
| 716 | ret=1; |
| 717 | err: |
| 718 | return(ret); |
| 719 | } |
| 720 | |
| 721 | int SSL_set_wfd(SSL *s,int fd) |
| 722 | { |
| 723 | int ret=0; |
| 724 | BIO *bio=NULL; |
| 725 | |
| 726 | if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_FD) |
| 727 | || ((int)BIO_get_fd(s->rbio,NULL) != fd)) |
| 728 | { |
| 729 | bio=BIO_new(BIO_s_fd()); |
| 730 | |
| 731 | if (bio == NULL) |
| 732 | { |
| 733 | OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB); |
| 734 | goto err; |
| 735 | } |
| 736 | BIO_set_fd(bio,fd,BIO_NOCLOSE); |
| 737 | SSL_set_bio(s,SSL_get_rbio(s),bio); |
| 738 | } |
| 739 | else |
| 740 | SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s)); |
| 741 | ret=1; |
| 742 | err: |
| 743 | return(ret); |
| 744 | } |
| 745 | |
| 746 | int SSL_set_rfd(SSL *s,int fd) |
| 747 | { |
| 748 | int ret=0; |
| 749 | BIO *bio=NULL; |
| 750 | |
| 751 | if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_FD) |
| 752 | || ((int)BIO_get_fd(s->wbio,NULL) != fd)) |
| 753 | { |
| 754 | bio=BIO_new(BIO_s_fd()); |
| 755 | |
| 756 | if (bio == NULL) |
| 757 | { |
| 758 | OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB); |
| 759 | goto err; |
| 760 | } |
| 761 | BIO_set_fd(bio,fd,BIO_NOCLOSE); |
| 762 | SSL_set_bio(s,bio,SSL_get_wbio(s)); |
| 763 | } |
| 764 | else |
| 765 | SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s)); |
| 766 | ret=1; |
| 767 | err: |
| 768 | return(ret); |
| 769 | } |
| 770 | #endif |
| 771 | |
| 772 | |
| 773 | /* return length of latest Finished message we sent, copy to 'buf' */ |
| 774 | size_t SSL_get_finished(const SSL *s, void *buf, size_t count) |
| 775 | { |
| 776 | size_t ret = 0; |
| 777 | |
| 778 | if (s->s3 != NULL) |
| 779 | { |
| 780 | ret = s->s3->tmp.finish_md_len; |
| 781 | if (count > ret) |
| 782 | count = ret; |
| 783 | memcpy(buf, s->s3->tmp.finish_md, count); |
| 784 | } |
| 785 | return ret; |
| 786 | } |
| 787 | |
| 788 | /* return length of latest Finished message we expected, copy to 'buf' */ |
| 789 | size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) |
| 790 | { |
| 791 | size_t ret = 0; |
| 792 | |
| 793 | if (s->s3 != NULL) |
| 794 | { |
| 795 | ret = s->s3->tmp.peer_finish_md_len; |
| 796 | if (count > ret) |
| 797 | count = ret; |
| 798 | memcpy(buf, s->s3->tmp.peer_finish_md, count); |
| 799 | } |
| 800 | return ret; |
| 801 | } |
| 802 | |
| 803 | |
| 804 | int SSL_get_verify_mode(const SSL *s) |
| 805 | { |
| 806 | return(s->verify_mode); |
| 807 | } |
| 808 | |
| 809 | int SSL_get_verify_depth(const SSL *s) |
| 810 | { |
| 811 | return X509_VERIFY_PARAM_get_depth(s->param); |
| 812 | } |
| 813 | |
| 814 | int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *) |
| 815 | { |
| 816 | return(s->verify_callback); |
| 817 | } |
| 818 | |
| 819 | int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) |
| 820 | { |
| 821 | return(ctx->verify_mode); |
| 822 | } |
| 823 | |
| 824 | int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) |
| 825 | { |
| 826 | return X509_VERIFY_PARAM_get_depth(ctx->param); |
| 827 | } |
| 828 | |
| 829 | int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *) |
| 830 | { |
| 831 | return(ctx->default_verify_callback); |
| 832 | } |
| 833 | |
| 834 | void SSL_set_verify(SSL *s,int mode, |
| 835 | int (*callback)(int ok,X509_STORE_CTX *ctx)) |
| 836 | { |
| 837 | s->verify_mode=mode; |
| 838 | if (callback != NULL) |
| 839 | s->verify_callback=callback; |
| 840 | } |
| 841 | |
| 842 | void SSL_set_verify_depth(SSL *s,int depth) |
| 843 | { |
| 844 | X509_VERIFY_PARAM_set_depth(s->param, depth); |
| 845 | } |
| 846 | |
| 847 | void SSL_set_read_ahead(SSL *s,int yes) |
| 848 | { |
| 849 | s->read_ahead=yes; |
| 850 | } |
| 851 | |
| 852 | int SSL_get_read_ahead(const SSL *s) |
| 853 | { |
| 854 | return(s->read_ahead); |
| 855 | } |
| 856 | |
| 857 | int SSL_pending(const SSL *s) |
| 858 | { |
| 859 | /* SSL_pending cannot work properly if read-ahead is enabled |
| 860 | * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), |
| 861 | * and it is impossible to fix since SSL_pending cannot report |
| 862 | * errors that may be observed while scanning the new data. |
| 863 | * (Note that SSL_pending() is often used as a boolean value, |
| 864 | * so we'd better not return -1.) |
| 865 | */ |
| 866 | return(s->method->ssl_pending(s)); |
| 867 | } |
| 868 | |
| 869 | X509 *SSL_get_peer_certificate(const SSL *s) |
| 870 | { |
| 871 | X509 *r; |
| 872 | |
| 873 | if ((s == NULL) || (s->session == NULL)) |
| 874 | r=NULL; |
| 875 | else |
| 876 | r=s->session->peer; |
| 877 | |
| 878 | if (r == NULL) return(r); |
| 879 | |
| 880 | CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509); |
| 881 | |
| 882 | return(r); |
| 883 | } |
| 884 | |
| 885 | STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s) |
| 886 | { |
| 887 | STACK_OF(X509) *r; |
| 888 | |
| 889 | if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL)) |
| 890 | r=NULL; |
| 891 | else |
| 892 | r=s->session->sess_cert->cert_chain; |
| 893 | |
| 894 | /* If we are a client, cert_chain includes the peer's own |
| 895 | * certificate; if we are a server, it does not. */ |
| 896 | |
| 897 | return(r); |
| 898 | } |
| 899 | |
| 900 | /* Now in theory, since the calling process own 't' it should be safe to |
| 901 | * modify. We need to be able to read f without being hassled */ |
| 902 | void SSL_copy_session_id(SSL *t,const SSL *f) |
| 903 | { |
| 904 | CERT *tmp; |
| 905 | |
| 906 | /* Do we need to to SSL locking? */ |
| 907 | SSL_set_session(t,SSL_get_session(f)); |
| 908 | |
| 909 | /* what if we are setup as SSLv2 but want to talk SSLv3 or |
| 910 | * vice-versa */ |
| 911 | if (t->method != f->method) |
| 912 | { |
| 913 | t->method->ssl_free(t); /* cleanup current */ |
| 914 | t->method=f->method; /* change method */ |
| 915 | t->method->ssl_new(t); /* setup new */ |
| 916 | } |
| 917 | |
| 918 | tmp=t->cert; |
| 919 | if (f->cert != NULL) |
| 920 | { |
| 921 | CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT); |
| 922 | t->cert=f->cert; |
| 923 | } |
| 924 | else |
| 925 | t->cert=NULL; |
| 926 | if (tmp != NULL) ssl_cert_free(tmp); |
| 927 | SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length); |
| 928 | } |
| 929 | |
| 930 | /* Fix this so it checks all the valid key/cert options */ |
| 931 | int SSL_CTX_check_private_key(const SSL_CTX *ctx) |
| 932 | { |
| 933 | if ( (ctx == NULL) || |
| 934 | (ctx->cert == NULL) || |
| 935 | (ctx->cert->key->x509 == NULL)) |
| 936 | { |
| 937 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED); |
| 938 | return(0); |
| 939 | } |
| 940 | if (ctx->cert->key->privatekey == NULL) |
| 941 | { |
| 942 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED); |
| 943 | return(0); |
| 944 | } |
| 945 | return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey)); |
| 946 | } |
| 947 | |
| 948 | /* Fix this function so that it takes an optional type parameter */ |
| 949 | int SSL_check_private_key(const SSL *ssl) |
| 950 | { |
| 951 | if (ssl == NULL) |
| 952 | { |
| 953 | OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER); |
| 954 | return(0); |
| 955 | } |
| 956 | if (ssl->cert == NULL) |
| 957 | { |
| 958 | OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED); |
| 959 | return 0; |
| 960 | } |
| 961 | if (ssl->cert->key->x509 == NULL) |
| 962 | { |
| 963 | OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED); |
| 964 | return(0); |
| 965 | } |
| 966 | if (ssl->cert->key->privatekey == NULL) |
| 967 | { |
| 968 | OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED); |
| 969 | return(0); |
| 970 | } |
| 971 | return(X509_check_private_key(ssl->cert->key->x509, |
| 972 | ssl->cert->key->privatekey)); |
| 973 | } |
| 974 | |
| 975 | int SSL_accept(SSL *s) |
| 976 | { |
| 977 | if (s->handshake_func == 0) |
| 978 | /* Not properly initialized yet */ |
| 979 | SSL_set_accept_state(s); |
| 980 | |
| 981 | return(s->method->ssl_accept(s)); |
| 982 | } |
| 983 | |
| 984 | int SSL_connect(SSL *s) |
| 985 | { |
| 986 | if (s->handshake_func == 0) |
| 987 | /* Not properly initialized yet */ |
| 988 | SSL_set_connect_state(s); |
| 989 | |
| 990 | return(s->method->ssl_connect(s)); |
| 991 | } |
| 992 | |
| 993 | long SSL_get_default_timeout(const SSL *s) |
| 994 | { |
| 995 | return(s->method->get_timeout()); |
| 996 | } |
| 997 | |
| 998 | int SSL_read(SSL *s,void *buf,int num) |
| 999 | { |
| 1000 | if (s->handshake_func == 0) |
| 1001 | { |
| 1002 | OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED); |
| 1003 | return -1; |
| 1004 | } |
| 1005 | |
| 1006 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) |
| 1007 | { |
| 1008 | s->rwstate=SSL_NOTHING; |
| 1009 | return(0); |
| 1010 | } |
| 1011 | return(s->method->ssl_read(s,buf,num)); |
| 1012 | } |
| 1013 | |
| 1014 | int SSL_peek(SSL *s,void *buf,int num) |
| 1015 | { |
| 1016 | if (s->handshake_func == 0) |
| 1017 | { |
| 1018 | OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED); |
| 1019 | return -1; |
| 1020 | } |
| 1021 | |
| 1022 | if (s->shutdown & SSL_RECEIVED_SHUTDOWN) |
| 1023 | { |
| 1024 | return(0); |
| 1025 | } |
| 1026 | return(s->method->ssl_peek(s,buf,num)); |
| 1027 | } |
| 1028 | |
| 1029 | int SSL_write(SSL *s,const void *buf,int num) |
| 1030 | { |
| 1031 | if (s->handshake_func == 0) |
| 1032 | { |
| 1033 | OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED); |
| 1034 | return -1; |
| 1035 | } |
| 1036 | |
| 1037 | if (s->shutdown & SSL_SENT_SHUTDOWN) |
| 1038 | { |
| 1039 | s->rwstate=SSL_NOTHING; |
| 1040 | OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 1041 | return(-1); |
| 1042 | } |
| 1043 | return(s->method->ssl_write(s,buf,num)); |
| 1044 | } |
| 1045 | |
| 1046 | int SSL_shutdown(SSL *s) |
| 1047 | { |
| 1048 | /* Note that this function behaves differently from what one might |
| 1049 | * expect. Return values are 0 for no success (yet), |
| 1050 | * 1 for success; but calling it once is usually not enough, |
| 1051 | * even if blocking I/O is used (see ssl3_shutdown). |
| 1052 | */ |
| 1053 | |
| 1054 | if (s->handshake_func == 0) |
| 1055 | { |
| 1056 | OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED); |
| 1057 | return -1; |
| 1058 | } |
| 1059 | |
| 1060 | if ((s != NULL) && !SSL_in_init(s)) |
| 1061 | return(s->method->ssl_shutdown(s)); |
| 1062 | else |
| 1063 | return(1); |
| 1064 | } |
| 1065 | |
| 1066 | int SSL_renegotiate(SSL *s) |
| 1067 | { |
| 1068 | if (s->renegotiate == 0) |
| 1069 | s->renegotiate=1; |
| 1070 | |
| 1071 | s->new_session=1; |
| 1072 | |
| 1073 | return(s->method->ssl_renegotiate(s)); |
| 1074 | } |
| 1075 | |
| 1076 | int SSL_renegotiate_abbreviated(SSL *s) |
| 1077 | { |
| 1078 | if (s->renegotiate == 0) |
| 1079 | s->renegotiate=1; |
| 1080 | |
| 1081 | s->new_session=0; |
| 1082 | |
| 1083 | return(s->method->ssl_renegotiate(s)); |
| 1084 | } |
| 1085 | |
| 1086 | int SSL_renegotiate_pending(SSL *s) |
| 1087 | { |
| 1088 | /* becomes true when negotiation is requested; |
| 1089 | * false again once a handshake has finished */ |
| 1090 | return (s->renegotiate != 0); |
| 1091 | } |
| 1092 | |
| 1093 | long SSL_ctrl(SSL *s,int cmd,long larg,void *parg) |
| 1094 | { |
| 1095 | long l; |
| 1096 | |
| 1097 | switch (cmd) |
| 1098 | { |
| 1099 | case SSL_CTRL_GET_READ_AHEAD: |
| 1100 | return(s->read_ahead); |
| 1101 | case SSL_CTRL_SET_READ_AHEAD: |
| 1102 | l=s->read_ahead; |
| 1103 | s->read_ahead=larg; |
| 1104 | return(l); |
| 1105 | |
| 1106 | case SSL_CTRL_SET_MSG_CALLBACK_ARG: |
| 1107 | s->msg_callback_arg = parg; |
| 1108 | return 1; |
| 1109 | |
| 1110 | case SSL_CTRL_OPTIONS: |
| 1111 | return(s->options|=larg); |
| 1112 | case SSL_CTRL_CLEAR_OPTIONS: |
| 1113 | return(s->options&=~larg); |
| 1114 | case SSL_CTRL_MODE: |
| 1115 | return(s->mode|=larg); |
| 1116 | case SSL_CTRL_CLEAR_MODE: |
| 1117 | return(s->mode &=~larg); |
| 1118 | case SSL_CTRL_GET_MAX_CERT_LIST: |
| 1119 | return(s->max_cert_list); |
| 1120 | case SSL_CTRL_SET_MAX_CERT_LIST: |
| 1121 | l=s->max_cert_list; |
| 1122 | s->max_cert_list=larg; |
| 1123 | return(l); |
| 1124 | case SSL_CTRL_SET_MTU: |
| 1125 | #ifndef OPENSSL_NO_DTLS1 |
| 1126 | if (larg < (long)dtls1_min_mtu()) |
| 1127 | return 0; |
| 1128 | #endif |
| 1129 | |
| 1130 | if (SSL_IS_DTLS(s)) |
| 1131 | { |
| 1132 | s->d1->mtu = larg; |
| 1133 | return larg; |
| 1134 | } |
| 1135 | return 0; |
| 1136 | case SSL_CTRL_SET_MAX_SEND_FRAGMENT: |
| 1137 | if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) |
| 1138 | return 0; |
| 1139 | s->max_send_fragment = larg; |
| 1140 | return 1; |
| 1141 | case SSL_CTRL_GET_RI_SUPPORT: |
| 1142 | if (s->s3) |
| 1143 | return s->s3->send_connection_binding; |
| 1144 | else return 0; |
| 1145 | case SSL_CTRL_CERT_FLAGS: |
| 1146 | return(s->cert->cert_flags|=larg); |
| 1147 | case SSL_CTRL_CLEAR_CERT_FLAGS: |
| 1148 | return(s->cert->cert_flags &=~larg); |
| 1149 | |
| 1150 | case SSL_CTRL_GET_RAW_CIPHERLIST: |
| 1151 | if (parg) |
| 1152 | { |
| 1153 | if (s->cert->ciphers_raw == NULL) |
| 1154 | return 0; |
| 1155 | *(unsigned char **)parg = s->cert->ciphers_raw; |
| 1156 | return (int)s->cert->ciphers_rawlen; |
| 1157 | } |
| 1158 | else |
| 1159 | return ssl_put_cipher_by_char(s,NULL,NULL); |
| 1160 | default: |
| 1161 | return(s->method->ssl_ctrl(s,cmd,larg,parg)); |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) |
| 1166 | { |
| 1167 | switch(cmd) |
| 1168 | { |
| 1169 | case SSL_CTRL_SET_MSG_CALLBACK: |
| 1170 | s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp); |
| 1171 | return 1; |
| 1172 | |
| 1173 | default: |
| 1174 | return(s->method->ssl_callback_ctrl(s,cmd,fp)); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) |
| 1179 | { |
| 1180 | return ctx->sessions; |
| 1181 | } |
| 1182 | |
| 1183 | long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg) |
| 1184 | { |
| 1185 | long l; |
| 1186 | |
| 1187 | switch (cmd) |
| 1188 | { |
| 1189 | case SSL_CTRL_GET_READ_AHEAD: |
| 1190 | return(ctx->read_ahead); |
| 1191 | case SSL_CTRL_SET_READ_AHEAD: |
| 1192 | l=ctx->read_ahead; |
| 1193 | ctx->read_ahead=larg; |
| 1194 | return(l); |
| 1195 | |
| 1196 | case SSL_CTRL_SET_MSG_CALLBACK_ARG: |
| 1197 | ctx->msg_callback_arg = parg; |
| 1198 | return 1; |
| 1199 | |
| 1200 | case SSL_CTRL_GET_MAX_CERT_LIST: |
| 1201 | return(ctx->max_cert_list); |
| 1202 | case SSL_CTRL_SET_MAX_CERT_LIST: |
| 1203 | l=ctx->max_cert_list; |
| 1204 | ctx->max_cert_list=larg; |
| 1205 | return(l); |
| 1206 | |
| 1207 | case SSL_CTRL_SET_SESS_CACHE_SIZE: |
| 1208 | l=ctx->session_cache_size; |
| 1209 | ctx->session_cache_size=larg; |
| 1210 | return(l); |
| 1211 | case SSL_CTRL_GET_SESS_CACHE_SIZE: |
| 1212 | return(ctx->session_cache_size); |
| 1213 | case SSL_CTRL_SET_SESS_CACHE_MODE: |
| 1214 | l=ctx->session_cache_mode; |
| 1215 | ctx->session_cache_mode=larg; |
| 1216 | return(l); |
| 1217 | case SSL_CTRL_GET_SESS_CACHE_MODE: |
| 1218 | return(ctx->session_cache_mode); |
| 1219 | |
| 1220 | case SSL_CTRL_SESS_NUMBER: |
| 1221 | return(lh_SSL_SESSION_num_items(ctx->sessions)); |
| 1222 | case SSL_CTRL_SESS_CONNECT: |
| 1223 | return(ctx->stats.sess_connect); |
| 1224 | case SSL_CTRL_SESS_CONNECT_GOOD: |
| 1225 | return(ctx->stats.sess_connect_good); |
| 1226 | case SSL_CTRL_SESS_CONNECT_RENEGOTIATE: |
| 1227 | return(ctx->stats.sess_connect_renegotiate); |
| 1228 | case SSL_CTRL_SESS_ACCEPT: |
| 1229 | return(ctx->stats.sess_accept); |
| 1230 | case SSL_CTRL_SESS_ACCEPT_GOOD: |
| 1231 | return(ctx->stats.sess_accept_good); |
| 1232 | case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE: |
| 1233 | return(ctx->stats.sess_accept_renegotiate); |
| 1234 | case SSL_CTRL_SESS_HIT: |
| 1235 | return(ctx->stats.sess_hit); |
| 1236 | case SSL_CTRL_SESS_CB_HIT: |
| 1237 | return(ctx->stats.sess_cb_hit); |
| 1238 | case SSL_CTRL_SESS_MISSES: |
| 1239 | return(ctx->stats.sess_miss); |
| 1240 | case SSL_CTRL_SESS_TIMEOUTS: |
| 1241 | return(ctx->stats.sess_timeout); |
| 1242 | case SSL_CTRL_SESS_CACHE_FULL: |
| 1243 | return(ctx->stats.sess_cache_full); |
| 1244 | case SSL_CTRL_OPTIONS: |
| 1245 | return(ctx->options|=larg); |
| 1246 | case SSL_CTRL_CLEAR_OPTIONS: |
| 1247 | return(ctx->options&=~larg); |
| 1248 | case SSL_CTRL_MODE: |
| 1249 | return(ctx->mode|=larg); |
| 1250 | case SSL_CTRL_CLEAR_MODE: |
| 1251 | return(ctx->mode&=~larg); |
| 1252 | case SSL_CTRL_SET_MAX_SEND_FRAGMENT: |
| 1253 | if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) |
| 1254 | return 0; |
| 1255 | ctx->max_send_fragment = larg; |
| 1256 | return 1; |
| 1257 | case SSL_CTRL_CERT_FLAGS: |
| 1258 | return(ctx->cert->cert_flags|=larg); |
| 1259 | case SSL_CTRL_CLEAR_CERT_FLAGS: |
| 1260 | return(ctx->cert->cert_flags &=~larg); |
| 1261 | default: |
| 1262 | return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg)); |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) |
| 1267 | { |
| 1268 | switch(cmd) |
| 1269 | { |
| 1270 | case SSL_CTRL_SET_MSG_CALLBACK: |
| 1271 | ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp); |
| 1272 | return 1; |
| 1273 | |
| 1274 | default: |
| 1275 | return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp)); |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | int ssl_cipher_id_cmp(const void *in_a, const void *in_b) |
| 1280 | { |
| 1281 | long l; |
| 1282 | const SSL_CIPHER *a = in_a; |
| 1283 | const SSL_CIPHER *b = in_b; |
| 1284 | const long a_id = a->id; |
| 1285 | const long b_id = b->id; |
| 1286 | |
| 1287 | l = a_id - b_id; |
| 1288 | if (l == 0L) |
| 1289 | return(0); |
| 1290 | else |
| 1291 | return((l > 0)?1:-1); |
| 1292 | } |
| 1293 | |
| 1294 | int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) |
| 1295 | { |
| 1296 | long l; |
| 1297 | const long a_id = (*ap)->id; |
| 1298 | const long b_id = (*bp)->id; |
| 1299 | |
| 1300 | l = a_id - b_id; |
| 1301 | if (l == 0) |
| 1302 | return(0); |
| 1303 | else |
| 1304 | return((l > 0)?1:-1); |
| 1305 | } |
| 1306 | |
| 1307 | /** return a STACK of the ciphers available for the SSL and in order of |
| 1308 | * preference */ |
| 1309 | STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) |
| 1310 | { |
Adam Langley | 0b5c1ac | 2014-06-20 12:00:00 -0700 | [diff] [blame^] | 1311 | if (s == NULL) |
| 1312 | return NULL; |
| 1313 | |
| 1314 | if (s->cipher_list != NULL) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1315 | { |
Adam Langley | 0b5c1ac | 2014-06-20 12:00:00 -0700 | [diff] [blame^] | 1316 | return(s->cipher_list); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1317 | } |
Adam Langley | 0b5c1ac | 2014-06-20 12:00:00 -0700 | [diff] [blame^] | 1318 | |
| 1319 | if (s->version >= TLS1_1_VERSION) |
| 1320 | { |
| 1321 | if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL) |
| 1322 | return s->ctx->cipher_list_tls11; |
| 1323 | } |
| 1324 | |
| 1325 | if ((s->ctx != NULL) && |
| 1326 | (s->ctx->cipher_list != NULL)) |
| 1327 | { |
| 1328 | return(s->ctx->cipher_list); |
| 1329 | } |
| 1330 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1331 | return(NULL); |
| 1332 | } |
| 1333 | |
| 1334 | /** return a STACK of the ciphers available for the SSL and in order of |
| 1335 | * algorithm id */ |
| 1336 | STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) |
| 1337 | { |
| 1338 | if (s != NULL) |
| 1339 | { |
| 1340 | if (s->cipher_list_by_id != NULL) |
| 1341 | { |
| 1342 | return(s->cipher_list_by_id); |
| 1343 | } |
| 1344 | else if ((s->ctx != NULL) && |
| 1345 | (s->ctx->cipher_list_by_id != NULL)) |
| 1346 | { |
| 1347 | return(s->ctx->cipher_list_by_id); |
| 1348 | } |
| 1349 | } |
| 1350 | return(NULL); |
| 1351 | } |
| 1352 | |
| 1353 | /** The old interface to get the same thing as SSL_get_ciphers() */ |
| 1354 | const char *SSL_get_cipher_list(const SSL *s,int n) |
| 1355 | { |
| 1356 | SSL_CIPHER *c; |
| 1357 | STACK_OF(SSL_CIPHER) *sk; |
| 1358 | |
| 1359 | if (s == NULL) return(NULL); |
| 1360 | sk=SSL_get_ciphers(s); |
| 1361 | if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n)) |
| 1362 | return(NULL); |
| 1363 | c=sk_SSL_CIPHER_value(sk,n); |
| 1364 | if (c == NULL) return(NULL); |
| 1365 | return(c->name); |
| 1366 | } |
| 1367 | |
| 1368 | /** specify the ciphers to be used by default by the SSL_CTX */ |
| 1369 | int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) |
| 1370 | { |
| 1371 | STACK_OF(SSL_CIPHER) *sk; |
| 1372 | |
| 1373 | sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list, |
| 1374 | &ctx->cipher_list_by_id,str, ctx->cert); |
| 1375 | /* ssl_create_cipher_list may return an empty stack if it |
| 1376 | * was unable to find a cipher matching the given rule string |
| 1377 | * (for example if the rule string specifies a cipher which |
| 1378 | * has been disabled). This is not an error as far as |
| 1379 | * ssl_create_cipher_list is concerned, and hence |
| 1380 | * ctx->cipher_list and ctx->cipher_list_by_id has been |
| 1381 | * updated. */ |
| 1382 | if (sk == NULL) |
| 1383 | return 0; |
| 1384 | else if (sk_SSL_CIPHER_num(sk) == 0) |
| 1385 | { |
| 1386 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH); |
| 1387 | return 0; |
| 1388 | } |
| 1389 | return 1; |
| 1390 | } |
| 1391 | |
Adam Langley | 0b5c1ac | 2014-06-20 12:00:00 -0700 | [diff] [blame^] | 1392 | int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) |
| 1393 | { |
| 1394 | STACK_OF(SSL_CIPHER) *sk; |
| 1395 | |
| 1396 | sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert); |
| 1397 | if (sk == NULL) |
| 1398 | return 0; |
| 1399 | else if (sk_SSL_CIPHER_num(sk) == 0) |
| 1400 | { |
| 1401 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH); |
| 1402 | return 0; |
| 1403 | } |
| 1404 | return 1; |
| 1405 | } |
| 1406 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1407 | /** specify the ciphers to be used by the SSL */ |
| 1408 | int SSL_set_cipher_list(SSL *s,const char *str) |
| 1409 | { |
| 1410 | STACK_OF(SSL_CIPHER) *sk; |
| 1411 | |
| 1412 | sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list, |
| 1413 | &s->cipher_list_by_id,str, s->cert); |
| 1414 | /* see comment in SSL_CTX_set_cipher_list */ |
| 1415 | if (sk == NULL) |
| 1416 | return 0; |
| 1417 | else if (sk_SSL_CIPHER_num(sk) == 0) |
| 1418 | { |
| 1419 | OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH); |
| 1420 | return 0; |
| 1421 | } |
| 1422 | return 1; |
| 1423 | } |
| 1424 | |
| 1425 | /* works well for SSLv2, not so good for SSLv3 */ |
| 1426 | char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len) |
| 1427 | { |
| 1428 | char *p; |
| 1429 | STACK_OF(SSL_CIPHER) *sk; |
| 1430 | SSL_CIPHER *c; |
| 1431 | int i; |
| 1432 | |
| 1433 | if ((s->session == NULL) || (s->session->ciphers == NULL) || |
| 1434 | (len < 2)) |
| 1435 | return(NULL); |
| 1436 | |
| 1437 | p=buf; |
| 1438 | sk=s->session->ciphers; |
| 1439 | for (i=0; i<sk_SSL_CIPHER_num(sk); i++) |
| 1440 | { |
| 1441 | int n; |
| 1442 | |
| 1443 | c=sk_SSL_CIPHER_value(sk,i); |
| 1444 | n=strlen(c->name); |
| 1445 | if (n+1 > len) |
| 1446 | { |
| 1447 | if (p != buf) |
| 1448 | --p; |
| 1449 | *p='\0'; |
| 1450 | return buf; |
| 1451 | } |
| 1452 | strcpy(p,c->name); |
| 1453 | p+=n; |
| 1454 | *(p++)=':'; |
| 1455 | len-=n+1; |
| 1456 | } |
| 1457 | p[-1]='\0'; |
| 1458 | return(buf); |
| 1459 | } |
| 1460 | |
| 1461 | int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p, |
| 1462 | int (*put_cb)(const SSL_CIPHER *, unsigned char *)) |
| 1463 | { |
| 1464 | int i,j=0; |
| 1465 | SSL_CIPHER *c; |
| 1466 | CERT *ct = s->cert; |
| 1467 | unsigned char *q; |
| 1468 | int no_scsv = s->renegotiate; |
| 1469 | /* Set disabled masks for this session */ |
| 1470 | ssl_set_client_disabled(s); |
| 1471 | |
| 1472 | if (sk == NULL) return(0); |
| 1473 | q=p; |
| 1474 | |
| 1475 | for (i=0; i<sk_SSL_CIPHER_num(sk); i++) |
| 1476 | { |
| 1477 | c=sk_SSL_CIPHER_value(sk,i); |
| 1478 | /* Skip disabled ciphers */ |
| 1479 | if (c->algorithm_ssl & ct->mask_ssl || |
| 1480 | c->algorithm_mkey & ct->mask_k || |
| 1481 | c->algorithm_auth & ct->mask_a) |
| 1482 | continue; |
| 1483 | #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL |
| 1484 | if (c->id == SSL3_CK_SCSV) |
| 1485 | { |
| 1486 | if (no_scsv) |
| 1487 | continue; |
| 1488 | else |
| 1489 | no_scsv = 1; |
| 1490 | } |
| 1491 | #endif |
| 1492 | j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p); |
| 1493 | p+=j; |
| 1494 | } |
| 1495 | /* If p == q, no ciphers and caller indicates an error. Otherwise |
| 1496 | * add SCSV if not renegotiating. |
| 1497 | */ |
| 1498 | if (p != q && !no_scsv) |
| 1499 | { |
| 1500 | static SSL_CIPHER scsv = |
| 1501 | { |
| 1502 | 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 1503 | }; |
| 1504 | j = put_cb ? put_cb(&scsv,p) : ssl_put_cipher_by_char(s,&scsv,p); |
| 1505 | p+=j; |
| 1506 | #ifdef OPENSSL_RI_DEBUG |
| 1507 | fprintf(stderr, "SCSV sent by client\n"); |
| 1508 | #endif |
| 1509 | } |
| 1510 | |
| 1511 | return(p-q); |
| 1512 | } |
| 1513 | |
| 1514 | STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num, |
| 1515 | STACK_OF(SSL_CIPHER) **skp) |
| 1516 | { |
| 1517 | const SSL_CIPHER *c; |
| 1518 | STACK_OF(SSL_CIPHER) *sk; |
| 1519 | int i,n; |
| 1520 | if (s->s3) |
| 1521 | s->s3->send_connection_binding = 0; |
| 1522 | |
| 1523 | n=ssl_put_cipher_by_char(s,NULL,NULL); |
| 1524 | if ((num%n) != 0) |
| 1525 | { |
| 1526 | OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); |
| 1527 | return(NULL); |
| 1528 | } |
| 1529 | if ((skp == NULL) || (*skp == NULL)) |
| 1530 | sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */ |
| 1531 | else |
| 1532 | { |
| 1533 | sk= *skp; |
| 1534 | sk_SSL_CIPHER_zero(sk); |
| 1535 | } |
| 1536 | |
| 1537 | if (s->cert->ciphers_raw) |
| 1538 | OPENSSL_free(s->cert->ciphers_raw); |
| 1539 | s->cert->ciphers_raw = BUF_memdup(p, num); |
| 1540 | if (s->cert->ciphers_raw == NULL) |
| 1541 | { |
| 1542 | OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE); |
| 1543 | goto err; |
| 1544 | } |
| 1545 | s->cert->ciphers_rawlen = (size_t)num; |
| 1546 | |
| 1547 | for (i=0; i<num; i+=n) |
| 1548 | { |
| 1549 | /* Check for SCSV */ |
| 1550 | if (s->s3 && (n != 3 || !p[0]) && |
| 1551 | (p[n-2] == ((SSL3_CK_SCSV >> 8) & 0xff)) && |
| 1552 | (p[n-1] == (SSL3_CK_SCSV & 0xff))) |
| 1553 | { |
| 1554 | /* SCSV fatal if renegotiating */ |
| 1555 | if (s->renegotiate) |
| 1556 | { |
| 1557 | OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING); |
| 1558 | ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); |
| 1559 | goto err; |
| 1560 | } |
| 1561 | s->s3->send_connection_binding = 1; |
| 1562 | p += n; |
| 1563 | #ifdef OPENSSL_RI_DEBUG |
| 1564 | fprintf(stderr, "SCSV received by server\n"); |
| 1565 | #endif |
| 1566 | continue; |
| 1567 | } |
| 1568 | |
| 1569 | c=ssl_get_cipher_by_char(s,p); |
| 1570 | p+=n; |
| 1571 | if (c != NULL) |
| 1572 | { |
| 1573 | if (!sk_SSL_CIPHER_push(sk,c)) |
| 1574 | { |
| 1575 | OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE); |
| 1576 | goto err; |
| 1577 | } |
| 1578 | } |
| 1579 | } |
| 1580 | |
| 1581 | if (skp != NULL) |
| 1582 | *skp=sk; |
| 1583 | return(sk); |
| 1584 | err: |
| 1585 | if ((skp == NULL) || (*skp == NULL)) |
| 1586 | sk_SSL_CIPHER_free(sk); |
| 1587 | return(NULL); |
| 1588 | } |
| 1589 | |
| 1590 | |
| 1591 | #ifndef OPENSSL_NO_TLSEXT |
| 1592 | /** return a servername extension value if provided in Client Hello, or NULL. |
| 1593 | * So far, only host_name types are defined (RFC 3546). |
| 1594 | */ |
| 1595 | |
| 1596 | const char *SSL_get_servername(const SSL *s, const int type) |
| 1597 | { |
| 1598 | if (type != TLSEXT_NAMETYPE_host_name) |
| 1599 | return NULL; |
| 1600 | |
| 1601 | return s->session && !s->tlsext_hostname ? |
| 1602 | s->session->tlsext_hostname : |
| 1603 | s->tlsext_hostname; |
| 1604 | } |
| 1605 | |
| 1606 | int SSL_get_servername_type(const SSL *s) |
| 1607 | { |
| 1608 | if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) |
| 1609 | return TLSEXT_NAMETYPE_host_name; |
| 1610 | return -1; |
| 1611 | } |
| 1612 | |
| 1613 | /* SSL_select_next_proto implements the standard protocol selection. It is |
| 1614 | * expected that this function is called from the callback set by |
| 1615 | * SSL_CTX_set_next_proto_select_cb. |
| 1616 | * |
| 1617 | * The protocol data is assumed to be a vector of 8-bit, length prefixed byte |
| 1618 | * strings. The length byte itself is not included in the length. A byte |
| 1619 | * string of length 0 is invalid. No byte string may be truncated. |
| 1620 | * |
| 1621 | * The current, but experimental algorithm for selecting the protocol is: |
| 1622 | * |
| 1623 | * 1) If the server doesn't support NPN then this is indicated to the |
| 1624 | * callback. In this case, the client application has to abort the connection |
| 1625 | * or have a default application level protocol. |
| 1626 | * |
| 1627 | * 2) If the server supports NPN, but advertises an empty list then the |
| 1628 | * client selects the first protcol in its list, but indicates via the |
| 1629 | * API that this fallback case was enacted. |
| 1630 | * |
| 1631 | * 3) Otherwise, the client finds the first protocol in the server's list |
| 1632 | * that it supports and selects this protocol. This is because it's |
| 1633 | * assumed that the server has better information about which protocol |
| 1634 | * a client should use. |
| 1635 | * |
| 1636 | * 4) If the client doesn't support any of the server's advertised |
| 1637 | * protocols, then this is treated the same as case 2. |
| 1638 | * |
| 1639 | * It returns either |
| 1640 | * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or |
| 1641 | * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached. |
| 1642 | */ |
| 1643 | int SSL_select_next_proto(unsigned char **out, unsigned char *outlen, const unsigned char *server, unsigned int server_len, const unsigned char *client, unsigned int client_len) |
| 1644 | { |
| 1645 | unsigned int i, j; |
| 1646 | const unsigned char *result; |
| 1647 | int status = OPENSSL_NPN_UNSUPPORTED; |
| 1648 | |
| 1649 | /* For each protocol in server preference order, see if we support it. */ |
| 1650 | for (i = 0; i < server_len; ) |
| 1651 | { |
| 1652 | for (j = 0; j < client_len; ) |
| 1653 | { |
| 1654 | if (server[i] == client[j] && |
| 1655 | memcmp(&server[i+1], &client[j+1], server[i]) == 0) |
| 1656 | { |
| 1657 | /* We found a match */ |
| 1658 | result = &server[i]; |
| 1659 | status = OPENSSL_NPN_NEGOTIATED; |
| 1660 | goto found; |
| 1661 | } |
| 1662 | j += client[j]; |
| 1663 | j++; |
| 1664 | } |
| 1665 | i += server[i]; |
| 1666 | i++; |
| 1667 | } |
| 1668 | |
| 1669 | /* There's no overlap between our protocols and the server's list. */ |
| 1670 | result = client; |
| 1671 | status = OPENSSL_NPN_NO_OVERLAP; |
| 1672 | |
| 1673 | found: |
| 1674 | *out = (unsigned char *) result + 1; |
| 1675 | *outlen = result[0]; |
| 1676 | return status; |
| 1677 | } |
| 1678 | |
| 1679 | # ifndef OPENSSL_NO_NEXTPROTONEG |
| 1680 | /* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's |
| 1681 | * requested protocol for this connection and returns 0. If the client didn't |
| 1682 | * request any protocol, then *data is set to NULL. |
| 1683 | * |
| 1684 | * Note that the client can request any protocol it chooses. The value returned |
| 1685 | * from this function need not be a member of the list of supported protocols |
| 1686 | * provided by the callback. |
| 1687 | */ |
| 1688 | void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data, unsigned *len) |
| 1689 | { |
| 1690 | *data = s->next_proto_negotiated; |
| 1691 | if (!*data) { |
| 1692 | *len = 0; |
| 1693 | } else { |
| 1694 | *len = s->next_proto_negotiated_len; |
| 1695 | } |
| 1696 | } |
| 1697 | |
| 1698 | /* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a |
| 1699 | * TLS server needs a list of supported protocols for Next Protocol |
| 1700 | * Negotiation. The returned list must be in wire format. The list is returned |
| 1701 | * by setting |out| to point to it and |outlen| to its length. This memory will |
| 1702 | * not be modified, but one should assume that the SSL* keeps a reference to |
| 1703 | * it. |
| 1704 | * |
| 1705 | * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no |
| 1706 | * such extension will be included in the ServerHello. */ |
| 1707 | void SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx, int (*cb) (SSL *ssl, const unsigned char **out, unsigned int *outlen, void *arg), void *arg) |
| 1708 | { |
| 1709 | ctx->next_protos_advertised_cb = cb; |
| 1710 | ctx->next_protos_advertised_cb_arg = arg; |
| 1711 | } |
| 1712 | |
| 1713 | /* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a |
| 1714 | * client needs to select a protocol from the server's provided list. |out| |
| 1715 | * must be set to point to the selected protocol (which may be within |in|). |
| 1716 | * The length of the protocol name must be written into |outlen|. The server's |
| 1717 | * advertised protocols are provided in |in| and |inlen|. The callback can |
| 1718 | * assume that |in| is syntactically valid. |
| 1719 | * |
| 1720 | * The client must select a protocol. It is fatal to the connection if this |
| 1721 | * callback returns a value other than SSL_TLSEXT_ERR_OK. |
| 1722 | */ |
| 1723 | void SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, int (*cb) (SSL *s, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg), void *arg) |
| 1724 | { |
| 1725 | ctx->next_proto_select_cb = cb; |
| 1726 | ctx->next_proto_select_cb_arg = arg; |
| 1727 | } |
| 1728 | # endif |
| 1729 | |
| 1730 | int SSL_CTX_set_custom_cli_ext(SSL_CTX *ctx, unsigned short ext_type, |
| 1731 | custom_cli_ext_first_cb_fn fn1, |
| 1732 | custom_cli_ext_second_cb_fn fn2, void* arg) |
| 1733 | { |
| 1734 | size_t i; |
| 1735 | custom_cli_ext_record* record; |
| 1736 | |
| 1737 | /* Check for duplicates */ |
| 1738 | for (i=0; i < ctx->custom_cli_ext_records_count; i++) |
| 1739 | if (ext_type == ctx->custom_cli_ext_records[i].ext_type) |
| 1740 | return 0; |
| 1741 | |
| 1742 | ctx->custom_cli_ext_records = OPENSSL_realloc(ctx->custom_cli_ext_records, |
| 1743 | (ctx->custom_cli_ext_records_count + 1) * |
| 1744 | sizeof(custom_cli_ext_record)); |
| 1745 | if (!ctx->custom_cli_ext_records) { |
| 1746 | ctx->custom_cli_ext_records_count = 0; |
| 1747 | return 0; |
| 1748 | } |
| 1749 | ctx->custom_cli_ext_records_count++; |
| 1750 | record = &ctx->custom_cli_ext_records[ctx->custom_cli_ext_records_count - 1]; |
| 1751 | record->ext_type = ext_type; |
| 1752 | record->fn1 = fn1; |
| 1753 | record->fn2 = fn2; |
| 1754 | record->arg = arg; |
| 1755 | return 1; |
| 1756 | } |
| 1757 | |
| 1758 | int SSL_CTX_set_custom_srv_ext(SSL_CTX *ctx, unsigned short ext_type, |
| 1759 | custom_srv_ext_first_cb_fn fn1, |
| 1760 | custom_srv_ext_second_cb_fn fn2, void* arg) |
| 1761 | { |
| 1762 | size_t i; |
| 1763 | custom_srv_ext_record* record; |
| 1764 | |
| 1765 | /* Check for duplicates */ |
| 1766 | for (i=0; i < ctx->custom_srv_ext_records_count; i++) |
| 1767 | if (ext_type == ctx->custom_srv_ext_records[i].ext_type) |
| 1768 | return 0; |
| 1769 | |
| 1770 | ctx->custom_srv_ext_records = OPENSSL_realloc(ctx->custom_srv_ext_records, |
| 1771 | (ctx->custom_srv_ext_records_count + 1) * |
| 1772 | sizeof(custom_srv_ext_record)); |
| 1773 | if (!ctx->custom_srv_ext_records) { |
| 1774 | ctx->custom_srv_ext_records_count = 0; |
| 1775 | return 0; |
| 1776 | } |
| 1777 | ctx->custom_srv_ext_records_count++; |
| 1778 | record = &ctx->custom_srv_ext_records[ctx->custom_srv_ext_records_count - 1]; |
| 1779 | record->ext_type = ext_type; |
| 1780 | record->fn1 = fn1; |
| 1781 | record->fn2 = fn2; |
| 1782 | record->arg = arg; |
| 1783 | return 1; |
| 1784 | } |
| 1785 | |
| 1786 | /* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|. |
| 1787 | * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit |
| 1788 | * length-prefixed strings). |
| 1789 | * |
| 1790 | * Returns 0 on success. */ |
| 1791 | int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos, |
| 1792 | unsigned protos_len) |
| 1793 | { |
| 1794 | if (ctx->alpn_client_proto_list) |
| 1795 | OPENSSL_free(ctx->alpn_client_proto_list); |
| 1796 | |
| 1797 | ctx->alpn_client_proto_list = OPENSSL_malloc(protos_len); |
| 1798 | if (!ctx->alpn_client_proto_list) |
| 1799 | return 1; |
| 1800 | memcpy(ctx->alpn_client_proto_list, protos, protos_len); |
| 1801 | ctx->alpn_client_proto_list_len = protos_len; |
| 1802 | |
| 1803 | return 0; |
| 1804 | } |
| 1805 | |
| 1806 | /* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|. |
| 1807 | * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit |
| 1808 | * length-prefixed strings). |
| 1809 | * |
| 1810 | * Returns 0 on success. */ |
| 1811 | int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos, |
| 1812 | unsigned protos_len) |
| 1813 | { |
| 1814 | if (ssl->alpn_client_proto_list) |
| 1815 | OPENSSL_free(ssl->alpn_client_proto_list); |
| 1816 | |
| 1817 | ssl->alpn_client_proto_list = OPENSSL_malloc(protos_len); |
| 1818 | if (!ssl->alpn_client_proto_list) |
| 1819 | return 1; |
| 1820 | memcpy(ssl->alpn_client_proto_list, protos, protos_len); |
| 1821 | ssl->alpn_client_proto_list_len = protos_len; |
| 1822 | |
| 1823 | return 0; |
| 1824 | } |
| 1825 | |
| 1826 | /* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called |
| 1827 | * during ClientHello processing in order to select an ALPN protocol from the |
| 1828 | * client's list of offered protocols. */ |
| 1829 | void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx, |
| 1830 | int (*cb) (SSL *ssl, |
| 1831 | const unsigned char **out, |
| 1832 | unsigned char *outlen, |
| 1833 | const unsigned char *in, |
| 1834 | unsigned int inlen, |
| 1835 | void *arg), |
| 1836 | void *arg) |
| 1837 | { |
| 1838 | ctx->alpn_select_cb = cb; |
| 1839 | ctx->alpn_select_cb_arg = arg; |
| 1840 | } |
| 1841 | |
| 1842 | /* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|. |
| 1843 | * On return it sets |*data| to point to |*len| bytes of protocol name (not |
| 1844 | * including the leading length-prefix byte). If the server didn't respond with |
| 1845 | * a negotiated protocol then |*len| will be zero. */ |
| 1846 | void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data, |
| 1847 | unsigned *len) |
| 1848 | { |
| 1849 | *data = NULL; |
| 1850 | if (ssl->s3) |
| 1851 | *data = ssl->s3->alpn_selected; |
| 1852 | if (*data == NULL) |
| 1853 | *len = 0; |
| 1854 | else |
| 1855 | *len = ssl->s3->alpn_selected_len; |
| 1856 | } |
| 1857 | #endif /* !OPENSSL_NO_TLSEXT */ |
| 1858 | |
| 1859 | int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen, |
| 1860 | const char *label, size_t llen, const unsigned char *p, size_t plen, |
| 1861 | int use_context) |
| 1862 | { |
| 1863 | if (s->version < TLS1_VERSION) |
| 1864 | return -1; |
| 1865 | |
| 1866 | return s->method->ssl3_enc->export_keying_material(s, out, olen, label, |
| 1867 | llen, p, plen, |
| 1868 | use_context); |
| 1869 | } |
| 1870 | |
| 1871 | static uint32_t ssl_session_hash(const SSL_SESSION *a) |
| 1872 | { |
| 1873 | uint32_t hash = ((uint32_t) a->session_id[0]) || |
| 1874 | ((uint32_t) a->session_id[1] << 8) || |
| 1875 | ((uint32_t) a->session_id[2] << 16) || |
| 1876 | ((uint32_t) a->session_id[3] << 24); |
| 1877 | |
| 1878 | return hash; |
| 1879 | } |
| 1880 | |
| 1881 | /* NB: If this function (or indeed the hash function which uses a sort of |
| 1882 | * coarser function than this one) is changed, ensure |
| 1883 | * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being |
| 1884 | * able to construct an SSL_SESSION that will collide with any existing session |
| 1885 | * with a matching session ID. */ |
| 1886 | static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) |
| 1887 | { |
| 1888 | if (a->ssl_version != b->ssl_version) |
| 1889 | return(1); |
| 1890 | if (a->session_id_length != b->session_id_length) |
| 1891 | return(1); |
| 1892 | return(memcmp(a->session_id,b->session_id,a->session_id_length)); |
| 1893 | } |
| 1894 | |
| 1895 | SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) |
| 1896 | { |
| 1897 | SSL_CTX *ret=NULL; |
| 1898 | |
| 1899 | if (meth == NULL) |
| 1900 | { |
| 1901 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED); |
| 1902 | return(NULL); |
| 1903 | } |
| 1904 | |
| 1905 | #ifdef OPENSSL_FIPS |
| 1906 | if (FIPS_mode() && (meth->version < TLS1_VERSION)) |
| 1907 | { |
| 1908 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE); |
| 1909 | return NULL; |
| 1910 | } |
| 1911 | #endif |
| 1912 | |
| 1913 | if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) |
| 1914 | { |
| 1915 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS); |
| 1916 | goto err; |
| 1917 | } |
| 1918 | ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX)); |
| 1919 | if (ret == NULL) |
| 1920 | goto err; |
| 1921 | |
| 1922 | memset(ret,0,sizeof(SSL_CTX)); |
| 1923 | |
| 1924 | ret->method=meth; |
| 1925 | |
| 1926 | ret->cert_store=NULL; |
| 1927 | ret->session_cache_mode=SSL_SESS_CACHE_SERVER; |
| 1928 | ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; |
| 1929 | ret->session_cache_head=NULL; |
| 1930 | ret->session_cache_tail=NULL; |
| 1931 | |
| 1932 | /* We take the system default */ |
| 1933 | ret->session_timeout=meth->get_timeout(); |
| 1934 | |
| 1935 | ret->new_session_cb=0; |
| 1936 | ret->remove_session_cb=0; |
| 1937 | ret->get_session_cb=0; |
| 1938 | ret->generate_session_id=0; |
| 1939 | |
| 1940 | memset((char *)&ret->stats,0,sizeof(ret->stats)); |
| 1941 | |
| 1942 | ret->references=1; |
| 1943 | ret->quiet_shutdown=0; |
| 1944 | |
| 1945 | /* ret->cipher=NULL;*/ |
| 1946 | /* ret->s2->challenge=NULL; |
| 1947 | ret->master_key=NULL; |
| 1948 | ret->key_arg=NULL; |
| 1949 | ret->s2->conn_id=NULL; */ |
| 1950 | |
| 1951 | ret->info_callback=NULL; |
| 1952 | |
| 1953 | ret->app_verify_callback=0; |
| 1954 | ret->app_verify_arg=NULL; |
| 1955 | |
| 1956 | ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT; |
| 1957 | ret->read_ahead=0; |
| 1958 | ret->msg_callback=0; |
| 1959 | ret->msg_callback_arg=NULL; |
| 1960 | ret->verify_mode=SSL_VERIFY_NONE; |
| 1961 | #if 0 |
| 1962 | ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */ |
| 1963 | #endif |
| 1964 | ret->sid_ctx_length=0; |
| 1965 | ret->default_verify_callback=NULL; |
| 1966 | if ((ret->cert=ssl_cert_new()) == NULL) |
| 1967 | goto err; |
| 1968 | |
| 1969 | ret->default_passwd_callback=0; |
| 1970 | ret->default_passwd_callback_userdata=NULL; |
| 1971 | ret->client_cert_cb=0; |
| 1972 | ret->app_gen_cookie_cb=0; |
| 1973 | ret->app_verify_cookie_cb=0; |
| 1974 | |
| 1975 | ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp); |
| 1976 | if (ret->sessions == NULL) goto err; |
| 1977 | ret->cert_store=X509_STORE_new(); |
| 1978 | if (ret->cert_store == NULL) goto err; |
| 1979 | |
| 1980 | ssl_create_cipher_list(ret->method, |
| 1981 | &ret->cipher_list,&ret->cipher_list_by_id, |
| 1982 | meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST, ret->cert); |
| 1983 | if (ret->cipher_list == NULL |
| 1984 | || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) |
| 1985 | { |
| 1986 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS); |
| 1987 | goto err2; |
| 1988 | } |
| 1989 | |
| 1990 | ret->param = X509_VERIFY_PARAM_new(); |
| 1991 | if (!ret->param) |
| 1992 | goto err; |
| 1993 | |
| 1994 | ret->rsa_md5 = EVP_md5(); |
| 1995 | ret->md5 = EVP_md5(); |
| 1996 | ret->sha1 = EVP_sha1(); |
| 1997 | |
| 1998 | if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL) |
| 1999 | goto err; |
| 2000 | |
| 2001 | CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data); |
| 2002 | |
| 2003 | ret->extra_certs=NULL; |
| 2004 | /* No compression for DTLS */ |
| 2005 | if (meth->version != DTLS1_VERSION) |
| 2006 | ret->comp_methods=SSL_COMP_get_compression_methods(); |
| 2007 | |
| 2008 | ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
| 2009 | |
| 2010 | #ifndef OPENSSL_NO_TLSEXT |
| 2011 | ret->tlsext_servername_callback = 0; |
| 2012 | ret->tlsext_servername_arg = NULL; |
| 2013 | /* Setup RFC4507 ticket keys */ |
| 2014 | if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0) |
| 2015 | || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0) |
| 2016 | || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0)) |
| 2017 | ret->options |= SSL_OP_NO_TICKET; |
| 2018 | |
| 2019 | ret->tlsext_status_cb = 0; |
| 2020 | ret->tlsext_status_arg = NULL; |
| 2021 | |
| 2022 | # ifndef OPENSSL_NO_NEXTPROTONEG |
| 2023 | ret->next_protos_advertised_cb = 0; |
| 2024 | ret->next_proto_select_cb = 0; |
| 2025 | # endif |
| 2026 | #endif |
| 2027 | #ifndef OPENSSL_NO_PSK |
| 2028 | ret->psk_identity_hint=NULL; |
| 2029 | ret->psk_client_callback=NULL; |
| 2030 | ret->psk_server_callback=NULL; |
| 2031 | #endif |
| 2032 | ret->custom_cli_ext_records = NULL; |
| 2033 | ret->custom_cli_ext_records_count = 0; |
| 2034 | ret->custom_srv_ext_records = NULL; |
| 2035 | ret->custom_srv_ext_records_count = 0; |
| 2036 | #ifndef OPENSSL_NO_BUF_FREELISTS |
| 2037 | ret->freelist_max_len = SSL_MAX_BUF_FREELIST_LEN_DEFAULT; |
| 2038 | ret->rbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST)); |
| 2039 | if (!ret->rbuf_freelist) |
| 2040 | goto err; |
| 2041 | ret->rbuf_freelist->chunklen = 0; |
| 2042 | ret->rbuf_freelist->len = 0; |
| 2043 | ret->rbuf_freelist->head = NULL; |
| 2044 | ret->wbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST)); |
| 2045 | if (!ret->wbuf_freelist) |
| 2046 | { |
| 2047 | OPENSSL_free(ret->rbuf_freelist); |
| 2048 | goto err; |
| 2049 | } |
| 2050 | ret->wbuf_freelist->chunklen = 0; |
| 2051 | ret->wbuf_freelist->len = 0; |
| 2052 | ret->wbuf_freelist->head = NULL; |
| 2053 | #endif |
| 2054 | #ifndef OPENSSL_NO_ENGINE |
| 2055 | ret->client_cert_engine = NULL; |
| 2056 | #ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO |
| 2057 | #define eng_strx(x) #x |
| 2058 | #define eng_str(x) eng_strx(x) |
| 2059 | /* Use specific client engine automatically... ignore errors */ |
| 2060 | { |
| 2061 | ENGINE *eng; |
| 2062 | eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); |
| 2063 | if (!eng) |
| 2064 | { |
| 2065 | ERR_clear_error(); |
| 2066 | ENGINE_load_builtin_engines(); |
| 2067 | eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO)); |
| 2068 | } |
| 2069 | if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng)) |
| 2070 | ERR_clear_error(); |
| 2071 | } |
| 2072 | #endif |
| 2073 | #endif |
| 2074 | /* Default is to connect to non-RI servers. When RI is more widely |
| 2075 | * deployed might change this. |
| 2076 | */ |
| 2077 | ret->options |= SSL_OP_LEGACY_SERVER_CONNECT; |
| 2078 | |
| 2079 | return(ret); |
| 2080 | err: |
| 2081 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE); |
| 2082 | err2: |
| 2083 | if (ret != NULL) SSL_CTX_free(ret); |
| 2084 | return(NULL); |
| 2085 | } |
| 2086 | |
| 2087 | #if 0 |
| 2088 | static void SSL_COMP_free(SSL_COMP *comp) |
| 2089 | { OPENSSL_free(comp); } |
| 2090 | #endif |
| 2091 | |
| 2092 | #ifndef OPENSSL_NO_BUF_FREELISTS |
| 2093 | static void |
| 2094 | ssl_buf_freelist_free(SSL3_BUF_FREELIST *list) |
| 2095 | { |
| 2096 | SSL3_BUF_FREELIST_ENTRY *ent, *next; |
| 2097 | for (ent = list->head; ent; ent = next) |
| 2098 | { |
| 2099 | next = ent->next; |
| 2100 | OPENSSL_free(ent); |
| 2101 | } |
| 2102 | OPENSSL_free(list); |
| 2103 | } |
| 2104 | #endif |
| 2105 | |
| 2106 | void SSL_CTX_free(SSL_CTX *a) |
| 2107 | { |
| 2108 | int i; |
| 2109 | |
| 2110 | if (a == NULL) return; |
| 2111 | |
| 2112 | i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX); |
| 2113 | #ifdef REF_PRINT |
| 2114 | REF_PRINT("SSL_CTX",a); |
| 2115 | #endif |
| 2116 | if (i > 0) return; |
| 2117 | #ifdef REF_CHECK |
| 2118 | if (i < 0) |
| 2119 | { |
| 2120 | fprintf(stderr,"SSL_CTX_free, bad reference count\n"); |
| 2121 | abort(); /* ok */ |
| 2122 | } |
| 2123 | #endif |
| 2124 | |
| 2125 | if (a->param) |
| 2126 | X509_VERIFY_PARAM_free(a->param); |
| 2127 | |
| 2128 | /* |
| 2129 | * Free internal session cache. However: the remove_cb() may reference |
| 2130 | * the ex_data of SSL_CTX, thus the ex_data store can only be removed |
| 2131 | * after the sessions were flushed. |
| 2132 | * As the ex_data handling routines might also touch the session cache, |
| 2133 | * the most secure solution seems to be: empty (flush) the cache, then |
| 2134 | * free ex_data, then finally free the cache. |
| 2135 | * (See ticket [openssl.org #212].) |
| 2136 | */ |
| 2137 | if (a->sessions != NULL) |
| 2138 | SSL_CTX_flush_sessions(a,0); |
| 2139 | |
| 2140 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data); |
| 2141 | |
| 2142 | if (a->sessions != NULL) |
| 2143 | lh_SSL_SESSION_free(a->sessions); |
| 2144 | |
| 2145 | if (a->cert_store != NULL) |
| 2146 | X509_STORE_free(a->cert_store); |
| 2147 | if (a->cipher_list != NULL) |
| 2148 | sk_SSL_CIPHER_free(a->cipher_list); |
| 2149 | if (a->cipher_list_by_id != NULL) |
| 2150 | sk_SSL_CIPHER_free(a->cipher_list_by_id); |
Adam Langley | 0b5c1ac | 2014-06-20 12:00:00 -0700 | [diff] [blame^] | 2151 | if (a->cipher_list_tls11 != NULL) |
| 2152 | sk_SSL_CIPHER_free(a->cipher_list_tls11); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2153 | if (a->cert != NULL) |
| 2154 | ssl_cert_free(a->cert); |
| 2155 | if (a->client_CA != NULL) |
| 2156 | sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free); |
| 2157 | if (a->extra_certs != NULL) |
| 2158 | sk_X509_pop_free(a->extra_certs,X509_free); |
| 2159 | #if 0 /* This should never be done, since it removes a global database */ |
| 2160 | if (a->comp_methods != NULL) |
| 2161 | sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free); |
| 2162 | #else |
| 2163 | a->comp_methods = NULL; |
| 2164 | #endif |
| 2165 | |
| 2166 | if (a->srtp_profiles) |
| 2167 | sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles); |
| 2168 | |
| 2169 | #ifndef OPENSSL_NO_PSK |
| 2170 | if (a->psk_identity_hint) |
| 2171 | OPENSSL_free(a->psk_identity_hint); |
| 2172 | #endif |
| 2173 | #ifndef OPENSSL_NO_TLSEXT |
| 2174 | OPENSSL_free(a->custom_cli_ext_records); |
| 2175 | OPENSSL_free(a->custom_srv_ext_records); |
| 2176 | #endif |
| 2177 | |
| 2178 | /* TODO(fork): remove. */ |
| 2179 | #if 0 |
| 2180 | #ifndef OPENSSL_NO_ENGINE |
| 2181 | if (a->client_cert_engine) |
| 2182 | ENGINE_finish(a->client_cert_engine); |
| 2183 | #endif |
| 2184 | #endif |
| 2185 | |
| 2186 | #ifndef OPENSSL_NO_BUF_FREELISTS |
| 2187 | if (a->wbuf_freelist) |
| 2188 | ssl_buf_freelist_free(a->wbuf_freelist); |
| 2189 | if (a->rbuf_freelist) |
| 2190 | ssl_buf_freelist_free(a->rbuf_freelist); |
| 2191 | #endif |
| 2192 | #ifndef OPENSSL_NO_TLSEXT |
| 2193 | # ifndef OPENSSL_NO_EC |
| 2194 | if (a->tlsext_ecpointformatlist) |
| 2195 | OPENSSL_free(a->tlsext_ecpointformatlist); |
| 2196 | if (a->tlsext_ellipticcurvelist) |
| 2197 | OPENSSL_free(a->tlsext_ellipticcurvelist); |
| 2198 | # endif /* OPENSSL_NO_EC */ |
| 2199 | if (a->alpn_client_proto_list != NULL) |
| 2200 | OPENSSL_free(a->alpn_client_proto_list); |
| 2201 | #endif |
| 2202 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2203 | #ifndef OPENSSL_NO_TLSEXT |
| 2204 | if (a->tlsext_channel_id_private) |
| 2205 | EVP_PKEY_free(a->tlsext_channel_id_private); |
| 2206 | #endif |
| 2207 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2208 | OPENSSL_free(a); |
| 2209 | } |
| 2210 | |
| 2211 | void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) |
| 2212 | { |
| 2213 | ctx->default_passwd_callback=cb; |
| 2214 | } |
| 2215 | |
| 2216 | void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u) |
| 2217 | { |
| 2218 | ctx->default_passwd_callback_userdata=u; |
| 2219 | } |
| 2220 | |
| 2221 | void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg) |
| 2222 | { |
| 2223 | ctx->app_verify_callback=cb; |
| 2224 | ctx->app_verify_arg=arg; |
| 2225 | } |
| 2226 | |
| 2227 | void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *)) |
| 2228 | { |
| 2229 | ctx->verify_mode=mode; |
| 2230 | ctx->default_verify_callback=cb; |
| 2231 | } |
| 2232 | |
| 2233 | void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth) |
| 2234 | { |
| 2235 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); |
| 2236 | } |
| 2237 | |
| 2238 | void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg) |
| 2239 | { |
| 2240 | ssl_cert_set_cert_cb(c->cert, cb, arg); |
| 2241 | } |
| 2242 | |
| 2243 | void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) |
| 2244 | { |
| 2245 | ssl_cert_set_cert_cb(s->cert, cb, arg); |
| 2246 | } |
| 2247 | |
| 2248 | void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher) |
| 2249 | { |
| 2250 | CERT_PKEY *cpk; |
| 2251 | int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign; |
| 2252 | int rsa_enc_export,dh_rsa_export,dh_dsa_export; |
| 2253 | int rsa_tmp_export,dh_tmp_export,kl; |
| 2254 | unsigned long mask_k,mask_a,emask_k,emask_a; |
| 2255 | #ifndef OPENSSL_NO_ECDSA |
| 2256 | int have_ecc_cert, ecdsa_ok, ecc_pkey_size; |
| 2257 | #endif |
| 2258 | #ifndef OPENSSL_NO_ECDH |
| 2259 | int have_ecdh_tmp, ecdh_ok; |
| 2260 | #endif |
| 2261 | #ifndef OPENSSL_NO_EC |
| 2262 | X509 *x = NULL; |
| 2263 | EVP_PKEY *ecc_pkey = NULL; |
| 2264 | int signature_nid = 0, pk_nid = 0, md_nid = 0; |
| 2265 | #endif |
| 2266 | if (c == NULL) return; |
| 2267 | |
| 2268 | kl=SSL_C_EXPORT_PKEYLENGTH(cipher); |
| 2269 | |
| 2270 | #ifndef OPENSSL_NO_RSA |
| 2271 | rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL); |
| 2272 | rsa_tmp_export=(c->rsa_tmp_cb != NULL || |
| 2273 | (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl)); |
| 2274 | #else |
| 2275 | rsa_tmp=rsa_tmp_export=0; |
| 2276 | #endif |
| 2277 | #ifndef OPENSSL_NO_DH |
| 2278 | dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL); |
| 2279 | dh_tmp_export=(c->dh_tmp_cb != NULL || |
| 2280 | (dh_tmp && DH_size(c->dh_tmp)*8 <= kl)); |
| 2281 | #else |
| 2282 | dh_tmp=dh_tmp_export=0; |
| 2283 | #endif |
| 2284 | |
| 2285 | #ifndef OPENSSL_NO_ECDH |
| 2286 | have_ecdh_tmp=(c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto); |
| 2287 | #endif |
| 2288 | cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]); |
| 2289 | rsa_enc= cpk->valid_flags & CERT_PKEY_VALID; |
| 2290 | rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl); |
| 2291 | cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]); |
| 2292 | rsa_sign= cpk->valid_flags & CERT_PKEY_SIGN; |
| 2293 | cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]); |
| 2294 | dsa_sign= cpk->valid_flags & CERT_PKEY_SIGN; |
| 2295 | cpk= &(c->pkeys[SSL_PKEY_DH_RSA]); |
| 2296 | dh_rsa= cpk->valid_flags & CERT_PKEY_VALID; |
| 2297 | dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl); |
| 2298 | cpk= &(c->pkeys[SSL_PKEY_DH_DSA]); |
| 2299 | /* FIX THIS EAY EAY EAY */ |
| 2300 | dh_dsa= cpk->valid_flags & CERT_PKEY_VALID; |
| 2301 | dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl); |
| 2302 | cpk= &(c->pkeys[SSL_PKEY_ECC]); |
| 2303 | #ifndef OPENSSL_NO_EC |
| 2304 | have_ecc_cert= cpk->valid_flags & CERT_PKEY_VALID; |
| 2305 | #endif |
| 2306 | mask_k=0; |
| 2307 | mask_a=0; |
| 2308 | emask_k=0; |
| 2309 | emask_a=0; |
| 2310 | |
| 2311 | |
| 2312 | |
| 2313 | #ifdef CIPHER_DEBUG |
| 2314 | printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n", |
| 2315 | rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp, |
| 2316 | rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa); |
| 2317 | #endif |
| 2318 | |
| 2319 | cpk = &(c->pkeys[SSL_PKEY_GOST01]); |
| 2320 | if (cpk->x509 != NULL && cpk->privatekey !=NULL) { |
| 2321 | mask_k |= SSL_kGOST; |
| 2322 | mask_a |= SSL_aGOST01; |
| 2323 | } |
| 2324 | cpk = &(c->pkeys[SSL_PKEY_GOST94]); |
| 2325 | if (cpk->x509 != NULL && cpk->privatekey !=NULL) { |
| 2326 | mask_k |= SSL_kGOST; |
| 2327 | mask_a |= SSL_aGOST94; |
| 2328 | } |
| 2329 | |
| 2330 | if (rsa_enc || (rsa_tmp && rsa_sign)) |
| 2331 | mask_k|=SSL_kRSA; |
| 2332 | if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc))) |
| 2333 | emask_k|=SSL_kRSA; |
| 2334 | |
| 2335 | #if 0 |
| 2336 | /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */ |
| 2337 | if ( (dh_tmp || dh_rsa || dh_dsa) && |
| 2338 | (rsa_enc || rsa_sign || dsa_sign)) |
| 2339 | mask_k|=SSL_kEDH; |
| 2340 | if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) && |
| 2341 | (rsa_enc || rsa_sign || dsa_sign)) |
| 2342 | emask_k|=SSL_kEDH; |
| 2343 | #endif |
| 2344 | |
| 2345 | if (dh_tmp_export) |
| 2346 | emask_k|=SSL_kEDH; |
| 2347 | |
| 2348 | if (dh_tmp) |
| 2349 | mask_k|=SSL_kEDH; |
| 2350 | |
| 2351 | if (dh_rsa) mask_k|=SSL_kDHr; |
| 2352 | if (dh_rsa_export) emask_k|=SSL_kDHr; |
| 2353 | |
| 2354 | if (dh_dsa) mask_k|=SSL_kDHd; |
| 2355 | if (dh_dsa_export) emask_k|=SSL_kDHd; |
| 2356 | |
| 2357 | if (emask_k & (SSL_kDHr|SSL_kDHd)) |
| 2358 | mask_a |= SSL_aDH; |
| 2359 | |
| 2360 | if (rsa_enc || rsa_sign) |
| 2361 | { |
| 2362 | mask_a|=SSL_aRSA; |
| 2363 | emask_a|=SSL_aRSA; |
| 2364 | } |
| 2365 | |
| 2366 | if (dsa_sign) |
| 2367 | { |
| 2368 | mask_a|=SSL_aDSS; |
| 2369 | emask_a|=SSL_aDSS; |
| 2370 | } |
| 2371 | |
| 2372 | mask_a|=SSL_aNULL; |
| 2373 | emask_a|=SSL_aNULL; |
| 2374 | |
| 2375 | /* An ECC certificate may be usable for ECDH and/or |
| 2376 | * ECDSA cipher suites depending on the key usage extension. |
| 2377 | */ |
| 2378 | #ifndef OPENSSL_NO_EC |
| 2379 | if (have_ecc_cert) |
| 2380 | { |
| 2381 | cpk = &c->pkeys[SSL_PKEY_ECC]; |
| 2382 | x = cpk->x509; |
| 2383 | /* This call populates extension flags (ex_flags) */ |
| 2384 | X509_check_purpose(x, -1, 0); |
| 2385 | ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ? |
| 2386 | (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1; |
| 2387 | ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ? |
| 2388 | (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1; |
| 2389 | if (!(cpk->valid_flags & CERT_PKEY_SIGN)) |
| 2390 | ecdsa_ok = 0; |
| 2391 | ecc_pkey = X509_get_pubkey(x); |
| 2392 | ecc_pkey_size = (ecc_pkey != NULL) ? |
| 2393 | EVP_PKEY_bits(ecc_pkey) : 0; |
| 2394 | EVP_PKEY_free(ecc_pkey); |
| 2395 | if ((x->sig_alg) && (x->sig_alg->algorithm)) |
| 2396 | { |
| 2397 | signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); |
| 2398 | OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid); |
| 2399 | } |
| 2400 | #ifndef OPENSSL_NO_ECDH |
| 2401 | if (ecdh_ok) |
| 2402 | { |
| 2403 | |
| 2404 | if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa) |
| 2405 | { |
| 2406 | mask_k|=SSL_kECDHr; |
| 2407 | mask_a|=SSL_aECDH; |
| 2408 | if (ecc_pkey_size <= 163) |
| 2409 | { |
| 2410 | emask_k|=SSL_kECDHr; |
| 2411 | emask_a|=SSL_aECDH; |
| 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | if (pk_nid == NID_X9_62_id_ecPublicKey) |
| 2416 | { |
| 2417 | mask_k|=SSL_kECDHe; |
| 2418 | mask_a|=SSL_aECDH; |
| 2419 | if (ecc_pkey_size <= 163) |
| 2420 | { |
| 2421 | emask_k|=SSL_kECDHe; |
| 2422 | emask_a|=SSL_aECDH; |
| 2423 | } |
| 2424 | } |
| 2425 | } |
| 2426 | #endif |
| 2427 | #ifndef OPENSSL_NO_ECDSA |
| 2428 | if (ecdsa_ok) |
| 2429 | { |
| 2430 | mask_a|=SSL_aECDSA; |
| 2431 | emask_a|=SSL_aECDSA; |
| 2432 | } |
| 2433 | #endif |
| 2434 | } |
| 2435 | #endif |
| 2436 | |
| 2437 | #ifndef OPENSSL_NO_ECDH |
| 2438 | if (have_ecdh_tmp) |
| 2439 | { |
| 2440 | mask_k|=SSL_kEECDH; |
| 2441 | emask_k|=SSL_kEECDH; |
| 2442 | } |
| 2443 | #endif |
| 2444 | |
| 2445 | #ifndef OPENSSL_NO_PSK |
| 2446 | mask_k |= SSL_kPSK; |
| 2447 | mask_a |= SSL_aPSK; |
| 2448 | emask_k |= SSL_kPSK; |
| 2449 | emask_a |= SSL_aPSK; |
| 2450 | #endif |
| 2451 | |
| 2452 | c->mask_k=mask_k; |
| 2453 | c->mask_a=mask_a; |
| 2454 | c->export_mask_k=emask_k; |
| 2455 | c->export_mask_a=emask_a; |
| 2456 | c->valid=1; |
| 2457 | } |
| 2458 | |
| 2459 | /* This handy macro borrowed from crypto/x509v3/v3_purp.c */ |
| 2460 | #define ku_reject(x, usage) \ |
| 2461 | (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage))) |
| 2462 | |
| 2463 | #ifndef OPENSSL_NO_EC |
| 2464 | |
| 2465 | int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) |
| 2466 | { |
| 2467 | unsigned long alg_k, alg_a; |
| 2468 | EVP_PKEY *pkey = NULL; |
| 2469 | int keysize = 0; |
| 2470 | int signature_nid = 0, md_nid = 0, pk_nid = 0; |
| 2471 | const SSL_CIPHER *cs = s->s3->tmp.new_cipher; |
| 2472 | |
| 2473 | alg_k = cs->algorithm_mkey; |
| 2474 | alg_a = cs->algorithm_auth; |
| 2475 | |
| 2476 | if (SSL_C_IS_EXPORT(cs)) |
| 2477 | { |
| 2478 | /* ECDH key length in export ciphers must be <= 163 bits */ |
| 2479 | pkey = X509_get_pubkey(x); |
| 2480 | if (pkey == NULL) return 0; |
| 2481 | keysize = EVP_PKEY_bits(pkey); |
| 2482 | EVP_PKEY_free(pkey); |
| 2483 | if (keysize > 163) return 0; |
| 2484 | } |
| 2485 | |
| 2486 | /* This call populates the ex_flags field correctly */ |
| 2487 | X509_check_purpose(x, -1, 0); |
| 2488 | if ((x->sig_alg) && (x->sig_alg->algorithm)) |
| 2489 | { |
| 2490 | signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); |
| 2491 | OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid); |
| 2492 | } |
| 2493 | if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr) |
| 2494 | { |
| 2495 | /* key usage, if present, must allow key agreement */ |
| 2496 | if (ku_reject(x, X509v3_KU_KEY_AGREEMENT)) |
| 2497 | { |
| 2498 | OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT); |
| 2499 | return 0; |
| 2500 | } |
| 2501 | if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < TLS1_2_VERSION) |
| 2502 | { |
| 2503 | /* signature alg must be ECDSA */ |
| 2504 | if (pk_nid != NID_X9_62_id_ecPublicKey) |
| 2505 | { |
| 2506 | OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE); |
| 2507 | return 0; |
| 2508 | } |
| 2509 | } |
| 2510 | if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < TLS1_2_VERSION) |
| 2511 | { |
| 2512 | /* signature alg must be RSA */ |
| 2513 | |
| 2514 | if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa) |
| 2515 | { |
| 2516 | OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE); |
| 2517 | return 0; |
| 2518 | } |
| 2519 | } |
| 2520 | } |
| 2521 | if (alg_a & SSL_aECDSA) |
| 2522 | { |
| 2523 | /* key usage, if present, must allow signing */ |
| 2524 | if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) |
| 2525 | { |
| 2526 | OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING); |
| 2527 | return 0; |
| 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | return 1; /* all checks are ok */ |
| 2532 | } |
| 2533 | |
| 2534 | #endif |
| 2535 | |
| 2536 | static int ssl_get_server_cert_index(const SSL *s) |
| 2537 | { |
| 2538 | int idx; |
| 2539 | idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); |
| 2540 | if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) |
| 2541 | idx = SSL_PKEY_RSA_SIGN; |
| 2542 | if (idx == -1) |
| 2543 | OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR); |
| 2544 | return idx; |
| 2545 | } |
| 2546 | |
| 2547 | CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) |
| 2548 | { |
| 2549 | CERT *c; |
| 2550 | int i; |
| 2551 | |
| 2552 | c = s->cert; |
| 2553 | ssl_set_cert_masks(c, s->s3->tmp.new_cipher); |
| 2554 | |
| 2555 | #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL |
| 2556 | /* Broken protocol test: return last used certificate: which may |
| 2557 | * mismatch the one expected. |
| 2558 | */ |
| 2559 | if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) |
| 2560 | return c->key; |
| 2561 | #endif |
| 2562 | |
| 2563 | i = ssl_get_server_cert_index(s); |
| 2564 | |
| 2565 | /* This may or may not be an error. */ |
| 2566 | if (i < 0) |
| 2567 | return NULL; |
| 2568 | |
| 2569 | /* May be NULL. */ |
| 2570 | return &c->pkeys[i]; |
| 2571 | } |
| 2572 | |
| 2573 | EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd) |
| 2574 | { |
| 2575 | unsigned long alg_a; |
| 2576 | CERT *c; |
| 2577 | int idx = -1; |
| 2578 | |
| 2579 | alg_a = cipher->algorithm_auth; |
| 2580 | c=s->cert; |
| 2581 | |
| 2582 | #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL |
| 2583 | /* Broken protocol test: use last key: which may |
| 2584 | * mismatch the one expected. |
| 2585 | */ |
| 2586 | if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL) |
| 2587 | idx = c->key - c->pkeys; |
| 2588 | else |
| 2589 | #endif |
| 2590 | |
| 2591 | if ((alg_a & SSL_aDSS) && |
| 2592 | (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL)) |
| 2593 | idx = SSL_PKEY_DSA_SIGN; |
| 2594 | else if (alg_a & SSL_aRSA) |
| 2595 | { |
| 2596 | if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) |
| 2597 | idx = SSL_PKEY_RSA_SIGN; |
| 2598 | else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) |
| 2599 | idx = SSL_PKEY_RSA_ENC; |
| 2600 | } |
| 2601 | else if ((alg_a & SSL_aECDSA) && |
| 2602 | (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) |
| 2603 | idx = SSL_PKEY_ECC; |
| 2604 | if (idx == -1) |
| 2605 | { |
| 2606 | OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR); |
| 2607 | return(NULL); |
| 2608 | } |
| 2609 | if (pmd) |
| 2610 | *pmd = c->pkeys[idx].digest; |
| 2611 | return c->pkeys[idx].privatekey; |
| 2612 | } |
| 2613 | |
| 2614 | #ifndef OPENSSL_NO_TLSEXT |
| 2615 | unsigned char *ssl_get_authz_data(SSL *s, size_t *authz_length) |
| 2616 | { |
| 2617 | CERT *c; |
| 2618 | int i; |
| 2619 | |
| 2620 | c = s->cert; |
| 2621 | i = ssl_get_server_cert_index(s); |
| 2622 | |
| 2623 | if (i == -1) |
| 2624 | return NULL; |
| 2625 | |
| 2626 | *authz_length = 0; |
| 2627 | if (c->pkeys[i].authz == NULL) |
| 2628 | return(NULL); |
| 2629 | *authz_length = c->pkeys[i].authz_length; |
| 2630 | |
| 2631 | return c->pkeys[i].authz; |
| 2632 | } |
| 2633 | |
| 2634 | int ssl_get_server_cert_serverinfo(SSL *s, const unsigned char **serverinfo, |
| 2635 | size_t *serverinfo_length) |
| 2636 | { |
| 2637 | CERT *c = NULL; |
| 2638 | int i = 0; |
| 2639 | *serverinfo_length = 0; |
| 2640 | |
| 2641 | c = s->cert; |
| 2642 | i = ssl_get_server_cert_index(s); |
| 2643 | |
| 2644 | if (i == -1) |
| 2645 | return 0; |
| 2646 | if (c->pkeys[i].serverinfo == NULL) |
| 2647 | return 0; |
| 2648 | |
| 2649 | *serverinfo = c->pkeys[i].serverinfo; |
| 2650 | *serverinfo_length = c->pkeys[i].serverinfo_length; |
| 2651 | return 1; |
| 2652 | } |
| 2653 | #endif |
| 2654 | |
| 2655 | void ssl_update_cache(SSL *s,int mode) |
| 2656 | { |
| 2657 | int i; |
| 2658 | |
| 2659 | /* If the session_id_length is 0, we are not supposed to cache it, |
| 2660 | * and it would be rather hard to do anyway :-) */ |
| 2661 | if (s->session->session_id_length == 0) return; |
| 2662 | |
| 2663 | i=s->session_ctx->session_cache_mode; |
| 2664 | if ((i & mode) && (!s->hit) |
| 2665 | && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) |
| 2666 | || SSL_CTX_add_session(s->session_ctx,s->session)) |
| 2667 | && (s->session_ctx->new_session_cb != NULL)) |
| 2668 | { |
| 2669 | CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION); |
| 2670 | if (!s->session_ctx->new_session_cb(s,s->session)) |
| 2671 | SSL_SESSION_free(s->session); |
| 2672 | } |
| 2673 | |
| 2674 | /* auto flush every 255 connections */ |
| 2675 | if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && |
| 2676 | ((i & mode) == mode)) |
| 2677 | { |
| 2678 | if ( (((mode & SSL_SESS_CACHE_CLIENT) |
| 2679 | ?s->session_ctx->stats.sess_connect_good |
| 2680 | :s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff) |
| 2681 | { |
| 2682 | SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL)); |
| 2683 | } |
| 2684 | } |
| 2685 | } |
| 2686 | |
| 2687 | const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx) |
| 2688 | { |
| 2689 | return ctx->method; |
| 2690 | } |
| 2691 | |
| 2692 | const SSL_METHOD *SSL_get_ssl_method(SSL *s) |
| 2693 | { |
| 2694 | return(s->method); |
| 2695 | } |
| 2696 | |
| 2697 | int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth) |
| 2698 | { |
| 2699 | int conn= -1; |
| 2700 | int ret=1; |
| 2701 | |
| 2702 | if (s->method != meth) |
| 2703 | { |
| 2704 | if (s->handshake_func != NULL) |
| 2705 | conn=(s->handshake_func == s->method->ssl_connect); |
| 2706 | |
| 2707 | if (s->method->version == meth->version) |
| 2708 | s->method=meth; |
| 2709 | else |
| 2710 | { |
| 2711 | s->method->ssl_free(s); |
| 2712 | s->method=meth; |
| 2713 | ret=s->method->ssl_new(s); |
| 2714 | } |
| 2715 | |
| 2716 | if (conn == 1) |
| 2717 | s->handshake_func=meth->ssl_connect; |
| 2718 | else if (conn == 0) |
| 2719 | s->handshake_func=meth->ssl_accept; |
| 2720 | } |
| 2721 | return(ret); |
| 2722 | } |
| 2723 | |
| 2724 | int SSL_get_error(const SSL *s,int i) |
| 2725 | { |
| 2726 | int reason; |
| 2727 | unsigned long l; |
| 2728 | BIO *bio; |
| 2729 | |
| 2730 | if (i > 0) return(SSL_ERROR_NONE); |
| 2731 | |
| 2732 | /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake |
| 2733 | * etc, where we do encode the error */ |
| 2734 | if ((l=ERR_peek_error()) != 0) |
| 2735 | { |
| 2736 | if (ERR_GET_LIB(l) == ERR_LIB_SYS) |
| 2737 | return(SSL_ERROR_SYSCALL); |
| 2738 | else |
| 2739 | return(SSL_ERROR_SSL); |
| 2740 | } |
| 2741 | |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2742 | if ((i < 0) && SSL_want_session(s)) |
| 2743 | return(SSL_ERROR_PENDING_SESSION); |
| 2744 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2745 | if ((i < 0) && SSL_want_read(s)) |
| 2746 | { |
| 2747 | bio=SSL_get_rbio(s); |
| 2748 | if (BIO_should_read(bio)) |
| 2749 | return(SSL_ERROR_WANT_READ); |
| 2750 | else if (BIO_should_write(bio)) |
| 2751 | /* This one doesn't make too much sense ... We never try |
| 2752 | * to write to the rbio, and an application program where |
| 2753 | * rbio and wbio are separate couldn't even know what it |
| 2754 | * should wait for. |
| 2755 | * However if we ever set s->rwstate incorrectly |
| 2756 | * (so that we have SSL_want_read(s) instead of |
| 2757 | * SSL_want_write(s)) and rbio and wbio *are* the same, |
| 2758 | * this test works around that bug; so it might be safer |
| 2759 | * to keep it. */ |
| 2760 | return(SSL_ERROR_WANT_WRITE); |
| 2761 | else if (BIO_should_io_special(bio)) |
| 2762 | { |
| 2763 | reason=BIO_get_retry_reason(bio); |
| 2764 | if (reason == BIO_RR_CONNECT) |
| 2765 | return(SSL_ERROR_WANT_CONNECT); |
| 2766 | else if (reason == BIO_RR_ACCEPT) |
| 2767 | return(SSL_ERROR_WANT_ACCEPT); |
| 2768 | else |
| 2769 | return(SSL_ERROR_SYSCALL); /* unknown */ |
| 2770 | } |
| 2771 | } |
| 2772 | |
| 2773 | if ((i < 0) && SSL_want_write(s)) |
| 2774 | { |
| 2775 | bio=SSL_get_wbio(s); |
| 2776 | if (BIO_should_write(bio)) |
| 2777 | return(SSL_ERROR_WANT_WRITE); |
| 2778 | else if (BIO_should_read(bio)) |
| 2779 | /* See above (SSL_want_read(s) with BIO_should_write(bio)) */ |
| 2780 | return(SSL_ERROR_WANT_READ); |
| 2781 | else if (BIO_should_io_special(bio)) |
| 2782 | { |
| 2783 | reason=BIO_get_retry_reason(bio); |
| 2784 | if (reason == BIO_RR_CONNECT) |
| 2785 | return(SSL_ERROR_WANT_CONNECT); |
| 2786 | else if (reason == BIO_RR_ACCEPT) |
| 2787 | return(SSL_ERROR_WANT_ACCEPT); |
| 2788 | else |
| 2789 | return(SSL_ERROR_SYSCALL); |
| 2790 | } |
| 2791 | } |
| 2792 | if ((i < 0) && SSL_want_x509_lookup(s)) |
| 2793 | { |
| 2794 | return(SSL_ERROR_WANT_X509_LOOKUP); |
| 2795 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2796 | if ((i < 0) && SSL_want_channel_id_lookup(s)) |
| 2797 | { |
| 2798 | return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP); |
| 2799 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2800 | |
| 2801 | if (i == 0) |
| 2802 | { |
| 2803 | if (s->version == SSL2_VERSION) |
| 2804 | { |
| 2805 | /* assume it is the socket being closed */ |
| 2806 | return(SSL_ERROR_ZERO_RETURN); |
| 2807 | } |
| 2808 | else |
| 2809 | { |
| 2810 | if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) && |
| 2811 | (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) |
| 2812 | return(SSL_ERROR_ZERO_RETURN); |
| 2813 | } |
| 2814 | } |
| 2815 | return(SSL_ERROR_SYSCALL); |
| 2816 | } |
| 2817 | |
| 2818 | int SSL_do_handshake(SSL *s) |
| 2819 | { |
| 2820 | int ret=1; |
| 2821 | |
| 2822 | if (s->handshake_func == NULL) |
| 2823 | { |
| 2824 | OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET); |
| 2825 | return(-1); |
| 2826 | } |
| 2827 | |
| 2828 | s->method->ssl_renegotiate_check(s); |
| 2829 | |
| 2830 | if (SSL_in_init(s) || SSL_in_before(s)) |
| 2831 | { |
| 2832 | ret=s->handshake_func(s); |
| 2833 | } |
| 2834 | return(ret); |
| 2835 | } |
| 2836 | |
| 2837 | /* For the next 2 functions, SSL_clear() sets shutdown and so |
| 2838 | * one of these calls will reset it */ |
| 2839 | void SSL_set_accept_state(SSL *s) |
| 2840 | { |
| 2841 | s->server=1; |
| 2842 | s->shutdown=0; |
| 2843 | s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE; |
| 2844 | s->handshake_func=s->method->ssl_accept; |
| 2845 | /* clear the current cipher */ |
| 2846 | ssl_clear_cipher_ctx(s); |
| 2847 | ssl_clear_hash_ctx(&s->read_hash); |
| 2848 | ssl_clear_hash_ctx(&s->write_hash); |
| 2849 | } |
| 2850 | |
| 2851 | void SSL_set_connect_state(SSL *s) |
| 2852 | { |
| 2853 | s->server=0; |
| 2854 | s->shutdown=0; |
| 2855 | s->state=SSL_ST_CONNECT|SSL_ST_BEFORE; |
| 2856 | s->handshake_func=s->method->ssl_connect; |
| 2857 | /* clear the current cipher */ |
| 2858 | ssl_clear_cipher_ctx(s); |
| 2859 | ssl_clear_hash_ctx(&s->read_hash); |
| 2860 | ssl_clear_hash_ctx(&s->write_hash); |
| 2861 | } |
| 2862 | |
| 2863 | int ssl_undefined_function(SSL *s) |
| 2864 | { |
| 2865 | OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2866 | return(0); |
| 2867 | } |
| 2868 | |
| 2869 | int ssl_undefined_void_function(void) |
| 2870 | { |
| 2871 | OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2872 | return(0); |
| 2873 | } |
| 2874 | |
| 2875 | int ssl_undefined_const_function(const SSL *s) |
| 2876 | { |
| 2877 | OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2878 | return(0); |
| 2879 | } |
| 2880 | |
| 2881 | SSL_METHOD *ssl_bad_method(int ver) |
| 2882 | { |
| 2883 | OPENSSL_PUT_ERROR(SSL, ssl_bad_method, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2884 | return(NULL); |
| 2885 | } |
| 2886 | |
| 2887 | const char *SSL_get_version(const SSL *s) |
| 2888 | { |
| 2889 | if (s->version == TLS1_2_VERSION) |
| 2890 | return("TLSv1.2"); |
| 2891 | else if (s->version == TLS1_1_VERSION) |
| 2892 | return("TLSv1.1"); |
| 2893 | else if (s->version == TLS1_VERSION) |
| 2894 | return("TLSv1"); |
| 2895 | else if (s->version == SSL3_VERSION) |
| 2896 | return("SSLv3"); |
| 2897 | else if (s->version == SSL2_VERSION) |
| 2898 | return("SSLv2"); |
| 2899 | else |
| 2900 | return("unknown"); |
| 2901 | } |
| 2902 | |
| 2903 | void ssl_clear_cipher_ctx(SSL *s) |
| 2904 | { |
| 2905 | if (s->enc_read_ctx != NULL) |
| 2906 | { |
| 2907 | EVP_CIPHER_CTX_cleanup(s->enc_read_ctx); |
| 2908 | OPENSSL_free(s->enc_read_ctx); |
| 2909 | s->enc_read_ctx=NULL; |
| 2910 | } |
| 2911 | if (s->enc_write_ctx != NULL) |
| 2912 | { |
| 2913 | EVP_CIPHER_CTX_cleanup(s->enc_write_ctx); |
| 2914 | OPENSSL_free(s->enc_write_ctx); |
| 2915 | s->enc_write_ctx=NULL; |
| 2916 | } |
| 2917 | } |
| 2918 | |
| 2919 | X509 *SSL_get_certificate(const SSL *s) |
| 2920 | { |
| 2921 | if (s->cert != NULL) |
| 2922 | return(s->cert->key->x509); |
| 2923 | else |
| 2924 | return(NULL); |
| 2925 | } |
| 2926 | |
| 2927 | EVP_PKEY *SSL_get_privatekey(const SSL *s) |
| 2928 | { |
| 2929 | if (s->cert != NULL) |
| 2930 | return(s->cert->key->privatekey); |
| 2931 | else |
| 2932 | return(NULL); |
| 2933 | } |
| 2934 | |
| 2935 | X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) |
| 2936 | { |
| 2937 | if (ctx->cert != NULL) |
| 2938 | return ctx->cert->key->x509; |
| 2939 | else |
| 2940 | return NULL; |
| 2941 | } |
| 2942 | |
| 2943 | EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) |
| 2944 | { |
| 2945 | if (ctx->cert != NULL) |
| 2946 | return ctx->cert->key->privatekey; |
| 2947 | else |
| 2948 | return NULL ; |
| 2949 | } |
| 2950 | |
| 2951 | const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) |
| 2952 | { |
| 2953 | if ((s->session != NULL) && (s->session->cipher != NULL)) |
| 2954 | return(s->session->cipher); |
| 2955 | return(NULL); |
| 2956 | } |
| 2957 | const void *SSL_get_current_compression(SSL *s) |
| 2958 | { |
| 2959 | return NULL; |
| 2960 | } |
| 2961 | const void *SSL_get_current_expansion(SSL *s) |
| 2962 | { |
| 2963 | return NULL; |
| 2964 | } |
| 2965 | |
| 2966 | int ssl_init_wbio_buffer(SSL *s,int push) |
| 2967 | { |
| 2968 | BIO *bbio; |
| 2969 | |
| 2970 | if (s->bbio == NULL) |
| 2971 | { |
| 2972 | bbio=BIO_new(BIO_f_buffer()); |
| 2973 | if (bbio == NULL) return(0); |
| 2974 | s->bbio=bbio; |
| 2975 | } |
| 2976 | else |
| 2977 | { |
| 2978 | bbio=s->bbio; |
| 2979 | if (s->bbio == s->wbio) |
| 2980 | s->wbio=BIO_pop(s->wbio); |
| 2981 | } |
| 2982 | (void)BIO_reset(bbio); |
| 2983 | /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */ |
| 2984 | if (!BIO_set_read_buffer_size(bbio,1)) |
| 2985 | { |
| 2986 | OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB); |
| 2987 | return(0); |
| 2988 | } |
| 2989 | if (push) |
| 2990 | { |
| 2991 | if (s->wbio != bbio) |
| 2992 | s->wbio=BIO_push(bbio,s->wbio); |
| 2993 | } |
| 2994 | else |
| 2995 | { |
| 2996 | if (s->wbio == bbio) |
| 2997 | s->wbio=BIO_pop(bbio); |
| 2998 | } |
| 2999 | return(1); |
| 3000 | } |
| 3001 | |
| 3002 | void ssl_free_wbio_buffer(SSL *s) |
| 3003 | { |
| 3004 | if (s->bbio == NULL) return; |
| 3005 | |
| 3006 | if (s->bbio == s->wbio) |
| 3007 | { |
| 3008 | /* remove buffering */ |
| 3009 | s->wbio=BIO_pop(s->wbio); |
| 3010 | #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */ |
| 3011 | assert(s->wbio != NULL); |
| 3012 | #endif |
| 3013 | } |
| 3014 | BIO_free(s->bbio); |
| 3015 | s->bbio=NULL; |
| 3016 | } |
| 3017 | |
| 3018 | void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode) |
| 3019 | { |
| 3020 | ctx->quiet_shutdown=mode; |
| 3021 | } |
| 3022 | |
| 3023 | int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) |
| 3024 | { |
| 3025 | return(ctx->quiet_shutdown); |
| 3026 | } |
| 3027 | |
| 3028 | void SSL_set_quiet_shutdown(SSL *s,int mode) |
| 3029 | { |
| 3030 | s->quiet_shutdown=mode; |
| 3031 | } |
| 3032 | |
| 3033 | int SSL_get_quiet_shutdown(const SSL *s) |
| 3034 | { |
| 3035 | return(s->quiet_shutdown); |
| 3036 | } |
| 3037 | |
| 3038 | void SSL_set_shutdown(SSL *s,int mode) |
| 3039 | { |
| 3040 | s->shutdown=mode; |
| 3041 | } |
| 3042 | |
| 3043 | int SSL_get_shutdown(const SSL *s) |
| 3044 | { |
| 3045 | return(s->shutdown); |
| 3046 | } |
| 3047 | |
| 3048 | int SSL_version(const SSL *s) |
| 3049 | { |
| 3050 | return(s->version); |
| 3051 | } |
| 3052 | |
| 3053 | SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) |
| 3054 | { |
| 3055 | return(ssl->ctx); |
| 3056 | } |
| 3057 | |
| 3058 | SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx) |
| 3059 | { |
| 3060 | if (ssl->ctx == ctx) |
| 3061 | return ssl->ctx; |
| 3062 | #ifndef OPENSSL_NO_TLSEXT |
| 3063 | if (ctx == NULL) |
| 3064 | ctx = ssl->initial_ctx; |
| 3065 | #endif |
| 3066 | if (ssl->cert != NULL) |
| 3067 | ssl_cert_free(ssl->cert); |
| 3068 | ssl->cert = ssl_cert_dup(ctx->cert); |
| 3069 | CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX); |
| 3070 | if (ssl->ctx != NULL) |
| 3071 | SSL_CTX_free(ssl->ctx); /* decrement reference count */ |
| 3072 | ssl->ctx = ctx; |
| 3073 | return(ssl->ctx); |
| 3074 | } |
| 3075 | |
| 3076 | #ifndef OPENSSL_NO_STDIO |
| 3077 | int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) |
| 3078 | { |
| 3079 | return(X509_STORE_set_default_paths(ctx->cert_store)); |
| 3080 | } |
| 3081 | |
| 3082 | int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, |
| 3083 | const char *CApath) |
| 3084 | { |
| 3085 | return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath)); |
| 3086 | } |
| 3087 | #endif |
| 3088 | |
| 3089 | void SSL_set_info_callback(SSL *ssl, |
| 3090 | void (*cb)(const SSL *ssl,int type,int val)) |
| 3091 | { |
| 3092 | ssl->info_callback=cb; |
| 3093 | } |
| 3094 | |
| 3095 | /* One compiler (Diab DCC) doesn't like argument names in returned |
| 3096 | function pointer. */ |
| 3097 | void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/) |
| 3098 | { |
| 3099 | return ssl->info_callback; |
| 3100 | } |
| 3101 | |
| 3102 | int SSL_state(const SSL *ssl) |
| 3103 | { |
| 3104 | return(ssl->state); |
| 3105 | } |
| 3106 | |
| 3107 | void SSL_set_state(SSL *ssl, int state) |
| 3108 | { |
| 3109 | ssl->state = state; |
| 3110 | } |
| 3111 | |
| 3112 | void SSL_set_verify_result(SSL *ssl,long arg) |
| 3113 | { |
| 3114 | ssl->verify_result=arg; |
| 3115 | } |
| 3116 | |
| 3117 | long SSL_get_verify_result(const SSL *ssl) |
| 3118 | { |
| 3119 | return(ssl->verify_result); |
| 3120 | } |
| 3121 | |
| 3122 | int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func, |
| 3123 | CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func) |
| 3124 | { |
| 3125 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, |
| 3126 | new_func, dup_func, free_func); |
| 3127 | } |
| 3128 | |
| 3129 | int SSL_set_ex_data(SSL *s,int idx,void *arg) |
| 3130 | { |
| 3131 | return(CRYPTO_set_ex_data(&s->ex_data,idx,arg)); |
| 3132 | } |
| 3133 | |
| 3134 | void *SSL_get_ex_data(const SSL *s,int idx) |
| 3135 | { |
| 3136 | return(CRYPTO_get_ex_data(&s->ex_data,idx)); |
| 3137 | } |
| 3138 | |
| 3139 | int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func, |
| 3140 | CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func) |
| 3141 | { |
| 3142 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, |
| 3143 | new_func, dup_func, free_func); |
| 3144 | } |
| 3145 | |
| 3146 | int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg) |
| 3147 | { |
| 3148 | return(CRYPTO_set_ex_data(&s->ex_data,idx,arg)); |
| 3149 | } |
| 3150 | |
| 3151 | void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx) |
| 3152 | { |
| 3153 | return(CRYPTO_get_ex_data(&s->ex_data,idx)); |
| 3154 | } |
| 3155 | |
| 3156 | int ssl_ok(SSL *s) |
| 3157 | { |
| 3158 | return(1); |
| 3159 | } |
| 3160 | |
| 3161 | X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) |
| 3162 | { |
| 3163 | return(ctx->cert_store); |
| 3164 | } |
| 3165 | |
| 3166 | void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store) |
| 3167 | { |
| 3168 | if (ctx->cert_store != NULL) |
| 3169 | X509_STORE_free(ctx->cert_store); |
| 3170 | ctx->cert_store=store; |
| 3171 | } |
| 3172 | |
| 3173 | int SSL_want(const SSL *s) |
| 3174 | { |
| 3175 | return(s->rwstate); |
| 3176 | } |
| 3177 | |
| 3178 | /*! |
| 3179 | * \brief Set the callback for generating temporary RSA keys. |
| 3180 | * \param ctx the SSL context. |
| 3181 | * \param cb the callback |
| 3182 | */ |
| 3183 | |
| 3184 | #ifndef OPENSSL_NO_RSA |
| 3185 | void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl, |
| 3186 | int is_export, |
| 3187 | int keylength)) |
| 3188 | { |
| 3189 | SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); |
| 3190 | } |
| 3191 | |
| 3192 | void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl, |
| 3193 | int is_export, |
| 3194 | int keylength)) |
| 3195 | { |
| 3196 | SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb); |
| 3197 | } |
| 3198 | #endif |
| 3199 | |
| 3200 | #ifdef DOXYGEN |
| 3201 | /*! |
| 3202 | * \brief The RSA temporary key callback function. |
| 3203 | * \param ssl the SSL session. |
| 3204 | * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite. |
| 3205 | * \param keylength if \c is_export is \c TRUE, then \c keylength is the size |
| 3206 | * of the required key in bits. |
| 3207 | * \return the temporary RSA key. |
| 3208 | * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback |
| 3209 | */ |
| 3210 | |
| 3211 | RSA *cb(SSL *ssl,int is_export,int keylength) |
| 3212 | {} |
| 3213 | #endif |
| 3214 | |
| 3215 | /*! |
| 3216 | * \brief Set the callback for generating temporary DH keys. |
| 3217 | * \param ctx the SSL context. |
| 3218 | * \param dh the callback |
| 3219 | */ |
| 3220 | |
| 3221 | #ifndef OPENSSL_NO_DH |
| 3222 | void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export, |
| 3223 | int keylength)) |
| 3224 | { |
| 3225 | SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); |
| 3226 | } |
| 3227 | |
| 3228 | void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export, |
| 3229 | int keylength)) |
| 3230 | { |
| 3231 | SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh); |
| 3232 | } |
| 3233 | #endif |
| 3234 | |
| 3235 | #ifndef OPENSSL_NO_ECDH |
| 3236 | void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export, |
| 3237 | int keylength)) |
| 3238 | { |
| 3239 | SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh); |
| 3240 | } |
| 3241 | |
| 3242 | void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export, |
| 3243 | int keylength)) |
| 3244 | { |
| 3245 | SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh); |
| 3246 | } |
| 3247 | #endif |
| 3248 | |
| 3249 | #ifndef OPENSSL_NO_PSK |
| 3250 | int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) |
| 3251 | { |
| 3252 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) |
| 3253 | { |
| 3254 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG); |
| 3255 | return 0; |
| 3256 | } |
| 3257 | if (ctx->psk_identity_hint != NULL) |
| 3258 | OPENSSL_free(ctx->psk_identity_hint); |
| 3259 | if (identity_hint != NULL) |
| 3260 | { |
| 3261 | ctx->psk_identity_hint = BUF_strdup(identity_hint); |
| 3262 | if (ctx->psk_identity_hint == NULL) |
| 3263 | return 0; |
| 3264 | } |
| 3265 | else |
| 3266 | ctx->psk_identity_hint = NULL; |
| 3267 | return 1; |
| 3268 | } |
| 3269 | |
| 3270 | int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) |
| 3271 | { |
| 3272 | if (s == NULL) |
| 3273 | return 0; |
| 3274 | |
| 3275 | if (s->session == NULL) |
| 3276 | return 1; /* session not created yet, ignored */ |
| 3277 | |
| 3278 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) |
| 3279 | { |
| 3280 | OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG); |
| 3281 | return 0; |
| 3282 | } |
| 3283 | if (s->session->psk_identity_hint != NULL) |
| 3284 | OPENSSL_free(s->session->psk_identity_hint); |
| 3285 | if (identity_hint != NULL) |
| 3286 | { |
| 3287 | s->session->psk_identity_hint = BUF_strdup(identity_hint); |
| 3288 | if (s->session->psk_identity_hint == NULL) |
| 3289 | return 0; |
| 3290 | } |
| 3291 | else |
| 3292 | s->session->psk_identity_hint = NULL; |
| 3293 | return 1; |
| 3294 | } |
| 3295 | |
| 3296 | const char *SSL_get_psk_identity_hint(const SSL *s) |
| 3297 | { |
| 3298 | if (s == NULL || s->session == NULL) |
| 3299 | return NULL; |
| 3300 | return(s->session->psk_identity_hint); |
| 3301 | } |
| 3302 | |
| 3303 | const char *SSL_get_psk_identity(const SSL *s) |
| 3304 | { |
| 3305 | if (s == NULL || s->session == NULL) |
| 3306 | return NULL; |
| 3307 | return(s->session->psk_identity); |
| 3308 | } |
| 3309 | |
| 3310 | void SSL_set_psk_client_callback(SSL *s, |
| 3311 | unsigned int (*cb)(SSL *ssl, const char *hint, |
| 3312 | char *identity, unsigned int max_identity_len, unsigned char *psk, |
| 3313 | unsigned int max_psk_len)) |
| 3314 | { |
| 3315 | s->psk_client_callback = cb; |
| 3316 | } |
| 3317 | |
| 3318 | void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, |
| 3319 | unsigned int (*cb)(SSL *ssl, const char *hint, |
| 3320 | char *identity, unsigned int max_identity_len, unsigned char *psk, |
| 3321 | unsigned int max_psk_len)) |
| 3322 | { |
| 3323 | ctx->psk_client_callback = cb; |
| 3324 | } |
| 3325 | |
| 3326 | void SSL_set_psk_server_callback(SSL *s, |
| 3327 | unsigned int (*cb)(SSL *ssl, const char *identity, |
| 3328 | unsigned char *psk, unsigned int max_psk_len)) |
| 3329 | { |
| 3330 | s->psk_server_callback = cb; |
| 3331 | } |
| 3332 | |
| 3333 | void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, |
| 3334 | unsigned int (*cb)(SSL *ssl, const char *identity, |
| 3335 | unsigned char *psk, unsigned int max_psk_len)) |
| 3336 | { |
| 3337 | ctx->psk_server_callback = cb; |
| 3338 | } |
| 3339 | #endif |
| 3340 | |
| 3341 | void SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)) |
| 3342 | { |
| 3343 | SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); |
| 3344 | } |
| 3345 | void SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)) |
| 3346 | { |
| 3347 | SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); |
| 3348 | } |
| 3349 | |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3350 | int SSL_cutthrough_complete(const SSL *s) |
| 3351 | { |
| 3352 | return (!s->server && /* cutthrough only applies to clients */ |
| 3353 | !s->hit && /* full-handshake */ |
| 3354 | s->version >= SSL3_VERSION && |
| 3355 | s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */ |
| 3356 | (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */ |
| 3357 | ssl3_can_cutthrough(s) && /* cutthrough allowed */ |
| 3358 | s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */ |
| 3359 | (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/ |
| 3360 | s->state == SSL3_ST_CR_FINISHED_A)); |
| 3361 | } |
| 3362 | |
Adam Langley | 95f2288 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3363 | void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size, |
| 3364 | size_t* ssl_session_size) |
| 3365 | { |
| 3366 | *ssl_size = sizeof(SSL); |
| 3367 | *ssl_ctx_size = sizeof(SSL_CTX); |
| 3368 | *ssl_session_size = sizeof(SSL_SESSION); |
| 3369 | } |
| 3370 | |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3371 | int ssl3_can_cutthrough(const SSL *s) |
| 3372 | { |
| 3373 | const SSL_CIPHER *c; |
| 3374 | |
| 3375 | /* require a strong enough cipher */ |
| 3376 | if (SSL_get_cipher_bits(s, NULL) < 128) |
| 3377 | return 0; |
| 3378 | |
| 3379 | /* require ALPN or NPN extension */ |
| 3380 | if (!s->s3->alpn_selected |
| 3381 | #ifndef OPENSSL_NO_NEXTPROTONEG |
| 3382 | && !s->s3->next_proto_neg_seen |
| 3383 | #endif |
| 3384 | ) |
| 3385 | { |
| 3386 | return 0; |
| 3387 | } |
| 3388 | |
| 3389 | /* require a forward-secret cipher */ |
| 3390 | c = SSL_get_current_cipher(s); |
| 3391 | if (!c || (c->algorithm_mkey != SSL_kEDH && |
| 3392 | c->algorithm_mkey != SSL_kEECDH)) |
| 3393 | { |
| 3394 | return 0; |
| 3395 | } |
| 3396 | |
| 3397 | return 1; |
| 3398 | } |
| 3399 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 3400 | /* Allocates new EVP_MD_CTX and sets pointer to it into given pointer |
| 3401 | * vairable, freeing EVP_MD_CTX previously stored in that variable, if |
| 3402 | * any. If EVP_MD pointer is passed, initializes ctx with this md |
| 3403 | * Returns newly allocated ctx; |
| 3404 | */ |
| 3405 | |
| 3406 | EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md) |
| 3407 | { |
| 3408 | ssl_clear_hash_ctx(hash); |
| 3409 | *hash = EVP_MD_CTX_create(); |
| 3410 | if (md) EVP_DigestInit_ex(*hash,md,NULL); |
| 3411 | return *hash; |
| 3412 | } |
| 3413 | void ssl_clear_hash_ctx(EVP_MD_CTX **hash) |
| 3414 | { |
| 3415 | |
| 3416 | if (*hash) EVP_MD_CTX_destroy(*hash); |
| 3417 | *hash=NULL; |
| 3418 | } |
| 3419 | |
| 3420 | void SSL_set_debug(SSL *s, int debug) |
| 3421 | { |
| 3422 | s->debug = debug; |
| 3423 | } |
| 3424 | |
| 3425 | int SSL_cache_hit(SSL *s) |
| 3426 | { |
| 3427 | return s->hit; |
| 3428 | } |
| 3429 | |
| 3430 | int SSL_is_server(SSL *s) |
| 3431 | { |
| 3432 | return s->server; |
| 3433 | } |