Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 112 | * ECC cipher suite support in OpenSSL originally developed by |
| 113 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
| 114 | */ |
| 115 | /* ==================================================================== |
| 116 | * Copyright 2005 Nokia. All rights reserved. |
| 117 | * |
| 118 | * The portions of the attached software ("Contribution") is developed by |
| 119 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 120 | * license. |
| 121 | * |
| 122 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 123 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 124 | * support (see RFC 4279) to OpenSSL. |
| 125 | * |
| 126 | * No patent licenses or other rights except those expressly stated in |
| 127 | * the OpenSSL open source license shall be deemed granted or received |
| 128 | * expressly, by implication, estoppel, or otherwise. |
| 129 | * |
| 130 | * No assurances are provided by Nokia that the Contribution does not |
| 131 | * infringe the patent or other intellectual property rights of any third |
| 132 | * party or that the license provides you with all the necessary rights |
| 133 | * to make use of the Contribution. |
| 134 | * |
| 135 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 136 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 137 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 138 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 139 | * OTHERWISE. */ |
| 140 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 141 | #include <openssl/ssl.h> |
| 142 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 143 | #include <assert.h> |
David Benjamin | 93d9743 | 2016-08-08 21:22:47 -0400 | [diff] [blame] | 144 | #include <stdlib.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 145 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 146 | |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 147 | #include <openssl/bytestring.h> |
David Benjamin | 7c6d09b | 2015-09-05 11:00:19 -0400 | [diff] [blame] | 148 | #include <openssl/crypto.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 149 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 150 | #include <openssl/lhash.h> |
| 151 | #include <openssl/mem.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 152 | #include <openssl/rand.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 153 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 154 | #include "internal.h" |
David Benjamin | 546f1a5 | 2015-04-15 16:46:09 -0400 | [diff] [blame] | 155 | #include "../crypto/internal.h" |
| 156 | |
David Benjamin | 721e8b7 | 2016-08-03 13:13:17 -0400 | [diff] [blame] | 157 | #if defined(OPENSSL_WINDOWS) |
| 158 | #include <sys/timeb.h> |
| 159 | #else |
| 160 | #include <sys/socket.h> |
| 161 | #include <sys/time.h> |
| 162 | #endif |
| 163 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 164 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 165 | namespace bssl { |
| 166 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 167 | // |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it |
| 168 | // to avoid downstream churn. |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 169 | OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL) |
David Benjamin | 97760d5 | 2015-07-24 23:02:49 -0400 | [diff] [blame] | 170 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 171 | // The following errors are no longer emitted, but are used in nginx without |
| 172 | // #ifdefs. |
David Benjamin | 311c257 | 2016-08-17 15:54:36 -0400 | [diff] [blame] | 173 | OPENSSL_DECLARE_ERROR_REASON(SSL, BLOCK_CIPHER_PAD_IS_WRONG) |
| 174 | OPENSSL_DECLARE_ERROR_REASON(SSL, NO_CIPHERS_SPECIFIED) |
| 175 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 176 | // Some error codes are special. Ensure the make_errors.go script never |
| 177 | // regresses this. |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 178 | static_assert(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION == |
| 179 | SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET, |
| 180 | "alert reason code mismatch"); |
David Benjamin | e113608 | 2014-09-20 12:28:58 -0400 | [diff] [blame] | 181 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 182 | // kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 183 | static const size_t kMaxHandshakeSize = (1u << 24) - 1; |
| 184 | |
David Benjamin | aa58513 | 2015-06-29 23:36:17 -0400 | [diff] [blame] | 185 | static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl = |
| 186 | CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA; |
| 187 | static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx = |
| 188 | CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA; |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 189 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 190 | void ssl_reset_error_state(SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 191 | // Functions which use |SSL_get_error| must reset I/O and error state on |
| 192 | // entry. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 193 | ssl->rwstate = SSL_NOTHING; |
| 194 | ERR_clear_error(); |
| 195 | ERR_clear_system_error(); |
| 196 | } |
| 197 | |
| 198 | int ssl_can_write(const SSL *ssl) { |
| 199 | return !SSL_in_init(ssl) || ssl->s3->hs->can_early_write; |
| 200 | } |
| 201 | |
| 202 | int ssl_can_read(const SSL *ssl) { |
| 203 | return !SSL_in_init(ssl) || ssl->s3->hs->can_early_read; |
| 204 | } |
| 205 | |
| 206 | void ssl_cipher_preference_list_free( |
| 207 | struct ssl_cipher_preference_list_st *cipher_list) { |
| 208 | if (cipher_list == NULL) { |
| 209 | return; |
| 210 | } |
| 211 | sk_SSL_CIPHER_free(cipher_list->ciphers); |
| 212 | OPENSSL_free(cipher_list->in_group_flags); |
| 213 | OPENSSL_free(cipher_list); |
| 214 | } |
| 215 | |
| 216 | void ssl_update_cache(SSL_HANDSHAKE *hs, int mode) { |
| 217 | SSL *const ssl = hs->ssl; |
| 218 | SSL_CTX *ctx = ssl->session_ctx; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 219 | // Never cache sessions with empty session IDs. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 220 | if (ssl->s3->established_session->session_id_length == 0 || |
David Benjamin | 5ef40c6 | 2017-08-23 21:28:29 -0700 | [diff] [blame] | 221 | ssl->s3->established_session->not_resumable || |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 222 | (ctx->session_cache_mode & mode) != mode) { |
| 223 | return; |
| 224 | } |
| 225 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 226 | // Clients never use the internal session cache. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 227 | int use_internal_cache = ssl->server && !(ctx->session_cache_mode & |
| 228 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 229 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 230 | // A client may see new sessions on abbreviated handshakes if the server |
| 231 | // decides to renew the ticket. Once the handshake is completed, it should be |
| 232 | // inserted into the cache. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 233 | if (ssl->s3->established_session != ssl->session || |
| 234 | (!ssl->server && hs->ticket_expected)) { |
| 235 | if (use_internal_cache) { |
| 236 | SSL_CTX_add_session(ctx, ssl->s3->established_session); |
| 237 | } |
| 238 | if (ctx->new_session_cb != NULL) { |
| 239 | SSL_SESSION_up_ref(ssl->s3->established_session); |
| 240 | if (!ctx->new_session_cb(ssl, ssl->s3->established_session)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 241 | // |new_session_cb|'s return value signals whether it took ownership. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 242 | SSL_SESSION_free(ssl->s3->established_session); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if (use_internal_cache && |
| 248 | !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 249 | // Automatically flush the internal session cache every 255 connections. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 250 | int flush_cache = 0; |
| 251 | CRYPTO_MUTEX_lock_write(&ctx->lock); |
| 252 | ctx->handshakes_since_cache_flush++; |
| 253 | if (ctx->handshakes_since_cache_flush >= 255) { |
| 254 | flush_cache = 1; |
| 255 | ctx->handshakes_since_cache_flush = 0; |
| 256 | } |
| 257 | CRYPTO_MUTEX_unlock_write(&ctx->lock); |
| 258 | |
| 259 | if (flush_cache) { |
| 260 | struct OPENSSL_timeval now; |
| 261 | ssl_get_current_time(ssl, &now); |
| 262 | SSL_CTX_flush_sessions(ctx, now.tv_sec); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) { |
| 268 | static const char hextable[] = "0123456789abcdef"; |
| 269 | uint8_t *out; |
| 270 | |
| 271 | if (!CBB_add_space(cbb, &out, in_len * 2)) { |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | for (size_t i = 0; i < in_len; i++) { |
| 276 | *(out++) = (uint8_t)hextable[in[i] >> 4]; |
| 277 | *(out++) = (uint8_t)hextable[in[i] & 0xf]; |
| 278 | } |
| 279 | |
| 280 | return 1; |
| 281 | } |
| 282 | |
| 283 | int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret, |
| 284 | size_t secret_len) { |
| 285 | if (ssl->ctx->keylog_callback == NULL) { |
| 286 | return 1; |
| 287 | } |
| 288 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 289 | ScopedCBB cbb; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 290 | uint8_t *out; |
| 291 | size_t out_len; |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 292 | if (!CBB_init(cbb.get(), strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 + |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 293 | secret_len * 2 + 1) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 294 | !CBB_add_bytes(cbb.get(), (const uint8_t *)label, strlen(label)) || |
| 295 | !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) || |
| 296 | !cbb_add_hex(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
| 297 | !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) || |
| 298 | !cbb_add_hex(cbb.get(), secret, secret_len) || |
| 299 | !CBB_add_u8(cbb.get(), 0 /* NUL */) || |
| 300 | !CBB_finish(cbb.get(), &out, &out_len)) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | ssl->ctx->keylog_callback(ssl, (const char *)out); |
| 305 | OPENSSL_free(out); |
| 306 | return 1; |
| 307 | } |
| 308 | |
| 309 | int ssl3_can_false_start(const SSL *ssl) { |
| 310 | const SSL_CIPHER *const cipher = SSL_get_current_cipher(ssl); |
| 311 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 312 | // False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 313 | return !SSL_is_dtls(ssl) && |
| 314 | SSL_version(ssl) == TLS1_2_VERSION && |
| 315 | (ssl->s3->alpn_selected != NULL || |
| 316 | ssl->s3->next_proto_negotiated != NULL) && |
| 317 | cipher != NULL && |
| 318 | cipher->algorithm_mkey == SSL_kECDHE && |
| 319 | cipher->algorithm_mac == SSL_AEAD; |
| 320 | } |
| 321 | |
| 322 | void ssl_do_info_callback(const SSL *ssl, int type, int value) { |
| 323 | void (*cb)(const SSL *ssl, int type, int value) = NULL; |
| 324 | if (ssl->info_callback != NULL) { |
| 325 | cb = ssl->info_callback; |
| 326 | } else if (ssl->ctx->info_callback != NULL) { |
| 327 | cb = ssl->ctx->info_callback; |
| 328 | } |
| 329 | |
| 330 | if (cb != NULL) { |
| 331 | cb(ssl, type, value); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type, |
| 336 | const void *buf, size_t len) { |
| 337 | if (ssl->msg_callback == NULL) { |
| 338 | return; |
| 339 | } |
| 340 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 341 | // |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for |
| 342 | // a V2ClientHello. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 343 | int version; |
| 344 | switch (content_type) { |
| 345 | case 0: |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 346 | // V2ClientHello |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 347 | version = SSL2_VERSION; |
| 348 | break; |
| 349 | case SSL3_RT_HEADER: |
| 350 | version = 0; |
| 351 | break; |
| 352 | default: |
| 353 | version = SSL_version(ssl); |
| 354 | } |
| 355 | |
| 356 | ssl->msg_callback(is_write, version, content_type, buf, len, ssl, |
| 357 | ssl->msg_callback_arg); |
| 358 | } |
| 359 | |
| 360 | void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 361 | // TODO(martinkr): Change callers to |ssl_ctx_get_current_time| and drop the |
| 362 | // |ssl| arg from |current_time_cb| if possible. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 363 | ssl_ctx_get_current_time(ssl->ctx, out_clock); |
| 364 | } |
| 365 | |
| 366 | void ssl_ctx_get_current_time(const SSL_CTX *ctx, |
| 367 | struct OPENSSL_timeval *out_clock) { |
| 368 | if (ctx->current_time_cb != NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 369 | // TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See |
| 370 | // https://crbug.com/boringssl/155. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 371 | struct timeval clock; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 372 | ctx->current_time_cb(nullptr /* ssl */, &clock); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 373 | if (clock.tv_sec < 0) { |
| 374 | assert(0); |
| 375 | out_clock->tv_sec = 0; |
| 376 | out_clock->tv_usec = 0; |
| 377 | } else { |
| 378 | out_clock->tv_sec = (uint64_t)clock.tv_sec; |
| 379 | out_clock->tv_usec = (uint32_t)clock.tv_usec; |
| 380 | } |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | #if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE) |
| 385 | out_clock->tv_sec = 1234; |
| 386 | out_clock->tv_usec = 1234; |
| 387 | #elif defined(OPENSSL_WINDOWS) |
| 388 | struct _timeb time; |
| 389 | _ftime(&time); |
| 390 | if (time.time < 0) { |
| 391 | assert(0); |
| 392 | out_clock->tv_sec = 0; |
| 393 | out_clock->tv_usec = 0; |
| 394 | } else { |
| 395 | out_clock->tv_sec = time.time; |
| 396 | out_clock->tv_usec = time.millitm * 1000; |
| 397 | } |
| 398 | #else |
| 399 | struct timeval clock; |
| 400 | gettimeofday(&clock, NULL); |
| 401 | if (clock.tv_sec < 0) { |
| 402 | assert(0); |
| 403 | out_clock->tv_sec = 0; |
| 404 | out_clock->tv_usec = 0; |
| 405 | } else { |
| 406 | out_clock->tv_sec = (uint64_t)clock.tv_sec; |
| 407 | out_clock->tv_usec = (uint32_t)clock.tv_usec; |
| 408 | } |
| 409 | #endif |
| 410 | } |
| 411 | |
| 412 | } // namespace bssl |
| 413 | |
| 414 | using namespace bssl; |
| 415 | |
David Benjamin | 7c6d09b | 2015-09-05 11:00:19 -0400 | [diff] [blame] | 416 | int SSL_library_init(void) { |
| 417 | CRYPTO_library_init(); |
| 418 | return 1; |
| 419 | } |
| 420 | |
Adam Langley | e976e43 | 2016-02-15 14:01:10 -0800 | [diff] [blame] | 421 | static uint32_t ssl_session_hash(const SSL_SESSION *sess) { |
| 422 | const uint8_t *session_id = sess->session_id; |
| 423 | |
| 424 | uint8_t tmp_storage[sizeof(uint32_t)]; |
| 425 | if (sess->session_id_length < sizeof(tmp_storage)) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 426 | OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage)); |
| 427 | OPENSSL_memcpy(tmp_storage, sess->session_id, sess->session_id_length); |
Adam Langley | e976e43 | 2016-02-15 14:01:10 -0800 | [diff] [blame] | 428 | session_id = tmp_storage; |
| 429 | } |
| 430 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 431 | uint32_t hash = |
Adam Langley | e976e43 | 2016-02-15 14:01:10 -0800 | [diff] [blame] | 432 | ((uint32_t)session_id[0]) | |
| 433 | ((uint32_t)session_id[1] << 8) | |
| 434 | ((uint32_t)session_id[2] << 16) | |
| 435 | ((uint32_t)session_id[3] << 24); |
David Benjamin | 7c6d09b | 2015-09-05 11:00:19 -0400 | [diff] [blame] | 436 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 437 | return hash; |
| 438 | } |
| 439 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 440 | // NB: If this function (or indeed the hash function which uses a sort of |
| 441 | // coarser function than this one) is changed, ensure |
| 442 | // SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being |
| 443 | // able to construct an SSL_SESSION that will collide with any existing session |
| 444 | // with a matching session ID. |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 445 | static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) { |
| 446 | if (a->ssl_version != b->ssl_version) { |
| 447 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 448 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 449 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 450 | if (a->session_id_length != b->session_id_length) { |
| 451 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 452 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 453 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 454 | return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 455 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 456 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 457 | SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) { |
| 458 | SSL_CTX *ret = NULL; |
| 459 | |
| 460 | if (method == NULL) { |
| 461 | OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED); |
| 462 | return NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 463 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 464 | |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 465 | ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX)); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 466 | if (ret == NULL) { |
| 467 | goto err; |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame] | 468 | } |
| 469 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 470 | OPENSSL_memset(ret, 0, sizeof(SSL_CTX)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 471 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 472 | ret->method = method->method; |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 473 | ret->x509_method = method->x509_method; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 474 | |
| 475 | CRYPTO_MUTEX_init(&ret->lock); |
| 476 | |
| 477 | ret->session_cache_mode = SSL_SESS_CACHE_SERVER; |
| 478 | ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; |
| 479 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 480 | ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 481 | ret->session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 482 | |
| 483 | ret->references = 1; |
| 484 | |
| 485 | ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT; |
| 486 | ret->verify_mode = SSL_VERIFY_NONE; |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 487 | ret->cert = ssl_cert_new(method->x509_method); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 488 | if (ret->cert == NULL) { |
| 489 | goto err; |
| 490 | } |
| 491 | |
| 492 | ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp); |
| 493 | if (ret->sessions == NULL) { |
| 494 | goto err; |
| 495 | } |
Adam Langley | 2a3b343 | 2017-02-10 13:56:48 -0800 | [diff] [blame] | 496 | |
| 497 | if (!ret->x509_method->ssl_ctx_new(ret)) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 498 | goto err; |
| 499 | } |
| 500 | |
Matthew Braithwaite | 6ad20dc | 2017-02-21 16:41:33 -0800 | [diff] [blame] | 501 | if (!SSL_CTX_set_strict_cipher_list(ret, SSL_DEFAULT_CIPHER_LIST)) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 502 | goto err2; |
| 503 | } |
| 504 | |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 505 | ret->client_CA = sk_CRYPTO_BUFFER_new_null(); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 506 | if (ret->client_CA == NULL) { |
| 507 | goto err; |
| 508 | } |
| 509 | |
David Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 510 | CRYPTO_new_ex_data(&ret->ex_data); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 511 | |
| 512 | ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
| 513 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 514 | // Disable the auto-chaining feature by default. Once this has stuck without |
| 515 | // problems, the feature will be removed entirely. |
David Benjamin | a983b4c | 2016-11-09 14:21:12 -0500 | [diff] [blame] | 516 | ret->mode = SSL_MODE_NO_AUTO_CHAIN; |
| 517 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 518 | // Lock the SSL_CTX to the specified version, for compatibility with legacy |
| 519 | // uses of SSL_METHOD, but we do not set the minimum version for |
| 520 | // |SSLv3_method|. |
David Benjamin | e34bcc9 | 2016-09-21 16:53:09 -0400 | [diff] [blame] | 521 | if (!SSL_CTX_set_max_proto_version(ret, method->version) || |
David Benjamin | 3cfeb95 | 2017-03-01 16:48:38 -0500 | [diff] [blame] | 522 | !SSL_CTX_set_min_proto_version(ret, method->version == SSL3_VERSION |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 523 | ? 0 // default |
David Benjamin | 3cfeb95 | 2017-03-01 16:48:38 -0500 | [diff] [blame] | 524 | : method->version)) { |
David Benjamin | e34bcc9 | 2016-09-21 16:53:09 -0400 | [diff] [blame] | 525 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 526 | goto err2; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | return ret; |
| 530 | |
| 531 | err: |
| 532 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 533 | err2: |
| 534 | SSL_CTX_free(ret); |
| 535 | return NULL; |
| 536 | } |
| 537 | |
Adam Langley | 84cd159 | 2016-06-30 18:29:44 -0700 | [diff] [blame] | 538 | int SSL_CTX_up_ref(SSL_CTX *ctx) { |
| 539 | CRYPTO_refcount_inc(&ctx->references); |
| 540 | return 1; |
| 541 | } |
| 542 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 543 | void SSL_CTX_free(SSL_CTX *ctx) { |
| 544 | if (ctx == NULL || |
| 545 | !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) { |
| 546 | return; |
| 547 | } |
| 548 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 549 | // Free internal session cache. However: the remove_cb() may reference the |
| 550 | // ex_data of SSL_CTX, thus the ex_data store can only be removed after the |
| 551 | // sessions were flushed. As the ex_data handling routines might also touch |
| 552 | // the session cache, the most secure solution seems to be: empty (flush) the |
| 553 | // cache, then free ex_data, then finally free the cache. (See ticket |
| 554 | // [openssl.org #212].) |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 555 | SSL_CTX_flush_sessions(ctx, 0); |
| 556 | |
| 557 | CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data); |
| 558 | |
| 559 | CRYPTO_MUTEX_cleanup(&ctx->lock); |
| 560 | lh_SSL_SESSION_free(ctx->sessions); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 561 | ssl_cipher_preference_list_free(ctx->cipher_list); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 562 | ssl_cert_free(ctx->cert); |
| 563 | sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions, |
| 564 | SSL_CUSTOM_EXTENSION_free); |
| 565 | sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions, |
| 566 | SSL_CUSTOM_EXTENSION_free); |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 567 | sk_CRYPTO_BUFFER_pop_free(ctx->client_CA, CRYPTO_BUFFER_free); |
Adam Langley | 2a3b343 | 2017-02-10 13:56:48 -0800 | [diff] [blame] | 568 | ctx->x509_method->ssl_ctx_free(ctx); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 569 | sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles); |
| 570 | OPENSSL_free(ctx->psk_identity_hint); |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 571 | OPENSSL_free(ctx->supported_group_list); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 572 | OPENSSL_free(ctx->alpn_client_proto_list); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 573 | EVP_PKEY_free(ctx->tlsext_channel_id_private); |
David Benjamin | 71c21b4 | 2017-04-14 17:05:40 -0400 | [diff] [blame] | 574 | OPENSSL_free(ctx->verify_sigalgs); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 575 | OPENSSL_free(ctx->tlsext_ticket_key_current); |
| 576 | OPENSSL_free(ctx->tlsext_ticket_key_prev); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 577 | |
| 578 | OPENSSL_free(ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 579 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 580 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 581 | SSL *SSL_new(SSL_CTX *ctx) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 582 | if (ctx == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 583 | OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 584 | return NULL; |
| 585 | } |
| 586 | if (ctx->method == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 587 | 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] | 588 | return NULL; |
| 589 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 590 | |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 591 | SSL *ssl = (SSL *)OPENSSL_malloc(sizeof(SSL)); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 592 | if (ssl == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 593 | goto err; |
| 594 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 595 | OPENSSL_memset(ssl, 0, sizeof(SSL)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 596 | |
David Benjamin | fc08dfc | 2017-06-20 14:39:32 -0400 | [diff] [blame] | 597 | ssl->conf_min_version = ctx->conf_min_version; |
| 598 | ssl->conf_max_version = ctx->conf_max_version; |
Steven Valdez | 52586f9 | 2017-07-11 15:08:32 -0400 | [diff] [blame] | 599 | ssl->tls13_variant = ctx->tls13_variant; |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 600 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 601 | // RFC 6347 states that implementations SHOULD use an initial timer value of |
| 602 | // 1 second. |
Taylor Brandstetter | 376a0fe | 2016-05-10 19:30:28 -0700 | [diff] [blame] | 603 | ssl->initial_timeout_duration_ms = 1000; |
| 604 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 605 | ssl->options = ctx->options; |
| 606 | ssl->mode = ctx->mode; |
| 607 | ssl->max_cert_list = ctx->max_cert_list; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 608 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 609 | ssl->cert = ssl_cert_dup(ctx->cert); |
| 610 | if (ssl->cert == NULL) { |
David Benjamin | a5a3eeb | 2015-03-18 20:26:30 -0400 | [diff] [blame] | 611 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 612 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 613 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 614 | ssl->msg_callback = ctx->msg_callback; |
| 615 | ssl->msg_callback_arg = ctx->msg_callback_arg; |
| 616 | ssl->verify_mode = ctx->verify_mode; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 617 | ssl->verify_callback = ctx->default_verify_callback; |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 618 | ssl->custom_verify_callback = ctx->custom_verify_callback; |
David Benjamin | bbaf367 | 2016-11-17 10:53:09 +0900 | [diff] [blame] | 619 | ssl->retain_only_sha256_of_client_certs = |
| 620 | ctx->retain_only_sha256_of_client_certs; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 621 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 622 | ssl->quiet_shutdown = ctx->quiet_shutdown; |
| 623 | ssl->max_send_fragment = ctx->max_send_fragment; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 624 | |
David Benjamin | f650c71 | 2016-12-22 15:54:51 -0500 | [diff] [blame] | 625 | SSL_CTX_up_ref(ctx); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 626 | ssl->ctx = ctx; |
David Benjamin | f650c71 | 2016-12-22 15:54:51 -0500 | [diff] [blame] | 627 | SSL_CTX_up_ref(ctx); |
David Benjamin | be49706 | 2017-03-10 16:08:36 -0500 | [diff] [blame] | 628 | ssl->session_ctx = ctx; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 629 | |
Adam Langley | 2a3b343 | 2017-02-10 13:56:48 -0800 | [diff] [blame] | 630 | if (!ssl->ctx->x509_method->ssl_new(ssl)) { |
| 631 | goto err; |
| 632 | } |
| 633 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 634 | if (ctx->supported_group_list) { |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 635 | ssl->supported_group_list = (uint16_t *)BUF_memdup( |
| 636 | ctx->supported_group_list, ctx->supported_group_list_len * 2); |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 637 | if (!ssl->supported_group_list) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 638 | goto err; |
| 639 | } |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 640 | ssl->supported_group_list_len = ctx->supported_group_list_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 641 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 642 | |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 643 | if (ctx->alpn_client_proto_list) { |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 644 | ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup( |
| 645 | ctx->alpn_client_proto_list, ctx->alpn_client_proto_list_len); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 646 | if (ssl->alpn_client_proto_list == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 647 | goto err; |
| 648 | } |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 649 | ssl->alpn_client_proto_list_len = ctx->alpn_client_proto_list_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 650 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 651 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 652 | ssl->method = ctx->method; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 653 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 654 | if (!ssl->method->ssl_new(ssl)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 655 | goto err; |
| 656 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 657 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 658 | ssl->rwstate = SSL_NOTHING; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 659 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 660 | CRYPTO_new_ex_data(&ssl->ex_data); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 661 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 662 | ssl->psk_identity_hint = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 663 | if (ctx->psk_identity_hint) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 664 | ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint); |
| 665 | if (ssl->psk_identity_hint == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 666 | goto err; |
| 667 | } |
| 668 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 669 | ssl->psk_client_callback = ctx->psk_client_callback; |
| 670 | ssl->psk_server_callback = ctx->psk_server_callback; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 671 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 672 | ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled; |
David Benjamin | 02ddbfd | 2015-01-11 13:09:11 -0500 | [diff] [blame] | 673 | if (ctx->tlsext_channel_id_private) { |
Adam Langley | 310d3f6 | 2016-07-12 10:39:20 -0700 | [diff] [blame] | 674 | EVP_PKEY_up_ref(ctx->tlsext_channel_id_private); |
| 675 | ssl->tlsext_channel_id_private = ctx->tlsext_channel_id_private; |
David Benjamin | 02ddbfd | 2015-01-11 13:09:11 -0500 | [diff] [blame] | 676 | } |
| 677 | |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 678 | ssl->signed_cert_timestamps_enabled = ctx->signed_cert_timestamps_enabled; |
| 679 | ssl->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled; |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 680 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 681 | return ssl; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 682 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 683 | err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 684 | SSL_free(ssl); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 685 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 686 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 687 | return NULL; |
| 688 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 689 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 690 | void SSL_free(SSL *ssl) { |
| 691 | if (ssl == NULL) { |
| 692 | return; |
| 693 | } |
| 694 | |
David Benjamin | 0391f16 | 2017-06-15 15:27:59 -0400 | [diff] [blame] | 695 | if (ssl->ctx != NULL) { |
| 696 | ssl->ctx->x509_method->ssl_free(ssl); |
| 697 | } |
| 698 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 699 | CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data); |
| 700 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 701 | BIO_free_all(ssl->rbio); |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 702 | BIO_free_all(ssl->wbio); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 703 | |
| 704 | BUF_MEM_free(ssl->init_buf); |
| 705 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 706 | // add extra stuff |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 707 | ssl_cipher_preference_list_free(ssl->cipher_list); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 708 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 709 | SSL_SESSION_free(ssl->session); |
| 710 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 711 | ssl_cert_free(ssl->cert); |
| 712 | |
| 713 | OPENSSL_free(ssl->tlsext_hostname); |
David Benjamin | be49706 | 2017-03-10 16:08:36 -0500 | [diff] [blame] | 714 | SSL_CTX_free(ssl->session_ctx); |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 715 | OPENSSL_free(ssl->supported_group_list); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 716 | OPENSSL_free(ssl->alpn_client_proto_list); |
| 717 | EVP_PKEY_free(ssl->tlsext_channel_id_private); |
| 718 | OPENSSL_free(ssl->psk_identity_hint); |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 719 | sk_CRYPTO_BUFFER_pop_free(ssl->client_CA, CRYPTO_BUFFER_free); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 720 | sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles); |
| 721 | |
| 722 | if (ssl->method != NULL) { |
| 723 | ssl->method->ssl_free(ssl); |
| 724 | } |
| 725 | SSL_CTX_free(ssl->ctx); |
| 726 | |
| 727 | OPENSSL_free(ssl); |
| 728 | } |
| 729 | |
| 730 | void SSL_set_connect_state(SSL *ssl) { |
| 731 | ssl->server = 0; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 732 | ssl->do_handshake = ssl_client_handshake; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | void SSL_set_accept_state(SSL *ssl) { |
| 736 | ssl->server = 1; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 737 | ssl->do_handshake = ssl_server_handshake; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 738 | } |
| 739 | |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 740 | void SSL_set0_rbio(SSL *ssl, BIO *rbio) { |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 741 | BIO_free_all(ssl->rbio); |
| 742 | ssl->rbio = rbio; |
| 743 | } |
| 744 | |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 745 | void SSL_set0_wbio(SSL *ssl, BIO *wbio) { |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 746 | BIO_free_all(ssl->wbio); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 747 | ssl->wbio = wbio; |
| 748 | } |
| 749 | |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 750 | void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 751 | // For historical reasons, this function has many different cases in ownership |
| 752 | // handling. |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 753 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 754 | // If nothing has changed, do nothing |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 755 | if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) { |
| 756 | return; |
| 757 | } |
| 758 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 759 | // If the two arguments are equal, one fewer reference is granted than |
| 760 | // taken. |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 761 | if (rbio != NULL && rbio == wbio) { |
| 762 | BIO_up_ref(rbio); |
| 763 | } |
| 764 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 765 | // If only the wbio is changed, adopt only one reference. |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 766 | if (rbio == SSL_get_rbio(ssl)) { |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 767 | SSL_set0_wbio(ssl, wbio); |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 768 | return; |
| 769 | } |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 770 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 771 | // There is an asymmetry here for historical reasons. If only the rbio is |
| 772 | // changed AND the rbio and wbio were originally different, then we only adopt |
| 773 | // one reference. |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 774 | if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) { |
| 775 | SSL_set0_rbio(ssl, rbio); |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 776 | return; |
| 777 | } |
| 778 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 779 | // Otherwise, adopt both references. |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 780 | SSL_set0_rbio(ssl, rbio); |
| 781 | SSL_set0_wbio(ssl, wbio); |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 782 | } |
| 783 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 784 | BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; } |
| 785 | |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 786 | BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; } |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 787 | |
David Benjamin | 2be4aa7 | 2017-01-02 07:56:51 -0500 | [diff] [blame] | 788 | int SSL_do_handshake(SSL *ssl) { |
| 789 | ssl_reset_error_state(ssl); |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 790 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 791 | if (ssl->do_handshake == NULL) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 792 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET); |
| 793 | return -1; |
| 794 | } |
| 795 | |
| 796 | if (!SSL_in_init(ssl)) { |
| 797 | return 1; |
| 798 | } |
| 799 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 800 | // Run the handshake. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 801 | SSL_HANDSHAKE *hs = ssl->s3->hs; |
| 802 | |
| 803 | int early_return = 0; |
| 804 | int ret = ssl_run_handshake(hs, &early_return); |
| 805 | ssl_do_info_callback( |
| 806 | ssl, ssl->server ? SSL_CB_ACCEPT_EXIT : SSL_CB_CONNECT_EXIT, ret); |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 807 | if (ret <= 0) { |
| 808 | return ret; |
| 809 | } |
| 810 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 811 | // Destroy the handshake object if the handshake has completely finished. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 812 | if (!early_return) { |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 813 | ssl_handshake_free(ssl->s3->hs); |
| 814 | ssl->s3->hs = NULL; |
| 815 | } |
| 816 | |
| 817 | return 1; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | int SSL_connect(SSL *ssl) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 821 | if (ssl->do_handshake == NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 822 | // Not properly initialized yet |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 823 | SSL_set_connect_state(ssl); |
| 824 | } |
| 825 | |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 826 | return SSL_do_handshake(ssl); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | int SSL_accept(SSL *ssl) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 830 | if (ssl->do_handshake == NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 831 | // Not properly initialized yet |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 832 | SSL_set_accept_state(ssl); |
| 833 | } |
| 834 | |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 835 | return SSL_do_handshake(ssl); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 836 | } |
| 837 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 838 | static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) { |
| 839 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 840 | return tls13_post_handshake(ssl, msg); |
| 841 | } |
| 842 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 843 | // We do not accept renegotiations as a server or SSL 3.0. SSL 3.0 will be |
| 844 | // removed entirely in the future and requires retaining more data for |
| 845 | // renegotiation_info. |
David Benjamin | 34941c0 | 2016-10-08 11:45:31 -0400 | [diff] [blame] | 846 | if (ssl->server || ssl->version == SSL3_VERSION) { |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 847 | goto no_renegotiation; |
| 848 | } |
| 849 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 850 | if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) { |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 851 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 852 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST); |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | switch (ssl->renegotiate_mode) { |
| 857 | case ssl_renegotiate_ignore: |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 858 | // Ignore the HelloRequest. |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 859 | return 1; |
| 860 | |
| 861 | case ssl_renegotiate_once: |
| 862 | if (ssl->s3->total_renegotiations != 0) { |
| 863 | goto no_renegotiation; |
| 864 | } |
| 865 | break; |
| 866 | |
| 867 | case ssl_renegotiate_never: |
| 868 | goto no_renegotiation; |
| 869 | |
| 870 | case ssl_renegotiate_freely: |
| 871 | break; |
| 872 | } |
| 873 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 874 | // Renegotiation is only supported at quiescent points in the application |
| 875 | // protocol, namely in HTTPS, just before reading the HTTP response. Require |
| 876 | // the record-layer be idle and avoid complexities of sending a handshake |
| 877 | // record while an application_data record is being written. |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 878 | if (ssl_write_buffer_is_pending(ssl)) { |
| 879 | goto no_renegotiation; |
| 880 | } |
| 881 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 882 | // Begin a new handshake. |
David Benjamin | 2644a13 | 2016-12-11 13:41:17 -0500 | [diff] [blame] | 883 | if (ssl->s3->hs != NULL) { |
| 884 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 885 | return 0; |
| 886 | } |
| 887 | ssl->s3->hs = ssl_handshake_new(ssl); |
| 888 | if (ssl->s3->hs == NULL) { |
| 889 | return 0; |
| 890 | } |
| 891 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 892 | ssl->s3->total_renegotiations++; |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 893 | return 1; |
| 894 | |
| 895 | no_renegotiation: |
| 896 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION); |
| 897 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION); |
| 898 | return 0; |
| 899 | } |
| 900 | |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 901 | static int ssl_read_impl(SSL *ssl, void *buf, int num, int peek) { |
David Benjamin | 2be4aa7 | 2017-01-02 07:56:51 -0500 | [diff] [blame] | 902 | ssl_reset_error_state(ssl); |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 903 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 904 | if (ssl->do_handshake == NULL) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 905 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 906 | return -1; |
| 907 | } |
| 908 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 909 | for (;;) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 910 | // Complete the current handshake, if any. False Start will cause |
| 911 | // |SSL_do_handshake| to return mid-handshake, so this may require multiple |
| 912 | // iterations. |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 913 | while (!ssl_can_read(ssl)) { |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 914 | int ret = SSL_do_handshake(ssl); |
| 915 | if (ret < 0) { |
| 916 | return ret; |
| 917 | } |
| 918 | if (ret == 0) { |
| 919 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 920 | return -1; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | int got_handshake; |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 925 | int ret = ssl->method->read_app_data(ssl, &got_handshake, (uint8_t *)buf, |
| 926 | num, peek); |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 927 | if (ret > 0 || !got_handshake) { |
Steven Valdez | 32635b8 | 2016-08-16 11:25:03 -0400 | [diff] [blame] | 928 | ssl->s3->key_update_count = 0; |
David Benjamin | c79845c | 2016-03-10 01:28:00 -0500 | [diff] [blame] | 929 | return ret; |
| 930 | } |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 931 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 932 | // If we received an interrupt in early read (the end_of_early_data alert), |
| 933 | // loop again for the handshake to process it. |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 934 | if (SSL_in_init(ssl)) { |
| 935 | continue; |
| 936 | } |
| 937 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 938 | SSLMessage msg; |
| 939 | while (ssl->method->get_message(ssl, &msg)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 940 | // Handle the post-handshake message and try again. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 941 | if (!ssl_do_post_handshake(ssl, msg)) { |
| 942 | return -1; |
| 943 | } |
| 944 | ssl->method->next_message(ssl); |
David Benjamin | c79845c | 2016-03-10 01:28:00 -0500 | [diff] [blame] | 945 | } |
| 946 | } |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | int SSL_read(SSL *ssl, void *buf, int num) { |
| 950 | return ssl_read_impl(ssl, buf, num, 0 /* consume bytes */); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | int SSL_peek(SSL *ssl, void *buf, int num) { |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 954 | return ssl_read_impl(ssl, buf, num, 1 /* peek */); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | int SSL_write(SSL *ssl, const void *buf, int num) { |
David Benjamin | 2be4aa7 | 2017-01-02 07:56:51 -0500 | [diff] [blame] | 958 | ssl_reset_error_state(ssl); |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 959 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 960 | if (ssl->do_handshake == NULL) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 961 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 962 | return -1; |
| 963 | } |
| 964 | |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 965 | if (ssl->s3->send_shutdown != ssl_shutdown_none) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 966 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 967 | return -1; |
| 968 | } |
| 969 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 970 | int ret = 0, needs_handshake = 0; |
| 971 | do { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 972 | // If necessary, complete the handshake implicitly. |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 973 | if (!ssl_can_write(ssl)) { |
| 974 | ret = SSL_do_handshake(ssl); |
| 975 | if (ret < 0) { |
| 976 | return ret; |
| 977 | } |
| 978 | if (ret == 0) { |
| 979 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 980 | return -1; |
| 981 | } |
David Benjamin | d7ac143 | 2016-03-10 00:41:25 -0500 | [diff] [blame] | 982 | } |
David Benjamin | d7ac143 | 2016-03-10 00:41:25 -0500 | [diff] [blame] | 983 | |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 984 | ret = ssl->method->write_app_data(ssl, &needs_handshake, |
| 985 | (const uint8_t *)buf, num); |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 986 | } while (needs_handshake); |
| 987 | return ret; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | int SSL_shutdown(SSL *ssl) { |
David Benjamin | 2be4aa7 | 2017-01-02 07:56:51 -0500 | [diff] [blame] | 991 | ssl_reset_error_state(ssl); |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 992 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 993 | if (ssl->do_handshake == NULL) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 994 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 995 | return -1; |
| 996 | } |
| 997 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 998 | // If we are in the middle of a handshake, silently succeed. Consumers often |
| 999 | // call this function before |SSL_free|, whether the handshake succeeded or |
| 1000 | // not. We assume the caller has already handled failed handshakes. |
Steven Valdez | a14934f | 2016-02-29 10:05:08 -0500 | [diff] [blame] | 1001 | if (SSL_in_init(ssl)) { |
David Benjamin | 12709db | 2017-02-17 13:51:50 -0500 | [diff] [blame] | 1002 | return 1; |
Steven Valdez | a14934f | 2016-02-29 10:05:08 -0500 | [diff] [blame] | 1003 | } |
| 1004 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1005 | if (ssl->quiet_shutdown) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1006 | // Do nothing if configured not to send a close_notify. |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 1007 | ssl->s3->send_shutdown = ssl_shutdown_close_notify; |
| 1008 | ssl->s3->recv_shutdown = ssl_shutdown_close_notify; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1009 | return 1; |
| 1010 | } |
| 1011 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1012 | // This function completes in two stages. It sends a close_notify and then it |
| 1013 | // waits for a close_notify to come in. Perform exactly one action and return |
| 1014 | // whether or not it succeeds. |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1015 | |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 1016 | if (ssl->s3->send_shutdown != ssl_shutdown_close_notify) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1017 | // Send a close_notify. |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 1018 | if (ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) { |
| 1019 | return -1; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1020 | } |
| 1021 | } else if (ssl->s3->alert_dispatch) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1022 | // Finish sending the close_notify. |
David Benjamin | f0ee907 | 2016-06-15 17:44:37 -0400 | [diff] [blame] | 1023 | if (ssl->method->dispatch_alert(ssl) <= 0) { |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 1024 | return -1; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1025 | } |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 1026 | } else if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1027 | // Wait for the peer's close_notify. |
David Benjamin | f0ee907 | 2016-06-15 17:44:37 -0400 | [diff] [blame] | 1028 | ssl->method->read_close_notify(ssl); |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 1029 | if (ssl->s3->recv_shutdown != ssl_shutdown_close_notify) { |
| 1030 | return -1; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1034 | // Return 0 for unidirectional shutdown and 1 for bidirectional shutdown. |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 1035 | return ssl->s3->recv_shutdown == ssl_shutdown_close_notify; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1036 | } |
| 1037 | |
David Benjamin | 1d4f4c0 | 2016-07-26 18:03:08 -0400 | [diff] [blame] | 1038 | int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) { |
| 1039 | if (ssl->s3->alert_dispatch) { |
| 1040 | if (ssl->s3->send_alert[0] != SSL3_AL_FATAL || |
| 1041 | ssl->s3->send_alert[1] != alert) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1042 | // We are already attempting to write a different alert. |
David Benjamin | 1d4f4c0 | 2016-07-26 18:03:08 -0400 | [diff] [blame] | 1043 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 1044 | return -1; |
| 1045 | } |
| 1046 | return ssl->method->dispatch_alert(ssl); |
| 1047 | } |
| 1048 | |
| 1049 | return ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1050 | } |
| 1051 | |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 1052 | void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) { |
Alessandro Ghedini | 67bb45f | 2017-03-30 16:33:24 -0500 | [diff] [blame] | 1053 | ctx->cert->enable_early_data = !!enabled; |
| 1054 | } |
| 1055 | |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 1056 | void SSL_CTX_set_tls13_variant(SSL_CTX *ctx, enum tls13_variant_t variant) { |
| 1057 | ctx->tls13_variant = variant; |
| 1058 | } |
| 1059 | |
Steven Valdez | 52586f9 | 2017-07-11 15:08:32 -0400 | [diff] [blame] | 1060 | void SSL_set_tls13_variant(SSL *ssl, enum tls13_variant_t variant) { |
| 1061 | ssl->tls13_variant = variant; |
| 1062 | } |
| 1063 | |
Alessandro Ghedini | 67bb45f | 2017-03-30 16:33:24 -0500 | [diff] [blame] | 1064 | void SSL_set_early_data_enabled(SSL *ssl, int enabled) { |
| 1065 | ssl->cert->enable_early_data = !!enabled; |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 1066 | } |
| 1067 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1068 | int SSL_in_early_data(const SSL *ssl) { |
| 1069 | if (ssl->s3->hs == NULL) { |
| 1070 | return 0; |
| 1071 | } |
| 1072 | return ssl->s3->hs->in_early_data; |
| 1073 | } |
| 1074 | |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1075 | int SSL_early_data_accepted(const SSL *ssl) { |
| 1076 | return ssl->early_data_accepted; |
| 1077 | } |
| 1078 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1079 | void SSL_reset_early_data_reject(SSL *ssl) { |
| 1080 | SSL_HANDSHAKE *hs = ssl->s3->hs; |
| 1081 | if (hs == NULL || |
| 1082 | hs->wait != ssl_hs_early_data_rejected) { |
| 1083 | abort(); |
| 1084 | } |
| 1085 | |
| 1086 | hs->wait = ssl_hs_ok; |
| 1087 | hs->in_early_data = 0; |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1088 | hs->early_session.reset(); |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1089 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1090 | // Discard any unfinished writes from the perspective of |SSL_write|'s |
| 1091 | // retry. The handshake will transparently flush out the pending record |
| 1092 | // (discarded by the server) to keep the framing correct. |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1093 | ssl->s3->wpend_pending = 0; |
| 1094 | } |
| 1095 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1096 | static int bio_retry_reason_to_error(int reason) { |
| 1097 | switch (reason) { |
| 1098 | case BIO_RR_CONNECT: |
| 1099 | return SSL_ERROR_WANT_CONNECT; |
| 1100 | case BIO_RR_ACCEPT: |
| 1101 | return SSL_ERROR_WANT_ACCEPT; |
| 1102 | default: |
| 1103 | return SSL_ERROR_SYSCALL; |
| 1104 | } |
| 1105 | } |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1106 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1107 | int SSL_get_error(const SSL *ssl, int ret_code) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1108 | if (ret_code > 0) { |
| 1109 | return SSL_ERROR_NONE; |
| 1110 | } |
| 1111 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1112 | // Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc, |
| 1113 | // where we do encode the error |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1114 | uint32_t err = ERR_peek_error(); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1115 | if (err != 0) { |
| 1116 | if (ERR_GET_LIB(err) == ERR_LIB_SYS) { |
| 1117 | return SSL_ERROR_SYSCALL; |
| 1118 | } |
| 1119 | return SSL_ERROR_SSL; |
| 1120 | } |
| 1121 | |
| 1122 | if (ret_code == 0) { |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 1123 | if (ssl->s3->recv_shutdown == ssl_shutdown_close_notify) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1124 | return SSL_ERROR_ZERO_RETURN; |
| 1125 | } |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1126 | // An EOF was observed which violates the protocol, and the underlying |
| 1127 | // transport does not participate in the error queue. Bubble up to the |
| 1128 | // caller. |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1129 | return SSL_ERROR_SYSCALL; |
| 1130 | } |
| 1131 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1132 | switch (ssl->rwstate) { |
| 1133 | case SSL_PENDING_SESSION: |
| 1134 | return SSL_ERROR_PENDING_SESSION; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1135 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1136 | case SSL_CERTIFICATE_SELECTION_PENDING: |
| 1137 | return SSL_ERROR_PENDING_CERTIFICATE; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1138 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1139 | case SSL_READING: { |
| 1140 | BIO *bio = SSL_get_rbio(ssl); |
| 1141 | if (BIO_should_read(bio)) { |
| 1142 | return SSL_ERROR_WANT_READ; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1143 | } |
| 1144 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1145 | if (BIO_should_write(bio)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1146 | // TODO(davidben): OpenSSL historically checked for writes on the read |
| 1147 | // BIO. Can this be removed? |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1148 | return SSL_ERROR_WANT_WRITE; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1149 | } |
| 1150 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1151 | if (BIO_should_io_special(bio)) { |
| 1152 | return bio_retry_reason_to_error(BIO_get_retry_reason(bio)); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1153 | } |
| 1154 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1155 | break; |
| 1156 | } |
| 1157 | |
| 1158 | case SSL_WRITING: { |
| 1159 | BIO *bio = SSL_get_wbio(ssl); |
| 1160 | if (BIO_should_write(bio)) { |
| 1161 | return SSL_ERROR_WANT_WRITE; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1162 | } |
| 1163 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1164 | if (BIO_should_read(bio)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1165 | // TODO(davidben): OpenSSL historically checked for reads on the write |
| 1166 | // BIO. Can this be removed? |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1167 | return SSL_ERROR_WANT_READ; |
| 1168 | } |
| 1169 | |
| 1170 | if (BIO_should_io_special(bio)) { |
| 1171 | return bio_retry_reason_to_error(BIO_get_retry_reason(bio)); |
| 1172 | } |
| 1173 | |
| 1174 | break; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1175 | } |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1176 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1177 | case SSL_X509_LOOKUP: |
| 1178 | return SSL_ERROR_WANT_X509_LOOKUP; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1179 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1180 | case SSL_CHANNEL_ID_LOOKUP: |
| 1181 | return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1182 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1183 | case SSL_PRIVATE_KEY_OPERATION: |
| 1184 | return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION; |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 1185 | |
| 1186 | case SSL_PENDING_TICKET: |
| 1187 | return SSL_ERROR_PENDING_TICKET; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1188 | |
| 1189 | case SSL_EARLY_DATA_REJECTED: |
| 1190 | return SSL_ERROR_EARLY_DATA_REJECTED; |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 1191 | |
| 1192 | case SSL_CERTIFICATE_VERIFY: |
| 1193 | return SSL_ERROR_WANT_CERTIFICATE_VERIFY; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | return SSL_ERROR_SYSCALL; |
| 1197 | } |
| 1198 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1199 | uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) { |
| 1200 | ctx->options |= options; |
| 1201 | return ctx->options; |
| 1202 | } |
| 1203 | |
| 1204 | uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) { |
| 1205 | ctx->options &= ~options; |
| 1206 | return ctx->options; |
| 1207 | } |
| 1208 | |
| 1209 | uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; } |
| 1210 | |
| 1211 | uint32_t SSL_set_options(SSL *ssl, uint32_t options) { |
| 1212 | ssl->options |= options; |
| 1213 | return ssl->options; |
| 1214 | } |
| 1215 | |
| 1216 | uint32_t SSL_clear_options(SSL *ssl, uint32_t options) { |
| 1217 | ssl->options &= ~options; |
| 1218 | return ssl->options; |
| 1219 | } |
| 1220 | |
| 1221 | uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; } |
| 1222 | |
| 1223 | uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) { |
| 1224 | ctx->mode |= mode; |
| 1225 | return ctx->mode; |
| 1226 | } |
| 1227 | |
| 1228 | uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) { |
| 1229 | ctx->mode &= ~mode; |
| 1230 | return ctx->mode; |
| 1231 | } |
| 1232 | |
| 1233 | uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; } |
| 1234 | |
| 1235 | uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) { |
| 1236 | ssl->mode |= mode; |
| 1237 | return ssl->mode; |
| 1238 | } |
| 1239 | |
| 1240 | uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) { |
| 1241 | ssl->mode &= ~mode; |
| 1242 | return ssl->mode; |
| 1243 | } |
| 1244 | |
| 1245 | uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; } |
| 1246 | |
Adam Langley | d519bf6 | 2016-12-12 11:16:44 -0800 | [diff] [blame] | 1247 | void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) { |
| 1248 | ctx->pool = pool; |
| 1249 | } |
| 1250 | |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1251 | int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len, |
| 1252 | size_t max_out) { |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1253 | *out_len = 0; |
| 1254 | OPENSSL_memset(out, 0, max_out); |
| 1255 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1256 | // tls-unique is not defined for SSL 3.0 or TLS 1.3. |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1257 | if (!ssl->s3->initial_handshake_complete || |
| 1258 | ssl3_protocol_version(ssl) < TLS1_VERSION || |
| 1259 | ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1260 | return 0; |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1261 | } |
| 1262 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1263 | // The tls-unique value is the first Finished message in the handshake, which |
| 1264 | // is the client's in a full handshake and the server's for a resumption. See |
| 1265 | // https://tools.ietf.org/html/rfc5929#section-3.1. |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1266 | const uint8_t *finished = ssl->s3->previous_client_finished; |
| 1267 | size_t finished_len = ssl->s3->previous_client_finished_len; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1268 | if (ssl->session != NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1269 | // tls-unique is broken for resumed sessions unless EMS is used. |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1270 | if (!ssl->session->extended_master_secret) { |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1271 | return 0; |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1272 | } |
| 1273 | finished = ssl->s3->previous_server_finished; |
| 1274 | finished_len = ssl->s3->previous_server_finished_len; |
| 1275 | } |
| 1276 | |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1277 | *out_len = finished_len; |
| 1278 | if (finished_len > max_out) { |
| 1279 | *out_len = max_out; |
| 1280 | } |
| 1281 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1282 | OPENSSL_memcpy(out, finished, *out_len); |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1283 | return 1; |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1284 | } |
| 1285 | |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 1286 | static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx, |
David Benjamin | 30c4c30 | 2016-12-07 22:35:24 -0500 | [diff] [blame] | 1287 | size_t sid_ctx_len) { |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 1288 | if (sid_ctx_len > sizeof(cert->sid_ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1289 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1290 | return 0; |
| 1291 | } |
David Benjamin | 30c4c30 | 2016-12-07 22:35:24 -0500 | [diff] [blame] | 1292 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 1293 | static_assert(sizeof(cert->sid_ctx) < 256, "sid_ctx too large"); |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 1294 | cert->sid_ctx_length = (uint8_t)sid_ctx_len; |
| 1295 | OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1296 | return 1; |
| 1297 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1298 | |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 1299 | int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx, |
| 1300 | size_t sid_ctx_len) { |
| 1301 | return set_session_id_context(ctx->cert, sid_ctx, sid_ctx_len); |
| 1302 | } |
| 1303 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1304 | int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx, |
David Benjamin | 30c4c30 | 2016-12-07 22:35:24 -0500 | [diff] [blame] | 1305 | size_t sid_ctx_len) { |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 1306 | return set_session_id_context(ssl->cert, sid_ctx, sid_ctx_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1307 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1308 | |
David Benjamin | 07820b5 | 2016-12-07 21:13:41 -0500 | [diff] [blame] | 1309 | const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) { |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 1310 | *out_len = ssl->cert->sid_ctx_length; |
| 1311 | return ssl->cert->sid_ctx; |
David Benjamin | 07820b5 | 2016-12-07 21:13:41 -0500 | [diff] [blame] | 1312 | } |
| 1313 | |
David Benjamin | 7481d39 | 2015-07-05 19:38:46 -0400 | [diff] [blame] | 1314 | void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1315 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1316 | int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1317 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1318 | int SSL_get_rfd(const SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1319 | int ret = -1; |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1320 | BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR); |
| 1321 | if (b != NULL) { |
| 1322 | BIO_get_fd(b, &ret); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1323 | } |
| 1324 | return ret; |
| 1325 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1326 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1327 | int SSL_get_wfd(const SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1328 | int ret = -1; |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1329 | BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR); |
| 1330 | if (b != NULL) { |
| 1331 | BIO_get_fd(b, &ret); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1332 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1333 | return ret; |
| 1334 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1335 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1336 | int SSL_set_fd(SSL *ssl, int fd) { |
David Benjamin | 22ce9b2 | 2016-03-06 19:26:52 -0500 | [diff] [blame] | 1337 | BIO *bio = BIO_new(BIO_s_socket()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1338 | if (bio == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1339 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1340 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1341 | } |
| 1342 | BIO_set_fd(bio, fd, BIO_NOCLOSE); |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1343 | SSL_set_bio(ssl, bio, bio); |
| 1344 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1345 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1346 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1347 | int SSL_set_wfd(SSL *ssl, int fd) { |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 1348 | BIO *rbio = SSL_get_rbio(ssl); |
| 1349 | if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET || |
| 1350 | BIO_get_fd(rbio, NULL) != fd) { |
David Benjamin | 22ce9b2 | 2016-03-06 19:26:52 -0500 | [diff] [blame] | 1351 | BIO *bio = BIO_new(BIO_s_socket()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1352 | if (bio == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1353 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1354 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1355 | } |
| 1356 | BIO_set_fd(bio, fd, BIO_NOCLOSE); |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 1357 | SSL_set0_wbio(ssl, bio); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1358 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1359 | // Copy the rbio over to the wbio. |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 1360 | BIO_up_ref(rbio); |
| 1361 | SSL_set0_wbio(ssl, rbio); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1362 | } |
| 1363 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1364 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1365 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1366 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1367 | int SSL_set_rfd(SSL *ssl, int fd) { |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 1368 | BIO *wbio = SSL_get_wbio(ssl); |
| 1369 | if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET || |
| 1370 | BIO_get_fd(wbio, NULL) != fd) { |
David Benjamin | 22ce9b2 | 2016-03-06 19:26:52 -0500 | [diff] [blame] | 1371 | BIO *bio = BIO_new(BIO_s_socket()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1372 | if (bio == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1373 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1374 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1375 | } |
| 1376 | BIO_set_fd(bio, fd, BIO_NOCLOSE); |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 1377 | SSL_set0_rbio(ssl, bio); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1378 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1379 | // Copy the wbio over to the rbio. |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 1380 | BIO_up_ref(wbio); |
| 1381 | SSL_set0_rbio(ssl, wbio); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1382 | } |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1383 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1384 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1385 | |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1386 | static size_t copy_finished(void *out, size_t out_len, const uint8_t *in, |
| 1387 | size_t in_len) { |
| 1388 | if (out_len > in_len) { |
| 1389 | out_len = in_len; |
| 1390 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1391 | OPENSSL_memcpy(out, in, out_len); |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1392 | return in_len; |
| 1393 | } |
| 1394 | |
David Benjamin | 1a1b34d | 2015-10-17 12:51:52 -0400 | [diff] [blame] | 1395 | size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) { |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1396 | if (!ssl->s3->initial_handshake_complete || |
| 1397 | ssl3_protocol_version(ssl) < TLS1_VERSION || |
| 1398 | ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1399 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1400 | } |
| 1401 | |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1402 | if (ssl->server) { |
| 1403 | return copy_finished(buf, count, ssl->s3->previous_server_finished, |
| 1404 | ssl->s3->previous_server_finished_len); |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1405 | } |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1406 | |
| 1407 | return copy_finished(buf, count, ssl->s3->previous_client_finished, |
| 1408 | ssl->s3->previous_client_finished_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1409 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1410 | |
David Benjamin | 1a1b34d | 2015-10-17 12:51:52 -0400 | [diff] [blame] | 1411 | size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) { |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1412 | if (!ssl->s3->initial_handshake_complete || |
| 1413 | ssl3_protocol_version(ssl) < TLS1_VERSION || |
| 1414 | ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1415 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1416 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1417 | |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1418 | if (ssl->server) { |
| 1419 | return copy_finished(buf, count, ssl->s3->previous_client_finished, |
| 1420 | ssl->s3->previous_client_finished_len); |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1421 | } |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1422 | |
| 1423 | return copy_finished(buf, count, ssl->s3->previous_server_finished, |
| 1424 | ssl->s3->previous_server_finished_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1425 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1426 | |
David Benjamin | 5993704 | 2015-09-19 13:04:22 -0400 | [diff] [blame] | 1427 | int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1428 | |
David Benjamin | 42fea37 | 2015-09-19 01:22:44 -0400 | [diff] [blame] | 1429 | int SSL_get_extms_support(const SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1430 | // TLS 1.3 does not require extended master secret and always reports as |
| 1431 | // supporting it. |
David Benjamin | a68c118 | 2016-07-24 00:14:33 -0400 | [diff] [blame] | 1432 | if (!ssl->s3->have_version) { |
| 1433 | return 0; |
| 1434 | } |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 1435 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 1436 | return 1; |
| 1437 | } |
| 1438 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1439 | // If the initial handshake completed, query the established session. |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 1440 | if (ssl->s3->established_session != NULL) { |
| 1441 | return ssl->s3->established_session->extended_master_secret; |
| 1442 | } |
| 1443 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1444 | // Otherwise, query the in-progress handshake. |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 1445 | if (ssl->s3->hs != NULL) { |
| 1446 | return ssl->s3->hs->extended_master_secret; |
| 1447 | } |
| 1448 | assert(0); |
| 1449 | return 0; |
Matt Braithwaite | cd6f54b | 2015-09-17 12:54:42 -0700 | [diff] [blame] | 1450 | } |
| 1451 | |
David Benjamin | 9a41d1b | 2015-05-16 01:30:09 -0400 | [diff] [blame] | 1452 | int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1453 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1454 | int SSL_get_read_ahead(const SSL *ssl) { return 0; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1455 | |
David Benjamin | 9a41d1b | 2015-05-16 01:30:09 -0400 | [diff] [blame] | 1456 | void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { } |
David Benjamin | 61ecccf | 2015-05-05 09:44:51 -0400 | [diff] [blame] | 1457 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1458 | void SSL_set_read_ahead(SSL *ssl, int yes) { } |
David Benjamin | 61ecccf | 2015-05-05 09:44:51 -0400 | [diff] [blame] | 1459 | |
David Benjamin | 9f85949 | 2015-10-03 10:44:30 -0400 | [diff] [blame] | 1460 | int SSL_pending(const SSL *ssl) { |
| 1461 | if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) { |
| 1462 | return 0; |
| 1463 | } |
| 1464 | return ssl->s3->rrec.length; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1465 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1466 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1467 | // Fix this so it checks all the valid key/cert options |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1468 | int SSL_CTX_check_private_key(const SSL_CTX *ctx) { |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 1469 | return ssl_cert_check_private_key(ctx->cert, ctx->cert->privatekey); |
Adam Langley | c5ac2b6 | 2016-11-07 12:02:35 -0800 | [diff] [blame] | 1470 | } |
| 1471 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1472 | // Fix this function so that it takes an optional type parameter |
Adam Langley | c5ac2b6 | 2016-11-07 12:02:35 -0800 | [diff] [blame] | 1473 | int SSL_check_private_key(const SSL *ssl) { |
Adam Langley | 3a2b47a | 2017-01-24 13:59:42 -0800 | [diff] [blame] | 1474 | return ssl_cert_check_private_key(ssl->cert, ssl->cert->privatekey); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1475 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1476 | |
David Benjamin | 42fea37 | 2015-09-19 01:22:44 -0400 | [diff] [blame] | 1477 | long SSL_get_default_timeout(const SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1478 | return SSL_DEFAULT_SESSION_TIMEOUT; |
| 1479 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1480 | |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 1481 | int SSL_renegotiate(SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1482 | // Caller-initiated renegotiation is not supported. |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1483 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 1484 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1485 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1486 | |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 1487 | int SSL_renegotiate_pending(SSL *ssl) { |
| 1488 | return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1489 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1490 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1491 | int SSL_total_renegotiations(const SSL *ssl) { |
| 1492 | return ssl->s3->total_renegotiations; |
| 1493 | } |
| 1494 | |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 1495 | size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) { |
| 1496 | return ctx->max_cert_list; |
| 1497 | } |
| 1498 | |
| 1499 | void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) { |
| 1500 | if (max_cert_list > kMaxHandshakeSize) { |
| 1501 | max_cert_list = kMaxHandshakeSize; |
| 1502 | } |
| 1503 | ctx->max_cert_list = (uint32_t)max_cert_list; |
| 1504 | } |
| 1505 | |
| 1506 | size_t SSL_get_max_cert_list(const SSL *ssl) { |
| 1507 | return ssl->max_cert_list; |
| 1508 | } |
| 1509 | |
| 1510 | void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) { |
| 1511 | if (max_cert_list > kMaxHandshakeSize) { |
| 1512 | max_cert_list = kMaxHandshakeSize; |
| 1513 | } |
| 1514 | ssl->max_cert_list = (uint32_t)max_cert_list; |
| 1515 | } |
| 1516 | |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1517 | int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) { |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 1518 | if (max_send_fragment < 512) { |
| 1519 | max_send_fragment = 512; |
| 1520 | } |
| 1521 | if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 1522 | max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
| 1523 | } |
| 1524 | ctx->max_send_fragment = (uint16_t)max_send_fragment; |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1525 | |
| 1526 | return 1; |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 1527 | } |
| 1528 | |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1529 | int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) { |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 1530 | if (max_send_fragment < 512) { |
| 1531 | max_send_fragment = 512; |
| 1532 | } |
| 1533 | if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 1534 | max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
| 1535 | } |
| 1536 | ssl->max_send_fragment = (uint16_t)max_send_fragment; |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1537 | |
| 1538 | return 1; |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 1539 | } |
| 1540 | |
David Benjamin | cb9cf79 | 2015-05-05 09:46:14 -0400 | [diff] [blame] | 1541 | int SSL_set_mtu(SSL *ssl, unsigned mtu) { |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1542 | if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) { |
David Benjamin | cb9cf79 | 2015-05-05 09:46:14 -0400 | [diff] [blame] | 1543 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1544 | } |
David Benjamin | cb9cf79 | 2015-05-05 09:46:14 -0400 | [diff] [blame] | 1545 | ssl->d1->mtu = mtu; |
| 1546 | return 1; |
| 1547 | } |
| 1548 | |
| 1549 | int SSL_get_secure_renegotiation_support(const SSL *ssl) { |
David Benjamin | d261004 | 2017-01-03 10:49:28 -0500 | [diff] [blame] | 1550 | if (!ssl->s3->have_version) { |
| 1551 | return 0; |
| 1552 | } |
| 1553 | return ssl3_protocol_version(ssl) >= TLS1_3_VERSION || |
| 1554 | ssl->s3->send_connection_binding; |
David Benjamin | cb9cf79 | 2015-05-05 09:46:14 -0400 | [diff] [blame] | 1555 | } |
| 1556 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1557 | LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; } |
| 1558 | |
David Benjamin | 71f7d3d | 2015-05-05 09:46:38 -0400 | [diff] [blame] | 1559 | size_t SSL_CTX_sess_number(const SSL_CTX *ctx) { |
| 1560 | return lh_SSL_SESSION_num_items(ctx->sessions); |
| 1561 | } |
| 1562 | |
| 1563 | unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) { |
| 1564 | unsigned long ret = ctx->session_cache_size; |
| 1565 | ctx->session_cache_size = size; |
| 1566 | return ret; |
| 1567 | } |
| 1568 | |
| 1569 | unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) { |
| 1570 | return ctx->session_cache_size; |
| 1571 | } |
| 1572 | |
| 1573 | int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) { |
| 1574 | int ret = ctx->session_cache_mode; |
| 1575 | ctx->session_cache_mode = mode; |
| 1576 | return ret; |
| 1577 | } |
| 1578 | |
| 1579 | int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) { |
| 1580 | return ctx->session_cache_mode; |
| 1581 | } |
| 1582 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1583 | |
| 1584 | int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) { |
| 1585 | if (out == NULL) { |
| 1586 | return 48; |
| 1587 | } |
| 1588 | if (len != 48) { |
| 1589 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH); |
| 1590 | return 0; |
| 1591 | } |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 1592 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1593 | // The default ticket keys are initialized lazily. Trigger a key |
| 1594 | // rotation to initialize them. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 1595 | if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) { |
| 1596 | return 0; |
| 1597 | } |
| 1598 | |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1599 | uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 1600 | MutexReadLock lock(&ctx->lock); |
| 1601 | OPENSSL_memcpy(out_bytes, ctx->tlsext_ticket_key_current->name, 16); |
| 1602 | OPENSSL_memcpy(out_bytes + 16, ctx->tlsext_ticket_key_current->hmac_key, 16); |
| 1603 | OPENSSL_memcpy(out_bytes + 32, ctx->tlsext_ticket_key_current->aes_key, 16); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1604 | return 1; |
| 1605 | } |
| 1606 | |
| 1607 | int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) { |
| 1608 | if (in == NULL) { |
| 1609 | return 48; |
| 1610 | } |
| 1611 | if (len != 48) { |
| 1612 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH); |
| 1613 | return 0; |
| 1614 | } |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 1615 | if (!ctx->tlsext_ticket_key_current) { |
| 1616 | ctx->tlsext_ticket_key_current = |
| 1617 | (tlsext_ticket_key *)OPENSSL_malloc(sizeof(tlsext_ticket_key)); |
| 1618 | if (!ctx->tlsext_ticket_key_current) { |
| 1619 | return 0; |
| 1620 | } |
| 1621 | } |
| 1622 | OPENSSL_memset(ctx->tlsext_ticket_key_current, 0, sizeof(tlsext_ticket_key)); |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1623 | const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 1624 | OPENSSL_memcpy(ctx->tlsext_ticket_key_current->name, in_bytes, 16); |
| 1625 | OPENSSL_memcpy(ctx->tlsext_ticket_key_current->hmac_key, in_bytes + 16, 16); |
| 1626 | OPENSSL_memcpy(ctx->tlsext_ticket_key_current->aes_key, in_bytes + 32, 16); |
| 1627 | OPENSSL_free(ctx->tlsext_ticket_key_prev); |
| 1628 | ctx->tlsext_ticket_key_prev = nullptr; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1629 | // Disable automatic key rotation. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 1630 | ctx->tlsext_ticket_key_current->next_rotation_tv_sec = 0; |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1631 | return 1; |
| 1632 | } |
| 1633 | |
| 1634 | int SSL_CTX_set_tlsext_ticket_key_cb( |
| 1635 | SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv, |
| 1636 | EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx, |
| 1637 | int encrypt)) { |
| 1638 | ctx->tlsext_ticket_key_cb = callback; |
| 1639 | return 1; |
| 1640 | } |
| 1641 | |
| 1642 | int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) { |
| 1643 | return tls1_set_curves(&ctx->supported_group_list, |
| 1644 | &ctx->supported_group_list_len, curves, |
| 1645 | curves_len); |
| 1646 | } |
| 1647 | |
| 1648 | int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) { |
| 1649 | return tls1_set_curves(&ssl->supported_group_list, |
| 1650 | &ssl->supported_group_list_len, curves, |
| 1651 | curves_len); |
| 1652 | } |
| 1653 | |
Alessandro Ghedini | 5fd1807 | 2016-09-28 21:04:25 +0100 | [diff] [blame] | 1654 | int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) { |
| 1655 | return tls1_set_curves_list(&ctx->supported_group_list, |
| 1656 | &ctx->supported_group_list_len, curves); |
| 1657 | } |
| 1658 | |
| 1659 | int SSL_set1_curves_list(SSL *ssl, const char *curves) { |
| 1660 | return tls1_set_curves_list(&ssl->supported_group_list, |
| 1661 | &ssl->supported_group_list_len, curves); |
| 1662 | } |
| 1663 | |
David Benjamin | 9e68f19 | 2016-06-30 14:55:33 -0400 | [diff] [blame] | 1664 | uint16_t SSL_get_curve_id(const SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1665 | // TODO(davidben): This checks the wrong session if there is a renegotiation |
| 1666 | // in progress. |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1667 | SSL_SESSION *session = SSL_get_session(ssl); |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 1668 | if (session == NULL) { |
David Benjamin | 9e68f19 | 2016-06-30 14:55:33 -0400 | [diff] [blame] | 1669 | return 0; |
| 1670 | } |
| 1671 | |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 1672 | return session->group_id; |
David Benjamin | 9e68f19 | 2016-06-30 14:55:33 -0400 | [diff] [blame] | 1673 | } |
| 1674 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1675 | int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) { |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1676 | return 1; |
| 1677 | } |
| 1678 | |
| 1679 | int SSL_set_tmp_dh(SSL *ssl, const DH *dh) { |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1680 | return 1; |
| 1681 | } |
| 1682 | |
David Benjamin | 70dbf04 | 2017-08-08 18:51:37 -0400 | [diff] [blame] | 1683 | STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) { |
David Benjamin | 8ebeabf | 2017-03-02 22:04:07 -0500 | [diff] [blame] | 1684 | return ctx->cipher_list->ciphers; |
| 1685 | } |
| 1686 | |
David Benjamin | 70dbf04 | 2017-08-08 18:51:37 -0400 | [diff] [blame] | 1687 | int SSL_CTX_cipher_in_group(const SSL_CTX *ctx, size_t i) { |
| 1688 | if (i >= sk_SSL_CIPHER_num(ctx->cipher_list->ciphers)) { |
| 1689 | return 0; |
| 1690 | } |
| 1691 | return ctx->cipher_list->in_group_flags[i]; |
| 1692 | } |
| 1693 | |
David Benjamin | 32876b3 | 2015-09-20 12:17:03 -0400 | [diff] [blame] | 1694 | STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) { |
| 1695 | if (ssl == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1696 | return NULL; |
| 1697 | } |
Adam Langley | 0b5c1ac | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1698 | |
David Benjamin | af3b3d3 | 2016-10-31 16:29:57 -0400 | [diff] [blame] | 1699 | const struct ssl_cipher_preference_list_st *prefs = |
| 1700 | ssl_get_cipher_preferences(ssl); |
| 1701 | if (prefs == NULL) { |
| 1702 | return NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1703 | } |
Adam Langley | 0b5c1ac | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1704 | |
David Benjamin | af3b3d3 | 2016-10-31 16:29:57 -0400 | [diff] [blame] | 1705 | return prefs->ciphers; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1706 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1707 | |
David Benjamin | 32876b3 | 2015-09-20 12:17:03 -0400 | [diff] [blame] | 1708 | const char *SSL_get_cipher_list(const SSL *ssl, int n) { |
David Benjamin | 32876b3 | 2015-09-20 12:17:03 -0400 | [diff] [blame] | 1709 | if (ssl == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1710 | return NULL; |
| 1711 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1712 | |
David Benjamin | 8ebeabf | 2017-03-02 22:04:07 -0500 | [diff] [blame] | 1713 | STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1714 | if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) { |
| 1715 | return NULL; |
| 1716 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1717 | |
David Benjamin | 8ebeabf | 2017-03-02 22:04:07 -0500 | [diff] [blame] | 1718 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1719 | if (c == NULL) { |
| 1720 | return NULL; |
| 1721 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1722 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1723 | return c->name; |
| 1724 | } |
David Benjamin | 5491e3f | 2014-09-29 19:33:09 -0400 | [diff] [blame] | 1725 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1726 | int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) { |
Matthew Braithwaite | 6ad20dc | 2017-02-21 16:41:33 -0800 | [diff] [blame] | 1727 | return ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str, |
| 1728 | 0 /* not strict */); |
Matthew Braithwaite | a57dcfb | 2017-02-17 22:08:23 -0800 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) { |
Matthew Braithwaite | 6ad20dc | 2017-02-21 16:41:33 -0800 | [diff] [blame] | 1732 | return ssl_create_cipher_list(ctx->method, &ctx->cipher_list, str, |
| 1733 | 1 /* strict */); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1734 | } |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 1735 | |
David Benjamin | 32876b3 | 2015-09-20 12:17:03 -0400 | [diff] [blame] | 1736 | int SSL_set_cipher_list(SSL *ssl, const char *str) { |
Matthew Braithwaite | 6ad20dc | 2017-02-21 16:41:33 -0800 | [diff] [blame] | 1737 | return ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str, |
| 1738 | 0 /* not strict */); |
Matthew Braithwaite | a57dcfb | 2017-02-17 22:08:23 -0800 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | int SSL_set_strict_cipher_list(SSL *ssl, const char *str) { |
Matthew Braithwaite | 6ad20dc | 2017-02-21 16:41:33 -0800 | [diff] [blame] | 1742 | return ssl_create_cipher_list(ssl->ctx->method, &ssl->cipher_list, str, |
| 1743 | 1 /* strict */); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1744 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1745 | |
David Benjamin | 07e1384 | 2015-10-17 13:48:04 -0400 | [diff] [blame] | 1746 | const char *SSL_get_servername(const SSL *ssl, const int type) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1747 | if (type != TLSEXT_NAMETYPE_host_name) { |
| 1748 | return NULL; |
| 1749 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1750 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1751 | // Historically, |SSL_get_servername| was also the configuration getter |
| 1752 | // corresponding to |SSL_set_tlsext_host_name|. |
David Benjamin | 07e1384 | 2015-10-17 13:48:04 -0400 | [diff] [blame] | 1753 | if (ssl->tlsext_hostname != NULL) { |
| 1754 | return ssl->tlsext_hostname; |
| 1755 | } |
| 1756 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1757 | // During the handshake, report the handshake value. |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 1758 | if (ssl->s3->hs != NULL) { |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1759 | return ssl->s3->hs->hostname.get(); |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 1760 | } |
| 1761 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1762 | // SSL_get_servername may also be called after the handshake to look up the |
| 1763 | // SNI value. |
| 1764 | // |
| 1765 | // TODO(davidben): This is almost unused. Can we remove it? |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1766 | SSL_SESSION *session = SSL_get_session(ssl); |
| 1767 | if (session == NULL) { |
David Benjamin | 07e1384 | 2015-10-17 13:48:04 -0400 | [diff] [blame] | 1768 | return NULL; |
| 1769 | } |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1770 | return session->tlsext_hostname; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1771 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1772 | |
David Benjamin | 07e1384 | 2015-10-17 13:48:04 -0400 | [diff] [blame] | 1773 | int SSL_get_servername_type(const SSL *ssl) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1774 | SSL_SESSION *session = SSL_get_session(ssl); |
| 1775 | if (session == NULL || session->tlsext_hostname == NULL) { |
| 1776 | return -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1777 | } |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1778 | return TLSEXT_NAMETYPE_host_name; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1779 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1780 | |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 1781 | void SSL_CTX_set_custom_verify( |
| 1782 | SSL_CTX *ctx, int mode, |
| 1783 | enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) { |
| 1784 | ctx->verify_mode = mode; |
| 1785 | ctx->custom_verify_callback = callback; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1786 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 1787 | |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 1788 | void SSL_set_custom_verify( |
| 1789 | SSL *ssl, int mode, |
| 1790 | enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) { |
| 1791 | ssl->verify_mode = mode; |
| 1792 | ssl->custom_verify_callback = callback; |
| 1793 | } |
| 1794 | |
| 1795 | void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) { |
| 1796 | ctx->signed_cert_timestamps_enabled = 1; |
Adam Langley | fe36672 | 2017-03-07 16:41:04 -0800 | [diff] [blame] | 1797 | } |
| 1798 | |
David Benjamin | 26e1ff3 | 2017-02-14 20:13:00 -0500 | [diff] [blame] | 1799 | void SSL_enable_signed_cert_timestamps(SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1800 | ssl->signed_cert_timestamps_enabled = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1801 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 1802 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1803 | void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) { |
| 1804 | ctx->ocsp_stapling_enabled = 1; |
| 1805 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 1806 | |
David Benjamin | 26e1ff3 | 2017-02-14 20:13:00 -0500 | [diff] [blame] | 1807 | void SSL_enable_ocsp_stapling(SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1808 | ssl->ocsp_stapling_enabled = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1809 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 1810 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1811 | void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out, |
| 1812 | size_t *out_len) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1813 | SSL_SESSION *session = SSL_get_session(ssl); |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 1814 | if (ssl->server || !session || !session->signed_cert_timestamp_list) { |
| 1815 | *out_len = 0; |
| 1816 | *out = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1817 | return; |
| 1818 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 1819 | |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 1820 | *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list); |
| 1821 | *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1822 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 1823 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1824 | void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out, |
| 1825 | size_t *out_len) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1826 | SSL_SESSION *session = SSL_get_session(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1827 | if (ssl->server || !session || !session->ocsp_response) { |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 1828 | *out_len = 0; |
| 1829 | *out = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1830 | return; |
| 1831 | } |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 1832 | |
| 1833 | *out = CRYPTO_BUFFER_data(session->ocsp_response); |
| 1834 | *out_len = CRYPTO_BUFFER_len(session->ocsp_response); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1835 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 1836 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1837 | int SSL_set_tlsext_host_name(SSL *ssl, const char *name) { |
| 1838 | OPENSSL_free(ssl->tlsext_hostname); |
| 1839 | ssl->tlsext_hostname = NULL; |
| 1840 | |
| 1841 | if (name == NULL) { |
| 1842 | return 1; |
| 1843 | } |
| 1844 | |
| 1845 | size_t len = strlen(name); |
| 1846 | if (len == 0 || len > TLSEXT_MAXLEN_host_name) { |
| 1847 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME); |
| 1848 | return 0; |
| 1849 | } |
| 1850 | ssl->tlsext_hostname = BUF_strdup(name); |
| 1851 | if (ssl->tlsext_hostname == NULL) { |
| 1852 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 1853 | return 0; |
| 1854 | } |
| 1855 | return 1; |
| 1856 | } |
| 1857 | |
| 1858 | int SSL_CTX_set_tlsext_servername_callback( |
| 1859 | SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) { |
| 1860 | ctx->tlsext_servername_callback = callback; |
| 1861 | return 1; |
| 1862 | } |
| 1863 | |
| 1864 | int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) { |
| 1865 | ctx->tlsext_servername_arg = arg; |
| 1866 | return 1; |
| 1867 | } |
| 1868 | |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 1869 | int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer, |
| 1870 | unsigned peer_len, const uint8_t *supported, |
| 1871 | unsigned supported_len) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1872 | const uint8_t *result; |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 1873 | int status; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1874 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1875 | // For each protocol in peer preference order, see if we support it. |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 1876 | for (unsigned i = 0; i < peer_len;) { |
| 1877 | for (unsigned j = 0; j < supported_len;) { |
| 1878 | if (peer[i] == supported[j] && |
| 1879 | OPENSSL_memcmp(&peer[i + 1], &supported[j + 1], peer[i]) == 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1880 | // We found a match |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 1881 | result = &peer[i]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1882 | status = OPENSSL_NPN_NEGOTIATED; |
| 1883 | goto found; |
| 1884 | } |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 1885 | j += supported[j]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1886 | j++; |
| 1887 | } |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 1888 | i += peer[i]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1889 | i++; |
| 1890 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1891 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1892 | // There's no overlap between our protocols and the peer's list. |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 1893 | result = supported; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1894 | status = OPENSSL_NPN_NO_OVERLAP; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1895 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1896 | found: |
| 1897 | *out = (uint8_t *)result + 1; |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 1898 | *out_len = result[0]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1899 | return status; |
| 1900 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1901 | |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 1902 | void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data, |
| 1903 | unsigned *out_len) { |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1904 | *out_data = ssl->s3->next_proto_negotiated; |
David Benjamin | 05d4c97 | 2017-06-21 16:35:10 -0400 | [diff] [blame] | 1905 | *out_len = ssl->s3->next_proto_negotiated_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1906 | } |
| 1907 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1908 | void SSL_CTX_set_next_protos_advertised_cb( |
| 1909 | SSL_CTX *ctx, |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 1910 | int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1911 | void *arg) { |
| 1912 | ctx->next_protos_advertised_cb = cb; |
| 1913 | ctx->next_protos_advertised_cb_arg = arg; |
| 1914 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1915 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1916 | void SSL_CTX_set_next_proto_select_cb( |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 1917 | SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len, |
| 1918 | const uint8_t *in, unsigned in_len, void *arg), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1919 | void *arg) { |
| 1920 | ctx->next_proto_select_cb = cb; |
| 1921 | ctx->next_proto_select_cb_arg = arg; |
| 1922 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1923 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1924 | int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos, |
| 1925 | unsigned protos_len) { |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1926 | OPENSSL_free(ctx->alpn_client_proto_list); |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1927 | ctx->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1928 | if (!ctx->alpn_client_proto_list) { |
| 1929 | return 1; |
| 1930 | } |
| 1931 | ctx->alpn_client_proto_list_len = protos_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1932 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1933 | return 0; |
| 1934 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1935 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1936 | 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] | 1937 | OPENSSL_free(ssl->alpn_client_proto_list); |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1938 | ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1939 | if (!ssl->alpn_client_proto_list) { |
| 1940 | return 1; |
| 1941 | } |
| 1942 | ssl->alpn_client_proto_list_len = protos_len; |
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 | return 0; |
| 1945 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1946 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1947 | void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx, |
| 1948 | int (*cb)(SSL *ssl, const uint8_t **out, |
David Benjamin | 8984f1f | 2015-09-16 00:10:19 -0400 | [diff] [blame] | 1949 | uint8_t *out_len, const uint8_t *in, |
| 1950 | unsigned in_len, void *arg), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1951 | void *arg) { |
| 1952 | ctx->alpn_select_cb = cb; |
| 1953 | ctx->alpn_select_cb_arg = arg; |
| 1954 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1955 | |
David Benjamin | 8984f1f | 2015-09-16 00:10:19 -0400 | [diff] [blame] | 1956 | void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data, |
| 1957 | unsigned *out_len) { |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1958 | if (SSL_in_early_data(ssl) && !ssl->server) { |
| 1959 | *out_data = ssl->s3->hs->early_session->early_alpn; |
| 1960 | *out_len = ssl->s3->hs->early_session->early_alpn_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1961 | } else { |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1962 | *out_data = ssl->s3->alpn_selected; |
David Benjamin | 8984f1f | 2015-09-16 00:10:19 -0400 | [diff] [blame] | 1963 | *out_len = ssl->s3->alpn_selected_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1964 | } |
| 1965 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1966 | |
David Benjamin | c8ff30c | 2017-04-04 13:52:36 -0400 | [diff] [blame] | 1967 | void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) { |
| 1968 | ctx->allow_unknown_alpn_protos = !!enabled; |
| 1969 | } |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1970 | |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 1971 | void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) { |
| 1972 | ctx->tlsext_channel_id_enabled = !!enabled; |
| 1973 | } |
| 1974 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1975 | int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) { |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 1976 | SSL_CTX_set_tls_channel_id_enabled(ctx, 1); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1977 | return 1; |
| 1978 | } |
| 1979 | |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 1980 | void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) { |
| 1981 | ssl->tlsext_channel_id_enabled = !!enabled; |
| 1982 | } |
| 1983 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1984 | int SSL_enable_tls_channel_id(SSL *ssl) { |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 1985 | SSL_set_tls_channel_id_enabled(ssl, 1); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1986 | return 1; |
| 1987 | } |
| 1988 | |
| 1989 | static int is_p256_key(EVP_PKEY *private_key) { |
| 1990 | const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key); |
| 1991 | return ec_key != NULL && |
| 1992 | EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) == |
| 1993 | NID_X9_62_prime256v1; |
| 1994 | } |
| 1995 | |
| 1996 | int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) { |
| 1997 | if (!is_p256_key(private_key)) { |
| 1998 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256); |
| 1999 | return 0; |
| 2000 | } |
| 2001 | |
| 2002 | EVP_PKEY_free(ctx->tlsext_channel_id_private); |
Adam Langley | 310d3f6 | 2016-07-12 10:39:20 -0700 | [diff] [blame] | 2003 | EVP_PKEY_up_ref(private_key); |
| 2004 | ctx->tlsext_channel_id_private = private_key; |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2005 | ctx->tlsext_channel_id_enabled = 1; |
| 2006 | |
| 2007 | return 1; |
| 2008 | } |
| 2009 | |
| 2010 | int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) { |
| 2011 | if (!is_p256_key(private_key)) { |
| 2012 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256); |
| 2013 | return 0; |
| 2014 | } |
| 2015 | |
| 2016 | EVP_PKEY_free(ssl->tlsext_channel_id_private); |
Adam Langley | 310d3f6 | 2016-07-12 10:39:20 -0700 | [diff] [blame] | 2017 | EVP_PKEY_up_ref(private_key); |
| 2018 | ssl->tlsext_channel_id_private = private_key; |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2019 | ssl->tlsext_channel_id_enabled = 1; |
| 2020 | |
| 2021 | return 1; |
| 2022 | } |
| 2023 | |
| 2024 | size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) { |
| 2025 | if (!ssl->s3->tlsext_channel_id_valid) { |
| 2026 | return 0; |
| 2027 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 2028 | OPENSSL_memcpy(out, ssl->s3->tlsext_channel_id, |
| 2029 | (max_out < 64) ? max_out : 64); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2030 | return 64; |
| 2031 | } |
| 2032 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2033 | size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) { |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 2034 | if (ssl->server || ssl->s3->hs == NULL) { |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2035 | *out_types = NULL; |
| 2036 | return 0; |
| 2037 | } |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 2038 | *out_types = ssl->s3->hs->certificate_types; |
| 2039 | return ssl->s3->hs->num_certificate_types; |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2040 | } |
| 2041 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2042 | EVP_PKEY *SSL_get_privatekey(const SSL *ssl) { |
| 2043 | if (ssl->cert != NULL) { |
| 2044 | return ssl->cert->privatekey; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | return NULL; |
| 2048 | } |
| 2049 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2050 | EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) { |
| 2051 | if (ctx->cert != NULL) { |
David Benjamin | d1d8078 | 2015-07-05 11:54:09 -0400 | [diff] [blame] | 2052 | return ctx->cert->privatekey; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | return NULL; |
| 2056 | } |
| 2057 | |
David Benjamin | 42fea37 | 2015-09-19 01:22:44 -0400 | [diff] [blame] | 2058 | const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 2059 | return ssl->s3->aead_write_ctx->cipher(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2060 | } |
| 2061 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2062 | int SSL_session_reused(const SSL *ssl) { |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2063 | return ssl->s3->session_reused || SSL_in_early_data(ssl); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2064 | } |
| 2065 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2066 | const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2067 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2068 | const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2069 | |
Adam Langley | 29ec5d1 | 2016-03-01 16:12:28 -0800 | [diff] [blame] | 2070 | int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; } |
| 2071 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2072 | void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) { |
Adam Langley | bb85f3d | 2015-10-28 18:44:11 -0700 | [diff] [blame] | 2073 | ctx->quiet_shutdown = (mode != 0); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) { |
| 2077 | return ctx->quiet_shutdown; |
| 2078 | } |
| 2079 | |
Adam Langley | bb85f3d | 2015-10-28 18:44:11 -0700 | [diff] [blame] | 2080 | void SSL_set_quiet_shutdown(SSL *ssl, int mode) { |
| 2081 | ssl->quiet_shutdown = (mode != 0); |
| 2082 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2083 | |
David Benjamin | 9f85949 | 2015-10-03 10:44:30 -0400 | [diff] [blame] | 2084 | int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2085 | |
David Benjamin | 63006a9 | 2015-10-18 00:00:06 -0400 | [diff] [blame] | 2086 | void SSL_set_shutdown(SSL *ssl, int mode) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2087 | // It is an error to clear any bits that have already been set. (We can't try |
| 2088 | // to get a second close_notify or send two.) |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2089 | assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl)); |
David Benjamin | 63006a9 | 2015-10-18 00:00:06 -0400 | [diff] [blame] | 2090 | |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2091 | if (mode & SSL_RECEIVED_SHUTDOWN && |
| 2092 | ssl->s3->recv_shutdown == ssl_shutdown_none) { |
| 2093 | ssl->s3->recv_shutdown = ssl_shutdown_close_notify; |
| 2094 | } |
| 2095 | |
| 2096 | if (mode & SSL_SENT_SHUTDOWN && |
| 2097 | ssl->s3->send_shutdown == ssl_shutdown_none) { |
| 2098 | ssl->s3->send_shutdown = ssl_shutdown_close_notify; |
| 2099 | } |
David Benjamin | 63006a9 | 2015-10-18 00:00:06 -0400 | [diff] [blame] | 2100 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2101 | |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2102 | int SSL_get_shutdown(const SSL *ssl) { |
| 2103 | int ret = 0; |
| 2104 | if (ssl->s3->recv_shutdown != ssl_shutdown_none) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2105 | // Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify |
| 2106 | // and fatal alert. |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2107 | ret |= SSL_RECEIVED_SHUTDOWN; |
| 2108 | } |
| 2109 | if (ssl->s3->send_shutdown == ssl_shutdown_close_notify) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2110 | // Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify. |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2111 | ret |= SSL_SENT_SHUTDOWN; |
| 2112 | } |
| 2113 | return ret; |
| 2114 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2115 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2116 | SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; } |
| 2117 | |
| 2118 | SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) { |
| 2119 | if (ssl->ctx == ctx) { |
| 2120 | return ssl->ctx; |
| 2121 | } |
| 2122 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2123 | // One cannot change the X.509 callbacks during a connection. |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 2124 | if (ssl->ctx->x509_method != ctx->x509_method) { |
| 2125 | assert(0); |
| 2126 | return NULL; |
| 2127 | } |
| 2128 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2129 | if (ctx == NULL) { |
David Benjamin | be49706 | 2017-03-10 16:08:36 -0500 | [diff] [blame] | 2130 | ctx = ssl->session_ctx; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2131 | } |
| 2132 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2133 | ssl_cert_free(ssl->cert); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2134 | ssl->cert = ssl_cert_dup(ctx->cert); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2135 | |
David Benjamin | f650c71 | 2016-12-22 15:54:51 -0500 | [diff] [blame] | 2136 | SSL_CTX_up_ref(ctx); |
| 2137 | SSL_CTX_free(ssl->ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2138 | ssl->ctx = ctx; |
| 2139 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2140 | return ssl->ctx; |
| 2141 | } |
| 2142 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2143 | void SSL_set_info_callback(SSL *ssl, |
David Benjamin | 8217024 | 2015-10-17 22:51:17 -0400 | [diff] [blame] | 2144 | void (*cb)(const SSL *ssl, int type, int value)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2145 | ssl->info_callback = cb; |
| 2146 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2147 | |
David Benjamin | 8217024 | 2015-10-17 22:51:17 -0400 | [diff] [blame] | 2148 | void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type, |
| 2149 | int value) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2150 | return ssl->info_callback; |
| 2151 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2152 | |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 2153 | int SSL_state(const SSL *ssl) { |
David Benjamin | 9e766d7 | 2017-02-10 23:14:17 -0500 | [diff] [blame] | 2154 | return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK; |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 2155 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2156 | |
David Benjamin | ece089c | 2015-05-15 23:52:42 -0400 | [diff] [blame] | 2157 | void SSL_set_state(SSL *ssl, int state) { } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2158 | |
Emily Stark | 95a79ee | 2016-02-25 21:12:28 -0800 | [diff] [blame] | 2159 | char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) { |
| 2160 | if (len <= 0) { |
| 2161 | return NULL; |
| 2162 | } |
| 2163 | buf[0] = '\0'; |
| 2164 | return buf; |
| 2165 | } |
| 2166 | |
David Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 2167 | int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, |
David Benjamin | d94682d | 2017-05-14 17:10:18 -0400 | [diff] [blame] | 2168 | CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 2169 | int index; |
| 2170 | if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp, |
David Benjamin | d94682d | 2017-05-14 17:10:18 -0400 | [diff] [blame] | 2171 | free_func)) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 2172 | return -1; |
| 2173 | } |
| 2174 | return index; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2175 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2176 | |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2177 | int SSL_set_ex_data(SSL *ssl, int idx, void *data) { |
| 2178 | return CRYPTO_set_ex_data(&ssl->ex_data, idx, data); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2179 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2180 | |
David Benjamin | 1ac08fe | 2015-09-15 01:36:22 -0400 | [diff] [blame] | 2181 | void *SSL_get_ex_data(const SSL *ssl, int idx) { |
| 2182 | return CRYPTO_get_ex_data(&ssl->ex_data, idx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2183 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2184 | |
David Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 2185 | int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused, |
David Benjamin | d94682d | 2017-05-14 17:10:18 -0400 | [diff] [blame] | 2186 | CRYPTO_EX_dup *dup_unused, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2187 | CRYPTO_EX_free *free_func) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 2188 | int index; |
| 2189 | if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp, |
David Benjamin | d94682d | 2017-05-14 17:10:18 -0400 | [diff] [blame] | 2190 | free_func)) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 2191 | return -1; |
| 2192 | } |
| 2193 | return index; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2194 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2195 | |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2196 | int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) { |
| 2197 | return CRYPTO_set_ex_data(&ctx->ex_data, idx, data); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2198 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2199 | |
David Benjamin | 1ac08fe | 2015-09-15 01:36:22 -0400 | [diff] [blame] | 2200 | void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) { |
| 2201 | return CRYPTO_get_ex_data(&ctx->ex_data, idx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2202 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2203 | |
David Benjamin | 93d1749 | 2015-10-17 12:43:36 -0400 | [diff] [blame] | 2204 | int SSL_want(const SSL *ssl) { return ssl->rwstate; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2205 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2206 | void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, |
| 2207 | RSA *(*cb)(SSL *ssl, int is_export, |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2208 | int keylength)) {} |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2209 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2210 | void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export, |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2211 | int keylength)) {} |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2212 | |
| 2213 | void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2214 | DH *(*cb)(SSL *ssl, int is_export, |
| 2215 | int keylength)) {} |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2216 | |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2217 | void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export, |
| 2218 | int keylength)) {} |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2219 | |
David Benjamin | 0a9bf66 | 2017-06-21 16:34:15 -0400 | [diff] [blame] | 2220 | static int use_psk_identity_hint(char **out, const char *identity_hint) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2221 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2222 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2223 | return 0; |
| 2224 | } |
| 2225 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2226 | // Clear currently configured hint, if any. |
David Benjamin | 0a9bf66 | 2017-06-21 16:34:15 -0400 | [diff] [blame] | 2227 | OPENSSL_free(*out); |
| 2228 | *out = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2229 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2230 | // Treat the empty hint as not supplying one. Plain PSK makes it possible to |
| 2231 | // send either no hint (omit ServerKeyExchange) or an empty hint, while |
| 2232 | // ECDHE_PSK can only spell empty hint. Having different capabilities is odd, |
| 2233 | // so we interpret empty and missing as identical. |
David Benjamin | 7867934 | 2016-09-16 19:42:05 -0400 | [diff] [blame] | 2234 | if (identity_hint != NULL && identity_hint[0] != '\0') { |
David Benjamin | 0a9bf66 | 2017-06-21 16:34:15 -0400 | [diff] [blame] | 2235 | *out = BUF_strdup(identity_hint); |
| 2236 | if (*out == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2237 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2238 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 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 | return 1; |
| 2242 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2243 | |
David Benjamin | 0a9bf66 | 2017-06-21 16:34:15 -0400 | [diff] [blame] | 2244 | int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) { |
| 2245 | return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint); |
| 2246 | } |
| 2247 | |
| 2248 | int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) { |
| 2249 | return use_psk_identity_hint(&ssl->psk_identity_hint, identity_hint); |
| 2250 | } |
| 2251 | |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2252 | const char *SSL_get_psk_identity_hint(const SSL *ssl) { |
| 2253 | if (ssl == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2254 | return NULL; |
| 2255 | } |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2256 | return ssl->psk_identity_hint; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2257 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2258 | |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2259 | const char *SSL_get_psk_identity(const SSL *ssl) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 2260 | if (ssl == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2261 | return NULL; |
| 2262 | } |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 2263 | SSL_SESSION *session = SSL_get_session(ssl); |
| 2264 | if (session == NULL) { |
| 2265 | return NULL; |
| 2266 | } |
| 2267 | return session->psk_identity; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2268 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2269 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2270 | void SSL_set_psk_client_callback( |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2271 | SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity, |
| 2272 | unsigned max_identity_len, uint8_t *psk, |
| 2273 | unsigned max_psk_len)) { |
| 2274 | ssl->psk_client_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2275 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2276 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2277 | void SSL_CTX_set_psk_client_callback( |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2278 | SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity, |
| 2279 | unsigned max_identity_len, uint8_t *psk, |
| 2280 | unsigned max_psk_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2281 | ctx->psk_client_callback = cb; |
| 2282 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2283 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2284 | void SSL_set_psk_server_callback( |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2285 | SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk, |
| 2286 | unsigned max_psk_len)) { |
| 2287 | ssl->psk_server_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2288 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2289 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2290 | void SSL_CTX_set_psk_server_callback( |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2291 | SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity, |
| 2292 | uint8_t *psk, unsigned max_psk_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2293 | ctx->psk_server_callback = cb; |
| 2294 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2295 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2296 | void SSL_CTX_set_msg_callback(SSL_CTX *ctx, |
| 2297 | void (*cb)(int write_p, int version, |
| 2298 | int content_type, const void *buf, |
| 2299 | size_t len, SSL *ssl, void *arg)) { |
David Benjamin | 59015c3 | 2015-04-26 13:13:55 -0400 | [diff] [blame] | 2300 | ctx->msg_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2301 | } |
David Benjamin | 61ecccf | 2015-05-05 09:44:51 -0400 | [diff] [blame] | 2302 | |
| 2303 | void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) { |
| 2304 | ctx->msg_callback_arg = arg; |
| 2305 | } |
| 2306 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2307 | void SSL_set_msg_callback(SSL *ssl, |
| 2308 | void (*cb)(int write_p, int version, int content_type, |
| 2309 | const void *buf, size_t len, SSL *ssl, |
| 2310 | void *arg)) { |
David Benjamin | 59015c3 | 2015-04-26 13:13:55 -0400 | [diff] [blame] | 2311 | ssl->msg_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2312 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2313 | |
David Benjamin | 61ecccf | 2015-05-05 09:44:51 -0400 | [diff] [blame] | 2314 | void SSL_set_msg_callback_arg(SSL *ssl, void *arg) { |
| 2315 | ssl->msg_callback_arg = arg; |
| 2316 | } |
| 2317 | |
David Benjamin | d28f59c | 2015-11-17 22:32:50 -0500 | [diff] [blame] | 2318 | void SSL_CTX_set_keylog_callback(SSL_CTX *ctx, |
| 2319 | void (*cb)(const SSL *ssl, const char *line)) { |
| 2320 | ctx->keylog_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2321 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2322 | |
David Benjamin | 6e3f5cc | 2016-09-14 14:43:14 -0400 | [diff] [blame] | 2323 | void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl, |
| 2324 | const char *line) { |
| 2325 | return ctx->keylog_callback; |
| 2326 | } |
| 2327 | |
Taylor Brandstetter | 9edb2c6 | 2016-06-08 15:26:59 -0700 | [diff] [blame] | 2328 | void SSL_CTX_set_current_time_cb(SSL_CTX *ctx, |
| 2329 | void (*cb)(const SSL *ssl, |
| 2330 | struct timeval *out_clock)) { |
| 2331 | ctx->current_time_cb = cb; |
| 2332 | } |
| 2333 | |
David Benjamin | 5d8b128 | 2015-10-17 23:26:35 -0400 | [diff] [blame] | 2334 | int SSL_is_init_finished(const SSL *ssl) { |
David Benjamin | 9e766d7 | 2017-02-10 23:14:17 -0500 | [diff] [blame] | 2335 | return !SSL_in_init(ssl); |
David Benjamin | 5d8b128 | 2015-10-17 23:26:35 -0400 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | int SSL_in_init(const SSL *ssl) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 2339 | return ssl->s3->hs != NULL; |
David Benjamin | 5d8b128 | 2015-10-17 23:26:35 -0400 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | int SSL_in_false_start(const SSL *ssl) { |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 2343 | if (ssl->s3->hs == NULL) { |
| 2344 | return 0; |
| 2345 | } |
| 2346 | return ssl->s3->hs->in_false_start; |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 2347 | } |
| 2348 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2349 | int SSL_cutthrough_complete(const SSL *ssl) { |
| 2350 | return SSL_in_false_start(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2351 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2352 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2353 | void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size, |
| 2354 | size_t *ssl_session_size) { |
| 2355 | *ssl_size = sizeof(SSL); |
| 2356 | *ssl_ctx_size = sizeof(SSL_CTX); |
| 2357 | *ssl_session_size = sizeof(SSL_SESSION); |
| 2358 | } |
Feng Lu | 41aa325 | 2014-11-21 22:47:56 -0800 | [diff] [blame] | 2359 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 2360 | int SSL_is_server(const SSL *ssl) { return ssl->server; } |
| 2361 | |
| 2362 | int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2363 | |
Alessandro Ghedini | 57e81e6 | 2017-03-14 23:36:00 +0000 | [diff] [blame] | 2364 | void SSL_CTX_set_select_certificate_cb( |
| 2365 | SSL_CTX *ctx, |
| 2366 | enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) { |
David Benjamin | d4c2bce | 2015-10-17 12:28:18 -0400 | [diff] [blame] | 2367 | ctx->select_certificate_cb = cb; |
| 2368 | } |
| 2369 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 2370 | void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx, |
| 2371 | int (*cb)(const SSL_CLIENT_HELLO *)) { |
Adam Langley | 524e717 | 2015-02-20 16:04:00 -0800 | [diff] [blame] | 2372 | ctx->dos_protection_cb = cb; |
| 2373 | } |
| 2374 | |
David Benjamin | 1d5ef3b | 2015-10-12 19:54:18 -0400 | [diff] [blame] | 2375 | void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) { |
| 2376 | ssl->renegotiate_mode = mode; |
| 2377 | } |
| 2378 | |
Adam Langley | c2d3280 | 2015-11-03 18:36:10 -0800 | [diff] [blame] | 2379 | int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv, |
| 2380 | const uint8_t **out_write_iv, size_t *out_iv_len) { |
Adam Langley | c2d3280 | 2015-11-03 18:36:10 -0800 | [diff] [blame] | 2381 | size_t write_iv_len; |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 2382 | if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) || |
| 2383 | !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) || |
Adam Langley | c2d3280 | 2015-11-03 18:36:10 -0800 | [diff] [blame] | 2384 | *out_iv_len != write_iv_len) { |
| 2385 | return 0; |
| 2386 | } |
| 2387 | |
| 2388 | return 1; |
David Benjamin | e348ff4 | 2015-11-06 17:55:30 -0500 | [diff] [blame] | 2389 | } |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 2390 | |
David Benjamin | 91b2501 | 2016-01-21 16:39:58 -0500 | [diff] [blame] | 2391 | static uint64_t be_to_u64(const uint8_t in[8]) { |
David Benjamin | de94238 | 2016-02-11 12:02:01 -0500 | [diff] [blame] | 2392 | return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) | |
| 2393 | (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) | |
| 2394 | (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) | |
| 2395 | (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]); |
David Benjamin | 91b2501 | 2016-01-21 16:39:58 -0500 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | uint64_t SSL_get_read_sequence(const SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2399 | // TODO(davidben): Internally represent sequence numbers as uint64_t. |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 2400 | if (SSL_is_dtls(ssl)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2401 | // max_seq_num already includes the epoch. |
David Benjamin | de94238 | 2016-02-11 12:02:01 -0500 | [diff] [blame] | 2402 | assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48)); |
| 2403 | return ssl->d1->bitmap.max_seq_num; |
| 2404 | } |
David Benjamin | 91b2501 | 2016-01-21 16:39:58 -0500 | [diff] [blame] | 2405 | return be_to_u64(ssl->s3->read_sequence); |
| 2406 | } |
| 2407 | |
| 2408 | uint64_t SSL_get_write_sequence(const SSL *ssl) { |
David Benjamin | de94238 | 2016-02-11 12:02:01 -0500 | [diff] [blame] | 2409 | uint64_t ret = be_to_u64(ssl->s3->write_sequence); |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 2410 | if (SSL_is_dtls(ssl)) { |
David Benjamin | de94238 | 2016-02-11 12:02:01 -0500 | [diff] [blame] | 2411 | assert((ret >> 48) == 0); |
| 2412 | ret |= ((uint64_t)ssl->d1->w_epoch) << 48; |
| 2413 | } |
| 2414 | return ret; |
David Benjamin | 91b2501 | 2016-01-21 16:39:58 -0500 | [diff] [blame] | 2415 | } |
| 2416 | |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 2417 | uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2418 | // TODO(davidben): This checks the wrong session if there is a renegotiation |
| 2419 | // in progress. |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 2420 | SSL_SESSION *session = SSL_get_session(ssl); |
| 2421 | if (session == NULL) { |
| 2422 | return 0; |
| 2423 | } |
| 2424 | |
| 2425 | return session->peer_signature_algorithm; |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 2426 | } |
| 2427 | |
David Benjamin | 96ba15f | 2015-12-25 14:10:17 -0500 | [diff] [blame] | 2428 | size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) { |
| 2429 | if (max_out == 0) { |
| 2430 | return sizeof(ssl->s3->client_random); |
| 2431 | } |
| 2432 | if (max_out > sizeof(ssl->s3->client_random)) { |
| 2433 | max_out = sizeof(ssl->s3->client_random); |
| 2434 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 2435 | OPENSSL_memcpy(out, ssl->s3->client_random, max_out); |
David Benjamin | 96ba15f | 2015-12-25 14:10:17 -0500 | [diff] [blame] | 2436 | return max_out; |
| 2437 | } |
| 2438 | |
| 2439 | size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) { |
| 2440 | if (max_out == 0) { |
| 2441 | return sizeof(ssl->s3->server_random); |
| 2442 | } |
| 2443 | if (max_out > sizeof(ssl->s3->server_random)) { |
| 2444 | max_out = sizeof(ssl->s3->server_random); |
| 2445 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 2446 | OPENSSL_memcpy(out, ssl->s3->server_random, max_out); |
David Benjamin | 96ba15f | 2015-12-25 14:10:17 -0500 | [diff] [blame] | 2447 | return max_out; |
| 2448 | } |
| 2449 | |
David Benjamin | f6494f4 | 2016-01-07 12:37:41 -0800 | [diff] [blame] | 2450 | const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) { |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 2451 | SSL_HANDSHAKE *hs = ssl->s3->hs; |
| 2452 | if (hs == NULL) { |
David Benjamin | f6494f4 | 2016-01-07 12:37:41 -0800 | [diff] [blame] | 2453 | return NULL; |
| 2454 | } |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 2455 | return hs->new_cipher; |
David Benjamin | f6494f4 | 2016-01-07 12:37:41 -0800 | [diff] [blame] | 2456 | } |
| 2457 | |
David Benjamin | bbaf367 | 2016-11-17 10:53:09 +0900 | [diff] [blame] | 2458 | void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) { |
| 2459 | ssl->retain_only_sha256_of_client_certs = !!enabled; |
| 2460 | } |
| 2461 | |
David Benjamin | cdd0b7e | 2016-01-07 10:10:41 -0800 | [diff] [blame] | 2462 | void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) { |
| 2463 | ctx->retain_only_sha256_of_client_certs = !!enabled; |
| 2464 | } |
| 2465 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2466 | void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) { |
| 2467 | ctx->grease_enabled = !!enabled; |
| 2468 | } |
| 2469 | |
David Benjamin | 35ac5b7 | 2017-03-03 15:05:56 -0500 | [diff] [blame] | 2470 | int32_t SSL_get_ticket_age_skew(const SSL *ssl) { |
| 2471 | return ssl->s3->ticket_age_skew; |
| 2472 | } |
| 2473 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 2474 | int SSL_clear(SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2475 | // In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously |
| 2476 | // established session to be offered the next time around. wpa_supplicant |
| 2477 | // depends on this behavior, so emulate it. |
David Benjamin | b79cc84 | 2016-12-07 15:57:14 -0500 | [diff] [blame] | 2478 | SSL_SESSION *session = NULL; |
| 2479 | if (!ssl->server && ssl->s3->established_session != NULL) { |
| 2480 | session = ssl->s3->established_session; |
| 2481 | SSL_SESSION_up_ref(session); |
| 2482 | } |
| 2483 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2484 | // TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and |
| 2485 | // |SSL_clear| because it is per-connection state rather than configuration |
| 2486 | // state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is |
| 2487 | // naturally reset at the right points between |SSL_new|, |SSL_clear|, and |
| 2488 | // |ssl3_new|. |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 2489 | |
| 2490 | ssl->rwstate = SSL_NOTHING; |
| 2491 | |
| 2492 | BUF_MEM_free(ssl->init_buf); |
| 2493 | ssl->init_buf = NULL; |
| 2494 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2495 | // The ssl->d1->mtu is simultaneously configuration (preserved across |
| 2496 | // clear) and connection-specific state (gets reset). |
| 2497 | // |
| 2498 | // TODO(davidben): Avoid this. |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 2499 | unsigned mtu = 0; |
| 2500 | if (ssl->d1 != NULL) { |
| 2501 | mtu = ssl->d1->mtu; |
| 2502 | } |
| 2503 | |
| 2504 | ssl->method->ssl_free(ssl); |
| 2505 | if (!ssl->method->ssl_new(ssl)) { |
David Benjamin | b79cc84 | 2016-12-07 15:57:14 -0500 | [diff] [blame] | 2506 | SSL_SESSION_free(session); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 2507 | return 0; |
| 2508 | } |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 2509 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 2510 | if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 2511 | ssl->d1->mtu = mtu; |
| 2512 | } |
| 2513 | |
David Benjamin | b79cc84 | 2016-12-07 15:57:14 -0500 | [diff] [blame] | 2514 | if (session != NULL) { |
| 2515 | SSL_set_session(ssl, session); |
| 2516 | SSL_SESSION_free(session); |
| 2517 | } |
| 2518 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 2519 | return 1; |
| 2520 | } |
| 2521 | |
David Benjamin | da881e9 | 2015-04-26 14:45:04 -0400 | [diff] [blame] | 2522 | int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; } |
| 2523 | int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; } |
| 2524 | int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; } |
| 2525 | int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; } |
| 2526 | int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; } |
| 2527 | int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; } |
| 2528 | int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; } |
| 2529 | int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; } |
| 2530 | int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; } |
| 2531 | int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; } |
| 2532 | int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; } |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2533 | |
| 2534 | int SSL_num_renegotiations(const SSL *ssl) { |
| 2535 | return SSL_total_renegotiations(ssl); |
| 2536 | } |
| 2537 | |
| 2538 | int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; } |
| 2539 | int SSL_need_tmp_RSA(const SSL *ssl) { return 0; } |
| 2540 | int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; } |
| 2541 | int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; } |
Matt Braithwaite | 4838d8a | 2015-08-20 13:20:03 -0700 | [diff] [blame] | 2542 | void ERR_load_SSL_strings(void) {} |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 2543 | void SSL_load_error_strings(void) {} |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2544 | int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); } |
| 2545 | |
| 2546 | int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) { |
| 2547 | if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) { |
| 2548 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 2549 | return 0; |
| 2550 | } |
| 2551 | int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)); |
| 2552 | return SSL_CTX_set1_curves(ctx, &nid, 1); |
| 2553 | } |
| 2554 | |
| 2555 | int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) { |
| 2556 | if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) { |
| 2557 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 2558 | return 0; |
| 2559 | } |
| 2560 | int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)); |
| 2561 | return SSL_set1_curves(ssl, &nid, 1); |
| 2562 | } |
David Benjamin | 721e8b7 | 2016-08-03 13:13:17 -0400 | [diff] [blame] | 2563 | |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 2564 | void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx, |
| 2565 | const SSL_TICKET_AEAD_METHOD *aead_method) { |
| 2566 | ctx->ticket_aead_method = aead_method; |
| 2567 | } |