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