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