blob: 47c91fcd12ff30dd2484d556c30c2f6b5aa414b4 [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
David Benjamine1136082014-09-20 12:28:58 -0400155/* Some error codes are special. Ensure the make_errors.go script
156 * never regresses this. */
157OPENSSL_COMPILE_ASSERT(
158 SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
159 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
160 ssl_alert_reason_code_mismatch);
161
Adam Langley95c29f32014-06-20 12:00:00 -0700162int SSL_clear(SSL *s)
163 {
164
165 if (s->method == NULL)
166 {
167 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
168 return(0);
169 }
170
171 if (ssl_clear_bad_session(s))
172 {
173 SSL_SESSION_free(s->session);
174 s->session=NULL;
175 }
176
Adam Langley95c29f32014-06-20 12:00:00 -0700177 s->hit=0;
178 s->shutdown=0;
179
180#if 0 /* Disabled since version 1.10 of this file (early return not
181 * needed because SSL_clear is not called when doing renegotiation) */
182 /* This is set if we are doing dynamic renegotiation so keep
183 * the old cipher. It is sort of a SSL_clear_lite :-) */
184 if (s->renegotiate) return(1);
185#else
186 if (s->renegotiate)
187 {
188 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
189 return 0;
190 }
191#endif
192
David Benjaminf34a0092014-11-30 02:01:26 -0500193 /* SSL_clear may be called before or after the |s| is initialized in
194 * either accept or connect state. In the latter case, SSL_clear should
195 * preserve the half and reset |s->state| accordingly. */
196 if (s->handshake_func != NULL)
197 {
198 if (s->server)
199 SSL_set_accept_state(s);
200 else
201 SSL_set_connect_state(s);
202 }
203 else
204 {
205 assert(s->state == 0);
206 }
Adam Langley95c29f32014-06-20 12:00:00 -0700207
Adam Langley95c29f32014-06-20 12:00:00 -0700208 s->rwstate=SSL_NOTHING;
209 s->rstate=SSL_ST_READ_HEADER;
210#if 0
211 s->read_ahead=s->ctx->read_ahead;
212#endif
213
214 if (s->init_buf != NULL)
215 {
216 BUF_MEM_free(s->init_buf);
217 s->init_buf=NULL;
218 }
219
220 ssl_clear_cipher_ctx(s);
221 ssl_clear_hash_ctx(&s->read_hash);
222 ssl_clear_hash_ctx(&s->write_hash);
223
David Benjaminbb15e3d2014-11-29 16:03:41 -0500224 s->method->ssl_clear(s);
David Benjamin82c9e902014-12-12 15:55:27 -0500225 s->client_version=s->version;
Adam Langley95c29f32014-06-20 12:00:00 -0700226 return(1);
227 }
228
Adam Langley95c29f32014-06-20 12:00:00 -0700229SSL *SSL_new(SSL_CTX *ctx)
230 {
231 SSL *s;
232
233 if (ctx == NULL)
234 {
235 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
236 return(NULL);
237 }
238 if (ctx->method == NULL)
239 {
240 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
241 return(NULL);
242 }
243
244 s=(SSL *)OPENSSL_malloc(sizeof(SSL));
245 if (s == NULL) goto err;
246 memset(s,0,sizeof(SSL));
247
David Benjamin1eb367c2014-12-12 18:17:51 -0500248 s->min_version = ctx->min_version;
249 s->max_version = ctx->max_version;
250
Adam Langley95c29f32014-06-20 12:00:00 -0700251 s->options=ctx->options;
252 s->mode=ctx->mode;
253 s->max_cert_list=ctx->max_cert_list;
254
255 if (ctx->cert != NULL)
256 {
257 /* Earlier library versions used to copy the pointer to
258 * the CERT, not its contents; only when setting new
259 * parameters for the per-SSL copy, ssl_cert_new would be
260 * called (and the direct reference to the per-SSL_CTX
261 * settings would be lost, but those still were indirectly
262 * accessed for various purposes, and for that reason they
263 * used to be known as s->ctx->default_cert).
264 * Now we don't look at the SSL_CTX's CERT after having
265 * duplicated it once. */
266
267 s->cert = ssl_cert_dup(ctx->cert);
268 if (s->cert == NULL)
269 goto err;
270 }
271 else
272 s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
273
274 s->read_ahead=ctx->read_ahead;
275 s->msg_callback=ctx->msg_callback;
276 s->msg_callback_arg=ctx->msg_callback_arg;
277 s->verify_mode=ctx->verify_mode;
278#if 0
279 s->verify_depth=ctx->verify_depth;
280#endif
281 s->sid_ctx_length=ctx->sid_ctx_length;
282 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
283 memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
284 s->verify_callback=ctx->default_verify_callback;
285 s->generate_session_id=ctx->generate_session_id;
286
287 s->param = X509_VERIFY_PARAM_new();
288 if (!s->param)
289 goto err;
290 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
291#if 0
292 s->purpose = ctx->purpose;
293 s->trust = ctx->trust;
294#endif
295 s->quiet_shutdown=ctx->quiet_shutdown;
296 s->max_send_fragment = ctx->max_send_fragment;
297
298 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
299 s->ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700300 s->tlsext_debug_cb = 0;
301 s->tlsext_debug_arg = NULL;
302 s->tlsext_ticket_expected = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700303 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
304 s->initial_ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700305 if (ctx->tlsext_ecpointformatlist)
306 {
307 s->tlsext_ecpointformatlist =
308 BUF_memdup(ctx->tlsext_ecpointformatlist,
309 ctx->tlsext_ecpointformatlist_length);
310 if (!s->tlsext_ecpointformatlist)
311 goto err;
312 s->tlsext_ecpointformatlist_length =
313 ctx->tlsext_ecpointformatlist_length;
314 }
315 if (ctx->tlsext_ellipticcurvelist)
316 {
317 s->tlsext_ellipticcurvelist =
318 BUF_memdup(ctx->tlsext_ellipticcurvelist,
David Benjamin072334d2014-07-13 16:24:27 -0400319 ctx->tlsext_ellipticcurvelist_length * 2);
Adam Langley95c29f32014-06-20 12:00:00 -0700320 if (!s->tlsext_ellipticcurvelist)
321 goto err;
322 s->tlsext_ellipticcurvelist_length =
323 ctx->tlsext_ellipticcurvelist_length;
324 }
Adam Langley95c29f32014-06-20 12:00:00 -0700325 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700326
327 if (s->ctx->alpn_client_proto_list)
328 {
David Benjamin072c9532014-07-26 11:44:25 -0400329 s->alpn_client_proto_list = BUF_memdup(
330 s->ctx->alpn_client_proto_list,
331 s->ctx->alpn_client_proto_list_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700332 if (s->alpn_client_proto_list == NULL)
333 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700334 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
335 }
Adam Langley95c29f32014-06-20 12:00:00 -0700336
337 s->verify_result=X509_V_OK;
338
339 s->method=ctx->method;
340
341 if (!s->method->ssl_new(s))
342 goto err;
David Benjamine99e9122014-12-11 01:46:01 -0500343 s->enc_method = ssl3_get_enc_method(s->version);
344 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700345
346 s->references=1;
Adam Langley95c29f32014-06-20 12:00:00 -0700347
348 SSL_clear(s);
349
350 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
351
Adam Langley0289c732014-06-20 12:00:00 -0700352 s->psk_identity_hint = NULL;
353 if (ctx->psk_identity_hint)
354 {
355 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
356 if (s->psk_identity_hint == NULL)
357 goto err;
358 }
Adam Langley95c29f32014-06-20 12:00:00 -0700359 s->psk_client_callback=ctx->psk_client_callback;
360 s->psk_server_callback=ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700361
David Benjaminff42cc12014-11-26 00:16:14 -0500362 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
363 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200364
Adam Langley95c29f32014-06-20 12:00:00 -0700365 return(s);
366err:
367 if (s != NULL)
368 {
369 if (s->cert != NULL)
370 ssl_cert_free(s->cert);
371 if (s->ctx != NULL)
372 SSL_CTX_free(s->ctx); /* decrement reference count */
373 OPENSSL_free(s);
374 }
375 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
376 return(NULL);
377 }
378
379int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
380 unsigned int sid_ctx_len)
381 {
382 if(sid_ctx_len > sizeof ctx->sid_ctx)
383 {
384 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
385 return 0;
386 }
387 ctx->sid_ctx_length=sid_ctx_len;
388 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
389
390 return 1;
391 }
392
393int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
394 unsigned int sid_ctx_len)
395 {
396 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
397 {
398 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
399 return 0;
400 }
401 ssl->sid_ctx_length=sid_ctx_len;
402 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
403
404 return 1;
405 }
406
407int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
408 {
409 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
410 ctx->generate_session_id = cb;
411 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
412 return 1;
413 }
414
415int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
416 {
417 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
418 ssl->generate_session_id = cb;
419 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
420 return 1;
421 }
422
423int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
424 unsigned int id_len)
425 {
426 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
427 * we can "construct" a session to give us the desired check - ie. to
428 * find if there's a session in the hash table that would conflict with
429 * any new session built out of this id/id_len and the ssl_version in
430 * use by this SSL. */
431 SSL_SESSION r, *p;
432
433 if(id_len > sizeof r.session_id)
434 return 0;
435
436 r.ssl_version = ssl->version;
437 r.session_id_length = id_len;
438 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700439
440 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
441 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
442 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
443 return (p != NULL);
444 }
445
446int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
447 {
448 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
449 }
450
451int SSL_set_purpose(SSL *s, int purpose)
452 {
453 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
454 }
455
456int SSL_CTX_set_trust(SSL_CTX *s, int trust)
457 {
458 return X509_VERIFY_PARAM_set_trust(s->param, trust);
459 }
460
461int SSL_set_trust(SSL *s, int trust)
462 {
463 return X509_VERIFY_PARAM_set_trust(s->param, trust);
464 }
465
466int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
467 {
468 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
469 }
470
471int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
472 {
473 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
474 }
475
Adam Langley858a88d2014-06-20 12:00:00 -0700476void ssl_cipher_preference_list_free(
477 struct ssl_cipher_preference_list_st *cipher_list)
478 {
479 sk_SSL_CIPHER_free(cipher_list->ciphers);
480 OPENSSL_free(cipher_list->in_group_flags);
481 OPENSSL_free(cipher_list);
482 }
483
484struct ssl_cipher_preference_list_st*
485ssl_cipher_preference_list_dup(
486 struct ssl_cipher_preference_list_st *cipher_list)
487 {
488 struct ssl_cipher_preference_list_st* ret = NULL;
489 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
490
491 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
492 if (!ret)
493 goto err;
494 ret->ciphers = NULL;
495 ret->in_group_flags = NULL;
496 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
497 if (!ret->ciphers)
498 goto err;
David Benjamin072c9532014-07-26 11:44:25 -0400499 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
Adam Langley858a88d2014-06-20 12:00:00 -0700500 if (!ret->in_group_flags)
501 goto err;
Adam Langley858a88d2014-06-20 12:00:00 -0700502 return ret;
503
504err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400505 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700506 sk_SSL_CIPHER_free(ret->ciphers);
507 if (ret)
508 OPENSSL_free(ret);
509 return NULL;
510 }
511
512struct ssl_cipher_preference_list_st*
513ssl_cipher_preference_list_from_ciphers(STACK_OF(SSL_CIPHER) *ciphers)
514 {
515 struct ssl_cipher_preference_list_st* ret = NULL;
516 size_t n = sk_SSL_CIPHER_num(ciphers);
517
518 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
519 if (!ret)
520 goto err;
521 ret->ciphers = NULL;
522 ret->in_group_flags = NULL;
523 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
524 if (!ret->ciphers)
525 goto err;
526 ret->in_group_flags = OPENSSL_malloc(n);
527 if (!ret->in_group_flags)
528 goto err;
529 memset(ret->in_group_flags, 0, n);
530 return ret;
531
532err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400533 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700534 sk_SSL_CIPHER_free(ret->ciphers);
535 if (ret)
536 OPENSSL_free(ret);
537 return NULL;
538 }
539
Adam Langley95c29f32014-06-20 12:00:00 -0700540X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
541 {
542 return ctx->param;
543 }
544
545X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
546 {
547 return ssl->param;
548 }
549
550void SSL_certs_clear(SSL *s)
551 {
552 ssl_cert_clear_certs(s->cert);
553 }
554
555void SSL_free(SSL *s)
556 {
557 int i;
558
559 if(s == NULL)
560 return;
561
562 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
563#ifdef REF_PRINT
564 REF_PRINT("SSL",s);
565#endif
566 if (i > 0) return;
567#ifdef REF_CHECK
568 if (i < 0)
569 {
570 fprintf(stderr,"SSL_free, bad reference count\n");
571 abort(); /* ok */
572 }
573#endif
574
575 if (s->param)
576 X509_VERIFY_PARAM_free(s->param);
577
578 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
579
580 if (s->bbio != NULL)
581 {
582 /* If the buffering BIO is in place, pop it off */
583 if (s->bbio == s->wbio)
584 {
585 s->wbio=BIO_pop(s->wbio);
586 }
587 BIO_free(s->bbio);
588 s->bbio=NULL;
589 }
590 if (s->rbio != NULL)
591 BIO_free_all(s->rbio);
592 if ((s->wbio != NULL) && (s->wbio != s->rbio))
593 BIO_free_all(s->wbio);
594
595 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
596
597 /* add extra stuff */
Adam Langley858a88d2014-06-20 12:00:00 -0700598 if (s->cipher_list != NULL)
599 ssl_cipher_preference_list_free(s->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -0700600 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
601
602 /* Make the next call work :-) */
603 if (s->session != NULL)
604 {
605 ssl_clear_bad_session(s);
606 SSL_SESSION_free(s->session);
607 }
608
609 ssl_clear_cipher_ctx(s);
610 ssl_clear_hash_ctx(&s->read_hash);
611 ssl_clear_hash_ctx(&s->write_hash);
612
613 if (s->cert != NULL) ssl_cert_free(s->cert);
614 /* Free up if allocated */
615
Adam Langley95c29f32014-06-20 12:00:00 -0700616 if (s->tlsext_hostname)
617 OPENSSL_free(s->tlsext_hostname);
618 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700619 if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
620 if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -0700621 if (s->alpn_client_proto_list)
622 OPENSSL_free(s->alpn_client_proto_list);
Adam Langley1258b6a2014-06-20 12:00:00 -0700623 if (s->tlsext_channel_id_private)
624 EVP_PKEY_free(s->tlsext_channel_id_private);
Adam Langley95c29f32014-06-20 12:00:00 -0700625
Adam Langley0289c732014-06-20 12:00:00 -0700626 if (s->psk_identity_hint)
627 OPENSSL_free(s->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -0700628
Adam Langley95c29f32014-06-20 12:00:00 -0700629 if (s->client_CA != NULL)
630 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
631
632 if (s->method != NULL) s->method->ssl_free(s);
633
634 if (s->ctx) SSL_CTX_free(s->ctx);
635
Adam Langley95c29f32014-06-20 12:00:00 -0700636 if (s->next_proto_negotiated)
637 OPENSSL_free(s->next_proto_negotiated);
Adam Langley95c29f32014-06-20 12:00:00 -0700638
639 if (s->srtp_profiles)
640 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
641
Adam Langley95c29f32014-06-20 12:00:00 -0700642 OPENSSL_free(s);
643 }
644
645void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
646 {
647 /* If the output buffering BIO is still in place, remove it
648 */
649 if (s->bbio != NULL)
650 {
651 if (s->wbio == s->bbio)
652 {
653 s->wbio=s->wbio->next_bio;
654 s->bbio->next_bio=NULL;
655 }
656 }
657 if ((s->rbio != NULL) && (s->rbio != rbio))
658 BIO_free_all(s->rbio);
659 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
660 BIO_free_all(s->wbio);
661 s->rbio=rbio;
662 s->wbio=wbio;
663 }
664
665BIO *SSL_get_rbio(const SSL *s)
666 { return(s->rbio); }
667
668BIO *SSL_get_wbio(const SSL *s)
669 { return(s->wbio); }
670
671int SSL_get_fd(const SSL *s)
672 {
673 return(SSL_get_rfd(s));
674 }
675
676int SSL_get_rfd(const SSL *s)
677 {
678 int ret= -1;
679 BIO *b,*r;
680
681 b=SSL_get_rbio(s);
682 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
683 if (r != NULL)
684 BIO_get_fd(r,&ret);
685 return(ret);
686 }
687
688int SSL_get_wfd(const SSL *s)
689 {
690 int ret= -1;
691 BIO *b,*r;
692
693 b=SSL_get_wbio(s);
694 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
695 if (r != NULL)
696 BIO_get_fd(r,&ret);
697 return(ret);
698 }
699
700#ifndef OPENSSL_NO_SOCK
701int SSL_set_fd(SSL *s,int fd)
702 {
703 int ret=0;
704 BIO *bio=NULL;
705
706 bio=BIO_new(BIO_s_fd());
707
708 if (bio == NULL)
709 {
710 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
711 goto err;
712 }
713 BIO_set_fd(bio,fd,BIO_NOCLOSE);
714 SSL_set_bio(s,bio,bio);
715 ret=1;
716err:
717 return(ret);
718 }
719
720int SSL_set_wfd(SSL *s,int fd)
721 {
722 int ret=0;
723 BIO *bio=NULL;
724
725 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_FD)
726 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
727 {
728 bio=BIO_new(BIO_s_fd());
729
730 if (bio == NULL)
731 {
732 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
733 goto err;
734 }
735 BIO_set_fd(bio,fd,BIO_NOCLOSE);
736 SSL_set_bio(s,SSL_get_rbio(s),bio);
737 }
738 else
739 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
740 ret=1;
741err:
742 return(ret);
743 }
744
745int SSL_set_rfd(SSL *s,int fd)
746 {
747 int ret=0;
748 BIO *bio=NULL;
749
750 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_FD)
751 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
752 {
753 bio=BIO_new(BIO_s_fd());
754
755 if (bio == NULL)
756 {
757 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
758 goto err;
759 }
760 BIO_set_fd(bio,fd,BIO_NOCLOSE);
761 SSL_set_bio(s,bio,SSL_get_wbio(s));
762 }
763 else
764 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
765 ret=1;
766err:
767 return(ret);
768 }
769#endif
770
771
772/* return length of latest Finished message we sent, copy to 'buf' */
773size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
774 {
775 size_t ret = 0;
776
777 if (s->s3 != NULL)
778 {
779 ret = s->s3->tmp.finish_md_len;
780 if (count > ret)
781 count = ret;
782 memcpy(buf, s->s3->tmp.finish_md, count);
783 }
784 return ret;
785 }
786
787/* return length of latest Finished message we expected, copy to 'buf' */
788size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
789 {
790 size_t ret = 0;
791
792 if (s->s3 != NULL)
793 {
794 ret = s->s3->tmp.peer_finish_md_len;
795 if (count > ret)
796 count = ret;
797 memcpy(buf, s->s3->tmp.peer_finish_md, count);
798 }
799 return ret;
800 }
801
802
803int SSL_get_verify_mode(const SSL *s)
804 {
805 return(s->verify_mode);
806 }
807
808int SSL_get_verify_depth(const SSL *s)
809 {
810 return X509_VERIFY_PARAM_get_depth(s->param);
811 }
812
813int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
814 {
815 return(s->verify_callback);
816 }
817
818int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
819 {
820 return(ctx->verify_mode);
821 }
822
823int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
824 {
825 return X509_VERIFY_PARAM_get_depth(ctx->param);
826 }
827
828int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
829 {
830 return(ctx->default_verify_callback);
831 }
832
833void SSL_set_verify(SSL *s,int mode,
834 int (*callback)(int ok,X509_STORE_CTX *ctx))
835 {
836 s->verify_mode=mode;
837 if (callback != NULL)
838 s->verify_callback=callback;
839 }
840
841void SSL_set_verify_depth(SSL *s,int depth)
842 {
843 X509_VERIFY_PARAM_set_depth(s->param, depth);
844 }
845
846void SSL_set_read_ahead(SSL *s,int yes)
847 {
848 s->read_ahead=yes;
849 }
850
851int SSL_get_read_ahead(const SSL *s)
852 {
853 return(s->read_ahead);
854 }
855
856int SSL_pending(const SSL *s)
857 {
858 /* SSL_pending cannot work properly if read-ahead is enabled
859 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
860 * and it is impossible to fix since SSL_pending cannot report
861 * errors that may be observed while scanning the new data.
862 * (Note that SSL_pending() is often used as a boolean value,
863 * so we'd better not return -1.)
864 */
865 return(s->method->ssl_pending(s));
866 }
867
868X509 *SSL_get_peer_certificate(const SSL *s)
869 {
870 X509 *r;
871
872 if ((s == NULL) || (s->session == NULL))
873 r=NULL;
874 else
875 r=s->session->peer;
876
David Benjamin150c6172014-08-05 22:22:49 -0400877 if (r == NULL)
878 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700879
David Benjamin150c6172014-08-05 22:22:49 -0400880 return X509_up_ref(r);
Adam Langley95c29f32014-06-20 12:00:00 -0700881 }
882
883STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
884 {
885 STACK_OF(X509) *r;
886
887 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
888 r=NULL;
889 else
890 r=s->session->sess_cert->cert_chain;
891
892 /* If we are a client, cert_chain includes the peer's own
893 * certificate; if we are a server, it does not. */
894
895 return(r);
896 }
897
Adam Langley95c29f32014-06-20 12:00:00 -0700898/* Fix this so it checks all the valid key/cert options */
899int SSL_CTX_check_private_key(const SSL_CTX *ctx)
900 {
901 if ( (ctx == NULL) ||
902 (ctx->cert == NULL) ||
903 (ctx->cert->key->x509 == NULL))
904 {
905 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
906 return(0);
907 }
908 if (ctx->cert->key->privatekey == NULL)
909 {
910 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
911 return(0);
912 }
913 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
914 }
915
916/* Fix this function so that it takes an optional type parameter */
917int SSL_check_private_key(const SSL *ssl)
918 {
919 if (ssl == NULL)
920 {
921 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
922 return(0);
923 }
924 if (ssl->cert == NULL)
925 {
926 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
927 return 0;
928 }
929 if (ssl->cert->key->x509 == NULL)
930 {
931 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
932 return(0);
933 }
934 if (ssl->cert->key->privatekey == NULL)
935 {
936 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
937 return(0);
938 }
939 return(X509_check_private_key(ssl->cert->key->x509,
940 ssl->cert->key->privatekey));
941 }
942
943int SSL_accept(SSL *s)
944 {
945 if (s->handshake_func == 0)
946 /* Not properly initialized yet */
947 SSL_set_accept_state(s);
948
David Benjamin0b145c22014-11-26 20:10:09 -0500949 if (s->handshake_func != s->method->ssl_accept)
950 {
951 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
952 return -1;
953 }
954
955 return s->handshake_func(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700956 }
957
958int SSL_connect(SSL *s)
959 {
960 if (s->handshake_func == 0)
961 /* Not properly initialized yet */
962 SSL_set_connect_state(s);
963
David Benjamin0b145c22014-11-26 20:10:09 -0500964 if (s->handshake_func != s->method->ssl_connect)
965 {
966 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
967 return -1;
968 }
969
970 return s->handshake_func(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700971 }
972
973long SSL_get_default_timeout(const SSL *s)
974 {
David Benjaminf4501342014-08-14 17:24:37 -0400975 return SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -0700976 }
977
978int SSL_read(SSL *s,void *buf,int num)
979 {
980 if (s->handshake_func == 0)
981 {
982 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
983 return -1;
984 }
985
986 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
987 {
988 s->rwstate=SSL_NOTHING;
989 return(0);
990 }
991 return(s->method->ssl_read(s,buf,num));
992 }
993
994int SSL_peek(SSL *s,void *buf,int num)
995 {
996 if (s->handshake_func == 0)
997 {
998 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
999 return -1;
1000 }
1001
1002 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1003 {
1004 return(0);
1005 }
1006 return(s->method->ssl_peek(s,buf,num));
1007 }
1008
1009int SSL_write(SSL *s,const void *buf,int num)
1010 {
1011 if (s->handshake_func == 0)
1012 {
1013 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
1014 return -1;
1015 }
1016
1017 if (s->shutdown & SSL_SENT_SHUTDOWN)
1018 {
1019 s->rwstate=SSL_NOTHING;
1020 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
1021 return(-1);
1022 }
1023 return(s->method->ssl_write(s,buf,num));
1024 }
1025
1026int SSL_shutdown(SSL *s)
1027 {
1028 /* Note that this function behaves differently from what one might
1029 * expect. Return values are 0 for no success (yet),
1030 * 1 for success; but calling it once is usually not enough,
1031 * even if blocking I/O is used (see ssl3_shutdown).
1032 */
1033
1034 if (s->handshake_func == 0)
1035 {
1036 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1037 return -1;
1038 }
1039
Adam Langleyb2cb0ec2014-09-02 14:28:49 -07001040 if (!SSL_in_init(s))
Adam Langley95c29f32014-06-20 12:00:00 -07001041 return(s->method->ssl_shutdown(s));
1042 else
1043 return(1);
1044 }
1045
1046int SSL_renegotiate(SSL *s)
1047 {
1048 if (s->renegotiate == 0)
1049 s->renegotiate=1;
1050
1051 s->new_session=1;
1052
1053 return(s->method->ssl_renegotiate(s));
1054 }
1055
1056int SSL_renegotiate_abbreviated(SSL *s)
1057 {
1058 if (s->renegotiate == 0)
1059 s->renegotiate=1;
1060
1061 s->new_session=0;
1062
1063 return(s->method->ssl_renegotiate(s));
1064 }
1065
1066int SSL_renegotiate_pending(SSL *s)
1067 {
1068 /* becomes true when negotiation is requested;
1069 * false again once a handshake has finished */
1070 return (s->renegotiate != 0);
1071 }
1072
1073long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1074 {
1075 long l;
1076
1077 switch (cmd)
1078 {
1079 case SSL_CTRL_GET_READ_AHEAD:
1080 return(s->read_ahead);
1081 case SSL_CTRL_SET_READ_AHEAD:
1082 l=s->read_ahead;
1083 s->read_ahead=larg;
1084 return(l);
1085
1086 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1087 s->msg_callback_arg = parg;
1088 return 1;
1089
1090 case SSL_CTRL_OPTIONS:
1091 return(s->options|=larg);
1092 case SSL_CTRL_CLEAR_OPTIONS:
1093 return(s->options&=~larg);
1094 case SSL_CTRL_MODE:
1095 return(s->mode|=larg);
1096 case SSL_CTRL_CLEAR_MODE:
1097 return(s->mode &=~larg);
1098 case SSL_CTRL_GET_MAX_CERT_LIST:
1099 return(s->max_cert_list);
1100 case SSL_CTRL_SET_MAX_CERT_LIST:
1101 l=s->max_cert_list;
1102 s->max_cert_list=larg;
1103 return(l);
1104 case SSL_CTRL_SET_MTU:
Adam Langley95c29f32014-06-20 12:00:00 -07001105 if (larg < (long)dtls1_min_mtu())
1106 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001107 if (SSL_IS_DTLS(s))
1108 {
1109 s->d1->mtu = larg;
1110 return larg;
1111 }
1112 return 0;
1113 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1114 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1115 return 0;
1116 s->max_send_fragment = larg;
1117 return 1;
1118 case SSL_CTRL_GET_RI_SUPPORT:
1119 if (s->s3)
1120 return s->s3->send_connection_binding;
1121 else return 0;
1122 case SSL_CTRL_CERT_FLAGS:
1123 return(s->cert->cert_flags|=larg);
1124 case SSL_CTRL_CLEAR_CERT_FLAGS:
1125 return(s->cert->cert_flags &=~larg);
1126
1127 case SSL_CTRL_GET_RAW_CIPHERLIST:
1128 if (parg)
1129 {
1130 if (s->cert->ciphers_raw == NULL)
1131 return 0;
1132 *(unsigned char **)parg = s->cert->ciphers_raw;
1133 return (int)s->cert->ciphers_rawlen;
1134 }
1135 else
David Benjamin39482a12014-07-20 13:30:15 -04001136 {
1137 /* Passing a NULL |parg| returns the size of a single
1138 * cipher suite value. */
1139 return 2;
1140 }
Adam Langley95c29f32014-06-20 12:00:00 -07001141 default:
1142 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1143 }
1144 }
1145
1146long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1147 {
1148 switch(cmd)
1149 {
1150 case SSL_CTRL_SET_MSG_CALLBACK:
1151 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1152 return 1;
1153
1154 default:
1155 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1156 }
1157 }
1158
1159LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1160 {
1161 return ctx->sessions;
1162 }
1163
1164long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1165 {
1166 long l;
1167
1168 switch (cmd)
1169 {
1170 case SSL_CTRL_GET_READ_AHEAD:
1171 return(ctx->read_ahead);
1172 case SSL_CTRL_SET_READ_AHEAD:
1173 l=ctx->read_ahead;
1174 ctx->read_ahead=larg;
1175 return(l);
1176
1177 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1178 ctx->msg_callback_arg = parg;
1179 return 1;
1180
1181 case SSL_CTRL_GET_MAX_CERT_LIST:
1182 return(ctx->max_cert_list);
1183 case SSL_CTRL_SET_MAX_CERT_LIST:
1184 l=ctx->max_cert_list;
1185 ctx->max_cert_list=larg;
1186 return(l);
1187
1188 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1189 l=ctx->session_cache_size;
1190 ctx->session_cache_size=larg;
1191 return(l);
1192 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1193 return(ctx->session_cache_size);
1194 case SSL_CTRL_SET_SESS_CACHE_MODE:
1195 l=ctx->session_cache_mode;
1196 ctx->session_cache_mode=larg;
1197 return(l);
1198 case SSL_CTRL_GET_SESS_CACHE_MODE:
1199 return(ctx->session_cache_mode);
1200
1201 case SSL_CTRL_SESS_NUMBER:
1202 return(lh_SSL_SESSION_num_items(ctx->sessions));
1203 case SSL_CTRL_SESS_CONNECT:
1204 return(ctx->stats.sess_connect);
1205 case SSL_CTRL_SESS_CONNECT_GOOD:
1206 return(ctx->stats.sess_connect_good);
1207 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1208 return(ctx->stats.sess_connect_renegotiate);
1209 case SSL_CTRL_SESS_ACCEPT:
1210 return(ctx->stats.sess_accept);
1211 case SSL_CTRL_SESS_ACCEPT_GOOD:
1212 return(ctx->stats.sess_accept_good);
1213 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1214 return(ctx->stats.sess_accept_renegotiate);
1215 case SSL_CTRL_SESS_HIT:
1216 return(ctx->stats.sess_hit);
1217 case SSL_CTRL_SESS_CB_HIT:
1218 return(ctx->stats.sess_cb_hit);
1219 case SSL_CTRL_SESS_MISSES:
1220 return(ctx->stats.sess_miss);
1221 case SSL_CTRL_SESS_TIMEOUTS:
1222 return(ctx->stats.sess_timeout);
1223 case SSL_CTRL_SESS_CACHE_FULL:
1224 return(ctx->stats.sess_cache_full);
1225 case SSL_CTRL_OPTIONS:
1226 return(ctx->options|=larg);
1227 case SSL_CTRL_CLEAR_OPTIONS:
1228 return(ctx->options&=~larg);
1229 case SSL_CTRL_MODE:
1230 return(ctx->mode|=larg);
1231 case SSL_CTRL_CLEAR_MODE:
1232 return(ctx->mode&=~larg);
1233 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1234 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1235 return 0;
1236 ctx->max_send_fragment = larg;
1237 return 1;
1238 case SSL_CTRL_CERT_FLAGS:
1239 return(ctx->cert->cert_flags|=larg);
1240 case SSL_CTRL_CLEAR_CERT_FLAGS:
1241 return(ctx->cert->cert_flags &=~larg);
1242 default:
1243 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1244 }
1245 }
1246
1247long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1248 {
1249 switch(cmd)
1250 {
1251 case SSL_CTRL_SET_MSG_CALLBACK:
1252 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1253 return 1;
1254
1255 default:
1256 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1257 }
1258 }
1259
1260int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1261 {
1262 long l;
1263 const SSL_CIPHER *a = in_a;
1264 const SSL_CIPHER *b = in_b;
1265 const long a_id = a->id;
1266 const long b_id = b->id;
1267
1268 l = a_id - b_id;
1269 if (l == 0L)
1270 return(0);
1271 else
1272 return((l > 0)?1:-1);
1273 }
1274
1275int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1276 {
1277 long l;
1278 const long a_id = (*ap)->id;
1279 const long b_id = (*bp)->id;
1280
1281 l = a_id - b_id;
1282 if (l == 0)
1283 return(0);
1284 else
1285 return((l > 0)?1:-1);
1286 }
1287
1288/** return a STACK of the ciphers available for the SSL and in order of
1289 * preference */
1290STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1291 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001292 if (s == NULL)
1293 return NULL;
1294
1295 if (s->cipher_list != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001296 {
Adam Langley858a88d2014-06-20 12:00:00 -07001297 return(s->cipher_list->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -07001298 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001299
1300 if (s->version >= TLS1_1_VERSION)
1301 {
1302 if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001303 return s->ctx->cipher_list_tls11->ciphers;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001304 }
1305
1306 if ((s->ctx != NULL) &&
1307 (s->ctx->cipher_list != NULL))
1308 {
Adam Langley858a88d2014-06-20 12:00:00 -07001309 return(s->ctx->cipher_list->ciphers);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001310 }
1311
Adam Langley95c29f32014-06-20 12:00:00 -07001312 return(NULL);
1313 }
1314
1315/** return a STACK of the ciphers available for the SSL and in order of
1316 * algorithm id */
1317STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1318 {
1319 if (s != NULL)
1320 {
1321 if (s->cipher_list_by_id != NULL)
1322 {
1323 return(s->cipher_list_by_id);
1324 }
1325 else if ((s->ctx != NULL) &&
1326 (s->ctx->cipher_list_by_id != NULL))
1327 {
1328 return(s->ctx->cipher_list_by_id);
1329 }
1330 }
1331 return(NULL);
1332 }
1333
1334/** The old interface to get the same thing as SSL_get_ciphers() */
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001335const char *SSL_get_cipher_list(const SSL *s, int n)
Adam Langley95c29f32014-06-20 12:00:00 -07001336 {
David Benjamin6f260012014-08-15 13:49:12 -04001337 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001338 STACK_OF(SSL_CIPHER) *sk;
1339
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001340 if (s == NULL)
1341 return NULL;
1342 sk = SSL_get_ciphers(s);
1343 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk))
1344 return NULL;
1345 c = sk_SSL_CIPHER_value(sk, n);
1346 if (c == NULL)
1347 return NULL;
1348 return c->name;
Adam Langley95c29f32014-06-20 12:00:00 -07001349 }
1350
1351/** specify the ciphers to be used by default by the SSL_CTX */
1352int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1353 {
1354 STACK_OF(SSL_CIPHER) *sk;
1355
1356 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1357 &ctx->cipher_list_by_id,str, ctx->cert);
1358 /* ssl_create_cipher_list may return an empty stack if it
1359 * was unable to find a cipher matching the given rule string
1360 * (for example if the rule string specifies a cipher which
1361 * has been disabled). This is not an error as far as
1362 * ssl_create_cipher_list is concerned, and hence
1363 * ctx->cipher_list and ctx->cipher_list_by_id has been
1364 * updated. */
1365 if (sk == NULL)
1366 return 0;
1367 else if (sk_SSL_CIPHER_num(sk) == 0)
1368 {
1369 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1370 return 0;
1371 }
1372 return 1;
1373 }
1374
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001375int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str)
1376 {
1377 STACK_OF(SSL_CIPHER) *sk;
1378
1379 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert);
1380 if (sk == NULL)
1381 return 0;
1382 else if (sk_SSL_CIPHER_num(sk) == 0)
1383 {
1384 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH);
1385 return 0;
1386 }
1387 return 1;
1388 }
1389
Adam Langley95c29f32014-06-20 12:00:00 -07001390/** specify the ciphers to be used by the SSL */
1391int SSL_set_cipher_list(SSL *s,const char *str)
1392 {
1393 STACK_OF(SSL_CIPHER) *sk;
1394
1395 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1396 &s->cipher_list_by_id,str, s->cert);
1397 /* see comment in SSL_CTX_set_cipher_list */
1398 if (sk == NULL)
1399 return 0;
1400 else if (sk_SSL_CIPHER_num(sk) == 0)
1401 {
1402 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1403 return 0;
1404 }
1405 return 1;
1406 }
1407
David Benjamin5491e3f2014-09-29 19:33:09 -04001408int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p)
Adam Langley95c29f32014-06-20 12:00:00 -07001409 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001410 size_t i;
David Benjamin6f260012014-08-15 13:49:12 -04001411 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001412 CERT *ct = s->cert;
David Benjamin5491e3f2014-09-29 19:33:09 -04001413 uint8_t *q;
Adam Langley95c29f32014-06-20 12:00:00 -07001414 /* Set disabled masks for this session */
1415 ssl_set_client_disabled(s);
1416
1417 if (sk == NULL) return(0);
1418 q=p;
1419
1420 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1421 {
1422 c=sk_SSL_CIPHER_value(sk,i);
1423 /* Skip disabled ciphers */
1424 if (c->algorithm_ssl & ct->mask_ssl ||
1425 c->algorithm_mkey & ct->mask_k ||
1426 c->algorithm_auth & ct->mask_a)
1427 continue;
David Benjamin39482a12014-07-20 13:30:15 -04001428 s2n(ssl3_get_cipher_value(c), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001429 }
David Benjamin5491e3f2014-09-29 19:33:09 -04001430 /* If all ciphers were disabled, return the error to the caller. */
1431 if (p == q)
Adam Langley95c29f32014-06-20 12:00:00 -07001432 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001433 return 0;
1434 }
1435
1436 /* Add SCSVs. */
1437 if (!s->renegotiate)
1438 {
1439 s2n(SSL3_CK_SCSV & 0xffff, p);
1440 }
1441 if (s->fallback_scsv)
1442 {
1443 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001444 }
1445
1446 return(p-q);
1447 }
1448
David Benjamin9f2c0d72014-10-21 22:00:19 -04001449STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001450 {
David Benjamin39482a12014-07-20 13:30:15 -04001451 CBS cipher_suites = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001452 const SSL_CIPHER *c;
1453 STACK_OF(SSL_CIPHER) *sk;
David Benjamin39482a12014-07-20 13:30:15 -04001454
Adam Langley95c29f32014-06-20 12:00:00 -07001455 if (s->s3)
1456 s->s3->send_connection_binding = 0;
1457
David Benjamin39482a12014-07-20 13:30:15 -04001458 if (CBS_len(&cipher_suites) % 2 != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001459 {
1460 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001461 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001462 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001463
1464 sk = sk_SSL_CIPHER_new_null();
1465 if (sk == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001466 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001467 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1468 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001469 }
1470
David Benjamin39482a12014-07-20 13:30:15 -04001471 if (!CBS_stow(&cipher_suites,
1472 &s->cert->ciphers_raw, &s->cert->ciphers_rawlen))
Adam Langley95c29f32014-06-20 12:00:00 -07001473 {
1474 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1475 goto err;
1476 }
Adam Langley95c29f32014-06-20 12:00:00 -07001477
David Benjamin39482a12014-07-20 13:30:15 -04001478 while (CBS_len(&cipher_suites) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001479 {
David Benjamin39482a12014-07-20 13:30:15 -04001480 uint16_t cipher_suite;
1481
1482 if (!CBS_get_u16(&cipher_suites, &cipher_suite))
1483 {
1484 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1485 goto err;
1486 }
1487
David Benjamin9f2c0d72014-10-21 22:00:19 -04001488 /* Check for SCSV. */
David Benjamin39482a12014-07-20 13:30:15 -04001489 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff))
Adam Langley95c29f32014-06-20 12:00:00 -07001490 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001491 /* SCSV is fatal if renegotiating. */
Adam Langley95c29f32014-06-20 12:00:00 -07001492 if (s->renegotiate)
1493 {
1494 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1495 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1496 goto err;
1497 }
1498 s->s3->send_connection_binding = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001499 continue;
1500 }
1501
David Benjamin9f2c0d72014-10-21 22:00:19 -04001502 /* Check for FALLBACK_SCSV. */
1503 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff))
Adam Langley29707792014-06-20 12:00:00 -07001504 {
David Benjaminceb6f282014-12-07 23:56:19 -05001505 uint16_t max_version = ssl3_get_max_server_version(s);
1506 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1507 : (uint16_t)s->version < max_version)
David Benjamin9f2c0d72014-10-21 22:00:19 -04001508 {
1509 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1510 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1511 goto err;
1512 }
1513 continue;
Adam Langley29707792014-06-20 12:00:00 -07001514 }
1515
David Benjamin39482a12014-07-20 13:30:15 -04001516 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -07001517 if (c != NULL)
1518 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001519 if (!sk_SSL_CIPHER_push(sk, c))
Adam Langley95c29f32014-06-20 12:00:00 -07001520 {
1521 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1522 goto err;
1523 }
1524 }
1525 }
1526
David Benjamin9f2c0d72014-10-21 22:00:19 -04001527 return sk;
1528
Adam Langley95c29f32014-06-20 12:00:00 -07001529err:
David Benjamin9f2c0d72014-10-21 22:00:19 -04001530 if (sk != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001531 sk_SSL_CIPHER_free(sk);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001532 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001533 }
1534
1535
Adam Langley95c29f32014-06-20 12:00:00 -07001536/** return a servername extension value if provided in Client Hello, or NULL.
1537 * So far, only host_name types are defined (RFC 3546).
1538 */
1539
1540const char *SSL_get_servername(const SSL *s, const int type)
1541 {
1542 if (type != TLSEXT_NAMETYPE_host_name)
1543 return NULL;
1544
1545 return s->session && !s->tlsext_hostname ?
1546 s->session->tlsext_hostname :
1547 s->tlsext_hostname;
1548 }
1549
1550int SSL_get_servername_type(const SSL *s)
1551 {
1552 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1553 return TLSEXT_NAMETYPE_host_name;
1554 return -1;
1555 }
1556
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001557void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx)
1558 {
1559 ctx->signed_cert_timestamps_enabled = 1;
1560 }
1561
1562int SSL_enable_signed_cert_timestamps(SSL *ssl)
1563 {
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001564 ssl->signed_cert_timestamps_enabled = 1;
1565 return 1;
1566 }
1567
David Benjamin6c7aed02014-08-27 16:42:38 -04001568void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx)
1569 {
1570 ctx->ocsp_stapling_enabled = 1;
1571 }
1572
1573int SSL_enable_ocsp_stapling(SSL *ssl)
1574 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001575 ssl->ocsp_stapling_enabled = 1;
1576 return 1;
1577 }
1578
David Benjamin52d699f2014-11-25 01:35:18 -05001579void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out, size_t *out_len)
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001580 {
Adam Langley3cb50e02014-08-26 14:00:31 -07001581 SSL_SESSION *session = ssl->session;
1582
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001583 *out_len = 0;
1584 *out = NULL;
1585 if (ssl->server)
1586 return;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001587 if (!session || !session->tlsext_signed_cert_timestamp_list)
1588 return;
1589 *out = session->tlsext_signed_cert_timestamp_list;
1590 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1591 }
1592
David Benjamin52d699f2014-11-25 01:35:18 -05001593void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out, size_t *out_len)
David Benjamin6c7aed02014-08-27 16:42:38 -04001594 {
1595 SSL_SESSION *session = ssl->session;
1596
1597 *out_len = 0;
1598 *out = NULL;
1599 if (ssl->server)
1600 return;
1601 if (!session || !session->ocsp_response)
1602 return;
1603 *out = session->ocsp_response;
1604 *out_len = session->ocsp_response_length;
1605 }
1606
Adam Langley95c29f32014-06-20 12:00:00 -07001607/* SSL_select_next_proto implements the standard protocol selection. It is
1608 * expected that this function is called from the callback set by
1609 * SSL_CTX_set_next_proto_select_cb.
1610 *
1611 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1612 * strings. The length byte itself is not included in the length. A byte
1613 * string of length 0 is invalid. No byte string may be truncated.
1614 *
1615 * The current, but experimental algorithm for selecting the protocol is:
1616 *
1617 * 1) If the server doesn't support NPN then this is indicated to the
1618 * callback. In this case, the client application has to abort the connection
1619 * or have a default application level protocol.
1620 *
1621 * 2) If the server supports NPN, but advertises an empty list then the
1622 * client selects the first protcol in its list, but indicates via the
1623 * API that this fallback case was enacted.
1624 *
1625 * 3) Otherwise, the client finds the first protocol in the server's list
1626 * that it supports and selects this protocol. This is because it's
1627 * assumed that the server has better information about which protocol
1628 * a client should use.
1629 *
1630 * 4) If the client doesn't support any of the server's advertised
1631 * protocols, then this is treated the same as case 2.
1632 *
1633 * It returns either
1634 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1635 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1636 */
1637int 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)
1638 {
1639 unsigned int i, j;
1640 const unsigned char *result;
1641 int status = OPENSSL_NPN_UNSUPPORTED;
1642
1643 /* For each protocol in server preference order, see if we support it. */
1644 for (i = 0; i < server_len; )
1645 {
1646 for (j = 0; j < client_len; )
1647 {
1648 if (server[i] == client[j] &&
1649 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1650 {
1651 /* We found a match */
1652 result = &server[i];
1653 status = OPENSSL_NPN_NEGOTIATED;
1654 goto found;
1655 }
1656 j += client[j];
1657 j++;
1658 }
1659 i += server[i];
1660 i++;
1661 }
1662
1663 /* There's no overlap between our protocols and the server's list. */
1664 result = client;
1665 status = OPENSSL_NPN_NO_OVERLAP;
1666
1667 found:
1668 *out = (unsigned char *) result + 1;
1669 *outlen = result[0];
1670 return status;
1671 }
1672
Adam Langley95c29f32014-06-20 12:00:00 -07001673/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1674 * requested protocol for this connection and returns 0. If the client didn't
1675 * request any protocol, then *data is set to NULL.
1676 *
1677 * Note that the client can request any protocol it chooses. The value returned
1678 * from this function need not be a member of the list of supported protocols
1679 * provided by the callback.
1680 */
David Benjaminced551f2014-07-11 23:16:16 -04001681void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, unsigned *len)
Adam Langley95c29f32014-06-20 12:00:00 -07001682 {
1683 *data = s->next_proto_negotiated;
1684 if (!*data) {
1685 *len = 0;
1686 } else {
1687 *len = s->next_proto_negotiated_len;
1688 }
1689}
1690
1691/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1692 * TLS server needs a list of supported protocols for Next Protocol
1693 * Negotiation. The returned list must be in wire format. The list is returned
1694 * by setting |out| to point to it and |outlen| to its length. This memory will
1695 * not be modified, but one should assume that the SSL* keeps a reference to
1696 * it.
1697 *
1698 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1699 * such extension will be included in the ServerHello. */
1700void 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)
1701 {
1702 ctx->next_protos_advertised_cb = cb;
1703 ctx->next_protos_advertised_cb_arg = arg;
1704 }
1705
1706/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1707 * client needs to select a protocol from the server's provided list. |out|
1708 * must be set to point to the selected protocol (which may be within |in|).
1709 * The length of the protocol name must be written into |outlen|. The server's
1710 * advertised protocols are provided in |in| and |inlen|. The callback can
1711 * assume that |in| is syntactically valid.
1712 *
1713 * The client must select a protocol. It is fatal to the connection if this
1714 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1715 */
1716void 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)
1717 {
1718 ctx->next_proto_select_cb = cb;
1719 ctx->next_proto_select_cb_arg = arg;
1720 }
Adam Langley95c29f32014-06-20 12:00:00 -07001721
Adam Langley95c29f32014-06-20 12:00:00 -07001722/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1723 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1724 * length-prefixed strings).
1725 *
1726 * Returns 0 on success. */
1727int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1728 unsigned protos_len)
1729 {
1730 if (ctx->alpn_client_proto_list)
1731 OPENSSL_free(ctx->alpn_client_proto_list);
1732
David Benjamin072c9532014-07-26 11:44:25 -04001733 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001734 if (!ctx->alpn_client_proto_list)
1735 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001736 ctx->alpn_client_proto_list_len = protos_len;
1737
1738 return 0;
1739 }
1740
1741/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1742 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1743 * length-prefixed strings).
1744 *
1745 * Returns 0 on success. */
1746int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1747 unsigned protos_len)
1748 {
1749 if (ssl->alpn_client_proto_list)
1750 OPENSSL_free(ssl->alpn_client_proto_list);
1751
David Benjamin072c9532014-07-26 11:44:25 -04001752 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001753 if (!ssl->alpn_client_proto_list)
1754 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001755 ssl->alpn_client_proto_list_len = protos_len;
1756
1757 return 0;
1758 }
1759
1760/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1761 * during ClientHello processing in order to select an ALPN protocol from the
1762 * client's list of offered protocols. */
1763void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1764 int (*cb) (SSL *ssl,
1765 const unsigned char **out,
1766 unsigned char *outlen,
1767 const unsigned char *in,
1768 unsigned int inlen,
1769 void *arg),
1770 void *arg)
1771 {
1772 ctx->alpn_select_cb = cb;
1773 ctx->alpn_select_cb_arg = arg;
1774 }
1775
1776/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1777 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1778 * including the leading length-prefix byte). If the server didn't respond with
1779 * a negotiated protocol then |*len| will be zero. */
1780void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1781 unsigned *len)
1782 {
1783 *data = NULL;
1784 if (ssl->s3)
1785 *data = ssl->s3->alpn_selected;
1786 if (*data == NULL)
1787 *len = 0;
1788 else
1789 *len = ssl->s3->alpn_selected_len;
1790 }
Adam Langley95c29f32014-06-20 12:00:00 -07001791
1792int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1793 const char *label, size_t llen, const unsigned char *p, size_t plen,
1794 int use_context)
1795 {
1796 if (s->version < TLS1_VERSION)
1797 return -1;
1798
David Benjamine99e9122014-12-11 01:46:01 -05001799 return s->enc_method->export_keying_material(s, out, olen, label,
Adam Langley95c29f32014-06-20 12:00:00 -07001800 llen, p, plen,
1801 use_context);
1802 }
1803
1804static uint32_t ssl_session_hash(const SSL_SESSION *a)
1805 {
1806 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1807 ((uint32_t) a->session_id[1] << 8) ||
1808 ((uint32_t) a->session_id[2] << 16) ||
1809 ((uint32_t) a->session_id[3] << 24);
1810
1811 return hash;
1812 }
1813
1814/* NB: If this function (or indeed the hash function which uses a sort of
1815 * coarser function than this one) is changed, ensure
1816 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1817 * able to construct an SSL_SESSION that will collide with any existing session
1818 * with a matching session ID. */
1819static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1820 {
1821 if (a->ssl_version != b->ssl_version)
1822 return(1);
1823 if (a->session_id_length != b->session_id_length)
1824 return(1);
1825 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1826 }
1827
1828SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1829 {
1830 SSL_CTX *ret=NULL;
1831
1832 if (meth == NULL)
1833 {
1834 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1835 return(NULL);
1836 }
1837
Adam Langley95c29f32014-06-20 12:00:00 -07001838 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1839 {
1840 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1841 goto err;
1842 }
1843 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1844 if (ret == NULL)
1845 goto err;
1846
1847 memset(ret,0,sizeof(SSL_CTX));
1848
David Benjamin82c9e902014-12-12 15:55:27 -05001849 ret->method = meth->method;
Adam Langley95c29f32014-06-20 12:00:00 -07001850
1851 ret->cert_store=NULL;
1852 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1853 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1854 ret->session_cache_head=NULL;
1855 ret->session_cache_tail=NULL;
1856
1857 /* We take the system default */
David Benjaminf4501342014-08-14 17:24:37 -04001858 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001859
1860 ret->new_session_cb=0;
1861 ret->remove_session_cb=0;
1862 ret->get_session_cb=0;
1863 ret->generate_session_id=0;
1864
1865 memset((char *)&ret->stats,0,sizeof(ret->stats));
1866
1867 ret->references=1;
1868 ret->quiet_shutdown=0;
1869
Adam Langley95c29f32014-06-20 12:00:00 -07001870 ret->info_callback=NULL;
1871
1872 ret->app_verify_callback=0;
1873 ret->app_verify_arg=NULL;
1874
1875 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1876 ret->read_ahead=0;
1877 ret->msg_callback=0;
1878 ret->msg_callback_arg=NULL;
1879 ret->verify_mode=SSL_VERIFY_NONE;
1880#if 0
1881 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1882#endif
1883 ret->sid_ctx_length=0;
1884 ret->default_verify_callback=NULL;
1885 if ((ret->cert=ssl_cert_new()) == NULL)
1886 goto err;
1887
1888 ret->default_passwd_callback=0;
1889 ret->default_passwd_callback_userdata=NULL;
1890 ret->client_cert_cb=0;
1891 ret->app_gen_cookie_cb=0;
1892 ret->app_verify_cookie_cb=0;
1893
1894 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1895 if (ret->sessions == NULL) goto err;
1896 ret->cert_store=X509_STORE_new();
1897 if (ret->cert_store == NULL) goto err;
1898
1899 ssl_create_cipher_list(ret->method,
1900 &ret->cipher_list,&ret->cipher_list_by_id,
David Benjamin5e4f6e92014-11-17 03:23:24 -05001901 SSL_DEFAULT_CIPHER_LIST, ret->cert);
Adam Langley95c29f32014-06-20 12:00:00 -07001902 if (ret->cipher_list == NULL
Adam Langley858a88d2014-06-20 12:00:00 -07001903 || sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001904 {
1905 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1906 goto err2;
1907 }
1908
1909 ret->param = X509_VERIFY_PARAM_new();
1910 if (!ret->param)
1911 goto err;
1912
Adam Langley95c29f32014-06-20 12:00:00 -07001913 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1914 goto err;
1915
1916 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1917
1918 ret->extra_certs=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001919
1920 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1921
Adam Langley95c29f32014-06-20 12:00:00 -07001922 ret->tlsext_servername_callback = 0;
1923 ret->tlsext_servername_arg = NULL;
1924 /* Setup RFC4507 ticket keys */
1925 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
1926 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
1927 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
1928 ret->options |= SSL_OP_NO_TICKET;
1929
1930 ret->tlsext_status_cb = 0;
1931 ret->tlsext_status_arg = NULL;
1932
Adam Langley95c29f32014-06-20 12:00:00 -07001933 ret->next_protos_advertised_cb = 0;
1934 ret->next_proto_select_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001935 ret->psk_identity_hint=NULL;
1936 ret->psk_client_callback=NULL;
1937 ret->psk_server_callback=NULL;
David Benjamin422d3a42014-08-20 11:09:03 -04001938
Adam Langley95c29f32014-06-20 12:00:00 -07001939 /* Default is to connect to non-RI servers. When RI is more widely
1940 * deployed might change this.
1941 */
1942 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1943
David Benjamin82c9e902014-12-12 15:55:27 -05001944 /* Lock the SSL_CTX to the specified version, for compatibility with
1945 * legacy uses of SSL_METHOD. */
1946 if (meth->version != 0)
1947 {
1948 SSL_CTX_set_max_version(ret, meth->version);
1949 SSL_CTX_set_min_version(ret, meth->version);
1950 }
1951
Adam Langley95c29f32014-06-20 12:00:00 -07001952 return(ret);
1953err:
1954 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
1955err2:
1956 if (ret != NULL) SSL_CTX_free(ret);
1957 return(NULL);
1958 }
1959
Adam Langley95c29f32014-06-20 12:00:00 -07001960
1961void SSL_CTX_free(SSL_CTX *a)
1962 {
1963 int i;
1964
1965 if (a == NULL) return;
1966
1967 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
1968#ifdef REF_PRINT
1969 REF_PRINT("SSL_CTX",a);
1970#endif
1971 if (i > 0) return;
1972#ifdef REF_CHECK
1973 if (i < 0)
1974 {
1975 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
1976 abort(); /* ok */
1977 }
1978#endif
1979
1980 if (a->param)
1981 X509_VERIFY_PARAM_free(a->param);
1982
1983 /*
1984 * Free internal session cache. However: the remove_cb() may reference
1985 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
1986 * after the sessions were flushed.
1987 * As the ex_data handling routines might also touch the session cache,
1988 * the most secure solution seems to be: empty (flush) the cache, then
1989 * free ex_data, then finally free the cache.
1990 * (See ticket [openssl.org #212].)
1991 */
1992 if (a->sessions != NULL)
1993 SSL_CTX_flush_sessions(a,0);
1994
1995 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
1996
1997 if (a->sessions != NULL)
1998 lh_SSL_SESSION_free(a->sessions);
1999
2000 if (a->cert_store != NULL)
2001 X509_STORE_free(a->cert_store);
2002 if (a->cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002003 ssl_cipher_preference_list_free(a->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002004 if (a->cipher_list_by_id != NULL)
2005 sk_SSL_CIPHER_free(a->cipher_list_by_id);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07002006 if (a->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002007 ssl_cipher_preference_list_free(a->cipher_list_tls11);
Adam Langley95c29f32014-06-20 12:00:00 -07002008 if (a->cert != NULL)
2009 ssl_cert_free(a->cert);
2010 if (a->client_CA != NULL)
2011 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2012 if (a->extra_certs != NULL)
2013 sk_X509_pop_free(a->extra_certs,X509_free);
Adam Langley95c29f32014-06-20 12:00:00 -07002014
2015 if (a->srtp_profiles)
2016 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2017
Adam Langley95c29f32014-06-20 12:00:00 -07002018 if (a->psk_identity_hint)
2019 OPENSSL_free(a->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07002020
Adam Langley95c29f32014-06-20 12:00:00 -07002021 if (a->tlsext_ecpointformatlist)
2022 OPENSSL_free(a->tlsext_ecpointformatlist);
2023 if (a->tlsext_ellipticcurvelist)
2024 OPENSSL_free(a->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -07002025 if (a->alpn_client_proto_list != NULL)
2026 OPENSSL_free(a->alpn_client_proto_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002027
Adam Langley1258b6a2014-06-20 12:00:00 -07002028 if (a->tlsext_channel_id_private)
2029 EVP_PKEY_free(a->tlsext_channel_id_private);
Adam Langley1258b6a2014-06-20 12:00:00 -07002030
David Benjamin859ec3c2014-09-02 16:29:36 -04002031 if (a->keylog_bio)
2032 BIO_free(a->keylog_bio);
2033
Adam Langley95c29f32014-06-20 12:00:00 -07002034 OPENSSL_free(a);
2035 }
2036
2037void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2038 {
2039 ctx->default_passwd_callback=cb;
2040 }
2041
2042void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2043 {
2044 ctx->default_passwd_callback_userdata=u;
2045 }
2046
2047void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2048 {
2049 ctx->app_verify_callback=cb;
2050 ctx->app_verify_arg=arg;
2051 }
2052
2053void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2054 {
2055 ctx->verify_mode=mode;
2056 ctx->default_verify_callback=cb;
2057 }
2058
2059void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2060 {
2061 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2062 }
2063
2064void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2065 {
2066 ssl_cert_set_cert_cb(c->cert, cb, arg);
2067 }
2068
2069void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2070 {
2071 ssl_cert_set_cert_cb(s->cert, cb, arg);
2072 }
2073
David Benjamin033e5f42014-11-13 18:47:41 -05002074static int ssl_has_key(SSL *s, size_t idx)
2075 {
2076 CERT_PKEY *cpk = &s->cert->pkeys[idx];
2077 return cpk->x509 && cpk->privatekey;
2078 }
2079
David Benjaminf31e6812014-11-13 18:05:55 -05002080void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
2081 unsigned long *out_mask_a)
Adam Langley95c29f32014-06-20 12:00:00 -07002082 {
David Benjaminf31e6812014-11-13 18:05:55 -05002083 CERT *c = s->cert;
David Benjaminf31e6812014-11-13 18:05:55 -05002084 int rsa_enc, rsa_sign, dh_tmp;
2085 unsigned long mask_k, mask_a;
David Benjamin060d9d22014-07-15 00:54:26 -04002086 int have_ecc_cert, ecdsa_ok;
David Benjamin0da0e182014-08-19 16:20:28 -04002087 int have_ecdh_tmp;
David Benjaminf31e6812014-11-13 18:05:55 -05002088 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002089
David Benjaminf31e6812014-11-13 18:05:55 -05002090 if (c == NULL)
2091 {
2092 /* TODO(davidben): Is this codepath possible? */
2093 *out_mask_k = 0;
2094 *out_mask_a = 0;
2095 return;
2096 }
Adam Langley95c29f32014-06-20 12:00:00 -07002097
David Benjaminf31e6812014-11-13 18:05:55 -05002098 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002099
David Benjaminf31e6812014-11-13 18:05:55 -05002100 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
David Benjamin033e5f42014-11-13 18:47:41 -05002101 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2102 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2103 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
David Benjaminf31e6812014-11-13 18:05:55 -05002104 mask_k = 0;
2105 mask_a = 0;
2106
David Benjamin77a942b2014-07-15 01:22:50 -04002107 if (rsa_enc)
David Benjaminf31e6812014-11-13 18:05:55 -05002108 mask_k |= SSL_kRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002109
Adam Langley95c29f32014-06-20 12:00:00 -07002110 if (dh_tmp)
David Benjaminf31e6812014-11-13 18:05:55 -05002111 mask_k |= SSL_kEDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002112
Adam Langley95c29f32014-06-20 12:00:00 -07002113 if (rsa_enc || rsa_sign)
2114 {
David Benjaminf31e6812014-11-13 18:05:55 -05002115 mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002116 }
2117
David Benjaminf31e6812014-11-13 18:05:55 -05002118 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002119
David Benjamin0da0e182014-08-19 16:20:28 -04002120 /* An ECC certificate may be usable for ECDSA cipher suites depending on
David Benjamin033e5f42014-11-13 18:47:41 -05002121 * the key usage extension and on the client's curve preferences. */
Adam Langley95c29f32014-06-20 12:00:00 -07002122 if (have_ecc_cert)
2123 {
David Benjamin033e5f42014-11-13 18:47:41 -05002124 x = c->pkeys[SSL_PKEY_ECC].x509;
Adam Langley95c29f32014-06-20 12:00:00 -07002125 /* This call populates extension flags (ex_flags) */
2126 X509_check_purpose(x, -1, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07002127 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2128 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
David Benjamin033e5f42014-11-13 18:47:41 -05002129 if (!tls1_check_ec_cert(s, x))
Adam Langley95c29f32014-06-20 12:00:00 -07002130 ecdsa_ok = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002131 if (ecdsa_ok)
2132 {
David Benjaminf31e6812014-11-13 18:05:55 -05002133 mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002134 }
Adam Langley95c29f32014-06-20 12:00:00 -07002135 }
Adam Langley95c29f32014-06-20 12:00:00 -07002136
David Benjaminf31e6812014-11-13 18:05:55 -05002137 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2138 * key, check it. */
2139 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002140 {
David Benjaminf31e6812014-11-13 18:05:55 -05002141 mask_k |= SSL_kEECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002142 }
Adam Langley95c29f32014-06-20 12:00:00 -07002143
David Benjaminf31e6812014-11-13 18:05:55 -05002144 /* PSK requires a server callback. */
2145 if (s->psk_server_callback != NULL)
2146 {
2147 mask_k |= SSL_kPSK;
2148 mask_a |= SSL_aPSK;
2149 }
Adam Langley95c29f32014-06-20 12:00:00 -07002150
David Benjaminf31e6812014-11-13 18:05:55 -05002151 *out_mask_k = mask_k;
2152 *out_mask_a = mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002153 }
2154
2155/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2156#define ku_reject(x, usage) \
2157 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2158
Adam Langley95c29f32014-06-20 12:00:00 -07002159
2160int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2161 {
David Benjamin0da0e182014-08-19 16:20:28 -04002162 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002163 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2164 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2165
Adam Langley95c29f32014-06-20 12:00:00 -07002166 alg_a = cs->algorithm_auth;
2167
Adam Langley95c29f32014-06-20 12:00:00 -07002168 /* This call populates the ex_flags field correctly */
2169 X509_check_purpose(x, -1, 0);
2170 if ((x->sig_alg) && (x->sig_alg->algorithm))
2171 {
2172 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2173 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2174 }
Adam Langley95c29f32014-06-20 12:00:00 -07002175 if (alg_a & SSL_aECDSA)
2176 {
2177 /* key usage, if present, must allow signing */
2178 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2179 {
2180 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2181 return 0;
2182 }
2183 }
2184
2185 return 1; /* all checks are ok */
2186 }
2187
Adam Langley95c29f32014-06-20 12:00:00 -07002188
2189static int ssl_get_server_cert_index(const SSL *s)
2190 {
2191 int idx;
2192 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2193 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2194 idx = SSL_PKEY_RSA_SIGN;
2195 if (idx == -1)
2196 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2197 return idx;
2198 }
2199
2200CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2201 {
David Benjaminf31e6812014-11-13 18:05:55 -05002202 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002203
2204 /* This may or may not be an error. */
2205 if (i < 0)
2206 return NULL;
2207
2208 /* May be NULL. */
David Benjaminf31e6812014-11-13 18:05:55 -05002209 return &s->cert->pkeys[i];
Adam Langley95c29f32014-06-20 12:00:00 -07002210 }
2211
David Benjaminec2f27d2014-11-13 19:17:25 -05002212EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher)
Adam Langley95c29f32014-06-20 12:00:00 -07002213 {
2214 unsigned long alg_a;
2215 CERT *c;
2216 int idx = -1;
2217
2218 alg_a = cipher->algorithm_auth;
2219 c=s->cert;
2220
David Benjaminef2116d2014-08-19 20:21:56 -04002221 if (alg_a & SSL_aRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002222 {
2223 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2224 idx = SSL_PKEY_RSA_SIGN;
2225 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2226 idx = SSL_PKEY_RSA_ENC;
2227 }
2228 else if ((alg_a & SSL_aECDSA) &&
2229 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2230 idx = SSL_PKEY_ECC;
2231 if (idx == -1)
2232 {
2233 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2234 return(NULL);
2235 }
Adam Langley95c29f32014-06-20 12:00:00 -07002236 return c->pkeys[idx].privatekey;
2237 }
2238
Adam Langley95c29f32014-06-20 12:00:00 -07002239void ssl_update_cache(SSL *s,int mode)
2240 {
2241 int i;
2242
2243 /* If the session_id_length is 0, we are not supposed to cache it,
2244 * and it would be rather hard to do anyway :-) */
2245 if (s->session->session_id_length == 0) return;
2246
David Benjamin041b58a2014-08-27 15:25:21 -04002247 i=s->initial_ctx->session_cache_mode;
Adam Langley95c29f32014-06-20 12:00:00 -07002248 if ((i & mode) && (!s->hit)
2249 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
David Benjamin041b58a2014-08-27 15:25:21 -04002250 || SSL_CTX_add_session(s->initial_ctx,s->session))
2251 && (s->initial_ctx->new_session_cb != NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07002252 {
2253 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
David Benjamin041b58a2014-08-27 15:25:21 -04002254 if (!s->initial_ctx->new_session_cb(s,s->session))
Adam Langley95c29f32014-06-20 12:00:00 -07002255 SSL_SESSION_free(s->session);
2256 }
2257
2258 /* auto flush every 255 connections */
2259 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2260 ((i & mode) == mode))
2261 {
2262 if ( (((mode & SSL_SESS_CACHE_CLIENT)
David Benjamin041b58a2014-08-27 15:25:21 -04002263 ?s->initial_ctx->stats.sess_connect_good
2264 :s->initial_ctx->stats.sess_accept_good) & 0xff) == 0xff)
Adam Langley95c29f32014-06-20 12:00:00 -07002265 {
David Benjamin041b58a2014-08-27 15:25:21 -04002266 SSL_CTX_flush_sessions(s->initial_ctx,(unsigned long)time(NULL));
Adam Langley95c29f32014-06-20 12:00:00 -07002267 }
2268 }
2269 }
2270
Adam Langley95c29f32014-06-20 12:00:00 -07002271int SSL_get_error(const SSL *s,int i)
2272 {
2273 int reason;
2274 unsigned long l;
2275 BIO *bio;
2276
2277 if (i > 0) return(SSL_ERROR_NONE);
2278
2279 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2280 * etc, where we do encode the error */
2281 if ((l=ERR_peek_error()) != 0)
2282 {
2283 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2284 return(SSL_ERROR_SYSCALL);
2285 else
2286 return(SSL_ERROR_SSL);
2287 }
2288
Adam Langleyb2ce0582014-06-20 12:00:00 -07002289 if ((i < 0) && SSL_want_session(s))
2290 return(SSL_ERROR_PENDING_SESSION);
2291
Adam Langleydc9b1412014-06-20 12:00:00 -07002292 if ((i < 0) && SSL_want_certificate(s))
2293 return(SSL_ERROR_PENDING_CERTIFICATE);
2294
Adam Langley95c29f32014-06-20 12:00:00 -07002295 if ((i < 0) && SSL_want_read(s))
2296 {
2297 bio=SSL_get_rbio(s);
2298 if (BIO_should_read(bio))
2299 return(SSL_ERROR_WANT_READ);
2300 else if (BIO_should_write(bio))
2301 /* This one doesn't make too much sense ... We never try
2302 * to write to the rbio, and an application program where
2303 * rbio and wbio are separate couldn't even know what it
2304 * should wait for.
2305 * However if we ever set s->rwstate incorrectly
2306 * (so that we have SSL_want_read(s) instead of
2307 * SSL_want_write(s)) and rbio and wbio *are* the same,
2308 * this test works around that bug; so it might be safer
2309 * to keep it. */
2310 return(SSL_ERROR_WANT_WRITE);
2311 else if (BIO_should_io_special(bio))
2312 {
2313 reason=BIO_get_retry_reason(bio);
2314 if (reason == BIO_RR_CONNECT)
2315 return(SSL_ERROR_WANT_CONNECT);
2316 else if (reason == BIO_RR_ACCEPT)
2317 return(SSL_ERROR_WANT_ACCEPT);
2318 else
2319 return(SSL_ERROR_SYSCALL); /* unknown */
2320 }
2321 }
2322
2323 if ((i < 0) && SSL_want_write(s))
2324 {
2325 bio=SSL_get_wbio(s);
2326 if (BIO_should_write(bio))
2327 return(SSL_ERROR_WANT_WRITE);
2328 else if (BIO_should_read(bio))
2329 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2330 return(SSL_ERROR_WANT_READ);
2331 else if (BIO_should_io_special(bio))
2332 {
2333 reason=BIO_get_retry_reason(bio);
2334 if (reason == BIO_RR_CONNECT)
2335 return(SSL_ERROR_WANT_CONNECT);
2336 else if (reason == BIO_RR_ACCEPT)
2337 return(SSL_ERROR_WANT_ACCEPT);
2338 else
2339 return(SSL_ERROR_SYSCALL);
2340 }
2341 }
2342 if ((i < 0) && SSL_want_x509_lookup(s))
2343 {
2344 return(SSL_ERROR_WANT_X509_LOOKUP);
2345 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002346 if ((i < 0) && SSL_want_channel_id_lookup(s))
2347 {
2348 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2349 }
Adam Langley95c29f32014-06-20 12:00:00 -07002350
2351 if (i == 0)
2352 {
David Benjamin5e4f6e92014-11-17 03:23:24 -05002353 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2354 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
Adam Langley95c29f32014-06-20 12:00:00 -07002355 return(SSL_ERROR_ZERO_RETURN);
Adam Langley95c29f32014-06-20 12:00:00 -07002356 }
2357 return(SSL_ERROR_SYSCALL);
2358 }
2359
2360int SSL_do_handshake(SSL *s)
2361 {
2362 int ret=1;
2363
2364 if (s->handshake_func == NULL)
2365 {
2366 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2367 return(-1);
2368 }
2369
2370 s->method->ssl_renegotiate_check(s);
2371
David Benjamin7e237462014-12-11 18:13:28 -05002372 if (SSL_in_init(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002373 {
2374 ret=s->handshake_func(s);
2375 }
2376 return(ret);
2377 }
2378
Adam Langley95c29f32014-06-20 12:00:00 -07002379void SSL_set_accept_state(SSL *s)
2380 {
2381 s->server=1;
2382 s->shutdown=0;
2383 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2384 s->handshake_func=s->method->ssl_accept;
2385 /* clear the current cipher */
2386 ssl_clear_cipher_ctx(s);
2387 ssl_clear_hash_ctx(&s->read_hash);
2388 ssl_clear_hash_ctx(&s->write_hash);
2389 }
2390
2391void SSL_set_connect_state(SSL *s)
2392 {
2393 s->server=0;
2394 s->shutdown=0;
2395 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2396 s->handshake_func=s->method->ssl_connect;
2397 /* clear the current cipher */
2398 ssl_clear_cipher_ctx(s);
2399 ssl_clear_hash_ctx(&s->read_hash);
2400 ssl_clear_hash_ctx(&s->write_hash);
2401 }
2402
2403int ssl_undefined_function(SSL *s)
2404 {
2405 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2406 return(0);
2407 }
2408
2409int ssl_undefined_void_function(void)
2410 {
2411 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2412 return(0);
2413 }
2414
2415int ssl_undefined_const_function(const SSL *s)
2416 {
2417 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2418 return(0);
2419 }
2420
Adam Langley0f4746e2014-08-13 12:26:32 -07002421static const char *ssl_get_version(int version)
Adam Langley95c29f32014-06-20 12:00:00 -07002422 {
Adam Langley0f4746e2014-08-13 12:26:32 -07002423 if (version == TLS1_2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002424 return("TLSv1.2");
Adam Langley0f4746e2014-08-13 12:26:32 -07002425 else if (version == TLS1_1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002426 return("TLSv1.1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002427 else if (version == TLS1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002428 return("TLSv1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002429 else if (version == SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002430 return("SSLv3");
Adam Langley95c29f32014-06-20 12:00:00 -07002431 else
2432 return("unknown");
2433 }
2434
Adam Langley0f4746e2014-08-13 12:26:32 -07002435const char *SSL_get_version(const SSL *s)
2436 {
2437 return ssl_get_version(s->version);
2438 }
2439
2440const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2441 {
2442 return ssl_get_version(sess->ssl_version);
2443 }
2444
Adam Langley95c29f32014-06-20 12:00:00 -07002445void ssl_clear_cipher_ctx(SSL *s)
2446 {
2447 if (s->enc_read_ctx != NULL)
2448 {
2449 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2450 OPENSSL_free(s->enc_read_ctx);
2451 s->enc_read_ctx=NULL;
2452 }
2453 if (s->enc_write_ctx != NULL)
2454 {
2455 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2456 OPENSSL_free(s->enc_write_ctx);
2457 s->enc_write_ctx=NULL;
2458 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002459 if (s->aead_read_ctx != NULL)
2460 {
2461 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2462 OPENSSL_free(s->aead_read_ctx);
2463 s->aead_read_ctx = NULL;
2464 }
2465 if (s->aead_write_ctx != NULL)
2466 {
2467 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2468 OPENSSL_free(s->aead_write_ctx);
2469 s->aead_write_ctx = NULL;
2470 }
Adam Langley95c29f32014-06-20 12:00:00 -07002471 }
2472
2473X509 *SSL_get_certificate(const SSL *s)
2474 {
2475 if (s->cert != NULL)
2476 return(s->cert->key->x509);
2477 else
2478 return(NULL);
2479 }
2480
2481EVP_PKEY *SSL_get_privatekey(const SSL *s)
2482 {
2483 if (s->cert != NULL)
2484 return(s->cert->key->privatekey);
2485 else
2486 return(NULL);
2487 }
2488
2489X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2490 {
2491 if (ctx->cert != NULL)
2492 return ctx->cert->key->x509;
2493 else
2494 return NULL;
2495 }
2496
2497EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2498 {
2499 if (ctx->cert != NULL)
2500 return ctx->cert->key->privatekey;
2501 else
2502 return NULL ;
2503 }
2504
2505const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2506 {
2507 if ((s->session != NULL) && (s->session->cipher != NULL))
2508 return(s->session->cipher);
2509 return(NULL);
2510 }
2511const void *SSL_get_current_compression(SSL *s)
2512 {
2513 return NULL;
2514 }
2515const void *SSL_get_current_expansion(SSL *s)
2516 {
2517 return NULL;
2518 }
2519
2520int ssl_init_wbio_buffer(SSL *s,int push)
2521 {
2522 BIO *bbio;
2523
2524 if (s->bbio == NULL)
2525 {
2526 bbio=BIO_new(BIO_f_buffer());
2527 if (bbio == NULL) return(0);
2528 s->bbio=bbio;
2529 }
2530 else
2531 {
2532 bbio=s->bbio;
2533 if (s->bbio == s->wbio)
2534 s->wbio=BIO_pop(s->wbio);
2535 }
2536 (void)BIO_reset(bbio);
2537/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2538 if (!BIO_set_read_buffer_size(bbio,1))
2539 {
2540 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2541 return(0);
2542 }
2543 if (push)
2544 {
2545 if (s->wbio != bbio)
2546 s->wbio=BIO_push(bbio,s->wbio);
2547 }
2548 else
2549 {
2550 if (s->wbio == bbio)
2551 s->wbio=BIO_pop(bbio);
2552 }
2553 return(1);
2554 }
2555
2556void ssl_free_wbio_buffer(SSL *s)
2557 {
2558 if (s->bbio == NULL) return;
2559
2560 if (s->bbio == s->wbio)
2561 {
2562 /* remove buffering */
2563 s->wbio=BIO_pop(s->wbio);
2564#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2565 assert(s->wbio != NULL);
2566#endif
2567 }
2568 BIO_free(s->bbio);
2569 s->bbio=NULL;
2570 }
2571
2572void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2573 {
2574 ctx->quiet_shutdown=mode;
2575 }
2576
2577int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2578 {
2579 return(ctx->quiet_shutdown);
2580 }
2581
2582void SSL_set_quiet_shutdown(SSL *s,int mode)
2583 {
2584 s->quiet_shutdown=mode;
2585 }
2586
2587int SSL_get_quiet_shutdown(const SSL *s)
2588 {
2589 return(s->quiet_shutdown);
2590 }
2591
2592void SSL_set_shutdown(SSL *s,int mode)
2593 {
2594 s->shutdown=mode;
2595 }
2596
2597int SSL_get_shutdown(const SSL *s)
2598 {
2599 return(s->shutdown);
2600 }
2601
2602int SSL_version(const SSL *s)
2603 {
2604 return(s->version);
2605 }
2606
2607SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2608 {
2609 return(ssl->ctx);
2610 }
2611
2612SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2613 {
2614 if (ssl->ctx == ctx)
2615 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002616 if (ctx == NULL)
2617 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002618 if (ssl->cert != NULL)
2619 ssl_cert_free(ssl->cert);
2620 ssl->cert = ssl_cert_dup(ctx->cert);
2621 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2622 if (ssl->ctx != NULL)
2623 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2624 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07002625
2626 ssl->sid_ctx_length = ctx->sid_ctx_length;
2627 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2628 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2629
Adam Langley95c29f32014-06-20 12:00:00 -07002630 return(ssl->ctx);
2631 }
2632
2633#ifndef OPENSSL_NO_STDIO
2634int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2635 {
2636 return(X509_STORE_set_default_paths(ctx->cert_store));
2637 }
2638
2639int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2640 const char *CApath)
2641 {
2642 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2643 }
2644#endif
2645
2646void SSL_set_info_callback(SSL *ssl,
2647 void (*cb)(const SSL *ssl,int type,int val))
2648 {
2649 ssl->info_callback=cb;
2650 }
2651
2652/* One compiler (Diab DCC) doesn't like argument names in returned
2653 function pointer. */
2654void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2655 {
2656 return ssl->info_callback;
2657 }
2658
2659int SSL_state(const SSL *ssl)
2660 {
2661 return(ssl->state);
2662 }
2663
2664void SSL_set_state(SSL *ssl, int state)
2665 {
2666 ssl->state = state;
2667 }
2668
2669void SSL_set_verify_result(SSL *ssl,long arg)
2670 {
2671 ssl->verify_result=arg;
2672 }
2673
2674long SSL_get_verify_result(const SSL *ssl)
2675 {
2676 return(ssl->verify_result);
2677 }
2678
2679int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2680 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2681 {
2682 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2683 new_func, dup_func, free_func);
2684 }
2685
2686int SSL_set_ex_data(SSL *s,int idx,void *arg)
2687 {
2688 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2689 }
2690
2691void *SSL_get_ex_data(const SSL *s,int idx)
2692 {
2693 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2694 }
2695
2696int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2697 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2698 {
2699 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2700 new_func, dup_func, free_func);
2701 }
2702
2703int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2704 {
2705 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2706 }
2707
2708void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2709 {
2710 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2711 }
2712
2713int ssl_ok(SSL *s)
2714 {
2715 return(1);
2716 }
2717
2718X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2719 {
2720 return(ctx->cert_store);
2721 }
2722
2723void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2724 {
2725 if (ctx->cert_store != NULL)
2726 X509_STORE_free(ctx->cert_store);
2727 ctx->cert_store=store;
2728 }
2729
2730int SSL_want(const SSL *s)
2731 {
2732 return(s->rwstate);
2733 }
2734
2735/*!
2736 * \brief Set the callback for generating temporary RSA keys.
2737 * \param ctx the SSL context.
2738 * \param cb the callback
2739 */
2740
Adam Langley95c29f32014-06-20 12:00:00 -07002741void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2742 int is_export,
2743 int keylength))
2744 {
2745 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2746 }
2747
2748void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2749 int is_export,
2750 int keylength))
2751 {
2752 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2753 }
Adam Langley95c29f32014-06-20 12:00:00 -07002754
2755#ifdef DOXYGEN
2756/*!
2757 * \brief The RSA temporary key callback function.
2758 * \param ssl the SSL session.
2759 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2760 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2761 * of the required key in bits.
2762 * \return the temporary RSA key.
2763 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2764 */
2765
2766RSA *cb(SSL *ssl,int is_export,int keylength)
2767 {}
2768#endif
2769
2770/*!
2771 * \brief Set the callback for generating temporary DH keys.
2772 * \param ctx the SSL context.
2773 * \param dh the callback
2774 */
2775
Adam Langley95c29f32014-06-20 12:00:00 -07002776void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2777 int keylength))
2778 {
2779 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2780 }
2781
2782void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2783 int keylength))
2784 {
2785 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2786 }
Adam Langley95c29f32014-06-20 12:00:00 -07002787
Adam Langley95c29f32014-06-20 12:00:00 -07002788void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2789 int keylength))
2790 {
2791 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2792 }
2793
2794void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2795 int keylength))
2796 {
2797 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2798 }
Adam Langley95c29f32014-06-20 12:00:00 -07002799
Adam Langley95c29f32014-06-20 12:00:00 -07002800int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2801 {
2802 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2803 {
2804 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2805 return 0;
2806 }
2807 if (ctx->psk_identity_hint != NULL)
2808 OPENSSL_free(ctx->psk_identity_hint);
2809 if (identity_hint != NULL)
2810 {
2811 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2812 if (ctx->psk_identity_hint == NULL)
2813 return 0;
2814 }
2815 else
2816 ctx->psk_identity_hint = NULL;
2817 return 1;
2818 }
2819
2820int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
2821 {
2822 if (s == NULL)
2823 return 0;
2824
Adam Langley95c29f32014-06-20 12:00:00 -07002825 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2826 {
2827 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2828 return 0;
2829 }
Adam Langley0289c732014-06-20 12:00:00 -07002830
David Benjamin688d8df2014-11-02 23:06:42 -05002831 /* Clear currently configured hint, if any. */
Adam Langley0289c732014-06-20 12:00:00 -07002832 if (s->psk_identity_hint != NULL)
2833 {
2834 OPENSSL_free(s->psk_identity_hint);
2835 s->psk_identity_hint = NULL;
2836 }
Adam Langley0289c732014-06-20 12:00:00 -07002837
Adam Langley95c29f32014-06-20 12:00:00 -07002838 if (identity_hint != NULL)
2839 {
David Benjamin688d8df2014-11-02 23:06:42 -05002840 s->psk_identity_hint = BUF_strdup(identity_hint);
2841 if (s->psk_identity_hint == NULL)
2842 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002843 }
Adam Langley95c29f32014-06-20 12:00:00 -07002844 return 1;
2845 }
2846
2847const char *SSL_get_psk_identity_hint(const SSL *s)
2848 {
Adam Langley0289c732014-06-20 12:00:00 -07002849 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002850 return NULL;
David Benjamin688d8df2014-11-02 23:06:42 -05002851 return s->psk_identity_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07002852 }
2853
2854const char *SSL_get_psk_identity(const SSL *s)
2855 {
2856 if (s == NULL || s->session == NULL)
2857 return NULL;
2858 return(s->session->psk_identity);
2859 }
2860
2861void SSL_set_psk_client_callback(SSL *s,
2862 unsigned int (*cb)(SSL *ssl, const char *hint,
2863 char *identity, unsigned int max_identity_len, unsigned char *psk,
2864 unsigned int max_psk_len))
2865 {
2866 s->psk_client_callback = cb;
2867 }
2868
2869void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
2870 unsigned int (*cb)(SSL *ssl, const char *hint,
2871 char *identity, unsigned int max_identity_len, unsigned char *psk,
2872 unsigned int max_psk_len))
2873 {
2874 ctx->psk_client_callback = cb;
2875 }
2876
2877void SSL_set_psk_server_callback(SSL *s,
2878 unsigned int (*cb)(SSL *ssl, const char *identity,
2879 unsigned char *psk, unsigned int max_psk_len))
2880 {
2881 s->psk_server_callback = cb;
2882 }
2883
2884void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
2885 unsigned int (*cb)(SSL *ssl, const char *identity,
2886 unsigned char *psk, unsigned int max_psk_len))
2887 {
2888 ctx->psk_server_callback = cb;
2889 }
Adam Langley95c29f32014-06-20 12:00:00 -07002890
David Benjamin1eb367c2014-12-12 18:17:51 -05002891void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version)
2892 {
2893 ctx->min_version = version;
2894 }
2895
2896void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version)
2897 {
2898 ctx->max_version = version;
2899 }
2900
2901void SSL_set_min_version(SSL *ssl, uint16_t version)
2902 {
2903 ssl->min_version = version;
2904 }
2905
2906void SSL_set_max_version(SSL *ssl, uint16_t version)
2907 {
2908 ssl->max_version = version;
2909 }
2910
Adam Langley95c29f32014-06-20 12:00:00 -07002911void 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))
2912 {
2913 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2914 }
2915void 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))
2916 {
2917 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2918 }
2919
David Benjamin859ec3c2014-09-02 16:29:36 -04002920void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio)
2921 {
2922 if (ctx->keylog_bio != NULL)
2923 BIO_free(ctx->keylog_bio);
2924 ctx->keylog_bio = keylog_bio;
2925 }
2926
2927static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len)
2928 {
2929 static const char hextable[] = "0123456789abcdef";
2930 uint8_t *out;
2931 size_t i;
2932
2933 if (!CBB_add_space(cbb, &out, in_len * 2))
2934 {
2935 return 0;
2936 }
2937
2938 for (i = 0; i < in_len; i++)
2939 {
2940 *(out++) = (uint8_t)hextable[in[i] >> 4];
2941 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2942 }
2943 return 1;
2944 }
2945
2946int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
2947 const uint8_t *encrypted_premaster, size_t encrypted_premaster_len,
2948 const uint8_t *premaster, size_t premaster_len)
2949 {
2950 BIO *bio = ctx->keylog_bio;
2951 CBB cbb;
2952 uint8_t *out;
2953 size_t out_len;
2954 int ret;
2955
2956 if (bio == NULL)
2957 {
2958 return 1;
2959 }
2960
2961 if (encrypted_premaster_len < 8)
2962 {
2963 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange, ERR_R_INTERNAL_ERROR);
2964 return 0;
2965 }
2966
2967 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len*2 + 1))
2968 {
2969 return 0;
2970 }
2971 if (!CBB_add_bytes(&cbb, (const uint8_t*)"RSA ", 4) ||
2972 /* Only the first 8 bytes of the encrypted premaster secret are
2973 * logged. */
2974 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2975 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
2976 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2977 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
2978 !CBB_finish(&cbb, &out, &out_len))
2979 {
2980 CBB_cleanup(&cbb);
2981 return 0;
2982 }
2983
2984 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2985 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2986 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2987
2988 OPENSSL_free(out);
2989 return ret;
2990 }
2991
2992int ssl_ctx_log_master_secret(SSL_CTX *ctx,
2993 const uint8_t *client_random, size_t client_random_len,
2994 const uint8_t *master, size_t master_len)
2995 {
2996 BIO *bio = ctx->keylog_bio;
2997 CBB cbb;
2998 uint8_t *out;
2999 size_t out_len;
3000 int ret;
3001
3002 if (bio == NULL)
3003 {
3004 return 1;
3005 }
3006
3007 if (client_random_len != 32)
3008 {
3009 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
3010 return 0;
3011 }
3012
3013 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len*2 + 1))
3014 {
3015 return 0;
3016 }
3017 if (!CBB_add_bytes(&cbb, (const uint8_t*)"CLIENT_RANDOM ", 14) ||
3018 !cbb_add_hex(&cbb, client_random, 32) ||
3019 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3020 !cbb_add_hex(&cbb, master, master_len) ||
3021 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3022 !CBB_finish(&cbb, &out, &out_len))
3023 {
3024 CBB_cleanup(&cbb);
3025 return 0;
3026 }
3027
3028 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3029 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3030 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3031
3032 OPENSSL_free(out);
3033 return ret;
3034 }
3035
Adam Langleyadb739e2014-06-20 12:00:00 -07003036int SSL_cutthrough_complete(const SSL *s)
3037 {
3038 return (!s->server && /* cutthrough only applies to clients */
3039 !s->hit && /* full-handshake */
3040 s->version >= SSL3_VERSION &&
3041 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3042 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3043 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3044 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3045 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
David Benjamin7e3305e2014-07-28 14:52:32 -04003046 s->state == SSL3_ST_CR_CHANGE ||
Adam Langleyadb739e2014-06-20 12:00:00 -07003047 s->state == SSL3_ST_CR_FINISHED_A));
3048 }
3049
Adam Langley95f22882014-06-20 12:00:00 -07003050void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3051 size_t* ssl_session_size)
3052{
3053 *ssl_size = sizeof(SSL);
3054 *ssl_ctx_size = sizeof(SSL_CTX);
3055 *ssl_session_size = sizeof(SSL_SESSION);
3056}
3057
Adam Langleyadb739e2014-06-20 12:00:00 -07003058int ssl3_can_cutthrough(const SSL *s)
3059 {
3060 const SSL_CIPHER *c;
3061
3062 /* require a strong enough cipher */
3063 if (SSL_get_cipher_bits(s, NULL) < 128)
3064 return 0;
3065
3066 /* require ALPN or NPN extension */
David Benjamin457112e2014-08-27 14:35:09 -04003067 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen)
Adam Langleyadb739e2014-06-20 12:00:00 -07003068 {
3069 return 0;
3070 }
3071
3072 /* require a forward-secret cipher */
3073 c = SSL_get_current_cipher(s);
3074 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3075 c->algorithm_mkey != SSL_kEECDH))
3076 {
3077 return 0;
3078 }
3079
3080 return 1;
3081 }
3082
David Benjamine99e9122014-12-11 01:46:01 -05003083const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version)
3084 {
3085 switch (version)
3086 {
3087 case SSL3_VERSION:
3088 return &SSLv3_enc_data;
3089 case TLS1_VERSION:
3090 return &TLSv1_enc_data;
3091 case TLS1_1_VERSION:
3092 return &TLSv1_1_enc_data;
3093 case TLS1_2_VERSION:
3094 return &TLSv1_2_enc_data;
3095 case DTLS1_VERSION:
3096 return &DTLSv1_enc_data;
3097 case DTLS1_2_VERSION:
3098 return &DTLSv1_2_enc_data;
3099 default:
3100 return NULL;
3101 }
3102 }
3103
David Benjaminceb6f282014-12-07 23:56:19 -05003104uint16_t ssl3_get_max_server_version(const SSL *s)
3105 {
David Benjamin1eb367c2014-12-12 18:17:51 -05003106 uint16_t max_version;
3107
David Benjaminceb6f282014-12-07 23:56:19 -05003108 if (SSL_IS_DTLS(s))
3109 {
David Benjamin1eb367c2014-12-12 18:17:51 -05003110 max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
3111 if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version)
David Benjaminceb6f282014-12-07 23:56:19 -05003112 return DTLS1_2_VERSION;
David Benjamin1eb367c2014-12-12 18:17:51 -05003113 if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version)
David Benjaminceb6f282014-12-07 23:56:19 -05003114 return DTLS1_VERSION;
3115 return 0;
3116 }
Adam Langley29707792014-06-20 12:00:00 -07003117
David Benjamin1eb367c2014-12-12 18:17:51 -05003118 max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
3119 if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version)
Adam Langley29707792014-06-20 12:00:00 -07003120 return TLS1_2_VERSION;
David Benjamin1eb367c2014-12-12 18:17:51 -05003121 if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version)
Adam Langley29707792014-06-20 12:00:00 -07003122 return TLS1_1_VERSION;
David Benjamin1eb367c2014-12-12 18:17:51 -05003123 if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version)
Adam Langley29707792014-06-20 12:00:00 -07003124 return TLS1_VERSION;
David Benjamin1eb367c2014-12-12 18:17:51 -05003125 if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version)
Adam Langley29707792014-06-20 12:00:00 -07003126 return SSL3_VERSION;
Adam Langley29707792014-06-20 12:00:00 -07003127 return 0;
3128 }
3129
David Benjaminceb6f282014-12-07 23:56:19 -05003130uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version)
3131 {
David Benjamin1eb367c2014-12-12 18:17:51 -05003132 uint16_t version = 0;
3133
David Benjaminceb6f282014-12-07 23:56:19 -05003134 if (SSL_IS_DTLS(s))
3135 {
David Benjamin1eb367c2014-12-12 18:17:51 -05003136 /* Clamp client_version to max_version. */
3137 if (s->max_version != 0 && client_version < s->max_version)
3138 client_version = s->max_version;
3139
David Benjaminceb6f282014-12-07 23:56:19 -05003140 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2))
David Benjamin1eb367c2014-12-12 18:17:51 -05003141 version = DTLS1_2_VERSION;
3142 else if (client_version <= DTLS1_VERSION && !(s->options & SSL_OP_NO_DTLSv1))
3143 version = DTLS1_VERSION;
3144
3145 /* Check against min_version. */
3146 if (version != 0 && s->min_version != 0 && version > s->min_version)
3147 return 0;
3148 return version;
David Benjaminceb6f282014-12-07 23:56:19 -05003149 }
3150 else
3151 {
David Benjamin1eb367c2014-12-12 18:17:51 -05003152 /* Clamp client_version to max_version. */
3153 if (s->max_version != 0 && client_version > s->max_version)
3154 client_version = s->max_version;
3155
David Benjaminceb6f282014-12-07 23:56:19 -05003156 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2))
David Benjamin1eb367c2014-12-12 18:17:51 -05003157 version = TLS1_2_VERSION;
3158 else if (client_version >= TLS1_1_VERSION && !(s->options & SSL_OP_NO_TLSv1_1))
3159 version = TLS1_1_VERSION;
3160 else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1))
3161 version = TLS1_VERSION;
3162 else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3))
3163 version = SSL3_VERSION;
3164
3165 /* Check against min_version. */
3166 if (version != 0 && s->min_version != 0 && version < s->min_version)
3167 return 0;
3168 return version;
David Benjaminceb6f282014-12-07 23:56:19 -05003169 }
3170 }
3171
3172uint16_t ssl3_get_max_client_version(SSL *s)
3173 {
3174 unsigned long options = s->options;
3175 uint16_t version = 0;
3176
3177 /* OpenSSL's API for controlling versions entails blacklisting
3178 * individual protocols. This has two problems. First, on the client,
3179 * the protocol can only express a contiguous range of versions. Second,
3180 * a library consumer trying to set a maximum version cannot disable
3181 * protocol versions that get added in a future version of the library.
3182 *
3183 * To account for both of these, OpenSSL interprets the client-side
3184 * bitmask as a min/max range by picking the lowest contiguous non-empty
3185 * range of enabled protocols. Note that this means it is impossible to
3186 * set a maximum version of TLS 1.2 in a future-proof way.
3187 *
3188 * By this scheme, the maximum version is the lowest version V such that
David Benjamin1eb367c2014-12-12 18:17:51 -05003189 * V is enabled and V+1 is disabled or unimplemented. */
David Benjaminceb6f282014-12-07 23:56:19 -05003190 if (SSL_IS_DTLS(s))
3191 {
3192 if (!(options & SSL_OP_NO_DTLSv1_2))
3193 version = DTLS1_2_VERSION;
3194 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2))
3195 version = DTLS1_VERSION;
David Benjamin1eb367c2014-12-12 18:17:51 -05003196 if (s->max_version != 0 && version < s->max_version)
3197 version = s->max_version;
David Benjaminceb6f282014-12-07 23:56:19 -05003198 }
3199 else
3200 {
3201 if (!(options & SSL_OP_NO_TLSv1_2))
3202 version = TLS1_2_VERSION;
3203 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2))
3204 version = TLS1_1_VERSION;
3205 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1))
3206 version = TLS1_VERSION;
3207 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1))
3208 version = SSL3_VERSION;
David Benjamin1eb367c2014-12-12 18:17:51 -05003209 if (s->max_version != 0 && version > s->max_version)
3210 version = s->max_version;
David Benjaminceb6f282014-12-07 23:56:19 -05003211 }
3212
3213 return version;
3214 }
3215
3216int ssl3_is_version_enabled(SSL *s, uint16_t version)
3217 {
3218 if (SSL_IS_DTLS(s))
3219 {
David Benjamin1eb367c2014-12-12 18:17:51 -05003220 if (s->max_version != 0 && version < s->max_version)
3221 return 0;
3222 if (s->min_version != 0 && version > s->min_version)
3223 return 0;
David Benjaminceb6f282014-12-07 23:56:19 -05003224 switch (version)
3225 {
3226 case DTLS1_VERSION:
3227 return !(s->options & SSL_OP_NO_DTLSv1);
3228 case DTLS1_2_VERSION:
3229 return !(s->options & SSL_OP_NO_DTLSv1_2);
3230 default:
3231 return 0;
3232 }
3233 }
3234 else
3235 {
David Benjamin1eb367c2014-12-12 18:17:51 -05003236 if (s->max_version != 0 && version > s->max_version)
3237 return 0;
3238 if (s->min_version != 0 && version < s->min_version)
3239 return 0;
David Benjaminceb6f282014-12-07 23:56:19 -05003240 switch (version)
3241 {
3242 case SSL3_VERSION:
3243 return !(s->options & SSL_OP_NO_SSLv3);
3244 case TLS1_VERSION:
3245 return !(s->options & SSL_OP_NO_TLSv1);
3246 case TLS1_1_VERSION:
3247 return !(s->options & SSL_OP_NO_TLSv1_1);
3248 case TLS1_2_VERSION:
3249 return !(s->options & SSL_OP_NO_TLSv1_2);
3250 default:
3251 return 0;
3252 }
3253 }
3254 }
3255
Adam Langley95c29f32014-06-20 12:00:00 -07003256/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3257 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3258 * any. If EVP_MD pointer is passed, initializes ctx with this md
3259 * Returns newly allocated ctx;
3260 */
3261
3262EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3263{
3264 ssl_clear_hash_ctx(hash);
3265 *hash = EVP_MD_CTX_create();
David Benjaminae3e4872014-11-18 21:52:26 -05003266 if (md != NULL && *hash != NULL &&
3267 !EVP_DigestInit_ex(*hash, md, NULL))
Adam Langley69a01602014-11-17 17:26:55 -08003268 {
David Benjaminae3e4872014-11-18 21:52:26 -05003269 EVP_MD_CTX_destroy(*hash);
3270 *hash = NULL;
Adam Langley69a01602014-11-17 17:26:55 -08003271 }
Adam Langley95c29f32014-06-20 12:00:00 -07003272 return *hash;
3273}
3274void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3275{
3276
3277 if (*hash) EVP_MD_CTX_destroy(*hash);
3278 *hash=NULL;
3279}
3280
Adam Langley95c29f32014-06-20 12:00:00 -07003281int SSL_cache_hit(SSL *s)
3282 {
3283 return s->hit;
3284 }
3285
3286int SSL_is_server(SSL *s)
3287 {
3288 return s->server;
3289 }
Feng Lu41aa3252014-11-21 22:47:56 -08003290
3291void SSL_enable_fastradio_padding(SSL *s, char on_off)
3292 {
3293 s->fastradio_padding = on_off;
3294 }