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