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