1*0a6a1f1dSLionel Sambuc /*
2*0a6a1f1dSLionel Sambuc * ! \file ssl/ssl_lib.c \brief Version independent SSL functions.
3ebfedea0SLionel Sambuc */
4ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5ebfedea0SLionel Sambuc * All rights reserved.
6ebfedea0SLionel Sambuc *
7ebfedea0SLionel Sambuc * This package is an SSL implementation written
8ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
9ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
10ebfedea0SLionel Sambuc *
11ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
12ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
13ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
14ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
15ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
16ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
17ebfedea0SLionel Sambuc *
18ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
19ebfedea0SLionel Sambuc * the code are not to be removed.
20ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
21ebfedea0SLionel Sambuc * as the author of the parts of the library used.
22ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
23ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
24ebfedea0SLionel Sambuc *
25ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
26ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
27ebfedea0SLionel Sambuc * are met:
28ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
29ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
30ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
31ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
32ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
33ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
34ebfedea0SLionel Sambuc * must display the following acknowledgement:
35ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
36ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
37ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
38ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
39ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
40ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
41ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
42ebfedea0SLionel Sambuc *
43ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
44ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53ebfedea0SLionel Sambuc * SUCH DAMAGE.
54ebfedea0SLionel Sambuc *
55ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
56ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
57ebfedea0SLionel Sambuc * copied and put under another distribution licence
58ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
59ebfedea0SLionel Sambuc */
60ebfedea0SLionel Sambuc /* ====================================================================
61ebfedea0SLionel Sambuc * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
62ebfedea0SLionel Sambuc *
63ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
64ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
65ebfedea0SLionel Sambuc * are met:
66ebfedea0SLionel Sambuc *
67ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
68ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
69ebfedea0SLionel Sambuc *
70ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
71ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
72ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
73ebfedea0SLionel Sambuc * distribution.
74ebfedea0SLionel Sambuc *
75ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
76ebfedea0SLionel Sambuc * software must display the following acknowledgment:
77ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
78ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
79ebfedea0SLionel Sambuc *
80ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
81ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
82ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
83ebfedea0SLionel Sambuc * openssl-core@openssl.org.
84ebfedea0SLionel Sambuc *
85ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
86ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
87ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
88ebfedea0SLionel Sambuc *
89ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
90ebfedea0SLionel Sambuc * acknowledgment:
91ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
92ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
93ebfedea0SLionel Sambuc *
94ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
95ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
97ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
98ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
99ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
100ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
101ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
102ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
103ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
104ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
105ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
106ebfedea0SLionel Sambuc * ====================================================================
107ebfedea0SLionel Sambuc *
108ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
109ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
110ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
111ebfedea0SLionel Sambuc *
112ebfedea0SLionel Sambuc */
113ebfedea0SLionel Sambuc /* ====================================================================
114ebfedea0SLionel Sambuc * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
115ebfedea0SLionel Sambuc * ECC cipher suite support in OpenSSL originally developed by
116ebfedea0SLionel Sambuc * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
117ebfedea0SLionel Sambuc */
118ebfedea0SLionel Sambuc /* ====================================================================
119ebfedea0SLionel Sambuc * Copyright 2005 Nokia. All rights reserved.
120ebfedea0SLionel Sambuc *
121ebfedea0SLionel Sambuc * The portions of the attached software ("Contribution") is developed by
122ebfedea0SLionel Sambuc * Nokia Corporation and is licensed pursuant to the OpenSSL open source
123ebfedea0SLionel Sambuc * license.
124ebfedea0SLionel Sambuc *
125ebfedea0SLionel Sambuc * The Contribution, originally written by Mika Kousa and Pasi Eronen of
126ebfedea0SLionel Sambuc * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
127ebfedea0SLionel Sambuc * support (see RFC 4279) to OpenSSL.
128ebfedea0SLionel Sambuc *
129ebfedea0SLionel Sambuc * No patent licenses or other rights except those expressly stated in
130ebfedea0SLionel Sambuc * the OpenSSL open source license shall be deemed granted or received
131ebfedea0SLionel Sambuc * expressly, by implication, estoppel, or otherwise.
132ebfedea0SLionel Sambuc *
133ebfedea0SLionel Sambuc * No assurances are provided by Nokia that the Contribution does not
134ebfedea0SLionel Sambuc * infringe the patent or other intellectual property rights of any third
135ebfedea0SLionel Sambuc * party or that the license provides you with all the necessary rights
136ebfedea0SLionel Sambuc * to make use of the Contribution.
137ebfedea0SLionel Sambuc *
138ebfedea0SLionel Sambuc * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
139ebfedea0SLionel Sambuc * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
140ebfedea0SLionel Sambuc * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
141ebfedea0SLionel Sambuc * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
142ebfedea0SLionel Sambuc * OTHERWISE.
143ebfedea0SLionel Sambuc */
144ebfedea0SLionel Sambuc
145ebfedea0SLionel Sambuc #ifdef REF_CHECK
146ebfedea0SLionel Sambuc # include <assert.h>
147ebfedea0SLionel Sambuc #endif
148ebfedea0SLionel Sambuc #include <stdio.h>
149ebfedea0SLionel Sambuc #include "ssl_locl.h"
150ebfedea0SLionel Sambuc #include "kssl_lcl.h"
151ebfedea0SLionel Sambuc #include <openssl/objects.h>
152ebfedea0SLionel Sambuc #include <openssl/lhash.h>
153ebfedea0SLionel Sambuc #include <openssl/x509v3.h>
154ebfedea0SLionel Sambuc #include <openssl/rand.h>
155ebfedea0SLionel Sambuc #include <openssl/ocsp.h>
156ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
157ebfedea0SLionel Sambuc # include <openssl/dh.h>
158ebfedea0SLionel Sambuc #endif
159ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
160ebfedea0SLionel Sambuc # include <openssl/engine.h>
161ebfedea0SLionel Sambuc #endif
162ebfedea0SLionel Sambuc
163ebfedea0SLionel Sambuc const char *SSL_version_str = OPENSSL_VERSION_TEXT;
164ebfedea0SLionel Sambuc
165ebfedea0SLionel Sambuc SSL3_ENC_METHOD ssl3_undef_enc_method = {
166*0a6a1f1dSLionel Sambuc /*
167*0a6a1f1dSLionel Sambuc * evil casts, but these functions are only called if there's a library
168*0a6a1f1dSLionel Sambuc * bug
169*0a6a1f1dSLionel Sambuc */
170ebfedea0SLionel Sambuc (int (*)(SSL *, int))ssl_undefined_function,
171ebfedea0SLionel Sambuc (int (*)(SSL *, unsigned char *, int))ssl_undefined_function,
172ebfedea0SLionel Sambuc ssl_undefined_function,
173*0a6a1f1dSLionel Sambuc (int (*)(SSL *, unsigned char *, unsigned char *, int))
174*0a6a1f1dSLionel Sambuc ssl_undefined_function,
175ebfedea0SLionel Sambuc (int (*)(SSL *, int))ssl_undefined_function,
176*0a6a1f1dSLionel Sambuc (int (*)(SSL *, const char *, int, unsigned char *))
177*0a6a1f1dSLionel Sambuc ssl_undefined_function,
178ebfedea0SLionel Sambuc 0, /* finish_mac_length */
179ebfedea0SLionel Sambuc (int (*)(SSL *, int, unsigned char *))ssl_undefined_function,
180ebfedea0SLionel Sambuc NULL, /* client_finished_label */
181ebfedea0SLionel Sambuc 0, /* client_finished_label_len */
182ebfedea0SLionel Sambuc NULL, /* server_finished_label */
183ebfedea0SLionel Sambuc 0, /* server_finished_label_len */
184ebfedea0SLionel Sambuc (int (*)(int))ssl_undefined_function,
185ebfedea0SLionel Sambuc (int (*)(SSL *, unsigned char *, size_t, const char *,
186ebfedea0SLionel Sambuc size_t, const unsigned char *, size_t,
187ebfedea0SLionel Sambuc int use_context))ssl_undefined_function,
188ebfedea0SLionel Sambuc };
189ebfedea0SLionel Sambuc
SSL_clear(SSL * s)190ebfedea0SLionel Sambuc int SSL_clear(SSL *s)
191ebfedea0SLionel Sambuc {
192ebfedea0SLionel Sambuc
193*0a6a1f1dSLionel Sambuc if (s->method == NULL) {
194ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CLEAR, SSL_R_NO_METHOD_SPECIFIED);
195ebfedea0SLionel Sambuc return (0);
196ebfedea0SLionel Sambuc }
197ebfedea0SLionel Sambuc
198*0a6a1f1dSLionel Sambuc if (ssl_clear_bad_session(s)) {
199ebfedea0SLionel Sambuc SSL_SESSION_free(s->session);
200ebfedea0SLionel Sambuc s->session = NULL;
201ebfedea0SLionel Sambuc }
202ebfedea0SLionel Sambuc
203ebfedea0SLionel Sambuc s->error = 0;
204ebfedea0SLionel Sambuc s->hit = 0;
205ebfedea0SLionel Sambuc s->shutdown = 0;
206ebfedea0SLionel Sambuc
207*0a6a1f1dSLionel Sambuc #if 0
208*0a6a1f1dSLionel Sambuc /*
209*0a6a1f1dSLionel Sambuc * Disabled since version 1.10 of this file (early return not
210*0a6a1f1dSLionel Sambuc * needed because SSL_clear is not called when doing renegotiation)
211*0a6a1f1dSLionel Sambuc */
212*0a6a1f1dSLionel Sambuc /*
213*0a6a1f1dSLionel Sambuc * This is set if we are doing dynamic renegotiation so keep
214*0a6a1f1dSLionel Sambuc * the old cipher. It is sort of a SSL_clear_lite :-)
215*0a6a1f1dSLionel Sambuc */
216ebfedea0SLionel Sambuc if (s->renegotiate)
217*0a6a1f1dSLionel Sambuc return (1);
218*0a6a1f1dSLionel Sambuc #else
219*0a6a1f1dSLionel Sambuc if (s->renegotiate) {
220ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CLEAR, ERR_R_INTERNAL_ERROR);
221ebfedea0SLionel Sambuc return 0;
222ebfedea0SLionel Sambuc }
223ebfedea0SLionel Sambuc #endif
224ebfedea0SLionel Sambuc
225ebfedea0SLionel Sambuc s->type = 0;
226ebfedea0SLionel Sambuc
227ebfedea0SLionel Sambuc s->state = SSL_ST_BEFORE | ((s->server) ? SSL_ST_ACCEPT : SSL_ST_CONNECT);
228ebfedea0SLionel Sambuc
229ebfedea0SLionel Sambuc s->version = s->method->version;
230ebfedea0SLionel Sambuc s->client_version = s->version;
231ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
232ebfedea0SLionel Sambuc s->rstate = SSL_ST_READ_HEADER;
233ebfedea0SLionel Sambuc #if 0
234ebfedea0SLionel Sambuc s->read_ahead = s->ctx->read_ahead;
235ebfedea0SLionel Sambuc #endif
236ebfedea0SLionel Sambuc
237*0a6a1f1dSLionel Sambuc if (s->init_buf != NULL) {
238ebfedea0SLionel Sambuc BUF_MEM_free(s->init_buf);
239ebfedea0SLionel Sambuc s->init_buf = NULL;
240ebfedea0SLionel Sambuc }
241ebfedea0SLionel Sambuc
242ebfedea0SLionel Sambuc ssl_clear_cipher_ctx(s);
243ebfedea0SLionel Sambuc ssl_clear_hash_ctx(&s->read_hash);
244ebfedea0SLionel Sambuc ssl_clear_hash_ctx(&s->write_hash);
245ebfedea0SLionel Sambuc
246ebfedea0SLionel Sambuc s->first_packet = 0;
247ebfedea0SLionel Sambuc
248ebfedea0SLionel Sambuc #if 1
249*0a6a1f1dSLionel Sambuc /*
250*0a6a1f1dSLionel Sambuc * Check to see if we were changed into a different method, if so, revert
251*0a6a1f1dSLionel Sambuc * back if we are not doing session-id reuse.
252*0a6a1f1dSLionel Sambuc */
253*0a6a1f1dSLionel Sambuc if (!s->in_handshake && (s->session == NULL)
254*0a6a1f1dSLionel Sambuc && (s->method != s->ctx->method)) {
255ebfedea0SLionel Sambuc s->method->ssl_free(s);
256ebfedea0SLionel Sambuc s->method = s->ctx->method;
257ebfedea0SLionel Sambuc if (!s->method->ssl_new(s))
258ebfedea0SLionel Sambuc return (0);
259*0a6a1f1dSLionel Sambuc } else
260ebfedea0SLionel Sambuc #endif
261ebfedea0SLionel Sambuc s->method->ssl_clear(s);
262ebfedea0SLionel Sambuc return (1);
263ebfedea0SLionel Sambuc }
264ebfedea0SLionel Sambuc
265ebfedea0SLionel Sambuc /** Used to change an SSL_CTXs default SSL method type */
SSL_CTX_set_ssl_version(SSL_CTX * ctx,const SSL_METHOD * meth)266ebfedea0SLionel Sambuc int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
267ebfedea0SLionel Sambuc {
268ebfedea0SLionel Sambuc STACK_OF(SSL_CIPHER) *sk;
269ebfedea0SLionel Sambuc
270ebfedea0SLionel Sambuc ctx->method = meth;
271ebfedea0SLionel Sambuc
272ebfedea0SLionel Sambuc sk = ssl_create_cipher_list(ctx->method, &(ctx->cipher_list),
273ebfedea0SLionel Sambuc &(ctx->cipher_list_by_id),
274*0a6a1f1dSLionel Sambuc meth->version ==
275*0a6a1f1dSLionel Sambuc SSL2_VERSION ? "SSLv2" :
276*0a6a1f1dSLionel Sambuc SSL_DEFAULT_CIPHER_LIST);
277*0a6a1f1dSLionel Sambuc if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) {
278*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION,
279*0a6a1f1dSLionel Sambuc SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
280ebfedea0SLionel Sambuc return (0);
281ebfedea0SLionel Sambuc }
282ebfedea0SLionel Sambuc return (1);
283ebfedea0SLionel Sambuc }
284ebfedea0SLionel Sambuc
SSL_new(SSL_CTX * ctx)285ebfedea0SLionel Sambuc SSL *SSL_new(SSL_CTX *ctx)
286ebfedea0SLionel Sambuc {
287ebfedea0SLionel Sambuc SSL *s;
288ebfedea0SLionel Sambuc
289*0a6a1f1dSLionel Sambuc if (ctx == NULL) {
290ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);
291ebfedea0SLionel Sambuc return (NULL);
292ebfedea0SLionel Sambuc }
293*0a6a1f1dSLionel Sambuc if (ctx->method == NULL) {
294ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
295ebfedea0SLionel Sambuc return (NULL);
296ebfedea0SLionel Sambuc }
297ebfedea0SLionel Sambuc
298ebfedea0SLionel Sambuc s = (SSL *)OPENSSL_malloc(sizeof(SSL));
299*0a6a1f1dSLionel Sambuc if (s == NULL)
300*0a6a1f1dSLionel Sambuc goto err;
301ebfedea0SLionel Sambuc memset(s, 0, sizeof(SSL));
302ebfedea0SLionel Sambuc
303ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_KRB5
304ebfedea0SLionel Sambuc s->kssl_ctx = kssl_ctx_new();
305ebfedea0SLionel Sambuc #endif /* OPENSSL_NO_KRB5 */
306ebfedea0SLionel Sambuc
307ebfedea0SLionel Sambuc s->options = ctx->options;
308ebfedea0SLionel Sambuc s->mode = ctx->mode;
309ebfedea0SLionel Sambuc s->max_cert_list = ctx->max_cert_list;
310ebfedea0SLionel Sambuc
311*0a6a1f1dSLionel Sambuc if (ctx->cert != NULL) {
312*0a6a1f1dSLionel Sambuc /*
313*0a6a1f1dSLionel Sambuc * Earlier library versions used to copy the pointer to the CERT, not
314*0a6a1f1dSLionel Sambuc * its contents; only when setting new parameters for the per-SSL
315*0a6a1f1dSLionel Sambuc * copy, ssl_cert_new would be called (and the direct reference to
316*0a6a1f1dSLionel Sambuc * the per-SSL_CTX settings would be lost, but those still were
317*0a6a1f1dSLionel Sambuc * indirectly accessed for various purposes, and for that reason they
318*0a6a1f1dSLionel Sambuc * used to be known as s->ctx->default_cert). Now we don't look at the
319*0a6a1f1dSLionel Sambuc * SSL_CTX's CERT after having duplicated it once.
320*0a6a1f1dSLionel Sambuc */
321ebfedea0SLionel Sambuc
322ebfedea0SLionel Sambuc s->cert = ssl_cert_dup(ctx->cert);
323ebfedea0SLionel Sambuc if (s->cert == NULL)
324ebfedea0SLionel Sambuc goto err;
325*0a6a1f1dSLionel Sambuc } else
326ebfedea0SLionel Sambuc s->cert = NULL; /* Cannot really happen (see SSL_CTX_new) */
327ebfedea0SLionel Sambuc
328ebfedea0SLionel Sambuc s->read_ahead = ctx->read_ahead;
329ebfedea0SLionel Sambuc s->msg_callback = ctx->msg_callback;
330ebfedea0SLionel Sambuc s->msg_callback_arg = ctx->msg_callback_arg;
331ebfedea0SLionel Sambuc s->verify_mode = ctx->verify_mode;
332ebfedea0SLionel Sambuc #if 0
333ebfedea0SLionel Sambuc s->verify_depth = ctx->verify_depth;
334ebfedea0SLionel Sambuc #endif
335ebfedea0SLionel Sambuc s->sid_ctx_length = ctx->sid_ctx_length;
336ebfedea0SLionel Sambuc OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
337ebfedea0SLionel Sambuc memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
338ebfedea0SLionel Sambuc s->verify_callback = ctx->default_verify_callback;
339ebfedea0SLionel Sambuc s->generate_session_id = ctx->generate_session_id;
340ebfedea0SLionel Sambuc
341ebfedea0SLionel Sambuc s->param = X509_VERIFY_PARAM_new();
342ebfedea0SLionel Sambuc if (!s->param)
343ebfedea0SLionel Sambuc goto err;
344ebfedea0SLionel Sambuc X509_VERIFY_PARAM_inherit(s->param, ctx->param);
345ebfedea0SLionel Sambuc #if 0
346ebfedea0SLionel Sambuc s->purpose = ctx->purpose;
347ebfedea0SLionel Sambuc s->trust = ctx->trust;
348ebfedea0SLionel Sambuc #endif
349ebfedea0SLionel Sambuc s->quiet_shutdown = ctx->quiet_shutdown;
350ebfedea0SLionel Sambuc s->max_send_fragment = ctx->max_send_fragment;
351ebfedea0SLionel Sambuc
352ebfedea0SLionel Sambuc CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
353ebfedea0SLionel Sambuc s->ctx = ctx;
354ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
355ebfedea0SLionel Sambuc s->tlsext_debug_cb = 0;
356ebfedea0SLionel Sambuc s->tlsext_debug_arg = NULL;
357ebfedea0SLionel Sambuc s->tlsext_ticket_expected = 0;
358ebfedea0SLionel Sambuc s->tlsext_status_type = -1;
359ebfedea0SLionel Sambuc s->tlsext_status_expected = 0;
360ebfedea0SLionel Sambuc s->tlsext_ocsp_ids = NULL;
361ebfedea0SLionel Sambuc s->tlsext_ocsp_exts = NULL;
362ebfedea0SLionel Sambuc s->tlsext_ocsp_resp = NULL;
363ebfedea0SLionel Sambuc s->tlsext_ocsp_resplen = -1;
364ebfedea0SLionel Sambuc CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
365ebfedea0SLionel Sambuc s->initial_ctx = ctx;
366ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_NEXTPROTONEG
367ebfedea0SLionel Sambuc s->next_proto_negotiated = NULL;
368ebfedea0SLionel Sambuc # endif
369ebfedea0SLionel Sambuc #endif
370ebfedea0SLionel Sambuc
371ebfedea0SLionel Sambuc s->verify_result = X509_V_OK;
372ebfedea0SLionel Sambuc
373ebfedea0SLionel Sambuc s->method = ctx->method;
374ebfedea0SLionel Sambuc
375ebfedea0SLionel Sambuc if (!s->method->ssl_new(s))
376ebfedea0SLionel Sambuc goto err;
377ebfedea0SLionel Sambuc
378ebfedea0SLionel Sambuc s->references = 1;
379ebfedea0SLionel Sambuc s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;
380ebfedea0SLionel Sambuc
381ebfedea0SLionel Sambuc SSL_clear(s);
382ebfedea0SLionel Sambuc
383ebfedea0SLionel Sambuc CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
384ebfedea0SLionel Sambuc
385ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
386ebfedea0SLionel Sambuc s->psk_client_callback = ctx->psk_client_callback;
387ebfedea0SLionel Sambuc s->psk_server_callback = ctx->psk_server_callback;
388ebfedea0SLionel Sambuc #endif
389ebfedea0SLionel Sambuc
390ebfedea0SLionel Sambuc return (s);
391ebfedea0SLionel Sambuc err:
392ebfedea0SLionel Sambuc if (s != NULL)
393*0a6a1f1dSLionel Sambuc SSL_free(s);
394ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
395ebfedea0SLionel Sambuc return (NULL);
396ebfedea0SLionel Sambuc }
397ebfedea0SLionel Sambuc
SSL_CTX_set_session_id_context(SSL_CTX * ctx,const unsigned char * sid_ctx,unsigned int sid_ctx_len)398ebfedea0SLionel Sambuc int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
399ebfedea0SLionel Sambuc unsigned int sid_ctx_len)
400ebfedea0SLionel Sambuc {
401*0a6a1f1dSLionel Sambuc if (sid_ctx_len > sizeof ctx->sid_ctx) {
402*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,
403*0a6a1f1dSLionel Sambuc SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
404ebfedea0SLionel Sambuc return 0;
405ebfedea0SLionel Sambuc }
406ebfedea0SLionel Sambuc ctx->sid_ctx_length = sid_ctx_len;
407ebfedea0SLionel Sambuc memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
408ebfedea0SLionel Sambuc
409ebfedea0SLionel Sambuc return 1;
410ebfedea0SLionel Sambuc }
411ebfedea0SLionel Sambuc
SSL_set_session_id_context(SSL * ssl,const unsigned char * sid_ctx,unsigned int sid_ctx_len)412ebfedea0SLionel Sambuc int SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx,
413ebfedea0SLionel Sambuc unsigned int sid_ctx_len)
414ebfedea0SLionel Sambuc {
415*0a6a1f1dSLionel Sambuc if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
416*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,
417*0a6a1f1dSLionel Sambuc SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
418ebfedea0SLionel Sambuc return 0;
419ebfedea0SLionel Sambuc }
420ebfedea0SLionel Sambuc ssl->sid_ctx_length = sid_ctx_len;
421ebfedea0SLionel Sambuc memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
422ebfedea0SLionel Sambuc
423ebfedea0SLionel Sambuc return 1;
424ebfedea0SLionel Sambuc }
425ebfedea0SLionel Sambuc
SSL_CTX_set_generate_session_id(SSL_CTX * ctx,GEN_SESSION_CB cb)426ebfedea0SLionel Sambuc int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
427ebfedea0SLionel Sambuc {
428ebfedea0SLionel Sambuc CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
429ebfedea0SLionel Sambuc ctx->generate_session_id = cb;
430ebfedea0SLionel Sambuc CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
431ebfedea0SLionel Sambuc return 1;
432ebfedea0SLionel Sambuc }
433ebfedea0SLionel Sambuc
SSL_set_generate_session_id(SSL * ssl,GEN_SESSION_CB cb)434ebfedea0SLionel Sambuc int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
435ebfedea0SLionel Sambuc {
436ebfedea0SLionel Sambuc CRYPTO_w_lock(CRYPTO_LOCK_SSL);
437ebfedea0SLionel Sambuc ssl->generate_session_id = cb;
438ebfedea0SLionel Sambuc CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
439ebfedea0SLionel Sambuc return 1;
440ebfedea0SLionel Sambuc }
441ebfedea0SLionel Sambuc
SSL_has_matching_session_id(const SSL * ssl,const unsigned char * id,unsigned int id_len)442ebfedea0SLionel Sambuc int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
443ebfedea0SLionel Sambuc unsigned int id_len)
444ebfedea0SLionel Sambuc {
445*0a6a1f1dSLionel Sambuc /*
446*0a6a1f1dSLionel Sambuc * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
447ebfedea0SLionel Sambuc * we can "construct" a session to give us the desired check - ie. to
448ebfedea0SLionel Sambuc * find if there's a session in the hash table that would conflict with
449*0a6a1f1dSLionel Sambuc * any new session built out of this id/id_len and the ssl_version in use
450*0a6a1f1dSLionel Sambuc * by this SSL.
451*0a6a1f1dSLionel Sambuc */
452ebfedea0SLionel Sambuc SSL_SESSION r, *p;
453ebfedea0SLionel Sambuc
454ebfedea0SLionel Sambuc if (id_len > sizeof r.session_id)
455ebfedea0SLionel Sambuc return 0;
456ebfedea0SLionel Sambuc
457ebfedea0SLionel Sambuc r.ssl_version = ssl->version;
458ebfedea0SLionel Sambuc r.session_id_length = id_len;
459ebfedea0SLionel Sambuc memcpy(r.session_id, id, id_len);
460*0a6a1f1dSLionel Sambuc /*
461*0a6a1f1dSLionel Sambuc * NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
462ebfedea0SLionel Sambuc * callback is calling us to check the uniqueness of a shorter ID, it
463ebfedea0SLionel Sambuc * must be compared as a padded-out ID because that is what it will be
464*0a6a1f1dSLionel Sambuc * converted to when the callback has finished choosing it.
465*0a6a1f1dSLionel Sambuc */
466ebfedea0SLionel Sambuc if ((r.ssl_version == SSL2_VERSION) &&
467*0a6a1f1dSLionel Sambuc (id_len < SSL2_SSL_SESSION_ID_LENGTH)) {
468*0a6a1f1dSLionel Sambuc memset(r.session_id + id_len, 0, SSL2_SSL_SESSION_ID_LENGTH - id_len);
469ebfedea0SLionel Sambuc r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
470ebfedea0SLionel Sambuc }
471ebfedea0SLionel Sambuc
472ebfedea0SLionel Sambuc CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
473ebfedea0SLionel Sambuc p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
474ebfedea0SLionel Sambuc CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
475ebfedea0SLionel Sambuc return (p != NULL);
476ebfedea0SLionel Sambuc }
477ebfedea0SLionel Sambuc
SSL_CTX_set_purpose(SSL_CTX * s,int purpose)478ebfedea0SLionel Sambuc int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
479ebfedea0SLionel Sambuc {
480ebfedea0SLionel Sambuc return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
481ebfedea0SLionel Sambuc }
482ebfedea0SLionel Sambuc
SSL_set_purpose(SSL * s,int purpose)483ebfedea0SLionel Sambuc int SSL_set_purpose(SSL *s, int purpose)
484ebfedea0SLionel Sambuc {
485ebfedea0SLionel Sambuc return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
486ebfedea0SLionel Sambuc }
487ebfedea0SLionel Sambuc
SSL_CTX_set_trust(SSL_CTX * s,int trust)488ebfedea0SLionel Sambuc int SSL_CTX_set_trust(SSL_CTX *s, int trust)
489ebfedea0SLionel Sambuc {
490ebfedea0SLionel Sambuc return X509_VERIFY_PARAM_set_trust(s->param, trust);
491ebfedea0SLionel Sambuc }
492ebfedea0SLionel Sambuc
SSL_set_trust(SSL * s,int trust)493ebfedea0SLionel Sambuc int SSL_set_trust(SSL *s, int trust)
494ebfedea0SLionel Sambuc {
495ebfedea0SLionel Sambuc return X509_VERIFY_PARAM_set_trust(s->param, trust);
496ebfedea0SLionel Sambuc }
497ebfedea0SLionel Sambuc
SSL_CTX_set1_param(SSL_CTX * ctx,X509_VERIFY_PARAM * vpm)498ebfedea0SLionel Sambuc int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
499ebfedea0SLionel Sambuc {
500ebfedea0SLionel Sambuc return X509_VERIFY_PARAM_set1(ctx->param, vpm);
501ebfedea0SLionel Sambuc }
502ebfedea0SLionel Sambuc
SSL_set1_param(SSL * ssl,X509_VERIFY_PARAM * vpm)503ebfedea0SLionel Sambuc int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
504ebfedea0SLionel Sambuc {
505ebfedea0SLionel Sambuc return X509_VERIFY_PARAM_set1(ssl->param, vpm);
506ebfedea0SLionel Sambuc }
507ebfedea0SLionel Sambuc
SSL_free(SSL * s)508ebfedea0SLionel Sambuc void SSL_free(SSL *s)
509ebfedea0SLionel Sambuc {
510ebfedea0SLionel Sambuc int i;
511ebfedea0SLionel Sambuc
512ebfedea0SLionel Sambuc if (s == NULL)
513ebfedea0SLionel Sambuc return;
514ebfedea0SLionel Sambuc
515ebfedea0SLionel Sambuc i = CRYPTO_add(&s->references, -1, CRYPTO_LOCK_SSL);
516ebfedea0SLionel Sambuc #ifdef REF_PRINT
517ebfedea0SLionel Sambuc REF_PRINT("SSL", s);
518ebfedea0SLionel Sambuc #endif
519*0a6a1f1dSLionel Sambuc if (i > 0)
520*0a6a1f1dSLionel Sambuc return;
521ebfedea0SLionel Sambuc #ifdef REF_CHECK
522*0a6a1f1dSLionel Sambuc if (i < 0) {
523ebfedea0SLionel Sambuc fprintf(stderr, "SSL_free, bad reference count\n");
524ebfedea0SLionel Sambuc abort(); /* ok */
525ebfedea0SLionel Sambuc }
526ebfedea0SLionel Sambuc #endif
527ebfedea0SLionel Sambuc
528ebfedea0SLionel Sambuc if (s->param)
529ebfedea0SLionel Sambuc X509_VERIFY_PARAM_free(s->param);
530ebfedea0SLionel Sambuc
531ebfedea0SLionel Sambuc CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
532ebfedea0SLionel Sambuc
533*0a6a1f1dSLionel Sambuc if (s->bbio != NULL) {
534ebfedea0SLionel Sambuc /* If the buffering BIO is in place, pop it off */
535*0a6a1f1dSLionel Sambuc if (s->bbio == s->wbio) {
536ebfedea0SLionel Sambuc s->wbio = BIO_pop(s->wbio);
537ebfedea0SLionel Sambuc }
538ebfedea0SLionel Sambuc BIO_free(s->bbio);
539ebfedea0SLionel Sambuc s->bbio = NULL;
540ebfedea0SLionel Sambuc }
541ebfedea0SLionel Sambuc if (s->rbio != NULL)
542ebfedea0SLionel Sambuc BIO_free_all(s->rbio);
543ebfedea0SLionel Sambuc if ((s->wbio != NULL) && (s->wbio != s->rbio))
544ebfedea0SLionel Sambuc BIO_free_all(s->wbio);
545ebfedea0SLionel Sambuc
546*0a6a1f1dSLionel Sambuc if (s->init_buf != NULL)
547*0a6a1f1dSLionel Sambuc BUF_MEM_free(s->init_buf);
548ebfedea0SLionel Sambuc
549ebfedea0SLionel Sambuc /* add extra stuff */
550*0a6a1f1dSLionel Sambuc if (s->cipher_list != NULL)
551*0a6a1f1dSLionel Sambuc sk_SSL_CIPHER_free(s->cipher_list);
552*0a6a1f1dSLionel Sambuc if (s->cipher_list_by_id != NULL)
553*0a6a1f1dSLionel Sambuc sk_SSL_CIPHER_free(s->cipher_list_by_id);
554ebfedea0SLionel Sambuc
555ebfedea0SLionel Sambuc /* Make the next call work :-) */
556*0a6a1f1dSLionel Sambuc if (s->session != NULL) {
557ebfedea0SLionel Sambuc ssl_clear_bad_session(s);
558ebfedea0SLionel Sambuc SSL_SESSION_free(s->session);
559ebfedea0SLionel Sambuc }
560ebfedea0SLionel Sambuc
561ebfedea0SLionel Sambuc ssl_clear_cipher_ctx(s);
562ebfedea0SLionel Sambuc ssl_clear_hash_ctx(&s->read_hash);
563ebfedea0SLionel Sambuc ssl_clear_hash_ctx(&s->write_hash);
564ebfedea0SLionel Sambuc
565*0a6a1f1dSLionel Sambuc if (s->cert != NULL)
566*0a6a1f1dSLionel Sambuc ssl_cert_free(s->cert);
567ebfedea0SLionel Sambuc /* Free up if allocated */
568ebfedea0SLionel Sambuc
569ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
570ebfedea0SLionel Sambuc if (s->tlsext_hostname)
571ebfedea0SLionel Sambuc OPENSSL_free(s->tlsext_hostname);
572*0a6a1f1dSLionel Sambuc if (s->initial_ctx)
573*0a6a1f1dSLionel Sambuc SSL_CTX_free(s->initial_ctx);
574ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_EC
575*0a6a1f1dSLionel Sambuc if (s->tlsext_ecpointformatlist)
576*0a6a1f1dSLionel Sambuc OPENSSL_free(s->tlsext_ecpointformatlist);
577*0a6a1f1dSLionel Sambuc if (s->tlsext_ellipticcurvelist)
578*0a6a1f1dSLionel Sambuc OPENSSL_free(s->tlsext_ellipticcurvelist);
579ebfedea0SLionel Sambuc # endif /* OPENSSL_NO_EC */
580*0a6a1f1dSLionel Sambuc if (s->tlsext_opaque_prf_input)
581*0a6a1f1dSLionel Sambuc OPENSSL_free(s->tlsext_opaque_prf_input);
582ebfedea0SLionel Sambuc if (s->tlsext_ocsp_exts)
583*0a6a1f1dSLionel Sambuc sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts, X509_EXTENSION_free);
584ebfedea0SLionel Sambuc if (s->tlsext_ocsp_ids)
585ebfedea0SLionel Sambuc sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);
586ebfedea0SLionel Sambuc if (s->tlsext_ocsp_resp)
587ebfedea0SLionel Sambuc OPENSSL_free(s->tlsext_ocsp_resp);
588ebfedea0SLionel Sambuc #endif
589ebfedea0SLionel Sambuc
590ebfedea0SLionel Sambuc if (s->client_CA != NULL)
591ebfedea0SLionel Sambuc sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
592ebfedea0SLionel Sambuc
593*0a6a1f1dSLionel Sambuc if (s->method != NULL)
594*0a6a1f1dSLionel Sambuc s->method->ssl_free(s);
595ebfedea0SLionel Sambuc
596*0a6a1f1dSLionel Sambuc if (s->ctx)
597*0a6a1f1dSLionel Sambuc SSL_CTX_free(s->ctx);
598ebfedea0SLionel Sambuc
599ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_KRB5
600ebfedea0SLionel Sambuc if (s->kssl_ctx != NULL)
601ebfedea0SLionel Sambuc kssl_ctx_free(s->kssl_ctx);
602ebfedea0SLionel Sambuc #endif /* OPENSSL_NO_KRB5 */
603ebfedea0SLionel Sambuc
604ebfedea0SLionel Sambuc #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
605ebfedea0SLionel Sambuc if (s->next_proto_negotiated)
606ebfedea0SLionel Sambuc OPENSSL_free(s->next_proto_negotiated);
607ebfedea0SLionel Sambuc #endif
608ebfedea0SLionel Sambuc
609ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SRTP
610ebfedea0SLionel Sambuc if (s->srtp_profiles)
611ebfedea0SLionel Sambuc sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
612ebfedea0SLionel Sambuc #endif
613ebfedea0SLionel Sambuc
614ebfedea0SLionel Sambuc OPENSSL_free(s);
615ebfedea0SLionel Sambuc }
616ebfedea0SLionel Sambuc
SSL_set_bio(SSL * s,BIO * rbio,BIO * wbio)617ebfedea0SLionel Sambuc void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio)
618ebfedea0SLionel Sambuc {
619*0a6a1f1dSLionel Sambuc /*
620*0a6a1f1dSLionel Sambuc * If the output buffering BIO is still in place, remove it
621ebfedea0SLionel Sambuc */
622*0a6a1f1dSLionel Sambuc if (s->bbio != NULL) {
623*0a6a1f1dSLionel Sambuc if (s->wbio == s->bbio) {
624ebfedea0SLionel Sambuc s->wbio = s->wbio->next_bio;
625ebfedea0SLionel Sambuc s->bbio->next_bio = NULL;
626ebfedea0SLionel Sambuc }
627ebfedea0SLionel Sambuc }
628ebfedea0SLionel Sambuc if ((s->rbio != NULL) && (s->rbio != rbio))
629ebfedea0SLionel Sambuc BIO_free_all(s->rbio);
630ebfedea0SLionel Sambuc if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
631ebfedea0SLionel Sambuc BIO_free_all(s->wbio);
632ebfedea0SLionel Sambuc s->rbio = rbio;
633ebfedea0SLionel Sambuc s->wbio = wbio;
634ebfedea0SLionel Sambuc }
635ebfedea0SLionel Sambuc
SSL_get_rbio(const SSL * s)636ebfedea0SLionel Sambuc BIO *SSL_get_rbio(const SSL *s)
637*0a6a1f1dSLionel Sambuc {
638*0a6a1f1dSLionel Sambuc return (s->rbio);
639*0a6a1f1dSLionel Sambuc }
640ebfedea0SLionel Sambuc
SSL_get_wbio(const SSL * s)641ebfedea0SLionel Sambuc BIO *SSL_get_wbio(const SSL *s)
642*0a6a1f1dSLionel Sambuc {
643*0a6a1f1dSLionel Sambuc return (s->wbio);
644*0a6a1f1dSLionel Sambuc }
645ebfedea0SLionel Sambuc
SSL_get_fd(const SSL * s)646ebfedea0SLionel Sambuc int SSL_get_fd(const SSL *s)
647ebfedea0SLionel Sambuc {
648ebfedea0SLionel Sambuc return (SSL_get_rfd(s));
649ebfedea0SLionel Sambuc }
650ebfedea0SLionel Sambuc
SSL_get_rfd(const SSL * s)651ebfedea0SLionel Sambuc int SSL_get_rfd(const SSL *s)
652ebfedea0SLionel Sambuc {
653ebfedea0SLionel Sambuc int ret = -1;
654ebfedea0SLionel Sambuc BIO *b, *r;
655ebfedea0SLionel Sambuc
656ebfedea0SLionel Sambuc b = SSL_get_rbio(s);
657ebfedea0SLionel Sambuc r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
658ebfedea0SLionel Sambuc if (r != NULL)
659ebfedea0SLionel Sambuc BIO_get_fd(r, &ret);
660ebfedea0SLionel Sambuc return (ret);
661ebfedea0SLionel Sambuc }
662ebfedea0SLionel Sambuc
SSL_get_wfd(const SSL * s)663ebfedea0SLionel Sambuc int SSL_get_wfd(const SSL *s)
664ebfedea0SLionel Sambuc {
665ebfedea0SLionel Sambuc int ret = -1;
666ebfedea0SLionel Sambuc BIO *b, *r;
667ebfedea0SLionel Sambuc
668ebfedea0SLionel Sambuc b = SSL_get_wbio(s);
669ebfedea0SLionel Sambuc r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
670ebfedea0SLionel Sambuc if (r != NULL)
671ebfedea0SLionel Sambuc BIO_get_fd(r, &ret);
672ebfedea0SLionel Sambuc return (ret);
673ebfedea0SLionel Sambuc }
674ebfedea0SLionel Sambuc
675ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SOCK
SSL_set_fd(SSL * s,int fd)676ebfedea0SLionel Sambuc int SSL_set_fd(SSL *s, int fd)
677ebfedea0SLionel Sambuc {
678ebfedea0SLionel Sambuc int ret = 0;
679ebfedea0SLionel Sambuc BIO *bio = NULL;
680ebfedea0SLionel Sambuc
681ebfedea0SLionel Sambuc bio = BIO_new(BIO_s_socket());
682ebfedea0SLionel Sambuc
683*0a6a1f1dSLionel Sambuc if (bio == NULL) {
684ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_SET_FD, ERR_R_BUF_LIB);
685ebfedea0SLionel Sambuc goto err;
686ebfedea0SLionel Sambuc }
687ebfedea0SLionel Sambuc BIO_set_fd(bio, fd, BIO_NOCLOSE);
688ebfedea0SLionel Sambuc SSL_set_bio(s, bio, bio);
689ebfedea0SLionel Sambuc ret = 1;
690ebfedea0SLionel Sambuc err:
691ebfedea0SLionel Sambuc return (ret);
692ebfedea0SLionel Sambuc }
693ebfedea0SLionel Sambuc
SSL_set_wfd(SSL * s,int fd)694ebfedea0SLionel Sambuc int SSL_set_wfd(SSL *s, int fd)
695ebfedea0SLionel Sambuc {
696ebfedea0SLionel Sambuc int ret = 0;
697ebfedea0SLionel Sambuc BIO *bio = NULL;
698ebfedea0SLionel Sambuc
699ebfedea0SLionel Sambuc if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
700*0a6a1f1dSLionel Sambuc || ((int)BIO_get_fd(s->rbio, NULL) != fd)) {
701ebfedea0SLionel Sambuc bio = BIO_new(BIO_s_socket());
702ebfedea0SLionel Sambuc
703*0a6a1f1dSLionel Sambuc if (bio == NULL) {
704*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_SET_WFD, ERR_R_BUF_LIB);
705*0a6a1f1dSLionel Sambuc goto err;
706*0a6a1f1dSLionel Sambuc }
707ebfedea0SLionel Sambuc BIO_set_fd(bio, fd, BIO_NOCLOSE);
708ebfedea0SLionel Sambuc SSL_set_bio(s, SSL_get_rbio(s), bio);
709*0a6a1f1dSLionel Sambuc } else
710ebfedea0SLionel Sambuc SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
711ebfedea0SLionel Sambuc ret = 1;
712ebfedea0SLionel Sambuc err:
713ebfedea0SLionel Sambuc return (ret);
714ebfedea0SLionel Sambuc }
715ebfedea0SLionel Sambuc
SSL_set_rfd(SSL * s,int fd)716ebfedea0SLionel Sambuc int SSL_set_rfd(SSL *s, int fd)
717ebfedea0SLionel Sambuc {
718ebfedea0SLionel Sambuc int ret = 0;
719ebfedea0SLionel Sambuc BIO *bio = NULL;
720ebfedea0SLionel Sambuc
721ebfedea0SLionel Sambuc if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
722*0a6a1f1dSLionel Sambuc || ((int)BIO_get_fd(s->wbio, NULL) != fd)) {
723ebfedea0SLionel Sambuc bio = BIO_new(BIO_s_socket());
724ebfedea0SLionel Sambuc
725*0a6a1f1dSLionel Sambuc if (bio == NULL) {
726ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_SET_RFD, ERR_R_BUF_LIB);
727ebfedea0SLionel Sambuc goto err;
728ebfedea0SLionel Sambuc }
729ebfedea0SLionel Sambuc BIO_set_fd(bio, fd, BIO_NOCLOSE);
730ebfedea0SLionel Sambuc SSL_set_bio(s, bio, SSL_get_wbio(s));
731*0a6a1f1dSLionel Sambuc } else
732ebfedea0SLionel Sambuc SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
733ebfedea0SLionel Sambuc ret = 1;
734ebfedea0SLionel Sambuc err:
735ebfedea0SLionel Sambuc return (ret);
736ebfedea0SLionel Sambuc }
737ebfedea0SLionel Sambuc #endif
738ebfedea0SLionel Sambuc
739ebfedea0SLionel Sambuc /* return length of latest Finished message we sent, copy to 'buf' */
SSL_get_finished(const SSL * s,void * buf,size_t count)740ebfedea0SLionel Sambuc size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
741ebfedea0SLionel Sambuc {
742ebfedea0SLionel Sambuc size_t ret = 0;
743ebfedea0SLionel Sambuc
744*0a6a1f1dSLionel Sambuc if (s->s3 != NULL) {
745ebfedea0SLionel Sambuc ret = s->s3->tmp.finish_md_len;
746ebfedea0SLionel Sambuc if (count > ret)
747ebfedea0SLionel Sambuc count = ret;
748ebfedea0SLionel Sambuc memcpy(buf, s->s3->tmp.finish_md, count);
749ebfedea0SLionel Sambuc }
750ebfedea0SLionel Sambuc return ret;
751ebfedea0SLionel Sambuc }
752ebfedea0SLionel Sambuc
753ebfedea0SLionel Sambuc /* return length of latest Finished message we expected, copy to 'buf' */
SSL_get_peer_finished(const SSL * s,void * buf,size_t count)754ebfedea0SLionel Sambuc size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
755ebfedea0SLionel Sambuc {
756ebfedea0SLionel Sambuc size_t ret = 0;
757ebfedea0SLionel Sambuc
758*0a6a1f1dSLionel Sambuc if (s->s3 != NULL) {
759ebfedea0SLionel Sambuc ret = s->s3->tmp.peer_finish_md_len;
760ebfedea0SLionel Sambuc if (count > ret)
761ebfedea0SLionel Sambuc count = ret;
762ebfedea0SLionel Sambuc memcpy(buf, s->s3->tmp.peer_finish_md, count);
763ebfedea0SLionel Sambuc }
764ebfedea0SLionel Sambuc return ret;
765ebfedea0SLionel Sambuc }
766ebfedea0SLionel Sambuc
SSL_get_verify_mode(const SSL * s)767ebfedea0SLionel Sambuc int SSL_get_verify_mode(const SSL *s)
768ebfedea0SLionel Sambuc {
769ebfedea0SLionel Sambuc return (s->verify_mode);
770ebfedea0SLionel Sambuc }
771ebfedea0SLionel Sambuc
SSL_get_verify_depth(const SSL * s)772ebfedea0SLionel Sambuc int SSL_get_verify_depth(const SSL *s)
773ebfedea0SLionel Sambuc {
774ebfedea0SLionel Sambuc return X509_VERIFY_PARAM_get_depth(s->param);
775ebfedea0SLionel Sambuc }
776ebfedea0SLionel Sambuc
SSL_get_verify_callback(const SSL * s)777*0a6a1f1dSLionel Sambuc int (*SSL_get_verify_callback(const SSL *s)) (int, X509_STORE_CTX *) {
778ebfedea0SLionel Sambuc return (s->verify_callback);
779ebfedea0SLionel Sambuc }
780ebfedea0SLionel Sambuc
SSL_CTX_get_verify_mode(const SSL_CTX * ctx)781ebfedea0SLionel Sambuc int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
782ebfedea0SLionel Sambuc {
783ebfedea0SLionel Sambuc return (ctx->verify_mode);
784ebfedea0SLionel Sambuc }
785ebfedea0SLionel Sambuc
SSL_CTX_get_verify_depth(const SSL_CTX * ctx)786ebfedea0SLionel Sambuc int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
787ebfedea0SLionel Sambuc {
788ebfedea0SLionel Sambuc return X509_VERIFY_PARAM_get_depth(ctx->param);
789ebfedea0SLionel Sambuc }
790ebfedea0SLionel Sambuc
SSL_CTX_get_verify_callback(const SSL_CTX * ctx)791*0a6a1f1dSLionel Sambuc int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx)) (int, X509_STORE_CTX *) {
792ebfedea0SLionel Sambuc return (ctx->default_verify_callback);
793ebfedea0SLionel Sambuc }
794ebfedea0SLionel Sambuc
SSL_set_verify(SSL * s,int mode,int (* callback)(int ok,X509_STORE_CTX * ctx))795ebfedea0SLionel Sambuc void SSL_set_verify(SSL *s, int mode,
796ebfedea0SLionel Sambuc int (*callback) (int ok, X509_STORE_CTX *ctx))
797ebfedea0SLionel Sambuc {
798ebfedea0SLionel Sambuc s->verify_mode = mode;
799ebfedea0SLionel Sambuc if (callback != NULL)
800ebfedea0SLionel Sambuc s->verify_callback = callback;
801ebfedea0SLionel Sambuc }
802ebfedea0SLionel Sambuc
SSL_set_verify_depth(SSL * s,int depth)803ebfedea0SLionel Sambuc void SSL_set_verify_depth(SSL *s, int depth)
804ebfedea0SLionel Sambuc {
805ebfedea0SLionel Sambuc X509_VERIFY_PARAM_set_depth(s->param, depth);
806ebfedea0SLionel Sambuc }
807ebfedea0SLionel Sambuc
SSL_set_read_ahead(SSL * s,int yes)808ebfedea0SLionel Sambuc void SSL_set_read_ahead(SSL *s, int yes)
809ebfedea0SLionel Sambuc {
810ebfedea0SLionel Sambuc s->read_ahead = yes;
811ebfedea0SLionel Sambuc }
812ebfedea0SLionel Sambuc
SSL_get_read_ahead(const SSL * s)813ebfedea0SLionel Sambuc int SSL_get_read_ahead(const SSL *s)
814ebfedea0SLionel Sambuc {
815ebfedea0SLionel Sambuc return (s->read_ahead);
816ebfedea0SLionel Sambuc }
817ebfedea0SLionel Sambuc
SSL_pending(const SSL * s)818ebfedea0SLionel Sambuc int SSL_pending(const SSL *s)
819ebfedea0SLionel Sambuc {
820*0a6a1f1dSLionel Sambuc /*
821*0a6a1f1dSLionel Sambuc * SSL_pending cannot work properly if read-ahead is enabled
822*0a6a1f1dSLionel Sambuc * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
823*0a6a1f1dSLionel Sambuc * impossible to fix since SSL_pending cannot report errors that may be
824*0a6a1f1dSLionel Sambuc * observed while scanning the new data. (Note that SSL_pending() is
825*0a6a1f1dSLionel Sambuc * often used as a boolean value, so we'd better not return -1.)
826ebfedea0SLionel Sambuc */
827ebfedea0SLionel Sambuc return (s->method->ssl_pending(s));
828ebfedea0SLionel Sambuc }
829ebfedea0SLionel Sambuc
SSL_get_peer_certificate(const SSL * s)830ebfedea0SLionel Sambuc X509 *SSL_get_peer_certificate(const SSL *s)
831ebfedea0SLionel Sambuc {
832ebfedea0SLionel Sambuc X509 *r;
833ebfedea0SLionel Sambuc
834ebfedea0SLionel Sambuc if ((s == NULL) || (s->session == NULL))
835ebfedea0SLionel Sambuc r = NULL;
836ebfedea0SLionel Sambuc else
837ebfedea0SLionel Sambuc r = s->session->peer;
838ebfedea0SLionel Sambuc
839*0a6a1f1dSLionel Sambuc if (r == NULL)
840*0a6a1f1dSLionel Sambuc return (r);
841ebfedea0SLionel Sambuc
842ebfedea0SLionel Sambuc CRYPTO_add(&r->references, 1, CRYPTO_LOCK_X509);
843ebfedea0SLionel Sambuc
844ebfedea0SLionel Sambuc return (r);
845ebfedea0SLionel Sambuc }
846ebfedea0SLionel Sambuc
STACK_OF(X509)847ebfedea0SLionel Sambuc STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
848ebfedea0SLionel Sambuc {
849ebfedea0SLionel Sambuc STACK_OF(X509) *r;
850ebfedea0SLionel Sambuc
851*0a6a1f1dSLionel Sambuc if ((s == NULL) || (s->session == NULL)
852*0a6a1f1dSLionel Sambuc || (s->session->sess_cert == NULL))
853ebfedea0SLionel Sambuc r = NULL;
854ebfedea0SLionel Sambuc else
855ebfedea0SLionel Sambuc r = s->session->sess_cert->cert_chain;
856ebfedea0SLionel Sambuc
857*0a6a1f1dSLionel Sambuc /*
858*0a6a1f1dSLionel Sambuc * If we are a client, cert_chain includes the peer's own certificate; if
859*0a6a1f1dSLionel Sambuc * we are a server, it does not.
860*0a6a1f1dSLionel Sambuc */
861ebfedea0SLionel Sambuc
862ebfedea0SLionel Sambuc return (r);
863ebfedea0SLionel Sambuc }
864ebfedea0SLionel Sambuc
865*0a6a1f1dSLionel Sambuc /*
866*0a6a1f1dSLionel Sambuc * Now in theory, since the calling process own 't' it should be safe to
867*0a6a1f1dSLionel Sambuc * modify. We need to be able to read f without being hassled
868*0a6a1f1dSLionel Sambuc */
SSL_copy_session_id(SSL * t,const SSL * f)869ebfedea0SLionel Sambuc void SSL_copy_session_id(SSL *t, const SSL *f)
870ebfedea0SLionel Sambuc {
871ebfedea0SLionel Sambuc CERT *tmp;
872ebfedea0SLionel Sambuc
873ebfedea0SLionel Sambuc /* Do we need to to SSL locking? */
874ebfedea0SLionel Sambuc SSL_set_session(t, SSL_get_session(f));
875ebfedea0SLionel Sambuc
876*0a6a1f1dSLionel Sambuc /*
877*0a6a1f1dSLionel Sambuc * what if we are setup as SSLv2 but want to talk SSLv3 or vice-versa
878*0a6a1f1dSLionel Sambuc */
879*0a6a1f1dSLionel Sambuc if (t->method != f->method) {
880ebfedea0SLionel Sambuc t->method->ssl_free(t); /* cleanup current */
881ebfedea0SLionel Sambuc t->method = f->method; /* change method */
882ebfedea0SLionel Sambuc t->method->ssl_new(t); /* setup new */
883ebfedea0SLionel Sambuc }
884ebfedea0SLionel Sambuc
885ebfedea0SLionel Sambuc tmp = t->cert;
886*0a6a1f1dSLionel Sambuc if (f->cert != NULL) {
887ebfedea0SLionel Sambuc CRYPTO_add(&f->cert->references, 1, CRYPTO_LOCK_SSL_CERT);
888ebfedea0SLionel Sambuc t->cert = f->cert;
889*0a6a1f1dSLionel Sambuc } else
890ebfedea0SLionel Sambuc t->cert = NULL;
891*0a6a1f1dSLionel Sambuc if (tmp != NULL)
892*0a6a1f1dSLionel Sambuc ssl_cert_free(tmp);
893ebfedea0SLionel Sambuc SSL_set_session_id_context(t, f->sid_ctx, f->sid_ctx_length);
894ebfedea0SLionel Sambuc }
895ebfedea0SLionel Sambuc
896ebfedea0SLionel Sambuc /* Fix this so it checks all the valid key/cert options */
SSL_CTX_check_private_key(const SSL_CTX * ctx)897ebfedea0SLionel Sambuc int SSL_CTX_check_private_key(const SSL_CTX *ctx)
898ebfedea0SLionel Sambuc {
899ebfedea0SLionel Sambuc if ((ctx == NULL) ||
900*0a6a1f1dSLionel Sambuc (ctx->cert == NULL) || (ctx->cert->key->x509 == NULL)) {
901*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,
902*0a6a1f1dSLionel Sambuc SSL_R_NO_CERTIFICATE_ASSIGNED);
903ebfedea0SLionel Sambuc return (0);
904ebfedea0SLionel Sambuc }
905*0a6a1f1dSLionel Sambuc if (ctx->cert->key->privatekey == NULL) {
906*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY,
907*0a6a1f1dSLionel Sambuc SSL_R_NO_PRIVATE_KEY_ASSIGNED);
908ebfedea0SLionel Sambuc return (0);
909ebfedea0SLionel Sambuc }
910*0a6a1f1dSLionel Sambuc return (X509_check_private_key
911*0a6a1f1dSLionel Sambuc (ctx->cert->key->x509, ctx->cert->key->privatekey));
912ebfedea0SLionel Sambuc }
913ebfedea0SLionel Sambuc
914ebfedea0SLionel Sambuc /* Fix this function so that it takes an optional type parameter */
SSL_check_private_key(const SSL * ssl)915ebfedea0SLionel Sambuc int SSL_check_private_key(const SSL *ssl)
916ebfedea0SLionel Sambuc {
917*0a6a1f1dSLionel Sambuc if (ssl == NULL) {
918ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
919ebfedea0SLionel Sambuc return (0);
920ebfedea0SLionel Sambuc }
921*0a6a1f1dSLionel Sambuc if (ssl->cert == NULL) {
922ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_CERTIFICATE_ASSIGNED);
923ebfedea0SLionel Sambuc return 0;
924ebfedea0SLionel Sambuc }
925*0a6a1f1dSLionel Sambuc if (ssl->cert->key->x509 == NULL) {
926ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_CERTIFICATE_ASSIGNED);
927ebfedea0SLionel Sambuc return (0);
928ebfedea0SLionel Sambuc }
929*0a6a1f1dSLionel Sambuc if (ssl->cert->key->privatekey == NULL) {
930ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
931ebfedea0SLionel Sambuc return (0);
932ebfedea0SLionel Sambuc }
933ebfedea0SLionel Sambuc return (X509_check_private_key(ssl->cert->key->x509,
934ebfedea0SLionel Sambuc ssl->cert->key->privatekey));
935ebfedea0SLionel Sambuc }
936ebfedea0SLionel Sambuc
SSL_accept(SSL * s)937ebfedea0SLionel Sambuc int SSL_accept(SSL *s)
938ebfedea0SLionel Sambuc {
939ebfedea0SLionel Sambuc if (s->handshake_func == 0)
940ebfedea0SLionel Sambuc /* Not properly initialized yet */
941ebfedea0SLionel Sambuc SSL_set_accept_state(s);
942ebfedea0SLionel Sambuc
943ebfedea0SLionel Sambuc return (s->method->ssl_accept(s));
944ebfedea0SLionel Sambuc }
945ebfedea0SLionel Sambuc
SSL_connect(SSL * s)946ebfedea0SLionel Sambuc int SSL_connect(SSL *s)
947ebfedea0SLionel Sambuc {
948ebfedea0SLionel Sambuc if (s->handshake_func == 0)
949ebfedea0SLionel Sambuc /* Not properly initialized yet */
950ebfedea0SLionel Sambuc SSL_set_connect_state(s);
951ebfedea0SLionel Sambuc
952ebfedea0SLionel Sambuc return (s->method->ssl_connect(s));
953ebfedea0SLionel Sambuc }
954ebfedea0SLionel Sambuc
SSL_get_default_timeout(const SSL * s)955ebfedea0SLionel Sambuc long SSL_get_default_timeout(const SSL *s)
956ebfedea0SLionel Sambuc {
957ebfedea0SLionel Sambuc return (s->method->get_timeout());
958ebfedea0SLionel Sambuc }
959ebfedea0SLionel Sambuc
SSL_read(SSL * s,void * buf,int num)960ebfedea0SLionel Sambuc int SSL_read(SSL *s, void *buf, int num)
961ebfedea0SLionel Sambuc {
962*0a6a1f1dSLionel Sambuc if (s->handshake_func == 0) {
963ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_READ, SSL_R_UNINITIALIZED);
964ebfedea0SLionel Sambuc return -1;
965ebfedea0SLionel Sambuc }
966ebfedea0SLionel Sambuc
967*0a6a1f1dSLionel Sambuc if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
968ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
969ebfedea0SLionel Sambuc return (0);
970ebfedea0SLionel Sambuc }
971ebfedea0SLionel Sambuc return (s->method->ssl_read(s, buf, num));
972ebfedea0SLionel Sambuc }
973ebfedea0SLionel Sambuc
SSL_peek(SSL * s,void * buf,int num)974ebfedea0SLionel Sambuc int SSL_peek(SSL *s, void *buf, int num)
975ebfedea0SLionel Sambuc {
976*0a6a1f1dSLionel Sambuc if (s->handshake_func == 0) {
977ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_PEEK, SSL_R_UNINITIALIZED);
978ebfedea0SLionel Sambuc return -1;
979ebfedea0SLionel Sambuc }
980ebfedea0SLionel Sambuc
981*0a6a1f1dSLionel Sambuc if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
982ebfedea0SLionel Sambuc return (0);
983ebfedea0SLionel Sambuc }
984ebfedea0SLionel Sambuc return (s->method->ssl_peek(s, buf, num));
985ebfedea0SLionel Sambuc }
986ebfedea0SLionel Sambuc
SSL_write(SSL * s,const void * buf,int num)987ebfedea0SLionel Sambuc int SSL_write(SSL *s, const void *buf, int num)
988ebfedea0SLionel Sambuc {
989*0a6a1f1dSLionel Sambuc if (s->handshake_func == 0) {
990ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_WRITE, SSL_R_UNINITIALIZED);
991ebfedea0SLionel Sambuc return -1;
992ebfedea0SLionel Sambuc }
993ebfedea0SLionel Sambuc
994*0a6a1f1dSLionel Sambuc if (s->shutdown & SSL_SENT_SHUTDOWN) {
995ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
996ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_WRITE, SSL_R_PROTOCOL_IS_SHUTDOWN);
997ebfedea0SLionel Sambuc return (-1);
998ebfedea0SLionel Sambuc }
999ebfedea0SLionel Sambuc return (s->method->ssl_write(s, buf, num));
1000ebfedea0SLionel Sambuc }
1001ebfedea0SLionel Sambuc
SSL_shutdown(SSL * s)1002ebfedea0SLionel Sambuc int SSL_shutdown(SSL *s)
1003ebfedea0SLionel Sambuc {
1004*0a6a1f1dSLionel Sambuc /*
1005*0a6a1f1dSLionel Sambuc * Note that this function behaves differently from what one might
1006*0a6a1f1dSLionel Sambuc * expect. Return values are 0 for no success (yet), 1 for success; but
1007*0a6a1f1dSLionel Sambuc * calling it once is usually not enough, even if blocking I/O is used
1008*0a6a1f1dSLionel Sambuc * (see ssl3_shutdown).
1009ebfedea0SLionel Sambuc */
1010ebfedea0SLionel Sambuc
1011*0a6a1f1dSLionel Sambuc if (s->handshake_func == 0) {
1012ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED);
1013ebfedea0SLionel Sambuc return -1;
1014ebfedea0SLionel Sambuc }
1015ebfedea0SLionel Sambuc
1016ebfedea0SLionel Sambuc if ((s != NULL) && !SSL_in_init(s))
1017ebfedea0SLionel Sambuc return (s->method->ssl_shutdown(s));
1018ebfedea0SLionel Sambuc else
1019ebfedea0SLionel Sambuc return (1);
1020ebfedea0SLionel Sambuc }
1021ebfedea0SLionel Sambuc
SSL_renegotiate(SSL * s)1022ebfedea0SLionel Sambuc int SSL_renegotiate(SSL *s)
1023ebfedea0SLionel Sambuc {
1024ebfedea0SLionel Sambuc if (s->renegotiate == 0)
1025ebfedea0SLionel Sambuc s->renegotiate = 1;
1026ebfedea0SLionel Sambuc
1027ebfedea0SLionel Sambuc s->new_session = 1;
1028ebfedea0SLionel Sambuc
1029ebfedea0SLionel Sambuc return (s->method->ssl_renegotiate(s));
1030ebfedea0SLionel Sambuc }
1031ebfedea0SLionel Sambuc
SSL_renegotiate_abbreviated(SSL * s)1032ebfedea0SLionel Sambuc int SSL_renegotiate_abbreviated(SSL *s)
1033ebfedea0SLionel Sambuc {
1034ebfedea0SLionel Sambuc if (s->renegotiate == 0)
1035ebfedea0SLionel Sambuc s->renegotiate = 1;
1036ebfedea0SLionel Sambuc
1037ebfedea0SLionel Sambuc s->new_session = 0;
1038ebfedea0SLionel Sambuc
1039ebfedea0SLionel Sambuc return (s->method->ssl_renegotiate(s));
1040ebfedea0SLionel Sambuc }
1041ebfedea0SLionel Sambuc
SSL_renegotiate_pending(SSL * s)1042ebfedea0SLionel Sambuc int SSL_renegotiate_pending(SSL *s)
1043ebfedea0SLionel Sambuc {
1044*0a6a1f1dSLionel Sambuc /*
1045*0a6a1f1dSLionel Sambuc * becomes true when negotiation is requested; false again once a
1046*0a6a1f1dSLionel Sambuc * handshake has finished
1047*0a6a1f1dSLionel Sambuc */
1048ebfedea0SLionel Sambuc return (s->renegotiate != 0);
1049ebfedea0SLionel Sambuc }
1050ebfedea0SLionel Sambuc
SSL_ctrl(SSL * s,int cmd,long larg,void * parg)1051ebfedea0SLionel Sambuc long SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
1052ebfedea0SLionel Sambuc {
1053ebfedea0SLionel Sambuc long l;
1054ebfedea0SLionel Sambuc
1055*0a6a1f1dSLionel Sambuc switch (cmd) {
1056ebfedea0SLionel Sambuc case SSL_CTRL_GET_READ_AHEAD:
1057ebfedea0SLionel Sambuc return (s->read_ahead);
1058ebfedea0SLionel Sambuc case SSL_CTRL_SET_READ_AHEAD:
1059ebfedea0SLionel Sambuc l = s->read_ahead;
1060ebfedea0SLionel Sambuc s->read_ahead = larg;
1061ebfedea0SLionel Sambuc return (l);
1062ebfedea0SLionel Sambuc
1063ebfedea0SLionel Sambuc case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1064ebfedea0SLionel Sambuc s->msg_callback_arg = parg;
1065ebfedea0SLionel Sambuc return 1;
1066ebfedea0SLionel Sambuc
1067ebfedea0SLionel Sambuc case SSL_CTRL_OPTIONS:
1068ebfedea0SLionel Sambuc return (s->options |= larg);
1069ebfedea0SLionel Sambuc case SSL_CTRL_CLEAR_OPTIONS:
1070ebfedea0SLionel Sambuc return (s->options &= ~larg);
1071ebfedea0SLionel Sambuc case SSL_CTRL_MODE:
1072ebfedea0SLionel Sambuc return (s->mode |= larg);
1073ebfedea0SLionel Sambuc case SSL_CTRL_CLEAR_MODE:
1074ebfedea0SLionel Sambuc return (s->mode &= ~larg);
1075ebfedea0SLionel Sambuc case SSL_CTRL_GET_MAX_CERT_LIST:
1076ebfedea0SLionel Sambuc return (s->max_cert_list);
1077ebfedea0SLionel Sambuc case SSL_CTRL_SET_MAX_CERT_LIST:
1078ebfedea0SLionel Sambuc l = s->max_cert_list;
1079ebfedea0SLionel Sambuc s->max_cert_list = larg;
1080ebfedea0SLionel Sambuc return (l);
1081ebfedea0SLionel Sambuc case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1082ebfedea0SLionel Sambuc if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1083ebfedea0SLionel Sambuc return 0;
1084ebfedea0SLionel Sambuc s->max_send_fragment = larg;
1085ebfedea0SLionel Sambuc return 1;
1086ebfedea0SLionel Sambuc case SSL_CTRL_GET_RI_SUPPORT:
1087ebfedea0SLionel Sambuc if (s->s3)
1088ebfedea0SLionel Sambuc return s->s3->send_connection_binding;
1089*0a6a1f1dSLionel Sambuc else
1090*0a6a1f1dSLionel Sambuc return 0;
1091ebfedea0SLionel Sambuc default:
1092ebfedea0SLionel Sambuc return (s->method->ssl_ctrl(s, cmd, larg, parg));
1093ebfedea0SLionel Sambuc }
1094ebfedea0SLionel Sambuc }
1095ebfedea0SLionel Sambuc
SSL_callback_ctrl(SSL * s,int cmd,void (* fp)(void))1096ebfedea0SLionel Sambuc long SSL_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
1097ebfedea0SLionel Sambuc {
1098*0a6a1f1dSLionel Sambuc switch (cmd) {
1099ebfedea0SLionel Sambuc case SSL_CTRL_SET_MSG_CALLBACK:
1100*0a6a1f1dSLionel Sambuc s->msg_callback = (void (*)
1101*0a6a1f1dSLionel Sambuc (int write_p, int version, int content_type,
1102*0a6a1f1dSLionel Sambuc const void *buf, size_t len, SSL *ssl,
1103*0a6a1f1dSLionel Sambuc void *arg))(fp);
1104ebfedea0SLionel Sambuc return 1;
1105ebfedea0SLionel Sambuc
1106ebfedea0SLionel Sambuc default:
1107ebfedea0SLionel Sambuc return (s->method->ssl_callback_ctrl(s, cmd, fp));
1108ebfedea0SLionel Sambuc }
1109ebfedea0SLionel Sambuc }
1110ebfedea0SLionel Sambuc
LHASH_OF(SSL_SESSION)1111ebfedea0SLionel Sambuc LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1112ebfedea0SLionel Sambuc {
1113ebfedea0SLionel Sambuc return ctx->sessions;
1114ebfedea0SLionel Sambuc }
1115ebfedea0SLionel Sambuc
SSL_CTX_ctrl(SSL_CTX * ctx,int cmd,long larg,void * parg)1116ebfedea0SLionel Sambuc long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
1117ebfedea0SLionel Sambuc {
1118ebfedea0SLionel Sambuc long l;
1119ebfedea0SLionel Sambuc
1120*0a6a1f1dSLionel Sambuc switch (cmd) {
1121ebfedea0SLionel Sambuc case SSL_CTRL_GET_READ_AHEAD:
1122ebfedea0SLionel Sambuc return (ctx->read_ahead);
1123ebfedea0SLionel Sambuc case SSL_CTRL_SET_READ_AHEAD:
1124ebfedea0SLionel Sambuc l = ctx->read_ahead;
1125ebfedea0SLionel Sambuc ctx->read_ahead = larg;
1126ebfedea0SLionel Sambuc return (l);
1127ebfedea0SLionel Sambuc
1128ebfedea0SLionel Sambuc case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1129ebfedea0SLionel Sambuc ctx->msg_callback_arg = parg;
1130ebfedea0SLionel Sambuc return 1;
1131ebfedea0SLionel Sambuc
1132ebfedea0SLionel Sambuc case SSL_CTRL_GET_MAX_CERT_LIST:
1133ebfedea0SLionel Sambuc return (ctx->max_cert_list);
1134ebfedea0SLionel Sambuc case SSL_CTRL_SET_MAX_CERT_LIST:
1135ebfedea0SLionel Sambuc l = ctx->max_cert_list;
1136ebfedea0SLionel Sambuc ctx->max_cert_list = larg;
1137ebfedea0SLionel Sambuc return (l);
1138ebfedea0SLionel Sambuc
1139ebfedea0SLionel Sambuc case SSL_CTRL_SET_SESS_CACHE_SIZE:
1140ebfedea0SLionel Sambuc l = ctx->session_cache_size;
1141ebfedea0SLionel Sambuc ctx->session_cache_size = larg;
1142ebfedea0SLionel Sambuc return (l);
1143ebfedea0SLionel Sambuc case SSL_CTRL_GET_SESS_CACHE_SIZE:
1144ebfedea0SLionel Sambuc return (ctx->session_cache_size);
1145ebfedea0SLionel Sambuc case SSL_CTRL_SET_SESS_CACHE_MODE:
1146ebfedea0SLionel Sambuc l = ctx->session_cache_mode;
1147ebfedea0SLionel Sambuc ctx->session_cache_mode = larg;
1148ebfedea0SLionel Sambuc return (l);
1149ebfedea0SLionel Sambuc case SSL_CTRL_GET_SESS_CACHE_MODE:
1150ebfedea0SLionel Sambuc return (ctx->session_cache_mode);
1151ebfedea0SLionel Sambuc
1152ebfedea0SLionel Sambuc case SSL_CTRL_SESS_NUMBER:
1153ebfedea0SLionel Sambuc return (lh_SSL_SESSION_num_items(ctx->sessions));
1154ebfedea0SLionel Sambuc case SSL_CTRL_SESS_CONNECT:
1155ebfedea0SLionel Sambuc return (ctx->stats.sess_connect);
1156ebfedea0SLionel Sambuc case SSL_CTRL_SESS_CONNECT_GOOD:
1157ebfedea0SLionel Sambuc return (ctx->stats.sess_connect_good);
1158ebfedea0SLionel Sambuc case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1159ebfedea0SLionel Sambuc return (ctx->stats.sess_connect_renegotiate);
1160ebfedea0SLionel Sambuc case SSL_CTRL_SESS_ACCEPT:
1161ebfedea0SLionel Sambuc return (ctx->stats.sess_accept);
1162ebfedea0SLionel Sambuc case SSL_CTRL_SESS_ACCEPT_GOOD:
1163ebfedea0SLionel Sambuc return (ctx->stats.sess_accept_good);
1164ebfedea0SLionel Sambuc case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1165ebfedea0SLionel Sambuc return (ctx->stats.sess_accept_renegotiate);
1166ebfedea0SLionel Sambuc case SSL_CTRL_SESS_HIT:
1167ebfedea0SLionel Sambuc return (ctx->stats.sess_hit);
1168ebfedea0SLionel Sambuc case SSL_CTRL_SESS_CB_HIT:
1169ebfedea0SLionel Sambuc return (ctx->stats.sess_cb_hit);
1170ebfedea0SLionel Sambuc case SSL_CTRL_SESS_MISSES:
1171ebfedea0SLionel Sambuc return (ctx->stats.sess_miss);
1172ebfedea0SLionel Sambuc case SSL_CTRL_SESS_TIMEOUTS:
1173ebfedea0SLionel Sambuc return (ctx->stats.sess_timeout);
1174ebfedea0SLionel Sambuc case SSL_CTRL_SESS_CACHE_FULL:
1175ebfedea0SLionel Sambuc return (ctx->stats.sess_cache_full);
1176ebfedea0SLionel Sambuc case SSL_CTRL_OPTIONS:
1177ebfedea0SLionel Sambuc return (ctx->options |= larg);
1178ebfedea0SLionel Sambuc case SSL_CTRL_CLEAR_OPTIONS:
1179ebfedea0SLionel Sambuc return (ctx->options &= ~larg);
1180ebfedea0SLionel Sambuc case SSL_CTRL_MODE:
1181ebfedea0SLionel Sambuc return (ctx->mode |= larg);
1182ebfedea0SLionel Sambuc case SSL_CTRL_CLEAR_MODE:
1183ebfedea0SLionel Sambuc return (ctx->mode &= ~larg);
1184ebfedea0SLionel Sambuc case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1185ebfedea0SLionel Sambuc if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1186ebfedea0SLionel Sambuc return 0;
1187ebfedea0SLionel Sambuc ctx->max_send_fragment = larg;
1188ebfedea0SLionel Sambuc return 1;
1189ebfedea0SLionel Sambuc default:
1190ebfedea0SLionel Sambuc return (ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg));
1191ebfedea0SLionel Sambuc }
1192ebfedea0SLionel Sambuc }
1193ebfedea0SLionel Sambuc
SSL_CTX_callback_ctrl(SSL_CTX * ctx,int cmd,void (* fp)(void))1194ebfedea0SLionel Sambuc long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
1195ebfedea0SLionel Sambuc {
1196*0a6a1f1dSLionel Sambuc switch (cmd) {
1197ebfedea0SLionel Sambuc case SSL_CTRL_SET_MSG_CALLBACK:
1198*0a6a1f1dSLionel Sambuc ctx->msg_callback = (void (*)
1199*0a6a1f1dSLionel Sambuc (int write_p, int version, int content_type,
1200*0a6a1f1dSLionel Sambuc const void *buf, size_t len, SSL *ssl,
1201*0a6a1f1dSLionel Sambuc void *arg))(fp);
1202ebfedea0SLionel Sambuc return 1;
1203ebfedea0SLionel Sambuc
1204ebfedea0SLionel Sambuc default:
1205ebfedea0SLionel Sambuc return (ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp));
1206ebfedea0SLionel Sambuc }
1207ebfedea0SLionel Sambuc }
1208ebfedea0SLionel Sambuc
ssl_cipher_id_cmp(const SSL_CIPHER * a,const SSL_CIPHER * b)1209ebfedea0SLionel Sambuc int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
1210ebfedea0SLionel Sambuc {
1211ebfedea0SLionel Sambuc long l;
1212ebfedea0SLionel Sambuc
1213ebfedea0SLionel Sambuc l = a->id - b->id;
1214ebfedea0SLionel Sambuc if (l == 0L)
1215ebfedea0SLionel Sambuc return (0);
1216ebfedea0SLionel Sambuc else
1217ebfedea0SLionel Sambuc return ((l > 0) ? 1 : -1);
1218ebfedea0SLionel Sambuc }
1219ebfedea0SLionel Sambuc
ssl_cipher_ptr_id_cmp(const SSL_CIPHER * const * ap,const SSL_CIPHER * const * bp)1220ebfedea0SLionel Sambuc int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap,
1221ebfedea0SLionel Sambuc const SSL_CIPHER *const *bp)
1222ebfedea0SLionel Sambuc {
1223ebfedea0SLionel Sambuc long l;
1224ebfedea0SLionel Sambuc
1225ebfedea0SLionel Sambuc l = (*ap)->id - (*bp)->id;
1226ebfedea0SLionel Sambuc if (l == 0L)
1227ebfedea0SLionel Sambuc return (0);
1228ebfedea0SLionel Sambuc else
1229ebfedea0SLionel Sambuc return ((l > 0) ? 1 : -1);
1230ebfedea0SLionel Sambuc }
1231ebfedea0SLionel Sambuc
1232ebfedea0SLionel Sambuc /** return a STACK of the ciphers available for the SSL and in order of
1233ebfedea0SLionel Sambuc * preference */
STACK_OF(SSL_CIPHER)1234ebfedea0SLionel Sambuc STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1235ebfedea0SLionel Sambuc {
1236*0a6a1f1dSLionel Sambuc if (s != NULL) {
1237*0a6a1f1dSLionel Sambuc if (s->cipher_list != NULL) {
1238ebfedea0SLionel Sambuc return (s->cipher_list);
1239*0a6a1f1dSLionel Sambuc } else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) {
1240ebfedea0SLionel Sambuc return (s->ctx->cipher_list);
1241ebfedea0SLionel Sambuc }
1242ebfedea0SLionel Sambuc }
1243ebfedea0SLionel Sambuc return (NULL);
1244ebfedea0SLionel Sambuc }
1245ebfedea0SLionel Sambuc
1246ebfedea0SLionel Sambuc /** return a STACK of the ciphers available for the SSL and in order of
1247ebfedea0SLionel Sambuc * algorithm id */
STACK_OF(SSL_CIPHER)1248ebfedea0SLionel Sambuc STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1249ebfedea0SLionel Sambuc {
1250*0a6a1f1dSLionel Sambuc if (s != NULL) {
1251*0a6a1f1dSLionel Sambuc if (s->cipher_list_by_id != NULL) {
1252ebfedea0SLionel Sambuc return (s->cipher_list_by_id);
1253*0a6a1f1dSLionel Sambuc } else if ((s->ctx != NULL) && (s->ctx->cipher_list_by_id != NULL)) {
1254ebfedea0SLionel Sambuc return (s->ctx->cipher_list_by_id);
1255ebfedea0SLionel Sambuc }
1256ebfedea0SLionel Sambuc }
1257ebfedea0SLionel Sambuc return (NULL);
1258ebfedea0SLionel Sambuc }
1259ebfedea0SLionel Sambuc
1260ebfedea0SLionel Sambuc /** The old interface to get the same thing as SSL_get_ciphers() */
SSL_get_cipher_list(const SSL * s,int n)1261ebfedea0SLionel Sambuc const char *SSL_get_cipher_list(const SSL *s, int n)
1262ebfedea0SLionel Sambuc {
1263ebfedea0SLionel Sambuc SSL_CIPHER *c;
1264ebfedea0SLionel Sambuc STACK_OF(SSL_CIPHER) *sk;
1265ebfedea0SLionel Sambuc
1266*0a6a1f1dSLionel Sambuc if (s == NULL)
1267*0a6a1f1dSLionel Sambuc return (NULL);
1268ebfedea0SLionel Sambuc sk = SSL_get_ciphers(s);
1269ebfedea0SLionel Sambuc if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
1270ebfedea0SLionel Sambuc return (NULL);
1271ebfedea0SLionel Sambuc c = sk_SSL_CIPHER_value(sk, n);
1272*0a6a1f1dSLionel Sambuc if (c == NULL)
1273*0a6a1f1dSLionel Sambuc return (NULL);
1274ebfedea0SLionel Sambuc return (c->name);
1275ebfedea0SLionel Sambuc }
1276ebfedea0SLionel Sambuc
1277ebfedea0SLionel Sambuc /** specify the ciphers to be used by default by the SSL_CTX */
SSL_CTX_set_cipher_list(SSL_CTX * ctx,const char * str)1278ebfedea0SLionel Sambuc int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1279ebfedea0SLionel Sambuc {
1280ebfedea0SLionel Sambuc STACK_OF(SSL_CIPHER) *sk;
1281ebfedea0SLionel Sambuc
1282ebfedea0SLionel Sambuc sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
1283ebfedea0SLionel Sambuc &ctx->cipher_list_by_id, str);
1284*0a6a1f1dSLionel Sambuc /*
1285*0a6a1f1dSLionel Sambuc * ssl_create_cipher_list may return an empty stack if it was unable to
1286*0a6a1f1dSLionel Sambuc * find a cipher matching the given rule string (for example if the rule
1287*0a6a1f1dSLionel Sambuc * string specifies a cipher which has been disabled). This is not an
1288*0a6a1f1dSLionel Sambuc * error as far as ssl_create_cipher_list is concerned, and hence
1289*0a6a1f1dSLionel Sambuc * ctx->cipher_list and ctx->cipher_list_by_id has been updated.
1290*0a6a1f1dSLionel Sambuc */
1291ebfedea0SLionel Sambuc if (sk == NULL)
1292ebfedea0SLionel Sambuc return 0;
1293*0a6a1f1dSLionel Sambuc else if (sk_SSL_CIPHER_num(sk) == 0) {
1294ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
1295ebfedea0SLionel Sambuc return 0;
1296ebfedea0SLionel Sambuc }
1297ebfedea0SLionel Sambuc return 1;
1298ebfedea0SLionel Sambuc }
1299ebfedea0SLionel Sambuc
1300ebfedea0SLionel Sambuc /** specify the ciphers to be used by the SSL */
SSL_set_cipher_list(SSL * s,const char * str)1301ebfedea0SLionel Sambuc int SSL_set_cipher_list(SSL *s, const char *str)
1302ebfedea0SLionel Sambuc {
1303ebfedea0SLionel Sambuc STACK_OF(SSL_CIPHER) *sk;
1304ebfedea0SLionel Sambuc
1305ebfedea0SLionel Sambuc sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
1306ebfedea0SLionel Sambuc &s->cipher_list_by_id, str);
1307ebfedea0SLionel Sambuc /* see comment in SSL_CTX_set_cipher_list */
1308ebfedea0SLionel Sambuc if (sk == NULL)
1309ebfedea0SLionel Sambuc return 0;
1310*0a6a1f1dSLionel Sambuc else if (sk_SSL_CIPHER_num(sk) == 0) {
1311ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
1312ebfedea0SLionel Sambuc return 0;
1313ebfedea0SLionel Sambuc }
1314ebfedea0SLionel Sambuc return 1;
1315ebfedea0SLionel Sambuc }
1316ebfedea0SLionel Sambuc
1317ebfedea0SLionel Sambuc /* works well for SSLv2, not so good for SSLv3 */
SSL_get_shared_ciphers(const SSL * s,char * buf,int len)1318ebfedea0SLionel Sambuc char *SSL_get_shared_ciphers(const SSL *s, char *buf, int len)
1319ebfedea0SLionel Sambuc {
1320ebfedea0SLionel Sambuc char *p;
1321ebfedea0SLionel Sambuc STACK_OF(SSL_CIPHER) *sk;
1322ebfedea0SLionel Sambuc SSL_CIPHER *c;
1323ebfedea0SLionel Sambuc int i;
1324ebfedea0SLionel Sambuc
1325*0a6a1f1dSLionel Sambuc if ((s->session == NULL) || (s->session->ciphers == NULL) || (len < 2))
1326ebfedea0SLionel Sambuc return (NULL);
1327ebfedea0SLionel Sambuc
1328ebfedea0SLionel Sambuc p = buf;
1329ebfedea0SLionel Sambuc sk = s->session->ciphers;
1330*0a6a1f1dSLionel Sambuc
1331*0a6a1f1dSLionel Sambuc if (sk_SSL_CIPHER_num(sk) == 0)
1332*0a6a1f1dSLionel Sambuc return NULL;
1333*0a6a1f1dSLionel Sambuc
1334*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1335ebfedea0SLionel Sambuc int n;
1336ebfedea0SLionel Sambuc
1337ebfedea0SLionel Sambuc c = sk_SSL_CIPHER_value(sk, i);
1338ebfedea0SLionel Sambuc n = strlen(c->name);
1339*0a6a1f1dSLionel Sambuc if (n + 1 > len) {
1340ebfedea0SLionel Sambuc if (p != buf)
1341ebfedea0SLionel Sambuc --p;
1342ebfedea0SLionel Sambuc *p = '\0';
1343ebfedea0SLionel Sambuc return buf;
1344ebfedea0SLionel Sambuc }
1345ebfedea0SLionel Sambuc strcpy(p, c->name);
1346ebfedea0SLionel Sambuc p += n;
1347ebfedea0SLionel Sambuc *(p++) = ':';
1348ebfedea0SLionel Sambuc len -= n + 1;
1349ebfedea0SLionel Sambuc }
1350ebfedea0SLionel Sambuc p[-1] = '\0';
1351ebfedea0SLionel Sambuc return (buf);
1352ebfedea0SLionel Sambuc }
1353ebfedea0SLionel Sambuc
ssl_cipher_list_to_bytes(SSL * s,STACK_OF (SSL_CIPHER)* sk,unsigned char * p,int (* put_cb)(const SSL_CIPHER *,unsigned char *))1354*0a6a1f1dSLionel Sambuc int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
1355*0a6a1f1dSLionel Sambuc unsigned char *p,
1356*0a6a1f1dSLionel Sambuc int (*put_cb) (const SSL_CIPHER *,
1357*0a6a1f1dSLionel Sambuc unsigned char *))
1358ebfedea0SLionel Sambuc {
1359ebfedea0SLionel Sambuc int i, j = 0;
1360ebfedea0SLionel Sambuc SSL_CIPHER *c;
1361ebfedea0SLionel Sambuc unsigned char *q;
1362ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_KRB5
1363ebfedea0SLionel Sambuc int nokrb5 = !kssl_tgt_is_available(s->kssl_ctx);
1364ebfedea0SLionel Sambuc #endif /* OPENSSL_NO_KRB5 */
1365ebfedea0SLionel Sambuc
1366*0a6a1f1dSLionel Sambuc if (sk == NULL)
1367*0a6a1f1dSLionel Sambuc return (0);
1368ebfedea0SLionel Sambuc q = p;
1369*0a6a1f1dSLionel Sambuc if (put_cb == NULL)
1370*0a6a1f1dSLionel Sambuc put_cb = s->method->put_cipher_by_char;
1371ebfedea0SLionel Sambuc
1372*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1373ebfedea0SLionel Sambuc c = sk_SSL_CIPHER_value(sk, i);
1374ebfedea0SLionel Sambuc /* Skip TLS v1.2 only ciphersuites if lower than v1.2 */
1375ebfedea0SLionel Sambuc if ((c->algorithm_ssl & SSL_TLSV1_2) &&
1376ebfedea0SLionel Sambuc (TLS1_get_client_version(s) < TLS1_2_VERSION))
1377ebfedea0SLionel Sambuc continue;
1378ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_KRB5
1379*0a6a1f1dSLionel Sambuc if (((c->algorithm_mkey & SSL_kKRB5)
1380*0a6a1f1dSLionel Sambuc || (c->algorithm_auth & SSL_aKRB5)) && nokrb5)
1381ebfedea0SLionel Sambuc continue;
1382ebfedea0SLionel Sambuc #endif /* OPENSSL_NO_KRB5 */
1383ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
1384ebfedea0SLionel Sambuc /* with PSK there must be client callback set */
1385*0a6a1f1dSLionel Sambuc if (((c->algorithm_mkey & SSL_kPSK) || (c->algorithm_auth & SSL_aPSK))
1386*0a6a1f1dSLionel Sambuc && s->psk_client_callback == NULL)
1387ebfedea0SLionel Sambuc continue;
1388ebfedea0SLionel Sambuc #endif /* OPENSSL_NO_PSK */
1389*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_SRP
1390*0a6a1f1dSLionel Sambuc if (((c->algorithm_mkey & SSL_kSRP) || (c->algorithm_auth & SSL_aSRP))
1391*0a6a1f1dSLionel Sambuc && !(s->srp_ctx.srp_Mask & SSL_kSRP))
1392*0a6a1f1dSLionel Sambuc continue;
1393*0a6a1f1dSLionel Sambuc #endif /* OPENSSL_NO_SRP */
1394*0a6a1f1dSLionel Sambuc j = put_cb(c, p);
1395ebfedea0SLionel Sambuc p += j;
1396ebfedea0SLionel Sambuc }
1397*0a6a1f1dSLionel Sambuc /*
1398*0a6a1f1dSLionel Sambuc * If p == q, no ciphers; caller indicates an error. Otherwise, add
1399*0a6a1f1dSLionel Sambuc * applicable SCSVs.
1400ebfedea0SLionel Sambuc */
1401*0a6a1f1dSLionel Sambuc if (p != q) {
1402*0a6a1f1dSLionel Sambuc if (!s->renegotiate) {
1403*0a6a1f1dSLionel Sambuc static SSL_CIPHER scsv = {
1404ebfedea0SLionel Sambuc 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1405ebfedea0SLionel Sambuc };
1406*0a6a1f1dSLionel Sambuc j = put_cb(&scsv, p);
1407ebfedea0SLionel Sambuc p += j;
1408ebfedea0SLionel Sambuc #ifdef OPENSSL_RI_DEBUG
1409*0a6a1f1dSLionel Sambuc fprintf(stderr,
1410*0a6a1f1dSLionel Sambuc "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
1411ebfedea0SLionel Sambuc #endif
1412ebfedea0SLionel Sambuc }
1413ebfedea0SLionel Sambuc
1414*0a6a1f1dSLionel Sambuc if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
1415*0a6a1f1dSLionel Sambuc static SSL_CIPHER scsv = {
1416*0a6a1f1dSLionel Sambuc 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1417*0a6a1f1dSLionel Sambuc };
1418*0a6a1f1dSLionel Sambuc j = put_cb(&scsv, p);
1419*0a6a1f1dSLionel Sambuc p += j;
1420*0a6a1f1dSLionel Sambuc }
1421*0a6a1f1dSLionel Sambuc }
1422*0a6a1f1dSLionel Sambuc
1423ebfedea0SLionel Sambuc return (p - q);
1424ebfedea0SLionel Sambuc }
1425ebfedea0SLionel Sambuc
STACK_OF(SSL_CIPHER)1426*0a6a1f1dSLionel Sambuc STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, unsigned char *p,
1427*0a6a1f1dSLionel Sambuc int num,
1428ebfedea0SLionel Sambuc STACK_OF(SSL_CIPHER) **skp)
1429ebfedea0SLionel Sambuc {
1430ebfedea0SLionel Sambuc const SSL_CIPHER *c;
1431ebfedea0SLionel Sambuc STACK_OF(SSL_CIPHER) *sk;
1432ebfedea0SLionel Sambuc int i, n;
1433*0a6a1f1dSLionel Sambuc
1434ebfedea0SLionel Sambuc if (s->s3)
1435ebfedea0SLionel Sambuc s->s3->send_connection_binding = 0;
1436ebfedea0SLionel Sambuc
1437ebfedea0SLionel Sambuc n = ssl_put_cipher_by_char(s, NULL, NULL);
1438*0a6a1f1dSLionel Sambuc if (n == 0 || (num % n) != 0) {
1439*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
1440*0a6a1f1dSLionel Sambuc SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1441ebfedea0SLionel Sambuc return (NULL);
1442ebfedea0SLionel Sambuc }
1443*0a6a1f1dSLionel Sambuc if ((skp == NULL) || (*skp == NULL)) {
1444ebfedea0SLionel Sambuc sk = sk_SSL_CIPHER_new_null(); /* change perhaps later */
1445*0a6a1f1dSLionel Sambuc if(sk == NULL) {
1446*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
1447*0a6a1f1dSLionel Sambuc return NULL;
1448*0a6a1f1dSLionel Sambuc }
1449*0a6a1f1dSLionel Sambuc } else {
1450ebfedea0SLionel Sambuc sk = *skp;
1451ebfedea0SLionel Sambuc sk_SSL_CIPHER_zero(sk);
1452ebfedea0SLionel Sambuc }
1453ebfedea0SLionel Sambuc
1454*0a6a1f1dSLionel Sambuc for (i = 0; i < num; i += n) {
1455*0a6a1f1dSLionel Sambuc /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
1456ebfedea0SLionel Sambuc if (s->s3 && (n != 3 || !p[0]) &&
1457ebfedea0SLionel Sambuc (p[n - 2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
1458*0a6a1f1dSLionel Sambuc (p[n - 1] == (SSL3_CK_SCSV & 0xff))) {
1459ebfedea0SLionel Sambuc /* SCSV fatal if renegotiating */
1460*0a6a1f1dSLionel Sambuc if (s->renegotiate) {
1461*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
1462*0a6a1f1dSLionel Sambuc SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1463ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1464ebfedea0SLionel Sambuc goto err;
1465ebfedea0SLionel Sambuc }
1466ebfedea0SLionel Sambuc s->s3->send_connection_binding = 1;
1467ebfedea0SLionel Sambuc p += n;
1468ebfedea0SLionel Sambuc #ifdef OPENSSL_RI_DEBUG
1469ebfedea0SLionel Sambuc fprintf(stderr, "SCSV received by server\n");
1470ebfedea0SLionel Sambuc #endif
1471ebfedea0SLionel Sambuc continue;
1472ebfedea0SLionel Sambuc }
1473ebfedea0SLionel Sambuc
1474*0a6a1f1dSLionel Sambuc /* Check for TLS_FALLBACK_SCSV */
1475*0a6a1f1dSLionel Sambuc if ((n != 3 || !p[0]) &&
1476*0a6a1f1dSLionel Sambuc (p[n - 2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) &&
1477*0a6a1f1dSLionel Sambuc (p[n - 1] == (SSL3_CK_FALLBACK_SCSV & 0xff))) {
1478*0a6a1f1dSLionel Sambuc /*
1479*0a6a1f1dSLionel Sambuc * The SCSV indicates that the client previously tried a higher
1480*0a6a1f1dSLionel Sambuc * version. Fail if the current version is an unexpected
1481*0a6a1f1dSLionel Sambuc * downgrade.
1482*0a6a1f1dSLionel Sambuc */
1483*0a6a1f1dSLionel Sambuc if (!SSL_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, 0, NULL)) {
1484*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
1485*0a6a1f1dSLionel Sambuc SSL_R_INAPPROPRIATE_FALLBACK);
1486*0a6a1f1dSLionel Sambuc if (s->s3)
1487*0a6a1f1dSLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL,
1488*0a6a1f1dSLionel Sambuc SSL_AD_INAPPROPRIATE_FALLBACK);
1489*0a6a1f1dSLionel Sambuc goto err;
1490*0a6a1f1dSLionel Sambuc }
1491*0a6a1f1dSLionel Sambuc p += n;
1492*0a6a1f1dSLionel Sambuc continue;
1493*0a6a1f1dSLionel Sambuc }
1494*0a6a1f1dSLionel Sambuc
1495ebfedea0SLionel Sambuc c = ssl_get_cipher_by_char(s, p);
1496ebfedea0SLionel Sambuc p += n;
1497*0a6a1f1dSLionel Sambuc if (c != NULL) {
1498*0a6a1f1dSLionel Sambuc if (!sk_SSL_CIPHER_push(sk, c)) {
1499ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
1500ebfedea0SLionel Sambuc goto err;
1501ebfedea0SLionel Sambuc }
1502ebfedea0SLionel Sambuc }
1503ebfedea0SLionel Sambuc }
1504ebfedea0SLionel Sambuc
1505ebfedea0SLionel Sambuc if (skp != NULL)
1506ebfedea0SLionel Sambuc *skp = sk;
1507ebfedea0SLionel Sambuc return (sk);
1508ebfedea0SLionel Sambuc err:
1509ebfedea0SLionel Sambuc if ((skp == NULL) || (*skp == NULL))
1510ebfedea0SLionel Sambuc sk_SSL_CIPHER_free(sk);
1511ebfedea0SLionel Sambuc return (NULL);
1512ebfedea0SLionel Sambuc }
1513ebfedea0SLionel Sambuc
1514ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
1515ebfedea0SLionel Sambuc /** return a servername extension value if provided in Client Hello, or NULL.
1516ebfedea0SLionel Sambuc * So far, only host_name types are defined (RFC 3546).
1517ebfedea0SLionel Sambuc */
1518ebfedea0SLionel Sambuc
SSL_get_servername(const SSL * s,const int type)1519ebfedea0SLionel Sambuc const char *SSL_get_servername(const SSL *s, const int type)
1520ebfedea0SLionel Sambuc {
1521ebfedea0SLionel Sambuc if (type != TLSEXT_NAMETYPE_host_name)
1522ebfedea0SLionel Sambuc return NULL;
1523ebfedea0SLionel Sambuc
1524ebfedea0SLionel Sambuc return s->session && !s->tlsext_hostname ?
1525*0a6a1f1dSLionel Sambuc s->session->tlsext_hostname : s->tlsext_hostname;
1526ebfedea0SLionel Sambuc }
1527ebfedea0SLionel Sambuc
SSL_get_servername_type(const SSL * s)1528ebfedea0SLionel Sambuc int SSL_get_servername_type(const SSL *s)
1529ebfedea0SLionel Sambuc {
1530*0a6a1f1dSLionel Sambuc if (s->session
1531*0a6a1f1dSLionel Sambuc && (!s->tlsext_hostname ? s->session->
1532*0a6a1f1dSLionel Sambuc tlsext_hostname : s->tlsext_hostname))
1533ebfedea0SLionel Sambuc return TLSEXT_NAMETYPE_host_name;
1534ebfedea0SLionel Sambuc return -1;
1535ebfedea0SLionel Sambuc }
1536ebfedea0SLionel Sambuc
1537ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_NEXTPROTONEG
1538*0a6a1f1dSLionel Sambuc /*
1539*0a6a1f1dSLionel Sambuc * SSL_select_next_proto implements the standard protocol selection. It is
1540ebfedea0SLionel Sambuc * expected that this function is called from the callback set by
1541*0a6a1f1dSLionel Sambuc * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a
1542*0a6a1f1dSLionel Sambuc * vector of 8-bit, length prefixed byte strings. The length byte itself is
1543*0a6a1f1dSLionel Sambuc * not included in the length. A byte string of length 0 is invalid. No byte
1544*0a6a1f1dSLionel Sambuc * string may be truncated. The current, but experimental algorithm for
1545*0a6a1f1dSLionel Sambuc * selecting the protocol is: 1) If the server doesn't support NPN then this
1546*0a6a1f1dSLionel Sambuc * is indicated to the callback. In this case, the client application has to
1547*0a6a1f1dSLionel Sambuc * abort the connection or have a default application level protocol. 2) If
1548*0a6a1f1dSLionel Sambuc * the server supports NPN, but advertises an empty list then the client
1549*0a6a1f1dSLionel Sambuc * selects the first protcol in its list, but indicates via the API that this
1550*0a6a1f1dSLionel Sambuc * fallback case was enacted. 3) Otherwise, the client finds the first
1551*0a6a1f1dSLionel Sambuc * protocol in the server's list that it supports and selects this protocol.
1552*0a6a1f1dSLionel Sambuc * This is because it's assumed that the server has better information about
1553*0a6a1f1dSLionel Sambuc * which protocol a client should use. 4) If the client doesn't support any
1554*0a6a1f1dSLionel Sambuc * of the server's advertised protocols, then this is treated the same as
1555*0a6a1f1dSLionel Sambuc * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was
1556*0a6a1f1dSLionel Sambuc * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1557ebfedea0SLionel Sambuc */
SSL_select_next_proto(unsigned char ** out,unsigned char * outlen,const unsigned char * server,unsigned int server_len,const unsigned char * client,unsigned int client_len)1558*0a6a1f1dSLionel Sambuc int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
1559*0a6a1f1dSLionel Sambuc const unsigned char *server,
1560*0a6a1f1dSLionel Sambuc unsigned int server_len,
1561*0a6a1f1dSLionel Sambuc const unsigned char *client,
1562*0a6a1f1dSLionel Sambuc unsigned int client_len)
1563ebfedea0SLionel Sambuc {
1564ebfedea0SLionel Sambuc unsigned int i, j;
1565ebfedea0SLionel Sambuc const unsigned char *result;
1566ebfedea0SLionel Sambuc int status = OPENSSL_NPN_UNSUPPORTED;
1567ebfedea0SLionel Sambuc
1568*0a6a1f1dSLionel Sambuc /*
1569*0a6a1f1dSLionel Sambuc * For each protocol in server preference order, see if we support it.
1570*0a6a1f1dSLionel Sambuc */
1571*0a6a1f1dSLionel Sambuc for (i = 0; i < server_len;) {
1572*0a6a1f1dSLionel Sambuc for (j = 0; j < client_len;) {
1573ebfedea0SLionel Sambuc if (server[i] == client[j] &&
1574*0a6a1f1dSLionel Sambuc memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
1575ebfedea0SLionel Sambuc /* We found a match */
1576ebfedea0SLionel Sambuc result = &server[i];
1577ebfedea0SLionel Sambuc status = OPENSSL_NPN_NEGOTIATED;
1578ebfedea0SLionel Sambuc goto found;
1579ebfedea0SLionel Sambuc }
1580ebfedea0SLionel Sambuc j += client[j];
1581ebfedea0SLionel Sambuc j++;
1582ebfedea0SLionel Sambuc }
1583ebfedea0SLionel Sambuc i += server[i];
1584ebfedea0SLionel Sambuc i++;
1585ebfedea0SLionel Sambuc }
1586ebfedea0SLionel Sambuc
1587ebfedea0SLionel Sambuc /* There's no overlap between our protocols and the server's list. */
1588ebfedea0SLionel Sambuc result = client;
1589ebfedea0SLionel Sambuc status = OPENSSL_NPN_NO_OVERLAP;
1590ebfedea0SLionel Sambuc
1591ebfedea0SLionel Sambuc found:
1592ebfedea0SLionel Sambuc *out = (unsigned char *)result + 1;
1593ebfedea0SLionel Sambuc *outlen = result[0];
1594ebfedea0SLionel Sambuc return status;
1595ebfedea0SLionel Sambuc }
1596ebfedea0SLionel Sambuc
1597*0a6a1f1dSLionel Sambuc /*
1598*0a6a1f1dSLionel Sambuc * SSL_get0_next_proto_negotiated sets *data and *len to point to the
1599*0a6a1f1dSLionel Sambuc * client's requested protocol for this connection and returns 0. If the
1600*0a6a1f1dSLionel Sambuc * client didn't request any protocol, then *data is set to NULL. Note that
1601*0a6a1f1dSLionel Sambuc * the client can request any protocol it chooses. The value returned from
1602*0a6a1f1dSLionel Sambuc * this function need not be a member of the list of supported protocols
1603ebfedea0SLionel Sambuc * provided by the callback.
1604ebfedea0SLionel Sambuc */
SSL_get0_next_proto_negotiated(const SSL * s,const unsigned char ** data,unsigned * len)1605*0a6a1f1dSLionel Sambuc void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
1606*0a6a1f1dSLionel Sambuc unsigned *len)
1607ebfedea0SLionel Sambuc {
1608ebfedea0SLionel Sambuc *data = s->next_proto_negotiated;
1609ebfedea0SLionel Sambuc if (!*data) {
1610ebfedea0SLionel Sambuc *len = 0;
1611ebfedea0SLionel Sambuc } else {
1612ebfedea0SLionel Sambuc *len = s->next_proto_negotiated_len;
1613ebfedea0SLionel Sambuc }
1614ebfedea0SLionel Sambuc }
1615ebfedea0SLionel Sambuc
1616*0a6a1f1dSLionel Sambuc /*
1617*0a6a1f1dSLionel Sambuc * SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when
1618*0a6a1f1dSLionel Sambuc * a TLS server needs a list of supported protocols for Next Protocol
1619*0a6a1f1dSLionel Sambuc * Negotiation. The returned list must be in wire format. The list is
1620*0a6a1f1dSLionel Sambuc * returned by setting |out| to point to it and |outlen| to its length. This
1621*0a6a1f1dSLionel Sambuc * memory will not be modified, but one should assume that the SSL* keeps a
1622*0a6a1f1dSLionel Sambuc * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it
1623*0a6a1f1dSLionel Sambuc * wishes to advertise. Otherwise, no such extension will be included in the
1624*0a6a1f1dSLionel Sambuc * ServerHello.
1625*0a6a1f1dSLionel Sambuc */
SSL_CTX_set_next_protos_advertised_cb(SSL_CTX * ctx,int (* cb)(SSL * ssl,const unsigned char ** out,unsigned int * outlen,void * arg),void * arg)1626*0a6a1f1dSLionel Sambuc void SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx,
1627*0a6a1f1dSLionel Sambuc int (*cb) (SSL *ssl,
1628*0a6a1f1dSLionel Sambuc const unsigned char
1629*0a6a1f1dSLionel Sambuc **out,
1630*0a6a1f1dSLionel Sambuc unsigned int *outlen,
1631*0a6a1f1dSLionel Sambuc void *arg), void *arg)
1632ebfedea0SLionel Sambuc {
1633ebfedea0SLionel Sambuc ctx->next_protos_advertised_cb = cb;
1634ebfedea0SLionel Sambuc ctx->next_protos_advertised_cb_arg = arg;
1635ebfedea0SLionel Sambuc }
1636ebfedea0SLionel Sambuc
1637*0a6a1f1dSLionel Sambuc /*
1638*0a6a1f1dSLionel Sambuc * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1639ebfedea0SLionel Sambuc * client needs to select a protocol from the server's provided list. |out|
1640ebfedea0SLionel Sambuc * must be set to point to the selected protocol (which may be within |in|).
1641*0a6a1f1dSLionel Sambuc * The length of the protocol name must be written into |outlen|. The
1642*0a6a1f1dSLionel Sambuc * server's advertised protocols are provided in |in| and |inlen|. The
1643*0a6a1f1dSLionel Sambuc * callback can assume that |in| is syntactically valid. The client must
1644*0a6a1f1dSLionel Sambuc * select a protocol. It is fatal to the connection if this callback returns
1645*0a6a1f1dSLionel Sambuc * a value other than SSL_TLSEXT_ERR_OK.
1646ebfedea0SLionel Sambuc */
SSL_CTX_set_next_proto_select_cb(SSL_CTX * ctx,int (* cb)(SSL * s,unsigned char ** out,unsigned char * outlen,const unsigned char * in,unsigned int inlen,void * arg),void * arg)1647*0a6a1f1dSLionel Sambuc void SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx,
1648*0a6a1f1dSLionel Sambuc int (*cb) (SSL *s, unsigned char **out,
1649*0a6a1f1dSLionel Sambuc unsigned char *outlen,
1650*0a6a1f1dSLionel Sambuc const unsigned char *in,
1651*0a6a1f1dSLionel Sambuc unsigned int inlen,
1652*0a6a1f1dSLionel Sambuc void *arg), void *arg)
1653ebfedea0SLionel Sambuc {
1654ebfedea0SLionel Sambuc ctx->next_proto_select_cb = cb;
1655ebfedea0SLionel Sambuc ctx->next_proto_select_cb_arg = arg;
1656ebfedea0SLionel Sambuc }
1657ebfedea0SLionel Sambuc # endif
1658ebfedea0SLionel Sambuc #endif
1659ebfedea0SLionel Sambuc
SSL_export_keying_material(SSL * s,unsigned char * out,size_t olen,const char * label,size_t llen,const unsigned char * p,size_t plen,int use_context)1660ebfedea0SLionel Sambuc int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1661*0a6a1f1dSLionel Sambuc const char *label, size_t llen,
1662*0a6a1f1dSLionel Sambuc const unsigned char *p, size_t plen,
1663ebfedea0SLionel Sambuc int use_context)
1664ebfedea0SLionel Sambuc {
1665ebfedea0SLionel Sambuc if (s->version < TLS1_VERSION)
1666ebfedea0SLionel Sambuc return -1;
1667ebfedea0SLionel Sambuc
1668ebfedea0SLionel Sambuc return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1669ebfedea0SLionel Sambuc llen, p, plen,
1670ebfedea0SLionel Sambuc use_context);
1671ebfedea0SLionel Sambuc }
1672ebfedea0SLionel Sambuc
ssl_session_hash(const SSL_SESSION * a)1673ebfedea0SLionel Sambuc static unsigned long ssl_session_hash(const SSL_SESSION *a)
1674ebfedea0SLionel Sambuc {
1675ebfedea0SLionel Sambuc unsigned long l;
1676ebfedea0SLionel Sambuc
1677ebfedea0SLionel Sambuc l = (unsigned long)
1678ebfedea0SLionel Sambuc ((unsigned int)a->session_id[0]) |
1679ebfedea0SLionel Sambuc ((unsigned int)a->session_id[1] << 8L) |
1680ebfedea0SLionel Sambuc ((unsigned long)a->session_id[2] << 16L) |
1681ebfedea0SLionel Sambuc ((unsigned long)a->session_id[3] << 24L);
1682ebfedea0SLionel Sambuc return (l);
1683ebfedea0SLionel Sambuc }
1684ebfedea0SLionel Sambuc
1685*0a6a1f1dSLionel Sambuc /*
1686*0a6a1f1dSLionel Sambuc * NB: If this function (or indeed the hash function which uses a sort of
1687ebfedea0SLionel Sambuc * coarser function than this one) is changed, ensure
1688*0a6a1f1dSLionel Sambuc * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on
1689*0a6a1f1dSLionel Sambuc * being able to construct an SSL_SESSION that will collide with any existing
1690*0a6a1f1dSLionel Sambuc * session with a matching session ID.
1691*0a6a1f1dSLionel Sambuc */
ssl_session_cmp(const SSL_SESSION * a,const SSL_SESSION * b)1692ebfedea0SLionel Sambuc static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1693ebfedea0SLionel Sambuc {
1694ebfedea0SLionel Sambuc if (a->ssl_version != b->ssl_version)
1695ebfedea0SLionel Sambuc return (1);
1696ebfedea0SLionel Sambuc if (a->session_id_length != b->session_id_length)
1697ebfedea0SLionel Sambuc return (1);
1698ebfedea0SLionel Sambuc return (memcmp(a->session_id, b->session_id, a->session_id_length));
1699ebfedea0SLionel Sambuc }
1700ebfedea0SLionel Sambuc
1701*0a6a1f1dSLionel Sambuc /*
1702*0a6a1f1dSLionel Sambuc * These wrapper functions should remain rather than redeclaring
1703ebfedea0SLionel Sambuc * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
1704*0a6a1f1dSLionel Sambuc * variable. The reason is that the functions aren't static, they're exposed
1705*0a6a1f1dSLionel Sambuc * via ssl.h.
1706*0a6a1f1dSLionel Sambuc */
IMPLEMENT_LHASH_HASH_FN(ssl_session,SSL_SESSION)1707ebfedea0SLionel Sambuc static IMPLEMENT_LHASH_HASH_FN(ssl_session, SSL_SESSION)
1708ebfedea0SLionel Sambuc static IMPLEMENT_LHASH_COMP_FN(ssl_session, SSL_SESSION)
1709ebfedea0SLionel Sambuc
1710ebfedea0SLionel Sambuc SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1711ebfedea0SLionel Sambuc {
1712ebfedea0SLionel Sambuc SSL_CTX *ret = NULL;
1713ebfedea0SLionel Sambuc
1714*0a6a1f1dSLionel Sambuc if (meth == NULL) {
1715ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_NULL_SSL_METHOD_PASSED);
1716ebfedea0SLionel Sambuc return (NULL);
1717ebfedea0SLionel Sambuc }
1718ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
1719*0a6a1f1dSLionel Sambuc if (FIPS_mode() && (meth->version < TLS1_VERSION)) {
1720ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
1721ebfedea0SLionel Sambuc return NULL;
1722ebfedea0SLionel Sambuc }
1723ebfedea0SLionel Sambuc #endif
1724ebfedea0SLionel Sambuc
1725*0a6a1f1dSLionel Sambuc if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1726ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1727ebfedea0SLionel Sambuc goto err;
1728ebfedea0SLionel Sambuc }
1729ebfedea0SLionel Sambuc ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1730ebfedea0SLionel Sambuc if (ret == NULL)
1731ebfedea0SLionel Sambuc goto err;
1732ebfedea0SLionel Sambuc
1733ebfedea0SLionel Sambuc memset(ret, 0, sizeof(SSL_CTX));
1734ebfedea0SLionel Sambuc
1735ebfedea0SLionel Sambuc ret->method = meth;
1736ebfedea0SLionel Sambuc
1737ebfedea0SLionel Sambuc ret->cert_store = NULL;
1738ebfedea0SLionel Sambuc ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
1739ebfedea0SLionel Sambuc ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1740ebfedea0SLionel Sambuc ret->session_cache_head = NULL;
1741ebfedea0SLionel Sambuc ret->session_cache_tail = NULL;
1742ebfedea0SLionel Sambuc
1743ebfedea0SLionel Sambuc /* We take the system default */
1744ebfedea0SLionel Sambuc ret->session_timeout = meth->get_timeout();
1745ebfedea0SLionel Sambuc
1746ebfedea0SLionel Sambuc ret->new_session_cb = 0;
1747ebfedea0SLionel Sambuc ret->remove_session_cb = 0;
1748ebfedea0SLionel Sambuc ret->get_session_cb = 0;
1749ebfedea0SLionel Sambuc ret->generate_session_id = 0;
1750ebfedea0SLionel Sambuc
1751ebfedea0SLionel Sambuc memset((char *)&ret->stats, 0, sizeof(ret->stats));
1752ebfedea0SLionel Sambuc
1753ebfedea0SLionel Sambuc ret->references = 1;
1754ebfedea0SLionel Sambuc ret->quiet_shutdown = 0;
1755ebfedea0SLionel Sambuc
1756ebfedea0SLionel Sambuc /* ret->cipher=NULL;*/
1757*0a6a1f1dSLionel Sambuc /*-
1758*0a6a1f1dSLionel Sambuc ret->s2->challenge=NULL;
1759ebfedea0SLionel Sambuc ret->master_key=NULL;
1760ebfedea0SLionel Sambuc ret->key_arg=NULL;
1761ebfedea0SLionel Sambuc ret->s2->conn_id=NULL; */
1762ebfedea0SLionel Sambuc
1763ebfedea0SLionel Sambuc ret->info_callback = NULL;
1764ebfedea0SLionel Sambuc
1765ebfedea0SLionel Sambuc ret->app_verify_callback = 0;
1766ebfedea0SLionel Sambuc ret->app_verify_arg = NULL;
1767ebfedea0SLionel Sambuc
1768ebfedea0SLionel Sambuc ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1769ebfedea0SLionel Sambuc ret->read_ahead = 0;
1770ebfedea0SLionel Sambuc ret->msg_callback = 0;
1771ebfedea0SLionel Sambuc ret->msg_callback_arg = NULL;
1772ebfedea0SLionel Sambuc ret->verify_mode = SSL_VERIFY_NONE;
1773ebfedea0SLionel Sambuc #if 0
1774ebfedea0SLionel Sambuc ret->verify_depth = -1; /* Don't impose a limit (but x509_lu.c does) */
1775ebfedea0SLionel Sambuc #endif
1776ebfedea0SLionel Sambuc ret->sid_ctx_length = 0;
1777ebfedea0SLionel Sambuc ret->default_verify_callback = NULL;
1778ebfedea0SLionel Sambuc if ((ret->cert = ssl_cert_new()) == NULL)
1779ebfedea0SLionel Sambuc goto err;
1780ebfedea0SLionel Sambuc
1781ebfedea0SLionel Sambuc ret->default_passwd_callback = 0;
1782ebfedea0SLionel Sambuc ret->default_passwd_callback_userdata = NULL;
1783ebfedea0SLionel Sambuc ret->client_cert_cb = 0;
1784ebfedea0SLionel Sambuc ret->app_gen_cookie_cb = 0;
1785ebfedea0SLionel Sambuc ret->app_verify_cookie_cb = 0;
1786ebfedea0SLionel Sambuc
1787ebfedea0SLionel Sambuc ret->sessions = lh_SSL_SESSION_new();
1788*0a6a1f1dSLionel Sambuc if (ret->sessions == NULL)
1789*0a6a1f1dSLionel Sambuc goto err;
1790ebfedea0SLionel Sambuc ret->cert_store = X509_STORE_new();
1791*0a6a1f1dSLionel Sambuc if (ret->cert_store == NULL)
1792*0a6a1f1dSLionel Sambuc goto err;
1793ebfedea0SLionel Sambuc
1794ebfedea0SLionel Sambuc ssl_create_cipher_list(ret->method,
1795ebfedea0SLionel Sambuc &ret->cipher_list, &ret->cipher_list_by_id,
1796*0a6a1f1dSLionel Sambuc meth->version ==
1797*0a6a1f1dSLionel Sambuc SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST);
1798*0a6a1f1dSLionel Sambuc if (ret->cipher_list == NULL || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
1799ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1800ebfedea0SLionel Sambuc goto err2;
1801ebfedea0SLionel Sambuc }
1802ebfedea0SLionel Sambuc
1803ebfedea0SLionel Sambuc ret->param = X509_VERIFY_PARAM_new();
1804ebfedea0SLionel Sambuc if (!ret->param)
1805ebfedea0SLionel Sambuc goto err;
1806ebfedea0SLionel Sambuc
1807*0a6a1f1dSLionel Sambuc if ((ret->rsa_md5 = EVP_get_digestbyname("ssl2-md5")) == NULL) {
1808ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL2_MD5_ROUTINES);
1809ebfedea0SLionel Sambuc goto err2;
1810ebfedea0SLionel Sambuc }
1811*0a6a1f1dSLionel Sambuc if ((ret->md5 = EVP_get_digestbyname("ssl3-md5")) == NULL) {
1812ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
1813ebfedea0SLionel Sambuc goto err2;
1814ebfedea0SLionel Sambuc }
1815*0a6a1f1dSLionel Sambuc if ((ret->sha1 = EVP_get_digestbyname("ssl3-sha1")) == NULL) {
1816ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
1817ebfedea0SLionel Sambuc goto err2;
1818ebfedea0SLionel Sambuc }
1819ebfedea0SLionel Sambuc
1820ebfedea0SLionel Sambuc if ((ret->client_CA = sk_X509_NAME_new_null()) == NULL)
1821ebfedea0SLionel Sambuc goto err;
1822ebfedea0SLionel Sambuc
1823ebfedea0SLionel Sambuc CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1824ebfedea0SLionel Sambuc
1825ebfedea0SLionel Sambuc ret->extra_certs = NULL;
1826*0a6a1f1dSLionel Sambuc /* No compression for DTLS */
1827*0a6a1f1dSLionel Sambuc if (meth->version != DTLS1_VERSION)
1828ebfedea0SLionel Sambuc ret->comp_methods = SSL_COMP_get_compression_methods();
1829ebfedea0SLionel Sambuc
1830ebfedea0SLionel Sambuc ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1831ebfedea0SLionel Sambuc
1832ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
1833ebfedea0SLionel Sambuc ret->tlsext_servername_callback = 0;
1834ebfedea0SLionel Sambuc ret->tlsext_servername_arg = NULL;
1835ebfedea0SLionel Sambuc /* Setup RFC4507 ticket keys */
1836ebfedea0SLionel Sambuc if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
1837ebfedea0SLionel Sambuc || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
1838ebfedea0SLionel Sambuc || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
1839ebfedea0SLionel Sambuc ret->options |= SSL_OP_NO_TICKET;
1840ebfedea0SLionel Sambuc
1841ebfedea0SLionel Sambuc ret->tlsext_status_cb = 0;
1842ebfedea0SLionel Sambuc ret->tlsext_status_arg = NULL;
1843ebfedea0SLionel Sambuc
1844ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_NEXTPROTONEG
1845ebfedea0SLionel Sambuc ret->next_protos_advertised_cb = 0;
1846ebfedea0SLionel Sambuc ret->next_proto_select_cb = 0;
1847ebfedea0SLionel Sambuc # endif
1848ebfedea0SLionel Sambuc #endif
1849ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
1850ebfedea0SLionel Sambuc ret->psk_identity_hint = NULL;
1851ebfedea0SLionel Sambuc ret->psk_client_callback = NULL;
1852ebfedea0SLionel Sambuc ret->psk_server_callback = NULL;
1853ebfedea0SLionel Sambuc #endif
1854ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SRP
1855ebfedea0SLionel Sambuc SSL_CTX_SRP_CTX_init(ret);
1856ebfedea0SLionel Sambuc #endif
1857ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_BUF_FREELISTS
1858ebfedea0SLionel Sambuc ret->freelist_max_len = SSL_MAX_BUF_FREELIST_LEN_DEFAULT;
1859ebfedea0SLionel Sambuc ret->rbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));
1860ebfedea0SLionel Sambuc if (!ret->rbuf_freelist)
1861ebfedea0SLionel Sambuc goto err;
1862ebfedea0SLionel Sambuc ret->rbuf_freelist->chunklen = 0;
1863ebfedea0SLionel Sambuc ret->rbuf_freelist->len = 0;
1864ebfedea0SLionel Sambuc ret->rbuf_freelist->head = NULL;
1865ebfedea0SLionel Sambuc ret->wbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));
1866*0a6a1f1dSLionel Sambuc if (!ret->wbuf_freelist) {
1867ebfedea0SLionel Sambuc OPENSSL_free(ret->rbuf_freelist);
1868ebfedea0SLionel Sambuc goto err;
1869ebfedea0SLionel Sambuc }
1870ebfedea0SLionel Sambuc ret->wbuf_freelist->chunklen = 0;
1871ebfedea0SLionel Sambuc ret->wbuf_freelist->len = 0;
1872ebfedea0SLionel Sambuc ret->wbuf_freelist->head = NULL;
1873ebfedea0SLionel Sambuc #endif
1874ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
1875ebfedea0SLionel Sambuc ret->client_cert_engine = NULL;
1876ebfedea0SLionel Sambuc # ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
1877ebfedea0SLionel Sambuc # define eng_strx(x) #x
1878ebfedea0SLionel Sambuc # define eng_str(x) eng_strx(x)
1879ebfedea0SLionel Sambuc /* Use specific client engine automatically... ignore errors */
1880ebfedea0SLionel Sambuc {
1881ebfedea0SLionel Sambuc ENGINE *eng;
1882ebfedea0SLionel Sambuc eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
1883*0a6a1f1dSLionel Sambuc if (!eng) {
1884ebfedea0SLionel Sambuc ERR_clear_error();
1885ebfedea0SLionel Sambuc ENGINE_load_builtin_engines();
1886ebfedea0SLionel Sambuc eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
1887ebfedea0SLionel Sambuc }
1888ebfedea0SLionel Sambuc if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
1889ebfedea0SLionel Sambuc ERR_clear_error();
1890ebfedea0SLionel Sambuc }
1891ebfedea0SLionel Sambuc # endif
1892ebfedea0SLionel Sambuc #endif
1893*0a6a1f1dSLionel Sambuc /*
1894*0a6a1f1dSLionel Sambuc * Default is to connect to non-RI servers. When RI is more widely
1895ebfedea0SLionel Sambuc * deployed might change this.
1896ebfedea0SLionel Sambuc */
1897ebfedea0SLionel Sambuc ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1898ebfedea0SLionel Sambuc
1899ebfedea0SLionel Sambuc return (ret);
1900ebfedea0SLionel Sambuc err:
1901ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE);
1902ebfedea0SLionel Sambuc err2:
1903*0a6a1f1dSLionel Sambuc if (ret != NULL)
1904*0a6a1f1dSLionel Sambuc SSL_CTX_free(ret);
1905ebfedea0SLionel Sambuc return (NULL);
1906ebfedea0SLionel Sambuc }
1907ebfedea0SLionel Sambuc
1908ebfedea0SLionel Sambuc #if 0
1909ebfedea0SLionel Sambuc static void SSL_COMP_free(SSL_COMP *comp)
1910*0a6a1f1dSLionel Sambuc {
1911*0a6a1f1dSLionel Sambuc OPENSSL_free(comp);
1912*0a6a1f1dSLionel Sambuc }
1913ebfedea0SLionel Sambuc #endif
1914ebfedea0SLionel Sambuc
1915ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_BUF_FREELISTS
ssl_buf_freelist_free(SSL3_BUF_FREELIST * list)1916*0a6a1f1dSLionel Sambuc static void ssl_buf_freelist_free(SSL3_BUF_FREELIST *list)
1917ebfedea0SLionel Sambuc {
1918ebfedea0SLionel Sambuc SSL3_BUF_FREELIST_ENTRY *ent, *next;
1919*0a6a1f1dSLionel Sambuc for (ent = list->head; ent; ent = next) {
1920ebfedea0SLionel Sambuc next = ent->next;
1921ebfedea0SLionel Sambuc OPENSSL_free(ent);
1922ebfedea0SLionel Sambuc }
1923ebfedea0SLionel Sambuc OPENSSL_free(list);
1924ebfedea0SLionel Sambuc }
1925ebfedea0SLionel Sambuc #endif
1926ebfedea0SLionel Sambuc
SSL_CTX_free(SSL_CTX * a)1927ebfedea0SLionel Sambuc void SSL_CTX_free(SSL_CTX *a)
1928ebfedea0SLionel Sambuc {
1929ebfedea0SLionel Sambuc int i;
1930ebfedea0SLionel Sambuc
1931*0a6a1f1dSLionel Sambuc if (a == NULL)
1932*0a6a1f1dSLionel Sambuc return;
1933ebfedea0SLionel Sambuc
1934ebfedea0SLionel Sambuc i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);
1935ebfedea0SLionel Sambuc #ifdef REF_PRINT
1936ebfedea0SLionel Sambuc REF_PRINT("SSL_CTX", a);
1937ebfedea0SLionel Sambuc #endif
1938*0a6a1f1dSLionel Sambuc if (i > 0)
1939*0a6a1f1dSLionel Sambuc return;
1940ebfedea0SLionel Sambuc #ifdef REF_CHECK
1941*0a6a1f1dSLionel Sambuc if (i < 0) {
1942ebfedea0SLionel Sambuc fprintf(stderr, "SSL_CTX_free, bad reference count\n");
1943ebfedea0SLionel Sambuc abort(); /* ok */
1944ebfedea0SLionel Sambuc }
1945ebfedea0SLionel Sambuc #endif
1946ebfedea0SLionel Sambuc
1947ebfedea0SLionel Sambuc if (a->param)
1948ebfedea0SLionel Sambuc X509_VERIFY_PARAM_free(a->param);
1949ebfedea0SLionel Sambuc
1950ebfedea0SLionel Sambuc /*
1951ebfedea0SLionel Sambuc * Free internal session cache. However: the remove_cb() may reference
1952ebfedea0SLionel Sambuc * the ex_data of SSL_CTX, thus the ex_data store can only be removed
1953ebfedea0SLionel Sambuc * after the sessions were flushed.
1954ebfedea0SLionel Sambuc * As the ex_data handling routines might also touch the session cache,
1955ebfedea0SLionel Sambuc * the most secure solution seems to be: empty (flush) the cache, then
1956ebfedea0SLionel Sambuc * free ex_data, then finally free the cache.
1957ebfedea0SLionel Sambuc * (See ticket [openssl.org #212].)
1958ebfedea0SLionel Sambuc */
1959ebfedea0SLionel Sambuc if (a->sessions != NULL)
1960ebfedea0SLionel Sambuc SSL_CTX_flush_sessions(a, 0);
1961ebfedea0SLionel Sambuc
1962ebfedea0SLionel Sambuc CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
1963ebfedea0SLionel Sambuc
1964ebfedea0SLionel Sambuc if (a->sessions != NULL)
1965ebfedea0SLionel Sambuc lh_SSL_SESSION_free(a->sessions);
1966ebfedea0SLionel Sambuc
1967ebfedea0SLionel Sambuc if (a->cert_store != NULL)
1968ebfedea0SLionel Sambuc X509_STORE_free(a->cert_store);
1969ebfedea0SLionel Sambuc if (a->cipher_list != NULL)
1970ebfedea0SLionel Sambuc sk_SSL_CIPHER_free(a->cipher_list);
1971ebfedea0SLionel Sambuc if (a->cipher_list_by_id != NULL)
1972ebfedea0SLionel Sambuc sk_SSL_CIPHER_free(a->cipher_list_by_id);
1973ebfedea0SLionel Sambuc if (a->cert != NULL)
1974ebfedea0SLionel Sambuc ssl_cert_free(a->cert);
1975ebfedea0SLionel Sambuc if (a->client_CA != NULL)
1976ebfedea0SLionel Sambuc sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);
1977ebfedea0SLionel Sambuc if (a->extra_certs != NULL)
1978ebfedea0SLionel Sambuc sk_X509_pop_free(a->extra_certs, X509_free);
1979*0a6a1f1dSLionel Sambuc #if 0 /* This should never be done, since it
1980*0a6a1f1dSLionel Sambuc * removes a global database */
1981ebfedea0SLionel Sambuc if (a->comp_methods != NULL)
1982ebfedea0SLionel Sambuc sk_SSL_COMP_pop_free(a->comp_methods, SSL_COMP_free);
1983ebfedea0SLionel Sambuc #else
1984ebfedea0SLionel Sambuc a->comp_methods = NULL;
1985ebfedea0SLionel Sambuc #endif
1986ebfedea0SLionel Sambuc
1987ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SRTP
1988ebfedea0SLionel Sambuc if (a->srtp_profiles)
1989ebfedea0SLionel Sambuc sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
1990ebfedea0SLionel Sambuc #endif
1991ebfedea0SLionel Sambuc
1992ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
1993ebfedea0SLionel Sambuc if (a->psk_identity_hint)
1994ebfedea0SLionel Sambuc OPENSSL_free(a->psk_identity_hint);
1995ebfedea0SLionel Sambuc #endif
1996ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SRP
1997ebfedea0SLionel Sambuc SSL_CTX_SRP_CTX_free(a);
1998ebfedea0SLionel Sambuc #endif
1999ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
2000ebfedea0SLionel Sambuc if (a->client_cert_engine)
2001ebfedea0SLionel Sambuc ENGINE_finish(a->client_cert_engine);
2002ebfedea0SLionel Sambuc #endif
2003ebfedea0SLionel Sambuc
2004ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_BUF_FREELISTS
2005ebfedea0SLionel Sambuc if (a->wbuf_freelist)
2006ebfedea0SLionel Sambuc ssl_buf_freelist_free(a->wbuf_freelist);
2007ebfedea0SLionel Sambuc if (a->rbuf_freelist)
2008ebfedea0SLionel Sambuc ssl_buf_freelist_free(a->rbuf_freelist);
2009ebfedea0SLionel Sambuc #endif
2010ebfedea0SLionel Sambuc
2011ebfedea0SLionel Sambuc OPENSSL_free(a);
2012ebfedea0SLionel Sambuc }
2013ebfedea0SLionel Sambuc
SSL_CTX_set_default_passwd_cb(SSL_CTX * ctx,pem_password_cb * cb)2014ebfedea0SLionel Sambuc void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2015ebfedea0SLionel Sambuc {
2016ebfedea0SLionel Sambuc ctx->default_passwd_callback = cb;
2017ebfedea0SLionel Sambuc }
2018ebfedea0SLionel Sambuc
SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX * ctx,void * u)2019ebfedea0SLionel Sambuc void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
2020ebfedea0SLionel Sambuc {
2021ebfedea0SLionel Sambuc ctx->default_passwd_callback_userdata = u;
2022ebfedea0SLionel Sambuc }
2023ebfedea0SLionel Sambuc
SSL_CTX_set_cert_verify_callback(SSL_CTX * ctx,int (* cb)(X509_STORE_CTX *,void *),void * arg)2024*0a6a1f1dSLionel Sambuc void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
2025*0a6a1f1dSLionel Sambuc int (*cb) (X509_STORE_CTX *, void *),
2026*0a6a1f1dSLionel Sambuc void *arg)
2027ebfedea0SLionel Sambuc {
2028ebfedea0SLionel Sambuc ctx->app_verify_callback = cb;
2029ebfedea0SLionel Sambuc ctx->app_verify_arg = arg;
2030ebfedea0SLionel Sambuc }
2031ebfedea0SLionel Sambuc
SSL_CTX_set_verify(SSL_CTX * ctx,int mode,int (* cb)(int,X509_STORE_CTX *))2032*0a6a1f1dSLionel Sambuc void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
2033*0a6a1f1dSLionel Sambuc int (*cb) (int, X509_STORE_CTX *))
2034ebfedea0SLionel Sambuc {
2035ebfedea0SLionel Sambuc ctx->verify_mode = mode;
2036ebfedea0SLionel Sambuc ctx->default_verify_callback = cb;
2037ebfedea0SLionel Sambuc }
2038ebfedea0SLionel Sambuc
SSL_CTX_set_verify_depth(SSL_CTX * ctx,int depth)2039ebfedea0SLionel Sambuc void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
2040ebfedea0SLionel Sambuc {
2041ebfedea0SLionel Sambuc X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2042ebfedea0SLionel Sambuc }
2043ebfedea0SLionel Sambuc
ssl_set_cert_masks(CERT * c,const SSL_CIPHER * cipher)2044ebfedea0SLionel Sambuc void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
2045ebfedea0SLionel Sambuc {
2046ebfedea0SLionel Sambuc CERT_PKEY *cpk;
2047ebfedea0SLionel Sambuc int rsa_enc, rsa_tmp, rsa_sign, dh_tmp, dh_rsa, dh_dsa, dsa_sign;
2048ebfedea0SLionel Sambuc int rsa_enc_export, dh_rsa_export, dh_dsa_export;
2049ebfedea0SLionel Sambuc int rsa_tmp_export, dh_tmp_export, kl;
2050ebfedea0SLionel Sambuc unsigned long mask_k, mask_a, emask_k, emask_a;
2051*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_ECDSA
2052*0a6a1f1dSLionel Sambuc int have_ecc_cert, ecdsa_ok, ecc_pkey_size;
2053ebfedea0SLionel Sambuc #endif
2054*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_ECDH
2055*0a6a1f1dSLionel Sambuc int have_ecdh_tmp, ecdh_ok;
2056*0a6a1f1dSLionel Sambuc #endif
2057*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_EC
2058ebfedea0SLionel Sambuc X509 *x = NULL;
2059ebfedea0SLionel Sambuc EVP_PKEY *ecc_pkey = NULL;
2060ebfedea0SLionel Sambuc int signature_nid = 0, pk_nid = 0, md_nid = 0;
2061*0a6a1f1dSLionel Sambuc #endif
2062*0a6a1f1dSLionel Sambuc if (c == NULL)
2063*0a6a1f1dSLionel Sambuc return;
2064ebfedea0SLionel Sambuc
2065ebfedea0SLionel Sambuc kl = SSL_C_EXPORT_PKEYLENGTH(cipher);
2066ebfedea0SLionel Sambuc
2067ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
2068ebfedea0SLionel Sambuc rsa_tmp = (c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
2069ebfedea0SLionel Sambuc rsa_tmp_export = (c->rsa_tmp_cb != NULL ||
2070ebfedea0SLionel Sambuc (rsa_tmp && RSA_size(c->rsa_tmp) * 8 <= kl));
2071ebfedea0SLionel Sambuc #else
2072ebfedea0SLionel Sambuc rsa_tmp = rsa_tmp_export = 0;
2073ebfedea0SLionel Sambuc #endif
2074ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
2075ebfedea0SLionel Sambuc dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
2076ebfedea0SLionel Sambuc dh_tmp_export = (c->dh_tmp_cb != NULL ||
2077ebfedea0SLionel Sambuc (dh_tmp && DH_size(c->dh_tmp) * 8 <= kl));
2078ebfedea0SLionel Sambuc #else
2079ebfedea0SLionel Sambuc dh_tmp = dh_tmp_export = 0;
2080ebfedea0SLionel Sambuc #endif
2081ebfedea0SLionel Sambuc
2082ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
2083ebfedea0SLionel Sambuc have_ecdh_tmp = (c->ecdh_tmp != NULL || c->ecdh_tmp_cb != NULL);
2084ebfedea0SLionel Sambuc #endif
2085ebfedea0SLionel Sambuc cpk = &(c->pkeys[SSL_PKEY_RSA_ENC]);
2086ebfedea0SLionel Sambuc rsa_enc = (cpk->x509 != NULL && cpk->privatekey != NULL);
2087ebfedea0SLionel Sambuc rsa_enc_export = (rsa_enc && EVP_PKEY_size(cpk->privatekey) * 8 <= kl);
2088ebfedea0SLionel Sambuc cpk = &(c->pkeys[SSL_PKEY_RSA_SIGN]);
2089ebfedea0SLionel Sambuc rsa_sign = (cpk->x509 != NULL && cpk->privatekey != NULL);
2090ebfedea0SLionel Sambuc cpk = &(c->pkeys[SSL_PKEY_DSA_SIGN]);
2091ebfedea0SLionel Sambuc dsa_sign = (cpk->x509 != NULL && cpk->privatekey != NULL);
2092ebfedea0SLionel Sambuc cpk = &(c->pkeys[SSL_PKEY_DH_RSA]);
2093ebfedea0SLionel Sambuc dh_rsa = (cpk->x509 != NULL && cpk->privatekey != NULL);
2094ebfedea0SLionel Sambuc dh_rsa_export = (dh_rsa && EVP_PKEY_size(cpk->privatekey) * 8 <= kl);
2095ebfedea0SLionel Sambuc cpk = &(c->pkeys[SSL_PKEY_DH_DSA]);
2096ebfedea0SLionel Sambuc /* FIX THIS EAY EAY EAY */
2097ebfedea0SLionel Sambuc dh_dsa = (cpk->x509 != NULL && cpk->privatekey != NULL);
2098ebfedea0SLionel Sambuc dh_dsa_export = (dh_dsa && EVP_PKEY_size(cpk->privatekey) * 8 <= kl);
2099ebfedea0SLionel Sambuc cpk = &(c->pkeys[SSL_PKEY_ECC]);
2100*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_EC
2101ebfedea0SLionel Sambuc have_ecc_cert = (cpk->x509 != NULL && cpk->privatekey != NULL);
2102*0a6a1f1dSLionel Sambuc #endif
2103ebfedea0SLionel Sambuc mask_k = 0;
2104ebfedea0SLionel Sambuc mask_a = 0;
2105ebfedea0SLionel Sambuc emask_k = 0;
2106ebfedea0SLionel Sambuc emask_a = 0;
2107ebfedea0SLionel Sambuc
2108ebfedea0SLionel Sambuc #ifdef CIPHER_DEBUG
2109*0a6a1f1dSLionel Sambuc fprintf(stderr,
2110*0a6a1f1dSLionel Sambuc "rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
2111*0a6a1f1dSLionel Sambuc rsa_tmp, rsa_tmp_export, dh_tmp, have_ecdh_tmp, rsa_enc,
2112*0a6a1f1dSLionel Sambuc rsa_enc_export, rsa_sign, dsa_sign, dh_rsa, dh_dsa);
2113ebfedea0SLionel Sambuc #endif
2114ebfedea0SLionel Sambuc
2115ebfedea0SLionel Sambuc cpk = &(c->pkeys[SSL_PKEY_GOST01]);
2116ebfedea0SLionel Sambuc if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2117ebfedea0SLionel Sambuc mask_k |= SSL_kGOST;
2118ebfedea0SLionel Sambuc mask_a |= SSL_aGOST01;
2119ebfedea0SLionel Sambuc }
2120ebfedea0SLionel Sambuc cpk = &(c->pkeys[SSL_PKEY_GOST94]);
2121ebfedea0SLionel Sambuc if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2122ebfedea0SLionel Sambuc mask_k |= SSL_kGOST;
2123ebfedea0SLionel Sambuc mask_a |= SSL_aGOST94;
2124ebfedea0SLionel Sambuc }
2125ebfedea0SLionel Sambuc
2126ebfedea0SLionel Sambuc if (rsa_enc || (rsa_tmp && rsa_sign))
2127ebfedea0SLionel Sambuc mask_k |= SSL_kRSA;
2128ebfedea0SLionel Sambuc if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc)))
2129ebfedea0SLionel Sambuc emask_k |= SSL_kRSA;
2130ebfedea0SLionel Sambuc
2131ebfedea0SLionel Sambuc #if 0
2132ebfedea0SLionel Sambuc /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
2133*0a6a1f1dSLionel Sambuc if ((dh_tmp || dh_rsa || dh_dsa) && (rsa_enc || rsa_sign || dsa_sign))
2134ebfedea0SLionel Sambuc mask_k |= SSL_kEDH;
2135ebfedea0SLionel Sambuc if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
2136ebfedea0SLionel Sambuc (rsa_enc || rsa_sign || dsa_sign))
2137ebfedea0SLionel Sambuc emask_k |= SSL_kEDH;
2138ebfedea0SLionel Sambuc #endif
2139ebfedea0SLionel Sambuc
2140ebfedea0SLionel Sambuc if (dh_tmp_export)
2141ebfedea0SLionel Sambuc emask_k |= SSL_kEDH;
2142ebfedea0SLionel Sambuc
2143ebfedea0SLionel Sambuc if (dh_tmp)
2144ebfedea0SLionel Sambuc mask_k |= SSL_kEDH;
2145ebfedea0SLionel Sambuc
2146*0a6a1f1dSLionel Sambuc if (dh_rsa)
2147*0a6a1f1dSLionel Sambuc mask_k |= SSL_kDHr;
2148*0a6a1f1dSLionel Sambuc if (dh_rsa_export)
2149*0a6a1f1dSLionel Sambuc emask_k |= SSL_kDHr;
2150ebfedea0SLionel Sambuc
2151*0a6a1f1dSLionel Sambuc if (dh_dsa)
2152*0a6a1f1dSLionel Sambuc mask_k |= SSL_kDHd;
2153*0a6a1f1dSLionel Sambuc if (dh_dsa_export)
2154*0a6a1f1dSLionel Sambuc emask_k |= SSL_kDHd;
2155ebfedea0SLionel Sambuc
2156*0a6a1f1dSLionel Sambuc if (rsa_enc || rsa_sign) {
2157ebfedea0SLionel Sambuc mask_a |= SSL_aRSA;
2158ebfedea0SLionel Sambuc emask_a |= SSL_aRSA;
2159ebfedea0SLionel Sambuc }
2160ebfedea0SLionel Sambuc
2161*0a6a1f1dSLionel Sambuc if (dsa_sign) {
2162ebfedea0SLionel Sambuc mask_a |= SSL_aDSS;
2163ebfedea0SLionel Sambuc emask_a |= SSL_aDSS;
2164ebfedea0SLionel Sambuc }
2165ebfedea0SLionel Sambuc
2166ebfedea0SLionel Sambuc mask_a |= SSL_aNULL;
2167ebfedea0SLionel Sambuc emask_a |= SSL_aNULL;
2168ebfedea0SLionel Sambuc
2169ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_KRB5
2170ebfedea0SLionel Sambuc mask_k |= SSL_kKRB5;
2171ebfedea0SLionel Sambuc mask_a |= SSL_aKRB5;
2172ebfedea0SLionel Sambuc emask_k |= SSL_kKRB5;
2173ebfedea0SLionel Sambuc emask_a |= SSL_aKRB5;
2174ebfedea0SLionel Sambuc #endif
2175ebfedea0SLionel Sambuc
2176*0a6a1f1dSLionel Sambuc /*
2177*0a6a1f1dSLionel Sambuc * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites
2178*0a6a1f1dSLionel Sambuc * depending on the key usage extension.
2179ebfedea0SLionel Sambuc */
2180*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_EC
2181*0a6a1f1dSLionel Sambuc if (have_ecc_cert) {
2182ebfedea0SLionel Sambuc /* This call populates extension flags (ex_flags) */
2183ebfedea0SLionel Sambuc x = (c->pkeys[SSL_PKEY_ECC]).x509;
2184ebfedea0SLionel Sambuc X509_check_purpose(x, -1, 0);
2185ebfedea0SLionel Sambuc ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2186ebfedea0SLionel Sambuc (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1;
2187ebfedea0SLionel Sambuc ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2188ebfedea0SLionel Sambuc (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
2189ebfedea0SLionel Sambuc ecc_pkey = X509_get_pubkey(x);
2190*0a6a1f1dSLionel Sambuc ecc_pkey_size = (ecc_pkey != NULL) ? EVP_PKEY_bits(ecc_pkey) : 0;
2191ebfedea0SLionel Sambuc EVP_PKEY_free(ecc_pkey);
2192*0a6a1f1dSLionel Sambuc if ((x->sig_alg) && (x->sig_alg->algorithm)) {
2193ebfedea0SLionel Sambuc signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2194ebfedea0SLionel Sambuc OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2195ebfedea0SLionel Sambuc }
2196ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
2197*0a6a1f1dSLionel Sambuc if (ecdh_ok) {
2198ebfedea0SLionel Sambuc
2199*0a6a1f1dSLionel Sambuc if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa) {
2200ebfedea0SLionel Sambuc mask_k |= SSL_kECDHr;
2201ebfedea0SLionel Sambuc mask_a |= SSL_aECDH;
2202*0a6a1f1dSLionel Sambuc if (ecc_pkey_size <= 163) {
2203ebfedea0SLionel Sambuc emask_k |= SSL_kECDHr;
2204ebfedea0SLionel Sambuc emask_a |= SSL_aECDH;
2205ebfedea0SLionel Sambuc }
2206ebfedea0SLionel Sambuc }
2207ebfedea0SLionel Sambuc
2208*0a6a1f1dSLionel Sambuc if (pk_nid == NID_X9_62_id_ecPublicKey) {
2209ebfedea0SLionel Sambuc mask_k |= SSL_kECDHe;
2210ebfedea0SLionel Sambuc mask_a |= SSL_aECDH;
2211*0a6a1f1dSLionel Sambuc if (ecc_pkey_size <= 163) {
2212ebfedea0SLionel Sambuc emask_k |= SSL_kECDHe;
2213ebfedea0SLionel Sambuc emask_a |= SSL_aECDH;
2214ebfedea0SLionel Sambuc }
2215ebfedea0SLionel Sambuc }
2216ebfedea0SLionel Sambuc }
2217ebfedea0SLionel Sambuc #endif
2218ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDSA
2219*0a6a1f1dSLionel Sambuc if (ecdsa_ok) {
2220ebfedea0SLionel Sambuc mask_a |= SSL_aECDSA;
2221ebfedea0SLionel Sambuc emask_a |= SSL_aECDSA;
2222ebfedea0SLionel Sambuc }
2223ebfedea0SLionel Sambuc #endif
2224ebfedea0SLionel Sambuc }
2225*0a6a1f1dSLionel Sambuc #endif
2226ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
2227*0a6a1f1dSLionel Sambuc if (have_ecdh_tmp) {
2228ebfedea0SLionel Sambuc mask_k |= SSL_kEECDH;
2229ebfedea0SLionel Sambuc emask_k |= SSL_kEECDH;
2230ebfedea0SLionel Sambuc }
2231ebfedea0SLionel Sambuc #endif
2232ebfedea0SLionel Sambuc
2233ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
2234ebfedea0SLionel Sambuc mask_k |= SSL_kPSK;
2235ebfedea0SLionel Sambuc mask_a |= SSL_aPSK;
2236ebfedea0SLionel Sambuc emask_k |= SSL_kPSK;
2237ebfedea0SLionel Sambuc emask_a |= SSL_aPSK;
2238ebfedea0SLionel Sambuc #endif
2239ebfedea0SLionel Sambuc
2240ebfedea0SLionel Sambuc c->mask_k = mask_k;
2241ebfedea0SLionel Sambuc c->mask_a = mask_a;
2242ebfedea0SLionel Sambuc c->export_mask_k = emask_k;
2243ebfedea0SLionel Sambuc c->export_mask_a = emask_a;
2244ebfedea0SLionel Sambuc c->valid = 1;
2245ebfedea0SLionel Sambuc }
2246ebfedea0SLionel Sambuc
2247ebfedea0SLionel Sambuc /* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2248ebfedea0SLionel Sambuc #define ku_reject(x, usage) \
2249ebfedea0SLionel Sambuc (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2250ebfedea0SLionel Sambuc
2251ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_EC
2252ebfedea0SLionel Sambuc
ssl_check_srvr_ecc_cert_and_alg(X509 * x,SSL * s)2253ebfedea0SLionel Sambuc int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2254ebfedea0SLionel Sambuc {
2255ebfedea0SLionel Sambuc unsigned long alg_k, alg_a;
2256ebfedea0SLionel Sambuc EVP_PKEY *pkey = NULL;
2257ebfedea0SLionel Sambuc int keysize = 0;
2258ebfedea0SLionel Sambuc int signature_nid = 0, md_nid = 0, pk_nid = 0;
2259ebfedea0SLionel Sambuc const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2260ebfedea0SLionel Sambuc
2261ebfedea0SLionel Sambuc alg_k = cs->algorithm_mkey;
2262ebfedea0SLionel Sambuc alg_a = cs->algorithm_auth;
2263ebfedea0SLionel Sambuc
2264*0a6a1f1dSLionel Sambuc if (SSL_C_IS_EXPORT(cs)) {
2265ebfedea0SLionel Sambuc /* ECDH key length in export ciphers must be <= 163 bits */
2266ebfedea0SLionel Sambuc pkey = X509_get_pubkey(x);
2267*0a6a1f1dSLionel Sambuc if (pkey == NULL)
2268*0a6a1f1dSLionel Sambuc return 0;
2269ebfedea0SLionel Sambuc keysize = EVP_PKEY_bits(pkey);
2270ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
2271*0a6a1f1dSLionel Sambuc if (keysize > 163)
2272*0a6a1f1dSLionel Sambuc return 0;
2273ebfedea0SLionel Sambuc }
2274ebfedea0SLionel Sambuc
2275ebfedea0SLionel Sambuc /* This call populates the ex_flags field correctly */
2276ebfedea0SLionel Sambuc X509_check_purpose(x, -1, 0);
2277*0a6a1f1dSLionel Sambuc if ((x->sig_alg) && (x->sig_alg->algorithm)) {
2278ebfedea0SLionel Sambuc signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2279ebfedea0SLionel Sambuc OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2280ebfedea0SLionel Sambuc }
2281*0a6a1f1dSLionel Sambuc if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr) {
2282ebfedea0SLionel Sambuc /* key usage, if present, must allow key agreement */
2283*0a6a1f1dSLionel Sambuc if (ku_reject(x, X509v3_KU_KEY_AGREEMENT)) {
2284*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG,
2285*0a6a1f1dSLionel Sambuc SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT);
2286ebfedea0SLionel Sambuc return 0;
2287ebfedea0SLionel Sambuc }
2288*0a6a1f1dSLionel Sambuc if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < TLS1_2_VERSION) {
2289ebfedea0SLionel Sambuc /* signature alg must be ECDSA */
2290*0a6a1f1dSLionel Sambuc if (pk_nid != NID_X9_62_id_ecPublicKey) {
2291*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG,
2292*0a6a1f1dSLionel Sambuc SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE);
2293ebfedea0SLionel Sambuc return 0;
2294ebfedea0SLionel Sambuc }
2295ebfedea0SLionel Sambuc }
2296*0a6a1f1dSLionel Sambuc if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < TLS1_2_VERSION) {
2297ebfedea0SLionel Sambuc /* signature alg must be RSA */
2298ebfedea0SLionel Sambuc
2299*0a6a1f1dSLionel Sambuc if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa) {
2300*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG,
2301*0a6a1f1dSLionel Sambuc SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE);
2302ebfedea0SLionel Sambuc return 0;
2303ebfedea0SLionel Sambuc }
2304ebfedea0SLionel Sambuc }
2305ebfedea0SLionel Sambuc }
2306*0a6a1f1dSLionel Sambuc if (alg_a & SSL_aECDSA) {
2307ebfedea0SLionel Sambuc /* key usage, if present, must allow signing */
2308*0a6a1f1dSLionel Sambuc if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
2309*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG,
2310*0a6a1f1dSLionel Sambuc SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2311ebfedea0SLionel Sambuc return 0;
2312ebfedea0SLionel Sambuc }
2313ebfedea0SLionel Sambuc }
2314ebfedea0SLionel Sambuc
2315ebfedea0SLionel Sambuc return 1; /* all checks are ok */
2316ebfedea0SLionel Sambuc }
2317ebfedea0SLionel Sambuc
2318ebfedea0SLionel Sambuc #endif
2319ebfedea0SLionel Sambuc
2320ebfedea0SLionel Sambuc /* THIS NEEDS CLEANING UP */
ssl_get_server_send_pkey(const SSL * s)2321ebfedea0SLionel Sambuc CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2322ebfedea0SLionel Sambuc {
2323ebfedea0SLionel Sambuc unsigned long alg_k, alg_a;
2324ebfedea0SLionel Sambuc CERT *c;
2325ebfedea0SLionel Sambuc int i;
2326ebfedea0SLionel Sambuc
2327ebfedea0SLionel Sambuc c = s->cert;
2328ebfedea0SLionel Sambuc ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
2329ebfedea0SLionel Sambuc
2330ebfedea0SLionel Sambuc alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2331ebfedea0SLionel Sambuc alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2332ebfedea0SLionel Sambuc
2333*0a6a1f1dSLionel Sambuc if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
2334*0a6a1f1dSLionel Sambuc /*
2335*0a6a1f1dSLionel Sambuc * we don't need to look at SSL_kEECDH since no certificate is needed
2336*0a6a1f1dSLionel Sambuc * for anon ECDH and for authenticated EECDH, the check for the auth
2337*0a6a1f1dSLionel Sambuc * algorithm will set i correctly NOTE: For ECDH-RSA, we need an ECC
2338*0a6a1f1dSLionel Sambuc * not an RSA cert but for EECDH-RSA we need an RSA cert. Placing the
2339*0a6a1f1dSLionel Sambuc * checks for SSL_kECDH before RSA checks ensures the correct cert is
2340*0a6a1f1dSLionel Sambuc * chosen.
2341ebfedea0SLionel Sambuc */
2342ebfedea0SLionel Sambuc i = SSL_PKEY_ECC;
2343*0a6a1f1dSLionel Sambuc } else if (alg_a & SSL_aECDSA) {
2344ebfedea0SLionel Sambuc i = SSL_PKEY_ECC;
2345*0a6a1f1dSLionel Sambuc } else if (alg_k & SSL_kDHr)
2346ebfedea0SLionel Sambuc i = SSL_PKEY_DH_RSA;
2347ebfedea0SLionel Sambuc else if (alg_k & SSL_kDHd)
2348ebfedea0SLionel Sambuc i = SSL_PKEY_DH_DSA;
2349ebfedea0SLionel Sambuc else if (alg_a & SSL_aDSS)
2350ebfedea0SLionel Sambuc i = SSL_PKEY_DSA_SIGN;
2351*0a6a1f1dSLionel Sambuc else if (alg_a & SSL_aRSA) {
2352ebfedea0SLionel Sambuc if (c->pkeys[SSL_PKEY_RSA_ENC].x509 == NULL)
2353ebfedea0SLionel Sambuc i = SSL_PKEY_RSA_SIGN;
2354ebfedea0SLionel Sambuc else
2355ebfedea0SLionel Sambuc i = SSL_PKEY_RSA_ENC;
2356*0a6a1f1dSLionel Sambuc } else if (alg_a & SSL_aKRB5) {
2357ebfedea0SLionel Sambuc /* VRS something else here? */
2358ebfedea0SLionel Sambuc return (NULL);
2359*0a6a1f1dSLionel Sambuc } else if (alg_a & SSL_aGOST94)
2360ebfedea0SLionel Sambuc i = SSL_PKEY_GOST94;
2361ebfedea0SLionel Sambuc else if (alg_a & SSL_aGOST01)
2362ebfedea0SLionel Sambuc i = SSL_PKEY_GOST01;
2363*0a6a1f1dSLionel Sambuc else { /* if (alg_a & SSL_aNULL) */
2364*0a6a1f1dSLionel Sambuc
2365ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_GET_SERVER_SEND_PKEY, ERR_R_INTERNAL_ERROR);
2366ebfedea0SLionel Sambuc return (NULL);
2367ebfedea0SLionel Sambuc }
2368ebfedea0SLionel Sambuc
2369ebfedea0SLionel Sambuc return c->pkeys + i;
2370ebfedea0SLionel Sambuc }
2371ebfedea0SLionel Sambuc
ssl_get_server_send_cert(const SSL * s)2372ebfedea0SLionel Sambuc X509 *ssl_get_server_send_cert(const SSL *s)
2373ebfedea0SLionel Sambuc {
2374ebfedea0SLionel Sambuc CERT_PKEY *cpk;
2375ebfedea0SLionel Sambuc cpk = ssl_get_server_send_pkey(s);
2376ebfedea0SLionel Sambuc if (!cpk)
2377ebfedea0SLionel Sambuc return NULL;
2378ebfedea0SLionel Sambuc return cpk->x509;
2379ebfedea0SLionel Sambuc }
2380ebfedea0SLionel Sambuc
ssl_get_sign_pkey(SSL * s,const SSL_CIPHER * cipher,const EVP_MD ** pmd)2381*0a6a1f1dSLionel Sambuc EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher,
2382*0a6a1f1dSLionel Sambuc const EVP_MD **pmd)
2383ebfedea0SLionel Sambuc {
2384ebfedea0SLionel Sambuc unsigned long alg_a;
2385ebfedea0SLionel Sambuc CERT *c;
2386ebfedea0SLionel Sambuc int idx = -1;
2387ebfedea0SLionel Sambuc
2388ebfedea0SLionel Sambuc alg_a = cipher->algorithm_auth;
2389ebfedea0SLionel Sambuc c = s->cert;
2390ebfedea0SLionel Sambuc
2391ebfedea0SLionel Sambuc if ((alg_a & SSL_aDSS) &&
2392ebfedea0SLionel Sambuc (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
2393ebfedea0SLionel Sambuc idx = SSL_PKEY_DSA_SIGN;
2394*0a6a1f1dSLionel Sambuc else if (alg_a & SSL_aRSA) {
2395ebfedea0SLionel Sambuc if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2396ebfedea0SLionel Sambuc idx = SSL_PKEY_RSA_SIGN;
2397ebfedea0SLionel Sambuc else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2398ebfedea0SLionel Sambuc idx = SSL_PKEY_RSA_ENC;
2399*0a6a1f1dSLionel Sambuc } else if ((alg_a & SSL_aECDSA) &&
2400ebfedea0SLionel Sambuc (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2401ebfedea0SLionel Sambuc idx = SSL_PKEY_ECC;
2402*0a6a1f1dSLionel Sambuc if (idx == -1) {
2403ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_GET_SIGN_PKEY, ERR_R_INTERNAL_ERROR);
2404ebfedea0SLionel Sambuc return (NULL);
2405ebfedea0SLionel Sambuc }
2406ebfedea0SLionel Sambuc if (pmd)
2407ebfedea0SLionel Sambuc *pmd = c->pkeys[idx].digest;
2408ebfedea0SLionel Sambuc return c->pkeys[idx].privatekey;
2409ebfedea0SLionel Sambuc }
2410ebfedea0SLionel Sambuc
ssl_update_cache(SSL * s,int mode)2411ebfedea0SLionel Sambuc void ssl_update_cache(SSL *s, int mode)
2412ebfedea0SLionel Sambuc {
2413ebfedea0SLionel Sambuc int i;
2414ebfedea0SLionel Sambuc
2415*0a6a1f1dSLionel Sambuc /*
2416*0a6a1f1dSLionel Sambuc * If the session_id_length is 0, we are not supposed to cache it, and it
2417*0a6a1f1dSLionel Sambuc * would be rather hard to do anyway :-)
2418*0a6a1f1dSLionel Sambuc */
2419*0a6a1f1dSLionel Sambuc if (s->session->session_id_length == 0)
2420*0a6a1f1dSLionel Sambuc return;
2421ebfedea0SLionel Sambuc
2422ebfedea0SLionel Sambuc i = s->session_ctx->session_cache_mode;
2423ebfedea0SLionel Sambuc if ((i & mode) && (!s->hit)
2424ebfedea0SLionel Sambuc && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
2425ebfedea0SLionel Sambuc || SSL_CTX_add_session(s->session_ctx, s->session))
2426*0a6a1f1dSLionel Sambuc && (s->session_ctx->new_session_cb != NULL)) {
2427ebfedea0SLionel Sambuc CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION);
2428ebfedea0SLionel Sambuc if (!s->session_ctx->new_session_cb(s, s->session))
2429ebfedea0SLionel Sambuc SSL_SESSION_free(s->session);
2430ebfedea0SLionel Sambuc }
2431ebfedea0SLionel Sambuc
2432ebfedea0SLionel Sambuc /* auto flush every 255 connections */
2433*0a6a1f1dSLionel Sambuc if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
2434ebfedea0SLionel Sambuc if ((((mode & SSL_SESS_CACHE_CLIENT)
2435ebfedea0SLionel Sambuc ? s->session_ctx->stats.sess_connect_good
2436*0a6a1f1dSLionel Sambuc : s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff) {
2437ebfedea0SLionel Sambuc SSL_CTX_flush_sessions(s->session_ctx, (unsigned long)time(NULL));
2438ebfedea0SLionel Sambuc }
2439ebfedea0SLionel Sambuc }
2440ebfedea0SLionel Sambuc }
2441ebfedea0SLionel Sambuc
SSL_get_ssl_method(SSL * s)2442ebfedea0SLionel Sambuc const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2443ebfedea0SLionel Sambuc {
2444ebfedea0SLionel Sambuc return (s->method);
2445ebfedea0SLionel Sambuc }
2446ebfedea0SLionel Sambuc
SSL_set_ssl_method(SSL * s,const SSL_METHOD * meth)2447ebfedea0SLionel Sambuc int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2448ebfedea0SLionel Sambuc {
2449ebfedea0SLionel Sambuc int conn = -1;
2450ebfedea0SLionel Sambuc int ret = 1;
2451ebfedea0SLionel Sambuc
2452*0a6a1f1dSLionel Sambuc if (s->method != meth) {
2453ebfedea0SLionel Sambuc if (s->handshake_func != NULL)
2454ebfedea0SLionel Sambuc conn = (s->handshake_func == s->method->ssl_connect);
2455ebfedea0SLionel Sambuc
2456ebfedea0SLionel Sambuc if (s->method->version == meth->version)
2457ebfedea0SLionel Sambuc s->method = meth;
2458*0a6a1f1dSLionel Sambuc else {
2459ebfedea0SLionel Sambuc s->method->ssl_free(s);
2460ebfedea0SLionel Sambuc s->method = meth;
2461ebfedea0SLionel Sambuc ret = s->method->ssl_new(s);
2462ebfedea0SLionel Sambuc }
2463ebfedea0SLionel Sambuc
2464ebfedea0SLionel Sambuc if (conn == 1)
2465ebfedea0SLionel Sambuc s->handshake_func = meth->ssl_connect;
2466ebfedea0SLionel Sambuc else if (conn == 0)
2467ebfedea0SLionel Sambuc s->handshake_func = meth->ssl_accept;
2468ebfedea0SLionel Sambuc }
2469ebfedea0SLionel Sambuc return (ret);
2470ebfedea0SLionel Sambuc }
2471ebfedea0SLionel Sambuc
SSL_get_error(const SSL * s,int i)2472ebfedea0SLionel Sambuc int SSL_get_error(const SSL *s, int i)
2473ebfedea0SLionel Sambuc {
2474ebfedea0SLionel Sambuc int reason;
2475ebfedea0SLionel Sambuc unsigned long l;
2476ebfedea0SLionel Sambuc BIO *bio;
2477ebfedea0SLionel Sambuc
2478*0a6a1f1dSLionel Sambuc if (i > 0)
2479*0a6a1f1dSLionel Sambuc return (SSL_ERROR_NONE);
2480ebfedea0SLionel Sambuc
2481*0a6a1f1dSLionel Sambuc /*
2482*0a6a1f1dSLionel Sambuc * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
2483*0a6a1f1dSLionel Sambuc * where we do encode the error
2484*0a6a1f1dSLionel Sambuc */
2485*0a6a1f1dSLionel Sambuc if ((l = ERR_peek_error()) != 0) {
2486ebfedea0SLionel Sambuc if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2487ebfedea0SLionel Sambuc return (SSL_ERROR_SYSCALL);
2488ebfedea0SLionel Sambuc else
2489ebfedea0SLionel Sambuc return (SSL_ERROR_SSL);
2490ebfedea0SLionel Sambuc }
2491ebfedea0SLionel Sambuc
2492*0a6a1f1dSLionel Sambuc if ((i < 0) && SSL_want_read(s)) {
2493ebfedea0SLionel Sambuc bio = SSL_get_rbio(s);
2494ebfedea0SLionel Sambuc if (BIO_should_read(bio))
2495ebfedea0SLionel Sambuc return (SSL_ERROR_WANT_READ);
2496ebfedea0SLionel Sambuc else if (BIO_should_write(bio))
2497*0a6a1f1dSLionel Sambuc /*
2498*0a6a1f1dSLionel Sambuc * This one doesn't make too much sense ... We never try to write
2499*0a6a1f1dSLionel Sambuc * to the rbio, and an application program where rbio and wbio
2500*0a6a1f1dSLionel Sambuc * are separate couldn't even know what it should wait for.
2501*0a6a1f1dSLionel Sambuc * However if we ever set s->rwstate incorrectly (so that we have
2502*0a6a1f1dSLionel Sambuc * SSL_want_read(s) instead of SSL_want_write(s)) and rbio and
2503*0a6a1f1dSLionel Sambuc * wbio *are* the same, this test works around that bug; so it
2504*0a6a1f1dSLionel Sambuc * might be safer to keep it.
2505*0a6a1f1dSLionel Sambuc */
2506ebfedea0SLionel Sambuc return (SSL_ERROR_WANT_WRITE);
2507*0a6a1f1dSLionel Sambuc else if (BIO_should_io_special(bio)) {
2508ebfedea0SLionel Sambuc reason = BIO_get_retry_reason(bio);
2509ebfedea0SLionel Sambuc if (reason == BIO_RR_CONNECT)
2510ebfedea0SLionel Sambuc return (SSL_ERROR_WANT_CONNECT);
2511ebfedea0SLionel Sambuc else if (reason == BIO_RR_ACCEPT)
2512ebfedea0SLionel Sambuc return (SSL_ERROR_WANT_ACCEPT);
2513ebfedea0SLionel Sambuc else
2514ebfedea0SLionel Sambuc return (SSL_ERROR_SYSCALL); /* unknown */
2515ebfedea0SLionel Sambuc }
2516ebfedea0SLionel Sambuc }
2517ebfedea0SLionel Sambuc
2518*0a6a1f1dSLionel Sambuc if ((i < 0) && SSL_want_write(s)) {
2519ebfedea0SLionel Sambuc bio = SSL_get_wbio(s);
2520ebfedea0SLionel Sambuc if (BIO_should_write(bio))
2521ebfedea0SLionel Sambuc return (SSL_ERROR_WANT_WRITE);
2522ebfedea0SLionel Sambuc else if (BIO_should_read(bio))
2523*0a6a1f1dSLionel Sambuc /*
2524*0a6a1f1dSLionel Sambuc * See above (SSL_want_read(s) with BIO_should_write(bio))
2525*0a6a1f1dSLionel Sambuc */
2526ebfedea0SLionel Sambuc return (SSL_ERROR_WANT_READ);
2527*0a6a1f1dSLionel Sambuc else if (BIO_should_io_special(bio)) {
2528ebfedea0SLionel Sambuc reason = BIO_get_retry_reason(bio);
2529ebfedea0SLionel Sambuc if (reason == BIO_RR_CONNECT)
2530ebfedea0SLionel Sambuc return (SSL_ERROR_WANT_CONNECT);
2531ebfedea0SLionel Sambuc else if (reason == BIO_RR_ACCEPT)
2532ebfedea0SLionel Sambuc return (SSL_ERROR_WANT_ACCEPT);
2533ebfedea0SLionel Sambuc else
2534ebfedea0SLionel Sambuc return (SSL_ERROR_SYSCALL);
2535ebfedea0SLionel Sambuc }
2536ebfedea0SLionel Sambuc }
2537*0a6a1f1dSLionel Sambuc if ((i < 0) && SSL_want_x509_lookup(s)) {
2538ebfedea0SLionel Sambuc return (SSL_ERROR_WANT_X509_LOOKUP);
2539ebfedea0SLionel Sambuc }
2540ebfedea0SLionel Sambuc
2541*0a6a1f1dSLionel Sambuc if (i == 0) {
2542*0a6a1f1dSLionel Sambuc if (s->version == SSL2_VERSION) {
2543ebfedea0SLionel Sambuc /* assume it is the socket being closed */
2544ebfedea0SLionel Sambuc return (SSL_ERROR_ZERO_RETURN);
2545*0a6a1f1dSLionel Sambuc } else {
2546ebfedea0SLionel Sambuc if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2547ebfedea0SLionel Sambuc (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2548ebfedea0SLionel Sambuc return (SSL_ERROR_ZERO_RETURN);
2549ebfedea0SLionel Sambuc }
2550ebfedea0SLionel Sambuc }
2551ebfedea0SLionel Sambuc return (SSL_ERROR_SYSCALL);
2552ebfedea0SLionel Sambuc }
2553ebfedea0SLionel Sambuc
SSL_do_handshake(SSL * s)2554ebfedea0SLionel Sambuc int SSL_do_handshake(SSL *s)
2555ebfedea0SLionel Sambuc {
2556ebfedea0SLionel Sambuc int ret = 1;
2557ebfedea0SLionel Sambuc
2558*0a6a1f1dSLionel Sambuc if (s->handshake_func == NULL) {
2559ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_DO_HANDSHAKE, SSL_R_CONNECTION_TYPE_NOT_SET);
2560ebfedea0SLionel Sambuc return (-1);
2561ebfedea0SLionel Sambuc }
2562ebfedea0SLionel Sambuc
2563ebfedea0SLionel Sambuc s->method->ssl_renegotiate_check(s);
2564ebfedea0SLionel Sambuc
2565*0a6a1f1dSLionel Sambuc if (SSL_in_init(s) || SSL_in_before(s)) {
2566ebfedea0SLionel Sambuc ret = s->handshake_func(s);
2567ebfedea0SLionel Sambuc }
2568ebfedea0SLionel Sambuc return (ret);
2569ebfedea0SLionel Sambuc }
2570ebfedea0SLionel Sambuc
2571*0a6a1f1dSLionel Sambuc /*
2572*0a6a1f1dSLionel Sambuc * For the next 2 functions, SSL_clear() sets shutdown and so one of these
2573*0a6a1f1dSLionel Sambuc * calls will reset it
2574*0a6a1f1dSLionel Sambuc */
SSL_set_accept_state(SSL * s)2575ebfedea0SLionel Sambuc void SSL_set_accept_state(SSL *s)
2576ebfedea0SLionel Sambuc {
2577ebfedea0SLionel Sambuc s->server = 1;
2578ebfedea0SLionel Sambuc s->shutdown = 0;
2579ebfedea0SLionel Sambuc s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
2580ebfedea0SLionel Sambuc s->handshake_func = s->method->ssl_accept;
2581ebfedea0SLionel Sambuc /* clear the current cipher */
2582ebfedea0SLionel Sambuc ssl_clear_cipher_ctx(s);
2583ebfedea0SLionel Sambuc ssl_clear_hash_ctx(&s->read_hash);
2584ebfedea0SLionel Sambuc ssl_clear_hash_ctx(&s->write_hash);
2585ebfedea0SLionel Sambuc }
2586ebfedea0SLionel Sambuc
SSL_set_connect_state(SSL * s)2587ebfedea0SLionel Sambuc void SSL_set_connect_state(SSL *s)
2588ebfedea0SLionel Sambuc {
2589ebfedea0SLionel Sambuc s->server = 0;
2590ebfedea0SLionel Sambuc s->shutdown = 0;
2591ebfedea0SLionel Sambuc s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
2592ebfedea0SLionel Sambuc s->handshake_func = s->method->ssl_connect;
2593ebfedea0SLionel Sambuc /* clear the current cipher */
2594ebfedea0SLionel Sambuc ssl_clear_cipher_ctx(s);
2595ebfedea0SLionel Sambuc ssl_clear_hash_ctx(&s->read_hash);
2596ebfedea0SLionel Sambuc ssl_clear_hash_ctx(&s->write_hash);
2597ebfedea0SLionel Sambuc }
2598ebfedea0SLionel Sambuc
ssl_undefined_function(SSL * s)2599ebfedea0SLionel Sambuc int ssl_undefined_function(SSL *s)
2600ebfedea0SLionel Sambuc {
2601ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2602ebfedea0SLionel Sambuc return (0);
2603ebfedea0SLionel Sambuc }
2604ebfedea0SLionel Sambuc
ssl_undefined_void_function(void)2605ebfedea0SLionel Sambuc int ssl_undefined_void_function(void)
2606ebfedea0SLionel Sambuc {
2607*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,
2608*0a6a1f1dSLionel Sambuc ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2609ebfedea0SLionel Sambuc return (0);
2610ebfedea0SLionel Sambuc }
2611ebfedea0SLionel Sambuc
ssl_undefined_const_function(const SSL * s)2612ebfedea0SLionel Sambuc int ssl_undefined_const_function(const SSL *s)
2613ebfedea0SLionel Sambuc {
2614*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_UNDEFINED_CONST_FUNCTION,
2615*0a6a1f1dSLionel Sambuc ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2616ebfedea0SLionel Sambuc return (0);
2617ebfedea0SLionel Sambuc }
2618ebfedea0SLionel Sambuc
ssl_bad_method(int ver)2619ebfedea0SLionel Sambuc SSL_METHOD *ssl_bad_method(int ver)
2620ebfedea0SLionel Sambuc {
2621ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_BAD_METHOD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2622ebfedea0SLionel Sambuc return (NULL);
2623ebfedea0SLionel Sambuc }
2624ebfedea0SLionel Sambuc
SSL_get_version(const SSL * s)2625ebfedea0SLionel Sambuc const char *SSL_get_version(const SSL *s)
2626ebfedea0SLionel Sambuc {
2627ebfedea0SLionel Sambuc if (s->version == TLS1_2_VERSION)
2628ebfedea0SLionel Sambuc return ("TLSv1.2");
2629ebfedea0SLionel Sambuc else if (s->version == TLS1_1_VERSION)
2630ebfedea0SLionel Sambuc return ("TLSv1.1");
2631ebfedea0SLionel Sambuc else if (s->version == TLS1_VERSION)
2632ebfedea0SLionel Sambuc return ("TLSv1");
2633ebfedea0SLionel Sambuc else if (s->version == SSL3_VERSION)
2634ebfedea0SLionel Sambuc return ("SSLv3");
2635ebfedea0SLionel Sambuc else if (s->version == SSL2_VERSION)
2636ebfedea0SLionel Sambuc return ("SSLv2");
2637ebfedea0SLionel Sambuc else
2638ebfedea0SLionel Sambuc return ("unknown");
2639ebfedea0SLionel Sambuc }
2640ebfedea0SLionel Sambuc
SSL_dup(SSL * s)2641ebfedea0SLionel Sambuc SSL *SSL_dup(SSL *s)
2642ebfedea0SLionel Sambuc {
2643ebfedea0SLionel Sambuc STACK_OF(X509_NAME) *sk;
2644ebfedea0SLionel Sambuc X509_NAME *xn;
2645ebfedea0SLionel Sambuc SSL *ret;
2646ebfedea0SLionel Sambuc int i;
2647ebfedea0SLionel Sambuc
2648ebfedea0SLionel Sambuc if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
2649ebfedea0SLionel Sambuc return (NULL);
2650ebfedea0SLionel Sambuc
2651ebfedea0SLionel Sambuc ret->version = s->version;
2652ebfedea0SLionel Sambuc ret->type = s->type;
2653ebfedea0SLionel Sambuc ret->method = s->method;
2654ebfedea0SLionel Sambuc
2655*0a6a1f1dSLionel Sambuc if (s->session != NULL) {
2656ebfedea0SLionel Sambuc /* This copies session-id, SSL_METHOD, sid_ctx, and 'cert' */
2657ebfedea0SLionel Sambuc SSL_copy_session_id(ret, s);
2658*0a6a1f1dSLionel Sambuc } else {
2659*0a6a1f1dSLionel Sambuc /*
2660*0a6a1f1dSLionel Sambuc * No session has been established yet, so we have to expect that
2661*0a6a1f1dSLionel Sambuc * s->cert or ret->cert will be changed later -- they should not both
2662*0a6a1f1dSLionel Sambuc * point to the same object, and thus we can't use
2663*0a6a1f1dSLionel Sambuc * SSL_copy_session_id.
2664*0a6a1f1dSLionel Sambuc */
2665ebfedea0SLionel Sambuc
2666ebfedea0SLionel Sambuc ret->method->ssl_free(ret);
2667ebfedea0SLionel Sambuc ret->method = s->method;
2668ebfedea0SLionel Sambuc ret->method->ssl_new(ret);
2669ebfedea0SLionel Sambuc
2670*0a6a1f1dSLionel Sambuc if (s->cert != NULL) {
2671*0a6a1f1dSLionel Sambuc if (ret->cert != NULL) {
2672ebfedea0SLionel Sambuc ssl_cert_free(ret->cert);
2673ebfedea0SLionel Sambuc }
2674ebfedea0SLionel Sambuc ret->cert = ssl_cert_dup(s->cert);
2675ebfedea0SLionel Sambuc if (ret->cert == NULL)
2676ebfedea0SLionel Sambuc goto err;
2677ebfedea0SLionel Sambuc }
2678ebfedea0SLionel Sambuc
2679*0a6a1f1dSLionel Sambuc SSL_set_session_id_context(ret, s->sid_ctx, s->sid_ctx_length);
2680ebfedea0SLionel Sambuc }
2681ebfedea0SLionel Sambuc
2682ebfedea0SLionel Sambuc ret->options = s->options;
2683ebfedea0SLionel Sambuc ret->mode = s->mode;
2684ebfedea0SLionel Sambuc SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
2685ebfedea0SLionel Sambuc SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
2686ebfedea0SLionel Sambuc ret->msg_callback = s->msg_callback;
2687ebfedea0SLionel Sambuc ret->msg_callback_arg = s->msg_callback_arg;
2688*0a6a1f1dSLionel Sambuc SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s));
2689ebfedea0SLionel Sambuc SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
2690ebfedea0SLionel Sambuc ret->generate_session_id = s->generate_session_id;
2691ebfedea0SLionel Sambuc
2692ebfedea0SLionel Sambuc SSL_set_info_callback(ret, SSL_get_info_callback(s));
2693ebfedea0SLionel Sambuc
2694ebfedea0SLionel Sambuc ret->debug = s->debug;
2695ebfedea0SLionel Sambuc
2696ebfedea0SLionel Sambuc /* copy app data, a little dangerous perhaps */
2697ebfedea0SLionel Sambuc if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
2698ebfedea0SLionel Sambuc goto err;
2699ebfedea0SLionel Sambuc
2700ebfedea0SLionel Sambuc /* setup rbio, and wbio */
2701*0a6a1f1dSLionel Sambuc if (s->rbio != NULL) {
2702ebfedea0SLionel Sambuc if (!BIO_dup_state(s->rbio, (char *)&ret->rbio))
2703ebfedea0SLionel Sambuc goto err;
2704ebfedea0SLionel Sambuc }
2705*0a6a1f1dSLionel Sambuc if (s->wbio != NULL) {
2706*0a6a1f1dSLionel Sambuc if (s->wbio != s->rbio) {
2707ebfedea0SLionel Sambuc if (!BIO_dup_state(s->wbio, (char *)&ret->wbio))
2708ebfedea0SLionel Sambuc goto err;
2709*0a6a1f1dSLionel Sambuc } else
2710ebfedea0SLionel Sambuc ret->wbio = ret->rbio;
2711ebfedea0SLionel Sambuc }
2712ebfedea0SLionel Sambuc ret->rwstate = s->rwstate;
2713ebfedea0SLionel Sambuc ret->in_handshake = s->in_handshake;
2714ebfedea0SLionel Sambuc ret->handshake_func = s->handshake_func;
2715ebfedea0SLionel Sambuc ret->server = s->server;
2716ebfedea0SLionel Sambuc ret->renegotiate = s->renegotiate;
2717ebfedea0SLionel Sambuc ret->new_session = s->new_session;
2718ebfedea0SLionel Sambuc ret->quiet_shutdown = s->quiet_shutdown;
2719ebfedea0SLionel Sambuc ret->shutdown = s->shutdown;
2720*0a6a1f1dSLionel Sambuc ret->state = s->state; /* SSL_dup does not really work at any state,
2721*0a6a1f1dSLionel Sambuc * though */
2722ebfedea0SLionel Sambuc ret->rstate = s->rstate;
2723*0a6a1f1dSLionel Sambuc ret->init_num = 0; /* would have to copy ret->init_buf,
2724*0a6a1f1dSLionel Sambuc * ret->init_msg, ret->init_num,
2725*0a6a1f1dSLionel Sambuc * ret->init_off */
2726ebfedea0SLionel Sambuc ret->hit = s->hit;
2727ebfedea0SLionel Sambuc
2728ebfedea0SLionel Sambuc X509_VERIFY_PARAM_inherit(ret->param, s->param);
2729ebfedea0SLionel Sambuc
2730ebfedea0SLionel Sambuc /* dup the cipher_list and cipher_list_by_id stacks */
2731*0a6a1f1dSLionel Sambuc if (s->cipher_list != NULL) {
2732ebfedea0SLionel Sambuc if ((ret->cipher_list = sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
2733ebfedea0SLionel Sambuc goto err;
2734ebfedea0SLionel Sambuc }
2735ebfedea0SLionel Sambuc if (s->cipher_list_by_id != NULL)
2736ebfedea0SLionel Sambuc if ((ret->cipher_list_by_id = sk_SSL_CIPHER_dup(s->cipher_list_by_id))
2737ebfedea0SLionel Sambuc == NULL)
2738ebfedea0SLionel Sambuc goto err;
2739ebfedea0SLionel Sambuc
2740ebfedea0SLionel Sambuc /* Dup the client_CA list */
2741*0a6a1f1dSLionel Sambuc if (s->client_CA != NULL) {
2742*0a6a1f1dSLionel Sambuc if ((sk = sk_X509_NAME_dup(s->client_CA)) == NULL)
2743*0a6a1f1dSLionel Sambuc goto err;
2744ebfedea0SLionel Sambuc ret->client_CA = sk;
2745*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2746ebfedea0SLionel Sambuc xn = sk_X509_NAME_value(sk, i);
2747*0a6a1f1dSLionel Sambuc if (sk_X509_NAME_set(sk, i, X509_NAME_dup(xn)) == NULL) {
2748ebfedea0SLionel Sambuc X509_NAME_free(xn);
2749ebfedea0SLionel Sambuc goto err;
2750ebfedea0SLionel Sambuc }
2751ebfedea0SLionel Sambuc }
2752ebfedea0SLionel Sambuc }
2753ebfedea0SLionel Sambuc
2754*0a6a1f1dSLionel Sambuc if (0) {
2755ebfedea0SLionel Sambuc err:
2756*0a6a1f1dSLionel Sambuc if (ret != NULL)
2757*0a6a1f1dSLionel Sambuc SSL_free(ret);
2758ebfedea0SLionel Sambuc ret = NULL;
2759ebfedea0SLionel Sambuc }
2760ebfedea0SLionel Sambuc return (ret);
2761ebfedea0SLionel Sambuc }
2762ebfedea0SLionel Sambuc
ssl_clear_cipher_ctx(SSL * s)2763ebfedea0SLionel Sambuc void ssl_clear_cipher_ctx(SSL *s)
2764ebfedea0SLionel Sambuc {
2765*0a6a1f1dSLionel Sambuc if (s->enc_read_ctx != NULL) {
2766ebfedea0SLionel Sambuc EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2767ebfedea0SLionel Sambuc OPENSSL_free(s->enc_read_ctx);
2768ebfedea0SLionel Sambuc s->enc_read_ctx = NULL;
2769ebfedea0SLionel Sambuc }
2770*0a6a1f1dSLionel Sambuc if (s->enc_write_ctx != NULL) {
2771ebfedea0SLionel Sambuc EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2772ebfedea0SLionel Sambuc OPENSSL_free(s->enc_write_ctx);
2773ebfedea0SLionel Sambuc s->enc_write_ctx = NULL;
2774ebfedea0SLionel Sambuc }
2775ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
2776*0a6a1f1dSLionel Sambuc if (s->expand != NULL) {
2777ebfedea0SLionel Sambuc COMP_CTX_free(s->expand);
2778ebfedea0SLionel Sambuc s->expand = NULL;
2779ebfedea0SLionel Sambuc }
2780*0a6a1f1dSLionel Sambuc if (s->compress != NULL) {
2781ebfedea0SLionel Sambuc COMP_CTX_free(s->compress);
2782ebfedea0SLionel Sambuc s->compress = NULL;
2783ebfedea0SLionel Sambuc }
2784ebfedea0SLionel Sambuc #endif
2785ebfedea0SLionel Sambuc }
2786ebfedea0SLionel Sambuc
2787ebfedea0SLionel Sambuc /* Fix this function so that it takes an optional type parameter */
SSL_get_certificate(const SSL * s)2788ebfedea0SLionel Sambuc X509 *SSL_get_certificate(const SSL *s)
2789ebfedea0SLionel Sambuc {
2790*0a6a1f1dSLionel Sambuc if (s->cert != NULL)
2791ebfedea0SLionel Sambuc return (s->cert->key->x509);
2792ebfedea0SLionel Sambuc else
2793ebfedea0SLionel Sambuc return (NULL);
2794ebfedea0SLionel Sambuc }
2795ebfedea0SLionel Sambuc
2796ebfedea0SLionel Sambuc /* Fix this function so that it takes an optional type parameter */
SSL_get_privatekey(SSL * s)2797ebfedea0SLionel Sambuc EVP_PKEY *SSL_get_privatekey(SSL *s)
2798ebfedea0SLionel Sambuc {
2799ebfedea0SLionel Sambuc if (s->cert != NULL)
2800ebfedea0SLionel Sambuc return (s->cert->key->privatekey);
2801ebfedea0SLionel Sambuc else
2802ebfedea0SLionel Sambuc return (NULL);
2803ebfedea0SLionel Sambuc }
2804ebfedea0SLionel Sambuc
SSL_get_current_cipher(const SSL * s)2805ebfedea0SLionel Sambuc const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2806ebfedea0SLionel Sambuc {
2807ebfedea0SLionel Sambuc if ((s->session != NULL) && (s->session->cipher != NULL))
2808ebfedea0SLionel Sambuc return (s->session->cipher);
2809ebfedea0SLionel Sambuc return (NULL);
2810ebfedea0SLionel Sambuc }
2811*0a6a1f1dSLionel Sambuc
2812ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_COMP
SSL_get_current_compression(SSL * s)2813ebfedea0SLionel Sambuc const void *SSL_get_current_compression(SSL *s)
2814ebfedea0SLionel Sambuc {
2815ebfedea0SLionel Sambuc return NULL;
2816ebfedea0SLionel Sambuc }
2817*0a6a1f1dSLionel Sambuc
SSL_get_current_expansion(SSL * s)2818ebfedea0SLionel Sambuc const void *SSL_get_current_expansion(SSL *s)
2819ebfedea0SLionel Sambuc {
2820ebfedea0SLionel Sambuc return NULL;
2821ebfedea0SLionel Sambuc }
2822ebfedea0SLionel Sambuc #else
2823ebfedea0SLionel Sambuc
SSL_get_current_compression(SSL * s)2824ebfedea0SLionel Sambuc const COMP_METHOD *SSL_get_current_compression(SSL *s)
2825ebfedea0SLionel Sambuc {
2826ebfedea0SLionel Sambuc if (s->compress != NULL)
2827ebfedea0SLionel Sambuc return (s->compress->meth);
2828ebfedea0SLionel Sambuc return (NULL);
2829ebfedea0SLionel Sambuc }
2830ebfedea0SLionel Sambuc
SSL_get_current_expansion(SSL * s)2831ebfedea0SLionel Sambuc const COMP_METHOD *SSL_get_current_expansion(SSL *s)
2832ebfedea0SLionel Sambuc {
2833ebfedea0SLionel Sambuc if (s->expand != NULL)
2834ebfedea0SLionel Sambuc return (s->expand->meth);
2835ebfedea0SLionel Sambuc return (NULL);
2836ebfedea0SLionel Sambuc }
2837ebfedea0SLionel Sambuc #endif
2838ebfedea0SLionel Sambuc
ssl_init_wbio_buffer(SSL * s,int push)2839ebfedea0SLionel Sambuc int ssl_init_wbio_buffer(SSL *s, int push)
2840ebfedea0SLionel Sambuc {
2841ebfedea0SLionel Sambuc BIO *bbio;
2842ebfedea0SLionel Sambuc
2843*0a6a1f1dSLionel Sambuc if (s->bbio == NULL) {
2844ebfedea0SLionel Sambuc bbio = BIO_new(BIO_f_buffer());
2845*0a6a1f1dSLionel Sambuc if (bbio == NULL)
2846*0a6a1f1dSLionel Sambuc return (0);
2847ebfedea0SLionel Sambuc s->bbio = bbio;
2848*0a6a1f1dSLionel Sambuc } else {
2849ebfedea0SLionel Sambuc bbio = s->bbio;
2850ebfedea0SLionel Sambuc if (s->bbio == s->wbio)
2851ebfedea0SLionel Sambuc s->wbio = BIO_pop(s->wbio);
2852ebfedea0SLionel Sambuc }
2853ebfedea0SLionel Sambuc (void)BIO_reset(bbio);
2854ebfedea0SLionel Sambuc /* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2855*0a6a1f1dSLionel Sambuc if (!BIO_set_read_buffer_size(bbio, 1)) {
2856ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER, ERR_R_BUF_LIB);
2857ebfedea0SLionel Sambuc return (0);
2858ebfedea0SLionel Sambuc }
2859*0a6a1f1dSLionel Sambuc if (push) {
2860ebfedea0SLionel Sambuc if (s->wbio != bbio)
2861ebfedea0SLionel Sambuc s->wbio = BIO_push(bbio, s->wbio);
2862*0a6a1f1dSLionel Sambuc } else {
2863ebfedea0SLionel Sambuc if (s->wbio == bbio)
2864ebfedea0SLionel Sambuc s->wbio = BIO_pop(bbio);
2865ebfedea0SLionel Sambuc }
2866ebfedea0SLionel Sambuc return (1);
2867ebfedea0SLionel Sambuc }
2868ebfedea0SLionel Sambuc
ssl_free_wbio_buffer(SSL * s)2869ebfedea0SLionel Sambuc void ssl_free_wbio_buffer(SSL *s)
2870ebfedea0SLionel Sambuc {
2871*0a6a1f1dSLionel Sambuc if (s->bbio == NULL)
2872*0a6a1f1dSLionel Sambuc return;
2873ebfedea0SLionel Sambuc
2874*0a6a1f1dSLionel Sambuc if (s->bbio == s->wbio) {
2875ebfedea0SLionel Sambuc /* remove buffering */
2876ebfedea0SLionel Sambuc s->wbio = BIO_pop(s->wbio);
2877*0a6a1f1dSLionel Sambuc #ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids
2878*0a6a1f1dSLionel Sambuc * adding one more preprocessor symbol */
2879ebfedea0SLionel Sambuc assert(s->wbio != NULL);
2880ebfedea0SLionel Sambuc #endif
2881ebfedea0SLionel Sambuc }
2882ebfedea0SLionel Sambuc BIO_free(s->bbio);
2883ebfedea0SLionel Sambuc s->bbio = NULL;
2884ebfedea0SLionel Sambuc }
2885ebfedea0SLionel Sambuc
SSL_CTX_set_quiet_shutdown(SSL_CTX * ctx,int mode)2886ebfedea0SLionel Sambuc void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
2887ebfedea0SLionel Sambuc {
2888ebfedea0SLionel Sambuc ctx->quiet_shutdown = mode;
2889ebfedea0SLionel Sambuc }
2890ebfedea0SLionel Sambuc
SSL_CTX_get_quiet_shutdown(const SSL_CTX * ctx)2891ebfedea0SLionel Sambuc int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2892ebfedea0SLionel Sambuc {
2893ebfedea0SLionel Sambuc return (ctx->quiet_shutdown);
2894ebfedea0SLionel Sambuc }
2895ebfedea0SLionel Sambuc
SSL_set_quiet_shutdown(SSL * s,int mode)2896ebfedea0SLionel Sambuc void SSL_set_quiet_shutdown(SSL *s, int mode)
2897ebfedea0SLionel Sambuc {
2898ebfedea0SLionel Sambuc s->quiet_shutdown = mode;
2899ebfedea0SLionel Sambuc }
2900ebfedea0SLionel Sambuc
SSL_get_quiet_shutdown(const SSL * s)2901ebfedea0SLionel Sambuc int SSL_get_quiet_shutdown(const SSL *s)
2902ebfedea0SLionel Sambuc {
2903ebfedea0SLionel Sambuc return (s->quiet_shutdown);
2904ebfedea0SLionel Sambuc }
2905ebfedea0SLionel Sambuc
SSL_set_shutdown(SSL * s,int mode)2906ebfedea0SLionel Sambuc void SSL_set_shutdown(SSL *s, int mode)
2907ebfedea0SLionel Sambuc {
2908ebfedea0SLionel Sambuc s->shutdown = mode;
2909ebfedea0SLionel Sambuc }
2910ebfedea0SLionel Sambuc
SSL_get_shutdown(const SSL * s)2911ebfedea0SLionel Sambuc int SSL_get_shutdown(const SSL *s)
2912ebfedea0SLionel Sambuc {
2913ebfedea0SLionel Sambuc return (s->shutdown);
2914ebfedea0SLionel Sambuc }
2915ebfedea0SLionel Sambuc
SSL_version(const SSL * s)2916ebfedea0SLionel Sambuc int SSL_version(const SSL *s)
2917ebfedea0SLionel Sambuc {
2918ebfedea0SLionel Sambuc return (s->version);
2919ebfedea0SLionel Sambuc }
2920ebfedea0SLionel Sambuc
SSL_get_SSL_CTX(const SSL * ssl)2921ebfedea0SLionel Sambuc SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2922ebfedea0SLionel Sambuc {
2923ebfedea0SLionel Sambuc return (ssl->ctx);
2924ebfedea0SLionel Sambuc }
2925ebfedea0SLionel Sambuc
SSL_set_SSL_CTX(SSL * ssl,SSL_CTX * ctx)2926ebfedea0SLionel Sambuc SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)
2927ebfedea0SLionel Sambuc {
2928*0a6a1f1dSLionel Sambuc CERT *ocert = ssl->cert;
2929ebfedea0SLionel Sambuc if (ssl->ctx == ctx)
2930ebfedea0SLionel Sambuc return ssl->ctx;
2931ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
2932ebfedea0SLionel Sambuc if (ctx == NULL)
2933ebfedea0SLionel Sambuc ctx = ssl->initial_ctx;
2934ebfedea0SLionel Sambuc #endif
2935ebfedea0SLionel Sambuc ssl->cert = ssl_cert_dup(ctx->cert);
2936*0a6a1f1dSLionel Sambuc if (ocert != NULL) {
2937*0a6a1f1dSLionel Sambuc int i;
2938*0a6a1f1dSLionel Sambuc /* Copy negotiated digests from original */
2939*0a6a1f1dSLionel Sambuc for (i = 0; i < SSL_PKEY_NUM; i++) {
2940*0a6a1f1dSLionel Sambuc CERT_PKEY *cpk = ocert->pkeys + i;
2941*0a6a1f1dSLionel Sambuc CERT_PKEY *rpk = ssl->cert->pkeys + i;
2942*0a6a1f1dSLionel Sambuc rpk->digest = cpk->digest;
2943*0a6a1f1dSLionel Sambuc }
2944*0a6a1f1dSLionel Sambuc ssl_cert_free(ocert);
2945*0a6a1f1dSLionel Sambuc }
2946*0a6a1f1dSLionel Sambuc
2947*0a6a1f1dSLionel Sambuc /*
2948*0a6a1f1dSLionel Sambuc * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
2949*0a6a1f1dSLionel Sambuc * so setter APIs must prevent invalid lengths from entering the system.
2950*0a6a1f1dSLionel Sambuc */
2951*0a6a1f1dSLionel Sambuc OPENSSL_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2952*0a6a1f1dSLionel Sambuc
2953*0a6a1f1dSLionel Sambuc /*
2954*0a6a1f1dSLionel Sambuc * If the session ID context matches that of the parent SSL_CTX,
2955*0a6a1f1dSLionel Sambuc * inherit it from the new SSL_CTX as well. If however the context does
2956*0a6a1f1dSLionel Sambuc * not match (i.e., it was set per-ssl with SSL_set_session_id_context),
2957*0a6a1f1dSLionel Sambuc * leave it unchanged.
2958*0a6a1f1dSLionel Sambuc */
2959*0a6a1f1dSLionel Sambuc if ((ssl->ctx != NULL) &&
2960*0a6a1f1dSLionel Sambuc (ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) &&
2961*0a6a1f1dSLionel Sambuc (memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0)) {
2962*0a6a1f1dSLionel Sambuc ssl->sid_ctx_length = ctx->sid_ctx_length;
2963*0a6a1f1dSLionel Sambuc memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));
2964*0a6a1f1dSLionel Sambuc }
2965*0a6a1f1dSLionel Sambuc
2966ebfedea0SLionel Sambuc CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2967ebfedea0SLionel Sambuc if (ssl->ctx != NULL)
2968ebfedea0SLionel Sambuc SSL_CTX_free(ssl->ctx); /* decrement reference count */
2969ebfedea0SLionel Sambuc ssl->ctx = ctx;
2970*0a6a1f1dSLionel Sambuc
2971ebfedea0SLionel Sambuc return (ssl->ctx);
2972ebfedea0SLionel Sambuc }
2973ebfedea0SLionel Sambuc
2974ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_STDIO
SSL_CTX_set_default_verify_paths(SSL_CTX * ctx)2975ebfedea0SLionel Sambuc int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2976ebfedea0SLionel Sambuc {
2977ebfedea0SLionel Sambuc return (X509_STORE_set_default_paths(ctx->cert_store));
2978ebfedea0SLionel Sambuc }
2979ebfedea0SLionel Sambuc
SSL_CTX_load_verify_locations(SSL_CTX * ctx,const char * CAfile,const char * CApath)2980ebfedea0SLionel Sambuc int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2981ebfedea0SLionel Sambuc const char *CApath)
2982ebfedea0SLionel Sambuc {
2983ebfedea0SLionel Sambuc return (X509_STORE_load_locations(ctx->cert_store, CAfile, CApath));
2984ebfedea0SLionel Sambuc }
2985ebfedea0SLionel Sambuc #endif
2986ebfedea0SLionel Sambuc
SSL_set_info_callback(SSL * ssl,void (* cb)(const SSL * ssl,int type,int val))2987ebfedea0SLionel Sambuc void SSL_set_info_callback(SSL *ssl,
2988ebfedea0SLionel Sambuc void (*cb) (const SSL *ssl, int type, int val))
2989ebfedea0SLionel Sambuc {
2990ebfedea0SLionel Sambuc ssl->info_callback = cb;
2991ebfedea0SLionel Sambuc }
2992ebfedea0SLionel Sambuc
2993*0a6a1f1dSLionel Sambuc /*
2994*0a6a1f1dSLionel Sambuc * One compiler (Diab DCC) doesn't like argument names in returned function
2995*0a6a1f1dSLionel Sambuc * pointer.
2996*0a6a1f1dSLionel Sambuc */
SSL_get_info_callback(const SSL * ssl)2997*0a6a1f1dSLionel Sambuc void (*SSL_get_info_callback(const SSL *ssl)) (const SSL * /* ssl */ ,
2998*0a6a1f1dSLionel Sambuc int /* type */ ,
2999*0a6a1f1dSLionel Sambuc int /* val */ ) {
3000ebfedea0SLionel Sambuc return ssl->info_callback;
3001ebfedea0SLionel Sambuc }
3002ebfedea0SLionel Sambuc
SSL_state(const SSL * ssl)3003ebfedea0SLionel Sambuc int SSL_state(const SSL *ssl)
3004ebfedea0SLionel Sambuc {
3005ebfedea0SLionel Sambuc return (ssl->state);
3006ebfedea0SLionel Sambuc }
3007ebfedea0SLionel Sambuc
SSL_set_state(SSL * ssl,int state)3008ebfedea0SLionel Sambuc void SSL_set_state(SSL *ssl, int state)
3009ebfedea0SLionel Sambuc {
3010ebfedea0SLionel Sambuc ssl->state = state;
3011ebfedea0SLionel Sambuc }
3012ebfedea0SLionel Sambuc
SSL_set_verify_result(SSL * ssl,long arg)3013ebfedea0SLionel Sambuc void SSL_set_verify_result(SSL *ssl, long arg)
3014ebfedea0SLionel Sambuc {
3015ebfedea0SLionel Sambuc ssl->verify_result = arg;
3016ebfedea0SLionel Sambuc }
3017ebfedea0SLionel Sambuc
SSL_get_verify_result(const SSL * ssl)3018ebfedea0SLionel Sambuc long SSL_get_verify_result(const SSL *ssl)
3019ebfedea0SLionel Sambuc {
3020ebfedea0SLionel Sambuc return (ssl->verify_result);
3021ebfedea0SLionel Sambuc }
3022ebfedea0SLionel Sambuc
SSL_get_ex_new_index(long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)3023ebfedea0SLionel Sambuc int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
3024ebfedea0SLionel Sambuc CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
3025ebfedea0SLionel Sambuc {
3026ebfedea0SLionel Sambuc return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
3027ebfedea0SLionel Sambuc new_func, dup_func, free_func);
3028ebfedea0SLionel Sambuc }
3029ebfedea0SLionel Sambuc
SSL_set_ex_data(SSL * s,int idx,void * arg)3030ebfedea0SLionel Sambuc int SSL_set_ex_data(SSL *s, int idx, void *arg)
3031ebfedea0SLionel Sambuc {
3032ebfedea0SLionel Sambuc return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
3033ebfedea0SLionel Sambuc }
3034ebfedea0SLionel Sambuc
SSL_get_ex_data(const SSL * s,int idx)3035ebfedea0SLionel Sambuc void *SSL_get_ex_data(const SSL *s, int idx)
3036ebfedea0SLionel Sambuc {
3037ebfedea0SLionel Sambuc return (CRYPTO_get_ex_data(&s->ex_data, idx));
3038ebfedea0SLionel Sambuc }
3039ebfedea0SLionel Sambuc
SSL_CTX_get_ex_new_index(long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)3040ebfedea0SLionel Sambuc int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
3041*0a6a1f1dSLionel Sambuc CRYPTO_EX_dup *dup_func,
3042*0a6a1f1dSLionel Sambuc CRYPTO_EX_free *free_func)
3043ebfedea0SLionel Sambuc {
3044ebfedea0SLionel Sambuc return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
3045ebfedea0SLionel Sambuc new_func, dup_func, free_func);
3046ebfedea0SLionel Sambuc }
3047ebfedea0SLionel Sambuc
SSL_CTX_set_ex_data(SSL_CTX * s,int idx,void * arg)3048ebfedea0SLionel Sambuc int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
3049ebfedea0SLionel Sambuc {
3050ebfedea0SLionel Sambuc return (CRYPTO_set_ex_data(&s->ex_data, idx, arg));
3051ebfedea0SLionel Sambuc }
3052ebfedea0SLionel Sambuc
SSL_CTX_get_ex_data(const SSL_CTX * s,int idx)3053ebfedea0SLionel Sambuc void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
3054ebfedea0SLionel Sambuc {
3055ebfedea0SLionel Sambuc return (CRYPTO_get_ex_data(&s->ex_data, idx));
3056ebfedea0SLionel Sambuc }
3057ebfedea0SLionel Sambuc
ssl_ok(SSL * s)3058ebfedea0SLionel Sambuc int ssl_ok(SSL *s)
3059ebfedea0SLionel Sambuc {
3060ebfedea0SLionel Sambuc return (1);
3061ebfedea0SLionel Sambuc }
3062ebfedea0SLionel Sambuc
SSL_CTX_get_cert_store(const SSL_CTX * ctx)3063ebfedea0SLionel Sambuc X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
3064ebfedea0SLionel Sambuc {
3065ebfedea0SLionel Sambuc return (ctx->cert_store);
3066ebfedea0SLionel Sambuc }
3067ebfedea0SLionel Sambuc
SSL_CTX_set_cert_store(SSL_CTX * ctx,X509_STORE * store)3068ebfedea0SLionel Sambuc void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
3069ebfedea0SLionel Sambuc {
3070ebfedea0SLionel Sambuc if (ctx->cert_store != NULL)
3071ebfedea0SLionel Sambuc X509_STORE_free(ctx->cert_store);
3072ebfedea0SLionel Sambuc ctx->cert_store = store;
3073ebfedea0SLionel Sambuc }
3074ebfedea0SLionel Sambuc
SSL_want(const SSL * s)3075ebfedea0SLionel Sambuc int SSL_want(const SSL *s)
3076ebfedea0SLionel Sambuc {
3077ebfedea0SLionel Sambuc return (s->rwstate);
3078ebfedea0SLionel Sambuc }
3079ebfedea0SLionel Sambuc
3080*0a6a1f1dSLionel Sambuc /**
3081ebfedea0SLionel Sambuc * \brief Set the callback for generating temporary RSA keys.
3082ebfedea0SLionel Sambuc * \param ctx the SSL context.
3083ebfedea0SLionel Sambuc * \param cb the callback
3084ebfedea0SLionel Sambuc */
3085ebfedea0SLionel Sambuc
3086ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
SSL_CTX_set_tmp_rsa_callback(SSL_CTX * ctx,RSA * (* cb)(SSL * ssl,int is_export,int keylength))3087ebfedea0SLionel Sambuc void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, RSA *(*cb) (SSL *ssl,
3088ebfedea0SLionel Sambuc int is_export,
3089ebfedea0SLionel Sambuc int keylength))
3090ebfedea0SLionel Sambuc {
3091ebfedea0SLionel Sambuc SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
3092ebfedea0SLionel Sambuc }
3093ebfedea0SLionel Sambuc
SSL_set_tmp_rsa_callback(SSL * ssl,RSA * (* cb)(SSL * ssl,int is_export,int keylength))3094ebfedea0SLionel Sambuc void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb) (SSL *ssl,
3095ebfedea0SLionel Sambuc int is_export,
3096ebfedea0SLionel Sambuc int keylength))
3097ebfedea0SLionel Sambuc {
3098ebfedea0SLionel Sambuc SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
3099ebfedea0SLionel Sambuc }
3100ebfedea0SLionel Sambuc #endif
3101ebfedea0SLionel Sambuc
3102ebfedea0SLionel Sambuc #ifdef DOXYGEN
3103*0a6a1f1dSLionel Sambuc /**
3104ebfedea0SLionel Sambuc * \brief The RSA temporary key callback function.
3105ebfedea0SLionel Sambuc * \param ssl the SSL session.
3106ebfedea0SLionel Sambuc * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
3107ebfedea0SLionel Sambuc * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
3108ebfedea0SLionel Sambuc * of the required key in bits.
3109ebfedea0SLionel Sambuc * \return the temporary RSA key.
3110ebfedea0SLionel Sambuc * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
3111ebfedea0SLionel Sambuc */
3112ebfedea0SLionel Sambuc
cb(SSL * ssl,int is_export,int keylength)3113ebfedea0SLionel Sambuc RSA *cb(SSL *ssl, int is_export, int keylength)
3114*0a6a1f1dSLionel Sambuc {
3115*0a6a1f1dSLionel Sambuc }
3116ebfedea0SLionel Sambuc #endif
3117ebfedea0SLionel Sambuc
3118*0a6a1f1dSLionel Sambuc /**
3119ebfedea0SLionel Sambuc * \brief Set the callback for generating temporary DH keys.
3120ebfedea0SLionel Sambuc * \param ctx the SSL context.
3121ebfedea0SLionel Sambuc * \param dh the callback
3122ebfedea0SLionel Sambuc */
3123ebfedea0SLionel Sambuc
3124ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
SSL_CTX_set_tmp_dh_callback(SSL_CTX * ctx,DH * (* dh)(SSL * ssl,int is_export,int keylength))3125*0a6a1f1dSLionel Sambuc void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
3126*0a6a1f1dSLionel Sambuc DH *(*dh) (SSL *ssl, int is_export,
3127ebfedea0SLionel Sambuc int keylength))
3128ebfedea0SLionel Sambuc {
3129ebfedea0SLionel Sambuc SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
3130ebfedea0SLionel Sambuc }
3131ebfedea0SLionel Sambuc
SSL_set_tmp_dh_callback(SSL * ssl,DH * (* dh)(SSL * ssl,int is_export,int keylength))3132ebfedea0SLionel Sambuc void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh) (SSL *ssl, int is_export,
3133ebfedea0SLionel Sambuc int keylength))
3134ebfedea0SLionel Sambuc {
3135ebfedea0SLionel Sambuc SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
3136ebfedea0SLionel Sambuc }
3137ebfedea0SLionel Sambuc #endif
3138ebfedea0SLionel Sambuc
3139ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
SSL_CTX_set_tmp_ecdh_callback(SSL_CTX * ctx,EC_KEY * (* ecdh)(SSL * ssl,int is_export,int keylength))3140*0a6a1f1dSLionel Sambuc void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
3141*0a6a1f1dSLionel Sambuc EC_KEY *(*ecdh) (SSL *ssl, int is_export,
3142ebfedea0SLionel Sambuc int keylength))
3143ebfedea0SLionel Sambuc {
3144*0a6a1f1dSLionel Sambuc SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB,
3145*0a6a1f1dSLionel Sambuc (void (*)(void))ecdh);
3146ebfedea0SLionel Sambuc }
3147ebfedea0SLionel Sambuc
SSL_set_tmp_ecdh_callback(SSL * ssl,EC_KEY * (* ecdh)(SSL * ssl,int is_export,int keylength))3148*0a6a1f1dSLionel Sambuc void SSL_set_tmp_ecdh_callback(SSL *ssl,
3149*0a6a1f1dSLionel Sambuc EC_KEY *(*ecdh) (SSL *ssl, int is_export,
3150ebfedea0SLionel Sambuc int keylength))
3151ebfedea0SLionel Sambuc {
3152ebfedea0SLionel Sambuc SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))ecdh);
3153ebfedea0SLionel Sambuc }
3154ebfedea0SLionel Sambuc #endif
3155ebfedea0SLionel Sambuc
3156ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
SSL_CTX_use_psk_identity_hint(SSL_CTX * ctx,const char * identity_hint)3157ebfedea0SLionel Sambuc int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
3158ebfedea0SLionel Sambuc {
3159*0a6a1f1dSLionel Sambuc if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
3160*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT,
3161*0a6a1f1dSLionel Sambuc SSL_R_DATA_LENGTH_TOO_LONG);
3162ebfedea0SLionel Sambuc return 0;
3163ebfedea0SLionel Sambuc }
3164ebfedea0SLionel Sambuc if (ctx->psk_identity_hint != NULL)
3165ebfedea0SLionel Sambuc OPENSSL_free(ctx->psk_identity_hint);
3166*0a6a1f1dSLionel Sambuc if (identity_hint != NULL) {
3167ebfedea0SLionel Sambuc ctx->psk_identity_hint = BUF_strdup(identity_hint);
3168ebfedea0SLionel Sambuc if (ctx->psk_identity_hint == NULL)
3169ebfedea0SLionel Sambuc return 0;
3170*0a6a1f1dSLionel Sambuc } else
3171ebfedea0SLionel Sambuc ctx->psk_identity_hint = NULL;
3172ebfedea0SLionel Sambuc return 1;
3173ebfedea0SLionel Sambuc }
3174ebfedea0SLionel Sambuc
SSL_use_psk_identity_hint(SSL * s,const char * identity_hint)3175ebfedea0SLionel Sambuc int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
3176ebfedea0SLionel Sambuc {
3177ebfedea0SLionel Sambuc if (s == NULL)
3178ebfedea0SLionel Sambuc return 0;
3179ebfedea0SLionel Sambuc
3180ebfedea0SLionel Sambuc if (s->session == NULL)
3181ebfedea0SLionel Sambuc return 1; /* session not created yet, ignored */
3182ebfedea0SLionel Sambuc
3183*0a6a1f1dSLionel Sambuc if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
3184ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
3185ebfedea0SLionel Sambuc return 0;
3186ebfedea0SLionel Sambuc }
3187ebfedea0SLionel Sambuc if (s->session->psk_identity_hint != NULL)
3188ebfedea0SLionel Sambuc OPENSSL_free(s->session->psk_identity_hint);
3189*0a6a1f1dSLionel Sambuc if (identity_hint != NULL) {
3190ebfedea0SLionel Sambuc s->session->psk_identity_hint = BUF_strdup(identity_hint);
3191ebfedea0SLionel Sambuc if (s->session->psk_identity_hint == NULL)
3192ebfedea0SLionel Sambuc return 0;
3193*0a6a1f1dSLionel Sambuc } else
3194ebfedea0SLionel Sambuc s->session->psk_identity_hint = NULL;
3195ebfedea0SLionel Sambuc return 1;
3196ebfedea0SLionel Sambuc }
3197ebfedea0SLionel Sambuc
SSL_get_psk_identity_hint(const SSL * s)3198ebfedea0SLionel Sambuc const char *SSL_get_psk_identity_hint(const SSL *s)
3199ebfedea0SLionel Sambuc {
3200ebfedea0SLionel Sambuc if (s == NULL || s->session == NULL)
3201ebfedea0SLionel Sambuc return NULL;
3202ebfedea0SLionel Sambuc return (s->session->psk_identity_hint);
3203ebfedea0SLionel Sambuc }
3204ebfedea0SLionel Sambuc
SSL_get_psk_identity(const SSL * s)3205ebfedea0SLionel Sambuc const char *SSL_get_psk_identity(const SSL *s)
3206ebfedea0SLionel Sambuc {
3207ebfedea0SLionel Sambuc if (s == NULL || s->session == NULL)
3208ebfedea0SLionel Sambuc return NULL;
3209ebfedea0SLionel Sambuc return (s->session->psk_identity);
3210ebfedea0SLionel Sambuc }
3211ebfedea0SLionel Sambuc
SSL_set_psk_client_callback(SSL * s,unsigned int (* cb)(SSL * ssl,const char * hint,char * identity,unsigned int max_identity_len,unsigned char * psk,unsigned int max_psk_len))3212ebfedea0SLionel Sambuc void SSL_set_psk_client_callback(SSL *s,
3213*0a6a1f1dSLionel Sambuc unsigned int (*cb) (SSL *ssl,
3214*0a6a1f1dSLionel Sambuc const char *hint,
3215*0a6a1f1dSLionel Sambuc char *identity,
3216*0a6a1f1dSLionel Sambuc unsigned int
3217*0a6a1f1dSLionel Sambuc max_identity_len,
3218*0a6a1f1dSLionel Sambuc unsigned char *psk,
3219*0a6a1f1dSLionel Sambuc unsigned int
3220*0a6a1f1dSLionel Sambuc max_psk_len))
3221ebfedea0SLionel Sambuc {
3222ebfedea0SLionel Sambuc s->psk_client_callback = cb;
3223ebfedea0SLionel Sambuc }
3224ebfedea0SLionel Sambuc
SSL_CTX_set_psk_client_callback(SSL_CTX * ctx,unsigned int (* cb)(SSL * ssl,const char * hint,char * identity,unsigned int max_identity_len,unsigned char * psk,unsigned int max_psk_len))3225ebfedea0SLionel Sambuc void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
3226*0a6a1f1dSLionel Sambuc unsigned int (*cb) (SSL *ssl,
3227*0a6a1f1dSLionel Sambuc const char *hint,
3228*0a6a1f1dSLionel Sambuc char *identity,
3229*0a6a1f1dSLionel Sambuc unsigned int
3230*0a6a1f1dSLionel Sambuc max_identity_len,
3231*0a6a1f1dSLionel Sambuc unsigned char *psk,
3232*0a6a1f1dSLionel Sambuc unsigned int
3233*0a6a1f1dSLionel Sambuc max_psk_len))
3234ebfedea0SLionel Sambuc {
3235ebfedea0SLionel Sambuc ctx->psk_client_callback = cb;
3236ebfedea0SLionel Sambuc }
3237ebfedea0SLionel Sambuc
SSL_set_psk_server_callback(SSL * s,unsigned int (* cb)(SSL * ssl,const char * identity,unsigned char * psk,unsigned int max_psk_len))3238ebfedea0SLionel Sambuc void SSL_set_psk_server_callback(SSL *s,
3239*0a6a1f1dSLionel Sambuc unsigned int (*cb) (SSL *ssl,
3240*0a6a1f1dSLionel Sambuc const char *identity,
3241*0a6a1f1dSLionel Sambuc unsigned char *psk,
3242*0a6a1f1dSLionel Sambuc unsigned int
3243*0a6a1f1dSLionel Sambuc max_psk_len))
3244ebfedea0SLionel Sambuc {
3245ebfedea0SLionel Sambuc s->psk_server_callback = cb;
3246ebfedea0SLionel Sambuc }
3247ebfedea0SLionel Sambuc
SSL_CTX_set_psk_server_callback(SSL_CTX * ctx,unsigned int (* cb)(SSL * ssl,const char * identity,unsigned char * psk,unsigned int max_psk_len))3248ebfedea0SLionel Sambuc void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
3249*0a6a1f1dSLionel Sambuc unsigned int (*cb) (SSL *ssl,
3250*0a6a1f1dSLionel Sambuc const char *identity,
3251*0a6a1f1dSLionel Sambuc unsigned char *psk,
3252*0a6a1f1dSLionel Sambuc unsigned int
3253*0a6a1f1dSLionel Sambuc max_psk_len))
3254ebfedea0SLionel Sambuc {
3255ebfedea0SLionel Sambuc ctx->psk_server_callback = cb;
3256ebfedea0SLionel Sambuc }
3257ebfedea0SLionel Sambuc #endif
3258ebfedea0SLionel Sambuc
SSL_CTX_set_msg_callback(SSL_CTX * ctx,void (* cb)(int write_p,int version,int content_type,const void * buf,size_t len,SSL * ssl,void * arg))3259*0a6a1f1dSLionel Sambuc void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
3260*0a6a1f1dSLionel Sambuc void (*cb) (int write_p, int version,
3261*0a6a1f1dSLionel Sambuc int content_type, const void *buf,
3262*0a6a1f1dSLionel Sambuc size_t len, SSL *ssl, void *arg))
3263ebfedea0SLionel Sambuc {
3264ebfedea0SLionel Sambuc SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3265ebfedea0SLionel Sambuc }
3266*0a6a1f1dSLionel Sambuc
SSL_set_msg_callback(SSL * ssl,void (* cb)(int write_p,int version,int content_type,const void * buf,size_t len,SSL * ssl,void * arg))3267*0a6a1f1dSLionel Sambuc void SSL_set_msg_callback(SSL *ssl,
3268*0a6a1f1dSLionel Sambuc void (*cb) (int write_p, int version,
3269*0a6a1f1dSLionel Sambuc int content_type, const void *buf,
3270*0a6a1f1dSLionel Sambuc size_t len, SSL *ssl, void *arg))
3271ebfedea0SLionel Sambuc {
3272ebfedea0SLionel Sambuc SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3273ebfedea0SLionel Sambuc }
3274ebfedea0SLionel Sambuc
3275*0a6a1f1dSLionel Sambuc /*
3276*0a6a1f1dSLionel Sambuc * Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3277*0a6a1f1dSLionel Sambuc * vairable, freeing EVP_MD_CTX previously stored in that variable, if any.
3278*0a6a1f1dSLionel Sambuc * If EVP_MD pointer is passed, initializes ctx with this md Returns newly
3279*0a6a1f1dSLionel Sambuc * allocated ctx;
3280ebfedea0SLionel Sambuc */
3281ebfedea0SLionel Sambuc
ssl_replace_hash(EVP_MD_CTX ** hash,const EVP_MD * md)3282ebfedea0SLionel Sambuc EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md)
3283ebfedea0SLionel Sambuc {
3284ebfedea0SLionel Sambuc ssl_clear_hash_ctx(hash);
3285ebfedea0SLionel Sambuc *hash = EVP_MD_CTX_create();
3286*0a6a1f1dSLionel Sambuc if (md)
3287*0a6a1f1dSLionel Sambuc EVP_DigestInit_ex(*hash, md, NULL);
3288ebfedea0SLionel Sambuc return *hash;
3289ebfedea0SLionel Sambuc }
3290*0a6a1f1dSLionel Sambuc
ssl_clear_hash_ctx(EVP_MD_CTX ** hash)3291ebfedea0SLionel Sambuc void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3292ebfedea0SLionel Sambuc {
3293ebfedea0SLionel Sambuc
3294*0a6a1f1dSLionel Sambuc if (*hash)
3295*0a6a1f1dSLionel Sambuc EVP_MD_CTX_destroy(*hash);
3296ebfedea0SLionel Sambuc *hash = NULL;
3297ebfedea0SLionel Sambuc }
3298ebfedea0SLionel Sambuc
SSL_set_debug(SSL * s,int debug)3299ebfedea0SLionel Sambuc void SSL_set_debug(SSL *s, int debug)
3300ebfedea0SLionel Sambuc {
3301ebfedea0SLionel Sambuc s->debug = debug;
3302ebfedea0SLionel Sambuc }
3303ebfedea0SLionel Sambuc
SSL_cache_hit(SSL * s)3304ebfedea0SLionel Sambuc int SSL_cache_hit(SSL *s)
3305ebfedea0SLionel Sambuc {
3306ebfedea0SLionel Sambuc return s->hit;
3307ebfedea0SLionel Sambuc }
3308ebfedea0SLionel Sambuc
3309ebfedea0SLionel Sambuc #if defined(_WINDLL) && defined(OPENSSL_SYS_WIN16)
3310ebfedea0SLionel Sambuc # include "../crypto/bio/bss_file.c"
3311ebfedea0SLionel Sambuc #endif
3312ebfedea0SLionel Sambuc
3313ebfedea0SLionel Sambuc IMPLEMENT_STACK_OF(SSL_CIPHER)
3314ebfedea0SLionel Sambuc IMPLEMENT_STACK_OF(SSL_COMP)
3315*0a6a1f1dSLionel Sambuc IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id);
3316