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