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