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, |
| 1301 | &ctx->cipher_list_by_id, str, ctx->cert); |
| 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 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1320 | sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, |
| 1321 | ctx->cert); |
| 1322 | if (sk == NULL) { |
| 1323 | return 0; |
| 1324 | } else if (sk_SSL_CIPHER_num(sk) == 0) { |
| 1325 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, |
| 1326 | SSL_R_NO_CIPHER_MATCH); |
| 1327 | return 0; |
| 1328 | } |
David Benjamin | 9f2c0d7 | 2014-10-21 22:00:19 -0400 | [diff] [blame] | 1329 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1330 | return 1; |
| 1331 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1332 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1333 | /* specify the ciphers to be used by the SSL */ |
| 1334 | int SSL_set_cipher_list(SSL *s, const char *str) { |
| 1335 | STACK_OF(SSL_CIPHER) *sk; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1336 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1337 | sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list, |
| 1338 | &s->cipher_list_by_id, str, s->cert); |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 1339 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1340 | /* see comment in SSL_CTX_set_cipher_list */ |
| 1341 | if (sk == NULL) { |
| 1342 | return 0; |
| 1343 | } else if (sk_SSL_CIPHER_num(sk) == 0) { |
| 1344 | OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH); |
| 1345 | return 0; |
| 1346 | } |
David Benjamin | 39482a1 | 2014-07-20 13:30:15 -0400 | [diff] [blame] | 1347 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1348 | return 1; |
| 1349 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1350 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1351 | int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) { |
| 1352 | size_t i; |
| 1353 | const SSL_CIPHER *c; |
| 1354 | CERT *ct = s->cert; |
| 1355 | uint8_t *q; |
| 1356 | /* Set disabled masks for this session */ |
| 1357 | ssl_set_client_disabled(s); |
Adam Langley | 2970779 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1358 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1359 | if (sk == NULL) { |
| 1360 | return 0; |
| 1361 | } |
| 1362 | q = p; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1363 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1364 | for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { |
| 1365 | c = sk_SSL_CIPHER_value(sk, i); |
| 1366 | /* Skip disabled ciphers */ |
| 1367 | if (c->algorithm_ssl & ct->mask_ssl || |
| 1368 | c->algorithm_mkey & ct->mask_k || |
| 1369 | c->algorithm_auth & ct->mask_a) { |
| 1370 | continue; |
| 1371 | } |
| 1372 | s2n(ssl3_get_cipher_value(c), p); |
| 1373 | } |
| 1374 | |
| 1375 | /* If all ciphers were disabled, return the error to the caller. */ |
| 1376 | if (p == q) { |
| 1377 | return 0; |
| 1378 | } |
| 1379 | |
| 1380 | /* Add SCSVs. */ |
| 1381 | if (!s->renegotiate) { |
| 1382 | s2n(SSL3_CK_SCSV & 0xffff, p); |
| 1383 | } |
| 1384 | |
Adam Langley | 5f0efe0 | 2015-02-20 13:03:16 -0800 | [diff] [blame] | 1385 | if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1386 | s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p); |
| 1387 | } |
| 1388 | |
| 1389 | return p - q; |
| 1390 | } |
| 1391 | |
| 1392 | STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) { |
| 1393 | CBS cipher_suites = *cbs; |
| 1394 | const SSL_CIPHER *c; |
| 1395 | STACK_OF(SSL_CIPHER) * sk; |
| 1396 | |
| 1397 | if (s->s3) { |
| 1398 | s->s3->send_connection_binding = 0; |
| 1399 | } |
| 1400 | |
| 1401 | if (CBS_len(&cipher_suites) % 2 != 0) { |
| 1402 | OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, |
| 1403 | SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); |
| 1404 | return NULL; |
| 1405 | } |
| 1406 | |
| 1407 | sk = sk_SSL_CIPHER_new_null(); |
| 1408 | if (sk == NULL) { |
| 1409 | OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE); |
| 1410 | goto err; |
| 1411 | } |
| 1412 | |
| 1413 | if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw, |
| 1414 | &s->cert->ciphers_rawlen)) { |
| 1415 | OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE); |
| 1416 | goto err; |
| 1417 | } |
| 1418 | |
| 1419 | while (CBS_len(&cipher_suites) > 0) { |
| 1420 | uint16_t cipher_suite; |
| 1421 | |
| 1422 | if (!CBS_get_u16(&cipher_suites, &cipher_suite)) { |
| 1423 | OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR); |
| 1424 | goto err; |
| 1425 | } |
| 1426 | |
| 1427 | /* Check for SCSV. */ |
| 1428 | if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) { |
| 1429 | /* SCSV is fatal if renegotiating. */ |
| 1430 | if (s->renegotiate) { |
| 1431 | OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, |
| 1432 | SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING); |
| 1433 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1434 | goto err; |
| 1435 | } |
| 1436 | s->s3->send_connection_binding = 1; |
| 1437 | continue; |
| 1438 | } |
| 1439 | |
| 1440 | /* Check for FALLBACK_SCSV. */ |
| 1441 | if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) { |
| 1442 | uint16_t max_version = ssl3_get_max_server_version(s); |
| 1443 | if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version |
| 1444 | : (uint16_t)s->version < max_version) { |
| 1445 | OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, |
| 1446 | SSL_R_INAPPROPRIATE_FALLBACK); |
| 1447 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK); |
| 1448 | goto err; |
| 1449 | } |
| 1450 | continue; |
| 1451 | } |
| 1452 | |
| 1453 | c = ssl3_get_cipher_by_value(cipher_suite); |
| 1454 | if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) { |
| 1455 | OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE); |
| 1456 | goto err; |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | return sk; |
David Benjamin | 9f2c0d7 | 2014-10-21 22:00:19 -0400 | [diff] [blame] | 1461 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1462 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1463 | if (sk != NULL) { |
| 1464 | sk_SSL_CIPHER_free(sk); |
| 1465 | } |
| 1466 | return NULL; |
| 1467 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1468 | |
| 1469 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1470 | /* return a servername extension value if provided in Client Hello, or NULL. So |
| 1471 | * far, only host_name types are defined (RFC 3546). */ |
| 1472 | const char *SSL_get_servername(const SSL *s, const int type) { |
| 1473 | if (type != TLSEXT_NAMETYPE_host_name) { |
| 1474 | return NULL; |
| 1475 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1476 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1477 | return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname |
| 1478 | : s->tlsext_hostname; |
| 1479 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1480 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1481 | int SSL_get_servername_type(const SSL *s) { |
| 1482 | if (s->session && |
| 1483 | (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) { |
| 1484 | return TLSEXT_NAMETYPE_host_name; |
| 1485 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1486 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1487 | return -1; |
| 1488 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1489 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1490 | void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) { |
| 1491 | ctx->signed_cert_timestamps_enabled = 1; |
| 1492 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 1493 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1494 | int SSL_enable_signed_cert_timestamps(SSL *ssl) { |
| 1495 | ssl->signed_cert_timestamps_enabled = 1; |
| 1496 | return 1; |
| 1497 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 1498 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1499 | void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) { |
| 1500 | ctx->ocsp_stapling_enabled = 1; |
| 1501 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 1502 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1503 | int SSL_enable_ocsp_stapling(SSL *ssl) { |
| 1504 | ssl->ocsp_stapling_enabled = 1; |
| 1505 | return 1; |
| 1506 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 1507 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1508 | void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out, |
| 1509 | size_t *out_len) { |
| 1510 | SSL_SESSION *session = ssl->session; |
Adam Langley | 3cb50e0 | 2014-08-26 14:00:31 -0700 | [diff] [blame] | 1511 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1512 | *out_len = 0; |
| 1513 | *out = NULL; |
| 1514 | if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) { |
| 1515 | return; |
| 1516 | } |
HÃ¥vard Molland | 9169c96 | 2014-08-14 14:42:37 +0200 | [diff] [blame] | 1517 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1518 | *out = session->tlsext_signed_cert_timestamp_list; |
| 1519 | *out_len = session->tlsext_signed_cert_timestamp_list_length; |
| 1520 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 1521 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1522 | void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out, |
| 1523 | size_t *out_len) { |
| 1524 | SSL_SESSION *session = ssl->session; |
| 1525 | |
| 1526 | *out_len = 0; |
| 1527 | *out = NULL; |
| 1528 | if (ssl->server || !session || !session->ocsp_response) { |
| 1529 | return; |
| 1530 | } |
| 1531 | *out = session->ocsp_response; |
| 1532 | *out_len = session->ocsp_response_length; |
| 1533 | } |
David Benjamin | 6c7aed0 | 2014-08-27 16:42:38 -0400 | [diff] [blame] | 1534 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1535 | /* SSL_select_next_proto implements the standard protocol selection. It is |
| 1536 | * expected that this function is called from the callback set by |
| 1537 | * SSL_CTX_set_next_proto_select_cb. |
| 1538 | * |
| 1539 | * The protocol data is assumed to be a vector of 8-bit, length prefixed byte |
| 1540 | * strings. The length byte itself is not included in the length. A byte |
| 1541 | * string of length 0 is invalid. No byte string may be truncated. |
| 1542 | * |
| 1543 | * The current, but experimental algorithm for selecting the protocol is: |
| 1544 | * |
| 1545 | * 1) If the server doesn't support NPN then this is indicated to the |
| 1546 | * callback. In this case, the client application has to abort the connection |
| 1547 | * or have a default application level protocol. |
| 1548 | * |
| 1549 | * 2) If the server supports NPN, but advertises an empty list then the |
| 1550 | * client selects the first protcol in its list, but indicates via the |
| 1551 | * API that this fallback case was enacted. |
| 1552 | * |
| 1553 | * 3) Otherwise, the client finds the first protocol in the server's list |
| 1554 | * that it supports and selects this protocol. This is because it's |
| 1555 | * assumed that the server has better information about which protocol |
| 1556 | * a client should use. |
| 1557 | * |
| 1558 | * 4) If the client doesn't support any of the server's advertised |
| 1559 | * protocols, then this is treated the same as case 2. |
| 1560 | * |
| 1561 | * It returns either |
| 1562 | * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or |
| 1563 | * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached. |
| 1564 | */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1565 | int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server, |
| 1566 | unsigned int server_len, const uint8_t *client, |
| 1567 | unsigned int client_len) { |
| 1568 | unsigned int i, j; |
| 1569 | const uint8_t *result; |
| 1570 | int status = OPENSSL_NPN_UNSUPPORTED; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1571 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1572 | /* For each protocol in server preference order, see if we support it. */ |
| 1573 | for (i = 0; i < server_len;) { |
| 1574 | for (j = 0; j < client_len;) { |
| 1575 | if (server[i] == client[j] && |
| 1576 | memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) { |
| 1577 | /* We found a match */ |
| 1578 | result = &server[i]; |
| 1579 | status = OPENSSL_NPN_NEGOTIATED; |
| 1580 | goto found; |
| 1581 | } |
| 1582 | j += client[j]; |
| 1583 | j++; |
| 1584 | } |
| 1585 | i += server[i]; |
| 1586 | i++; |
| 1587 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1588 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1589 | /* There's no overlap between our protocols and the server's list. */ |
| 1590 | result = client; |
| 1591 | status = OPENSSL_NPN_NO_OVERLAP; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1592 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1593 | found: |
| 1594 | *out = (uint8_t *)result + 1; |
| 1595 | *outlen = result[0]; |
| 1596 | return status; |
| 1597 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1598 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1599 | /* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's |
| 1600 | * requested protocol for this connection and returns 0. If the client didn't |
| 1601 | * request any protocol, then *data is set to NULL. |
| 1602 | * |
| 1603 | * Note that the client can request any protocol it chooses. The value returned |
| 1604 | * 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] | 1605 | * provided by the callback. */ |
| 1606 | void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, |
| 1607 | unsigned *len) { |
| 1608 | *data = s->next_proto_negotiated; |
| 1609 | if (!*data) { |
| 1610 | *len = 0; |
| 1611 | } else { |
| 1612 | *len = s->next_proto_negotiated_len; |
| 1613 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | /* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a |
| 1617 | * TLS server needs a list of supported protocols for Next Protocol |
| 1618 | * Negotiation. The returned list must be in wire format. The list is returned |
| 1619 | * by setting |out| to point to it and |outlen| to its length. This memory will |
| 1620 | * not be modified, but one should assume that the SSL* keeps a reference to |
| 1621 | * it. |
| 1622 | * |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1623 | * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. |
| 1624 | * Otherwise, no such extension will be included in the ServerHello. */ |
| 1625 | void SSL_CTX_set_next_protos_advertised_cb( |
| 1626 | SSL_CTX *ctx, |
| 1627 | int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg), |
| 1628 | void *arg) { |
| 1629 | ctx->next_protos_advertised_cb = cb; |
| 1630 | ctx->next_protos_advertised_cb_arg = arg; |
| 1631 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1632 | |
| 1633 | /* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a |
| 1634 | * client needs to select a protocol from the server's provided list. |out| |
| 1635 | * must be set to point to the selected protocol (which may be within |in|). |
| 1636 | * The length of the protocol name must be written into |outlen|. The server's |
| 1637 | * advertised protocols are provided in |in| and |inlen|. The callback can |
| 1638 | * assume that |in| is syntactically valid. |
| 1639 | * |
| 1640 | * The client must select a protocol. It is fatal to the connection if this |
| 1641 | * callback returns a value other than SSL_TLSEXT_ERR_OK. |
| 1642 | */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1643 | void SSL_CTX_set_next_proto_select_cb( |
| 1644 | SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen, |
| 1645 | const uint8_t *in, unsigned int inlen, void *arg), |
| 1646 | void *arg) { |
| 1647 | ctx->next_proto_select_cb = cb; |
| 1648 | ctx->next_proto_select_cb_arg = arg; |
| 1649 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1650 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1651 | int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos, |
| 1652 | unsigned protos_len) { |
| 1653 | if (ctx->alpn_client_proto_list) { |
| 1654 | OPENSSL_free(ctx->alpn_client_proto_list); |
| 1655 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1656 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1657 | ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len); |
| 1658 | if (!ctx->alpn_client_proto_list) { |
| 1659 | return 1; |
| 1660 | } |
| 1661 | ctx->alpn_client_proto_list_len = protos_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1662 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1663 | return 0; |
| 1664 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1665 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1666 | int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) { |
| 1667 | if (ssl->alpn_client_proto_list) { |
| 1668 | OPENSSL_free(ssl->alpn_client_proto_list); |
| 1669 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1670 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1671 | ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len); |
| 1672 | if (!ssl->alpn_client_proto_list) { |
| 1673 | return 1; |
| 1674 | } |
| 1675 | ssl->alpn_client_proto_list_len = protos_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1676 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1677 | return 0; |
| 1678 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1679 | |
| 1680 | /* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called |
| 1681 | * during ClientHello processing in order to select an ALPN protocol from the |
| 1682 | * client's list of offered protocols. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1683 | void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx, |
| 1684 | int (*cb)(SSL *ssl, const uint8_t **out, |
| 1685 | uint8_t *outlen, const uint8_t *in, |
| 1686 | unsigned int inlen, void *arg), |
| 1687 | void *arg) { |
| 1688 | ctx->alpn_select_cb = cb; |
| 1689 | ctx->alpn_select_cb_arg = arg; |
| 1690 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1691 | |
| 1692 | /* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|. |
| 1693 | * On return it sets |*data| to point to |*len| bytes of protocol name (not |
| 1694 | * including the leading length-prefix byte). If the server didn't respond with |
| 1695 | * a negotiated protocol then |*len| will be zero. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1696 | void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data, |
| 1697 | unsigned *len) { |
| 1698 | *data = NULL; |
| 1699 | if (ssl->s3) { |
| 1700 | *data = ssl->s3->alpn_selected; |
| 1701 | } |
| 1702 | if (*data == NULL) { |
| 1703 | *len = 0; |
| 1704 | } else { |
| 1705 | *len = ssl->s3->alpn_selected_len; |
| 1706 | } |
| 1707 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1708 | |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 1709 | int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len, |
| 1710 | const char *label, size_t label_len, |
| 1711 | const uint8_t *context, size_t context_len, |
| 1712 | int use_context) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1713 | if (s->version < TLS1_VERSION) { |
David Benjamin | c565ebb | 2015-04-03 04:06:36 -0400 | [diff] [blame] | 1714 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1715 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1716 | |
David Benjamin | cfd248b | 2015-04-03 11:02:24 -0400 | [diff] [blame] | 1717 | return s->enc_method->export_keying_material( |
| 1718 | s, out, out_len, label, label_len, context, context_len, use_context); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1719 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1720 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1721 | static uint32_t ssl_session_hash(const SSL_SESSION *a) { |
| 1722 | uint32_t hash = |
| 1723 | ((uint32_t)a->session_id[0]) || |
| 1724 | ((uint32_t)a->session_id[1] << 8) || |
| 1725 | ((uint32_t)a->session_id[2] << 16) || |
| 1726 | ((uint32_t)a->session_id[3] << 24); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1727 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1728 | return hash; |
| 1729 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1730 | |
| 1731 | /* NB: If this function (or indeed the hash function which uses a sort of |
| 1732 | * coarser function than this one) is changed, ensure |
| 1733 | * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being |
| 1734 | * able to construct an SSL_SESSION that will collide with any existing session |
| 1735 | * with a matching session ID. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1736 | static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) { |
| 1737 | if (a->ssl_version != b->ssl_version) { |
| 1738 | return 1; |
| 1739 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1740 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1741 | if (a->session_id_length != b->session_id_length) { |
| 1742 | return 1; |
| 1743 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1744 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1745 | return memcmp(a->session_id, b->session_id, a->session_id_length); |
| 1746 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1747 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1748 | SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) { |
| 1749 | SSL_CTX *ret = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1750 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1751 | if (meth == NULL) { |
| 1752 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED); |
| 1753 | return NULL; |
| 1754 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1755 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1756 | if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) { |
| 1757 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS); |
| 1758 | goto err; |
| 1759 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1760 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1761 | ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX)); |
| 1762 | if (ret == NULL) { |
| 1763 | goto err; |
| 1764 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1765 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1766 | memset(ret, 0, sizeof(SSL_CTX)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1767 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1768 | ret->method = meth->method; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1769 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1770 | ret->cert_store = NULL; |
| 1771 | ret->session_cache_mode = SSL_SESS_CACHE_SERVER; |
| 1772 | ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT; |
| 1773 | ret->session_cache_head = NULL; |
| 1774 | ret->session_cache_tail = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1775 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1776 | /* We take the system default */ |
| 1777 | ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1778 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1779 | ret->new_session_cb = 0; |
| 1780 | ret->remove_session_cb = 0; |
| 1781 | ret->get_session_cb = 0; |
| 1782 | ret->generate_session_id = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1783 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1784 | ret->references = 1; |
| 1785 | ret->quiet_shutdown = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1786 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1787 | ret->info_callback = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1788 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1789 | ret->app_verify_callback = 0; |
| 1790 | ret->app_verify_arg = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1791 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1792 | ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT; |
| 1793 | ret->read_ahead = 0; |
| 1794 | ret->msg_callback = 0; |
| 1795 | ret->msg_callback_arg = NULL; |
| 1796 | ret->verify_mode = SSL_VERIFY_NONE; |
| 1797 | ret->sid_ctx_length = 0; |
| 1798 | ret->default_verify_callback = NULL; |
| 1799 | ret->cert = ssl_cert_new(); |
| 1800 | if (ret->cert == NULL) { |
| 1801 | goto err; |
| 1802 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1803 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1804 | ret->default_passwd_callback = 0; |
| 1805 | ret->default_passwd_callback_userdata = NULL; |
| 1806 | ret->client_cert_cb = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1807 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1808 | ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp); |
| 1809 | if (ret->sessions == NULL) { |
| 1810 | goto err; |
| 1811 | } |
| 1812 | ret->cert_store = X509_STORE_new(); |
| 1813 | if (ret->cert_store == NULL) { |
| 1814 | goto err; |
| 1815 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1816 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1817 | ssl_create_cipher_list(ret->method, &ret->cipher_list, |
| 1818 | &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST, |
| 1819 | ret->cert); |
| 1820 | if (ret->cipher_list == NULL || |
| 1821 | sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) { |
| 1822 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS); |
| 1823 | goto err2; |
| 1824 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1825 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1826 | ret->param = X509_VERIFY_PARAM_new(); |
| 1827 | if (!ret->param) { |
| 1828 | goto err; |
| 1829 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1830 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1831 | ret->client_CA = sk_X509_NAME_new_null(); |
| 1832 | if (ret->client_CA == NULL) { |
| 1833 | goto err; |
| 1834 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1835 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1836 | 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] | 1837 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1838 | ret->extra_certs = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1839 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1840 | ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH; |
David Benjamin | 422d3a4 | 2014-08-20 11:09:03 -0400 | [diff] [blame] | 1841 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1842 | ret->tlsext_servername_callback = 0; |
| 1843 | ret->tlsext_servername_arg = NULL; |
| 1844 | /* Setup RFC4507 ticket keys */ |
| 1845 | if (!RAND_bytes(ret->tlsext_tick_key_name, 16) || |
| 1846 | !RAND_bytes(ret->tlsext_tick_hmac_key, 16) || |
| 1847 | !RAND_bytes(ret->tlsext_tick_aes_key, 16)) { |
| 1848 | ret->options |= SSL_OP_NO_TICKET; |
| 1849 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1850 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1851 | ret->tlsext_status_cb = 0; |
| 1852 | ret->tlsext_status_arg = NULL; |
David Benjamin | 82c9e90 | 2014-12-12 15:55:27 -0500 | [diff] [blame] | 1853 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1854 | ret->next_protos_advertised_cb = 0; |
| 1855 | ret->next_proto_select_cb = 0; |
| 1856 | ret->psk_identity_hint = NULL; |
| 1857 | ret->psk_client_callback = NULL; |
| 1858 | ret->psk_server_callback = NULL; |
| 1859 | |
| 1860 | /* Default is to connect to non-RI servers. When RI is more widely deployed |
| 1861 | * might change this. */ |
| 1862 | ret->options |= SSL_OP_LEGACY_SERVER_CONNECT; |
| 1863 | |
| 1864 | /* Lock the SSL_CTX to the specified version, for compatibility with legacy |
| 1865 | * uses of SSL_METHOD. */ |
| 1866 | if (meth->version != 0) { |
| 1867 | SSL_CTX_set_max_version(ret, meth->version); |
| 1868 | SSL_CTX_set_min_version(ret, meth->version); |
| 1869 | } |
| 1870 | |
| 1871 | return ret; |
| 1872 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1873 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1874 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1875 | err2: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1876 | if (ret != NULL) { |
| 1877 | SSL_CTX_free(ret); |
| 1878 | } |
| 1879 | return NULL; |
| 1880 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1881 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1882 | void SSL_CTX_free(SSL_CTX *a) { |
| 1883 | int i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1884 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1885 | if (a == NULL) { |
| 1886 | return; |
| 1887 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1888 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1889 | i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX); |
| 1890 | if (i > 0) { |
| 1891 | return; |
| 1892 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1893 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1894 | if (a->param) { |
| 1895 | X509_VERIFY_PARAM_free(a->param); |
| 1896 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1897 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1898 | /* Free internal session cache. However: the remove_cb() may reference the |
| 1899 | * ex_data of SSL_CTX, thus the ex_data store can only be removed after the |
| 1900 | * sessions were flushed. As the ex_data handling routines might also touch |
| 1901 | * the session cache, the most secure solution seems to be: empty (flush) the |
| 1902 | * cache, then free ex_data, then finally free the cache. (See ticket |
| 1903 | * [openssl.org #212].) */ |
| 1904 | if (a->sessions != NULL) { |
| 1905 | SSL_CTX_flush_sessions(a, 0); |
| 1906 | } |
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 | 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] | 1909 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1910 | if (a->sessions != NULL) { |
| 1911 | lh_SSL_SESSION_free(a->sessions); |
| 1912 | } |
| 1913 | if (a->cert_store != NULL) { |
| 1914 | X509_STORE_free(a->cert_store); |
| 1915 | } |
| 1916 | if (a->cipher_list != NULL) { |
| 1917 | ssl_cipher_preference_list_free(a->cipher_list); |
| 1918 | } |
| 1919 | if (a->cipher_list_by_id != NULL) { |
| 1920 | sk_SSL_CIPHER_free(a->cipher_list_by_id); |
| 1921 | } |
| 1922 | if (a->cipher_list_tls11 != NULL) { |
| 1923 | ssl_cipher_preference_list_free(a->cipher_list_tls11); |
| 1924 | } |
| 1925 | if (a->cert != NULL) { |
| 1926 | ssl_cert_free(a->cert); |
| 1927 | } |
| 1928 | if (a->client_CA != NULL) { |
| 1929 | sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free); |
| 1930 | } |
| 1931 | if (a->extra_certs != NULL) { |
| 1932 | sk_X509_pop_free(a->extra_certs, X509_free); |
| 1933 | } |
| 1934 | if (a->srtp_profiles) { |
| 1935 | sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles); |
| 1936 | } |
| 1937 | if (a->psk_identity_hint) { |
| 1938 | OPENSSL_free(a->psk_identity_hint); |
| 1939 | } |
| 1940 | if (a->tlsext_ecpointformatlist) { |
| 1941 | OPENSSL_free(a->tlsext_ecpointformatlist); |
| 1942 | } |
| 1943 | if (a->tlsext_ellipticcurvelist) { |
| 1944 | OPENSSL_free(a->tlsext_ellipticcurvelist); |
| 1945 | } |
| 1946 | if (a->alpn_client_proto_list != NULL) { |
| 1947 | OPENSSL_free(a->alpn_client_proto_list); |
| 1948 | } |
| 1949 | if (a->tlsext_channel_id_private) { |
| 1950 | EVP_PKEY_free(a->tlsext_channel_id_private); |
| 1951 | } |
| 1952 | if (a->keylog_bio) { |
| 1953 | BIO_free(a->keylog_bio); |
| 1954 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1955 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1956 | OPENSSL_free(a); |
| 1957 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1958 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1959 | void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) { |
| 1960 | ctx->default_passwd_callback = cb; |
| 1961 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1962 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1963 | void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) { |
| 1964 | ctx->default_passwd_callback_userdata = u; |
| 1965 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1966 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1967 | void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, |
| 1968 | int (*cb)(X509_STORE_CTX *, void *), |
| 1969 | void *arg) { |
| 1970 | ctx->app_verify_callback = cb; |
| 1971 | ctx->app_verify_arg = arg; |
| 1972 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1973 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1974 | void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, |
| 1975 | int (*cb)(int, X509_STORE_CTX *)) { |
| 1976 | ctx->verify_mode = mode; |
| 1977 | ctx->default_verify_callback = cb; |
| 1978 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1979 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1980 | void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) { |
| 1981 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); |
| 1982 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1983 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1984 | void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), |
| 1985 | void *arg) { |
| 1986 | ssl_cert_set_cert_cb(c->cert, cb, arg); |
| 1987 | } |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 1988 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1989 | void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) { |
| 1990 | ssl_cert_set_cert_cb(s->cert, cb, arg); |
| 1991 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1992 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1993 | static int ssl_has_key(SSL *s, size_t idx) { |
| 1994 | CERT_PKEY *cpk = &s->cert->pkeys[idx]; |
| 1995 | return cpk->x509 && cpk->privatekey; |
| 1996 | } |
David Benjamin | 033e5f4 | 2014-11-13 18:47:41 -0500 | [diff] [blame] | 1997 | |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame^] | 1998 | void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k, |
| 1999 | uint32_t *out_mask_a) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2000 | CERT *c = s->cert; |
| 2001 | int rsa_enc, rsa_sign, dh_tmp; |
David Benjamin | 107db58 | 2015-04-08 00:41:59 -0400 | [diff] [blame^] | 2002 | uint32_t mask_k, mask_a; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2003 | int have_ecc_cert, ecdsa_ok; |
| 2004 | int have_ecdh_tmp; |
| 2005 | X509 *x; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2006 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2007 | if (c == NULL) { |
| 2008 | /* TODO(davidben): Is this codepath possible? */ |
| 2009 | *out_mask_k = 0; |
| 2010 | *out_mask_a = 0; |
| 2011 | return; |
| 2012 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2013 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2014 | dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2015 | |
David Benjamin | c0f763b | 2015-03-27 02:05:39 -0400 | [diff] [blame] | 2016 | have_ecdh_tmp = (c->ecdh_nid != NID_undef || c->ecdh_tmp_cb != NULL || |
| 2017 | c->ecdh_tmp_auto); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2018 | rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC); |
| 2019 | rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN); |
| 2020 | have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC); |
| 2021 | mask_k = 0; |
| 2022 | mask_a = 0; |
David Benjamin | f31e681 | 2014-11-13 18:05:55 -0500 | [diff] [blame] | 2023 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2024 | if (rsa_enc) { |
| 2025 | mask_k |= SSL_kRSA; |
| 2026 | } |
| 2027 | if (dh_tmp) { |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 2028 | mask_k |= SSL_kDHE; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2029 | } |
| 2030 | if (rsa_enc || rsa_sign) { |
| 2031 | mask_a |= SSL_aRSA; |
| 2032 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2033 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2034 | /* An ECC certificate may be usable for ECDSA cipher suites depending on the |
| 2035 | * key usage extension and on the client's curve preferences. */ |
| 2036 | if (have_ecc_cert) { |
| 2037 | x = c->pkeys[SSL_PKEY_ECC].x509; |
| 2038 | /* This call populates extension flags (ex_flags). */ |
| 2039 | X509_check_purpose(x, -1, 0); |
| 2040 | ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) |
| 2041 | ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) |
| 2042 | : 1; |
| 2043 | if (!tls1_check_ec_cert(s, x)) { |
| 2044 | ecdsa_ok = 0; |
| 2045 | } |
| 2046 | if (ecdsa_ok) { |
| 2047 | mask_a |= SSL_aECDSA; |
| 2048 | } |
| 2049 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2050 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2051 | /* If we are considering an ECC cipher suite that uses an ephemeral EC |
| 2052 | * key, check it. */ |
| 2053 | if (have_ecdh_tmp && tls1_check_ec_tmp_key(s)) { |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 2054 | mask_k |= SSL_kECDHE; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2055 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2056 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2057 | /* PSK requires a server callback. */ |
| 2058 | if (s->psk_server_callback != NULL) { |
| 2059 | mask_k |= SSL_kPSK; |
| 2060 | mask_a |= SSL_aPSK; |
| 2061 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2062 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2063 | *out_mask_k = mask_k; |
| 2064 | *out_mask_a = mask_a; |
| 2065 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2066 | |
| 2067 | /* This handy macro borrowed from crypto/x509v3/v3_purp.c */ |
| 2068 | #define ku_reject(x, usage) \ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2069 | (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage))) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2070 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2071 | int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) { |
| 2072 | unsigned long alg_a; |
| 2073 | int signature_nid = 0, md_nid = 0, pk_nid = 0; |
| 2074 | const SSL_CIPHER *cs = s->s3->tmp.new_cipher; |
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 | alg_a = cs->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2077 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2078 | /* This call populates the ex_flags field correctly */ |
| 2079 | X509_check_purpose(x, -1, 0); |
| 2080 | if (x->sig_alg && x->sig_alg->algorithm) { |
| 2081 | signature_nid = OBJ_obj2nid(x->sig_alg->algorithm); |
| 2082 | OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid); |
| 2083 | } |
| 2084 | if (alg_a & SSL_aECDSA) { |
| 2085 | /* key usage, if present, must allow signing */ |
| 2086 | if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) { |
| 2087 | OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, |
| 2088 | SSL_R_ECC_CERT_NOT_FOR_SIGNING); |
| 2089 | return 0; |
| 2090 | } |
| 2091 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2092 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2093 | return 1; /* all checks are ok */ |
| 2094 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2095 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2096 | static int ssl_get_server_cert_index(const SSL *s) { |
| 2097 | int idx; |
| 2098 | idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); |
| 2099 | if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) { |
| 2100 | idx = SSL_PKEY_RSA_SIGN; |
| 2101 | } |
| 2102 | if (idx == -1) { |
| 2103 | OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR); |
| 2104 | } |
| 2105 | return idx; |
| 2106 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2107 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2108 | CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) { |
| 2109 | int i = ssl_get_server_cert_index(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2110 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2111 | /* This may or may not be an error. */ |
| 2112 | if (i < 0) { |
| 2113 | return NULL; |
| 2114 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2115 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2116 | /* May be NULL. */ |
| 2117 | return &s->cert->pkeys[i]; |
| 2118 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2119 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2120 | EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) { |
| 2121 | unsigned long alg_a; |
| 2122 | CERT *c; |
| 2123 | int idx = -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2124 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2125 | alg_a = cipher->algorithm_auth; |
| 2126 | c = s->cert; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2127 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2128 | if (alg_a & SSL_aRSA) { |
| 2129 | if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) { |
| 2130 | idx = SSL_PKEY_RSA_SIGN; |
| 2131 | } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) { |
| 2132 | idx = SSL_PKEY_RSA_ENC; |
| 2133 | } |
| 2134 | } else if ((alg_a & SSL_aECDSA) && |
| 2135 | (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) { |
| 2136 | idx = SSL_PKEY_ECC; |
| 2137 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2138 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2139 | if (idx == -1) { |
| 2140 | OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR); |
| 2141 | return NULL; |
| 2142 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2143 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2144 | return c->pkeys[idx].privatekey; |
| 2145 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2146 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2147 | void ssl_update_cache(SSL *s, int mode) { |
David Benjamin | b6d0c6d | 2015-03-19 19:07:26 -0400 | [diff] [blame] | 2148 | /* Never cache sessions with empty session IDs. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2149 | if (s->session->session_id_length == 0) { |
| 2150 | return; |
| 2151 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2152 | |
David Benjamin | b6d0c6d | 2015-03-19 19:07:26 -0400 | [diff] [blame] | 2153 | SSL_CTX *ctx = s->initial_ctx; |
| 2154 | if ((ctx->session_cache_mode & mode) == mode && !s->hit && |
| 2155 | ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) || |
| 2156 | SSL_CTX_add_session(ctx, s->session)) && |
| 2157 | ctx->new_session_cb != NULL) { |
| 2158 | /* Note: |new_session_cb| is called whether the internal session cache is |
| 2159 | * used or not. */ |
| 2160 | if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2161 | SSL_SESSION_free(s->session); |
| 2162 | } |
| 2163 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2164 | |
David Benjamin | b6d0c6d | 2015-03-19 19:07:26 -0400 | [diff] [blame] | 2165 | if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) && |
| 2166 | !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) && |
| 2167 | (ctx->session_cache_mode & mode) == mode) { |
| 2168 | /* Automatically flush the internal session cache every 255 connections. */ |
| 2169 | int flush_cache = 0; |
| 2170 | CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); |
| 2171 | ctx->handshakes_since_cache_flush++; |
| 2172 | if (ctx->handshakes_since_cache_flush >= 255) { |
| 2173 | flush_cache = 1; |
| 2174 | ctx->handshakes_since_cache_flush = 0; |
| 2175 | } |
| 2176 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); |
| 2177 | |
| 2178 | if (flush_cache) { |
| 2179 | SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2180 | } |
| 2181 | } |
| 2182 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2183 | |
David Benjamin | 1a5c50f | 2015-03-11 16:22:37 -0400 | [diff] [blame] | 2184 | int SSL_get_error(const SSL *s, int ret_code) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2185 | int reason; |
David Benjamin | 1a5c50f | 2015-03-11 16:22:37 -0400 | [diff] [blame] | 2186 | uint32_t err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2187 | BIO *bio; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2188 | |
David Benjamin | 1a5c50f | 2015-03-11 16:22:37 -0400 | [diff] [blame] | 2189 | if (ret_code > 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2190 | return SSL_ERROR_NONE; |
| 2191 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2192 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2193 | /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc, |
| 2194 | * where we do encode the error */ |
David Benjamin | 1a5c50f | 2015-03-11 16:22:37 -0400 | [diff] [blame] | 2195 | err = ERR_peek_error(); |
| 2196 | if (err != 0) { |
| 2197 | if (ERR_GET_LIB(err) == ERR_LIB_SYS) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2198 | return SSL_ERROR_SYSCALL; |
| 2199 | } |
| 2200 | return SSL_ERROR_SSL; |
| 2201 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2202 | |
David Benjamin | 1a5c50f | 2015-03-11 16:22:37 -0400 | [diff] [blame] | 2203 | if (ret_code == 0) { |
David Benjamin | 9a38e92 | 2015-01-22 16:06:11 -0500 | [diff] [blame] | 2204 | if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) && |
| 2205 | (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) { |
| 2206 | /* The socket was cleanly shut down with a close_notify. */ |
| 2207 | return SSL_ERROR_ZERO_RETURN; |
| 2208 | } |
| 2209 | /* An EOF was observed which violates the protocol, and the underlying |
| 2210 | * transport does not participate in the error queue. Bubble up to the |
| 2211 | * caller. */ |
| 2212 | return SSL_ERROR_SYSCALL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2213 | } |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2214 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2215 | if (SSL_want_session(s)) { |
| 2216 | return SSL_ERROR_PENDING_SESSION; |
| 2217 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2218 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2219 | if (SSL_want_certificate(s)) { |
| 2220 | return SSL_ERROR_PENDING_CERTIFICATE; |
| 2221 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2222 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2223 | if (SSL_want_read(s)) { |
| 2224 | bio = SSL_get_rbio(s); |
| 2225 | if (BIO_should_read(bio)) { |
| 2226 | return SSL_ERROR_WANT_READ; |
| 2227 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2228 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2229 | if (BIO_should_write(bio)) { |
| 2230 | /* This one doesn't make too much sense ... We never try to write to the |
| 2231 | * rbio, and an application program where rbio and wbio are separate |
| 2232 | * couldn't even know what it should wait for. However if we ever set |
| 2233 | * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of |
| 2234 | * SSL_want_write(s)) and rbio and wbio *are* the same, this test works |
| 2235 | * around that bug; so it might be safer to keep it. */ |
| 2236 | return SSL_ERROR_WANT_WRITE; |
| 2237 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2238 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2239 | if (BIO_should_io_special(bio)) { |
| 2240 | reason = BIO_get_retry_reason(bio); |
| 2241 | if (reason == BIO_RR_CONNECT) { |
| 2242 | return SSL_ERROR_WANT_CONNECT; |
| 2243 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2244 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2245 | if (reason == BIO_RR_ACCEPT) { |
| 2246 | return SSL_ERROR_WANT_ACCEPT; |
| 2247 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2248 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2249 | return SSL_ERROR_SYSCALL; /* unknown */ |
| 2250 | } |
| 2251 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2252 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2253 | if (SSL_want_write(s)) { |
| 2254 | bio = SSL_get_wbio(s); |
| 2255 | if (BIO_should_write(bio)) { |
| 2256 | return SSL_ERROR_WANT_WRITE; |
| 2257 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2258 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2259 | if (BIO_should_read(bio)) { |
| 2260 | /* See above (SSL_want_read(s) with BIO_should_write(bio)) */ |
| 2261 | return SSL_ERROR_WANT_READ; |
| 2262 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2263 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2264 | if (BIO_should_io_special(bio)) { |
| 2265 | reason = BIO_get_retry_reason(bio); |
| 2266 | if (reason == BIO_RR_CONNECT) { |
| 2267 | return SSL_ERROR_WANT_CONNECT; |
| 2268 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2269 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2270 | if (reason == BIO_RR_ACCEPT) { |
| 2271 | return SSL_ERROR_WANT_ACCEPT; |
| 2272 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2273 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2274 | return SSL_ERROR_SYSCALL; |
| 2275 | } |
| 2276 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2277 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2278 | if (SSL_want_x509_lookup(s)) { |
| 2279 | return SSL_ERROR_WANT_X509_LOOKUP; |
| 2280 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2281 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2282 | if (SSL_want_channel_id_lookup(s)) { |
| 2283 | return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP; |
| 2284 | } |
Adam Langley | 0f4746e | 2014-08-13 12:26:32 -0700 | [diff] [blame] | 2285 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2286 | return SSL_ERROR_SYSCALL; |
| 2287 | } |
Adam Langley | 0f4746e | 2014-08-13 12:26:32 -0700 | [diff] [blame] | 2288 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2289 | int SSL_do_handshake(SSL *s) { |
| 2290 | int ret = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2291 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2292 | if (s->handshake_func == NULL) { |
| 2293 | OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET); |
| 2294 | return -1; |
| 2295 | } |
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 | s->method->ssl_renegotiate_check(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2298 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2299 | if (SSL_in_init(s)) { |
| 2300 | ret = s->handshake_func(s); |
| 2301 | } |
| 2302 | return ret; |
| 2303 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2304 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2305 | void SSL_set_accept_state(SSL *s) { |
| 2306 | s->server = 1; |
| 2307 | s->shutdown = 0; |
| 2308 | s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE; |
| 2309 | s->handshake_func = s->method->ssl_accept; |
| 2310 | /* clear the current cipher */ |
| 2311 | ssl_clear_cipher_ctx(s); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2312 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2313 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2314 | void SSL_set_connect_state(SSL *s) { |
| 2315 | s->server = 0; |
| 2316 | s->shutdown = 0; |
| 2317 | s->state = SSL_ST_CONNECT | SSL_ST_BEFORE; |
| 2318 | s->handshake_func = s->method->ssl_connect; |
| 2319 | /* clear the current cipher */ |
| 2320 | ssl_clear_cipher_ctx(s); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2321 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2322 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2323 | int ssl_undefined_function(SSL *s) { |
| 2324 | OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, |
| 2325 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2326 | return 0; |
| 2327 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2328 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2329 | int ssl_undefined_void_function(void) { |
| 2330 | OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, |
| 2331 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2332 | return 0; |
| 2333 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2334 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2335 | int ssl_undefined_const_function(const SSL *s) { |
| 2336 | OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, |
| 2337 | ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 2338 | return 0; |
| 2339 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2340 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2341 | static const char *ssl_get_version(int version) { |
| 2342 | switch (version) { |
| 2343 | case TLS1_2_VERSION: |
| 2344 | return "TLSv1.2"; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2345 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2346 | case TLS1_1_VERSION: |
| 2347 | return "TLSv1.1"; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2348 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2349 | case TLS1_VERSION: |
| 2350 | return "TLSv1"; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2351 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2352 | case SSL3_VERSION: |
| 2353 | return "SSLv3"; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2354 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2355 | default: |
| 2356 | return "unknown"; |
| 2357 | } |
| 2358 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2359 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2360 | const char *SSL_get_version(const SSL *s) { |
| 2361 | return ssl_get_version(s->version); |
| 2362 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2363 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2364 | const char *SSL_SESSION_get_version(const SSL_SESSION *sess) { |
| 2365 | return ssl_get_version(sess->ssl_version); |
| 2366 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2367 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2368 | void ssl_clear_cipher_ctx(SSL *s) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2369 | if (s->aead_read_ctx != NULL) { |
| 2370 | EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx); |
| 2371 | OPENSSL_free(s->aead_read_ctx); |
| 2372 | s->aead_read_ctx = NULL; |
| 2373 | } |
Adam Langley | a5dc545 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2374 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2375 | if (s->aead_write_ctx != NULL) { |
| 2376 | EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx); |
| 2377 | OPENSSL_free(s->aead_write_ctx); |
| 2378 | s->aead_write_ctx = NULL; |
| 2379 | } |
| 2380 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2381 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2382 | X509 *SSL_get_certificate(const SSL *s) { |
| 2383 | if (s->cert != NULL) { |
| 2384 | return s->cert->key->x509; |
| 2385 | } |
| 2386 | |
| 2387 | return NULL; |
| 2388 | } |
| 2389 | |
| 2390 | EVP_PKEY *SSL_get_privatekey(const SSL *s) { |
| 2391 | if (s->cert != NULL) { |
| 2392 | return s->cert->key->privatekey; |
| 2393 | } |
| 2394 | |
| 2395 | return NULL; |
| 2396 | } |
| 2397 | |
| 2398 | X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) { |
| 2399 | if (ctx->cert != NULL) { |
| 2400 | return ctx->cert->key->x509; |
| 2401 | } |
| 2402 | |
| 2403 | return NULL; |
| 2404 | } |
| 2405 | |
| 2406 | EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) { |
| 2407 | if (ctx->cert != NULL) { |
| 2408 | return ctx->cert->key->privatekey; |
| 2409 | } |
| 2410 | |
| 2411 | return NULL; |
| 2412 | } |
| 2413 | |
| 2414 | const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) { |
| 2415 | if (s->session != NULL && s->session->cipher != NULL) { |
| 2416 | return s->session->cipher; |
| 2417 | } |
| 2418 | |
| 2419 | return NULL; |
| 2420 | } |
| 2421 | |
| 2422 | const void *SSL_get_current_compression(SSL *s) { return NULL; } |
| 2423 | |
| 2424 | const void *SSL_get_current_expansion(SSL *s) { return NULL; } |
| 2425 | |
| 2426 | int ssl_init_wbio_buffer(SSL *s, int push) { |
| 2427 | BIO *bbio; |
| 2428 | |
| 2429 | if (s->bbio == NULL) { |
| 2430 | bbio = BIO_new(BIO_f_buffer()); |
| 2431 | if (bbio == NULL) { |
| 2432 | return 0; |
| 2433 | } |
| 2434 | s->bbio = bbio; |
| 2435 | } else { |
| 2436 | bbio = s->bbio; |
| 2437 | if (s->bbio == s->wbio) { |
| 2438 | s->wbio = BIO_pop(s->wbio); |
| 2439 | } |
| 2440 | } |
| 2441 | |
| 2442 | BIO_reset(bbio); |
| 2443 | if (!BIO_set_read_buffer_size(bbio, 1)) { |
| 2444 | OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB); |
| 2445 | return 0; |
| 2446 | } |
| 2447 | |
| 2448 | if (push) { |
| 2449 | if (s->wbio != bbio) { |
| 2450 | s->wbio = BIO_push(bbio, s->wbio); |
| 2451 | } |
| 2452 | } else { |
| 2453 | if (s->wbio == bbio) { |
| 2454 | s->wbio = BIO_pop(bbio); |
| 2455 | } |
| 2456 | } |
| 2457 | |
| 2458 | return 1; |
| 2459 | } |
| 2460 | |
| 2461 | void ssl_free_wbio_buffer(SSL *s) { |
| 2462 | if (s->bbio == NULL) { |
| 2463 | return; |
| 2464 | } |
| 2465 | |
| 2466 | if (s->bbio == s->wbio) { |
| 2467 | /* remove buffering */ |
| 2468 | s->wbio = BIO_pop(s->wbio); |
| 2469 | } |
| 2470 | |
| 2471 | BIO_free(s->bbio); |
| 2472 | s->bbio = NULL; |
| 2473 | } |
| 2474 | |
| 2475 | void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) { |
| 2476 | ctx->quiet_shutdown = mode; |
| 2477 | } |
| 2478 | |
| 2479 | int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) { |
| 2480 | return ctx->quiet_shutdown; |
| 2481 | } |
| 2482 | |
| 2483 | void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; } |
| 2484 | |
| 2485 | int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; } |
| 2486 | |
| 2487 | void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; } |
| 2488 | |
| 2489 | int SSL_get_shutdown(const SSL *s) { return s->shutdown; } |
| 2490 | |
| 2491 | int SSL_version(const SSL *s) { return s->version; } |
| 2492 | |
| 2493 | SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; } |
| 2494 | |
| 2495 | SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) { |
| 2496 | if (ssl->ctx == ctx) { |
| 2497 | return ssl->ctx; |
| 2498 | } |
| 2499 | |
| 2500 | if (ctx == NULL) { |
| 2501 | ctx = ssl->initial_ctx; |
| 2502 | } |
| 2503 | |
| 2504 | if (ssl->cert != NULL) { |
| 2505 | ssl_cert_free(ssl->cert); |
| 2506 | } |
| 2507 | |
| 2508 | ssl->cert = ssl_cert_dup(ctx->cert); |
| 2509 | CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX); |
| 2510 | if (ssl->ctx != NULL) { |
| 2511 | SSL_CTX_free(ssl->ctx); /* decrement reference count */ |
| 2512 | } |
| 2513 | ssl->ctx = ctx; |
| 2514 | |
| 2515 | ssl->sid_ctx_length = ctx->sid_ctx_length; |
| 2516 | assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx)); |
| 2517 | memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx)); |
| 2518 | |
| 2519 | return ssl->ctx; |
| 2520 | } |
| 2521 | |
| 2522 | int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) { |
| 2523 | return X509_STORE_set_default_paths(ctx->cert_store); |
| 2524 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2525 | |
| 2526 | int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2527 | const char *CApath) { |
| 2528 | return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath); |
| 2529 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2530 | |
| 2531 | void SSL_set_info_callback(SSL *ssl, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2532 | void (*cb)(const SSL *ssl, int type, int val)) { |
| 2533 | ssl->info_callback = cb; |
| 2534 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2535 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2536 | void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/, |
| 2537 | int /*val*/) { |
| 2538 | return ssl->info_callback; |
| 2539 | } |
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 | int SSL_state(const SSL *ssl) { return ssl->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_state(SSL *ssl, int state) { ssl->state = state; } |
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 | 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] | 2546 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2547 | long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2548 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2549 | int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, |
| 2550 | CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) { |
| 2551 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp, new_func, |
| 2552 | dup_func, free_func); |
| 2553 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2554 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2555 | int SSL_set_ex_data(SSL *s, int idx, void *arg) { |
| 2556 | return CRYPTO_set_ex_data(&s->ex_data, idx, arg); |
| 2557 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2558 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2559 | void *SSL_get_ex_data(const SSL *s, int idx) { |
| 2560 | return CRYPTO_get_ex_data(&s->ex_data, idx); |
| 2561 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2562 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2563 | int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, |
| 2564 | CRYPTO_EX_dup *dup_func, |
| 2565 | CRYPTO_EX_free *free_func) { |
| 2566 | return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp, new_func, |
| 2567 | dup_func, free_func); |
| 2568 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2569 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2570 | int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) { |
| 2571 | return CRYPTO_set_ex_data(&s->ex_data, idx, arg); |
| 2572 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2573 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2574 | void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) { |
| 2575 | return CRYPTO_get_ex_data(&s->ex_data, idx); |
| 2576 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2577 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2578 | int ssl_ok(SSL *s) { return 1; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2579 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2580 | X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) { |
| 2581 | return ctx->cert_store; |
| 2582 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2583 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2584 | void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 2585 | if (ctx->cert_store != NULL) { |
| 2586 | X509_STORE_free(ctx->cert_store); |
| 2587 | } |
| 2588 | ctx->cert_store = store; |
| 2589 | } |
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 | int SSL_want(const SSL *s) { return s->rwstate; } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2592 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2593 | void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, |
| 2594 | RSA *(*cb)(SSL *ssl, int is_export, |
| 2595 | int keylength)) { |
| 2596 | SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb); |
| 2597 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2598 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2599 | void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export, |
| 2600 | int keylength)) { |
| 2601 | SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb); |
| 2602 | } |
| 2603 | |
| 2604 | void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, |
| 2605 | DH *(*dh)(SSL *ssl, int is_export, |
| 2606 | int keylength)) { |
| 2607 | SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh); |
| 2608 | } |
| 2609 | |
| 2610 | void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export, |
| 2611 | int keylength)) { |
| 2612 | SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh); |
| 2613 | } |
| 2614 | |
| 2615 | void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx, |
| 2616 | EC_KEY *(*ecdh)(SSL *ssl, int is_export, |
| 2617 | int keylength)) { |
| 2618 | SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh); |
| 2619 | } |
| 2620 | |
| 2621 | void SSL_set_tmp_ecdh_callback(SSL *ssl, |
| 2622 | EC_KEY *(*ecdh)(SSL *ssl, int is_export, |
| 2623 | int keylength)) { |
| 2624 | SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh); |
| 2625 | } |
| 2626 | |
| 2627 | int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) { |
| 2628 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) { |
| 2629 | OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, |
| 2630 | SSL_R_DATA_LENGTH_TOO_LONG); |
| 2631 | return 0; |
| 2632 | } |
| 2633 | |
| 2634 | if (ctx->psk_identity_hint != NULL) { |
| 2635 | OPENSSL_free(ctx->psk_identity_hint); |
| 2636 | } |
| 2637 | |
| 2638 | if (identity_hint != NULL) { |
| 2639 | ctx->psk_identity_hint = BUF_strdup(identity_hint); |
| 2640 | if (ctx->psk_identity_hint == NULL) { |
| 2641 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2642 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2643 | } else { |
| 2644 | ctx->psk_identity_hint = NULL; |
| 2645 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2646 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2647 | return 1; |
| 2648 | } |
| 2649 | |
| 2650 | int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) { |
| 2651 | if (s == NULL) { |
| 2652 | return 0; |
| 2653 | } |
| 2654 | |
| 2655 | if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) { |
| 2656 | OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, |
| 2657 | SSL_R_DATA_LENGTH_TOO_LONG); |
| 2658 | return 0; |
| 2659 | } |
| 2660 | |
| 2661 | /* Clear currently configured hint, if any. */ |
| 2662 | if (s->psk_identity_hint != NULL) { |
| 2663 | OPENSSL_free(s->psk_identity_hint); |
| 2664 | s->psk_identity_hint = NULL; |
| 2665 | } |
| 2666 | |
| 2667 | if (identity_hint != NULL) { |
| 2668 | s->psk_identity_hint = BUF_strdup(identity_hint); |
| 2669 | if (s->psk_identity_hint == NULL) { |
| 2670 | return 0; |
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 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2673 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2674 | return 1; |
| 2675 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2676 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2677 | const char *SSL_get_psk_identity_hint(const SSL *s) { |
| 2678 | if (s == NULL) { |
| 2679 | return NULL; |
| 2680 | } |
| 2681 | return s->psk_identity_hint; |
| 2682 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2683 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2684 | const char *SSL_get_psk_identity(const SSL *s) { |
| 2685 | if (s == NULL || s->session == NULL) { |
| 2686 | return NULL; |
| 2687 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2688 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2689 | return s->session->psk_identity; |
| 2690 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2691 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2692 | void SSL_set_psk_client_callback( |
| 2693 | SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity, |
| 2694 | unsigned int max_identity_len, uint8_t *psk, |
| 2695 | unsigned int max_psk_len)) { |
| 2696 | s->psk_client_callback = cb; |
| 2697 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2698 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2699 | void SSL_CTX_set_psk_client_callback( |
| 2700 | SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity, |
| 2701 | unsigned int max_identity_len, |
| 2702 | uint8_t *psk, unsigned int max_psk_len)) { |
| 2703 | ctx->psk_client_callback = cb; |
| 2704 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2705 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2706 | void SSL_set_psk_server_callback( |
| 2707 | SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk, |
| 2708 | unsigned int max_psk_len)) { |
| 2709 | s->psk_server_callback = cb; |
| 2710 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2711 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2712 | void SSL_CTX_set_psk_server_callback( |
| 2713 | SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity, |
| 2714 | uint8_t *psk, unsigned int max_psk_len)) { |
| 2715 | ctx->psk_server_callback = cb; |
| 2716 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2717 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2718 | void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) { |
| 2719 | ctx->min_version = version; |
| 2720 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2721 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2722 | void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) { |
| 2723 | ctx->max_version = version; |
| 2724 | } |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2725 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2726 | void SSL_set_min_version(SSL *ssl, uint16_t version) { |
| 2727 | ssl->min_version = version; |
| 2728 | } |
Adam Langley | 0289c73 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2729 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2730 | void SSL_set_max_version(SSL *ssl, uint16_t version) { |
| 2731 | ssl->max_version = version; |
| 2732 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2733 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2734 | void SSL_CTX_set_msg_callback(SSL_CTX *ctx, |
| 2735 | void (*cb)(int write_p, int version, |
| 2736 | int content_type, const void *buf, |
| 2737 | size_t len, SSL *ssl, void *arg)) { |
| 2738 | SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); |
| 2739 | } |
| 2740 | void SSL_set_msg_callback(SSL *ssl, |
| 2741 | void (*cb)(int write_p, int version, int content_type, |
| 2742 | const void *buf, size_t len, SSL *ssl, |
| 2743 | void *arg)) { |
| 2744 | SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb); |
| 2745 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2746 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2747 | void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) { |
| 2748 | if (ctx->keylog_bio != NULL) { |
| 2749 | BIO_free(ctx->keylog_bio); |
| 2750 | } |
| 2751 | ctx->keylog_bio = keylog_bio; |
| 2752 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2753 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2754 | static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) { |
| 2755 | static const char hextable[] = "0123456789abcdef"; |
| 2756 | uint8_t *out; |
| 2757 | size_t i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2758 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2759 | if (!CBB_add_space(cbb, &out, in_len * 2)) { |
| 2760 | return 0; |
| 2761 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2762 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2763 | for (i = 0; i < in_len; i++) { |
| 2764 | *(out++) = (uint8_t)hextable[in[i] >> 4]; |
| 2765 | *(out++) = (uint8_t)hextable[in[i] & 0xf]; |
| 2766 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2767 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2768 | return 1; |
| 2769 | } |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 2770 | |
| 2771 | int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx, |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2772 | const uint8_t *encrypted_premaster, |
| 2773 | size_t encrypted_premaster_len, |
| 2774 | const uint8_t *premaster, |
| 2775 | size_t premaster_len) { |
| 2776 | BIO *bio = ctx->keylog_bio; |
| 2777 | CBB cbb; |
| 2778 | uint8_t *out; |
| 2779 | size_t out_len; |
| 2780 | int ret; |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 2781 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2782 | if (bio == NULL) { |
| 2783 | return 1; |
| 2784 | } |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 2785 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2786 | if (encrypted_premaster_len < 8) { |
| 2787 | OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange, |
| 2788 | ERR_R_INTERNAL_ERROR); |
| 2789 | return 0; |
| 2790 | } |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 2791 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2792 | if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) { |
| 2793 | return 0; |
| 2794 | } |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 2795 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2796 | if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) || |
| 2797 | /* Only the first 8 bytes of the encrypted premaster secret are |
| 2798 | * logged. */ |
| 2799 | !cbb_add_hex(&cbb, encrypted_premaster, 8) || |
| 2800 | !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) || |
| 2801 | !cbb_add_hex(&cbb, premaster, premaster_len) || |
| 2802 | !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) || |
| 2803 | !CBB_finish(&cbb, &out, &out_len)) { |
| 2804 | CBB_cleanup(&cbb); |
| 2805 | return 0; |
| 2806 | } |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 2807 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2808 | CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); |
| 2809 | ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio); |
| 2810 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); |
David Benjamin | 859ec3c | 2014-09-02 16:29:36 -0400 | [diff] [blame] | 2811 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2812 | OPENSSL_free(out); |
| 2813 | return ret; |
Adam Langley | 95f2288 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2814 | } |
| 2815 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2816 | int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random, |
| 2817 | size_t client_random_len, const uint8_t *master, |
| 2818 | size_t master_len) { |
| 2819 | BIO *bio = ctx->keylog_bio; |
| 2820 | CBB cbb; |
| 2821 | uint8_t *out; |
| 2822 | size_t out_len; |
| 2823 | int ret; |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2824 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2825 | if (bio == NULL) { |
| 2826 | return 1; |
| 2827 | } |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2828 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2829 | if (client_random_len != 32) { |
| 2830 | OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR); |
| 2831 | return 0; |
| 2832 | } |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2833 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2834 | if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) { |
| 2835 | return 0; |
| 2836 | } |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2837 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2838 | if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) || |
| 2839 | !cbb_add_hex(&cbb, client_random, 32) || |
| 2840 | !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) || |
| 2841 | !cbb_add_hex(&cbb, master, master_len) || |
| 2842 | !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) || |
| 2843 | !CBB_finish(&cbb, &out, &out_len)) { |
| 2844 | CBB_cleanup(&cbb); |
| 2845 | return 0; |
| 2846 | } |
Adam Langley | adb739e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2847 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2848 | CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX); |
| 2849 | ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio); |
| 2850 | CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX); |
David Benjamin | e99e912 | 2014-12-11 01:46:01 -0500 | [diff] [blame] | 2851 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2852 | OPENSSL_free(out); |
| 2853 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2854 | } |
| 2855 | |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 2856 | int SSL_in_false_start(const SSL *s) { |
| 2857 | return s->s3->tmp.in_false_start; |
| 2858 | } |
| 2859 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2860 | int SSL_cutthrough_complete(const SSL *s) { |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 2861 | return SSL_in_false_start(s); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2862 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2863 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2864 | void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size, |
| 2865 | size_t *ssl_session_size) { |
| 2866 | *ssl_size = sizeof(SSL); |
| 2867 | *ssl_ctx_size = sizeof(SSL_CTX); |
| 2868 | *ssl_session_size = sizeof(SSL_SESSION); |
| 2869 | } |
Feng Lu | 41aa325 | 2014-11-21 22:47:56 -0800 | [diff] [blame] | 2870 | |
David Benjamin | ed7c475 | 2015-02-16 19:16:46 -0500 | [diff] [blame] | 2871 | int ssl3_can_false_start(const SSL *s) { |
David Benjamin | 195dc78 | 2015-02-19 13:27:05 -0500 | [diff] [blame] | 2872 | const SSL_CIPHER *const cipher = SSL_get_current_cipher(s); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2873 | |
Adam Langley | e631d96 | 2015-04-01 13:11:01 -0700 | [diff] [blame] | 2874 | /* 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] | 2875 | return !SSL_IS_DTLS(s) && |
| 2876 | SSL_version(s) >= TLS1_2_VERSION && |
| 2877 | (s->s3->alpn_selected || s->s3->next_proto_neg_seen) && |
| 2878 | cipher != NULL && |
Adam Langley | e631d96 | 2015-04-01 13:11:01 -0700 | [diff] [blame] | 2879 | cipher->algorithm_mkey == SSL_kECDHE && |
David Benjamin | 195dc78 | 2015-02-19 13:27:05 -0500 | [diff] [blame] | 2880 | (cipher->algorithm_enc == SSL_AES128GCM || |
| 2881 | cipher->algorithm_enc == SSL_AES256GCM || |
| 2882 | cipher->algorithm_enc == SSL_CHACHA20POLY1305); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) { |
| 2886 | switch (version) { |
| 2887 | case SSL3_VERSION: |
| 2888 | return &SSLv3_enc_data; |
| 2889 | |
| 2890 | case TLS1_VERSION: |
| 2891 | return &TLSv1_enc_data; |
| 2892 | |
David Benjamin | 9e13e1a | 2015-03-05 01:56:32 -0500 | [diff] [blame] | 2893 | case DTLS1_VERSION: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2894 | case TLS1_1_VERSION: |
| 2895 | return &TLSv1_1_enc_data; |
| 2896 | |
David Benjamin | 9e13e1a | 2015-03-05 01:56:32 -0500 | [diff] [blame] | 2897 | case DTLS1_2_VERSION: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2898 | case TLS1_2_VERSION: |
| 2899 | return &TLSv1_2_enc_data; |
| 2900 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2901 | default: |
| 2902 | return NULL; |
| 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | uint16_t ssl3_get_max_server_version(const SSL *s) { |
| 2907 | uint16_t max_version; |
| 2908 | |
| 2909 | if (SSL_IS_DTLS(s)) { |
| 2910 | max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION; |
| 2911 | if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) { |
| 2912 | return DTLS1_2_VERSION; |
| 2913 | } |
| 2914 | if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) { |
| 2915 | return DTLS1_VERSION; |
| 2916 | } |
| 2917 | return 0; |
| 2918 | } |
| 2919 | |
| 2920 | max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION; |
| 2921 | if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) { |
| 2922 | return TLS1_2_VERSION; |
| 2923 | } |
| 2924 | if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) { |
| 2925 | return TLS1_1_VERSION; |
| 2926 | } |
| 2927 | if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) { |
| 2928 | return TLS1_VERSION; |
| 2929 | } |
| 2930 | if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) { |
| 2931 | return SSL3_VERSION; |
| 2932 | } |
| 2933 | return 0; |
| 2934 | } |
| 2935 | |
| 2936 | uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) { |
| 2937 | uint16_t version = 0; |
| 2938 | |
| 2939 | if (SSL_IS_DTLS(s)) { |
| 2940 | /* Clamp client_version to max_version. */ |
| 2941 | if (s->max_version != 0 && client_version < s->max_version) { |
| 2942 | client_version = s->max_version; |
| 2943 | } |
| 2944 | |
| 2945 | if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) { |
| 2946 | version = DTLS1_2_VERSION; |
| 2947 | } else if (client_version <= DTLS1_VERSION && |
David Benjamin | b18f024 | 2015-03-10 18:30:08 -0400 | [diff] [blame] | 2948 | !(s->options & SSL_OP_NO_DTLSv1)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2949 | version = DTLS1_VERSION; |
| 2950 | } |
| 2951 | |
| 2952 | /* Check against min_version. */ |
| 2953 | if (version != 0 && s->min_version != 0 && version > s->min_version) { |
| 2954 | return 0; |
| 2955 | } |
| 2956 | return version; |
| 2957 | } else { |
| 2958 | /* Clamp client_version to max_version. */ |
| 2959 | if (s->max_version != 0 && client_version > s->max_version) { |
| 2960 | client_version = s->max_version; |
| 2961 | } |
| 2962 | |
| 2963 | if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) { |
| 2964 | version = TLS1_2_VERSION; |
| 2965 | } else if (client_version >= TLS1_1_VERSION && |
| 2966 | !(s->options & SSL_OP_NO_TLSv1_1)) { |
| 2967 | version = TLS1_1_VERSION; |
| 2968 | } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) { |
| 2969 | version = TLS1_VERSION; |
| 2970 | } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) { |
| 2971 | version = SSL3_VERSION; |
| 2972 | } |
| 2973 | |
| 2974 | /* Check against min_version. */ |
| 2975 | if (version != 0 && s->min_version != 0 && version < s->min_version) { |
| 2976 | return 0; |
| 2977 | } |
| 2978 | return version; |
| 2979 | } |
| 2980 | } |
| 2981 | |
| 2982 | uint16_t ssl3_get_max_client_version(SSL *s) { |
| 2983 | unsigned long options = s->options; |
| 2984 | uint16_t version = 0; |
| 2985 | |
| 2986 | /* OpenSSL's API for controlling versions entails blacklisting individual |
| 2987 | * protocols. This has two problems. First, on the client, the protocol can |
| 2988 | * only express a contiguous range of versions. Second, a library consumer |
| 2989 | * trying to set a maximum version cannot disable protocol versions that get |
| 2990 | * added in a future version of the library. |
| 2991 | * |
| 2992 | * To account for both of these, OpenSSL interprets the client-side bitmask |
| 2993 | * as a min/max range by picking the lowest contiguous non-empty range of |
| 2994 | * enabled protocols. Note that this means it is impossible to set a maximum |
| 2995 | * version of TLS 1.2 in a future-proof way. |
| 2996 | * |
| 2997 | * By this scheme, the maximum version is the lowest version V such that V is |
| 2998 | * enabled and V+1 is disabled or unimplemented. */ |
| 2999 | if (SSL_IS_DTLS(s)) { |
| 3000 | if (!(options & SSL_OP_NO_DTLSv1_2)) { |
| 3001 | version = DTLS1_2_VERSION; |
| 3002 | } |
| 3003 | if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) { |
| 3004 | version = DTLS1_VERSION; |
| 3005 | } |
| 3006 | if (s->max_version != 0 && version < s->max_version) { |
| 3007 | version = s->max_version; |
| 3008 | } |
| 3009 | } else { |
| 3010 | if (!(options & SSL_OP_NO_TLSv1_2)) { |
| 3011 | version = TLS1_2_VERSION; |
| 3012 | } |
| 3013 | if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) { |
| 3014 | version = TLS1_1_VERSION; |
| 3015 | } |
| 3016 | if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) { |
| 3017 | version = TLS1_VERSION; |
| 3018 | } |
| 3019 | if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) { |
| 3020 | version = SSL3_VERSION; |
| 3021 | } |
| 3022 | if (s->max_version != 0 && version > s->max_version) { |
| 3023 | version = s->max_version; |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | return version; |
| 3028 | } |
| 3029 | |
| 3030 | int ssl3_is_version_enabled(SSL *s, uint16_t version) { |
| 3031 | if (SSL_IS_DTLS(s)) { |
| 3032 | if (s->max_version != 0 && version < s->max_version) { |
| 3033 | return 0; |
| 3034 | } |
| 3035 | if (s->min_version != 0 && version > s->min_version) { |
| 3036 | return 0; |
| 3037 | } |
| 3038 | |
| 3039 | switch (version) { |
| 3040 | case DTLS1_VERSION: |
| 3041 | return !(s->options & SSL_OP_NO_DTLSv1); |
| 3042 | |
| 3043 | case DTLS1_2_VERSION: |
| 3044 | return !(s->options & SSL_OP_NO_DTLSv1_2); |
| 3045 | |
| 3046 | default: |
| 3047 | return 0; |
| 3048 | } |
| 3049 | } else { |
| 3050 | if (s->max_version != 0 && version > s->max_version) { |
| 3051 | return 0; |
| 3052 | } |
| 3053 | if (s->min_version != 0 && version < s->min_version) { |
| 3054 | return 0; |
| 3055 | } |
| 3056 | |
| 3057 | switch (version) { |
| 3058 | case SSL3_VERSION: |
| 3059 | return !(s->options & SSL_OP_NO_SSLv3); |
| 3060 | |
| 3061 | case TLS1_VERSION: |
| 3062 | return !(s->options & SSL_OP_NO_TLSv1); |
| 3063 | |
| 3064 | case TLS1_1_VERSION: |
| 3065 | return !(s->options & SSL_OP_NO_TLSv1_1); |
| 3066 | |
| 3067 | case TLS1_2_VERSION: |
| 3068 | return !(s->options & SSL_OP_NO_TLSv1_2); |
| 3069 | |
| 3070 | default: |
| 3071 | return 0; |
| 3072 | } |
| 3073 | } |
| 3074 | } |
| 3075 | |
David Benjamin | ea72bd0 | 2014-12-21 21:27:41 -0500 | [diff] [blame] | 3076 | uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) { |
| 3077 | if (!SSL_IS_DTLS(s)) { |
| 3078 | return wire_version; |
| 3079 | } |
| 3080 | |
| 3081 | uint16_t tls_version = ~wire_version; |
| 3082 | uint16_t version = tls_version + 0x0201; |
| 3083 | /* If either component overflowed, clamp it so comparisons still work. */ |
| 3084 | if ((version >> 8) < (tls_version >> 8)) { |
| 3085 | version = 0xff00 | (version & 0xff); |
| 3086 | } |
| 3087 | if ((version & 0xff) < (tls_version & 0xff)) { |
| 3088 | version = (version & 0xff00) | 0xff; |
| 3089 | } |
| 3090 | /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */ |
| 3091 | if (version == TLS1_VERSION) { |
| 3092 | version = TLS1_1_VERSION; |
| 3093 | } |
| 3094 | return version; |
| 3095 | } |
| 3096 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3097 | int SSL_cache_hit(SSL *s) { return s->hit; } |
| 3098 | |
| 3099 | int SSL_is_server(SSL *s) { return s->server; } |
| 3100 | |
Adam Langley | 524e717 | 2015-02-20 16:04:00 -0800 | [diff] [blame] | 3101 | void SSL_CTX_set_dos_protection_cb( |
| 3102 | SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) { |
| 3103 | ctx->dos_protection_cb = cb; |
| 3104 | } |
| 3105 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 3106 | void SSL_enable_fastradio_padding(SSL *s, char on_off) { |
| 3107 | s->fastradio_padding = on_off; |
| 3108 | } |
Adam Langley | 4497294 | 2015-02-20 13:43:23 -0800 | [diff] [blame] | 3109 | |
| 3110 | const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) { |
| 3111 | return ssl3_get_cipher_by_value(value); |
| 3112 | } |
Adam Langley | 3f92d21 | 2015-02-20 15:32:52 -0800 | [diff] [blame] | 3113 | |
| 3114 | int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key, |
| 3115 | const RC4_KEY **write_key) { |
| 3116 | if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) { |
| 3117 | return 0; |
| 3118 | } |
| 3119 | |
| 3120 | return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) && |
| 3121 | EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key); |
| 3122 | } |