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 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 141 | #include <openssl/ssl.h> |
| 142 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 143 | #include <assert.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 144 | #include <stdio.h> |
| 145 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 146 | |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 147 | #include <openssl/bytestring.h> |
David Benjamin | 7c6d09b | 2015-09-05 11:00:19 -0400 | [diff] [blame] | 148 | #include <openssl/crypto.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 149 | #include <openssl/dh.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 150 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 151 | #include <openssl/lhash.h> |
| 152 | #include <openssl/mem.h> |
| 153 | #include <openssl/obj.h> |
| 154 | #include <openssl/rand.h> |
| 155 | #include <openssl/x509v3.h> |
| 156 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 157 | #include "internal.h" |
David Benjamin | 546f1a5 | 2015-04-15 16:46:09 -0400 | [diff] [blame] | 158 | #include "../crypto/internal.h" |
| 159 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 160 | |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 161 | /* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it |
David Benjamin | 97760d5 | 2015-07-24 23:02:49 -0400 | [diff] [blame] | 162 | * to avoid downstream churn. */ |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 163 | OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL) |
David Benjamin | 97760d5 | 2015-07-24 23:02:49 -0400 | [diff] [blame] | 164 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 165 | /* Some error codes are special. Ensure the make_errors.go script never |
| 166 | * regresses this. */ |
| 167 | OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION == |
| 168 | SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET, |
| 169 | ssl_alert_reason_code_mismatch); |
David Benjamin | e113608 | 2014-09-20 12:28:58 -0400 | [diff] [blame] | 170 | |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 171 | /* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */ |
| 172 | static const size_t kMaxHandshakeSize = (1u << 24) - 1; |
| 173 | |
David Benjamin | aa58513 | 2015-06-29 23:36:17 -0400 | [diff] [blame] | 174 | static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl = |
| 175 | CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA; |
| 176 | static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx = |
| 177 | CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA; |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 178 | |
David Benjamin | 7c6d09b | 2015-09-05 11:00:19 -0400 | [diff] [blame] | 179 | int SSL_library_init(void) { |
| 180 | CRYPTO_library_init(); |
| 181 | return 1; |
| 182 | } |
| 183 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 184 | static uint32_t ssl_session_hash(const SSL_SESSION *a) { |
| 185 | uint32_t hash = |
| 186 | ((uint32_t)a->session_id[0]) || |
| 187 | ((uint32_t)a->session_id[1] << 8) || |
| 188 | ((uint32_t)a->session_id[2] << 16) || |
| 189 | ((uint32_t)a->session_id[3] << 24); |
David Benjamin | 7c6d09b | 2015-09-05 11:00:19 -0400 | [diff] [blame] | 190 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 191 | return hash; |
| 192 | } |
| 193 | |
| 194 | /* NB: If this function (or indeed the hash function which uses a sort of |
| 195 | * coarser function than this one) is changed, ensure |
| 196 | * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being |
| 197 | * able to construct an SSL_SESSION that will collide with any existing session |
| 198 | * with a matching session ID. */ |
| 199 | static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) { |
| 200 | if (a->ssl_version != b->ssl_version) { |
| 201 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 202 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 204 | if (a->session_id_length != b->session_id_length) { |
| 205 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 206 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 207 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 208 | return memcmp(a->session_id, b->session_id, a->session_id_length); |
| 209 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 210 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 211 | SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) { |
| 212 | SSL_CTX *ret = NULL; |
| 213 | |
| 214 | if (method == NULL) { |
| 215 | OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED); |
| 216 | return NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 217 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 218 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 219 | if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) { |
| 220 | OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS); |
| 221 | goto err; |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame] | 222 | } |
| 223 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 224 | ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX)); |
| 225 | if (ret == NULL) { |
| 226 | goto err; |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame] | 227 | } |
| 228 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 229 | memset(ret, 0, sizeof(SSL_CTX)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 230 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 231 | ret->method = method->method; |
| 232 | |
| 233 | CRYPTO_MUTEX_init(&ret->lock); |
| 234 | |
| 235 | ret->session_cache_mode = SSL_SESS_CACHE_SERVER; |
| 236 | ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; |
| 237 | |
| 238 | /* We take the system default */ |
| 239 | ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
| 240 | |
| 241 | ret->references = 1; |
| 242 | |
| 243 | ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT; |
| 244 | ret->verify_mode = SSL_VERIFY_NONE; |
| 245 | ret->cert = ssl_cert_new(); |
| 246 | if (ret->cert == NULL) { |
| 247 | goto err; |
| 248 | } |
| 249 | |
| 250 | ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp); |
| 251 | if (ret->sessions == NULL) { |
| 252 | goto err; |
| 253 | } |
| 254 | ret->cert_store = X509_STORE_new(); |
| 255 | if (ret->cert_store == NULL) { |
| 256 | goto err; |
| 257 | } |
| 258 | |
| 259 | ssl_create_cipher_list(ret->method, &ret->cipher_list, |
| 260 | &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST); |
| 261 | if (ret->cipher_list == NULL || |
| 262 | sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) { |
| 263 | OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS); |
| 264 | goto err2; |
| 265 | } |
| 266 | |
| 267 | ret->param = X509_VERIFY_PARAM_new(); |
| 268 | if (!ret->param) { |
| 269 | goto err; |
| 270 | } |
| 271 | |
| 272 | ret->client_CA = sk_X509_NAME_new_null(); |
| 273 | if (ret->client_CA == NULL) { |
| 274 | goto err; |
| 275 | } |
| 276 | |
| 277 | CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data); |
| 278 | |
| 279 | ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
| 280 | |
| 281 | /* Setup RFC4507 ticket keys */ |
| 282 | if (!RAND_bytes(ret->tlsext_tick_key_name, 16) || |
| 283 | !RAND_bytes(ret->tlsext_tick_hmac_key, 16) || |
| 284 | !RAND_bytes(ret->tlsext_tick_aes_key, 16)) { |
| 285 | ret->options |= SSL_OP_NO_TICKET; |
| 286 | } |
| 287 | |
| 288 | /* Default is to connect to non-RI servers. When RI is more widely deployed |
| 289 | * might change this. */ |
| 290 | ret->options |= SSL_OP_LEGACY_SERVER_CONNECT; |
| 291 | |
| 292 | /* Lock the SSL_CTX to the specified version, for compatibility with legacy |
| 293 | * uses of SSL_METHOD. */ |
| 294 | if (method->version != 0) { |
| 295 | SSL_CTX_set_max_version(ret, method->version); |
| 296 | SSL_CTX_set_min_version(ret, method->version); |
| 297 | } |
| 298 | |
| 299 | return ret; |
| 300 | |
| 301 | err: |
| 302 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 303 | err2: |
| 304 | SSL_CTX_free(ret); |
| 305 | return NULL; |
| 306 | } |
| 307 | |
| 308 | void SSL_CTX_free(SSL_CTX *ctx) { |
| 309 | if (ctx == NULL || |
| 310 | !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) { |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | X509_VERIFY_PARAM_free(ctx->param); |
| 315 | |
| 316 | /* Free internal session cache. However: the remove_cb() may reference the |
| 317 | * ex_data of SSL_CTX, thus the ex_data store can only be removed after the |
| 318 | * sessions were flushed. As the ex_data handling routines might also touch |
| 319 | * the session cache, the most secure solution seems to be: empty (flush) the |
| 320 | * cache, then free ex_data, then finally free the cache. (See ticket |
| 321 | * [openssl.org #212].) */ |
| 322 | SSL_CTX_flush_sessions(ctx, 0); |
| 323 | |
| 324 | CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data); |
| 325 | |
| 326 | CRYPTO_MUTEX_cleanup(&ctx->lock); |
| 327 | lh_SSL_SESSION_free(ctx->sessions); |
| 328 | X509_STORE_free(ctx->cert_store); |
| 329 | ssl_cipher_preference_list_free(ctx->cipher_list); |
| 330 | sk_SSL_CIPHER_free(ctx->cipher_list_by_id); |
| 331 | ssl_cipher_preference_list_free(ctx->cipher_list_tls10); |
| 332 | ssl_cipher_preference_list_free(ctx->cipher_list_tls11); |
| 333 | ssl_cert_free(ctx->cert); |
| 334 | sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions, |
| 335 | SSL_CUSTOM_EXTENSION_free); |
| 336 | sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions, |
| 337 | SSL_CUSTOM_EXTENSION_free); |
| 338 | sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free); |
| 339 | sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles); |
| 340 | OPENSSL_free(ctx->psk_identity_hint); |
| 341 | OPENSSL_free(ctx->tlsext_ellipticcurvelist); |
| 342 | OPENSSL_free(ctx->alpn_client_proto_list); |
| 343 | OPENSSL_free(ctx->ocsp_response); |
| 344 | OPENSSL_free(ctx->signed_cert_timestamp_list); |
| 345 | EVP_PKEY_free(ctx->tlsext_channel_id_private); |
| 346 | BIO_free(ctx->keylog_bio); |
| 347 | |
| 348 | OPENSSL_free(ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 349 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 350 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 351 | SSL *SSL_new(SSL_CTX *ctx) { |
| 352 | SSL *s; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 353 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 354 | if (ctx == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 355 | OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 356 | return NULL; |
| 357 | } |
| 358 | if (ctx->method == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 359 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 360 | return NULL; |
| 361 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 362 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 363 | s = (SSL *)OPENSSL_malloc(sizeof(SSL)); |
| 364 | if (s == NULL) { |
| 365 | goto err; |
| 366 | } |
| 367 | memset(s, 0, sizeof(SSL)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 368 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 369 | s->min_version = ctx->min_version; |
| 370 | s->max_version = ctx->max_version; |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 371 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 372 | s->options = ctx->options; |
| 373 | s->mode = ctx->mode; |
| 374 | s->max_cert_list = ctx->max_cert_list; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 375 | |
David Benjamin | a5a3eeb | 2015-03-18 20:26:30 -0400 | [diff] [blame] | 376 | s->cert = ssl_cert_dup(ctx->cert); |
| 377 | if (s->cert == NULL) { |
| 378 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 379 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 380 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 381 | s->msg_callback = ctx->msg_callback; |
| 382 | s->msg_callback_arg = ctx->msg_callback_arg; |
| 383 | s->verify_mode = ctx->verify_mode; |
| 384 | s->sid_ctx_length = ctx->sid_ctx_length; |
| 385 | assert(s->sid_ctx_length <= sizeof s->sid_ctx); |
| 386 | memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx)); |
| 387 | s->verify_callback = ctx->default_verify_callback; |
| 388 | s->generate_session_id = ctx->generate_session_id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 389 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 390 | s->param = X509_VERIFY_PARAM_new(); |
| 391 | if (!s->param) { |
| 392 | goto err; |
| 393 | } |
| 394 | X509_VERIFY_PARAM_inherit(s->param, ctx->param); |
| 395 | s->quiet_shutdown = ctx->quiet_shutdown; |
| 396 | s->max_send_fragment = ctx->max_send_fragment; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 397 | |
Adam Langley | 0b5e390 | 2015-05-15 13:08:38 -0700 | [diff] [blame] | 398 | CRYPTO_refcount_inc(&ctx->references); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 399 | s->ctx = ctx; |
Adam Langley | 0b5e390 | 2015-05-15 13:08:38 -0700 | [diff] [blame] | 400 | CRYPTO_refcount_inc(&ctx->references); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 401 | s->initial_ctx = ctx; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 402 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 403 | if (ctx->tlsext_ellipticcurvelist) { |
| 404 | s->tlsext_ellipticcurvelist = |
| 405 | BUF_memdup(ctx->tlsext_ellipticcurvelist, |
| 406 | ctx->tlsext_ellipticcurvelist_length * 2); |
| 407 | if (!s->tlsext_ellipticcurvelist) { |
| 408 | goto err; |
| 409 | } |
| 410 | s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length; |
| 411 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 412 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 413 | if (s->ctx->alpn_client_proto_list) { |
| 414 | s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list, |
| 415 | s->ctx->alpn_client_proto_list_len); |
| 416 | if (s->alpn_client_proto_list == NULL) { |
| 417 | goto err; |
| 418 | } |
| 419 | s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len; |
| 420 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 421 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 422 | s->verify_result = X509_V_OK; |
| 423 | s->method = ctx->method; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 424 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 425 | if (!s->method->ssl_new(s)) { |
| 426 | goto err; |
| 427 | } |
| 428 | s->enc_method = ssl3_get_enc_method(s->version); |
| 429 | assert(s->enc_method != NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 430 | |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame] | 431 | s->rwstate = SSL_NOTHING; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 432 | |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 433 | CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 434 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 435 | s->psk_identity_hint = NULL; |
| 436 | if (ctx->psk_identity_hint) { |
| 437 | s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint); |
| 438 | if (s->psk_identity_hint == NULL) { |
| 439 | goto err; |
| 440 | } |
| 441 | } |
| 442 | s->psk_client_callback = ctx->psk_client_callback; |
| 443 | s->psk_server_callback = ctx->psk_server_callback; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 444 | |
David Benjamin | 02ddbfd | 2015-01-11 13:09:11 -0500 | [diff] [blame] | 445 | s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled; |
| 446 | if (ctx->tlsext_channel_id_private) { |
David Benjamin | 9a10f8f | 2015-05-05 22:22:40 -0400 | [diff] [blame] | 447 | s->tlsext_channel_id_private = |
| 448 | EVP_PKEY_up_ref(ctx->tlsext_channel_id_private); |
David Benjamin | 02ddbfd | 2015-01-11 13:09:11 -0500 | [diff] [blame] | 449 | } |
| 450 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 451 | s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled; |
| 452 | s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled; |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 453 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 454 | return s; |
| 455 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 456 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 457 | SSL_free(s); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 458 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 459 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 460 | return NULL; |
| 461 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 462 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 463 | void SSL_free(SSL *ssl) { |
| 464 | if (ssl == NULL) { |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | X509_VERIFY_PARAM_free(ssl->param); |
| 469 | |
| 470 | CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data); |
| 471 | |
| 472 | if (ssl->bbio != NULL) { |
| 473 | /* If the buffering BIO is in place, pop it off */ |
| 474 | if (ssl->bbio == ssl->wbio) { |
| 475 | ssl->wbio = BIO_pop(ssl->wbio); |
| 476 | } |
| 477 | BIO_free(ssl->bbio); |
| 478 | ssl->bbio = NULL; |
| 479 | } |
| 480 | |
| 481 | int free_wbio = ssl->wbio != ssl->rbio; |
| 482 | BIO_free_all(ssl->rbio); |
| 483 | if (free_wbio) { |
| 484 | BIO_free_all(ssl->wbio); |
| 485 | } |
| 486 | |
| 487 | BUF_MEM_free(ssl->init_buf); |
| 488 | |
| 489 | /* add extra stuff */ |
| 490 | ssl_cipher_preference_list_free(ssl->cipher_list); |
| 491 | sk_SSL_CIPHER_free(ssl->cipher_list_by_id); |
| 492 | |
| 493 | ssl_clear_bad_session(ssl); |
| 494 | SSL_SESSION_free(ssl->session); |
| 495 | |
| 496 | ssl_clear_cipher_ctx(ssl); |
| 497 | |
| 498 | ssl_cert_free(ssl->cert); |
| 499 | |
| 500 | OPENSSL_free(ssl->tlsext_hostname); |
| 501 | SSL_CTX_free(ssl->initial_ctx); |
| 502 | OPENSSL_free(ssl->tlsext_ellipticcurvelist); |
| 503 | OPENSSL_free(ssl->alpn_client_proto_list); |
| 504 | EVP_PKEY_free(ssl->tlsext_channel_id_private); |
| 505 | OPENSSL_free(ssl->psk_identity_hint); |
| 506 | sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free); |
| 507 | OPENSSL_free(ssl->next_proto_negotiated); |
| 508 | sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles); |
| 509 | |
| 510 | if (ssl->method != NULL) { |
| 511 | ssl->method->ssl_free(ssl); |
| 512 | } |
| 513 | SSL_CTX_free(ssl->ctx); |
| 514 | |
| 515 | OPENSSL_free(ssl); |
| 516 | } |
| 517 | |
| 518 | void SSL_set_connect_state(SSL *ssl) { |
| 519 | ssl->server = 0; |
| 520 | ssl->shutdown = 0; |
| 521 | ssl->state = SSL_ST_CONNECT; |
| 522 | ssl->handshake_func = ssl->method->ssl_connect; |
| 523 | /* clear the current cipher */ |
| 524 | ssl_clear_cipher_ctx(ssl); |
| 525 | } |
| 526 | |
| 527 | void SSL_set_accept_state(SSL *ssl) { |
| 528 | ssl->server = 1; |
| 529 | ssl->shutdown = 0; |
| 530 | ssl->state = SSL_ST_ACCEPT; |
| 531 | ssl->handshake_func = ssl->method->ssl_accept; |
| 532 | /* clear the current cipher */ |
| 533 | ssl_clear_cipher_ctx(ssl); |
| 534 | } |
| 535 | |
| 536 | void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) { |
| 537 | /* If the output buffering BIO is still in place, remove it. */ |
| 538 | if (ssl->bbio != NULL) { |
| 539 | if (ssl->wbio == ssl->bbio) { |
| 540 | ssl->wbio = ssl->wbio->next_bio; |
| 541 | ssl->bbio->next_bio = NULL; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | if (ssl->rbio != rbio) { |
| 546 | BIO_free_all(ssl->rbio); |
| 547 | } |
| 548 | if (ssl->wbio != wbio && ssl->rbio != ssl->wbio) { |
| 549 | BIO_free_all(ssl->wbio); |
| 550 | } |
| 551 | ssl->rbio = rbio; |
| 552 | ssl->wbio = wbio; |
| 553 | } |
| 554 | |
| 555 | BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; } |
| 556 | |
| 557 | BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; } |
| 558 | |
| 559 | int SSL_do_handshake(SSL *ssl) { |
| 560 | if (ssl->handshake_func == NULL) { |
| 561 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET); |
| 562 | return -1; |
| 563 | } |
| 564 | |
| 565 | if (!SSL_in_init(ssl)) { |
| 566 | return 1; |
| 567 | } |
| 568 | |
| 569 | return ssl->handshake_func(ssl); |
| 570 | } |
| 571 | |
| 572 | int SSL_connect(SSL *ssl) { |
| 573 | if (ssl->handshake_func == 0) { |
| 574 | /* Not properly initialized yet */ |
| 575 | SSL_set_connect_state(ssl); |
| 576 | } |
| 577 | |
| 578 | if (ssl->handshake_func != ssl->method->ssl_connect) { |
| 579 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 580 | return -1; |
| 581 | } |
| 582 | |
| 583 | return ssl->handshake_func(ssl); |
| 584 | } |
| 585 | |
| 586 | int SSL_accept(SSL *ssl) { |
| 587 | if (ssl->handshake_func == 0) { |
| 588 | /* Not properly initialized yet */ |
| 589 | SSL_set_accept_state(ssl); |
| 590 | } |
| 591 | |
| 592 | if (ssl->handshake_func != ssl->method->ssl_accept) { |
| 593 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 594 | return -1; |
| 595 | } |
| 596 | |
| 597 | return ssl->handshake_func(ssl); |
| 598 | } |
| 599 | |
| 600 | int SSL_read(SSL *ssl, void *buf, int num) { |
| 601 | if (ssl->handshake_func == 0) { |
| 602 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 603 | return -1; |
| 604 | } |
| 605 | |
| 606 | if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) { |
| 607 | ssl->rwstate = SSL_NOTHING; |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | ERR_clear_system_error(); |
| 612 | return ssl->method->ssl_read_app_data(ssl, buf, num, 0); |
| 613 | } |
| 614 | |
| 615 | int SSL_peek(SSL *ssl, void *buf, int num) { |
| 616 | if (ssl->handshake_func == 0) { |
| 617 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 618 | return -1; |
| 619 | } |
| 620 | |
| 621 | if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) { |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | ERR_clear_system_error(); |
| 626 | return ssl->method->ssl_read_app_data(ssl, buf, num, 1); |
| 627 | } |
| 628 | |
| 629 | int SSL_write(SSL *ssl, const void *buf, int num) { |
| 630 | if (ssl->handshake_func == 0) { |
| 631 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 632 | return -1; |
| 633 | } |
| 634 | |
| 635 | if (ssl->shutdown & SSL_SENT_SHUTDOWN) { |
| 636 | ssl->rwstate = SSL_NOTHING; |
| 637 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 638 | return -1; |
| 639 | } |
| 640 | |
| 641 | ERR_clear_system_error(); |
| 642 | return ssl->method->ssl_write_app_data(ssl, buf, num); |
| 643 | } |
| 644 | |
| 645 | int SSL_shutdown(SSL *ssl) { |
| 646 | /* Note that this function behaves differently from what one might expect. |
| 647 | * Return values are 0 for no success (yet), 1 for success; but calling it |
| 648 | * once is usually not enough, even if blocking I/O is used (see |
| 649 | * ssl3_shutdown). */ |
| 650 | |
| 651 | if (ssl->handshake_func == 0) { |
| 652 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 653 | return -1; |
| 654 | } |
| 655 | |
| 656 | if (SSL_in_init(ssl)) { |
| 657 | return 1; |
| 658 | } |
| 659 | |
| 660 | /* Do nothing if configured not to send a close_notify. */ |
| 661 | if (ssl->quiet_shutdown) { |
| 662 | ssl->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN; |
| 663 | return 1; |
| 664 | } |
| 665 | |
| 666 | if (!(ssl->shutdown & SSL_SENT_SHUTDOWN)) { |
| 667 | ssl->shutdown |= SSL_SENT_SHUTDOWN; |
| 668 | ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY); |
| 669 | |
| 670 | /* our shutdown alert has been sent now, and if it still needs to be |
| 671 | * written, ssl->s3->alert_dispatch will be true */ |
| 672 | if (ssl->s3->alert_dispatch) { |
| 673 | return -1; /* return WANT_WRITE */ |
| 674 | } |
| 675 | } else if (ssl->s3->alert_dispatch) { |
| 676 | /* resend it if not sent */ |
| 677 | int ret = ssl->method->ssl_dispatch_alert(ssl); |
| 678 | if (ret == -1) { |
| 679 | /* we only get to return -1 here the 2nd/Nth invocation, we must have |
| 680 | * already signalled return 0 upon a previous invoation, return |
| 681 | * WANT_WRITE */ |
| 682 | return ret; |
| 683 | } |
| 684 | } else if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) { |
| 685 | /* If we are waiting for a close from our peer, we are closed */ |
| 686 | ssl->method->ssl_read_close_notify(ssl); |
| 687 | if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) { |
| 688 | return -1; /* return WANT_READ */ |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | if (ssl->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) && |
| 693 | !ssl->s3->alert_dispatch) { |
| 694 | return 1; |
| 695 | } else { |
| 696 | return 0; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | int SSL_get_error(const SSL *ssl, int ret_code) { |
| 701 | int reason; |
| 702 | uint32_t err; |
| 703 | BIO *bio; |
| 704 | |
| 705 | if (ret_code > 0) { |
| 706 | return SSL_ERROR_NONE; |
| 707 | } |
| 708 | |
| 709 | /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc, |
| 710 | * where we do encode the error */ |
| 711 | err = ERR_peek_error(); |
| 712 | if (err != 0) { |
| 713 | if (ERR_GET_LIB(err) == ERR_LIB_SYS) { |
| 714 | return SSL_ERROR_SYSCALL; |
| 715 | } |
| 716 | return SSL_ERROR_SSL; |
| 717 | } |
| 718 | |
| 719 | if (ret_code == 0) { |
| 720 | if ((ssl->shutdown & SSL_RECEIVED_SHUTDOWN) && |
| 721 | (ssl->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) { |
| 722 | /* The socket was cleanly shut down with a close_notify. */ |
| 723 | return SSL_ERROR_ZERO_RETURN; |
| 724 | } |
| 725 | /* An EOF was observed which violates the protocol, and the underlying |
| 726 | * transport does not participate in the error queue. Bubble up to the |
| 727 | * caller. */ |
| 728 | return SSL_ERROR_SYSCALL; |
| 729 | } |
| 730 | |
| 731 | if (SSL_want_session(ssl)) { |
| 732 | return SSL_ERROR_PENDING_SESSION; |
| 733 | } |
| 734 | |
| 735 | if (SSL_want_certificate(ssl)) { |
| 736 | return SSL_ERROR_PENDING_CERTIFICATE; |
| 737 | } |
| 738 | |
| 739 | if (SSL_want_read(ssl)) { |
| 740 | bio = SSL_get_rbio(ssl); |
| 741 | if (BIO_should_read(bio)) { |
| 742 | return SSL_ERROR_WANT_READ; |
| 743 | } |
| 744 | |
| 745 | if (BIO_should_write(bio)) { |
| 746 | /* This one doesn't make too much sense ... We never try to write to the |
| 747 | * rbio, and an application program where rbio and wbio are separate |
| 748 | * couldn't even know what it should wait for. However if we ever set |
| 749 | * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of |
| 750 | * SSL_want_write(s)) and rbio and wbio *are* the same, this test works |
| 751 | * around that bug; so it might be safer to keep it. */ |
| 752 | return SSL_ERROR_WANT_WRITE; |
| 753 | } |
| 754 | |
| 755 | if (BIO_should_io_special(bio)) { |
| 756 | reason = BIO_get_retry_reason(bio); |
| 757 | if (reason == BIO_RR_CONNECT) { |
| 758 | return SSL_ERROR_WANT_CONNECT; |
| 759 | } |
| 760 | |
| 761 | if (reason == BIO_RR_ACCEPT) { |
| 762 | return SSL_ERROR_WANT_ACCEPT; |
| 763 | } |
| 764 | |
| 765 | return SSL_ERROR_SYSCALL; /* unknown */ |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | if (SSL_want_write(ssl)) { |
| 770 | bio = SSL_get_wbio(ssl); |
| 771 | if (BIO_should_write(bio)) { |
| 772 | return SSL_ERROR_WANT_WRITE; |
| 773 | } |
| 774 | |
| 775 | if (BIO_should_read(bio)) { |
| 776 | /* See above (SSL_want_read(ssl) with BIO_should_write(bio)) */ |
| 777 | return SSL_ERROR_WANT_READ; |
| 778 | } |
| 779 | |
| 780 | if (BIO_should_io_special(bio)) { |
| 781 | reason = BIO_get_retry_reason(bio); |
| 782 | if (reason == BIO_RR_CONNECT) { |
| 783 | return SSL_ERROR_WANT_CONNECT; |
| 784 | } |
| 785 | |
| 786 | if (reason == BIO_RR_ACCEPT) { |
| 787 | return SSL_ERROR_WANT_ACCEPT; |
| 788 | } |
| 789 | |
| 790 | return SSL_ERROR_SYSCALL; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | if (SSL_want_x509_lookup(ssl)) { |
| 795 | return SSL_ERROR_WANT_X509_LOOKUP; |
| 796 | } |
| 797 | |
| 798 | if (SSL_want_channel_id_lookup(ssl)) { |
| 799 | return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP; |
| 800 | } |
| 801 | |
| 802 | if (SSL_want_private_key_operation(ssl)) { |
| 803 | return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION; |
| 804 | } |
| 805 | |
| 806 | return SSL_ERROR_SYSCALL; |
| 807 | } |
| 808 | |
| 809 | void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) { |
| 810 | ctx->min_version = version; |
| 811 | } |
| 812 | |
| 813 | void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) { |
| 814 | ctx->max_version = version; |
| 815 | } |
| 816 | |
| 817 | void SSL_set_min_version(SSL *ssl, uint16_t version) { |
| 818 | ssl->min_version = version; |
| 819 | } |
| 820 | |
| 821 | void SSL_set_max_version(SSL *ssl, uint16_t version) { |
| 822 | ssl->max_version = version; |
| 823 | } |
| 824 | |
| 825 | uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) { |
| 826 | ctx->options |= options; |
| 827 | return ctx->options; |
| 828 | } |
| 829 | |
| 830 | uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) { |
| 831 | ctx->options &= ~options; |
| 832 | return ctx->options; |
| 833 | } |
| 834 | |
| 835 | uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; } |
| 836 | |
| 837 | uint32_t SSL_set_options(SSL *ssl, uint32_t options) { |
| 838 | ssl->options |= options; |
| 839 | return ssl->options; |
| 840 | } |
| 841 | |
| 842 | uint32_t SSL_clear_options(SSL *ssl, uint32_t options) { |
| 843 | ssl->options &= ~options; |
| 844 | return ssl->options; |
| 845 | } |
| 846 | |
| 847 | uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; } |
| 848 | |
| 849 | uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) { |
| 850 | ctx->mode |= mode; |
| 851 | return ctx->mode; |
| 852 | } |
| 853 | |
| 854 | uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) { |
| 855 | ctx->mode &= ~mode; |
| 856 | return ctx->mode; |
| 857 | } |
| 858 | |
| 859 | uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; } |
| 860 | |
| 861 | uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) { |
| 862 | ssl->mode |= mode; |
| 863 | return ssl->mode; |
| 864 | } |
| 865 | |
| 866 | uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) { |
| 867 | ssl->mode &= ~mode; |
| 868 | return ssl->mode; |
| 869 | } |
| 870 | |
| 871 | uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; } |
| 872 | |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 873 | X509 *SSL_get_peer_certificate(const SSL *ssl) { |
| 874 | if (ssl == NULL || ssl->session == NULL || ssl->session->peer == NULL) { |
| 875 | return NULL; |
| 876 | } |
| 877 | return X509_up_ref(ssl->session->peer); |
| 878 | } |
| 879 | |
| 880 | STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) { |
| 881 | if (ssl == NULL || ssl->session == NULL) { |
| 882 | return NULL; |
| 883 | } |
| 884 | return ssl->session->cert_chain; |
| 885 | } |
| 886 | |
| 887 | int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len, |
| 888 | size_t max_out) { |
| 889 | /* The tls-unique value is the first Finished message in the handshake, which |
| 890 | * is the client's in a full handshake and the server's for a resumption. See |
| 891 | * https://tools.ietf.org/html/rfc5929#section-3.1. */ |
| 892 | const uint8_t *finished = ssl->s3->previous_client_finished; |
| 893 | size_t finished_len = ssl->s3->previous_client_finished_len; |
| 894 | if (ssl->hit) { |
| 895 | /* tls-unique is broken for resumed sessions unless EMS is used. */ |
| 896 | if (!ssl->session->extended_master_secret) { |
| 897 | goto err; |
| 898 | } |
| 899 | finished = ssl->s3->previous_server_finished; |
| 900 | finished_len = ssl->s3->previous_server_finished_len; |
| 901 | } |
| 902 | |
| 903 | if (!ssl->s3->initial_handshake_complete || |
| 904 | ssl->version < TLS1_VERSION) { |
| 905 | goto err; |
| 906 | } |
| 907 | |
| 908 | *out_len = finished_len; |
| 909 | if (finished_len > max_out) { |
| 910 | *out_len = max_out; |
| 911 | } |
| 912 | |
| 913 | memcpy(out, finished, *out_len); |
| 914 | return 1; |
| 915 | |
| 916 | err: |
| 917 | *out_len = 0; |
| 918 | memset(out, 0, max_out); |
| 919 | return 0; |
| 920 | } |
| 921 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 922 | int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx, |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 923 | unsigned sid_ctx_len) { |
| 924 | if (sid_ctx_len > sizeof(ctx->sid_ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 925 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 926 | return 0; |
| 927 | } |
| 928 | ctx->sid_ctx_length = sid_ctx_len; |
| 929 | memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 930 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 931 | return 1; |
| 932 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 933 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 934 | int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx, |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 935 | unsigned sid_ctx_len) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 936 | if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 937 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 938 | return 0; |
| 939 | } |
| 940 | ssl->sid_ctx_length = sid_ctx_len; |
| 941 | memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 942 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 943 | return 1; |
| 944 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 945 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 946 | int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 947 | ctx->generate_session_id = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 948 | return 1; |
| 949 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 950 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 951 | int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 952 | ssl->generate_session_id = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 953 | return 1; |
| 954 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 955 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 956 | int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id, |
David Benjamin | dafbdd4 | 2015-09-14 01:40:10 -0400 | [diff] [blame] | 957 | unsigned id_len) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 958 | /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we |
| 959 | * can "construct" a session to give us the desired check - ie. to find if |
| 960 | * there's a session in the hash table that would conflict with any new |
| 961 | * session built out of this id/id_len and the ssl_version in use by this |
| 962 | * SSL. */ |
| 963 | SSL_SESSION r, *p; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 964 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 965 | if (id_len > sizeof r.session_id) { |
| 966 | return 0; |
| 967 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 968 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 969 | r.ssl_version = ssl->version; |
| 970 | r.session_id_length = id_len; |
| 971 | memcpy(r.session_id, id, id_len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 972 | |
Adam Langley | 4bdb6e4 | 2015-05-15 15:29:21 -0700 | [diff] [blame] | 973 | CRYPTO_MUTEX_lock_read(&ssl->ctx->lock); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 974 | p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r); |
Adam Langley | 4bdb6e4 | 2015-05-15 15:29:21 -0700 | [diff] [blame] | 975 | CRYPTO_MUTEX_unlock(&ssl->ctx->lock); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 976 | return p != NULL; |
| 977 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 978 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame^] | 979 | int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) { |
| 980 | return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 981 | } |
| 982 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame^] | 983 | int SSL_set_purpose(SSL *ssl, int purpose) { |
| 984 | return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 985 | } |
| 986 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame^] | 987 | int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) { |
| 988 | return X509_VERIFY_PARAM_set_trust(ctx->param, trust); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 989 | } |
| 990 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame^] | 991 | int SSL_set_trust(SSL *ssl, int trust) { |
| 992 | return X509_VERIFY_PARAM_set_trust(ssl->param, trust); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 993 | } |
| 994 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame^] | 995 | int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) { |
| 996 | return X509_VERIFY_PARAM_set1(ctx->param, param); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 997 | } |
| 998 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame^] | 999 | int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) { |
| 1000 | return X509_VERIFY_PARAM_set1(ssl->param, param); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1001 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1002 | |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1003 | void ssl_cipher_preference_list_free( |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1004 | struct ssl_cipher_preference_list_st *cipher_list) { |
David Benjamin | 5d1ec73 | 2015-04-22 13:38:00 -0400 | [diff] [blame] | 1005 | if (cipher_list == NULL) { |
| 1006 | return; |
| 1007 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1008 | sk_SSL_CIPHER_free(cipher_list->ciphers); |
| 1009 | OPENSSL_free(cipher_list->in_group_flags); |
| 1010 | OPENSSL_free(cipher_list); |
| 1011 | } |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1012 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1013 | struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup( |
| 1014 | struct ssl_cipher_preference_list_st *cipher_list) { |
| 1015 | struct ssl_cipher_preference_list_st *ret = NULL; |
| 1016 | size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers); |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1017 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1018 | ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st)); |
| 1019 | if (!ret) { |
| 1020 | goto err; |
| 1021 | } |
| 1022 | |
| 1023 | ret->ciphers = NULL; |
| 1024 | ret->in_group_flags = NULL; |
| 1025 | ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers); |
| 1026 | if (!ret->ciphers) { |
| 1027 | goto err; |
| 1028 | } |
| 1029 | ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n); |
| 1030 | if (!ret->in_group_flags) { |
| 1031 | goto err; |
| 1032 | } |
| 1033 | |
| 1034 | return ret; |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1035 | |
| 1036 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1037 | ssl_cipher_preference_list_free(ret); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1038 | return NULL; |
| 1039 | } |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1040 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1041 | struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers( |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 1042 | STACK_OF(SSL_CIPHER) *ciphers) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1043 | struct ssl_cipher_preference_list_st *ret = NULL; |
| 1044 | size_t n = sk_SSL_CIPHER_num(ciphers); |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1045 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1046 | ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st)); |
| 1047 | if (!ret) { |
| 1048 | goto err; |
| 1049 | } |
| 1050 | ret->ciphers = NULL; |
| 1051 | ret->in_group_flags = NULL; |
| 1052 | ret->ciphers = sk_SSL_CIPHER_dup(ciphers); |
| 1053 | if (!ret->ciphers) { |
| 1054 | goto err; |
| 1055 | } |
| 1056 | ret->in_group_flags = OPENSSL_malloc(n); |
| 1057 | if (!ret->in_group_flags) { |
| 1058 | goto err; |
| 1059 | } |
| 1060 | memset(ret->in_group_flags, 0, n); |
| 1061 | return ret; |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1062 | |
| 1063 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1064 | ssl_cipher_preference_list_free(ret); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1065 | return NULL; |
| 1066 | } |
Adam Langley | 858a88d | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1067 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1068 | X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1069 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1070 | X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1071 | |
David Benjamin | 7481d39 | 2015-07-05 19:38:46 -0400 | [diff] [blame] | 1072 | void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1073 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1074 | int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1075 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1076 | int SSL_get_rfd(const SSL *s) { |
| 1077 | int ret = -1; |
| 1078 | BIO *b, *r; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1079 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1080 | b = SSL_get_rbio(s); |
| 1081 | r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR); |
| 1082 | if (r != NULL) { |
| 1083 | BIO_get_fd(r, &ret); |
| 1084 | } |
| 1085 | return ret; |
| 1086 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1087 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1088 | int SSL_get_wfd(const SSL *s) { |
| 1089 | int ret = -1; |
| 1090 | BIO *b, *r; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1091 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1092 | b = SSL_get_wbio(s); |
| 1093 | r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR); |
| 1094 | if (r != NULL) { |
| 1095 | BIO_get_fd(r, &ret); |
| 1096 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1097 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1098 | return ret; |
| 1099 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1100 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1101 | int SSL_set_fd(SSL *s, int fd) { |
| 1102 | int ret = 0; |
| 1103 | BIO *bio = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1104 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1105 | bio = BIO_new(BIO_s_fd()); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1106 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1107 | if (bio == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1108 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1109 | goto err; |
| 1110 | } |
| 1111 | BIO_set_fd(bio, fd, BIO_NOCLOSE); |
| 1112 | SSL_set_bio(s, bio, bio); |
| 1113 | ret = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1114 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1115 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1116 | return ret; |
| 1117 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1118 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1119 | int SSL_set_wfd(SSL *s, int fd) { |
| 1120 | int ret = 0; |
| 1121 | BIO *bio = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1122 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1123 | if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD || |
| 1124 | (int)BIO_get_fd(s->rbio, NULL) != fd) { |
| 1125 | bio = BIO_new(BIO_s_fd()); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1126 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1127 | if (bio == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1128 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1129 | goto err; |
| 1130 | } |
| 1131 | BIO_set_fd(bio, fd, BIO_NOCLOSE); |
| 1132 | SSL_set_bio(s, SSL_get_rbio(s), bio); |
| 1133 | } else { |
| 1134 | SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s)); |
| 1135 | } |
| 1136 | |
| 1137 | ret = 1; |
| 1138 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1139 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1140 | return ret; |
| 1141 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1142 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1143 | int SSL_set_rfd(SSL *s, int fd) { |
| 1144 | int ret = 0; |
| 1145 | BIO *bio = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1146 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1147 | if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD || |
| 1148 | (int)BIO_get_fd(s->wbio, NULL) != fd) { |
| 1149 | bio = BIO_new(BIO_s_fd()); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1150 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1151 | if (bio == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1152 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1153 | goto err; |
| 1154 | } |
| 1155 | BIO_set_fd(bio, fd, BIO_NOCLOSE); |
| 1156 | SSL_set_bio(s, bio, SSL_get_wbio(s)); |
| 1157 | } else { |
| 1158 | SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s)); |
| 1159 | } |
| 1160 | ret = 1; |
| 1161 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1162 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1163 | return ret; |
| 1164 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1165 | |
| 1166 | /* return length of latest Finished message we sent, copy to 'buf' */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1167 | size_t SSL_get_finished(const SSL *s, void *buf, size_t count) { |
| 1168 | size_t ret = 0; |
| 1169 | |
| 1170 | if (s->s3 != NULL) { |
| 1171 | ret = s->s3->tmp.finish_md_len; |
| 1172 | if (count > ret) { |
| 1173 | count = ret; |
| 1174 | } |
| 1175 | memcpy(buf, s->s3->tmp.finish_md, count); |
| 1176 | } |
| 1177 | |
| 1178 | return ret; |
| 1179 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1180 | |
| 1181 | /* return length of latest Finished message we expected, copy to 'buf' */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1182 | size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) { |
| 1183 | size_t ret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1184 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1185 | if (s->s3 != NULL) { |
| 1186 | ret = s->s3->tmp.peer_finish_md_len; |
| 1187 | if (count > ret) { |
| 1188 | count = ret; |
| 1189 | } |
| 1190 | memcpy(buf, s->s3->tmp.peer_finish_md, count); |
| 1191 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1192 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1193 | return ret; |
| 1194 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1195 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame^] | 1196 | int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1197 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame^] | 1198 | int SSL_get_verify_depth(const SSL *ssl) { |
| 1199 | return X509_VERIFY_PARAM_get_depth(ssl->param); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1200 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1201 | |
David Benjamin | 42fea37 | 2015-09-19 01:22:44 -0400 | [diff] [blame] | 1202 | int SSL_get_extms_support(const SSL *ssl) { |
| 1203 | return ssl->s3->tmp.extended_master_secret == 1; |
Matt Braithwaite | cd6f54b | 2015-09-17 12:54:42 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame^] | 1206 | int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) { |
| 1207 | return ssl->verify_callback; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1208 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1209 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1210 | int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1211 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1212 | int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) { |
| 1213 | return X509_VERIFY_PARAM_get_depth(ctx->param); |
| 1214 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1215 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame^] | 1216 | int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))( |
| 1217 | int ok, X509_STORE_CTX *store_ctx) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1218 | return ctx->default_verify_callback; |
| 1219 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1220 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame^] | 1221 | void SSL_set_verify(SSL *ssl, int mode, |
| 1222 | int (*callback)(int ok, X509_STORE_CTX *store_ctx)) { |
| 1223 | ssl->verify_mode = mode; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1224 | if (callback != NULL) { |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame^] | 1225 | ssl->verify_callback = callback; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1226 | } |
| 1227 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1228 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame^] | 1229 | void SSL_set_verify_depth(SSL *ssl, int depth) { |
| 1230 | X509_VERIFY_PARAM_set_depth(ssl->param, depth); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1231 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1232 | |
David Benjamin | 9a41d1b | 2015-05-16 01:30:09 -0400 | [diff] [blame] | 1233 | int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1234 | |
David Benjamin | 9a41d1b | 2015-05-16 01:30:09 -0400 | [diff] [blame] | 1235 | int SSL_get_read_ahead(const SSL *s) { return 0; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1236 | |
David Benjamin | 9a41d1b | 2015-05-16 01:30:09 -0400 | [diff] [blame] | 1237 | void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { } |
David Benjamin | 61ecccf | 2015-05-05 09:44:51 -0400 | [diff] [blame] | 1238 | |
David Benjamin | 9a41d1b | 2015-05-16 01:30:09 -0400 | [diff] [blame] | 1239 | void SSL_set_read_ahead(SSL *s, int yes) { } |
David Benjamin | 61ecccf | 2015-05-05 09:44:51 -0400 | [diff] [blame] | 1240 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1241 | int SSL_pending(const SSL *s) { |
David Benjamin | 904dc72 | 2015-05-30 16:39:07 -0400 | [diff] [blame] | 1242 | return (s->s3->rrec.type == SSL3_RT_APPLICATION_DATA) ? s->s3->rrec.length |
| 1243 | : 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1244 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1245 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1246 | /* Fix this so it checks all the valid key/cert options */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1247 | int SSL_CTX_check_private_key(const SSL_CTX *ctx) { |
David Benjamin | 651b3d9 | 2015-08-09 12:07:25 -0400 | [diff] [blame] | 1248 | if (ctx->cert->x509 == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1249 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1250 | return 0; |
| 1251 | } |
| 1252 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1253 | if (ctx->cert->privatekey == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1254 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1255 | return 0; |
| 1256 | } |
| 1257 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1258 | return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1259 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1260 | |
| 1261 | /* Fix this function so that it takes an optional type parameter */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1262 | int SSL_check_private_key(const SSL *ssl) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1263 | if (ssl->cert->x509 == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1264 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1265 | return 0; |
| 1266 | } |
David Benjamin | 0b145c2 | 2014-11-26 20:10:09 -0500 | [diff] [blame] | 1267 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1268 | if (ssl->cert->privatekey == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1269 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1270 | return 0; |
| 1271 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1272 | |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1273 | return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1274 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1275 | |
David Benjamin | 42fea37 | 2015-09-19 01:22:44 -0400 | [diff] [blame] | 1276 | long SSL_get_default_timeout(const SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1277 | return SSL_DEFAULT_SESSION_TIMEOUT; |
| 1278 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1279 | |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 1280 | int SSL_renegotiate(SSL *ssl) { |
| 1281 | /* Caller-initiated renegotiation is not supported. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1282 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 1283 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1284 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1285 | |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 1286 | int SSL_renegotiate_pending(SSL *ssl) { |
| 1287 | return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1288 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1289 | |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 1290 | size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) { |
| 1291 | return ctx->max_cert_list; |
| 1292 | } |
| 1293 | |
| 1294 | void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) { |
| 1295 | if (max_cert_list > kMaxHandshakeSize) { |
| 1296 | max_cert_list = kMaxHandshakeSize; |
| 1297 | } |
| 1298 | ctx->max_cert_list = (uint32_t)max_cert_list; |
| 1299 | } |
| 1300 | |
| 1301 | size_t SSL_get_max_cert_list(const SSL *ssl) { |
| 1302 | return ssl->max_cert_list; |
| 1303 | } |
| 1304 | |
| 1305 | void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) { |
| 1306 | if (max_cert_list > kMaxHandshakeSize) { |
| 1307 | max_cert_list = kMaxHandshakeSize; |
| 1308 | } |
| 1309 | ssl->max_cert_list = (uint32_t)max_cert_list; |
| 1310 | } |
| 1311 | |
| 1312 | void SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) { |
| 1313 | if (max_send_fragment < 512) { |
| 1314 | max_send_fragment = 512; |
| 1315 | } |
| 1316 | if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 1317 | max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
| 1318 | } |
| 1319 | ctx->max_send_fragment = (uint16_t)max_send_fragment; |
| 1320 | } |
| 1321 | |
| 1322 | void SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) { |
| 1323 | if (max_send_fragment < 512) { |
| 1324 | max_send_fragment = 512; |
| 1325 | } |
| 1326 | if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 1327 | max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
| 1328 | } |
| 1329 | ssl->max_send_fragment = (uint16_t)max_send_fragment; |
| 1330 | } |
| 1331 | |
David Benjamin | cb9cf79 | 2015-05-05 09:46:14 -0400 | [diff] [blame] | 1332 | int SSL_set_mtu(SSL *ssl, unsigned mtu) { |
| 1333 | if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) { |
| 1334 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1335 | } |
David Benjamin | cb9cf79 | 2015-05-05 09:46:14 -0400 | [diff] [blame] | 1336 | ssl->d1->mtu = mtu; |
| 1337 | return 1; |
| 1338 | } |
| 1339 | |
| 1340 | int SSL_get_secure_renegotiation_support(const SSL *ssl) { |
| 1341 | return ssl->s3->send_connection_binding; |
| 1342 | } |
| 1343 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1344 | LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; } |
| 1345 | |
David Benjamin | 71f7d3d | 2015-05-05 09:46:38 -0400 | [diff] [blame] | 1346 | size_t SSL_CTX_sess_number(const SSL_CTX *ctx) { |
| 1347 | return lh_SSL_SESSION_num_items(ctx->sessions); |
| 1348 | } |
| 1349 | |
| 1350 | unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) { |
| 1351 | unsigned long ret = ctx->session_cache_size; |
| 1352 | ctx->session_cache_size = size; |
| 1353 | return ret; |
| 1354 | } |
| 1355 | |
| 1356 | unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) { |
| 1357 | return ctx->session_cache_size; |
| 1358 | } |
| 1359 | |
| 1360 | int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) { |
| 1361 | int ret = ctx->session_cache_mode; |
| 1362 | ctx->session_cache_mode = mode; |
| 1363 | return ret; |
| 1364 | } |
| 1365 | |
| 1366 | int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) { |
| 1367 | return ctx->session_cache_mode; |
| 1368 | } |
| 1369 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1370 | /* return a STACK of the ciphers available for the SSL and in order of |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1371 | * preference */ |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 1372 | STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1373 | if (s == NULL) { |
| 1374 | return NULL; |
| 1375 | } |
Adam Langley | 0b5c1ac | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1376 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1377 | if (s->cipher_list != NULL) { |
| 1378 | return s->cipher_list->ciphers; |
| 1379 | } |
Adam Langley | 0b5c1ac | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1380 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1381 | if (s->version >= TLS1_1_VERSION && s->ctx != NULL && |
| 1382 | s->ctx->cipher_list_tls11 != NULL) { |
| 1383 | return s->ctx->cipher_list_tls11->ciphers; |
| 1384 | } |
Adam Langley | 0b5c1ac | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1385 | |
Adam Langley | cef7583 | 2015-09-03 14:51:12 -0700 | [diff] [blame] | 1386 | if (s->version >= TLS1_VERSION && s->ctx != NULL && |
| 1387 | s->ctx->cipher_list_tls10 != NULL) { |
| 1388 | return s->ctx->cipher_list_tls10->ciphers; |
| 1389 | } |
| 1390 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1391 | if (s->ctx != NULL && s->ctx->cipher_list != NULL) { |
| 1392 | return s->ctx->cipher_list->ciphers; |
| 1393 | } |
Adam Langley | 0b5c1ac | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1394 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1395 | return NULL; |
| 1396 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1397 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1398 | /* return a STACK of the ciphers available for the SSL and in order of |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1399 | * algorithm id */ |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 1400 | STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1401 | if (s == NULL) { |
| 1402 | return NULL; |
| 1403 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1404 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1405 | if (s->cipher_list_by_id != NULL) { |
| 1406 | return s->cipher_list_by_id; |
| 1407 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1408 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1409 | if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) { |
| 1410 | return s->ctx->cipher_list_by_id; |
| 1411 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1412 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1413 | return NULL; |
| 1414 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1415 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1416 | /* The old interface to get the same thing as SSL_get_ciphers() */ |
| 1417 | const char *SSL_get_cipher_list(const SSL *s, int n) { |
| 1418 | const SSL_CIPHER *c; |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 1419 | STACK_OF(SSL_CIPHER) *sk; |
Adam Langley | 0b5c1ac | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1420 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1421 | if (s == NULL) { |
| 1422 | return NULL; |
| 1423 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1424 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1425 | sk = SSL_get_ciphers(s); |
| 1426 | if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) { |
| 1427 | return NULL; |
| 1428 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1429 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1430 | c = sk_SSL_CIPHER_value(sk, n); |
| 1431 | if (c == NULL) { |
| 1432 | return NULL; |
| 1433 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1434 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1435 | return c->name; |
| 1436 | } |
David Benjamin | 5491e3f | 2014-09-29 19:33:09 -0400 | [diff] [blame] | 1437 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1438 | /* specify the ciphers to be used by default by the SSL_CTX */ |
| 1439 | int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) { |
| 1440 | STACK_OF(SSL_CIPHER) *sk; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1441 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1442 | sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list, |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1443 | &ctx->cipher_list_by_id, str); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1444 | /* ssl_create_cipher_list may return an empty stack if it was unable to find |
| 1445 | * a cipher matching the given rule string (for example if the rule string |
| 1446 | * specifies a cipher which has been disabled). This is not an error as far |
| 1447 | * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and |
| 1448 | * ctx->cipher_list_by_id has been updated. */ |
| 1449 | if (sk == NULL) { |
| 1450 | return 0; |
| 1451 | } else if (sk_SSL_CIPHER_num(sk) == 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1452 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1453 | return 0; |
| 1454 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1455 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1456 | return 1; |
| 1457 | } |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 1458 | |
Adam Langley | cef7583 | 2015-09-03 14:51:12 -0700 | [diff] [blame] | 1459 | int SSL_CTX_set_cipher_list_tls10(SSL_CTX *ctx, const char *str) { |
| 1460 | STACK_OF(SSL_CIPHER) *sk; |
| 1461 | |
| 1462 | sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls10, NULL, str); |
| 1463 | if (sk == NULL) { |
| 1464 | return 0; |
| 1465 | } else if (sk_SSL_CIPHER_num(sk) == 0) { |
| 1466 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH); |
| 1467 | return 0; |
| 1468 | } |
| 1469 | |
| 1470 | return 1; |
| 1471 | } |
| 1472 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1473 | int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) { |
| 1474 | STACK_OF(SSL_CIPHER) *sk; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1475 | |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1476 | sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1477 | if (sk == NULL) { |
| 1478 | return 0; |
| 1479 | } else if (sk_SSL_CIPHER_num(sk) == 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1480 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1481 | return 0; |
| 1482 | } |
David Benjamin | 9f2c0d7 | 2014-10-21 22:00:19 -0400 | [diff] [blame] | 1483 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1484 | return 1; |
| 1485 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1486 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1487 | /* specify the ciphers to be used by the SSL */ |
| 1488 | int SSL_set_cipher_list(SSL *s, const char *str) { |
| 1489 | STACK_OF(SSL_CIPHER) *sk; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1490 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1491 | sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list, |
David Benjamin | 71f0794 | 2015-04-08 02:36:59 -0400 | [diff] [blame] | 1492 | &s->cipher_list_by_id, str); |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 1493 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1494 | /* see comment in SSL_CTX_set_cipher_list */ |
| 1495 | if (sk == NULL) { |
| 1496 | return 0; |
| 1497 | } else if (sk_SSL_CIPHER_num(sk) == 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1498 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1499 | return 0; |
| 1500 | } |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 1501 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1502 | return 1; |
| 1503 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1504 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1505 | int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) { |
| 1506 | size_t i; |
| 1507 | const SSL_CIPHER *c; |
| 1508 | CERT *ct = s->cert; |
| 1509 | uint8_t *q; |
| 1510 | /* Set disabled masks for this session */ |
| 1511 | ssl_set_client_disabled(s); |
Adam Langley | 2970779 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1512 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1513 | if (sk == NULL) { |
| 1514 | return 0; |
| 1515 | } |
| 1516 | q = p; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1517 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1518 | for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { |
| 1519 | c = sk_SSL_CIPHER_value(sk, i); |
| 1520 | /* Skip disabled ciphers */ |
| 1521 | if (c->algorithm_ssl & ct->mask_ssl || |
| 1522 | c->algorithm_mkey & ct->mask_k || |
| 1523 | c->algorithm_auth & ct->mask_a) { |
| 1524 | continue; |
| 1525 | } |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1526 | s2n(ssl_cipher_get_value(c), p); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | /* If all ciphers were disabled, return the error to the caller. */ |
| 1530 | if (p == q) { |
| 1531 | return 0; |
| 1532 | } |
| 1533 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 1534 | /* For SSLv3, the SCSV is added. Otherwise the renegotiation extension is |
| 1535 | * added. */ |
| 1536 | if (s->client_version == SSL3_VERSION && |
| 1537 | !s->s3->initial_handshake_complete) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1538 | s2n(SSL3_CK_SCSV & 0xffff, p); |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 1539 | /* The renegotiation extension is required to be at index zero. */ |
| 1540 | s->s3->tmp.extensions.sent |= (1u << 0); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1541 | } |
| 1542 | |
Adam Langley | 5f0efe0 | 2015-02-20 13:03:16 -0800 | [diff] [blame] | 1543 | if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1544 | s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p); |
| 1545 | } |
| 1546 | |
| 1547 | return p - q; |
| 1548 | } |
| 1549 | |
| 1550 | STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) { |
| 1551 | CBS cipher_suites = *cbs; |
| 1552 | const SSL_CIPHER *c; |
David Benjamin | 60da0cd | 2015-05-03 15:21:28 -0400 | [diff] [blame] | 1553 | STACK_OF(SSL_CIPHER) *sk; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1554 | |
| 1555 | if (s->s3) { |
| 1556 | s->s3->send_connection_binding = 0; |
| 1557 | } |
| 1558 | |
| 1559 | if (CBS_len(&cipher_suites) % 2 != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1560 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1561 | return NULL; |
| 1562 | } |
| 1563 | |
| 1564 | sk = sk_SSL_CIPHER_new_null(); |
| 1565 | if (sk == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1566 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1567 | goto err; |
| 1568 | } |
| 1569 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1570 | while (CBS_len(&cipher_suites) > 0) { |
| 1571 | uint16_t cipher_suite; |
| 1572 | |
| 1573 | if (!CBS_get_u16(&cipher_suites, &cipher_suite)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1574 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1575 | goto err; |
| 1576 | } |
| 1577 | |
| 1578 | /* Check for SCSV. */ |
| 1579 | if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) { |
| 1580 | /* SCSV is fatal if renegotiating. */ |
David Benjamin | 20f6e97 | 2015-05-15 21:51:49 -0400 | [diff] [blame] | 1581 | if (s->s3->initial_handshake_complete) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1582 | OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1583 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1584 | goto err; |
| 1585 | } |
| 1586 | s->s3->send_connection_binding = 1; |
| 1587 | continue; |
| 1588 | } |
| 1589 | |
| 1590 | /* Check for FALLBACK_SCSV. */ |
| 1591 | if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) { |
| 1592 | uint16_t max_version = ssl3_get_max_server_version(s); |
| 1593 | if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version |
| 1594 | : (uint16_t)s->version < max_version) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1595 | OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1596 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK); |
| 1597 | goto err; |
| 1598 | } |
| 1599 | continue; |
| 1600 | } |
| 1601 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 1602 | c = SSL_get_cipher_by_value(cipher_suite); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1603 | if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1604 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1605 | goto err; |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | return sk; |
David Benjamin | 9f2c0d7 | 2014-10-21 22:00:19 -0400 | [diff] [blame] | 1610 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1611 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1612 | sk_SSL_CIPHER_free(sk); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1613 | return NULL; |
| 1614 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1615 | |
| 1616 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1617 | /* return a servername extension value if provided in Client Hello, or NULL. So |
| 1618 | * far, only host_name types are defined (RFC 3546). */ |
| 1619 | const char *SSL_get_servername(const SSL *s, const int type) { |
| 1620 | if (type != TLSEXT_NAMETYPE_host_name) { |
| 1621 | return NULL; |
| 1622 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1623 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1624 | return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname |
| 1625 | : s->tlsext_hostname; |
| 1626 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1627 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1628 | int SSL_get_servername_type(const SSL *s) { |
| 1629 | if (s->session && |
| 1630 | (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) { |
| 1631 | return TLSEXT_NAMETYPE_host_name; |
| 1632 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1633 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1634 | return -1; |
| 1635 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1636 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1637 | void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) { |
| 1638 | ctx->signed_cert_timestamps_enabled = 1; |
| 1639 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 1640 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1641 | int SSL_enable_signed_cert_timestamps(SSL *ssl) { |
| 1642 | ssl->signed_cert_timestamps_enabled = 1; |
| 1643 | return 1; |
| 1644 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 1645 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1646 | void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) { |
| 1647 | ctx->ocsp_stapling_enabled = 1; |
| 1648 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 1649 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1650 | int SSL_enable_ocsp_stapling(SSL *ssl) { |
| 1651 | ssl->ocsp_stapling_enabled = 1; |
| 1652 | return 1; |
| 1653 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 1654 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1655 | void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out, |
| 1656 | size_t *out_len) { |
| 1657 | SSL_SESSION *session = ssl->session; |
Adam Langley | 3cb50e0 | 2014-08-26 14:00:31 -0700 | [diff] [blame] | 1658 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1659 | *out_len = 0; |
| 1660 | *out = NULL; |
| 1661 | if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) { |
| 1662 | return; |
| 1663 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 1664 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1665 | *out = session->tlsext_signed_cert_timestamp_list; |
| 1666 | *out_len = session->tlsext_signed_cert_timestamp_list_length; |
| 1667 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 1668 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1669 | void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out, |
| 1670 | size_t *out_len) { |
| 1671 | SSL_SESSION *session = ssl->session; |
| 1672 | |
| 1673 | *out_len = 0; |
| 1674 | *out = NULL; |
| 1675 | if (ssl->server || !session || !session->ocsp_response) { |
| 1676 | return; |
| 1677 | } |
| 1678 | *out = session->ocsp_response; |
| 1679 | *out_len = session->ocsp_response_length; |
| 1680 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 1681 | |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 1682 | int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list, |
| 1683 | size_t list_len) { |
| 1684 | OPENSSL_free(ctx->signed_cert_timestamp_list); |
| 1685 | ctx->signed_cert_timestamp_list_length = 0; |
| 1686 | |
| 1687 | ctx->signed_cert_timestamp_list = BUF_memdup(list, list_len); |
| 1688 | if (ctx->signed_cert_timestamp_list == NULL) { |
| 1689 | return 0; |
| 1690 | } |
| 1691 | ctx->signed_cert_timestamp_list_length = list_len; |
| 1692 | |
| 1693 | return 1; |
| 1694 | } |
| 1695 | |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1696 | int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response, |
| 1697 | size_t response_len) { |
| 1698 | OPENSSL_free(ctx->ocsp_response); |
| 1699 | ctx->ocsp_response_length = 0; |
| 1700 | |
| 1701 | ctx->ocsp_response = BUF_memdup(response, response_len); |
| 1702 | if (ctx->ocsp_response == NULL) { |
| 1703 | return 0; |
| 1704 | } |
| 1705 | ctx->ocsp_response_length = response_len; |
| 1706 | |
| 1707 | return 1; |
| 1708 | } |
| 1709 | |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 1710 | int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, |
| 1711 | const uint8_t *server, unsigned server_len, |
| 1712 | const uint8_t *client, unsigned client_len) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1713 | unsigned int i, j; |
| 1714 | const uint8_t *result; |
| 1715 | int status = OPENSSL_NPN_UNSUPPORTED; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1716 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1717 | /* For each protocol in server preference order, see if we support it. */ |
| 1718 | for (i = 0; i < server_len;) { |
| 1719 | for (j = 0; j < client_len;) { |
| 1720 | if (server[i] == client[j] && |
| 1721 | memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) { |
| 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 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1733 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1734 | /* There's no overlap between our protocols and the server's list. */ |
| 1735 | result = client; |
| 1736 | status = OPENSSL_NPN_NO_OVERLAP; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1737 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1738 | found: |
| 1739 | *out = (uint8_t *)result + 1; |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 1740 | *out_len = result[0]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1741 | return status; |
| 1742 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1743 | |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 1744 | void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data, |
| 1745 | unsigned *out_len) { |
| 1746 | *out_data = ssl->next_proto_negotiated; |
| 1747 | if (*out_data == NULL) { |
| 1748 | *out_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1749 | } else { |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 1750 | *out_len = ssl->next_proto_negotiated_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1751 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1752 | } |
| 1753 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1754 | void SSL_CTX_set_next_protos_advertised_cb( |
| 1755 | SSL_CTX *ctx, |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 1756 | int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1757 | void *arg) { |
| 1758 | ctx->next_protos_advertised_cb = cb; |
| 1759 | ctx->next_protos_advertised_cb_arg = arg; |
| 1760 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1761 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1762 | void SSL_CTX_set_next_proto_select_cb( |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 1763 | SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len, |
| 1764 | const uint8_t *in, unsigned in_len, void *arg), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1765 | void *arg) { |
| 1766 | ctx->next_proto_select_cb = cb; |
| 1767 | ctx->next_proto_select_cb_arg = arg; |
| 1768 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1769 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1770 | int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos, |
| 1771 | unsigned protos_len) { |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1772 | OPENSSL_free(ctx->alpn_client_proto_list); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1773 | ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len); |
| 1774 | if (!ctx->alpn_client_proto_list) { |
| 1775 | return 1; |
| 1776 | } |
| 1777 | ctx->alpn_client_proto_list_len = protos_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1778 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1779 | return 0; |
| 1780 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1781 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1782 | int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) { |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1783 | OPENSSL_free(ssl->alpn_client_proto_list); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1784 | ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len); |
| 1785 | if (!ssl->alpn_client_proto_list) { |
| 1786 | return 1; |
| 1787 | } |
| 1788 | ssl->alpn_client_proto_list_len = protos_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1789 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1790 | return 0; |
| 1791 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1792 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1793 | void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx, |
| 1794 | int (*cb)(SSL *ssl, const uint8_t **out, |
David Benjamin | 8984f1f | 2015-09-16 00:10:19 -0400 | [diff] [blame] | 1795 | uint8_t *out_len, const uint8_t *in, |
| 1796 | unsigned in_len, void *arg), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1797 | void *arg) { |
| 1798 | ctx->alpn_select_cb = cb; |
| 1799 | ctx->alpn_select_cb_arg = arg; |
| 1800 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1801 | |
David Benjamin | 8984f1f | 2015-09-16 00:10:19 -0400 | [diff] [blame] | 1802 | void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data, |
| 1803 | unsigned *out_len) { |
| 1804 | *out_data = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1805 | if (ssl->s3) { |
David Benjamin | 8984f1f | 2015-09-16 00:10:19 -0400 | [diff] [blame] | 1806 | *out_data = ssl->s3->alpn_selected; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1807 | } |
David Benjamin | 8984f1f | 2015-09-16 00:10:19 -0400 | [diff] [blame] | 1808 | if (*out_data == NULL) { |
| 1809 | *out_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1810 | } else { |
David Benjamin | 8984f1f | 2015-09-16 00:10:19 -0400 | [diff] [blame] | 1811 | *out_len = ssl->s3->alpn_selected_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1812 | } |
| 1813 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1814 | |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 1815 | int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len, |
| 1816 | const char *label, size_t label_len, |
| 1817 | const uint8_t *context, size_t context_len, |
| 1818 | int use_context) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1819 | if (s->version < TLS1_VERSION) { |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 1820 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1821 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1822 | |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 1823 | return s->enc_method->export_keying_material( |
| 1824 | s, out, out_len, label, label_len, context, context_len, use_context); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1825 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1826 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1827 | void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame^] | 1828 | int (*cb)(X509_STORE_CTX *store_ctx, |
| 1829 | void *arg), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1830 | void *arg) { |
| 1831 | ctx->app_verify_callback = cb; |
| 1832 | ctx->app_verify_arg = arg; |
| 1833 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1834 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1835 | void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, |
| 1836 | int (*cb)(int, X509_STORE_CTX *)) { |
| 1837 | ctx->verify_mode = mode; |
| 1838 | ctx->default_verify_callback = cb; |
| 1839 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1840 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1841 | void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) { |
| 1842 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); |
| 1843 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1844 | |
David Benjamin | 7481d39 | 2015-07-05 19:38:46 -0400 | [diff] [blame] | 1845 | void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1846 | void *arg) { |
David Benjamin | 7481d39 | 2015-07-05 19:38:46 -0400 | [diff] [blame] | 1847 | ssl_cert_set_cert_cb(ctx->cert, cb, arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1848 | } |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 1849 | |
David Benjamin | 7481d39 | 2015-07-05 19:38:46 -0400 | [diff] [blame] | 1850 | void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) { |
| 1851 | ssl_cert_set_cert_cb(ssl->cert, cb, arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1852 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1853 | |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 1854 | void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k, |
| 1855 | uint32_t *out_mask_a) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1856 | CERT *c = s->cert; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1857 | int have_rsa_cert = 0, dh_tmp; |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame] | 1858 | uint32_t mask_k, mask_a; |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1859 | int have_ecc_cert = 0, ecdsa_ok; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1860 | X509 *x; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1861 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1862 | dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1863 | |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 1864 | if (s->cert->x509 != NULL && ssl_has_private_key(s)) { |
| 1865 | if (ssl_private_key_type(s) == EVP_PKEY_RSA) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1866 | have_rsa_cert = 1; |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 1867 | } else if (ssl_private_key_type(s) == EVP_PKEY_EC) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1868 | have_ecc_cert = 1; |
| 1869 | } |
| 1870 | } |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 1871 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1872 | mask_k = 0; |
| 1873 | mask_a = 0; |
David Benjamin | f31e681 | 2014-11-13 18:05:55 -0500 | [diff] [blame] | 1874 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1875 | if (dh_tmp) { |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1876 | mask_k |= SSL_kDHE; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1877 | } |
David Benjamin | bb20f52 | 2015-07-04 17:18:14 -0400 | [diff] [blame] | 1878 | if (have_rsa_cert) { |
| 1879 | mask_k |= SSL_kRSA; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1880 | mask_a |= SSL_aRSA; |
| 1881 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1882 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1883 | /* An ECC certificate may be usable for ECDSA cipher suites depending on the |
| 1884 | * key usage extension and on the client's curve preferences. */ |
| 1885 | if (have_ecc_cert) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 1886 | x = c->x509; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1887 | /* This call populates extension flags (ex_flags). */ |
| 1888 | X509_check_purpose(x, -1, 0); |
| 1889 | ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) |
| 1890 | ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) |
| 1891 | : 1; |
| 1892 | if (!tls1_check_ec_cert(s, x)) { |
| 1893 | ecdsa_ok = 0; |
| 1894 | } |
| 1895 | if (ecdsa_ok) { |
| 1896 | mask_a |= SSL_aECDSA; |
| 1897 | } |
| 1898 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1899 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1900 | /* If we are considering an ECC cipher suite that uses an ephemeral EC |
| 1901 | * key, check it. */ |
David Benjamin | dd97878 | 2015-04-24 15:20:13 -0400 | [diff] [blame] | 1902 | if (tls1_check_ec_tmp_key(s)) { |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1903 | mask_k |= SSL_kECDHE; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1904 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1905 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1906 | /* PSK requires a server callback. */ |
| 1907 | if (s->psk_server_callback != NULL) { |
| 1908 | mask_k |= SSL_kPSK; |
| 1909 | mask_a |= SSL_aPSK; |
| 1910 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1911 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1912 | *out_mask_k = mask_k; |
| 1913 | *out_mask_a = mask_a; |
| 1914 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1915 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1916 | void ssl_update_cache(SSL *s, int mode) { |
David Benjamin | b6d0c6d | 2015-03-19 19:07:26 -0400 | [diff] [blame] | 1917 | /* Never cache sessions with empty session IDs. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1918 | if (s->session->session_id_length == 0) { |
| 1919 | return; |
| 1920 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1921 | |
David Benjamin | 95d3182 | 2015-06-15 19:53:32 -0400 | [diff] [blame] | 1922 | int has_new_session = !s->hit; |
| 1923 | if (!s->server && s->tlsext_ticket_expected) { |
| 1924 | /* A client may see new sessions on abbreviated handshakes if the server |
| 1925 | * decides to renew the ticket. Once the handshake is completed, it should |
| 1926 | * be inserted into the cache. */ |
| 1927 | has_new_session = 1; |
| 1928 | } |
| 1929 | |
David Benjamin | b6d0c6d | 2015-03-19 19:07:26 -0400 | [diff] [blame] | 1930 | SSL_CTX *ctx = s->initial_ctx; |
David Benjamin | 95d3182 | 2015-06-15 19:53:32 -0400 | [diff] [blame] | 1931 | if ((ctx->session_cache_mode & mode) == mode && has_new_session && |
David Benjamin | b6d0c6d | 2015-03-19 19:07:26 -0400 | [diff] [blame] | 1932 | ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) || |
| 1933 | SSL_CTX_add_session(ctx, s->session)) && |
| 1934 | ctx->new_session_cb != NULL) { |
| 1935 | /* Note: |new_session_cb| is called whether the internal session cache is |
| 1936 | * used or not. */ |
| 1937 | if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1938 | SSL_SESSION_free(s->session); |
| 1939 | } |
| 1940 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1941 | |
David Benjamin | b6d0c6d | 2015-03-19 19:07:26 -0400 | [diff] [blame] | 1942 | if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) && |
| 1943 | !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) && |
| 1944 | (ctx->session_cache_mode & mode) == mode) { |
| 1945 | /* Automatically flush the internal session cache every 255 connections. */ |
| 1946 | int flush_cache = 0; |
Adam Langley | 4bdb6e4 | 2015-05-15 15:29:21 -0700 | [diff] [blame] | 1947 | CRYPTO_MUTEX_lock_write(&ctx->lock); |
David Benjamin | b6d0c6d | 2015-03-19 19:07:26 -0400 | [diff] [blame] | 1948 | ctx->handshakes_since_cache_flush++; |
| 1949 | if (ctx->handshakes_since_cache_flush >= 255) { |
| 1950 | flush_cache = 1; |
| 1951 | ctx->handshakes_since_cache_flush = 0; |
| 1952 | } |
Adam Langley | 4bdb6e4 | 2015-05-15 15:29:21 -0700 | [diff] [blame] | 1953 | CRYPTO_MUTEX_unlock(&ctx->lock); |
David Benjamin | b6d0c6d | 2015-03-19 19:07:26 -0400 | [diff] [blame] | 1954 | |
| 1955 | if (flush_cache) { |
| 1956 | SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1957 | } |
| 1958 | } |
| 1959 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1960 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1961 | static const char *ssl_get_version(int version) { |
| 1962 | switch (version) { |
| 1963 | case TLS1_2_VERSION: |
| 1964 | return "TLSv1.2"; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1965 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1966 | case TLS1_1_VERSION: |
| 1967 | return "TLSv1.1"; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1968 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1969 | case TLS1_VERSION: |
| 1970 | return "TLSv1"; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1971 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1972 | case SSL3_VERSION: |
| 1973 | return "SSLv3"; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1974 | |
David Benjamin | 1c722b7 | 2015-04-20 13:53:10 -0400 | [diff] [blame] | 1975 | case DTLS1_VERSION: |
| 1976 | return "DTLSv1"; |
| 1977 | |
| 1978 | case DTLS1_2_VERSION: |
| 1979 | return "DTLSv1.2"; |
| 1980 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1981 | default: |
| 1982 | return "unknown"; |
| 1983 | } |
| 1984 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1985 | |
David Benjamin | 42fea37 | 2015-09-19 01:22:44 -0400 | [diff] [blame] | 1986 | const char *SSL_get_version(const SSL *ssl) { |
| 1987 | return ssl_get_version(ssl->version); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1988 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1989 | |
David Benjamin | a6b8cdf | 2015-09-13 14:07:33 -0400 | [diff] [blame] | 1990 | const char *SSL_SESSION_get_version(const SSL_SESSION *session) { |
| 1991 | return ssl_get_version(session->ssl_version); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1992 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1993 | |
Sigbjorn Vik | 2b23d24 | 2015-06-29 15:07:26 +0200 | [diff] [blame] | 1994 | const char* SSL_get_curve_name(uint16_t curve_id) { |
| 1995 | return tls1_ec_curve_id2name(curve_id); |
| 1996 | } |
| 1997 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1998 | void ssl_clear_cipher_ctx(SSL *s) { |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 1999 | SSL_AEAD_CTX_free(s->aead_read_ctx); |
| 2000 | s->aead_read_ctx = NULL; |
| 2001 | SSL_AEAD_CTX_free(s->aead_write_ctx); |
| 2002 | s->aead_write_ctx = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2003 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2004 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2005 | X509 *SSL_get_certificate(const SSL *s) { |
| 2006 | if (s->cert != NULL) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 2007 | return s->cert->x509; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | return NULL; |
| 2011 | } |
| 2012 | |
| 2013 | EVP_PKEY *SSL_get_privatekey(const SSL *s) { |
| 2014 | if (s->cert != NULL) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 2015 | return s->cert->privatekey; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | return NULL; |
| 2019 | } |
| 2020 | |
| 2021 | X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) { |
| 2022 | if (ctx->cert != NULL) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 2023 | return ctx->cert->x509; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | return NULL; |
| 2027 | } |
| 2028 | |
| 2029 | EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) { |
| 2030 | if (ctx->cert != NULL) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 2031 | return ctx->cert->privatekey; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | return NULL; |
| 2035 | } |
| 2036 | |
David Benjamin | 42fea37 | 2015-09-19 01:22:44 -0400 | [diff] [blame] | 2037 | const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) { |
| 2038 | if (ssl->aead_write_ctx == NULL) { |
David Benjamin | a07c0fc | 2015-05-13 13:19:42 -0400 | [diff] [blame] | 2039 | return NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2040 | } |
David Benjamin | 42fea37 | 2015-09-19 01:22:44 -0400 | [diff] [blame] | 2041 | return ssl->aead_write_ctx->cipher; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2042 | } |
| 2043 | |
Matt Braithwaite | 6a1275b | 2015-06-26 12:09:10 -0700 | [diff] [blame] | 2044 | const COMP_METHOD *SSL_get_current_compression(SSL *s) { return NULL; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2045 | |
Matt Braithwaite | 6a1275b | 2015-06-26 12:09:10 -0700 | [diff] [blame] | 2046 | const COMP_METHOD *SSL_get_current_expansion(SSL *s) { return NULL; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2047 | |
| 2048 | int ssl_init_wbio_buffer(SSL *s, int push) { |
| 2049 | BIO *bbio; |
| 2050 | |
| 2051 | if (s->bbio == NULL) { |
| 2052 | bbio = BIO_new(BIO_f_buffer()); |
| 2053 | if (bbio == NULL) { |
| 2054 | return 0; |
| 2055 | } |
| 2056 | s->bbio = bbio; |
| 2057 | } else { |
| 2058 | bbio = s->bbio; |
| 2059 | if (s->bbio == s->wbio) { |
| 2060 | s->wbio = BIO_pop(s->wbio); |
| 2061 | } |
| 2062 | } |
| 2063 | |
| 2064 | BIO_reset(bbio); |
| 2065 | if (!BIO_set_read_buffer_size(bbio, 1)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2066 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2067 | return 0; |
| 2068 | } |
| 2069 | |
| 2070 | if (push) { |
| 2071 | if (s->wbio != bbio) { |
| 2072 | s->wbio = BIO_push(bbio, s->wbio); |
| 2073 | } |
| 2074 | } else { |
| 2075 | if (s->wbio == bbio) { |
| 2076 | s->wbio = BIO_pop(bbio); |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | return 1; |
| 2081 | } |
| 2082 | |
| 2083 | void ssl_free_wbio_buffer(SSL *s) { |
| 2084 | if (s->bbio == NULL) { |
| 2085 | return; |
| 2086 | } |
| 2087 | |
| 2088 | if (s->bbio == s->wbio) { |
| 2089 | /* remove buffering */ |
| 2090 | s->wbio = BIO_pop(s->wbio); |
| 2091 | } |
| 2092 | |
| 2093 | BIO_free(s->bbio); |
| 2094 | s->bbio = NULL; |
| 2095 | } |
| 2096 | |
| 2097 | void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) { |
| 2098 | ctx->quiet_shutdown = mode; |
| 2099 | } |
| 2100 | |
| 2101 | int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) { |
| 2102 | return ctx->quiet_shutdown; |
| 2103 | } |
| 2104 | |
| 2105 | void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; } |
| 2106 | |
| 2107 | int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; } |
| 2108 | |
| 2109 | void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; } |
| 2110 | |
| 2111 | int SSL_get_shutdown(const SSL *s) { return s->shutdown; } |
| 2112 | |
| 2113 | int SSL_version(const SSL *s) { return s->version; } |
| 2114 | |
| 2115 | SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; } |
| 2116 | |
| 2117 | SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) { |
| 2118 | if (ssl->ctx == ctx) { |
| 2119 | return ssl->ctx; |
| 2120 | } |
| 2121 | |
| 2122 | if (ctx == NULL) { |
| 2123 | ctx = ssl->initial_ctx; |
| 2124 | } |
| 2125 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2126 | ssl_cert_free(ssl->cert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2127 | ssl->cert = ssl_cert_dup(ctx->cert); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2128 | |
Adam Langley | 0b5e390 | 2015-05-15 13:08:38 -0700 | [diff] [blame] | 2129 | CRYPTO_refcount_inc(&ctx->references); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2130 | SSL_CTX_free(ssl->ctx); /* decrement reference count */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2131 | ssl->ctx = ctx; |
| 2132 | |
| 2133 | ssl->sid_ctx_length = ctx->sid_ctx_length; |
| 2134 | assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx)); |
| 2135 | memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx)); |
| 2136 | |
| 2137 | return ssl->ctx; |
| 2138 | } |
| 2139 | |
| 2140 | int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) { |
| 2141 | return X509_STORE_set_default_paths(ctx->cert_store); |
| 2142 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2143 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame^] | 2144 | int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file, |
| 2145 | const char *ca_dir) { |
| 2146 | return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2147 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2148 | |
| 2149 | void SSL_set_info_callback(SSL *ssl, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2150 | void (*cb)(const SSL *ssl, int type, int val)) { |
| 2151 | ssl->info_callback = cb; |
| 2152 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2153 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2154 | void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/, |
| 2155 | int /*val*/) { |
| 2156 | return ssl->info_callback; |
| 2157 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2158 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2159 | int SSL_state(const SSL *ssl) { return ssl->state; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2160 | |
David Benjamin | ece089c | 2015-05-15 23:52:42 -0400 | [diff] [blame] | 2161 | void SSL_set_state(SSL *ssl, int state) { } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2162 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame^] | 2163 | void SSL_set_verify_result(SSL *ssl, long result) { |
| 2164 | ssl->verify_result = result; |
| 2165 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2166 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2167 | long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2168 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2169 | int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, |
| 2170 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 2171 | int index; |
| 2172 | if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp, |
| 2173 | new_func, dup_func, free_func)) { |
| 2174 | return -1; |
| 2175 | } |
| 2176 | return index; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2177 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2178 | |
David Benjamin | 1ac08fe | 2015-09-15 01:36:22 -0400 | [diff] [blame] | 2179 | int SSL_set_ex_data(SSL *ssl, int idx, void *arg) { |
| 2180 | return CRYPTO_set_ex_data(&ssl->ex_data, idx, arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2181 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2182 | |
David Benjamin | 1ac08fe | 2015-09-15 01:36:22 -0400 | [diff] [blame] | 2183 | void *SSL_get_ex_data(const SSL *ssl, int idx) { |
| 2184 | return CRYPTO_get_ex_data(&ssl->ex_data, idx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2185 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2186 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2187 | int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, |
| 2188 | CRYPTO_EX_dup *dup_func, |
| 2189 | CRYPTO_EX_free *free_func) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 2190 | int index; |
| 2191 | if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp, |
| 2192 | new_func, dup_func, free_func)) { |
| 2193 | return -1; |
| 2194 | } |
| 2195 | return index; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2196 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2197 | |
David Benjamin | 1ac08fe | 2015-09-15 01:36:22 -0400 | [diff] [blame] | 2198 | int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *arg) { |
| 2199 | return CRYPTO_set_ex_data(&ctx->ex_data, idx, arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2200 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2201 | |
David Benjamin | 1ac08fe | 2015-09-15 01:36:22 -0400 | [diff] [blame] | 2202 | void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) { |
| 2203 | return CRYPTO_get_ex_data(&ctx->ex_data, idx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2204 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2205 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2206 | X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) { |
| 2207 | return ctx->cert_store; |
| 2208 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2209 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2210 | void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2211 | X509_STORE_free(ctx->cert_store); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2212 | ctx->cert_store = store; |
| 2213 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2214 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2215 | int SSL_want(const SSL *s) { return s->rwstate; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2216 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2217 | void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, |
| 2218 | RSA *(*cb)(SSL *ssl, int is_export, |
| 2219 | int keylength)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2220 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2221 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2222 | void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export, |
| 2223 | int keylength)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, |
David Benjamin | 59015c3 | 2015-04-26 13:13:55 -0400 | [diff] [blame] | 2227 | DH *(*callback)(SSL *ssl, int is_export, |
| 2228 | int keylength)) { |
| 2229 | ctx->cert->dh_tmp_cb = callback; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2230 | } |
| 2231 | |
David Benjamin | 59015c3 | 2015-04-26 13:13:55 -0400 | [diff] [blame] | 2232 | void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export, |
| 2233 | int keylength)) { |
| 2234 | ssl->cert->dh_tmp_cb = callback; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx, |
David Benjamin | dd97878 | 2015-04-24 15:20:13 -0400 | [diff] [blame] | 2238 | EC_KEY *(*callback)(SSL *ssl, int is_export, |
| 2239 | int keylength)) { |
David Benjamin | 59015c3 | 2015-04-26 13:13:55 -0400 | [diff] [blame] | 2240 | ctx->cert->ecdh_tmp_cb = callback; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | void SSL_set_tmp_ecdh_callback(SSL *ssl, |
David Benjamin | dd97878 | 2015-04-24 15:20:13 -0400 | [diff] [blame] | 2244 | EC_KEY *(*callback)(SSL *ssl, int is_export, |
| 2245 | int keylength)) { |
David Benjamin | 59015c3 | 2015-04-26 13:13:55 -0400 | [diff] [blame] | 2246 | ssl->cert->ecdh_tmp_cb = callback; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) { |
| 2250 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2251 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2252 | return 0; |
| 2253 | } |
| 2254 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2255 | OPENSSL_free(ctx->psk_identity_hint); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2256 | |
| 2257 | if (identity_hint != NULL) { |
| 2258 | ctx->psk_identity_hint = BUF_strdup(identity_hint); |
| 2259 | if (ctx->psk_identity_hint == NULL) { |
| 2260 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2261 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2262 | } else { |
| 2263 | ctx->psk_identity_hint = NULL; |
| 2264 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2265 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2266 | return 1; |
| 2267 | } |
| 2268 | |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2269 | int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) { |
| 2270 | if (ssl == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2271 | return 0; |
| 2272 | } |
| 2273 | |
| 2274 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2275 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2276 | return 0; |
| 2277 | } |
| 2278 | |
| 2279 | /* Clear currently configured hint, if any. */ |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2280 | OPENSSL_free(ssl->psk_identity_hint); |
| 2281 | ssl->psk_identity_hint = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2282 | |
| 2283 | if (identity_hint != NULL) { |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2284 | ssl->psk_identity_hint = BUF_strdup(identity_hint); |
| 2285 | if (ssl->psk_identity_hint == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2286 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2287 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2288 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2289 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2290 | return 1; |
| 2291 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2292 | |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2293 | const char *SSL_get_psk_identity_hint(const SSL *ssl) { |
| 2294 | if (ssl == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2295 | return NULL; |
| 2296 | } |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2297 | return ssl->psk_identity_hint; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2298 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2299 | |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2300 | const char *SSL_get_psk_identity(const SSL *ssl) { |
| 2301 | if (ssl == NULL || ssl->session == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2302 | return NULL; |
| 2303 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2304 | |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2305 | return ssl->session->psk_identity; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2306 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2307 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2308 | void SSL_set_psk_client_callback( |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2309 | SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity, |
| 2310 | unsigned max_identity_len, uint8_t *psk, |
| 2311 | unsigned max_psk_len)) { |
| 2312 | ssl->psk_client_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2313 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2314 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2315 | void SSL_CTX_set_psk_client_callback( |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2316 | SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity, |
| 2317 | unsigned max_identity_len, uint8_t *psk, |
| 2318 | unsigned max_psk_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2319 | ctx->psk_client_callback = cb; |
| 2320 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2321 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2322 | void SSL_set_psk_server_callback( |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2323 | SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk, |
| 2324 | unsigned max_psk_len)) { |
| 2325 | ssl->psk_server_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2326 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2327 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2328 | void SSL_CTX_set_psk_server_callback( |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2329 | SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity, |
| 2330 | uint8_t *psk, unsigned max_psk_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2331 | ctx->psk_server_callback = cb; |
| 2332 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2333 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2334 | void SSL_CTX_set_msg_callback(SSL_CTX *ctx, |
| 2335 | void (*cb)(int write_p, int version, |
| 2336 | int content_type, const void *buf, |
| 2337 | size_t len, SSL *ssl, void *arg)) { |
David Benjamin | 59015c3 | 2015-04-26 13:13:55 -0400 | [diff] [blame] | 2338 | ctx->msg_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2339 | } |
David Benjamin | 61ecccf | 2015-05-05 09:44:51 -0400 | [diff] [blame] | 2340 | |
| 2341 | void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) { |
| 2342 | ctx->msg_callback_arg = arg; |
| 2343 | } |
| 2344 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2345 | void SSL_set_msg_callback(SSL *ssl, |
| 2346 | void (*cb)(int write_p, int version, int content_type, |
| 2347 | const void *buf, size_t len, SSL *ssl, |
| 2348 | void *arg)) { |
David Benjamin | 59015c3 | 2015-04-26 13:13:55 -0400 | [diff] [blame] | 2349 | ssl->msg_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2350 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2351 | |
David Benjamin | 61ecccf | 2015-05-05 09:44:51 -0400 | [diff] [blame] | 2352 | void SSL_set_msg_callback_arg(SSL *ssl, void *arg) { |
| 2353 | ssl->msg_callback_arg = arg; |
| 2354 | } |
| 2355 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2356 | void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) { |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2357 | BIO_free(ctx->keylog_bio); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2358 | ctx->keylog_bio = keylog_bio; |
| 2359 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2360 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2361 | static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) { |
| 2362 | static const char hextable[] = "0123456789abcdef"; |
| 2363 | uint8_t *out; |
| 2364 | size_t i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2365 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2366 | if (!CBB_add_space(cbb, &out, in_len * 2)) { |
| 2367 | return 0; |
| 2368 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2369 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2370 | for (i = 0; i < in_len; i++) { |
| 2371 | *(out++) = (uint8_t)hextable[in[i] >> 4]; |
| 2372 | *(out++) = (uint8_t)hextable[in[i] & 0xf]; |
| 2373 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2374 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2375 | return 1; |
| 2376 | } |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 2377 | |
| 2378 | int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2379 | const uint8_t *encrypted_premaster, |
| 2380 | size_t encrypted_premaster_len, |
| 2381 | const uint8_t *premaster, |
| 2382 | size_t premaster_len) { |
| 2383 | BIO *bio = ctx->keylog_bio; |
| 2384 | CBB cbb; |
| 2385 | uint8_t *out; |
| 2386 | size_t out_len; |
| 2387 | int ret; |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 2388 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2389 | if (bio == NULL) { |
| 2390 | return 1; |
| 2391 | } |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 2392 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2393 | if (encrypted_premaster_len < 8) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2394 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2395 | return 0; |
| 2396 | } |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 2397 | |
David Benjamin | a865320 | 2015-06-28 01:26:10 -0400 | [diff] [blame] | 2398 | CBB_zero(&cbb); |
| 2399 | if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) || |
| 2400 | !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2401 | /* Only the first 8 bytes of the encrypted premaster secret are |
| 2402 | * logged. */ |
| 2403 | !cbb_add_hex(&cbb, encrypted_premaster, 8) || |
| 2404 | !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) || |
| 2405 | !cbb_add_hex(&cbb, premaster, premaster_len) || |
| 2406 | !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) || |
| 2407 | !CBB_finish(&cbb, &out, &out_len)) { |
| 2408 | CBB_cleanup(&cbb); |
| 2409 | return 0; |
| 2410 | } |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 2411 | |
Adam Langley | 4bdb6e4 | 2015-05-15 15:29:21 -0700 | [diff] [blame] | 2412 | CRYPTO_MUTEX_lock_write(&ctx->lock); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2413 | ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio); |
Adam Langley | 4bdb6e4 | 2015-05-15 15:29:21 -0700 | [diff] [blame] | 2414 | CRYPTO_MUTEX_unlock(&ctx->lock); |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 2415 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2416 | OPENSSL_free(out); |
| 2417 | return ret; |
Adam Langley | 95f2288 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2418 | } |
| 2419 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2420 | int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random, |
| 2421 | size_t client_random_len, const uint8_t *master, |
| 2422 | size_t master_len) { |
| 2423 | BIO *bio = ctx->keylog_bio; |
| 2424 | CBB cbb; |
| 2425 | uint8_t *out; |
| 2426 | size_t out_len; |
| 2427 | int ret; |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2428 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2429 | if (bio == NULL) { |
| 2430 | return 1; |
| 2431 | } |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2432 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2433 | if (client_random_len != 32) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2434 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2435 | return 0; |
| 2436 | } |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2437 | |
David Benjamin | a865320 | 2015-06-28 01:26:10 -0400 | [diff] [blame] | 2438 | CBB_zero(&cbb); |
| 2439 | if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) || |
| 2440 | !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2441 | !cbb_add_hex(&cbb, client_random, 32) || |
| 2442 | !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) || |
| 2443 | !cbb_add_hex(&cbb, master, master_len) || |
| 2444 | !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) || |
| 2445 | !CBB_finish(&cbb, &out, &out_len)) { |
| 2446 | CBB_cleanup(&cbb); |
| 2447 | return 0; |
| 2448 | } |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2449 | |
Adam Langley | 4bdb6e4 | 2015-05-15 15:29:21 -0700 | [diff] [blame] | 2450 | CRYPTO_MUTEX_lock_write(&ctx->lock); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2451 | ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio); |
Adam Langley | 4bdb6e4 | 2015-05-15 15:29:21 -0700 | [diff] [blame] | 2452 | CRYPTO_MUTEX_unlock(&ctx->lock); |
David Benjamin | e99e912 | 2014-12-11 01:46:01 -0500 | [diff] [blame] | 2453 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2454 | OPENSSL_free(out); |
| 2455 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 2458 | int SSL_in_false_start(const SSL *s) { |
| 2459 | return s->s3->tmp.in_false_start; |
| 2460 | } |
| 2461 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2462 | int SSL_cutthrough_complete(const SSL *s) { |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 2463 | return SSL_in_false_start(s); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2464 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2465 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2466 | void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size, |
| 2467 | size_t *ssl_session_size) { |
| 2468 | *ssl_size = sizeof(SSL); |
| 2469 | *ssl_ctx_size = sizeof(SSL_CTX); |
| 2470 | *ssl_session_size = sizeof(SSL_SESSION); |
| 2471 | } |
Feng Lu | 41aa325 | 2014-11-21 22:47:56 -0800 | [diff] [blame] | 2472 | |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 2473 | int ssl3_can_false_start(const SSL *s) { |
David Benjamin | 195dc78 | 2015-02-19 13:27:05 -0500 | [diff] [blame] | 2474 | const SSL_CIPHER *const cipher = SSL_get_current_cipher(s); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2475 | |
Adam Langley | e631d96 | 2015-04-01 13:11:01 -0700 | [diff] [blame] | 2476 | /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */ |
David Benjamin | 195dc78 | 2015-02-19 13:27:05 -0500 | [diff] [blame] | 2477 | return !SSL_IS_DTLS(s) && |
| 2478 | SSL_version(s) >= TLS1_2_VERSION && |
| 2479 | (s->s3->alpn_selected || s->s3->next_proto_neg_seen) && |
| 2480 | cipher != NULL && |
Adam Langley | e631d96 | 2015-04-01 13:11:01 -0700 | [diff] [blame] | 2481 | cipher->algorithm_mkey == SSL_kECDHE && |
David Benjamin | 195dc78 | 2015-02-19 13:27:05 -0500 | [diff] [blame] | 2482 | (cipher->algorithm_enc == SSL_AES128GCM || |
| 2483 | cipher->algorithm_enc == SSL_AES256GCM || |
| 2484 | cipher->algorithm_enc == SSL_CHACHA20POLY1305); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) { |
| 2488 | switch (version) { |
| 2489 | case SSL3_VERSION: |
| 2490 | return &SSLv3_enc_data; |
| 2491 | |
| 2492 | case TLS1_VERSION: |
| 2493 | return &TLSv1_enc_data; |
| 2494 | |
David Benjamin | 9e13e1a | 2015-03-05 01:56:32 -0500 | [diff] [blame] | 2495 | case DTLS1_VERSION: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2496 | case TLS1_1_VERSION: |
| 2497 | return &TLSv1_1_enc_data; |
| 2498 | |
David Benjamin | 9e13e1a | 2015-03-05 01:56:32 -0500 | [diff] [blame] | 2499 | case DTLS1_2_VERSION: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2500 | case TLS1_2_VERSION: |
| 2501 | return &TLSv1_2_enc_data; |
| 2502 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2503 | default: |
| 2504 | return NULL; |
| 2505 | } |
| 2506 | } |
| 2507 | |
| 2508 | uint16_t ssl3_get_max_server_version(const SSL *s) { |
| 2509 | uint16_t max_version; |
| 2510 | |
| 2511 | if (SSL_IS_DTLS(s)) { |
| 2512 | max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION; |
| 2513 | if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) { |
| 2514 | return DTLS1_2_VERSION; |
| 2515 | } |
| 2516 | if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) { |
| 2517 | return DTLS1_VERSION; |
| 2518 | } |
| 2519 | return 0; |
| 2520 | } |
| 2521 | |
| 2522 | max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION; |
| 2523 | if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) { |
| 2524 | return TLS1_2_VERSION; |
| 2525 | } |
| 2526 | if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) { |
| 2527 | return TLS1_1_VERSION; |
| 2528 | } |
| 2529 | if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) { |
| 2530 | return TLS1_VERSION; |
| 2531 | } |
| 2532 | if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) { |
| 2533 | return SSL3_VERSION; |
| 2534 | } |
| 2535 | return 0; |
| 2536 | } |
| 2537 | |
| 2538 | uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) { |
| 2539 | uint16_t version = 0; |
| 2540 | |
| 2541 | if (SSL_IS_DTLS(s)) { |
| 2542 | /* Clamp client_version to max_version. */ |
| 2543 | if (s->max_version != 0 && client_version < s->max_version) { |
| 2544 | client_version = s->max_version; |
| 2545 | } |
| 2546 | |
| 2547 | if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) { |
| 2548 | version = DTLS1_2_VERSION; |
| 2549 | } else if (client_version <= DTLS1_VERSION && |
David Benjamin | b18f024 | 2015-03-10 18:30:08 -0400 | [diff] [blame] | 2550 | !(s->options & SSL_OP_NO_DTLSv1)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2551 | version = DTLS1_VERSION; |
| 2552 | } |
| 2553 | |
| 2554 | /* Check against min_version. */ |
| 2555 | if (version != 0 && s->min_version != 0 && version > s->min_version) { |
| 2556 | return 0; |
| 2557 | } |
| 2558 | return version; |
| 2559 | } else { |
| 2560 | /* Clamp client_version to max_version. */ |
| 2561 | if (s->max_version != 0 && client_version > s->max_version) { |
| 2562 | client_version = s->max_version; |
| 2563 | } |
| 2564 | |
| 2565 | if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) { |
| 2566 | version = TLS1_2_VERSION; |
| 2567 | } else if (client_version >= TLS1_1_VERSION && |
| 2568 | !(s->options & SSL_OP_NO_TLSv1_1)) { |
| 2569 | version = TLS1_1_VERSION; |
| 2570 | } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) { |
| 2571 | version = TLS1_VERSION; |
| 2572 | } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) { |
| 2573 | version = SSL3_VERSION; |
| 2574 | } |
| 2575 | |
| 2576 | /* Check against min_version. */ |
| 2577 | if (version != 0 && s->min_version != 0 && version < s->min_version) { |
| 2578 | return 0; |
| 2579 | } |
| 2580 | return version; |
| 2581 | } |
| 2582 | } |
| 2583 | |
| 2584 | uint16_t ssl3_get_max_client_version(SSL *s) { |
David Benjamin | 123a8fd | 2015-04-26 14:16:41 -0400 | [diff] [blame] | 2585 | uint32_t options = s->options; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2586 | uint16_t version = 0; |
| 2587 | |
| 2588 | /* OpenSSL's API for controlling versions entails blacklisting individual |
| 2589 | * protocols. This has two problems. First, on the client, the protocol can |
| 2590 | * only express a contiguous range of versions. Second, a library consumer |
| 2591 | * trying to set a maximum version cannot disable protocol versions that get |
| 2592 | * added in a future version of the library. |
| 2593 | * |
| 2594 | * To account for both of these, OpenSSL interprets the client-side bitmask |
| 2595 | * as a min/max range by picking the lowest contiguous non-empty range of |
| 2596 | * enabled protocols. Note that this means it is impossible to set a maximum |
| 2597 | * version of TLS 1.2 in a future-proof way. |
| 2598 | * |
| 2599 | * By this scheme, the maximum version is the lowest version V such that V is |
| 2600 | * enabled and V+1 is disabled or unimplemented. */ |
| 2601 | if (SSL_IS_DTLS(s)) { |
| 2602 | if (!(options & SSL_OP_NO_DTLSv1_2)) { |
| 2603 | version = DTLS1_2_VERSION; |
| 2604 | } |
| 2605 | if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) { |
| 2606 | version = DTLS1_VERSION; |
| 2607 | } |
| 2608 | if (s->max_version != 0 && version < s->max_version) { |
| 2609 | version = s->max_version; |
| 2610 | } |
| 2611 | } else { |
| 2612 | if (!(options & SSL_OP_NO_TLSv1_2)) { |
| 2613 | version = TLS1_2_VERSION; |
| 2614 | } |
| 2615 | if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) { |
| 2616 | version = TLS1_1_VERSION; |
| 2617 | } |
| 2618 | if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) { |
| 2619 | version = TLS1_VERSION; |
| 2620 | } |
| 2621 | if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) { |
| 2622 | version = SSL3_VERSION; |
| 2623 | } |
| 2624 | if (s->max_version != 0 && version > s->max_version) { |
| 2625 | version = s->max_version; |
| 2626 | } |
| 2627 | } |
| 2628 | |
| 2629 | return version; |
| 2630 | } |
| 2631 | |
| 2632 | int ssl3_is_version_enabled(SSL *s, uint16_t version) { |
| 2633 | if (SSL_IS_DTLS(s)) { |
| 2634 | if (s->max_version != 0 && version < s->max_version) { |
| 2635 | return 0; |
| 2636 | } |
| 2637 | if (s->min_version != 0 && version > s->min_version) { |
| 2638 | return 0; |
| 2639 | } |
| 2640 | |
| 2641 | switch (version) { |
| 2642 | case DTLS1_VERSION: |
| 2643 | return !(s->options & SSL_OP_NO_DTLSv1); |
| 2644 | |
| 2645 | case DTLS1_2_VERSION: |
| 2646 | return !(s->options & SSL_OP_NO_DTLSv1_2); |
| 2647 | |
| 2648 | default: |
| 2649 | return 0; |
| 2650 | } |
| 2651 | } else { |
| 2652 | if (s->max_version != 0 && version > s->max_version) { |
| 2653 | return 0; |
| 2654 | } |
| 2655 | if (s->min_version != 0 && version < s->min_version) { |
| 2656 | return 0; |
| 2657 | } |
| 2658 | |
| 2659 | switch (version) { |
| 2660 | case SSL3_VERSION: |
| 2661 | return !(s->options & SSL_OP_NO_SSLv3); |
| 2662 | |
| 2663 | case TLS1_VERSION: |
| 2664 | return !(s->options & SSL_OP_NO_TLSv1); |
| 2665 | |
| 2666 | case TLS1_1_VERSION: |
| 2667 | return !(s->options & SSL_OP_NO_TLSv1_1); |
| 2668 | |
| 2669 | case TLS1_2_VERSION: |
| 2670 | return !(s->options & SSL_OP_NO_TLSv1_2); |
| 2671 | |
| 2672 | default: |
| 2673 | return 0; |
| 2674 | } |
| 2675 | } |
| 2676 | } |
| 2677 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 2678 | uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) { |
| 2679 | if (!SSL_IS_DTLS(s)) { |
| 2680 | return wire_version; |
| 2681 | } |
| 2682 | |
| 2683 | uint16_t tls_version = ~wire_version; |
| 2684 | uint16_t version = tls_version + 0x0201; |
| 2685 | /* If either component overflowed, clamp it so comparisons still work. */ |
| 2686 | if ((version >> 8) < (tls_version >> 8)) { |
| 2687 | version = 0xff00 | (version & 0xff); |
| 2688 | } |
| 2689 | if ((version & 0xff) < (tls_version & 0xff)) { |
| 2690 | version = (version & 0xff00) | 0xff; |
| 2691 | } |
| 2692 | /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */ |
| 2693 | if (version == TLS1_VERSION) { |
| 2694 | version = TLS1_1_VERSION; |
| 2695 | } |
| 2696 | return version; |
| 2697 | } |
| 2698 | |
David Benjamin | 42fea37 | 2015-09-19 01:22:44 -0400 | [diff] [blame] | 2699 | int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2700 | |
David Benjamin | 42fea37 | 2015-09-19 01:22:44 -0400 | [diff] [blame] | 2701 | int SSL_is_server(SSL *ssl) { return ssl->server; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2702 | |
Adam Langley | 524e717 | 2015-02-20 16:04:00 -0800 | [diff] [blame] | 2703 | void SSL_CTX_set_dos_protection_cb( |
| 2704 | SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) { |
| 2705 | ctx->dos_protection_cb = cb; |
| 2706 | } |
| 2707 | |
David Benjamin | b16346b | 2015-04-08 19:16:58 -0400 | [diff] [blame] | 2708 | void SSL_set_reject_peer_renegotiations(SSL *s, int reject) { |
David Benjamin | 897e5e0 | 2015-05-12 17:03:54 -0400 | [diff] [blame] | 2709 | s->accept_peer_renegotiations = !reject; |
David Benjamin | b16346b | 2015-04-08 19:16:58 -0400 | [diff] [blame] | 2710 | } |
| 2711 | |
Adam Langley | 3f92d21 | 2015-02-20 15:32:52 -0800 | [diff] [blame] | 2712 | int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key, |
| 2713 | const RC4_KEY **write_key) { |
| 2714 | if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) { |
| 2715 | return 0; |
| 2716 | } |
| 2717 | |
| 2718 | return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) && |
| 2719 | EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key); |
| 2720 | } |
David Benjamin | da881e9 | 2015-04-26 14:45:04 -0400 | [diff] [blame] | 2721 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 2722 | int SSL_clear(SSL *ssl) { |
| 2723 | if (ssl->method == NULL) { |
| 2724 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED); |
| 2725 | return 0; |
| 2726 | } |
| 2727 | |
| 2728 | if (ssl_clear_bad_session(ssl)) { |
| 2729 | SSL_SESSION_free(ssl->session); |
| 2730 | ssl->session = NULL; |
| 2731 | } |
| 2732 | |
| 2733 | ssl->hit = 0; |
| 2734 | ssl->shutdown = 0; |
| 2735 | |
| 2736 | /* SSL_clear may be called before or after the |ssl| is initialized in either |
| 2737 | * accept or connect state. In the latter case, SSL_clear should preserve the |
| 2738 | * half and reset |ssl->state| accordingly. */ |
| 2739 | if (ssl->handshake_func != NULL) { |
| 2740 | if (ssl->server) { |
| 2741 | SSL_set_accept_state(ssl); |
| 2742 | } else { |
| 2743 | SSL_set_connect_state(ssl); |
| 2744 | } |
| 2745 | } else { |
| 2746 | assert(ssl->state == 0); |
| 2747 | } |
| 2748 | |
| 2749 | /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and |
| 2750 | * |SSL_clear| because it is per-connection state rather than configuration |
| 2751 | * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is |
| 2752 | * naturally reset at the right points between |SSL_new|, |SSL_clear|, and |
| 2753 | * |ssl3_new|. */ |
| 2754 | |
| 2755 | ssl->rwstate = SSL_NOTHING; |
| 2756 | |
| 2757 | BUF_MEM_free(ssl->init_buf); |
| 2758 | ssl->init_buf = NULL; |
| 2759 | |
| 2760 | ssl_clear_cipher_ctx(ssl); |
| 2761 | |
| 2762 | OPENSSL_free(ssl->next_proto_negotiated); |
| 2763 | ssl->next_proto_negotiated = NULL; |
| 2764 | ssl->next_proto_negotiated_len = 0; |
| 2765 | |
| 2766 | /* The ssl->d1->mtu is simultaneously configuration (preserved across |
| 2767 | * clear) and connection-specific state (gets reset). |
| 2768 | * |
| 2769 | * TODO(davidben): Avoid this. */ |
| 2770 | unsigned mtu = 0; |
| 2771 | if (ssl->d1 != NULL) { |
| 2772 | mtu = ssl->d1->mtu; |
| 2773 | } |
| 2774 | |
| 2775 | ssl->method->ssl_free(ssl); |
| 2776 | if (!ssl->method->ssl_new(ssl)) { |
| 2777 | return 0; |
| 2778 | } |
| 2779 | ssl->enc_method = ssl3_get_enc_method(ssl->version); |
| 2780 | assert(ssl->enc_method != NULL); |
| 2781 | |
| 2782 | if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
| 2783 | ssl->d1->mtu = mtu; |
| 2784 | } |
| 2785 | |
| 2786 | ssl->client_version = ssl->version; |
| 2787 | |
| 2788 | return 1; |
| 2789 | } |
| 2790 | |
David Benjamin | da881e9 | 2015-04-26 14:45:04 -0400 | [diff] [blame] | 2791 | int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; } |
| 2792 | int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; } |
| 2793 | int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; } |
| 2794 | int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; } |
| 2795 | int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; } |
| 2796 | int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; } |
| 2797 | int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; } |
| 2798 | int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; } |
| 2799 | int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; } |
| 2800 | int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; } |
| 2801 | int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; } |
Matt Braithwaite | 4838d8a | 2015-08-20 13:20:03 -0700 | [diff] [blame] | 2802 | void ERR_load_SSL_strings(void) {} |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 2803 | void SSL_load_error_strings(void) {} |