blob: 2b4f07c86b1be29f258d7824993ea0d3b0adec5e [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;
Adam Langley04124042014-06-20 12:00:00 -07001521
1522 if (sk_SSL_CIPHER_num(sk) == 0)
1523 return NULL;
1524
Adam Langley95c29f32014-06-20 12:00:00 -07001525 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1526 {
1527 int n;
1528
1529 c=sk_SSL_CIPHER_value(sk,i);
1530 n=strlen(c->name);
1531 if (n+1 > len)
1532 {
1533 if (p != buf)
1534 --p;
1535 *p='\0';
1536 return buf;
1537 }
1538 strcpy(p,c->name);
1539 p+=n;
1540 *(p++)=':';
1541 len-=n+1;
1542 }
1543 p[-1]='\0';
1544 return(buf);
1545 }
1546
1547int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p,
1548 int (*put_cb)(const SSL_CIPHER *, unsigned char *))
1549 {
1550 int i,j=0;
1551 SSL_CIPHER *c;
1552 CERT *ct = s->cert;
1553 unsigned char *q;
1554 int no_scsv = s->renegotiate;
1555 /* Set disabled masks for this session */
1556 ssl_set_client_disabled(s);
1557
1558 if (sk == NULL) return(0);
1559 q=p;
1560
1561 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1562 {
1563 c=sk_SSL_CIPHER_value(sk,i);
1564 /* Skip disabled ciphers */
1565 if (c->algorithm_ssl & ct->mask_ssl ||
1566 c->algorithm_mkey & ct->mask_k ||
1567 c->algorithm_auth & ct->mask_a)
1568 continue;
1569#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
1570 if (c->id == SSL3_CK_SCSV)
1571 {
1572 if (no_scsv)
1573 continue;
1574 else
1575 no_scsv = 1;
1576 }
1577#endif
1578 j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p);
1579 p+=j;
1580 }
1581 /* If p == q, no ciphers and caller indicates an error. Otherwise
1582 * add SCSV if not renegotiating.
1583 */
1584 if (p != q && !no_scsv)
1585 {
1586 static SSL_CIPHER scsv =
1587 {
1588 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1589 };
1590 j = put_cb ? put_cb(&scsv,p) : ssl_put_cipher_by_char(s,&scsv,p);
1591 p+=j;
1592#ifdef OPENSSL_RI_DEBUG
1593 fprintf(stderr, "SCSV sent by client\n");
1594#endif
1595 }
1596
1597 return(p-q);
1598 }
1599
1600STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,
1601 STACK_OF(SSL_CIPHER) **skp)
1602 {
1603 const SSL_CIPHER *c;
1604 STACK_OF(SSL_CIPHER) *sk;
1605 int i,n;
1606 if (s->s3)
1607 s->s3->send_connection_binding = 0;
1608
1609 n=ssl_put_cipher_by_char(s,NULL,NULL);
1610 if ((num%n) != 0)
1611 {
1612 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1613 return(NULL);
1614 }
1615 if ((skp == NULL) || (*skp == NULL))
1616 sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
1617 else
1618 {
1619 sk= *skp;
1620 sk_SSL_CIPHER_zero(sk);
1621 }
1622
1623 if (s->cert->ciphers_raw)
1624 OPENSSL_free(s->cert->ciphers_raw);
1625 s->cert->ciphers_raw = BUF_memdup(p, num);
1626 if (s->cert->ciphers_raw == NULL)
1627 {
1628 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1629 goto err;
1630 }
1631 s->cert->ciphers_rawlen = (size_t)num;
1632
1633 for (i=0; i<num; i+=n)
1634 {
1635 /* Check for SCSV */
1636 if (s->s3 && (n != 3 || !p[0]) &&
1637 (p[n-2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
1638 (p[n-1] == (SSL3_CK_SCSV & 0xff)))
1639 {
1640 /* SCSV fatal if renegotiating */
1641 if (s->renegotiate)
1642 {
1643 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1644 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1645 goto err;
1646 }
1647 s->s3->send_connection_binding = 1;
1648 p += n;
1649#ifdef OPENSSL_RI_DEBUG
1650 fprintf(stderr, "SCSV received by server\n");
1651#endif
1652 continue;
1653 }
1654
Adam Langley29707792014-06-20 12:00:00 -07001655 /* Check for FALLBACK_SCSV */
1656 if (s->s3 && n == 2 &&
1657 (p[0] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) &&
1658 (p[1] == (SSL3_CK_FALLBACK_SCSV & 0xff)) &&
1659 s->version < ssl_get_max_version(s))
1660 {
1661 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1662 ssl3_send_alert(s,SSL3_AL_FATAL,SSL3_AD_INAPPROPRIATE_FALLBACK);
1663 goto err;
1664 }
1665
Adam Langley95c29f32014-06-20 12:00:00 -07001666 c=ssl_get_cipher_by_char(s,p);
1667 p+=n;
1668 if (c != NULL)
1669 {
1670 if (!sk_SSL_CIPHER_push(sk,c))
1671 {
1672 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1673 goto err;
1674 }
1675 }
1676 }
1677
1678 if (skp != NULL)
1679 *skp=sk;
1680 return(sk);
1681err:
1682 if ((skp == NULL) || (*skp == NULL))
1683 sk_SSL_CIPHER_free(sk);
1684 return(NULL);
1685 }
1686
1687
1688#ifndef OPENSSL_NO_TLSEXT
1689/** return a servername extension value if provided in Client Hello, or NULL.
1690 * So far, only host_name types are defined (RFC 3546).
1691 */
1692
1693const char *SSL_get_servername(const SSL *s, const int type)
1694 {
1695 if (type != TLSEXT_NAMETYPE_host_name)
1696 return NULL;
1697
1698 return s->session && !s->tlsext_hostname ?
1699 s->session->tlsext_hostname :
1700 s->tlsext_hostname;
1701 }
1702
1703int SSL_get_servername_type(const SSL *s)
1704 {
1705 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1706 return TLSEXT_NAMETYPE_host_name;
1707 return -1;
1708 }
1709
1710/* SSL_select_next_proto implements the standard protocol selection. It is
1711 * expected that this function is called from the callback set by
1712 * SSL_CTX_set_next_proto_select_cb.
1713 *
1714 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1715 * strings. The length byte itself is not included in the length. A byte
1716 * string of length 0 is invalid. No byte string may be truncated.
1717 *
1718 * The current, but experimental algorithm for selecting the protocol is:
1719 *
1720 * 1) If the server doesn't support NPN then this is indicated to the
1721 * callback. In this case, the client application has to abort the connection
1722 * or have a default application level protocol.
1723 *
1724 * 2) If the server supports NPN, but advertises an empty list then the
1725 * client selects the first protcol in its list, but indicates via the
1726 * API that this fallback case was enacted.
1727 *
1728 * 3) Otherwise, the client finds the first protocol in the server's list
1729 * that it supports and selects this protocol. This is because it's
1730 * assumed that the server has better information about which protocol
1731 * a client should use.
1732 *
1733 * 4) If the client doesn't support any of the server's advertised
1734 * protocols, then this is treated the same as case 2.
1735 *
1736 * It returns either
1737 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1738 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1739 */
1740int 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)
1741 {
1742 unsigned int i, j;
1743 const unsigned char *result;
1744 int status = OPENSSL_NPN_UNSUPPORTED;
1745
1746 /* For each protocol in server preference order, see if we support it. */
1747 for (i = 0; i < server_len; )
1748 {
1749 for (j = 0; j < client_len; )
1750 {
1751 if (server[i] == client[j] &&
1752 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1753 {
1754 /* We found a match */
1755 result = &server[i];
1756 status = OPENSSL_NPN_NEGOTIATED;
1757 goto found;
1758 }
1759 j += client[j];
1760 j++;
1761 }
1762 i += server[i];
1763 i++;
1764 }
1765
1766 /* There's no overlap between our protocols and the server's list. */
1767 result = client;
1768 status = OPENSSL_NPN_NO_OVERLAP;
1769
1770 found:
1771 *out = (unsigned char *) result + 1;
1772 *outlen = result[0];
1773 return status;
1774 }
1775
1776# ifndef OPENSSL_NO_NEXTPROTONEG
1777/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1778 * requested protocol for this connection and returns 0. If the client didn't
1779 * request any protocol, then *data is set to NULL.
1780 *
1781 * Note that the client can request any protocol it chooses. The value returned
1782 * from this function need not be a member of the list of supported protocols
1783 * provided by the callback.
1784 */
1785void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data, unsigned *len)
1786 {
1787 *data = s->next_proto_negotiated;
1788 if (!*data) {
1789 *len = 0;
1790 } else {
1791 *len = s->next_proto_negotiated_len;
1792 }
1793}
1794
1795/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1796 * TLS server needs a list of supported protocols for Next Protocol
1797 * Negotiation. The returned list must be in wire format. The list is returned
1798 * by setting |out| to point to it and |outlen| to its length. This memory will
1799 * not be modified, but one should assume that the SSL* keeps a reference to
1800 * it.
1801 *
1802 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1803 * such extension will be included in the ServerHello. */
1804void 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)
1805 {
1806 ctx->next_protos_advertised_cb = cb;
1807 ctx->next_protos_advertised_cb_arg = arg;
1808 }
1809
1810/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1811 * client needs to select a protocol from the server's provided list. |out|
1812 * must be set to point to the selected protocol (which may be within |in|).
1813 * The length of the protocol name must be written into |outlen|. The server's
1814 * advertised protocols are provided in |in| and |inlen|. The callback can
1815 * assume that |in| is syntactically valid.
1816 *
1817 * The client must select a protocol. It is fatal to the connection if this
1818 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1819 */
1820void 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)
1821 {
1822 ctx->next_proto_select_cb = cb;
1823 ctx->next_proto_select_cb_arg = arg;
1824 }
1825# endif
1826
1827int SSL_CTX_set_custom_cli_ext(SSL_CTX *ctx, unsigned short ext_type,
1828 custom_cli_ext_first_cb_fn fn1,
1829 custom_cli_ext_second_cb_fn fn2, void* arg)
1830 {
1831 size_t i;
1832 custom_cli_ext_record* record;
1833
1834 /* Check for duplicates */
1835 for (i=0; i < ctx->custom_cli_ext_records_count; i++)
1836 if (ext_type == ctx->custom_cli_ext_records[i].ext_type)
1837 return 0;
1838
1839 ctx->custom_cli_ext_records = OPENSSL_realloc(ctx->custom_cli_ext_records,
1840 (ctx->custom_cli_ext_records_count + 1) *
1841 sizeof(custom_cli_ext_record));
1842 if (!ctx->custom_cli_ext_records) {
1843 ctx->custom_cli_ext_records_count = 0;
1844 return 0;
1845 }
1846 ctx->custom_cli_ext_records_count++;
1847 record = &ctx->custom_cli_ext_records[ctx->custom_cli_ext_records_count - 1];
1848 record->ext_type = ext_type;
1849 record->fn1 = fn1;
1850 record->fn2 = fn2;
1851 record->arg = arg;
1852 return 1;
1853 }
1854
1855int SSL_CTX_set_custom_srv_ext(SSL_CTX *ctx, unsigned short ext_type,
1856 custom_srv_ext_first_cb_fn fn1,
1857 custom_srv_ext_second_cb_fn fn2, void* arg)
1858 {
1859 size_t i;
1860 custom_srv_ext_record* record;
1861
1862 /* Check for duplicates */
1863 for (i=0; i < ctx->custom_srv_ext_records_count; i++)
1864 if (ext_type == ctx->custom_srv_ext_records[i].ext_type)
1865 return 0;
1866
1867 ctx->custom_srv_ext_records = OPENSSL_realloc(ctx->custom_srv_ext_records,
1868 (ctx->custom_srv_ext_records_count + 1) *
1869 sizeof(custom_srv_ext_record));
1870 if (!ctx->custom_srv_ext_records) {
1871 ctx->custom_srv_ext_records_count = 0;
1872 return 0;
1873 }
1874 ctx->custom_srv_ext_records_count++;
1875 record = &ctx->custom_srv_ext_records[ctx->custom_srv_ext_records_count - 1];
1876 record->ext_type = ext_type;
1877 record->fn1 = fn1;
1878 record->fn2 = fn2;
1879 record->arg = arg;
1880 return 1;
1881 }
1882
1883/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1884 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1885 * length-prefixed strings).
1886 *
1887 * Returns 0 on success. */
1888int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1889 unsigned protos_len)
1890 {
1891 if (ctx->alpn_client_proto_list)
1892 OPENSSL_free(ctx->alpn_client_proto_list);
1893
1894 ctx->alpn_client_proto_list = OPENSSL_malloc(protos_len);
1895 if (!ctx->alpn_client_proto_list)
1896 return 1;
1897 memcpy(ctx->alpn_client_proto_list, protos, protos_len);
1898 ctx->alpn_client_proto_list_len = protos_len;
1899
1900 return 0;
1901 }
1902
1903/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1904 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1905 * length-prefixed strings).
1906 *
1907 * Returns 0 on success. */
1908int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1909 unsigned protos_len)
1910 {
1911 if (ssl->alpn_client_proto_list)
1912 OPENSSL_free(ssl->alpn_client_proto_list);
1913
1914 ssl->alpn_client_proto_list = OPENSSL_malloc(protos_len);
1915 if (!ssl->alpn_client_proto_list)
1916 return 1;
1917 memcpy(ssl->alpn_client_proto_list, protos, protos_len);
1918 ssl->alpn_client_proto_list_len = protos_len;
1919
1920 return 0;
1921 }
1922
1923/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1924 * during ClientHello processing in order to select an ALPN protocol from the
1925 * client's list of offered protocols. */
1926void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1927 int (*cb) (SSL *ssl,
1928 const unsigned char **out,
1929 unsigned char *outlen,
1930 const unsigned char *in,
1931 unsigned int inlen,
1932 void *arg),
1933 void *arg)
1934 {
1935 ctx->alpn_select_cb = cb;
1936 ctx->alpn_select_cb_arg = arg;
1937 }
1938
1939/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1940 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1941 * including the leading length-prefix byte). If the server didn't respond with
1942 * a negotiated protocol then |*len| will be zero. */
1943void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1944 unsigned *len)
1945 {
1946 *data = NULL;
1947 if (ssl->s3)
1948 *data = ssl->s3->alpn_selected;
1949 if (*data == NULL)
1950 *len = 0;
1951 else
1952 *len = ssl->s3->alpn_selected_len;
1953 }
1954#endif /* !OPENSSL_NO_TLSEXT */
1955
1956int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1957 const char *label, size_t llen, const unsigned char *p, size_t plen,
1958 int use_context)
1959 {
1960 if (s->version < TLS1_VERSION)
1961 return -1;
1962
1963 return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1964 llen, p, plen,
1965 use_context);
1966 }
1967
1968static uint32_t ssl_session_hash(const SSL_SESSION *a)
1969 {
1970 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1971 ((uint32_t) a->session_id[1] << 8) ||
1972 ((uint32_t) a->session_id[2] << 16) ||
1973 ((uint32_t) a->session_id[3] << 24);
1974
1975 return hash;
1976 }
1977
1978/* NB: If this function (or indeed the hash function which uses a sort of
1979 * coarser function than this one) is changed, ensure
1980 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1981 * able to construct an SSL_SESSION that will collide with any existing session
1982 * with a matching session ID. */
1983static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1984 {
1985 if (a->ssl_version != b->ssl_version)
1986 return(1);
1987 if (a->session_id_length != b->session_id_length)
1988 return(1);
1989 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1990 }
1991
1992SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1993 {
1994 SSL_CTX *ret=NULL;
1995
1996 if (meth == NULL)
1997 {
1998 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1999 return(NULL);
2000 }
2001
2002#ifdef OPENSSL_FIPS
2003 if (FIPS_mode() && (meth->version < TLS1_VERSION))
2004 {
2005 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
2006 return NULL;
2007 }
2008#endif
2009
2010 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
2011 {
2012 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
2013 goto err;
2014 }
2015 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
2016 if (ret == NULL)
2017 goto err;
2018
2019 memset(ret,0,sizeof(SSL_CTX));
2020
2021 ret->method=meth;
2022
2023 ret->cert_store=NULL;
2024 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
2025 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
2026 ret->session_cache_head=NULL;
2027 ret->session_cache_tail=NULL;
2028
2029 /* We take the system default */
2030 ret->session_timeout=meth->get_timeout();
2031
2032 ret->new_session_cb=0;
2033 ret->remove_session_cb=0;
2034 ret->get_session_cb=0;
2035 ret->generate_session_id=0;
2036
2037 memset((char *)&ret->stats,0,sizeof(ret->stats));
2038
2039 ret->references=1;
2040 ret->quiet_shutdown=0;
2041
2042/* ret->cipher=NULL;*/
2043/* ret->s2->challenge=NULL;
2044 ret->master_key=NULL;
2045 ret->key_arg=NULL;
2046 ret->s2->conn_id=NULL; */
2047
2048 ret->info_callback=NULL;
2049
2050 ret->app_verify_callback=0;
2051 ret->app_verify_arg=NULL;
2052
2053 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
2054 ret->read_ahead=0;
2055 ret->msg_callback=0;
2056 ret->msg_callback_arg=NULL;
2057 ret->verify_mode=SSL_VERIFY_NONE;
2058#if 0
2059 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
2060#endif
2061 ret->sid_ctx_length=0;
2062 ret->default_verify_callback=NULL;
2063 if ((ret->cert=ssl_cert_new()) == NULL)
2064 goto err;
2065
2066 ret->default_passwd_callback=0;
2067 ret->default_passwd_callback_userdata=NULL;
2068 ret->client_cert_cb=0;
2069 ret->app_gen_cookie_cb=0;
2070 ret->app_verify_cookie_cb=0;
2071
2072 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
2073 if (ret->sessions == NULL) goto err;
2074 ret->cert_store=X509_STORE_new();
2075 if (ret->cert_store == NULL) goto err;
2076
2077 ssl_create_cipher_list(ret->method,
2078 &ret->cipher_list,&ret->cipher_list_by_id,
2079 meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST, ret->cert);
2080 if (ret->cipher_list == NULL
Adam Langley858a88d2014-06-20 12:00:00 -07002081 || sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002082 {
2083 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
2084 goto err2;
2085 }
2086
2087 ret->param = X509_VERIFY_PARAM_new();
2088 if (!ret->param)
2089 goto err;
2090
2091 ret->rsa_md5 = EVP_md5();
2092 ret->md5 = EVP_md5();
2093 ret->sha1 = EVP_sha1();
2094
2095 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
2096 goto err;
2097
2098 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
2099
2100 ret->extra_certs=NULL;
2101 /* No compression for DTLS */
2102 if (meth->version != DTLS1_VERSION)
2103 ret->comp_methods=SSL_COMP_get_compression_methods();
2104
2105 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
2106
2107#ifndef OPENSSL_NO_TLSEXT
2108 ret->tlsext_servername_callback = 0;
2109 ret->tlsext_servername_arg = NULL;
2110 /* Setup RFC4507 ticket keys */
2111 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
2112 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
2113 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
2114 ret->options |= SSL_OP_NO_TICKET;
2115
2116 ret->tlsext_status_cb = 0;
2117 ret->tlsext_status_arg = NULL;
2118
2119# ifndef OPENSSL_NO_NEXTPROTONEG
2120 ret->next_protos_advertised_cb = 0;
2121 ret->next_proto_select_cb = 0;
2122# endif
2123#endif
2124#ifndef OPENSSL_NO_PSK
2125 ret->psk_identity_hint=NULL;
2126 ret->psk_client_callback=NULL;
2127 ret->psk_server_callback=NULL;
2128#endif
2129 ret->custom_cli_ext_records = NULL;
2130 ret->custom_cli_ext_records_count = 0;
2131 ret->custom_srv_ext_records = NULL;
2132 ret->custom_srv_ext_records_count = 0;
2133#ifndef OPENSSL_NO_BUF_FREELISTS
2134 ret->freelist_max_len = SSL_MAX_BUF_FREELIST_LEN_DEFAULT;
2135 ret->rbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));
2136 if (!ret->rbuf_freelist)
2137 goto err;
2138 ret->rbuf_freelist->chunklen = 0;
2139 ret->rbuf_freelist->len = 0;
2140 ret->rbuf_freelist->head = NULL;
2141 ret->wbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));
2142 if (!ret->wbuf_freelist)
2143 {
2144 OPENSSL_free(ret->rbuf_freelist);
2145 goto err;
2146 }
2147 ret->wbuf_freelist->chunklen = 0;
2148 ret->wbuf_freelist->len = 0;
2149 ret->wbuf_freelist->head = NULL;
2150#endif
2151#ifndef OPENSSL_NO_ENGINE
2152 ret->client_cert_engine = NULL;
2153#ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
2154#define eng_strx(x) #x
2155#define eng_str(x) eng_strx(x)
2156 /* Use specific client engine automatically... ignore errors */
2157 {
2158 ENGINE *eng;
2159 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
2160 if (!eng)
2161 {
2162 ERR_clear_error();
2163 ENGINE_load_builtin_engines();
2164 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
2165 }
2166 if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
2167 ERR_clear_error();
2168 }
2169#endif
2170#endif
2171 /* Default is to connect to non-RI servers. When RI is more widely
2172 * deployed might change this.
2173 */
2174 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
2175
2176 return(ret);
2177err:
2178 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
2179err2:
2180 if (ret != NULL) SSL_CTX_free(ret);
2181 return(NULL);
2182 }
2183
2184#if 0
2185static void SSL_COMP_free(SSL_COMP *comp)
2186 { OPENSSL_free(comp); }
2187#endif
2188
2189#ifndef OPENSSL_NO_BUF_FREELISTS
2190static void
2191ssl_buf_freelist_free(SSL3_BUF_FREELIST *list)
2192 {
2193 SSL3_BUF_FREELIST_ENTRY *ent, *next;
2194 for (ent = list->head; ent; ent = next)
2195 {
2196 next = ent->next;
2197 OPENSSL_free(ent);
2198 }
2199 OPENSSL_free(list);
2200 }
2201#endif
2202
2203void SSL_CTX_free(SSL_CTX *a)
2204 {
2205 int i;
2206
2207 if (a == NULL) return;
2208
2209 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
2210#ifdef REF_PRINT
2211 REF_PRINT("SSL_CTX",a);
2212#endif
2213 if (i > 0) return;
2214#ifdef REF_CHECK
2215 if (i < 0)
2216 {
2217 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
2218 abort(); /* ok */
2219 }
2220#endif
2221
2222 if (a->param)
2223 X509_VERIFY_PARAM_free(a->param);
2224
2225 /*
2226 * Free internal session cache. However: the remove_cb() may reference
2227 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2228 * after the sessions were flushed.
2229 * As the ex_data handling routines might also touch the session cache,
2230 * the most secure solution seems to be: empty (flush) the cache, then
2231 * free ex_data, then finally free the cache.
2232 * (See ticket [openssl.org #212].)
2233 */
2234 if (a->sessions != NULL)
2235 SSL_CTX_flush_sessions(a,0);
2236
2237 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
2238
2239 if (a->sessions != NULL)
2240 lh_SSL_SESSION_free(a->sessions);
2241
2242 if (a->cert_store != NULL)
2243 X509_STORE_free(a->cert_store);
2244 if (a->cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002245 ssl_cipher_preference_list_free(a->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002246 if (a->cipher_list_by_id != NULL)
2247 sk_SSL_CIPHER_free(a->cipher_list_by_id);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07002248 if (a->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002249 ssl_cipher_preference_list_free(a->cipher_list_tls11);
Adam Langley95c29f32014-06-20 12:00:00 -07002250 if (a->cert != NULL)
2251 ssl_cert_free(a->cert);
2252 if (a->client_CA != NULL)
2253 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2254 if (a->extra_certs != NULL)
2255 sk_X509_pop_free(a->extra_certs,X509_free);
2256#if 0 /* This should never be done, since it removes a global database */
2257 if (a->comp_methods != NULL)
2258 sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free);
2259#else
2260 a->comp_methods = NULL;
2261#endif
2262
2263 if (a->srtp_profiles)
2264 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2265
2266#ifndef OPENSSL_NO_PSK
2267 if (a->psk_identity_hint)
2268 OPENSSL_free(a->psk_identity_hint);
2269#endif
2270#ifndef OPENSSL_NO_TLSEXT
2271 OPENSSL_free(a->custom_cli_ext_records);
2272 OPENSSL_free(a->custom_srv_ext_records);
2273#endif
2274
2275 /* TODO(fork): remove. */
2276#if 0
2277#ifndef OPENSSL_NO_ENGINE
2278 if (a->client_cert_engine)
2279 ENGINE_finish(a->client_cert_engine);
2280#endif
2281#endif
2282
2283#ifndef OPENSSL_NO_BUF_FREELISTS
2284 if (a->wbuf_freelist)
2285 ssl_buf_freelist_free(a->wbuf_freelist);
2286 if (a->rbuf_freelist)
2287 ssl_buf_freelist_free(a->rbuf_freelist);
2288#endif
2289#ifndef OPENSSL_NO_TLSEXT
2290# ifndef OPENSSL_NO_EC
2291 if (a->tlsext_ecpointformatlist)
2292 OPENSSL_free(a->tlsext_ecpointformatlist);
2293 if (a->tlsext_ellipticcurvelist)
2294 OPENSSL_free(a->tlsext_ellipticcurvelist);
2295# endif /* OPENSSL_NO_EC */
2296 if (a->alpn_client_proto_list != NULL)
2297 OPENSSL_free(a->alpn_client_proto_list);
2298#endif
2299
Adam Langley1258b6a2014-06-20 12:00:00 -07002300#ifndef OPENSSL_NO_TLSEXT
2301 if (a->tlsext_channel_id_private)
2302 EVP_PKEY_free(a->tlsext_channel_id_private);
2303#endif
2304
Adam Langley95c29f32014-06-20 12:00:00 -07002305 OPENSSL_free(a);
2306 }
2307
2308void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2309 {
2310 ctx->default_passwd_callback=cb;
2311 }
2312
2313void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2314 {
2315 ctx->default_passwd_callback_userdata=u;
2316 }
2317
2318void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2319 {
2320 ctx->app_verify_callback=cb;
2321 ctx->app_verify_arg=arg;
2322 }
2323
2324void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2325 {
2326 ctx->verify_mode=mode;
2327 ctx->default_verify_callback=cb;
2328 }
2329
2330void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2331 {
2332 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2333 }
2334
2335void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2336 {
2337 ssl_cert_set_cert_cb(c->cert, cb, arg);
2338 }
2339
2340void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2341 {
2342 ssl_cert_set_cert_cb(s->cert, cb, arg);
2343 }
2344
2345void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
2346 {
2347 CERT_PKEY *cpk;
2348 int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
2349 int rsa_enc_export,dh_rsa_export,dh_dsa_export;
2350 int rsa_tmp_export,dh_tmp_export,kl;
2351 unsigned long mask_k,mask_a,emask_k,emask_a;
2352#ifndef OPENSSL_NO_ECDSA
2353 int have_ecc_cert, ecdsa_ok, ecc_pkey_size;
2354#endif
2355#ifndef OPENSSL_NO_ECDH
2356 int have_ecdh_tmp, ecdh_ok;
2357#endif
2358#ifndef OPENSSL_NO_EC
2359 X509 *x = NULL;
2360 EVP_PKEY *ecc_pkey = NULL;
2361 int signature_nid = 0, pk_nid = 0, md_nid = 0;
2362#endif
2363 if (c == NULL) return;
2364
2365 kl=SSL_C_EXPORT_PKEYLENGTH(cipher);
2366
2367#ifndef OPENSSL_NO_RSA
2368 rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
2369 rsa_tmp_export=(c->rsa_tmp_cb != NULL ||
2370 (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl));
2371#else
2372 rsa_tmp=rsa_tmp_export=0;
2373#endif
2374#ifndef OPENSSL_NO_DH
2375 dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
2376 dh_tmp_export=(c->dh_tmp_cb != NULL ||
2377 (dh_tmp && DH_size(c->dh_tmp)*8 <= kl));
2378#else
2379 dh_tmp=dh_tmp_export=0;
2380#endif
2381
2382#ifndef OPENSSL_NO_ECDH
2383 have_ecdh_tmp=(c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
2384#endif
2385 cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
2386 rsa_enc= cpk->valid_flags & CERT_PKEY_VALID;
2387 rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
2388 cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
2389 rsa_sign= cpk->valid_flags & CERT_PKEY_SIGN;
2390 cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
2391 dsa_sign= cpk->valid_flags & CERT_PKEY_SIGN;
2392 cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
2393 dh_rsa= cpk->valid_flags & CERT_PKEY_VALID;
2394 dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
2395 cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
2396/* FIX THIS EAY EAY EAY */
2397 dh_dsa= cpk->valid_flags & CERT_PKEY_VALID;
2398 dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
2399 cpk= &(c->pkeys[SSL_PKEY_ECC]);
2400#ifndef OPENSSL_NO_EC
2401 have_ecc_cert= cpk->valid_flags & CERT_PKEY_VALID;
2402#endif
2403 mask_k=0;
2404 mask_a=0;
2405 emask_k=0;
2406 emask_a=0;
2407
2408
2409
2410#ifdef CIPHER_DEBUG
2411 printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
2412 rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
2413 rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
2414#endif
2415
2416 cpk = &(c->pkeys[SSL_PKEY_GOST01]);
2417 if (cpk->x509 != NULL && cpk->privatekey !=NULL) {
2418 mask_k |= SSL_kGOST;
2419 mask_a |= SSL_aGOST01;
2420 }
2421 cpk = &(c->pkeys[SSL_PKEY_GOST94]);
2422 if (cpk->x509 != NULL && cpk->privatekey !=NULL) {
2423 mask_k |= SSL_kGOST;
2424 mask_a |= SSL_aGOST94;
2425 }
2426
2427 if (rsa_enc || (rsa_tmp && rsa_sign))
2428 mask_k|=SSL_kRSA;
2429 if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc)))
2430 emask_k|=SSL_kRSA;
2431
2432#if 0
2433 /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
2434 if ( (dh_tmp || dh_rsa || dh_dsa) &&
2435 (rsa_enc || rsa_sign || dsa_sign))
2436 mask_k|=SSL_kEDH;
2437 if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
2438 (rsa_enc || rsa_sign || dsa_sign))
2439 emask_k|=SSL_kEDH;
2440#endif
2441
2442 if (dh_tmp_export)
2443 emask_k|=SSL_kEDH;
2444
2445 if (dh_tmp)
2446 mask_k|=SSL_kEDH;
2447
2448 if (dh_rsa) mask_k|=SSL_kDHr;
2449 if (dh_rsa_export) emask_k|=SSL_kDHr;
2450
2451 if (dh_dsa) mask_k|=SSL_kDHd;
2452 if (dh_dsa_export) emask_k|=SSL_kDHd;
2453
2454 if (emask_k & (SSL_kDHr|SSL_kDHd))
2455 mask_a |= SSL_aDH;
2456
2457 if (rsa_enc || rsa_sign)
2458 {
2459 mask_a|=SSL_aRSA;
2460 emask_a|=SSL_aRSA;
2461 }
2462
2463 if (dsa_sign)
2464 {
2465 mask_a|=SSL_aDSS;
2466 emask_a|=SSL_aDSS;
2467 }
2468
2469 mask_a|=SSL_aNULL;
2470 emask_a|=SSL_aNULL;
2471
2472 /* An ECC certificate may be usable for ECDH and/or
2473 * ECDSA cipher suites depending on the key usage extension.
2474 */
2475#ifndef OPENSSL_NO_EC
2476 if (have_ecc_cert)
2477 {
2478 cpk = &c->pkeys[SSL_PKEY_ECC];
2479 x = cpk->x509;
2480 /* This call populates extension flags (ex_flags) */
2481 X509_check_purpose(x, -1, 0);
2482 ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2483 (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1;
2484 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2485 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
2486 if (!(cpk->valid_flags & CERT_PKEY_SIGN))
2487 ecdsa_ok = 0;
2488 ecc_pkey = X509_get_pubkey(x);
2489 ecc_pkey_size = (ecc_pkey != NULL) ?
2490 EVP_PKEY_bits(ecc_pkey) : 0;
2491 EVP_PKEY_free(ecc_pkey);
2492 if ((x->sig_alg) && (x->sig_alg->algorithm))
2493 {
2494 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2495 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2496 }
2497#ifndef OPENSSL_NO_ECDH
2498 if (ecdh_ok)
2499 {
2500
2501 if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa)
2502 {
2503 mask_k|=SSL_kECDHr;
2504 mask_a|=SSL_aECDH;
2505 if (ecc_pkey_size <= 163)
2506 {
2507 emask_k|=SSL_kECDHr;
2508 emask_a|=SSL_aECDH;
2509 }
2510 }
2511
2512 if (pk_nid == NID_X9_62_id_ecPublicKey)
2513 {
2514 mask_k|=SSL_kECDHe;
2515 mask_a|=SSL_aECDH;
2516 if (ecc_pkey_size <= 163)
2517 {
2518 emask_k|=SSL_kECDHe;
2519 emask_a|=SSL_aECDH;
2520 }
2521 }
2522 }
2523#endif
2524#ifndef OPENSSL_NO_ECDSA
2525 if (ecdsa_ok)
2526 {
2527 mask_a|=SSL_aECDSA;
2528 emask_a|=SSL_aECDSA;
2529 }
2530#endif
2531 }
2532#endif
2533
2534#ifndef OPENSSL_NO_ECDH
2535 if (have_ecdh_tmp)
2536 {
2537 mask_k|=SSL_kEECDH;
2538 emask_k|=SSL_kEECDH;
2539 }
2540#endif
2541
2542#ifndef OPENSSL_NO_PSK
2543 mask_k |= SSL_kPSK;
2544 mask_a |= SSL_aPSK;
2545 emask_k |= SSL_kPSK;
2546 emask_a |= SSL_aPSK;
2547#endif
2548
2549 c->mask_k=mask_k;
2550 c->mask_a=mask_a;
2551 c->export_mask_k=emask_k;
2552 c->export_mask_a=emask_a;
2553 c->valid=1;
2554 }
2555
2556/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2557#define ku_reject(x, usage) \
2558 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2559
2560#ifndef OPENSSL_NO_EC
2561
2562int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2563 {
2564 unsigned long alg_k, alg_a;
2565 EVP_PKEY *pkey = NULL;
2566 int keysize = 0;
2567 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2568 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2569
2570 alg_k = cs->algorithm_mkey;
2571 alg_a = cs->algorithm_auth;
2572
2573 if (SSL_C_IS_EXPORT(cs))
2574 {
2575 /* ECDH key length in export ciphers must be <= 163 bits */
2576 pkey = X509_get_pubkey(x);
2577 if (pkey == NULL) return 0;
2578 keysize = EVP_PKEY_bits(pkey);
2579 EVP_PKEY_free(pkey);
2580 if (keysize > 163) return 0;
2581 }
2582
2583 /* This call populates the ex_flags field correctly */
2584 X509_check_purpose(x, -1, 0);
2585 if ((x->sig_alg) && (x->sig_alg->algorithm))
2586 {
2587 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2588 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2589 }
2590 if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr)
2591 {
2592 /* key usage, if present, must allow key agreement */
2593 if (ku_reject(x, X509v3_KU_KEY_AGREEMENT))
2594 {
2595 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT);
2596 return 0;
2597 }
2598 if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < TLS1_2_VERSION)
2599 {
2600 /* signature alg must be ECDSA */
2601 if (pk_nid != NID_X9_62_id_ecPublicKey)
2602 {
2603 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE);
2604 return 0;
2605 }
2606 }
2607 if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < TLS1_2_VERSION)
2608 {
2609 /* signature alg must be RSA */
2610
2611 if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa)
2612 {
2613 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE);
2614 return 0;
2615 }
2616 }
2617 }
2618 if (alg_a & SSL_aECDSA)
2619 {
2620 /* key usage, if present, must allow signing */
2621 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2622 {
2623 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2624 return 0;
2625 }
2626 }
2627
2628 return 1; /* all checks are ok */
2629 }
2630
2631#endif
2632
2633static int ssl_get_server_cert_index(const SSL *s)
2634 {
2635 int idx;
2636 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2637 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2638 idx = SSL_PKEY_RSA_SIGN;
2639 if (idx == -1)
2640 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2641 return idx;
2642 }
2643
2644CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2645 {
2646 CERT *c;
2647 int i;
2648
2649 c = s->cert;
2650 ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
2651
2652#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2653 /* Broken protocol test: return last used certificate: which may
2654 * mismatch the one expected.
2655 */
2656 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2657 return c->key;
2658#endif
2659
2660 i = ssl_get_server_cert_index(s);
2661
2662 /* This may or may not be an error. */
2663 if (i < 0)
2664 return NULL;
2665
2666 /* May be NULL. */
2667 return &c->pkeys[i];
2668 }
2669
2670EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd)
2671 {
2672 unsigned long alg_a;
2673 CERT *c;
2674 int idx = -1;
2675
2676 alg_a = cipher->algorithm_auth;
2677 c=s->cert;
2678
2679#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2680 /* Broken protocol test: use last key: which may
2681 * mismatch the one expected.
2682 */
2683 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2684 idx = c->key - c->pkeys;
2685 else
2686#endif
2687
2688 if ((alg_a & SSL_aDSS) &&
2689 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
2690 idx = SSL_PKEY_DSA_SIGN;
2691 else if (alg_a & SSL_aRSA)
2692 {
2693 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2694 idx = SSL_PKEY_RSA_SIGN;
2695 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2696 idx = SSL_PKEY_RSA_ENC;
2697 }
2698 else if ((alg_a & SSL_aECDSA) &&
2699 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2700 idx = SSL_PKEY_ECC;
2701 if (idx == -1)
2702 {
2703 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2704 return(NULL);
2705 }
2706 if (pmd)
2707 *pmd = c->pkeys[idx].digest;
2708 return c->pkeys[idx].privatekey;
2709 }
2710
2711#ifndef OPENSSL_NO_TLSEXT
2712unsigned char *ssl_get_authz_data(SSL *s, size_t *authz_length)
2713 {
2714 CERT *c;
2715 int i;
2716
2717 c = s->cert;
2718 i = ssl_get_server_cert_index(s);
2719
2720 if (i == -1)
2721 return NULL;
2722
2723 *authz_length = 0;
2724 if (c->pkeys[i].authz == NULL)
2725 return(NULL);
2726 *authz_length = c->pkeys[i].authz_length;
2727
2728 return c->pkeys[i].authz;
2729 }
2730
2731int ssl_get_server_cert_serverinfo(SSL *s, const unsigned char **serverinfo,
2732 size_t *serverinfo_length)
2733 {
2734 CERT *c = NULL;
2735 int i = 0;
2736 *serverinfo_length = 0;
2737
2738 c = s->cert;
2739 i = ssl_get_server_cert_index(s);
2740
2741 if (i == -1)
2742 return 0;
2743 if (c->pkeys[i].serverinfo == NULL)
2744 return 0;
2745
2746 *serverinfo = c->pkeys[i].serverinfo;
2747 *serverinfo_length = c->pkeys[i].serverinfo_length;
2748 return 1;
2749 }
2750#endif
2751
2752void ssl_update_cache(SSL *s,int mode)
2753 {
2754 int i;
2755
2756 /* If the session_id_length is 0, we are not supposed to cache it,
2757 * and it would be rather hard to do anyway :-) */
2758 if (s->session->session_id_length == 0) return;
2759
2760 i=s->session_ctx->session_cache_mode;
2761 if ((i & mode) && (!s->hit)
2762 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
2763 || SSL_CTX_add_session(s->session_ctx,s->session))
2764 && (s->session_ctx->new_session_cb != NULL))
2765 {
2766 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
2767 if (!s->session_ctx->new_session_cb(s,s->session))
2768 SSL_SESSION_free(s->session);
2769 }
2770
2771 /* auto flush every 255 connections */
2772 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2773 ((i & mode) == mode))
2774 {
2775 if ( (((mode & SSL_SESS_CACHE_CLIENT)
2776 ?s->session_ctx->stats.sess_connect_good
2777 :s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff)
2778 {
2779 SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL));
2780 }
2781 }
2782 }
2783
2784const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
2785 {
2786 return ctx->method;
2787 }
2788
2789const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2790 {
2791 return(s->method);
2792 }
2793
2794int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2795 {
2796 int conn= -1;
2797 int ret=1;
2798
2799 if (s->method != meth)
2800 {
2801 if (s->handshake_func != NULL)
2802 conn=(s->handshake_func == s->method->ssl_connect);
2803
2804 if (s->method->version == meth->version)
2805 s->method=meth;
2806 else
2807 {
2808 s->method->ssl_free(s);
2809 s->method=meth;
2810 ret=s->method->ssl_new(s);
2811 }
2812
2813 if (conn == 1)
2814 s->handshake_func=meth->ssl_connect;
2815 else if (conn == 0)
2816 s->handshake_func=meth->ssl_accept;
2817 }
2818 return(ret);
2819 }
2820
2821int SSL_get_error(const SSL *s,int i)
2822 {
2823 int reason;
2824 unsigned long l;
2825 BIO *bio;
2826
2827 if (i > 0) return(SSL_ERROR_NONE);
2828
2829 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2830 * etc, where we do encode the error */
2831 if ((l=ERR_peek_error()) != 0)
2832 {
2833 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2834 return(SSL_ERROR_SYSCALL);
2835 else
2836 return(SSL_ERROR_SSL);
2837 }
2838
Adam Langleyb2ce0582014-06-20 12:00:00 -07002839 if ((i < 0) && SSL_want_session(s))
2840 return(SSL_ERROR_PENDING_SESSION);
2841
Adam Langleydc9b1412014-06-20 12:00:00 -07002842 if ((i < 0) && SSL_want_certificate(s))
2843 return(SSL_ERROR_PENDING_CERTIFICATE);
2844
Adam Langley95c29f32014-06-20 12:00:00 -07002845 if ((i < 0) && SSL_want_read(s))
2846 {
2847 bio=SSL_get_rbio(s);
2848 if (BIO_should_read(bio))
2849 return(SSL_ERROR_WANT_READ);
2850 else if (BIO_should_write(bio))
2851 /* This one doesn't make too much sense ... We never try
2852 * to write to the rbio, and an application program where
2853 * rbio and wbio are separate couldn't even know what it
2854 * should wait for.
2855 * However if we ever set s->rwstate incorrectly
2856 * (so that we have SSL_want_read(s) instead of
2857 * SSL_want_write(s)) and rbio and wbio *are* the same,
2858 * this test works around that bug; so it might be safer
2859 * to keep it. */
2860 return(SSL_ERROR_WANT_WRITE);
2861 else if (BIO_should_io_special(bio))
2862 {
2863 reason=BIO_get_retry_reason(bio);
2864 if (reason == BIO_RR_CONNECT)
2865 return(SSL_ERROR_WANT_CONNECT);
2866 else if (reason == BIO_RR_ACCEPT)
2867 return(SSL_ERROR_WANT_ACCEPT);
2868 else
2869 return(SSL_ERROR_SYSCALL); /* unknown */
2870 }
2871 }
2872
2873 if ((i < 0) && SSL_want_write(s))
2874 {
2875 bio=SSL_get_wbio(s);
2876 if (BIO_should_write(bio))
2877 return(SSL_ERROR_WANT_WRITE);
2878 else if (BIO_should_read(bio))
2879 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2880 return(SSL_ERROR_WANT_READ);
2881 else if (BIO_should_io_special(bio))
2882 {
2883 reason=BIO_get_retry_reason(bio);
2884 if (reason == BIO_RR_CONNECT)
2885 return(SSL_ERROR_WANT_CONNECT);
2886 else if (reason == BIO_RR_ACCEPT)
2887 return(SSL_ERROR_WANT_ACCEPT);
2888 else
2889 return(SSL_ERROR_SYSCALL);
2890 }
2891 }
2892 if ((i < 0) && SSL_want_x509_lookup(s))
2893 {
2894 return(SSL_ERROR_WANT_X509_LOOKUP);
2895 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002896 if ((i < 0) && SSL_want_channel_id_lookup(s))
2897 {
2898 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2899 }
Adam Langley95c29f32014-06-20 12:00:00 -07002900
2901 if (i == 0)
2902 {
2903 if (s->version == SSL2_VERSION)
2904 {
2905 /* assume it is the socket being closed */
2906 return(SSL_ERROR_ZERO_RETURN);
2907 }
2908 else
2909 {
2910 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2911 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2912 return(SSL_ERROR_ZERO_RETURN);
2913 }
2914 }
2915 return(SSL_ERROR_SYSCALL);
2916 }
2917
2918int SSL_do_handshake(SSL *s)
2919 {
2920 int ret=1;
2921
2922 if (s->handshake_func == NULL)
2923 {
2924 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2925 return(-1);
2926 }
2927
2928 s->method->ssl_renegotiate_check(s);
2929
2930 if (SSL_in_init(s) || SSL_in_before(s))
2931 {
2932 ret=s->handshake_func(s);
2933 }
2934 return(ret);
2935 }
2936
2937/* For the next 2 functions, SSL_clear() sets shutdown and so
2938 * one of these calls will reset it */
2939void SSL_set_accept_state(SSL *s)
2940 {
2941 s->server=1;
2942 s->shutdown=0;
2943 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2944 s->handshake_func=s->method->ssl_accept;
2945 /* clear the current cipher */
2946 ssl_clear_cipher_ctx(s);
2947 ssl_clear_hash_ctx(&s->read_hash);
2948 ssl_clear_hash_ctx(&s->write_hash);
2949 }
2950
2951void SSL_set_connect_state(SSL *s)
2952 {
2953 s->server=0;
2954 s->shutdown=0;
2955 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2956 s->handshake_func=s->method->ssl_connect;
2957 /* clear the current cipher */
2958 ssl_clear_cipher_ctx(s);
2959 ssl_clear_hash_ctx(&s->read_hash);
2960 ssl_clear_hash_ctx(&s->write_hash);
2961 }
2962
2963int ssl_undefined_function(SSL *s)
2964 {
2965 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2966 return(0);
2967 }
2968
2969int ssl_undefined_void_function(void)
2970 {
2971 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2972 return(0);
2973 }
2974
2975int ssl_undefined_const_function(const SSL *s)
2976 {
2977 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2978 return(0);
2979 }
2980
2981SSL_METHOD *ssl_bad_method(int ver)
2982 {
2983 OPENSSL_PUT_ERROR(SSL, ssl_bad_method, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2984 return(NULL);
2985 }
2986
2987const char *SSL_get_version(const SSL *s)
2988 {
2989 if (s->version == TLS1_2_VERSION)
2990 return("TLSv1.2");
2991 else if (s->version == TLS1_1_VERSION)
2992 return("TLSv1.1");
2993 else if (s->version == TLS1_VERSION)
2994 return("TLSv1");
2995 else if (s->version == SSL3_VERSION)
2996 return("SSLv3");
2997 else if (s->version == SSL2_VERSION)
2998 return("SSLv2");
2999 else
3000 return("unknown");
3001 }
3002
3003void ssl_clear_cipher_ctx(SSL *s)
3004 {
3005 if (s->enc_read_ctx != NULL)
3006 {
3007 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
3008 OPENSSL_free(s->enc_read_ctx);
3009 s->enc_read_ctx=NULL;
3010 }
3011 if (s->enc_write_ctx != NULL)
3012 {
3013 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
3014 OPENSSL_free(s->enc_write_ctx);
3015 s->enc_write_ctx=NULL;
3016 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07003017 if (s->aead_read_ctx != NULL)
3018 {
3019 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
3020 OPENSSL_free(s->aead_read_ctx);
3021 s->aead_read_ctx = NULL;
3022 }
3023 if (s->aead_write_ctx != NULL)
3024 {
3025 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
3026 OPENSSL_free(s->aead_write_ctx);
3027 s->aead_write_ctx = NULL;
3028 }
Adam Langley95c29f32014-06-20 12:00:00 -07003029 }
3030
3031X509 *SSL_get_certificate(const SSL *s)
3032 {
3033 if (s->cert != NULL)
3034 return(s->cert->key->x509);
3035 else
3036 return(NULL);
3037 }
3038
3039EVP_PKEY *SSL_get_privatekey(const SSL *s)
3040 {
3041 if (s->cert != NULL)
3042 return(s->cert->key->privatekey);
3043 else
3044 return(NULL);
3045 }
3046
3047X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
3048 {
3049 if (ctx->cert != NULL)
3050 return ctx->cert->key->x509;
3051 else
3052 return NULL;
3053 }
3054
3055EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
3056 {
3057 if (ctx->cert != NULL)
3058 return ctx->cert->key->privatekey;
3059 else
3060 return NULL ;
3061 }
3062
3063const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
3064 {
3065 if ((s->session != NULL) && (s->session->cipher != NULL))
3066 return(s->session->cipher);
3067 return(NULL);
3068 }
3069const void *SSL_get_current_compression(SSL *s)
3070 {
3071 return NULL;
3072 }
3073const void *SSL_get_current_expansion(SSL *s)
3074 {
3075 return NULL;
3076 }
3077
3078int ssl_init_wbio_buffer(SSL *s,int push)
3079 {
3080 BIO *bbio;
3081
3082 if (s->bbio == NULL)
3083 {
3084 bbio=BIO_new(BIO_f_buffer());
3085 if (bbio == NULL) return(0);
3086 s->bbio=bbio;
3087 }
3088 else
3089 {
3090 bbio=s->bbio;
3091 if (s->bbio == s->wbio)
3092 s->wbio=BIO_pop(s->wbio);
3093 }
3094 (void)BIO_reset(bbio);
3095/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
3096 if (!BIO_set_read_buffer_size(bbio,1))
3097 {
3098 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
3099 return(0);
3100 }
3101 if (push)
3102 {
3103 if (s->wbio != bbio)
3104 s->wbio=BIO_push(bbio,s->wbio);
3105 }
3106 else
3107 {
3108 if (s->wbio == bbio)
3109 s->wbio=BIO_pop(bbio);
3110 }
3111 return(1);
3112 }
3113
3114void ssl_free_wbio_buffer(SSL *s)
3115 {
3116 if (s->bbio == NULL) return;
3117
3118 if (s->bbio == s->wbio)
3119 {
3120 /* remove buffering */
3121 s->wbio=BIO_pop(s->wbio);
3122#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
3123 assert(s->wbio != NULL);
3124#endif
3125 }
3126 BIO_free(s->bbio);
3127 s->bbio=NULL;
3128 }
3129
3130void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
3131 {
3132 ctx->quiet_shutdown=mode;
3133 }
3134
3135int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
3136 {
3137 return(ctx->quiet_shutdown);
3138 }
3139
3140void SSL_set_quiet_shutdown(SSL *s,int mode)
3141 {
3142 s->quiet_shutdown=mode;
3143 }
3144
3145int SSL_get_quiet_shutdown(const SSL *s)
3146 {
3147 return(s->quiet_shutdown);
3148 }
3149
3150void SSL_set_shutdown(SSL *s,int mode)
3151 {
3152 s->shutdown=mode;
3153 }
3154
3155int SSL_get_shutdown(const SSL *s)
3156 {
3157 return(s->shutdown);
3158 }
3159
3160int SSL_version(const SSL *s)
3161 {
3162 return(s->version);
3163 }
3164
3165SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
3166 {
3167 return(ssl->ctx);
3168 }
3169
3170SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
3171 {
3172 if (ssl->ctx == ctx)
3173 return ssl->ctx;
3174#ifndef OPENSSL_NO_TLSEXT
3175 if (ctx == NULL)
3176 ctx = ssl->initial_ctx;
3177#endif
3178 if (ssl->cert != NULL)
3179 ssl_cert_free(ssl->cert);
3180 ssl->cert = ssl_cert_dup(ctx->cert);
3181 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
3182 if (ssl->ctx != NULL)
3183 SSL_CTX_free(ssl->ctx); /* decrement reference count */
3184 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07003185
3186 ssl->sid_ctx_length = ctx->sid_ctx_length;
3187 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
3188 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
3189
Adam Langley95c29f32014-06-20 12:00:00 -07003190 return(ssl->ctx);
3191 }
3192
3193#ifndef OPENSSL_NO_STDIO
3194int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
3195 {
3196 return(X509_STORE_set_default_paths(ctx->cert_store));
3197 }
3198
3199int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
3200 const char *CApath)
3201 {
3202 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
3203 }
3204#endif
3205
3206void SSL_set_info_callback(SSL *ssl,
3207 void (*cb)(const SSL *ssl,int type,int val))
3208 {
3209 ssl->info_callback=cb;
3210 }
3211
3212/* One compiler (Diab DCC) doesn't like argument names in returned
3213 function pointer. */
3214void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
3215 {
3216 return ssl->info_callback;
3217 }
3218
3219int SSL_state(const SSL *ssl)
3220 {
3221 return(ssl->state);
3222 }
3223
3224void SSL_set_state(SSL *ssl, int state)
3225 {
3226 ssl->state = state;
3227 }
3228
3229void SSL_set_verify_result(SSL *ssl,long arg)
3230 {
3231 ssl->verify_result=arg;
3232 }
3233
3234long SSL_get_verify_result(const SSL *ssl)
3235 {
3236 return(ssl->verify_result);
3237 }
3238
3239int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
3240 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
3241 {
3242 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
3243 new_func, dup_func, free_func);
3244 }
3245
3246int SSL_set_ex_data(SSL *s,int idx,void *arg)
3247 {
3248 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
3249 }
3250
3251void *SSL_get_ex_data(const SSL *s,int idx)
3252 {
3253 return(CRYPTO_get_ex_data(&s->ex_data,idx));
3254 }
3255
3256int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
3257 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
3258 {
3259 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
3260 new_func, dup_func, free_func);
3261 }
3262
3263int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
3264 {
3265 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
3266 }
3267
3268void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
3269 {
3270 return(CRYPTO_get_ex_data(&s->ex_data,idx));
3271 }
3272
3273int ssl_ok(SSL *s)
3274 {
3275 return(1);
3276 }
3277
3278X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
3279 {
3280 return(ctx->cert_store);
3281 }
3282
3283void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
3284 {
3285 if (ctx->cert_store != NULL)
3286 X509_STORE_free(ctx->cert_store);
3287 ctx->cert_store=store;
3288 }
3289
3290int SSL_want(const SSL *s)
3291 {
3292 return(s->rwstate);
3293 }
3294
3295/*!
3296 * \brief Set the callback for generating temporary RSA keys.
3297 * \param ctx the SSL context.
3298 * \param cb the callback
3299 */
3300
3301#ifndef OPENSSL_NO_RSA
3302void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
3303 int is_export,
3304 int keylength))
3305 {
3306 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3307 }
3308
3309void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
3310 int is_export,
3311 int keylength))
3312 {
3313 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3314 }
3315#endif
3316
3317#ifdef DOXYGEN
3318/*!
3319 * \brief The RSA temporary key callback function.
3320 * \param ssl the SSL session.
3321 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
3322 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
3323 * of the required key in bits.
3324 * \return the temporary RSA key.
3325 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
3326 */
3327
3328RSA *cb(SSL *ssl,int is_export,int keylength)
3329 {}
3330#endif
3331
3332/*!
3333 * \brief Set the callback for generating temporary DH keys.
3334 * \param ctx the SSL context.
3335 * \param dh the callback
3336 */
3337
3338#ifndef OPENSSL_NO_DH
3339void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
3340 int keylength))
3341 {
3342 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3343 }
3344
3345void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
3346 int keylength))
3347 {
3348 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3349 }
3350#endif
3351
3352#ifndef OPENSSL_NO_ECDH
3353void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
3354 int keylength))
3355 {
3356 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
3357 }
3358
3359void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
3360 int keylength))
3361 {
3362 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
3363 }
3364#endif
3365
3366#ifndef OPENSSL_NO_PSK
3367int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
3368 {
3369 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
3370 {
3371 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
3372 return 0;
3373 }
3374 if (ctx->psk_identity_hint != NULL)
3375 OPENSSL_free(ctx->psk_identity_hint);
3376 if (identity_hint != NULL)
3377 {
3378 ctx->psk_identity_hint = BUF_strdup(identity_hint);
3379 if (ctx->psk_identity_hint == NULL)
3380 return 0;
3381 }
3382 else
3383 ctx->psk_identity_hint = NULL;
3384 return 1;
3385 }
3386
3387int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
3388 {
3389 if (s == NULL)
3390 return 0;
3391
Adam Langley95c29f32014-06-20 12:00:00 -07003392 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
3393 {
3394 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
3395 return 0;
3396 }
Adam Langley0289c732014-06-20 12:00:00 -07003397
3398 /* Clear hint in SSL and associated SSL_SESSION (if any). */
3399 if (s->psk_identity_hint != NULL)
3400 {
3401 OPENSSL_free(s->psk_identity_hint);
3402 s->psk_identity_hint = NULL;
3403 }
3404 if (s->session != NULL && s->session->psk_identity_hint != NULL)
3405 {
Adam Langley95c29f32014-06-20 12:00:00 -07003406 OPENSSL_free(s->session->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -07003407 s->session->psk_identity_hint = NULL;
3408 }
3409
Adam Langley95c29f32014-06-20 12:00:00 -07003410 if (identity_hint != NULL)
3411 {
Adam Langley0289c732014-06-20 12:00:00 -07003412 /* The hint is stored in SSL and SSL_SESSION with the one in
3413 * SSL_SESSION taking precedence. Thus, if SSL_SESSION is avaiable,
3414 * we store the hint there, otherwise we store it in SSL. */
3415 if (s->session != NULL)
3416 {
3417 s->session->psk_identity_hint = BUF_strdup(identity_hint);
3418 if (s->session->psk_identity_hint == NULL)
3419 return 0;
3420 }
3421 else
3422 {
3423 s->psk_identity_hint = BUF_strdup(identity_hint);
3424 if (s->psk_identity_hint == NULL)
3425 return 0;
3426 }
Adam Langley95c29f32014-06-20 12:00:00 -07003427 }
Adam Langley95c29f32014-06-20 12:00:00 -07003428 return 1;
3429 }
3430
3431const char *SSL_get_psk_identity_hint(const SSL *s)
3432 {
Adam Langley0289c732014-06-20 12:00:00 -07003433 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07003434 return NULL;
Adam Langley0289c732014-06-20 12:00:00 -07003435 /* The hint is stored in SSL and SSL_SESSION with the one in SSL_SESSION
3436 * taking precedence. */
3437 if (s->session != NULL)
3438 return(s->session->psk_identity_hint);
3439 return(s->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07003440 }
3441
3442const char *SSL_get_psk_identity(const SSL *s)
3443 {
3444 if (s == NULL || s->session == NULL)
3445 return NULL;
3446 return(s->session->psk_identity);
3447 }
3448
3449void SSL_set_psk_client_callback(SSL *s,
3450 unsigned int (*cb)(SSL *ssl, const char *hint,
3451 char *identity, unsigned int max_identity_len, unsigned char *psk,
3452 unsigned int max_psk_len))
3453 {
3454 s->psk_client_callback = cb;
3455 }
3456
3457void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
3458 unsigned int (*cb)(SSL *ssl, const char *hint,
3459 char *identity, unsigned int max_identity_len, unsigned char *psk,
3460 unsigned int max_psk_len))
3461 {
3462 ctx->psk_client_callback = cb;
3463 }
3464
3465void SSL_set_psk_server_callback(SSL *s,
3466 unsigned int (*cb)(SSL *ssl, const char *identity,
3467 unsigned char *psk, unsigned int max_psk_len))
3468 {
3469 s->psk_server_callback = cb;
3470 }
3471
3472void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
3473 unsigned int (*cb)(SSL *ssl, const char *identity,
3474 unsigned char *psk, unsigned int max_psk_len))
3475 {
3476 ctx->psk_server_callback = cb;
3477 }
3478#endif
3479
3480void 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))
3481 {
3482 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3483 }
3484void 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))
3485 {
3486 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3487 }
3488
Adam Langleyadb739e2014-06-20 12:00:00 -07003489int SSL_cutthrough_complete(const SSL *s)
3490 {
3491 return (!s->server && /* cutthrough only applies to clients */
3492 !s->hit && /* full-handshake */
3493 s->version >= SSL3_VERSION &&
3494 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3495 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3496 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3497 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3498 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
3499 s->state == SSL3_ST_CR_FINISHED_A));
3500 }
3501
Adam Langley95f22882014-06-20 12:00:00 -07003502void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3503 size_t* ssl_session_size)
3504{
3505 *ssl_size = sizeof(SSL);
3506 *ssl_ctx_size = sizeof(SSL_CTX);
3507 *ssl_session_size = sizeof(SSL_SESSION);
3508}
3509
Adam Langleyadb739e2014-06-20 12:00:00 -07003510int ssl3_can_cutthrough(const SSL *s)
3511 {
3512 const SSL_CIPHER *c;
3513
3514 /* require a strong enough cipher */
3515 if (SSL_get_cipher_bits(s, NULL) < 128)
3516 return 0;
3517
3518 /* require ALPN or NPN extension */
3519 if (!s->s3->alpn_selected
3520#ifndef OPENSSL_NO_NEXTPROTONEG
3521 && !s->s3->next_proto_neg_seen
3522#endif
3523 )
3524 {
3525 return 0;
3526 }
3527
3528 /* require a forward-secret cipher */
3529 c = SSL_get_current_cipher(s);
3530 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3531 c->algorithm_mkey != SSL_kEECDH))
3532 {
3533 return 0;
3534 }
3535
3536 return 1;
3537 }
3538
Adam Langley29707792014-06-20 12:00:00 -07003539/* ssl_get_max_version returns the maximum SSL/TLS version number supported by
3540 * |s|, or zero if all versions are disabled. */
3541int ssl_get_max_version(const SSL *s)
3542 {
3543 /* Only one version supported for DTLS. */
3544 if (s->version == DTLS1_VERSION)
3545 return DTLS1_VERSION;
3546
3547 if (!(s->options & SSL_OP_NO_TLSv1_2))
3548 return TLS1_2_VERSION;
3549 if (!(s->options & SSL_OP_NO_TLSv1_1))
3550 return TLS1_1_VERSION;
3551 if (!(s->options & SSL_OP_NO_TLSv1))
3552 return TLS1_VERSION;
3553 if (!(s->options & SSL_OP_NO_SSLv3))
3554 return SSL3_VERSION;
3555 if (!(s->options & SSL_OP_NO_SSLv2))
3556 return SSL2_VERSION;
3557 return 0;
3558 }
3559
Adam Langley95c29f32014-06-20 12:00:00 -07003560/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3561 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3562 * any. If EVP_MD pointer is passed, initializes ctx with this md
3563 * Returns newly allocated ctx;
3564 */
3565
3566EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3567{
3568 ssl_clear_hash_ctx(hash);
3569 *hash = EVP_MD_CTX_create();
3570 if (md) EVP_DigestInit_ex(*hash,md,NULL);
3571 return *hash;
3572}
3573void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3574{
3575
3576 if (*hash) EVP_MD_CTX_destroy(*hash);
3577 *hash=NULL;
3578}
3579
3580void SSL_set_debug(SSL *s, int debug)
3581 {
3582 s->debug = debug;
3583 }
3584
3585int SSL_cache_hit(SSL *s)
3586 {
3587 return s->hit;
3588 }
3589
3590int SSL_is_server(SSL *s)
3591 {
3592 return s->server;
3593 }