blob: 535baeb2dd0f877f6c56d388efb61bdd399de146 [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
David Benjaminbb15e3d2014-11-29 16:03:41 -0500246 s->method->ssl_clear(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700247 return(1);
248 }
249
250/** Used to change an SSL_CTXs default SSL method type */
251int SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth)
252 {
253 STACK_OF(SSL_CIPHER) *sk;
254
255 ctx->method=meth;
256
Adam Langley858a88d2014-06-20 12:00:00 -0700257 sk=ssl_create_cipher_list(
258 ctx->method, &ctx->cipher_list, &ctx->cipher_list_by_id,
David Benjamin5e4f6e92014-11-17 03:23:24 -0500259 SSL_DEFAULT_CIPHER_LIST,
Adam Langley858a88d2014-06-20 12:00:00 -0700260 ctx->cert);
261
Adam Langley95c29f32014-06-20 12:00:00 -0700262 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
263 {
264 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_ssl_version, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
265 return(0);
266 }
267 return(1);
268 }
269
270SSL *SSL_new(SSL_CTX *ctx)
271 {
272 SSL *s;
273
274 if (ctx == NULL)
275 {
276 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
277 return(NULL);
278 }
279 if (ctx->method == NULL)
280 {
281 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
282 return(NULL);
283 }
284
285 s=(SSL *)OPENSSL_malloc(sizeof(SSL));
286 if (s == NULL) goto err;
287 memset(s,0,sizeof(SSL));
288
289 s->options=ctx->options;
290 s->mode=ctx->mode;
291 s->max_cert_list=ctx->max_cert_list;
292
293 if (ctx->cert != NULL)
294 {
295 /* Earlier library versions used to copy the pointer to
296 * the CERT, not its contents; only when setting new
297 * parameters for the per-SSL copy, ssl_cert_new would be
298 * called (and the direct reference to the per-SSL_CTX
299 * settings would be lost, but those still were indirectly
300 * accessed for various purposes, and for that reason they
301 * used to be known as s->ctx->default_cert).
302 * Now we don't look at the SSL_CTX's CERT after having
303 * duplicated it once. */
304
305 s->cert = ssl_cert_dup(ctx->cert);
306 if (s->cert == NULL)
307 goto err;
308 }
309 else
310 s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
311
312 s->read_ahead=ctx->read_ahead;
313 s->msg_callback=ctx->msg_callback;
314 s->msg_callback_arg=ctx->msg_callback_arg;
315 s->verify_mode=ctx->verify_mode;
316#if 0
317 s->verify_depth=ctx->verify_depth;
318#endif
319 s->sid_ctx_length=ctx->sid_ctx_length;
320 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
321 memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
322 s->verify_callback=ctx->default_verify_callback;
323 s->generate_session_id=ctx->generate_session_id;
324
325 s->param = X509_VERIFY_PARAM_new();
326 if (!s->param)
327 goto err;
328 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
329#if 0
330 s->purpose = ctx->purpose;
331 s->trust = ctx->trust;
332#endif
333 s->quiet_shutdown=ctx->quiet_shutdown;
334 s->max_send_fragment = ctx->max_send_fragment;
335
336 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
337 s->ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700338 s->tlsext_debug_cb = 0;
339 s->tlsext_debug_arg = NULL;
340 s->tlsext_ticket_expected = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700341 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
342 s->initial_ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700343 if (ctx->tlsext_ecpointformatlist)
344 {
345 s->tlsext_ecpointformatlist =
346 BUF_memdup(ctx->tlsext_ecpointformatlist,
347 ctx->tlsext_ecpointformatlist_length);
348 if (!s->tlsext_ecpointformatlist)
349 goto err;
350 s->tlsext_ecpointformatlist_length =
351 ctx->tlsext_ecpointformatlist_length;
352 }
353 if (ctx->tlsext_ellipticcurvelist)
354 {
355 s->tlsext_ellipticcurvelist =
356 BUF_memdup(ctx->tlsext_ellipticcurvelist,
David Benjamin072334d2014-07-13 16:24:27 -0400357 ctx->tlsext_ellipticcurvelist_length * 2);
Adam Langley95c29f32014-06-20 12:00:00 -0700358 if (!s->tlsext_ellipticcurvelist)
359 goto err;
360 s->tlsext_ellipticcurvelist_length =
361 ctx->tlsext_ellipticcurvelist_length;
362 }
Adam Langley95c29f32014-06-20 12:00:00 -0700363 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700364
365 if (s->ctx->alpn_client_proto_list)
366 {
David Benjamin072c9532014-07-26 11:44:25 -0400367 s->alpn_client_proto_list = BUF_memdup(
368 s->ctx->alpn_client_proto_list,
369 s->ctx->alpn_client_proto_list_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700370 if (s->alpn_client_proto_list == NULL)
371 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700372 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
373 }
Adam Langley95c29f32014-06-20 12:00:00 -0700374
375 s->verify_result=X509_V_OK;
376
377 s->method=ctx->method;
378
379 if (!s->method->ssl_new(s))
380 goto err;
381
382 s->references=1;
Adam Langley95c29f32014-06-20 12:00:00 -0700383
384 SSL_clear(s);
385
386 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
387
Adam Langley0289c732014-06-20 12:00:00 -0700388 s->psk_identity_hint = NULL;
389 if (ctx->psk_identity_hint)
390 {
391 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
392 if (s->psk_identity_hint == NULL)
393 goto err;
394 }
Adam Langley95c29f32014-06-20 12:00:00 -0700395 s->psk_client_callback=ctx->psk_client_callback;
396 s->psk_server_callback=ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700397
David Benjaminff42cc12014-11-26 00:16:14 -0500398 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
399 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200400
Adam Langley95c29f32014-06-20 12:00:00 -0700401 return(s);
402err:
403 if (s != NULL)
404 {
405 if (s->cert != NULL)
406 ssl_cert_free(s->cert);
407 if (s->ctx != NULL)
408 SSL_CTX_free(s->ctx); /* decrement reference count */
409 OPENSSL_free(s);
410 }
411 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
412 return(NULL);
413 }
414
415int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
416 unsigned int sid_ctx_len)
417 {
418 if(sid_ctx_len > sizeof ctx->sid_ctx)
419 {
420 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
421 return 0;
422 }
423 ctx->sid_ctx_length=sid_ctx_len;
424 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
425
426 return 1;
427 }
428
429int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
430 unsigned int sid_ctx_len)
431 {
432 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
433 {
434 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
435 return 0;
436 }
437 ssl->sid_ctx_length=sid_ctx_len;
438 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
439
440 return 1;
441 }
442
443int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
444 {
445 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
446 ctx->generate_session_id = cb;
447 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
448 return 1;
449 }
450
451int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
452 {
453 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
454 ssl->generate_session_id = cb;
455 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
456 return 1;
457 }
458
459int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
460 unsigned int id_len)
461 {
462 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
463 * we can "construct" a session to give us the desired check - ie. to
464 * find if there's a session in the hash table that would conflict with
465 * any new session built out of this id/id_len and the ssl_version in
466 * use by this SSL. */
467 SSL_SESSION r, *p;
468
469 if(id_len > sizeof r.session_id)
470 return 0;
471
472 r.ssl_version = ssl->version;
473 r.session_id_length = id_len;
474 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700475
476 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
477 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
478 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
479 return (p != NULL);
480 }
481
482int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
483 {
484 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
485 }
486
487int SSL_set_purpose(SSL *s, int purpose)
488 {
489 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
490 }
491
492int SSL_CTX_set_trust(SSL_CTX *s, int trust)
493 {
494 return X509_VERIFY_PARAM_set_trust(s->param, trust);
495 }
496
497int SSL_set_trust(SSL *s, int trust)
498 {
499 return X509_VERIFY_PARAM_set_trust(s->param, trust);
500 }
501
502int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
503 {
504 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
505 }
506
507int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
508 {
509 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
510 }
511
Adam Langley858a88d2014-06-20 12:00:00 -0700512void ssl_cipher_preference_list_free(
513 struct ssl_cipher_preference_list_st *cipher_list)
514 {
515 sk_SSL_CIPHER_free(cipher_list->ciphers);
516 OPENSSL_free(cipher_list->in_group_flags);
517 OPENSSL_free(cipher_list);
518 }
519
520struct ssl_cipher_preference_list_st*
521ssl_cipher_preference_list_dup(
522 struct ssl_cipher_preference_list_st *cipher_list)
523 {
524 struct ssl_cipher_preference_list_st* ret = NULL;
525 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
526
527 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
528 if (!ret)
529 goto err;
530 ret->ciphers = NULL;
531 ret->in_group_flags = NULL;
532 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
533 if (!ret->ciphers)
534 goto err;
David Benjamin072c9532014-07-26 11:44:25 -0400535 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
Adam Langley858a88d2014-06-20 12:00:00 -0700536 if (!ret->in_group_flags)
537 goto err;
Adam Langley858a88d2014-06-20 12:00:00 -0700538 return ret;
539
540err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400541 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700542 sk_SSL_CIPHER_free(ret->ciphers);
543 if (ret)
544 OPENSSL_free(ret);
545 return NULL;
546 }
547
548struct ssl_cipher_preference_list_st*
549ssl_cipher_preference_list_from_ciphers(STACK_OF(SSL_CIPHER) *ciphers)
550 {
551 struct ssl_cipher_preference_list_st* ret = NULL;
552 size_t n = sk_SSL_CIPHER_num(ciphers);
553
554 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
555 if (!ret)
556 goto err;
557 ret->ciphers = NULL;
558 ret->in_group_flags = NULL;
559 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
560 if (!ret->ciphers)
561 goto err;
562 ret->in_group_flags = OPENSSL_malloc(n);
563 if (!ret->in_group_flags)
564 goto err;
565 memset(ret->in_group_flags, 0, n);
566 return ret;
567
568err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400569 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700570 sk_SSL_CIPHER_free(ret->ciphers);
571 if (ret)
572 OPENSSL_free(ret);
573 return NULL;
574 }
575
Adam Langley95c29f32014-06-20 12:00:00 -0700576X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
577 {
578 return ctx->param;
579 }
580
581X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
582 {
583 return ssl->param;
584 }
585
586void SSL_certs_clear(SSL *s)
587 {
588 ssl_cert_clear_certs(s->cert);
589 }
590
591void SSL_free(SSL *s)
592 {
593 int i;
594
595 if(s == NULL)
596 return;
597
598 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
599#ifdef REF_PRINT
600 REF_PRINT("SSL",s);
601#endif
602 if (i > 0) return;
603#ifdef REF_CHECK
604 if (i < 0)
605 {
606 fprintf(stderr,"SSL_free, bad reference count\n");
607 abort(); /* ok */
608 }
609#endif
610
611 if (s->param)
612 X509_VERIFY_PARAM_free(s->param);
613
614 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
615
616 if (s->bbio != NULL)
617 {
618 /* If the buffering BIO is in place, pop it off */
619 if (s->bbio == s->wbio)
620 {
621 s->wbio=BIO_pop(s->wbio);
622 }
623 BIO_free(s->bbio);
624 s->bbio=NULL;
625 }
626 if (s->rbio != NULL)
627 BIO_free_all(s->rbio);
628 if ((s->wbio != NULL) && (s->wbio != s->rbio))
629 BIO_free_all(s->wbio);
630
631 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
632
633 /* add extra stuff */
Adam Langley858a88d2014-06-20 12:00:00 -0700634 if (s->cipher_list != NULL)
635 ssl_cipher_preference_list_free(s->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -0700636 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
637
638 /* Make the next call work :-) */
639 if (s->session != NULL)
640 {
641 ssl_clear_bad_session(s);
642 SSL_SESSION_free(s->session);
643 }
644
645 ssl_clear_cipher_ctx(s);
646 ssl_clear_hash_ctx(&s->read_hash);
647 ssl_clear_hash_ctx(&s->write_hash);
648
649 if (s->cert != NULL) ssl_cert_free(s->cert);
650 /* Free up if allocated */
651
Adam Langley95c29f32014-06-20 12:00:00 -0700652 if (s->tlsext_hostname)
653 OPENSSL_free(s->tlsext_hostname);
654 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700655 if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
656 if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -0700657 if (s->alpn_client_proto_list)
658 OPENSSL_free(s->alpn_client_proto_list);
Adam Langley1258b6a2014-06-20 12:00:00 -0700659 if (s->tlsext_channel_id_private)
660 EVP_PKEY_free(s->tlsext_channel_id_private);
Adam Langley95c29f32014-06-20 12:00:00 -0700661
Adam Langley0289c732014-06-20 12:00:00 -0700662 if (s->psk_identity_hint)
663 OPENSSL_free(s->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -0700664
Adam Langley95c29f32014-06-20 12:00:00 -0700665 if (s->client_CA != NULL)
666 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
667
668 if (s->method != NULL) s->method->ssl_free(s);
669
670 if (s->ctx) SSL_CTX_free(s->ctx);
671
Adam Langley95c29f32014-06-20 12:00:00 -0700672 if (s->next_proto_negotiated)
673 OPENSSL_free(s->next_proto_negotiated);
Adam Langley95c29f32014-06-20 12:00:00 -0700674
675 if (s->srtp_profiles)
676 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
677
Adam Langley95c29f32014-06-20 12:00:00 -0700678 OPENSSL_free(s);
679 }
680
681void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
682 {
683 /* If the output buffering BIO is still in place, remove it
684 */
685 if (s->bbio != NULL)
686 {
687 if (s->wbio == s->bbio)
688 {
689 s->wbio=s->wbio->next_bio;
690 s->bbio->next_bio=NULL;
691 }
692 }
693 if ((s->rbio != NULL) && (s->rbio != rbio))
694 BIO_free_all(s->rbio);
695 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
696 BIO_free_all(s->wbio);
697 s->rbio=rbio;
698 s->wbio=wbio;
699 }
700
701BIO *SSL_get_rbio(const SSL *s)
702 { return(s->rbio); }
703
704BIO *SSL_get_wbio(const SSL *s)
705 { return(s->wbio); }
706
707int SSL_get_fd(const SSL *s)
708 {
709 return(SSL_get_rfd(s));
710 }
711
712int SSL_get_rfd(const SSL *s)
713 {
714 int ret= -1;
715 BIO *b,*r;
716
717 b=SSL_get_rbio(s);
718 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
719 if (r != NULL)
720 BIO_get_fd(r,&ret);
721 return(ret);
722 }
723
724int SSL_get_wfd(const SSL *s)
725 {
726 int ret= -1;
727 BIO *b,*r;
728
729 b=SSL_get_wbio(s);
730 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
731 if (r != NULL)
732 BIO_get_fd(r,&ret);
733 return(ret);
734 }
735
736#ifndef OPENSSL_NO_SOCK
737int SSL_set_fd(SSL *s,int fd)
738 {
739 int ret=0;
740 BIO *bio=NULL;
741
742 bio=BIO_new(BIO_s_fd());
743
744 if (bio == NULL)
745 {
746 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
747 goto err;
748 }
749 BIO_set_fd(bio,fd,BIO_NOCLOSE);
750 SSL_set_bio(s,bio,bio);
751 ret=1;
752err:
753 return(ret);
754 }
755
756int SSL_set_wfd(SSL *s,int fd)
757 {
758 int ret=0;
759 BIO *bio=NULL;
760
761 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_FD)
762 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
763 {
764 bio=BIO_new(BIO_s_fd());
765
766 if (bio == NULL)
767 {
768 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
769 goto err;
770 }
771 BIO_set_fd(bio,fd,BIO_NOCLOSE);
772 SSL_set_bio(s,SSL_get_rbio(s),bio);
773 }
774 else
775 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
776 ret=1;
777err:
778 return(ret);
779 }
780
781int SSL_set_rfd(SSL *s,int fd)
782 {
783 int ret=0;
784 BIO *bio=NULL;
785
786 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_FD)
787 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
788 {
789 bio=BIO_new(BIO_s_fd());
790
791 if (bio == NULL)
792 {
793 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
794 goto err;
795 }
796 BIO_set_fd(bio,fd,BIO_NOCLOSE);
797 SSL_set_bio(s,bio,SSL_get_wbio(s));
798 }
799 else
800 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
801 ret=1;
802err:
803 return(ret);
804 }
805#endif
806
807
808/* return length of latest Finished message we sent, copy to 'buf' */
809size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
810 {
811 size_t ret = 0;
812
813 if (s->s3 != NULL)
814 {
815 ret = s->s3->tmp.finish_md_len;
816 if (count > ret)
817 count = ret;
818 memcpy(buf, s->s3->tmp.finish_md, count);
819 }
820 return ret;
821 }
822
823/* return length of latest Finished message we expected, copy to 'buf' */
824size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
825 {
826 size_t ret = 0;
827
828 if (s->s3 != NULL)
829 {
830 ret = s->s3->tmp.peer_finish_md_len;
831 if (count > ret)
832 count = ret;
833 memcpy(buf, s->s3->tmp.peer_finish_md, count);
834 }
835 return ret;
836 }
837
838
839int SSL_get_verify_mode(const SSL *s)
840 {
841 return(s->verify_mode);
842 }
843
844int SSL_get_verify_depth(const SSL *s)
845 {
846 return X509_VERIFY_PARAM_get_depth(s->param);
847 }
848
849int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
850 {
851 return(s->verify_callback);
852 }
853
854int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
855 {
856 return(ctx->verify_mode);
857 }
858
859int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
860 {
861 return X509_VERIFY_PARAM_get_depth(ctx->param);
862 }
863
864int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
865 {
866 return(ctx->default_verify_callback);
867 }
868
869void SSL_set_verify(SSL *s,int mode,
870 int (*callback)(int ok,X509_STORE_CTX *ctx))
871 {
872 s->verify_mode=mode;
873 if (callback != NULL)
874 s->verify_callback=callback;
875 }
876
877void SSL_set_verify_depth(SSL *s,int depth)
878 {
879 X509_VERIFY_PARAM_set_depth(s->param, depth);
880 }
881
882void SSL_set_read_ahead(SSL *s,int yes)
883 {
884 s->read_ahead=yes;
885 }
886
887int SSL_get_read_ahead(const SSL *s)
888 {
889 return(s->read_ahead);
890 }
891
892int SSL_pending(const SSL *s)
893 {
894 /* SSL_pending cannot work properly if read-ahead is enabled
895 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
896 * and it is impossible to fix since SSL_pending cannot report
897 * errors that may be observed while scanning the new data.
898 * (Note that SSL_pending() is often used as a boolean value,
899 * so we'd better not return -1.)
900 */
901 return(s->method->ssl_pending(s));
902 }
903
904X509 *SSL_get_peer_certificate(const SSL *s)
905 {
906 X509 *r;
907
908 if ((s == NULL) || (s->session == NULL))
909 r=NULL;
910 else
911 r=s->session->peer;
912
David Benjamin150c6172014-08-05 22:22:49 -0400913 if (r == NULL)
914 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700915
David Benjamin150c6172014-08-05 22:22:49 -0400916 return X509_up_ref(r);
Adam Langley95c29f32014-06-20 12:00:00 -0700917 }
918
919STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
920 {
921 STACK_OF(X509) *r;
922
923 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
924 r=NULL;
925 else
926 r=s->session->sess_cert->cert_chain;
927
928 /* If we are a client, cert_chain includes the peer's own
929 * certificate; if we are a server, it does not. */
930
931 return(r);
932 }
933
Adam Langley95c29f32014-06-20 12:00:00 -0700934/* Fix this so it checks all the valid key/cert options */
935int SSL_CTX_check_private_key(const SSL_CTX *ctx)
936 {
937 if ( (ctx == NULL) ||
938 (ctx->cert == NULL) ||
939 (ctx->cert->key->x509 == NULL))
940 {
941 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
942 return(0);
943 }
944 if (ctx->cert->key->privatekey == NULL)
945 {
946 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
947 return(0);
948 }
949 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
950 }
951
952/* Fix this function so that it takes an optional type parameter */
953int SSL_check_private_key(const SSL *ssl)
954 {
955 if (ssl == NULL)
956 {
957 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
958 return(0);
959 }
960 if (ssl->cert == NULL)
961 {
962 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
963 return 0;
964 }
965 if (ssl->cert->key->x509 == NULL)
966 {
967 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
968 return(0);
969 }
970 if (ssl->cert->key->privatekey == NULL)
971 {
972 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
973 return(0);
974 }
975 return(X509_check_private_key(ssl->cert->key->x509,
976 ssl->cert->key->privatekey));
977 }
978
979int SSL_accept(SSL *s)
980 {
981 if (s->handshake_func == 0)
982 /* Not properly initialized yet */
983 SSL_set_accept_state(s);
984
David Benjamin0b145c22014-11-26 20:10:09 -0500985 if (s->handshake_func != s->method->ssl_accept)
986 {
987 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
988 return -1;
989 }
990
991 return s->handshake_func(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700992 }
993
994int SSL_connect(SSL *s)
995 {
996 if (s->handshake_func == 0)
997 /* Not properly initialized yet */
998 SSL_set_connect_state(s);
999
David Benjamin0b145c22014-11-26 20:10:09 -05001000 if (s->handshake_func != s->method->ssl_connect)
1001 {
1002 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
1003 return -1;
1004 }
1005
1006 return s->handshake_func(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001007 }
1008
1009long SSL_get_default_timeout(const SSL *s)
1010 {
David Benjaminf4501342014-08-14 17:24:37 -04001011 return SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001012 }
1013
1014int SSL_read(SSL *s,void *buf,int num)
1015 {
1016 if (s->handshake_func == 0)
1017 {
1018 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
1019 return -1;
1020 }
1021
1022 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1023 {
1024 s->rwstate=SSL_NOTHING;
1025 return(0);
1026 }
1027 return(s->method->ssl_read(s,buf,num));
1028 }
1029
1030int SSL_peek(SSL *s,void *buf,int num)
1031 {
1032 if (s->handshake_func == 0)
1033 {
1034 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
1035 return -1;
1036 }
1037
1038 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1039 {
1040 return(0);
1041 }
1042 return(s->method->ssl_peek(s,buf,num));
1043 }
1044
1045int SSL_write(SSL *s,const void *buf,int num)
1046 {
1047 if (s->handshake_func == 0)
1048 {
1049 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
1050 return -1;
1051 }
1052
1053 if (s->shutdown & SSL_SENT_SHUTDOWN)
1054 {
1055 s->rwstate=SSL_NOTHING;
1056 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
1057 return(-1);
1058 }
1059 return(s->method->ssl_write(s,buf,num));
1060 }
1061
1062int SSL_shutdown(SSL *s)
1063 {
1064 /* Note that this function behaves differently from what one might
1065 * expect. Return values are 0 for no success (yet),
1066 * 1 for success; but calling it once is usually not enough,
1067 * even if blocking I/O is used (see ssl3_shutdown).
1068 */
1069
1070 if (s->handshake_func == 0)
1071 {
1072 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1073 return -1;
1074 }
1075
Adam Langleyb2cb0ec2014-09-02 14:28:49 -07001076 if (!SSL_in_init(s))
Adam Langley95c29f32014-06-20 12:00:00 -07001077 return(s->method->ssl_shutdown(s));
1078 else
1079 return(1);
1080 }
1081
1082int SSL_renegotiate(SSL *s)
1083 {
1084 if (s->renegotiate == 0)
1085 s->renegotiate=1;
1086
1087 s->new_session=1;
1088
1089 return(s->method->ssl_renegotiate(s));
1090 }
1091
1092int SSL_renegotiate_abbreviated(SSL *s)
1093 {
1094 if (s->renegotiate == 0)
1095 s->renegotiate=1;
1096
1097 s->new_session=0;
1098
1099 return(s->method->ssl_renegotiate(s));
1100 }
1101
1102int SSL_renegotiate_pending(SSL *s)
1103 {
1104 /* becomes true when negotiation is requested;
1105 * false again once a handshake has finished */
1106 return (s->renegotiate != 0);
1107 }
1108
1109long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1110 {
1111 long l;
1112
1113 switch (cmd)
1114 {
1115 case SSL_CTRL_GET_READ_AHEAD:
1116 return(s->read_ahead);
1117 case SSL_CTRL_SET_READ_AHEAD:
1118 l=s->read_ahead;
1119 s->read_ahead=larg;
1120 return(l);
1121
1122 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1123 s->msg_callback_arg = parg;
1124 return 1;
1125
1126 case SSL_CTRL_OPTIONS:
1127 return(s->options|=larg);
1128 case SSL_CTRL_CLEAR_OPTIONS:
1129 return(s->options&=~larg);
1130 case SSL_CTRL_MODE:
1131 return(s->mode|=larg);
1132 case SSL_CTRL_CLEAR_MODE:
1133 return(s->mode &=~larg);
1134 case SSL_CTRL_GET_MAX_CERT_LIST:
1135 return(s->max_cert_list);
1136 case SSL_CTRL_SET_MAX_CERT_LIST:
1137 l=s->max_cert_list;
1138 s->max_cert_list=larg;
1139 return(l);
1140 case SSL_CTRL_SET_MTU:
Adam Langley95c29f32014-06-20 12:00:00 -07001141 if (larg < (long)dtls1_min_mtu())
1142 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001143 if (SSL_IS_DTLS(s))
1144 {
1145 s->d1->mtu = larg;
1146 return larg;
1147 }
1148 return 0;
1149 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1150 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1151 return 0;
1152 s->max_send_fragment = larg;
1153 return 1;
1154 case SSL_CTRL_GET_RI_SUPPORT:
1155 if (s->s3)
1156 return s->s3->send_connection_binding;
1157 else return 0;
1158 case SSL_CTRL_CERT_FLAGS:
1159 return(s->cert->cert_flags|=larg);
1160 case SSL_CTRL_CLEAR_CERT_FLAGS:
1161 return(s->cert->cert_flags &=~larg);
1162
1163 case SSL_CTRL_GET_RAW_CIPHERLIST:
1164 if (parg)
1165 {
1166 if (s->cert->ciphers_raw == NULL)
1167 return 0;
1168 *(unsigned char **)parg = s->cert->ciphers_raw;
1169 return (int)s->cert->ciphers_rawlen;
1170 }
1171 else
David Benjamin39482a12014-07-20 13:30:15 -04001172 {
1173 /* Passing a NULL |parg| returns the size of a single
1174 * cipher suite value. */
1175 return 2;
1176 }
Adam Langley95c29f32014-06-20 12:00:00 -07001177 default:
1178 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1179 }
1180 }
1181
1182long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1183 {
1184 switch(cmd)
1185 {
1186 case SSL_CTRL_SET_MSG_CALLBACK:
1187 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1188 return 1;
1189
1190 default:
1191 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1192 }
1193 }
1194
1195LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1196 {
1197 return ctx->sessions;
1198 }
1199
1200long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1201 {
1202 long l;
1203
1204 switch (cmd)
1205 {
1206 case SSL_CTRL_GET_READ_AHEAD:
1207 return(ctx->read_ahead);
1208 case SSL_CTRL_SET_READ_AHEAD:
1209 l=ctx->read_ahead;
1210 ctx->read_ahead=larg;
1211 return(l);
1212
1213 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1214 ctx->msg_callback_arg = parg;
1215 return 1;
1216
1217 case SSL_CTRL_GET_MAX_CERT_LIST:
1218 return(ctx->max_cert_list);
1219 case SSL_CTRL_SET_MAX_CERT_LIST:
1220 l=ctx->max_cert_list;
1221 ctx->max_cert_list=larg;
1222 return(l);
1223
1224 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1225 l=ctx->session_cache_size;
1226 ctx->session_cache_size=larg;
1227 return(l);
1228 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1229 return(ctx->session_cache_size);
1230 case SSL_CTRL_SET_SESS_CACHE_MODE:
1231 l=ctx->session_cache_mode;
1232 ctx->session_cache_mode=larg;
1233 return(l);
1234 case SSL_CTRL_GET_SESS_CACHE_MODE:
1235 return(ctx->session_cache_mode);
1236
1237 case SSL_CTRL_SESS_NUMBER:
1238 return(lh_SSL_SESSION_num_items(ctx->sessions));
1239 case SSL_CTRL_SESS_CONNECT:
1240 return(ctx->stats.sess_connect);
1241 case SSL_CTRL_SESS_CONNECT_GOOD:
1242 return(ctx->stats.sess_connect_good);
1243 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1244 return(ctx->stats.sess_connect_renegotiate);
1245 case SSL_CTRL_SESS_ACCEPT:
1246 return(ctx->stats.sess_accept);
1247 case SSL_CTRL_SESS_ACCEPT_GOOD:
1248 return(ctx->stats.sess_accept_good);
1249 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1250 return(ctx->stats.sess_accept_renegotiate);
1251 case SSL_CTRL_SESS_HIT:
1252 return(ctx->stats.sess_hit);
1253 case SSL_CTRL_SESS_CB_HIT:
1254 return(ctx->stats.sess_cb_hit);
1255 case SSL_CTRL_SESS_MISSES:
1256 return(ctx->stats.sess_miss);
1257 case SSL_CTRL_SESS_TIMEOUTS:
1258 return(ctx->stats.sess_timeout);
1259 case SSL_CTRL_SESS_CACHE_FULL:
1260 return(ctx->stats.sess_cache_full);
1261 case SSL_CTRL_OPTIONS:
1262 return(ctx->options|=larg);
1263 case SSL_CTRL_CLEAR_OPTIONS:
1264 return(ctx->options&=~larg);
1265 case SSL_CTRL_MODE:
1266 return(ctx->mode|=larg);
1267 case SSL_CTRL_CLEAR_MODE:
1268 return(ctx->mode&=~larg);
1269 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1270 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1271 return 0;
1272 ctx->max_send_fragment = larg;
1273 return 1;
1274 case SSL_CTRL_CERT_FLAGS:
1275 return(ctx->cert->cert_flags|=larg);
1276 case SSL_CTRL_CLEAR_CERT_FLAGS:
1277 return(ctx->cert->cert_flags &=~larg);
1278 default:
1279 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1280 }
1281 }
1282
1283long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1284 {
1285 switch(cmd)
1286 {
1287 case SSL_CTRL_SET_MSG_CALLBACK:
1288 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1289 return 1;
1290
1291 default:
1292 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1293 }
1294 }
1295
1296int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1297 {
1298 long l;
1299 const SSL_CIPHER *a = in_a;
1300 const SSL_CIPHER *b = in_b;
1301 const long a_id = a->id;
1302 const long b_id = b->id;
1303
1304 l = a_id - b_id;
1305 if (l == 0L)
1306 return(0);
1307 else
1308 return((l > 0)?1:-1);
1309 }
1310
1311int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1312 {
1313 long l;
1314 const long a_id = (*ap)->id;
1315 const long b_id = (*bp)->id;
1316
1317 l = a_id - b_id;
1318 if (l == 0)
1319 return(0);
1320 else
1321 return((l > 0)?1:-1);
1322 }
1323
1324/** return a STACK of the ciphers available for the SSL and in order of
1325 * preference */
1326STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1327 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001328 if (s == NULL)
1329 return NULL;
1330
1331 if (s->cipher_list != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001332 {
Adam Langley858a88d2014-06-20 12:00:00 -07001333 return(s->cipher_list->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -07001334 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001335
1336 if (s->version >= TLS1_1_VERSION)
1337 {
1338 if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001339 return s->ctx->cipher_list_tls11->ciphers;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001340 }
1341
1342 if ((s->ctx != NULL) &&
1343 (s->ctx->cipher_list != NULL))
1344 {
Adam Langley858a88d2014-06-20 12:00:00 -07001345 return(s->ctx->cipher_list->ciphers);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001346 }
1347
Adam Langley95c29f32014-06-20 12:00:00 -07001348 return(NULL);
1349 }
1350
1351/** return a STACK of the ciphers available for the SSL and in order of
1352 * algorithm id */
1353STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1354 {
1355 if (s != NULL)
1356 {
1357 if (s->cipher_list_by_id != NULL)
1358 {
1359 return(s->cipher_list_by_id);
1360 }
1361 else if ((s->ctx != NULL) &&
1362 (s->ctx->cipher_list_by_id != NULL))
1363 {
1364 return(s->ctx->cipher_list_by_id);
1365 }
1366 }
1367 return(NULL);
1368 }
1369
1370/** The old interface to get the same thing as SSL_get_ciphers() */
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001371const char *SSL_get_cipher_list(const SSL *s, int n)
Adam Langley95c29f32014-06-20 12:00:00 -07001372 {
David Benjamin6f260012014-08-15 13:49:12 -04001373 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001374 STACK_OF(SSL_CIPHER) *sk;
1375
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001376 if (s == NULL)
1377 return NULL;
1378 sk = SSL_get_ciphers(s);
1379 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk))
1380 return NULL;
1381 c = sk_SSL_CIPHER_value(sk, n);
1382 if (c == NULL)
1383 return NULL;
1384 return c->name;
Adam Langley95c29f32014-06-20 12:00:00 -07001385 }
1386
1387/** specify the ciphers to be used by default by the SSL_CTX */
1388int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1389 {
1390 STACK_OF(SSL_CIPHER) *sk;
1391
1392 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1393 &ctx->cipher_list_by_id,str, ctx->cert);
1394 /* ssl_create_cipher_list may return an empty stack if it
1395 * was unable to find a cipher matching the given rule string
1396 * (for example if the rule string specifies a cipher which
1397 * has been disabled). This is not an error as far as
1398 * ssl_create_cipher_list is concerned, and hence
1399 * ctx->cipher_list and ctx->cipher_list_by_id has been
1400 * updated. */
1401 if (sk == NULL)
1402 return 0;
1403 else if (sk_SSL_CIPHER_num(sk) == 0)
1404 {
1405 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1406 return 0;
1407 }
1408 return 1;
1409 }
1410
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001411int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str)
1412 {
1413 STACK_OF(SSL_CIPHER) *sk;
1414
1415 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert);
1416 if (sk == NULL)
1417 return 0;
1418 else if (sk_SSL_CIPHER_num(sk) == 0)
1419 {
1420 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH);
1421 return 0;
1422 }
1423 return 1;
1424 }
1425
Adam Langley95c29f32014-06-20 12:00:00 -07001426/** specify the ciphers to be used by the SSL */
1427int SSL_set_cipher_list(SSL *s,const char *str)
1428 {
1429 STACK_OF(SSL_CIPHER) *sk;
1430
1431 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1432 &s->cipher_list_by_id,str, s->cert);
1433 /* see comment in SSL_CTX_set_cipher_list */
1434 if (sk == NULL)
1435 return 0;
1436 else if (sk_SSL_CIPHER_num(sk) == 0)
1437 {
1438 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1439 return 0;
1440 }
1441 return 1;
1442 }
1443
David Benjamin5491e3f2014-09-29 19:33:09 -04001444int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p)
Adam Langley95c29f32014-06-20 12:00:00 -07001445 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001446 size_t i;
David Benjamin6f260012014-08-15 13:49:12 -04001447 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001448 CERT *ct = s->cert;
David Benjamin5491e3f2014-09-29 19:33:09 -04001449 uint8_t *q;
Adam Langley95c29f32014-06-20 12:00:00 -07001450 /* Set disabled masks for this session */
1451 ssl_set_client_disabled(s);
1452
1453 if (sk == NULL) return(0);
1454 q=p;
1455
1456 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1457 {
1458 c=sk_SSL_CIPHER_value(sk,i);
1459 /* Skip disabled ciphers */
1460 if (c->algorithm_ssl & ct->mask_ssl ||
1461 c->algorithm_mkey & ct->mask_k ||
1462 c->algorithm_auth & ct->mask_a)
1463 continue;
David Benjamin39482a12014-07-20 13:30:15 -04001464 s2n(ssl3_get_cipher_value(c), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001465 }
David Benjamin5491e3f2014-09-29 19:33:09 -04001466 /* If all ciphers were disabled, return the error to the caller. */
1467 if (p == q)
Adam Langley95c29f32014-06-20 12:00:00 -07001468 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001469 return 0;
1470 }
1471
1472 /* Add SCSVs. */
1473 if (!s->renegotiate)
1474 {
1475 s2n(SSL3_CK_SCSV & 0xffff, p);
1476 }
1477 if (s->fallback_scsv)
1478 {
1479 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001480 }
1481
1482 return(p-q);
1483 }
1484
David Benjamin9f2c0d72014-10-21 22:00:19 -04001485STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001486 {
David Benjamin39482a12014-07-20 13:30:15 -04001487 CBS cipher_suites = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001488 const SSL_CIPHER *c;
1489 STACK_OF(SSL_CIPHER) *sk;
David Benjamin39482a12014-07-20 13:30:15 -04001490
Adam Langley95c29f32014-06-20 12:00:00 -07001491 if (s->s3)
1492 s->s3->send_connection_binding = 0;
1493
David Benjamin39482a12014-07-20 13:30:15 -04001494 if (CBS_len(&cipher_suites) % 2 != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001495 {
1496 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001497 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001498 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001499
1500 sk = sk_SSL_CIPHER_new_null();
1501 if (sk == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001502 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001503 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1504 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001505 }
1506
David Benjamin39482a12014-07-20 13:30:15 -04001507 if (!CBS_stow(&cipher_suites,
1508 &s->cert->ciphers_raw, &s->cert->ciphers_rawlen))
Adam Langley95c29f32014-06-20 12:00:00 -07001509 {
1510 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1511 goto err;
1512 }
Adam Langley95c29f32014-06-20 12:00:00 -07001513
David Benjamin39482a12014-07-20 13:30:15 -04001514 while (CBS_len(&cipher_suites) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001515 {
David Benjamin39482a12014-07-20 13:30:15 -04001516 uint16_t cipher_suite;
1517
1518 if (!CBS_get_u16(&cipher_suites, &cipher_suite))
1519 {
1520 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1521 goto err;
1522 }
1523
David Benjamin9f2c0d72014-10-21 22:00:19 -04001524 /* Check for SCSV. */
David Benjamin39482a12014-07-20 13:30:15 -04001525 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff))
Adam Langley95c29f32014-06-20 12:00:00 -07001526 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001527 /* SCSV is fatal if renegotiating. */
Adam Langley95c29f32014-06-20 12:00:00 -07001528 if (s->renegotiate)
1529 {
1530 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1531 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1532 goto err;
1533 }
1534 s->s3->send_connection_binding = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001535 continue;
1536 }
1537
David Benjamin9f2c0d72014-10-21 22:00:19 -04001538 /* Check for FALLBACK_SCSV. */
1539 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff))
Adam Langley29707792014-06-20 12:00:00 -07001540 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001541 if (s->version < ssl_get_max_version(s))
1542 {
1543 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1544 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1545 goto err;
1546 }
1547 continue;
Adam Langley29707792014-06-20 12:00:00 -07001548 }
1549
David Benjamin39482a12014-07-20 13:30:15 -04001550 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -07001551 if (c != NULL)
1552 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001553 if (!sk_SSL_CIPHER_push(sk, c))
Adam Langley95c29f32014-06-20 12:00:00 -07001554 {
1555 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1556 goto err;
1557 }
1558 }
1559 }
1560
David Benjamin9f2c0d72014-10-21 22:00:19 -04001561 return sk;
1562
Adam Langley95c29f32014-06-20 12:00:00 -07001563err:
David Benjamin9f2c0d72014-10-21 22:00:19 -04001564 if (sk != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001565 sk_SSL_CIPHER_free(sk);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001566 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001567 }
1568
1569
Adam Langley95c29f32014-06-20 12:00:00 -07001570/** return a servername extension value if provided in Client Hello, or NULL.
1571 * So far, only host_name types are defined (RFC 3546).
1572 */
1573
1574const char *SSL_get_servername(const SSL *s, const int type)
1575 {
1576 if (type != TLSEXT_NAMETYPE_host_name)
1577 return NULL;
1578
1579 return s->session && !s->tlsext_hostname ?
1580 s->session->tlsext_hostname :
1581 s->tlsext_hostname;
1582 }
1583
1584int SSL_get_servername_type(const SSL *s)
1585 {
1586 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1587 return TLSEXT_NAMETYPE_host_name;
1588 return -1;
1589 }
1590
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001591void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx)
1592 {
1593 ctx->signed_cert_timestamps_enabled = 1;
1594 }
1595
1596int SSL_enable_signed_cert_timestamps(SSL *ssl)
1597 {
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001598 ssl->signed_cert_timestamps_enabled = 1;
1599 return 1;
1600 }
1601
David Benjamin6c7aed02014-08-27 16:42:38 -04001602void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx)
1603 {
1604 ctx->ocsp_stapling_enabled = 1;
1605 }
1606
1607int SSL_enable_ocsp_stapling(SSL *ssl)
1608 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001609 ssl->ocsp_stapling_enabled = 1;
1610 return 1;
1611 }
1612
David Benjamin52d699f2014-11-25 01:35:18 -05001613void 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 +02001614 {
Adam Langley3cb50e02014-08-26 14:00:31 -07001615 SSL_SESSION *session = ssl->session;
1616
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001617 *out_len = 0;
1618 *out = NULL;
1619 if (ssl->server)
1620 return;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001621 if (!session || !session->tlsext_signed_cert_timestamp_list)
1622 return;
1623 *out = session->tlsext_signed_cert_timestamp_list;
1624 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1625 }
1626
David Benjamin52d699f2014-11-25 01:35:18 -05001627void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out, size_t *out_len)
David Benjamin6c7aed02014-08-27 16:42:38 -04001628 {
1629 SSL_SESSION *session = ssl->session;
1630
1631 *out_len = 0;
1632 *out = NULL;
1633 if (ssl->server)
1634 return;
1635 if (!session || !session->ocsp_response)
1636 return;
1637 *out = session->ocsp_response;
1638 *out_len = session->ocsp_response_length;
1639 }
1640
Adam Langley95c29f32014-06-20 12:00:00 -07001641/* SSL_select_next_proto implements the standard protocol selection. It is
1642 * expected that this function is called from the callback set by
1643 * SSL_CTX_set_next_proto_select_cb.
1644 *
1645 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1646 * strings. The length byte itself is not included in the length. A byte
1647 * string of length 0 is invalid. No byte string may be truncated.
1648 *
1649 * The current, but experimental algorithm for selecting the protocol is:
1650 *
1651 * 1) If the server doesn't support NPN then this is indicated to the
1652 * callback. In this case, the client application has to abort the connection
1653 * or have a default application level protocol.
1654 *
1655 * 2) If the server supports NPN, but advertises an empty list then the
1656 * client selects the first protcol in its list, but indicates via the
1657 * API that this fallback case was enacted.
1658 *
1659 * 3) Otherwise, the client finds the first protocol in the server's list
1660 * that it supports and selects this protocol. This is because it's
1661 * assumed that the server has better information about which protocol
1662 * a client should use.
1663 *
1664 * 4) If the client doesn't support any of the server's advertised
1665 * protocols, then this is treated the same as case 2.
1666 *
1667 * It returns either
1668 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1669 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1670 */
1671int 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)
1672 {
1673 unsigned int i, j;
1674 const unsigned char *result;
1675 int status = OPENSSL_NPN_UNSUPPORTED;
1676
1677 /* For each protocol in server preference order, see if we support it. */
1678 for (i = 0; i < server_len; )
1679 {
1680 for (j = 0; j < client_len; )
1681 {
1682 if (server[i] == client[j] &&
1683 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1684 {
1685 /* We found a match */
1686 result = &server[i];
1687 status = OPENSSL_NPN_NEGOTIATED;
1688 goto found;
1689 }
1690 j += client[j];
1691 j++;
1692 }
1693 i += server[i];
1694 i++;
1695 }
1696
1697 /* There's no overlap between our protocols and the server's list. */
1698 result = client;
1699 status = OPENSSL_NPN_NO_OVERLAP;
1700
1701 found:
1702 *out = (unsigned char *) result + 1;
1703 *outlen = result[0];
1704 return status;
1705 }
1706
Adam Langley95c29f32014-06-20 12:00:00 -07001707/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1708 * requested protocol for this connection and returns 0. If the client didn't
1709 * request any protocol, then *data is set to NULL.
1710 *
1711 * Note that the client can request any protocol it chooses. The value returned
1712 * from this function need not be a member of the list of supported protocols
1713 * provided by the callback.
1714 */
David Benjaminced551f2014-07-11 23:16:16 -04001715void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, unsigned *len)
Adam Langley95c29f32014-06-20 12:00:00 -07001716 {
1717 *data = s->next_proto_negotiated;
1718 if (!*data) {
1719 *len = 0;
1720 } else {
1721 *len = s->next_proto_negotiated_len;
1722 }
1723}
1724
1725/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1726 * TLS server needs a list of supported protocols for Next Protocol
1727 * Negotiation. The returned list must be in wire format. The list is returned
1728 * by setting |out| to point to it and |outlen| to its length. This memory will
1729 * not be modified, but one should assume that the SSL* keeps a reference to
1730 * it.
1731 *
1732 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1733 * such extension will be included in the ServerHello. */
1734void 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)
1735 {
1736 ctx->next_protos_advertised_cb = cb;
1737 ctx->next_protos_advertised_cb_arg = arg;
1738 }
1739
1740/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1741 * client needs to select a protocol from the server's provided list. |out|
1742 * must be set to point to the selected protocol (which may be within |in|).
1743 * The length of the protocol name must be written into |outlen|. The server's
1744 * advertised protocols are provided in |in| and |inlen|. The callback can
1745 * assume that |in| is syntactically valid.
1746 *
1747 * The client must select a protocol. It is fatal to the connection if this
1748 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1749 */
1750void 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)
1751 {
1752 ctx->next_proto_select_cb = cb;
1753 ctx->next_proto_select_cb_arg = arg;
1754 }
Adam Langley95c29f32014-06-20 12:00:00 -07001755
Adam Langley95c29f32014-06-20 12:00:00 -07001756/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1757 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1758 * length-prefixed strings).
1759 *
1760 * Returns 0 on success. */
1761int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1762 unsigned protos_len)
1763 {
1764 if (ctx->alpn_client_proto_list)
1765 OPENSSL_free(ctx->alpn_client_proto_list);
1766
David Benjamin072c9532014-07-26 11:44:25 -04001767 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001768 if (!ctx->alpn_client_proto_list)
1769 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001770 ctx->alpn_client_proto_list_len = protos_len;
1771
1772 return 0;
1773 }
1774
1775/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1776 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1777 * length-prefixed strings).
1778 *
1779 * Returns 0 on success. */
1780int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1781 unsigned protos_len)
1782 {
1783 if (ssl->alpn_client_proto_list)
1784 OPENSSL_free(ssl->alpn_client_proto_list);
1785
David Benjamin072c9532014-07-26 11:44:25 -04001786 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001787 if (!ssl->alpn_client_proto_list)
1788 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001789 ssl->alpn_client_proto_list_len = protos_len;
1790
1791 return 0;
1792 }
1793
1794/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1795 * during ClientHello processing in order to select an ALPN protocol from the
1796 * client's list of offered protocols. */
1797void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1798 int (*cb) (SSL *ssl,
1799 const unsigned char **out,
1800 unsigned char *outlen,
1801 const unsigned char *in,
1802 unsigned int inlen,
1803 void *arg),
1804 void *arg)
1805 {
1806 ctx->alpn_select_cb = cb;
1807 ctx->alpn_select_cb_arg = arg;
1808 }
1809
1810/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1811 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1812 * including the leading length-prefix byte). If the server didn't respond with
1813 * a negotiated protocol then |*len| will be zero. */
1814void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1815 unsigned *len)
1816 {
1817 *data = NULL;
1818 if (ssl->s3)
1819 *data = ssl->s3->alpn_selected;
1820 if (*data == NULL)
1821 *len = 0;
1822 else
1823 *len = ssl->s3->alpn_selected_len;
1824 }
Adam Langley95c29f32014-06-20 12:00:00 -07001825
1826int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1827 const char *label, size_t llen, const unsigned char *p, size_t plen,
1828 int use_context)
1829 {
1830 if (s->version < TLS1_VERSION)
1831 return -1;
1832
1833 return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1834 llen, p, plen,
1835 use_context);
1836 }
1837
1838static uint32_t ssl_session_hash(const SSL_SESSION *a)
1839 {
1840 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1841 ((uint32_t) a->session_id[1] << 8) ||
1842 ((uint32_t) a->session_id[2] << 16) ||
1843 ((uint32_t) a->session_id[3] << 24);
1844
1845 return hash;
1846 }
1847
1848/* NB: If this function (or indeed the hash function which uses a sort of
1849 * coarser function than this one) is changed, ensure
1850 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1851 * able to construct an SSL_SESSION that will collide with any existing session
1852 * with a matching session ID. */
1853static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1854 {
1855 if (a->ssl_version != b->ssl_version)
1856 return(1);
1857 if (a->session_id_length != b->session_id_length)
1858 return(1);
1859 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1860 }
1861
1862SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1863 {
1864 SSL_CTX *ret=NULL;
1865
1866 if (meth == NULL)
1867 {
1868 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1869 return(NULL);
1870 }
1871
Adam Langley95c29f32014-06-20 12:00:00 -07001872 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1873 {
1874 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1875 goto err;
1876 }
1877 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1878 if (ret == NULL)
1879 goto err;
1880
1881 memset(ret,0,sizeof(SSL_CTX));
1882
1883 ret->method=meth;
1884
1885 ret->cert_store=NULL;
1886 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1887 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1888 ret->session_cache_head=NULL;
1889 ret->session_cache_tail=NULL;
1890
1891 /* We take the system default */
David Benjaminf4501342014-08-14 17:24:37 -04001892 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001893
1894 ret->new_session_cb=0;
1895 ret->remove_session_cb=0;
1896 ret->get_session_cb=0;
1897 ret->generate_session_id=0;
1898
1899 memset((char *)&ret->stats,0,sizeof(ret->stats));
1900
1901 ret->references=1;
1902 ret->quiet_shutdown=0;
1903
Adam Langley95c29f32014-06-20 12:00:00 -07001904 ret->info_callback=NULL;
1905
1906 ret->app_verify_callback=0;
1907 ret->app_verify_arg=NULL;
1908
1909 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1910 ret->read_ahead=0;
1911 ret->msg_callback=0;
1912 ret->msg_callback_arg=NULL;
1913 ret->verify_mode=SSL_VERIFY_NONE;
1914#if 0
1915 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1916#endif
1917 ret->sid_ctx_length=0;
1918 ret->default_verify_callback=NULL;
1919 if ((ret->cert=ssl_cert_new()) == NULL)
1920 goto err;
1921
1922 ret->default_passwd_callback=0;
1923 ret->default_passwd_callback_userdata=NULL;
1924 ret->client_cert_cb=0;
1925 ret->app_gen_cookie_cb=0;
1926 ret->app_verify_cookie_cb=0;
1927
1928 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1929 if (ret->sessions == NULL) goto err;
1930 ret->cert_store=X509_STORE_new();
1931 if (ret->cert_store == NULL) goto err;
1932
1933 ssl_create_cipher_list(ret->method,
1934 &ret->cipher_list,&ret->cipher_list_by_id,
David Benjamin5e4f6e92014-11-17 03:23:24 -05001935 SSL_DEFAULT_CIPHER_LIST, ret->cert);
Adam Langley95c29f32014-06-20 12:00:00 -07001936 if (ret->cipher_list == NULL
Adam Langley858a88d2014-06-20 12:00:00 -07001937 || sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001938 {
1939 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1940 goto err2;
1941 }
1942
1943 ret->param = X509_VERIFY_PARAM_new();
1944 if (!ret->param)
1945 goto err;
1946
Adam Langley95c29f32014-06-20 12:00:00 -07001947 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1948 goto err;
1949
1950 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1951
1952 ret->extra_certs=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001953
1954 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1955
Adam Langley95c29f32014-06-20 12:00:00 -07001956 ret->tlsext_servername_callback = 0;
1957 ret->tlsext_servername_arg = NULL;
1958 /* Setup RFC4507 ticket keys */
1959 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
1960 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
1961 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
1962 ret->options |= SSL_OP_NO_TICKET;
1963
1964 ret->tlsext_status_cb = 0;
1965 ret->tlsext_status_arg = NULL;
1966
Adam Langley95c29f32014-06-20 12:00:00 -07001967 ret->next_protos_advertised_cb = 0;
1968 ret->next_proto_select_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001969 ret->psk_identity_hint=NULL;
1970 ret->psk_client_callback=NULL;
1971 ret->psk_server_callback=NULL;
David Benjamin422d3a42014-08-20 11:09:03 -04001972
Adam Langley95c29f32014-06-20 12:00:00 -07001973 /* Default is to connect to non-RI servers. When RI is more widely
1974 * deployed might change this.
1975 */
1976 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1977
1978 return(ret);
1979err:
1980 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
1981err2:
1982 if (ret != NULL) SSL_CTX_free(ret);
1983 return(NULL);
1984 }
1985
Adam Langley95c29f32014-06-20 12:00:00 -07001986
1987void SSL_CTX_free(SSL_CTX *a)
1988 {
1989 int i;
1990
1991 if (a == NULL) return;
1992
1993 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
1994#ifdef REF_PRINT
1995 REF_PRINT("SSL_CTX",a);
1996#endif
1997 if (i > 0) return;
1998#ifdef REF_CHECK
1999 if (i < 0)
2000 {
2001 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
2002 abort(); /* ok */
2003 }
2004#endif
2005
2006 if (a->param)
2007 X509_VERIFY_PARAM_free(a->param);
2008
2009 /*
2010 * Free internal session cache. However: the remove_cb() may reference
2011 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2012 * after the sessions were flushed.
2013 * As the ex_data handling routines might also touch the session cache,
2014 * the most secure solution seems to be: empty (flush) the cache, then
2015 * free ex_data, then finally free the cache.
2016 * (See ticket [openssl.org #212].)
2017 */
2018 if (a->sessions != NULL)
2019 SSL_CTX_flush_sessions(a,0);
2020
2021 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
2022
2023 if (a->sessions != NULL)
2024 lh_SSL_SESSION_free(a->sessions);
2025
2026 if (a->cert_store != NULL)
2027 X509_STORE_free(a->cert_store);
2028 if (a->cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002029 ssl_cipher_preference_list_free(a->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002030 if (a->cipher_list_by_id != NULL)
2031 sk_SSL_CIPHER_free(a->cipher_list_by_id);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07002032 if (a->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002033 ssl_cipher_preference_list_free(a->cipher_list_tls11);
Adam Langley95c29f32014-06-20 12:00:00 -07002034 if (a->cert != NULL)
2035 ssl_cert_free(a->cert);
2036 if (a->client_CA != NULL)
2037 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2038 if (a->extra_certs != NULL)
2039 sk_X509_pop_free(a->extra_certs,X509_free);
Adam Langley95c29f32014-06-20 12:00:00 -07002040
2041 if (a->srtp_profiles)
2042 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2043
Adam Langley95c29f32014-06-20 12:00:00 -07002044 if (a->psk_identity_hint)
2045 OPENSSL_free(a->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07002046
Adam Langley95c29f32014-06-20 12:00:00 -07002047 if (a->tlsext_ecpointformatlist)
2048 OPENSSL_free(a->tlsext_ecpointformatlist);
2049 if (a->tlsext_ellipticcurvelist)
2050 OPENSSL_free(a->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -07002051 if (a->alpn_client_proto_list != NULL)
2052 OPENSSL_free(a->alpn_client_proto_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002053
Adam Langley1258b6a2014-06-20 12:00:00 -07002054 if (a->tlsext_channel_id_private)
2055 EVP_PKEY_free(a->tlsext_channel_id_private);
Adam Langley1258b6a2014-06-20 12:00:00 -07002056
David Benjamin859ec3c2014-09-02 16:29:36 -04002057 if (a->keylog_bio)
2058 BIO_free(a->keylog_bio);
2059
Adam Langley95c29f32014-06-20 12:00:00 -07002060 OPENSSL_free(a);
2061 }
2062
2063void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2064 {
2065 ctx->default_passwd_callback=cb;
2066 }
2067
2068void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2069 {
2070 ctx->default_passwd_callback_userdata=u;
2071 }
2072
2073void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2074 {
2075 ctx->app_verify_callback=cb;
2076 ctx->app_verify_arg=arg;
2077 }
2078
2079void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2080 {
2081 ctx->verify_mode=mode;
2082 ctx->default_verify_callback=cb;
2083 }
2084
2085void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2086 {
2087 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2088 }
2089
2090void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2091 {
2092 ssl_cert_set_cert_cb(c->cert, cb, arg);
2093 }
2094
2095void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2096 {
2097 ssl_cert_set_cert_cb(s->cert, cb, arg);
2098 }
2099
David Benjamin033e5f42014-11-13 18:47:41 -05002100static int ssl_has_key(SSL *s, size_t idx)
2101 {
2102 CERT_PKEY *cpk = &s->cert->pkeys[idx];
2103 return cpk->x509 && cpk->privatekey;
2104 }
2105
David Benjaminf31e6812014-11-13 18:05:55 -05002106void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
2107 unsigned long *out_mask_a)
Adam Langley95c29f32014-06-20 12:00:00 -07002108 {
David Benjaminf31e6812014-11-13 18:05:55 -05002109 CERT *c = s->cert;
David Benjaminf31e6812014-11-13 18:05:55 -05002110 int rsa_enc, rsa_sign, dh_tmp;
2111 unsigned long mask_k, mask_a;
David Benjamin060d9d22014-07-15 00:54:26 -04002112 int have_ecc_cert, ecdsa_ok;
David Benjamin0da0e182014-08-19 16:20:28 -04002113 int have_ecdh_tmp;
David Benjaminf31e6812014-11-13 18:05:55 -05002114 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002115
David Benjaminf31e6812014-11-13 18:05:55 -05002116 if (c == NULL)
2117 {
2118 /* TODO(davidben): Is this codepath possible? */
2119 *out_mask_k = 0;
2120 *out_mask_a = 0;
2121 return;
2122 }
Adam Langley95c29f32014-06-20 12:00:00 -07002123
David Benjaminf31e6812014-11-13 18:05:55 -05002124 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002125
David Benjaminf31e6812014-11-13 18:05:55 -05002126 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
David Benjamin033e5f42014-11-13 18:47:41 -05002127 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2128 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2129 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
David Benjaminf31e6812014-11-13 18:05:55 -05002130 mask_k = 0;
2131 mask_a = 0;
2132
David Benjamin77a942b2014-07-15 01:22:50 -04002133 if (rsa_enc)
David Benjaminf31e6812014-11-13 18:05:55 -05002134 mask_k |= SSL_kRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002135
Adam Langley95c29f32014-06-20 12:00:00 -07002136 if (dh_tmp)
David Benjaminf31e6812014-11-13 18:05:55 -05002137 mask_k |= SSL_kEDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002138
Adam Langley95c29f32014-06-20 12:00:00 -07002139 if (rsa_enc || rsa_sign)
2140 {
David Benjaminf31e6812014-11-13 18:05:55 -05002141 mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002142 }
2143
David Benjaminf31e6812014-11-13 18:05:55 -05002144 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002145
David Benjamin0da0e182014-08-19 16:20:28 -04002146 /* An ECC certificate may be usable for ECDSA cipher suites depending on
David Benjamin033e5f42014-11-13 18:47:41 -05002147 * the key usage extension and on the client's curve preferences. */
Adam Langley95c29f32014-06-20 12:00:00 -07002148 if (have_ecc_cert)
2149 {
David Benjamin033e5f42014-11-13 18:47:41 -05002150 x = c->pkeys[SSL_PKEY_ECC].x509;
Adam Langley95c29f32014-06-20 12:00:00 -07002151 /* This call populates extension flags (ex_flags) */
2152 X509_check_purpose(x, -1, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07002153 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2154 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
David Benjamin033e5f42014-11-13 18:47:41 -05002155 if (!tls1_check_ec_cert(s, x))
Adam Langley95c29f32014-06-20 12:00:00 -07002156 ecdsa_ok = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002157 if (ecdsa_ok)
2158 {
David Benjaminf31e6812014-11-13 18:05:55 -05002159 mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002160 }
Adam Langley95c29f32014-06-20 12:00:00 -07002161 }
Adam Langley95c29f32014-06-20 12:00:00 -07002162
David Benjaminf31e6812014-11-13 18:05:55 -05002163 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2164 * key, check it. */
2165 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002166 {
David Benjaminf31e6812014-11-13 18:05:55 -05002167 mask_k |= SSL_kEECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002168 }
Adam Langley95c29f32014-06-20 12:00:00 -07002169
David Benjaminf31e6812014-11-13 18:05:55 -05002170 /* PSK requires a server callback. */
2171 if (s->psk_server_callback != NULL)
2172 {
2173 mask_k |= SSL_kPSK;
2174 mask_a |= SSL_aPSK;
2175 }
Adam Langley95c29f32014-06-20 12:00:00 -07002176
David Benjaminf31e6812014-11-13 18:05:55 -05002177 *out_mask_k = mask_k;
2178 *out_mask_a = mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002179 }
2180
2181/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2182#define ku_reject(x, usage) \
2183 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2184
Adam Langley95c29f32014-06-20 12:00:00 -07002185
2186int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2187 {
David Benjamin0da0e182014-08-19 16:20:28 -04002188 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002189 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2190 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2191
Adam Langley95c29f32014-06-20 12:00:00 -07002192 alg_a = cs->algorithm_auth;
2193
Adam Langley95c29f32014-06-20 12:00:00 -07002194 /* This call populates the ex_flags field correctly */
2195 X509_check_purpose(x, -1, 0);
2196 if ((x->sig_alg) && (x->sig_alg->algorithm))
2197 {
2198 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2199 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2200 }
Adam Langley95c29f32014-06-20 12:00:00 -07002201 if (alg_a & SSL_aECDSA)
2202 {
2203 /* key usage, if present, must allow signing */
2204 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2205 {
2206 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2207 return 0;
2208 }
2209 }
2210
2211 return 1; /* all checks are ok */
2212 }
2213
Adam Langley95c29f32014-06-20 12:00:00 -07002214
2215static int ssl_get_server_cert_index(const SSL *s)
2216 {
2217 int idx;
2218 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2219 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2220 idx = SSL_PKEY_RSA_SIGN;
2221 if (idx == -1)
2222 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2223 return idx;
2224 }
2225
2226CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2227 {
David Benjaminf31e6812014-11-13 18:05:55 -05002228 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002229
2230 /* This may or may not be an error. */
2231 if (i < 0)
2232 return NULL;
2233
2234 /* May be NULL. */
David Benjaminf31e6812014-11-13 18:05:55 -05002235 return &s->cert->pkeys[i];
Adam Langley95c29f32014-06-20 12:00:00 -07002236 }
2237
David Benjaminec2f27d2014-11-13 19:17:25 -05002238EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher)
Adam Langley95c29f32014-06-20 12:00:00 -07002239 {
2240 unsigned long alg_a;
2241 CERT *c;
2242 int idx = -1;
2243
2244 alg_a = cipher->algorithm_auth;
2245 c=s->cert;
2246
David Benjaminef2116d2014-08-19 20:21:56 -04002247 if (alg_a & SSL_aRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002248 {
2249 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2250 idx = SSL_PKEY_RSA_SIGN;
2251 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2252 idx = SSL_PKEY_RSA_ENC;
2253 }
2254 else if ((alg_a & SSL_aECDSA) &&
2255 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2256 idx = SSL_PKEY_ECC;
2257 if (idx == -1)
2258 {
2259 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2260 return(NULL);
2261 }
Adam Langley95c29f32014-06-20 12:00:00 -07002262 return c->pkeys[idx].privatekey;
2263 }
2264
Adam Langley95c29f32014-06-20 12:00:00 -07002265void ssl_update_cache(SSL *s,int mode)
2266 {
2267 int i;
2268
2269 /* If the session_id_length is 0, we are not supposed to cache it,
2270 * and it would be rather hard to do anyway :-) */
2271 if (s->session->session_id_length == 0) return;
2272
David Benjamin041b58a2014-08-27 15:25:21 -04002273 i=s->initial_ctx->session_cache_mode;
Adam Langley95c29f32014-06-20 12:00:00 -07002274 if ((i & mode) && (!s->hit)
2275 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
David Benjamin041b58a2014-08-27 15:25:21 -04002276 || SSL_CTX_add_session(s->initial_ctx,s->session))
2277 && (s->initial_ctx->new_session_cb != NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07002278 {
2279 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
David Benjamin041b58a2014-08-27 15:25:21 -04002280 if (!s->initial_ctx->new_session_cb(s,s->session))
Adam Langley95c29f32014-06-20 12:00:00 -07002281 SSL_SESSION_free(s->session);
2282 }
2283
2284 /* auto flush every 255 connections */
2285 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2286 ((i & mode) == mode))
2287 {
2288 if ( (((mode & SSL_SESS_CACHE_CLIENT)
David Benjamin041b58a2014-08-27 15:25:21 -04002289 ?s->initial_ctx->stats.sess_connect_good
2290 :s->initial_ctx->stats.sess_accept_good) & 0xff) == 0xff)
Adam Langley95c29f32014-06-20 12:00:00 -07002291 {
David Benjamin041b58a2014-08-27 15:25:21 -04002292 SSL_CTX_flush_sessions(s->initial_ctx,(unsigned long)time(NULL));
Adam Langley95c29f32014-06-20 12:00:00 -07002293 }
2294 }
2295 }
2296
Adam Langley95c29f32014-06-20 12:00:00 -07002297int SSL_get_error(const SSL *s,int i)
2298 {
2299 int reason;
2300 unsigned long l;
2301 BIO *bio;
2302
2303 if (i > 0) return(SSL_ERROR_NONE);
2304
2305 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2306 * etc, where we do encode the error */
2307 if ((l=ERR_peek_error()) != 0)
2308 {
2309 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2310 return(SSL_ERROR_SYSCALL);
2311 else
2312 return(SSL_ERROR_SSL);
2313 }
2314
Adam Langleyb2ce0582014-06-20 12:00:00 -07002315 if ((i < 0) && SSL_want_session(s))
2316 return(SSL_ERROR_PENDING_SESSION);
2317
Adam Langleydc9b1412014-06-20 12:00:00 -07002318 if ((i < 0) && SSL_want_certificate(s))
2319 return(SSL_ERROR_PENDING_CERTIFICATE);
2320
Adam Langley95c29f32014-06-20 12:00:00 -07002321 if ((i < 0) && SSL_want_read(s))
2322 {
2323 bio=SSL_get_rbio(s);
2324 if (BIO_should_read(bio))
2325 return(SSL_ERROR_WANT_READ);
2326 else if (BIO_should_write(bio))
2327 /* This one doesn't make too much sense ... We never try
2328 * to write to the rbio, and an application program where
2329 * rbio and wbio are separate couldn't even know what it
2330 * should wait for.
2331 * However if we ever set s->rwstate incorrectly
2332 * (so that we have SSL_want_read(s) instead of
2333 * SSL_want_write(s)) and rbio and wbio *are* the same,
2334 * this test works around that bug; so it might be safer
2335 * to keep it. */
2336 return(SSL_ERROR_WANT_WRITE);
2337 else if (BIO_should_io_special(bio))
2338 {
2339 reason=BIO_get_retry_reason(bio);
2340 if (reason == BIO_RR_CONNECT)
2341 return(SSL_ERROR_WANT_CONNECT);
2342 else if (reason == BIO_RR_ACCEPT)
2343 return(SSL_ERROR_WANT_ACCEPT);
2344 else
2345 return(SSL_ERROR_SYSCALL); /* unknown */
2346 }
2347 }
2348
2349 if ((i < 0) && SSL_want_write(s))
2350 {
2351 bio=SSL_get_wbio(s);
2352 if (BIO_should_write(bio))
2353 return(SSL_ERROR_WANT_WRITE);
2354 else if (BIO_should_read(bio))
2355 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2356 return(SSL_ERROR_WANT_READ);
2357 else if (BIO_should_io_special(bio))
2358 {
2359 reason=BIO_get_retry_reason(bio);
2360 if (reason == BIO_RR_CONNECT)
2361 return(SSL_ERROR_WANT_CONNECT);
2362 else if (reason == BIO_RR_ACCEPT)
2363 return(SSL_ERROR_WANT_ACCEPT);
2364 else
2365 return(SSL_ERROR_SYSCALL);
2366 }
2367 }
2368 if ((i < 0) && SSL_want_x509_lookup(s))
2369 {
2370 return(SSL_ERROR_WANT_X509_LOOKUP);
2371 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002372 if ((i < 0) && SSL_want_channel_id_lookup(s))
2373 {
2374 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2375 }
Adam Langley95c29f32014-06-20 12:00:00 -07002376
2377 if (i == 0)
2378 {
David Benjamin5e4f6e92014-11-17 03:23:24 -05002379 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2380 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
Adam Langley95c29f32014-06-20 12:00:00 -07002381 return(SSL_ERROR_ZERO_RETURN);
Adam Langley95c29f32014-06-20 12:00:00 -07002382 }
2383 return(SSL_ERROR_SYSCALL);
2384 }
2385
2386int SSL_do_handshake(SSL *s)
2387 {
2388 int ret=1;
2389
2390 if (s->handshake_func == NULL)
2391 {
2392 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2393 return(-1);
2394 }
2395
2396 s->method->ssl_renegotiate_check(s);
2397
2398 if (SSL_in_init(s) || SSL_in_before(s))
2399 {
2400 ret=s->handshake_func(s);
2401 }
2402 return(ret);
2403 }
2404
Adam Langley95c29f32014-06-20 12:00:00 -07002405void SSL_set_accept_state(SSL *s)
2406 {
2407 s->server=1;
2408 s->shutdown=0;
2409 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2410 s->handshake_func=s->method->ssl_accept;
2411 /* clear the current cipher */
2412 ssl_clear_cipher_ctx(s);
2413 ssl_clear_hash_ctx(&s->read_hash);
2414 ssl_clear_hash_ctx(&s->write_hash);
2415 }
2416
2417void SSL_set_connect_state(SSL *s)
2418 {
2419 s->server=0;
2420 s->shutdown=0;
2421 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2422 s->handshake_func=s->method->ssl_connect;
2423 /* clear the current cipher */
2424 ssl_clear_cipher_ctx(s);
2425 ssl_clear_hash_ctx(&s->read_hash);
2426 ssl_clear_hash_ctx(&s->write_hash);
2427 }
2428
2429int ssl_undefined_function(SSL *s)
2430 {
2431 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2432 return(0);
2433 }
2434
2435int ssl_undefined_void_function(void)
2436 {
2437 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2438 return(0);
2439 }
2440
2441int ssl_undefined_const_function(const SSL *s)
2442 {
2443 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2444 return(0);
2445 }
2446
Adam Langley0f4746e2014-08-13 12:26:32 -07002447static const char *ssl_get_version(int version)
Adam Langley95c29f32014-06-20 12:00:00 -07002448 {
Adam Langley0f4746e2014-08-13 12:26:32 -07002449 if (version == TLS1_2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002450 return("TLSv1.2");
Adam Langley0f4746e2014-08-13 12:26:32 -07002451 else if (version == TLS1_1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002452 return("TLSv1.1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002453 else if (version == TLS1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002454 return("TLSv1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002455 else if (version == SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002456 return("SSLv3");
Adam Langley95c29f32014-06-20 12:00:00 -07002457 else
2458 return("unknown");
2459 }
2460
Adam Langley0f4746e2014-08-13 12:26:32 -07002461const char *SSL_get_version(const SSL *s)
2462 {
2463 return ssl_get_version(s->version);
2464 }
2465
2466const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2467 {
2468 return ssl_get_version(sess->ssl_version);
2469 }
2470
Adam Langley95c29f32014-06-20 12:00:00 -07002471void ssl_clear_cipher_ctx(SSL *s)
2472 {
2473 if (s->enc_read_ctx != NULL)
2474 {
2475 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2476 OPENSSL_free(s->enc_read_ctx);
2477 s->enc_read_ctx=NULL;
2478 }
2479 if (s->enc_write_ctx != NULL)
2480 {
2481 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2482 OPENSSL_free(s->enc_write_ctx);
2483 s->enc_write_ctx=NULL;
2484 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002485 if (s->aead_read_ctx != NULL)
2486 {
2487 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2488 OPENSSL_free(s->aead_read_ctx);
2489 s->aead_read_ctx = NULL;
2490 }
2491 if (s->aead_write_ctx != NULL)
2492 {
2493 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2494 OPENSSL_free(s->aead_write_ctx);
2495 s->aead_write_ctx = NULL;
2496 }
Adam Langley95c29f32014-06-20 12:00:00 -07002497 }
2498
2499X509 *SSL_get_certificate(const SSL *s)
2500 {
2501 if (s->cert != NULL)
2502 return(s->cert->key->x509);
2503 else
2504 return(NULL);
2505 }
2506
2507EVP_PKEY *SSL_get_privatekey(const SSL *s)
2508 {
2509 if (s->cert != NULL)
2510 return(s->cert->key->privatekey);
2511 else
2512 return(NULL);
2513 }
2514
2515X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2516 {
2517 if (ctx->cert != NULL)
2518 return ctx->cert->key->x509;
2519 else
2520 return NULL;
2521 }
2522
2523EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2524 {
2525 if (ctx->cert != NULL)
2526 return ctx->cert->key->privatekey;
2527 else
2528 return NULL ;
2529 }
2530
2531const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2532 {
2533 if ((s->session != NULL) && (s->session->cipher != NULL))
2534 return(s->session->cipher);
2535 return(NULL);
2536 }
2537const void *SSL_get_current_compression(SSL *s)
2538 {
2539 return NULL;
2540 }
2541const void *SSL_get_current_expansion(SSL *s)
2542 {
2543 return NULL;
2544 }
2545
2546int ssl_init_wbio_buffer(SSL *s,int push)
2547 {
2548 BIO *bbio;
2549
2550 if (s->bbio == NULL)
2551 {
2552 bbio=BIO_new(BIO_f_buffer());
2553 if (bbio == NULL) return(0);
2554 s->bbio=bbio;
2555 }
2556 else
2557 {
2558 bbio=s->bbio;
2559 if (s->bbio == s->wbio)
2560 s->wbio=BIO_pop(s->wbio);
2561 }
2562 (void)BIO_reset(bbio);
2563/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2564 if (!BIO_set_read_buffer_size(bbio,1))
2565 {
2566 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2567 return(0);
2568 }
2569 if (push)
2570 {
2571 if (s->wbio != bbio)
2572 s->wbio=BIO_push(bbio,s->wbio);
2573 }
2574 else
2575 {
2576 if (s->wbio == bbio)
2577 s->wbio=BIO_pop(bbio);
2578 }
2579 return(1);
2580 }
2581
2582void ssl_free_wbio_buffer(SSL *s)
2583 {
2584 if (s->bbio == NULL) return;
2585
2586 if (s->bbio == s->wbio)
2587 {
2588 /* remove buffering */
2589 s->wbio=BIO_pop(s->wbio);
2590#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2591 assert(s->wbio != NULL);
2592#endif
2593 }
2594 BIO_free(s->bbio);
2595 s->bbio=NULL;
2596 }
2597
2598void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2599 {
2600 ctx->quiet_shutdown=mode;
2601 }
2602
2603int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2604 {
2605 return(ctx->quiet_shutdown);
2606 }
2607
2608void SSL_set_quiet_shutdown(SSL *s,int mode)
2609 {
2610 s->quiet_shutdown=mode;
2611 }
2612
2613int SSL_get_quiet_shutdown(const SSL *s)
2614 {
2615 return(s->quiet_shutdown);
2616 }
2617
2618void SSL_set_shutdown(SSL *s,int mode)
2619 {
2620 s->shutdown=mode;
2621 }
2622
2623int SSL_get_shutdown(const SSL *s)
2624 {
2625 return(s->shutdown);
2626 }
2627
2628int SSL_version(const SSL *s)
2629 {
2630 return(s->version);
2631 }
2632
2633SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2634 {
2635 return(ssl->ctx);
2636 }
2637
2638SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2639 {
2640 if (ssl->ctx == ctx)
2641 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002642 if (ctx == NULL)
2643 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002644 if (ssl->cert != NULL)
2645 ssl_cert_free(ssl->cert);
2646 ssl->cert = ssl_cert_dup(ctx->cert);
2647 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2648 if (ssl->ctx != NULL)
2649 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2650 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07002651
2652 ssl->sid_ctx_length = ctx->sid_ctx_length;
2653 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2654 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2655
Adam Langley95c29f32014-06-20 12:00:00 -07002656 return(ssl->ctx);
2657 }
2658
2659#ifndef OPENSSL_NO_STDIO
2660int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2661 {
2662 return(X509_STORE_set_default_paths(ctx->cert_store));
2663 }
2664
2665int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2666 const char *CApath)
2667 {
2668 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2669 }
2670#endif
2671
2672void SSL_set_info_callback(SSL *ssl,
2673 void (*cb)(const SSL *ssl,int type,int val))
2674 {
2675 ssl->info_callback=cb;
2676 }
2677
2678/* One compiler (Diab DCC) doesn't like argument names in returned
2679 function pointer. */
2680void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2681 {
2682 return ssl->info_callback;
2683 }
2684
2685int SSL_state(const SSL *ssl)
2686 {
2687 return(ssl->state);
2688 }
2689
2690void SSL_set_state(SSL *ssl, int state)
2691 {
2692 ssl->state = state;
2693 }
2694
2695void SSL_set_verify_result(SSL *ssl,long arg)
2696 {
2697 ssl->verify_result=arg;
2698 }
2699
2700long SSL_get_verify_result(const SSL *ssl)
2701 {
2702 return(ssl->verify_result);
2703 }
2704
2705int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2706 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2707 {
2708 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2709 new_func, dup_func, free_func);
2710 }
2711
2712int SSL_set_ex_data(SSL *s,int idx,void *arg)
2713 {
2714 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2715 }
2716
2717void *SSL_get_ex_data(const SSL *s,int idx)
2718 {
2719 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2720 }
2721
2722int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2723 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2724 {
2725 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2726 new_func, dup_func, free_func);
2727 }
2728
2729int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2730 {
2731 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2732 }
2733
2734void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2735 {
2736 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2737 }
2738
2739int ssl_ok(SSL *s)
2740 {
2741 return(1);
2742 }
2743
2744X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2745 {
2746 return(ctx->cert_store);
2747 }
2748
2749void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2750 {
2751 if (ctx->cert_store != NULL)
2752 X509_STORE_free(ctx->cert_store);
2753 ctx->cert_store=store;
2754 }
2755
2756int SSL_want(const SSL *s)
2757 {
2758 return(s->rwstate);
2759 }
2760
2761/*!
2762 * \brief Set the callback for generating temporary RSA keys.
2763 * \param ctx the SSL context.
2764 * \param cb the callback
2765 */
2766
Adam Langley95c29f32014-06-20 12:00:00 -07002767void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2768 int is_export,
2769 int keylength))
2770 {
2771 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2772 }
2773
2774void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2775 int is_export,
2776 int keylength))
2777 {
2778 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2779 }
Adam Langley95c29f32014-06-20 12:00:00 -07002780
2781#ifdef DOXYGEN
2782/*!
2783 * \brief The RSA temporary key callback function.
2784 * \param ssl the SSL session.
2785 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2786 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2787 * of the required key in bits.
2788 * \return the temporary RSA key.
2789 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2790 */
2791
2792RSA *cb(SSL *ssl,int is_export,int keylength)
2793 {}
2794#endif
2795
2796/*!
2797 * \brief Set the callback for generating temporary DH keys.
2798 * \param ctx the SSL context.
2799 * \param dh the callback
2800 */
2801
Adam Langley95c29f32014-06-20 12:00:00 -07002802void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2803 int keylength))
2804 {
2805 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2806 }
2807
2808void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2809 int keylength))
2810 {
2811 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2812 }
Adam Langley95c29f32014-06-20 12:00:00 -07002813
Adam Langley95c29f32014-06-20 12:00:00 -07002814void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2815 int keylength))
2816 {
2817 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2818 }
2819
2820void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2821 int keylength))
2822 {
2823 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2824 }
Adam Langley95c29f32014-06-20 12:00:00 -07002825
Adam Langley95c29f32014-06-20 12:00:00 -07002826int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2827 {
2828 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2829 {
2830 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2831 return 0;
2832 }
2833 if (ctx->psk_identity_hint != NULL)
2834 OPENSSL_free(ctx->psk_identity_hint);
2835 if (identity_hint != NULL)
2836 {
2837 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2838 if (ctx->psk_identity_hint == NULL)
2839 return 0;
2840 }
2841 else
2842 ctx->psk_identity_hint = NULL;
2843 return 1;
2844 }
2845
2846int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
2847 {
2848 if (s == NULL)
2849 return 0;
2850
Adam Langley95c29f32014-06-20 12:00:00 -07002851 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2852 {
2853 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2854 return 0;
2855 }
Adam Langley0289c732014-06-20 12:00:00 -07002856
David Benjamin688d8df2014-11-02 23:06:42 -05002857 /* Clear currently configured hint, if any. */
Adam Langley0289c732014-06-20 12:00:00 -07002858 if (s->psk_identity_hint != NULL)
2859 {
2860 OPENSSL_free(s->psk_identity_hint);
2861 s->psk_identity_hint = NULL;
2862 }
Adam Langley0289c732014-06-20 12:00:00 -07002863
Adam Langley95c29f32014-06-20 12:00:00 -07002864 if (identity_hint != NULL)
2865 {
David Benjamin688d8df2014-11-02 23:06:42 -05002866 s->psk_identity_hint = BUF_strdup(identity_hint);
2867 if (s->psk_identity_hint == NULL)
2868 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002869 }
Adam Langley95c29f32014-06-20 12:00:00 -07002870 return 1;
2871 }
2872
2873const char *SSL_get_psk_identity_hint(const SSL *s)
2874 {
Adam Langley0289c732014-06-20 12:00:00 -07002875 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002876 return NULL;
David Benjamin688d8df2014-11-02 23:06:42 -05002877 return s->psk_identity_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07002878 }
2879
2880const char *SSL_get_psk_identity(const SSL *s)
2881 {
2882 if (s == NULL || s->session == NULL)
2883 return NULL;
2884 return(s->session->psk_identity);
2885 }
2886
2887void SSL_set_psk_client_callback(SSL *s,
2888 unsigned int (*cb)(SSL *ssl, const char *hint,
2889 char *identity, unsigned int max_identity_len, unsigned char *psk,
2890 unsigned int max_psk_len))
2891 {
2892 s->psk_client_callback = cb;
2893 }
2894
2895void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
2896 unsigned int (*cb)(SSL *ssl, const char *hint,
2897 char *identity, unsigned int max_identity_len, unsigned char *psk,
2898 unsigned int max_psk_len))
2899 {
2900 ctx->psk_client_callback = cb;
2901 }
2902
2903void SSL_set_psk_server_callback(SSL *s,
2904 unsigned int (*cb)(SSL *ssl, const char *identity,
2905 unsigned char *psk, unsigned int max_psk_len))
2906 {
2907 s->psk_server_callback = cb;
2908 }
2909
2910void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
2911 unsigned int (*cb)(SSL *ssl, const char *identity,
2912 unsigned char *psk, unsigned int max_psk_len))
2913 {
2914 ctx->psk_server_callback = cb;
2915 }
Adam Langley95c29f32014-06-20 12:00:00 -07002916
2917void 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))
2918 {
2919 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2920 }
2921void 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))
2922 {
2923 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2924 }
2925
David Benjamin859ec3c2014-09-02 16:29:36 -04002926void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio)
2927 {
2928 if (ctx->keylog_bio != NULL)
2929 BIO_free(ctx->keylog_bio);
2930 ctx->keylog_bio = keylog_bio;
2931 }
2932
2933static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len)
2934 {
2935 static const char hextable[] = "0123456789abcdef";
2936 uint8_t *out;
2937 size_t i;
2938
2939 if (!CBB_add_space(cbb, &out, in_len * 2))
2940 {
2941 return 0;
2942 }
2943
2944 for (i = 0; i < in_len; i++)
2945 {
2946 *(out++) = (uint8_t)hextable[in[i] >> 4];
2947 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2948 }
2949 return 1;
2950 }
2951
2952int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
2953 const uint8_t *encrypted_premaster, size_t encrypted_premaster_len,
2954 const uint8_t *premaster, size_t premaster_len)
2955 {
2956 BIO *bio = ctx->keylog_bio;
2957 CBB cbb;
2958 uint8_t *out;
2959 size_t out_len;
2960 int ret;
2961
2962 if (bio == NULL)
2963 {
2964 return 1;
2965 }
2966
2967 if (encrypted_premaster_len < 8)
2968 {
2969 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange, ERR_R_INTERNAL_ERROR);
2970 return 0;
2971 }
2972
2973 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len*2 + 1))
2974 {
2975 return 0;
2976 }
2977 if (!CBB_add_bytes(&cbb, (const uint8_t*)"RSA ", 4) ||
2978 /* Only the first 8 bytes of the encrypted premaster secret are
2979 * logged. */
2980 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2981 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
2982 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2983 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
2984 !CBB_finish(&cbb, &out, &out_len))
2985 {
2986 CBB_cleanup(&cbb);
2987 return 0;
2988 }
2989
2990 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2991 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2992 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2993
2994 OPENSSL_free(out);
2995 return ret;
2996 }
2997
2998int ssl_ctx_log_master_secret(SSL_CTX *ctx,
2999 const uint8_t *client_random, size_t client_random_len,
3000 const uint8_t *master, size_t master_len)
3001 {
3002 BIO *bio = ctx->keylog_bio;
3003 CBB cbb;
3004 uint8_t *out;
3005 size_t out_len;
3006 int ret;
3007
3008 if (bio == NULL)
3009 {
3010 return 1;
3011 }
3012
3013 if (client_random_len != 32)
3014 {
3015 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
3016 return 0;
3017 }
3018
3019 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len*2 + 1))
3020 {
3021 return 0;
3022 }
3023 if (!CBB_add_bytes(&cbb, (const uint8_t*)"CLIENT_RANDOM ", 14) ||
3024 !cbb_add_hex(&cbb, client_random, 32) ||
3025 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3026 !cbb_add_hex(&cbb, master, master_len) ||
3027 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3028 !CBB_finish(&cbb, &out, &out_len))
3029 {
3030 CBB_cleanup(&cbb);
3031 return 0;
3032 }
3033
3034 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3035 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3036 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3037
3038 OPENSSL_free(out);
3039 return ret;
3040 }
3041
Adam Langleyadb739e2014-06-20 12:00:00 -07003042int SSL_cutthrough_complete(const SSL *s)
3043 {
3044 return (!s->server && /* cutthrough only applies to clients */
3045 !s->hit && /* full-handshake */
3046 s->version >= SSL3_VERSION &&
3047 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3048 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3049 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3050 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3051 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
David Benjamin7e3305e2014-07-28 14:52:32 -04003052 s->state == SSL3_ST_CR_CHANGE ||
Adam Langleyadb739e2014-06-20 12:00:00 -07003053 s->state == SSL3_ST_CR_FINISHED_A));
3054 }
3055
Adam Langley95f22882014-06-20 12:00:00 -07003056void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3057 size_t* ssl_session_size)
3058{
3059 *ssl_size = sizeof(SSL);
3060 *ssl_ctx_size = sizeof(SSL_CTX);
3061 *ssl_session_size = sizeof(SSL_SESSION);
3062}
3063
Adam Langleyadb739e2014-06-20 12:00:00 -07003064int ssl3_can_cutthrough(const SSL *s)
3065 {
3066 const SSL_CIPHER *c;
3067
3068 /* require a strong enough cipher */
3069 if (SSL_get_cipher_bits(s, NULL) < 128)
3070 return 0;
3071
3072 /* require ALPN or NPN extension */
David Benjamin457112e2014-08-27 14:35:09 -04003073 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen)
Adam Langleyadb739e2014-06-20 12:00:00 -07003074 {
3075 return 0;
3076 }
3077
3078 /* require a forward-secret cipher */
3079 c = SSL_get_current_cipher(s);
3080 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3081 c->algorithm_mkey != SSL_kEECDH))
3082 {
3083 return 0;
3084 }
3085
3086 return 1;
3087 }
3088
Adam Langley29707792014-06-20 12:00:00 -07003089/* ssl_get_max_version returns the maximum SSL/TLS version number supported by
3090 * |s|, or zero if all versions are disabled. */
3091int ssl_get_max_version(const SSL *s)
3092 {
3093 /* Only one version supported for DTLS. */
3094 if (s->version == DTLS1_VERSION)
3095 return DTLS1_VERSION;
3096
3097 if (!(s->options & SSL_OP_NO_TLSv1_2))
3098 return TLS1_2_VERSION;
3099 if (!(s->options & SSL_OP_NO_TLSv1_1))
3100 return TLS1_1_VERSION;
3101 if (!(s->options & SSL_OP_NO_TLSv1))
3102 return TLS1_VERSION;
3103 if (!(s->options & SSL_OP_NO_SSLv3))
3104 return SSL3_VERSION;
Adam Langley29707792014-06-20 12:00:00 -07003105 return 0;
3106 }
3107
Adam Langley95c29f32014-06-20 12:00:00 -07003108/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3109 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3110 * any. If EVP_MD pointer is passed, initializes ctx with this md
3111 * Returns newly allocated ctx;
3112 */
3113
3114EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3115{
3116 ssl_clear_hash_ctx(hash);
3117 *hash = EVP_MD_CTX_create();
David Benjaminae3e4872014-11-18 21:52:26 -05003118 if (md != NULL && *hash != NULL &&
3119 !EVP_DigestInit_ex(*hash, md, NULL))
Adam Langley69a01602014-11-17 17:26:55 -08003120 {
David Benjaminae3e4872014-11-18 21:52:26 -05003121 EVP_MD_CTX_destroy(*hash);
3122 *hash = NULL;
Adam Langley69a01602014-11-17 17:26:55 -08003123 }
Adam Langley95c29f32014-06-20 12:00:00 -07003124 return *hash;
3125}
3126void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3127{
3128
3129 if (*hash) EVP_MD_CTX_destroy(*hash);
3130 *hash=NULL;
3131}
3132
Adam Langley95c29f32014-06-20 12:00:00 -07003133int SSL_cache_hit(SSL *s)
3134 {
3135 return s->hit;
3136 }
3137
3138int SSL_is_server(SSL *s)
3139 {
3140 return s->server;
3141 }