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