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