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