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