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