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