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