blob: 80a8c2b613290f5e03ad28ddfa34b574a686c04a [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
141#include <stdio.h>
142#include <assert.h>
143
David Benjamin39482a12014-07-20 13:30:15 -0400144#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700145#include <openssl/dh.h>
146#include <openssl/engine.h>
147#include <openssl/lhash.h>
148#include <openssl/mem.h>
149#include <openssl/obj.h>
150#include <openssl/rand.h>
151#include <openssl/x509v3.h>
152
153#include "ssl_locl.h"
154
155SSL3_ENC_METHOD ssl3_undef_enc_method={
156 /* evil casts, but these functions are only called if there's a library bug */
157 (int (*)(SSL *,int))ssl_undefined_function,
158 (int (*)(SSL *, unsigned char *, int))ssl_undefined_function,
159 ssl_undefined_function,
160 (int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function,
161 (int (*)(SSL*, int))ssl_undefined_function,
162 (int (*)(SSL *, const char*, int, unsigned char *))ssl_undefined_function,
163 0, /* finish_mac_length */
164 (int (*)(SSL *, int, unsigned char *))ssl_undefined_function,
165 NULL, /* client_finished_label */
166 0, /* client_finished_label_len */
167 NULL, /* server_finished_label */
168 0, /* server_finished_label_len */
169 (int (*)(int))ssl_undefined_function,
170 (int (*)(SSL *, unsigned char *, size_t, const char *,
171 size_t, const unsigned char *, size_t,
172 int use_context)) ssl_undefined_function,
173 };
174
David Benjamine1136082014-09-20 12:28:58 -0400175/* Some error codes are special. Ensure the make_errors.go script
176 * never regresses this. */
177OPENSSL_COMPILE_ASSERT(
178 SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
179 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
180 ssl_alert_reason_code_mismatch);
181
Adam Langley95c29f32014-06-20 12:00:00 -0700182int SSL_clear(SSL *s)
183 {
184
185 if (s->method == NULL)
186 {
187 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
188 return(0);
189 }
190
191 if (ssl_clear_bad_session(s))
192 {
193 SSL_SESSION_free(s->session);
194 s->session=NULL;
195 }
196
Adam Langley95c29f32014-06-20 12:00:00 -0700197 s->hit=0;
198 s->shutdown=0;
199
200#if 0 /* Disabled since version 1.10 of this file (early return not
201 * needed because SSL_clear is not called when doing renegotiation) */
202 /* This is set if we are doing dynamic renegotiation so keep
203 * the old cipher. It is sort of a SSL_clear_lite :-) */
204 if (s->renegotiate) return(1);
205#else
206 if (s->renegotiate)
207 {
208 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
209 return 0;
210 }
211#endif
212
Adam Langley95c29f32014-06-20 12:00:00 -0700213 s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
214
215 s->version=s->method->version;
216 s->client_version=s->version;
217 s->rwstate=SSL_NOTHING;
218 s->rstate=SSL_ST_READ_HEADER;
219#if 0
220 s->read_ahead=s->ctx->read_ahead;
221#endif
222
223 if (s->init_buf != NULL)
224 {
225 BUF_MEM_free(s->init_buf);
226 s->init_buf=NULL;
227 }
228
229 ssl_clear_cipher_ctx(s);
230 ssl_clear_hash_ctx(&s->read_hash);
231 ssl_clear_hash_ctx(&s->write_hash);
232
233 s->first_packet=0;
234
David Benjaminbb15e3d2014-11-29 16:03:41 -0500235 s->method->ssl_clear(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700236 return(1);
237 }
238
239/** Used to change an SSL_CTXs default SSL method type */
240int SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth)
241 {
242 STACK_OF(SSL_CIPHER) *sk;
243
244 ctx->method=meth;
245
Adam Langley858a88d2014-06-20 12:00:00 -0700246 sk=ssl_create_cipher_list(
247 ctx->method, &ctx->cipher_list, &ctx->cipher_list_by_id,
David Benjamin5e4f6e92014-11-17 03:23:24 -0500248 SSL_DEFAULT_CIPHER_LIST,
Adam Langley858a88d2014-06-20 12:00:00 -0700249 ctx->cert);
250
Adam Langley95c29f32014-06-20 12:00:00 -0700251 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
252 {
253 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_ssl_version, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
254 return(0);
255 }
256 return(1);
257 }
258
259SSL *SSL_new(SSL_CTX *ctx)
260 {
261 SSL *s;
262
263 if (ctx == NULL)
264 {
265 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
266 return(NULL);
267 }
268 if (ctx->method == NULL)
269 {
270 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
271 return(NULL);
272 }
273
274 s=(SSL *)OPENSSL_malloc(sizeof(SSL));
275 if (s == NULL) goto err;
276 memset(s,0,sizeof(SSL));
277
278 s->options=ctx->options;
279 s->mode=ctx->mode;
280 s->max_cert_list=ctx->max_cert_list;
281
282 if (ctx->cert != NULL)
283 {
284 /* Earlier library versions used to copy the pointer to
285 * the CERT, not its contents; only when setting new
286 * parameters for the per-SSL copy, ssl_cert_new would be
287 * called (and the direct reference to the per-SSL_CTX
288 * settings would be lost, but those still were indirectly
289 * accessed for various purposes, and for that reason they
290 * used to be known as s->ctx->default_cert).
291 * Now we don't look at the SSL_CTX's CERT after having
292 * duplicated it once. */
293
294 s->cert = ssl_cert_dup(ctx->cert);
295 if (s->cert == NULL)
296 goto err;
297 }
298 else
299 s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
300
301 s->read_ahead=ctx->read_ahead;
302 s->msg_callback=ctx->msg_callback;
303 s->msg_callback_arg=ctx->msg_callback_arg;
304 s->verify_mode=ctx->verify_mode;
305#if 0
306 s->verify_depth=ctx->verify_depth;
307#endif
308 s->sid_ctx_length=ctx->sid_ctx_length;
309 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
310 memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
311 s->verify_callback=ctx->default_verify_callback;
312 s->generate_session_id=ctx->generate_session_id;
313
314 s->param = X509_VERIFY_PARAM_new();
315 if (!s->param)
316 goto err;
317 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
318#if 0
319 s->purpose = ctx->purpose;
320 s->trust = ctx->trust;
321#endif
322 s->quiet_shutdown=ctx->quiet_shutdown;
323 s->max_send_fragment = ctx->max_send_fragment;
324
325 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
326 s->ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700327 s->tlsext_debug_cb = 0;
328 s->tlsext_debug_arg = NULL;
329 s->tlsext_ticket_expected = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700330 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
331 s->initial_ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700332 if (ctx->tlsext_ecpointformatlist)
333 {
334 s->tlsext_ecpointformatlist =
335 BUF_memdup(ctx->tlsext_ecpointformatlist,
336 ctx->tlsext_ecpointformatlist_length);
337 if (!s->tlsext_ecpointformatlist)
338 goto err;
339 s->tlsext_ecpointformatlist_length =
340 ctx->tlsext_ecpointformatlist_length;
341 }
342 if (ctx->tlsext_ellipticcurvelist)
343 {
344 s->tlsext_ellipticcurvelist =
345 BUF_memdup(ctx->tlsext_ellipticcurvelist,
David Benjamin072334d2014-07-13 16:24:27 -0400346 ctx->tlsext_ellipticcurvelist_length * 2);
Adam Langley95c29f32014-06-20 12:00:00 -0700347 if (!s->tlsext_ellipticcurvelist)
348 goto err;
349 s->tlsext_ellipticcurvelist_length =
350 ctx->tlsext_ellipticcurvelist_length;
351 }
Adam Langley95c29f32014-06-20 12:00:00 -0700352 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700353
354 if (s->ctx->alpn_client_proto_list)
355 {
David Benjamin072c9532014-07-26 11:44:25 -0400356 s->alpn_client_proto_list = BUF_memdup(
357 s->ctx->alpn_client_proto_list,
358 s->ctx->alpn_client_proto_list_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700359 if (s->alpn_client_proto_list == NULL)
360 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700361 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
362 }
Adam Langley95c29f32014-06-20 12:00:00 -0700363
364 s->verify_result=X509_V_OK;
365
366 s->method=ctx->method;
367
368 if (!s->method->ssl_new(s))
369 goto err;
370
371 s->references=1;
372 s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
373
374 SSL_clear(s);
375
376 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
377
Adam Langley0289c732014-06-20 12:00:00 -0700378 s->psk_identity_hint = NULL;
379 if (ctx->psk_identity_hint)
380 {
381 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
382 if (s->psk_identity_hint == NULL)
383 goto err;
384 }
Adam Langley95c29f32014-06-20 12:00:00 -0700385 s->psk_client_callback=ctx->psk_client_callback;
386 s->psk_server_callback=ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700387
David Benjaminff42cc12014-11-26 00:16:14 -0500388 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
389 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200390
Adam Langley95c29f32014-06-20 12:00:00 -0700391 return(s);
392err:
393 if (s != NULL)
394 {
395 if (s->cert != NULL)
396 ssl_cert_free(s->cert);
397 if (s->ctx != NULL)
398 SSL_CTX_free(s->ctx); /* decrement reference count */
399 OPENSSL_free(s);
400 }
401 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
402 return(NULL);
403 }
404
405int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
406 unsigned int sid_ctx_len)
407 {
408 if(sid_ctx_len > sizeof ctx->sid_ctx)
409 {
410 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
411 return 0;
412 }
413 ctx->sid_ctx_length=sid_ctx_len;
414 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
415
416 return 1;
417 }
418
419int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
420 unsigned int sid_ctx_len)
421 {
422 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
423 {
424 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
425 return 0;
426 }
427 ssl->sid_ctx_length=sid_ctx_len;
428 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
429
430 return 1;
431 }
432
433int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
434 {
435 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
436 ctx->generate_session_id = cb;
437 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
438 return 1;
439 }
440
441int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
442 {
443 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
444 ssl->generate_session_id = cb;
445 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
446 return 1;
447 }
448
449int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
450 unsigned int id_len)
451 {
452 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
453 * we can "construct" a session to give us the desired check - ie. to
454 * find if there's a session in the hash table that would conflict with
455 * any new session built out of this id/id_len and the ssl_version in
456 * use by this SSL. */
457 SSL_SESSION r, *p;
458
459 if(id_len > sizeof r.session_id)
460 return 0;
461
462 r.ssl_version = ssl->version;
463 r.session_id_length = id_len;
464 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700465
466 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
467 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
468 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
469 return (p != NULL);
470 }
471
472int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
473 {
474 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
475 }
476
477int SSL_set_purpose(SSL *s, int purpose)
478 {
479 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
480 }
481
482int SSL_CTX_set_trust(SSL_CTX *s, int trust)
483 {
484 return X509_VERIFY_PARAM_set_trust(s->param, trust);
485 }
486
487int SSL_set_trust(SSL *s, int trust)
488 {
489 return X509_VERIFY_PARAM_set_trust(s->param, trust);
490 }
491
492int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
493 {
494 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
495 }
496
497int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
498 {
499 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
500 }
501
Adam Langley858a88d2014-06-20 12:00:00 -0700502void ssl_cipher_preference_list_free(
503 struct ssl_cipher_preference_list_st *cipher_list)
504 {
505 sk_SSL_CIPHER_free(cipher_list->ciphers);
506 OPENSSL_free(cipher_list->in_group_flags);
507 OPENSSL_free(cipher_list);
508 }
509
510struct ssl_cipher_preference_list_st*
511ssl_cipher_preference_list_dup(
512 struct ssl_cipher_preference_list_st *cipher_list)
513 {
514 struct ssl_cipher_preference_list_st* ret = NULL;
515 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
516
517 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
518 if (!ret)
519 goto err;
520 ret->ciphers = NULL;
521 ret->in_group_flags = NULL;
522 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
523 if (!ret->ciphers)
524 goto err;
David Benjamin072c9532014-07-26 11:44:25 -0400525 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
Adam Langley858a88d2014-06-20 12:00:00 -0700526 if (!ret->in_group_flags)
527 goto err;
Adam Langley858a88d2014-06-20 12:00:00 -0700528 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
538struct ssl_cipher_preference_list_st*
539ssl_cipher_preference_list_from_ciphers(STACK_OF(SSL_CIPHER) *ciphers)
540 {
541 struct ssl_cipher_preference_list_st* ret = NULL;
542 size_t n = sk_SSL_CIPHER_num(ciphers);
543
544 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
545 if (!ret)
546 goto err;
547 ret->ciphers = NULL;
548 ret->in_group_flags = NULL;
549 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
550 if (!ret->ciphers)
551 goto err;
552 ret->in_group_flags = OPENSSL_malloc(n);
553 if (!ret->in_group_flags)
554 goto err;
555 memset(ret->in_group_flags, 0, n);
556 return ret;
557
558err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400559 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700560 sk_SSL_CIPHER_free(ret->ciphers);
561 if (ret)
562 OPENSSL_free(ret);
563 return NULL;
564 }
565
Adam Langley95c29f32014-06-20 12:00:00 -0700566X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
567 {
568 return ctx->param;
569 }
570
571X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
572 {
573 return ssl->param;
574 }
575
576void SSL_certs_clear(SSL *s)
577 {
578 ssl_cert_clear_certs(s->cert);
579 }
580
581void SSL_free(SSL *s)
582 {
583 int i;
584
585 if(s == NULL)
586 return;
587
588 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
589#ifdef REF_PRINT
590 REF_PRINT("SSL",s);
591#endif
592 if (i > 0) return;
593#ifdef REF_CHECK
594 if (i < 0)
595 {
596 fprintf(stderr,"SSL_free, bad reference count\n");
597 abort(); /* ok */
598 }
599#endif
600
601 if (s->param)
602 X509_VERIFY_PARAM_free(s->param);
603
604 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
605
606 if (s->bbio != NULL)
607 {
608 /* If the buffering BIO is in place, pop it off */
609 if (s->bbio == s->wbio)
610 {
611 s->wbio=BIO_pop(s->wbio);
612 }
613 BIO_free(s->bbio);
614 s->bbio=NULL;
615 }
616 if (s->rbio != NULL)
617 BIO_free_all(s->rbio);
618 if ((s->wbio != NULL) && (s->wbio != s->rbio))
619 BIO_free_all(s->wbio);
620
621 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
622
623 /* add extra stuff */
Adam Langley858a88d2014-06-20 12:00:00 -0700624 if (s->cipher_list != NULL)
625 ssl_cipher_preference_list_free(s->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -0700626 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
627
628 /* Make the next call work :-) */
629 if (s->session != NULL)
630 {
631 ssl_clear_bad_session(s);
632 SSL_SESSION_free(s->session);
633 }
634
635 ssl_clear_cipher_ctx(s);
636 ssl_clear_hash_ctx(&s->read_hash);
637 ssl_clear_hash_ctx(&s->write_hash);
638
639 if (s->cert != NULL) ssl_cert_free(s->cert);
640 /* Free up if allocated */
641
Adam Langley95c29f32014-06-20 12:00:00 -0700642 if (s->tlsext_hostname)
643 OPENSSL_free(s->tlsext_hostname);
644 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700645 if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
646 if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -0700647 if (s->alpn_client_proto_list)
648 OPENSSL_free(s->alpn_client_proto_list);
Adam Langley1258b6a2014-06-20 12:00:00 -0700649 if (s->tlsext_channel_id_private)
650 EVP_PKEY_free(s->tlsext_channel_id_private);
Adam Langley95c29f32014-06-20 12:00:00 -0700651
Adam Langley0289c732014-06-20 12:00:00 -0700652 if (s->psk_identity_hint)
653 OPENSSL_free(s->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -0700654
Adam Langley95c29f32014-06-20 12:00:00 -0700655 if (s->client_CA != NULL)
656 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
657
658 if (s->method != NULL) s->method->ssl_free(s);
659
660 if (s->ctx) SSL_CTX_free(s->ctx);
661
Adam Langley95c29f32014-06-20 12:00:00 -0700662 if (s->next_proto_negotiated)
663 OPENSSL_free(s->next_proto_negotiated);
Adam Langley95c29f32014-06-20 12:00:00 -0700664
665 if (s->srtp_profiles)
666 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
667
Adam Langley95c29f32014-06-20 12:00:00 -0700668 OPENSSL_free(s);
669 }
670
671void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
672 {
673 /* If the output buffering BIO is still in place, remove it
674 */
675 if (s->bbio != NULL)
676 {
677 if (s->wbio == s->bbio)
678 {
679 s->wbio=s->wbio->next_bio;
680 s->bbio->next_bio=NULL;
681 }
682 }
683 if ((s->rbio != NULL) && (s->rbio != rbio))
684 BIO_free_all(s->rbio);
685 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
686 BIO_free_all(s->wbio);
687 s->rbio=rbio;
688 s->wbio=wbio;
689 }
690
691BIO *SSL_get_rbio(const SSL *s)
692 { return(s->rbio); }
693
694BIO *SSL_get_wbio(const SSL *s)
695 { return(s->wbio); }
696
697int SSL_get_fd(const SSL *s)
698 {
699 return(SSL_get_rfd(s));
700 }
701
702int SSL_get_rfd(const SSL *s)
703 {
704 int ret= -1;
705 BIO *b,*r;
706
707 b=SSL_get_rbio(s);
708 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
709 if (r != NULL)
710 BIO_get_fd(r,&ret);
711 return(ret);
712 }
713
714int SSL_get_wfd(const SSL *s)
715 {
716 int ret= -1;
717 BIO *b,*r;
718
719 b=SSL_get_wbio(s);
720 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
721 if (r != NULL)
722 BIO_get_fd(r,&ret);
723 return(ret);
724 }
725
726#ifndef OPENSSL_NO_SOCK
727int SSL_set_fd(SSL *s,int fd)
728 {
729 int ret=0;
730 BIO *bio=NULL;
731
732 bio=BIO_new(BIO_s_fd());
733
734 if (bio == NULL)
735 {
736 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
737 goto err;
738 }
739 BIO_set_fd(bio,fd,BIO_NOCLOSE);
740 SSL_set_bio(s,bio,bio);
741 ret=1;
742err:
743 return(ret);
744 }
745
746int SSL_set_wfd(SSL *s,int fd)
747 {
748 int ret=0;
749 BIO *bio=NULL;
750
751 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_FD)
752 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
753 {
754 bio=BIO_new(BIO_s_fd());
755
756 if (bio == NULL)
757 {
758 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
759 goto err;
760 }
761 BIO_set_fd(bio,fd,BIO_NOCLOSE);
762 SSL_set_bio(s,SSL_get_rbio(s),bio);
763 }
764 else
765 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
766 ret=1;
767err:
768 return(ret);
769 }
770
771int SSL_set_rfd(SSL *s,int fd)
772 {
773 int ret=0;
774 BIO *bio=NULL;
775
776 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_FD)
777 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
778 {
779 bio=BIO_new(BIO_s_fd());
780
781 if (bio == NULL)
782 {
783 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
784 goto err;
785 }
786 BIO_set_fd(bio,fd,BIO_NOCLOSE);
787 SSL_set_bio(s,bio,SSL_get_wbio(s));
788 }
789 else
790 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
791 ret=1;
792err:
793 return(ret);
794 }
795#endif
796
797
798/* return length of latest Finished message we sent, copy to 'buf' */
799size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
800 {
801 size_t ret = 0;
802
803 if (s->s3 != NULL)
804 {
805 ret = s->s3->tmp.finish_md_len;
806 if (count > ret)
807 count = ret;
808 memcpy(buf, s->s3->tmp.finish_md, count);
809 }
810 return ret;
811 }
812
813/* return length of latest Finished message we expected, copy to 'buf' */
814size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
815 {
816 size_t ret = 0;
817
818 if (s->s3 != NULL)
819 {
820 ret = s->s3->tmp.peer_finish_md_len;
821 if (count > ret)
822 count = ret;
823 memcpy(buf, s->s3->tmp.peer_finish_md, count);
824 }
825 return ret;
826 }
827
828
829int SSL_get_verify_mode(const SSL *s)
830 {
831 return(s->verify_mode);
832 }
833
834int SSL_get_verify_depth(const SSL *s)
835 {
836 return X509_VERIFY_PARAM_get_depth(s->param);
837 }
838
839int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
840 {
841 return(s->verify_callback);
842 }
843
844int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
845 {
846 return(ctx->verify_mode);
847 }
848
849int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
850 {
851 return X509_VERIFY_PARAM_get_depth(ctx->param);
852 }
853
854int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
855 {
856 return(ctx->default_verify_callback);
857 }
858
859void SSL_set_verify(SSL *s,int mode,
860 int (*callback)(int ok,X509_STORE_CTX *ctx))
861 {
862 s->verify_mode=mode;
863 if (callback != NULL)
864 s->verify_callback=callback;
865 }
866
867void SSL_set_verify_depth(SSL *s,int depth)
868 {
869 X509_VERIFY_PARAM_set_depth(s->param, depth);
870 }
871
872void SSL_set_read_ahead(SSL *s,int yes)
873 {
874 s->read_ahead=yes;
875 }
876
877int SSL_get_read_ahead(const SSL *s)
878 {
879 return(s->read_ahead);
880 }
881
882int SSL_pending(const SSL *s)
883 {
884 /* SSL_pending cannot work properly if read-ahead is enabled
885 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
886 * and it is impossible to fix since SSL_pending cannot report
887 * errors that may be observed while scanning the new data.
888 * (Note that SSL_pending() is often used as a boolean value,
889 * so we'd better not return -1.)
890 */
891 return(s->method->ssl_pending(s));
892 }
893
894X509 *SSL_get_peer_certificate(const SSL *s)
895 {
896 X509 *r;
897
898 if ((s == NULL) || (s->session == NULL))
899 r=NULL;
900 else
901 r=s->session->peer;
902
David Benjamin150c6172014-08-05 22:22:49 -0400903 if (r == NULL)
904 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700905
David Benjamin150c6172014-08-05 22:22:49 -0400906 return X509_up_ref(r);
Adam Langley95c29f32014-06-20 12:00:00 -0700907 }
908
909STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
910 {
911 STACK_OF(X509) *r;
912
913 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
914 r=NULL;
915 else
916 r=s->session->sess_cert->cert_chain;
917
918 /* If we are a client, cert_chain includes the peer's own
919 * certificate; if we are a server, it does not. */
920
921 return(r);
922 }
923
Adam Langley95c29f32014-06-20 12:00:00 -0700924/* Fix this so it checks all the valid key/cert options */
925int SSL_CTX_check_private_key(const SSL_CTX *ctx)
926 {
927 if ( (ctx == NULL) ||
928 (ctx->cert == NULL) ||
929 (ctx->cert->key->x509 == NULL))
930 {
931 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
932 return(0);
933 }
934 if (ctx->cert->key->privatekey == NULL)
935 {
936 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
937 return(0);
938 }
939 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
940 }
941
942/* Fix this function so that it takes an optional type parameter */
943int SSL_check_private_key(const SSL *ssl)
944 {
945 if (ssl == NULL)
946 {
947 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
948 return(0);
949 }
950 if (ssl->cert == NULL)
951 {
952 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
953 return 0;
954 }
955 if (ssl->cert->key->x509 == NULL)
956 {
957 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
958 return(0);
959 }
960 if (ssl->cert->key->privatekey == NULL)
961 {
962 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
963 return(0);
964 }
965 return(X509_check_private_key(ssl->cert->key->x509,
966 ssl->cert->key->privatekey));
967 }
968
969int SSL_accept(SSL *s)
970 {
971 if (s->handshake_func == 0)
972 /* Not properly initialized yet */
973 SSL_set_accept_state(s);
974
David Benjamin0b145c22014-11-26 20:10:09 -0500975 if (s->handshake_func != s->method->ssl_accept)
976 {
977 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
978 return -1;
979 }
980
981 return s->handshake_func(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700982 }
983
984int SSL_connect(SSL *s)
985 {
986 if (s->handshake_func == 0)
987 /* Not properly initialized yet */
988 SSL_set_connect_state(s);
989
David Benjamin0b145c22014-11-26 20:10:09 -0500990 if (s->handshake_func != s->method->ssl_connect)
991 {
992 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
993 return -1;
994 }
995
996 return s->handshake_func(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700997 }
998
999long SSL_get_default_timeout(const SSL *s)
1000 {
David Benjaminf4501342014-08-14 17:24:37 -04001001 return SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001002 }
1003
1004int SSL_read(SSL *s,void *buf,int num)
1005 {
1006 if (s->handshake_func == 0)
1007 {
1008 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
1009 return -1;
1010 }
1011
1012 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1013 {
1014 s->rwstate=SSL_NOTHING;
1015 return(0);
1016 }
1017 return(s->method->ssl_read(s,buf,num));
1018 }
1019
1020int SSL_peek(SSL *s,void *buf,int num)
1021 {
1022 if (s->handshake_func == 0)
1023 {
1024 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
1025 return -1;
1026 }
1027
1028 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1029 {
1030 return(0);
1031 }
1032 return(s->method->ssl_peek(s,buf,num));
1033 }
1034
1035int SSL_write(SSL *s,const void *buf,int num)
1036 {
1037 if (s->handshake_func == 0)
1038 {
1039 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
1040 return -1;
1041 }
1042
1043 if (s->shutdown & SSL_SENT_SHUTDOWN)
1044 {
1045 s->rwstate=SSL_NOTHING;
1046 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
1047 return(-1);
1048 }
1049 return(s->method->ssl_write(s,buf,num));
1050 }
1051
1052int SSL_shutdown(SSL *s)
1053 {
1054 /* Note that this function behaves differently from what one might
1055 * expect. Return values are 0 for no success (yet),
1056 * 1 for success; but calling it once is usually not enough,
1057 * even if blocking I/O is used (see ssl3_shutdown).
1058 */
1059
1060 if (s->handshake_func == 0)
1061 {
1062 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1063 return -1;
1064 }
1065
Adam Langleyb2cb0ec2014-09-02 14:28:49 -07001066 if (!SSL_in_init(s))
Adam Langley95c29f32014-06-20 12:00:00 -07001067 return(s->method->ssl_shutdown(s));
1068 else
1069 return(1);
1070 }
1071
1072int SSL_renegotiate(SSL *s)
1073 {
1074 if (s->renegotiate == 0)
1075 s->renegotiate=1;
1076
1077 s->new_session=1;
1078
1079 return(s->method->ssl_renegotiate(s));
1080 }
1081
1082int SSL_renegotiate_abbreviated(SSL *s)
1083 {
1084 if (s->renegotiate == 0)
1085 s->renegotiate=1;
1086
1087 s->new_session=0;
1088
1089 return(s->method->ssl_renegotiate(s));
1090 }
1091
1092int SSL_renegotiate_pending(SSL *s)
1093 {
1094 /* becomes true when negotiation is requested;
1095 * false again once a handshake has finished */
1096 return (s->renegotiate != 0);
1097 }
1098
1099long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1100 {
1101 long l;
1102
1103 switch (cmd)
1104 {
1105 case SSL_CTRL_GET_READ_AHEAD:
1106 return(s->read_ahead);
1107 case SSL_CTRL_SET_READ_AHEAD:
1108 l=s->read_ahead;
1109 s->read_ahead=larg;
1110 return(l);
1111
1112 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1113 s->msg_callback_arg = parg;
1114 return 1;
1115
1116 case SSL_CTRL_OPTIONS:
1117 return(s->options|=larg);
1118 case SSL_CTRL_CLEAR_OPTIONS:
1119 return(s->options&=~larg);
1120 case SSL_CTRL_MODE:
1121 return(s->mode|=larg);
1122 case SSL_CTRL_CLEAR_MODE:
1123 return(s->mode &=~larg);
1124 case SSL_CTRL_GET_MAX_CERT_LIST:
1125 return(s->max_cert_list);
1126 case SSL_CTRL_SET_MAX_CERT_LIST:
1127 l=s->max_cert_list;
1128 s->max_cert_list=larg;
1129 return(l);
1130 case SSL_CTRL_SET_MTU:
Adam Langley95c29f32014-06-20 12:00:00 -07001131 if (larg < (long)dtls1_min_mtu())
1132 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001133 if (SSL_IS_DTLS(s))
1134 {
1135 s->d1->mtu = larg;
1136 return larg;
1137 }
1138 return 0;
1139 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1140 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1141 return 0;
1142 s->max_send_fragment = larg;
1143 return 1;
1144 case SSL_CTRL_GET_RI_SUPPORT:
1145 if (s->s3)
1146 return s->s3->send_connection_binding;
1147 else return 0;
1148 case SSL_CTRL_CERT_FLAGS:
1149 return(s->cert->cert_flags|=larg);
1150 case SSL_CTRL_CLEAR_CERT_FLAGS:
1151 return(s->cert->cert_flags &=~larg);
1152
1153 case SSL_CTRL_GET_RAW_CIPHERLIST:
1154 if (parg)
1155 {
1156 if (s->cert->ciphers_raw == NULL)
1157 return 0;
1158 *(unsigned char **)parg = s->cert->ciphers_raw;
1159 return (int)s->cert->ciphers_rawlen;
1160 }
1161 else
David Benjamin39482a12014-07-20 13:30:15 -04001162 {
1163 /* Passing a NULL |parg| returns the size of a single
1164 * cipher suite value. */
1165 return 2;
1166 }
Adam Langley95c29f32014-06-20 12:00:00 -07001167 default:
1168 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1169 }
1170 }
1171
1172long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1173 {
1174 switch(cmd)
1175 {
1176 case SSL_CTRL_SET_MSG_CALLBACK:
1177 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1178 return 1;
1179
1180 default:
1181 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1182 }
1183 }
1184
1185LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1186 {
1187 return ctx->sessions;
1188 }
1189
1190long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1191 {
1192 long l;
1193
1194 switch (cmd)
1195 {
1196 case SSL_CTRL_GET_READ_AHEAD:
1197 return(ctx->read_ahead);
1198 case SSL_CTRL_SET_READ_AHEAD:
1199 l=ctx->read_ahead;
1200 ctx->read_ahead=larg;
1201 return(l);
1202
1203 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1204 ctx->msg_callback_arg = parg;
1205 return 1;
1206
1207 case SSL_CTRL_GET_MAX_CERT_LIST:
1208 return(ctx->max_cert_list);
1209 case SSL_CTRL_SET_MAX_CERT_LIST:
1210 l=ctx->max_cert_list;
1211 ctx->max_cert_list=larg;
1212 return(l);
1213
1214 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1215 l=ctx->session_cache_size;
1216 ctx->session_cache_size=larg;
1217 return(l);
1218 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1219 return(ctx->session_cache_size);
1220 case SSL_CTRL_SET_SESS_CACHE_MODE:
1221 l=ctx->session_cache_mode;
1222 ctx->session_cache_mode=larg;
1223 return(l);
1224 case SSL_CTRL_GET_SESS_CACHE_MODE:
1225 return(ctx->session_cache_mode);
1226
1227 case SSL_CTRL_SESS_NUMBER:
1228 return(lh_SSL_SESSION_num_items(ctx->sessions));
1229 case SSL_CTRL_SESS_CONNECT:
1230 return(ctx->stats.sess_connect);
1231 case SSL_CTRL_SESS_CONNECT_GOOD:
1232 return(ctx->stats.sess_connect_good);
1233 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1234 return(ctx->stats.sess_connect_renegotiate);
1235 case SSL_CTRL_SESS_ACCEPT:
1236 return(ctx->stats.sess_accept);
1237 case SSL_CTRL_SESS_ACCEPT_GOOD:
1238 return(ctx->stats.sess_accept_good);
1239 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1240 return(ctx->stats.sess_accept_renegotiate);
1241 case SSL_CTRL_SESS_HIT:
1242 return(ctx->stats.sess_hit);
1243 case SSL_CTRL_SESS_CB_HIT:
1244 return(ctx->stats.sess_cb_hit);
1245 case SSL_CTRL_SESS_MISSES:
1246 return(ctx->stats.sess_miss);
1247 case SSL_CTRL_SESS_TIMEOUTS:
1248 return(ctx->stats.sess_timeout);
1249 case SSL_CTRL_SESS_CACHE_FULL:
1250 return(ctx->stats.sess_cache_full);
1251 case SSL_CTRL_OPTIONS:
1252 return(ctx->options|=larg);
1253 case SSL_CTRL_CLEAR_OPTIONS:
1254 return(ctx->options&=~larg);
1255 case SSL_CTRL_MODE:
1256 return(ctx->mode|=larg);
1257 case SSL_CTRL_CLEAR_MODE:
1258 return(ctx->mode&=~larg);
1259 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1260 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1261 return 0;
1262 ctx->max_send_fragment = larg;
1263 return 1;
1264 case SSL_CTRL_CERT_FLAGS:
1265 return(ctx->cert->cert_flags|=larg);
1266 case SSL_CTRL_CLEAR_CERT_FLAGS:
1267 return(ctx->cert->cert_flags &=~larg);
1268 default:
1269 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1270 }
1271 }
1272
1273long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1274 {
1275 switch(cmd)
1276 {
1277 case SSL_CTRL_SET_MSG_CALLBACK:
1278 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1279 return 1;
1280
1281 default:
1282 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1283 }
1284 }
1285
1286int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1287 {
1288 long l;
1289 const SSL_CIPHER *a = in_a;
1290 const SSL_CIPHER *b = in_b;
1291 const long a_id = a->id;
1292 const long b_id = b->id;
1293
1294 l = a_id - b_id;
1295 if (l == 0L)
1296 return(0);
1297 else
1298 return((l > 0)?1:-1);
1299 }
1300
1301int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1302 {
1303 long l;
1304 const long a_id = (*ap)->id;
1305 const long b_id = (*bp)->id;
1306
1307 l = a_id - b_id;
1308 if (l == 0)
1309 return(0);
1310 else
1311 return((l > 0)?1:-1);
1312 }
1313
1314/** return a STACK of the ciphers available for the SSL and in order of
1315 * preference */
1316STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1317 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001318 if (s == NULL)
1319 return NULL;
1320
1321 if (s->cipher_list != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001322 {
Adam Langley858a88d2014-06-20 12:00:00 -07001323 return(s->cipher_list->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -07001324 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001325
1326 if (s->version >= TLS1_1_VERSION)
1327 {
1328 if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001329 return s->ctx->cipher_list_tls11->ciphers;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001330 }
1331
1332 if ((s->ctx != NULL) &&
1333 (s->ctx->cipher_list != NULL))
1334 {
Adam Langley858a88d2014-06-20 12:00:00 -07001335 return(s->ctx->cipher_list->ciphers);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001336 }
1337
Adam Langley95c29f32014-06-20 12:00:00 -07001338 return(NULL);
1339 }
1340
1341/** return a STACK of the ciphers available for the SSL and in order of
1342 * algorithm id */
1343STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1344 {
1345 if (s != NULL)
1346 {
1347 if (s->cipher_list_by_id != NULL)
1348 {
1349 return(s->cipher_list_by_id);
1350 }
1351 else if ((s->ctx != NULL) &&
1352 (s->ctx->cipher_list_by_id != NULL))
1353 {
1354 return(s->ctx->cipher_list_by_id);
1355 }
1356 }
1357 return(NULL);
1358 }
1359
1360/** The old interface to get the same thing as SSL_get_ciphers() */
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001361const char *SSL_get_cipher_list(const SSL *s, int n)
Adam Langley95c29f32014-06-20 12:00:00 -07001362 {
David Benjamin6f260012014-08-15 13:49:12 -04001363 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001364 STACK_OF(SSL_CIPHER) *sk;
1365
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001366 if (s == NULL)
1367 return NULL;
1368 sk = SSL_get_ciphers(s);
1369 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk))
1370 return NULL;
1371 c = sk_SSL_CIPHER_value(sk, n);
1372 if (c == NULL)
1373 return NULL;
1374 return c->name;
Adam Langley95c29f32014-06-20 12:00:00 -07001375 }
1376
1377/** specify the ciphers to be used by default by the SSL_CTX */
1378int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1379 {
1380 STACK_OF(SSL_CIPHER) *sk;
1381
1382 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1383 &ctx->cipher_list_by_id,str, ctx->cert);
1384 /* ssl_create_cipher_list may return an empty stack if it
1385 * was unable to find a cipher matching the given rule string
1386 * (for example if the rule string specifies a cipher which
1387 * has been disabled). This is not an error as far as
1388 * ssl_create_cipher_list is concerned, and hence
1389 * ctx->cipher_list and ctx->cipher_list_by_id has been
1390 * updated. */
1391 if (sk == NULL)
1392 return 0;
1393 else if (sk_SSL_CIPHER_num(sk) == 0)
1394 {
1395 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1396 return 0;
1397 }
1398 return 1;
1399 }
1400
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001401int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str)
1402 {
1403 STACK_OF(SSL_CIPHER) *sk;
1404
1405 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert);
1406 if (sk == NULL)
1407 return 0;
1408 else if (sk_SSL_CIPHER_num(sk) == 0)
1409 {
1410 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH);
1411 return 0;
1412 }
1413 return 1;
1414 }
1415
Adam Langley95c29f32014-06-20 12:00:00 -07001416/** specify the ciphers to be used by the SSL */
1417int SSL_set_cipher_list(SSL *s,const char *str)
1418 {
1419 STACK_OF(SSL_CIPHER) *sk;
1420
1421 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1422 &s->cipher_list_by_id,str, s->cert);
1423 /* see comment in SSL_CTX_set_cipher_list */
1424 if (sk == NULL)
1425 return 0;
1426 else if (sk_SSL_CIPHER_num(sk) == 0)
1427 {
1428 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1429 return 0;
1430 }
1431 return 1;
1432 }
1433
David Benjamin5491e3f2014-09-29 19:33:09 -04001434int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p)
Adam Langley95c29f32014-06-20 12:00:00 -07001435 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001436 size_t i;
David Benjamin6f260012014-08-15 13:49:12 -04001437 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001438 CERT *ct = s->cert;
David Benjamin5491e3f2014-09-29 19:33:09 -04001439 uint8_t *q;
Adam Langley95c29f32014-06-20 12:00:00 -07001440 /* Set disabled masks for this session */
1441 ssl_set_client_disabled(s);
1442
1443 if (sk == NULL) return(0);
1444 q=p;
1445
1446 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1447 {
1448 c=sk_SSL_CIPHER_value(sk,i);
1449 /* Skip disabled ciphers */
1450 if (c->algorithm_ssl & ct->mask_ssl ||
1451 c->algorithm_mkey & ct->mask_k ||
1452 c->algorithm_auth & ct->mask_a)
1453 continue;
David Benjamin39482a12014-07-20 13:30:15 -04001454 s2n(ssl3_get_cipher_value(c), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001455 }
David Benjamin5491e3f2014-09-29 19:33:09 -04001456 /* If all ciphers were disabled, return the error to the caller. */
1457 if (p == q)
Adam Langley95c29f32014-06-20 12:00:00 -07001458 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001459 return 0;
1460 }
1461
1462 /* Add SCSVs. */
1463 if (!s->renegotiate)
1464 {
1465 s2n(SSL3_CK_SCSV & 0xffff, p);
1466 }
1467 if (s->fallback_scsv)
1468 {
1469 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001470 }
1471
1472 return(p-q);
1473 }
1474
David Benjamin9f2c0d72014-10-21 22:00:19 -04001475STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001476 {
David Benjamin39482a12014-07-20 13:30:15 -04001477 CBS cipher_suites = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001478 const SSL_CIPHER *c;
1479 STACK_OF(SSL_CIPHER) *sk;
David Benjamin39482a12014-07-20 13:30:15 -04001480
Adam Langley95c29f32014-06-20 12:00:00 -07001481 if (s->s3)
1482 s->s3->send_connection_binding = 0;
1483
David Benjamin39482a12014-07-20 13:30:15 -04001484 if (CBS_len(&cipher_suites) % 2 != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001485 {
1486 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001487 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001488 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001489
1490 sk = sk_SSL_CIPHER_new_null();
1491 if (sk == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001492 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001493 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1494 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001495 }
1496
David Benjamin39482a12014-07-20 13:30:15 -04001497 if (!CBS_stow(&cipher_suites,
1498 &s->cert->ciphers_raw, &s->cert->ciphers_rawlen))
Adam Langley95c29f32014-06-20 12:00:00 -07001499 {
1500 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1501 goto err;
1502 }
Adam Langley95c29f32014-06-20 12:00:00 -07001503
David Benjamin39482a12014-07-20 13:30:15 -04001504 while (CBS_len(&cipher_suites) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001505 {
David Benjamin39482a12014-07-20 13:30:15 -04001506 uint16_t cipher_suite;
1507
1508 if (!CBS_get_u16(&cipher_suites, &cipher_suite))
1509 {
1510 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1511 goto err;
1512 }
1513
David Benjamin9f2c0d72014-10-21 22:00:19 -04001514 /* Check for SCSV. */
David Benjamin39482a12014-07-20 13:30:15 -04001515 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff))
Adam Langley95c29f32014-06-20 12:00:00 -07001516 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001517 /* SCSV is fatal if renegotiating. */
Adam Langley95c29f32014-06-20 12:00:00 -07001518 if (s->renegotiate)
1519 {
1520 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1521 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1522 goto err;
1523 }
1524 s->s3->send_connection_binding = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001525 continue;
1526 }
1527
David Benjamin9f2c0d72014-10-21 22:00:19 -04001528 /* Check for FALLBACK_SCSV. */
1529 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff))
Adam Langley29707792014-06-20 12:00:00 -07001530 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001531 if (s->version < ssl_get_max_version(s))
1532 {
1533 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1534 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1535 goto err;
1536 }
1537 continue;
Adam Langley29707792014-06-20 12:00:00 -07001538 }
1539
David Benjamin39482a12014-07-20 13:30:15 -04001540 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -07001541 if (c != NULL)
1542 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001543 if (!sk_SSL_CIPHER_push(sk, c))
Adam Langley95c29f32014-06-20 12:00:00 -07001544 {
1545 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1546 goto err;
1547 }
1548 }
1549 }
1550
David Benjamin9f2c0d72014-10-21 22:00:19 -04001551 return sk;
1552
Adam Langley95c29f32014-06-20 12:00:00 -07001553err:
David Benjamin9f2c0d72014-10-21 22:00:19 -04001554 if (sk != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001555 sk_SSL_CIPHER_free(sk);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001556 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001557 }
1558
1559
Adam Langley95c29f32014-06-20 12:00:00 -07001560/** return a servername extension value if provided in Client Hello, or NULL.
1561 * So far, only host_name types are defined (RFC 3546).
1562 */
1563
1564const char *SSL_get_servername(const SSL *s, const int type)
1565 {
1566 if (type != TLSEXT_NAMETYPE_host_name)
1567 return NULL;
1568
1569 return s->session && !s->tlsext_hostname ?
1570 s->session->tlsext_hostname :
1571 s->tlsext_hostname;
1572 }
1573
1574int SSL_get_servername_type(const SSL *s)
1575 {
1576 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1577 return TLSEXT_NAMETYPE_host_name;
1578 return -1;
1579 }
1580
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001581void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx)
1582 {
1583 ctx->signed_cert_timestamps_enabled = 1;
1584 }
1585
1586int SSL_enable_signed_cert_timestamps(SSL *ssl)
1587 {
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001588 ssl->signed_cert_timestamps_enabled = 1;
1589 return 1;
1590 }
1591
David Benjamin6c7aed02014-08-27 16:42:38 -04001592void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx)
1593 {
1594 ctx->ocsp_stapling_enabled = 1;
1595 }
1596
1597int SSL_enable_ocsp_stapling(SSL *ssl)
1598 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001599 ssl->ocsp_stapling_enabled = 1;
1600 return 1;
1601 }
1602
David Benjamin52d699f2014-11-25 01:35:18 -05001603void 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 +02001604 {
Adam Langley3cb50e02014-08-26 14:00:31 -07001605 SSL_SESSION *session = ssl->session;
1606
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001607 *out_len = 0;
1608 *out = NULL;
1609 if (ssl->server)
1610 return;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001611 if (!session || !session->tlsext_signed_cert_timestamp_list)
1612 return;
1613 *out = session->tlsext_signed_cert_timestamp_list;
1614 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1615 }
1616
David Benjamin52d699f2014-11-25 01:35:18 -05001617void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out, size_t *out_len)
David Benjamin6c7aed02014-08-27 16:42:38 -04001618 {
1619 SSL_SESSION *session = ssl->session;
1620
1621 *out_len = 0;
1622 *out = NULL;
1623 if (ssl->server)
1624 return;
1625 if (!session || !session->ocsp_response)
1626 return;
1627 *out = session->ocsp_response;
1628 *out_len = session->ocsp_response_length;
1629 }
1630
Adam Langley95c29f32014-06-20 12:00:00 -07001631/* SSL_select_next_proto implements the standard protocol selection. It is
1632 * expected that this function is called from the callback set by
1633 * SSL_CTX_set_next_proto_select_cb.
1634 *
1635 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1636 * strings. The length byte itself is not included in the length. A byte
1637 * string of length 0 is invalid. No byte string may be truncated.
1638 *
1639 * The current, but experimental algorithm for selecting the protocol is:
1640 *
1641 * 1) If the server doesn't support NPN then this is indicated to the
1642 * callback. In this case, the client application has to abort the connection
1643 * or have a default application level protocol.
1644 *
1645 * 2) If the server supports NPN, but advertises an empty list then the
1646 * client selects the first protcol in its list, but indicates via the
1647 * API that this fallback case was enacted.
1648 *
1649 * 3) Otherwise, the client finds the first protocol in the server's list
1650 * that it supports and selects this protocol. This is because it's
1651 * assumed that the server has better information about which protocol
1652 * a client should use.
1653 *
1654 * 4) If the client doesn't support any of the server's advertised
1655 * protocols, then this is treated the same as case 2.
1656 *
1657 * It returns either
1658 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1659 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1660 */
1661int 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)
1662 {
1663 unsigned int i, j;
1664 const unsigned char *result;
1665 int status = OPENSSL_NPN_UNSUPPORTED;
1666
1667 /* For each protocol in server preference order, see if we support it. */
1668 for (i = 0; i < server_len; )
1669 {
1670 for (j = 0; j < client_len; )
1671 {
1672 if (server[i] == client[j] &&
1673 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1674 {
1675 /* We found a match */
1676 result = &server[i];
1677 status = OPENSSL_NPN_NEGOTIATED;
1678 goto found;
1679 }
1680 j += client[j];
1681 j++;
1682 }
1683 i += server[i];
1684 i++;
1685 }
1686
1687 /* There's no overlap between our protocols and the server's list. */
1688 result = client;
1689 status = OPENSSL_NPN_NO_OVERLAP;
1690
1691 found:
1692 *out = (unsigned char *) result + 1;
1693 *outlen = result[0];
1694 return status;
1695 }
1696
Adam Langley95c29f32014-06-20 12:00:00 -07001697/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1698 * requested protocol for this connection and returns 0. If the client didn't
1699 * request any protocol, then *data is set to NULL.
1700 *
1701 * Note that the client can request any protocol it chooses. The value returned
1702 * from this function need not be a member of the list of supported protocols
1703 * provided by the callback.
1704 */
David Benjaminced551f2014-07-11 23:16:16 -04001705void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, unsigned *len)
Adam Langley95c29f32014-06-20 12:00:00 -07001706 {
1707 *data = s->next_proto_negotiated;
1708 if (!*data) {
1709 *len = 0;
1710 } else {
1711 *len = s->next_proto_negotiated_len;
1712 }
1713}
1714
1715/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1716 * TLS server needs a list of supported protocols for Next Protocol
1717 * Negotiation. The returned list must be in wire format. The list is returned
1718 * by setting |out| to point to it and |outlen| to its length. This memory will
1719 * not be modified, but one should assume that the SSL* keeps a reference to
1720 * it.
1721 *
1722 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1723 * such extension will be included in the ServerHello. */
1724void 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)
1725 {
1726 ctx->next_protos_advertised_cb = cb;
1727 ctx->next_protos_advertised_cb_arg = arg;
1728 }
1729
1730/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1731 * client needs to select a protocol from the server's provided list. |out|
1732 * must be set to point to the selected protocol (which may be within |in|).
1733 * The length of the protocol name must be written into |outlen|. The server's
1734 * advertised protocols are provided in |in| and |inlen|. The callback can
1735 * assume that |in| is syntactically valid.
1736 *
1737 * The client must select a protocol. It is fatal to the connection if this
1738 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1739 */
1740void 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)
1741 {
1742 ctx->next_proto_select_cb = cb;
1743 ctx->next_proto_select_cb_arg = arg;
1744 }
Adam Langley95c29f32014-06-20 12:00:00 -07001745
Adam Langley95c29f32014-06-20 12:00:00 -07001746/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1747 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1748 * length-prefixed strings).
1749 *
1750 * Returns 0 on success. */
1751int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1752 unsigned protos_len)
1753 {
1754 if (ctx->alpn_client_proto_list)
1755 OPENSSL_free(ctx->alpn_client_proto_list);
1756
David Benjamin072c9532014-07-26 11:44:25 -04001757 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001758 if (!ctx->alpn_client_proto_list)
1759 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001760 ctx->alpn_client_proto_list_len = protos_len;
1761
1762 return 0;
1763 }
1764
1765/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1766 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1767 * length-prefixed strings).
1768 *
1769 * Returns 0 on success. */
1770int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1771 unsigned protos_len)
1772 {
1773 if (ssl->alpn_client_proto_list)
1774 OPENSSL_free(ssl->alpn_client_proto_list);
1775
David Benjamin072c9532014-07-26 11:44:25 -04001776 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001777 if (!ssl->alpn_client_proto_list)
1778 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001779 ssl->alpn_client_proto_list_len = protos_len;
1780
1781 return 0;
1782 }
1783
1784/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1785 * during ClientHello processing in order to select an ALPN protocol from the
1786 * client's list of offered protocols. */
1787void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1788 int (*cb) (SSL *ssl,
1789 const unsigned char **out,
1790 unsigned char *outlen,
1791 const unsigned char *in,
1792 unsigned int inlen,
1793 void *arg),
1794 void *arg)
1795 {
1796 ctx->alpn_select_cb = cb;
1797 ctx->alpn_select_cb_arg = arg;
1798 }
1799
1800/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1801 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1802 * including the leading length-prefix byte). If the server didn't respond with
1803 * a negotiated protocol then |*len| will be zero. */
1804void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1805 unsigned *len)
1806 {
1807 *data = NULL;
1808 if (ssl->s3)
1809 *data = ssl->s3->alpn_selected;
1810 if (*data == NULL)
1811 *len = 0;
1812 else
1813 *len = ssl->s3->alpn_selected_len;
1814 }
Adam Langley95c29f32014-06-20 12:00:00 -07001815
1816int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1817 const char *label, size_t llen, const unsigned char *p, size_t plen,
1818 int use_context)
1819 {
1820 if (s->version < TLS1_VERSION)
1821 return -1;
1822
1823 return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1824 llen, p, plen,
1825 use_context);
1826 }
1827
1828static uint32_t ssl_session_hash(const SSL_SESSION *a)
1829 {
1830 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1831 ((uint32_t) a->session_id[1] << 8) ||
1832 ((uint32_t) a->session_id[2] << 16) ||
1833 ((uint32_t) a->session_id[3] << 24);
1834
1835 return hash;
1836 }
1837
1838/* NB: If this function (or indeed the hash function which uses a sort of
1839 * coarser function than this one) is changed, ensure
1840 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1841 * able to construct an SSL_SESSION that will collide with any existing session
1842 * with a matching session ID. */
1843static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1844 {
1845 if (a->ssl_version != b->ssl_version)
1846 return(1);
1847 if (a->session_id_length != b->session_id_length)
1848 return(1);
1849 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1850 }
1851
1852SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1853 {
1854 SSL_CTX *ret=NULL;
1855
1856 if (meth == NULL)
1857 {
1858 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1859 return(NULL);
1860 }
1861
Adam Langley95c29f32014-06-20 12:00:00 -07001862 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1863 {
1864 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1865 goto err;
1866 }
1867 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1868 if (ret == NULL)
1869 goto err;
1870
1871 memset(ret,0,sizeof(SSL_CTX));
1872
1873 ret->method=meth;
1874
1875 ret->cert_store=NULL;
1876 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1877 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1878 ret->session_cache_head=NULL;
1879 ret->session_cache_tail=NULL;
1880
1881 /* We take the system default */
David Benjaminf4501342014-08-14 17:24:37 -04001882 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001883
1884 ret->new_session_cb=0;
1885 ret->remove_session_cb=0;
1886 ret->get_session_cb=0;
1887 ret->generate_session_id=0;
1888
1889 memset((char *)&ret->stats,0,sizeof(ret->stats));
1890
1891 ret->references=1;
1892 ret->quiet_shutdown=0;
1893
Adam Langley95c29f32014-06-20 12:00:00 -07001894 ret->info_callback=NULL;
1895
1896 ret->app_verify_callback=0;
1897 ret->app_verify_arg=NULL;
1898
1899 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1900 ret->read_ahead=0;
1901 ret->msg_callback=0;
1902 ret->msg_callback_arg=NULL;
1903 ret->verify_mode=SSL_VERIFY_NONE;
1904#if 0
1905 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1906#endif
1907 ret->sid_ctx_length=0;
1908 ret->default_verify_callback=NULL;
1909 if ((ret->cert=ssl_cert_new()) == NULL)
1910 goto err;
1911
1912 ret->default_passwd_callback=0;
1913 ret->default_passwd_callback_userdata=NULL;
1914 ret->client_cert_cb=0;
1915 ret->app_gen_cookie_cb=0;
1916 ret->app_verify_cookie_cb=0;
1917
1918 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1919 if (ret->sessions == NULL) goto err;
1920 ret->cert_store=X509_STORE_new();
1921 if (ret->cert_store == NULL) goto err;
1922
1923 ssl_create_cipher_list(ret->method,
1924 &ret->cipher_list,&ret->cipher_list_by_id,
David Benjamin5e4f6e92014-11-17 03:23:24 -05001925 SSL_DEFAULT_CIPHER_LIST, ret->cert);
Adam Langley95c29f32014-06-20 12:00:00 -07001926 if (ret->cipher_list == NULL
Adam Langley858a88d2014-06-20 12:00:00 -07001927 || sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001928 {
1929 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1930 goto err2;
1931 }
1932
1933 ret->param = X509_VERIFY_PARAM_new();
1934 if (!ret->param)
1935 goto err;
1936
Adam Langley95c29f32014-06-20 12:00:00 -07001937 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1938 goto err;
1939
1940 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1941
1942 ret->extra_certs=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001943
1944 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1945
Adam Langley95c29f32014-06-20 12:00:00 -07001946 ret->tlsext_servername_callback = 0;
1947 ret->tlsext_servername_arg = NULL;
1948 /* Setup RFC4507 ticket keys */
1949 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
1950 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
1951 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
1952 ret->options |= SSL_OP_NO_TICKET;
1953
1954 ret->tlsext_status_cb = 0;
1955 ret->tlsext_status_arg = NULL;
1956
Adam Langley95c29f32014-06-20 12:00:00 -07001957 ret->next_protos_advertised_cb = 0;
1958 ret->next_proto_select_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001959 ret->psk_identity_hint=NULL;
1960 ret->psk_client_callback=NULL;
1961 ret->psk_server_callback=NULL;
David Benjamin422d3a42014-08-20 11:09:03 -04001962
Adam Langley95c29f32014-06-20 12:00:00 -07001963 /* Default is to connect to non-RI servers. When RI is more widely
1964 * deployed might change this.
1965 */
1966 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1967
1968 return(ret);
1969err:
1970 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
1971err2:
1972 if (ret != NULL) SSL_CTX_free(ret);
1973 return(NULL);
1974 }
1975
Adam Langley95c29f32014-06-20 12:00:00 -07001976
1977void SSL_CTX_free(SSL_CTX *a)
1978 {
1979 int i;
1980
1981 if (a == NULL) return;
1982
1983 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
1984#ifdef REF_PRINT
1985 REF_PRINT("SSL_CTX",a);
1986#endif
1987 if (i > 0) return;
1988#ifdef REF_CHECK
1989 if (i < 0)
1990 {
1991 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
1992 abort(); /* ok */
1993 }
1994#endif
1995
1996 if (a->param)
1997 X509_VERIFY_PARAM_free(a->param);
1998
1999 /*
2000 * Free internal session cache. However: the remove_cb() may reference
2001 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2002 * after the sessions were flushed.
2003 * As the ex_data handling routines might also touch the session cache,
2004 * the most secure solution seems to be: empty (flush) the cache, then
2005 * free ex_data, then finally free the cache.
2006 * (See ticket [openssl.org #212].)
2007 */
2008 if (a->sessions != NULL)
2009 SSL_CTX_flush_sessions(a,0);
2010
2011 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
2012
2013 if (a->sessions != NULL)
2014 lh_SSL_SESSION_free(a->sessions);
2015
2016 if (a->cert_store != NULL)
2017 X509_STORE_free(a->cert_store);
2018 if (a->cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002019 ssl_cipher_preference_list_free(a->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002020 if (a->cipher_list_by_id != NULL)
2021 sk_SSL_CIPHER_free(a->cipher_list_by_id);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07002022 if (a->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002023 ssl_cipher_preference_list_free(a->cipher_list_tls11);
Adam Langley95c29f32014-06-20 12:00:00 -07002024 if (a->cert != NULL)
2025 ssl_cert_free(a->cert);
2026 if (a->client_CA != NULL)
2027 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2028 if (a->extra_certs != NULL)
2029 sk_X509_pop_free(a->extra_certs,X509_free);
Adam Langley95c29f32014-06-20 12:00:00 -07002030
2031 if (a->srtp_profiles)
2032 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2033
Adam Langley95c29f32014-06-20 12:00:00 -07002034 if (a->psk_identity_hint)
2035 OPENSSL_free(a->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07002036
Adam Langley95c29f32014-06-20 12:00:00 -07002037 if (a->tlsext_ecpointformatlist)
2038 OPENSSL_free(a->tlsext_ecpointformatlist);
2039 if (a->tlsext_ellipticcurvelist)
2040 OPENSSL_free(a->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -07002041 if (a->alpn_client_proto_list != NULL)
2042 OPENSSL_free(a->alpn_client_proto_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002043
Adam Langley1258b6a2014-06-20 12:00:00 -07002044 if (a->tlsext_channel_id_private)
2045 EVP_PKEY_free(a->tlsext_channel_id_private);
Adam Langley1258b6a2014-06-20 12:00:00 -07002046
David Benjamin859ec3c2014-09-02 16:29:36 -04002047 if (a->keylog_bio)
2048 BIO_free(a->keylog_bio);
2049
Adam Langley95c29f32014-06-20 12:00:00 -07002050 OPENSSL_free(a);
2051 }
2052
2053void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2054 {
2055 ctx->default_passwd_callback=cb;
2056 }
2057
2058void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2059 {
2060 ctx->default_passwd_callback_userdata=u;
2061 }
2062
2063void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2064 {
2065 ctx->app_verify_callback=cb;
2066 ctx->app_verify_arg=arg;
2067 }
2068
2069void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2070 {
2071 ctx->verify_mode=mode;
2072 ctx->default_verify_callback=cb;
2073 }
2074
2075void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2076 {
2077 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2078 }
2079
2080void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2081 {
2082 ssl_cert_set_cert_cb(c->cert, cb, arg);
2083 }
2084
2085void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2086 {
2087 ssl_cert_set_cert_cb(s->cert, cb, arg);
2088 }
2089
David Benjamin033e5f42014-11-13 18:47:41 -05002090static int ssl_has_key(SSL *s, size_t idx)
2091 {
2092 CERT_PKEY *cpk = &s->cert->pkeys[idx];
2093 return cpk->x509 && cpk->privatekey;
2094 }
2095
David Benjaminf31e6812014-11-13 18:05:55 -05002096void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
2097 unsigned long *out_mask_a)
Adam Langley95c29f32014-06-20 12:00:00 -07002098 {
David Benjaminf31e6812014-11-13 18:05:55 -05002099 CERT *c = s->cert;
David Benjaminf31e6812014-11-13 18:05:55 -05002100 int rsa_enc, rsa_sign, dh_tmp;
2101 unsigned long mask_k, mask_a;
David Benjamin060d9d22014-07-15 00:54:26 -04002102 int have_ecc_cert, ecdsa_ok;
David Benjamin0da0e182014-08-19 16:20:28 -04002103 int have_ecdh_tmp;
David Benjaminf31e6812014-11-13 18:05:55 -05002104 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002105
David Benjaminf31e6812014-11-13 18:05:55 -05002106 if (c == NULL)
2107 {
2108 /* TODO(davidben): Is this codepath possible? */
2109 *out_mask_k = 0;
2110 *out_mask_a = 0;
2111 return;
2112 }
Adam Langley95c29f32014-06-20 12:00:00 -07002113
David Benjaminf31e6812014-11-13 18:05:55 -05002114 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002115
David Benjaminf31e6812014-11-13 18:05:55 -05002116 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
David Benjamin033e5f42014-11-13 18:47:41 -05002117 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2118 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2119 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
David Benjaminf31e6812014-11-13 18:05:55 -05002120 mask_k = 0;
2121 mask_a = 0;
2122
David Benjamin77a942b2014-07-15 01:22:50 -04002123 if (rsa_enc)
David Benjaminf31e6812014-11-13 18:05:55 -05002124 mask_k |= SSL_kRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002125
Adam Langley95c29f32014-06-20 12:00:00 -07002126 if (dh_tmp)
David Benjaminf31e6812014-11-13 18:05:55 -05002127 mask_k |= SSL_kEDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002128
Adam Langley95c29f32014-06-20 12:00:00 -07002129 if (rsa_enc || rsa_sign)
2130 {
David Benjaminf31e6812014-11-13 18:05:55 -05002131 mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002132 }
2133
David Benjaminf31e6812014-11-13 18:05:55 -05002134 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002135
David Benjamin0da0e182014-08-19 16:20:28 -04002136 /* An ECC certificate may be usable for ECDSA cipher suites depending on
David Benjamin033e5f42014-11-13 18:47:41 -05002137 * the key usage extension and on the client's curve preferences. */
Adam Langley95c29f32014-06-20 12:00:00 -07002138 if (have_ecc_cert)
2139 {
David Benjamin033e5f42014-11-13 18:47:41 -05002140 x = c->pkeys[SSL_PKEY_ECC].x509;
Adam Langley95c29f32014-06-20 12:00:00 -07002141 /* This call populates extension flags (ex_flags) */
2142 X509_check_purpose(x, -1, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07002143 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2144 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
David Benjamin033e5f42014-11-13 18:47:41 -05002145 if (!tls1_check_ec_cert(s, x))
Adam Langley95c29f32014-06-20 12:00:00 -07002146 ecdsa_ok = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002147 if (ecdsa_ok)
2148 {
David Benjaminf31e6812014-11-13 18:05:55 -05002149 mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002150 }
Adam Langley95c29f32014-06-20 12:00:00 -07002151 }
Adam Langley95c29f32014-06-20 12:00:00 -07002152
David Benjaminf31e6812014-11-13 18:05:55 -05002153 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2154 * key, check it. */
2155 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002156 {
David Benjaminf31e6812014-11-13 18:05:55 -05002157 mask_k |= SSL_kEECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002158 }
Adam Langley95c29f32014-06-20 12:00:00 -07002159
David Benjaminf31e6812014-11-13 18:05:55 -05002160 /* PSK requires a server callback. */
2161 if (s->psk_server_callback != NULL)
2162 {
2163 mask_k |= SSL_kPSK;
2164 mask_a |= SSL_aPSK;
2165 }
Adam Langley95c29f32014-06-20 12:00:00 -07002166
David Benjaminf31e6812014-11-13 18:05:55 -05002167 *out_mask_k = mask_k;
2168 *out_mask_a = mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002169 }
2170
2171/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2172#define ku_reject(x, usage) \
2173 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2174
Adam Langley95c29f32014-06-20 12:00:00 -07002175
2176int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2177 {
David Benjamin0da0e182014-08-19 16:20:28 -04002178 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002179 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2180 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2181
Adam Langley95c29f32014-06-20 12:00:00 -07002182 alg_a = cs->algorithm_auth;
2183
Adam Langley95c29f32014-06-20 12:00:00 -07002184 /* This call populates the ex_flags field correctly */
2185 X509_check_purpose(x, -1, 0);
2186 if ((x->sig_alg) && (x->sig_alg->algorithm))
2187 {
2188 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2189 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2190 }
Adam Langley95c29f32014-06-20 12:00:00 -07002191 if (alg_a & SSL_aECDSA)
2192 {
2193 /* key usage, if present, must allow signing */
2194 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2195 {
2196 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2197 return 0;
2198 }
2199 }
2200
2201 return 1; /* all checks are ok */
2202 }
2203
Adam Langley95c29f32014-06-20 12:00:00 -07002204
2205static int ssl_get_server_cert_index(const SSL *s)
2206 {
2207 int idx;
2208 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2209 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2210 idx = SSL_PKEY_RSA_SIGN;
2211 if (idx == -1)
2212 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2213 return idx;
2214 }
2215
2216CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2217 {
David Benjaminf31e6812014-11-13 18:05:55 -05002218 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002219
2220 /* This may or may not be an error. */
2221 if (i < 0)
2222 return NULL;
2223
2224 /* May be NULL. */
David Benjaminf31e6812014-11-13 18:05:55 -05002225 return &s->cert->pkeys[i];
Adam Langley95c29f32014-06-20 12:00:00 -07002226 }
2227
David Benjaminec2f27d2014-11-13 19:17:25 -05002228EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher)
Adam Langley95c29f32014-06-20 12:00:00 -07002229 {
2230 unsigned long alg_a;
2231 CERT *c;
2232 int idx = -1;
2233
2234 alg_a = cipher->algorithm_auth;
2235 c=s->cert;
2236
David Benjaminef2116d2014-08-19 20:21:56 -04002237 if (alg_a & SSL_aRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002238 {
2239 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2240 idx = SSL_PKEY_RSA_SIGN;
2241 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2242 idx = SSL_PKEY_RSA_ENC;
2243 }
2244 else if ((alg_a & SSL_aECDSA) &&
2245 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2246 idx = SSL_PKEY_ECC;
2247 if (idx == -1)
2248 {
2249 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2250 return(NULL);
2251 }
Adam Langley95c29f32014-06-20 12:00:00 -07002252 return c->pkeys[idx].privatekey;
2253 }
2254
Adam Langley95c29f32014-06-20 12:00:00 -07002255void ssl_update_cache(SSL *s,int mode)
2256 {
2257 int i;
2258
2259 /* If the session_id_length is 0, we are not supposed to cache it,
2260 * and it would be rather hard to do anyway :-) */
2261 if (s->session->session_id_length == 0) return;
2262
David Benjamin041b58a2014-08-27 15:25:21 -04002263 i=s->initial_ctx->session_cache_mode;
Adam Langley95c29f32014-06-20 12:00:00 -07002264 if ((i & mode) && (!s->hit)
2265 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
David Benjamin041b58a2014-08-27 15:25:21 -04002266 || SSL_CTX_add_session(s->initial_ctx,s->session))
2267 && (s->initial_ctx->new_session_cb != NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07002268 {
2269 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
David Benjamin041b58a2014-08-27 15:25:21 -04002270 if (!s->initial_ctx->new_session_cb(s,s->session))
Adam Langley95c29f32014-06-20 12:00:00 -07002271 SSL_SESSION_free(s->session);
2272 }
2273
2274 /* auto flush every 255 connections */
2275 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2276 ((i & mode) == mode))
2277 {
2278 if ( (((mode & SSL_SESS_CACHE_CLIENT)
David Benjamin041b58a2014-08-27 15:25:21 -04002279 ?s->initial_ctx->stats.sess_connect_good
2280 :s->initial_ctx->stats.sess_accept_good) & 0xff) == 0xff)
Adam Langley95c29f32014-06-20 12:00:00 -07002281 {
David Benjamin041b58a2014-08-27 15:25:21 -04002282 SSL_CTX_flush_sessions(s->initial_ctx,(unsigned long)time(NULL));
Adam Langley95c29f32014-06-20 12:00:00 -07002283 }
2284 }
2285 }
2286
Adam Langley95c29f32014-06-20 12:00:00 -07002287int SSL_get_error(const SSL *s,int i)
2288 {
2289 int reason;
2290 unsigned long l;
2291 BIO *bio;
2292
2293 if (i > 0) return(SSL_ERROR_NONE);
2294
2295 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2296 * etc, where we do encode the error */
2297 if ((l=ERR_peek_error()) != 0)
2298 {
2299 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2300 return(SSL_ERROR_SYSCALL);
2301 else
2302 return(SSL_ERROR_SSL);
2303 }
2304
Adam Langleyb2ce0582014-06-20 12:00:00 -07002305 if ((i < 0) && SSL_want_session(s))
2306 return(SSL_ERROR_PENDING_SESSION);
2307
Adam Langleydc9b1412014-06-20 12:00:00 -07002308 if ((i < 0) && SSL_want_certificate(s))
2309 return(SSL_ERROR_PENDING_CERTIFICATE);
2310
Adam Langley95c29f32014-06-20 12:00:00 -07002311 if ((i < 0) && SSL_want_read(s))
2312 {
2313 bio=SSL_get_rbio(s);
2314 if (BIO_should_read(bio))
2315 return(SSL_ERROR_WANT_READ);
2316 else if (BIO_should_write(bio))
2317 /* This one doesn't make too much sense ... We never try
2318 * to write to the rbio, and an application program where
2319 * rbio and wbio are separate couldn't even know what it
2320 * should wait for.
2321 * However if we ever set s->rwstate incorrectly
2322 * (so that we have SSL_want_read(s) instead of
2323 * SSL_want_write(s)) and rbio and wbio *are* the same,
2324 * this test works around that bug; so it might be safer
2325 * to keep it. */
2326 return(SSL_ERROR_WANT_WRITE);
2327 else if (BIO_should_io_special(bio))
2328 {
2329 reason=BIO_get_retry_reason(bio);
2330 if (reason == BIO_RR_CONNECT)
2331 return(SSL_ERROR_WANT_CONNECT);
2332 else if (reason == BIO_RR_ACCEPT)
2333 return(SSL_ERROR_WANT_ACCEPT);
2334 else
2335 return(SSL_ERROR_SYSCALL); /* unknown */
2336 }
2337 }
2338
2339 if ((i < 0) && SSL_want_write(s))
2340 {
2341 bio=SSL_get_wbio(s);
2342 if (BIO_should_write(bio))
2343 return(SSL_ERROR_WANT_WRITE);
2344 else if (BIO_should_read(bio))
2345 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2346 return(SSL_ERROR_WANT_READ);
2347 else if (BIO_should_io_special(bio))
2348 {
2349 reason=BIO_get_retry_reason(bio);
2350 if (reason == BIO_RR_CONNECT)
2351 return(SSL_ERROR_WANT_CONNECT);
2352 else if (reason == BIO_RR_ACCEPT)
2353 return(SSL_ERROR_WANT_ACCEPT);
2354 else
2355 return(SSL_ERROR_SYSCALL);
2356 }
2357 }
2358 if ((i < 0) && SSL_want_x509_lookup(s))
2359 {
2360 return(SSL_ERROR_WANT_X509_LOOKUP);
2361 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002362 if ((i < 0) && SSL_want_channel_id_lookup(s))
2363 {
2364 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2365 }
Adam Langley95c29f32014-06-20 12:00:00 -07002366
2367 if (i == 0)
2368 {
David Benjamin5e4f6e92014-11-17 03:23:24 -05002369 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2370 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
Adam Langley95c29f32014-06-20 12:00:00 -07002371 return(SSL_ERROR_ZERO_RETURN);
Adam Langley95c29f32014-06-20 12:00:00 -07002372 }
2373 return(SSL_ERROR_SYSCALL);
2374 }
2375
2376int SSL_do_handshake(SSL *s)
2377 {
2378 int ret=1;
2379
2380 if (s->handshake_func == NULL)
2381 {
2382 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2383 return(-1);
2384 }
2385
2386 s->method->ssl_renegotiate_check(s);
2387
2388 if (SSL_in_init(s) || SSL_in_before(s))
2389 {
2390 ret=s->handshake_func(s);
2391 }
2392 return(ret);
2393 }
2394
Adam Langley95c29f32014-06-20 12:00:00 -07002395void SSL_set_accept_state(SSL *s)
2396 {
2397 s->server=1;
2398 s->shutdown=0;
2399 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2400 s->handshake_func=s->method->ssl_accept;
2401 /* clear the current cipher */
2402 ssl_clear_cipher_ctx(s);
2403 ssl_clear_hash_ctx(&s->read_hash);
2404 ssl_clear_hash_ctx(&s->write_hash);
2405 }
2406
2407void SSL_set_connect_state(SSL *s)
2408 {
2409 s->server=0;
2410 s->shutdown=0;
2411 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2412 s->handshake_func=s->method->ssl_connect;
2413 /* clear the current cipher */
2414 ssl_clear_cipher_ctx(s);
2415 ssl_clear_hash_ctx(&s->read_hash);
2416 ssl_clear_hash_ctx(&s->write_hash);
2417 }
2418
2419int ssl_undefined_function(SSL *s)
2420 {
2421 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2422 return(0);
2423 }
2424
2425int ssl_undefined_void_function(void)
2426 {
2427 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2428 return(0);
2429 }
2430
2431int ssl_undefined_const_function(const SSL *s)
2432 {
2433 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2434 return(0);
2435 }
2436
Adam Langley0f4746e2014-08-13 12:26:32 -07002437static const char *ssl_get_version(int version)
Adam Langley95c29f32014-06-20 12:00:00 -07002438 {
Adam Langley0f4746e2014-08-13 12:26:32 -07002439 if (version == TLS1_2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002440 return("TLSv1.2");
Adam Langley0f4746e2014-08-13 12:26:32 -07002441 else if (version == TLS1_1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002442 return("TLSv1.1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002443 else if (version == TLS1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002444 return("TLSv1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002445 else if (version == SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002446 return("SSLv3");
Adam Langley95c29f32014-06-20 12:00:00 -07002447 else
2448 return("unknown");
2449 }
2450
Adam Langley0f4746e2014-08-13 12:26:32 -07002451const char *SSL_get_version(const SSL *s)
2452 {
2453 return ssl_get_version(s->version);
2454 }
2455
2456const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2457 {
2458 return ssl_get_version(sess->ssl_version);
2459 }
2460
Adam Langley95c29f32014-06-20 12:00:00 -07002461void ssl_clear_cipher_ctx(SSL *s)
2462 {
2463 if (s->enc_read_ctx != NULL)
2464 {
2465 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2466 OPENSSL_free(s->enc_read_ctx);
2467 s->enc_read_ctx=NULL;
2468 }
2469 if (s->enc_write_ctx != NULL)
2470 {
2471 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2472 OPENSSL_free(s->enc_write_ctx);
2473 s->enc_write_ctx=NULL;
2474 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002475 if (s->aead_read_ctx != NULL)
2476 {
2477 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2478 OPENSSL_free(s->aead_read_ctx);
2479 s->aead_read_ctx = NULL;
2480 }
2481 if (s->aead_write_ctx != NULL)
2482 {
2483 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2484 OPENSSL_free(s->aead_write_ctx);
2485 s->aead_write_ctx = NULL;
2486 }
Adam Langley95c29f32014-06-20 12:00:00 -07002487 }
2488
2489X509 *SSL_get_certificate(const SSL *s)
2490 {
2491 if (s->cert != NULL)
2492 return(s->cert->key->x509);
2493 else
2494 return(NULL);
2495 }
2496
2497EVP_PKEY *SSL_get_privatekey(const SSL *s)
2498 {
2499 if (s->cert != NULL)
2500 return(s->cert->key->privatekey);
2501 else
2502 return(NULL);
2503 }
2504
2505X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2506 {
2507 if (ctx->cert != NULL)
2508 return ctx->cert->key->x509;
2509 else
2510 return NULL;
2511 }
2512
2513EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2514 {
2515 if (ctx->cert != NULL)
2516 return ctx->cert->key->privatekey;
2517 else
2518 return NULL ;
2519 }
2520
2521const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2522 {
2523 if ((s->session != NULL) && (s->session->cipher != NULL))
2524 return(s->session->cipher);
2525 return(NULL);
2526 }
2527const void *SSL_get_current_compression(SSL *s)
2528 {
2529 return NULL;
2530 }
2531const void *SSL_get_current_expansion(SSL *s)
2532 {
2533 return NULL;
2534 }
2535
2536int ssl_init_wbio_buffer(SSL *s,int push)
2537 {
2538 BIO *bbio;
2539
2540 if (s->bbio == NULL)
2541 {
2542 bbio=BIO_new(BIO_f_buffer());
2543 if (bbio == NULL) return(0);
2544 s->bbio=bbio;
2545 }
2546 else
2547 {
2548 bbio=s->bbio;
2549 if (s->bbio == s->wbio)
2550 s->wbio=BIO_pop(s->wbio);
2551 }
2552 (void)BIO_reset(bbio);
2553/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2554 if (!BIO_set_read_buffer_size(bbio,1))
2555 {
2556 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2557 return(0);
2558 }
2559 if (push)
2560 {
2561 if (s->wbio != bbio)
2562 s->wbio=BIO_push(bbio,s->wbio);
2563 }
2564 else
2565 {
2566 if (s->wbio == bbio)
2567 s->wbio=BIO_pop(bbio);
2568 }
2569 return(1);
2570 }
2571
2572void ssl_free_wbio_buffer(SSL *s)
2573 {
2574 if (s->bbio == NULL) return;
2575
2576 if (s->bbio == s->wbio)
2577 {
2578 /* remove buffering */
2579 s->wbio=BIO_pop(s->wbio);
2580#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2581 assert(s->wbio != NULL);
2582#endif
2583 }
2584 BIO_free(s->bbio);
2585 s->bbio=NULL;
2586 }
2587
2588void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2589 {
2590 ctx->quiet_shutdown=mode;
2591 }
2592
2593int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2594 {
2595 return(ctx->quiet_shutdown);
2596 }
2597
2598void SSL_set_quiet_shutdown(SSL *s,int mode)
2599 {
2600 s->quiet_shutdown=mode;
2601 }
2602
2603int SSL_get_quiet_shutdown(const SSL *s)
2604 {
2605 return(s->quiet_shutdown);
2606 }
2607
2608void SSL_set_shutdown(SSL *s,int mode)
2609 {
2610 s->shutdown=mode;
2611 }
2612
2613int SSL_get_shutdown(const SSL *s)
2614 {
2615 return(s->shutdown);
2616 }
2617
2618int SSL_version(const SSL *s)
2619 {
2620 return(s->version);
2621 }
2622
2623SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2624 {
2625 return(ssl->ctx);
2626 }
2627
2628SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2629 {
2630 if (ssl->ctx == ctx)
2631 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002632 if (ctx == NULL)
2633 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002634 if (ssl->cert != NULL)
2635 ssl_cert_free(ssl->cert);
2636 ssl->cert = ssl_cert_dup(ctx->cert);
2637 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2638 if (ssl->ctx != NULL)
2639 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2640 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07002641
2642 ssl->sid_ctx_length = ctx->sid_ctx_length;
2643 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2644 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2645
Adam Langley95c29f32014-06-20 12:00:00 -07002646 return(ssl->ctx);
2647 }
2648
2649#ifndef OPENSSL_NO_STDIO
2650int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2651 {
2652 return(X509_STORE_set_default_paths(ctx->cert_store));
2653 }
2654
2655int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2656 const char *CApath)
2657 {
2658 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2659 }
2660#endif
2661
2662void SSL_set_info_callback(SSL *ssl,
2663 void (*cb)(const SSL *ssl,int type,int val))
2664 {
2665 ssl->info_callback=cb;
2666 }
2667
2668/* One compiler (Diab DCC) doesn't like argument names in returned
2669 function pointer. */
2670void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2671 {
2672 return ssl->info_callback;
2673 }
2674
2675int SSL_state(const SSL *ssl)
2676 {
2677 return(ssl->state);
2678 }
2679
2680void SSL_set_state(SSL *ssl, int state)
2681 {
2682 ssl->state = state;
2683 }
2684
2685void SSL_set_verify_result(SSL *ssl,long arg)
2686 {
2687 ssl->verify_result=arg;
2688 }
2689
2690long SSL_get_verify_result(const SSL *ssl)
2691 {
2692 return(ssl->verify_result);
2693 }
2694
2695int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2696 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2697 {
2698 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2699 new_func, dup_func, free_func);
2700 }
2701
2702int SSL_set_ex_data(SSL *s,int idx,void *arg)
2703 {
2704 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2705 }
2706
2707void *SSL_get_ex_data(const SSL *s,int idx)
2708 {
2709 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2710 }
2711
2712int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2713 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2714 {
2715 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2716 new_func, dup_func, free_func);
2717 }
2718
2719int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2720 {
2721 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2722 }
2723
2724void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2725 {
2726 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2727 }
2728
2729int ssl_ok(SSL *s)
2730 {
2731 return(1);
2732 }
2733
2734X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2735 {
2736 return(ctx->cert_store);
2737 }
2738
2739void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2740 {
2741 if (ctx->cert_store != NULL)
2742 X509_STORE_free(ctx->cert_store);
2743 ctx->cert_store=store;
2744 }
2745
2746int SSL_want(const SSL *s)
2747 {
2748 return(s->rwstate);
2749 }
2750
2751/*!
2752 * \brief Set the callback for generating temporary RSA keys.
2753 * \param ctx the SSL context.
2754 * \param cb the callback
2755 */
2756
Adam Langley95c29f32014-06-20 12:00:00 -07002757void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2758 int is_export,
2759 int keylength))
2760 {
2761 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2762 }
2763
2764void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2765 int is_export,
2766 int keylength))
2767 {
2768 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2769 }
Adam Langley95c29f32014-06-20 12:00:00 -07002770
2771#ifdef DOXYGEN
2772/*!
2773 * \brief The RSA temporary key callback function.
2774 * \param ssl the SSL session.
2775 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2776 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2777 * of the required key in bits.
2778 * \return the temporary RSA key.
2779 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2780 */
2781
2782RSA *cb(SSL *ssl,int is_export,int keylength)
2783 {}
2784#endif
2785
2786/*!
2787 * \brief Set the callback for generating temporary DH keys.
2788 * \param ctx the SSL context.
2789 * \param dh the callback
2790 */
2791
Adam Langley95c29f32014-06-20 12:00:00 -07002792void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2793 int keylength))
2794 {
2795 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2796 }
2797
2798void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2799 int keylength))
2800 {
2801 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2802 }
Adam Langley95c29f32014-06-20 12:00:00 -07002803
Adam Langley95c29f32014-06-20 12:00:00 -07002804void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2805 int keylength))
2806 {
2807 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2808 }
2809
2810void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2811 int keylength))
2812 {
2813 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2814 }
Adam Langley95c29f32014-06-20 12:00:00 -07002815
Adam Langley95c29f32014-06-20 12:00:00 -07002816int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2817 {
2818 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2819 {
2820 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2821 return 0;
2822 }
2823 if (ctx->psk_identity_hint != NULL)
2824 OPENSSL_free(ctx->psk_identity_hint);
2825 if (identity_hint != NULL)
2826 {
2827 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2828 if (ctx->psk_identity_hint == NULL)
2829 return 0;
2830 }
2831 else
2832 ctx->psk_identity_hint = NULL;
2833 return 1;
2834 }
2835
2836int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
2837 {
2838 if (s == NULL)
2839 return 0;
2840
Adam Langley95c29f32014-06-20 12:00:00 -07002841 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2842 {
2843 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2844 return 0;
2845 }
Adam Langley0289c732014-06-20 12:00:00 -07002846
David Benjamin688d8df2014-11-02 23:06:42 -05002847 /* Clear currently configured hint, if any. */
Adam Langley0289c732014-06-20 12:00:00 -07002848 if (s->psk_identity_hint != NULL)
2849 {
2850 OPENSSL_free(s->psk_identity_hint);
2851 s->psk_identity_hint = NULL;
2852 }
Adam Langley0289c732014-06-20 12:00:00 -07002853
Adam Langley95c29f32014-06-20 12:00:00 -07002854 if (identity_hint != NULL)
2855 {
David Benjamin688d8df2014-11-02 23:06:42 -05002856 s->psk_identity_hint = BUF_strdup(identity_hint);
2857 if (s->psk_identity_hint == NULL)
2858 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002859 }
Adam Langley95c29f32014-06-20 12:00:00 -07002860 return 1;
2861 }
2862
2863const char *SSL_get_psk_identity_hint(const SSL *s)
2864 {
Adam Langley0289c732014-06-20 12:00:00 -07002865 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002866 return NULL;
David Benjamin688d8df2014-11-02 23:06:42 -05002867 return s->psk_identity_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07002868 }
2869
2870const char *SSL_get_psk_identity(const SSL *s)
2871 {
2872 if (s == NULL || s->session == NULL)
2873 return NULL;
2874 return(s->session->psk_identity);
2875 }
2876
2877void SSL_set_psk_client_callback(SSL *s,
2878 unsigned int (*cb)(SSL *ssl, const char *hint,
2879 char *identity, unsigned int max_identity_len, unsigned char *psk,
2880 unsigned int max_psk_len))
2881 {
2882 s->psk_client_callback = cb;
2883 }
2884
2885void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
2886 unsigned int (*cb)(SSL *ssl, const char *hint,
2887 char *identity, unsigned int max_identity_len, unsigned char *psk,
2888 unsigned int max_psk_len))
2889 {
2890 ctx->psk_client_callback = cb;
2891 }
2892
2893void SSL_set_psk_server_callback(SSL *s,
2894 unsigned int (*cb)(SSL *ssl, const char *identity,
2895 unsigned char *psk, unsigned int max_psk_len))
2896 {
2897 s->psk_server_callback = cb;
2898 }
2899
2900void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
2901 unsigned int (*cb)(SSL *ssl, const char *identity,
2902 unsigned char *psk, unsigned int max_psk_len))
2903 {
2904 ctx->psk_server_callback = cb;
2905 }
Adam Langley95c29f32014-06-20 12:00:00 -07002906
2907void 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))
2908 {
2909 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2910 }
2911void 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))
2912 {
2913 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2914 }
2915
David Benjamin859ec3c2014-09-02 16:29:36 -04002916void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio)
2917 {
2918 if (ctx->keylog_bio != NULL)
2919 BIO_free(ctx->keylog_bio);
2920 ctx->keylog_bio = keylog_bio;
2921 }
2922
2923static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len)
2924 {
2925 static const char hextable[] = "0123456789abcdef";
2926 uint8_t *out;
2927 size_t i;
2928
2929 if (!CBB_add_space(cbb, &out, in_len * 2))
2930 {
2931 return 0;
2932 }
2933
2934 for (i = 0; i < in_len; i++)
2935 {
2936 *(out++) = (uint8_t)hextable[in[i] >> 4];
2937 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2938 }
2939 return 1;
2940 }
2941
2942int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
2943 const uint8_t *encrypted_premaster, size_t encrypted_premaster_len,
2944 const uint8_t *premaster, size_t premaster_len)
2945 {
2946 BIO *bio = ctx->keylog_bio;
2947 CBB cbb;
2948 uint8_t *out;
2949 size_t out_len;
2950 int ret;
2951
2952 if (bio == NULL)
2953 {
2954 return 1;
2955 }
2956
2957 if (encrypted_premaster_len < 8)
2958 {
2959 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange, ERR_R_INTERNAL_ERROR);
2960 return 0;
2961 }
2962
2963 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len*2 + 1))
2964 {
2965 return 0;
2966 }
2967 if (!CBB_add_bytes(&cbb, (const uint8_t*)"RSA ", 4) ||
2968 /* Only the first 8 bytes of the encrypted premaster secret are
2969 * logged. */
2970 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2971 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
2972 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2973 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
2974 !CBB_finish(&cbb, &out, &out_len))
2975 {
2976 CBB_cleanup(&cbb);
2977 return 0;
2978 }
2979
2980 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2981 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2982 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2983
2984 OPENSSL_free(out);
2985 return ret;
2986 }
2987
2988int ssl_ctx_log_master_secret(SSL_CTX *ctx,
2989 const uint8_t *client_random, size_t client_random_len,
2990 const uint8_t *master, size_t master_len)
2991 {
2992 BIO *bio = ctx->keylog_bio;
2993 CBB cbb;
2994 uint8_t *out;
2995 size_t out_len;
2996 int ret;
2997
2998 if (bio == NULL)
2999 {
3000 return 1;
3001 }
3002
3003 if (client_random_len != 32)
3004 {
3005 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
3006 return 0;
3007 }
3008
3009 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len*2 + 1))
3010 {
3011 return 0;
3012 }
3013 if (!CBB_add_bytes(&cbb, (const uint8_t*)"CLIENT_RANDOM ", 14) ||
3014 !cbb_add_hex(&cbb, client_random, 32) ||
3015 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3016 !cbb_add_hex(&cbb, master, master_len) ||
3017 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3018 !CBB_finish(&cbb, &out, &out_len))
3019 {
3020 CBB_cleanup(&cbb);
3021 return 0;
3022 }
3023
3024 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3025 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3026 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3027
3028 OPENSSL_free(out);
3029 return ret;
3030 }
3031
Adam Langleyadb739e2014-06-20 12:00:00 -07003032int SSL_cutthrough_complete(const SSL *s)
3033 {
3034 return (!s->server && /* cutthrough only applies to clients */
3035 !s->hit && /* full-handshake */
3036 s->version >= SSL3_VERSION &&
3037 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3038 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3039 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3040 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3041 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
David Benjamin7e3305e2014-07-28 14:52:32 -04003042 s->state == SSL3_ST_CR_CHANGE ||
Adam Langleyadb739e2014-06-20 12:00:00 -07003043 s->state == SSL3_ST_CR_FINISHED_A));
3044 }
3045
Adam Langley95f22882014-06-20 12:00:00 -07003046void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3047 size_t* ssl_session_size)
3048{
3049 *ssl_size = sizeof(SSL);
3050 *ssl_ctx_size = sizeof(SSL_CTX);
3051 *ssl_session_size = sizeof(SSL_SESSION);
3052}
3053
Adam Langleyadb739e2014-06-20 12:00:00 -07003054int ssl3_can_cutthrough(const SSL *s)
3055 {
3056 const SSL_CIPHER *c;
3057
3058 /* require a strong enough cipher */
3059 if (SSL_get_cipher_bits(s, NULL) < 128)
3060 return 0;
3061
3062 /* require ALPN or NPN extension */
David Benjamin457112e2014-08-27 14:35:09 -04003063 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen)
Adam Langleyadb739e2014-06-20 12:00:00 -07003064 {
3065 return 0;
3066 }
3067
3068 /* require a forward-secret cipher */
3069 c = SSL_get_current_cipher(s);
3070 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3071 c->algorithm_mkey != SSL_kEECDH))
3072 {
3073 return 0;
3074 }
3075
3076 return 1;
3077 }
3078
Adam Langley29707792014-06-20 12:00:00 -07003079/* ssl_get_max_version returns the maximum SSL/TLS version number supported by
3080 * |s|, or zero if all versions are disabled. */
3081int ssl_get_max_version(const SSL *s)
3082 {
3083 /* Only one version supported for DTLS. */
3084 if (s->version == DTLS1_VERSION)
3085 return DTLS1_VERSION;
3086
3087 if (!(s->options & SSL_OP_NO_TLSv1_2))
3088 return TLS1_2_VERSION;
3089 if (!(s->options & SSL_OP_NO_TLSv1_1))
3090 return TLS1_1_VERSION;
3091 if (!(s->options & SSL_OP_NO_TLSv1))
3092 return TLS1_VERSION;
3093 if (!(s->options & SSL_OP_NO_SSLv3))
3094 return SSL3_VERSION;
Adam Langley29707792014-06-20 12:00:00 -07003095 return 0;
3096 }
3097
Adam Langley95c29f32014-06-20 12:00:00 -07003098/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3099 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3100 * any. If EVP_MD pointer is passed, initializes ctx with this md
3101 * Returns newly allocated ctx;
3102 */
3103
3104EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3105{
3106 ssl_clear_hash_ctx(hash);
3107 *hash = EVP_MD_CTX_create();
David Benjaminae3e4872014-11-18 21:52:26 -05003108 if (md != NULL && *hash != NULL &&
3109 !EVP_DigestInit_ex(*hash, md, NULL))
Adam Langley69a01602014-11-17 17:26:55 -08003110 {
David Benjaminae3e4872014-11-18 21:52:26 -05003111 EVP_MD_CTX_destroy(*hash);
3112 *hash = NULL;
Adam Langley69a01602014-11-17 17:26:55 -08003113 }
Adam Langley95c29f32014-06-20 12:00:00 -07003114 return *hash;
3115}
3116void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3117{
3118
3119 if (*hash) EVP_MD_CTX_destroy(*hash);
3120 *hash=NULL;
3121}
3122
3123void SSL_set_debug(SSL *s, int debug)
3124 {
3125 s->debug = debug;
3126 }
3127
3128int SSL_cache_hit(SSL *s)
3129 {
3130 return s->hit;
3131 }
3132
3133int SSL_is_server(SSL *s)
3134 {
3135 return s->server;
3136 }