blob: 241bcc3c666b78ee51b34999ef94c2fc82c7d5d2 [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
208 s->version=s->method->version;
209 s->client_version=s->version;
210 s->rwstate=SSL_NOTHING;
211 s->rstate=SSL_ST_READ_HEADER;
212#if 0
213 s->read_ahead=s->ctx->read_ahead;
214#endif
215
216 if (s->init_buf != NULL)
217 {
218 BUF_MEM_free(s->init_buf);
219 s->init_buf=NULL;
220 }
221
222 ssl_clear_cipher_ctx(s);
223 ssl_clear_hash_ctx(&s->read_hash);
224 ssl_clear_hash_ctx(&s->write_hash);
225
David Benjaminbb15e3d2014-11-29 16:03:41 -0500226 s->method->ssl_clear(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700227 return(1);
228 }
229
Adam Langley95c29f32014-06-20 12:00:00 -0700230SSL *SSL_new(SSL_CTX *ctx)
231 {
232 SSL *s;
233
234 if (ctx == NULL)
235 {
236 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
237 return(NULL);
238 }
239 if (ctx->method == NULL)
240 {
241 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
242 return(NULL);
243 }
244
245 s=(SSL *)OPENSSL_malloc(sizeof(SSL));
246 if (s == NULL) goto err;
247 memset(s,0,sizeof(SSL));
248
249 s->options=ctx->options;
250 s->mode=ctx->mode;
251 s->max_cert_list=ctx->max_cert_list;
252
253 if (ctx->cert != NULL)
254 {
255 /* Earlier library versions used to copy the pointer to
256 * the CERT, not its contents; only when setting new
257 * parameters for the per-SSL copy, ssl_cert_new would be
258 * called (and the direct reference to the per-SSL_CTX
259 * settings would be lost, but those still were indirectly
260 * accessed for various purposes, and for that reason they
261 * used to be known as s->ctx->default_cert).
262 * Now we don't look at the SSL_CTX's CERT after having
263 * duplicated it once. */
264
265 s->cert = ssl_cert_dup(ctx->cert);
266 if (s->cert == NULL)
267 goto err;
268 }
269 else
270 s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
271
272 s->read_ahead=ctx->read_ahead;
273 s->msg_callback=ctx->msg_callback;
274 s->msg_callback_arg=ctx->msg_callback_arg;
275 s->verify_mode=ctx->verify_mode;
276#if 0
277 s->verify_depth=ctx->verify_depth;
278#endif
279 s->sid_ctx_length=ctx->sid_ctx_length;
280 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
281 memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
282 s->verify_callback=ctx->default_verify_callback;
283 s->generate_session_id=ctx->generate_session_id;
284
285 s->param = X509_VERIFY_PARAM_new();
286 if (!s->param)
287 goto err;
288 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
289#if 0
290 s->purpose = ctx->purpose;
291 s->trust = ctx->trust;
292#endif
293 s->quiet_shutdown=ctx->quiet_shutdown;
294 s->max_send_fragment = ctx->max_send_fragment;
295
296 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
297 s->ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700298 s->tlsext_debug_cb = 0;
299 s->tlsext_debug_arg = NULL;
300 s->tlsext_ticket_expected = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700301 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
302 s->initial_ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700303 if (ctx->tlsext_ecpointformatlist)
304 {
305 s->tlsext_ecpointformatlist =
306 BUF_memdup(ctx->tlsext_ecpointformatlist,
307 ctx->tlsext_ecpointformatlist_length);
308 if (!s->tlsext_ecpointformatlist)
309 goto err;
310 s->tlsext_ecpointformatlist_length =
311 ctx->tlsext_ecpointformatlist_length;
312 }
313 if (ctx->tlsext_ellipticcurvelist)
314 {
315 s->tlsext_ellipticcurvelist =
316 BUF_memdup(ctx->tlsext_ellipticcurvelist,
David Benjamin072334d2014-07-13 16:24:27 -0400317 ctx->tlsext_ellipticcurvelist_length * 2);
Adam Langley95c29f32014-06-20 12:00:00 -0700318 if (!s->tlsext_ellipticcurvelist)
319 goto err;
320 s->tlsext_ellipticcurvelist_length =
321 ctx->tlsext_ellipticcurvelist_length;
322 }
Adam Langley95c29f32014-06-20 12:00:00 -0700323 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700324
325 if (s->ctx->alpn_client_proto_list)
326 {
David Benjamin072c9532014-07-26 11:44:25 -0400327 s->alpn_client_proto_list = BUF_memdup(
328 s->ctx->alpn_client_proto_list,
329 s->ctx->alpn_client_proto_list_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700330 if (s->alpn_client_proto_list == NULL)
331 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700332 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
333 }
Adam Langley95c29f32014-06-20 12:00:00 -0700334
335 s->verify_result=X509_V_OK;
336
337 s->method=ctx->method;
338
339 if (!s->method->ssl_new(s))
340 goto err;
David Benjamine99e9122014-12-11 01:46:01 -0500341 s->enc_method = ssl3_get_enc_method(s->version);
342 assert(s->enc_method != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -0700343
344 s->references=1;
Adam Langley95c29f32014-06-20 12:00:00 -0700345
346 SSL_clear(s);
347
348 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
349
Adam Langley0289c732014-06-20 12:00:00 -0700350 s->psk_identity_hint = NULL;
351 if (ctx->psk_identity_hint)
352 {
353 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
354 if (s->psk_identity_hint == NULL)
355 goto err;
356 }
Adam Langley95c29f32014-06-20 12:00:00 -0700357 s->psk_client_callback=ctx->psk_client_callback;
358 s->psk_server_callback=ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700359
David Benjaminff42cc12014-11-26 00:16:14 -0500360 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
361 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200362
Adam Langley95c29f32014-06-20 12:00:00 -0700363 return(s);
364err:
365 if (s != NULL)
366 {
367 if (s->cert != NULL)
368 ssl_cert_free(s->cert);
369 if (s->ctx != NULL)
370 SSL_CTX_free(s->ctx); /* decrement reference count */
371 OPENSSL_free(s);
372 }
373 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
374 return(NULL);
375 }
376
377int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
378 unsigned int sid_ctx_len)
379 {
380 if(sid_ctx_len > sizeof ctx->sid_ctx)
381 {
382 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
383 return 0;
384 }
385 ctx->sid_ctx_length=sid_ctx_len;
386 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
387
388 return 1;
389 }
390
391int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
392 unsigned int sid_ctx_len)
393 {
394 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
395 {
396 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
397 return 0;
398 }
399 ssl->sid_ctx_length=sid_ctx_len;
400 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
401
402 return 1;
403 }
404
405int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
406 {
407 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
408 ctx->generate_session_id = cb;
409 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
410 return 1;
411 }
412
413int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
414 {
415 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
416 ssl->generate_session_id = cb;
417 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
418 return 1;
419 }
420
421int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
422 unsigned int id_len)
423 {
424 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
425 * we can "construct" a session to give us the desired check - ie. to
426 * find if there's a session in the hash table that would conflict with
427 * any new session built out of this id/id_len and the ssl_version in
428 * use by this SSL. */
429 SSL_SESSION r, *p;
430
431 if(id_len > sizeof r.session_id)
432 return 0;
433
434 r.ssl_version = ssl->version;
435 r.session_id_length = id_len;
436 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700437
438 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
439 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
440 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
441 return (p != NULL);
442 }
443
444int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
445 {
446 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
447 }
448
449int SSL_set_purpose(SSL *s, int purpose)
450 {
451 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
452 }
453
454int SSL_CTX_set_trust(SSL_CTX *s, int trust)
455 {
456 return X509_VERIFY_PARAM_set_trust(s->param, trust);
457 }
458
459int SSL_set_trust(SSL *s, int trust)
460 {
461 return X509_VERIFY_PARAM_set_trust(s->param, trust);
462 }
463
464int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
465 {
466 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
467 }
468
469int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
470 {
471 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
472 }
473
Adam Langley858a88d2014-06-20 12:00:00 -0700474void ssl_cipher_preference_list_free(
475 struct ssl_cipher_preference_list_st *cipher_list)
476 {
477 sk_SSL_CIPHER_free(cipher_list->ciphers);
478 OPENSSL_free(cipher_list->in_group_flags);
479 OPENSSL_free(cipher_list);
480 }
481
482struct ssl_cipher_preference_list_st*
483ssl_cipher_preference_list_dup(
484 struct ssl_cipher_preference_list_st *cipher_list)
485 {
486 struct ssl_cipher_preference_list_st* ret = NULL;
487 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
488
489 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
490 if (!ret)
491 goto err;
492 ret->ciphers = NULL;
493 ret->in_group_flags = NULL;
494 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
495 if (!ret->ciphers)
496 goto err;
David Benjamin072c9532014-07-26 11:44:25 -0400497 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
Adam Langley858a88d2014-06-20 12:00:00 -0700498 if (!ret->in_group_flags)
499 goto err;
Adam Langley858a88d2014-06-20 12:00:00 -0700500 return ret;
501
502err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400503 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700504 sk_SSL_CIPHER_free(ret->ciphers);
505 if (ret)
506 OPENSSL_free(ret);
507 return NULL;
508 }
509
510struct ssl_cipher_preference_list_st*
511ssl_cipher_preference_list_from_ciphers(STACK_OF(SSL_CIPHER) *ciphers)
512 {
513 struct ssl_cipher_preference_list_st* ret = NULL;
514 size_t n = sk_SSL_CIPHER_num(ciphers);
515
516 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
517 if (!ret)
518 goto err;
519 ret->ciphers = NULL;
520 ret->in_group_flags = NULL;
521 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
522 if (!ret->ciphers)
523 goto err;
524 ret->in_group_flags = OPENSSL_malloc(n);
525 if (!ret->in_group_flags)
526 goto err;
527 memset(ret->in_group_flags, 0, n);
528 return ret;
529
530err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400531 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700532 sk_SSL_CIPHER_free(ret->ciphers);
533 if (ret)
534 OPENSSL_free(ret);
535 return NULL;
536 }
537
Adam Langley95c29f32014-06-20 12:00:00 -0700538X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
539 {
540 return ctx->param;
541 }
542
543X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
544 {
545 return ssl->param;
546 }
547
548void SSL_certs_clear(SSL *s)
549 {
550 ssl_cert_clear_certs(s->cert);
551 }
552
553void SSL_free(SSL *s)
554 {
555 int i;
556
557 if(s == NULL)
558 return;
559
560 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
561#ifdef REF_PRINT
562 REF_PRINT("SSL",s);
563#endif
564 if (i > 0) return;
565#ifdef REF_CHECK
566 if (i < 0)
567 {
568 fprintf(stderr,"SSL_free, bad reference count\n");
569 abort(); /* ok */
570 }
571#endif
572
573 if (s->param)
574 X509_VERIFY_PARAM_free(s->param);
575
576 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
577
578 if (s->bbio != NULL)
579 {
580 /* If the buffering BIO is in place, pop it off */
581 if (s->bbio == s->wbio)
582 {
583 s->wbio=BIO_pop(s->wbio);
584 }
585 BIO_free(s->bbio);
586 s->bbio=NULL;
587 }
588 if (s->rbio != NULL)
589 BIO_free_all(s->rbio);
590 if ((s->wbio != NULL) && (s->wbio != s->rbio))
591 BIO_free_all(s->wbio);
592
593 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
594
595 /* add extra stuff */
Adam Langley858a88d2014-06-20 12:00:00 -0700596 if (s->cipher_list != NULL)
597 ssl_cipher_preference_list_free(s->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -0700598 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
599
600 /* Make the next call work :-) */
601 if (s->session != NULL)
602 {
603 ssl_clear_bad_session(s);
604 SSL_SESSION_free(s->session);
605 }
606
607 ssl_clear_cipher_ctx(s);
608 ssl_clear_hash_ctx(&s->read_hash);
609 ssl_clear_hash_ctx(&s->write_hash);
610
611 if (s->cert != NULL) ssl_cert_free(s->cert);
612 /* Free up if allocated */
613
Adam Langley95c29f32014-06-20 12:00:00 -0700614 if (s->tlsext_hostname)
615 OPENSSL_free(s->tlsext_hostname);
616 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700617 if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
618 if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -0700619 if (s->alpn_client_proto_list)
620 OPENSSL_free(s->alpn_client_proto_list);
Adam Langley1258b6a2014-06-20 12:00:00 -0700621 if (s->tlsext_channel_id_private)
622 EVP_PKEY_free(s->tlsext_channel_id_private);
Adam Langley95c29f32014-06-20 12:00:00 -0700623
Adam Langley0289c732014-06-20 12:00:00 -0700624 if (s->psk_identity_hint)
625 OPENSSL_free(s->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -0700626
Adam Langley95c29f32014-06-20 12:00:00 -0700627 if (s->client_CA != NULL)
628 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
629
630 if (s->method != NULL) s->method->ssl_free(s);
631
632 if (s->ctx) SSL_CTX_free(s->ctx);
633
Adam Langley95c29f32014-06-20 12:00:00 -0700634 if (s->next_proto_negotiated)
635 OPENSSL_free(s->next_proto_negotiated);
Adam Langley95c29f32014-06-20 12:00:00 -0700636
637 if (s->srtp_profiles)
638 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
639
Adam Langley95c29f32014-06-20 12:00:00 -0700640 OPENSSL_free(s);
641 }
642
643void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
644 {
645 /* If the output buffering BIO is still in place, remove it
646 */
647 if (s->bbio != NULL)
648 {
649 if (s->wbio == s->bbio)
650 {
651 s->wbio=s->wbio->next_bio;
652 s->bbio->next_bio=NULL;
653 }
654 }
655 if ((s->rbio != NULL) && (s->rbio != rbio))
656 BIO_free_all(s->rbio);
657 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
658 BIO_free_all(s->wbio);
659 s->rbio=rbio;
660 s->wbio=wbio;
661 }
662
663BIO *SSL_get_rbio(const SSL *s)
664 { return(s->rbio); }
665
666BIO *SSL_get_wbio(const SSL *s)
667 { return(s->wbio); }
668
669int SSL_get_fd(const SSL *s)
670 {
671 return(SSL_get_rfd(s));
672 }
673
674int SSL_get_rfd(const SSL *s)
675 {
676 int ret= -1;
677 BIO *b,*r;
678
679 b=SSL_get_rbio(s);
680 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
681 if (r != NULL)
682 BIO_get_fd(r,&ret);
683 return(ret);
684 }
685
686int SSL_get_wfd(const SSL *s)
687 {
688 int ret= -1;
689 BIO *b,*r;
690
691 b=SSL_get_wbio(s);
692 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
693 if (r != NULL)
694 BIO_get_fd(r,&ret);
695 return(ret);
696 }
697
698#ifndef OPENSSL_NO_SOCK
699int SSL_set_fd(SSL *s,int fd)
700 {
701 int ret=0;
702 BIO *bio=NULL;
703
704 bio=BIO_new(BIO_s_fd());
705
706 if (bio == NULL)
707 {
708 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
709 goto err;
710 }
711 BIO_set_fd(bio,fd,BIO_NOCLOSE);
712 SSL_set_bio(s,bio,bio);
713 ret=1;
714err:
715 return(ret);
716 }
717
718int SSL_set_wfd(SSL *s,int fd)
719 {
720 int ret=0;
721 BIO *bio=NULL;
722
723 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_FD)
724 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
725 {
726 bio=BIO_new(BIO_s_fd());
727
728 if (bio == NULL)
729 {
730 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
731 goto err;
732 }
733 BIO_set_fd(bio,fd,BIO_NOCLOSE);
734 SSL_set_bio(s,SSL_get_rbio(s),bio);
735 }
736 else
737 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
738 ret=1;
739err:
740 return(ret);
741 }
742
743int SSL_set_rfd(SSL *s,int fd)
744 {
745 int ret=0;
746 BIO *bio=NULL;
747
748 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_FD)
749 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
750 {
751 bio=BIO_new(BIO_s_fd());
752
753 if (bio == NULL)
754 {
755 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
756 goto err;
757 }
758 BIO_set_fd(bio,fd,BIO_NOCLOSE);
759 SSL_set_bio(s,bio,SSL_get_wbio(s));
760 }
761 else
762 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
763 ret=1;
764err:
765 return(ret);
766 }
767#endif
768
769
770/* return length of latest Finished message we sent, copy to 'buf' */
771size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
772 {
773 size_t ret = 0;
774
775 if (s->s3 != NULL)
776 {
777 ret = s->s3->tmp.finish_md_len;
778 if (count > ret)
779 count = ret;
780 memcpy(buf, s->s3->tmp.finish_md, count);
781 }
782 return ret;
783 }
784
785/* return length of latest Finished message we expected, copy to 'buf' */
786size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
787 {
788 size_t ret = 0;
789
790 if (s->s3 != NULL)
791 {
792 ret = s->s3->tmp.peer_finish_md_len;
793 if (count > ret)
794 count = ret;
795 memcpy(buf, s->s3->tmp.peer_finish_md, count);
796 }
797 return ret;
798 }
799
800
801int SSL_get_verify_mode(const SSL *s)
802 {
803 return(s->verify_mode);
804 }
805
806int SSL_get_verify_depth(const SSL *s)
807 {
808 return X509_VERIFY_PARAM_get_depth(s->param);
809 }
810
811int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
812 {
813 return(s->verify_callback);
814 }
815
816int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
817 {
818 return(ctx->verify_mode);
819 }
820
821int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
822 {
823 return X509_VERIFY_PARAM_get_depth(ctx->param);
824 }
825
826int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
827 {
828 return(ctx->default_verify_callback);
829 }
830
831void SSL_set_verify(SSL *s,int mode,
832 int (*callback)(int ok,X509_STORE_CTX *ctx))
833 {
834 s->verify_mode=mode;
835 if (callback != NULL)
836 s->verify_callback=callback;
837 }
838
839void SSL_set_verify_depth(SSL *s,int depth)
840 {
841 X509_VERIFY_PARAM_set_depth(s->param, depth);
842 }
843
844void SSL_set_read_ahead(SSL *s,int yes)
845 {
846 s->read_ahead=yes;
847 }
848
849int SSL_get_read_ahead(const SSL *s)
850 {
851 return(s->read_ahead);
852 }
853
854int SSL_pending(const SSL *s)
855 {
856 /* SSL_pending cannot work properly if read-ahead is enabled
857 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
858 * and it is impossible to fix since SSL_pending cannot report
859 * errors that may be observed while scanning the new data.
860 * (Note that SSL_pending() is often used as a boolean value,
861 * so we'd better not return -1.)
862 */
863 return(s->method->ssl_pending(s));
864 }
865
866X509 *SSL_get_peer_certificate(const SSL *s)
867 {
868 X509 *r;
869
870 if ((s == NULL) || (s->session == NULL))
871 r=NULL;
872 else
873 r=s->session->peer;
874
David Benjamin150c6172014-08-05 22:22:49 -0400875 if (r == NULL)
876 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700877
David Benjamin150c6172014-08-05 22:22:49 -0400878 return X509_up_ref(r);
Adam Langley95c29f32014-06-20 12:00:00 -0700879 }
880
881STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
882 {
883 STACK_OF(X509) *r;
884
885 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
886 r=NULL;
887 else
888 r=s->session->sess_cert->cert_chain;
889
890 /* If we are a client, cert_chain includes the peer's own
891 * certificate; if we are a server, it does not. */
892
893 return(r);
894 }
895
Adam Langley95c29f32014-06-20 12:00:00 -0700896/* Fix this so it checks all the valid key/cert options */
897int SSL_CTX_check_private_key(const SSL_CTX *ctx)
898 {
899 if ( (ctx == NULL) ||
900 (ctx->cert == NULL) ||
901 (ctx->cert->key->x509 == NULL))
902 {
903 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
904 return(0);
905 }
906 if (ctx->cert->key->privatekey == NULL)
907 {
908 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
909 return(0);
910 }
911 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
912 }
913
914/* Fix this function so that it takes an optional type parameter */
915int SSL_check_private_key(const SSL *ssl)
916 {
917 if (ssl == NULL)
918 {
919 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
920 return(0);
921 }
922 if (ssl->cert == NULL)
923 {
924 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
925 return 0;
926 }
927 if (ssl->cert->key->x509 == NULL)
928 {
929 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
930 return(0);
931 }
932 if (ssl->cert->key->privatekey == NULL)
933 {
934 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
935 return(0);
936 }
937 return(X509_check_private_key(ssl->cert->key->x509,
938 ssl->cert->key->privatekey));
939 }
940
941int SSL_accept(SSL *s)
942 {
943 if (s->handshake_func == 0)
944 /* Not properly initialized yet */
945 SSL_set_accept_state(s);
946
David Benjamin0b145c22014-11-26 20:10:09 -0500947 if (s->handshake_func != s->method->ssl_accept)
948 {
949 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
950 return -1;
951 }
952
953 return s->handshake_func(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700954 }
955
956int SSL_connect(SSL *s)
957 {
958 if (s->handshake_func == 0)
959 /* Not properly initialized yet */
960 SSL_set_connect_state(s);
961
David Benjamin0b145c22014-11-26 20:10:09 -0500962 if (s->handshake_func != s->method->ssl_connect)
963 {
964 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
965 return -1;
966 }
967
968 return s->handshake_func(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700969 }
970
971long SSL_get_default_timeout(const SSL *s)
972 {
David Benjaminf4501342014-08-14 17:24:37 -0400973 return SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -0700974 }
975
976int SSL_read(SSL *s,void *buf,int num)
977 {
978 if (s->handshake_func == 0)
979 {
980 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
981 return -1;
982 }
983
984 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
985 {
986 s->rwstate=SSL_NOTHING;
987 return(0);
988 }
989 return(s->method->ssl_read(s,buf,num));
990 }
991
992int SSL_peek(SSL *s,void *buf,int num)
993 {
994 if (s->handshake_func == 0)
995 {
996 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
997 return -1;
998 }
999
1000 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1001 {
1002 return(0);
1003 }
1004 return(s->method->ssl_peek(s,buf,num));
1005 }
1006
1007int SSL_write(SSL *s,const void *buf,int num)
1008 {
1009 if (s->handshake_func == 0)
1010 {
1011 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
1012 return -1;
1013 }
1014
1015 if (s->shutdown & SSL_SENT_SHUTDOWN)
1016 {
1017 s->rwstate=SSL_NOTHING;
1018 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
1019 return(-1);
1020 }
1021 return(s->method->ssl_write(s,buf,num));
1022 }
1023
1024int SSL_shutdown(SSL *s)
1025 {
1026 /* Note that this function behaves differently from what one might
1027 * expect. Return values are 0 for no success (yet),
1028 * 1 for success; but calling it once is usually not enough,
1029 * even if blocking I/O is used (see ssl3_shutdown).
1030 */
1031
1032 if (s->handshake_func == 0)
1033 {
1034 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1035 return -1;
1036 }
1037
Adam Langleyb2cb0ec2014-09-02 14:28:49 -07001038 if (!SSL_in_init(s))
Adam Langley95c29f32014-06-20 12:00:00 -07001039 return(s->method->ssl_shutdown(s));
1040 else
1041 return(1);
1042 }
1043
1044int SSL_renegotiate(SSL *s)
1045 {
1046 if (s->renegotiate == 0)
1047 s->renegotiate=1;
1048
1049 s->new_session=1;
1050
1051 return(s->method->ssl_renegotiate(s));
1052 }
1053
1054int SSL_renegotiate_abbreviated(SSL *s)
1055 {
1056 if (s->renegotiate == 0)
1057 s->renegotiate=1;
1058
1059 s->new_session=0;
1060
1061 return(s->method->ssl_renegotiate(s));
1062 }
1063
1064int SSL_renegotiate_pending(SSL *s)
1065 {
1066 /* becomes true when negotiation is requested;
1067 * false again once a handshake has finished */
1068 return (s->renegotiate != 0);
1069 }
1070
1071long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1072 {
1073 long l;
1074
1075 switch (cmd)
1076 {
1077 case SSL_CTRL_GET_READ_AHEAD:
1078 return(s->read_ahead);
1079 case SSL_CTRL_SET_READ_AHEAD:
1080 l=s->read_ahead;
1081 s->read_ahead=larg;
1082 return(l);
1083
1084 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1085 s->msg_callback_arg = parg;
1086 return 1;
1087
1088 case SSL_CTRL_OPTIONS:
1089 return(s->options|=larg);
1090 case SSL_CTRL_CLEAR_OPTIONS:
1091 return(s->options&=~larg);
1092 case SSL_CTRL_MODE:
1093 return(s->mode|=larg);
1094 case SSL_CTRL_CLEAR_MODE:
1095 return(s->mode &=~larg);
1096 case SSL_CTRL_GET_MAX_CERT_LIST:
1097 return(s->max_cert_list);
1098 case SSL_CTRL_SET_MAX_CERT_LIST:
1099 l=s->max_cert_list;
1100 s->max_cert_list=larg;
1101 return(l);
1102 case SSL_CTRL_SET_MTU:
Adam Langley95c29f32014-06-20 12:00:00 -07001103 if (larg < (long)dtls1_min_mtu())
1104 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001105 if (SSL_IS_DTLS(s))
1106 {
1107 s->d1->mtu = larg;
1108 return larg;
1109 }
1110 return 0;
1111 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1112 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1113 return 0;
1114 s->max_send_fragment = larg;
1115 return 1;
1116 case SSL_CTRL_GET_RI_SUPPORT:
1117 if (s->s3)
1118 return s->s3->send_connection_binding;
1119 else return 0;
1120 case SSL_CTRL_CERT_FLAGS:
1121 return(s->cert->cert_flags|=larg);
1122 case SSL_CTRL_CLEAR_CERT_FLAGS:
1123 return(s->cert->cert_flags &=~larg);
1124
1125 case SSL_CTRL_GET_RAW_CIPHERLIST:
1126 if (parg)
1127 {
1128 if (s->cert->ciphers_raw == NULL)
1129 return 0;
1130 *(unsigned char **)parg = s->cert->ciphers_raw;
1131 return (int)s->cert->ciphers_rawlen;
1132 }
1133 else
David Benjamin39482a12014-07-20 13:30:15 -04001134 {
1135 /* Passing a NULL |parg| returns the size of a single
1136 * cipher suite value. */
1137 return 2;
1138 }
Adam Langley95c29f32014-06-20 12:00:00 -07001139 default:
1140 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1141 }
1142 }
1143
1144long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1145 {
1146 switch(cmd)
1147 {
1148 case SSL_CTRL_SET_MSG_CALLBACK:
1149 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1150 return 1;
1151
1152 default:
1153 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1154 }
1155 }
1156
1157LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1158 {
1159 return ctx->sessions;
1160 }
1161
1162long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1163 {
1164 long l;
1165
1166 switch (cmd)
1167 {
1168 case SSL_CTRL_GET_READ_AHEAD:
1169 return(ctx->read_ahead);
1170 case SSL_CTRL_SET_READ_AHEAD:
1171 l=ctx->read_ahead;
1172 ctx->read_ahead=larg;
1173 return(l);
1174
1175 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1176 ctx->msg_callback_arg = parg;
1177 return 1;
1178
1179 case SSL_CTRL_GET_MAX_CERT_LIST:
1180 return(ctx->max_cert_list);
1181 case SSL_CTRL_SET_MAX_CERT_LIST:
1182 l=ctx->max_cert_list;
1183 ctx->max_cert_list=larg;
1184 return(l);
1185
1186 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1187 l=ctx->session_cache_size;
1188 ctx->session_cache_size=larg;
1189 return(l);
1190 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1191 return(ctx->session_cache_size);
1192 case SSL_CTRL_SET_SESS_CACHE_MODE:
1193 l=ctx->session_cache_mode;
1194 ctx->session_cache_mode=larg;
1195 return(l);
1196 case SSL_CTRL_GET_SESS_CACHE_MODE:
1197 return(ctx->session_cache_mode);
1198
1199 case SSL_CTRL_SESS_NUMBER:
1200 return(lh_SSL_SESSION_num_items(ctx->sessions));
1201 case SSL_CTRL_SESS_CONNECT:
1202 return(ctx->stats.sess_connect);
1203 case SSL_CTRL_SESS_CONNECT_GOOD:
1204 return(ctx->stats.sess_connect_good);
1205 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1206 return(ctx->stats.sess_connect_renegotiate);
1207 case SSL_CTRL_SESS_ACCEPT:
1208 return(ctx->stats.sess_accept);
1209 case SSL_CTRL_SESS_ACCEPT_GOOD:
1210 return(ctx->stats.sess_accept_good);
1211 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1212 return(ctx->stats.sess_accept_renegotiate);
1213 case SSL_CTRL_SESS_HIT:
1214 return(ctx->stats.sess_hit);
1215 case SSL_CTRL_SESS_CB_HIT:
1216 return(ctx->stats.sess_cb_hit);
1217 case SSL_CTRL_SESS_MISSES:
1218 return(ctx->stats.sess_miss);
1219 case SSL_CTRL_SESS_TIMEOUTS:
1220 return(ctx->stats.sess_timeout);
1221 case SSL_CTRL_SESS_CACHE_FULL:
1222 return(ctx->stats.sess_cache_full);
1223 case SSL_CTRL_OPTIONS:
1224 return(ctx->options|=larg);
1225 case SSL_CTRL_CLEAR_OPTIONS:
1226 return(ctx->options&=~larg);
1227 case SSL_CTRL_MODE:
1228 return(ctx->mode|=larg);
1229 case SSL_CTRL_CLEAR_MODE:
1230 return(ctx->mode&=~larg);
1231 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1232 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1233 return 0;
1234 ctx->max_send_fragment = larg;
1235 return 1;
1236 case SSL_CTRL_CERT_FLAGS:
1237 return(ctx->cert->cert_flags|=larg);
1238 case SSL_CTRL_CLEAR_CERT_FLAGS:
1239 return(ctx->cert->cert_flags &=~larg);
1240 default:
1241 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1242 }
1243 }
1244
1245long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1246 {
1247 switch(cmd)
1248 {
1249 case SSL_CTRL_SET_MSG_CALLBACK:
1250 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1251 return 1;
1252
1253 default:
1254 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1255 }
1256 }
1257
1258int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1259 {
1260 long l;
1261 const SSL_CIPHER *a = in_a;
1262 const SSL_CIPHER *b = in_b;
1263 const long a_id = a->id;
1264 const long b_id = b->id;
1265
1266 l = a_id - b_id;
1267 if (l == 0L)
1268 return(0);
1269 else
1270 return((l > 0)?1:-1);
1271 }
1272
1273int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1274 {
1275 long l;
1276 const long a_id = (*ap)->id;
1277 const long b_id = (*bp)->id;
1278
1279 l = a_id - b_id;
1280 if (l == 0)
1281 return(0);
1282 else
1283 return((l > 0)?1:-1);
1284 }
1285
1286/** return a STACK of the ciphers available for the SSL and in order of
1287 * preference */
1288STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1289 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001290 if (s == NULL)
1291 return NULL;
1292
1293 if (s->cipher_list != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001294 {
Adam Langley858a88d2014-06-20 12:00:00 -07001295 return(s->cipher_list->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -07001296 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001297
1298 if (s->version >= TLS1_1_VERSION)
1299 {
1300 if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001301 return s->ctx->cipher_list_tls11->ciphers;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001302 }
1303
1304 if ((s->ctx != NULL) &&
1305 (s->ctx->cipher_list != NULL))
1306 {
Adam Langley858a88d2014-06-20 12:00:00 -07001307 return(s->ctx->cipher_list->ciphers);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001308 }
1309
Adam Langley95c29f32014-06-20 12:00:00 -07001310 return(NULL);
1311 }
1312
1313/** return a STACK of the ciphers available for the SSL and in order of
1314 * algorithm id */
1315STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1316 {
1317 if (s != NULL)
1318 {
1319 if (s->cipher_list_by_id != NULL)
1320 {
1321 return(s->cipher_list_by_id);
1322 }
1323 else if ((s->ctx != NULL) &&
1324 (s->ctx->cipher_list_by_id != NULL))
1325 {
1326 return(s->ctx->cipher_list_by_id);
1327 }
1328 }
1329 return(NULL);
1330 }
1331
1332/** The old interface to get the same thing as SSL_get_ciphers() */
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001333const char *SSL_get_cipher_list(const SSL *s, int n)
Adam Langley95c29f32014-06-20 12:00:00 -07001334 {
David Benjamin6f260012014-08-15 13:49:12 -04001335 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001336 STACK_OF(SSL_CIPHER) *sk;
1337
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001338 if (s == NULL)
1339 return NULL;
1340 sk = SSL_get_ciphers(s);
1341 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk))
1342 return NULL;
1343 c = sk_SSL_CIPHER_value(sk, n);
1344 if (c == NULL)
1345 return NULL;
1346 return c->name;
Adam Langley95c29f32014-06-20 12:00:00 -07001347 }
1348
1349/** specify the ciphers to be used by default by the SSL_CTX */
1350int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1351 {
1352 STACK_OF(SSL_CIPHER) *sk;
1353
1354 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1355 &ctx->cipher_list_by_id,str, ctx->cert);
1356 /* ssl_create_cipher_list may return an empty stack if it
1357 * was unable to find a cipher matching the given rule string
1358 * (for example if the rule string specifies a cipher which
1359 * has been disabled). This is not an error as far as
1360 * ssl_create_cipher_list is concerned, and hence
1361 * ctx->cipher_list and ctx->cipher_list_by_id has been
1362 * updated. */
1363 if (sk == NULL)
1364 return 0;
1365 else if (sk_SSL_CIPHER_num(sk) == 0)
1366 {
1367 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1368 return 0;
1369 }
1370 return 1;
1371 }
1372
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001373int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str)
1374 {
1375 STACK_OF(SSL_CIPHER) *sk;
1376
1377 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert);
1378 if (sk == NULL)
1379 return 0;
1380 else if (sk_SSL_CIPHER_num(sk) == 0)
1381 {
1382 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH);
1383 return 0;
1384 }
1385 return 1;
1386 }
1387
Adam Langley95c29f32014-06-20 12:00:00 -07001388/** specify the ciphers to be used by the SSL */
1389int SSL_set_cipher_list(SSL *s,const char *str)
1390 {
1391 STACK_OF(SSL_CIPHER) *sk;
1392
1393 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1394 &s->cipher_list_by_id,str, s->cert);
1395 /* see comment in SSL_CTX_set_cipher_list */
1396 if (sk == NULL)
1397 return 0;
1398 else if (sk_SSL_CIPHER_num(sk) == 0)
1399 {
1400 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1401 return 0;
1402 }
1403 return 1;
1404 }
1405
David Benjamin5491e3f2014-09-29 19:33:09 -04001406int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p)
Adam Langley95c29f32014-06-20 12:00:00 -07001407 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001408 size_t i;
David Benjamin6f260012014-08-15 13:49:12 -04001409 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001410 CERT *ct = s->cert;
David Benjamin5491e3f2014-09-29 19:33:09 -04001411 uint8_t *q;
Adam Langley95c29f32014-06-20 12:00:00 -07001412 /* Set disabled masks for this session */
1413 ssl_set_client_disabled(s);
1414
1415 if (sk == NULL) return(0);
1416 q=p;
1417
1418 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1419 {
1420 c=sk_SSL_CIPHER_value(sk,i);
1421 /* Skip disabled ciphers */
1422 if (c->algorithm_ssl & ct->mask_ssl ||
1423 c->algorithm_mkey & ct->mask_k ||
1424 c->algorithm_auth & ct->mask_a)
1425 continue;
David Benjamin39482a12014-07-20 13:30:15 -04001426 s2n(ssl3_get_cipher_value(c), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001427 }
David Benjamin5491e3f2014-09-29 19:33:09 -04001428 /* If all ciphers were disabled, return the error to the caller. */
1429 if (p == q)
Adam Langley95c29f32014-06-20 12:00:00 -07001430 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001431 return 0;
1432 }
1433
1434 /* Add SCSVs. */
1435 if (!s->renegotiate)
1436 {
1437 s2n(SSL3_CK_SCSV & 0xffff, p);
1438 }
1439 if (s->fallback_scsv)
1440 {
1441 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001442 }
1443
1444 return(p-q);
1445 }
1446
David Benjamin9f2c0d72014-10-21 22:00:19 -04001447STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001448 {
David Benjamin39482a12014-07-20 13:30:15 -04001449 CBS cipher_suites = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001450 const SSL_CIPHER *c;
1451 STACK_OF(SSL_CIPHER) *sk;
David Benjamin39482a12014-07-20 13:30:15 -04001452
Adam Langley95c29f32014-06-20 12:00:00 -07001453 if (s->s3)
1454 s->s3->send_connection_binding = 0;
1455
David Benjamin39482a12014-07-20 13:30:15 -04001456 if (CBS_len(&cipher_suites) % 2 != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001457 {
1458 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001459 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001460 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001461
1462 sk = sk_SSL_CIPHER_new_null();
1463 if (sk == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001464 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001465 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1466 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001467 }
1468
David Benjamin39482a12014-07-20 13:30:15 -04001469 if (!CBS_stow(&cipher_suites,
1470 &s->cert->ciphers_raw, &s->cert->ciphers_rawlen))
Adam Langley95c29f32014-06-20 12:00:00 -07001471 {
1472 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1473 goto err;
1474 }
Adam Langley95c29f32014-06-20 12:00:00 -07001475
David Benjamin39482a12014-07-20 13:30:15 -04001476 while (CBS_len(&cipher_suites) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001477 {
David Benjamin39482a12014-07-20 13:30:15 -04001478 uint16_t cipher_suite;
1479
1480 if (!CBS_get_u16(&cipher_suites, &cipher_suite))
1481 {
1482 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1483 goto err;
1484 }
1485
David Benjamin9f2c0d72014-10-21 22:00:19 -04001486 /* Check for SCSV. */
David Benjamin39482a12014-07-20 13:30:15 -04001487 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff))
Adam Langley95c29f32014-06-20 12:00:00 -07001488 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001489 /* SCSV is fatal if renegotiating. */
Adam Langley95c29f32014-06-20 12:00:00 -07001490 if (s->renegotiate)
1491 {
1492 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1493 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1494 goto err;
1495 }
1496 s->s3->send_connection_binding = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001497 continue;
1498 }
1499
David Benjamin9f2c0d72014-10-21 22:00:19 -04001500 /* Check for FALLBACK_SCSV. */
1501 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff))
Adam Langley29707792014-06-20 12:00:00 -07001502 {
David Benjaminceb6f282014-12-07 23:56:19 -05001503 uint16_t max_version = ssl3_get_max_server_version(s);
1504 if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
1505 : (uint16_t)s->version < max_version)
David Benjamin9f2c0d72014-10-21 22:00:19 -04001506 {
1507 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1508 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1509 goto err;
1510 }
1511 continue;
Adam Langley29707792014-06-20 12:00:00 -07001512 }
1513
David Benjamin39482a12014-07-20 13:30:15 -04001514 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -07001515 if (c != NULL)
1516 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001517 if (!sk_SSL_CIPHER_push(sk, c))
Adam Langley95c29f32014-06-20 12:00:00 -07001518 {
1519 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1520 goto err;
1521 }
1522 }
1523 }
1524
David Benjamin9f2c0d72014-10-21 22:00:19 -04001525 return sk;
1526
Adam Langley95c29f32014-06-20 12:00:00 -07001527err:
David Benjamin9f2c0d72014-10-21 22:00:19 -04001528 if (sk != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001529 sk_SSL_CIPHER_free(sk);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001530 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001531 }
1532
1533
Adam Langley95c29f32014-06-20 12:00:00 -07001534/** return a servername extension value if provided in Client Hello, or NULL.
1535 * So far, only host_name types are defined (RFC 3546).
1536 */
1537
1538const char *SSL_get_servername(const SSL *s, const int type)
1539 {
1540 if (type != TLSEXT_NAMETYPE_host_name)
1541 return NULL;
1542
1543 return s->session && !s->tlsext_hostname ?
1544 s->session->tlsext_hostname :
1545 s->tlsext_hostname;
1546 }
1547
1548int SSL_get_servername_type(const SSL *s)
1549 {
1550 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1551 return TLSEXT_NAMETYPE_host_name;
1552 return -1;
1553 }
1554
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001555void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx)
1556 {
1557 ctx->signed_cert_timestamps_enabled = 1;
1558 }
1559
1560int SSL_enable_signed_cert_timestamps(SSL *ssl)
1561 {
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001562 ssl->signed_cert_timestamps_enabled = 1;
1563 return 1;
1564 }
1565
David Benjamin6c7aed02014-08-27 16:42:38 -04001566void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx)
1567 {
1568 ctx->ocsp_stapling_enabled = 1;
1569 }
1570
1571int SSL_enable_ocsp_stapling(SSL *ssl)
1572 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001573 ssl->ocsp_stapling_enabled = 1;
1574 return 1;
1575 }
1576
David Benjamin52d699f2014-11-25 01:35:18 -05001577void 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 +02001578 {
Adam Langley3cb50e02014-08-26 14:00:31 -07001579 SSL_SESSION *session = ssl->session;
1580
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001581 *out_len = 0;
1582 *out = NULL;
1583 if (ssl->server)
1584 return;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001585 if (!session || !session->tlsext_signed_cert_timestamp_list)
1586 return;
1587 *out = session->tlsext_signed_cert_timestamp_list;
1588 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1589 }
1590
David Benjamin52d699f2014-11-25 01:35:18 -05001591void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out, size_t *out_len)
David Benjamin6c7aed02014-08-27 16:42:38 -04001592 {
1593 SSL_SESSION *session = ssl->session;
1594
1595 *out_len = 0;
1596 *out = NULL;
1597 if (ssl->server)
1598 return;
1599 if (!session || !session->ocsp_response)
1600 return;
1601 *out = session->ocsp_response;
1602 *out_len = session->ocsp_response_length;
1603 }
1604
Adam Langley95c29f32014-06-20 12:00:00 -07001605/* SSL_select_next_proto implements the standard protocol selection. It is
1606 * expected that this function is called from the callback set by
1607 * SSL_CTX_set_next_proto_select_cb.
1608 *
1609 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1610 * strings. The length byte itself is not included in the length. A byte
1611 * string of length 0 is invalid. No byte string may be truncated.
1612 *
1613 * The current, but experimental algorithm for selecting the protocol is:
1614 *
1615 * 1) If the server doesn't support NPN then this is indicated to the
1616 * callback. In this case, the client application has to abort the connection
1617 * or have a default application level protocol.
1618 *
1619 * 2) If the server supports NPN, but advertises an empty list then the
1620 * client selects the first protcol in its list, but indicates via the
1621 * API that this fallback case was enacted.
1622 *
1623 * 3) Otherwise, the client finds the first protocol in the server's list
1624 * that it supports and selects this protocol. This is because it's
1625 * assumed that the server has better information about which protocol
1626 * a client should use.
1627 *
1628 * 4) If the client doesn't support any of the server's advertised
1629 * protocols, then this is treated the same as case 2.
1630 *
1631 * It returns either
1632 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1633 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1634 */
1635int 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)
1636 {
1637 unsigned int i, j;
1638 const unsigned char *result;
1639 int status = OPENSSL_NPN_UNSUPPORTED;
1640
1641 /* For each protocol in server preference order, see if we support it. */
1642 for (i = 0; i < server_len; )
1643 {
1644 for (j = 0; j < client_len; )
1645 {
1646 if (server[i] == client[j] &&
1647 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1648 {
1649 /* We found a match */
1650 result = &server[i];
1651 status = OPENSSL_NPN_NEGOTIATED;
1652 goto found;
1653 }
1654 j += client[j];
1655 j++;
1656 }
1657 i += server[i];
1658 i++;
1659 }
1660
1661 /* There's no overlap between our protocols and the server's list. */
1662 result = client;
1663 status = OPENSSL_NPN_NO_OVERLAP;
1664
1665 found:
1666 *out = (unsigned char *) result + 1;
1667 *outlen = result[0];
1668 return status;
1669 }
1670
Adam Langley95c29f32014-06-20 12:00:00 -07001671/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1672 * requested protocol for this connection and returns 0. If the client didn't
1673 * request any protocol, then *data is set to NULL.
1674 *
1675 * Note that the client can request any protocol it chooses. The value returned
1676 * from this function need not be a member of the list of supported protocols
1677 * provided by the callback.
1678 */
David Benjaminced551f2014-07-11 23:16:16 -04001679void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, unsigned *len)
Adam Langley95c29f32014-06-20 12:00:00 -07001680 {
1681 *data = s->next_proto_negotiated;
1682 if (!*data) {
1683 *len = 0;
1684 } else {
1685 *len = s->next_proto_negotiated_len;
1686 }
1687}
1688
1689/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1690 * TLS server needs a list of supported protocols for Next Protocol
1691 * Negotiation. The returned list must be in wire format. The list is returned
1692 * by setting |out| to point to it and |outlen| to its length. This memory will
1693 * not be modified, but one should assume that the SSL* keeps a reference to
1694 * it.
1695 *
1696 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1697 * such extension will be included in the ServerHello. */
1698void 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)
1699 {
1700 ctx->next_protos_advertised_cb = cb;
1701 ctx->next_protos_advertised_cb_arg = arg;
1702 }
1703
1704/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1705 * client needs to select a protocol from the server's provided list. |out|
1706 * must be set to point to the selected protocol (which may be within |in|).
1707 * The length of the protocol name must be written into |outlen|. The server's
1708 * advertised protocols are provided in |in| and |inlen|. The callback can
1709 * assume that |in| is syntactically valid.
1710 *
1711 * The client must select a protocol. It is fatal to the connection if this
1712 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1713 */
1714void 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)
1715 {
1716 ctx->next_proto_select_cb = cb;
1717 ctx->next_proto_select_cb_arg = arg;
1718 }
Adam Langley95c29f32014-06-20 12:00:00 -07001719
Adam Langley95c29f32014-06-20 12:00:00 -07001720/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1721 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1722 * length-prefixed strings).
1723 *
1724 * Returns 0 on success. */
1725int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1726 unsigned protos_len)
1727 {
1728 if (ctx->alpn_client_proto_list)
1729 OPENSSL_free(ctx->alpn_client_proto_list);
1730
David Benjamin072c9532014-07-26 11:44:25 -04001731 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001732 if (!ctx->alpn_client_proto_list)
1733 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001734 ctx->alpn_client_proto_list_len = protos_len;
1735
1736 return 0;
1737 }
1738
1739/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1740 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1741 * length-prefixed strings).
1742 *
1743 * Returns 0 on success. */
1744int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1745 unsigned protos_len)
1746 {
1747 if (ssl->alpn_client_proto_list)
1748 OPENSSL_free(ssl->alpn_client_proto_list);
1749
David Benjamin072c9532014-07-26 11:44:25 -04001750 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001751 if (!ssl->alpn_client_proto_list)
1752 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001753 ssl->alpn_client_proto_list_len = protos_len;
1754
1755 return 0;
1756 }
1757
1758/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1759 * during ClientHello processing in order to select an ALPN protocol from the
1760 * client's list of offered protocols. */
1761void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1762 int (*cb) (SSL *ssl,
1763 const unsigned char **out,
1764 unsigned char *outlen,
1765 const unsigned char *in,
1766 unsigned int inlen,
1767 void *arg),
1768 void *arg)
1769 {
1770 ctx->alpn_select_cb = cb;
1771 ctx->alpn_select_cb_arg = arg;
1772 }
1773
1774/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1775 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1776 * including the leading length-prefix byte). If the server didn't respond with
1777 * a negotiated protocol then |*len| will be zero. */
1778void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1779 unsigned *len)
1780 {
1781 *data = NULL;
1782 if (ssl->s3)
1783 *data = ssl->s3->alpn_selected;
1784 if (*data == NULL)
1785 *len = 0;
1786 else
1787 *len = ssl->s3->alpn_selected_len;
1788 }
Adam Langley95c29f32014-06-20 12:00:00 -07001789
1790int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1791 const char *label, size_t llen, const unsigned char *p, size_t plen,
1792 int use_context)
1793 {
1794 if (s->version < TLS1_VERSION)
1795 return -1;
1796
David Benjamine99e9122014-12-11 01:46:01 -05001797 return s->enc_method->export_keying_material(s, out, olen, label,
Adam Langley95c29f32014-06-20 12:00:00 -07001798 llen, p, plen,
1799 use_context);
1800 }
1801
1802static uint32_t ssl_session_hash(const SSL_SESSION *a)
1803 {
1804 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1805 ((uint32_t) a->session_id[1] << 8) ||
1806 ((uint32_t) a->session_id[2] << 16) ||
1807 ((uint32_t) a->session_id[3] << 24);
1808
1809 return hash;
1810 }
1811
1812/* NB: If this function (or indeed the hash function which uses a sort of
1813 * coarser function than this one) is changed, ensure
1814 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1815 * able to construct an SSL_SESSION that will collide with any existing session
1816 * with a matching session ID. */
1817static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1818 {
1819 if (a->ssl_version != b->ssl_version)
1820 return(1);
1821 if (a->session_id_length != b->session_id_length)
1822 return(1);
1823 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1824 }
1825
1826SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1827 {
1828 SSL_CTX *ret=NULL;
1829
1830 if (meth == NULL)
1831 {
1832 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1833 return(NULL);
1834 }
1835
Adam Langley95c29f32014-06-20 12:00:00 -07001836 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1837 {
1838 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1839 goto err;
1840 }
1841 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1842 if (ret == NULL)
1843 goto err;
1844
1845 memset(ret,0,sizeof(SSL_CTX));
1846
1847 ret->method=meth;
1848
1849 ret->cert_store=NULL;
1850 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1851 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1852 ret->session_cache_head=NULL;
1853 ret->session_cache_tail=NULL;
1854
1855 /* We take the system default */
David Benjaminf4501342014-08-14 17:24:37 -04001856 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001857
1858 ret->new_session_cb=0;
1859 ret->remove_session_cb=0;
1860 ret->get_session_cb=0;
1861 ret->generate_session_id=0;
1862
1863 memset((char *)&ret->stats,0,sizeof(ret->stats));
1864
1865 ret->references=1;
1866 ret->quiet_shutdown=0;
1867
Adam Langley95c29f32014-06-20 12:00:00 -07001868 ret->info_callback=NULL;
1869
1870 ret->app_verify_callback=0;
1871 ret->app_verify_arg=NULL;
1872
1873 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1874 ret->read_ahead=0;
1875 ret->msg_callback=0;
1876 ret->msg_callback_arg=NULL;
1877 ret->verify_mode=SSL_VERIFY_NONE;
1878#if 0
1879 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1880#endif
1881 ret->sid_ctx_length=0;
1882 ret->default_verify_callback=NULL;
1883 if ((ret->cert=ssl_cert_new()) == NULL)
1884 goto err;
1885
1886 ret->default_passwd_callback=0;
1887 ret->default_passwd_callback_userdata=NULL;
1888 ret->client_cert_cb=0;
1889 ret->app_gen_cookie_cb=0;
1890 ret->app_verify_cookie_cb=0;
1891
1892 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1893 if (ret->sessions == NULL) goto err;
1894 ret->cert_store=X509_STORE_new();
1895 if (ret->cert_store == NULL) goto err;
1896
1897 ssl_create_cipher_list(ret->method,
1898 &ret->cipher_list,&ret->cipher_list_by_id,
David Benjamin5e4f6e92014-11-17 03:23:24 -05001899 SSL_DEFAULT_CIPHER_LIST, ret->cert);
Adam Langley95c29f32014-06-20 12:00:00 -07001900 if (ret->cipher_list == NULL
Adam Langley858a88d2014-06-20 12:00:00 -07001901 || sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001902 {
1903 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1904 goto err2;
1905 }
1906
1907 ret->param = X509_VERIFY_PARAM_new();
1908 if (!ret->param)
1909 goto err;
1910
Adam Langley95c29f32014-06-20 12:00:00 -07001911 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1912 goto err;
1913
1914 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1915
1916 ret->extra_certs=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001917
1918 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1919
Adam Langley95c29f32014-06-20 12:00:00 -07001920 ret->tlsext_servername_callback = 0;
1921 ret->tlsext_servername_arg = NULL;
1922 /* Setup RFC4507 ticket keys */
1923 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
1924 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
1925 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
1926 ret->options |= SSL_OP_NO_TICKET;
1927
1928 ret->tlsext_status_cb = 0;
1929 ret->tlsext_status_arg = NULL;
1930
Adam Langley95c29f32014-06-20 12:00:00 -07001931 ret->next_protos_advertised_cb = 0;
1932 ret->next_proto_select_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001933 ret->psk_identity_hint=NULL;
1934 ret->psk_client_callback=NULL;
1935 ret->psk_server_callback=NULL;
David Benjamin422d3a42014-08-20 11:09:03 -04001936
Adam Langley95c29f32014-06-20 12:00:00 -07001937 /* Default is to connect to non-RI servers. When RI is more widely
1938 * deployed might change this.
1939 */
1940 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1941
1942 return(ret);
1943err:
1944 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
1945err2:
1946 if (ret != NULL) SSL_CTX_free(ret);
1947 return(NULL);
1948 }
1949
Adam Langley95c29f32014-06-20 12:00:00 -07001950
1951void SSL_CTX_free(SSL_CTX *a)
1952 {
1953 int i;
1954
1955 if (a == NULL) return;
1956
1957 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
1958#ifdef REF_PRINT
1959 REF_PRINT("SSL_CTX",a);
1960#endif
1961 if (i > 0) return;
1962#ifdef REF_CHECK
1963 if (i < 0)
1964 {
1965 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
1966 abort(); /* ok */
1967 }
1968#endif
1969
1970 if (a->param)
1971 X509_VERIFY_PARAM_free(a->param);
1972
1973 /*
1974 * Free internal session cache. However: the remove_cb() may reference
1975 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
1976 * after the sessions were flushed.
1977 * As the ex_data handling routines might also touch the session cache,
1978 * the most secure solution seems to be: empty (flush) the cache, then
1979 * free ex_data, then finally free the cache.
1980 * (See ticket [openssl.org #212].)
1981 */
1982 if (a->sessions != NULL)
1983 SSL_CTX_flush_sessions(a,0);
1984
1985 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
1986
1987 if (a->sessions != NULL)
1988 lh_SSL_SESSION_free(a->sessions);
1989
1990 if (a->cert_store != NULL)
1991 X509_STORE_free(a->cert_store);
1992 if (a->cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001993 ssl_cipher_preference_list_free(a->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07001994 if (a->cipher_list_by_id != NULL)
1995 sk_SSL_CIPHER_free(a->cipher_list_by_id);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001996 if (a->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001997 ssl_cipher_preference_list_free(a->cipher_list_tls11);
Adam Langley95c29f32014-06-20 12:00:00 -07001998 if (a->cert != NULL)
1999 ssl_cert_free(a->cert);
2000 if (a->client_CA != NULL)
2001 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2002 if (a->extra_certs != NULL)
2003 sk_X509_pop_free(a->extra_certs,X509_free);
Adam Langley95c29f32014-06-20 12:00:00 -07002004
2005 if (a->srtp_profiles)
2006 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2007
Adam Langley95c29f32014-06-20 12:00:00 -07002008 if (a->psk_identity_hint)
2009 OPENSSL_free(a->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07002010
Adam Langley95c29f32014-06-20 12:00:00 -07002011 if (a->tlsext_ecpointformatlist)
2012 OPENSSL_free(a->tlsext_ecpointformatlist);
2013 if (a->tlsext_ellipticcurvelist)
2014 OPENSSL_free(a->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -07002015 if (a->alpn_client_proto_list != NULL)
2016 OPENSSL_free(a->alpn_client_proto_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002017
Adam Langley1258b6a2014-06-20 12:00:00 -07002018 if (a->tlsext_channel_id_private)
2019 EVP_PKEY_free(a->tlsext_channel_id_private);
Adam Langley1258b6a2014-06-20 12:00:00 -07002020
David Benjamin859ec3c2014-09-02 16:29:36 -04002021 if (a->keylog_bio)
2022 BIO_free(a->keylog_bio);
2023
Adam Langley95c29f32014-06-20 12:00:00 -07002024 OPENSSL_free(a);
2025 }
2026
2027void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2028 {
2029 ctx->default_passwd_callback=cb;
2030 }
2031
2032void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2033 {
2034 ctx->default_passwd_callback_userdata=u;
2035 }
2036
2037void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2038 {
2039 ctx->app_verify_callback=cb;
2040 ctx->app_verify_arg=arg;
2041 }
2042
2043void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2044 {
2045 ctx->verify_mode=mode;
2046 ctx->default_verify_callback=cb;
2047 }
2048
2049void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2050 {
2051 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2052 }
2053
2054void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2055 {
2056 ssl_cert_set_cert_cb(c->cert, cb, arg);
2057 }
2058
2059void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2060 {
2061 ssl_cert_set_cert_cb(s->cert, cb, arg);
2062 }
2063
David Benjamin033e5f42014-11-13 18:47:41 -05002064static int ssl_has_key(SSL *s, size_t idx)
2065 {
2066 CERT_PKEY *cpk = &s->cert->pkeys[idx];
2067 return cpk->x509 && cpk->privatekey;
2068 }
2069
David Benjaminf31e6812014-11-13 18:05:55 -05002070void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
2071 unsigned long *out_mask_a)
Adam Langley95c29f32014-06-20 12:00:00 -07002072 {
David Benjaminf31e6812014-11-13 18:05:55 -05002073 CERT *c = s->cert;
David Benjaminf31e6812014-11-13 18:05:55 -05002074 int rsa_enc, rsa_sign, dh_tmp;
2075 unsigned long mask_k, mask_a;
David Benjamin060d9d22014-07-15 00:54:26 -04002076 int have_ecc_cert, ecdsa_ok;
David Benjamin0da0e182014-08-19 16:20:28 -04002077 int have_ecdh_tmp;
David Benjaminf31e6812014-11-13 18:05:55 -05002078 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002079
David Benjaminf31e6812014-11-13 18:05:55 -05002080 if (c == NULL)
2081 {
2082 /* TODO(davidben): Is this codepath possible? */
2083 *out_mask_k = 0;
2084 *out_mask_a = 0;
2085 return;
2086 }
Adam Langley95c29f32014-06-20 12:00:00 -07002087
David Benjaminf31e6812014-11-13 18:05:55 -05002088 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002089
David Benjaminf31e6812014-11-13 18:05:55 -05002090 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
David Benjamin033e5f42014-11-13 18:47:41 -05002091 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2092 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2093 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
David Benjaminf31e6812014-11-13 18:05:55 -05002094 mask_k = 0;
2095 mask_a = 0;
2096
David Benjamin77a942b2014-07-15 01:22:50 -04002097 if (rsa_enc)
David Benjaminf31e6812014-11-13 18:05:55 -05002098 mask_k |= SSL_kRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002099
Adam Langley95c29f32014-06-20 12:00:00 -07002100 if (dh_tmp)
David Benjaminf31e6812014-11-13 18:05:55 -05002101 mask_k |= SSL_kEDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002102
Adam Langley95c29f32014-06-20 12:00:00 -07002103 if (rsa_enc || rsa_sign)
2104 {
David Benjaminf31e6812014-11-13 18:05:55 -05002105 mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002106 }
2107
David Benjaminf31e6812014-11-13 18:05:55 -05002108 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002109
David Benjamin0da0e182014-08-19 16:20:28 -04002110 /* An ECC certificate may be usable for ECDSA cipher suites depending on
David Benjamin033e5f42014-11-13 18:47:41 -05002111 * the key usage extension and on the client's curve preferences. */
Adam Langley95c29f32014-06-20 12:00:00 -07002112 if (have_ecc_cert)
2113 {
David Benjamin033e5f42014-11-13 18:47:41 -05002114 x = c->pkeys[SSL_PKEY_ECC].x509;
Adam Langley95c29f32014-06-20 12:00:00 -07002115 /* This call populates extension flags (ex_flags) */
2116 X509_check_purpose(x, -1, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07002117 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2118 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
David Benjamin033e5f42014-11-13 18:47:41 -05002119 if (!tls1_check_ec_cert(s, x))
Adam Langley95c29f32014-06-20 12:00:00 -07002120 ecdsa_ok = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002121 if (ecdsa_ok)
2122 {
David Benjaminf31e6812014-11-13 18:05:55 -05002123 mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002124 }
Adam Langley95c29f32014-06-20 12:00:00 -07002125 }
Adam Langley95c29f32014-06-20 12:00:00 -07002126
David Benjaminf31e6812014-11-13 18:05:55 -05002127 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2128 * key, check it. */
2129 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002130 {
David Benjaminf31e6812014-11-13 18:05:55 -05002131 mask_k |= SSL_kEECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002132 }
Adam Langley95c29f32014-06-20 12:00:00 -07002133
David Benjaminf31e6812014-11-13 18:05:55 -05002134 /* PSK requires a server callback. */
2135 if (s->psk_server_callback != NULL)
2136 {
2137 mask_k |= SSL_kPSK;
2138 mask_a |= SSL_aPSK;
2139 }
Adam Langley95c29f32014-06-20 12:00:00 -07002140
David Benjaminf31e6812014-11-13 18:05:55 -05002141 *out_mask_k = mask_k;
2142 *out_mask_a = mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002143 }
2144
2145/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2146#define ku_reject(x, usage) \
2147 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2148
Adam Langley95c29f32014-06-20 12:00:00 -07002149
2150int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2151 {
David Benjamin0da0e182014-08-19 16:20:28 -04002152 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002153 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2154 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2155
Adam Langley95c29f32014-06-20 12:00:00 -07002156 alg_a = cs->algorithm_auth;
2157
Adam Langley95c29f32014-06-20 12:00:00 -07002158 /* This call populates the ex_flags field correctly */
2159 X509_check_purpose(x, -1, 0);
2160 if ((x->sig_alg) && (x->sig_alg->algorithm))
2161 {
2162 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2163 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2164 }
Adam Langley95c29f32014-06-20 12:00:00 -07002165 if (alg_a & SSL_aECDSA)
2166 {
2167 /* key usage, if present, must allow signing */
2168 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2169 {
2170 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2171 return 0;
2172 }
2173 }
2174
2175 return 1; /* all checks are ok */
2176 }
2177
Adam Langley95c29f32014-06-20 12:00:00 -07002178
2179static int ssl_get_server_cert_index(const SSL *s)
2180 {
2181 int idx;
2182 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2183 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2184 idx = SSL_PKEY_RSA_SIGN;
2185 if (idx == -1)
2186 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2187 return idx;
2188 }
2189
2190CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2191 {
David Benjaminf31e6812014-11-13 18:05:55 -05002192 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002193
2194 /* This may or may not be an error. */
2195 if (i < 0)
2196 return NULL;
2197
2198 /* May be NULL. */
David Benjaminf31e6812014-11-13 18:05:55 -05002199 return &s->cert->pkeys[i];
Adam Langley95c29f32014-06-20 12:00:00 -07002200 }
2201
David Benjaminec2f27d2014-11-13 19:17:25 -05002202EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher)
Adam Langley95c29f32014-06-20 12:00:00 -07002203 {
2204 unsigned long alg_a;
2205 CERT *c;
2206 int idx = -1;
2207
2208 alg_a = cipher->algorithm_auth;
2209 c=s->cert;
2210
David Benjaminef2116d2014-08-19 20:21:56 -04002211 if (alg_a & SSL_aRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002212 {
2213 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2214 idx = SSL_PKEY_RSA_SIGN;
2215 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2216 idx = SSL_PKEY_RSA_ENC;
2217 }
2218 else if ((alg_a & SSL_aECDSA) &&
2219 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2220 idx = SSL_PKEY_ECC;
2221 if (idx == -1)
2222 {
2223 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2224 return(NULL);
2225 }
Adam Langley95c29f32014-06-20 12:00:00 -07002226 return c->pkeys[idx].privatekey;
2227 }
2228
Adam Langley95c29f32014-06-20 12:00:00 -07002229void ssl_update_cache(SSL *s,int mode)
2230 {
2231 int i;
2232
2233 /* If the session_id_length is 0, we are not supposed to cache it,
2234 * and it would be rather hard to do anyway :-) */
2235 if (s->session->session_id_length == 0) return;
2236
David Benjamin041b58a2014-08-27 15:25:21 -04002237 i=s->initial_ctx->session_cache_mode;
Adam Langley95c29f32014-06-20 12:00:00 -07002238 if ((i & mode) && (!s->hit)
2239 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
David Benjamin041b58a2014-08-27 15:25:21 -04002240 || SSL_CTX_add_session(s->initial_ctx,s->session))
2241 && (s->initial_ctx->new_session_cb != NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07002242 {
2243 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
David Benjamin041b58a2014-08-27 15:25:21 -04002244 if (!s->initial_ctx->new_session_cb(s,s->session))
Adam Langley95c29f32014-06-20 12:00:00 -07002245 SSL_SESSION_free(s->session);
2246 }
2247
2248 /* auto flush every 255 connections */
2249 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2250 ((i & mode) == mode))
2251 {
2252 if ( (((mode & SSL_SESS_CACHE_CLIENT)
David Benjamin041b58a2014-08-27 15:25:21 -04002253 ?s->initial_ctx->stats.sess_connect_good
2254 :s->initial_ctx->stats.sess_accept_good) & 0xff) == 0xff)
Adam Langley95c29f32014-06-20 12:00:00 -07002255 {
David Benjamin041b58a2014-08-27 15:25:21 -04002256 SSL_CTX_flush_sessions(s->initial_ctx,(unsigned long)time(NULL));
Adam Langley95c29f32014-06-20 12:00:00 -07002257 }
2258 }
2259 }
2260
Adam Langley95c29f32014-06-20 12:00:00 -07002261int SSL_get_error(const SSL *s,int i)
2262 {
2263 int reason;
2264 unsigned long l;
2265 BIO *bio;
2266
2267 if (i > 0) return(SSL_ERROR_NONE);
2268
2269 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2270 * etc, where we do encode the error */
2271 if ((l=ERR_peek_error()) != 0)
2272 {
2273 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2274 return(SSL_ERROR_SYSCALL);
2275 else
2276 return(SSL_ERROR_SSL);
2277 }
2278
Adam Langleyb2ce0582014-06-20 12:00:00 -07002279 if ((i < 0) && SSL_want_session(s))
2280 return(SSL_ERROR_PENDING_SESSION);
2281
Adam Langleydc9b1412014-06-20 12:00:00 -07002282 if ((i < 0) && SSL_want_certificate(s))
2283 return(SSL_ERROR_PENDING_CERTIFICATE);
2284
Adam Langley95c29f32014-06-20 12:00:00 -07002285 if ((i < 0) && SSL_want_read(s))
2286 {
2287 bio=SSL_get_rbio(s);
2288 if (BIO_should_read(bio))
2289 return(SSL_ERROR_WANT_READ);
2290 else if (BIO_should_write(bio))
2291 /* This one doesn't make too much sense ... We never try
2292 * to write to the rbio, and an application program where
2293 * rbio and wbio are separate couldn't even know what it
2294 * should wait for.
2295 * However if we ever set s->rwstate incorrectly
2296 * (so that we have SSL_want_read(s) instead of
2297 * SSL_want_write(s)) and rbio and wbio *are* the same,
2298 * this test works around that bug; so it might be safer
2299 * to keep it. */
2300 return(SSL_ERROR_WANT_WRITE);
2301 else if (BIO_should_io_special(bio))
2302 {
2303 reason=BIO_get_retry_reason(bio);
2304 if (reason == BIO_RR_CONNECT)
2305 return(SSL_ERROR_WANT_CONNECT);
2306 else if (reason == BIO_RR_ACCEPT)
2307 return(SSL_ERROR_WANT_ACCEPT);
2308 else
2309 return(SSL_ERROR_SYSCALL); /* unknown */
2310 }
2311 }
2312
2313 if ((i < 0) && SSL_want_write(s))
2314 {
2315 bio=SSL_get_wbio(s);
2316 if (BIO_should_write(bio))
2317 return(SSL_ERROR_WANT_WRITE);
2318 else if (BIO_should_read(bio))
2319 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2320 return(SSL_ERROR_WANT_READ);
2321 else if (BIO_should_io_special(bio))
2322 {
2323 reason=BIO_get_retry_reason(bio);
2324 if (reason == BIO_RR_CONNECT)
2325 return(SSL_ERROR_WANT_CONNECT);
2326 else if (reason == BIO_RR_ACCEPT)
2327 return(SSL_ERROR_WANT_ACCEPT);
2328 else
2329 return(SSL_ERROR_SYSCALL);
2330 }
2331 }
2332 if ((i < 0) && SSL_want_x509_lookup(s))
2333 {
2334 return(SSL_ERROR_WANT_X509_LOOKUP);
2335 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002336 if ((i < 0) && SSL_want_channel_id_lookup(s))
2337 {
2338 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2339 }
Adam Langley95c29f32014-06-20 12:00:00 -07002340
2341 if (i == 0)
2342 {
David Benjamin5e4f6e92014-11-17 03:23:24 -05002343 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2344 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
Adam Langley95c29f32014-06-20 12:00:00 -07002345 return(SSL_ERROR_ZERO_RETURN);
Adam Langley95c29f32014-06-20 12:00:00 -07002346 }
2347 return(SSL_ERROR_SYSCALL);
2348 }
2349
2350int SSL_do_handshake(SSL *s)
2351 {
2352 int ret=1;
2353
2354 if (s->handshake_func == NULL)
2355 {
2356 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2357 return(-1);
2358 }
2359
2360 s->method->ssl_renegotiate_check(s);
2361
David Benjamin7e237462014-12-11 18:13:28 -05002362 if (SSL_in_init(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002363 {
2364 ret=s->handshake_func(s);
2365 }
2366 return(ret);
2367 }
2368
Adam Langley95c29f32014-06-20 12:00:00 -07002369void SSL_set_accept_state(SSL *s)
2370 {
2371 s->server=1;
2372 s->shutdown=0;
2373 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2374 s->handshake_func=s->method->ssl_accept;
2375 /* clear the current cipher */
2376 ssl_clear_cipher_ctx(s);
2377 ssl_clear_hash_ctx(&s->read_hash);
2378 ssl_clear_hash_ctx(&s->write_hash);
2379 }
2380
2381void SSL_set_connect_state(SSL *s)
2382 {
2383 s->server=0;
2384 s->shutdown=0;
2385 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2386 s->handshake_func=s->method->ssl_connect;
2387 /* clear the current cipher */
2388 ssl_clear_cipher_ctx(s);
2389 ssl_clear_hash_ctx(&s->read_hash);
2390 ssl_clear_hash_ctx(&s->write_hash);
2391 }
2392
2393int ssl_undefined_function(SSL *s)
2394 {
2395 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2396 return(0);
2397 }
2398
2399int ssl_undefined_void_function(void)
2400 {
2401 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2402 return(0);
2403 }
2404
2405int ssl_undefined_const_function(const SSL *s)
2406 {
2407 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2408 return(0);
2409 }
2410
Adam Langley0f4746e2014-08-13 12:26:32 -07002411static const char *ssl_get_version(int version)
Adam Langley95c29f32014-06-20 12:00:00 -07002412 {
Adam Langley0f4746e2014-08-13 12:26:32 -07002413 if (version == TLS1_2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002414 return("TLSv1.2");
Adam Langley0f4746e2014-08-13 12:26:32 -07002415 else if (version == TLS1_1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002416 return("TLSv1.1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002417 else if (version == TLS1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002418 return("TLSv1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002419 else if (version == SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002420 return("SSLv3");
Adam Langley95c29f32014-06-20 12:00:00 -07002421 else
2422 return("unknown");
2423 }
2424
Adam Langley0f4746e2014-08-13 12:26:32 -07002425const char *SSL_get_version(const SSL *s)
2426 {
2427 return ssl_get_version(s->version);
2428 }
2429
2430const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2431 {
2432 return ssl_get_version(sess->ssl_version);
2433 }
2434
Adam Langley95c29f32014-06-20 12:00:00 -07002435void ssl_clear_cipher_ctx(SSL *s)
2436 {
2437 if (s->enc_read_ctx != NULL)
2438 {
2439 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2440 OPENSSL_free(s->enc_read_ctx);
2441 s->enc_read_ctx=NULL;
2442 }
2443 if (s->enc_write_ctx != NULL)
2444 {
2445 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2446 OPENSSL_free(s->enc_write_ctx);
2447 s->enc_write_ctx=NULL;
2448 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002449 if (s->aead_read_ctx != NULL)
2450 {
2451 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2452 OPENSSL_free(s->aead_read_ctx);
2453 s->aead_read_ctx = NULL;
2454 }
2455 if (s->aead_write_ctx != NULL)
2456 {
2457 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2458 OPENSSL_free(s->aead_write_ctx);
2459 s->aead_write_ctx = NULL;
2460 }
Adam Langley95c29f32014-06-20 12:00:00 -07002461 }
2462
2463X509 *SSL_get_certificate(const SSL *s)
2464 {
2465 if (s->cert != NULL)
2466 return(s->cert->key->x509);
2467 else
2468 return(NULL);
2469 }
2470
2471EVP_PKEY *SSL_get_privatekey(const SSL *s)
2472 {
2473 if (s->cert != NULL)
2474 return(s->cert->key->privatekey);
2475 else
2476 return(NULL);
2477 }
2478
2479X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2480 {
2481 if (ctx->cert != NULL)
2482 return ctx->cert->key->x509;
2483 else
2484 return NULL;
2485 }
2486
2487EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2488 {
2489 if (ctx->cert != NULL)
2490 return ctx->cert->key->privatekey;
2491 else
2492 return NULL ;
2493 }
2494
2495const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2496 {
2497 if ((s->session != NULL) && (s->session->cipher != NULL))
2498 return(s->session->cipher);
2499 return(NULL);
2500 }
2501const void *SSL_get_current_compression(SSL *s)
2502 {
2503 return NULL;
2504 }
2505const void *SSL_get_current_expansion(SSL *s)
2506 {
2507 return NULL;
2508 }
2509
2510int ssl_init_wbio_buffer(SSL *s,int push)
2511 {
2512 BIO *bbio;
2513
2514 if (s->bbio == NULL)
2515 {
2516 bbio=BIO_new(BIO_f_buffer());
2517 if (bbio == NULL) return(0);
2518 s->bbio=bbio;
2519 }
2520 else
2521 {
2522 bbio=s->bbio;
2523 if (s->bbio == s->wbio)
2524 s->wbio=BIO_pop(s->wbio);
2525 }
2526 (void)BIO_reset(bbio);
2527/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2528 if (!BIO_set_read_buffer_size(bbio,1))
2529 {
2530 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2531 return(0);
2532 }
2533 if (push)
2534 {
2535 if (s->wbio != bbio)
2536 s->wbio=BIO_push(bbio,s->wbio);
2537 }
2538 else
2539 {
2540 if (s->wbio == bbio)
2541 s->wbio=BIO_pop(bbio);
2542 }
2543 return(1);
2544 }
2545
2546void ssl_free_wbio_buffer(SSL *s)
2547 {
2548 if (s->bbio == NULL) return;
2549
2550 if (s->bbio == s->wbio)
2551 {
2552 /* remove buffering */
2553 s->wbio=BIO_pop(s->wbio);
2554#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2555 assert(s->wbio != NULL);
2556#endif
2557 }
2558 BIO_free(s->bbio);
2559 s->bbio=NULL;
2560 }
2561
2562void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2563 {
2564 ctx->quiet_shutdown=mode;
2565 }
2566
2567int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2568 {
2569 return(ctx->quiet_shutdown);
2570 }
2571
2572void SSL_set_quiet_shutdown(SSL *s,int mode)
2573 {
2574 s->quiet_shutdown=mode;
2575 }
2576
2577int SSL_get_quiet_shutdown(const SSL *s)
2578 {
2579 return(s->quiet_shutdown);
2580 }
2581
2582void SSL_set_shutdown(SSL *s,int mode)
2583 {
2584 s->shutdown=mode;
2585 }
2586
2587int SSL_get_shutdown(const SSL *s)
2588 {
2589 return(s->shutdown);
2590 }
2591
2592int SSL_version(const SSL *s)
2593 {
2594 return(s->version);
2595 }
2596
2597SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2598 {
2599 return(ssl->ctx);
2600 }
2601
2602SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2603 {
2604 if (ssl->ctx == ctx)
2605 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002606 if (ctx == NULL)
2607 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002608 if (ssl->cert != NULL)
2609 ssl_cert_free(ssl->cert);
2610 ssl->cert = ssl_cert_dup(ctx->cert);
2611 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2612 if (ssl->ctx != NULL)
2613 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2614 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07002615
2616 ssl->sid_ctx_length = ctx->sid_ctx_length;
2617 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2618 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2619
Adam Langley95c29f32014-06-20 12:00:00 -07002620 return(ssl->ctx);
2621 }
2622
2623#ifndef OPENSSL_NO_STDIO
2624int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2625 {
2626 return(X509_STORE_set_default_paths(ctx->cert_store));
2627 }
2628
2629int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2630 const char *CApath)
2631 {
2632 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2633 }
2634#endif
2635
2636void SSL_set_info_callback(SSL *ssl,
2637 void (*cb)(const SSL *ssl,int type,int val))
2638 {
2639 ssl->info_callback=cb;
2640 }
2641
2642/* One compiler (Diab DCC) doesn't like argument names in returned
2643 function pointer. */
2644void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2645 {
2646 return ssl->info_callback;
2647 }
2648
2649int SSL_state(const SSL *ssl)
2650 {
2651 return(ssl->state);
2652 }
2653
2654void SSL_set_state(SSL *ssl, int state)
2655 {
2656 ssl->state = state;
2657 }
2658
2659void SSL_set_verify_result(SSL *ssl,long arg)
2660 {
2661 ssl->verify_result=arg;
2662 }
2663
2664long SSL_get_verify_result(const SSL *ssl)
2665 {
2666 return(ssl->verify_result);
2667 }
2668
2669int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2670 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2671 {
2672 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2673 new_func, dup_func, free_func);
2674 }
2675
2676int SSL_set_ex_data(SSL *s,int idx,void *arg)
2677 {
2678 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2679 }
2680
2681void *SSL_get_ex_data(const SSL *s,int idx)
2682 {
2683 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2684 }
2685
2686int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2687 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2688 {
2689 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2690 new_func, dup_func, free_func);
2691 }
2692
2693int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2694 {
2695 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2696 }
2697
2698void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2699 {
2700 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2701 }
2702
2703int ssl_ok(SSL *s)
2704 {
2705 return(1);
2706 }
2707
2708X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2709 {
2710 return(ctx->cert_store);
2711 }
2712
2713void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2714 {
2715 if (ctx->cert_store != NULL)
2716 X509_STORE_free(ctx->cert_store);
2717 ctx->cert_store=store;
2718 }
2719
2720int SSL_want(const SSL *s)
2721 {
2722 return(s->rwstate);
2723 }
2724
2725/*!
2726 * \brief Set the callback for generating temporary RSA keys.
2727 * \param ctx the SSL context.
2728 * \param cb the callback
2729 */
2730
Adam Langley95c29f32014-06-20 12:00:00 -07002731void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2732 int is_export,
2733 int keylength))
2734 {
2735 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2736 }
2737
2738void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2739 int is_export,
2740 int keylength))
2741 {
2742 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2743 }
Adam Langley95c29f32014-06-20 12:00:00 -07002744
2745#ifdef DOXYGEN
2746/*!
2747 * \brief The RSA temporary key callback function.
2748 * \param ssl the SSL session.
2749 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2750 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2751 * of the required key in bits.
2752 * \return the temporary RSA key.
2753 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2754 */
2755
2756RSA *cb(SSL *ssl,int is_export,int keylength)
2757 {}
2758#endif
2759
2760/*!
2761 * \brief Set the callback for generating temporary DH keys.
2762 * \param ctx the SSL context.
2763 * \param dh the callback
2764 */
2765
Adam Langley95c29f32014-06-20 12:00:00 -07002766void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2767 int keylength))
2768 {
2769 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2770 }
2771
2772void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2773 int keylength))
2774 {
2775 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2776 }
Adam Langley95c29f32014-06-20 12:00:00 -07002777
Adam Langley95c29f32014-06-20 12:00:00 -07002778void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2779 int keylength))
2780 {
2781 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2782 }
2783
2784void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2785 int keylength))
2786 {
2787 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2788 }
Adam Langley95c29f32014-06-20 12:00:00 -07002789
Adam Langley95c29f32014-06-20 12:00:00 -07002790int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2791 {
2792 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2793 {
2794 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2795 return 0;
2796 }
2797 if (ctx->psk_identity_hint != NULL)
2798 OPENSSL_free(ctx->psk_identity_hint);
2799 if (identity_hint != NULL)
2800 {
2801 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2802 if (ctx->psk_identity_hint == NULL)
2803 return 0;
2804 }
2805 else
2806 ctx->psk_identity_hint = NULL;
2807 return 1;
2808 }
2809
2810int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
2811 {
2812 if (s == NULL)
2813 return 0;
2814
Adam Langley95c29f32014-06-20 12:00:00 -07002815 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2816 {
2817 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2818 return 0;
2819 }
Adam Langley0289c732014-06-20 12:00:00 -07002820
David Benjamin688d8df2014-11-02 23:06:42 -05002821 /* Clear currently configured hint, if any. */
Adam Langley0289c732014-06-20 12:00:00 -07002822 if (s->psk_identity_hint != NULL)
2823 {
2824 OPENSSL_free(s->psk_identity_hint);
2825 s->psk_identity_hint = NULL;
2826 }
Adam Langley0289c732014-06-20 12:00:00 -07002827
Adam Langley95c29f32014-06-20 12:00:00 -07002828 if (identity_hint != NULL)
2829 {
David Benjamin688d8df2014-11-02 23:06:42 -05002830 s->psk_identity_hint = BUF_strdup(identity_hint);
2831 if (s->psk_identity_hint == NULL)
2832 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002833 }
Adam Langley95c29f32014-06-20 12:00:00 -07002834 return 1;
2835 }
2836
2837const char *SSL_get_psk_identity_hint(const SSL *s)
2838 {
Adam Langley0289c732014-06-20 12:00:00 -07002839 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002840 return NULL;
David Benjamin688d8df2014-11-02 23:06:42 -05002841 return s->psk_identity_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07002842 }
2843
2844const char *SSL_get_psk_identity(const SSL *s)
2845 {
2846 if (s == NULL || s->session == NULL)
2847 return NULL;
2848 return(s->session->psk_identity);
2849 }
2850
2851void SSL_set_psk_client_callback(SSL *s,
2852 unsigned int (*cb)(SSL *ssl, const char *hint,
2853 char *identity, unsigned int max_identity_len, unsigned char *psk,
2854 unsigned int max_psk_len))
2855 {
2856 s->psk_client_callback = cb;
2857 }
2858
2859void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
2860 unsigned int (*cb)(SSL *ssl, const char *hint,
2861 char *identity, unsigned int max_identity_len, unsigned char *psk,
2862 unsigned int max_psk_len))
2863 {
2864 ctx->psk_client_callback = cb;
2865 }
2866
2867void SSL_set_psk_server_callback(SSL *s,
2868 unsigned int (*cb)(SSL *ssl, const char *identity,
2869 unsigned char *psk, unsigned int max_psk_len))
2870 {
2871 s->psk_server_callback = cb;
2872 }
2873
2874void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
2875 unsigned int (*cb)(SSL *ssl, const char *identity,
2876 unsigned char *psk, unsigned int max_psk_len))
2877 {
2878 ctx->psk_server_callback = cb;
2879 }
Adam Langley95c29f32014-06-20 12:00:00 -07002880
2881void 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))
2882 {
2883 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2884 }
2885void 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))
2886 {
2887 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2888 }
2889
David Benjamin859ec3c2014-09-02 16:29:36 -04002890void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio)
2891 {
2892 if (ctx->keylog_bio != NULL)
2893 BIO_free(ctx->keylog_bio);
2894 ctx->keylog_bio = keylog_bio;
2895 }
2896
2897static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len)
2898 {
2899 static const char hextable[] = "0123456789abcdef";
2900 uint8_t *out;
2901 size_t i;
2902
2903 if (!CBB_add_space(cbb, &out, in_len * 2))
2904 {
2905 return 0;
2906 }
2907
2908 for (i = 0; i < in_len; i++)
2909 {
2910 *(out++) = (uint8_t)hextable[in[i] >> 4];
2911 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2912 }
2913 return 1;
2914 }
2915
2916int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
2917 const uint8_t *encrypted_premaster, size_t encrypted_premaster_len,
2918 const uint8_t *premaster, size_t premaster_len)
2919 {
2920 BIO *bio = ctx->keylog_bio;
2921 CBB cbb;
2922 uint8_t *out;
2923 size_t out_len;
2924 int ret;
2925
2926 if (bio == NULL)
2927 {
2928 return 1;
2929 }
2930
2931 if (encrypted_premaster_len < 8)
2932 {
2933 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange, ERR_R_INTERNAL_ERROR);
2934 return 0;
2935 }
2936
2937 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len*2 + 1))
2938 {
2939 return 0;
2940 }
2941 if (!CBB_add_bytes(&cbb, (const uint8_t*)"RSA ", 4) ||
2942 /* Only the first 8 bytes of the encrypted premaster secret are
2943 * logged. */
2944 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2945 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
2946 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2947 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
2948 !CBB_finish(&cbb, &out, &out_len))
2949 {
2950 CBB_cleanup(&cbb);
2951 return 0;
2952 }
2953
2954 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2955 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2956 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2957
2958 OPENSSL_free(out);
2959 return ret;
2960 }
2961
2962int ssl_ctx_log_master_secret(SSL_CTX *ctx,
2963 const uint8_t *client_random, size_t client_random_len,
2964 const uint8_t *master, size_t master_len)
2965 {
2966 BIO *bio = ctx->keylog_bio;
2967 CBB cbb;
2968 uint8_t *out;
2969 size_t out_len;
2970 int ret;
2971
2972 if (bio == NULL)
2973 {
2974 return 1;
2975 }
2976
2977 if (client_random_len != 32)
2978 {
2979 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
2980 return 0;
2981 }
2982
2983 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len*2 + 1))
2984 {
2985 return 0;
2986 }
2987 if (!CBB_add_bytes(&cbb, (const uint8_t*)"CLIENT_RANDOM ", 14) ||
2988 !cbb_add_hex(&cbb, client_random, 32) ||
2989 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
2990 !cbb_add_hex(&cbb, master, master_len) ||
2991 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
2992 !CBB_finish(&cbb, &out, &out_len))
2993 {
2994 CBB_cleanup(&cbb);
2995 return 0;
2996 }
2997
2998 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2999 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3000 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3001
3002 OPENSSL_free(out);
3003 return ret;
3004 }
3005
Adam Langleyadb739e2014-06-20 12:00:00 -07003006int SSL_cutthrough_complete(const SSL *s)
3007 {
3008 return (!s->server && /* cutthrough only applies to clients */
3009 !s->hit && /* full-handshake */
3010 s->version >= SSL3_VERSION &&
3011 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3012 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3013 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3014 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3015 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
David Benjamin7e3305e2014-07-28 14:52:32 -04003016 s->state == SSL3_ST_CR_CHANGE ||
Adam Langleyadb739e2014-06-20 12:00:00 -07003017 s->state == SSL3_ST_CR_FINISHED_A));
3018 }
3019
Adam Langley95f22882014-06-20 12:00:00 -07003020void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3021 size_t* ssl_session_size)
3022{
3023 *ssl_size = sizeof(SSL);
3024 *ssl_ctx_size = sizeof(SSL_CTX);
3025 *ssl_session_size = sizeof(SSL_SESSION);
3026}
3027
Adam Langleyadb739e2014-06-20 12:00:00 -07003028int ssl3_can_cutthrough(const SSL *s)
3029 {
3030 const SSL_CIPHER *c;
3031
3032 /* require a strong enough cipher */
3033 if (SSL_get_cipher_bits(s, NULL) < 128)
3034 return 0;
3035
3036 /* require ALPN or NPN extension */
David Benjamin457112e2014-08-27 14:35:09 -04003037 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen)
Adam Langleyadb739e2014-06-20 12:00:00 -07003038 {
3039 return 0;
3040 }
3041
3042 /* require a forward-secret cipher */
3043 c = SSL_get_current_cipher(s);
3044 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3045 c->algorithm_mkey != SSL_kEECDH))
3046 {
3047 return 0;
3048 }
3049
3050 return 1;
3051 }
3052
David Benjaminceb6f282014-12-07 23:56:19 -05003053const SSL_METHOD *ssl3_get_method(uint16_t version)
Adam Langley29707792014-06-20 12:00:00 -07003054 {
David Benjaminceb6f282014-12-07 23:56:19 -05003055 switch (version)
3056 {
3057 case SSL3_VERSION:
3058 return SSLv3_method();
3059 case TLS1_VERSION:
3060 return TLSv1_method();
3061 case TLS1_1_VERSION:
3062 return TLSv1_1_method();
3063 case TLS1_2_VERSION:
3064 return TLSv1_2_method();
3065 case DTLS1_VERSION:
3066 return DTLSv1_method();
3067 case DTLS1_2_VERSION:
3068 return DTLSv1_2_method();
3069 default:
3070 return NULL;
3071 }
3072 }
3073
David Benjamine99e9122014-12-11 01:46:01 -05003074const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version)
3075 {
3076 switch (version)
3077 {
3078 case SSL3_VERSION:
3079 return &SSLv3_enc_data;
3080 case TLS1_VERSION:
3081 return &TLSv1_enc_data;
3082 case TLS1_1_VERSION:
3083 return &TLSv1_1_enc_data;
3084 case TLS1_2_VERSION:
3085 return &TLSv1_2_enc_data;
3086 case DTLS1_VERSION:
3087 return &DTLSv1_enc_data;
3088 case DTLS1_2_VERSION:
3089 return &DTLSv1_2_enc_data;
3090 default:
3091 return NULL;
3092 }
3093 }
3094
David Benjaminceb6f282014-12-07 23:56:19 -05003095uint16_t ssl3_get_max_server_version(const SSL *s)
3096 {
3097 if (SSL_IS_DTLS(s))
3098 {
3099 if (!(s->options & SSL_OP_NO_DTLSv1_2))
3100 return DTLS1_2_VERSION;
3101 if (!(s->options & SSL_OP_NO_DTLSv1))
3102 return DTLS1_VERSION;
3103 return 0;
3104 }
Adam Langley29707792014-06-20 12:00:00 -07003105
3106 if (!(s->options & SSL_OP_NO_TLSv1_2))
3107 return TLS1_2_VERSION;
3108 if (!(s->options & SSL_OP_NO_TLSv1_1))
3109 return TLS1_1_VERSION;
3110 if (!(s->options & SSL_OP_NO_TLSv1))
3111 return TLS1_VERSION;
3112 if (!(s->options & SSL_OP_NO_SSLv3))
3113 return SSL3_VERSION;
Adam Langley29707792014-06-20 12:00:00 -07003114 return 0;
3115 }
3116
David Benjaminceb6f282014-12-07 23:56:19 -05003117uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version)
3118 {
3119 if (SSL_IS_DTLS(s))
3120 {
3121 if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2))
3122 return DTLS1_2_VERSION;
3123 if (client_version <= DTLS1_VERSION && !(s->options & SSL_OP_NO_DTLSv1))
3124 return DTLS1_VERSION;
3125 return 0;
3126 }
3127 else
3128 {
3129 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2))
3130 return TLS1_2_VERSION;
3131 if (client_version >= TLS1_1_VERSION && !(s->options & SSL_OP_NO_TLSv1_1))
3132 return TLS1_1_VERSION;
3133 if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1))
3134 return TLS1_VERSION;
3135 if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3))
3136 return SSL3_VERSION;
3137 return 0;
3138 }
3139 }
3140
3141uint16_t ssl3_get_max_client_version(SSL *s)
3142 {
3143 unsigned long options = s->options;
3144 uint16_t version = 0;
3145
3146 /* OpenSSL's API for controlling versions entails blacklisting
3147 * individual protocols. This has two problems. First, on the client,
3148 * the protocol can only express a contiguous range of versions. Second,
3149 * a library consumer trying to set a maximum version cannot disable
3150 * protocol versions that get added in a future version of the library.
3151 *
3152 * To account for both of these, OpenSSL interprets the client-side
3153 * bitmask as a min/max range by picking the lowest contiguous non-empty
3154 * range of enabled protocols. Note that this means it is impossible to
3155 * set a maximum version of TLS 1.2 in a future-proof way.
3156 *
3157 * By this scheme, the maximum version is the lowest version V such that
3158 * V is enabled and V+1 is disabled or unimplemented.
3159 *
3160 * TODO(davidben): Deprecate this API in favor of more sensible
3161 * min_version/max_version settings. */
3162 if (SSL_IS_DTLS(s))
3163 {
3164 if (!(options & SSL_OP_NO_DTLSv1_2))
3165 version = DTLS1_2_VERSION;
3166 if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2))
3167 version = DTLS1_VERSION;
3168 }
3169 else
3170 {
3171 if (!(options & SSL_OP_NO_TLSv1_2))
3172 version = TLS1_2_VERSION;
3173 if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2))
3174 version = TLS1_1_VERSION;
3175 if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1))
3176 version = TLS1_VERSION;
3177 if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1))
3178 version = SSL3_VERSION;
3179 }
3180
3181 return version;
3182 }
3183
3184int ssl3_is_version_enabled(SSL *s, uint16_t version)
3185 {
3186 if (SSL_IS_DTLS(s))
3187 {
3188 switch (version)
3189 {
3190 case DTLS1_VERSION:
3191 return !(s->options & SSL_OP_NO_DTLSv1);
3192 case DTLS1_2_VERSION:
3193 return !(s->options & SSL_OP_NO_DTLSv1_2);
3194 default:
3195 return 0;
3196 }
3197 }
3198 else
3199 {
3200 switch (version)
3201 {
3202 case SSL3_VERSION:
3203 return !(s->options & SSL_OP_NO_SSLv3);
3204 case TLS1_VERSION:
3205 return !(s->options & SSL_OP_NO_TLSv1);
3206 case TLS1_1_VERSION:
3207 return !(s->options & SSL_OP_NO_TLSv1_1);
3208 case TLS1_2_VERSION:
3209 return !(s->options & SSL_OP_NO_TLSv1_2);
3210 default:
3211 return 0;
3212 }
3213 }
3214 }
3215
Adam Langley95c29f32014-06-20 12:00:00 -07003216/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3217 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3218 * any. If EVP_MD pointer is passed, initializes ctx with this md
3219 * Returns newly allocated ctx;
3220 */
3221
3222EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3223{
3224 ssl_clear_hash_ctx(hash);
3225 *hash = EVP_MD_CTX_create();
David Benjaminae3e4872014-11-18 21:52:26 -05003226 if (md != NULL && *hash != NULL &&
3227 !EVP_DigestInit_ex(*hash, md, NULL))
Adam Langley69a01602014-11-17 17:26:55 -08003228 {
David Benjaminae3e4872014-11-18 21:52:26 -05003229 EVP_MD_CTX_destroy(*hash);
3230 *hash = NULL;
Adam Langley69a01602014-11-17 17:26:55 -08003231 }
Adam Langley95c29f32014-06-20 12:00:00 -07003232 return *hash;
3233}
3234void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3235{
3236
3237 if (*hash) EVP_MD_CTX_destroy(*hash);
3238 *hash=NULL;
3239}
3240
Adam Langley95c29f32014-06-20 12:00:00 -07003241int SSL_cache_hit(SSL *s)
3242 {
3243 return s->hit;
3244 }
3245
3246int SSL_is_server(SSL *s)
3247 {
3248 return s->server;
3249 }
Feng Lu41aa3252014-11-21 22:47:56 -08003250
3251void SSL_enable_fastradio_padding(SSL *s, char on_off)
3252 {
3253 s->fastradio_padding = on_off;
3254 }