blob: ecc0c276fd4f80beef586975738dce8ab94bebf1 [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 {
David Benjamin072c9532014-07-26 11:44:25 -0400376 s->alpn_client_proto_list = BUF_memdup(
377 s->ctx->alpn_client_proto_list,
378 s->ctx->alpn_client_proto_list_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700379 if (s->alpn_client_proto_list == NULL)
380 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700381 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
382 }
Adam Langley95c29f32014-06-20 12:00:00 -0700383
384 s->verify_result=X509_V_OK;
385
386 s->method=ctx->method;
387
388 if (!s->method->ssl_new(s))
389 goto err;
390
391 s->references=1;
392 s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
393
394 SSL_clear(s);
395
396 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
397
Adam Langley0289c732014-06-20 12:00:00 -0700398 s->psk_identity_hint = NULL;
399 if (ctx->psk_identity_hint)
400 {
401 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
402 if (s->psk_identity_hint == NULL)
403 goto err;
404 }
Adam Langley95c29f32014-06-20 12:00:00 -0700405 s->psk_client_callback=ctx->psk_client_callback;
406 s->psk_server_callback=ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700407
408 return(s);
409err:
410 if (s != NULL)
411 {
412 if (s->cert != NULL)
413 ssl_cert_free(s->cert);
414 if (s->ctx != NULL)
415 SSL_CTX_free(s->ctx); /* decrement reference count */
416 OPENSSL_free(s);
417 }
418 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
419 return(NULL);
420 }
421
422int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
423 unsigned int sid_ctx_len)
424 {
425 if(sid_ctx_len > sizeof ctx->sid_ctx)
426 {
427 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
428 return 0;
429 }
430 ctx->sid_ctx_length=sid_ctx_len;
431 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
432
433 return 1;
434 }
435
436int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
437 unsigned int sid_ctx_len)
438 {
439 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
440 {
441 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
442 return 0;
443 }
444 ssl->sid_ctx_length=sid_ctx_len;
445 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
446
447 return 1;
448 }
449
450int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
451 {
452 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
453 ctx->generate_session_id = cb;
454 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
455 return 1;
456 }
457
458int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
459 {
460 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
461 ssl->generate_session_id = cb;
462 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
463 return 1;
464 }
465
466int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
467 unsigned int id_len)
468 {
469 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
470 * we can "construct" a session to give us the desired check - ie. to
471 * find if there's a session in the hash table that would conflict with
472 * any new session built out of this id/id_len and the ssl_version in
473 * use by this SSL. */
474 SSL_SESSION r, *p;
475
476 if(id_len > sizeof r.session_id)
477 return 0;
478
479 r.ssl_version = ssl->version;
480 r.session_id_length = id_len;
481 memcpy(r.session_id, id, id_len);
482 /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
483 * callback is calling us to check the uniqueness of a shorter ID, it
484 * must be compared as a padded-out ID because that is what it will be
485 * converted to when the callback has finished choosing it. */
486 if((r.ssl_version == SSL2_VERSION) &&
487 (id_len < SSL2_SSL_SESSION_ID_LENGTH))
488 {
489 memset(r.session_id + id_len, 0,
490 SSL2_SSL_SESSION_ID_LENGTH - id_len);
491 r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
492 }
493
494 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
495 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
496 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
497 return (p != NULL);
498 }
499
500int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
501 {
502 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
503 }
504
505int SSL_set_purpose(SSL *s, int purpose)
506 {
507 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
508 }
509
510int SSL_CTX_set_trust(SSL_CTX *s, int trust)
511 {
512 return X509_VERIFY_PARAM_set_trust(s->param, trust);
513 }
514
515int SSL_set_trust(SSL *s, int trust)
516 {
517 return X509_VERIFY_PARAM_set_trust(s->param, trust);
518 }
519
520int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
521 {
522 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
523 }
524
525int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
526 {
527 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
528 }
529
Adam Langley858a88d2014-06-20 12:00:00 -0700530void ssl_cipher_preference_list_free(
531 struct ssl_cipher_preference_list_st *cipher_list)
532 {
533 sk_SSL_CIPHER_free(cipher_list->ciphers);
534 OPENSSL_free(cipher_list->in_group_flags);
535 OPENSSL_free(cipher_list);
536 }
537
538struct ssl_cipher_preference_list_st*
539ssl_cipher_preference_list_dup(
540 struct ssl_cipher_preference_list_st *cipher_list)
541 {
542 struct ssl_cipher_preference_list_st* ret = NULL;
543 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
544
545 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
546 if (!ret)
547 goto err;
548 ret->ciphers = NULL;
549 ret->in_group_flags = NULL;
550 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
551 if (!ret->ciphers)
552 goto err;
David Benjamin072c9532014-07-26 11:44:25 -0400553 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
Adam Langley858a88d2014-06-20 12:00:00 -0700554 if (!ret->in_group_flags)
555 goto err;
Adam Langley858a88d2014-06-20 12:00:00 -0700556 return ret;
557
558err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400559 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700560 sk_SSL_CIPHER_free(ret->ciphers);
561 if (ret)
562 OPENSSL_free(ret);
563 return NULL;
564 }
565
566struct ssl_cipher_preference_list_st*
567ssl_cipher_preference_list_from_ciphers(STACK_OF(SSL_CIPHER) *ciphers)
568 {
569 struct ssl_cipher_preference_list_st* ret = NULL;
570 size_t n = sk_SSL_CIPHER_num(ciphers);
571
572 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
573 if (!ret)
574 goto err;
575 ret->ciphers = NULL;
576 ret->in_group_flags = NULL;
577 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
578 if (!ret->ciphers)
579 goto err;
580 ret->in_group_flags = OPENSSL_malloc(n);
581 if (!ret->in_group_flags)
582 goto err;
583 memset(ret->in_group_flags, 0, n);
584 return ret;
585
586err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400587 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700588 sk_SSL_CIPHER_free(ret->ciphers);
589 if (ret)
590 OPENSSL_free(ret);
591 return NULL;
592 }
593
Adam Langley95c29f32014-06-20 12:00:00 -0700594X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
595 {
596 return ctx->param;
597 }
598
599X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
600 {
601 return ssl->param;
602 }
603
604void SSL_certs_clear(SSL *s)
605 {
606 ssl_cert_clear_certs(s->cert);
607 }
608
609void SSL_free(SSL *s)
610 {
611 int i;
612
613 if(s == NULL)
614 return;
615
616 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
617#ifdef REF_PRINT
618 REF_PRINT("SSL",s);
619#endif
620 if (i > 0) return;
621#ifdef REF_CHECK
622 if (i < 0)
623 {
624 fprintf(stderr,"SSL_free, bad reference count\n");
625 abort(); /* ok */
626 }
627#endif
628
629 if (s->param)
630 X509_VERIFY_PARAM_free(s->param);
631
632 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
633
634 if (s->bbio != NULL)
635 {
636 /* If the buffering BIO is in place, pop it off */
637 if (s->bbio == s->wbio)
638 {
639 s->wbio=BIO_pop(s->wbio);
640 }
641 BIO_free(s->bbio);
642 s->bbio=NULL;
643 }
644 if (s->rbio != NULL)
645 BIO_free_all(s->rbio);
646 if ((s->wbio != NULL) && (s->wbio != s->rbio))
647 BIO_free_all(s->wbio);
648
649 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
650
651 /* add extra stuff */
Adam Langley858a88d2014-06-20 12:00:00 -0700652 if (s->cipher_list != NULL)
653 ssl_cipher_preference_list_free(s->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -0700654 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
655
656 /* Make the next call work :-) */
657 if (s->session != NULL)
658 {
659 ssl_clear_bad_session(s);
660 SSL_SESSION_free(s->session);
661 }
662
663 ssl_clear_cipher_ctx(s);
664 ssl_clear_hash_ctx(&s->read_hash);
665 ssl_clear_hash_ctx(&s->write_hash);
666
667 if (s->cert != NULL) ssl_cert_free(s->cert);
668 /* Free up if allocated */
669
Adam Langley95c29f32014-06-20 12:00:00 -0700670 if (s->tlsext_hostname)
671 OPENSSL_free(s->tlsext_hostname);
672 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
673#ifndef OPENSSL_NO_EC
674 if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
675 if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
676#endif /* OPENSSL_NO_EC */
Adam Langley95c29f32014-06-20 12:00:00 -0700677 if (s->tlsext_ocsp_exts)
678 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
679 X509_EXTENSION_free);
680 /* TODO(fork): OCSP support */
681#if 0
682 if (s->tlsext_ocsp_ids)
683 sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);
684#endif
685 if (s->tlsext_ocsp_resp)
686 OPENSSL_free(s->tlsext_ocsp_resp);
687 if (s->alpn_client_proto_list)
688 OPENSSL_free(s->alpn_client_proto_list);
Adam Langley1258b6a2014-06-20 12:00:00 -0700689 if (s->tlsext_channel_id_private)
690 EVP_PKEY_free(s->tlsext_channel_id_private);
Adam Langley95c29f32014-06-20 12:00:00 -0700691
Adam Langley0289c732014-06-20 12:00:00 -0700692 if (s->psk_identity_hint)
693 OPENSSL_free(s->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -0700694
Adam Langley95c29f32014-06-20 12:00:00 -0700695 if (s->client_CA != NULL)
696 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
697
698 if (s->method != NULL) s->method->ssl_free(s);
699
700 if (s->ctx) SSL_CTX_free(s->ctx);
701
David Benjamin6dbd73d2014-07-03 15:59:49 -0400702#if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -0700703 if (s->next_proto_negotiated)
704 OPENSSL_free(s->next_proto_negotiated);
705#endif
706
707 if (s->srtp_profiles)
708 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
709
David Benjamin1b965262014-08-13 20:06:29 -0400710 if (s->tlsext_session_ticket)
711 {
712 OPENSSL_free(s->tlsext_session_ticket);
713 }
714
Adam Langley95c29f32014-06-20 12:00:00 -0700715 OPENSSL_free(s);
716 }
717
718void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
719 {
720 /* If the output buffering BIO is still in place, remove it
721 */
722 if (s->bbio != NULL)
723 {
724 if (s->wbio == s->bbio)
725 {
726 s->wbio=s->wbio->next_bio;
727 s->bbio->next_bio=NULL;
728 }
729 }
730 if ((s->rbio != NULL) && (s->rbio != rbio))
731 BIO_free_all(s->rbio);
732 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
733 BIO_free_all(s->wbio);
734 s->rbio=rbio;
735 s->wbio=wbio;
736 }
737
738BIO *SSL_get_rbio(const SSL *s)
739 { return(s->rbio); }
740
741BIO *SSL_get_wbio(const SSL *s)
742 { return(s->wbio); }
743
744int SSL_get_fd(const SSL *s)
745 {
746 return(SSL_get_rfd(s));
747 }
748
749int SSL_get_rfd(const SSL *s)
750 {
751 int ret= -1;
752 BIO *b,*r;
753
754 b=SSL_get_rbio(s);
755 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
756 if (r != NULL)
757 BIO_get_fd(r,&ret);
758 return(ret);
759 }
760
761int SSL_get_wfd(const SSL *s)
762 {
763 int ret= -1;
764 BIO *b,*r;
765
766 b=SSL_get_wbio(s);
767 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
768 if (r != NULL)
769 BIO_get_fd(r,&ret);
770 return(ret);
771 }
772
773#ifndef OPENSSL_NO_SOCK
774int SSL_set_fd(SSL *s,int fd)
775 {
776 int ret=0;
777 BIO *bio=NULL;
778
779 bio=BIO_new(BIO_s_fd());
780
781 if (bio == NULL)
782 {
783 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
784 goto err;
785 }
786 BIO_set_fd(bio,fd,BIO_NOCLOSE);
787 SSL_set_bio(s,bio,bio);
788 ret=1;
789err:
790 return(ret);
791 }
792
793int SSL_set_wfd(SSL *s,int fd)
794 {
795 int ret=0;
796 BIO *bio=NULL;
797
798 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_FD)
799 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
800 {
801 bio=BIO_new(BIO_s_fd());
802
803 if (bio == NULL)
804 {
805 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
806 goto err;
807 }
808 BIO_set_fd(bio,fd,BIO_NOCLOSE);
809 SSL_set_bio(s,SSL_get_rbio(s),bio);
810 }
811 else
812 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
813 ret=1;
814err:
815 return(ret);
816 }
817
818int SSL_set_rfd(SSL *s,int fd)
819 {
820 int ret=0;
821 BIO *bio=NULL;
822
823 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_FD)
824 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
825 {
826 bio=BIO_new(BIO_s_fd());
827
828 if (bio == NULL)
829 {
830 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
831 goto err;
832 }
833 BIO_set_fd(bio,fd,BIO_NOCLOSE);
834 SSL_set_bio(s,bio,SSL_get_wbio(s));
835 }
836 else
837 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
838 ret=1;
839err:
840 return(ret);
841 }
842#endif
843
844
845/* return length of latest Finished message we sent, copy to 'buf' */
846size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
847 {
848 size_t ret = 0;
849
850 if (s->s3 != NULL)
851 {
852 ret = s->s3->tmp.finish_md_len;
853 if (count > ret)
854 count = ret;
855 memcpy(buf, s->s3->tmp.finish_md, count);
856 }
857 return ret;
858 }
859
860/* return length of latest Finished message we expected, copy to 'buf' */
861size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
862 {
863 size_t ret = 0;
864
865 if (s->s3 != NULL)
866 {
867 ret = s->s3->tmp.peer_finish_md_len;
868 if (count > ret)
869 count = ret;
870 memcpy(buf, s->s3->tmp.peer_finish_md, count);
871 }
872 return ret;
873 }
874
875
876int SSL_get_verify_mode(const SSL *s)
877 {
878 return(s->verify_mode);
879 }
880
881int SSL_get_verify_depth(const SSL *s)
882 {
883 return X509_VERIFY_PARAM_get_depth(s->param);
884 }
885
886int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
887 {
888 return(s->verify_callback);
889 }
890
891int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
892 {
893 return(ctx->verify_mode);
894 }
895
896int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
897 {
898 return X509_VERIFY_PARAM_get_depth(ctx->param);
899 }
900
901int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
902 {
903 return(ctx->default_verify_callback);
904 }
905
906void SSL_set_verify(SSL *s,int mode,
907 int (*callback)(int ok,X509_STORE_CTX *ctx))
908 {
909 s->verify_mode=mode;
910 if (callback != NULL)
911 s->verify_callback=callback;
912 }
913
914void SSL_set_verify_depth(SSL *s,int depth)
915 {
916 X509_VERIFY_PARAM_set_depth(s->param, depth);
917 }
918
919void SSL_set_read_ahead(SSL *s,int yes)
920 {
921 s->read_ahead=yes;
922 }
923
924int SSL_get_read_ahead(const SSL *s)
925 {
926 return(s->read_ahead);
927 }
928
929int SSL_pending(const SSL *s)
930 {
931 /* SSL_pending cannot work properly if read-ahead is enabled
932 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
933 * and it is impossible to fix since SSL_pending cannot report
934 * errors that may be observed while scanning the new data.
935 * (Note that SSL_pending() is often used as a boolean value,
936 * so we'd better not return -1.)
937 */
938 return(s->method->ssl_pending(s));
939 }
940
941X509 *SSL_get_peer_certificate(const SSL *s)
942 {
943 X509 *r;
944
945 if ((s == NULL) || (s->session == NULL))
946 r=NULL;
947 else
948 r=s->session->peer;
949
David Benjamin150c6172014-08-05 22:22:49 -0400950 if (r == NULL)
951 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700952
David Benjamin150c6172014-08-05 22:22:49 -0400953 return X509_up_ref(r);
Adam Langley95c29f32014-06-20 12:00:00 -0700954 }
955
956STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
957 {
958 STACK_OF(X509) *r;
959
960 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
961 r=NULL;
962 else
963 r=s->session->sess_cert->cert_chain;
964
965 /* If we are a client, cert_chain includes the peer's own
966 * certificate; if we are a server, it does not. */
967
968 return(r);
969 }
970
Adam Langley95c29f32014-06-20 12:00:00 -0700971/* Fix this so it checks all the valid key/cert options */
972int SSL_CTX_check_private_key(const SSL_CTX *ctx)
973 {
974 if ( (ctx == NULL) ||
975 (ctx->cert == NULL) ||
976 (ctx->cert->key->x509 == NULL))
977 {
978 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
979 return(0);
980 }
981 if (ctx->cert->key->privatekey == NULL)
982 {
983 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
984 return(0);
985 }
986 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
987 }
988
989/* Fix this function so that it takes an optional type parameter */
990int SSL_check_private_key(const SSL *ssl)
991 {
992 if (ssl == NULL)
993 {
994 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
995 return(0);
996 }
997 if (ssl->cert == NULL)
998 {
999 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
1000 return 0;
1001 }
1002 if (ssl->cert->key->x509 == NULL)
1003 {
1004 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
1005 return(0);
1006 }
1007 if (ssl->cert->key->privatekey == NULL)
1008 {
1009 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
1010 return(0);
1011 }
1012 return(X509_check_private_key(ssl->cert->key->x509,
1013 ssl->cert->key->privatekey));
1014 }
1015
1016int SSL_accept(SSL *s)
1017 {
1018 if (s->handshake_func == 0)
1019 /* Not properly initialized yet */
1020 SSL_set_accept_state(s);
1021
1022 return(s->method->ssl_accept(s));
1023 }
1024
1025int SSL_connect(SSL *s)
1026 {
1027 if (s->handshake_func == 0)
1028 /* Not properly initialized yet */
1029 SSL_set_connect_state(s);
1030
1031 return(s->method->ssl_connect(s));
1032 }
1033
1034long SSL_get_default_timeout(const SSL *s)
1035 {
David Benjaminf4501342014-08-14 17:24:37 -04001036 return SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001037 }
1038
1039int SSL_read(SSL *s,void *buf,int num)
1040 {
1041 if (s->handshake_func == 0)
1042 {
1043 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
1044 return -1;
1045 }
1046
1047 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1048 {
1049 s->rwstate=SSL_NOTHING;
1050 return(0);
1051 }
1052 return(s->method->ssl_read(s,buf,num));
1053 }
1054
1055int SSL_peek(SSL *s,void *buf,int num)
1056 {
1057 if (s->handshake_func == 0)
1058 {
1059 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
1060 return -1;
1061 }
1062
1063 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1064 {
1065 return(0);
1066 }
1067 return(s->method->ssl_peek(s,buf,num));
1068 }
1069
1070int SSL_write(SSL *s,const void *buf,int num)
1071 {
1072 if (s->handshake_func == 0)
1073 {
1074 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
1075 return -1;
1076 }
1077
1078 if (s->shutdown & SSL_SENT_SHUTDOWN)
1079 {
1080 s->rwstate=SSL_NOTHING;
1081 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
1082 return(-1);
1083 }
1084 return(s->method->ssl_write(s,buf,num));
1085 }
1086
1087int SSL_shutdown(SSL *s)
1088 {
1089 /* Note that this function behaves differently from what one might
1090 * expect. Return values are 0 for no success (yet),
1091 * 1 for success; but calling it once is usually not enough,
1092 * even if blocking I/O is used (see ssl3_shutdown).
1093 */
1094
1095 if (s->handshake_func == 0)
1096 {
1097 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1098 return -1;
1099 }
1100
1101 if ((s != NULL) && !SSL_in_init(s))
1102 return(s->method->ssl_shutdown(s));
1103 else
1104 return(1);
1105 }
1106
1107int SSL_renegotiate(SSL *s)
1108 {
1109 if (s->renegotiate == 0)
1110 s->renegotiate=1;
1111
1112 s->new_session=1;
1113
1114 return(s->method->ssl_renegotiate(s));
1115 }
1116
1117int SSL_renegotiate_abbreviated(SSL *s)
1118 {
1119 if (s->renegotiate == 0)
1120 s->renegotiate=1;
1121
1122 s->new_session=0;
1123
1124 return(s->method->ssl_renegotiate(s));
1125 }
1126
1127int SSL_renegotiate_pending(SSL *s)
1128 {
1129 /* becomes true when negotiation is requested;
1130 * false again once a handshake has finished */
1131 return (s->renegotiate != 0);
1132 }
1133
1134long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1135 {
1136 long l;
1137
1138 switch (cmd)
1139 {
1140 case SSL_CTRL_GET_READ_AHEAD:
1141 return(s->read_ahead);
1142 case SSL_CTRL_SET_READ_AHEAD:
1143 l=s->read_ahead;
1144 s->read_ahead=larg;
1145 return(l);
1146
1147 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1148 s->msg_callback_arg = parg;
1149 return 1;
1150
1151 case SSL_CTRL_OPTIONS:
1152 return(s->options|=larg);
1153 case SSL_CTRL_CLEAR_OPTIONS:
1154 return(s->options&=~larg);
1155 case SSL_CTRL_MODE:
1156 return(s->mode|=larg);
1157 case SSL_CTRL_CLEAR_MODE:
1158 return(s->mode &=~larg);
1159 case SSL_CTRL_GET_MAX_CERT_LIST:
1160 return(s->max_cert_list);
1161 case SSL_CTRL_SET_MAX_CERT_LIST:
1162 l=s->max_cert_list;
1163 s->max_cert_list=larg;
1164 return(l);
1165 case SSL_CTRL_SET_MTU:
1166#ifndef OPENSSL_NO_DTLS1
1167 if (larg < (long)dtls1_min_mtu())
1168 return 0;
1169#endif
1170
1171 if (SSL_IS_DTLS(s))
1172 {
1173 s->d1->mtu = larg;
1174 return larg;
1175 }
1176 return 0;
1177 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1178 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1179 return 0;
1180 s->max_send_fragment = larg;
1181 return 1;
1182 case SSL_CTRL_GET_RI_SUPPORT:
1183 if (s->s3)
1184 return s->s3->send_connection_binding;
1185 else return 0;
1186 case SSL_CTRL_CERT_FLAGS:
1187 return(s->cert->cert_flags|=larg);
1188 case SSL_CTRL_CLEAR_CERT_FLAGS:
1189 return(s->cert->cert_flags &=~larg);
1190
1191 case SSL_CTRL_GET_RAW_CIPHERLIST:
1192 if (parg)
1193 {
1194 if (s->cert->ciphers_raw == NULL)
1195 return 0;
1196 *(unsigned char **)parg = s->cert->ciphers_raw;
1197 return (int)s->cert->ciphers_rawlen;
1198 }
1199 else
David Benjamin39482a12014-07-20 13:30:15 -04001200 {
1201 /* Passing a NULL |parg| returns the size of a single
1202 * cipher suite value. */
1203 return 2;
1204 }
Adam Langley95c29f32014-06-20 12:00:00 -07001205 default:
1206 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1207 }
1208 }
1209
1210long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1211 {
1212 switch(cmd)
1213 {
1214 case SSL_CTRL_SET_MSG_CALLBACK:
1215 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1216 return 1;
1217
1218 default:
1219 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1220 }
1221 }
1222
1223LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1224 {
1225 return ctx->sessions;
1226 }
1227
1228long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1229 {
1230 long l;
1231
1232 switch (cmd)
1233 {
1234 case SSL_CTRL_GET_READ_AHEAD:
1235 return(ctx->read_ahead);
1236 case SSL_CTRL_SET_READ_AHEAD:
1237 l=ctx->read_ahead;
1238 ctx->read_ahead=larg;
1239 return(l);
1240
1241 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1242 ctx->msg_callback_arg = parg;
1243 return 1;
1244
1245 case SSL_CTRL_GET_MAX_CERT_LIST:
1246 return(ctx->max_cert_list);
1247 case SSL_CTRL_SET_MAX_CERT_LIST:
1248 l=ctx->max_cert_list;
1249 ctx->max_cert_list=larg;
1250 return(l);
1251
1252 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1253 l=ctx->session_cache_size;
1254 ctx->session_cache_size=larg;
1255 return(l);
1256 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1257 return(ctx->session_cache_size);
1258 case SSL_CTRL_SET_SESS_CACHE_MODE:
1259 l=ctx->session_cache_mode;
1260 ctx->session_cache_mode=larg;
1261 return(l);
1262 case SSL_CTRL_GET_SESS_CACHE_MODE:
1263 return(ctx->session_cache_mode);
1264
1265 case SSL_CTRL_SESS_NUMBER:
1266 return(lh_SSL_SESSION_num_items(ctx->sessions));
1267 case SSL_CTRL_SESS_CONNECT:
1268 return(ctx->stats.sess_connect);
1269 case SSL_CTRL_SESS_CONNECT_GOOD:
1270 return(ctx->stats.sess_connect_good);
1271 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1272 return(ctx->stats.sess_connect_renegotiate);
1273 case SSL_CTRL_SESS_ACCEPT:
1274 return(ctx->stats.sess_accept);
1275 case SSL_CTRL_SESS_ACCEPT_GOOD:
1276 return(ctx->stats.sess_accept_good);
1277 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1278 return(ctx->stats.sess_accept_renegotiate);
1279 case SSL_CTRL_SESS_HIT:
1280 return(ctx->stats.sess_hit);
1281 case SSL_CTRL_SESS_CB_HIT:
1282 return(ctx->stats.sess_cb_hit);
1283 case SSL_CTRL_SESS_MISSES:
1284 return(ctx->stats.sess_miss);
1285 case SSL_CTRL_SESS_TIMEOUTS:
1286 return(ctx->stats.sess_timeout);
1287 case SSL_CTRL_SESS_CACHE_FULL:
1288 return(ctx->stats.sess_cache_full);
1289 case SSL_CTRL_OPTIONS:
1290 return(ctx->options|=larg);
1291 case SSL_CTRL_CLEAR_OPTIONS:
1292 return(ctx->options&=~larg);
1293 case SSL_CTRL_MODE:
1294 return(ctx->mode|=larg);
1295 case SSL_CTRL_CLEAR_MODE:
1296 return(ctx->mode&=~larg);
1297 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1298 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1299 return 0;
1300 ctx->max_send_fragment = larg;
1301 return 1;
1302 case SSL_CTRL_CERT_FLAGS:
1303 return(ctx->cert->cert_flags|=larg);
1304 case SSL_CTRL_CLEAR_CERT_FLAGS:
1305 return(ctx->cert->cert_flags &=~larg);
1306 default:
1307 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1308 }
1309 }
1310
1311long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1312 {
1313 switch(cmd)
1314 {
1315 case SSL_CTRL_SET_MSG_CALLBACK:
1316 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1317 return 1;
1318
1319 default:
1320 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1321 }
1322 }
1323
1324int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1325 {
1326 long l;
1327 const SSL_CIPHER *a = in_a;
1328 const SSL_CIPHER *b = in_b;
1329 const long a_id = a->id;
1330 const long b_id = b->id;
1331
1332 l = a_id - b_id;
1333 if (l == 0L)
1334 return(0);
1335 else
1336 return((l > 0)?1:-1);
1337 }
1338
1339int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1340 {
1341 long l;
1342 const long a_id = (*ap)->id;
1343 const long b_id = (*bp)->id;
1344
1345 l = a_id - b_id;
1346 if (l == 0)
1347 return(0);
1348 else
1349 return((l > 0)?1:-1);
1350 }
1351
1352/** return a STACK of the ciphers available for the SSL and in order of
1353 * preference */
1354STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1355 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001356 if (s == NULL)
1357 return NULL;
1358
1359 if (s->cipher_list != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001360 {
Adam Langley858a88d2014-06-20 12:00:00 -07001361 return(s->cipher_list->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -07001362 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001363
1364 if (s->version >= TLS1_1_VERSION)
1365 {
1366 if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001367 return s->ctx->cipher_list_tls11->ciphers;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001368 }
1369
1370 if ((s->ctx != NULL) &&
1371 (s->ctx->cipher_list != NULL))
1372 {
Adam Langley858a88d2014-06-20 12:00:00 -07001373 return(s->ctx->cipher_list->ciphers);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001374 }
1375
Adam Langley95c29f32014-06-20 12:00:00 -07001376 return(NULL);
1377 }
1378
1379/** return a STACK of the ciphers available for the SSL and in order of
1380 * algorithm id */
1381STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1382 {
1383 if (s != NULL)
1384 {
1385 if (s->cipher_list_by_id != NULL)
1386 {
1387 return(s->cipher_list_by_id);
1388 }
1389 else if ((s->ctx != NULL) &&
1390 (s->ctx->cipher_list_by_id != NULL))
1391 {
1392 return(s->ctx->cipher_list_by_id);
1393 }
1394 }
1395 return(NULL);
1396 }
1397
1398/** The old interface to get the same thing as SSL_get_ciphers() */
1399const char *SSL_get_cipher_list(const SSL *s,int n)
1400 {
1401 SSL_CIPHER *c;
1402 STACK_OF(SSL_CIPHER) *sk;
1403
1404 if (s == NULL) return(NULL);
1405 sk=SSL_get_ciphers(s);
1406 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
1407 return(NULL);
1408 c=sk_SSL_CIPHER_value(sk,n);
1409 if (c == NULL) return(NULL);
1410 return(c->name);
1411 }
1412
1413/** specify the ciphers to be used by default by the SSL_CTX */
1414int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1415 {
1416 STACK_OF(SSL_CIPHER) *sk;
1417
1418 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1419 &ctx->cipher_list_by_id,str, ctx->cert);
1420 /* ssl_create_cipher_list may return an empty stack if it
1421 * was unable to find a cipher matching the given rule string
1422 * (for example if the rule string specifies a cipher which
1423 * has been disabled). This is not an error as far as
1424 * ssl_create_cipher_list is concerned, and hence
1425 * ctx->cipher_list and ctx->cipher_list_by_id has been
1426 * updated. */
1427 if (sk == NULL)
1428 return 0;
1429 else if (sk_SSL_CIPHER_num(sk) == 0)
1430 {
1431 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1432 return 0;
1433 }
1434 return 1;
1435 }
1436
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001437int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str)
1438 {
1439 STACK_OF(SSL_CIPHER) *sk;
1440
1441 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert);
1442 if (sk == NULL)
1443 return 0;
1444 else if (sk_SSL_CIPHER_num(sk) == 0)
1445 {
1446 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH);
1447 return 0;
1448 }
1449 return 1;
1450 }
1451
Adam Langley95c29f32014-06-20 12:00:00 -07001452/** specify the ciphers to be used by the SSL */
1453int SSL_set_cipher_list(SSL *s,const char *str)
1454 {
1455 STACK_OF(SSL_CIPHER) *sk;
1456
1457 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1458 &s->cipher_list_by_id,str, s->cert);
1459 /* see comment in SSL_CTX_set_cipher_list */
1460 if (sk == NULL)
1461 return 0;
1462 else if (sk_SSL_CIPHER_num(sk) == 0)
1463 {
1464 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1465 return 0;
1466 }
1467 return 1;
1468 }
1469
1470/* works well for SSLv2, not so good for SSLv3 */
1471char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
1472 {
1473 char *p;
1474 STACK_OF(SSL_CIPHER) *sk;
1475 SSL_CIPHER *c;
1476 int i;
1477
1478 if ((s->session == NULL) || (s->session->ciphers == NULL) ||
1479 (len < 2))
1480 return(NULL);
1481
1482 p=buf;
1483 sk=s->session->ciphers;
Adam Langley04124042014-06-20 12:00:00 -07001484
1485 if (sk_SSL_CIPHER_num(sk) == 0)
1486 return NULL;
1487
Adam Langley95c29f32014-06-20 12:00:00 -07001488 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1489 {
1490 int n;
1491
1492 c=sk_SSL_CIPHER_value(sk,i);
1493 n=strlen(c->name);
1494 if (n+1 > len)
1495 {
1496 if (p != buf)
1497 --p;
1498 *p='\0';
1499 return buf;
1500 }
1501 strcpy(p,c->name);
1502 p+=n;
1503 *(p++)=':';
1504 len-=n+1;
1505 }
1506 p[-1]='\0';
1507 return(buf);
1508 }
1509
David Benjamin39482a12014-07-20 13:30:15 -04001510int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p)
Adam Langley95c29f32014-06-20 12:00:00 -07001511 {
David Benjamin39482a12014-07-20 13:30:15 -04001512 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001513 SSL_CIPHER *c;
1514 CERT *ct = s->cert;
1515 unsigned char *q;
1516 int no_scsv = s->renegotiate;
1517 /* Set disabled masks for this session */
1518 ssl_set_client_disabled(s);
1519
1520 if (sk == NULL) return(0);
1521 q=p;
1522
1523 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1524 {
1525 c=sk_SSL_CIPHER_value(sk,i);
1526 /* Skip disabled ciphers */
1527 if (c->algorithm_ssl & ct->mask_ssl ||
1528 c->algorithm_mkey & ct->mask_k ||
1529 c->algorithm_auth & ct->mask_a)
1530 continue;
1531#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
1532 if (c->id == SSL3_CK_SCSV)
1533 {
1534 if (no_scsv)
1535 continue;
1536 else
1537 no_scsv = 1;
1538 }
1539#endif
David Benjamin39482a12014-07-20 13:30:15 -04001540 s2n(ssl3_get_cipher_value(c), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001541 }
1542 /* If p == q, no ciphers and caller indicates an error. Otherwise
1543 * add SCSV if not renegotiating.
1544 */
Adam Langleyac61fa32014-06-23 12:03:11 -07001545 if (p != q)
Adam Langley95c29f32014-06-20 12:00:00 -07001546 {
Adam Langleyac61fa32014-06-23 12:03:11 -07001547 if (!no_scsv)
Adam Langley95c29f32014-06-20 12:00:00 -07001548 {
Adam Langleyac61fa32014-06-23 12:03:11 -07001549 static SSL_CIPHER scsv =
1550 {
1551 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1552 };
David Benjamin39482a12014-07-20 13:30:15 -04001553 s2n(ssl3_get_cipher_value(&scsv), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001554#ifdef OPENSSL_RI_DEBUG
Adam Langleyac61fa32014-06-23 12:03:11 -07001555 fprintf(stderr, "SCSV sent by client\n");
Adam Langley95c29f32014-06-20 12:00:00 -07001556#endif
Adam Langleyac61fa32014-06-23 12:03:11 -07001557 }
1558 if (s->fallback_scsv)
1559 {
1560 static SSL_CIPHER fallback_scsv =
1561 {
1562 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1563 };
David Benjamin39482a12014-07-20 13:30:15 -04001564 s2n(ssl3_get_cipher_value(&fallback_scsv), p);
Adam Langleyac61fa32014-06-23 12:03:11 -07001565 }
Adam Langley95c29f32014-06-20 12:00:00 -07001566 }
1567
1568 return(p-q);
1569 }
1570
David Benjamin39482a12014-07-20 13:30:15 -04001571STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs,
Adam Langley95c29f32014-06-20 12:00:00 -07001572 STACK_OF(SSL_CIPHER) **skp)
1573 {
David Benjamin39482a12014-07-20 13:30:15 -04001574 CBS cipher_suites = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001575 const SSL_CIPHER *c;
1576 STACK_OF(SSL_CIPHER) *sk;
David Benjamin39482a12014-07-20 13:30:15 -04001577
Adam Langley95c29f32014-06-20 12:00:00 -07001578 if (s->s3)
1579 s->s3->send_connection_binding = 0;
1580
David Benjamin39482a12014-07-20 13:30:15 -04001581 if (CBS_len(&cipher_suites) % 2 != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001582 {
1583 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1584 return(NULL);
1585 }
1586 if ((skp == NULL) || (*skp == NULL))
1587 sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
1588 else
1589 {
1590 sk= *skp;
1591 sk_SSL_CIPHER_zero(sk);
1592 }
1593
David Benjamin39482a12014-07-20 13:30:15 -04001594 if (!CBS_stow(&cipher_suites,
1595 &s->cert->ciphers_raw, &s->cert->ciphers_rawlen))
Adam Langley95c29f32014-06-20 12:00:00 -07001596 {
1597 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1598 goto err;
1599 }
Adam Langley95c29f32014-06-20 12:00:00 -07001600
David Benjamin39482a12014-07-20 13:30:15 -04001601 while (CBS_len(&cipher_suites) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001602 {
David Benjamin39482a12014-07-20 13:30:15 -04001603 uint16_t cipher_suite;
1604
1605 if (!CBS_get_u16(&cipher_suites, &cipher_suite))
1606 {
1607 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1608 goto err;
1609 }
1610
Adam Langley95c29f32014-06-20 12:00:00 -07001611 /* Check for SCSV */
David Benjamin39482a12014-07-20 13:30:15 -04001612 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff))
Adam Langley95c29f32014-06-20 12:00:00 -07001613 {
1614 /* SCSV fatal if renegotiating */
1615 if (s->renegotiate)
1616 {
1617 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1618 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1619 goto err;
1620 }
1621 s->s3->send_connection_binding = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001622#ifdef OPENSSL_RI_DEBUG
1623 fprintf(stderr, "SCSV received by server\n");
1624#endif
1625 continue;
1626 }
1627
Adam Langley29707792014-06-20 12:00:00 -07001628 /* Check for FALLBACK_SCSV */
David Benjamin39482a12014-07-20 13:30:15 -04001629 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff) &&
Adam Langley29707792014-06-20 12:00:00 -07001630 s->version < ssl_get_max_version(s))
1631 {
1632 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1633 ssl3_send_alert(s,SSL3_AL_FATAL,SSL3_AD_INAPPROPRIATE_FALLBACK);
1634 goto err;
1635 }
1636
David Benjamin39482a12014-07-20 13:30:15 -04001637 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -07001638 if (c != NULL)
1639 {
1640 if (!sk_SSL_CIPHER_push(sk,c))
1641 {
1642 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1643 goto err;
1644 }
1645 }
1646 }
1647
1648 if (skp != NULL)
1649 *skp=sk;
1650 return(sk);
1651err:
1652 if ((skp == NULL) || (*skp == NULL))
1653 sk_SSL_CIPHER_free(sk);
1654 return(NULL);
1655 }
1656
1657
Adam Langley95c29f32014-06-20 12:00:00 -07001658/** return a servername extension value if provided in Client Hello, or NULL.
1659 * So far, only host_name types are defined (RFC 3546).
1660 */
1661
1662const char *SSL_get_servername(const SSL *s, const int type)
1663 {
1664 if (type != TLSEXT_NAMETYPE_host_name)
1665 return NULL;
1666
1667 return s->session && !s->tlsext_hostname ?
1668 s->session->tlsext_hostname :
1669 s->tlsext_hostname;
1670 }
1671
1672int SSL_get_servername_type(const SSL *s)
1673 {
1674 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1675 return TLSEXT_NAMETYPE_host_name;
1676 return -1;
1677 }
1678
1679/* SSL_select_next_proto implements the standard protocol selection. It is
1680 * expected that this function is called from the callback set by
1681 * SSL_CTX_set_next_proto_select_cb.
1682 *
1683 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1684 * strings. The length byte itself is not included in the length. A byte
1685 * string of length 0 is invalid. No byte string may be truncated.
1686 *
1687 * The current, but experimental algorithm for selecting the protocol is:
1688 *
1689 * 1) If the server doesn't support NPN then this is indicated to the
1690 * callback. In this case, the client application has to abort the connection
1691 * or have a default application level protocol.
1692 *
1693 * 2) If the server supports NPN, but advertises an empty list then the
1694 * client selects the first protcol in its list, but indicates via the
1695 * API that this fallback case was enacted.
1696 *
1697 * 3) Otherwise, the client finds the first protocol in the server's list
1698 * that it supports and selects this protocol. This is because it's
1699 * assumed that the server has better information about which protocol
1700 * a client should use.
1701 *
1702 * 4) If the client doesn't support any of the server's advertised
1703 * protocols, then this is treated the same as case 2.
1704 *
1705 * It returns either
1706 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1707 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1708 */
1709int 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)
1710 {
1711 unsigned int i, j;
1712 const unsigned char *result;
1713 int status = OPENSSL_NPN_UNSUPPORTED;
1714
1715 /* For each protocol in server preference order, see if we support it. */
1716 for (i = 0; i < server_len; )
1717 {
1718 for (j = 0; j < client_len; )
1719 {
1720 if (server[i] == client[j] &&
1721 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1722 {
1723 /* We found a match */
1724 result = &server[i];
1725 status = OPENSSL_NPN_NEGOTIATED;
1726 goto found;
1727 }
1728 j += client[j];
1729 j++;
1730 }
1731 i += server[i];
1732 i++;
1733 }
1734
1735 /* There's no overlap between our protocols and the server's list. */
1736 result = client;
1737 status = OPENSSL_NPN_NO_OVERLAP;
1738
1739 found:
1740 *out = (unsigned char *) result + 1;
1741 *outlen = result[0];
1742 return status;
1743 }
1744
1745# ifndef OPENSSL_NO_NEXTPROTONEG
1746/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1747 * requested protocol for this connection and returns 0. If the client didn't
1748 * request any protocol, then *data is set to NULL.
1749 *
1750 * Note that the client can request any protocol it chooses. The value returned
1751 * from this function need not be a member of the list of supported protocols
1752 * provided by the callback.
1753 */
David Benjaminced551f2014-07-11 23:16:16 -04001754void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, unsigned *len)
Adam Langley95c29f32014-06-20 12:00:00 -07001755 {
1756 *data = s->next_proto_negotiated;
1757 if (!*data) {
1758 *len = 0;
1759 } else {
1760 *len = s->next_proto_negotiated_len;
1761 }
1762}
1763
1764/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1765 * TLS server needs a list of supported protocols for Next Protocol
1766 * Negotiation. The returned list must be in wire format. The list is returned
1767 * by setting |out| to point to it and |outlen| to its length. This memory will
1768 * not be modified, but one should assume that the SSL* keeps a reference to
1769 * it.
1770 *
1771 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1772 * such extension will be included in the ServerHello. */
1773void 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)
1774 {
1775 ctx->next_protos_advertised_cb = cb;
1776 ctx->next_protos_advertised_cb_arg = arg;
1777 }
1778
1779/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1780 * client needs to select a protocol from the server's provided list. |out|
1781 * must be set to point to the selected protocol (which may be within |in|).
1782 * The length of the protocol name must be written into |outlen|. The server's
1783 * advertised protocols are provided in |in| and |inlen|. The callback can
1784 * assume that |in| is syntactically valid.
1785 *
1786 * The client must select a protocol. It is fatal to the connection if this
1787 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1788 */
1789void 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)
1790 {
1791 ctx->next_proto_select_cb = cb;
1792 ctx->next_proto_select_cb_arg = arg;
1793 }
1794# endif
1795
Adam Langley95c29f32014-06-20 12:00:00 -07001796/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1797 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1798 * length-prefixed strings).
1799 *
1800 * Returns 0 on success. */
1801int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1802 unsigned protos_len)
1803 {
1804 if (ctx->alpn_client_proto_list)
1805 OPENSSL_free(ctx->alpn_client_proto_list);
1806
David Benjamin072c9532014-07-26 11:44:25 -04001807 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001808 if (!ctx->alpn_client_proto_list)
1809 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001810 ctx->alpn_client_proto_list_len = protos_len;
1811
1812 return 0;
1813 }
1814
1815/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1816 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1817 * length-prefixed strings).
1818 *
1819 * Returns 0 on success. */
1820int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1821 unsigned protos_len)
1822 {
1823 if (ssl->alpn_client_proto_list)
1824 OPENSSL_free(ssl->alpn_client_proto_list);
1825
David Benjamin072c9532014-07-26 11:44:25 -04001826 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001827 if (!ssl->alpn_client_proto_list)
1828 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001829 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 */
David Benjaminf4501342014-08-14 17:24:37 -04001932 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001933
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_ENGINE
2019 ret->client_cert_engine = NULL;
2020#ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
2021#define eng_strx(x) #x
2022#define eng_str(x) eng_strx(x)
2023 /* Use specific client engine automatically... ignore errors */
2024 {
2025 ENGINE *eng;
2026 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
2027 if (!eng)
2028 {
2029 ERR_clear_error();
2030 ENGINE_load_builtin_engines();
2031 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
2032 }
2033 if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
2034 ERR_clear_error();
2035 }
2036#endif
2037#endif
2038 /* Default is to connect to non-RI servers. When RI is more widely
2039 * deployed might change this.
2040 */
2041 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
2042
2043 return(ret);
2044err:
2045 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
2046err2:
2047 if (ret != NULL) SSL_CTX_free(ret);
2048 return(NULL);
2049 }
2050
Adam Langley95c29f32014-06-20 12:00:00 -07002051
2052void SSL_CTX_free(SSL_CTX *a)
2053 {
2054 int i;
2055
2056 if (a == NULL) return;
2057
2058 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
2059#ifdef REF_PRINT
2060 REF_PRINT("SSL_CTX",a);
2061#endif
2062 if (i > 0) return;
2063#ifdef REF_CHECK
2064 if (i < 0)
2065 {
2066 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
2067 abort(); /* ok */
2068 }
2069#endif
2070
2071 if (a->param)
2072 X509_VERIFY_PARAM_free(a->param);
2073
2074 /*
2075 * Free internal session cache. However: the remove_cb() may reference
2076 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2077 * after the sessions were flushed.
2078 * As the ex_data handling routines might also touch the session cache,
2079 * the most secure solution seems to be: empty (flush) the cache, then
2080 * free ex_data, then finally free the cache.
2081 * (See ticket [openssl.org #212].)
2082 */
2083 if (a->sessions != NULL)
2084 SSL_CTX_flush_sessions(a,0);
2085
2086 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
2087
2088 if (a->sessions != NULL)
2089 lh_SSL_SESSION_free(a->sessions);
2090
2091 if (a->cert_store != NULL)
2092 X509_STORE_free(a->cert_store);
2093 if (a->cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002094 ssl_cipher_preference_list_free(a->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002095 if (a->cipher_list_by_id != NULL)
2096 sk_SSL_CIPHER_free(a->cipher_list_by_id);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07002097 if (a->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002098 ssl_cipher_preference_list_free(a->cipher_list_tls11);
Adam Langley95c29f32014-06-20 12:00:00 -07002099 if (a->cert != NULL)
2100 ssl_cert_free(a->cert);
2101 if (a->client_CA != NULL)
2102 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2103 if (a->extra_certs != NULL)
2104 sk_X509_pop_free(a->extra_certs,X509_free);
Adam Langley95c29f32014-06-20 12:00:00 -07002105
2106 if (a->srtp_profiles)
2107 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2108
Adam Langley95c29f32014-06-20 12:00:00 -07002109 if (a->psk_identity_hint)
2110 OPENSSL_free(a->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07002111
2112 /* TODO(fork): remove. */
2113#if 0
2114#ifndef OPENSSL_NO_ENGINE
2115 if (a->client_cert_engine)
2116 ENGINE_finish(a->client_cert_engine);
2117#endif
2118#endif
2119
Adam Langley95c29f32014-06-20 12:00:00 -07002120# ifndef OPENSSL_NO_EC
2121 if (a->tlsext_ecpointformatlist)
2122 OPENSSL_free(a->tlsext_ecpointformatlist);
2123 if (a->tlsext_ellipticcurvelist)
2124 OPENSSL_free(a->tlsext_ellipticcurvelist);
2125# endif /* OPENSSL_NO_EC */
2126 if (a->alpn_client_proto_list != NULL)
2127 OPENSSL_free(a->alpn_client_proto_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002128
Adam Langley1258b6a2014-06-20 12:00:00 -07002129 if (a->tlsext_channel_id_private)
2130 EVP_PKEY_free(a->tlsext_channel_id_private);
Adam Langley1258b6a2014-06-20 12:00:00 -07002131
Adam Langley95c29f32014-06-20 12:00:00 -07002132 OPENSSL_free(a);
2133 }
2134
2135void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2136 {
2137 ctx->default_passwd_callback=cb;
2138 }
2139
2140void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2141 {
2142 ctx->default_passwd_callback_userdata=u;
2143 }
2144
2145void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2146 {
2147 ctx->app_verify_callback=cb;
2148 ctx->app_verify_arg=arg;
2149 }
2150
2151void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2152 {
2153 ctx->verify_mode=mode;
2154 ctx->default_verify_callback=cb;
2155 }
2156
2157void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2158 {
2159 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2160 }
2161
2162void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2163 {
2164 ssl_cert_set_cert_cb(c->cert, cb, arg);
2165 }
2166
2167void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2168 {
2169 ssl_cert_set_cert_cb(s->cert, cb, arg);
2170 }
2171
2172void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
2173 {
2174 CERT_PKEY *cpk;
David Benjamin77a942b2014-07-15 01:22:50 -04002175 int rsa_enc,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
David Benjamin060d9d22014-07-15 00:54:26 -04002176 unsigned long mask_k,mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002177#ifndef OPENSSL_NO_ECDSA
David Benjamin060d9d22014-07-15 00:54:26 -04002178 int have_ecc_cert, ecdsa_ok;
Adam Langley95c29f32014-06-20 12:00:00 -07002179#endif
2180#ifndef OPENSSL_NO_ECDH
2181 int have_ecdh_tmp, ecdh_ok;
2182#endif
2183#ifndef OPENSSL_NO_EC
2184 X509 *x = NULL;
2185 EVP_PKEY *ecc_pkey = NULL;
2186 int signature_nid = 0, pk_nid = 0, md_nid = 0;
2187#endif
2188 if (c == NULL) return;
2189
Adam Langley95c29f32014-06-20 12:00:00 -07002190#ifndef OPENSSL_NO_DH
2191 dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002192#else
David Benjamin060d9d22014-07-15 00:54:26 -04002193 dh_tmp=0;
Adam Langley95c29f32014-06-20 12:00:00 -07002194#endif
2195
2196#ifndef OPENSSL_NO_ECDH
2197 have_ecdh_tmp=(c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
2198#endif
2199 cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
2200 rsa_enc= cpk->valid_flags & CERT_PKEY_VALID;
Adam Langley95c29f32014-06-20 12:00:00 -07002201 cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
2202 rsa_sign= cpk->valid_flags & CERT_PKEY_SIGN;
2203 cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
2204 dsa_sign= cpk->valid_flags & CERT_PKEY_SIGN;
2205 cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
2206 dh_rsa= cpk->valid_flags & CERT_PKEY_VALID;
Adam Langley95c29f32014-06-20 12:00:00 -07002207 cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
2208/* FIX THIS EAY EAY EAY */
2209 dh_dsa= cpk->valid_flags & CERT_PKEY_VALID;
Adam Langley95c29f32014-06-20 12:00:00 -07002210 cpk= &(c->pkeys[SSL_PKEY_ECC]);
2211#ifndef OPENSSL_NO_EC
2212 have_ecc_cert= cpk->valid_flags & CERT_PKEY_VALID;
2213#endif
2214 mask_k=0;
2215 mask_a=0;
Adam Langley95c29f32014-06-20 12:00:00 -07002216
2217#ifdef CIPHER_DEBUG
2218 printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
2219 rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
2220 rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
2221#endif
2222
David Benjamin77a942b2014-07-15 01:22:50 -04002223 if (rsa_enc)
Adam Langley95c29f32014-06-20 12:00:00 -07002224 mask_k|=SSL_kRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002225
2226#if 0
2227 /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
2228 if ( (dh_tmp || dh_rsa || dh_dsa) &&
2229 (rsa_enc || rsa_sign || dsa_sign))
2230 mask_k|=SSL_kEDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002231#endif
2232
Adam Langley95c29f32014-06-20 12:00:00 -07002233 if (dh_tmp)
2234 mask_k|=SSL_kEDH;
2235
2236 if (dh_rsa) mask_k|=SSL_kDHr;
Adam Langley95c29f32014-06-20 12:00:00 -07002237
2238 if (dh_dsa) mask_k|=SSL_kDHd;
Adam Langley95c29f32014-06-20 12:00:00 -07002239
David Benjamin060d9d22014-07-15 00:54:26 -04002240 if (mask_k & (SSL_kDHr|SSL_kDHd))
Adam Langley95c29f32014-06-20 12:00:00 -07002241 mask_a |= SSL_aDH;
2242
2243 if (rsa_enc || rsa_sign)
2244 {
2245 mask_a|=SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002246 }
2247
2248 if (dsa_sign)
2249 {
2250 mask_a|=SSL_aDSS;
Adam Langley95c29f32014-06-20 12:00:00 -07002251 }
2252
2253 mask_a|=SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002254
2255 /* An ECC certificate may be usable for ECDH and/or
2256 * ECDSA cipher suites depending on the key usage extension.
2257 */
2258#ifndef OPENSSL_NO_EC
2259 if (have_ecc_cert)
2260 {
2261 cpk = &c->pkeys[SSL_PKEY_ECC];
2262 x = cpk->x509;
2263 /* This call populates extension flags (ex_flags) */
2264 X509_check_purpose(x, -1, 0);
2265 ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2266 (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1;
2267 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2268 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
2269 if (!(cpk->valid_flags & CERT_PKEY_SIGN))
2270 ecdsa_ok = 0;
2271 ecc_pkey = X509_get_pubkey(x);
Adam Langley95c29f32014-06-20 12:00:00 -07002272 EVP_PKEY_free(ecc_pkey);
2273 if ((x->sig_alg) && (x->sig_alg->algorithm))
2274 {
2275 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2276 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2277 }
2278#ifndef OPENSSL_NO_ECDH
2279 if (ecdh_ok)
2280 {
2281
2282 if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa)
2283 {
2284 mask_k|=SSL_kECDHr;
2285 mask_a|=SSL_aECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002286 }
2287
2288 if (pk_nid == NID_X9_62_id_ecPublicKey)
2289 {
2290 mask_k|=SSL_kECDHe;
2291 mask_a|=SSL_aECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002292 }
2293 }
2294#endif
2295#ifndef OPENSSL_NO_ECDSA
2296 if (ecdsa_ok)
2297 {
2298 mask_a|=SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002299 }
2300#endif
2301 }
2302#endif
2303
2304#ifndef OPENSSL_NO_ECDH
2305 if (have_ecdh_tmp)
2306 {
2307 mask_k|=SSL_kEECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002308 }
2309#endif
2310
Adam Langley95c29f32014-06-20 12:00:00 -07002311 mask_k |= SSL_kPSK;
2312 mask_a |= SSL_aPSK;
Adam Langley95c29f32014-06-20 12:00:00 -07002313
2314 c->mask_k=mask_k;
2315 c->mask_a=mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002316 c->valid=1;
2317 }
2318
2319/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2320#define ku_reject(x, usage) \
2321 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2322
2323#ifndef OPENSSL_NO_EC
2324
2325int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2326 {
2327 unsigned long alg_k, alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002328 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2329 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2330
2331 alg_k = cs->algorithm_mkey;
2332 alg_a = cs->algorithm_auth;
2333
Adam Langley95c29f32014-06-20 12:00:00 -07002334 /* This call populates the ex_flags field correctly */
2335 X509_check_purpose(x, -1, 0);
2336 if ((x->sig_alg) && (x->sig_alg->algorithm))
2337 {
2338 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2339 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2340 }
2341 if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr)
2342 {
2343 /* key usage, if present, must allow key agreement */
2344 if (ku_reject(x, X509v3_KU_KEY_AGREEMENT))
2345 {
2346 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT);
2347 return 0;
2348 }
2349 if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < TLS1_2_VERSION)
2350 {
2351 /* signature alg must be ECDSA */
2352 if (pk_nid != NID_X9_62_id_ecPublicKey)
2353 {
2354 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE);
2355 return 0;
2356 }
2357 }
2358 if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < TLS1_2_VERSION)
2359 {
2360 /* signature alg must be RSA */
2361
2362 if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa)
2363 {
2364 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE);
2365 return 0;
2366 }
2367 }
2368 }
2369 if (alg_a & SSL_aECDSA)
2370 {
2371 /* key usage, if present, must allow signing */
2372 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2373 {
2374 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2375 return 0;
2376 }
2377 }
2378
2379 return 1; /* all checks are ok */
2380 }
2381
2382#endif
2383
2384static int ssl_get_server_cert_index(const SSL *s)
2385 {
2386 int idx;
2387 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2388 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2389 idx = SSL_PKEY_RSA_SIGN;
2390 if (idx == -1)
2391 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2392 return idx;
2393 }
2394
2395CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2396 {
2397 CERT *c;
2398 int i;
2399
2400 c = s->cert;
2401 ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
2402
2403#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2404 /* Broken protocol test: return last used certificate: which may
2405 * mismatch the one expected.
2406 */
2407 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2408 return c->key;
2409#endif
2410
2411 i = ssl_get_server_cert_index(s);
2412
2413 /* This may or may not be an error. */
2414 if (i < 0)
2415 return NULL;
2416
2417 /* May be NULL. */
2418 return &c->pkeys[i];
2419 }
2420
2421EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd)
2422 {
2423 unsigned long alg_a;
2424 CERT *c;
2425 int idx = -1;
2426
2427 alg_a = cipher->algorithm_auth;
2428 c=s->cert;
2429
2430#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2431 /* Broken protocol test: use last key: which may
2432 * mismatch the one expected.
2433 */
2434 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2435 idx = c->key - c->pkeys;
2436 else
2437#endif
2438
2439 if ((alg_a & SSL_aDSS) &&
2440 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
2441 idx = SSL_PKEY_DSA_SIGN;
2442 else if (alg_a & SSL_aRSA)
2443 {
2444 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2445 idx = SSL_PKEY_RSA_SIGN;
2446 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2447 idx = SSL_PKEY_RSA_ENC;
2448 }
2449 else if ((alg_a & SSL_aECDSA) &&
2450 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2451 idx = SSL_PKEY_ECC;
2452 if (idx == -1)
2453 {
2454 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2455 return(NULL);
2456 }
2457 if (pmd)
2458 *pmd = c->pkeys[idx].digest;
2459 return c->pkeys[idx].privatekey;
2460 }
2461
Adam Langley95c29f32014-06-20 12:00:00 -07002462void ssl_update_cache(SSL *s,int mode)
2463 {
2464 int i;
2465
2466 /* If the session_id_length is 0, we are not supposed to cache it,
2467 * and it would be rather hard to do anyway :-) */
2468 if (s->session->session_id_length == 0) return;
2469
2470 i=s->session_ctx->session_cache_mode;
2471 if ((i & mode) && (!s->hit)
2472 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
2473 || SSL_CTX_add_session(s->session_ctx,s->session))
2474 && (s->session_ctx->new_session_cb != NULL))
2475 {
2476 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
2477 if (!s->session_ctx->new_session_cb(s,s->session))
2478 SSL_SESSION_free(s->session);
2479 }
2480
2481 /* auto flush every 255 connections */
2482 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2483 ((i & mode) == mode))
2484 {
2485 if ( (((mode & SSL_SESS_CACHE_CLIENT)
2486 ?s->session_ctx->stats.sess_connect_good
2487 :s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff)
2488 {
2489 SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL));
2490 }
2491 }
2492 }
2493
2494const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
2495 {
2496 return ctx->method;
2497 }
2498
2499const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2500 {
2501 return(s->method);
2502 }
2503
2504int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2505 {
2506 int conn= -1;
2507 int ret=1;
2508
2509 if (s->method != meth)
2510 {
2511 if (s->handshake_func != NULL)
2512 conn=(s->handshake_func == s->method->ssl_connect);
2513
2514 if (s->method->version == meth->version)
2515 s->method=meth;
2516 else
2517 {
2518 s->method->ssl_free(s);
2519 s->method=meth;
2520 ret=s->method->ssl_new(s);
2521 }
2522
2523 if (conn == 1)
2524 s->handshake_func=meth->ssl_connect;
2525 else if (conn == 0)
2526 s->handshake_func=meth->ssl_accept;
2527 }
2528 return(ret);
2529 }
2530
2531int SSL_get_error(const SSL *s,int i)
2532 {
2533 int reason;
2534 unsigned long l;
2535 BIO *bio;
2536
2537 if (i > 0) return(SSL_ERROR_NONE);
2538
2539 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2540 * etc, where we do encode the error */
2541 if ((l=ERR_peek_error()) != 0)
2542 {
2543 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2544 return(SSL_ERROR_SYSCALL);
2545 else
2546 return(SSL_ERROR_SSL);
2547 }
2548
Adam Langleyb2ce0582014-06-20 12:00:00 -07002549 if ((i < 0) && SSL_want_session(s))
2550 return(SSL_ERROR_PENDING_SESSION);
2551
Adam Langleydc9b1412014-06-20 12:00:00 -07002552 if ((i < 0) && SSL_want_certificate(s))
2553 return(SSL_ERROR_PENDING_CERTIFICATE);
2554
Adam Langley95c29f32014-06-20 12:00:00 -07002555 if ((i < 0) && SSL_want_read(s))
2556 {
2557 bio=SSL_get_rbio(s);
2558 if (BIO_should_read(bio))
2559 return(SSL_ERROR_WANT_READ);
2560 else if (BIO_should_write(bio))
2561 /* This one doesn't make too much sense ... We never try
2562 * to write to the rbio, and an application program where
2563 * rbio and wbio are separate couldn't even know what it
2564 * should wait for.
2565 * However if we ever set s->rwstate incorrectly
2566 * (so that we have SSL_want_read(s) instead of
2567 * SSL_want_write(s)) and rbio and wbio *are* the same,
2568 * this test works around that bug; so it might be safer
2569 * to keep it. */
2570 return(SSL_ERROR_WANT_WRITE);
2571 else if (BIO_should_io_special(bio))
2572 {
2573 reason=BIO_get_retry_reason(bio);
2574 if (reason == BIO_RR_CONNECT)
2575 return(SSL_ERROR_WANT_CONNECT);
2576 else if (reason == BIO_RR_ACCEPT)
2577 return(SSL_ERROR_WANT_ACCEPT);
2578 else
2579 return(SSL_ERROR_SYSCALL); /* unknown */
2580 }
2581 }
2582
2583 if ((i < 0) && SSL_want_write(s))
2584 {
2585 bio=SSL_get_wbio(s);
2586 if (BIO_should_write(bio))
2587 return(SSL_ERROR_WANT_WRITE);
2588 else if (BIO_should_read(bio))
2589 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2590 return(SSL_ERROR_WANT_READ);
2591 else if (BIO_should_io_special(bio))
2592 {
2593 reason=BIO_get_retry_reason(bio);
2594 if (reason == BIO_RR_CONNECT)
2595 return(SSL_ERROR_WANT_CONNECT);
2596 else if (reason == BIO_RR_ACCEPT)
2597 return(SSL_ERROR_WANT_ACCEPT);
2598 else
2599 return(SSL_ERROR_SYSCALL);
2600 }
2601 }
2602 if ((i < 0) && SSL_want_x509_lookup(s))
2603 {
2604 return(SSL_ERROR_WANT_X509_LOOKUP);
2605 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002606 if ((i < 0) && SSL_want_channel_id_lookup(s))
2607 {
2608 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2609 }
Adam Langley95c29f32014-06-20 12:00:00 -07002610
2611 if (i == 0)
2612 {
2613 if (s->version == SSL2_VERSION)
2614 {
2615 /* assume it is the socket being closed */
2616 return(SSL_ERROR_ZERO_RETURN);
2617 }
2618 else
2619 {
2620 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2621 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2622 return(SSL_ERROR_ZERO_RETURN);
2623 }
2624 }
2625 return(SSL_ERROR_SYSCALL);
2626 }
2627
2628int SSL_do_handshake(SSL *s)
2629 {
2630 int ret=1;
2631
2632 if (s->handshake_func == NULL)
2633 {
2634 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2635 return(-1);
2636 }
2637
2638 s->method->ssl_renegotiate_check(s);
2639
2640 if (SSL_in_init(s) || SSL_in_before(s))
2641 {
2642 ret=s->handshake_func(s);
2643 }
2644 return(ret);
2645 }
2646
2647/* For the next 2 functions, SSL_clear() sets shutdown and so
2648 * one of these calls will reset it */
2649void SSL_set_accept_state(SSL *s)
2650 {
2651 s->server=1;
2652 s->shutdown=0;
2653 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2654 s->handshake_func=s->method->ssl_accept;
2655 /* clear the current cipher */
2656 ssl_clear_cipher_ctx(s);
2657 ssl_clear_hash_ctx(&s->read_hash);
2658 ssl_clear_hash_ctx(&s->write_hash);
2659 }
2660
2661void SSL_set_connect_state(SSL *s)
2662 {
2663 s->server=0;
2664 s->shutdown=0;
2665 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2666 s->handshake_func=s->method->ssl_connect;
2667 /* clear the current cipher */
2668 ssl_clear_cipher_ctx(s);
2669 ssl_clear_hash_ctx(&s->read_hash);
2670 ssl_clear_hash_ctx(&s->write_hash);
2671 }
2672
2673int ssl_undefined_function(SSL *s)
2674 {
2675 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2676 return(0);
2677 }
2678
2679int ssl_undefined_void_function(void)
2680 {
2681 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2682 return(0);
2683 }
2684
2685int ssl_undefined_const_function(const SSL *s)
2686 {
2687 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2688 return(0);
2689 }
2690
2691SSL_METHOD *ssl_bad_method(int ver)
2692 {
2693 OPENSSL_PUT_ERROR(SSL, ssl_bad_method, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2694 return(NULL);
2695 }
2696
Adam Langley0f4746e2014-08-13 12:26:32 -07002697static const char *ssl_get_version(int version)
Adam Langley95c29f32014-06-20 12:00:00 -07002698 {
Adam Langley0f4746e2014-08-13 12:26:32 -07002699 if (version == TLS1_2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002700 return("TLSv1.2");
Adam Langley0f4746e2014-08-13 12:26:32 -07002701 else if (version == TLS1_1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002702 return("TLSv1.1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002703 else if (version == TLS1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002704 return("TLSv1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002705 else if (version == SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002706 return("SSLv3");
Adam Langley0f4746e2014-08-13 12:26:32 -07002707 else if (version == SSL2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002708 return("SSLv2");
2709 else
2710 return("unknown");
2711 }
2712
Adam Langley0f4746e2014-08-13 12:26:32 -07002713const char *SSL_get_version(const SSL *s)
2714 {
2715 return ssl_get_version(s->version);
2716 }
2717
2718const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2719 {
2720 return ssl_get_version(sess->ssl_version);
2721 }
2722
Adam Langley95c29f32014-06-20 12:00:00 -07002723void ssl_clear_cipher_ctx(SSL *s)
2724 {
2725 if (s->enc_read_ctx != NULL)
2726 {
2727 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2728 OPENSSL_free(s->enc_read_ctx);
2729 s->enc_read_ctx=NULL;
2730 }
2731 if (s->enc_write_ctx != NULL)
2732 {
2733 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2734 OPENSSL_free(s->enc_write_ctx);
2735 s->enc_write_ctx=NULL;
2736 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002737 if (s->aead_read_ctx != NULL)
2738 {
2739 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2740 OPENSSL_free(s->aead_read_ctx);
2741 s->aead_read_ctx = NULL;
2742 }
2743 if (s->aead_write_ctx != NULL)
2744 {
2745 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2746 OPENSSL_free(s->aead_write_ctx);
2747 s->aead_write_ctx = NULL;
2748 }
Adam Langley95c29f32014-06-20 12:00:00 -07002749 }
2750
2751X509 *SSL_get_certificate(const SSL *s)
2752 {
2753 if (s->cert != NULL)
2754 return(s->cert->key->x509);
2755 else
2756 return(NULL);
2757 }
2758
2759EVP_PKEY *SSL_get_privatekey(const SSL *s)
2760 {
2761 if (s->cert != NULL)
2762 return(s->cert->key->privatekey);
2763 else
2764 return(NULL);
2765 }
2766
2767X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2768 {
2769 if (ctx->cert != NULL)
2770 return ctx->cert->key->x509;
2771 else
2772 return NULL;
2773 }
2774
2775EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2776 {
2777 if (ctx->cert != NULL)
2778 return ctx->cert->key->privatekey;
2779 else
2780 return NULL ;
2781 }
2782
2783const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2784 {
2785 if ((s->session != NULL) && (s->session->cipher != NULL))
2786 return(s->session->cipher);
2787 return(NULL);
2788 }
2789const void *SSL_get_current_compression(SSL *s)
2790 {
2791 return NULL;
2792 }
2793const void *SSL_get_current_expansion(SSL *s)
2794 {
2795 return NULL;
2796 }
2797
2798int ssl_init_wbio_buffer(SSL *s,int push)
2799 {
2800 BIO *bbio;
2801
2802 if (s->bbio == NULL)
2803 {
2804 bbio=BIO_new(BIO_f_buffer());
2805 if (bbio == NULL) return(0);
2806 s->bbio=bbio;
2807 }
2808 else
2809 {
2810 bbio=s->bbio;
2811 if (s->bbio == s->wbio)
2812 s->wbio=BIO_pop(s->wbio);
2813 }
2814 (void)BIO_reset(bbio);
2815/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2816 if (!BIO_set_read_buffer_size(bbio,1))
2817 {
2818 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2819 return(0);
2820 }
2821 if (push)
2822 {
2823 if (s->wbio != bbio)
2824 s->wbio=BIO_push(bbio,s->wbio);
2825 }
2826 else
2827 {
2828 if (s->wbio == bbio)
2829 s->wbio=BIO_pop(bbio);
2830 }
2831 return(1);
2832 }
2833
2834void ssl_free_wbio_buffer(SSL *s)
2835 {
2836 if (s->bbio == NULL) return;
2837
2838 if (s->bbio == s->wbio)
2839 {
2840 /* remove buffering */
2841 s->wbio=BIO_pop(s->wbio);
2842#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2843 assert(s->wbio != NULL);
2844#endif
2845 }
2846 BIO_free(s->bbio);
2847 s->bbio=NULL;
2848 }
2849
2850void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2851 {
2852 ctx->quiet_shutdown=mode;
2853 }
2854
2855int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2856 {
2857 return(ctx->quiet_shutdown);
2858 }
2859
2860void SSL_set_quiet_shutdown(SSL *s,int mode)
2861 {
2862 s->quiet_shutdown=mode;
2863 }
2864
2865int SSL_get_quiet_shutdown(const SSL *s)
2866 {
2867 return(s->quiet_shutdown);
2868 }
2869
2870void SSL_set_shutdown(SSL *s,int mode)
2871 {
2872 s->shutdown=mode;
2873 }
2874
2875int SSL_get_shutdown(const SSL *s)
2876 {
2877 return(s->shutdown);
2878 }
2879
2880int SSL_version(const SSL *s)
2881 {
2882 return(s->version);
2883 }
2884
2885SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2886 {
2887 return(ssl->ctx);
2888 }
2889
2890SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2891 {
2892 if (ssl->ctx == ctx)
2893 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002894 if (ctx == NULL)
2895 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002896 if (ssl->cert != NULL)
2897 ssl_cert_free(ssl->cert);
2898 ssl->cert = ssl_cert_dup(ctx->cert);
2899 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2900 if (ssl->ctx != NULL)
2901 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2902 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07002903
2904 ssl->sid_ctx_length = ctx->sid_ctx_length;
2905 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2906 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2907
Adam Langley95c29f32014-06-20 12:00:00 -07002908 return(ssl->ctx);
2909 }
2910
2911#ifndef OPENSSL_NO_STDIO
2912int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2913 {
2914 return(X509_STORE_set_default_paths(ctx->cert_store));
2915 }
2916
2917int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2918 const char *CApath)
2919 {
2920 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2921 }
2922#endif
2923
2924void SSL_set_info_callback(SSL *ssl,
2925 void (*cb)(const SSL *ssl,int type,int val))
2926 {
2927 ssl->info_callback=cb;
2928 }
2929
2930/* One compiler (Diab DCC) doesn't like argument names in returned
2931 function pointer. */
2932void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2933 {
2934 return ssl->info_callback;
2935 }
2936
2937int SSL_state(const SSL *ssl)
2938 {
2939 return(ssl->state);
2940 }
2941
2942void SSL_set_state(SSL *ssl, int state)
2943 {
2944 ssl->state = state;
2945 }
2946
2947void SSL_set_verify_result(SSL *ssl,long arg)
2948 {
2949 ssl->verify_result=arg;
2950 }
2951
2952long SSL_get_verify_result(const SSL *ssl)
2953 {
2954 return(ssl->verify_result);
2955 }
2956
2957int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2958 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2959 {
2960 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2961 new_func, dup_func, free_func);
2962 }
2963
2964int SSL_set_ex_data(SSL *s,int idx,void *arg)
2965 {
2966 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2967 }
2968
2969void *SSL_get_ex_data(const SSL *s,int idx)
2970 {
2971 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2972 }
2973
2974int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2975 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2976 {
2977 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2978 new_func, dup_func, free_func);
2979 }
2980
2981int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2982 {
2983 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2984 }
2985
2986void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2987 {
2988 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2989 }
2990
2991int ssl_ok(SSL *s)
2992 {
2993 return(1);
2994 }
2995
2996X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2997 {
2998 return(ctx->cert_store);
2999 }
3000
3001void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
3002 {
3003 if (ctx->cert_store != NULL)
3004 X509_STORE_free(ctx->cert_store);
3005 ctx->cert_store=store;
3006 }
3007
3008int SSL_want(const SSL *s)
3009 {
3010 return(s->rwstate);
3011 }
3012
3013/*!
3014 * \brief Set the callback for generating temporary RSA keys.
3015 * \param ctx the SSL context.
3016 * \param cb the callback
3017 */
3018
Adam Langley95c29f32014-06-20 12:00:00 -07003019void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
3020 int is_export,
3021 int keylength))
3022 {
3023 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3024 }
3025
3026void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
3027 int is_export,
3028 int keylength))
3029 {
3030 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3031 }
Adam Langley95c29f32014-06-20 12:00:00 -07003032
3033#ifdef DOXYGEN
3034/*!
3035 * \brief The RSA temporary key callback function.
3036 * \param ssl the SSL session.
3037 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
3038 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
3039 * of the required key in bits.
3040 * \return the temporary RSA key.
3041 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
3042 */
3043
3044RSA *cb(SSL *ssl,int is_export,int keylength)
3045 {}
3046#endif
3047
3048/*!
3049 * \brief Set the callback for generating temporary DH keys.
3050 * \param ctx the SSL context.
3051 * \param dh the callback
3052 */
3053
3054#ifndef OPENSSL_NO_DH
3055void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
3056 int keylength))
3057 {
3058 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3059 }
3060
3061void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
3062 int keylength))
3063 {
3064 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3065 }
3066#endif
3067
3068#ifndef OPENSSL_NO_ECDH
3069void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
3070 int keylength))
3071 {
3072 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
3073 }
3074
3075void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
3076 int keylength))
3077 {
3078 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
3079 }
3080#endif
3081
Adam Langley95c29f32014-06-20 12:00:00 -07003082int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
3083 {
3084 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
3085 {
3086 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
3087 return 0;
3088 }
3089 if (ctx->psk_identity_hint != NULL)
3090 OPENSSL_free(ctx->psk_identity_hint);
3091 if (identity_hint != NULL)
3092 {
3093 ctx->psk_identity_hint = BUF_strdup(identity_hint);
3094 if (ctx->psk_identity_hint == NULL)
3095 return 0;
3096 }
3097 else
3098 ctx->psk_identity_hint = NULL;
3099 return 1;
3100 }
3101
3102int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
3103 {
3104 if (s == NULL)
3105 return 0;
3106
Adam Langley95c29f32014-06-20 12:00:00 -07003107 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
3108 {
3109 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
3110 return 0;
3111 }
Adam Langley0289c732014-06-20 12:00:00 -07003112
3113 /* Clear hint in SSL and associated SSL_SESSION (if any). */
3114 if (s->psk_identity_hint != NULL)
3115 {
3116 OPENSSL_free(s->psk_identity_hint);
3117 s->psk_identity_hint = NULL;
3118 }
3119 if (s->session != NULL && s->session->psk_identity_hint != NULL)
3120 {
Adam Langley95c29f32014-06-20 12:00:00 -07003121 OPENSSL_free(s->session->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -07003122 s->session->psk_identity_hint = NULL;
3123 }
3124
Adam Langley95c29f32014-06-20 12:00:00 -07003125 if (identity_hint != NULL)
3126 {
Adam Langley0289c732014-06-20 12:00:00 -07003127 /* The hint is stored in SSL and SSL_SESSION with the one in
3128 * SSL_SESSION taking precedence. Thus, if SSL_SESSION is avaiable,
3129 * we store the hint there, otherwise we store it in SSL. */
3130 if (s->session != NULL)
3131 {
3132 s->session->psk_identity_hint = BUF_strdup(identity_hint);
3133 if (s->session->psk_identity_hint == NULL)
3134 return 0;
3135 }
3136 else
3137 {
3138 s->psk_identity_hint = BUF_strdup(identity_hint);
3139 if (s->psk_identity_hint == NULL)
3140 return 0;
3141 }
Adam Langley95c29f32014-06-20 12:00:00 -07003142 }
Adam Langley95c29f32014-06-20 12:00:00 -07003143 return 1;
3144 }
3145
3146const char *SSL_get_psk_identity_hint(const SSL *s)
3147 {
Adam Langley0289c732014-06-20 12:00:00 -07003148 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07003149 return NULL;
Adam Langley0289c732014-06-20 12:00:00 -07003150 /* The hint is stored in SSL and SSL_SESSION with the one in SSL_SESSION
3151 * taking precedence. */
3152 if (s->session != NULL)
3153 return(s->session->psk_identity_hint);
3154 return(s->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07003155 }
3156
3157const char *SSL_get_psk_identity(const SSL *s)
3158 {
3159 if (s == NULL || s->session == NULL)
3160 return NULL;
3161 return(s->session->psk_identity);
3162 }
3163
3164void SSL_set_psk_client_callback(SSL *s,
3165 unsigned int (*cb)(SSL *ssl, const char *hint,
3166 char *identity, unsigned int max_identity_len, unsigned char *psk,
3167 unsigned int max_psk_len))
3168 {
3169 s->psk_client_callback = cb;
3170 }
3171
3172void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
3173 unsigned int (*cb)(SSL *ssl, const char *hint,
3174 char *identity, unsigned int max_identity_len, unsigned char *psk,
3175 unsigned int max_psk_len))
3176 {
3177 ctx->psk_client_callback = cb;
3178 }
3179
3180void SSL_set_psk_server_callback(SSL *s,
3181 unsigned int (*cb)(SSL *ssl, const char *identity,
3182 unsigned char *psk, unsigned int max_psk_len))
3183 {
3184 s->psk_server_callback = cb;
3185 }
3186
3187void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
3188 unsigned int (*cb)(SSL *ssl, const char *identity,
3189 unsigned char *psk, unsigned int max_psk_len))
3190 {
3191 ctx->psk_server_callback = cb;
3192 }
Adam Langley95c29f32014-06-20 12:00:00 -07003193
3194void 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))
3195 {
3196 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3197 }
3198void 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))
3199 {
3200 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3201 }
3202
Adam Langleyadb739e2014-06-20 12:00:00 -07003203int SSL_cutthrough_complete(const SSL *s)
3204 {
3205 return (!s->server && /* cutthrough only applies to clients */
3206 !s->hit && /* full-handshake */
3207 s->version >= SSL3_VERSION &&
3208 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3209 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3210 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3211 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3212 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
David Benjamin7e3305e2014-07-28 14:52:32 -04003213 s->state == SSL3_ST_CR_CHANGE ||
Adam Langleyadb739e2014-06-20 12:00:00 -07003214 s->state == SSL3_ST_CR_FINISHED_A));
3215 }
3216
Adam Langley95f22882014-06-20 12:00:00 -07003217void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3218 size_t* ssl_session_size)
3219{
3220 *ssl_size = sizeof(SSL);
3221 *ssl_ctx_size = sizeof(SSL_CTX);
3222 *ssl_session_size = sizeof(SSL_SESSION);
3223}
3224
Adam Langleyadb739e2014-06-20 12:00:00 -07003225int ssl3_can_cutthrough(const SSL *s)
3226 {
3227 const SSL_CIPHER *c;
3228
3229 /* require a strong enough cipher */
3230 if (SSL_get_cipher_bits(s, NULL) < 128)
3231 return 0;
3232
3233 /* require ALPN or NPN extension */
3234 if (!s->s3->alpn_selected
3235#ifndef OPENSSL_NO_NEXTPROTONEG
3236 && !s->s3->next_proto_neg_seen
3237#endif
3238 )
3239 {
3240 return 0;
3241 }
3242
3243 /* require a forward-secret cipher */
3244 c = SSL_get_current_cipher(s);
3245 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3246 c->algorithm_mkey != SSL_kEECDH))
3247 {
3248 return 0;
3249 }
3250
3251 return 1;
3252 }
3253
Adam Langley29707792014-06-20 12:00:00 -07003254/* ssl_get_max_version returns the maximum SSL/TLS version number supported by
3255 * |s|, or zero if all versions are disabled. */
3256int ssl_get_max_version(const SSL *s)
3257 {
3258 /* Only one version supported for DTLS. */
3259 if (s->version == DTLS1_VERSION)
3260 return DTLS1_VERSION;
3261
3262 if (!(s->options & SSL_OP_NO_TLSv1_2))
3263 return TLS1_2_VERSION;
3264 if (!(s->options & SSL_OP_NO_TLSv1_1))
3265 return TLS1_1_VERSION;
3266 if (!(s->options & SSL_OP_NO_TLSv1))
3267 return TLS1_VERSION;
3268 if (!(s->options & SSL_OP_NO_SSLv3))
3269 return SSL3_VERSION;
3270 if (!(s->options & SSL_OP_NO_SSLv2))
3271 return SSL2_VERSION;
3272 return 0;
3273 }
3274
Adam Langley95c29f32014-06-20 12:00:00 -07003275/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3276 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3277 * any. If EVP_MD pointer is passed, initializes ctx with this md
3278 * Returns newly allocated ctx;
3279 */
3280
3281EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3282{
3283 ssl_clear_hash_ctx(hash);
3284 *hash = EVP_MD_CTX_create();
3285 if (md) EVP_DigestInit_ex(*hash,md,NULL);
3286 return *hash;
3287}
3288void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3289{
3290
3291 if (*hash) EVP_MD_CTX_destroy(*hash);
3292 *hash=NULL;
3293}
3294
3295void SSL_set_debug(SSL *s, int debug)
3296 {
3297 s->debug = debug;
3298 }
3299
3300int SSL_cache_hit(SSL *s)
3301 {
3302 return s->hit;
3303 }
3304
3305int SSL_is_server(SSL *s)
3306 {
3307 return s->server;
3308 }