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. |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 7 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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). |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 14 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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. |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 21 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 :-). |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 39 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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. |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 51 | * |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 65 | * notice, this list of conditions and the following disclaimer. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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. |
Adam Langley | e745b25 | 2018-02-26 14:02:17 -0800 | [diff] [blame] | 112 | * ECC cipher suite support in OpenSSL originally developed by |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | 879efc3 | 2017-09-21 11:20:53 -0400 | [diff] [blame] | 190 | bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out) { |
| 191 | uint8_t *ptr; |
| 192 | size_t len; |
| 193 | if (!CBB_finish(cbb, &ptr, &len)) { |
| 194 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 195 | return false; |
| 196 | } |
| 197 | out->Reset(ptr, len); |
| 198 | return true; |
| 199 | } |
| 200 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 201 | void ssl_reset_error_state(SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 202 | // Functions which use |SSL_get_error| must reset I/O and error state on |
| 203 | // entry. |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 204 | ssl->s3->rwstate = SSL_NOTHING; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 205 | ERR_clear_error(); |
| 206 | ERR_clear_system_error(); |
| 207 | } |
| 208 | |
David Benjamin | 31aad2d | 2017-10-06 16:04:06 -0400 | [diff] [blame] | 209 | void ssl_set_read_error(SSL* ssl) { |
| 210 | ssl->s3->read_shutdown = ssl_shutdown_error; |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 211 | ssl->s3->read_error.reset(ERR_save_state()); |
David Benjamin | 31aad2d | 2017-10-06 16:04:06 -0400 | [diff] [blame] | 212 | } |
| 213 | |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 214 | static bool check_read_error(const SSL *ssl) { |
| 215 | if (ssl->s3->read_shutdown == ssl_shutdown_error) { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 216 | ERR_restore_state(ssl->s3->read_error.get()); |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 217 | return false; |
| 218 | } |
| 219 | return true; |
| 220 | } |
| 221 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 222 | int ssl_can_write(const SSL *ssl) { |
| 223 | return !SSL_in_init(ssl) || ssl->s3->hs->can_early_write; |
| 224 | } |
| 225 | |
| 226 | int ssl_can_read(const SSL *ssl) { |
| 227 | return !SSL_in_init(ssl) || ssl->s3->hs->can_early_read; |
| 228 | } |
| 229 | |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 230 | ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed, |
| 231 | uint8_t *out_alert, Span<uint8_t> in) { |
| 232 | *out_consumed = 0; |
| 233 | if (!check_read_error(ssl)) { |
| 234 | *out_alert = 0; |
| 235 | return ssl_open_record_error; |
| 236 | } |
| 237 | auto ret = ssl->method->open_handshake(ssl, out_consumed, out_alert, in); |
| 238 | if (ret == ssl_open_record_error) { |
| 239 | ssl_set_read_error(ssl); |
| 240 | } |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed, |
| 245 | uint8_t *out_alert, |
| 246 | Span<uint8_t> in) { |
| 247 | *out_consumed = 0; |
| 248 | if (!check_read_error(ssl)) { |
| 249 | *out_alert = 0; |
| 250 | return ssl_open_record_error; |
| 251 | } |
| 252 | auto ret = |
| 253 | ssl->method->open_change_cipher_spec(ssl, out_consumed, out_alert, in); |
| 254 | if (ret == ssl_open_record_error) { |
| 255 | ssl_set_read_error(ssl); |
| 256 | } |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out, |
| 261 | size_t *out_consumed, uint8_t *out_alert, |
| 262 | Span<uint8_t> in) { |
| 263 | *out_consumed = 0; |
| 264 | if (!check_read_error(ssl)) { |
| 265 | *out_alert = 0; |
| 266 | return ssl_open_record_error; |
| 267 | } |
| 268 | auto ret = ssl->method->open_app_data(ssl, out, out_consumed, out_alert, in); |
| 269 | if (ret == ssl_open_record_error) { |
| 270 | ssl_set_read_error(ssl); |
| 271 | } |
| 272 | return ret; |
| 273 | } |
| 274 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 275 | void ssl_update_cache(SSL_HANDSHAKE *hs, int mode) { |
| 276 | SSL *const ssl = hs->ssl; |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 277 | SSL_CTX *ctx = ssl->session_ctx; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 278 | // Never cache sessions with empty session IDs. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 279 | if (ssl->s3->established_session->session_id_length == 0 || |
David Benjamin | 5ef40c6 | 2017-08-23 21:28:29 -0700 | [diff] [blame] | 280 | ssl->s3->established_session->not_resumable || |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 281 | (ctx->session_cache_mode & mode) != mode) { |
| 282 | return; |
| 283 | } |
| 284 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 285 | // Clients never use the internal session cache. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 286 | int use_internal_cache = ssl->server && !(ctx->session_cache_mode & |
| 287 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 288 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 289 | // A client may see new sessions on abbreviated handshakes if the server |
| 290 | // decides to renew the ticket. Once the handshake is completed, it should be |
| 291 | // inserted into the cache. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 292 | if (ssl->s3->established_session.get() != ssl->session || |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 293 | (!ssl->server && hs->ticket_expected)) { |
| 294 | if (use_internal_cache) { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 295 | SSL_CTX_add_session(ctx, ssl->s3->established_session.get()); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 296 | } |
| 297 | if (ctx->new_session_cb != NULL) { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 298 | SSL_SESSION_up_ref(ssl->s3->established_session.get()); |
| 299 | if (!ctx->new_session_cb(ssl, ssl->s3->established_session.get())) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 300 | // |new_session_cb|'s return value signals whether it took ownership. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 301 | SSL_SESSION_free(ssl->s3->established_session.get()); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (use_internal_cache && |
| 307 | !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 308 | // Automatically flush the internal session cache every 255 connections. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 309 | int flush_cache = 0; |
| 310 | CRYPTO_MUTEX_lock_write(&ctx->lock); |
| 311 | ctx->handshakes_since_cache_flush++; |
| 312 | if (ctx->handshakes_since_cache_flush >= 255) { |
| 313 | flush_cache = 1; |
| 314 | ctx->handshakes_since_cache_flush = 0; |
| 315 | } |
| 316 | CRYPTO_MUTEX_unlock_write(&ctx->lock); |
| 317 | |
| 318 | if (flush_cache) { |
| 319 | struct OPENSSL_timeval now; |
| 320 | ssl_get_current_time(ssl, &now); |
| 321 | SSL_CTX_flush_sessions(ctx, now.tv_sec); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) { |
| 327 | static const char hextable[] = "0123456789abcdef"; |
| 328 | uint8_t *out; |
| 329 | |
| 330 | if (!CBB_add_space(cbb, &out, in_len * 2)) { |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | for (size_t i = 0; i < in_len; i++) { |
| 335 | *(out++) = (uint8_t)hextable[in[i] >> 4]; |
| 336 | *(out++) = (uint8_t)hextable[in[i] & 0xf]; |
| 337 | } |
| 338 | |
| 339 | return 1; |
| 340 | } |
| 341 | |
| 342 | int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret, |
| 343 | size_t secret_len) { |
| 344 | if (ssl->ctx->keylog_callback == NULL) { |
| 345 | return 1; |
| 346 | } |
| 347 | |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 348 | ScopedCBB cbb; |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 349 | uint8_t *out; |
| 350 | size_t out_len; |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 351 | 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] | 352 | secret_len * 2 + 1) || |
David Benjamin | 1386aad | 2017-07-19 23:57:40 -0400 | [diff] [blame] | 353 | !CBB_add_bytes(cbb.get(), (const uint8_t *)label, strlen(label)) || |
| 354 | !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) || |
| 355 | !cbb_add_hex(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
| 356 | !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) || |
| 357 | !cbb_add_hex(cbb.get(), secret, secret_len) || |
| 358 | !CBB_add_u8(cbb.get(), 0 /* NUL */) || |
| 359 | !CBB_finish(cbb.get(), &out, &out_len)) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | ssl->ctx->keylog_callback(ssl, (const char *)out); |
| 364 | OPENSSL_free(out); |
| 365 | return 1; |
| 366 | } |
| 367 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 368 | void ssl_do_info_callback(const SSL *ssl, int type, int value) { |
| 369 | void (*cb)(const SSL *ssl, int type, int value) = NULL; |
| 370 | if (ssl->info_callback != NULL) { |
| 371 | cb = ssl->info_callback; |
| 372 | } else if (ssl->ctx->info_callback != NULL) { |
| 373 | cb = ssl->ctx->info_callback; |
| 374 | } |
| 375 | |
| 376 | if (cb != NULL) { |
| 377 | cb(ssl, type, value); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type, |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 382 | Span<const uint8_t> in) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 383 | if (ssl->msg_callback == NULL) { |
| 384 | return; |
| 385 | } |
| 386 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 387 | // |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for |
| 388 | // a V2ClientHello. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 389 | int version; |
| 390 | switch (content_type) { |
| 391 | case 0: |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 392 | // V2ClientHello |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 393 | version = SSL2_VERSION; |
| 394 | break; |
| 395 | case SSL3_RT_HEADER: |
| 396 | version = 0; |
| 397 | break; |
| 398 | default: |
| 399 | version = SSL_version(ssl); |
| 400 | } |
| 401 | |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 402 | ssl->msg_callback(is_write, version, content_type, in.data(), in.size(), ssl, |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 403 | ssl->msg_callback_arg); |
| 404 | } |
| 405 | |
| 406 | 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] | 407 | // TODO(martinkr): Change callers to |ssl_ctx_get_current_time| and drop the |
| 408 | // |ssl| arg from |current_time_cb| if possible. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 409 | ssl_ctx_get_current_time(ssl->ctx, out_clock); |
| 410 | } |
| 411 | |
| 412 | void ssl_ctx_get_current_time(const SSL_CTX *ctx, |
| 413 | struct OPENSSL_timeval *out_clock) { |
| 414 | if (ctx->current_time_cb != NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 415 | // TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See |
| 416 | // https://crbug.com/boringssl/155. |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 417 | struct timeval clock; |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 418 | ctx->current_time_cb(nullptr /* ssl */, &clock); |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 419 | if (clock.tv_sec < 0) { |
| 420 | assert(0); |
| 421 | out_clock->tv_sec = 0; |
| 422 | out_clock->tv_usec = 0; |
| 423 | } else { |
| 424 | out_clock->tv_sec = (uint64_t)clock.tv_sec; |
| 425 | out_clock->tv_usec = (uint32_t)clock.tv_usec; |
| 426 | } |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | #if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE) |
| 431 | out_clock->tv_sec = 1234; |
| 432 | out_clock->tv_usec = 1234; |
| 433 | #elif defined(OPENSSL_WINDOWS) |
| 434 | struct _timeb time; |
| 435 | _ftime(&time); |
| 436 | if (time.time < 0) { |
| 437 | assert(0); |
| 438 | out_clock->tv_sec = 0; |
| 439 | out_clock->tv_usec = 0; |
| 440 | } else { |
| 441 | out_clock->tv_sec = time.time; |
| 442 | out_clock->tv_usec = time.millitm * 1000; |
| 443 | } |
| 444 | #else |
| 445 | struct timeval clock; |
| 446 | gettimeofday(&clock, NULL); |
| 447 | if (clock.tv_sec < 0) { |
| 448 | assert(0); |
| 449 | out_clock->tv_sec = 0; |
| 450 | out_clock->tv_usec = 0; |
| 451 | } else { |
| 452 | out_clock->tv_sec = (uint64_t)clock.tv_sec; |
| 453 | out_clock->tv_usec = (uint32_t)clock.tv_usec; |
| 454 | } |
| 455 | #endif |
| 456 | } |
| 457 | |
Adam Langley | 3fe8fa7 | 2018-01-26 09:14:30 -0800 | [diff] [blame] | 458 | void SSL_CTX_set_handoff_mode(SSL_CTX *ctx, bool on) { |
| 459 | ctx->handoff = on; |
| 460 | } |
| 461 | |
David Benjamin | 71666cb | 2018-05-01 17:14:27 -0400 | [diff] [blame] | 462 | static bool ssl_can_renegotiate(const SSL *ssl) { |
| 463 | if (ssl->server || SSL_is_dtls(ssl)) { |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | // We do not accept at SSL 3.0. SSL 3.0 will be removed entirely in the future |
| 468 | // and requires retaining more data for renegotiation_info. |
| 469 | uint16_t version = ssl_protocol_version(ssl); |
| 470 | if (version == SSL3_VERSION || version >= TLS1_3_VERSION) { |
| 471 | return false; |
| 472 | } |
| 473 | |
| 474 | // The config has already been shed. |
| 475 | if (!ssl->config) { |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | switch (ssl->renegotiate_mode) { |
| 480 | case ssl_renegotiate_ignore: |
| 481 | case ssl_renegotiate_never: |
| 482 | return false; |
| 483 | |
| 484 | case ssl_renegotiate_freely: |
| 485 | return true; |
| 486 | case ssl_renegotiate_once: |
| 487 | return ssl->s3->total_renegotiations == 0; |
| 488 | } |
| 489 | |
| 490 | assert(0); |
| 491 | return false; |
| 492 | } |
| 493 | |
| 494 | static void ssl_maybe_shed_handshake_config(SSL *ssl) { |
| 495 | if (ssl->s3->hs != nullptr || |
| 496 | ssl->config == nullptr || |
| 497 | !ssl->config->shed_handshake_config || |
| 498 | ssl_can_renegotiate(ssl)) { |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | Delete(ssl->config); |
| 503 | ssl->config = nullptr; |
| 504 | } |
| 505 | |
Matthew Braithwaite | e30fac6 | 2018-04-05 16:50:21 -0700 | [diff] [blame^] | 506 | void SSL_set_handoff_mode(SSL *ssl, bool on) { |
| 507 | if (!ssl->config) { |
| 508 | return; |
| 509 | } |
| 510 | ssl->config->handoff = on; |
| 511 | } |
| 512 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 513 | } // namespace bssl |
| 514 | |
| 515 | using namespace bssl; |
| 516 | |
David Benjamin | 7c6d09b | 2015-09-05 11:00:19 -0400 | [diff] [blame] | 517 | int SSL_library_init(void) { |
| 518 | CRYPTO_library_init(); |
| 519 | return 1; |
| 520 | } |
| 521 | |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 522 | int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) { |
| 523 | CRYPTO_library_init(); |
| 524 | return 1; |
| 525 | } |
| 526 | |
Adam Langley | e976e43 | 2016-02-15 14:01:10 -0800 | [diff] [blame] | 527 | static uint32_t ssl_session_hash(const SSL_SESSION *sess) { |
| 528 | const uint8_t *session_id = sess->session_id; |
| 529 | |
| 530 | uint8_t tmp_storage[sizeof(uint32_t)]; |
| 531 | if (sess->session_id_length < sizeof(tmp_storage)) { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 532 | OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage)); |
| 533 | OPENSSL_memcpy(tmp_storage, sess->session_id, sess->session_id_length); |
Adam Langley | e976e43 | 2016-02-15 14:01:10 -0800 | [diff] [blame] | 534 | session_id = tmp_storage; |
| 535 | } |
| 536 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 537 | uint32_t hash = |
Adam Langley | e976e43 | 2016-02-15 14:01:10 -0800 | [diff] [blame] | 538 | ((uint32_t)session_id[0]) | |
| 539 | ((uint32_t)session_id[1] << 8) | |
| 540 | ((uint32_t)session_id[2] << 16) | |
| 541 | ((uint32_t)session_id[3] << 24); |
David Benjamin | 7c6d09b | 2015-09-05 11:00:19 -0400 | [diff] [blame] | 542 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 543 | return hash; |
| 544 | } |
| 545 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 546 | // NB: If this function (or indeed the hash function which uses a sort of |
| 547 | // coarser function than this one) is changed, ensure |
| 548 | // SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being |
| 549 | // able to construct an SSL_SESSION that will collide with any existing session |
| 550 | // with a matching session ID. |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 551 | static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) { |
| 552 | if (a->ssl_version != b->ssl_version) { |
| 553 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 554 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 555 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 556 | if (a->session_id_length != b->session_id_length) { |
| 557 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 558 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 559 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 560 | 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] | 561 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 562 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 563 | SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) { |
| 564 | SSL_CTX *ret = NULL; |
| 565 | |
| 566 | if (method == NULL) { |
| 567 | OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED); |
| 568 | return NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 569 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 570 | |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 571 | ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX)); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 572 | if (ret == NULL) { |
| 573 | goto err; |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame] | 574 | } |
| 575 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 576 | OPENSSL_memset(ret, 0, sizeof(SSL_CTX)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 577 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 578 | ret->method = method->method; |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 579 | ret->x509_method = method->x509_method; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 580 | |
| 581 | CRYPTO_MUTEX_init(&ret->lock); |
| 582 | |
| 583 | ret->session_cache_mode = SSL_SESS_CACHE_SERVER; |
| 584 | ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; |
| 585 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 586 | ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 587 | ret->session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 588 | |
| 589 | ret->references = 1; |
| 590 | |
| 591 | ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT; |
| 592 | ret->verify_mode = SSL_VERIFY_NONE; |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 593 | ret->cert = New<CERT>(method->x509_method); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 594 | if (ret->cert == NULL) { |
| 595 | goto err; |
| 596 | } |
| 597 | |
| 598 | ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp); |
| 599 | if (ret->sessions == NULL) { |
| 600 | goto err; |
| 601 | } |
Adam Langley | 2a3b343 | 2017-02-10 13:56:48 -0800 | [diff] [blame] | 602 | |
| 603 | if (!ret->x509_method->ssl_ctx_new(ret)) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 604 | goto err; |
| 605 | } |
| 606 | |
Matthew Braithwaite | 6ad20dc | 2017-02-21 16:41:33 -0800 | [diff] [blame] | 607 | if (!SSL_CTX_set_strict_cipher_list(ret, SSL_DEFAULT_CIPHER_LIST)) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 608 | goto err2; |
| 609 | } |
| 610 | |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 611 | ret->client_CA = sk_CRYPTO_BUFFER_new_null(); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 612 | if (ret->client_CA == NULL) { |
| 613 | goto err; |
| 614 | } |
| 615 | |
David Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 616 | CRYPTO_new_ex_data(&ret->ex_data); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 617 | |
| 618 | ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
| 619 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 620 | // Disable the auto-chaining feature by default. Once this has stuck without |
| 621 | // problems, the feature will be removed entirely. |
David Benjamin | a983b4c | 2016-11-09 14:21:12 -0500 | [diff] [blame] | 622 | ret->mode = SSL_MODE_NO_AUTO_CHAIN; |
| 623 | |
David Benjamin | e28552d | 2018-04-08 13:59:25 -0400 | [diff] [blame] | 624 | ret->rsa_pss_rsae_certs_enabled = true; |
| 625 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 626 | // Lock the SSL_CTX to the specified version, for compatibility with legacy |
David Benjamin | c03ecb9 | 2018-02-02 16:07:50 -0500 | [diff] [blame] | 627 | // uses of SSL_METHOD. |
David Benjamin | e34bcc9 | 2016-09-21 16:53:09 -0400 | [diff] [blame] | 628 | if (!SSL_CTX_set_max_proto_version(ret, method->version) || |
David Benjamin | c03ecb9 | 2018-02-02 16:07:50 -0500 | [diff] [blame] | 629 | !SSL_CTX_set_min_proto_version(ret, method->version)) { |
David Benjamin | e34bcc9 | 2016-09-21 16:53:09 -0400 | [diff] [blame] | 630 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 631 | goto err2; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | return ret; |
| 635 | |
| 636 | err: |
| 637 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 638 | err2: |
| 639 | SSL_CTX_free(ret); |
| 640 | return NULL; |
| 641 | } |
| 642 | |
Adam Langley | 84cd159 | 2016-06-30 18:29:44 -0700 | [diff] [blame] | 643 | int SSL_CTX_up_ref(SSL_CTX *ctx) { |
| 644 | CRYPTO_refcount_inc(&ctx->references); |
| 645 | return 1; |
| 646 | } |
| 647 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 648 | void SSL_CTX_free(SSL_CTX *ctx) { |
| 649 | if (ctx == NULL || |
| 650 | !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) { |
| 651 | return; |
| 652 | } |
| 653 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 654 | // Free internal session cache. However: the remove_cb() may reference the |
| 655 | // ex_data of SSL_CTX, thus the ex_data store can only be removed after the |
| 656 | // sessions were flushed. As the ex_data handling routines might also touch |
| 657 | // the session cache, the most secure solution seems to be: empty (flush) the |
| 658 | // cache, then free ex_data, then finally free the cache. (See ticket |
| 659 | // [openssl.org #212].) |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 660 | SSL_CTX_flush_sessions(ctx, 0); |
| 661 | |
| 662 | CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data); |
| 663 | |
| 664 | CRYPTO_MUTEX_cleanup(&ctx->lock); |
| 665 | lh_SSL_SESSION_free(ctx->sessions); |
David Benjamin | 48b276d | 2018-04-12 17:37:32 -0400 | [diff] [blame] | 666 | Delete(ctx->cipher_list); |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 667 | Delete(ctx->cert); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 668 | sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions, |
| 669 | SSL_CUSTOM_EXTENSION_free); |
| 670 | sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions, |
| 671 | SSL_CUSTOM_EXTENSION_free); |
Adam Langley | 34b4c82 | 2017-02-02 10:57:17 -0800 | [diff] [blame] | 672 | sk_CRYPTO_BUFFER_pop_free(ctx->client_CA, CRYPTO_BUFFER_free); |
Adam Langley | 2a3b343 | 2017-02-10 13:56:48 -0800 | [diff] [blame] | 673 | ctx->x509_method->ssl_ctx_free(ctx); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 674 | sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles); |
| 675 | OPENSSL_free(ctx->psk_identity_hint); |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 676 | OPENSSL_free(ctx->supported_group_list); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 677 | OPENSSL_free(ctx->alpn_client_proto_list); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 678 | EVP_PKEY_free(ctx->tlsext_channel_id_private); |
David Benjamin | 71c21b4 | 2017-04-14 17:05:40 -0400 | [diff] [blame] | 679 | OPENSSL_free(ctx->verify_sigalgs); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 680 | OPENSSL_free(ctx->tlsext_ticket_key_current); |
| 681 | OPENSSL_free(ctx->tlsext_ticket_key_prev); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 682 | |
| 683 | OPENSSL_free(ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 684 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 685 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 686 | SSL *SSL_new(SSL_CTX *ctx) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 687 | if (ctx == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 688 | OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 689 | return NULL; |
| 690 | } |
| 691 | if (ctx->method == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 692 | 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] | 693 | return NULL; |
| 694 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 695 | |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 696 | SSL *ssl = (SSL *)OPENSSL_malloc(sizeof(SSL)); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 697 | if (ssl == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 698 | goto err; |
| 699 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 700 | OPENSSL_memset(ssl, 0, sizeof(SSL)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 701 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 702 | ssl->config = New<SSL_CONFIG>(ssl); |
| 703 | if (ssl->config == nullptr) { |
| 704 | goto err; |
| 705 | } |
| 706 | ssl->config->conf_min_version = ctx->conf_min_version; |
| 707 | ssl->config->conf_max_version = ctx->conf_max_version; |
Steven Valdez | 52586f9 | 2017-07-11 15:08:32 -0400 | [diff] [blame] | 708 | ssl->tls13_variant = ctx->tls13_variant; |
David Benjamin | 1eb367c | 2014-12-12 18:17:51 -0500 | [diff] [blame] | 709 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 710 | // RFC 6347 states that implementations SHOULD use an initial timer value of |
| 711 | // 1 second. |
Taylor Brandstetter | 376a0fe | 2016-05-10 19:30:28 -0700 | [diff] [blame] | 712 | ssl->initial_timeout_duration_ms = 1000; |
| 713 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 714 | ssl->options = ctx->options; |
| 715 | ssl->mode = ctx->mode; |
| 716 | ssl->max_cert_list = ctx->max_cert_list; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 717 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 718 | ssl->config->cert = ssl_cert_dup(ctx->cert).release(); |
| 719 | if (ssl->config->cert == NULL) { |
David Benjamin | a5a3eeb | 2015-03-18 20:26:30 -0400 | [diff] [blame] | 720 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 721 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 722 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 723 | ssl->msg_callback = ctx->msg_callback; |
| 724 | ssl->msg_callback_arg = ctx->msg_callback_arg; |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 725 | ssl->config->verify_mode = ctx->verify_mode; |
| 726 | ssl->config->verify_callback = ctx->default_verify_callback; |
| 727 | ssl->config->custom_verify_callback = ctx->custom_verify_callback; |
| 728 | ssl->config->retain_only_sha256_of_client_certs = |
David Benjamin | bbaf367 | 2016-11-17 10:53:09 +0900 | [diff] [blame] | 729 | ctx->retain_only_sha256_of_client_certs; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 730 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 731 | ssl->quiet_shutdown = ctx->quiet_shutdown; |
| 732 | ssl->max_send_fragment = ctx->max_send_fragment; |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 733 | ssl->enable_early_data = ctx->enable_early_data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 734 | |
David Benjamin | f650c71 | 2016-12-22 15:54:51 -0500 | [diff] [blame] | 735 | SSL_CTX_up_ref(ctx); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 736 | ssl->ctx = ctx; |
David Benjamin | f650c71 | 2016-12-22 15:54:51 -0500 | [diff] [blame] | 737 | SSL_CTX_up_ref(ctx); |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 738 | ssl->session_ctx = ctx; |
Adam Langley | 2a3b343 | 2017-02-10 13:56:48 -0800 | [diff] [blame] | 739 | |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 740 | if (ctx->supported_group_list) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 741 | ssl->config->supported_group_list = (uint16_t *)BUF_memdup( |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 742 | ctx->supported_group_list, ctx->supported_group_list_len * 2); |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 743 | if (!ssl->config->supported_group_list) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 744 | goto err; |
| 745 | } |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 746 | ssl->config->supported_group_list_len = ctx->supported_group_list_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 747 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 748 | |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 749 | if (ctx->alpn_client_proto_list) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 750 | ssl->config->alpn_client_proto_list = (uint8_t *)BUF_memdup( |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 751 | ctx->alpn_client_proto_list, ctx->alpn_client_proto_list_len); |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 752 | if (ssl->config->alpn_client_proto_list == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 753 | goto err; |
| 754 | } |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 755 | ssl->config->alpn_client_proto_list_len = ctx->alpn_client_proto_list_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 756 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 757 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 758 | ssl->method = ctx->method; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 759 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 760 | if (!ssl->method->ssl_new(ssl)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 761 | goto err; |
| 762 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 763 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 764 | if (!ssl->ctx->x509_method->ssl_new(ssl->s3->hs.get())) { |
| 765 | goto err; |
| 766 | } |
| 767 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 768 | CRYPTO_new_ex_data(&ssl->ex_data); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 769 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 770 | if (ctx->psk_identity_hint) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 771 | ssl->config->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint); |
| 772 | if (ssl->config->psk_identity_hint == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 773 | goto err; |
| 774 | } |
| 775 | } |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 776 | ssl->config->psk_client_callback = ctx->psk_client_callback; |
| 777 | ssl->config->psk_server_callback = ctx->psk_server_callback; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 778 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 779 | ssl->config->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled; |
David Benjamin | 02ddbfd | 2015-01-11 13:09:11 -0500 | [diff] [blame] | 780 | if (ctx->tlsext_channel_id_private) { |
Adam Langley | 310d3f6 | 2016-07-12 10:39:20 -0700 | [diff] [blame] | 781 | EVP_PKEY_up_ref(ctx->tlsext_channel_id_private); |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 782 | ssl->config->tlsext_channel_id_private = ctx->tlsext_channel_id_private; |
David Benjamin | 02ddbfd | 2015-01-11 13:09:11 -0500 | [diff] [blame] | 783 | } |
| 784 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 785 | ssl->config->signed_cert_timestamps_enabled = |
| 786 | ctx->signed_cert_timestamps_enabled; |
| 787 | ssl->config->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled; |
| 788 | ssl->config->handoff = ctx->handoff; |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 789 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 790 | return ssl; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 791 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 792 | err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 793 | SSL_free(ssl); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 794 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 795 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 796 | return NULL; |
| 797 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 798 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 799 | SSL_CONFIG::SSL_CONFIG(SSL *ssl_arg) |
| 800 | : ssl(ssl_arg), |
| 801 | signed_cert_timestamps_enabled(false), |
| 802 | ocsp_stapling_enabled(false), |
| 803 | tlsext_channel_id_enabled(false), |
| 804 | retain_only_sha256_of_client_certs(false), |
| 805 | handoff(false), |
| 806 | shed_handshake_config(false) { |
| 807 | assert(ssl); |
| 808 | } |
| 809 | |
| 810 | SSL_CONFIG::~SSL_CONFIG() { |
| 811 | if (ssl->ctx != nullptr) { |
| 812 | ssl->ctx->x509_method->ssl_config_free(this); |
| 813 | } |
| 814 | Delete(cipher_list); |
| 815 | Delete(cert); |
David Benjamin | b95d4b4 | 2018-05-02 16:11:34 -0400 | [diff] [blame] | 816 | OPENSSL_free(psk_identity_hint); |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 817 | OPENSSL_free(supported_group_list); |
David Benjamin | b95d4b4 | 2018-05-02 16:11:34 -0400 | [diff] [blame] | 818 | EVP_PKEY_free(tlsext_channel_id_private); |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 819 | OPENSSL_free(alpn_client_proto_list); |
| 820 | OPENSSL_free(token_binding_params); |
| 821 | OPENSSL_free(quic_transport_params); |
David Benjamin | b95d4b4 | 2018-05-02 16:11:34 -0400 | [diff] [blame] | 822 | sk_SRTP_PROTECTION_PROFILE_free(srtp_profiles); |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 823 | sk_CRYPTO_BUFFER_pop_free(client_CA, CRYPTO_BUFFER_free); |
| 824 | } |
| 825 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 826 | void SSL_free(SSL *ssl) { |
| 827 | if (ssl == NULL) { |
| 828 | return; |
| 829 | } |
| 830 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 831 | CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data); |
| 832 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 833 | BIO_free_all(ssl->rbio); |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 834 | BIO_free_all(ssl->wbio); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 835 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 836 | Delete(ssl->config); |
| 837 | ssl->config = NULL; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 838 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 839 | SSL_SESSION_free(ssl->session); |
| 840 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 841 | OPENSSL_free(ssl->tlsext_hostname); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 842 | |
| 843 | if (ssl->method != NULL) { |
| 844 | ssl->method->ssl_free(ssl); |
| 845 | } |
| 846 | SSL_CTX_free(ssl->ctx); |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 847 | SSL_CTX_free(ssl->session_ctx); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 848 | |
| 849 | OPENSSL_free(ssl); |
| 850 | } |
| 851 | |
| 852 | void SSL_set_connect_state(SSL *ssl) { |
David Benjamin | 7e58c5e | 2017-10-11 13:01:08 -0400 | [diff] [blame] | 853 | ssl->server = false; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 854 | ssl->do_handshake = ssl_client_handshake; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | void SSL_set_accept_state(SSL *ssl) { |
David Benjamin | 7e58c5e | 2017-10-11 13:01:08 -0400 | [diff] [blame] | 858 | ssl->server = true; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 859 | ssl->do_handshake = ssl_server_handshake; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 860 | } |
| 861 | |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 862 | void SSL_set0_rbio(SSL *ssl, BIO *rbio) { |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 863 | BIO_free_all(ssl->rbio); |
| 864 | ssl->rbio = rbio; |
| 865 | } |
| 866 | |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 867 | void SSL_set0_wbio(SSL *ssl, BIO *wbio) { |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 868 | BIO_free_all(ssl->wbio); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 869 | ssl->wbio = wbio; |
| 870 | } |
| 871 | |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 872 | void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 873 | // For historical reasons, this function has many different cases in ownership |
| 874 | // handling. |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 875 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 876 | // If nothing has changed, do nothing |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 877 | if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) { |
| 878 | return; |
| 879 | } |
| 880 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 881 | // If the two arguments are equal, one fewer reference is granted than |
| 882 | // taken. |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 883 | if (rbio != NULL && rbio == wbio) { |
| 884 | BIO_up_ref(rbio); |
| 885 | } |
| 886 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 887 | // If only the wbio is changed, adopt only one reference. |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 888 | if (rbio == SSL_get_rbio(ssl)) { |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 889 | SSL_set0_wbio(ssl, wbio); |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 890 | return; |
| 891 | } |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 892 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 893 | // There is an asymmetry here for historical reasons. If only the rbio is |
| 894 | // changed AND the rbio and wbio were originally different, then we only adopt |
| 895 | // one reference. |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 896 | if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) { |
| 897 | SSL_set0_rbio(ssl, rbio); |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 898 | return; |
| 899 | } |
| 900 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 901 | // Otherwise, adopt both references. |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 902 | SSL_set0_rbio(ssl, rbio); |
| 903 | SSL_set0_wbio(ssl, wbio); |
David Benjamin | f715c42 | 2016-06-11 19:01:56 -0400 | [diff] [blame] | 904 | } |
| 905 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 906 | BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; } |
| 907 | |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 908 | BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; } |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 909 | |
David Benjamin | 2be4aa7 | 2017-01-02 07:56:51 -0500 | [diff] [blame] | 910 | int SSL_do_handshake(SSL *ssl) { |
| 911 | ssl_reset_error_state(ssl); |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 912 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 913 | if (ssl->do_handshake == NULL) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 914 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET); |
| 915 | return -1; |
| 916 | } |
| 917 | |
| 918 | if (!SSL_in_init(ssl)) { |
| 919 | return 1; |
| 920 | } |
| 921 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 922 | // Run the handshake. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 923 | SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 924 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 925 | bool early_return = false; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 926 | int ret = ssl_run_handshake(hs, &early_return); |
| 927 | ssl_do_info_callback( |
| 928 | ssl, ssl->server ? SSL_CB_ACCEPT_EXIT : SSL_CB_CONNECT_EXIT, ret); |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 929 | if (ret <= 0) { |
| 930 | return ret; |
| 931 | } |
| 932 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 933 | // Destroy the handshake object if the handshake has completely finished. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 934 | if (!early_return) { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 935 | ssl->s3->hs.reset(); |
David Benjamin | 71666cb | 2018-05-01 17:14:27 -0400 | [diff] [blame] | 936 | ssl_maybe_shed_handshake_config(ssl); |
David Benjamin | ce8c9d2 | 2016-11-14 10:45:16 +0900 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | return 1; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | int SSL_connect(SSL *ssl) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 943 | if (ssl->do_handshake == NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 944 | // Not properly initialized yet |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 945 | SSL_set_connect_state(ssl); |
| 946 | } |
| 947 | |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 948 | return SSL_do_handshake(ssl); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | int SSL_accept(SSL *ssl) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 952 | if (ssl->do_handshake == NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 953 | // Not properly initialized yet |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 954 | SSL_set_accept_state(ssl); |
| 955 | } |
| 956 | |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 957 | return SSL_do_handshake(ssl); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 958 | } |
| 959 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 960 | static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 961 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 962 | return tls13_post_handshake(ssl, msg); |
| 963 | } |
| 964 | |
David Benjamin | 71666cb | 2018-05-01 17:14:27 -0400 | [diff] [blame] | 965 | // Check for renegotiation on the server before parsing to use the correct |
| 966 | // error. Renegotiation is triggered by a different message for servers. |
| 967 | if (ssl->server) { |
| 968 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION); |
| 969 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION); |
| 970 | return 0; |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 971 | } |
| 972 | |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 973 | if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) { |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 974 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 975 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST); |
| 976 | return 0; |
| 977 | } |
| 978 | |
David Benjamin | 71666cb | 2018-05-01 17:14:27 -0400 | [diff] [blame] | 979 | if (ssl->renegotiate_mode == ssl_renegotiate_ignore) { |
| 980 | return 1; // Ignore the HelloRequest. |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 981 | } |
| 982 | |
David Benjamin | 71666cb | 2018-05-01 17:14:27 -0400 | [diff] [blame] | 983 | if (!ssl_can_renegotiate(ssl) || |
| 984 | // Renegotiation is only supported at quiescent points in the application |
| 985 | // protocol, namely in HTTPS, just before reading the HTTP response. |
| 986 | // Require the record-layer be idle and avoid complexities of sending a |
| 987 | // handshake record while an application_data record is being written. |
| 988 | !ssl->s3->write_buffer.empty() || |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 989 | ssl->s3->write_shutdown != ssl_shutdown_none) { |
David Benjamin | 71666cb | 2018-05-01 17:14:27 -0400 | [diff] [blame] | 990 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION); |
| 991 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION); |
| 992 | return 0; |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 993 | } |
| 994 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 995 | // Begin a new handshake. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 996 | if (ssl->s3->hs != nullptr) { |
David Benjamin | 2644a13 | 2016-12-11 13:41:17 -0500 | [diff] [blame] | 997 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 998 | return 0; |
| 999 | } |
| 1000 | ssl->s3->hs = ssl_handshake_new(ssl); |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1001 | if (ssl->s3->hs == nullptr) { |
David Benjamin | 2644a13 | 2016-12-11 13:41:17 -0500 | [diff] [blame] | 1002 | return 0; |
| 1003 | } |
| 1004 | |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 1005 | ssl->s3->total_renegotiations++; |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 1006 | return 1; |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 1007 | } |
| 1008 | |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1009 | static int ssl_read_impl(SSL *ssl) { |
David Benjamin | 2be4aa7 | 2017-01-02 07:56:51 -0500 | [diff] [blame] | 1010 | ssl_reset_error_state(ssl); |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 1011 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1012 | if (ssl->do_handshake == NULL) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1013 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 1014 | return -1; |
| 1015 | } |
| 1016 | |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 1017 | // Replay post-handshake message errors. |
| 1018 | if (!check_read_error(ssl)) { |
| 1019 | return -1; |
| 1020 | } |
| 1021 | |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1022 | while (ssl->s3->pending_app_data.empty()) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1023 | // Complete the current handshake, if any. False Start will cause |
| 1024 | // |SSL_do_handshake| to return mid-handshake, so this may require multiple |
| 1025 | // iterations. |
Steven Valdez | 681eb6a | 2016-12-19 13:19:29 -0500 | [diff] [blame] | 1026 | while (!ssl_can_read(ssl)) { |
David Benjamin | 163f29a | 2016-07-28 11:05:58 -0400 | [diff] [blame] | 1027 | int ret = SSL_do_handshake(ssl); |
| 1028 | if (ret < 0) { |
| 1029 | return ret; |
| 1030 | } |
| 1031 | if (ret == 0) { |
| 1032 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1033 | return -1; |
| 1034 | } |
| 1035 | } |
| 1036 | |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 1037 | // Process any buffered post-handshake messages. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1038 | SSLMessage msg; |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 1039 | if (ssl->method->get_message(ssl, &msg)) { |
Steven Valdez | cd8470f | 2017-10-11 12:29:36 -0400 | [diff] [blame] | 1040 | // If we received an interrupt in early read (EndOfEarlyData), loop again |
| 1041 | // for the handshake to process it. |
| 1042 | if (SSL_in_init(ssl)) { |
| 1043 | ssl->s3->hs->can_early_read = false; |
| 1044 | continue; |
| 1045 | } |
| 1046 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1047 | // Handle the post-handshake message and try again. |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1048 | if (!ssl_do_post_handshake(ssl, msg)) { |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 1049 | ssl_set_read_error(ssl); |
David Benjamin | 7934f08 | 2017-08-01 16:32:25 -0400 | [diff] [blame] | 1050 | return -1; |
| 1051 | } |
| 1052 | ssl->method->next_message(ssl); |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 1053 | continue; // Loop again. We may have begun a new handshake. |
David Benjamin | c79845c | 2016-03-10 01:28:00 -0500 | [diff] [blame] | 1054 | } |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 1055 | |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1056 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 1057 | size_t consumed = 0; |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 1058 | auto ret = ssl_open_app_data(ssl, &ssl->s3->pending_app_data, &consumed, |
David Benjamin | ea712e3 | 2017-10-13 16:50:39 -0400 | [diff] [blame] | 1059 | &alert, ssl->s3->read_buffer.span()); |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1060 | bool retry; |
| 1061 | int bio_ret = ssl_handle_open_record(ssl, &retry, ret, consumed, alert); |
| 1062 | if (bio_ret <= 0) { |
| 1063 | return bio_ret; |
| 1064 | } |
| 1065 | if (!retry) { |
| 1066 | assert(!ssl->s3->pending_app_data.empty()); |
David Benjamin | 40e9470 | 2017-10-06 18:26:36 -0400 | [diff] [blame] | 1067 | ssl->s3->key_update_count = 0; |
| 1068 | } |
David Benjamin | c79845c | 2016-03-10 01:28:00 -0500 | [diff] [blame] | 1069 | } |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1070 | |
| 1071 | return 1; |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | int SSL_read(SSL *ssl, void *buf, int num) { |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1075 | int ret = SSL_peek(ssl, buf, num); |
| 1076 | if (ret <= 0) { |
| 1077 | return ret; |
| 1078 | } |
| 1079 | // TODO(davidben): In DTLS, should the rest of the record be discarded? DTLS |
| 1080 | // is not a stream. See https://crbug.com/boringssl/65. |
| 1081 | ssl->s3->pending_app_data = |
| 1082 | ssl->s3->pending_app_data.subspan(static_cast<size_t>(ret)); |
| 1083 | if (ssl->s3->pending_app_data.empty()) { |
David Benjamin | ea712e3 | 2017-10-13 16:50:39 -0400 | [diff] [blame] | 1084 | ssl->s3->read_buffer.DiscardConsumed(); |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1085 | } |
| 1086 | return ret; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | int SSL_peek(SSL *ssl, void *buf, int num) { |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1090 | int ret = ssl_read_impl(ssl); |
| 1091 | if (ret <= 0) { |
| 1092 | return ret; |
| 1093 | } |
| 1094 | if (num <= 0) { |
| 1095 | return num; |
| 1096 | } |
| 1097 | size_t todo = |
| 1098 | std::min(ssl->s3->pending_app_data.size(), static_cast<size_t>(num)); |
| 1099 | OPENSSL_memcpy(buf, ssl->s3->pending_app_data.data(), todo); |
| 1100 | return static_cast<int>(todo); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | int SSL_write(SSL *ssl, const void *buf, int num) { |
David Benjamin | 2be4aa7 | 2017-01-02 07:56:51 -0500 | [diff] [blame] | 1104 | ssl_reset_error_state(ssl); |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 1105 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1106 | if (ssl->do_handshake == NULL) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1107 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 1108 | return -1; |
| 1109 | } |
| 1110 | |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 1111 | if (ssl->s3->write_shutdown != ssl_shutdown_none) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1112 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 1113 | return -1; |
| 1114 | } |
| 1115 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 1116 | int ret = 0; |
| 1117 | bool needs_handshake = false; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1118 | do { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1119 | // If necessary, complete the handshake implicitly. |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1120 | if (!ssl_can_write(ssl)) { |
| 1121 | ret = SSL_do_handshake(ssl); |
| 1122 | if (ret < 0) { |
| 1123 | return ret; |
| 1124 | } |
| 1125 | if (ret == 0) { |
| 1126 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1127 | return -1; |
| 1128 | } |
David Benjamin | d7ac143 | 2016-03-10 00:41:25 -0500 | [diff] [blame] | 1129 | } |
David Benjamin | d7ac143 | 2016-03-10 00:41:25 -0500 | [diff] [blame] | 1130 | |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1131 | ret = ssl->method->write_app_data(ssl, &needs_handshake, |
| 1132 | (const uint8_t *)buf, num); |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1133 | } while (needs_handshake); |
| 1134 | return ret; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | int SSL_shutdown(SSL *ssl) { |
David Benjamin | 2be4aa7 | 2017-01-02 07:56:51 -0500 | [diff] [blame] | 1138 | ssl_reset_error_state(ssl); |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 1139 | |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1140 | if (ssl->do_handshake == NULL) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1141 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED); |
| 1142 | return -1; |
| 1143 | } |
| 1144 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1145 | // If we are in the middle of a handshake, silently succeed. Consumers often |
| 1146 | // call this function before |SSL_free|, whether the handshake succeeded or |
| 1147 | // not. We assume the caller has already handled failed handshakes. |
Steven Valdez | a14934f | 2016-02-29 10:05:08 -0500 | [diff] [blame] | 1148 | if (SSL_in_init(ssl)) { |
David Benjamin | 12709db | 2017-02-17 13:51:50 -0500 | [diff] [blame] | 1149 | return 1; |
Steven Valdez | a14934f | 2016-02-29 10:05:08 -0500 | [diff] [blame] | 1150 | } |
| 1151 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1152 | if (ssl->quiet_shutdown) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1153 | // Do nothing if configured not to send a close_notify. |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 1154 | ssl->s3->write_shutdown = ssl_shutdown_close_notify; |
| 1155 | ssl->s3->read_shutdown = ssl_shutdown_close_notify; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1156 | return 1; |
| 1157 | } |
| 1158 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1159 | // This function completes in two stages. It sends a close_notify and then it |
| 1160 | // waits for a close_notify to come in. Perform exactly one action and return |
| 1161 | // whether or not it succeeds. |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1162 | |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 1163 | if (ssl->s3->write_shutdown != ssl_shutdown_close_notify) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1164 | // Send a close_notify. |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1165 | if (ssl_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) { |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 1166 | return -1; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1167 | } |
| 1168 | } else if (ssl->s3->alert_dispatch) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1169 | // Finish sending the close_notify. |
David Benjamin | f0ee907 | 2016-06-15 17:44:37 -0400 | [diff] [blame] | 1170 | if (ssl->method->dispatch_alert(ssl) <= 0) { |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 1171 | return -1; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1172 | } |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 1173 | } else if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) { |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1174 | if (SSL_is_dtls(ssl)) { |
| 1175 | // Bidirectional shutdown doesn't make sense for an unordered |
| 1176 | // transport. DTLS alerts also aren't delivered reliably, so we may even |
| 1177 | // time out because the peer never received our close_notify. Report to |
| 1178 | // the caller that the channel has fully shut down. |
| 1179 | if (ssl->s3->read_shutdown == ssl_shutdown_error) { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1180 | ERR_restore_state(ssl->s3->read_error.get()); |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1181 | return -1; |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 1182 | } |
David Benjamin | a031b61 | 2017-10-11 20:48:25 -0400 | [diff] [blame] | 1183 | ssl->s3->read_shutdown = ssl_shutdown_close_notify; |
| 1184 | } else { |
| 1185 | // Keep discarding data until we see a close_notify. |
| 1186 | for (;;) { |
| 1187 | ssl->s3->pending_app_data = Span<uint8_t>(); |
| 1188 | int ret = ssl_read_impl(ssl); |
| 1189 | if (ret <= 0) { |
| 1190 | break; |
| 1191 | } |
| 1192 | } |
| 1193 | if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) { |
| 1194 | return -1; |
| 1195 | } |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1199 | // Return 0 for unidirectional shutdown and 1 for bidirectional shutdown. |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 1200 | return ssl->s3->read_shutdown == ssl_shutdown_close_notify; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1201 | } |
| 1202 | |
David Benjamin | 1d4f4c0 | 2016-07-26 18:03:08 -0400 | [diff] [blame] | 1203 | int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) { |
| 1204 | if (ssl->s3->alert_dispatch) { |
| 1205 | if (ssl->s3->send_alert[0] != SSL3_AL_FATAL || |
| 1206 | ssl->s3->send_alert[1] != alert) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1207 | // We are already attempting to write a different alert. |
David Benjamin | 1d4f4c0 | 2016-07-26 18:03:08 -0400 | [diff] [blame] | 1208 | OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 1209 | return -1; |
| 1210 | } |
| 1211 | return ssl->method->dispatch_alert(ssl); |
| 1212 | } |
| 1213 | |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1214 | return ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
David Benjamin | 1d4f4c0 | 2016-07-26 18:03:08 -0400 | [diff] [blame] | 1215 | } |
| 1216 | |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 1217 | int SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params, |
| 1218 | size_t params_len) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1219 | if (!ssl->config) { |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 1220 | return 0; |
| 1221 | } |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1222 | ssl->config->quic_transport_params = |
| 1223 | (uint8_t *)BUF_memdup(params, params_len); |
| 1224 | if (!ssl->config->quic_transport_params) { |
| 1225 | return 0; |
| 1226 | } |
| 1227 | ssl->config->quic_transport_params_len = params_len; |
Nick Harper | 3c034b2 | 2017-12-22 15:50:43 -0800 | [diff] [blame] | 1228 | return 1; |
| 1229 | } |
| 1230 | |
| 1231 | void SSL_get_peer_quic_transport_params(const SSL *ssl, |
| 1232 | const uint8_t **out_params, |
| 1233 | size_t *out_params_len) { |
| 1234 | *out_params = ssl->s3->peer_quic_transport_params.data(); |
| 1235 | *out_params_len = ssl->s3->peer_quic_transport_params.size(); |
| 1236 | } |
| 1237 | |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 1238 | void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1239 | ctx->enable_early_data = !!enabled; |
Alessandro Ghedini | 67bb45f | 2017-03-30 16:33:24 -0500 | [diff] [blame] | 1240 | } |
| 1241 | |
Steven Valdez | 520e122 | 2017-06-13 12:45:25 -0400 | [diff] [blame] | 1242 | void SSL_CTX_set_tls13_variant(SSL_CTX *ctx, enum tls13_variant_t variant) { |
| 1243 | ctx->tls13_variant = variant; |
| 1244 | } |
| 1245 | |
Steven Valdez | 52586f9 | 2017-07-11 15:08:32 -0400 | [diff] [blame] | 1246 | void SSL_set_tls13_variant(SSL *ssl, enum tls13_variant_t variant) { |
| 1247 | ssl->tls13_variant = variant; |
| 1248 | } |
| 1249 | |
Alessandro Ghedini | 67bb45f | 2017-03-30 16:33:24 -0500 | [diff] [blame] | 1250 | void SSL_set_early_data_enabled(SSL *ssl, int enabled) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1251 | ssl->enable_early_data = !!enabled; |
Steven Valdez | 08b65f4 | 2016-12-07 15:29:45 -0500 | [diff] [blame] | 1252 | } |
| 1253 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1254 | int SSL_in_early_data(const SSL *ssl) { |
| 1255 | if (ssl->s3->hs == NULL) { |
| 1256 | return 0; |
| 1257 | } |
| 1258 | return ssl->s3->hs->in_early_data; |
| 1259 | } |
| 1260 | |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1261 | int SSL_early_data_accepted(const SSL *ssl) { |
David Benjamin | 02e6256 | 2017-12-18 18:04:01 -0500 | [diff] [blame] | 1262 | return ssl->s3->early_data_accepted; |
Steven Valdez | 2d85062 | 2017-01-11 11:34:52 -0500 | [diff] [blame] | 1263 | } |
| 1264 | |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1265 | void SSL_reset_early_data_reject(SSL *ssl) { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1266 | SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1267 | if (hs == NULL || |
| 1268 | hs->wait != ssl_hs_early_data_rejected) { |
| 1269 | abort(); |
| 1270 | } |
| 1271 | |
| 1272 | hs->wait = ssl_hs_ok; |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 1273 | hs->in_early_data = false; |
David Benjamin | 31b0c9b | 2017-07-20 14:49:15 -0400 | [diff] [blame] | 1274 | hs->early_session.reset(); |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1275 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1276 | // Discard any unfinished writes from the perspective of |SSL_write|'s |
| 1277 | // retry. The handshake will transparently flush out the pending record |
| 1278 | // (discarded by the server) to keep the framing correct. |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 1279 | ssl->s3->wpend_pending = false; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1280 | } |
| 1281 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1282 | static int bio_retry_reason_to_error(int reason) { |
| 1283 | switch (reason) { |
| 1284 | case BIO_RR_CONNECT: |
| 1285 | return SSL_ERROR_WANT_CONNECT; |
| 1286 | case BIO_RR_ACCEPT: |
| 1287 | return SSL_ERROR_WANT_ACCEPT; |
| 1288 | default: |
| 1289 | return SSL_ERROR_SYSCALL; |
| 1290 | } |
| 1291 | } |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1292 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1293 | int SSL_get_error(const SSL *ssl, int ret_code) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1294 | if (ret_code > 0) { |
| 1295 | return SSL_ERROR_NONE; |
| 1296 | } |
| 1297 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1298 | // Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc, |
| 1299 | // where we do encode the error |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1300 | uint32_t err = ERR_peek_error(); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1301 | if (err != 0) { |
| 1302 | if (ERR_GET_LIB(err) == ERR_LIB_SYS) { |
| 1303 | return SSL_ERROR_SYSCALL; |
| 1304 | } |
| 1305 | return SSL_ERROR_SSL; |
| 1306 | } |
| 1307 | |
| 1308 | if (ret_code == 0) { |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 1309 | if (ssl->s3->read_shutdown == ssl_shutdown_close_notify) { |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1310 | return SSL_ERROR_ZERO_RETURN; |
| 1311 | } |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1312 | // An EOF was observed which violates the protocol, and the underlying |
| 1313 | // transport does not participate in the error queue. Bubble up to the |
| 1314 | // caller. |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1315 | return SSL_ERROR_SYSCALL; |
| 1316 | } |
| 1317 | |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 1318 | switch (ssl->s3->rwstate) { |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1319 | case SSL_PENDING_SESSION: |
| 1320 | return SSL_ERROR_PENDING_SESSION; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1321 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1322 | case SSL_CERTIFICATE_SELECTION_PENDING: |
| 1323 | return SSL_ERROR_PENDING_CERTIFICATE; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1324 | |
Adam Langley | 3fe8fa7 | 2018-01-26 09:14:30 -0800 | [diff] [blame] | 1325 | case SSL_HANDOFF: |
| 1326 | return SSL_ERROR_HANDOFF; |
| 1327 | |
Matthew Braithwaite | 56986f9 | 2018-03-22 11:48:33 -0700 | [diff] [blame] | 1328 | case SSL_HANDBACK: |
| 1329 | return SSL_ERROR_HANDBACK; |
| 1330 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1331 | case SSL_READING: { |
| 1332 | BIO *bio = SSL_get_rbio(ssl); |
| 1333 | if (BIO_should_read(bio)) { |
| 1334 | return SSL_ERROR_WANT_READ; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1335 | } |
| 1336 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1337 | if (BIO_should_write(bio)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1338 | // TODO(davidben): OpenSSL historically checked for writes on the read |
| 1339 | // BIO. Can this be removed? |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1340 | return SSL_ERROR_WANT_WRITE; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1341 | } |
| 1342 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1343 | if (BIO_should_io_special(bio)) { |
| 1344 | return bio_retry_reason_to_error(BIO_get_retry_reason(bio)); |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1345 | } |
| 1346 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1347 | break; |
| 1348 | } |
| 1349 | |
| 1350 | case SSL_WRITING: { |
| 1351 | BIO *bio = SSL_get_wbio(ssl); |
| 1352 | if (BIO_should_write(bio)) { |
| 1353 | return SSL_ERROR_WANT_WRITE; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1354 | } |
| 1355 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1356 | if (BIO_should_read(bio)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1357 | // TODO(davidben): OpenSSL historically checked for reads on the write |
| 1358 | // BIO. Can this be removed? |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1359 | return SSL_ERROR_WANT_READ; |
| 1360 | } |
| 1361 | |
| 1362 | if (BIO_should_io_special(bio)) { |
| 1363 | return bio_retry_reason_to_error(BIO_get_retry_reason(bio)); |
| 1364 | } |
| 1365 | |
| 1366 | break; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1367 | } |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1368 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1369 | case SSL_X509_LOOKUP: |
| 1370 | return SSL_ERROR_WANT_X509_LOOKUP; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1371 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1372 | case SSL_CHANNEL_ID_LOOKUP: |
| 1373 | return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1374 | |
David Benjamin | 287fc4f | 2016-12-10 15:12:03 -0500 | [diff] [blame] | 1375 | case SSL_PRIVATE_KEY_OPERATION: |
| 1376 | return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION; |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 1377 | |
| 1378 | case SSL_PENDING_TICKET: |
| 1379 | return SSL_ERROR_PENDING_TICKET; |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 1380 | |
| 1381 | case SSL_EARLY_DATA_REJECTED: |
| 1382 | return SSL_ERROR_EARLY_DATA_REJECTED; |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 1383 | |
| 1384 | case SSL_CERTIFICATE_VERIFY: |
| 1385 | return SSL_ERROR_WANT_CERTIFICATE_VERIFY; |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | return SSL_ERROR_SYSCALL; |
| 1389 | } |
| 1390 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 1391 | uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) { |
| 1392 | ctx->options |= options; |
| 1393 | return ctx->options; |
| 1394 | } |
| 1395 | |
| 1396 | uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) { |
| 1397 | ctx->options &= ~options; |
| 1398 | return ctx->options; |
| 1399 | } |
| 1400 | |
| 1401 | uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; } |
| 1402 | |
| 1403 | uint32_t SSL_set_options(SSL *ssl, uint32_t options) { |
| 1404 | ssl->options |= options; |
| 1405 | return ssl->options; |
| 1406 | } |
| 1407 | |
| 1408 | uint32_t SSL_clear_options(SSL *ssl, uint32_t options) { |
| 1409 | ssl->options &= ~options; |
| 1410 | return ssl->options; |
| 1411 | } |
| 1412 | |
| 1413 | uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; } |
| 1414 | |
| 1415 | uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) { |
| 1416 | ctx->mode |= mode; |
| 1417 | return ctx->mode; |
| 1418 | } |
| 1419 | |
| 1420 | uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) { |
| 1421 | ctx->mode &= ~mode; |
| 1422 | return ctx->mode; |
| 1423 | } |
| 1424 | |
| 1425 | uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; } |
| 1426 | |
| 1427 | uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) { |
| 1428 | ssl->mode |= mode; |
| 1429 | return ssl->mode; |
| 1430 | } |
| 1431 | |
| 1432 | uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) { |
| 1433 | ssl->mode &= ~mode; |
| 1434 | return ssl->mode; |
| 1435 | } |
| 1436 | |
| 1437 | uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; } |
| 1438 | |
Adam Langley | d519bf6 | 2016-12-12 11:16:44 -0800 | [diff] [blame] | 1439 | void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) { |
| 1440 | ctx->pool = pool; |
| 1441 | } |
| 1442 | |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1443 | int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len, |
| 1444 | size_t max_out) { |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1445 | *out_len = 0; |
| 1446 | OPENSSL_memset(out, 0, max_out); |
| 1447 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1448 | // 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] | 1449 | if (!ssl->s3->initial_handshake_complete || |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1450 | ssl_protocol_version(ssl) < TLS1_VERSION || |
| 1451 | ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1452 | return 0; |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1453 | } |
| 1454 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1455 | // The tls-unique value is the first Finished message in the handshake, which |
| 1456 | // is the client's in a full handshake and the server's for a resumption. See |
| 1457 | // https://tools.ietf.org/html/rfc5929#section-3.1. |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1458 | const uint8_t *finished = ssl->s3->previous_client_finished; |
| 1459 | size_t finished_len = ssl->s3->previous_client_finished_len; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1460 | if (ssl->session != NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1461 | // tls-unique is broken for resumed sessions unless EMS is used. |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1462 | if (!ssl->session->extended_master_secret) { |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1463 | return 0; |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1464 | } |
| 1465 | finished = ssl->s3->previous_server_finished; |
| 1466 | finished_len = ssl->s3->previous_server_finished_len; |
| 1467 | } |
| 1468 | |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1469 | *out_len = finished_len; |
| 1470 | if (finished_len > max_out) { |
| 1471 | *out_len = max_out; |
| 1472 | } |
| 1473 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1474 | OPENSSL_memcpy(out, finished, *out_len); |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1475 | return 1; |
David Benjamin | ee0c827 | 2015-09-13 01:03:54 -0400 | [diff] [blame] | 1476 | } |
| 1477 | |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 1478 | 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] | 1479 | size_t sid_ctx_len) { |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 1480 | if (sid_ctx_len > sizeof(cert->sid_ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1481 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1482 | return 0; |
| 1483 | } |
David Benjamin | 30c4c30 | 2016-12-07 22:35:24 -0500 | [diff] [blame] | 1484 | |
David Benjamin | a3d76d0 | 2017-07-14 19:36:07 -0400 | [diff] [blame] | 1485 | static_assert(sizeof(cert->sid_ctx) < 256, "sid_ctx too large"); |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 1486 | cert->sid_ctx_length = (uint8_t)sid_ctx_len; |
| 1487 | OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1488 | return 1; |
| 1489 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1490 | |
David Benjamin | 5960a90 | 2017-02-14 20:07:11 -0500 | [diff] [blame] | 1491 | int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx, |
| 1492 | size_t sid_ctx_len) { |
| 1493 | return set_session_id_context(ctx->cert, sid_ctx, sid_ctx_len); |
| 1494 | } |
| 1495 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1496 | 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] | 1497 | size_t sid_ctx_len) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1498 | if (!ssl->config) { |
| 1499 | return 0; |
| 1500 | } |
| 1501 | return set_session_id_context(ssl->config->cert, sid_ctx, sid_ctx_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1502 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1503 | |
David Benjamin | 07820b5 | 2016-12-07 21:13:41 -0500 | [diff] [blame] | 1504 | const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1505 | if (!ssl->config) { |
| 1506 | assert(ssl->config); |
| 1507 | *out_len = 0; |
| 1508 | return NULL; |
| 1509 | } |
| 1510 | *out_len = ssl->config->cert->sid_ctx_length; |
| 1511 | return ssl->config->cert->sid_ctx; |
David Benjamin | 07820b5 | 2016-12-07 21:13:41 -0500 | [diff] [blame] | 1512 | } |
| 1513 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1514 | void SSL_certs_clear(SSL *ssl) { |
| 1515 | if (!ssl->config) { |
| 1516 | return; |
| 1517 | } |
| 1518 | ssl_cert_clear_certs(ssl->config->cert); |
| 1519 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1520 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1521 | int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1522 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1523 | int SSL_get_rfd(const SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1524 | int ret = -1; |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1525 | BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR); |
| 1526 | if (b != NULL) { |
| 1527 | BIO_get_fd(b, &ret); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1528 | } |
| 1529 | return ret; |
| 1530 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1531 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1532 | int SSL_get_wfd(const SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1533 | int ret = -1; |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1534 | BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR); |
| 1535 | if (b != NULL) { |
| 1536 | BIO_get_fd(b, &ret); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1537 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1538 | return ret; |
| 1539 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1540 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1541 | int SSL_set_fd(SSL *ssl, int fd) { |
David Benjamin | 22ce9b2 | 2016-03-06 19:26:52 -0500 | [diff] [blame] | 1542 | BIO *bio = BIO_new(BIO_s_socket()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1543 | if (bio == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1544 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1545 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1546 | } |
| 1547 | BIO_set_fd(bio, fd, BIO_NOCLOSE); |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1548 | SSL_set_bio(ssl, bio, bio); |
| 1549 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1550 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1551 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1552 | int SSL_set_wfd(SSL *ssl, int fd) { |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 1553 | BIO *rbio = SSL_get_rbio(ssl); |
| 1554 | if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET || |
| 1555 | BIO_get_fd(rbio, NULL) != fd) { |
David Benjamin | 22ce9b2 | 2016-03-06 19:26:52 -0500 | [diff] [blame] | 1556 | BIO *bio = BIO_new(BIO_s_socket()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1557 | if (bio == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1558 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1559 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1560 | } |
| 1561 | BIO_set_fd(bio, fd, BIO_NOCLOSE); |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 1562 | SSL_set0_wbio(ssl, bio); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1563 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1564 | // Copy the rbio over to the wbio. |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 1565 | BIO_up_ref(rbio); |
| 1566 | SSL_set0_wbio(ssl, rbio); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1567 | } |
| 1568 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1569 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1570 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1571 | |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1572 | int SSL_set_rfd(SSL *ssl, int fd) { |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 1573 | BIO *wbio = SSL_get_wbio(ssl); |
| 1574 | if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET || |
| 1575 | BIO_get_fd(wbio, NULL) != fd) { |
David Benjamin | 22ce9b2 | 2016-03-06 19:26:52 -0500 | [diff] [blame] | 1576 | BIO *bio = BIO_new(BIO_s_socket()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1577 | if (bio == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1578 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1579 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1580 | } |
| 1581 | BIO_set_fd(bio, fd, BIO_NOCLOSE); |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 1582 | SSL_set0_rbio(ssl, bio); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1583 | } else { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1584 | // Copy the wbio over to the rbio. |
David Benjamin | 4501bd5 | 2016-08-01 13:39:41 -0400 | [diff] [blame] | 1585 | BIO_up_ref(wbio); |
| 1586 | SSL_set0_rbio(ssl, wbio); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1587 | } |
David Benjamin | 066fe0a | 2015-10-17 21:11:33 -0400 | [diff] [blame] | 1588 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1589 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1590 | |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1591 | static size_t copy_finished(void *out, size_t out_len, const uint8_t *in, |
| 1592 | size_t in_len) { |
| 1593 | if (out_len > in_len) { |
| 1594 | out_len = in_len; |
| 1595 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1596 | OPENSSL_memcpy(out, in, out_len); |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1597 | return in_len; |
| 1598 | } |
| 1599 | |
David Benjamin | 1a1b34d | 2015-10-17 12:51:52 -0400 | [diff] [blame] | 1600 | 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] | 1601 | if (!ssl->s3->initial_handshake_complete || |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1602 | ssl_protocol_version(ssl) < TLS1_VERSION || |
| 1603 | ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1604 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1605 | } |
| 1606 | |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1607 | if (ssl->server) { |
| 1608 | return copy_finished(buf, count, ssl->s3->previous_server_finished, |
| 1609 | ssl->s3->previous_server_finished_len); |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1610 | } |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1611 | |
| 1612 | return copy_finished(buf, count, ssl->s3->previous_client_finished, |
| 1613 | ssl->s3->previous_client_finished_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1614 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1615 | |
David Benjamin | 1a1b34d | 2015-10-17 12:51:52 -0400 | [diff] [blame] | 1616 | 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] | 1617 | if (!ssl->s3->initial_handshake_complete || |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1618 | ssl_protocol_version(ssl) < TLS1_VERSION || |
| 1619 | ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1620 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1621 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1622 | |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1623 | if (ssl->server) { |
| 1624 | return copy_finished(buf, count, ssl->s3->previous_client_finished, |
| 1625 | ssl->s3->previous_client_finished_len); |
David Benjamin | ced00b4 | 2016-10-08 11:24:09 -0400 | [diff] [blame] | 1626 | } |
David Benjamin | 49ddf41 | 2016-10-08 11:56:01 -0400 | [diff] [blame] | 1627 | |
| 1628 | return copy_finished(buf, count, ssl->s3->previous_server_finished, |
| 1629 | ssl->s3->previous_server_finished_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1630 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1631 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1632 | int SSL_get_verify_mode(const SSL *ssl) { |
| 1633 | if (!ssl->config) { |
| 1634 | assert(ssl->config); |
| 1635 | return -1; |
| 1636 | } |
| 1637 | return ssl->config->verify_mode; |
| 1638 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1639 | |
David Benjamin | 42fea37 | 2015-09-19 01:22:44 -0400 | [diff] [blame] | 1640 | int SSL_get_extms_support(const SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1641 | // TLS 1.3 does not require extended master secret and always reports as |
| 1642 | // supporting it. |
David Benjamin | a68c118 | 2016-07-24 00:14:33 -0400 | [diff] [blame] | 1643 | if (!ssl->s3->have_version) { |
| 1644 | return 0; |
| 1645 | } |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1646 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 1647 | return 1; |
| 1648 | } |
| 1649 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1650 | // If the initial handshake completed, query the established session. |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 1651 | if (ssl->s3->established_session != NULL) { |
| 1652 | return ssl->s3->established_session->extended_master_secret; |
| 1653 | } |
| 1654 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1655 | // Otherwise, query the in-progress handshake. |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 1656 | if (ssl->s3->hs != NULL) { |
| 1657 | return ssl->s3->hs->extended_master_secret; |
| 1658 | } |
| 1659 | assert(0); |
| 1660 | return 0; |
Matt Braithwaite | cd6f54b | 2015-09-17 12:54:42 -0700 | [diff] [blame] | 1661 | } |
| 1662 | |
David Benjamin | 9a41d1b | 2015-05-16 01:30:09 -0400 | [diff] [blame] | 1663 | int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1664 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1665 | int SSL_get_read_ahead(const SSL *ssl) { return 0; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1666 | |
David Benjamin | 9a41d1b | 2015-05-16 01:30:09 -0400 | [diff] [blame] | 1667 | void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { } |
David Benjamin | 61ecccf | 2015-05-05 09:44:51 -0400 | [diff] [blame] | 1668 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1669 | void SSL_set_read_ahead(SSL *ssl, int yes) { } |
David Benjamin | 61ecccf | 2015-05-05 09:44:51 -0400 | [diff] [blame] | 1670 | |
David Benjamin | 9f85949 | 2015-10-03 10:44:30 -0400 | [diff] [blame] | 1671 | int SSL_pending(const SSL *ssl) { |
David Benjamin | d9229f9 | 2017-10-06 17:36:20 -0400 | [diff] [blame] | 1672 | return static_cast<int>(ssl->s3->pending_app_data.size()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1673 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1674 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1675 | // Fix this so it checks all the valid key/cert options |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1676 | int SSL_CTX_check_private_key(const SSL_CTX *ctx) { |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 1677 | return ssl_cert_check_private_key(ctx->cert, ctx->cert->privatekey.get()); |
Adam Langley | c5ac2b6 | 2016-11-07 12:02:35 -0800 | [diff] [blame] | 1678 | } |
| 1679 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1680 | // Fix this function so that it takes an optional type parameter |
Adam Langley | c5ac2b6 | 2016-11-07 12:02:35 -0800 | [diff] [blame] | 1681 | int SSL_check_private_key(const SSL *ssl) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1682 | if (!ssl->config) { |
| 1683 | return 0; |
| 1684 | } |
| 1685 | return ssl_cert_check_private_key(ssl->config->cert, |
| 1686 | ssl->config->cert->privatekey.get()); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1687 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1688 | |
David Benjamin | 42fea37 | 2015-09-19 01:22:44 -0400 | [diff] [blame] | 1689 | long SSL_get_default_timeout(const SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1690 | return SSL_DEFAULT_SESSION_TIMEOUT; |
| 1691 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1692 | |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 1693 | int SSL_renegotiate(SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1694 | // Caller-initiated renegotiation is not supported. |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1695 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 1696 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1697 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1698 | |
David Benjamin | 44d3eed | 2015-05-21 01:29:55 -0400 | [diff] [blame] | 1699 | int SSL_renegotiate_pending(SSL *ssl) { |
| 1700 | return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1701 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1702 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1703 | int SSL_total_renegotiations(const SSL *ssl) { |
| 1704 | return ssl->s3->total_renegotiations; |
| 1705 | } |
| 1706 | |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 1707 | size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) { |
| 1708 | return ctx->max_cert_list; |
| 1709 | } |
| 1710 | |
| 1711 | void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) { |
| 1712 | if (max_cert_list > kMaxHandshakeSize) { |
| 1713 | max_cert_list = kMaxHandshakeSize; |
| 1714 | } |
| 1715 | ctx->max_cert_list = (uint32_t)max_cert_list; |
| 1716 | } |
| 1717 | |
| 1718 | size_t SSL_get_max_cert_list(const SSL *ssl) { |
| 1719 | return ssl->max_cert_list; |
| 1720 | } |
| 1721 | |
| 1722 | void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) { |
| 1723 | if (max_cert_list > kMaxHandshakeSize) { |
| 1724 | max_cert_list = kMaxHandshakeSize; |
| 1725 | } |
| 1726 | ssl->max_cert_list = (uint32_t)max_cert_list; |
| 1727 | } |
| 1728 | |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1729 | 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] | 1730 | if (max_send_fragment < 512) { |
| 1731 | max_send_fragment = 512; |
| 1732 | } |
| 1733 | if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 1734 | max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
| 1735 | } |
| 1736 | ctx->max_send_fragment = (uint16_t)max_send_fragment; |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1737 | |
| 1738 | return 1; |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 1739 | } |
| 1740 | |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1741 | 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] | 1742 | if (max_send_fragment < 512) { |
| 1743 | max_send_fragment = 512; |
| 1744 | } |
| 1745 | if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) { |
| 1746 | max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
| 1747 | } |
| 1748 | ssl->max_send_fragment = (uint16_t)max_send_fragment; |
Adam Langley | ce9d85e | 2016-01-24 15:58:39 -0800 | [diff] [blame] | 1749 | |
| 1750 | return 1; |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 1751 | } |
| 1752 | |
David Benjamin | cb9cf79 | 2015-05-05 09:46:14 -0400 | [diff] [blame] | 1753 | int SSL_set_mtu(SSL *ssl, unsigned mtu) { |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 1754 | if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) { |
David Benjamin | cb9cf79 | 2015-05-05 09:46:14 -0400 | [diff] [blame] | 1755 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1756 | } |
David Benjamin | cb9cf79 | 2015-05-05 09:46:14 -0400 | [diff] [blame] | 1757 | ssl->d1->mtu = mtu; |
| 1758 | return 1; |
| 1759 | } |
| 1760 | |
| 1761 | int SSL_get_secure_renegotiation_support(const SSL *ssl) { |
David Benjamin | d261004 | 2017-01-03 10:49:28 -0500 | [diff] [blame] | 1762 | if (!ssl->s3->have_version) { |
| 1763 | return 0; |
| 1764 | } |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 1765 | return ssl_protocol_version(ssl) >= TLS1_3_VERSION || |
David Benjamin | d261004 | 2017-01-03 10:49:28 -0500 | [diff] [blame] | 1766 | ssl->s3->send_connection_binding; |
David Benjamin | cb9cf79 | 2015-05-05 09:46:14 -0400 | [diff] [blame] | 1767 | } |
| 1768 | |
David Benjamin | 71f7d3d | 2015-05-05 09:46:38 -0400 | [diff] [blame] | 1769 | size_t SSL_CTX_sess_number(const SSL_CTX *ctx) { |
David Benjamin | 9eaa3bd | 2017-09-27 17:03:54 -0400 | [diff] [blame] | 1770 | MutexReadLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock)); |
David Benjamin | 71f7d3d | 2015-05-05 09:46:38 -0400 | [diff] [blame] | 1771 | return lh_SSL_SESSION_num_items(ctx->sessions); |
| 1772 | } |
| 1773 | |
| 1774 | unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) { |
| 1775 | unsigned long ret = ctx->session_cache_size; |
| 1776 | ctx->session_cache_size = size; |
| 1777 | return ret; |
| 1778 | } |
| 1779 | |
| 1780 | unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) { |
| 1781 | return ctx->session_cache_size; |
| 1782 | } |
| 1783 | |
| 1784 | int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) { |
| 1785 | int ret = ctx->session_cache_mode; |
| 1786 | ctx->session_cache_mode = mode; |
| 1787 | return ret; |
| 1788 | } |
| 1789 | |
| 1790 | int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) { |
| 1791 | return ctx->session_cache_mode; |
| 1792 | } |
| 1793 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1794 | |
| 1795 | int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) { |
| 1796 | if (out == NULL) { |
| 1797 | return 48; |
| 1798 | } |
| 1799 | if (len != 48) { |
| 1800 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH); |
| 1801 | return 0; |
| 1802 | } |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 1803 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1804 | // The default ticket keys are initialized lazily. Trigger a key |
| 1805 | // rotation to initialize them. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 1806 | if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) { |
| 1807 | return 0; |
| 1808 | } |
| 1809 | |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1810 | uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 1811 | MutexReadLock lock(&ctx->lock); |
| 1812 | OPENSSL_memcpy(out_bytes, ctx->tlsext_ticket_key_current->name, 16); |
| 1813 | OPENSSL_memcpy(out_bytes + 16, ctx->tlsext_ticket_key_current->hmac_key, 16); |
| 1814 | 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] | 1815 | return 1; |
| 1816 | } |
| 1817 | |
| 1818 | int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) { |
| 1819 | if (in == NULL) { |
| 1820 | return 48; |
| 1821 | } |
| 1822 | if (len != 48) { |
| 1823 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH); |
| 1824 | return 0; |
| 1825 | } |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 1826 | if (!ctx->tlsext_ticket_key_current) { |
| 1827 | ctx->tlsext_ticket_key_current = |
| 1828 | (tlsext_ticket_key *)OPENSSL_malloc(sizeof(tlsext_ticket_key)); |
| 1829 | if (!ctx->tlsext_ticket_key_current) { |
| 1830 | return 0; |
| 1831 | } |
| 1832 | } |
| 1833 | OPENSSL_memset(ctx->tlsext_ticket_key_current, 0, sizeof(tlsext_ticket_key)); |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 1834 | const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in); |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 1835 | OPENSSL_memcpy(ctx->tlsext_ticket_key_current->name, in_bytes, 16); |
| 1836 | OPENSSL_memcpy(ctx->tlsext_ticket_key_current->hmac_key, in_bytes + 16, 16); |
| 1837 | OPENSSL_memcpy(ctx->tlsext_ticket_key_current->aes_key, in_bytes + 32, 16); |
| 1838 | OPENSSL_free(ctx->tlsext_ticket_key_prev); |
| 1839 | ctx->tlsext_ticket_key_prev = nullptr; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1840 | // Disable automatic key rotation. |
Martin Kreichgauer | 72912d2 | 2017-08-04 12:06:43 -0700 | [diff] [blame] | 1841 | ctx->tlsext_ticket_key_current->next_rotation_tv_sec = 0; |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1842 | return 1; |
| 1843 | } |
| 1844 | |
| 1845 | int SSL_CTX_set_tlsext_ticket_key_cb( |
| 1846 | SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv, |
| 1847 | EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx, |
| 1848 | int encrypt)) { |
| 1849 | ctx->tlsext_ticket_key_cb = callback; |
| 1850 | return 1; |
| 1851 | } |
| 1852 | |
| 1853 | int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) { |
| 1854 | return tls1_set_curves(&ctx->supported_group_list, |
| 1855 | &ctx->supported_group_list_len, curves, |
| 1856 | curves_len); |
| 1857 | } |
| 1858 | |
| 1859 | int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1860 | if (!ssl->config) { |
| 1861 | return 0; |
| 1862 | } |
| 1863 | return tls1_set_curves(&ssl->config->supported_group_list, |
| 1864 | &ssl->config->supported_group_list_len, curves, |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1865 | curves_len); |
| 1866 | } |
| 1867 | |
Alessandro Ghedini | 5fd1807 | 2016-09-28 21:04:25 +0100 | [diff] [blame] | 1868 | int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) { |
| 1869 | return tls1_set_curves_list(&ctx->supported_group_list, |
| 1870 | &ctx->supported_group_list_len, curves); |
| 1871 | } |
| 1872 | |
| 1873 | int SSL_set1_curves_list(SSL *ssl, const char *curves) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1874 | if (!ssl->config) { |
| 1875 | return 0; |
| 1876 | } |
| 1877 | return tls1_set_curves_list(&ssl->config->supported_group_list, |
| 1878 | &ssl->config->supported_group_list_len, curves); |
Alessandro Ghedini | 5fd1807 | 2016-09-28 21:04:25 +0100 | [diff] [blame] | 1879 | } |
| 1880 | |
David Benjamin | 9e68f19 | 2016-06-30 14:55:33 -0400 | [diff] [blame] | 1881 | uint16_t SSL_get_curve_id(const SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1882 | // TODO(davidben): This checks the wrong session if there is a renegotiation |
| 1883 | // in progress. |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1884 | SSL_SESSION *session = SSL_get_session(ssl); |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 1885 | if (session == NULL) { |
David Benjamin | 9e68f19 | 2016-06-30 14:55:33 -0400 | [diff] [blame] | 1886 | return 0; |
| 1887 | } |
| 1888 | |
David Benjamin | 4882a6c | 2016-12-11 02:48:12 -0500 | [diff] [blame] | 1889 | return session->group_id; |
David Benjamin | 9e68f19 | 2016-06-30 14:55:33 -0400 | [diff] [blame] | 1890 | } |
| 1891 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1892 | int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) { |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1893 | return 1; |
| 1894 | } |
| 1895 | |
| 1896 | int SSL_set_tmp_dh(SSL *ssl, const DH *dh) { |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 1897 | return 1; |
| 1898 | } |
| 1899 | |
David Benjamin | 70dbf04 | 2017-08-08 18:51:37 -0400 | [diff] [blame] | 1900 | STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) { |
David Benjamin | 48b276d | 2018-04-12 17:37:32 -0400 | [diff] [blame] | 1901 | return ctx->cipher_list->ciphers.get(); |
David Benjamin | 8ebeabf | 2017-03-02 22:04:07 -0500 | [diff] [blame] | 1902 | } |
| 1903 | |
David Benjamin | 70dbf04 | 2017-08-08 18:51:37 -0400 | [diff] [blame] | 1904 | int SSL_CTX_cipher_in_group(const SSL_CTX *ctx, size_t i) { |
David Benjamin | 48b276d | 2018-04-12 17:37:32 -0400 | [diff] [blame] | 1905 | if (i >= sk_SSL_CIPHER_num(ctx->cipher_list->ciphers.get())) { |
David Benjamin | 70dbf04 | 2017-08-08 18:51:37 -0400 | [diff] [blame] | 1906 | return 0; |
| 1907 | } |
| 1908 | return ctx->cipher_list->in_group_flags[i]; |
| 1909 | } |
| 1910 | |
David Benjamin | 32876b3 | 2015-09-20 12:17:03 -0400 | [diff] [blame] | 1911 | STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) { |
| 1912 | if (ssl == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1913 | return NULL; |
| 1914 | } |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1915 | if (ssl->config == NULL) { |
| 1916 | assert(ssl->config); |
| 1917 | return NULL; |
| 1918 | } |
Adam Langley | 0b5c1ac | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1919 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1920 | return ssl->config->cipher_list ? ssl->config->cipher_list->ciphers.get() |
| 1921 | : ssl->ctx->cipher_list->ciphers.get(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1922 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1923 | |
David Benjamin | 32876b3 | 2015-09-20 12:17:03 -0400 | [diff] [blame] | 1924 | const char *SSL_get_cipher_list(const SSL *ssl, int n) { |
David Benjamin | 32876b3 | 2015-09-20 12:17:03 -0400 | [diff] [blame] | 1925 | if (ssl == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1926 | return NULL; |
| 1927 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1928 | |
David Benjamin | 8ebeabf | 2017-03-02 22:04:07 -0500 | [diff] [blame] | 1929 | STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1930 | if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) { |
| 1931 | return NULL; |
| 1932 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1933 | |
David Benjamin | 8ebeabf | 2017-03-02 22:04:07 -0500 | [diff] [blame] | 1934 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1935 | if (c == NULL) { |
| 1936 | return NULL; |
| 1937 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1938 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1939 | return c->name; |
| 1940 | } |
David Benjamin | 5491e3f | 2014-09-29 19:33:09 -0400 | [diff] [blame] | 1941 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1942 | int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) { |
David Benjamin | 5be3a74 | 2017-10-28 16:08:58 -0400 | [diff] [blame] | 1943 | return ssl_create_cipher_list(&ctx->cipher_list, str, false /* not strict */); |
Matthew Braithwaite | a57dcfb | 2017-02-17 22:08:23 -0800 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) { |
David Benjamin | 5be3a74 | 2017-10-28 16:08:58 -0400 | [diff] [blame] | 1947 | return ssl_create_cipher_list(&ctx->cipher_list, str, true /* strict */); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1948 | } |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 1949 | |
David Benjamin | 32876b3 | 2015-09-20 12:17:03 -0400 | [diff] [blame] | 1950 | int SSL_set_cipher_list(SSL *ssl, const char *str) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1951 | if (!ssl->config) { |
| 1952 | return 0; |
| 1953 | } |
| 1954 | return ssl_create_cipher_list(&ssl->config->cipher_list, str, |
| 1955 | false /* not strict */); |
Matthew Braithwaite | a57dcfb | 2017-02-17 22:08:23 -0800 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | int SSL_set_strict_cipher_list(SSL *ssl, const char *str) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1959 | if (!ssl->config) { |
| 1960 | return 0; |
| 1961 | } |
| 1962 | return ssl_create_cipher_list(&ssl->config->cipher_list, str, |
| 1963 | true /* strict */); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1964 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1965 | |
David Benjamin | 07e1384 | 2015-10-17 13:48:04 -0400 | [diff] [blame] | 1966 | const char *SSL_get_servername(const SSL *ssl, const int type) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1967 | if (type != TLSEXT_NAMETYPE_host_name) { |
| 1968 | return NULL; |
| 1969 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1970 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 1971 | // Historically, |SSL_get_servername| was also the configuration getter |
| 1972 | // corresponding to |SSL_set_tlsext_host_name|. |
David Benjamin | 07e1384 | 2015-10-17 13:48:04 -0400 | [diff] [blame] | 1973 | if (ssl->tlsext_hostname != NULL) { |
| 1974 | return ssl->tlsext_hostname; |
| 1975 | } |
| 1976 | |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 1977 | return ssl->s3->hostname.get(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1978 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1979 | |
David Benjamin | 07e1384 | 2015-10-17 13:48:04 -0400 | [diff] [blame] | 1980 | int SSL_get_servername_type(const SSL *ssl) { |
David Benjamin | a861460 | 2017-09-06 15:40:19 -0400 | [diff] [blame] | 1981 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) == NULL) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1982 | return -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1983 | } |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1984 | return TLSEXT_NAMETYPE_host_name; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1985 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1986 | |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 1987 | void SSL_CTX_set_custom_verify( |
| 1988 | SSL_CTX *ctx, int mode, |
| 1989 | enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) { |
| 1990 | ctx->verify_mode = mode; |
| 1991 | ctx->custom_verify_callback = callback; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1992 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 1993 | |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 1994 | void SSL_set_custom_verify( |
| 1995 | SSL *ssl, int mode, |
| 1996 | enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 1997 | if (!ssl->config) { |
| 1998 | return; |
| 1999 | } |
| 2000 | ssl->config->verify_mode = mode; |
| 2001 | ssl->config->custom_verify_callback = callback; |
David Benjamin | 3a1dd46 | 2017-07-11 16:13:10 -0400 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) { |
David Benjamin | 7e58c5e | 2017-10-11 13:01:08 -0400 | [diff] [blame] | 2005 | ctx->signed_cert_timestamps_enabled = true; |
Adam Langley | fe36672 | 2017-03-07 16:41:04 -0800 | [diff] [blame] | 2006 | } |
| 2007 | |
David Benjamin | 26e1ff3 | 2017-02-14 20:13:00 -0500 | [diff] [blame] | 2008 | void SSL_enable_signed_cert_timestamps(SSL *ssl) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2009 | if (!ssl->config) { |
| 2010 | return; |
| 2011 | } |
| 2012 | ssl->config->signed_cert_timestamps_enabled = true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2013 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 2014 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2015 | void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) { |
David Benjamin | 7e58c5e | 2017-10-11 13:01:08 -0400 | [diff] [blame] | 2016 | ctx->ocsp_stapling_enabled = true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2017 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 2018 | |
David Benjamin | 26e1ff3 | 2017-02-14 20:13:00 -0500 | [diff] [blame] | 2019 | void SSL_enable_ocsp_stapling(SSL *ssl) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2020 | if (!ssl->config) { |
| 2021 | return; |
| 2022 | } |
| 2023 | ssl->config->ocsp_stapling_enabled = true; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2024 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 2025 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2026 | void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out, |
| 2027 | size_t *out_len) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 2028 | SSL_SESSION *session = SSL_get_session(ssl); |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 2029 | if (ssl->server || !session || !session->signed_cert_timestamp_list) { |
| 2030 | *out_len = 0; |
| 2031 | *out = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2032 | return; |
| 2033 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 2034 | |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 2035 | *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list); |
| 2036 | *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2037 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 2038 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2039 | void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out, |
| 2040 | size_t *out_len) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 2041 | SSL_SESSION *session = SSL_get_session(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2042 | if (ssl->server || !session || !session->ocsp_response) { |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 2043 | *out_len = 0; |
| 2044 | *out = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2045 | return; |
| 2046 | } |
David Benjamin | 8fc2dc0 | 2017-08-22 15:07:51 -0700 | [diff] [blame] | 2047 | |
| 2048 | *out = CRYPTO_BUFFER_data(session->ocsp_response); |
| 2049 | *out_len = CRYPTO_BUFFER_len(session->ocsp_response); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2050 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 2051 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2052 | int SSL_set_tlsext_host_name(SSL *ssl, const char *name) { |
| 2053 | OPENSSL_free(ssl->tlsext_hostname); |
| 2054 | ssl->tlsext_hostname = NULL; |
| 2055 | |
| 2056 | if (name == NULL) { |
| 2057 | return 1; |
| 2058 | } |
| 2059 | |
| 2060 | size_t len = strlen(name); |
| 2061 | if (len == 0 || len > TLSEXT_MAXLEN_host_name) { |
| 2062 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME); |
| 2063 | return 0; |
| 2064 | } |
| 2065 | ssl->tlsext_hostname = BUF_strdup(name); |
| 2066 | if (ssl->tlsext_hostname == NULL) { |
| 2067 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 2068 | return 0; |
| 2069 | } |
| 2070 | return 1; |
| 2071 | } |
| 2072 | |
| 2073 | int SSL_CTX_set_tlsext_servername_callback( |
| 2074 | SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) { |
| 2075 | ctx->tlsext_servername_callback = callback; |
| 2076 | return 1; |
| 2077 | } |
| 2078 | |
| 2079 | int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) { |
| 2080 | ctx->tlsext_servername_arg = arg; |
| 2081 | return 1; |
| 2082 | } |
| 2083 | |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 2084 | int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer, |
| 2085 | unsigned peer_len, const uint8_t *supported, |
| 2086 | unsigned supported_len) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2087 | const uint8_t *result; |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 2088 | int status; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2089 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2090 | // For each protocol in peer preference order, see if we support it. |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 2091 | for (unsigned i = 0; i < peer_len;) { |
| 2092 | for (unsigned j = 0; j < supported_len;) { |
| 2093 | if (peer[i] == supported[j] && |
| 2094 | OPENSSL_memcmp(&peer[i + 1], &supported[j + 1], peer[i]) == 0) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2095 | // We found a match |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 2096 | result = &peer[i]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2097 | status = OPENSSL_NPN_NEGOTIATED; |
| 2098 | goto found; |
| 2099 | } |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 2100 | j += supported[j]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2101 | j++; |
| 2102 | } |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 2103 | i += peer[i]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2104 | i++; |
| 2105 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2106 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2107 | // There's no overlap between our protocols and the peer's list. |
David Benjamin | ca74358 | 2017-06-15 17:51:35 -0400 | [diff] [blame] | 2108 | result = supported; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2109 | status = OPENSSL_NPN_NO_OVERLAP; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2110 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2111 | found: |
| 2112 | *out = (uint8_t *)result + 1; |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 2113 | *out_len = result[0]; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2114 | return status; |
| 2115 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2116 | |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 2117 | void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data, |
| 2118 | unsigned *out_len) { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2119 | *out_data = ssl->s3->next_proto_negotiated.data(); |
| 2120 | *out_len = ssl->s3->next_proto_negotiated.size(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2121 | } |
| 2122 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2123 | void SSL_CTX_set_next_protos_advertised_cb( |
| 2124 | SSL_CTX *ctx, |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 2125 | 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] | 2126 | void *arg) { |
| 2127 | ctx->next_protos_advertised_cb = cb; |
| 2128 | ctx->next_protos_advertised_cb_arg = arg; |
| 2129 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2130 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2131 | void SSL_CTX_set_next_proto_select_cb( |
David Benjamin | 977547b | 2015-09-16 00:25:52 -0400 | [diff] [blame] | 2132 | SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len, |
| 2133 | const uint8_t *in, unsigned in_len, void *arg), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2134 | void *arg) { |
| 2135 | ctx->next_proto_select_cb = cb; |
| 2136 | ctx->next_proto_select_cb_arg = arg; |
| 2137 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2138 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2139 | int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos, |
| 2140 | unsigned protos_len) { |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2141 | OPENSSL_free(ctx->alpn_client_proto_list); |
David Benjamin | f526081 | 2017-07-12 17:16:39 -0400 | [diff] [blame] | 2142 | ctx->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2143 | if (!ctx->alpn_client_proto_list) { |
| 2144 | return 1; |
| 2145 | } |
| 2146 | ctx->alpn_client_proto_list_len = protos_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2147 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2148 | return 0; |
| 2149 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2150 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2151 | int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2152 | if (!ssl->config) { |
| 2153 | return 0; |
| 2154 | } |
| 2155 | OPENSSL_free(ssl->config->alpn_client_proto_list); |
| 2156 | ssl->config->alpn_client_proto_list = |
| 2157 | (uint8_t *)BUF_memdup(protos, protos_len); |
| 2158 | if (!ssl->config->alpn_client_proto_list) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2159 | return 1; |
| 2160 | } |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2161 | ssl->config->alpn_client_proto_list_len = protos_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2162 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2163 | return 0; |
| 2164 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2165 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2166 | void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx, |
| 2167 | int (*cb)(SSL *ssl, const uint8_t **out, |
David Benjamin | 8984f1f | 2015-09-16 00:10:19 -0400 | [diff] [blame] | 2168 | uint8_t *out_len, const uint8_t *in, |
| 2169 | unsigned in_len, void *arg), |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2170 | void *arg) { |
| 2171 | ctx->alpn_select_cb = cb; |
| 2172 | ctx->alpn_select_cb_arg = arg; |
| 2173 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2174 | |
David Benjamin | 8984f1f | 2015-09-16 00:10:19 -0400 | [diff] [blame] | 2175 | void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data, |
| 2176 | unsigned *out_len) { |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2177 | if (SSL_in_early_data(ssl) && !ssl->server) { |
| 2178 | *out_data = ssl->s3->hs->early_session->early_alpn; |
| 2179 | *out_len = ssl->s3->hs->early_session->early_alpn_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2180 | } else { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2181 | *out_data = ssl->s3->alpn_selected.data(); |
| 2182 | *out_len = ssl->s3->alpn_selected.size(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2183 | } |
| 2184 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2185 | |
David Benjamin | c8ff30c | 2017-04-04 13:52:36 -0400 | [diff] [blame] | 2186 | void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) { |
| 2187 | ctx->allow_unknown_alpn_protos = !!enabled; |
| 2188 | } |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2189 | |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 2190 | void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) { |
| 2191 | ctx->tlsext_channel_id_enabled = !!enabled; |
| 2192 | } |
| 2193 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2194 | int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) { |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 2195 | SSL_CTX_set_tls_channel_id_enabled(ctx, 1); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2196 | return 1; |
| 2197 | } |
| 2198 | |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 2199 | void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2200 | if (!ssl->config) { |
| 2201 | return; |
| 2202 | } |
| 2203 | ssl->config->tlsext_channel_id_enabled = !!enabled; |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 2204 | } |
| 2205 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2206 | int SSL_enable_tls_channel_id(SSL *ssl) { |
David Benjamin | eebd3c8 | 2016-12-06 17:43:58 -0500 | [diff] [blame] | 2207 | SSL_set_tls_channel_id_enabled(ssl, 1); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2208 | return 1; |
| 2209 | } |
| 2210 | |
| 2211 | static int is_p256_key(EVP_PKEY *private_key) { |
| 2212 | const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key); |
| 2213 | return ec_key != NULL && |
| 2214 | EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) == |
| 2215 | NID_X9_62_prime256v1; |
| 2216 | } |
| 2217 | |
| 2218 | int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) { |
| 2219 | if (!is_p256_key(private_key)) { |
| 2220 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256); |
| 2221 | return 0; |
| 2222 | } |
| 2223 | |
| 2224 | EVP_PKEY_free(ctx->tlsext_channel_id_private); |
Adam Langley | 310d3f6 | 2016-07-12 10:39:20 -0700 | [diff] [blame] | 2225 | EVP_PKEY_up_ref(private_key); |
| 2226 | ctx->tlsext_channel_id_private = private_key; |
David Benjamin | 7e58c5e | 2017-10-11 13:01:08 -0400 | [diff] [blame] | 2227 | ctx->tlsext_channel_id_enabled = true; |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2228 | |
| 2229 | return 1; |
| 2230 | } |
| 2231 | |
| 2232 | int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2233 | if (!ssl->config) { |
| 2234 | return 0; |
| 2235 | } |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2236 | if (!is_p256_key(private_key)) { |
| 2237 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256); |
| 2238 | return 0; |
| 2239 | } |
| 2240 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2241 | EVP_PKEY_free(ssl->config->tlsext_channel_id_private); |
Adam Langley | 310d3f6 | 2016-07-12 10:39:20 -0700 | [diff] [blame] | 2242 | EVP_PKEY_up_ref(private_key); |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2243 | ssl->config->tlsext_channel_id_private = private_key; |
| 2244 | ssl->config->tlsext_channel_id_enabled = true; |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2245 | |
| 2246 | return 1; |
| 2247 | } |
| 2248 | |
| 2249 | size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) { |
| 2250 | if (!ssl->s3->tlsext_channel_id_valid) { |
| 2251 | return 0; |
| 2252 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 2253 | OPENSSL_memcpy(out, ssl->s3->tlsext_channel_id, |
| 2254 | (max_out < 64) ? max_out : 64); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2255 | return 64; |
| 2256 | } |
| 2257 | |
Nick Harper | 36fcc4c | 2017-09-21 15:02:22 -0700 | [diff] [blame] | 2258 | int SSL_set_token_binding_params(SSL *ssl, const uint8_t *params, size_t len) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2259 | if (!ssl->config) { |
| 2260 | return 0; |
| 2261 | } |
Nick Harper | 36fcc4c | 2017-09-21 15:02:22 -0700 | [diff] [blame] | 2262 | if (len > 256) { |
| 2263 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 2264 | return 0; |
| 2265 | } |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2266 | OPENSSL_free(ssl->config->token_binding_params); |
| 2267 | ssl->config->token_binding_params = (uint8_t *)BUF_memdup(params, len); |
| 2268 | if (!ssl->config->token_binding_params) { |
Nick Harper | 36fcc4c | 2017-09-21 15:02:22 -0700 | [diff] [blame] | 2269 | return 0; |
| 2270 | } |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2271 | ssl->config->token_binding_params_len = len; |
Nick Harper | 36fcc4c | 2017-09-21 15:02:22 -0700 | [diff] [blame] | 2272 | return 1; |
| 2273 | } |
| 2274 | |
| 2275 | int SSL_is_token_binding_negotiated(const SSL *ssl) { |
David Benjamin | 9f0e7cb | 2018-04-12 15:36:30 -0400 | [diff] [blame] | 2276 | return ssl->s3->token_binding_negotiated; |
Nick Harper | 36fcc4c | 2017-09-21 15:02:22 -0700 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | uint8_t SSL_get_negotiated_token_binding_param(const SSL *ssl) { |
David Benjamin | 9f0e7cb | 2018-04-12 15:36:30 -0400 | [diff] [blame] | 2280 | return ssl->s3->negotiated_token_binding_param; |
Nick Harper | 36fcc4c | 2017-09-21 15:02:22 -0700 | [diff] [blame] | 2281 | } |
| 2282 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2283 | 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] | 2284 | if (ssl->server || ssl->s3->hs == NULL) { |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2285 | *out_types = NULL; |
| 2286 | return 0; |
| 2287 | } |
David Benjamin | 08f5c76 | 2017-09-21 02:43:05 -0400 | [diff] [blame] | 2288 | *out_types = ssl->s3->hs->certificate_types.data(); |
| 2289 | return ssl->s3->hs->certificate_types.size(); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2290 | } |
| 2291 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2292 | EVP_PKEY *SSL_get_privatekey(const SSL *ssl) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2293 | if (!ssl->config) { |
| 2294 | assert(ssl->config); |
| 2295 | return NULL; |
| 2296 | } |
| 2297 | if (ssl->config->cert != NULL) { |
| 2298 | return ssl->config->cert->privatekey.get(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | return NULL; |
| 2302 | } |
| 2303 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2304 | EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) { |
| 2305 | if (ctx->cert != NULL) { |
David Benjamin | e325c3f | 2018-04-12 16:11:15 -0400 | [diff] [blame] | 2306 | return ctx->cert->privatekey.get(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | return NULL; |
| 2310 | } |
| 2311 | |
David Benjamin | 42fea37 | 2015-09-19 01:22:44 -0400 | [diff] [blame] | 2312 | const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 2313 | return ssl->s3->aead_write_ctx->cipher(); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2314 | } |
| 2315 | |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2316 | int SSL_session_reused(const SSL *ssl) { |
Steven Valdez | e831a81 | 2017-03-09 14:56:07 -0500 | [diff] [blame] | 2317 | return ssl->s3->session_reused || SSL_in_early_data(ssl); |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2318 | } |
| 2319 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2320 | const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2321 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2322 | const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2323 | |
Adam Langley | 29ec5d1 | 2016-03-01 16:12:28 -0800 | [diff] [blame] | 2324 | int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; } |
| 2325 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2326 | void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) { |
Adam Langley | bb85f3d | 2015-10-28 18:44:11 -0700 | [diff] [blame] | 2327 | ctx->quiet_shutdown = (mode != 0); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) { |
| 2331 | return ctx->quiet_shutdown; |
| 2332 | } |
| 2333 | |
Adam Langley | bb85f3d | 2015-10-28 18:44:11 -0700 | [diff] [blame] | 2334 | void SSL_set_quiet_shutdown(SSL *ssl, int mode) { |
| 2335 | ssl->quiet_shutdown = (mode != 0); |
| 2336 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2337 | |
David Benjamin | 9f85949 | 2015-10-03 10:44:30 -0400 | [diff] [blame] | 2338 | int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2339 | |
David Benjamin | 63006a9 | 2015-10-18 00:00:06 -0400 | [diff] [blame] | 2340 | void SSL_set_shutdown(SSL *ssl, int mode) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2341 | // It is an error to clear any bits that have already been set. (We can't try |
| 2342 | // to get a second close_notify or send two.) |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2343 | assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl)); |
David Benjamin | 63006a9 | 2015-10-18 00:00:06 -0400 | [diff] [blame] | 2344 | |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2345 | if (mode & SSL_RECEIVED_SHUTDOWN && |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 2346 | ssl->s3->read_shutdown == ssl_shutdown_none) { |
| 2347 | ssl->s3->read_shutdown = ssl_shutdown_close_notify; |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | if (mode & SSL_SENT_SHUTDOWN && |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 2351 | ssl->s3->write_shutdown == ssl_shutdown_none) { |
| 2352 | ssl->s3->write_shutdown = ssl_shutdown_close_notify; |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2353 | } |
David Benjamin | 63006a9 | 2015-10-18 00:00:06 -0400 | [diff] [blame] | 2354 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2355 | |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2356 | int SSL_get_shutdown(const SSL *ssl) { |
| 2357 | int ret = 0; |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 2358 | if (ssl->s3->read_shutdown != ssl_shutdown_none) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2359 | // Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify |
| 2360 | // and fatal alert. |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2361 | ret |= SSL_RECEIVED_SHUTDOWN; |
| 2362 | } |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 2363 | if (ssl->s3->write_shutdown == ssl_shutdown_close_notify) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2364 | // Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify. |
David Benjamin | fa214e4 | 2016-05-10 17:03:10 -0400 | [diff] [blame] | 2365 | ret |= SSL_SENT_SHUTDOWN; |
| 2366 | } |
| 2367 | return ret; |
| 2368 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2369 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2370 | SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; } |
| 2371 | |
| 2372 | SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2373 | if (!ssl->config) { |
| 2374 | return NULL; |
| 2375 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2376 | if (ssl->ctx == ctx) { |
| 2377 | return ssl->ctx; |
| 2378 | } |
| 2379 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2380 | // One cannot change the X.509 callbacks during a connection. |
Adam Langley | 3509dac | 2017-02-01 11:59:18 -0800 | [diff] [blame] | 2381 | if (ssl->ctx->x509_method != ctx->x509_method) { |
| 2382 | assert(0); |
| 2383 | return NULL; |
| 2384 | } |
| 2385 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2386 | if (ctx == NULL) { |
David Benjamin | 98472cb | 2018-05-02 16:05:36 -0400 | [diff] [blame] | 2387 | ctx = ssl->session_ctx; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2388 | } |
| 2389 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2390 | Delete(ssl->config->cert); |
| 2391 | ssl->config->cert = ssl_cert_dup(ctx->cert).release(); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2392 | |
David Benjamin | f650c71 | 2016-12-22 15:54:51 -0500 | [diff] [blame] | 2393 | SSL_CTX_up_ref(ctx); |
| 2394 | SSL_CTX_free(ssl->ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2395 | ssl->ctx = ctx; |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2396 | ssl->enable_early_data = ssl->ctx->enable_early_data; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2397 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2398 | return ssl->ctx; |
| 2399 | } |
| 2400 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2401 | void SSL_set_info_callback(SSL *ssl, |
David Benjamin | 8217024 | 2015-10-17 22:51:17 -0400 | [diff] [blame] | 2402 | void (*cb)(const SSL *ssl, int type, int value)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2403 | ssl->info_callback = cb; |
| 2404 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2405 | |
David Benjamin | 8217024 | 2015-10-17 22:51:17 -0400 | [diff] [blame] | 2406 | void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type, |
| 2407 | int value) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2408 | return ssl->info_callback; |
| 2409 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2410 | |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 2411 | int SSL_state(const SSL *ssl) { |
David Benjamin | 9e766d7 | 2017-02-10 23:14:17 -0500 | [diff] [blame] | 2412 | return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK; |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 2413 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2414 | |
David Benjamin | ece089c | 2015-05-15 23:52:42 -0400 | [diff] [blame] | 2415 | void SSL_set_state(SSL *ssl, int state) { } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2416 | |
Emily Stark | 95a79ee | 2016-02-25 21:12:28 -0800 | [diff] [blame] | 2417 | char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) { |
| 2418 | if (len <= 0) { |
| 2419 | return NULL; |
| 2420 | } |
| 2421 | buf[0] = '\0'; |
| 2422 | return buf; |
| 2423 | } |
| 2424 | |
David Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 2425 | 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] | 2426 | CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 2427 | int index; |
| 2428 | 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] | 2429 | free_func)) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 2430 | return -1; |
| 2431 | } |
| 2432 | return index; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2433 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2434 | |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2435 | int SSL_set_ex_data(SSL *ssl, int idx, void *data) { |
| 2436 | return CRYPTO_set_ex_data(&ssl->ex_data, idx, data); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2437 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2438 | |
David Benjamin | 1ac08fe | 2015-09-15 01:36:22 -0400 | [diff] [blame] | 2439 | void *SSL_get_ex_data(const SSL *ssl, int idx) { |
| 2440 | return CRYPTO_get_ex_data(&ssl->ex_data, idx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2441 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2442 | |
David Benjamin | 8a58933 | 2015-12-04 23:14:35 -0500 | [diff] [blame] | 2443 | 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] | 2444 | CRYPTO_EX_dup *dup_unused, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2445 | CRYPTO_EX_free *free_func) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 2446 | int index; |
| 2447 | 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] | 2448 | free_func)) { |
David Benjamin | 9f33fc6 | 2015-04-15 17:29:53 -0400 | [diff] [blame] | 2449 | return -1; |
| 2450 | } |
| 2451 | return index; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2452 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2453 | |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2454 | int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) { |
| 2455 | return CRYPTO_set_ex_data(&ctx->ex_data, idx, data); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2456 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2457 | |
David Benjamin | 1ac08fe | 2015-09-15 01:36:22 -0400 | [diff] [blame] | 2458 | void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) { |
| 2459 | return CRYPTO_get_ex_data(&ctx->ex_data, idx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2460 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2461 | |
David Benjamin | 32ce0ac | 2017-10-13 19:17:22 -0400 | [diff] [blame] | 2462 | int SSL_want(const SSL *ssl) { return ssl->s3->rwstate; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2463 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2464 | void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, |
| 2465 | RSA *(*cb)(SSL *ssl, int is_export, |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2466 | int keylength)) {} |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2467 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2468 | 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] | 2469 | int keylength)) {} |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2470 | |
| 2471 | void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2472 | DH *(*cb)(SSL *ssl, int is_export, |
| 2473 | int keylength)) {} |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2474 | |
David Benjamin | 27e377e | 2017-07-31 19:09:42 -0400 | [diff] [blame] | 2475 | void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export, |
| 2476 | int keylength)) {} |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2477 | |
David Benjamin | 0a9bf66 | 2017-06-21 16:34:15 -0400 | [diff] [blame] | 2478 | static int use_psk_identity_hint(char **out, const char *identity_hint) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2479 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2480 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2481 | return 0; |
| 2482 | } |
| 2483 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2484 | // Clear currently configured hint, if any. |
David Benjamin | 0a9bf66 | 2017-06-21 16:34:15 -0400 | [diff] [blame] | 2485 | OPENSSL_free(*out); |
| 2486 | *out = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2487 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2488 | // Treat the empty hint as not supplying one. Plain PSK makes it possible to |
| 2489 | // send either no hint (omit ServerKeyExchange) or an empty hint, while |
| 2490 | // ECDHE_PSK can only spell empty hint. Having different capabilities is odd, |
| 2491 | // so we interpret empty and missing as identical. |
David Benjamin | 7867934 | 2016-09-16 19:42:05 -0400 | [diff] [blame] | 2492 | if (identity_hint != NULL && identity_hint[0] != '\0') { |
David Benjamin | 0a9bf66 | 2017-06-21 16:34:15 -0400 | [diff] [blame] | 2493 | *out = BUF_strdup(identity_hint); |
| 2494 | if (*out == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2495 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2496 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2497 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2498 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2499 | return 1; |
| 2500 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2501 | |
David Benjamin | 0a9bf66 | 2017-06-21 16:34:15 -0400 | [diff] [blame] | 2502 | int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) { |
| 2503 | return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint); |
| 2504 | } |
| 2505 | |
| 2506 | int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2507 | if (!ssl->config) { |
| 2508 | return 0; |
| 2509 | } |
| 2510 | return use_psk_identity_hint(&ssl->config->psk_identity_hint, identity_hint); |
David Benjamin | 0a9bf66 | 2017-06-21 16:34:15 -0400 | [diff] [blame] | 2511 | } |
| 2512 | |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2513 | const char *SSL_get_psk_identity_hint(const SSL *ssl) { |
| 2514 | if (ssl == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2515 | return NULL; |
| 2516 | } |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2517 | if (ssl->config == NULL) { |
| 2518 | assert(ssl->config); |
| 2519 | return NULL; |
| 2520 | } |
| 2521 | return ssl->config->psk_identity_hint; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2522 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2523 | |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2524 | const char *SSL_get_psk_identity(const SSL *ssl) { |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 2525 | if (ssl == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2526 | return NULL; |
| 2527 | } |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 2528 | SSL_SESSION *session = SSL_get_session(ssl); |
| 2529 | if (session == NULL) { |
| 2530 | return NULL; |
| 2531 | } |
| 2532 | return session->psk_identity; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2533 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2534 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2535 | void SSL_set_psk_client_callback( |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2536 | SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity, |
| 2537 | unsigned max_identity_len, uint8_t *psk, |
| 2538 | unsigned max_psk_len)) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2539 | if (!ssl->config) { |
| 2540 | return; |
| 2541 | } |
| 2542 | ssl->config->psk_client_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2543 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2544 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2545 | void SSL_CTX_set_psk_client_callback( |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2546 | SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity, |
| 2547 | unsigned max_identity_len, uint8_t *psk, |
| 2548 | unsigned max_psk_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2549 | ctx->psk_client_callback = cb; |
| 2550 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2551 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2552 | void SSL_set_psk_server_callback( |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2553 | SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk, |
| 2554 | unsigned max_psk_len)) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2555 | if (!ssl->config) { |
| 2556 | return; |
| 2557 | } |
| 2558 | ssl->config->psk_server_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2559 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2560 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2561 | void SSL_CTX_set_psk_server_callback( |
David Benjamin | e8814df | 2015-09-15 08:05:54 -0400 | [diff] [blame] | 2562 | SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity, |
| 2563 | uint8_t *psk, unsigned max_psk_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2564 | ctx->psk_server_callback = cb; |
| 2565 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2566 | |
Adam Langley | 512a289 | 2017-12-30 08:04:39 -0800 | [diff] [blame] | 2567 | int SSL_set_dummy_pq_padding_size(SSL *ssl, size_t num_bytes) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2568 | if (!ssl->config) { |
| 2569 | return 0; |
| 2570 | } |
Adam Langley | 512a289 | 2017-12-30 08:04:39 -0800 | [diff] [blame] | 2571 | if (num_bytes > 0xffff) { |
| 2572 | return 0; |
| 2573 | } |
| 2574 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2575 | ssl->config->dummy_pq_padding_len = num_bytes; |
Adam Langley | 512a289 | 2017-12-30 08:04:39 -0800 | [diff] [blame] | 2576 | return 1; |
| 2577 | } |
| 2578 | |
Adam Langley | 8df8e64 | 2018-02-28 13:35:39 -0800 | [diff] [blame] | 2579 | int SSL_dummy_pq_padding_used(SSL *ssl) { |
Adam Langley | 40cdb3b | 2018-03-07 12:03:35 -0800 | [diff] [blame] | 2580 | if (ssl->server) { |
Adam Langley | 8df8e64 | 2018-02-28 13:35:39 -0800 | [diff] [blame] | 2581 | return 0; |
| 2582 | } |
| 2583 | |
| 2584 | return ssl->did_dummy_pq_padding; |
| 2585 | } |
| 2586 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2587 | void SSL_CTX_set_msg_callback(SSL_CTX *ctx, |
| 2588 | void (*cb)(int write_p, int version, |
| 2589 | int content_type, const void *buf, |
| 2590 | size_t len, SSL *ssl, void *arg)) { |
David Benjamin | 59015c3 | 2015-04-26 13:13:55 -0400 | [diff] [blame] | 2591 | ctx->msg_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2592 | } |
David Benjamin | 61ecccf | 2015-05-05 09:44:51 -0400 | [diff] [blame] | 2593 | |
| 2594 | void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) { |
| 2595 | ctx->msg_callback_arg = arg; |
| 2596 | } |
| 2597 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2598 | void SSL_set_msg_callback(SSL *ssl, |
| 2599 | void (*cb)(int write_p, int version, int content_type, |
| 2600 | const void *buf, size_t len, SSL *ssl, |
| 2601 | void *arg)) { |
David Benjamin | 59015c3 | 2015-04-26 13:13:55 -0400 | [diff] [blame] | 2602 | ssl->msg_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2603 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2604 | |
David Benjamin | 61ecccf | 2015-05-05 09:44:51 -0400 | [diff] [blame] | 2605 | void SSL_set_msg_callback_arg(SSL *ssl, void *arg) { |
| 2606 | ssl->msg_callback_arg = arg; |
| 2607 | } |
| 2608 | |
David Benjamin | d28f59c | 2015-11-17 22:32:50 -0500 | [diff] [blame] | 2609 | void SSL_CTX_set_keylog_callback(SSL_CTX *ctx, |
| 2610 | void (*cb)(const SSL *ssl, const char *line)) { |
| 2611 | ctx->keylog_callback = cb; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2612 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2613 | |
David Benjamin | 6e3f5cc | 2016-09-14 14:43:14 -0400 | [diff] [blame] | 2614 | void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl, |
| 2615 | const char *line) { |
| 2616 | return ctx->keylog_callback; |
| 2617 | } |
| 2618 | |
Taylor Brandstetter | 9edb2c6 | 2016-06-08 15:26:59 -0700 | [diff] [blame] | 2619 | void SSL_CTX_set_current_time_cb(SSL_CTX *ctx, |
| 2620 | void (*cb)(const SSL *ssl, |
| 2621 | struct timeval *out_clock)) { |
| 2622 | ctx->current_time_cb = cb; |
| 2623 | } |
| 2624 | |
David Benjamin | 5d8b128 | 2015-10-17 23:26:35 -0400 | [diff] [blame] | 2625 | int SSL_is_init_finished(const SSL *ssl) { |
David Benjamin | 9e766d7 | 2017-02-10 23:14:17 -0500 | [diff] [blame] | 2626 | return !SSL_in_init(ssl); |
David Benjamin | 5d8b128 | 2015-10-17 23:26:35 -0400 | [diff] [blame] | 2627 | } |
| 2628 | |
| 2629 | int SSL_in_init(const SSL *ssl) { |
Steven Valdez | d816874 | 2017-08-31 10:15:48 -0400 | [diff] [blame] | 2630 | // This returns false once all the handshake state has been finalized, to |
| 2631 | // allow callbacks and getters based on SSL_in_init to return the correct |
| 2632 | // values. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2633 | SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
Steven Valdez | d816874 | 2017-08-31 10:15:48 -0400 | [diff] [blame] | 2634 | return hs != nullptr && !hs->handshake_finalized; |
David Benjamin | 5d8b128 | 2015-10-17 23:26:35 -0400 | [diff] [blame] | 2635 | } |
| 2636 | |
| 2637 | int SSL_in_false_start(const SSL *ssl) { |
David Benjamin | a048678 | 2016-10-06 19:11:32 -0400 | [diff] [blame] | 2638 | if (ssl->s3->hs == NULL) { |
| 2639 | return 0; |
| 2640 | } |
| 2641 | return ssl->s3->hs->in_false_start; |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 2642 | } |
| 2643 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2644 | int SSL_cutthrough_complete(const SSL *ssl) { |
| 2645 | return SSL_in_false_start(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2646 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2647 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2648 | void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size, |
| 2649 | size_t *ssl_session_size) { |
| 2650 | *ssl_size = sizeof(SSL); |
| 2651 | *ssl_ctx_size = sizeof(SSL_CTX); |
| 2652 | *ssl_session_size = sizeof(SSL_SESSION); |
| 2653 | } |
Feng Lu | 41aa325 | 2014-11-21 22:47:56 -0800 | [diff] [blame] | 2654 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 2655 | int SSL_is_server(const SSL *ssl) { return ssl->server; } |
| 2656 | |
| 2657 | int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2658 | |
Alessandro Ghedini | 57e81e6 | 2017-03-14 23:36:00 +0000 | [diff] [blame] | 2659 | void SSL_CTX_set_select_certificate_cb( |
| 2660 | SSL_CTX *ctx, |
| 2661 | enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) { |
David Benjamin | d4c2bce | 2015-10-17 12:28:18 -0400 | [diff] [blame] | 2662 | ctx->select_certificate_cb = cb; |
| 2663 | } |
| 2664 | |
David Benjamin | 731058e | 2016-12-03 23:15:13 -0500 | [diff] [blame] | 2665 | void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx, |
| 2666 | int (*cb)(const SSL_CLIENT_HELLO *)) { |
Adam Langley | 524e717 | 2015-02-20 16:04:00 -0800 | [diff] [blame] | 2667 | ctx->dos_protection_cb = cb; |
| 2668 | } |
| 2669 | |
David Benjamin | 1d5ef3b | 2015-10-12 19:54:18 -0400 | [diff] [blame] | 2670 | void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) { |
| 2671 | ssl->renegotiate_mode = mode; |
David Benjamin | 71666cb | 2018-05-01 17:14:27 -0400 | [diff] [blame] | 2672 | |
| 2673 | // Check if |ssl_can_renegotiate| has changed and the configuration may now be |
| 2674 | // shed. HTTP clients may initially allow renegotiation for HTTP/1.1, and then |
| 2675 | // disable after the handshake once the ALPN protocol is known to be HTTP/2. |
| 2676 | ssl_maybe_shed_handshake_config(ssl); |
David Benjamin | 1d5ef3b | 2015-10-12 19:54:18 -0400 | [diff] [blame] | 2677 | } |
| 2678 | |
Adam Langley | c2d3280 | 2015-11-03 18:36:10 -0800 | [diff] [blame] | 2679 | int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv, |
| 2680 | const uint8_t **out_write_iv, size_t *out_iv_len) { |
Adam Langley | c2d3280 | 2015-11-03 18:36:10 -0800 | [diff] [blame] | 2681 | size_t write_iv_len; |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 2682 | if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) || |
| 2683 | !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) || |
Adam Langley | c2d3280 | 2015-11-03 18:36:10 -0800 | [diff] [blame] | 2684 | *out_iv_len != write_iv_len) { |
| 2685 | return 0; |
| 2686 | } |
| 2687 | |
| 2688 | return 1; |
David Benjamin | e348ff4 | 2015-11-06 17:55:30 -0500 | [diff] [blame] | 2689 | } |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 2690 | |
David Benjamin | 91b2501 | 2016-01-21 16:39:58 -0500 | [diff] [blame] | 2691 | static uint64_t be_to_u64(const uint8_t in[8]) { |
David Benjamin | de94238 | 2016-02-11 12:02:01 -0500 | [diff] [blame] | 2692 | return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) | |
| 2693 | (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) | |
| 2694 | (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) | |
| 2695 | (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]); |
David Benjamin | 91b2501 | 2016-01-21 16:39:58 -0500 | [diff] [blame] | 2696 | } |
| 2697 | |
| 2698 | uint64_t SSL_get_read_sequence(const SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2699 | // TODO(davidben): Internally represent sequence numbers as uint64_t. |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 2700 | if (SSL_is_dtls(ssl)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2701 | // max_seq_num already includes the epoch. |
David Benjamin | de94238 | 2016-02-11 12:02:01 -0500 | [diff] [blame] | 2702 | assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48)); |
| 2703 | return ssl->d1->bitmap.max_seq_num; |
| 2704 | } |
David Benjamin | 91b2501 | 2016-01-21 16:39:58 -0500 | [diff] [blame] | 2705 | return be_to_u64(ssl->s3->read_sequence); |
| 2706 | } |
| 2707 | |
| 2708 | uint64_t SSL_get_write_sequence(const SSL *ssl) { |
David Benjamin | de94238 | 2016-02-11 12:02:01 -0500 | [diff] [blame] | 2709 | uint64_t ret = be_to_u64(ssl->s3->write_sequence); |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 2710 | if (SSL_is_dtls(ssl)) { |
David Benjamin | de94238 | 2016-02-11 12:02:01 -0500 | [diff] [blame] | 2711 | assert((ret >> 48) == 0); |
| 2712 | ret |= ((uint64_t)ssl->d1->w_epoch) << 48; |
| 2713 | } |
| 2714 | return ret; |
David Benjamin | 91b2501 | 2016-01-21 16:39:58 -0500 | [diff] [blame] | 2715 | } |
| 2716 | |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 2717 | uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2718 | // TODO(davidben): This checks the wrong session if there is a renegotiation |
| 2719 | // in progress. |
David Benjamin | f1050fd | 2016-12-13 20:05:36 -0500 | [diff] [blame] | 2720 | SSL_SESSION *session = SSL_get_session(ssl); |
| 2721 | if (session == NULL) { |
| 2722 | return 0; |
| 2723 | } |
| 2724 | |
| 2725 | return session->peer_signature_algorithm; |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 2726 | } |
| 2727 | |
David Benjamin | 96ba15f | 2015-12-25 14:10:17 -0500 | [diff] [blame] | 2728 | size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) { |
| 2729 | if (max_out == 0) { |
| 2730 | return sizeof(ssl->s3->client_random); |
| 2731 | } |
| 2732 | if (max_out > sizeof(ssl->s3->client_random)) { |
| 2733 | max_out = sizeof(ssl->s3->client_random); |
| 2734 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 2735 | OPENSSL_memcpy(out, ssl->s3->client_random, max_out); |
David Benjamin | 96ba15f | 2015-12-25 14:10:17 -0500 | [diff] [blame] | 2736 | return max_out; |
| 2737 | } |
| 2738 | |
| 2739 | size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) { |
| 2740 | if (max_out == 0) { |
| 2741 | return sizeof(ssl->s3->server_random); |
| 2742 | } |
| 2743 | if (max_out > sizeof(ssl->s3->server_random)) { |
| 2744 | max_out = sizeof(ssl->s3->server_random); |
| 2745 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 2746 | OPENSSL_memcpy(out, ssl->s3->server_random, max_out); |
David Benjamin | 96ba15f | 2015-12-25 14:10:17 -0500 | [diff] [blame] | 2747 | return max_out; |
| 2748 | } |
| 2749 | |
David Benjamin | f6494f4 | 2016-01-07 12:37:41 -0800 | [diff] [blame] | 2750 | const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2751 | SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 2752 | if (hs == NULL) { |
David Benjamin | f6494f4 | 2016-01-07 12:37:41 -0800 | [diff] [blame] | 2753 | return NULL; |
| 2754 | } |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 2755 | return hs->new_cipher; |
David Benjamin | f6494f4 | 2016-01-07 12:37:41 -0800 | [diff] [blame] | 2756 | } |
| 2757 | |
David Benjamin | bbaf367 | 2016-11-17 10:53:09 +0900 | [diff] [blame] | 2758 | void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2759 | if (!ssl->config) { |
| 2760 | return; |
| 2761 | } |
| 2762 | ssl->config->retain_only_sha256_of_client_certs = !!enabled; |
David Benjamin | bbaf367 | 2016-11-17 10:53:09 +0900 | [diff] [blame] | 2763 | } |
| 2764 | |
David Benjamin | cdd0b7e | 2016-01-07 10:10:41 -0800 | [diff] [blame] | 2765 | void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) { |
| 2766 | ctx->retain_only_sha256_of_client_certs = !!enabled; |
| 2767 | } |
| 2768 | |
David Benjamin | 65ac997 | 2016-09-02 21:35:25 -0400 | [diff] [blame] | 2769 | void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) { |
| 2770 | ctx->grease_enabled = !!enabled; |
| 2771 | } |
| 2772 | |
David Benjamin | 35ac5b7 | 2017-03-03 15:05:56 -0500 | [diff] [blame] | 2773 | int32_t SSL_get_ticket_age_skew(const SSL *ssl) { |
| 2774 | return ssl->s3->ticket_age_skew; |
| 2775 | } |
| 2776 | |
David Benjamin | 494e4d0 | 2017-12-01 13:05:12 -0500 | [diff] [blame] | 2777 | void SSL_CTX_set_false_start_allowed_without_alpn(SSL_CTX *ctx, int allowed) { |
| 2778 | ctx->false_start_allowed_without_alpn = !!allowed; |
| 2779 | } |
| 2780 | |
David Benjamin | 6df6540 | 2017-12-18 18:00:23 -0500 | [diff] [blame] | 2781 | int SSL_is_draft_downgrade(const SSL *ssl) { return ssl->s3->draft_downgrade; } |
| 2782 | |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2783 | void SSL_set_shed_handshake_config(SSL *ssl, int enable) { |
| 2784 | if (!ssl->config) { |
| 2785 | return; |
| 2786 | } |
| 2787 | ssl->config->shed_handshake_config = !!enable; |
| 2788 | } |
| 2789 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 2790 | int SSL_clear(SSL *ssl) { |
Matthew Braithwaite | b7bc80a | 2018-04-13 15:51:30 -0700 | [diff] [blame] | 2791 | if (!ssl->config) { |
| 2792 | return 0; // SSL_clear may not be used after shedding config. |
| 2793 | } |
| 2794 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2795 | // In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously |
| 2796 | // established session to be offered the next time around. wpa_supplicant |
| 2797 | // depends on this behavior, so emulate it. |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2798 | UniquePtr<SSL_SESSION> session; |
David Benjamin | b79cc84 | 2016-12-07 15:57:14 -0500 | [diff] [blame] | 2799 | if (!ssl->server && ssl->s3->established_session != NULL) { |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2800 | session.reset(ssl->s3->established_session.get()); |
| 2801 | SSL_SESSION_up_ref(session.get()); |
David Benjamin | b79cc84 | 2016-12-07 15:57:14 -0500 | [diff] [blame] | 2802 | } |
| 2803 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 2804 | // The ssl->d1->mtu is simultaneously configuration (preserved across |
| 2805 | // clear) and connection-specific state (gets reset). |
| 2806 | // |
| 2807 | // TODO(davidben): Avoid this. |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 2808 | unsigned mtu = 0; |
| 2809 | if (ssl->d1 != NULL) { |
| 2810 | mtu = ssl->d1->mtu; |
| 2811 | } |
| 2812 | |
| 2813 | ssl->method->ssl_free(ssl); |
| 2814 | if (!ssl->method->ssl_new(ssl)) { |
| 2815 | return 0; |
| 2816 | } |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 2817 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 2818 | 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] | 2819 | ssl->d1->mtu = mtu; |
| 2820 | } |
| 2821 | |
David Benjamin | 8e7bbba | 2017-10-13 17:18:35 -0400 | [diff] [blame] | 2822 | if (session != nullptr) { |
| 2823 | SSL_set_session(ssl, session.get()); |
David Benjamin | b79cc84 | 2016-12-07 15:57:14 -0500 | [diff] [blame] | 2824 | } |
| 2825 | |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 2826 | return 1; |
| 2827 | } |
| 2828 | |
David Benjamin | da881e9 | 2015-04-26 14:45:04 -0400 | [diff] [blame] | 2829 | int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; } |
| 2830 | int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; } |
| 2831 | int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; } |
| 2832 | int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; } |
| 2833 | int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; } |
| 2834 | int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; } |
| 2835 | int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; } |
| 2836 | int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; } |
| 2837 | int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; } |
| 2838 | int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; } |
| 2839 | int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; } |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2840 | |
| 2841 | int SSL_num_renegotiations(const SSL *ssl) { |
| 2842 | return SSL_total_renegotiations(ssl); |
| 2843 | } |
| 2844 | |
| 2845 | int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; } |
| 2846 | int SSL_need_tmp_RSA(const SSL *ssl) { return 0; } |
| 2847 | int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; } |
| 2848 | int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; } |
Matt Braithwaite | 4838d8a | 2015-08-20 13:20:03 -0700 | [diff] [blame] | 2849 | void ERR_load_SSL_strings(void) {} |
David Benjamin | 27bbae4 | 2015-09-13 00:54:37 -0400 | [diff] [blame] | 2850 | void SSL_load_error_strings(void) {} |
David Benjamin | 936aada | 2016-06-07 19:09:22 -0400 | [diff] [blame] | 2851 | int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); } |
| 2852 | |
| 2853 | int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) { |
| 2854 | if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) { |
| 2855 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 2856 | return 0; |
| 2857 | } |
| 2858 | int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)); |
| 2859 | return SSL_CTX_set1_curves(ctx, &nid, 1); |
| 2860 | } |
| 2861 | |
| 2862 | int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) { |
| 2863 | if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) { |
| 2864 | OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 2865 | return 0; |
| 2866 | } |
| 2867 | int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)); |
| 2868 | return SSL_set1_curves(ssl, &nid, 1); |
| 2869 | } |
David Benjamin | 721e8b7 | 2016-08-03 13:13:17 -0400 | [diff] [blame] | 2870 | |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 2871 | void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx, |
| 2872 | const SSL_TICKET_AEAD_METHOD *aead_method) { |
| 2873 | ctx->ticket_aead_method = aead_method; |
| 2874 | } |