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