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