blob: 68f49474750adf19b9fefbccc7f12466bf655b22 [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;
Adam Langley95c29f32014-06-20 12:00:00 -0700336 s->tlsext_debug_cb = 0;
337 s->tlsext_debug_arg = NULL;
338 s->tlsext_ticket_expected = 0;
339 s->tlsext_status_type = -1;
340 s->tlsext_status_expected = 0;
341 s->tlsext_ocsp_ids = NULL;
342 s->tlsext_ocsp_exts = NULL;
343 s->tlsext_ocsp_resp = NULL;
344 s->tlsext_ocsp_resplen = -1;
345 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
346 s->initial_ctx=ctx;
347#ifndef OPENSSL_NO_EC
348 if (ctx->tlsext_ecpointformatlist)
349 {
350 s->tlsext_ecpointformatlist =
351 BUF_memdup(ctx->tlsext_ecpointformatlist,
352 ctx->tlsext_ecpointformatlist_length);
353 if (!s->tlsext_ecpointformatlist)
354 goto err;
355 s->tlsext_ecpointformatlist_length =
356 ctx->tlsext_ecpointformatlist_length;
357 }
358 if (ctx->tlsext_ellipticcurvelist)
359 {
360 s->tlsext_ellipticcurvelist =
361 BUF_memdup(ctx->tlsext_ellipticcurvelist,
362 ctx->tlsext_ellipticcurvelist_length);
363 if (!s->tlsext_ellipticcurvelist)
364 goto err;
365 s->tlsext_ellipticcurvelist_length =
366 ctx->tlsext_ellipticcurvelist_length;
367 }
368#endif
369# ifndef OPENSSL_NO_NEXTPROTONEG
370 s->next_proto_negotiated = NULL;
371# endif
372
373 if (s->ctx->alpn_client_proto_list)
374 {
375 s->alpn_client_proto_list =
376 OPENSSL_malloc(s->ctx->alpn_client_proto_list_len);
377 if (s->alpn_client_proto_list == NULL)
378 goto err;
379 memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list,
380 s->ctx->alpn_client_proto_list_len);
381 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
382 }
Adam Langley95c29f32014-06-20 12:00:00 -0700383
384 s->verify_result=X509_V_OK;
385
386 s->method=ctx->method;
387
388 if (!s->method->ssl_new(s))
389 goto err;
390
391 s->references=1;
392 s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
393
394 SSL_clear(s);
395
396 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
397
398#ifndef OPENSSL_NO_PSK
Adam Langley0289c732014-06-20 12:00:00 -0700399 s->psk_identity_hint = NULL;
400 if (ctx->psk_identity_hint)
401 {
402 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
403 if (s->psk_identity_hint == NULL)
404 goto err;
405 }
Adam Langley95c29f32014-06-20 12:00:00 -0700406 s->psk_client_callback=ctx->psk_client_callback;
407 s->psk_server_callback=ctx->psk_server_callback;
408#endif
409
410 return(s);
411err:
412 if (s != NULL)
413 {
414 if (s->cert != NULL)
415 ssl_cert_free(s->cert);
416 if (s->ctx != NULL)
417 SSL_CTX_free(s->ctx); /* decrement reference count */
418 OPENSSL_free(s);
419 }
420 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
421 return(NULL);
422 }
423
424int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
425 unsigned int sid_ctx_len)
426 {
427 if(sid_ctx_len > sizeof ctx->sid_ctx)
428 {
429 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
430 return 0;
431 }
432 ctx->sid_ctx_length=sid_ctx_len;
433 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
434
435 return 1;
436 }
437
438int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
439 unsigned int sid_ctx_len)
440 {
441 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
442 {
443 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
444 return 0;
445 }
446 ssl->sid_ctx_length=sid_ctx_len;
447 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
448
449 return 1;
450 }
451
452int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
453 {
454 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
455 ctx->generate_session_id = cb;
456 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
457 return 1;
458 }
459
460int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
461 {
462 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
463 ssl->generate_session_id = cb;
464 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
465 return 1;
466 }
467
468int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
469 unsigned int id_len)
470 {
471 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
472 * we can "construct" a session to give us the desired check - ie. to
473 * find if there's a session in the hash table that would conflict with
474 * any new session built out of this id/id_len and the ssl_version in
475 * use by this SSL. */
476 SSL_SESSION r, *p;
477
478 if(id_len > sizeof r.session_id)
479 return 0;
480
481 r.ssl_version = ssl->version;
482 r.session_id_length = id_len;
483 memcpy(r.session_id, id, id_len);
484 /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
485 * callback is calling us to check the uniqueness of a shorter ID, it
486 * must be compared as a padded-out ID because that is what it will be
487 * converted to when the callback has finished choosing it. */
488 if((r.ssl_version == SSL2_VERSION) &&
489 (id_len < SSL2_SSL_SESSION_ID_LENGTH))
490 {
491 memset(r.session_id + id_len, 0,
492 SSL2_SSL_SESSION_ID_LENGTH - id_len);
493 r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
494 }
495
496 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
497 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
498 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
499 return (p != NULL);
500 }
501
502int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
503 {
504 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
505 }
506
507int SSL_set_purpose(SSL *s, int purpose)
508 {
509 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
510 }
511
512int SSL_CTX_set_trust(SSL_CTX *s, int trust)
513 {
514 return X509_VERIFY_PARAM_set_trust(s->param, trust);
515 }
516
517int SSL_set_trust(SSL *s, int trust)
518 {
519 return X509_VERIFY_PARAM_set_trust(s->param, trust);
520 }
521
522int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
523 {
524 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
525 }
526
527int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
528 {
529 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
530 }
531
Adam Langley858a88d2014-06-20 12:00:00 -0700532void ssl_cipher_preference_list_free(
533 struct ssl_cipher_preference_list_st *cipher_list)
534 {
535 sk_SSL_CIPHER_free(cipher_list->ciphers);
536 OPENSSL_free(cipher_list->in_group_flags);
537 OPENSSL_free(cipher_list);
538 }
539
540struct ssl_cipher_preference_list_st*
541ssl_cipher_preference_list_dup(
542 struct ssl_cipher_preference_list_st *cipher_list)
543 {
544 struct ssl_cipher_preference_list_st* ret = NULL;
545 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
546
547 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
548 if (!ret)
549 goto err;
550 ret->ciphers = NULL;
551 ret->in_group_flags = NULL;
552 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
553 if (!ret->ciphers)
554 goto err;
555 ret->in_group_flags = OPENSSL_malloc(n);
556 if (!ret->in_group_flags)
557 goto err;
558 memcpy(ret->in_group_flags, cipher_list->in_group_flags, n);
559 return ret;
560
561err:
562 if (ret->ciphers)
563 sk_SSL_CIPHER_free(ret->ciphers);
564 if (ret)
565 OPENSSL_free(ret);
566 return NULL;
567 }
568
569struct ssl_cipher_preference_list_st*
570ssl_cipher_preference_list_from_ciphers(STACK_OF(SSL_CIPHER) *ciphers)
571 {
572 struct ssl_cipher_preference_list_st* ret = NULL;
573 size_t n = sk_SSL_CIPHER_num(ciphers);
574
575 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
576 if (!ret)
577 goto err;
578 ret->ciphers = NULL;
579 ret->in_group_flags = NULL;
580 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
581 if (!ret->ciphers)
582 goto err;
583 ret->in_group_flags = OPENSSL_malloc(n);
584 if (!ret->in_group_flags)
585 goto err;
586 memset(ret->in_group_flags, 0, n);
587 return ret;
588
589err:
590 if (ret->ciphers)
591 sk_SSL_CIPHER_free(ret->ciphers);
592 if (ret)
593 OPENSSL_free(ret);
594 return NULL;
595 }
596
Adam Langley95c29f32014-06-20 12:00:00 -0700597X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
598 {
599 return ctx->param;
600 }
601
602X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
603 {
604 return ssl->param;
605 }
606
607void SSL_certs_clear(SSL *s)
608 {
609 ssl_cert_clear_certs(s->cert);
610 }
611
612void SSL_free(SSL *s)
613 {
614 int i;
615
616 if(s == NULL)
617 return;
618
619 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
620#ifdef REF_PRINT
621 REF_PRINT("SSL",s);
622#endif
623 if (i > 0) return;
624#ifdef REF_CHECK
625 if (i < 0)
626 {
627 fprintf(stderr,"SSL_free, bad reference count\n");
628 abort(); /* ok */
629 }
630#endif
631
632 if (s->param)
633 X509_VERIFY_PARAM_free(s->param);
634
635 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
636
637 if (s->bbio != NULL)
638 {
639 /* If the buffering BIO is in place, pop it off */
640 if (s->bbio == s->wbio)
641 {
642 s->wbio=BIO_pop(s->wbio);
643 }
644 BIO_free(s->bbio);
645 s->bbio=NULL;
646 }
647 if (s->rbio != NULL)
648 BIO_free_all(s->rbio);
649 if ((s->wbio != NULL) && (s->wbio != s->rbio))
650 BIO_free_all(s->wbio);
651
652 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
653
654 /* add extra stuff */
Adam Langley858a88d2014-06-20 12:00:00 -0700655 if (s->cipher_list != NULL)
656 ssl_cipher_preference_list_free(s->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -0700657 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
658
659 /* Make the next call work :-) */
660 if (s->session != NULL)
661 {
662 ssl_clear_bad_session(s);
663 SSL_SESSION_free(s->session);
664 }
665
666 ssl_clear_cipher_ctx(s);
667 ssl_clear_hash_ctx(&s->read_hash);
668 ssl_clear_hash_ctx(&s->write_hash);
669
670 if (s->cert != NULL) ssl_cert_free(s->cert);
671 /* Free up if allocated */
672
Adam Langley95c29f32014-06-20 12:00:00 -0700673 if (s->tlsext_hostname)
674 OPENSSL_free(s->tlsext_hostname);
675 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
676#ifndef OPENSSL_NO_EC
677 if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
678 if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
679#endif /* OPENSSL_NO_EC */
Adam Langley95c29f32014-06-20 12:00:00 -0700680 if (s->tlsext_ocsp_exts)
681 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
682 X509_EXTENSION_free);
683 /* TODO(fork): OCSP support */
684#if 0
685 if (s->tlsext_ocsp_ids)
686 sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);
687#endif
688 if (s->tlsext_ocsp_resp)
689 OPENSSL_free(s->tlsext_ocsp_resp);
690 if (s->alpn_client_proto_list)
691 OPENSSL_free(s->alpn_client_proto_list);
Adam Langley1258b6a2014-06-20 12:00:00 -0700692 if (s->tlsext_channel_id_private)
693 EVP_PKEY_free(s->tlsext_channel_id_private);
Adam Langley95c29f32014-06-20 12:00:00 -0700694
Adam Langley0289c732014-06-20 12:00:00 -0700695#ifndef OPENSSL_NO_PSK
696 if (s->psk_identity_hint)
697 OPENSSL_free(s->psk_identity_hint);
698#endif
699
Adam Langley95c29f32014-06-20 12:00:00 -0700700 if (s->client_CA != NULL)
701 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
702
703 if (s->method != NULL) s->method->ssl_free(s);
704
705 if (s->ctx) SSL_CTX_free(s->ctx);
706
David Benjamin6dbd73d2014-07-03 15:59:49 -0400707#if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -0700708 if (s->next_proto_negotiated)
709 OPENSSL_free(s->next_proto_negotiated);
710#endif
711
712 if (s->srtp_profiles)
713 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
714
Adam Langley95c29f32014-06-20 12:00:00 -0700715 OPENSSL_free(s);
716 }
717
718void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
719 {
720 /* If the output buffering BIO is still in place, remove it
721 */
722 if (s->bbio != NULL)
723 {
724 if (s->wbio == s->bbio)
725 {
726 s->wbio=s->wbio->next_bio;
727 s->bbio->next_bio=NULL;
728 }
729 }
730 if ((s->rbio != NULL) && (s->rbio != rbio))
731 BIO_free_all(s->rbio);
732 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
733 BIO_free_all(s->wbio);
734 s->rbio=rbio;
735 s->wbio=wbio;
736 }
737
738BIO *SSL_get_rbio(const SSL *s)
739 { return(s->rbio); }
740
741BIO *SSL_get_wbio(const SSL *s)
742 { return(s->wbio); }
743
744int SSL_get_fd(const SSL *s)
745 {
746 return(SSL_get_rfd(s));
747 }
748
749int SSL_get_rfd(const SSL *s)
750 {
751 int ret= -1;
752 BIO *b,*r;
753
754 b=SSL_get_rbio(s);
755 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
756 if (r != NULL)
757 BIO_get_fd(r,&ret);
758 return(ret);
759 }
760
761int SSL_get_wfd(const SSL *s)
762 {
763 int ret= -1;
764 BIO *b,*r;
765
766 b=SSL_get_wbio(s);
767 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
768 if (r != NULL)
769 BIO_get_fd(r,&ret);
770 return(ret);
771 }
772
773#ifndef OPENSSL_NO_SOCK
774int SSL_set_fd(SSL *s,int fd)
775 {
776 int ret=0;
777 BIO *bio=NULL;
778
779 bio=BIO_new(BIO_s_fd());
780
781 if (bio == NULL)
782 {
783 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
784 goto err;
785 }
786 BIO_set_fd(bio,fd,BIO_NOCLOSE);
787 SSL_set_bio(s,bio,bio);
788 ret=1;
789err:
790 return(ret);
791 }
792
793int SSL_set_wfd(SSL *s,int fd)
794 {
795 int ret=0;
796 BIO *bio=NULL;
797
798 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_FD)
799 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
800 {
801 bio=BIO_new(BIO_s_fd());
802
803 if (bio == NULL)
804 {
805 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
806 goto err;
807 }
808 BIO_set_fd(bio,fd,BIO_NOCLOSE);
809 SSL_set_bio(s,SSL_get_rbio(s),bio);
810 }
811 else
812 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
813 ret=1;
814err:
815 return(ret);
816 }
817
818int SSL_set_rfd(SSL *s,int fd)
819 {
820 int ret=0;
821 BIO *bio=NULL;
822
823 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_FD)
824 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
825 {
826 bio=BIO_new(BIO_s_fd());
827
828 if (bio == NULL)
829 {
830 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
831 goto err;
832 }
833 BIO_set_fd(bio,fd,BIO_NOCLOSE);
834 SSL_set_bio(s,bio,SSL_get_wbio(s));
835 }
836 else
837 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
838 ret=1;
839err:
840 return(ret);
841 }
842#endif
843
844
845/* return length of latest Finished message we sent, copy to 'buf' */
846size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
847 {
848 size_t ret = 0;
849
850 if (s->s3 != NULL)
851 {
852 ret = s->s3->tmp.finish_md_len;
853 if (count > ret)
854 count = ret;
855 memcpy(buf, s->s3->tmp.finish_md, count);
856 }
857 return ret;
858 }
859
860/* return length of latest Finished message we expected, copy to 'buf' */
861size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
862 {
863 size_t ret = 0;
864
865 if (s->s3 != NULL)
866 {
867 ret = s->s3->tmp.peer_finish_md_len;
868 if (count > ret)
869 count = ret;
870 memcpy(buf, s->s3->tmp.peer_finish_md, count);
871 }
872 return ret;
873 }
874
875
876int SSL_get_verify_mode(const SSL *s)
877 {
878 return(s->verify_mode);
879 }
880
881int SSL_get_verify_depth(const SSL *s)
882 {
883 return X509_VERIFY_PARAM_get_depth(s->param);
884 }
885
886int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
887 {
888 return(s->verify_callback);
889 }
890
891int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
892 {
893 return(ctx->verify_mode);
894 }
895
896int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
897 {
898 return X509_VERIFY_PARAM_get_depth(ctx->param);
899 }
900
901int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
902 {
903 return(ctx->default_verify_callback);
904 }
905
906void SSL_set_verify(SSL *s,int mode,
907 int (*callback)(int ok,X509_STORE_CTX *ctx))
908 {
909 s->verify_mode=mode;
910 if (callback != NULL)
911 s->verify_callback=callback;
912 }
913
914void SSL_set_verify_depth(SSL *s,int depth)
915 {
916 X509_VERIFY_PARAM_set_depth(s->param, depth);
917 }
918
919void SSL_set_read_ahead(SSL *s,int yes)
920 {
921 s->read_ahead=yes;
922 }
923
924int SSL_get_read_ahead(const SSL *s)
925 {
926 return(s->read_ahead);
927 }
928
929int SSL_pending(const SSL *s)
930 {
931 /* SSL_pending cannot work properly if read-ahead is enabled
932 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
933 * and it is impossible to fix since SSL_pending cannot report
934 * errors that may be observed while scanning the new data.
935 * (Note that SSL_pending() is often used as a boolean value,
936 * so we'd better not return -1.)
937 */
938 return(s->method->ssl_pending(s));
939 }
940
941X509 *SSL_get_peer_certificate(const SSL *s)
942 {
943 X509 *r;
944
945 if ((s == NULL) || (s->session == NULL))
946 r=NULL;
947 else
948 r=s->session->peer;
949
950 if (r == NULL) return(r);
951
952 CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509);
953
954 return(r);
955 }
956
957STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
958 {
959 STACK_OF(X509) *r;
960
961 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
962 r=NULL;
963 else
964 r=s->session->sess_cert->cert_chain;
965
966 /* If we are a client, cert_chain includes the peer's own
967 * certificate; if we are a server, it does not. */
968
969 return(r);
970 }
971
Adam Langley95c29f32014-06-20 12:00:00 -0700972/* Fix this so it checks all the valid key/cert options */
973int SSL_CTX_check_private_key(const SSL_CTX *ctx)
974 {
975 if ( (ctx == NULL) ||
976 (ctx->cert == NULL) ||
977 (ctx->cert->key->x509 == NULL))
978 {
979 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
980 return(0);
981 }
982 if (ctx->cert->key->privatekey == NULL)
983 {
984 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
985 return(0);
986 }
987 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
988 }
989
990/* Fix this function so that it takes an optional type parameter */
991int SSL_check_private_key(const SSL *ssl)
992 {
993 if (ssl == NULL)
994 {
995 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
996 return(0);
997 }
998 if (ssl->cert == NULL)
999 {
1000 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
1001 return 0;
1002 }
1003 if (ssl->cert->key->x509 == NULL)
1004 {
1005 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
1006 return(0);
1007 }
1008 if (ssl->cert->key->privatekey == NULL)
1009 {
1010 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
1011 return(0);
1012 }
1013 return(X509_check_private_key(ssl->cert->key->x509,
1014 ssl->cert->key->privatekey));
1015 }
1016
1017int SSL_accept(SSL *s)
1018 {
1019 if (s->handshake_func == 0)
1020 /* Not properly initialized yet */
1021 SSL_set_accept_state(s);
1022
1023 return(s->method->ssl_accept(s));
1024 }
1025
1026int SSL_connect(SSL *s)
1027 {
1028 if (s->handshake_func == 0)
1029 /* Not properly initialized yet */
1030 SSL_set_connect_state(s);
1031
1032 return(s->method->ssl_connect(s));
1033 }
1034
1035long SSL_get_default_timeout(const SSL *s)
1036 {
1037 return(s->method->get_timeout());
1038 }
1039
1040int SSL_read(SSL *s,void *buf,int num)
1041 {
1042 if (s->handshake_func == 0)
1043 {
1044 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
1045 return -1;
1046 }
1047
1048 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1049 {
1050 s->rwstate=SSL_NOTHING;
1051 return(0);
1052 }
1053 return(s->method->ssl_read(s,buf,num));
1054 }
1055
1056int SSL_peek(SSL *s,void *buf,int num)
1057 {
1058 if (s->handshake_func == 0)
1059 {
1060 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
1061 return -1;
1062 }
1063
1064 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1065 {
1066 return(0);
1067 }
1068 return(s->method->ssl_peek(s,buf,num));
1069 }
1070
1071int SSL_write(SSL *s,const void *buf,int num)
1072 {
1073 if (s->handshake_func == 0)
1074 {
1075 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
1076 return -1;
1077 }
1078
1079 if (s->shutdown & SSL_SENT_SHUTDOWN)
1080 {
1081 s->rwstate=SSL_NOTHING;
1082 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
1083 return(-1);
1084 }
1085 return(s->method->ssl_write(s,buf,num));
1086 }
1087
1088int SSL_shutdown(SSL *s)
1089 {
1090 /* Note that this function behaves differently from what one might
1091 * expect. Return values are 0 for no success (yet),
1092 * 1 for success; but calling it once is usually not enough,
1093 * even if blocking I/O is used (see ssl3_shutdown).
1094 */
1095
1096 if (s->handshake_func == 0)
1097 {
1098 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1099 return -1;
1100 }
1101
1102 if ((s != NULL) && !SSL_in_init(s))
1103 return(s->method->ssl_shutdown(s));
1104 else
1105 return(1);
1106 }
1107
1108int SSL_renegotiate(SSL *s)
1109 {
1110 if (s->renegotiate == 0)
1111 s->renegotiate=1;
1112
1113 s->new_session=1;
1114
1115 return(s->method->ssl_renegotiate(s));
1116 }
1117
1118int SSL_renegotiate_abbreviated(SSL *s)
1119 {
1120 if (s->renegotiate == 0)
1121 s->renegotiate=1;
1122
1123 s->new_session=0;
1124
1125 return(s->method->ssl_renegotiate(s));
1126 }
1127
1128int SSL_renegotiate_pending(SSL *s)
1129 {
1130 /* becomes true when negotiation is requested;
1131 * false again once a handshake has finished */
1132 return (s->renegotiate != 0);
1133 }
1134
1135long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1136 {
1137 long l;
1138
1139 switch (cmd)
1140 {
1141 case SSL_CTRL_GET_READ_AHEAD:
1142 return(s->read_ahead);
1143 case SSL_CTRL_SET_READ_AHEAD:
1144 l=s->read_ahead;
1145 s->read_ahead=larg;
1146 return(l);
1147
1148 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1149 s->msg_callback_arg = parg;
1150 return 1;
1151
1152 case SSL_CTRL_OPTIONS:
1153 return(s->options|=larg);
1154 case SSL_CTRL_CLEAR_OPTIONS:
1155 return(s->options&=~larg);
1156 case SSL_CTRL_MODE:
1157 return(s->mode|=larg);
1158 case SSL_CTRL_CLEAR_MODE:
1159 return(s->mode &=~larg);
1160 case SSL_CTRL_GET_MAX_CERT_LIST:
1161 return(s->max_cert_list);
1162 case SSL_CTRL_SET_MAX_CERT_LIST:
1163 l=s->max_cert_list;
1164 s->max_cert_list=larg;
1165 return(l);
1166 case SSL_CTRL_SET_MTU:
1167#ifndef OPENSSL_NO_DTLS1
1168 if (larg < (long)dtls1_min_mtu())
1169 return 0;
1170#endif
1171
1172 if (SSL_IS_DTLS(s))
1173 {
1174 s->d1->mtu = larg;
1175 return larg;
1176 }
1177 return 0;
1178 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1179 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1180 return 0;
1181 s->max_send_fragment = larg;
1182 return 1;
1183 case SSL_CTRL_GET_RI_SUPPORT:
1184 if (s->s3)
1185 return s->s3->send_connection_binding;
1186 else return 0;
1187 case SSL_CTRL_CERT_FLAGS:
1188 return(s->cert->cert_flags|=larg);
1189 case SSL_CTRL_CLEAR_CERT_FLAGS:
1190 return(s->cert->cert_flags &=~larg);
1191
1192 case SSL_CTRL_GET_RAW_CIPHERLIST:
1193 if (parg)
1194 {
1195 if (s->cert->ciphers_raw == NULL)
1196 return 0;
1197 *(unsigned char **)parg = s->cert->ciphers_raw;
1198 return (int)s->cert->ciphers_rawlen;
1199 }
1200 else
1201 return ssl_put_cipher_by_char(s,NULL,NULL);
1202 default:
1203 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1204 }
1205 }
1206
1207long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1208 {
1209 switch(cmd)
1210 {
1211 case SSL_CTRL_SET_MSG_CALLBACK:
1212 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1213 return 1;
1214
1215 default:
1216 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1217 }
1218 }
1219
1220LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1221 {
1222 return ctx->sessions;
1223 }
1224
1225long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1226 {
1227 long l;
1228
1229 switch (cmd)
1230 {
1231 case SSL_CTRL_GET_READ_AHEAD:
1232 return(ctx->read_ahead);
1233 case SSL_CTRL_SET_READ_AHEAD:
1234 l=ctx->read_ahead;
1235 ctx->read_ahead=larg;
1236 return(l);
1237
1238 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1239 ctx->msg_callback_arg = parg;
1240 return 1;
1241
1242 case SSL_CTRL_GET_MAX_CERT_LIST:
1243 return(ctx->max_cert_list);
1244 case SSL_CTRL_SET_MAX_CERT_LIST:
1245 l=ctx->max_cert_list;
1246 ctx->max_cert_list=larg;
1247 return(l);
1248
1249 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1250 l=ctx->session_cache_size;
1251 ctx->session_cache_size=larg;
1252 return(l);
1253 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1254 return(ctx->session_cache_size);
1255 case SSL_CTRL_SET_SESS_CACHE_MODE:
1256 l=ctx->session_cache_mode;
1257 ctx->session_cache_mode=larg;
1258 return(l);
1259 case SSL_CTRL_GET_SESS_CACHE_MODE:
1260 return(ctx->session_cache_mode);
1261
1262 case SSL_CTRL_SESS_NUMBER:
1263 return(lh_SSL_SESSION_num_items(ctx->sessions));
1264 case SSL_CTRL_SESS_CONNECT:
1265 return(ctx->stats.sess_connect);
1266 case SSL_CTRL_SESS_CONNECT_GOOD:
1267 return(ctx->stats.sess_connect_good);
1268 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1269 return(ctx->stats.sess_connect_renegotiate);
1270 case SSL_CTRL_SESS_ACCEPT:
1271 return(ctx->stats.sess_accept);
1272 case SSL_CTRL_SESS_ACCEPT_GOOD:
1273 return(ctx->stats.sess_accept_good);
1274 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1275 return(ctx->stats.sess_accept_renegotiate);
1276 case SSL_CTRL_SESS_HIT:
1277 return(ctx->stats.sess_hit);
1278 case SSL_CTRL_SESS_CB_HIT:
1279 return(ctx->stats.sess_cb_hit);
1280 case SSL_CTRL_SESS_MISSES:
1281 return(ctx->stats.sess_miss);
1282 case SSL_CTRL_SESS_TIMEOUTS:
1283 return(ctx->stats.sess_timeout);
1284 case SSL_CTRL_SESS_CACHE_FULL:
1285 return(ctx->stats.sess_cache_full);
1286 case SSL_CTRL_OPTIONS:
1287 return(ctx->options|=larg);
1288 case SSL_CTRL_CLEAR_OPTIONS:
1289 return(ctx->options&=~larg);
1290 case SSL_CTRL_MODE:
1291 return(ctx->mode|=larg);
1292 case SSL_CTRL_CLEAR_MODE:
1293 return(ctx->mode&=~larg);
1294 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1295 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1296 return 0;
1297 ctx->max_send_fragment = larg;
1298 return 1;
1299 case SSL_CTRL_CERT_FLAGS:
1300 return(ctx->cert->cert_flags|=larg);
1301 case SSL_CTRL_CLEAR_CERT_FLAGS:
1302 return(ctx->cert->cert_flags &=~larg);
1303 default:
1304 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1305 }
1306 }
1307
1308long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1309 {
1310 switch(cmd)
1311 {
1312 case SSL_CTRL_SET_MSG_CALLBACK:
1313 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1314 return 1;
1315
1316 default:
1317 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1318 }
1319 }
1320
1321int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1322 {
1323 long l;
1324 const SSL_CIPHER *a = in_a;
1325 const SSL_CIPHER *b = in_b;
1326 const long a_id = a->id;
1327 const long b_id = b->id;
1328
1329 l = a_id - b_id;
1330 if (l == 0L)
1331 return(0);
1332 else
1333 return((l > 0)?1:-1);
1334 }
1335
1336int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1337 {
1338 long l;
1339 const long a_id = (*ap)->id;
1340 const long b_id = (*bp)->id;
1341
1342 l = a_id - b_id;
1343 if (l == 0)
1344 return(0);
1345 else
1346 return((l > 0)?1:-1);
1347 }
1348
1349/** return a STACK of the ciphers available for the SSL and in order of
1350 * preference */
1351STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1352 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001353 if (s == NULL)
1354 return NULL;
1355
1356 if (s->cipher_list != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001357 {
Adam Langley858a88d2014-06-20 12:00:00 -07001358 return(s->cipher_list->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -07001359 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001360
1361 if (s->version >= TLS1_1_VERSION)
1362 {
1363 if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001364 return s->ctx->cipher_list_tls11->ciphers;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001365 }
1366
1367 if ((s->ctx != NULL) &&
1368 (s->ctx->cipher_list != NULL))
1369 {
Adam Langley858a88d2014-06-20 12:00:00 -07001370 return(s->ctx->cipher_list->ciphers);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001371 }
1372
Adam Langley95c29f32014-06-20 12:00:00 -07001373 return(NULL);
1374 }
1375
1376/** return a STACK of the ciphers available for the SSL and in order of
1377 * algorithm id */
1378STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1379 {
1380 if (s != NULL)
1381 {
1382 if (s->cipher_list_by_id != NULL)
1383 {
1384 return(s->cipher_list_by_id);
1385 }
1386 else if ((s->ctx != NULL) &&
1387 (s->ctx->cipher_list_by_id != NULL))
1388 {
1389 return(s->ctx->cipher_list_by_id);
1390 }
1391 }
1392 return(NULL);
1393 }
1394
1395/** The old interface to get the same thing as SSL_get_ciphers() */
1396const char *SSL_get_cipher_list(const SSL *s,int n)
1397 {
1398 SSL_CIPHER *c;
1399 STACK_OF(SSL_CIPHER) *sk;
1400
1401 if (s == NULL) return(NULL);
1402 sk=SSL_get_ciphers(s);
1403 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
1404 return(NULL);
1405 c=sk_SSL_CIPHER_value(sk,n);
1406 if (c == NULL) return(NULL);
1407 return(c->name);
1408 }
1409
1410/** specify the ciphers to be used by default by the SSL_CTX */
1411int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1412 {
1413 STACK_OF(SSL_CIPHER) *sk;
1414
1415 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1416 &ctx->cipher_list_by_id,str, ctx->cert);
1417 /* ssl_create_cipher_list may return an empty stack if it
1418 * was unable to find a cipher matching the given rule string
1419 * (for example if the rule string specifies a cipher which
1420 * has been disabled). This is not an error as far as
1421 * ssl_create_cipher_list is concerned, and hence
1422 * ctx->cipher_list and ctx->cipher_list_by_id has been
1423 * updated. */
1424 if (sk == NULL)
1425 return 0;
1426 else if (sk_SSL_CIPHER_num(sk) == 0)
1427 {
1428 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1429 return 0;
1430 }
1431 return 1;
1432 }
1433
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001434int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str)
1435 {
1436 STACK_OF(SSL_CIPHER) *sk;
1437
1438 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert);
1439 if (sk == NULL)
1440 return 0;
1441 else if (sk_SSL_CIPHER_num(sk) == 0)
1442 {
1443 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH);
1444 return 0;
1445 }
1446 return 1;
1447 }
1448
Adam Langley95c29f32014-06-20 12:00:00 -07001449/** specify the ciphers to be used by the SSL */
1450int SSL_set_cipher_list(SSL *s,const char *str)
1451 {
1452 STACK_OF(SSL_CIPHER) *sk;
1453
1454 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1455 &s->cipher_list_by_id,str, s->cert);
1456 /* see comment in SSL_CTX_set_cipher_list */
1457 if (sk == NULL)
1458 return 0;
1459 else if (sk_SSL_CIPHER_num(sk) == 0)
1460 {
1461 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1462 return 0;
1463 }
1464 return 1;
1465 }
1466
1467/* works well for SSLv2, not so good for SSLv3 */
1468char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
1469 {
1470 char *p;
1471 STACK_OF(SSL_CIPHER) *sk;
1472 SSL_CIPHER *c;
1473 int i;
1474
1475 if ((s->session == NULL) || (s->session->ciphers == NULL) ||
1476 (len < 2))
1477 return(NULL);
1478
1479 p=buf;
1480 sk=s->session->ciphers;
Adam Langley04124042014-06-20 12:00:00 -07001481
1482 if (sk_SSL_CIPHER_num(sk) == 0)
1483 return NULL;
1484
Adam Langley95c29f32014-06-20 12:00:00 -07001485 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1486 {
1487 int n;
1488
1489 c=sk_SSL_CIPHER_value(sk,i);
1490 n=strlen(c->name);
1491 if (n+1 > len)
1492 {
1493 if (p != buf)
1494 --p;
1495 *p='\0';
1496 return buf;
1497 }
1498 strcpy(p,c->name);
1499 p+=n;
1500 *(p++)=':';
1501 len-=n+1;
1502 }
1503 p[-1]='\0';
1504 return(buf);
1505 }
1506
1507int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p,
1508 int (*put_cb)(const SSL_CIPHER *, unsigned char *))
1509 {
1510 int i,j=0;
1511 SSL_CIPHER *c;
1512 CERT *ct = s->cert;
1513 unsigned char *q;
1514 int no_scsv = s->renegotiate;
1515 /* Set disabled masks for this session */
1516 ssl_set_client_disabled(s);
1517
1518 if (sk == NULL) return(0);
1519 q=p;
1520
Adam Langleyac61fa32014-06-23 12:03:11 -07001521 if (put_cb == NULL)
1522 put_cb = s->method->put_cipher_by_char;
1523
Adam Langley95c29f32014-06-20 12:00:00 -07001524 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1525 {
1526 c=sk_SSL_CIPHER_value(sk,i);
1527 /* Skip disabled ciphers */
1528 if (c->algorithm_ssl & ct->mask_ssl ||
1529 c->algorithm_mkey & ct->mask_k ||
1530 c->algorithm_auth & ct->mask_a)
1531 continue;
1532#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
1533 if (c->id == SSL3_CK_SCSV)
1534 {
1535 if (no_scsv)
1536 continue;
1537 else
1538 no_scsv = 1;
1539 }
1540#endif
Adam Langleyac61fa32014-06-23 12:03:11 -07001541 j = put_cb(c, p);
1542 p += j;
Adam Langley95c29f32014-06-20 12:00:00 -07001543 }
1544 /* If p == q, no ciphers and caller indicates an error. Otherwise
1545 * add SCSV if not renegotiating.
1546 */
Adam Langleyac61fa32014-06-23 12:03:11 -07001547 if (p != q)
Adam Langley95c29f32014-06-20 12:00:00 -07001548 {
Adam Langleyac61fa32014-06-23 12:03:11 -07001549 if (!no_scsv)
Adam Langley95c29f32014-06-20 12:00:00 -07001550 {
Adam Langleyac61fa32014-06-23 12:03:11 -07001551 static SSL_CIPHER scsv =
1552 {
1553 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1554 };
1555 j = put_cb(&scsv, p);
1556 p += j;
Adam Langley95c29f32014-06-20 12:00:00 -07001557#ifdef OPENSSL_RI_DEBUG
Adam Langleyac61fa32014-06-23 12:03:11 -07001558 fprintf(stderr, "SCSV sent by client\n");
Adam Langley95c29f32014-06-20 12:00:00 -07001559#endif
Adam Langleyac61fa32014-06-23 12:03:11 -07001560 }
1561 if (s->fallback_scsv)
1562 {
1563 static SSL_CIPHER fallback_scsv =
1564 {
1565 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1566 };
1567 j = put_cb(&fallback_scsv, p);
1568 p += j;
1569 }
Adam Langley95c29f32014-06-20 12:00:00 -07001570 }
1571
1572 return(p-q);
1573 }
1574
David Benjamin22f9bcc2014-07-13 12:29:21 -04001575STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const uint8_t *p,int num,
Adam Langley95c29f32014-06-20 12:00:00 -07001576 STACK_OF(SSL_CIPHER) **skp)
1577 {
1578 const SSL_CIPHER *c;
1579 STACK_OF(SSL_CIPHER) *sk;
1580 int i,n;
1581 if (s->s3)
1582 s->s3->send_connection_binding = 0;
1583
1584 n=ssl_put_cipher_by_char(s,NULL,NULL);
1585 if ((num%n) != 0)
1586 {
1587 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1588 return(NULL);
1589 }
1590 if ((skp == NULL) || (*skp == NULL))
1591 sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
1592 else
1593 {
1594 sk= *skp;
1595 sk_SSL_CIPHER_zero(sk);
1596 }
1597
1598 if (s->cert->ciphers_raw)
1599 OPENSSL_free(s->cert->ciphers_raw);
1600 s->cert->ciphers_raw = BUF_memdup(p, num);
1601 if (s->cert->ciphers_raw == NULL)
1602 {
1603 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1604 goto err;
1605 }
1606 s->cert->ciphers_rawlen = (size_t)num;
1607
1608 for (i=0; i<num; i+=n)
1609 {
1610 /* Check for SCSV */
1611 if (s->s3 && (n != 3 || !p[0]) &&
1612 (p[n-2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
1613 (p[n-1] == (SSL3_CK_SCSV & 0xff)))
1614 {
1615 /* SCSV fatal if renegotiating */
1616 if (s->renegotiate)
1617 {
1618 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1619 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1620 goto err;
1621 }
1622 s->s3->send_connection_binding = 1;
1623 p += n;
1624#ifdef OPENSSL_RI_DEBUG
1625 fprintf(stderr, "SCSV received by server\n");
1626#endif
1627 continue;
1628 }
1629
Adam Langley29707792014-06-20 12:00:00 -07001630 /* Check for FALLBACK_SCSV */
1631 if (s->s3 && n == 2 &&
1632 (p[0] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) &&
1633 (p[1] == (SSL3_CK_FALLBACK_SCSV & 0xff)) &&
1634 s->version < ssl_get_max_version(s))
1635 {
1636 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1637 ssl3_send_alert(s,SSL3_AL_FATAL,SSL3_AD_INAPPROPRIATE_FALLBACK);
1638 goto err;
1639 }
1640
Adam Langley95c29f32014-06-20 12:00:00 -07001641 c=ssl_get_cipher_by_char(s,p);
1642 p+=n;
1643 if (c != NULL)
1644 {
1645 if (!sk_SSL_CIPHER_push(sk,c))
1646 {
1647 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1648 goto err;
1649 }
1650 }
1651 }
1652
1653 if (skp != NULL)
1654 *skp=sk;
1655 return(sk);
1656err:
1657 if ((skp == NULL) || (*skp == NULL))
1658 sk_SSL_CIPHER_free(sk);
1659 return(NULL);
1660 }
1661
1662
Adam Langley95c29f32014-06-20 12:00:00 -07001663/** return a servername extension value if provided in Client Hello, or NULL.
1664 * So far, only host_name types are defined (RFC 3546).
1665 */
1666
1667const char *SSL_get_servername(const SSL *s, const int type)
1668 {
1669 if (type != TLSEXT_NAMETYPE_host_name)
1670 return NULL;
1671
1672 return s->session && !s->tlsext_hostname ?
1673 s->session->tlsext_hostname :
1674 s->tlsext_hostname;
1675 }
1676
1677int SSL_get_servername_type(const SSL *s)
1678 {
1679 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1680 return TLSEXT_NAMETYPE_host_name;
1681 return -1;
1682 }
1683
1684/* SSL_select_next_proto implements the standard protocol selection. It is
1685 * expected that this function is called from the callback set by
1686 * SSL_CTX_set_next_proto_select_cb.
1687 *
1688 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1689 * strings. The length byte itself is not included in the length. A byte
1690 * string of length 0 is invalid. No byte string may be truncated.
1691 *
1692 * The current, but experimental algorithm for selecting the protocol is:
1693 *
1694 * 1) If the server doesn't support NPN then this is indicated to the
1695 * callback. In this case, the client application has to abort the connection
1696 * or have a default application level protocol.
1697 *
1698 * 2) If the server supports NPN, but advertises an empty list then the
1699 * client selects the first protcol in its list, but indicates via the
1700 * API that this fallback case was enacted.
1701 *
1702 * 3) Otherwise, the client finds the first protocol in the server's list
1703 * that it supports and selects this protocol. This is because it's
1704 * assumed that the server has better information about which protocol
1705 * a client should use.
1706 *
1707 * 4) If the client doesn't support any of the server's advertised
1708 * protocols, then this is treated the same as case 2.
1709 *
1710 * It returns either
1711 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1712 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1713 */
1714int 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)
1715 {
1716 unsigned int i, j;
1717 const unsigned char *result;
1718 int status = OPENSSL_NPN_UNSUPPORTED;
1719
1720 /* For each protocol in server preference order, see if we support it. */
1721 for (i = 0; i < server_len; )
1722 {
1723 for (j = 0; j < client_len; )
1724 {
1725 if (server[i] == client[j] &&
1726 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1727 {
1728 /* We found a match */
1729 result = &server[i];
1730 status = OPENSSL_NPN_NEGOTIATED;
1731 goto found;
1732 }
1733 j += client[j];
1734 j++;
1735 }
1736 i += server[i];
1737 i++;
1738 }
1739
1740 /* There's no overlap between our protocols and the server's list. */
1741 result = client;
1742 status = OPENSSL_NPN_NO_OVERLAP;
1743
1744 found:
1745 *out = (unsigned char *) result + 1;
1746 *outlen = result[0];
1747 return status;
1748 }
1749
1750# ifndef OPENSSL_NO_NEXTPROTONEG
1751/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1752 * requested protocol for this connection and returns 0. If the client didn't
1753 * request any protocol, then *data is set to NULL.
1754 *
1755 * Note that the client can request any protocol it chooses. The value returned
1756 * from this function need not be a member of the list of supported protocols
1757 * provided by the callback.
1758 */
David Benjaminced551f2014-07-11 23:16:16 -04001759void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, unsigned *len)
Adam Langley95c29f32014-06-20 12:00:00 -07001760 {
1761 *data = s->next_proto_negotiated;
1762 if (!*data) {
1763 *len = 0;
1764 } else {
1765 *len = s->next_proto_negotiated_len;
1766 }
1767}
1768
1769/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1770 * TLS server needs a list of supported protocols for Next Protocol
1771 * Negotiation. The returned list must be in wire format. The list is returned
1772 * by setting |out| to point to it and |outlen| to its length. This memory will
1773 * not be modified, but one should assume that the SSL* keeps a reference to
1774 * it.
1775 *
1776 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1777 * such extension will be included in the ServerHello. */
1778void 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)
1779 {
1780 ctx->next_protos_advertised_cb = cb;
1781 ctx->next_protos_advertised_cb_arg = arg;
1782 }
1783
1784/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1785 * client needs to select a protocol from the server's provided list. |out|
1786 * must be set to point to the selected protocol (which may be within |in|).
1787 * The length of the protocol name must be written into |outlen|. The server's
1788 * advertised protocols are provided in |in| and |inlen|. The callback can
1789 * assume that |in| is syntactically valid.
1790 *
1791 * The client must select a protocol. It is fatal to the connection if this
1792 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1793 */
1794void 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)
1795 {
1796 ctx->next_proto_select_cb = cb;
1797 ctx->next_proto_select_cb_arg = arg;
1798 }
1799# endif
1800
Adam Langley95c29f32014-06-20 12:00:00 -07001801/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1802 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1803 * length-prefixed strings).
1804 *
1805 * Returns 0 on success. */
1806int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1807 unsigned protos_len)
1808 {
1809 if (ctx->alpn_client_proto_list)
1810 OPENSSL_free(ctx->alpn_client_proto_list);
1811
1812 ctx->alpn_client_proto_list = OPENSSL_malloc(protos_len);
1813 if (!ctx->alpn_client_proto_list)
1814 return 1;
1815 memcpy(ctx->alpn_client_proto_list, protos, protos_len);
1816 ctx->alpn_client_proto_list_len = protos_len;
1817
1818 return 0;
1819 }
1820
1821/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1822 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1823 * length-prefixed strings).
1824 *
1825 * Returns 0 on success. */
1826int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1827 unsigned protos_len)
1828 {
1829 if (ssl->alpn_client_proto_list)
1830 OPENSSL_free(ssl->alpn_client_proto_list);
1831
1832 ssl->alpn_client_proto_list = OPENSSL_malloc(protos_len);
1833 if (!ssl->alpn_client_proto_list)
1834 return 1;
1835 memcpy(ssl->alpn_client_proto_list, protos, protos_len);
1836 ssl->alpn_client_proto_list_len = protos_len;
1837
1838 return 0;
1839 }
1840
1841/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1842 * during ClientHello processing in order to select an ALPN protocol from the
1843 * client's list of offered protocols. */
1844void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1845 int (*cb) (SSL *ssl,
1846 const unsigned char **out,
1847 unsigned char *outlen,
1848 const unsigned char *in,
1849 unsigned int inlen,
1850 void *arg),
1851 void *arg)
1852 {
1853 ctx->alpn_select_cb = cb;
1854 ctx->alpn_select_cb_arg = arg;
1855 }
1856
1857/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1858 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1859 * including the leading length-prefix byte). If the server didn't respond with
1860 * a negotiated protocol then |*len| will be zero. */
1861void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1862 unsigned *len)
1863 {
1864 *data = NULL;
1865 if (ssl->s3)
1866 *data = ssl->s3->alpn_selected;
1867 if (*data == NULL)
1868 *len = 0;
1869 else
1870 *len = ssl->s3->alpn_selected_len;
1871 }
Adam Langley95c29f32014-06-20 12:00:00 -07001872
1873int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1874 const char *label, size_t llen, const unsigned char *p, size_t plen,
1875 int use_context)
1876 {
1877 if (s->version < TLS1_VERSION)
1878 return -1;
1879
1880 return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1881 llen, p, plen,
1882 use_context);
1883 }
1884
1885static uint32_t ssl_session_hash(const SSL_SESSION *a)
1886 {
1887 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1888 ((uint32_t) a->session_id[1] << 8) ||
1889 ((uint32_t) a->session_id[2] << 16) ||
1890 ((uint32_t) a->session_id[3] << 24);
1891
1892 return hash;
1893 }
1894
1895/* NB: If this function (or indeed the hash function which uses a sort of
1896 * coarser function than this one) is changed, ensure
1897 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1898 * able to construct an SSL_SESSION that will collide with any existing session
1899 * with a matching session ID. */
1900static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1901 {
1902 if (a->ssl_version != b->ssl_version)
1903 return(1);
1904 if (a->session_id_length != b->session_id_length)
1905 return(1);
1906 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1907 }
1908
1909SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1910 {
1911 SSL_CTX *ret=NULL;
1912
1913 if (meth == NULL)
1914 {
1915 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1916 return(NULL);
1917 }
1918
Adam Langley95c29f32014-06-20 12:00:00 -07001919 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1920 {
1921 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1922 goto err;
1923 }
1924 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1925 if (ret == NULL)
1926 goto err;
1927
1928 memset(ret,0,sizeof(SSL_CTX));
1929
1930 ret->method=meth;
1931
1932 ret->cert_store=NULL;
1933 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1934 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1935 ret->session_cache_head=NULL;
1936 ret->session_cache_tail=NULL;
1937
1938 /* We take the system default */
1939 ret->session_timeout=meth->get_timeout();
1940
1941 ret->new_session_cb=0;
1942 ret->remove_session_cb=0;
1943 ret->get_session_cb=0;
1944 ret->generate_session_id=0;
1945
1946 memset((char *)&ret->stats,0,sizeof(ret->stats));
1947
1948 ret->references=1;
1949 ret->quiet_shutdown=0;
1950
1951/* ret->cipher=NULL;*/
1952/* ret->s2->challenge=NULL;
1953 ret->master_key=NULL;
1954 ret->key_arg=NULL;
1955 ret->s2->conn_id=NULL; */
1956
1957 ret->info_callback=NULL;
1958
1959 ret->app_verify_callback=0;
1960 ret->app_verify_arg=NULL;
1961
1962 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1963 ret->read_ahead=0;
1964 ret->msg_callback=0;
1965 ret->msg_callback_arg=NULL;
1966 ret->verify_mode=SSL_VERIFY_NONE;
1967#if 0
1968 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1969#endif
1970 ret->sid_ctx_length=0;
1971 ret->default_verify_callback=NULL;
1972 if ((ret->cert=ssl_cert_new()) == NULL)
1973 goto err;
1974
1975 ret->default_passwd_callback=0;
1976 ret->default_passwd_callback_userdata=NULL;
1977 ret->client_cert_cb=0;
1978 ret->app_gen_cookie_cb=0;
1979 ret->app_verify_cookie_cb=0;
1980
1981 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1982 if (ret->sessions == NULL) goto err;
1983 ret->cert_store=X509_STORE_new();
1984 if (ret->cert_store == NULL) goto err;
1985
1986 ssl_create_cipher_list(ret->method,
1987 &ret->cipher_list,&ret->cipher_list_by_id,
1988 meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST, ret->cert);
1989 if (ret->cipher_list == NULL
Adam Langley858a88d2014-06-20 12:00:00 -07001990 || sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001991 {
1992 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1993 goto err2;
1994 }
1995
1996 ret->param = X509_VERIFY_PARAM_new();
1997 if (!ret->param)
1998 goto err;
1999
2000 ret->rsa_md5 = EVP_md5();
2001 ret->md5 = EVP_md5();
2002 ret->sha1 = EVP_sha1();
2003
2004 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
2005 goto err;
2006
2007 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
2008
2009 ret->extra_certs=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002010
2011 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
2012
Adam Langley95c29f32014-06-20 12:00:00 -07002013 ret->tlsext_servername_callback = 0;
2014 ret->tlsext_servername_arg = NULL;
2015 /* Setup RFC4507 ticket keys */
2016 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
2017 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
2018 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
2019 ret->options |= SSL_OP_NO_TICKET;
2020
2021 ret->tlsext_status_cb = 0;
2022 ret->tlsext_status_arg = NULL;
2023
2024# ifndef OPENSSL_NO_NEXTPROTONEG
2025 ret->next_protos_advertised_cb = 0;
2026 ret->next_proto_select_cb = 0;
2027# endif
Adam Langley95c29f32014-06-20 12:00:00 -07002028#ifndef OPENSSL_NO_PSK
2029 ret->psk_identity_hint=NULL;
2030 ret->psk_client_callback=NULL;
2031 ret->psk_server_callback=NULL;
2032#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002033#ifndef OPENSSL_NO_BUF_FREELISTS
2034 ret->freelist_max_len = SSL_MAX_BUF_FREELIST_LEN_DEFAULT;
2035 ret->rbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));
2036 if (!ret->rbuf_freelist)
2037 goto err;
2038 ret->rbuf_freelist->chunklen = 0;
2039 ret->rbuf_freelist->len = 0;
2040 ret->rbuf_freelist->head = NULL;
2041 ret->wbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));
2042 if (!ret->wbuf_freelist)
2043 {
2044 OPENSSL_free(ret->rbuf_freelist);
2045 goto err;
2046 }
2047 ret->wbuf_freelist->chunklen = 0;
2048 ret->wbuf_freelist->len = 0;
2049 ret->wbuf_freelist->head = NULL;
2050#endif
2051#ifndef OPENSSL_NO_ENGINE
2052 ret->client_cert_engine = NULL;
2053#ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
2054#define eng_strx(x) #x
2055#define eng_str(x) eng_strx(x)
2056 /* Use specific client engine automatically... ignore errors */
2057 {
2058 ENGINE *eng;
2059 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
2060 if (!eng)
2061 {
2062 ERR_clear_error();
2063 ENGINE_load_builtin_engines();
2064 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
2065 }
2066 if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
2067 ERR_clear_error();
2068 }
2069#endif
2070#endif
2071 /* Default is to connect to non-RI servers. When RI is more widely
2072 * deployed might change this.
2073 */
2074 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
2075
2076 return(ret);
2077err:
2078 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
2079err2:
2080 if (ret != NULL) SSL_CTX_free(ret);
2081 return(NULL);
2082 }
2083
Adam Langley95c29f32014-06-20 12:00:00 -07002084#ifndef OPENSSL_NO_BUF_FREELISTS
2085static void
2086ssl_buf_freelist_free(SSL3_BUF_FREELIST *list)
2087 {
2088 SSL3_BUF_FREELIST_ENTRY *ent, *next;
2089 for (ent = list->head; ent; ent = next)
2090 {
2091 next = ent->next;
2092 OPENSSL_free(ent);
2093 }
2094 OPENSSL_free(list);
2095 }
2096#endif
2097
2098void SSL_CTX_free(SSL_CTX *a)
2099 {
2100 int i;
2101
2102 if (a == NULL) return;
2103
2104 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
2105#ifdef REF_PRINT
2106 REF_PRINT("SSL_CTX",a);
2107#endif
2108 if (i > 0) return;
2109#ifdef REF_CHECK
2110 if (i < 0)
2111 {
2112 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
2113 abort(); /* ok */
2114 }
2115#endif
2116
2117 if (a->param)
2118 X509_VERIFY_PARAM_free(a->param);
2119
2120 /*
2121 * Free internal session cache. However: the remove_cb() may reference
2122 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2123 * after the sessions were flushed.
2124 * As the ex_data handling routines might also touch the session cache,
2125 * the most secure solution seems to be: empty (flush) the cache, then
2126 * free ex_data, then finally free the cache.
2127 * (See ticket [openssl.org #212].)
2128 */
2129 if (a->sessions != NULL)
2130 SSL_CTX_flush_sessions(a,0);
2131
2132 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
2133
2134 if (a->sessions != NULL)
2135 lh_SSL_SESSION_free(a->sessions);
2136
2137 if (a->cert_store != NULL)
2138 X509_STORE_free(a->cert_store);
2139 if (a->cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002140 ssl_cipher_preference_list_free(a->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002141 if (a->cipher_list_by_id != NULL)
2142 sk_SSL_CIPHER_free(a->cipher_list_by_id);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07002143 if (a->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002144 ssl_cipher_preference_list_free(a->cipher_list_tls11);
Adam Langley95c29f32014-06-20 12:00:00 -07002145 if (a->cert != NULL)
2146 ssl_cert_free(a->cert);
2147 if (a->client_CA != NULL)
2148 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2149 if (a->extra_certs != NULL)
2150 sk_X509_pop_free(a->extra_certs,X509_free);
Adam Langley95c29f32014-06-20 12:00:00 -07002151
2152 if (a->srtp_profiles)
2153 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2154
2155#ifndef OPENSSL_NO_PSK
2156 if (a->psk_identity_hint)
2157 OPENSSL_free(a->psk_identity_hint);
2158#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002159
2160 /* TODO(fork): remove. */
2161#if 0
2162#ifndef OPENSSL_NO_ENGINE
2163 if (a->client_cert_engine)
2164 ENGINE_finish(a->client_cert_engine);
2165#endif
2166#endif
2167
2168#ifndef OPENSSL_NO_BUF_FREELISTS
2169 if (a->wbuf_freelist)
2170 ssl_buf_freelist_free(a->wbuf_freelist);
2171 if (a->rbuf_freelist)
2172 ssl_buf_freelist_free(a->rbuf_freelist);
2173#endif
Adam Langley95c29f32014-06-20 12:00:00 -07002174# ifndef OPENSSL_NO_EC
2175 if (a->tlsext_ecpointformatlist)
2176 OPENSSL_free(a->tlsext_ecpointformatlist);
2177 if (a->tlsext_ellipticcurvelist)
2178 OPENSSL_free(a->tlsext_ellipticcurvelist);
2179# endif /* OPENSSL_NO_EC */
2180 if (a->alpn_client_proto_list != NULL)
2181 OPENSSL_free(a->alpn_client_proto_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002182
Adam Langley1258b6a2014-06-20 12:00:00 -07002183 if (a->tlsext_channel_id_private)
2184 EVP_PKEY_free(a->tlsext_channel_id_private);
Adam Langley1258b6a2014-06-20 12:00:00 -07002185
Adam Langley95c29f32014-06-20 12:00:00 -07002186 OPENSSL_free(a);
2187 }
2188
2189void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2190 {
2191 ctx->default_passwd_callback=cb;
2192 }
2193
2194void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2195 {
2196 ctx->default_passwd_callback_userdata=u;
2197 }
2198
2199void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2200 {
2201 ctx->app_verify_callback=cb;
2202 ctx->app_verify_arg=arg;
2203 }
2204
2205void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2206 {
2207 ctx->verify_mode=mode;
2208 ctx->default_verify_callback=cb;
2209 }
2210
2211void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2212 {
2213 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2214 }
2215
2216void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2217 {
2218 ssl_cert_set_cert_cb(c->cert, cb, arg);
2219 }
2220
2221void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2222 {
2223 ssl_cert_set_cert_cb(s->cert, cb, arg);
2224 }
2225
2226void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
2227 {
2228 CERT_PKEY *cpk;
2229 int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
2230 int rsa_enc_export,dh_rsa_export,dh_dsa_export;
2231 int rsa_tmp_export,dh_tmp_export,kl;
2232 unsigned long mask_k,mask_a,emask_k,emask_a;
2233#ifndef OPENSSL_NO_ECDSA
2234 int have_ecc_cert, ecdsa_ok, ecc_pkey_size;
2235#endif
2236#ifndef OPENSSL_NO_ECDH
2237 int have_ecdh_tmp, ecdh_ok;
2238#endif
2239#ifndef OPENSSL_NO_EC
2240 X509 *x = NULL;
2241 EVP_PKEY *ecc_pkey = NULL;
2242 int signature_nid = 0, pk_nid = 0, md_nid = 0;
2243#endif
2244 if (c == NULL) return;
2245
2246 kl=SSL_C_EXPORT_PKEYLENGTH(cipher);
2247
Adam Langley95c29f32014-06-20 12:00:00 -07002248 rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
2249 rsa_tmp_export=(c->rsa_tmp_cb != NULL ||
2250 (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl));
Adam Langley95c29f32014-06-20 12:00:00 -07002251#ifndef OPENSSL_NO_DH
2252 dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
2253 dh_tmp_export=(c->dh_tmp_cb != NULL ||
2254 (dh_tmp && DH_size(c->dh_tmp)*8 <= kl));
2255#else
2256 dh_tmp=dh_tmp_export=0;
2257#endif
2258
2259#ifndef OPENSSL_NO_ECDH
2260 have_ecdh_tmp=(c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
2261#endif
2262 cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
2263 rsa_enc= cpk->valid_flags & CERT_PKEY_VALID;
2264 rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
2265 cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
2266 rsa_sign= cpk->valid_flags & CERT_PKEY_SIGN;
2267 cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
2268 dsa_sign= cpk->valid_flags & CERT_PKEY_SIGN;
2269 cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
2270 dh_rsa= cpk->valid_flags & CERT_PKEY_VALID;
2271 dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
2272 cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
2273/* FIX THIS EAY EAY EAY */
2274 dh_dsa= cpk->valid_flags & CERT_PKEY_VALID;
2275 dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
2276 cpk= &(c->pkeys[SSL_PKEY_ECC]);
2277#ifndef OPENSSL_NO_EC
2278 have_ecc_cert= cpk->valid_flags & CERT_PKEY_VALID;
2279#endif
2280 mask_k=0;
2281 mask_a=0;
2282 emask_k=0;
2283 emask_a=0;
2284
2285
2286
2287#ifdef CIPHER_DEBUG
2288 printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
2289 rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
2290 rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
2291#endif
2292
2293 cpk = &(c->pkeys[SSL_PKEY_GOST01]);
2294 if (cpk->x509 != NULL && cpk->privatekey !=NULL) {
2295 mask_k |= SSL_kGOST;
2296 mask_a |= SSL_aGOST01;
2297 }
2298 cpk = &(c->pkeys[SSL_PKEY_GOST94]);
2299 if (cpk->x509 != NULL && cpk->privatekey !=NULL) {
2300 mask_k |= SSL_kGOST;
2301 mask_a |= SSL_aGOST94;
2302 }
2303
2304 if (rsa_enc || (rsa_tmp && rsa_sign))
2305 mask_k|=SSL_kRSA;
2306 if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc)))
2307 emask_k|=SSL_kRSA;
2308
2309#if 0
2310 /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
2311 if ( (dh_tmp || dh_rsa || dh_dsa) &&
2312 (rsa_enc || rsa_sign || dsa_sign))
2313 mask_k|=SSL_kEDH;
2314 if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
2315 (rsa_enc || rsa_sign || dsa_sign))
2316 emask_k|=SSL_kEDH;
2317#endif
2318
2319 if (dh_tmp_export)
2320 emask_k|=SSL_kEDH;
2321
2322 if (dh_tmp)
2323 mask_k|=SSL_kEDH;
2324
2325 if (dh_rsa) mask_k|=SSL_kDHr;
2326 if (dh_rsa_export) emask_k|=SSL_kDHr;
2327
2328 if (dh_dsa) mask_k|=SSL_kDHd;
2329 if (dh_dsa_export) emask_k|=SSL_kDHd;
2330
2331 if (emask_k & (SSL_kDHr|SSL_kDHd))
2332 mask_a |= SSL_aDH;
2333
2334 if (rsa_enc || rsa_sign)
2335 {
2336 mask_a|=SSL_aRSA;
2337 emask_a|=SSL_aRSA;
2338 }
2339
2340 if (dsa_sign)
2341 {
2342 mask_a|=SSL_aDSS;
2343 emask_a|=SSL_aDSS;
2344 }
2345
2346 mask_a|=SSL_aNULL;
2347 emask_a|=SSL_aNULL;
2348
2349 /* An ECC certificate may be usable for ECDH and/or
2350 * ECDSA cipher suites depending on the key usage extension.
2351 */
2352#ifndef OPENSSL_NO_EC
2353 if (have_ecc_cert)
2354 {
2355 cpk = &c->pkeys[SSL_PKEY_ECC];
2356 x = cpk->x509;
2357 /* This call populates extension flags (ex_flags) */
2358 X509_check_purpose(x, -1, 0);
2359 ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2360 (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1;
2361 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2362 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
2363 if (!(cpk->valid_flags & CERT_PKEY_SIGN))
2364 ecdsa_ok = 0;
2365 ecc_pkey = X509_get_pubkey(x);
2366 ecc_pkey_size = (ecc_pkey != NULL) ?
2367 EVP_PKEY_bits(ecc_pkey) : 0;
2368 EVP_PKEY_free(ecc_pkey);
2369 if ((x->sig_alg) && (x->sig_alg->algorithm))
2370 {
2371 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2372 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2373 }
2374#ifndef OPENSSL_NO_ECDH
2375 if (ecdh_ok)
2376 {
2377
2378 if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa)
2379 {
2380 mask_k|=SSL_kECDHr;
2381 mask_a|=SSL_aECDH;
2382 if (ecc_pkey_size <= 163)
2383 {
2384 emask_k|=SSL_kECDHr;
2385 emask_a|=SSL_aECDH;
2386 }
2387 }
2388
2389 if (pk_nid == NID_X9_62_id_ecPublicKey)
2390 {
2391 mask_k|=SSL_kECDHe;
2392 mask_a|=SSL_aECDH;
2393 if (ecc_pkey_size <= 163)
2394 {
2395 emask_k|=SSL_kECDHe;
2396 emask_a|=SSL_aECDH;
2397 }
2398 }
2399 }
2400#endif
2401#ifndef OPENSSL_NO_ECDSA
2402 if (ecdsa_ok)
2403 {
2404 mask_a|=SSL_aECDSA;
2405 emask_a|=SSL_aECDSA;
2406 }
2407#endif
2408 }
2409#endif
2410
2411#ifndef OPENSSL_NO_ECDH
2412 if (have_ecdh_tmp)
2413 {
2414 mask_k|=SSL_kEECDH;
2415 emask_k|=SSL_kEECDH;
2416 }
2417#endif
2418
2419#ifndef OPENSSL_NO_PSK
2420 mask_k |= SSL_kPSK;
2421 mask_a |= SSL_aPSK;
2422 emask_k |= SSL_kPSK;
2423 emask_a |= SSL_aPSK;
2424#endif
2425
2426 c->mask_k=mask_k;
2427 c->mask_a=mask_a;
2428 c->export_mask_k=emask_k;
2429 c->export_mask_a=emask_a;
2430 c->valid=1;
2431 }
2432
2433/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2434#define ku_reject(x, usage) \
2435 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2436
2437#ifndef OPENSSL_NO_EC
2438
2439int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2440 {
2441 unsigned long alg_k, alg_a;
2442 EVP_PKEY *pkey = NULL;
2443 int keysize = 0;
2444 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2445 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2446
2447 alg_k = cs->algorithm_mkey;
2448 alg_a = cs->algorithm_auth;
2449
2450 if (SSL_C_IS_EXPORT(cs))
2451 {
2452 /* ECDH key length in export ciphers must be <= 163 bits */
2453 pkey = X509_get_pubkey(x);
2454 if (pkey == NULL) return 0;
2455 keysize = EVP_PKEY_bits(pkey);
2456 EVP_PKEY_free(pkey);
2457 if (keysize > 163) return 0;
2458 }
2459
2460 /* This call populates the ex_flags field correctly */
2461 X509_check_purpose(x, -1, 0);
2462 if ((x->sig_alg) && (x->sig_alg->algorithm))
2463 {
2464 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2465 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2466 }
2467 if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr)
2468 {
2469 /* key usage, if present, must allow key agreement */
2470 if (ku_reject(x, X509v3_KU_KEY_AGREEMENT))
2471 {
2472 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT);
2473 return 0;
2474 }
2475 if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < TLS1_2_VERSION)
2476 {
2477 /* signature alg must be ECDSA */
2478 if (pk_nid != NID_X9_62_id_ecPublicKey)
2479 {
2480 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE);
2481 return 0;
2482 }
2483 }
2484 if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < TLS1_2_VERSION)
2485 {
2486 /* signature alg must be RSA */
2487
2488 if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa)
2489 {
2490 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE);
2491 return 0;
2492 }
2493 }
2494 }
2495 if (alg_a & SSL_aECDSA)
2496 {
2497 /* key usage, if present, must allow signing */
2498 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2499 {
2500 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2501 return 0;
2502 }
2503 }
2504
2505 return 1; /* all checks are ok */
2506 }
2507
2508#endif
2509
2510static int ssl_get_server_cert_index(const SSL *s)
2511 {
2512 int idx;
2513 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2514 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2515 idx = SSL_PKEY_RSA_SIGN;
2516 if (idx == -1)
2517 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2518 return idx;
2519 }
2520
2521CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2522 {
2523 CERT *c;
2524 int i;
2525
2526 c = s->cert;
2527 ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
2528
2529#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2530 /* Broken protocol test: return last used certificate: which may
2531 * mismatch the one expected.
2532 */
2533 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2534 return c->key;
2535#endif
2536
2537 i = ssl_get_server_cert_index(s);
2538
2539 /* This may or may not be an error. */
2540 if (i < 0)
2541 return NULL;
2542
2543 /* May be NULL. */
2544 return &c->pkeys[i];
2545 }
2546
2547EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd)
2548 {
2549 unsigned long alg_a;
2550 CERT *c;
2551 int idx = -1;
2552
2553 alg_a = cipher->algorithm_auth;
2554 c=s->cert;
2555
2556#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2557 /* Broken protocol test: use last key: which may
2558 * mismatch the one expected.
2559 */
2560 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2561 idx = c->key - c->pkeys;
2562 else
2563#endif
2564
2565 if ((alg_a & SSL_aDSS) &&
2566 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
2567 idx = SSL_PKEY_DSA_SIGN;
2568 else if (alg_a & SSL_aRSA)
2569 {
2570 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2571 idx = SSL_PKEY_RSA_SIGN;
2572 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2573 idx = SSL_PKEY_RSA_ENC;
2574 }
2575 else if ((alg_a & SSL_aECDSA) &&
2576 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2577 idx = SSL_PKEY_ECC;
2578 if (idx == -1)
2579 {
2580 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2581 return(NULL);
2582 }
2583 if (pmd)
2584 *pmd = c->pkeys[idx].digest;
2585 return c->pkeys[idx].privatekey;
2586 }
2587
Adam Langley95c29f32014-06-20 12:00:00 -07002588void ssl_update_cache(SSL *s,int mode)
2589 {
2590 int i;
2591
2592 /* If the session_id_length is 0, we are not supposed to cache it,
2593 * and it would be rather hard to do anyway :-) */
2594 if (s->session->session_id_length == 0) return;
2595
2596 i=s->session_ctx->session_cache_mode;
2597 if ((i & mode) && (!s->hit)
2598 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
2599 || SSL_CTX_add_session(s->session_ctx,s->session))
2600 && (s->session_ctx->new_session_cb != NULL))
2601 {
2602 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
2603 if (!s->session_ctx->new_session_cb(s,s->session))
2604 SSL_SESSION_free(s->session);
2605 }
2606
2607 /* auto flush every 255 connections */
2608 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2609 ((i & mode) == mode))
2610 {
2611 if ( (((mode & SSL_SESS_CACHE_CLIENT)
2612 ?s->session_ctx->stats.sess_connect_good
2613 :s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff)
2614 {
2615 SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL));
2616 }
2617 }
2618 }
2619
2620const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
2621 {
2622 return ctx->method;
2623 }
2624
2625const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2626 {
2627 return(s->method);
2628 }
2629
2630int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2631 {
2632 int conn= -1;
2633 int ret=1;
2634
2635 if (s->method != meth)
2636 {
2637 if (s->handshake_func != NULL)
2638 conn=(s->handshake_func == s->method->ssl_connect);
2639
2640 if (s->method->version == meth->version)
2641 s->method=meth;
2642 else
2643 {
2644 s->method->ssl_free(s);
2645 s->method=meth;
2646 ret=s->method->ssl_new(s);
2647 }
2648
2649 if (conn == 1)
2650 s->handshake_func=meth->ssl_connect;
2651 else if (conn == 0)
2652 s->handshake_func=meth->ssl_accept;
2653 }
2654 return(ret);
2655 }
2656
2657int SSL_get_error(const SSL *s,int i)
2658 {
2659 int reason;
2660 unsigned long l;
2661 BIO *bio;
2662
2663 if (i > 0) return(SSL_ERROR_NONE);
2664
2665 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2666 * etc, where we do encode the error */
2667 if ((l=ERR_peek_error()) != 0)
2668 {
2669 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2670 return(SSL_ERROR_SYSCALL);
2671 else
2672 return(SSL_ERROR_SSL);
2673 }
2674
Adam Langleyb2ce0582014-06-20 12:00:00 -07002675 if ((i < 0) && SSL_want_session(s))
2676 return(SSL_ERROR_PENDING_SESSION);
2677
Adam Langleydc9b1412014-06-20 12:00:00 -07002678 if ((i < 0) && SSL_want_certificate(s))
2679 return(SSL_ERROR_PENDING_CERTIFICATE);
2680
Adam Langley95c29f32014-06-20 12:00:00 -07002681 if ((i < 0) && SSL_want_read(s))
2682 {
2683 bio=SSL_get_rbio(s);
2684 if (BIO_should_read(bio))
2685 return(SSL_ERROR_WANT_READ);
2686 else if (BIO_should_write(bio))
2687 /* This one doesn't make too much sense ... We never try
2688 * to write to the rbio, and an application program where
2689 * rbio and wbio are separate couldn't even know what it
2690 * should wait for.
2691 * However if we ever set s->rwstate incorrectly
2692 * (so that we have SSL_want_read(s) instead of
2693 * SSL_want_write(s)) and rbio and wbio *are* the same,
2694 * this test works around that bug; so it might be safer
2695 * to keep it. */
2696 return(SSL_ERROR_WANT_WRITE);
2697 else if (BIO_should_io_special(bio))
2698 {
2699 reason=BIO_get_retry_reason(bio);
2700 if (reason == BIO_RR_CONNECT)
2701 return(SSL_ERROR_WANT_CONNECT);
2702 else if (reason == BIO_RR_ACCEPT)
2703 return(SSL_ERROR_WANT_ACCEPT);
2704 else
2705 return(SSL_ERROR_SYSCALL); /* unknown */
2706 }
2707 }
2708
2709 if ((i < 0) && SSL_want_write(s))
2710 {
2711 bio=SSL_get_wbio(s);
2712 if (BIO_should_write(bio))
2713 return(SSL_ERROR_WANT_WRITE);
2714 else if (BIO_should_read(bio))
2715 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2716 return(SSL_ERROR_WANT_READ);
2717 else if (BIO_should_io_special(bio))
2718 {
2719 reason=BIO_get_retry_reason(bio);
2720 if (reason == BIO_RR_CONNECT)
2721 return(SSL_ERROR_WANT_CONNECT);
2722 else if (reason == BIO_RR_ACCEPT)
2723 return(SSL_ERROR_WANT_ACCEPT);
2724 else
2725 return(SSL_ERROR_SYSCALL);
2726 }
2727 }
2728 if ((i < 0) && SSL_want_x509_lookup(s))
2729 {
2730 return(SSL_ERROR_WANT_X509_LOOKUP);
2731 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002732 if ((i < 0) && SSL_want_channel_id_lookup(s))
2733 {
2734 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2735 }
Adam Langley95c29f32014-06-20 12:00:00 -07002736
2737 if (i == 0)
2738 {
2739 if (s->version == SSL2_VERSION)
2740 {
2741 /* assume it is the socket being closed */
2742 return(SSL_ERROR_ZERO_RETURN);
2743 }
2744 else
2745 {
2746 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2747 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2748 return(SSL_ERROR_ZERO_RETURN);
2749 }
2750 }
2751 return(SSL_ERROR_SYSCALL);
2752 }
2753
2754int SSL_do_handshake(SSL *s)
2755 {
2756 int ret=1;
2757
2758 if (s->handshake_func == NULL)
2759 {
2760 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2761 return(-1);
2762 }
2763
2764 s->method->ssl_renegotiate_check(s);
2765
2766 if (SSL_in_init(s) || SSL_in_before(s))
2767 {
2768 ret=s->handshake_func(s);
2769 }
2770 return(ret);
2771 }
2772
2773/* For the next 2 functions, SSL_clear() sets shutdown and so
2774 * one of these calls will reset it */
2775void SSL_set_accept_state(SSL *s)
2776 {
2777 s->server=1;
2778 s->shutdown=0;
2779 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2780 s->handshake_func=s->method->ssl_accept;
2781 /* clear the current cipher */
2782 ssl_clear_cipher_ctx(s);
2783 ssl_clear_hash_ctx(&s->read_hash);
2784 ssl_clear_hash_ctx(&s->write_hash);
2785 }
2786
2787void SSL_set_connect_state(SSL *s)
2788 {
2789 s->server=0;
2790 s->shutdown=0;
2791 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2792 s->handshake_func=s->method->ssl_connect;
2793 /* clear the current cipher */
2794 ssl_clear_cipher_ctx(s);
2795 ssl_clear_hash_ctx(&s->read_hash);
2796 ssl_clear_hash_ctx(&s->write_hash);
2797 }
2798
2799int ssl_undefined_function(SSL *s)
2800 {
2801 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2802 return(0);
2803 }
2804
2805int ssl_undefined_void_function(void)
2806 {
2807 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2808 return(0);
2809 }
2810
2811int ssl_undefined_const_function(const SSL *s)
2812 {
2813 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2814 return(0);
2815 }
2816
2817SSL_METHOD *ssl_bad_method(int ver)
2818 {
2819 OPENSSL_PUT_ERROR(SSL, ssl_bad_method, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2820 return(NULL);
2821 }
2822
2823const char *SSL_get_version(const SSL *s)
2824 {
2825 if (s->version == TLS1_2_VERSION)
2826 return("TLSv1.2");
2827 else if (s->version == TLS1_1_VERSION)
2828 return("TLSv1.1");
2829 else if (s->version == TLS1_VERSION)
2830 return("TLSv1");
2831 else if (s->version == SSL3_VERSION)
2832 return("SSLv3");
2833 else if (s->version == SSL2_VERSION)
2834 return("SSLv2");
2835 else
2836 return("unknown");
2837 }
2838
2839void ssl_clear_cipher_ctx(SSL *s)
2840 {
2841 if (s->enc_read_ctx != NULL)
2842 {
2843 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2844 OPENSSL_free(s->enc_read_ctx);
2845 s->enc_read_ctx=NULL;
2846 }
2847 if (s->enc_write_ctx != NULL)
2848 {
2849 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2850 OPENSSL_free(s->enc_write_ctx);
2851 s->enc_write_ctx=NULL;
2852 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002853 if (s->aead_read_ctx != NULL)
2854 {
2855 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2856 OPENSSL_free(s->aead_read_ctx);
2857 s->aead_read_ctx = NULL;
2858 }
2859 if (s->aead_write_ctx != NULL)
2860 {
2861 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2862 OPENSSL_free(s->aead_write_ctx);
2863 s->aead_write_ctx = NULL;
2864 }
Adam Langley95c29f32014-06-20 12:00:00 -07002865 }
2866
2867X509 *SSL_get_certificate(const SSL *s)
2868 {
2869 if (s->cert != NULL)
2870 return(s->cert->key->x509);
2871 else
2872 return(NULL);
2873 }
2874
2875EVP_PKEY *SSL_get_privatekey(const SSL *s)
2876 {
2877 if (s->cert != NULL)
2878 return(s->cert->key->privatekey);
2879 else
2880 return(NULL);
2881 }
2882
2883X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2884 {
2885 if (ctx->cert != NULL)
2886 return ctx->cert->key->x509;
2887 else
2888 return NULL;
2889 }
2890
2891EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2892 {
2893 if (ctx->cert != NULL)
2894 return ctx->cert->key->privatekey;
2895 else
2896 return NULL ;
2897 }
2898
2899const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2900 {
2901 if ((s->session != NULL) && (s->session->cipher != NULL))
2902 return(s->session->cipher);
2903 return(NULL);
2904 }
2905const void *SSL_get_current_compression(SSL *s)
2906 {
2907 return NULL;
2908 }
2909const void *SSL_get_current_expansion(SSL *s)
2910 {
2911 return NULL;
2912 }
2913
2914int ssl_init_wbio_buffer(SSL *s,int push)
2915 {
2916 BIO *bbio;
2917
2918 if (s->bbio == NULL)
2919 {
2920 bbio=BIO_new(BIO_f_buffer());
2921 if (bbio == NULL) return(0);
2922 s->bbio=bbio;
2923 }
2924 else
2925 {
2926 bbio=s->bbio;
2927 if (s->bbio == s->wbio)
2928 s->wbio=BIO_pop(s->wbio);
2929 }
2930 (void)BIO_reset(bbio);
2931/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2932 if (!BIO_set_read_buffer_size(bbio,1))
2933 {
2934 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2935 return(0);
2936 }
2937 if (push)
2938 {
2939 if (s->wbio != bbio)
2940 s->wbio=BIO_push(bbio,s->wbio);
2941 }
2942 else
2943 {
2944 if (s->wbio == bbio)
2945 s->wbio=BIO_pop(bbio);
2946 }
2947 return(1);
2948 }
2949
2950void ssl_free_wbio_buffer(SSL *s)
2951 {
2952 if (s->bbio == NULL) return;
2953
2954 if (s->bbio == s->wbio)
2955 {
2956 /* remove buffering */
2957 s->wbio=BIO_pop(s->wbio);
2958#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2959 assert(s->wbio != NULL);
2960#endif
2961 }
2962 BIO_free(s->bbio);
2963 s->bbio=NULL;
2964 }
2965
2966void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2967 {
2968 ctx->quiet_shutdown=mode;
2969 }
2970
2971int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2972 {
2973 return(ctx->quiet_shutdown);
2974 }
2975
2976void SSL_set_quiet_shutdown(SSL *s,int mode)
2977 {
2978 s->quiet_shutdown=mode;
2979 }
2980
2981int SSL_get_quiet_shutdown(const SSL *s)
2982 {
2983 return(s->quiet_shutdown);
2984 }
2985
2986void SSL_set_shutdown(SSL *s,int mode)
2987 {
2988 s->shutdown=mode;
2989 }
2990
2991int SSL_get_shutdown(const SSL *s)
2992 {
2993 return(s->shutdown);
2994 }
2995
2996int SSL_version(const SSL *s)
2997 {
2998 return(s->version);
2999 }
3000
3001SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
3002 {
3003 return(ssl->ctx);
3004 }
3005
3006SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
3007 {
3008 if (ssl->ctx == ctx)
3009 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07003010 if (ctx == NULL)
3011 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07003012 if (ssl->cert != NULL)
3013 ssl_cert_free(ssl->cert);
3014 ssl->cert = ssl_cert_dup(ctx->cert);
3015 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
3016 if (ssl->ctx != NULL)
3017 SSL_CTX_free(ssl->ctx); /* decrement reference count */
3018 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07003019
3020 ssl->sid_ctx_length = ctx->sid_ctx_length;
3021 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
3022 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
3023
Adam Langley95c29f32014-06-20 12:00:00 -07003024 return(ssl->ctx);
3025 }
3026
3027#ifndef OPENSSL_NO_STDIO
3028int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
3029 {
3030 return(X509_STORE_set_default_paths(ctx->cert_store));
3031 }
3032
3033int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
3034 const char *CApath)
3035 {
3036 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
3037 }
3038#endif
3039
3040void SSL_set_info_callback(SSL *ssl,
3041 void (*cb)(const SSL *ssl,int type,int val))
3042 {
3043 ssl->info_callback=cb;
3044 }
3045
3046/* One compiler (Diab DCC) doesn't like argument names in returned
3047 function pointer. */
3048void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
3049 {
3050 return ssl->info_callback;
3051 }
3052
3053int SSL_state(const SSL *ssl)
3054 {
3055 return(ssl->state);
3056 }
3057
3058void SSL_set_state(SSL *ssl, int state)
3059 {
3060 ssl->state = state;
3061 }
3062
3063void SSL_set_verify_result(SSL *ssl,long arg)
3064 {
3065 ssl->verify_result=arg;
3066 }
3067
3068long SSL_get_verify_result(const SSL *ssl)
3069 {
3070 return(ssl->verify_result);
3071 }
3072
3073int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
3074 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
3075 {
3076 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
3077 new_func, dup_func, free_func);
3078 }
3079
3080int SSL_set_ex_data(SSL *s,int idx,void *arg)
3081 {
3082 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
3083 }
3084
3085void *SSL_get_ex_data(const SSL *s,int idx)
3086 {
3087 return(CRYPTO_get_ex_data(&s->ex_data,idx));
3088 }
3089
3090int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
3091 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
3092 {
3093 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
3094 new_func, dup_func, free_func);
3095 }
3096
3097int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
3098 {
3099 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
3100 }
3101
3102void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
3103 {
3104 return(CRYPTO_get_ex_data(&s->ex_data,idx));
3105 }
3106
3107int ssl_ok(SSL *s)
3108 {
3109 return(1);
3110 }
3111
3112X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
3113 {
3114 return(ctx->cert_store);
3115 }
3116
3117void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
3118 {
3119 if (ctx->cert_store != NULL)
3120 X509_STORE_free(ctx->cert_store);
3121 ctx->cert_store=store;
3122 }
3123
3124int SSL_want(const SSL *s)
3125 {
3126 return(s->rwstate);
3127 }
3128
3129/*!
3130 * \brief Set the callback for generating temporary RSA keys.
3131 * \param ctx the SSL context.
3132 * \param cb the callback
3133 */
3134
Adam Langley95c29f32014-06-20 12:00:00 -07003135void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
3136 int is_export,
3137 int keylength))
3138 {
3139 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3140 }
3141
3142void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
3143 int is_export,
3144 int keylength))
3145 {
3146 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3147 }
Adam Langley95c29f32014-06-20 12:00:00 -07003148
3149#ifdef DOXYGEN
3150/*!
3151 * \brief The RSA temporary key callback function.
3152 * \param ssl the SSL session.
3153 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
3154 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
3155 * of the required key in bits.
3156 * \return the temporary RSA key.
3157 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
3158 */
3159
3160RSA *cb(SSL *ssl,int is_export,int keylength)
3161 {}
3162#endif
3163
3164/*!
3165 * \brief Set the callback for generating temporary DH keys.
3166 * \param ctx the SSL context.
3167 * \param dh the callback
3168 */
3169
3170#ifndef OPENSSL_NO_DH
3171void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
3172 int keylength))
3173 {
3174 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3175 }
3176
3177void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
3178 int keylength))
3179 {
3180 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3181 }
3182#endif
3183
3184#ifndef OPENSSL_NO_ECDH
3185void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
3186 int keylength))
3187 {
3188 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
3189 }
3190
3191void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
3192 int keylength))
3193 {
3194 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
3195 }
3196#endif
3197
3198#ifndef OPENSSL_NO_PSK
3199int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
3200 {
3201 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
3202 {
3203 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
3204 return 0;
3205 }
3206 if (ctx->psk_identity_hint != NULL)
3207 OPENSSL_free(ctx->psk_identity_hint);
3208 if (identity_hint != NULL)
3209 {
3210 ctx->psk_identity_hint = BUF_strdup(identity_hint);
3211 if (ctx->psk_identity_hint == NULL)
3212 return 0;
3213 }
3214 else
3215 ctx->psk_identity_hint = NULL;
3216 return 1;
3217 }
3218
3219int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
3220 {
3221 if (s == NULL)
3222 return 0;
3223
Adam Langley95c29f32014-06-20 12:00:00 -07003224 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
3225 {
3226 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
3227 return 0;
3228 }
Adam Langley0289c732014-06-20 12:00:00 -07003229
3230 /* Clear hint in SSL and associated SSL_SESSION (if any). */
3231 if (s->psk_identity_hint != NULL)
3232 {
3233 OPENSSL_free(s->psk_identity_hint);
3234 s->psk_identity_hint = NULL;
3235 }
3236 if (s->session != NULL && s->session->psk_identity_hint != NULL)
3237 {
Adam Langley95c29f32014-06-20 12:00:00 -07003238 OPENSSL_free(s->session->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -07003239 s->session->psk_identity_hint = NULL;
3240 }
3241
Adam Langley95c29f32014-06-20 12:00:00 -07003242 if (identity_hint != NULL)
3243 {
Adam Langley0289c732014-06-20 12:00:00 -07003244 /* The hint is stored in SSL and SSL_SESSION with the one in
3245 * SSL_SESSION taking precedence. Thus, if SSL_SESSION is avaiable,
3246 * we store the hint there, otherwise we store it in SSL. */
3247 if (s->session != NULL)
3248 {
3249 s->session->psk_identity_hint = BUF_strdup(identity_hint);
3250 if (s->session->psk_identity_hint == NULL)
3251 return 0;
3252 }
3253 else
3254 {
3255 s->psk_identity_hint = BUF_strdup(identity_hint);
3256 if (s->psk_identity_hint == NULL)
3257 return 0;
3258 }
Adam Langley95c29f32014-06-20 12:00:00 -07003259 }
Adam Langley95c29f32014-06-20 12:00:00 -07003260 return 1;
3261 }
3262
3263const char *SSL_get_psk_identity_hint(const SSL *s)
3264 {
Adam Langley0289c732014-06-20 12:00:00 -07003265 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07003266 return NULL;
Adam Langley0289c732014-06-20 12:00:00 -07003267 /* The hint is stored in SSL and SSL_SESSION with the one in SSL_SESSION
3268 * taking precedence. */
3269 if (s->session != NULL)
3270 return(s->session->psk_identity_hint);
3271 return(s->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07003272 }
3273
3274const char *SSL_get_psk_identity(const SSL *s)
3275 {
3276 if (s == NULL || s->session == NULL)
3277 return NULL;
3278 return(s->session->psk_identity);
3279 }
3280
3281void SSL_set_psk_client_callback(SSL *s,
3282 unsigned int (*cb)(SSL *ssl, const char *hint,
3283 char *identity, unsigned int max_identity_len, unsigned char *psk,
3284 unsigned int max_psk_len))
3285 {
3286 s->psk_client_callback = cb;
3287 }
3288
3289void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
3290 unsigned int (*cb)(SSL *ssl, const char *hint,
3291 char *identity, unsigned int max_identity_len, unsigned char *psk,
3292 unsigned int max_psk_len))
3293 {
3294 ctx->psk_client_callback = cb;
3295 }
3296
3297void SSL_set_psk_server_callback(SSL *s,
3298 unsigned int (*cb)(SSL *ssl, const char *identity,
3299 unsigned char *psk, unsigned int max_psk_len))
3300 {
3301 s->psk_server_callback = cb;
3302 }
3303
3304void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
3305 unsigned int (*cb)(SSL *ssl, const char *identity,
3306 unsigned char *psk, unsigned int max_psk_len))
3307 {
3308 ctx->psk_server_callback = cb;
3309 }
3310#endif
3311
3312void 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))
3313 {
3314 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3315 }
3316void 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))
3317 {
3318 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3319 }
3320
Adam Langleyadb739e2014-06-20 12:00:00 -07003321int SSL_cutthrough_complete(const SSL *s)
3322 {
3323 return (!s->server && /* cutthrough only applies to clients */
3324 !s->hit && /* full-handshake */
3325 s->version >= SSL3_VERSION &&
3326 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3327 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3328 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3329 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3330 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
3331 s->state == SSL3_ST_CR_FINISHED_A));
3332 }
3333
Adam Langley95f22882014-06-20 12:00:00 -07003334void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3335 size_t* ssl_session_size)
3336{
3337 *ssl_size = sizeof(SSL);
3338 *ssl_ctx_size = sizeof(SSL_CTX);
3339 *ssl_session_size = sizeof(SSL_SESSION);
3340}
3341
Adam Langleyadb739e2014-06-20 12:00:00 -07003342int ssl3_can_cutthrough(const SSL *s)
3343 {
3344 const SSL_CIPHER *c;
3345
3346 /* require a strong enough cipher */
3347 if (SSL_get_cipher_bits(s, NULL) < 128)
3348 return 0;
3349
3350 /* require ALPN or NPN extension */
3351 if (!s->s3->alpn_selected
3352#ifndef OPENSSL_NO_NEXTPROTONEG
3353 && !s->s3->next_proto_neg_seen
3354#endif
3355 )
3356 {
3357 return 0;
3358 }
3359
3360 /* require a forward-secret cipher */
3361 c = SSL_get_current_cipher(s);
3362 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3363 c->algorithm_mkey != SSL_kEECDH))
3364 {
3365 return 0;
3366 }
3367
3368 return 1;
3369 }
3370
Adam Langley29707792014-06-20 12:00:00 -07003371/* ssl_get_max_version returns the maximum SSL/TLS version number supported by
3372 * |s|, or zero if all versions are disabled. */
3373int ssl_get_max_version(const SSL *s)
3374 {
3375 /* Only one version supported for DTLS. */
3376 if (s->version == DTLS1_VERSION)
3377 return DTLS1_VERSION;
3378
3379 if (!(s->options & SSL_OP_NO_TLSv1_2))
3380 return TLS1_2_VERSION;
3381 if (!(s->options & SSL_OP_NO_TLSv1_1))
3382 return TLS1_1_VERSION;
3383 if (!(s->options & SSL_OP_NO_TLSv1))
3384 return TLS1_VERSION;
3385 if (!(s->options & SSL_OP_NO_SSLv3))
3386 return SSL3_VERSION;
3387 if (!(s->options & SSL_OP_NO_SSLv2))
3388 return SSL2_VERSION;
3389 return 0;
3390 }
3391
Adam Langley95c29f32014-06-20 12:00:00 -07003392/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3393 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3394 * any. If EVP_MD pointer is passed, initializes ctx with this md
3395 * Returns newly allocated ctx;
3396 */
3397
3398EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3399{
3400 ssl_clear_hash_ctx(hash);
3401 *hash = EVP_MD_CTX_create();
3402 if (md) EVP_DigestInit_ex(*hash,md,NULL);
3403 return *hash;
3404}
3405void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3406{
3407
3408 if (*hash) EVP_MD_CTX_destroy(*hash);
3409 *hash=NULL;
3410}
3411
3412void SSL_set_debug(SSL *s, int debug)
3413 {
3414 s->debug = debug;
3415 }
3416
3417int SSL_cache_hit(SSL *s)
3418 {
3419 return s->hit;
3420 }
3421
3422int SSL_is_server(SSL *s)
3423 {
3424 return s->server;
3425 }