blob: 483608bce0b76ca4d4b3e9705a93e378263878e0 [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
David Benjamin0b145c22014-11-26 20:10:09 -0500980 if (s->handshake_func != s->method->ssl_accept)
981 {
982 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
983 return -1;
984 }
985
986 return s->handshake_func(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700987 }
988
989int SSL_connect(SSL *s)
990 {
991 if (s->handshake_func == 0)
992 /* Not properly initialized yet */
993 SSL_set_connect_state(s);
994
David Benjamin0b145c22014-11-26 20:10:09 -0500995 if (s->handshake_func != s->method->ssl_connect)
996 {
997 OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
998 return -1;
999 }
1000
1001 return s->handshake_func(s);
Adam Langley95c29f32014-06-20 12:00:00 -07001002 }
1003
1004long SSL_get_default_timeout(const SSL *s)
1005 {
David Benjaminf4501342014-08-14 17:24:37 -04001006 return SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001007 }
1008
1009int SSL_read(SSL *s,void *buf,int num)
1010 {
1011 if (s->handshake_func == 0)
1012 {
1013 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
1014 return -1;
1015 }
1016
1017 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1018 {
1019 s->rwstate=SSL_NOTHING;
1020 return(0);
1021 }
1022 return(s->method->ssl_read(s,buf,num));
1023 }
1024
1025int SSL_peek(SSL *s,void *buf,int num)
1026 {
1027 if (s->handshake_func == 0)
1028 {
1029 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
1030 return -1;
1031 }
1032
1033 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1034 {
1035 return(0);
1036 }
1037 return(s->method->ssl_peek(s,buf,num));
1038 }
1039
1040int SSL_write(SSL *s,const void *buf,int num)
1041 {
1042 if (s->handshake_func == 0)
1043 {
1044 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
1045 return -1;
1046 }
1047
1048 if (s->shutdown & SSL_SENT_SHUTDOWN)
1049 {
1050 s->rwstate=SSL_NOTHING;
1051 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
1052 return(-1);
1053 }
1054 return(s->method->ssl_write(s,buf,num));
1055 }
1056
1057int SSL_shutdown(SSL *s)
1058 {
1059 /* Note that this function behaves differently from what one might
1060 * expect. Return values are 0 for no success (yet),
1061 * 1 for success; but calling it once is usually not enough,
1062 * even if blocking I/O is used (see ssl3_shutdown).
1063 */
1064
1065 if (s->handshake_func == 0)
1066 {
1067 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1068 return -1;
1069 }
1070
Adam Langleyb2cb0ec2014-09-02 14:28:49 -07001071 if (!SSL_in_init(s))
Adam Langley95c29f32014-06-20 12:00:00 -07001072 return(s->method->ssl_shutdown(s));
1073 else
1074 return(1);
1075 }
1076
1077int SSL_renegotiate(SSL *s)
1078 {
1079 if (s->renegotiate == 0)
1080 s->renegotiate=1;
1081
1082 s->new_session=1;
1083
1084 return(s->method->ssl_renegotiate(s));
1085 }
1086
1087int SSL_renegotiate_abbreviated(SSL *s)
1088 {
1089 if (s->renegotiate == 0)
1090 s->renegotiate=1;
1091
1092 s->new_session=0;
1093
1094 return(s->method->ssl_renegotiate(s));
1095 }
1096
1097int SSL_renegotiate_pending(SSL *s)
1098 {
1099 /* becomes true when negotiation is requested;
1100 * false again once a handshake has finished */
1101 return (s->renegotiate != 0);
1102 }
1103
1104long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1105 {
1106 long l;
1107
1108 switch (cmd)
1109 {
1110 case SSL_CTRL_GET_READ_AHEAD:
1111 return(s->read_ahead);
1112 case SSL_CTRL_SET_READ_AHEAD:
1113 l=s->read_ahead;
1114 s->read_ahead=larg;
1115 return(l);
1116
1117 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1118 s->msg_callback_arg = parg;
1119 return 1;
1120
1121 case SSL_CTRL_OPTIONS:
1122 return(s->options|=larg);
1123 case SSL_CTRL_CLEAR_OPTIONS:
1124 return(s->options&=~larg);
1125 case SSL_CTRL_MODE:
1126 return(s->mode|=larg);
1127 case SSL_CTRL_CLEAR_MODE:
1128 return(s->mode &=~larg);
1129 case SSL_CTRL_GET_MAX_CERT_LIST:
1130 return(s->max_cert_list);
1131 case SSL_CTRL_SET_MAX_CERT_LIST:
1132 l=s->max_cert_list;
1133 s->max_cert_list=larg;
1134 return(l);
1135 case SSL_CTRL_SET_MTU:
Adam Langley95c29f32014-06-20 12:00:00 -07001136 if (larg < (long)dtls1_min_mtu())
1137 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001138 if (SSL_IS_DTLS(s))
1139 {
1140 s->d1->mtu = larg;
1141 return larg;
1142 }
1143 return 0;
1144 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1145 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1146 return 0;
1147 s->max_send_fragment = larg;
1148 return 1;
1149 case SSL_CTRL_GET_RI_SUPPORT:
1150 if (s->s3)
1151 return s->s3->send_connection_binding;
1152 else return 0;
1153 case SSL_CTRL_CERT_FLAGS:
1154 return(s->cert->cert_flags|=larg);
1155 case SSL_CTRL_CLEAR_CERT_FLAGS:
1156 return(s->cert->cert_flags &=~larg);
1157
1158 case SSL_CTRL_GET_RAW_CIPHERLIST:
1159 if (parg)
1160 {
1161 if (s->cert->ciphers_raw == NULL)
1162 return 0;
1163 *(unsigned char **)parg = s->cert->ciphers_raw;
1164 return (int)s->cert->ciphers_rawlen;
1165 }
1166 else
David Benjamin39482a12014-07-20 13:30:15 -04001167 {
1168 /* Passing a NULL |parg| returns the size of a single
1169 * cipher suite value. */
1170 return 2;
1171 }
Adam Langley95c29f32014-06-20 12:00:00 -07001172 default:
1173 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1174 }
1175 }
1176
1177long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1178 {
1179 switch(cmd)
1180 {
1181 case SSL_CTRL_SET_MSG_CALLBACK:
1182 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1183 return 1;
1184
1185 default:
1186 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1187 }
1188 }
1189
1190LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1191 {
1192 return ctx->sessions;
1193 }
1194
1195long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1196 {
1197 long l;
1198
1199 switch (cmd)
1200 {
1201 case SSL_CTRL_GET_READ_AHEAD:
1202 return(ctx->read_ahead);
1203 case SSL_CTRL_SET_READ_AHEAD:
1204 l=ctx->read_ahead;
1205 ctx->read_ahead=larg;
1206 return(l);
1207
1208 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1209 ctx->msg_callback_arg = parg;
1210 return 1;
1211
1212 case SSL_CTRL_GET_MAX_CERT_LIST:
1213 return(ctx->max_cert_list);
1214 case SSL_CTRL_SET_MAX_CERT_LIST:
1215 l=ctx->max_cert_list;
1216 ctx->max_cert_list=larg;
1217 return(l);
1218
1219 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1220 l=ctx->session_cache_size;
1221 ctx->session_cache_size=larg;
1222 return(l);
1223 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1224 return(ctx->session_cache_size);
1225 case SSL_CTRL_SET_SESS_CACHE_MODE:
1226 l=ctx->session_cache_mode;
1227 ctx->session_cache_mode=larg;
1228 return(l);
1229 case SSL_CTRL_GET_SESS_CACHE_MODE:
1230 return(ctx->session_cache_mode);
1231
1232 case SSL_CTRL_SESS_NUMBER:
1233 return(lh_SSL_SESSION_num_items(ctx->sessions));
1234 case SSL_CTRL_SESS_CONNECT:
1235 return(ctx->stats.sess_connect);
1236 case SSL_CTRL_SESS_CONNECT_GOOD:
1237 return(ctx->stats.sess_connect_good);
1238 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1239 return(ctx->stats.sess_connect_renegotiate);
1240 case SSL_CTRL_SESS_ACCEPT:
1241 return(ctx->stats.sess_accept);
1242 case SSL_CTRL_SESS_ACCEPT_GOOD:
1243 return(ctx->stats.sess_accept_good);
1244 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1245 return(ctx->stats.sess_accept_renegotiate);
1246 case SSL_CTRL_SESS_HIT:
1247 return(ctx->stats.sess_hit);
1248 case SSL_CTRL_SESS_CB_HIT:
1249 return(ctx->stats.sess_cb_hit);
1250 case SSL_CTRL_SESS_MISSES:
1251 return(ctx->stats.sess_miss);
1252 case SSL_CTRL_SESS_TIMEOUTS:
1253 return(ctx->stats.sess_timeout);
1254 case SSL_CTRL_SESS_CACHE_FULL:
1255 return(ctx->stats.sess_cache_full);
1256 case SSL_CTRL_OPTIONS:
1257 return(ctx->options|=larg);
1258 case SSL_CTRL_CLEAR_OPTIONS:
1259 return(ctx->options&=~larg);
1260 case SSL_CTRL_MODE:
1261 return(ctx->mode|=larg);
1262 case SSL_CTRL_CLEAR_MODE:
1263 return(ctx->mode&=~larg);
1264 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1265 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1266 return 0;
1267 ctx->max_send_fragment = larg;
1268 return 1;
1269 case SSL_CTRL_CERT_FLAGS:
1270 return(ctx->cert->cert_flags|=larg);
1271 case SSL_CTRL_CLEAR_CERT_FLAGS:
1272 return(ctx->cert->cert_flags &=~larg);
1273 default:
1274 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1275 }
1276 }
1277
1278long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1279 {
1280 switch(cmd)
1281 {
1282 case SSL_CTRL_SET_MSG_CALLBACK:
1283 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1284 return 1;
1285
1286 default:
1287 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1288 }
1289 }
1290
1291int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1292 {
1293 long l;
1294 const SSL_CIPHER *a = in_a;
1295 const SSL_CIPHER *b = in_b;
1296 const long a_id = a->id;
1297 const long b_id = b->id;
1298
1299 l = a_id - b_id;
1300 if (l == 0L)
1301 return(0);
1302 else
1303 return((l > 0)?1:-1);
1304 }
1305
1306int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1307 {
1308 long l;
1309 const long a_id = (*ap)->id;
1310 const long b_id = (*bp)->id;
1311
1312 l = a_id - b_id;
1313 if (l == 0)
1314 return(0);
1315 else
1316 return((l > 0)?1:-1);
1317 }
1318
1319/** return a STACK of the ciphers available for the SSL and in order of
1320 * preference */
1321STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1322 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001323 if (s == NULL)
1324 return NULL;
1325
1326 if (s->cipher_list != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001327 {
Adam Langley858a88d2014-06-20 12:00:00 -07001328 return(s->cipher_list->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -07001329 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001330
1331 if (s->version >= TLS1_1_VERSION)
1332 {
1333 if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001334 return s->ctx->cipher_list_tls11->ciphers;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001335 }
1336
1337 if ((s->ctx != NULL) &&
1338 (s->ctx->cipher_list != NULL))
1339 {
Adam Langley858a88d2014-06-20 12:00:00 -07001340 return(s->ctx->cipher_list->ciphers);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001341 }
1342
Adam Langley95c29f32014-06-20 12:00:00 -07001343 return(NULL);
1344 }
1345
1346/** return a STACK of the ciphers available for the SSL and in order of
1347 * algorithm id */
1348STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1349 {
1350 if (s != NULL)
1351 {
1352 if (s->cipher_list_by_id != NULL)
1353 {
1354 return(s->cipher_list_by_id);
1355 }
1356 else if ((s->ctx != NULL) &&
1357 (s->ctx->cipher_list_by_id != NULL))
1358 {
1359 return(s->ctx->cipher_list_by_id);
1360 }
1361 }
1362 return(NULL);
1363 }
1364
1365/** The old interface to get the same thing as SSL_get_ciphers() */
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001366const char *SSL_get_cipher_list(const SSL *s, int n)
Adam Langley95c29f32014-06-20 12:00:00 -07001367 {
David Benjamin6f260012014-08-15 13:49:12 -04001368 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001369 STACK_OF(SSL_CIPHER) *sk;
1370
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001371 if (s == NULL)
1372 return NULL;
1373 sk = SSL_get_ciphers(s);
1374 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk))
1375 return NULL;
1376 c = sk_SSL_CIPHER_value(sk, n);
1377 if (c == NULL)
1378 return NULL;
1379 return c->name;
Adam Langley95c29f32014-06-20 12:00:00 -07001380 }
1381
1382/** specify the ciphers to be used by default by the SSL_CTX */
1383int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1384 {
1385 STACK_OF(SSL_CIPHER) *sk;
1386
1387 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1388 &ctx->cipher_list_by_id,str, ctx->cert);
1389 /* ssl_create_cipher_list may return an empty stack if it
1390 * was unable to find a cipher matching the given rule string
1391 * (for example if the rule string specifies a cipher which
1392 * has been disabled). This is not an error as far as
1393 * ssl_create_cipher_list is concerned, and hence
1394 * ctx->cipher_list and ctx->cipher_list_by_id has been
1395 * updated. */
1396 if (sk == NULL)
1397 return 0;
1398 else if (sk_SSL_CIPHER_num(sk) == 0)
1399 {
1400 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1401 return 0;
1402 }
1403 return 1;
1404 }
1405
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001406int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str)
1407 {
1408 STACK_OF(SSL_CIPHER) *sk;
1409
1410 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert);
1411 if (sk == NULL)
1412 return 0;
1413 else if (sk_SSL_CIPHER_num(sk) == 0)
1414 {
1415 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH);
1416 return 0;
1417 }
1418 return 1;
1419 }
1420
Adam Langley95c29f32014-06-20 12:00:00 -07001421/** specify the ciphers to be used by the SSL */
1422int SSL_set_cipher_list(SSL *s,const char *str)
1423 {
1424 STACK_OF(SSL_CIPHER) *sk;
1425
1426 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1427 &s->cipher_list_by_id,str, s->cert);
1428 /* see comment in SSL_CTX_set_cipher_list */
1429 if (sk == NULL)
1430 return 0;
1431 else if (sk_SSL_CIPHER_num(sk) == 0)
1432 {
1433 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1434 return 0;
1435 }
1436 return 1;
1437 }
1438
David Benjamin5491e3f2014-09-29 19:33:09 -04001439int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p)
Adam Langley95c29f32014-06-20 12:00:00 -07001440 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001441 size_t i;
David Benjamin6f260012014-08-15 13:49:12 -04001442 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001443 CERT *ct = s->cert;
David Benjamin5491e3f2014-09-29 19:33:09 -04001444 uint8_t *q;
Adam Langley95c29f32014-06-20 12:00:00 -07001445 /* Set disabled masks for this session */
1446 ssl_set_client_disabled(s);
1447
1448 if (sk == NULL) return(0);
1449 q=p;
1450
1451 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1452 {
1453 c=sk_SSL_CIPHER_value(sk,i);
1454 /* Skip disabled ciphers */
1455 if (c->algorithm_ssl & ct->mask_ssl ||
1456 c->algorithm_mkey & ct->mask_k ||
1457 c->algorithm_auth & ct->mask_a)
1458 continue;
David Benjamin39482a12014-07-20 13:30:15 -04001459 s2n(ssl3_get_cipher_value(c), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001460 }
David Benjamin5491e3f2014-09-29 19:33:09 -04001461 /* If all ciphers were disabled, return the error to the caller. */
1462 if (p == q)
Adam Langley95c29f32014-06-20 12:00:00 -07001463 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001464 return 0;
1465 }
1466
1467 /* Add SCSVs. */
1468 if (!s->renegotiate)
1469 {
1470 s2n(SSL3_CK_SCSV & 0xffff, p);
1471 }
1472 if (s->fallback_scsv)
1473 {
1474 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001475 }
1476
1477 return(p-q);
1478 }
1479
David Benjamin9f2c0d72014-10-21 22:00:19 -04001480STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001481 {
David Benjamin39482a12014-07-20 13:30:15 -04001482 CBS cipher_suites = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001483 const SSL_CIPHER *c;
1484 STACK_OF(SSL_CIPHER) *sk;
David Benjamin39482a12014-07-20 13:30:15 -04001485
Adam Langley95c29f32014-06-20 12:00:00 -07001486 if (s->s3)
1487 s->s3->send_connection_binding = 0;
1488
David Benjamin39482a12014-07-20 13:30:15 -04001489 if (CBS_len(&cipher_suites) % 2 != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001490 {
1491 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001492 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001493 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001494
1495 sk = sk_SSL_CIPHER_new_null();
1496 if (sk == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001497 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001498 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1499 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001500 }
1501
David Benjamin39482a12014-07-20 13:30:15 -04001502 if (!CBS_stow(&cipher_suites,
1503 &s->cert->ciphers_raw, &s->cert->ciphers_rawlen))
Adam Langley95c29f32014-06-20 12:00:00 -07001504 {
1505 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1506 goto err;
1507 }
Adam Langley95c29f32014-06-20 12:00:00 -07001508
David Benjamin39482a12014-07-20 13:30:15 -04001509 while (CBS_len(&cipher_suites) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001510 {
David Benjamin39482a12014-07-20 13:30:15 -04001511 uint16_t cipher_suite;
1512
1513 if (!CBS_get_u16(&cipher_suites, &cipher_suite))
1514 {
1515 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1516 goto err;
1517 }
1518
David Benjamin9f2c0d72014-10-21 22:00:19 -04001519 /* Check for SCSV. */
David Benjamin39482a12014-07-20 13:30:15 -04001520 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff))
Adam Langley95c29f32014-06-20 12:00:00 -07001521 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001522 /* SCSV is fatal if renegotiating. */
Adam Langley95c29f32014-06-20 12:00:00 -07001523 if (s->renegotiate)
1524 {
1525 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1526 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1527 goto err;
1528 }
1529 s->s3->send_connection_binding = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001530 continue;
1531 }
1532
David Benjamin9f2c0d72014-10-21 22:00:19 -04001533 /* Check for FALLBACK_SCSV. */
1534 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff))
Adam Langley29707792014-06-20 12:00:00 -07001535 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001536 if (s->version < ssl_get_max_version(s))
1537 {
1538 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1539 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1540 goto err;
1541 }
1542 continue;
Adam Langley29707792014-06-20 12:00:00 -07001543 }
1544
David Benjamin39482a12014-07-20 13:30:15 -04001545 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -07001546 if (c != NULL)
1547 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001548 if (!sk_SSL_CIPHER_push(sk, c))
Adam Langley95c29f32014-06-20 12:00:00 -07001549 {
1550 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1551 goto err;
1552 }
1553 }
1554 }
1555
David Benjamin9f2c0d72014-10-21 22:00:19 -04001556 return sk;
1557
Adam Langley95c29f32014-06-20 12:00:00 -07001558err:
David Benjamin9f2c0d72014-10-21 22:00:19 -04001559 if (sk != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001560 sk_SSL_CIPHER_free(sk);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001561 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001562 }
1563
1564
Adam Langley95c29f32014-06-20 12:00:00 -07001565/** return a servername extension value if provided in Client Hello, or NULL.
1566 * So far, only host_name types are defined (RFC 3546).
1567 */
1568
1569const char *SSL_get_servername(const SSL *s, const int type)
1570 {
1571 if (type != TLSEXT_NAMETYPE_host_name)
1572 return NULL;
1573
1574 return s->session && !s->tlsext_hostname ?
1575 s->session->tlsext_hostname :
1576 s->tlsext_hostname;
1577 }
1578
1579int SSL_get_servername_type(const SSL *s)
1580 {
1581 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1582 return TLSEXT_NAMETYPE_host_name;
1583 return -1;
1584 }
1585
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001586void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx)
1587 {
1588 ctx->signed_cert_timestamps_enabled = 1;
1589 }
1590
1591int SSL_enable_signed_cert_timestamps(SSL *ssl)
1592 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001593 /* Currently not implemented server-side. */
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001594 if (ssl->server)
1595 return 0;
1596
1597 ssl->signed_cert_timestamps_enabled = 1;
1598 return 1;
1599 }
1600
David Benjamin6c7aed02014-08-27 16:42:38 -04001601void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx)
1602 {
1603 ctx->ocsp_stapling_enabled = 1;
1604 }
1605
1606int SSL_enable_ocsp_stapling(SSL *ssl)
1607 {
1608 /* Currently not implemented server-side. */
1609 if (ssl->server)
1610 return 0;
1611 ssl->ocsp_stapling_enabled = 1;
1612 return 1;
1613 }
1614
David Benjamin52d699f2014-11-25 01:35:18 -05001615void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out, size_t *out_len)
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001616 {
Adam Langley3cb50e02014-08-26 14:00:31 -07001617 SSL_SESSION *session = ssl->session;
1618
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001619 *out_len = 0;
1620 *out = NULL;
1621 if (ssl->server)
1622 return;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001623 if (!session || !session->tlsext_signed_cert_timestamp_list)
1624 return;
1625 *out = session->tlsext_signed_cert_timestamp_list;
1626 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1627 }
1628
David Benjamin52d699f2014-11-25 01:35:18 -05001629void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out, size_t *out_len)
David Benjamin6c7aed02014-08-27 16:42:38 -04001630 {
1631 SSL_SESSION *session = ssl->session;
1632
1633 *out_len = 0;
1634 *out = NULL;
1635 if (ssl->server)
1636 return;
1637 if (!session || !session->ocsp_response)
1638 return;
1639 *out = session->ocsp_response;
1640 *out_len = session->ocsp_response_length;
1641 }
1642
Adam Langley95c29f32014-06-20 12:00:00 -07001643/* SSL_select_next_proto implements the standard protocol selection. It is
1644 * expected that this function is called from the callback set by
1645 * SSL_CTX_set_next_proto_select_cb.
1646 *
1647 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1648 * strings. The length byte itself is not included in the length. A byte
1649 * string of length 0 is invalid. No byte string may be truncated.
1650 *
1651 * The current, but experimental algorithm for selecting the protocol is:
1652 *
1653 * 1) If the server doesn't support NPN then this is indicated to the
1654 * callback. In this case, the client application has to abort the connection
1655 * or have a default application level protocol.
1656 *
1657 * 2) If the server supports NPN, but advertises an empty list then the
1658 * client selects the first protcol in its list, but indicates via the
1659 * API that this fallback case was enacted.
1660 *
1661 * 3) Otherwise, the client finds the first protocol in the server's list
1662 * that it supports and selects this protocol. This is because it's
1663 * assumed that the server has better information about which protocol
1664 * a client should use.
1665 *
1666 * 4) If the client doesn't support any of the server's advertised
1667 * protocols, then this is treated the same as case 2.
1668 *
1669 * It returns either
1670 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1671 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1672 */
1673int SSL_select_next_proto(unsigned char **out, unsigned char *outlen, const unsigned char *server, unsigned int server_len, const unsigned char *client, unsigned int client_len)
1674 {
1675 unsigned int i, j;
1676 const unsigned char *result;
1677 int status = OPENSSL_NPN_UNSUPPORTED;
1678
1679 /* For each protocol in server preference order, see if we support it. */
1680 for (i = 0; i < server_len; )
1681 {
1682 for (j = 0; j < client_len; )
1683 {
1684 if (server[i] == client[j] &&
1685 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1686 {
1687 /* We found a match */
1688 result = &server[i];
1689 status = OPENSSL_NPN_NEGOTIATED;
1690 goto found;
1691 }
1692 j += client[j];
1693 j++;
1694 }
1695 i += server[i];
1696 i++;
1697 }
1698
1699 /* There's no overlap between our protocols and the server's list. */
1700 result = client;
1701 status = OPENSSL_NPN_NO_OVERLAP;
1702
1703 found:
1704 *out = (unsigned char *) result + 1;
1705 *outlen = result[0];
1706 return status;
1707 }
1708
Adam Langley95c29f32014-06-20 12:00:00 -07001709/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1710 * requested protocol for this connection and returns 0. If the client didn't
1711 * request any protocol, then *data is set to NULL.
1712 *
1713 * Note that the client can request any protocol it chooses. The value returned
1714 * from this function need not be a member of the list of supported protocols
1715 * provided by the callback.
1716 */
David Benjaminced551f2014-07-11 23:16:16 -04001717void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, unsigned *len)
Adam Langley95c29f32014-06-20 12:00:00 -07001718 {
1719 *data = s->next_proto_negotiated;
1720 if (!*data) {
1721 *len = 0;
1722 } else {
1723 *len = s->next_proto_negotiated_len;
1724 }
1725}
1726
1727/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1728 * TLS server needs a list of supported protocols for Next Protocol
1729 * Negotiation. The returned list must be in wire format. The list is returned
1730 * by setting |out| to point to it and |outlen| to its length. This memory will
1731 * not be modified, but one should assume that the SSL* keeps a reference to
1732 * it.
1733 *
1734 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1735 * such extension will be included in the ServerHello. */
1736void SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx, int (*cb) (SSL *ssl, const unsigned char **out, unsigned int *outlen, void *arg), void *arg)
1737 {
1738 ctx->next_protos_advertised_cb = cb;
1739 ctx->next_protos_advertised_cb_arg = arg;
1740 }
1741
1742/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1743 * client needs to select a protocol from the server's provided list. |out|
1744 * must be set to point to the selected protocol (which may be within |in|).
1745 * The length of the protocol name must be written into |outlen|. The server's
1746 * advertised protocols are provided in |in| and |inlen|. The callback can
1747 * assume that |in| is syntactically valid.
1748 *
1749 * The client must select a protocol. It is fatal to the connection if this
1750 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1751 */
1752void SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, int (*cb) (SSL *s, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg), void *arg)
1753 {
1754 ctx->next_proto_select_cb = cb;
1755 ctx->next_proto_select_cb_arg = arg;
1756 }
Adam Langley95c29f32014-06-20 12:00:00 -07001757
Adam Langley95c29f32014-06-20 12:00:00 -07001758/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1759 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1760 * length-prefixed strings).
1761 *
1762 * Returns 0 on success. */
1763int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1764 unsigned protos_len)
1765 {
1766 if (ctx->alpn_client_proto_list)
1767 OPENSSL_free(ctx->alpn_client_proto_list);
1768
David Benjamin072c9532014-07-26 11:44:25 -04001769 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001770 if (!ctx->alpn_client_proto_list)
1771 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001772 ctx->alpn_client_proto_list_len = protos_len;
1773
1774 return 0;
1775 }
1776
1777/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1778 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1779 * length-prefixed strings).
1780 *
1781 * Returns 0 on success. */
1782int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1783 unsigned protos_len)
1784 {
1785 if (ssl->alpn_client_proto_list)
1786 OPENSSL_free(ssl->alpn_client_proto_list);
1787
David Benjamin072c9532014-07-26 11:44:25 -04001788 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001789 if (!ssl->alpn_client_proto_list)
1790 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001791 ssl->alpn_client_proto_list_len = protos_len;
1792
1793 return 0;
1794 }
1795
1796/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1797 * during ClientHello processing in order to select an ALPN protocol from the
1798 * client's list of offered protocols. */
1799void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1800 int (*cb) (SSL *ssl,
1801 const unsigned char **out,
1802 unsigned char *outlen,
1803 const unsigned char *in,
1804 unsigned int inlen,
1805 void *arg),
1806 void *arg)
1807 {
1808 ctx->alpn_select_cb = cb;
1809 ctx->alpn_select_cb_arg = arg;
1810 }
1811
1812/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1813 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1814 * including the leading length-prefix byte). If the server didn't respond with
1815 * a negotiated protocol then |*len| will be zero. */
1816void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1817 unsigned *len)
1818 {
1819 *data = NULL;
1820 if (ssl->s3)
1821 *data = ssl->s3->alpn_selected;
1822 if (*data == NULL)
1823 *len = 0;
1824 else
1825 *len = ssl->s3->alpn_selected_len;
1826 }
Adam Langley95c29f32014-06-20 12:00:00 -07001827
1828int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1829 const char *label, size_t llen, const unsigned char *p, size_t plen,
1830 int use_context)
1831 {
1832 if (s->version < TLS1_VERSION)
1833 return -1;
1834
1835 return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1836 llen, p, plen,
1837 use_context);
1838 }
1839
1840static uint32_t ssl_session_hash(const SSL_SESSION *a)
1841 {
1842 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1843 ((uint32_t) a->session_id[1] << 8) ||
1844 ((uint32_t) a->session_id[2] << 16) ||
1845 ((uint32_t) a->session_id[3] << 24);
1846
1847 return hash;
1848 }
1849
1850/* NB: If this function (or indeed the hash function which uses a sort of
1851 * coarser function than this one) is changed, ensure
1852 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1853 * able to construct an SSL_SESSION that will collide with any existing session
1854 * with a matching session ID. */
1855static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1856 {
1857 if (a->ssl_version != b->ssl_version)
1858 return(1);
1859 if (a->session_id_length != b->session_id_length)
1860 return(1);
1861 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1862 }
1863
1864SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1865 {
1866 SSL_CTX *ret=NULL;
1867
1868 if (meth == NULL)
1869 {
1870 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1871 return(NULL);
1872 }
1873
Adam Langley95c29f32014-06-20 12:00:00 -07001874 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1875 {
1876 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1877 goto err;
1878 }
1879 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1880 if (ret == NULL)
1881 goto err;
1882
1883 memset(ret,0,sizeof(SSL_CTX));
1884
1885 ret->method=meth;
1886
1887 ret->cert_store=NULL;
1888 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1889 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1890 ret->session_cache_head=NULL;
1891 ret->session_cache_tail=NULL;
1892
1893 /* We take the system default */
David Benjaminf4501342014-08-14 17:24:37 -04001894 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001895
1896 ret->new_session_cb=0;
1897 ret->remove_session_cb=0;
1898 ret->get_session_cb=0;
1899 ret->generate_session_id=0;
1900
1901 memset((char *)&ret->stats,0,sizeof(ret->stats));
1902
1903 ret->references=1;
1904 ret->quiet_shutdown=0;
1905
Adam Langley95c29f32014-06-20 12:00:00 -07001906 ret->info_callback=NULL;
1907
1908 ret->app_verify_callback=0;
1909 ret->app_verify_arg=NULL;
1910
1911 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1912 ret->read_ahead=0;
1913 ret->msg_callback=0;
1914 ret->msg_callback_arg=NULL;
1915 ret->verify_mode=SSL_VERIFY_NONE;
1916#if 0
1917 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1918#endif
1919 ret->sid_ctx_length=0;
1920 ret->default_verify_callback=NULL;
1921 if ((ret->cert=ssl_cert_new()) == NULL)
1922 goto err;
1923
1924 ret->default_passwd_callback=0;
1925 ret->default_passwd_callback_userdata=NULL;
1926 ret->client_cert_cb=0;
1927 ret->app_gen_cookie_cb=0;
1928 ret->app_verify_cookie_cb=0;
1929
1930 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1931 if (ret->sessions == NULL) goto err;
1932 ret->cert_store=X509_STORE_new();
1933 if (ret->cert_store == NULL) goto err;
1934
1935 ssl_create_cipher_list(ret->method,
1936 &ret->cipher_list,&ret->cipher_list_by_id,
David Benjamin5e4f6e92014-11-17 03:23:24 -05001937 SSL_DEFAULT_CIPHER_LIST, ret->cert);
Adam Langley95c29f32014-06-20 12:00:00 -07001938 if (ret->cipher_list == NULL
Adam Langley858a88d2014-06-20 12:00:00 -07001939 || sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001940 {
1941 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1942 goto err2;
1943 }
1944
1945 ret->param = X509_VERIFY_PARAM_new();
1946 if (!ret->param)
1947 goto err;
1948
Adam Langley95c29f32014-06-20 12:00:00 -07001949 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1950 goto err;
1951
1952 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1953
1954 ret->extra_certs=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001955
1956 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1957
Adam Langley95c29f32014-06-20 12:00:00 -07001958 ret->tlsext_servername_callback = 0;
1959 ret->tlsext_servername_arg = NULL;
1960 /* Setup RFC4507 ticket keys */
1961 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
1962 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
1963 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
1964 ret->options |= SSL_OP_NO_TICKET;
1965
1966 ret->tlsext_status_cb = 0;
1967 ret->tlsext_status_arg = NULL;
1968
Adam Langley95c29f32014-06-20 12:00:00 -07001969 ret->next_protos_advertised_cb = 0;
1970 ret->next_proto_select_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001971 ret->psk_identity_hint=NULL;
1972 ret->psk_client_callback=NULL;
1973 ret->psk_server_callback=NULL;
David Benjamin422d3a42014-08-20 11:09:03 -04001974
Adam Langley95c29f32014-06-20 12:00:00 -07001975 /* Default is to connect to non-RI servers. When RI is more widely
1976 * deployed might change this.
1977 */
1978 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1979
1980 return(ret);
1981err:
1982 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
1983err2:
1984 if (ret != NULL) SSL_CTX_free(ret);
1985 return(NULL);
1986 }
1987
Adam Langley95c29f32014-06-20 12:00:00 -07001988
1989void SSL_CTX_free(SSL_CTX *a)
1990 {
1991 int i;
1992
1993 if (a == NULL) return;
1994
1995 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
1996#ifdef REF_PRINT
1997 REF_PRINT("SSL_CTX",a);
1998#endif
1999 if (i > 0) return;
2000#ifdef REF_CHECK
2001 if (i < 0)
2002 {
2003 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
2004 abort(); /* ok */
2005 }
2006#endif
2007
2008 if (a->param)
2009 X509_VERIFY_PARAM_free(a->param);
2010
2011 /*
2012 * Free internal session cache. However: the remove_cb() may reference
2013 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2014 * after the sessions were flushed.
2015 * As the ex_data handling routines might also touch the session cache,
2016 * the most secure solution seems to be: empty (flush) the cache, then
2017 * free ex_data, then finally free the cache.
2018 * (See ticket [openssl.org #212].)
2019 */
2020 if (a->sessions != NULL)
2021 SSL_CTX_flush_sessions(a,0);
2022
2023 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
2024
2025 if (a->sessions != NULL)
2026 lh_SSL_SESSION_free(a->sessions);
2027
2028 if (a->cert_store != NULL)
2029 X509_STORE_free(a->cert_store);
2030 if (a->cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002031 ssl_cipher_preference_list_free(a->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002032 if (a->cipher_list_by_id != NULL)
2033 sk_SSL_CIPHER_free(a->cipher_list_by_id);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07002034 if (a->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002035 ssl_cipher_preference_list_free(a->cipher_list_tls11);
Adam Langley95c29f32014-06-20 12:00:00 -07002036 if (a->cert != NULL)
2037 ssl_cert_free(a->cert);
2038 if (a->client_CA != NULL)
2039 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2040 if (a->extra_certs != NULL)
2041 sk_X509_pop_free(a->extra_certs,X509_free);
Adam Langley95c29f32014-06-20 12:00:00 -07002042
2043 if (a->srtp_profiles)
2044 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2045
Adam Langley95c29f32014-06-20 12:00:00 -07002046 if (a->psk_identity_hint)
2047 OPENSSL_free(a->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07002048
Adam Langley95c29f32014-06-20 12:00:00 -07002049 if (a->tlsext_ecpointformatlist)
2050 OPENSSL_free(a->tlsext_ecpointformatlist);
2051 if (a->tlsext_ellipticcurvelist)
2052 OPENSSL_free(a->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -07002053 if (a->alpn_client_proto_list != NULL)
2054 OPENSSL_free(a->alpn_client_proto_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002055
Adam Langley1258b6a2014-06-20 12:00:00 -07002056 if (a->tlsext_channel_id_private)
2057 EVP_PKEY_free(a->tlsext_channel_id_private);
Adam Langley1258b6a2014-06-20 12:00:00 -07002058
David Benjamin859ec3c2014-09-02 16:29:36 -04002059 if (a->keylog_bio)
2060 BIO_free(a->keylog_bio);
2061
Adam Langley95c29f32014-06-20 12:00:00 -07002062 OPENSSL_free(a);
2063 }
2064
2065void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2066 {
2067 ctx->default_passwd_callback=cb;
2068 }
2069
2070void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2071 {
2072 ctx->default_passwd_callback_userdata=u;
2073 }
2074
2075void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2076 {
2077 ctx->app_verify_callback=cb;
2078 ctx->app_verify_arg=arg;
2079 }
2080
2081void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2082 {
2083 ctx->verify_mode=mode;
2084 ctx->default_verify_callback=cb;
2085 }
2086
2087void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2088 {
2089 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2090 }
2091
2092void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2093 {
2094 ssl_cert_set_cert_cb(c->cert, cb, arg);
2095 }
2096
2097void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2098 {
2099 ssl_cert_set_cert_cb(s->cert, cb, arg);
2100 }
2101
David Benjamin033e5f42014-11-13 18:47:41 -05002102static int ssl_has_key(SSL *s, size_t idx)
2103 {
2104 CERT_PKEY *cpk = &s->cert->pkeys[idx];
2105 return cpk->x509 && cpk->privatekey;
2106 }
2107
David Benjaminf31e6812014-11-13 18:05:55 -05002108void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
2109 unsigned long *out_mask_a)
Adam Langley95c29f32014-06-20 12:00:00 -07002110 {
David Benjaminf31e6812014-11-13 18:05:55 -05002111 CERT *c = s->cert;
David Benjaminf31e6812014-11-13 18:05:55 -05002112 int rsa_enc, rsa_sign, dh_tmp;
2113 unsigned long mask_k, mask_a;
David Benjamin060d9d22014-07-15 00:54:26 -04002114 int have_ecc_cert, ecdsa_ok;
David Benjamin0da0e182014-08-19 16:20:28 -04002115 int have_ecdh_tmp;
David Benjaminf31e6812014-11-13 18:05:55 -05002116 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002117
David Benjaminf31e6812014-11-13 18:05:55 -05002118 if (c == NULL)
2119 {
2120 /* TODO(davidben): Is this codepath possible? */
2121 *out_mask_k = 0;
2122 *out_mask_a = 0;
2123 return;
2124 }
Adam Langley95c29f32014-06-20 12:00:00 -07002125
David Benjaminf31e6812014-11-13 18:05:55 -05002126 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002127
David Benjaminf31e6812014-11-13 18:05:55 -05002128 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
David Benjamin033e5f42014-11-13 18:47:41 -05002129 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2130 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2131 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
David Benjaminf31e6812014-11-13 18:05:55 -05002132 mask_k = 0;
2133 mask_a = 0;
2134
David Benjamin77a942b2014-07-15 01:22:50 -04002135 if (rsa_enc)
David Benjaminf31e6812014-11-13 18:05:55 -05002136 mask_k |= SSL_kRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002137
Adam Langley95c29f32014-06-20 12:00:00 -07002138 if (dh_tmp)
David Benjaminf31e6812014-11-13 18:05:55 -05002139 mask_k |= SSL_kEDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002140
Adam Langley95c29f32014-06-20 12:00:00 -07002141 if (rsa_enc || rsa_sign)
2142 {
David Benjaminf31e6812014-11-13 18:05:55 -05002143 mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002144 }
2145
David Benjaminf31e6812014-11-13 18:05:55 -05002146 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002147
David Benjamin0da0e182014-08-19 16:20:28 -04002148 /* An ECC certificate may be usable for ECDSA cipher suites depending on
David Benjamin033e5f42014-11-13 18:47:41 -05002149 * the key usage extension and on the client's curve preferences. */
Adam Langley95c29f32014-06-20 12:00:00 -07002150 if (have_ecc_cert)
2151 {
David Benjamin033e5f42014-11-13 18:47:41 -05002152 x = c->pkeys[SSL_PKEY_ECC].x509;
Adam Langley95c29f32014-06-20 12:00:00 -07002153 /* This call populates extension flags (ex_flags) */
2154 X509_check_purpose(x, -1, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07002155 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2156 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
David Benjamin033e5f42014-11-13 18:47:41 -05002157 if (!tls1_check_ec_cert(s, x))
Adam Langley95c29f32014-06-20 12:00:00 -07002158 ecdsa_ok = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002159 if (ecdsa_ok)
2160 {
David Benjaminf31e6812014-11-13 18:05:55 -05002161 mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002162 }
Adam Langley95c29f32014-06-20 12:00:00 -07002163 }
Adam Langley95c29f32014-06-20 12:00:00 -07002164
David Benjaminf31e6812014-11-13 18:05:55 -05002165 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2166 * key, check it. */
2167 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002168 {
David Benjaminf31e6812014-11-13 18:05:55 -05002169 mask_k |= SSL_kEECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002170 }
Adam Langley95c29f32014-06-20 12:00:00 -07002171
David Benjaminf31e6812014-11-13 18:05:55 -05002172 /* PSK requires a server callback. */
2173 if (s->psk_server_callback != NULL)
2174 {
2175 mask_k |= SSL_kPSK;
2176 mask_a |= SSL_aPSK;
2177 }
Adam Langley95c29f32014-06-20 12:00:00 -07002178
David Benjaminf31e6812014-11-13 18:05:55 -05002179 *out_mask_k = mask_k;
2180 *out_mask_a = mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002181 }
2182
2183/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2184#define ku_reject(x, usage) \
2185 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2186
Adam Langley95c29f32014-06-20 12:00:00 -07002187
2188int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2189 {
David Benjamin0da0e182014-08-19 16:20:28 -04002190 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002191 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2192 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2193
Adam Langley95c29f32014-06-20 12:00:00 -07002194 alg_a = cs->algorithm_auth;
2195
Adam Langley95c29f32014-06-20 12:00:00 -07002196 /* This call populates the ex_flags field correctly */
2197 X509_check_purpose(x, -1, 0);
2198 if ((x->sig_alg) && (x->sig_alg->algorithm))
2199 {
2200 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2201 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2202 }
Adam Langley95c29f32014-06-20 12:00:00 -07002203 if (alg_a & SSL_aECDSA)
2204 {
2205 /* key usage, if present, must allow signing */
2206 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2207 {
2208 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2209 return 0;
2210 }
2211 }
2212
2213 return 1; /* all checks are ok */
2214 }
2215
Adam Langley95c29f32014-06-20 12:00:00 -07002216
2217static int ssl_get_server_cert_index(const SSL *s)
2218 {
2219 int idx;
2220 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2221 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2222 idx = SSL_PKEY_RSA_SIGN;
2223 if (idx == -1)
2224 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2225 return idx;
2226 }
2227
2228CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2229 {
David Benjaminf31e6812014-11-13 18:05:55 -05002230 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002231
2232 /* This may or may not be an error. */
2233 if (i < 0)
2234 return NULL;
2235
2236 /* May be NULL. */
David Benjaminf31e6812014-11-13 18:05:55 -05002237 return &s->cert->pkeys[i];
Adam Langley95c29f32014-06-20 12:00:00 -07002238 }
2239
David Benjaminec2f27d2014-11-13 19:17:25 -05002240EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher)
Adam Langley95c29f32014-06-20 12:00:00 -07002241 {
2242 unsigned long alg_a;
2243 CERT *c;
2244 int idx = -1;
2245
2246 alg_a = cipher->algorithm_auth;
2247 c=s->cert;
2248
David Benjaminef2116d2014-08-19 20:21:56 -04002249 if (alg_a & SSL_aRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002250 {
2251 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2252 idx = SSL_PKEY_RSA_SIGN;
2253 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2254 idx = SSL_PKEY_RSA_ENC;
2255 }
2256 else if ((alg_a & SSL_aECDSA) &&
2257 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2258 idx = SSL_PKEY_ECC;
2259 if (idx == -1)
2260 {
2261 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2262 return(NULL);
2263 }
Adam Langley95c29f32014-06-20 12:00:00 -07002264 return c->pkeys[idx].privatekey;
2265 }
2266
Adam Langley95c29f32014-06-20 12:00:00 -07002267void ssl_update_cache(SSL *s,int mode)
2268 {
2269 int i;
2270
2271 /* If the session_id_length is 0, we are not supposed to cache it,
2272 * and it would be rather hard to do anyway :-) */
2273 if (s->session->session_id_length == 0) return;
2274
David Benjamin041b58a2014-08-27 15:25:21 -04002275 i=s->initial_ctx->session_cache_mode;
Adam Langley95c29f32014-06-20 12:00:00 -07002276 if ((i & mode) && (!s->hit)
2277 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
David Benjamin041b58a2014-08-27 15:25:21 -04002278 || SSL_CTX_add_session(s->initial_ctx,s->session))
2279 && (s->initial_ctx->new_session_cb != NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07002280 {
2281 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
David Benjamin041b58a2014-08-27 15:25:21 -04002282 if (!s->initial_ctx->new_session_cb(s,s->session))
Adam Langley95c29f32014-06-20 12:00:00 -07002283 SSL_SESSION_free(s->session);
2284 }
2285
2286 /* auto flush every 255 connections */
2287 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2288 ((i & mode) == mode))
2289 {
2290 if ( (((mode & SSL_SESS_CACHE_CLIENT)
David Benjamin041b58a2014-08-27 15:25:21 -04002291 ?s->initial_ctx->stats.sess_connect_good
2292 :s->initial_ctx->stats.sess_accept_good) & 0xff) == 0xff)
Adam Langley95c29f32014-06-20 12:00:00 -07002293 {
David Benjamin041b58a2014-08-27 15:25:21 -04002294 SSL_CTX_flush_sessions(s->initial_ctx,(unsigned long)time(NULL));
Adam Langley95c29f32014-06-20 12:00:00 -07002295 }
2296 }
2297 }
2298
Adam Langley95c29f32014-06-20 12:00:00 -07002299int SSL_get_error(const SSL *s,int i)
2300 {
2301 int reason;
2302 unsigned long l;
2303 BIO *bio;
2304
2305 if (i > 0) return(SSL_ERROR_NONE);
2306
2307 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2308 * etc, where we do encode the error */
2309 if ((l=ERR_peek_error()) != 0)
2310 {
2311 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2312 return(SSL_ERROR_SYSCALL);
2313 else
2314 return(SSL_ERROR_SSL);
2315 }
2316
Adam Langleyb2ce0582014-06-20 12:00:00 -07002317 if ((i < 0) && SSL_want_session(s))
2318 return(SSL_ERROR_PENDING_SESSION);
2319
Adam Langleydc9b1412014-06-20 12:00:00 -07002320 if ((i < 0) && SSL_want_certificate(s))
2321 return(SSL_ERROR_PENDING_CERTIFICATE);
2322
Adam Langley95c29f32014-06-20 12:00:00 -07002323 if ((i < 0) && SSL_want_read(s))
2324 {
2325 bio=SSL_get_rbio(s);
2326 if (BIO_should_read(bio))
2327 return(SSL_ERROR_WANT_READ);
2328 else if (BIO_should_write(bio))
2329 /* This one doesn't make too much sense ... We never try
2330 * to write to the rbio, and an application program where
2331 * rbio and wbio are separate couldn't even know what it
2332 * should wait for.
2333 * However if we ever set s->rwstate incorrectly
2334 * (so that we have SSL_want_read(s) instead of
2335 * SSL_want_write(s)) and rbio and wbio *are* the same,
2336 * this test works around that bug; so it might be safer
2337 * to keep it. */
2338 return(SSL_ERROR_WANT_WRITE);
2339 else if (BIO_should_io_special(bio))
2340 {
2341 reason=BIO_get_retry_reason(bio);
2342 if (reason == BIO_RR_CONNECT)
2343 return(SSL_ERROR_WANT_CONNECT);
2344 else if (reason == BIO_RR_ACCEPT)
2345 return(SSL_ERROR_WANT_ACCEPT);
2346 else
2347 return(SSL_ERROR_SYSCALL); /* unknown */
2348 }
2349 }
2350
2351 if ((i < 0) && SSL_want_write(s))
2352 {
2353 bio=SSL_get_wbio(s);
2354 if (BIO_should_write(bio))
2355 return(SSL_ERROR_WANT_WRITE);
2356 else if (BIO_should_read(bio))
2357 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2358 return(SSL_ERROR_WANT_READ);
2359 else if (BIO_should_io_special(bio))
2360 {
2361 reason=BIO_get_retry_reason(bio);
2362 if (reason == BIO_RR_CONNECT)
2363 return(SSL_ERROR_WANT_CONNECT);
2364 else if (reason == BIO_RR_ACCEPT)
2365 return(SSL_ERROR_WANT_ACCEPT);
2366 else
2367 return(SSL_ERROR_SYSCALL);
2368 }
2369 }
2370 if ((i < 0) && SSL_want_x509_lookup(s))
2371 {
2372 return(SSL_ERROR_WANT_X509_LOOKUP);
2373 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002374 if ((i < 0) && SSL_want_channel_id_lookup(s))
2375 {
2376 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2377 }
Adam Langley95c29f32014-06-20 12:00:00 -07002378
2379 if (i == 0)
2380 {
David Benjamin5e4f6e92014-11-17 03:23:24 -05002381 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2382 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
Adam Langley95c29f32014-06-20 12:00:00 -07002383 return(SSL_ERROR_ZERO_RETURN);
Adam Langley95c29f32014-06-20 12:00:00 -07002384 }
2385 return(SSL_ERROR_SYSCALL);
2386 }
2387
2388int SSL_do_handshake(SSL *s)
2389 {
2390 int ret=1;
2391
2392 if (s->handshake_func == NULL)
2393 {
2394 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2395 return(-1);
2396 }
2397
2398 s->method->ssl_renegotiate_check(s);
2399
2400 if (SSL_in_init(s) || SSL_in_before(s))
2401 {
2402 ret=s->handshake_func(s);
2403 }
2404 return(ret);
2405 }
2406
Adam Langley95c29f32014-06-20 12:00:00 -07002407void SSL_set_accept_state(SSL *s)
2408 {
2409 s->server=1;
2410 s->shutdown=0;
2411 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2412 s->handshake_func=s->method->ssl_accept;
2413 /* clear the current cipher */
2414 ssl_clear_cipher_ctx(s);
2415 ssl_clear_hash_ctx(&s->read_hash);
2416 ssl_clear_hash_ctx(&s->write_hash);
2417 }
2418
2419void SSL_set_connect_state(SSL *s)
2420 {
2421 s->server=0;
2422 s->shutdown=0;
2423 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2424 s->handshake_func=s->method->ssl_connect;
2425 /* clear the current cipher */
2426 ssl_clear_cipher_ctx(s);
2427 ssl_clear_hash_ctx(&s->read_hash);
2428 ssl_clear_hash_ctx(&s->write_hash);
2429 }
2430
2431int ssl_undefined_function(SSL *s)
2432 {
2433 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2434 return(0);
2435 }
2436
2437int ssl_undefined_void_function(void)
2438 {
2439 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2440 return(0);
2441 }
2442
2443int ssl_undefined_const_function(const SSL *s)
2444 {
2445 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2446 return(0);
2447 }
2448
Adam Langley0f4746e2014-08-13 12:26:32 -07002449static const char *ssl_get_version(int version)
Adam Langley95c29f32014-06-20 12:00:00 -07002450 {
Adam Langley0f4746e2014-08-13 12:26:32 -07002451 if (version == TLS1_2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002452 return("TLSv1.2");
Adam Langley0f4746e2014-08-13 12:26:32 -07002453 else if (version == TLS1_1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002454 return("TLSv1.1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002455 else if (version == TLS1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002456 return("TLSv1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002457 else if (version == SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002458 return("SSLv3");
Adam Langley95c29f32014-06-20 12:00:00 -07002459 else
2460 return("unknown");
2461 }
2462
Adam Langley0f4746e2014-08-13 12:26:32 -07002463const char *SSL_get_version(const SSL *s)
2464 {
2465 return ssl_get_version(s->version);
2466 }
2467
2468const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2469 {
2470 return ssl_get_version(sess->ssl_version);
2471 }
2472
Adam Langley95c29f32014-06-20 12:00:00 -07002473void ssl_clear_cipher_ctx(SSL *s)
2474 {
2475 if (s->enc_read_ctx != NULL)
2476 {
2477 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2478 OPENSSL_free(s->enc_read_ctx);
2479 s->enc_read_ctx=NULL;
2480 }
2481 if (s->enc_write_ctx != NULL)
2482 {
2483 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2484 OPENSSL_free(s->enc_write_ctx);
2485 s->enc_write_ctx=NULL;
2486 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002487 if (s->aead_read_ctx != NULL)
2488 {
2489 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2490 OPENSSL_free(s->aead_read_ctx);
2491 s->aead_read_ctx = NULL;
2492 }
2493 if (s->aead_write_ctx != NULL)
2494 {
2495 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2496 OPENSSL_free(s->aead_write_ctx);
2497 s->aead_write_ctx = NULL;
2498 }
Adam Langley95c29f32014-06-20 12:00:00 -07002499 }
2500
2501X509 *SSL_get_certificate(const SSL *s)
2502 {
2503 if (s->cert != NULL)
2504 return(s->cert->key->x509);
2505 else
2506 return(NULL);
2507 }
2508
2509EVP_PKEY *SSL_get_privatekey(const SSL *s)
2510 {
2511 if (s->cert != NULL)
2512 return(s->cert->key->privatekey);
2513 else
2514 return(NULL);
2515 }
2516
2517X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2518 {
2519 if (ctx->cert != NULL)
2520 return ctx->cert->key->x509;
2521 else
2522 return NULL;
2523 }
2524
2525EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2526 {
2527 if (ctx->cert != NULL)
2528 return ctx->cert->key->privatekey;
2529 else
2530 return NULL ;
2531 }
2532
2533const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2534 {
2535 if ((s->session != NULL) && (s->session->cipher != NULL))
2536 return(s->session->cipher);
2537 return(NULL);
2538 }
2539const void *SSL_get_current_compression(SSL *s)
2540 {
2541 return NULL;
2542 }
2543const void *SSL_get_current_expansion(SSL *s)
2544 {
2545 return NULL;
2546 }
2547
2548int ssl_init_wbio_buffer(SSL *s,int push)
2549 {
2550 BIO *bbio;
2551
2552 if (s->bbio == NULL)
2553 {
2554 bbio=BIO_new(BIO_f_buffer());
2555 if (bbio == NULL) return(0);
2556 s->bbio=bbio;
2557 }
2558 else
2559 {
2560 bbio=s->bbio;
2561 if (s->bbio == s->wbio)
2562 s->wbio=BIO_pop(s->wbio);
2563 }
2564 (void)BIO_reset(bbio);
2565/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2566 if (!BIO_set_read_buffer_size(bbio,1))
2567 {
2568 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2569 return(0);
2570 }
2571 if (push)
2572 {
2573 if (s->wbio != bbio)
2574 s->wbio=BIO_push(bbio,s->wbio);
2575 }
2576 else
2577 {
2578 if (s->wbio == bbio)
2579 s->wbio=BIO_pop(bbio);
2580 }
2581 return(1);
2582 }
2583
2584void ssl_free_wbio_buffer(SSL *s)
2585 {
2586 if (s->bbio == NULL) return;
2587
2588 if (s->bbio == s->wbio)
2589 {
2590 /* remove buffering */
2591 s->wbio=BIO_pop(s->wbio);
2592#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2593 assert(s->wbio != NULL);
2594#endif
2595 }
2596 BIO_free(s->bbio);
2597 s->bbio=NULL;
2598 }
2599
2600void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2601 {
2602 ctx->quiet_shutdown=mode;
2603 }
2604
2605int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2606 {
2607 return(ctx->quiet_shutdown);
2608 }
2609
2610void SSL_set_quiet_shutdown(SSL *s,int mode)
2611 {
2612 s->quiet_shutdown=mode;
2613 }
2614
2615int SSL_get_quiet_shutdown(const SSL *s)
2616 {
2617 return(s->quiet_shutdown);
2618 }
2619
2620void SSL_set_shutdown(SSL *s,int mode)
2621 {
2622 s->shutdown=mode;
2623 }
2624
2625int SSL_get_shutdown(const SSL *s)
2626 {
2627 return(s->shutdown);
2628 }
2629
2630int SSL_version(const SSL *s)
2631 {
2632 return(s->version);
2633 }
2634
2635SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2636 {
2637 return(ssl->ctx);
2638 }
2639
2640SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2641 {
2642 if (ssl->ctx == ctx)
2643 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002644 if (ctx == NULL)
2645 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002646 if (ssl->cert != NULL)
2647 ssl_cert_free(ssl->cert);
2648 ssl->cert = ssl_cert_dup(ctx->cert);
2649 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2650 if (ssl->ctx != NULL)
2651 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2652 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07002653
2654 ssl->sid_ctx_length = ctx->sid_ctx_length;
2655 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2656 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2657
Adam Langley95c29f32014-06-20 12:00:00 -07002658 return(ssl->ctx);
2659 }
2660
2661#ifndef OPENSSL_NO_STDIO
2662int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2663 {
2664 return(X509_STORE_set_default_paths(ctx->cert_store));
2665 }
2666
2667int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2668 const char *CApath)
2669 {
2670 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2671 }
2672#endif
2673
2674void SSL_set_info_callback(SSL *ssl,
2675 void (*cb)(const SSL *ssl,int type,int val))
2676 {
2677 ssl->info_callback=cb;
2678 }
2679
2680/* One compiler (Diab DCC) doesn't like argument names in returned
2681 function pointer. */
2682void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2683 {
2684 return ssl->info_callback;
2685 }
2686
2687int SSL_state(const SSL *ssl)
2688 {
2689 return(ssl->state);
2690 }
2691
2692void SSL_set_state(SSL *ssl, int state)
2693 {
2694 ssl->state = state;
2695 }
2696
2697void SSL_set_verify_result(SSL *ssl,long arg)
2698 {
2699 ssl->verify_result=arg;
2700 }
2701
2702long SSL_get_verify_result(const SSL *ssl)
2703 {
2704 return(ssl->verify_result);
2705 }
2706
2707int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2708 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2709 {
2710 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2711 new_func, dup_func, free_func);
2712 }
2713
2714int SSL_set_ex_data(SSL *s,int idx,void *arg)
2715 {
2716 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2717 }
2718
2719void *SSL_get_ex_data(const SSL *s,int idx)
2720 {
2721 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2722 }
2723
2724int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2725 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2726 {
2727 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2728 new_func, dup_func, free_func);
2729 }
2730
2731int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2732 {
2733 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2734 }
2735
2736void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2737 {
2738 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2739 }
2740
2741int ssl_ok(SSL *s)
2742 {
2743 return(1);
2744 }
2745
2746X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2747 {
2748 return(ctx->cert_store);
2749 }
2750
2751void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2752 {
2753 if (ctx->cert_store != NULL)
2754 X509_STORE_free(ctx->cert_store);
2755 ctx->cert_store=store;
2756 }
2757
2758int SSL_want(const SSL *s)
2759 {
2760 return(s->rwstate);
2761 }
2762
2763/*!
2764 * \brief Set the callback for generating temporary RSA keys.
2765 * \param ctx the SSL context.
2766 * \param cb the callback
2767 */
2768
Adam Langley95c29f32014-06-20 12:00:00 -07002769void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2770 int is_export,
2771 int keylength))
2772 {
2773 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2774 }
2775
2776void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2777 int is_export,
2778 int keylength))
2779 {
2780 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2781 }
Adam Langley95c29f32014-06-20 12:00:00 -07002782
2783#ifdef DOXYGEN
2784/*!
2785 * \brief The RSA temporary key callback function.
2786 * \param ssl the SSL session.
2787 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2788 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2789 * of the required key in bits.
2790 * \return the temporary RSA key.
2791 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2792 */
2793
2794RSA *cb(SSL *ssl,int is_export,int keylength)
2795 {}
2796#endif
2797
2798/*!
2799 * \brief Set the callback for generating temporary DH keys.
2800 * \param ctx the SSL context.
2801 * \param dh the callback
2802 */
2803
Adam Langley95c29f32014-06-20 12:00:00 -07002804void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2805 int keylength))
2806 {
2807 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2808 }
2809
2810void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2811 int keylength))
2812 {
2813 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2814 }
Adam Langley95c29f32014-06-20 12:00:00 -07002815
Adam Langley95c29f32014-06-20 12:00:00 -07002816void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2817 int keylength))
2818 {
2819 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2820 }
2821
2822void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2823 int keylength))
2824 {
2825 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2826 }
Adam Langley95c29f32014-06-20 12:00:00 -07002827
Adam Langley95c29f32014-06-20 12:00:00 -07002828int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2829 {
2830 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2831 {
2832 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2833 return 0;
2834 }
2835 if (ctx->psk_identity_hint != NULL)
2836 OPENSSL_free(ctx->psk_identity_hint);
2837 if (identity_hint != NULL)
2838 {
2839 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2840 if (ctx->psk_identity_hint == NULL)
2841 return 0;
2842 }
2843 else
2844 ctx->psk_identity_hint = NULL;
2845 return 1;
2846 }
2847
2848int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
2849 {
2850 if (s == NULL)
2851 return 0;
2852
Adam Langley95c29f32014-06-20 12:00:00 -07002853 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2854 {
2855 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2856 return 0;
2857 }
Adam Langley0289c732014-06-20 12:00:00 -07002858
David Benjamin688d8df2014-11-02 23:06:42 -05002859 /* Clear currently configured hint, if any. */
Adam Langley0289c732014-06-20 12:00:00 -07002860 if (s->psk_identity_hint != NULL)
2861 {
2862 OPENSSL_free(s->psk_identity_hint);
2863 s->psk_identity_hint = NULL;
2864 }
Adam Langley0289c732014-06-20 12:00:00 -07002865
Adam Langley95c29f32014-06-20 12:00:00 -07002866 if (identity_hint != NULL)
2867 {
David Benjamin688d8df2014-11-02 23:06:42 -05002868 s->psk_identity_hint = BUF_strdup(identity_hint);
2869 if (s->psk_identity_hint == NULL)
2870 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002871 }
Adam Langley95c29f32014-06-20 12:00:00 -07002872 return 1;
2873 }
2874
2875const char *SSL_get_psk_identity_hint(const SSL *s)
2876 {
Adam Langley0289c732014-06-20 12:00:00 -07002877 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002878 return NULL;
David Benjamin688d8df2014-11-02 23:06:42 -05002879 return s->psk_identity_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07002880 }
2881
2882const char *SSL_get_psk_identity(const SSL *s)
2883 {
2884 if (s == NULL || s->session == NULL)
2885 return NULL;
2886 return(s->session->psk_identity);
2887 }
2888
2889void SSL_set_psk_client_callback(SSL *s,
2890 unsigned int (*cb)(SSL *ssl, const char *hint,
2891 char *identity, unsigned int max_identity_len, unsigned char *psk,
2892 unsigned int max_psk_len))
2893 {
2894 s->psk_client_callback = cb;
2895 }
2896
2897void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
2898 unsigned int (*cb)(SSL *ssl, const char *hint,
2899 char *identity, unsigned int max_identity_len, unsigned char *psk,
2900 unsigned int max_psk_len))
2901 {
2902 ctx->psk_client_callback = cb;
2903 }
2904
2905void SSL_set_psk_server_callback(SSL *s,
2906 unsigned int (*cb)(SSL *ssl, const char *identity,
2907 unsigned char *psk, unsigned int max_psk_len))
2908 {
2909 s->psk_server_callback = cb;
2910 }
2911
2912void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
2913 unsigned int (*cb)(SSL *ssl, const char *identity,
2914 unsigned char *psk, unsigned int max_psk_len))
2915 {
2916 ctx->psk_server_callback = cb;
2917 }
Adam Langley95c29f32014-06-20 12:00:00 -07002918
2919void SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
2920 {
2921 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2922 }
2923void SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
2924 {
2925 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2926 }
2927
David Benjamin859ec3c2014-09-02 16:29:36 -04002928void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio)
2929 {
2930 if (ctx->keylog_bio != NULL)
2931 BIO_free(ctx->keylog_bio);
2932 ctx->keylog_bio = keylog_bio;
2933 }
2934
2935static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len)
2936 {
2937 static const char hextable[] = "0123456789abcdef";
2938 uint8_t *out;
2939 size_t i;
2940
2941 if (!CBB_add_space(cbb, &out, in_len * 2))
2942 {
2943 return 0;
2944 }
2945
2946 for (i = 0; i < in_len; i++)
2947 {
2948 *(out++) = (uint8_t)hextable[in[i] >> 4];
2949 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2950 }
2951 return 1;
2952 }
2953
2954int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
2955 const uint8_t *encrypted_premaster, size_t encrypted_premaster_len,
2956 const uint8_t *premaster, size_t premaster_len)
2957 {
2958 BIO *bio = ctx->keylog_bio;
2959 CBB cbb;
2960 uint8_t *out;
2961 size_t out_len;
2962 int ret;
2963
2964 if (bio == NULL)
2965 {
2966 return 1;
2967 }
2968
2969 if (encrypted_premaster_len < 8)
2970 {
2971 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange, ERR_R_INTERNAL_ERROR);
2972 return 0;
2973 }
2974
2975 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len*2 + 1))
2976 {
2977 return 0;
2978 }
2979 if (!CBB_add_bytes(&cbb, (const uint8_t*)"RSA ", 4) ||
2980 /* Only the first 8 bytes of the encrypted premaster secret are
2981 * logged. */
2982 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
2983 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
2984 !cbb_add_hex(&cbb, premaster, premaster_len) ||
2985 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
2986 !CBB_finish(&cbb, &out, &out_len))
2987 {
2988 CBB_cleanup(&cbb);
2989 return 0;
2990 }
2991
2992 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
2993 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
2994 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
2995
2996 OPENSSL_free(out);
2997 return ret;
2998 }
2999
3000int ssl_ctx_log_master_secret(SSL_CTX *ctx,
3001 const uint8_t *client_random, size_t client_random_len,
3002 const uint8_t *master, size_t master_len)
3003 {
3004 BIO *bio = ctx->keylog_bio;
3005 CBB cbb;
3006 uint8_t *out;
3007 size_t out_len;
3008 int ret;
3009
3010 if (bio == NULL)
3011 {
3012 return 1;
3013 }
3014
3015 if (client_random_len != 32)
3016 {
3017 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
3018 return 0;
3019 }
3020
3021 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len*2 + 1))
3022 {
3023 return 0;
3024 }
3025 if (!CBB_add_bytes(&cbb, (const uint8_t*)"CLIENT_RANDOM ", 14) ||
3026 !cbb_add_hex(&cbb, client_random, 32) ||
3027 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3028 !cbb_add_hex(&cbb, master, master_len) ||
3029 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3030 !CBB_finish(&cbb, &out, &out_len))
3031 {
3032 CBB_cleanup(&cbb);
3033 return 0;
3034 }
3035
3036 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3037 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3038 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3039
3040 OPENSSL_free(out);
3041 return ret;
3042 }
3043
Adam Langleyadb739e2014-06-20 12:00:00 -07003044int SSL_cutthrough_complete(const SSL *s)
3045 {
3046 return (!s->server && /* cutthrough only applies to clients */
3047 !s->hit && /* full-handshake */
3048 s->version >= SSL3_VERSION &&
3049 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3050 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3051 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3052 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3053 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
David Benjamin7e3305e2014-07-28 14:52:32 -04003054 s->state == SSL3_ST_CR_CHANGE ||
Adam Langleyadb739e2014-06-20 12:00:00 -07003055 s->state == SSL3_ST_CR_FINISHED_A));
3056 }
3057
Adam Langley95f22882014-06-20 12:00:00 -07003058void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3059 size_t* ssl_session_size)
3060{
3061 *ssl_size = sizeof(SSL);
3062 *ssl_ctx_size = sizeof(SSL_CTX);
3063 *ssl_session_size = sizeof(SSL_SESSION);
3064}
3065
Adam Langleyadb739e2014-06-20 12:00:00 -07003066int ssl3_can_cutthrough(const SSL *s)
3067 {
3068 const SSL_CIPHER *c;
3069
3070 /* require a strong enough cipher */
3071 if (SSL_get_cipher_bits(s, NULL) < 128)
3072 return 0;
3073
3074 /* require ALPN or NPN extension */
David Benjamin457112e2014-08-27 14:35:09 -04003075 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen)
Adam Langleyadb739e2014-06-20 12:00:00 -07003076 {
3077 return 0;
3078 }
3079
3080 /* require a forward-secret cipher */
3081 c = SSL_get_current_cipher(s);
3082 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3083 c->algorithm_mkey != SSL_kEECDH))
3084 {
3085 return 0;
3086 }
3087
3088 return 1;
3089 }
3090
Adam Langley29707792014-06-20 12:00:00 -07003091/* ssl_get_max_version returns the maximum SSL/TLS version number supported by
3092 * |s|, or zero if all versions are disabled. */
3093int ssl_get_max_version(const SSL *s)
3094 {
3095 /* Only one version supported for DTLS. */
3096 if (s->version == DTLS1_VERSION)
3097 return DTLS1_VERSION;
3098
3099 if (!(s->options & SSL_OP_NO_TLSv1_2))
3100 return TLS1_2_VERSION;
3101 if (!(s->options & SSL_OP_NO_TLSv1_1))
3102 return TLS1_1_VERSION;
3103 if (!(s->options & SSL_OP_NO_TLSv1))
3104 return TLS1_VERSION;
3105 if (!(s->options & SSL_OP_NO_SSLv3))
3106 return SSL3_VERSION;
Adam Langley29707792014-06-20 12:00:00 -07003107 return 0;
3108 }
3109
Adam Langley95c29f32014-06-20 12:00:00 -07003110/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3111 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3112 * any. If EVP_MD pointer is passed, initializes ctx with this md
3113 * Returns newly allocated ctx;
3114 */
3115
3116EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3117{
3118 ssl_clear_hash_ctx(hash);
3119 *hash = EVP_MD_CTX_create();
David Benjaminae3e4872014-11-18 21:52:26 -05003120 if (md != NULL && *hash != NULL &&
3121 !EVP_DigestInit_ex(*hash, md, NULL))
Adam Langley69a01602014-11-17 17:26:55 -08003122 {
David Benjaminae3e4872014-11-18 21:52:26 -05003123 EVP_MD_CTX_destroy(*hash);
3124 *hash = NULL;
Adam Langley69a01602014-11-17 17:26:55 -08003125 }
Adam Langley95c29f32014-06-20 12:00:00 -07003126 return *hash;
3127}
3128void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3129{
3130
3131 if (*hash) EVP_MD_CTX_destroy(*hash);
3132 *hash=NULL;
3133}
3134
3135void SSL_set_debug(SSL *s, int debug)
3136 {
3137 s->debug = debug;
3138 }
3139
3140int SSL_cache_hit(SSL *s)
3141 {
3142 return s->hit;
3143 }
3144
3145int SSL_is_server(SSL *s)
3146 {
3147 return s->server;
3148 }