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