blob: 783e8aefb45c7dd452c7c467378ddc71dc9f9012 [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
David Benjaminf34a0092014-11-30 02:01:26 -0500213 /* SSL_clear may be called before or after the |s| is initialized in
214 * either accept or connect state. In the latter case, SSL_clear should
215 * preserve the half and reset |s->state| accordingly. */
216 if (s->handshake_func != NULL)
217 {
218 if (s->server)
219 SSL_set_accept_state(s);
220 else
221 SSL_set_connect_state(s);
222 }
223 else
224 {
225 assert(s->state == 0);
226 }
Adam Langley95c29f32014-06-20 12:00:00 -0700227
228 s->version=s->method->version;
229 s->client_version=s->version;
230 s->rwstate=SSL_NOTHING;
231 s->rstate=SSL_ST_READ_HEADER;
232#if 0
233 s->read_ahead=s->ctx->read_ahead;
234#endif
235
236 if (s->init_buf != NULL)
237 {
238 BUF_MEM_free(s->init_buf);
239 s->init_buf=NULL;
240 }
241
242 ssl_clear_cipher_ctx(s);
243 ssl_clear_hash_ctx(&s->read_hash);
244 ssl_clear_hash_ctx(&s->write_hash);
245
246 s->first_packet=0;
247
David Benjaminbb15e3d2014-11-29 16:03:41 -0500248 s->method->ssl_clear(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700249 return(1);
250 }
251
252/** Used to change an SSL_CTXs default SSL method type */
253int SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth)
254 {
255 STACK_OF(SSL_CIPHER) *sk;
256
257 ctx->method=meth;
258
Adam Langley858a88d2014-06-20 12:00:00 -0700259 sk=ssl_create_cipher_list(
260 ctx->method, &ctx->cipher_list, &ctx->cipher_list_by_id,
David Benjamin5e4f6e92014-11-17 03:23:24 -0500261 SSL_DEFAULT_CIPHER_LIST,
Adam Langley858a88d2014-06-20 12:00:00 -0700262 ctx->cert);
263
Adam Langley95c29f32014-06-20 12:00:00 -0700264 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
265 {
266 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_ssl_version, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
267 return(0);
268 }
269 return(1);
270 }
271
272SSL *SSL_new(SSL_CTX *ctx)
273 {
274 SSL *s;
275
276 if (ctx == NULL)
277 {
278 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
279 return(NULL);
280 }
281 if (ctx->method == NULL)
282 {
283 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
284 return(NULL);
285 }
286
287 s=(SSL *)OPENSSL_malloc(sizeof(SSL));
288 if (s == NULL) goto err;
289 memset(s,0,sizeof(SSL));
290
291 s->options=ctx->options;
292 s->mode=ctx->mode;
293 s->max_cert_list=ctx->max_cert_list;
294
295 if (ctx->cert != NULL)
296 {
297 /* Earlier library versions used to copy the pointer to
298 * the CERT, not its contents; only when setting new
299 * parameters for the per-SSL copy, ssl_cert_new would be
300 * called (and the direct reference to the per-SSL_CTX
301 * settings would be lost, but those still were indirectly
302 * accessed for various purposes, and for that reason they
303 * used to be known as s->ctx->default_cert).
304 * Now we don't look at the SSL_CTX's CERT after having
305 * duplicated it once. */
306
307 s->cert = ssl_cert_dup(ctx->cert);
308 if (s->cert == NULL)
309 goto err;
310 }
311 else
312 s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
313
314 s->read_ahead=ctx->read_ahead;
315 s->msg_callback=ctx->msg_callback;
316 s->msg_callback_arg=ctx->msg_callback_arg;
317 s->verify_mode=ctx->verify_mode;
318#if 0
319 s->verify_depth=ctx->verify_depth;
320#endif
321 s->sid_ctx_length=ctx->sid_ctx_length;
322 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
323 memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
324 s->verify_callback=ctx->default_verify_callback;
325 s->generate_session_id=ctx->generate_session_id;
326
327 s->param = X509_VERIFY_PARAM_new();
328 if (!s->param)
329 goto err;
330 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
331#if 0
332 s->purpose = ctx->purpose;
333 s->trust = ctx->trust;
334#endif
335 s->quiet_shutdown=ctx->quiet_shutdown;
336 s->max_send_fragment = ctx->max_send_fragment;
337
338 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
339 s->ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700340 s->tlsext_debug_cb = 0;
341 s->tlsext_debug_arg = NULL;
342 s->tlsext_ticket_expected = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700343 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
344 s->initial_ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700345 if (ctx->tlsext_ecpointformatlist)
346 {
347 s->tlsext_ecpointformatlist =
348 BUF_memdup(ctx->tlsext_ecpointformatlist,
349 ctx->tlsext_ecpointformatlist_length);
350 if (!s->tlsext_ecpointformatlist)
351 goto err;
352 s->tlsext_ecpointformatlist_length =
353 ctx->tlsext_ecpointformatlist_length;
354 }
355 if (ctx->tlsext_ellipticcurvelist)
356 {
357 s->tlsext_ellipticcurvelist =
358 BUF_memdup(ctx->tlsext_ellipticcurvelist,
David Benjamin072334d2014-07-13 16:24:27 -0400359 ctx->tlsext_ellipticcurvelist_length * 2);
Adam Langley95c29f32014-06-20 12:00:00 -0700360 if (!s->tlsext_ellipticcurvelist)
361 goto err;
362 s->tlsext_ellipticcurvelist_length =
363 ctx->tlsext_ellipticcurvelist_length;
364 }
Adam Langley95c29f32014-06-20 12:00:00 -0700365 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700366
367 if (s->ctx->alpn_client_proto_list)
368 {
David Benjamin072c9532014-07-26 11:44:25 -0400369 s->alpn_client_proto_list = BUF_memdup(
370 s->ctx->alpn_client_proto_list,
371 s->ctx->alpn_client_proto_list_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700372 if (s->alpn_client_proto_list == NULL)
373 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700374 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
375 }
Adam Langley95c29f32014-06-20 12:00:00 -0700376
377 s->verify_result=X509_V_OK;
378
379 s->method=ctx->method;
380
381 if (!s->method->ssl_new(s))
382 goto err;
383
384 s->references=1;
Adam Langley95c29f32014-06-20 12:00:00 -0700385
386 SSL_clear(s);
387
388 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
389
Adam Langley0289c732014-06-20 12:00:00 -0700390 s->psk_identity_hint = NULL;
391 if (ctx->psk_identity_hint)
392 {
393 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
394 if (s->psk_identity_hint == NULL)
395 goto err;
396 }
Adam Langley95c29f32014-06-20 12:00:00 -0700397 s->psk_client_callback=ctx->psk_client_callback;
398 s->psk_server_callback=ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700399
David Benjaminff42cc12014-11-26 00:16:14 -0500400 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
401 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200402
Adam Langley95c29f32014-06-20 12:00:00 -0700403 return(s);
404err:
405 if (s != NULL)
406 {
407 if (s->cert != NULL)
408 ssl_cert_free(s->cert);
409 if (s->ctx != NULL)
410 SSL_CTX_free(s->ctx); /* decrement reference count */
411 OPENSSL_free(s);
412 }
413 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
414 return(NULL);
415 }
416
417int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
418 unsigned int sid_ctx_len)
419 {
420 if(sid_ctx_len > sizeof ctx->sid_ctx)
421 {
422 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
423 return 0;
424 }
425 ctx->sid_ctx_length=sid_ctx_len;
426 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
427
428 return 1;
429 }
430
431int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
432 unsigned int sid_ctx_len)
433 {
434 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
435 {
436 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
437 return 0;
438 }
439 ssl->sid_ctx_length=sid_ctx_len;
440 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
441
442 return 1;
443 }
444
445int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
446 {
447 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
448 ctx->generate_session_id = cb;
449 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
450 return 1;
451 }
452
453int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
454 {
455 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
456 ssl->generate_session_id = cb;
457 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
458 return 1;
459 }
460
461int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
462 unsigned int id_len)
463 {
464 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
465 * we can "construct" a session to give us the desired check - ie. to
466 * find if there's a session in the hash table that would conflict with
467 * any new session built out of this id/id_len and the ssl_version in
468 * use by this SSL. */
469 SSL_SESSION r, *p;
470
471 if(id_len > sizeof r.session_id)
472 return 0;
473
474 r.ssl_version = ssl->version;
475 r.session_id_length = id_len;
476 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700477
478 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
479 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
480 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
481 return (p != NULL);
482 }
483
484int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
485 {
486 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
487 }
488
489int SSL_set_purpose(SSL *s, int purpose)
490 {
491 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
492 }
493
494int SSL_CTX_set_trust(SSL_CTX *s, int trust)
495 {
496 return X509_VERIFY_PARAM_set_trust(s->param, trust);
497 }
498
499int SSL_set_trust(SSL *s, int trust)
500 {
501 return X509_VERIFY_PARAM_set_trust(s->param, trust);
502 }
503
504int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
505 {
506 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
507 }
508
509int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
510 {
511 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
512 }
513
Adam Langley858a88d2014-06-20 12:00:00 -0700514void ssl_cipher_preference_list_free(
515 struct ssl_cipher_preference_list_st *cipher_list)
516 {
517 sk_SSL_CIPHER_free(cipher_list->ciphers);
518 OPENSSL_free(cipher_list->in_group_flags);
519 OPENSSL_free(cipher_list);
520 }
521
522struct ssl_cipher_preference_list_st*
523ssl_cipher_preference_list_dup(
524 struct ssl_cipher_preference_list_st *cipher_list)
525 {
526 struct ssl_cipher_preference_list_st* ret = NULL;
527 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
528
529 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
530 if (!ret)
531 goto err;
532 ret->ciphers = NULL;
533 ret->in_group_flags = NULL;
534 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
535 if (!ret->ciphers)
536 goto err;
David Benjamin072c9532014-07-26 11:44:25 -0400537 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
Adam Langley858a88d2014-06-20 12:00:00 -0700538 if (!ret->in_group_flags)
539 goto err;
Adam Langley858a88d2014-06-20 12:00:00 -0700540 return ret;
541
542err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400543 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700544 sk_SSL_CIPHER_free(ret->ciphers);
545 if (ret)
546 OPENSSL_free(ret);
547 return NULL;
548 }
549
550struct ssl_cipher_preference_list_st*
551ssl_cipher_preference_list_from_ciphers(STACK_OF(SSL_CIPHER) *ciphers)
552 {
553 struct ssl_cipher_preference_list_st* ret = NULL;
554 size_t n = sk_SSL_CIPHER_num(ciphers);
555
556 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
557 if (!ret)
558 goto err;
559 ret->ciphers = NULL;
560 ret->in_group_flags = NULL;
561 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
562 if (!ret->ciphers)
563 goto err;
564 ret->in_group_flags = OPENSSL_malloc(n);
565 if (!ret->in_group_flags)
566 goto err;
567 memset(ret->in_group_flags, 0, n);
568 return ret;
569
570err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400571 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700572 sk_SSL_CIPHER_free(ret->ciphers);
573 if (ret)
574 OPENSSL_free(ret);
575 return NULL;
576 }
577
Adam Langley95c29f32014-06-20 12:00:00 -0700578X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
579 {
580 return ctx->param;
581 }
582
583X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
584 {
585 return ssl->param;
586 }
587
588void SSL_certs_clear(SSL *s)
589 {
590 ssl_cert_clear_certs(s->cert);
591 }
592
593void SSL_free(SSL *s)
594 {
595 int i;
596
597 if(s == NULL)
598 return;
599
600 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
601#ifdef REF_PRINT
602 REF_PRINT("SSL",s);
603#endif
604 if (i > 0) return;
605#ifdef REF_CHECK
606 if (i < 0)
607 {
608 fprintf(stderr,"SSL_free, bad reference count\n");
609 abort(); /* ok */
610 }
611#endif
612
613 if (s->param)
614 X509_VERIFY_PARAM_free(s->param);
615
616 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
617
618 if (s->bbio != NULL)
619 {
620 /* If the buffering BIO is in place, pop it off */
621 if (s->bbio == s->wbio)
622 {
623 s->wbio=BIO_pop(s->wbio);
624 }
625 BIO_free(s->bbio);
626 s->bbio=NULL;
627 }
628 if (s->rbio != NULL)
629 BIO_free_all(s->rbio);
630 if ((s->wbio != NULL) && (s->wbio != s->rbio))
631 BIO_free_all(s->wbio);
632
633 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
634
635 /* add extra stuff */
Adam Langley858a88d2014-06-20 12:00:00 -0700636 if (s->cipher_list != NULL)
637 ssl_cipher_preference_list_free(s->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -0700638 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
639
640 /* Make the next call work :-) */
641 if (s->session != NULL)
642 {
643 ssl_clear_bad_session(s);
644 SSL_SESSION_free(s->session);
645 }
646
647 ssl_clear_cipher_ctx(s);
648 ssl_clear_hash_ctx(&s->read_hash);
649 ssl_clear_hash_ctx(&s->write_hash);
650
651 if (s->cert != NULL) ssl_cert_free(s->cert);
652 /* Free up if allocated */
653
Adam Langley95c29f32014-06-20 12:00:00 -0700654 if (s->tlsext_hostname)
655 OPENSSL_free(s->tlsext_hostname);
656 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700657 if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
658 if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -0700659 if (s->alpn_client_proto_list)
660 OPENSSL_free(s->alpn_client_proto_list);
Adam Langley1258b6a2014-06-20 12:00:00 -0700661 if (s->tlsext_channel_id_private)
662 EVP_PKEY_free(s->tlsext_channel_id_private);
Adam Langley95c29f32014-06-20 12:00:00 -0700663
Adam Langley0289c732014-06-20 12:00:00 -0700664 if (s->psk_identity_hint)
665 OPENSSL_free(s->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -0700666
Adam Langley95c29f32014-06-20 12:00:00 -0700667 if (s->client_CA != NULL)
668 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
669
670 if (s->method != NULL) s->method->ssl_free(s);
671
672 if (s->ctx) SSL_CTX_free(s->ctx);
673
Adam Langley95c29f32014-06-20 12:00:00 -0700674 if (s->next_proto_negotiated)
675 OPENSSL_free(s->next_proto_negotiated);
Adam Langley95c29f32014-06-20 12:00:00 -0700676
677 if (s->srtp_profiles)
678 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
679
Adam Langley95c29f32014-06-20 12:00:00 -0700680 OPENSSL_free(s);
681 }
682
683void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
684 {
685 /* If the output buffering BIO is still in place, remove it
686 */
687 if (s->bbio != NULL)
688 {
689 if (s->wbio == s->bbio)
690 {
691 s->wbio=s->wbio->next_bio;
692 s->bbio->next_bio=NULL;
693 }
694 }
695 if ((s->rbio != NULL) && (s->rbio != rbio))
696 BIO_free_all(s->rbio);
697 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
698 BIO_free_all(s->wbio);
699 s->rbio=rbio;
700 s->wbio=wbio;
701 }
702
703BIO *SSL_get_rbio(const SSL *s)
704 { return(s->rbio); }
705
706BIO *SSL_get_wbio(const SSL *s)
707 { return(s->wbio); }
708
709int SSL_get_fd(const SSL *s)
710 {
711 return(SSL_get_rfd(s));
712 }
713
714int SSL_get_rfd(const SSL *s)
715 {
716 int ret= -1;
717 BIO *b,*r;
718
719 b=SSL_get_rbio(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
726int SSL_get_wfd(const SSL *s)
727 {
728 int ret= -1;
729 BIO *b,*r;
730
731 b=SSL_get_wbio(s);
732 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
733 if (r != NULL)
734 BIO_get_fd(r,&ret);
735 return(ret);
736 }
737
738#ifndef OPENSSL_NO_SOCK
739int SSL_set_fd(SSL *s,int fd)
740 {
741 int ret=0;
742 BIO *bio=NULL;
743
744 bio=BIO_new(BIO_s_fd());
745
746 if (bio == NULL)
747 {
748 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
749 goto err;
750 }
751 BIO_set_fd(bio,fd,BIO_NOCLOSE);
752 SSL_set_bio(s,bio,bio);
753 ret=1;
754err:
755 return(ret);
756 }
757
758int SSL_set_wfd(SSL *s,int fd)
759 {
760 int ret=0;
761 BIO *bio=NULL;
762
763 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_FD)
764 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
765 {
766 bio=BIO_new(BIO_s_fd());
767
768 if (bio == NULL)
769 {
770 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
771 goto err;
772 }
773 BIO_set_fd(bio,fd,BIO_NOCLOSE);
774 SSL_set_bio(s,SSL_get_rbio(s),bio);
775 }
776 else
777 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
778 ret=1;
779err:
780 return(ret);
781 }
782
783int SSL_set_rfd(SSL *s,int fd)
784 {
785 int ret=0;
786 BIO *bio=NULL;
787
788 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_FD)
789 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
790 {
791 bio=BIO_new(BIO_s_fd());
792
793 if (bio == NULL)
794 {
795 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
796 goto err;
797 }
798 BIO_set_fd(bio,fd,BIO_NOCLOSE);
799 SSL_set_bio(s,bio,SSL_get_wbio(s));
800 }
801 else
802 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
803 ret=1;
804err:
805 return(ret);
806 }
807#endif
808
809
810/* return length of latest Finished message we sent, copy to 'buf' */
811size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
812 {
813 size_t ret = 0;
814
815 if (s->s3 != NULL)
816 {
817 ret = s->s3->tmp.finish_md_len;
818 if (count > ret)
819 count = ret;
820 memcpy(buf, s->s3->tmp.finish_md, count);
821 }
822 return ret;
823 }
824
825/* return length of latest Finished message we expected, copy to 'buf' */
826size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
827 {
828 size_t ret = 0;
829
830 if (s->s3 != NULL)
831 {
832 ret = s->s3->tmp.peer_finish_md_len;
833 if (count > ret)
834 count = ret;
835 memcpy(buf, s->s3->tmp.peer_finish_md, count);
836 }
837 return ret;
838 }
839
840
841int SSL_get_verify_mode(const SSL *s)
842 {
843 return(s->verify_mode);
844 }
845
846int SSL_get_verify_depth(const SSL *s)
847 {
848 return X509_VERIFY_PARAM_get_depth(s->param);
849 }
850
851int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
852 {
853 return(s->verify_callback);
854 }
855
856int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
857 {
858 return(ctx->verify_mode);
859 }
860
861int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
862 {
863 return X509_VERIFY_PARAM_get_depth(ctx->param);
864 }
865
866int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
867 {
868 return(ctx->default_verify_callback);
869 }
870
871void SSL_set_verify(SSL *s,int mode,
872 int (*callback)(int ok,X509_STORE_CTX *ctx))
873 {
874 s->verify_mode=mode;
875 if (callback != NULL)
876 s->verify_callback=callback;
877 }
878
879void SSL_set_verify_depth(SSL *s,int depth)
880 {
881 X509_VERIFY_PARAM_set_depth(s->param, depth);
882 }
883
884void SSL_set_read_ahead(SSL *s,int yes)
885 {
886 s->read_ahead=yes;
887 }
888
889int SSL_get_read_ahead(const SSL *s)
890 {
891 return(s->read_ahead);
892 }
893
894int SSL_pending(const SSL *s)
895 {
896 /* SSL_pending cannot work properly if read-ahead is enabled
897 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
898 * and it is impossible to fix since SSL_pending cannot report
899 * errors that may be observed while scanning the new data.
900 * (Note that SSL_pending() is often used as a boolean value,
901 * so we'd better not return -1.)
902 */
903 return(s->method->ssl_pending(s));
904 }
905
906X509 *SSL_get_peer_certificate(const SSL *s)
907 {
908 X509 *r;
909
910 if ((s == NULL) || (s->session == NULL))
911 r=NULL;
912 else
913 r=s->session->peer;
914
David Benjamin150c6172014-08-05 22:22:49 -0400915 if (r == NULL)
916 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700917
David Benjamin150c6172014-08-05 22:22:49 -0400918 return X509_up_ref(r);
Adam Langley95c29f32014-06-20 12:00:00 -0700919 }
920
921STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
922 {
923 STACK_OF(X509) *r;
924
925 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
926 r=NULL;
927 else
928 r=s->session->sess_cert->cert_chain;
929
930 /* If we are a client, cert_chain includes the peer's own
931 * certificate; if we are a server, it does not. */
932
933 return(r);
934 }
935
Adam Langley95c29f32014-06-20 12:00:00 -0700936/* Fix this so it checks all the valid key/cert options */
937int SSL_CTX_check_private_key(const SSL_CTX *ctx)
938 {
939 if ( (ctx == NULL) ||
940 (ctx->cert == NULL) ||
941 (ctx->cert->key->x509 == NULL))
942 {
943 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
944 return(0);
945 }
946 if (ctx->cert->key->privatekey == NULL)
947 {
948 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
949 return(0);
950 }
951 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
952 }
953
954/* Fix this function so that it takes an optional type parameter */
955int SSL_check_private_key(const SSL *ssl)
956 {
957 if (ssl == NULL)
958 {
959 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
960 return(0);
961 }
962 if (ssl->cert == NULL)
963 {
964 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
965 return 0;
966 }
967 if (ssl->cert->key->x509 == NULL)
968 {
969 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
970 return(0);
971 }
972 if (ssl->cert->key->privatekey == NULL)
973 {
974 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
975 return(0);
976 }
977 return(X509_check_private_key(ssl->cert->key->x509,
978 ssl->cert->key->privatekey));
979 }
980
981int SSL_accept(SSL *s)
982 {
983 if (s->handshake_func == 0)
984 /* Not properly initialized yet */
985 SSL_set_accept_state(s);
986
David Benjamin0b145c22014-11-26 20:10:09 -0500987 if (s->handshake_func != s->method->ssl_accept)
988 {
989 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
990 return -1;
991 }
992
993 return s->handshake_func(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700994 }
995
996int SSL_connect(SSL *s)
997 {
998 if (s->handshake_func == 0)
999 /* Not properly initialized yet */
1000 SSL_set_connect_state(s);
1001
David Benjamin0b145c22014-11-26 20:10:09 -05001002 if (s->handshake_func != s->method->ssl_connect)
1003 {
1004 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
1005 return -1;
1006 }
1007
1008 return s->handshake_func(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001009 }
1010
1011long SSL_get_default_timeout(const SSL *s)
1012 {
David Benjaminf4501342014-08-14 17:24:37 -04001013 return SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001014 }
1015
1016int SSL_read(SSL *s,void *buf,int num)
1017 {
1018 if (s->handshake_func == 0)
1019 {
1020 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
1021 return -1;
1022 }
1023
1024 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1025 {
1026 s->rwstate=SSL_NOTHING;
1027 return(0);
1028 }
1029 return(s->method->ssl_read(s,buf,num));
1030 }
1031
1032int SSL_peek(SSL *s,void *buf,int num)
1033 {
1034 if (s->handshake_func == 0)
1035 {
1036 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
1037 return -1;
1038 }
1039
1040 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1041 {
1042 return(0);
1043 }
1044 return(s->method->ssl_peek(s,buf,num));
1045 }
1046
1047int SSL_write(SSL *s,const void *buf,int num)
1048 {
1049 if (s->handshake_func == 0)
1050 {
1051 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
1052 return -1;
1053 }
1054
1055 if (s->shutdown & SSL_SENT_SHUTDOWN)
1056 {
1057 s->rwstate=SSL_NOTHING;
1058 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
1059 return(-1);
1060 }
1061 return(s->method->ssl_write(s,buf,num));
1062 }
1063
1064int SSL_shutdown(SSL *s)
1065 {
1066 /* Note that this function behaves differently from what one might
1067 * expect. Return values are 0 for no success (yet),
1068 * 1 for success; but calling it once is usually not enough,
1069 * even if blocking I/O is used (see ssl3_shutdown).
1070 */
1071
1072 if (s->handshake_func == 0)
1073 {
1074 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1075 return -1;
1076 }
1077
Adam Langleyb2cb0ec2014-09-02 14:28:49 -07001078 if (!SSL_in_init(s))
Adam Langley95c29f32014-06-20 12:00:00 -07001079 return(s->method->ssl_shutdown(s));
1080 else
1081 return(1);
1082 }
1083
1084int SSL_renegotiate(SSL *s)
1085 {
1086 if (s->renegotiate == 0)
1087 s->renegotiate=1;
1088
1089 s->new_session=1;
1090
1091 return(s->method->ssl_renegotiate(s));
1092 }
1093
1094int SSL_renegotiate_abbreviated(SSL *s)
1095 {
1096 if (s->renegotiate == 0)
1097 s->renegotiate=1;
1098
1099 s->new_session=0;
1100
1101 return(s->method->ssl_renegotiate(s));
1102 }
1103
1104int SSL_renegotiate_pending(SSL *s)
1105 {
1106 /* becomes true when negotiation is requested;
1107 * false again once a handshake has finished */
1108 return (s->renegotiate != 0);
1109 }
1110
1111long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1112 {
1113 long l;
1114
1115 switch (cmd)
1116 {
1117 case SSL_CTRL_GET_READ_AHEAD:
1118 return(s->read_ahead);
1119 case SSL_CTRL_SET_READ_AHEAD:
1120 l=s->read_ahead;
1121 s->read_ahead=larg;
1122 return(l);
1123
1124 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1125 s->msg_callback_arg = parg;
1126 return 1;
1127
1128 case SSL_CTRL_OPTIONS:
1129 return(s->options|=larg);
1130 case SSL_CTRL_CLEAR_OPTIONS:
1131 return(s->options&=~larg);
1132 case SSL_CTRL_MODE:
1133 return(s->mode|=larg);
1134 case SSL_CTRL_CLEAR_MODE:
1135 return(s->mode &=~larg);
1136 case SSL_CTRL_GET_MAX_CERT_LIST:
1137 return(s->max_cert_list);
1138 case SSL_CTRL_SET_MAX_CERT_LIST:
1139 l=s->max_cert_list;
1140 s->max_cert_list=larg;
1141 return(l);
1142 case SSL_CTRL_SET_MTU:
Adam Langley95c29f32014-06-20 12:00:00 -07001143 if (larg < (long)dtls1_min_mtu())
1144 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001145 if (SSL_IS_DTLS(s))
1146 {
1147 s->d1->mtu = larg;
1148 return larg;
1149 }
1150 return 0;
1151 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1152 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1153 return 0;
1154 s->max_send_fragment = larg;
1155 return 1;
1156 case SSL_CTRL_GET_RI_SUPPORT:
1157 if (s->s3)
1158 return s->s3->send_connection_binding;
1159 else return 0;
1160 case SSL_CTRL_CERT_FLAGS:
1161 return(s->cert->cert_flags|=larg);
1162 case SSL_CTRL_CLEAR_CERT_FLAGS:
1163 return(s->cert->cert_flags &=~larg);
1164
1165 case SSL_CTRL_GET_RAW_CIPHERLIST:
1166 if (parg)
1167 {
1168 if (s->cert->ciphers_raw == NULL)
1169 return 0;
1170 *(unsigned char **)parg = s->cert->ciphers_raw;
1171 return (int)s->cert->ciphers_rawlen;
1172 }
1173 else
David Benjamin39482a12014-07-20 13:30:15 -04001174 {
1175 /* Passing a NULL |parg| returns the size of a single
1176 * cipher suite value. */
1177 return 2;
1178 }
Adam Langley95c29f32014-06-20 12:00:00 -07001179 default:
1180 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1181 }
1182 }
1183
1184long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1185 {
1186 switch(cmd)
1187 {
1188 case SSL_CTRL_SET_MSG_CALLBACK:
1189 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1190 return 1;
1191
1192 default:
1193 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1194 }
1195 }
1196
1197LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1198 {
1199 return ctx->sessions;
1200 }
1201
1202long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1203 {
1204 long l;
1205
1206 switch (cmd)
1207 {
1208 case SSL_CTRL_GET_READ_AHEAD:
1209 return(ctx->read_ahead);
1210 case SSL_CTRL_SET_READ_AHEAD:
1211 l=ctx->read_ahead;
1212 ctx->read_ahead=larg;
1213 return(l);
1214
1215 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1216 ctx->msg_callback_arg = parg;
1217 return 1;
1218
1219 case SSL_CTRL_GET_MAX_CERT_LIST:
1220 return(ctx->max_cert_list);
1221 case SSL_CTRL_SET_MAX_CERT_LIST:
1222 l=ctx->max_cert_list;
1223 ctx->max_cert_list=larg;
1224 return(l);
1225
1226 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1227 l=ctx->session_cache_size;
1228 ctx->session_cache_size=larg;
1229 return(l);
1230 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1231 return(ctx->session_cache_size);
1232 case SSL_CTRL_SET_SESS_CACHE_MODE:
1233 l=ctx->session_cache_mode;
1234 ctx->session_cache_mode=larg;
1235 return(l);
1236 case SSL_CTRL_GET_SESS_CACHE_MODE:
1237 return(ctx->session_cache_mode);
1238
1239 case SSL_CTRL_SESS_NUMBER:
1240 return(lh_SSL_SESSION_num_items(ctx->sessions));
1241 case SSL_CTRL_SESS_CONNECT:
1242 return(ctx->stats.sess_connect);
1243 case SSL_CTRL_SESS_CONNECT_GOOD:
1244 return(ctx->stats.sess_connect_good);
1245 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1246 return(ctx->stats.sess_connect_renegotiate);
1247 case SSL_CTRL_SESS_ACCEPT:
1248 return(ctx->stats.sess_accept);
1249 case SSL_CTRL_SESS_ACCEPT_GOOD:
1250 return(ctx->stats.sess_accept_good);
1251 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1252 return(ctx->stats.sess_accept_renegotiate);
1253 case SSL_CTRL_SESS_HIT:
1254 return(ctx->stats.sess_hit);
1255 case SSL_CTRL_SESS_CB_HIT:
1256 return(ctx->stats.sess_cb_hit);
1257 case SSL_CTRL_SESS_MISSES:
1258 return(ctx->stats.sess_miss);
1259 case SSL_CTRL_SESS_TIMEOUTS:
1260 return(ctx->stats.sess_timeout);
1261 case SSL_CTRL_SESS_CACHE_FULL:
1262 return(ctx->stats.sess_cache_full);
1263 case SSL_CTRL_OPTIONS:
1264 return(ctx->options|=larg);
1265 case SSL_CTRL_CLEAR_OPTIONS:
1266 return(ctx->options&=~larg);
1267 case SSL_CTRL_MODE:
1268 return(ctx->mode|=larg);
1269 case SSL_CTRL_CLEAR_MODE:
1270 return(ctx->mode&=~larg);
1271 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1272 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1273 return 0;
1274 ctx->max_send_fragment = larg;
1275 return 1;
1276 case SSL_CTRL_CERT_FLAGS:
1277 return(ctx->cert->cert_flags|=larg);
1278 case SSL_CTRL_CLEAR_CERT_FLAGS:
1279 return(ctx->cert->cert_flags &=~larg);
1280 default:
1281 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1282 }
1283 }
1284
1285long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1286 {
1287 switch(cmd)
1288 {
1289 case SSL_CTRL_SET_MSG_CALLBACK:
1290 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1291 return 1;
1292
1293 default:
1294 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1295 }
1296 }
1297
1298int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1299 {
1300 long l;
1301 const SSL_CIPHER *a = in_a;
1302 const SSL_CIPHER *b = in_b;
1303 const long a_id = a->id;
1304 const long b_id = b->id;
1305
1306 l = a_id - b_id;
1307 if (l == 0L)
1308 return(0);
1309 else
1310 return((l > 0)?1:-1);
1311 }
1312
1313int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1314 {
1315 long l;
1316 const long a_id = (*ap)->id;
1317 const long b_id = (*bp)->id;
1318
1319 l = a_id - b_id;
1320 if (l == 0)
1321 return(0);
1322 else
1323 return((l > 0)?1:-1);
1324 }
1325
1326/** return a STACK of the ciphers available for the SSL and in order of
1327 * preference */
1328STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1329 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001330 if (s == NULL)
1331 return NULL;
1332
1333 if (s->cipher_list != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001334 {
Adam Langley858a88d2014-06-20 12:00:00 -07001335 return(s->cipher_list->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -07001336 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001337
1338 if (s->version >= TLS1_1_VERSION)
1339 {
1340 if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001341 return s->ctx->cipher_list_tls11->ciphers;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001342 }
1343
1344 if ((s->ctx != NULL) &&
1345 (s->ctx->cipher_list != NULL))
1346 {
Adam Langley858a88d2014-06-20 12:00:00 -07001347 return(s->ctx->cipher_list->ciphers);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001348 }
1349
Adam Langley95c29f32014-06-20 12:00:00 -07001350 return(NULL);
1351 }
1352
1353/** return a STACK of the ciphers available for the SSL and in order of
1354 * algorithm id */
1355STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1356 {
1357 if (s != NULL)
1358 {
1359 if (s->cipher_list_by_id != NULL)
1360 {
1361 return(s->cipher_list_by_id);
1362 }
1363 else if ((s->ctx != NULL) &&
1364 (s->ctx->cipher_list_by_id != NULL))
1365 {
1366 return(s->ctx->cipher_list_by_id);
1367 }
1368 }
1369 return(NULL);
1370 }
1371
1372/** The old interface to get the same thing as SSL_get_ciphers() */
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001373const char *SSL_get_cipher_list(const SSL *s, int n)
Adam Langley95c29f32014-06-20 12:00:00 -07001374 {
David Benjamin6f260012014-08-15 13:49:12 -04001375 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001376 STACK_OF(SSL_CIPHER) *sk;
1377
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001378 if (s == NULL)
1379 return NULL;
1380 sk = SSL_get_ciphers(s);
1381 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk))
1382 return NULL;
1383 c = sk_SSL_CIPHER_value(sk, n);
1384 if (c == NULL)
1385 return NULL;
1386 return c->name;
Adam Langley95c29f32014-06-20 12:00:00 -07001387 }
1388
1389/** specify the ciphers to be used by default by the SSL_CTX */
1390int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1391 {
1392 STACK_OF(SSL_CIPHER) *sk;
1393
1394 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1395 &ctx->cipher_list_by_id,str, ctx->cert);
1396 /* ssl_create_cipher_list may return an empty stack if it
1397 * was unable to find a cipher matching the given rule string
1398 * (for example if the rule string specifies a cipher which
1399 * has been disabled). This is not an error as far as
1400 * ssl_create_cipher_list is concerned, and hence
1401 * ctx->cipher_list and ctx->cipher_list_by_id has been
1402 * updated. */
1403 if (sk == NULL)
1404 return 0;
1405 else if (sk_SSL_CIPHER_num(sk) == 0)
1406 {
1407 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1408 return 0;
1409 }
1410 return 1;
1411 }
1412
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001413int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str)
1414 {
1415 STACK_OF(SSL_CIPHER) *sk;
1416
1417 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert);
1418 if (sk == NULL)
1419 return 0;
1420 else if (sk_SSL_CIPHER_num(sk) == 0)
1421 {
1422 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH);
1423 return 0;
1424 }
1425 return 1;
1426 }
1427
Adam Langley95c29f32014-06-20 12:00:00 -07001428/** specify the ciphers to be used by the SSL */
1429int SSL_set_cipher_list(SSL *s,const char *str)
1430 {
1431 STACK_OF(SSL_CIPHER) *sk;
1432
1433 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1434 &s->cipher_list_by_id,str, s->cert);
1435 /* see comment in SSL_CTX_set_cipher_list */
1436 if (sk == NULL)
1437 return 0;
1438 else if (sk_SSL_CIPHER_num(sk) == 0)
1439 {
1440 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1441 return 0;
1442 }
1443 return 1;
1444 }
1445
David Benjamin5491e3f2014-09-29 19:33:09 -04001446int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p)
Adam Langley95c29f32014-06-20 12:00:00 -07001447 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001448 size_t i;
David Benjamin6f260012014-08-15 13:49:12 -04001449 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001450 CERT *ct = s->cert;
David Benjamin5491e3f2014-09-29 19:33:09 -04001451 uint8_t *q;
Adam Langley95c29f32014-06-20 12:00:00 -07001452 /* Set disabled masks for this session */
1453 ssl_set_client_disabled(s);
1454
1455 if (sk == NULL) return(0);
1456 q=p;
1457
1458 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1459 {
1460 c=sk_SSL_CIPHER_value(sk,i);
1461 /* Skip disabled ciphers */
1462 if (c->algorithm_ssl & ct->mask_ssl ||
1463 c->algorithm_mkey & ct->mask_k ||
1464 c->algorithm_auth & ct->mask_a)
1465 continue;
David Benjamin39482a12014-07-20 13:30:15 -04001466 s2n(ssl3_get_cipher_value(c), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001467 }
David Benjamin5491e3f2014-09-29 19:33:09 -04001468 /* If all ciphers were disabled, return the error to the caller. */
1469 if (p == q)
Adam Langley95c29f32014-06-20 12:00:00 -07001470 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001471 return 0;
1472 }
1473
1474 /* Add SCSVs. */
1475 if (!s->renegotiate)
1476 {
1477 s2n(SSL3_CK_SCSV & 0xffff, p);
1478 }
1479 if (s->fallback_scsv)
1480 {
1481 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001482 }
1483
1484 return(p-q);
1485 }
1486
David Benjamin9f2c0d72014-10-21 22:00:19 -04001487STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001488 {
David Benjamin39482a12014-07-20 13:30:15 -04001489 CBS cipher_suites = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001490 const SSL_CIPHER *c;
1491 STACK_OF(SSL_CIPHER) *sk;
David Benjamin39482a12014-07-20 13:30:15 -04001492
Adam Langley95c29f32014-06-20 12:00:00 -07001493 if (s->s3)
1494 s->s3->send_connection_binding = 0;
1495
David Benjamin39482a12014-07-20 13:30:15 -04001496 if (CBS_len(&cipher_suites) % 2 != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001497 {
1498 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001499 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001500 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001501
1502 sk = sk_SSL_CIPHER_new_null();
1503 if (sk == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001504 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001505 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1506 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001507 }
1508
David Benjamin39482a12014-07-20 13:30:15 -04001509 if (!CBS_stow(&cipher_suites,
1510 &s->cert->ciphers_raw, &s->cert->ciphers_rawlen))
Adam Langley95c29f32014-06-20 12:00:00 -07001511 {
1512 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1513 goto err;
1514 }
Adam Langley95c29f32014-06-20 12:00:00 -07001515
David Benjamin39482a12014-07-20 13:30:15 -04001516 while (CBS_len(&cipher_suites) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001517 {
David Benjamin39482a12014-07-20 13:30:15 -04001518 uint16_t cipher_suite;
1519
1520 if (!CBS_get_u16(&cipher_suites, &cipher_suite))
1521 {
1522 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1523 goto err;
1524 }
1525
David Benjamin9f2c0d72014-10-21 22:00:19 -04001526 /* Check for SCSV. */
David Benjamin39482a12014-07-20 13:30:15 -04001527 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff))
Adam Langley95c29f32014-06-20 12:00:00 -07001528 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001529 /* SCSV is fatal if renegotiating. */
Adam Langley95c29f32014-06-20 12:00:00 -07001530 if (s->renegotiate)
1531 {
1532 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1533 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1534 goto err;
1535 }
1536 s->s3->send_connection_binding = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001537 continue;
1538 }
1539
David Benjamin9f2c0d72014-10-21 22:00:19 -04001540 /* Check for FALLBACK_SCSV. */
1541 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff))
Adam Langley29707792014-06-20 12:00:00 -07001542 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001543 if (s->version < ssl_get_max_version(s))
1544 {
1545 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1546 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1547 goto err;
1548 }
1549 continue;
Adam Langley29707792014-06-20 12:00:00 -07001550 }
1551
David Benjamin39482a12014-07-20 13:30:15 -04001552 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -07001553 if (c != NULL)
1554 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001555 if (!sk_SSL_CIPHER_push(sk, c))
Adam Langley95c29f32014-06-20 12:00:00 -07001556 {
1557 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1558 goto err;
1559 }
1560 }
1561 }
1562
David Benjamin9f2c0d72014-10-21 22:00:19 -04001563 return sk;
1564
Adam Langley95c29f32014-06-20 12:00:00 -07001565err:
David Benjamin9f2c0d72014-10-21 22:00:19 -04001566 if (sk != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001567 sk_SSL_CIPHER_free(sk);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001568 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001569 }
1570
1571
Adam Langley95c29f32014-06-20 12:00:00 -07001572/** return a servername extension value if provided in Client Hello, or NULL.
1573 * So far, only host_name types are defined (RFC 3546).
1574 */
1575
1576const char *SSL_get_servername(const SSL *s, const int type)
1577 {
1578 if (type != TLSEXT_NAMETYPE_host_name)
1579 return NULL;
1580
1581 return s->session && !s->tlsext_hostname ?
1582 s->session->tlsext_hostname :
1583 s->tlsext_hostname;
1584 }
1585
1586int SSL_get_servername_type(const SSL *s)
1587 {
1588 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1589 return TLSEXT_NAMETYPE_host_name;
1590 return -1;
1591 }
1592
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001593void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx)
1594 {
1595 ctx->signed_cert_timestamps_enabled = 1;
1596 }
1597
1598int SSL_enable_signed_cert_timestamps(SSL *ssl)
1599 {
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001600 ssl->signed_cert_timestamps_enabled = 1;
1601 return 1;
1602 }
1603
David Benjamin6c7aed02014-08-27 16:42:38 -04001604void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx)
1605 {
1606 ctx->ocsp_stapling_enabled = 1;
1607 }
1608
1609int SSL_enable_ocsp_stapling(SSL *ssl)
1610 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001611 ssl->ocsp_stapling_enabled = 1;
1612 return 1;
1613 }
1614
David Benjamin52d699f2014-11-25 01:35:18 -05001615void 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 +02001616 {
Adam Langley3cb50e02014-08-26 14:00:31 -07001617 SSL_SESSION *session = ssl->session;
1618
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001619 *out_len = 0;
1620 *out = NULL;
1621 if (ssl->server)
1622 return;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001623 if (!session || !session->tlsext_signed_cert_timestamp_list)
1624 return;
1625 *out = session->tlsext_signed_cert_timestamp_list;
1626 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1627 }
1628
David Benjamin52d699f2014-11-25 01:35:18 -05001629void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out, size_t *out_len)
David Benjamin6c7aed02014-08-27 16:42:38 -04001630 {
1631 SSL_SESSION *session = ssl->session;
1632
1633 *out_len = 0;
1634 *out = NULL;
1635 if (ssl->server)
1636 return;
1637 if (!session || !session->ocsp_response)
1638 return;
1639 *out = session->ocsp_response;
1640 *out_len = session->ocsp_response_length;
1641 }
1642
Adam Langley95c29f32014-06-20 12:00:00 -07001643/* SSL_select_next_proto implements the standard protocol selection. It is
1644 * expected that this function is called from the callback set by
1645 * SSL_CTX_set_next_proto_select_cb.
1646 *
1647 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1648 * strings. The length byte itself is not included in the length. A byte
1649 * string of length 0 is invalid. No byte string may be truncated.
1650 *
1651 * The current, but experimental algorithm for selecting the protocol is:
1652 *
1653 * 1) If the server doesn't support NPN then this is indicated to the
1654 * callback. In this case, the client application has to abort the connection
1655 * or have a default application level protocol.
1656 *
1657 * 2) If the server supports NPN, but advertises an empty list then the
1658 * client selects the first protcol in its list, but indicates via the
1659 * API that this fallback case was enacted.
1660 *
1661 * 3) Otherwise, the client finds the first protocol in the server's list
1662 * that it supports and selects this protocol. This is because it's
1663 * assumed that the server has better information about which protocol
1664 * a client should use.
1665 *
1666 * 4) If the client doesn't support any of the server's advertised
1667 * protocols, then this is treated the same as case 2.
1668 *
1669 * It returns either
1670 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1671 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1672 */
1673int 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)
1674 {
1675 unsigned int i, j;
1676 const unsigned char *result;
1677 int status = OPENSSL_NPN_UNSUPPORTED;
1678
1679 /* For each protocol in server preference order, see if we support it. */
1680 for (i = 0; i < server_len; )
1681 {
1682 for (j = 0; j < client_len; )
1683 {
1684 if (server[i] == client[j] &&
1685 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1686 {
1687 /* We found a match */
1688 result = &server[i];
1689 status = OPENSSL_NPN_NEGOTIATED;
1690 goto found;
1691 }
1692 j += client[j];
1693 j++;
1694 }
1695 i += server[i];
1696 i++;
1697 }
1698
1699 /* There's no overlap between our protocols and the server's list. */
1700 result = client;
1701 status = OPENSSL_NPN_NO_OVERLAP;
1702
1703 found:
1704 *out = (unsigned char *) result + 1;
1705 *outlen = result[0];
1706 return status;
1707 }
1708
Adam Langley95c29f32014-06-20 12:00:00 -07001709/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1710 * requested protocol for this connection and returns 0. If the client didn't
1711 * request any protocol, then *data is set to NULL.
1712 *
1713 * Note that the client can request any protocol it chooses. The value returned
1714 * from this function need not be a member of the list of supported protocols
1715 * provided by the callback.
1716 */
David Benjaminced551f2014-07-11 23:16:16 -04001717void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, unsigned *len)
Adam Langley95c29f32014-06-20 12:00:00 -07001718 {
1719 *data = s->next_proto_negotiated;
1720 if (!*data) {
1721 *len = 0;
1722 } else {
1723 *len = s->next_proto_negotiated_len;
1724 }
1725}
1726
1727/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1728 * TLS server needs a list of supported protocols for Next Protocol
1729 * Negotiation. The returned list must be in wire format. The list is returned
1730 * by setting |out| to point to it and |outlen| to its length. This memory will
1731 * not be modified, but one should assume that the SSL* keeps a reference to
1732 * it.
1733 *
1734 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1735 * such extension will be included in the ServerHello. */
1736void 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)
1737 {
1738 ctx->next_protos_advertised_cb = cb;
1739 ctx->next_protos_advertised_cb_arg = arg;
1740 }
1741
1742/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1743 * client needs to select a protocol from the server's provided list. |out|
1744 * must be set to point to the selected protocol (which may be within |in|).
1745 * The length of the protocol name must be written into |outlen|. The server's
1746 * advertised protocols are provided in |in| and |inlen|. The callback can
1747 * assume that |in| is syntactically valid.
1748 *
1749 * The client must select a protocol. It is fatal to the connection if this
1750 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1751 */
1752void 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)
1753 {
1754 ctx->next_proto_select_cb = cb;
1755 ctx->next_proto_select_cb_arg = arg;
1756 }
Adam Langley95c29f32014-06-20 12:00:00 -07001757
Adam Langley95c29f32014-06-20 12:00:00 -07001758/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1759 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1760 * length-prefixed strings).
1761 *
1762 * Returns 0 on success. */
1763int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1764 unsigned protos_len)
1765 {
1766 if (ctx->alpn_client_proto_list)
1767 OPENSSL_free(ctx->alpn_client_proto_list);
1768
David Benjamin072c9532014-07-26 11:44:25 -04001769 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001770 if (!ctx->alpn_client_proto_list)
1771 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001772 ctx->alpn_client_proto_list_len = protos_len;
1773
1774 return 0;
1775 }
1776
1777/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1778 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1779 * length-prefixed strings).
1780 *
1781 * Returns 0 on success. */
1782int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1783 unsigned protos_len)
1784 {
1785 if (ssl->alpn_client_proto_list)
1786 OPENSSL_free(ssl->alpn_client_proto_list);
1787
David Benjamin072c9532014-07-26 11:44:25 -04001788 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001789 if (!ssl->alpn_client_proto_list)
1790 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001791 ssl->alpn_client_proto_list_len = protos_len;
1792
1793 return 0;
1794 }
1795
1796/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1797 * during ClientHello processing in order to select an ALPN protocol from the
1798 * client's list of offered protocols. */
1799void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1800 int (*cb) (SSL *ssl,
1801 const unsigned char **out,
1802 unsigned char *outlen,
1803 const unsigned char *in,
1804 unsigned int inlen,
1805 void *arg),
1806 void *arg)
1807 {
1808 ctx->alpn_select_cb = cb;
1809 ctx->alpn_select_cb_arg = arg;
1810 }
1811
1812/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1813 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1814 * including the leading length-prefix byte). If the server didn't respond with
1815 * a negotiated protocol then |*len| will be zero. */
1816void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1817 unsigned *len)
1818 {
1819 *data = NULL;
1820 if (ssl->s3)
1821 *data = ssl->s3->alpn_selected;
1822 if (*data == NULL)
1823 *len = 0;
1824 else
1825 *len = ssl->s3->alpn_selected_len;
1826 }
Adam Langley95c29f32014-06-20 12:00:00 -07001827
1828int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1829 const char *label, size_t llen, const unsigned char *p, size_t plen,
1830 int use_context)
1831 {
1832 if (s->version < TLS1_VERSION)
1833 return -1;
1834
1835 return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1836 llen, p, plen,
1837 use_context);
1838 }
1839
1840static uint32_t ssl_session_hash(const SSL_SESSION *a)
1841 {
1842 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1843 ((uint32_t) a->session_id[1] << 8) ||
1844 ((uint32_t) a->session_id[2] << 16) ||
1845 ((uint32_t) a->session_id[3] << 24);
1846
1847 return hash;
1848 }
1849
1850/* NB: If this function (or indeed the hash function which uses a sort of
1851 * coarser function than this one) is changed, ensure
1852 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1853 * able to construct an SSL_SESSION that will collide with any existing session
1854 * with a matching session ID. */
1855static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1856 {
1857 if (a->ssl_version != b->ssl_version)
1858 return(1);
1859 if (a->session_id_length != b->session_id_length)
1860 return(1);
1861 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1862 }
1863
1864SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1865 {
1866 SSL_CTX *ret=NULL;
1867
1868 if (meth == NULL)
1869 {
1870 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1871 return(NULL);
1872 }
1873
Adam Langley95c29f32014-06-20 12:00:00 -07001874 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1875 {
1876 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1877 goto err;
1878 }
1879 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1880 if (ret == NULL)
1881 goto err;
1882
1883 memset(ret,0,sizeof(SSL_CTX));
1884
1885 ret->method=meth;
1886
1887 ret->cert_store=NULL;
1888 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1889 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1890 ret->session_cache_head=NULL;
1891 ret->session_cache_tail=NULL;
1892
1893 /* We take the system default */
David Benjaminf4501342014-08-14 17:24:37 -04001894 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001895
1896 ret->new_session_cb=0;
1897 ret->remove_session_cb=0;
1898 ret->get_session_cb=0;
1899 ret->generate_session_id=0;
1900
1901 memset((char *)&ret->stats,0,sizeof(ret->stats));
1902
1903 ret->references=1;
1904 ret->quiet_shutdown=0;
1905
Adam Langley95c29f32014-06-20 12:00:00 -07001906 ret->info_callback=NULL;
1907
1908 ret->app_verify_callback=0;
1909 ret->app_verify_arg=NULL;
1910
1911 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1912 ret->read_ahead=0;
1913 ret->msg_callback=0;
1914 ret->msg_callback_arg=NULL;
1915 ret->verify_mode=SSL_VERIFY_NONE;
1916#if 0
1917 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1918#endif
1919 ret->sid_ctx_length=0;
1920 ret->default_verify_callback=NULL;
1921 if ((ret->cert=ssl_cert_new()) == NULL)
1922 goto err;
1923
1924 ret->default_passwd_callback=0;
1925 ret->default_passwd_callback_userdata=NULL;
1926 ret->client_cert_cb=0;
1927 ret->app_gen_cookie_cb=0;
1928 ret->app_verify_cookie_cb=0;
1929
1930 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1931 if (ret->sessions == NULL) goto err;
1932 ret->cert_store=X509_STORE_new();
1933 if (ret->cert_store == NULL) goto err;
1934
1935 ssl_create_cipher_list(ret->method,
1936 &ret->cipher_list,&ret->cipher_list_by_id,
David Benjamin5e4f6e92014-11-17 03:23:24 -05001937 SSL_DEFAULT_CIPHER_LIST, ret->cert);
Adam Langley95c29f32014-06-20 12:00:00 -07001938 if (ret->cipher_list == NULL
Adam Langley858a88d2014-06-20 12:00:00 -07001939 || sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001940 {
1941 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1942 goto err2;
1943 }
1944
1945 ret->param = X509_VERIFY_PARAM_new();
1946 if (!ret->param)
1947 goto err;
1948
Adam Langley95c29f32014-06-20 12:00:00 -07001949 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1950 goto err;
1951
1952 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1953
1954 ret->extra_certs=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001955
1956 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1957
Adam Langley95c29f32014-06-20 12:00:00 -07001958 ret->tlsext_servername_callback = 0;
1959 ret->tlsext_servername_arg = NULL;
1960 /* Setup RFC4507 ticket keys */
1961 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
1962 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
1963 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
1964 ret->options |= SSL_OP_NO_TICKET;
1965
1966 ret->tlsext_status_cb = 0;
1967 ret->tlsext_status_arg = NULL;
1968
Adam Langley95c29f32014-06-20 12:00:00 -07001969 ret->next_protos_advertised_cb = 0;
1970 ret->next_proto_select_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001971 ret->psk_identity_hint=NULL;
1972 ret->psk_client_callback=NULL;
1973 ret->psk_server_callback=NULL;
David Benjamin422d3a42014-08-20 11:09:03 -04001974
Adam Langley95c29f32014-06-20 12:00:00 -07001975 /* Default is to connect to non-RI servers. When RI is more widely
1976 * deployed might change this.
1977 */
1978 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1979
1980 return(ret);
1981err:
1982 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
1983err2:
1984 if (ret != NULL) SSL_CTX_free(ret);
1985 return(NULL);
1986 }
1987
Adam Langley95c29f32014-06-20 12:00:00 -07001988
1989void SSL_CTX_free(SSL_CTX *a)
1990 {
1991 int i;
1992
1993 if (a == NULL) return;
1994
1995 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
1996#ifdef REF_PRINT
1997 REF_PRINT("SSL_CTX",a);
1998#endif
1999 if (i > 0) return;
2000#ifdef REF_CHECK
2001 if (i < 0)
2002 {
2003 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
2004 abort(); /* ok */
2005 }
2006#endif
2007
2008 if (a->param)
2009 X509_VERIFY_PARAM_free(a->param);
2010
2011 /*
2012 * Free internal session cache. However: the remove_cb() may reference
2013 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2014 * after the sessions were flushed.
2015 * As the ex_data handling routines might also touch the session cache,
2016 * the most secure solution seems to be: empty (flush) the cache, then
2017 * free ex_data, then finally free the cache.
2018 * (See ticket [openssl.org #212].)
2019 */
2020 if (a->sessions != NULL)
2021 SSL_CTX_flush_sessions(a,0);
2022
2023 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
2024
2025 if (a->sessions != NULL)
2026 lh_SSL_SESSION_free(a->sessions);
2027
2028 if (a->cert_store != NULL)
2029 X509_STORE_free(a->cert_store);
2030 if (a->cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002031 ssl_cipher_preference_list_free(a->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002032 if (a->cipher_list_by_id != NULL)
2033 sk_SSL_CIPHER_free(a->cipher_list_by_id);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07002034 if (a->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002035 ssl_cipher_preference_list_free(a->cipher_list_tls11);
Adam Langley95c29f32014-06-20 12:00:00 -07002036 if (a->cert != NULL)
2037 ssl_cert_free(a->cert);
2038 if (a->client_CA != NULL)
2039 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2040 if (a->extra_certs != NULL)
2041 sk_X509_pop_free(a->extra_certs,X509_free);
Adam Langley95c29f32014-06-20 12:00:00 -07002042
2043 if (a->srtp_profiles)
2044 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2045
Adam Langley95c29f32014-06-20 12:00:00 -07002046 if (a->psk_identity_hint)
2047 OPENSSL_free(a->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07002048
Adam Langley95c29f32014-06-20 12:00:00 -07002049 if (a->tlsext_ecpointformatlist)
2050 OPENSSL_free(a->tlsext_ecpointformatlist);
2051 if (a->tlsext_ellipticcurvelist)
2052 OPENSSL_free(a->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -07002053 if (a->alpn_client_proto_list != NULL)
2054 OPENSSL_free(a->alpn_client_proto_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002055
Adam Langley1258b6a2014-06-20 12:00:00 -07002056 if (a->tlsext_channel_id_private)
2057 EVP_PKEY_free(a->tlsext_channel_id_private);
Adam Langley1258b6a2014-06-20 12:00:00 -07002058
David Benjamin859ec3c2014-09-02 16:29:36 -04002059 if (a->keylog_bio)
2060 BIO_free(a->keylog_bio);
2061
Adam Langley95c29f32014-06-20 12:00:00 -07002062 OPENSSL_free(a);
2063 }
2064
2065void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2066 {
2067 ctx->default_passwd_callback=cb;
2068 }
2069
2070void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2071 {
2072 ctx->default_passwd_callback_userdata=u;
2073 }
2074
2075void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2076 {
2077 ctx->app_verify_callback=cb;
2078 ctx->app_verify_arg=arg;
2079 }
2080
2081void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2082 {
2083 ctx->verify_mode=mode;
2084 ctx->default_verify_callback=cb;
2085 }
2086
2087void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2088 {
2089 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2090 }
2091
2092void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2093 {
2094 ssl_cert_set_cert_cb(c->cert, cb, arg);
2095 }
2096
2097void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2098 {
2099 ssl_cert_set_cert_cb(s->cert, cb, arg);
2100 }
2101
David Benjamin033e5f42014-11-13 18:47:41 -05002102static int ssl_has_key(SSL *s, size_t idx)
2103 {
2104 CERT_PKEY *cpk = &s->cert->pkeys[idx];
2105 return cpk->x509 && cpk->privatekey;
2106 }
2107
David Benjaminf31e6812014-11-13 18:05:55 -05002108void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
2109 unsigned long *out_mask_a)
Adam Langley95c29f32014-06-20 12:00:00 -07002110 {
David Benjaminf31e6812014-11-13 18:05:55 -05002111 CERT *c = s->cert;
David Benjaminf31e6812014-11-13 18:05:55 -05002112 int rsa_enc, rsa_sign, dh_tmp;
2113 unsigned long mask_k, mask_a;
David Benjamin060d9d22014-07-15 00:54:26 -04002114 int have_ecc_cert, ecdsa_ok;
David Benjamin0da0e182014-08-19 16:20:28 -04002115 int have_ecdh_tmp;
David Benjaminf31e6812014-11-13 18:05:55 -05002116 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002117
David Benjaminf31e6812014-11-13 18:05:55 -05002118 if (c == NULL)
2119 {
2120 /* TODO(davidben): Is this codepath possible? */
2121 *out_mask_k = 0;
2122 *out_mask_a = 0;
2123 return;
2124 }
Adam Langley95c29f32014-06-20 12:00:00 -07002125
David Benjaminf31e6812014-11-13 18:05:55 -05002126 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002127
David Benjaminf31e6812014-11-13 18:05:55 -05002128 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
David Benjamin033e5f42014-11-13 18:47:41 -05002129 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2130 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2131 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
David Benjaminf31e6812014-11-13 18:05:55 -05002132 mask_k = 0;
2133 mask_a = 0;
2134
David Benjamin77a942b2014-07-15 01:22:50 -04002135 if (rsa_enc)
David Benjaminf31e6812014-11-13 18:05:55 -05002136 mask_k |= SSL_kRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002137
Adam Langley95c29f32014-06-20 12:00:00 -07002138 if (dh_tmp)
David Benjaminf31e6812014-11-13 18:05:55 -05002139 mask_k |= SSL_kEDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002140
Adam Langley95c29f32014-06-20 12:00:00 -07002141 if (rsa_enc || rsa_sign)
2142 {
David Benjaminf31e6812014-11-13 18:05:55 -05002143 mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002144 }
2145
David Benjaminf31e6812014-11-13 18:05:55 -05002146 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002147
David Benjamin0da0e182014-08-19 16:20:28 -04002148 /* An ECC certificate may be usable for ECDSA cipher suites depending on
David Benjamin033e5f42014-11-13 18:47:41 -05002149 * the key usage extension and on the client's curve preferences. */
Adam Langley95c29f32014-06-20 12:00:00 -07002150 if (have_ecc_cert)
2151 {
David Benjamin033e5f42014-11-13 18:47:41 -05002152 x = c->pkeys[SSL_PKEY_ECC].x509;
Adam Langley95c29f32014-06-20 12:00:00 -07002153 /* This call populates extension flags (ex_flags) */
2154 X509_check_purpose(x, -1, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07002155 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2156 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
David Benjamin033e5f42014-11-13 18:47:41 -05002157 if (!tls1_check_ec_cert(s, x))
Adam Langley95c29f32014-06-20 12:00:00 -07002158 ecdsa_ok = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002159 if (ecdsa_ok)
2160 {
David Benjaminf31e6812014-11-13 18:05:55 -05002161 mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002162 }
Adam Langley95c29f32014-06-20 12:00:00 -07002163 }
Adam Langley95c29f32014-06-20 12:00:00 -07002164
David Benjaminf31e6812014-11-13 18:05:55 -05002165 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2166 * key, check it. */
2167 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002168 {
David Benjaminf31e6812014-11-13 18:05:55 -05002169 mask_k |= SSL_kEECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002170 }
Adam Langley95c29f32014-06-20 12:00:00 -07002171
David Benjaminf31e6812014-11-13 18:05:55 -05002172 /* PSK requires a server callback. */
2173 if (s->psk_server_callback != NULL)
2174 {
2175 mask_k |= SSL_kPSK;
2176 mask_a |= SSL_aPSK;
2177 }
Adam Langley95c29f32014-06-20 12:00:00 -07002178
David Benjaminf31e6812014-11-13 18:05:55 -05002179 *out_mask_k = mask_k;
2180 *out_mask_a = mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002181 }
2182
2183/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2184#define ku_reject(x, usage) \
2185 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2186
Adam Langley95c29f32014-06-20 12:00:00 -07002187
2188int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2189 {
David Benjamin0da0e182014-08-19 16:20:28 -04002190 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002191 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2192 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2193
Adam Langley95c29f32014-06-20 12:00:00 -07002194 alg_a = cs->algorithm_auth;
2195
Adam Langley95c29f32014-06-20 12:00:00 -07002196 /* This call populates the ex_flags field correctly */
2197 X509_check_purpose(x, -1, 0);
2198 if ((x->sig_alg) && (x->sig_alg->algorithm))
2199 {
2200 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2201 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2202 }
Adam Langley95c29f32014-06-20 12:00:00 -07002203 if (alg_a & SSL_aECDSA)
2204 {
2205 /* key usage, if present, must allow signing */
2206 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2207 {
2208 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2209 return 0;
2210 }
2211 }
2212
2213 return 1; /* all checks are ok */
2214 }
2215
Adam Langley95c29f32014-06-20 12:00:00 -07002216
2217static int ssl_get_server_cert_index(const SSL *s)
2218 {
2219 int idx;
2220 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2221 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2222 idx = SSL_PKEY_RSA_SIGN;
2223 if (idx == -1)
2224 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2225 return idx;
2226 }
2227
2228CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2229 {
David Benjaminf31e6812014-11-13 18:05:55 -05002230 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002231
2232 /* This may or may not be an error. */
2233 if (i < 0)
2234 return NULL;
2235
2236 /* May be NULL. */
David Benjaminf31e6812014-11-13 18:05:55 -05002237 return &s->cert->pkeys[i];
Adam Langley95c29f32014-06-20 12:00:00 -07002238 }
2239
David Benjaminec2f27d2014-11-13 19:17:25 -05002240EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher)
Adam Langley95c29f32014-06-20 12:00:00 -07002241 {
2242 unsigned long alg_a;
2243 CERT *c;
2244 int idx = -1;
2245
2246 alg_a = cipher->algorithm_auth;
2247 c=s->cert;
2248
David Benjaminef2116d2014-08-19 20:21:56 -04002249 if (alg_a & SSL_aRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002250 {
2251 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2252 idx = SSL_PKEY_RSA_SIGN;
2253 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2254 idx = SSL_PKEY_RSA_ENC;
2255 }
2256 else if ((alg_a & SSL_aECDSA) &&
2257 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2258 idx = SSL_PKEY_ECC;
2259 if (idx == -1)
2260 {
2261 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2262 return(NULL);
2263 }
Adam Langley95c29f32014-06-20 12:00:00 -07002264 return c->pkeys[idx].privatekey;
2265 }
2266
Adam Langley95c29f32014-06-20 12:00:00 -07002267void ssl_update_cache(SSL *s,int mode)
2268 {
2269 int i;
2270
2271 /* If the session_id_length is 0, we are not supposed to cache it,
2272 * and it would be rather hard to do anyway :-) */
2273 if (s->session->session_id_length == 0) return;
2274
David Benjamin041b58a2014-08-27 15:25:21 -04002275 i=s->initial_ctx->session_cache_mode;
Adam Langley95c29f32014-06-20 12:00:00 -07002276 if ((i & mode) && (!s->hit)
2277 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
David Benjamin041b58a2014-08-27 15:25:21 -04002278 || SSL_CTX_add_session(s->initial_ctx,s->session))
2279 && (s->initial_ctx->new_session_cb != NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07002280 {
2281 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
David Benjamin041b58a2014-08-27 15:25:21 -04002282 if (!s->initial_ctx->new_session_cb(s,s->session))
Adam Langley95c29f32014-06-20 12:00:00 -07002283 SSL_SESSION_free(s->session);
2284 }
2285
2286 /* auto flush every 255 connections */
2287 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2288 ((i & mode) == mode))
2289 {
2290 if ( (((mode & SSL_SESS_CACHE_CLIENT)
David Benjamin041b58a2014-08-27 15:25:21 -04002291 ?s->initial_ctx->stats.sess_connect_good
2292 :s->initial_ctx->stats.sess_accept_good) & 0xff) == 0xff)
Adam Langley95c29f32014-06-20 12:00:00 -07002293 {
David Benjamin041b58a2014-08-27 15:25:21 -04002294 SSL_CTX_flush_sessions(s->initial_ctx,(unsigned long)time(NULL));
Adam Langley95c29f32014-06-20 12:00:00 -07002295 }
2296 }
2297 }
2298
Adam Langley95c29f32014-06-20 12:00:00 -07002299int SSL_get_error(const SSL *s,int i)
2300 {
2301 int reason;
2302 unsigned long l;
2303 BIO *bio;
2304
2305 if (i > 0) return(SSL_ERROR_NONE);
2306
2307 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2308 * etc, where we do encode the error */
2309 if ((l=ERR_peek_error()) != 0)
2310 {
2311 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2312 return(SSL_ERROR_SYSCALL);
2313 else
2314 return(SSL_ERROR_SSL);
2315 }
2316
Adam Langleyb2ce0582014-06-20 12:00:00 -07002317 if ((i < 0) && SSL_want_session(s))
2318 return(SSL_ERROR_PENDING_SESSION);
2319
Adam Langleydc9b1412014-06-20 12:00:00 -07002320 if ((i < 0) && SSL_want_certificate(s))
2321 return(SSL_ERROR_PENDING_CERTIFICATE);
2322
Adam Langley95c29f32014-06-20 12:00:00 -07002323 if ((i < 0) && SSL_want_read(s))
2324 {
2325 bio=SSL_get_rbio(s);
2326 if (BIO_should_read(bio))
2327 return(SSL_ERROR_WANT_READ);
2328 else if (BIO_should_write(bio))
2329 /* This one doesn't make too much sense ... We never try
2330 * to write to the rbio, and an application program where
2331 * rbio and wbio are separate couldn't even know what it
2332 * should wait for.
2333 * However if we ever set s->rwstate incorrectly
2334 * (so that we have SSL_want_read(s) instead of
2335 * SSL_want_write(s)) and rbio and wbio *are* the same,
2336 * this test works around that bug; so it might be safer
2337 * to keep it. */
2338 return(SSL_ERROR_WANT_WRITE);
2339 else if (BIO_should_io_special(bio))
2340 {
2341 reason=BIO_get_retry_reason(bio);
2342 if (reason == BIO_RR_CONNECT)
2343 return(SSL_ERROR_WANT_CONNECT);
2344 else if (reason == BIO_RR_ACCEPT)
2345 return(SSL_ERROR_WANT_ACCEPT);
2346 else
2347 return(SSL_ERROR_SYSCALL); /* unknown */
2348 }
2349 }
2350
2351 if ((i < 0) && SSL_want_write(s))
2352 {
2353 bio=SSL_get_wbio(s);
2354 if (BIO_should_write(bio))
2355 return(SSL_ERROR_WANT_WRITE);
2356 else if (BIO_should_read(bio))
2357 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2358 return(SSL_ERROR_WANT_READ);
2359 else if (BIO_should_io_special(bio))
2360 {
2361 reason=BIO_get_retry_reason(bio);
2362 if (reason == BIO_RR_CONNECT)
2363 return(SSL_ERROR_WANT_CONNECT);
2364 else if (reason == BIO_RR_ACCEPT)
2365 return(SSL_ERROR_WANT_ACCEPT);
2366 else
2367 return(SSL_ERROR_SYSCALL);
2368 }
2369 }
2370 if ((i < 0) && SSL_want_x509_lookup(s))
2371 {
2372 return(SSL_ERROR_WANT_X509_LOOKUP);
2373 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002374 if ((i < 0) && SSL_want_channel_id_lookup(s))
2375 {
2376 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2377 }
Adam Langley95c29f32014-06-20 12:00:00 -07002378
2379 if (i == 0)
2380 {
David Benjamin5e4f6e92014-11-17 03:23:24 -05002381 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2382 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
Adam Langley95c29f32014-06-20 12:00:00 -07002383 return(SSL_ERROR_ZERO_RETURN);
Adam Langley95c29f32014-06-20 12:00:00 -07002384 }
2385 return(SSL_ERROR_SYSCALL);
2386 }
2387
2388int SSL_do_handshake(SSL *s)
2389 {
2390 int ret=1;
2391
2392 if (s->handshake_func == NULL)
2393 {
2394 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2395 return(-1);
2396 }
2397
2398 s->method->ssl_renegotiate_check(s);
2399
2400 if (SSL_in_init(s) || SSL_in_before(s))
2401 {
2402 ret=s->handshake_func(s);
2403 }
2404 return(ret);
2405 }
2406
Adam Langley95c29f32014-06-20 12:00:00 -07002407void SSL_set_accept_state(SSL *s)
2408 {
2409 s->server=1;
2410 s->shutdown=0;
2411 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2412 s->handshake_func=s->method->ssl_accept;
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
2419void SSL_set_connect_state(SSL *s)
2420 {
2421 s->server=0;
2422 s->shutdown=0;
2423 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2424 s->handshake_func=s->method->ssl_connect;
2425 /* clear the current cipher */
2426 ssl_clear_cipher_ctx(s);
2427 ssl_clear_hash_ctx(&s->read_hash);
2428 ssl_clear_hash_ctx(&s->write_hash);
2429 }
2430
2431int ssl_undefined_function(SSL *s)
2432 {
2433 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2434 return(0);
2435 }
2436
2437int ssl_undefined_void_function(void)
2438 {
2439 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2440 return(0);
2441 }
2442
2443int ssl_undefined_const_function(const SSL *s)
2444 {
2445 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2446 return(0);
2447 }
2448
Adam Langley0f4746e2014-08-13 12:26:32 -07002449static const char *ssl_get_version(int version)
Adam Langley95c29f32014-06-20 12:00:00 -07002450 {
Adam Langley0f4746e2014-08-13 12:26:32 -07002451 if (version == TLS1_2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002452 return("TLSv1.2");
Adam Langley0f4746e2014-08-13 12:26:32 -07002453 else if (version == TLS1_1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002454 return("TLSv1.1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002455 else if (version == TLS1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002456 return("TLSv1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002457 else if (version == SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002458 return("SSLv3");
Adam Langley95c29f32014-06-20 12:00:00 -07002459 else
2460 return("unknown");
2461 }
2462
Adam Langley0f4746e2014-08-13 12:26:32 -07002463const char *SSL_get_version(const SSL *s)
2464 {
2465 return ssl_get_version(s->version);
2466 }
2467
2468const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2469 {
2470 return ssl_get_version(sess->ssl_version);
2471 }
2472
Adam Langley95c29f32014-06-20 12:00:00 -07002473void ssl_clear_cipher_ctx(SSL *s)
2474 {
2475 if (s->enc_read_ctx != NULL)
2476 {
2477 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2478 OPENSSL_free(s->enc_read_ctx);
2479 s->enc_read_ctx=NULL;
2480 }
2481 if (s->enc_write_ctx != NULL)
2482 {
2483 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2484 OPENSSL_free(s->enc_write_ctx);
2485 s->enc_write_ctx=NULL;
2486 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002487 if (s->aead_read_ctx != NULL)
2488 {
2489 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2490 OPENSSL_free(s->aead_read_ctx);
2491 s->aead_read_ctx = NULL;
2492 }
2493 if (s->aead_write_ctx != NULL)
2494 {
2495 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2496 OPENSSL_free(s->aead_write_ctx);
2497 s->aead_write_ctx = NULL;
2498 }
Adam Langley95c29f32014-06-20 12:00:00 -07002499 }
2500
2501X509 *SSL_get_certificate(const SSL *s)
2502 {
2503 if (s->cert != NULL)
2504 return(s->cert->key->x509);
2505 else
2506 return(NULL);
2507 }
2508
2509EVP_PKEY *SSL_get_privatekey(const SSL *s)
2510 {
2511 if (s->cert != NULL)
2512 return(s->cert->key->privatekey);
2513 else
2514 return(NULL);
2515 }
2516
2517X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2518 {
2519 if (ctx->cert != NULL)
2520 return ctx->cert->key->x509;
2521 else
2522 return NULL;
2523 }
2524
2525EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2526 {
2527 if (ctx->cert != NULL)
2528 return ctx->cert->key->privatekey;
2529 else
2530 return NULL ;
2531 }
2532
2533const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2534 {
2535 if ((s->session != NULL) && (s->session->cipher != NULL))
2536 return(s->session->cipher);
2537 return(NULL);
2538 }
2539const void *SSL_get_current_compression(SSL *s)
2540 {
2541 return NULL;
2542 }
2543const void *SSL_get_current_expansion(SSL *s)
2544 {
2545 return NULL;
2546 }
2547
2548int ssl_init_wbio_buffer(SSL *s,int push)
2549 {
2550 BIO *bbio;
2551
2552 if (s->bbio == NULL)
2553 {
2554 bbio=BIO_new(BIO_f_buffer());
2555 if (bbio == NULL) return(0);
2556 s->bbio=bbio;
2557 }
2558 else
2559 {
2560 bbio=s->bbio;
2561 if (s->bbio == s->wbio)
2562 s->wbio=BIO_pop(s->wbio);
2563 }
2564 (void)BIO_reset(bbio);
2565/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2566 if (!BIO_set_read_buffer_size(bbio,1))
2567 {
2568 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2569 return(0);
2570 }
2571 if (push)
2572 {
2573 if (s->wbio != bbio)
2574 s->wbio=BIO_push(bbio,s->wbio);
2575 }
2576 else
2577 {
2578 if (s->wbio == bbio)
2579 s->wbio=BIO_pop(bbio);
2580 }
2581 return(1);
2582 }
2583
2584void ssl_free_wbio_buffer(SSL *s)
2585 {
2586 if (s->bbio == NULL) return;
2587
2588 if (s->bbio == s->wbio)
2589 {
2590 /* remove buffering */
2591 s->wbio=BIO_pop(s->wbio);
2592#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2593 assert(s->wbio != NULL);
2594#endif
2595 }
2596 BIO_free(s->bbio);
2597 s->bbio=NULL;
2598 }
2599
2600void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2601 {
2602 ctx->quiet_shutdown=mode;
2603 }
2604
2605int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2606 {
2607 return(ctx->quiet_shutdown);
2608 }
2609
2610void SSL_set_quiet_shutdown(SSL *s,int mode)
2611 {
2612 s->quiet_shutdown=mode;
2613 }
2614
2615int SSL_get_quiet_shutdown(const SSL *s)
2616 {
2617 return(s->quiet_shutdown);
2618 }
2619
2620void SSL_set_shutdown(SSL *s,int mode)
2621 {
2622 s->shutdown=mode;
2623 }
2624
2625int SSL_get_shutdown(const SSL *s)
2626 {
2627 return(s->shutdown);
2628 }
2629
2630int SSL_version(const SSL *s)
2631 {
2632 return(s->version);
2633 }
2634
2635SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2636 {
2637 return(ssl->ctx);
2638 }
2639
2640SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2641 {
2642 if (ssl->ctx == ctx)
2643 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002644 if (ctx == NULL)
2645 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002646 if (ssl->cert != NULL)
2647 ssl_cert_free(ssl->cert);
2648 ssl->cert = ssl_cert_dup(ctx->cert);
2649 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2650 if (ssl->ctx != NULL)
2651 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2652 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07002653
2654 ssl->sid_ctx_length = ctx->sid_ctx_length;
2655 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2656 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2657
Adam Langley95c29f32014-06-20 12:00:00 -07002658 return(ssl->ctx);
2659 }
2660
2661#ifndef OPENSSL_NO_STDIO
2662int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2663 {
2664 return(X509_STORE_set_default_paths(ctx->cert_store));
2665 }
2666
2667int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2668 const char *CApath)
2669 {
2670 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2671 }
2672#endif
2673
2674void SSL_set_info_callback(SSL *ssl,
2675 void (*cb)(const SSL *ssl,int type,int val))
2676 {
2677 ssl->info_callback=cb;
2678 }
2679
2680/* One compiler (Diab DCC) doesn't like argument names in returned
2681 function pointer. */
2682void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2683 {
2684 return ssl->info_callback;
2685 }
2686
2687int SSL_state(const SSL *ssl)
2688 {
2689 return(ssl->state);
2690 }
2691
2692void SSL_set_state(SSL *ssl, int state)
2693 {
2694 ssl->state = state;
2695 }
2696
2697void SSL_set_verify_result(SSL *ssl,long arg)
2698 {
2699 ssl->verify_result=arg;
2700 }
2701
2702long SSL_get_verify_result(const SSL *ssl)
2703 {
2704 return(ssl->verify_result);
2705 }
2706
2707int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2708 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2709 {
2710 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2711 new_func, dup_func, free_func);
2712 }
2713
2714int SSL_set_ex_data(SSL *s,int idx,void *arg)
2715 {
2716 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2717 }
2718
2719void *SSL_get_ex_data(const SSL *s,int idx)
2720 {
2721 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2722 }
2723
2724int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2725 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2726 {
2727 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2728 new_func, dup_func, free_func);
2729 }
2730
2731int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2732 {
2733 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2734 }
2735
2736void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2737 {
2738 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2739 }
2740
2741int ssl_ok(SSL *s)
2742 {
2743 return(1);
2744 }
2745
2746X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2747 {
2748 return(ctx->cert_store);
2749 }
2750
2751void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2752 {
2753 if (ctx->cert_store != NULL)
2754 X509_STORE_free(ctx->cert_store);
2755 ctx->cert_store=store;
2756 }
2757
2758int SSL_want(const SSL *s)
2759 {
2760 return(s->rwstate);
2761 }
2762
2763/*!
2764 * \brief Set the callback for generating temporary RSA keys.
2765 * \param ctx the SSL context.
2766 * \param cb the callback
2767 */
2768
Adam Langley95c29f32014-06-20 12:00:00 -07002769void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2770 int is_export,
2771 int keylength))
2772 {
2773 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2774 }
2775
2776void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2777 int is_export,
2778 int keylength))
2779 {
2780 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2781 }
Adam Langley95c29f32014-06-20 12:00:00 -07002782
2783#ifdef DOXYGEN
2784/*!
2785 * \brief The RSA temporary key callback function.
2786 * \param ssl the SSL session.
2787 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2788 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2789 * of the required key in bits.
2790 * \return the temporary RSA key.
2791 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2792 */
2793
2794RSA *cb(SSL *ssl,int is_export,int keylength)
2795 {}
2796#endif
2797
2798/*!
2799 * \brief Set the callback for generating temporary DH keys.
2800 * \param ctx the SSL context.
2801 * \param dh the callback
2802 */
2803
Adam Langley95c29f32014-06-20 12:00:00 -07002804void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2805 int keylength))
2806 {
2807 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2808 }
2809
2810void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2811 int keylength))
2812 {
2813 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2814 }
Adam Langley95c29f32014-06-20 12:00:00 -07002815
Adam Langley95c29f32014-06-20 12:00:00 -07002816void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2817 int keylength))
2818 {
2819 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2820 }
2821
2822void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2823 int keylength))
2824 {
2825 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2826 }
Adam Langley95c29f32014-06-20 12:00:00 -07002827
Adam Langley95c29f32014-06-20 12:00:00 -07002828int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2829 {
2830 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2831 {
2832 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2833 return 0;
2834 }
2835 if (ctx->psk_identity_hint != NULL)
2836 OPENSSL_free(ctx->psk_identity_hint);
2837 if (identity_hint != NULL)
2838 {
2839 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2840 if (ctx->psk_identity_hint == NULL)
2841 return 0;
2842 }
2843 else
2844 ctx->psk_identity_hint = NULL;
2845 return 1;
2846 }
2847
2848int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
2849 {
2850 if (s == NULL)
2851 return 0;
2852
Adam Langley95c29f32014-06-20 12:00:00 -07002853 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2854 {
2855 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2856 return 0;
2857 }
Adam Langley0289c732014-06-20 12:00:00 -07002858
David Benjamin688d8df2014-11-02 23:06:42 -05002859 /* Clear currently configured hint, if any. */
Adam Langley0289c732014-06-20 12:00:00 -07002860 if (s->psk_identity_hint != NULL)
2861 {
2862 OPENSSL_free(s->psk_identity_hint);
2863 s->psk_identity_hint = NULL;
2864 }
Adam Langley0289c732014-06-20 12:00:00 -07002865
Adam Langley95c29f32014-06-20 12:00:00 -07002866 if (identity_hint != NULL)
2867 {
David Benjamin688d8df2014-11-02 23:06:42 -05002868 s->psk_identity_hint = BUF_strdup(identity_hint);
2869 if (s->psk_identity_hint == NULL)
2870 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002871 }
Adam Langley95c29f32014-06-20 12:00:00 -07002872 return 1;
2873 }
2874
2875const char *SSL_get_psk_identity_hint(const SSL *s)
2876 {
Adam Langley0289c732014-06-20 12:00:00 -07002877 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002878 return NULL;
David Benjamin688d8df2014-11-02 23:06:42 -05002879 return s->psk_identity_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07002880 }
2881
2882const char *SSL_get_psk_identity(const SSL *s)
2883 {
2884 if (s == NULL || s->session == NULL)
2885 return NULL;
2886 return(s->session->psk_identity);
2887 }
2888
2889void SSL_set_psk_client_callback(SSL *s,
2890 unsigned int (*cb)(SSL *ssl, const char *hint,
2891 char *identity, unsigned int max_identity_len, unsigned char *psk,
2892 unsigned int max_psk_len))
2893 {
2894 s->psk_client_callback = cb;
2895 }
2896
2897void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
2898 unsigned int (*cb)(SSL *ssl, const char *hint,
2899 char *identity, unsigned int max_identity_len, unsigned char *psk,
2900 unsigned int max_psk_len))
2901 {
2902 ctx->psk_client_callback = cb;
2903 }
2904
2905void SSL_set_psk_server_callback(SSL *s,
2906 unsigned int (*cb)(SSL *ssl, const char *identity,
2907 unsigned char *psk, unsigned int max_psk_len))
2908 {
2909 s->psk_server_callback = cb;
2910 }
2911
2912void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
2913 unsigned int (*cb)(SSL *ssl, const char *identity,
2914 unsigned char *psk, unsigned int max_psk_len))
2915 {
2916 ctx->psk_server_callback = cb;
2917 }
Adam Langley95c29f32014-06-20 12:00:00 -07002918
2919void 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))
2920 {
2921 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2922 }
2923void 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))
2924 {
2925 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2926 }
2927
David Benjamin859ec3c2014-09-02 16:29:36 -04002928void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio)
2929 {
2930 if (ctx->keylog_bio != NULL)
2931 BIO_free(ctx->keylog_bio);
2932 ctx->keylog_bio = keylog_bio;
2933 }
2934
2935static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len)
2936 {
2937 static const char hextable[] = "0123456789abcdef";
2938 uint8_t *out;
2939 size_t i;
2940
2941 if (!CBB_add_space(cbb, &out, in_len * 2))
2942 {
2943 return 0;
2944 }
2945
2946 for (i = 0; i < in_len; i++)
2947 {
2948 *(out++) = (uint8_t)hextable[in[i] >> 4];
2949 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2950 }
2951 return 1;
2952 }
2953
2954int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
2955 const uint8_t *encrypted_premaster, size_t encrypted_premaster_len,
2956 const uint8_t *premaster, size_t premaster_len)
2957 {
2958 BIO *bio = ctx->keylog_bio;
2959 CBB cbb;
2960 uint8_t *out;
2961 size_t out_len;
2962 int ret;
2963
2964 if (bio == NULL)
2965 {
2966 return 1;
2967 }
2968
2969 if (encrypted_premaster_len < 8)
2970 {
2971 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange, ERR_R_INTERNAL_ERROR);
2972 return 0;
2973 }
2974
2975 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len*2 + 1))
2976 {
2977 return 0;
2978 }
2979 if (!CBB_add_bytes(&cbb, (const uint8_t*)"RSA ", 4) ||
2980 /* Only the first 8 bytes of the encrypted premaster secret are
2981 * logged. */
2982 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2983 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
2984 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2985 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
2986 !CBB_finish(&cbb, &out, &out_len))
2987 {
2988 CBB_cleanup(&cbb);
2989 return 0;
2990 }
2991
2992 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2993 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2994 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2995
2996 OPENSSL_free(out);
2997 return ret;
2998 }
2999
3000int ssl_ctx_log_master_secret(SSL_CTX *ctx,
3001 const uint8_t *client_random, size_t client_random_len,
3002 const uint8_t *master, size_t master_len)
3003 {
3004 BIO *bio = ctx->keylog_bio;
3005 CBB cbb;
3006 uint8_t *out;
3007 size_t out_len;
3008 int ret;
3009
3010 if (bio == NULL)
3011 {
3012 return 1;
3013 }
3014
3015 if (client_random_len != 32)
3016 {
3017 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
3018 return 0;
3019 }
3020
3021 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len*2 + 1))
3022 {
3023 return 0;
3024 }
3025 if (!CBB_add_bytes(&cbb, (const uint8_t*)"CLIENT_RANDOM ", 14) ||
3026 !cbb_add_hex(&cbb, client_random, 32) ||
3027 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3028 !cbb_add_hex(&cbb, master, master_len) ||
3029 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3030 !CBB_finish(&cbb, &out, &out_len))
3031 {
3032 CBB_cleanup(&cbb);
3033 return 0;
3034 }
3035
3036 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3037 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3038 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3039
3040 OPENSSL_free(out);
3041 return ret;
3042 }
3043
Adam Langleyadb739e2014-06-20 12:00:00 -07003044int SSL_cutthrough_complete(const SSL *s)
3045 {
3046 return (!s->server && /* cutthrough only applies to clients */
3047 !s->hit && /* full-handshake */
3048 s->version >= SSL3_VERSION &&
3049 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3050 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3051 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3052 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3053 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
David Benjamin7e3305e2014-07-28 14:52:32 -04003054 s->state == SSL3_ST_CR_CHANGE ||
Adam Langleyadb739e2014-06-20 12:00:00 -07003055 s->state == SSL3_ST_CR_FINISHED_A));
3056 }
3057
Adam Langley95f22882014-06-20 12:00:00 -07003058void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3059 size_t* ssl_session_size)
3060{
3061 *ssl_size = sizeof(SSL);
3062 *ssl_ctx_size = sizeof(SSL_CTX);
3063 *ssl_session_size = sizeof(SSL_SESSION);
3064}
3065
Adam Langleyadb739e2014-06-20 12:00:00 -07003066int ssl3_can_cutthrough(const SSL *s)
3067 {
3068 const SSL_CIPHER *c;
3069
3070 /* require a strong enough cipher */
3071 if (SSL_get_cipher_bits(s, NULL) < 128)
3072 return 0;
3073
3074 /* require ALPN or NPN extension */
David Benjamin457112e2014-08-27 14:35:09 -04003075 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen)
Adam Langleyadb739e2014-06-20 12:00:00 -07003076 {
3077 return 0;
3078 }
3079
3080 /* require a forward-secret cipher */
3081 c = SSL_get_current_cipher(s);
3082 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3083 c->algorithm_mkey != SSL_kEECDH))
3084 {
3085 return 0;
3086 }
3087
3088 return 1;
3089 }
3090
Adam Langley29707792014-06-20 12:00:00 -07003091/* ssl_get_max_version returns the maximum SSL/TLS version number supported by
3092 * |s|, or zero if all versions are disabled. */
3093int ssl_get_max_version(const SSL *s)
3094 {
3095 /* Only one version supported for DTLS. */
3096 if (s->version == DTLS1_VERSION)
3097 return DTLS1_VERSION;
3098
3099 if (!(s->options & SSL_OP_NO_TLSv1_2))
3100 return TLS1_2_VERSION;
3101 if (!(s->options & SSL_OP_NO_TLSv1_1))
3102 return TLS1_1_VERSION;
3103 if (!(s->options & SSL_OP_NO_TLSv1))
3104 return TLS1_VERSION;
3105 if (!(s->options & SSL_OP_NO_SSLv3))
3106 return SSL3_VERSION;
Adam Langley29707792014-06-20 12:00:00 -07003107 return 0;
3108 }
3109
Adam Langley95c29f32014-06-20 12:00:00 -07003110/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3111 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3112 * any. If EVP_MD pointer is passed, initializes ctx with this md
3113 * Returns newly allocated ctx;
3114 */
3115
3116EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3117{
3118 ssl_clear_hash_ctx(hash);
3119 *hash = EVP_MD_CTX_create();
David Benjaminae3e4872014-11-18 21:52:26 -05003120 if (md != NULL && *hash != NULL &&
3121 !EVP_DigestInit_ex(*hash, md, NULL))
Adam Langley69a01602014-11-17 17:26:55 -08003122 {
David Benjaminae3e4872014-11-18 21:52:26 -05003123 EVP_MD_CTX_destroy(*hash);
3124 *hash = NULL;
Adam Langley69a01602014-11-17 17:26:55 -08003125 }
Adam Langley95c29f32014-06-20 12:00:00 -07003126 return *hash;
3127}
3128void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3129{
3130
3131 if (*hash) EVP_MD_CTX_destroy(*hash);
3132 *hash=NULL;
3133}
3134
3135void SSL_set_debug(SSL *s, int debug)
3136 {
3137 s->debug = debug;
3138 }
3139
3140int SSL_cache_hit(SSL *s)
3141 {
3142 return s->hit;
3143 }
3144
3145int SSL_is_server(SSL *s)
3146 {
3147 return s->server;
3148 }