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