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