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