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