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