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