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