blob: 5bf24385759526e1232d2dacbc11b3980fd8e242 [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
175int SSL_clear(SSL *s)
176 {
177
178 if (s->method == NULL)
179 {
180 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
181 return(0);
182 }
183
184 if (ssl_clear_bad_session(s))
185 {
186 SSL_SESSION_free(s->session);
187 s->session=NULL;
188 }
189
190 s->error=0;
191 s->hit=0;
192 s->shutdown=0;
193
194#if 0 /* Disabled since version 1.10 of this file (early return not
195 * needed because SSL_clear is not called when doing renegotiation) */
196 /* This is set if we are doing dynamic renegotiation so keep
197 * the old cipher. It is sort of a SSL_clear_lite :-) */
198 if (s->renegotiate) return(1);
199#else
200 if (s->renegotiate)
201 {
202 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
203 return 0;
204 }
205#endif
206
207 s->type=0;
208
209 s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
210
211 s->version=s->method->version;
212 s->client_version=s->version;
213 s->rwstate=SSL_NOTHING;
214 s->rstate=SSL_ST_READ_HEADER;
215#if 0
216 s->read_ahead=s->ctx->read_ahead;
217#endif
218
219 if (s->init_buf != NULL)
220 {
221 BUF_MEM_free(s->init_buf);
222 s->init_buf=NULL;
223 }
224
225 ssl_clear_cipher_ctx(s);
226 ssl_clear_hash_ctx(&s->read_hash);
227 ssl_clear_hash_ctx(&s->write_hash);
228
229 s->first_packet=0;
230
231#if 1
232 /* Check to see if we were changed into a different method, if
233 * so, revert back if we are not doing session-id reuse. */
234 if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method))
235 {
236 s->method->ssl_free(s);
237 s->method=s->ctx->method;
238 if (!s->method->ssl_new(s))
239 return(0);
240 }
241 else
242#endif
243 s->method->ssl_clear(s);
244 return(1);
245 }
246
247/** Used to change an SSL_CTXs default SSL method type */
248int SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth)
249 {
250 STACK_OF(SSL_CIPHER) *sk;
251
252 ctx->method=meth;
253
Adam Langley858a88d2014-06-20 12:00:00 -0700254 sk=ssl_create_cipher_list(
255 ctx->method, &ctx->cipher_list, &ctx->cipher_list_by_id,
256 meth->version == SSL2_VERSION ?
257 "SSLv2" :
258 SSL_DEFAULT_CIPHER_LIST,
259 ctx->cert);
260
Adam Langley95c29f32014-06-20 12:00:00 -0700261 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
262 {
263 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_ssl_version, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
264 return(0);
265 }
266 return(1);
267 }
268
269SSL *SSL_new(SSL_CTX *ctx)
270 {
271 SSL *s;
272
273 if (ctx == NULL)
274 {
275 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
276 return(NULL);
277 }
278 if (ctx->method == NULL)
279 {
280 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
281 return(NULL);
282 }
283
284 s=(SSL *)OPENSSL_malloc(sizeof(SSL));
285 if (s == NULL) goto err;
286 memset(s,0,sizeof(SSL));
287
288 s->options=ctx->options;
289 s->mode=ctx->mode;
290 s->max_cert_list=ctx->max_cert_list;
291
292 if (ctx->cert != NULL)
293 {
294 /* Earlier library versions used to copy the pointer to
295 * the CERT, not its contents; only when setting new
296 * parameters for the per-SSL copy, ssl_cert_new would be
297 * called (and the direct reference to the per-SSL_CTX
298 * settings would be lost, but those still were indirectly
299 * accessed for various purposes, and for that reason they
300 * used to be known as s->ctx->default_cert).
301 * Now we don't look at the SSL_CTX's CERT after having
302 * duplicated it once. */
303
304 s->cert = ssl_cert_dup(ctx->cert);
305 if (s->cert == NULL)
306 goto err;
307 }
308 else
309 s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
310
311 s->read_ahead=ctx->read_ahead;
312 s->msg_callback=ctx->msg_callback;
313 s->msg_callback_arg=ctx->msg_callback_arg;
314 s->verify_mode=ctx->verify_mode;
315#if 0
316 s->verify_depth=ctx->verify_depth;
317#endif
318 s->sid_ctx_length=ctx->sid_ctx_length;
319 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
320 memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
321 s->verify_callback=ctx->default_verify_callback;
322 s->generate_session_id=ctx->generate_session_id;
323
324 s->param = X509_VERIFY_PARAM_new();
325 if (!s->param)
326 goto err;
327 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
328#if 0
329 s->purpose = ctx->purpose;
330 s->trust = ctx->trust;
331#endif
332 s->quiet_shutdown=ctx->quiet_shutdown;
333 s->max_send_fragment = ctx->max_send_fragment;
334
335 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
336 s->ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700337 s->tlsext_debug_cb = 0;
338 s->tlsext_debug_arg = NULL;
339 s->tlsext_ticket_expected = 0;
340 s->tlsext_status_type = -1;
341 s->tlsext_status_expected = 0;
342 s->tlsext_ocsp_ids = NULL;
343 s->tlsext_ocsp_exts = NULL;
344 s->tlsext_ocsp_resp = NULL;
345 s->tlsext_ocsp_resplen = -1;
346 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
347 s->initial_ctx=ctx;
348#ifndef OPENSSL_NO_EC
349 if (ctx->tlsext_ecpointformatlist)
350 {
351 s->tlsext_ecpointformatlist =
352 BUF_memdup(ctx->tlsext_ecpointformatlist,
353 ctx->tlsext_ecpointformatlist_length);
354 if (!s->tlsext_ecpointformatlist)
355 goto err;
356 s->tlsext_ecpointformatlist_length =
357 ctx->tlsext_ecpointformatlist_length;
358 }
359 if (ctx->tlsext_ellipticcurvelist)
360 {
361 s->tlsext_ellipticcurvelist =
362 BUF_memdup(ctx->tlsext_ellipticcurvelist,
David Benjamin072334d2014-07-13 16:24:27 -0400363 ctx->tlsext_ellipticcurvelist_length * 2);
Adam Langley95c29f32014-06-20 12:00:00 -0700364 if (!s->tlsext_ellipticcurvelist)
365 goto err;
366 s->tlsext_ellipticcurvelist_length =
367 ctx->tlsext_ellipticcurvelist_length;
368 }
369#endif
370# ifndef OPENSSL_NO_NEXTPROTONEG
371 s->next_proto_negotiated = NULL;
372# endif
373
374 if (s->ctx->alpn_client_proto_list)
375 {
David Benjamin072c9532014-07-26 11:44:25 -0400376 s->alpn_client_proto_list = BUF_memdup(
377 s->ctx->alpn_client_proto_list,
378 s->ctx->alpn_client_proto_list_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700379 if (s->alpn_client_proto_list == NULL)
380 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700381 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
382 }
Adam Langley95c29f32014-06-20 12:00:00 -0700383
384 s->verify_result=X509_V_OK;
385
386 s->method=ctx->method;
387
388 if (!s->method->ssl_new(s))
389 goto err;
390
391 s->references=1;
392 s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
393
394 SSL_clear(s);
395
396 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
397
Adam Langley0289c732014-06-20 12:00:00 -0700398 s->psk_identity_hint = NULL;
399 if (ctx->psk_identity_hint)
400 {
401 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
402 if (s->psk_identity_hint == NULL)
403 goto err;
404 }
Adam Langley95c29f32014-06-20 12:00:00 -0700405 s->psk_client_callback=ctx->psk_client_callback;
406 s->psk_server_callback=ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700407
408 return(s);
409err:
410 if (s != NULL)
411 {
412 if (s->cert != NULL)
413 ssl_cert_free(s->cert);
414 if (s->ctx != NULL)
415 SSL_CTX_free(s->ctx); /* decrement reference count */
416 OPENSSL_free(s);
417 }
418 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
419 return(NULL);
420 }
421
422int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
423 unsigned int sid_ctx_len)
424 {
425 if(sid_ctx_len > sizeof ctx->sid_ctx)
426 {
427 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
428 return 0;
429 }
430 ctx->sid_ctx_length=sid_ctx_len;
431 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
432
433 return 1;
434 }
435
436int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
437 unsigned int sid_ctx_len)
438 {
439 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
440 {
441 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
442 return 0;
443 }
444 ssl->sid_ctx_length=sid_ctx_len;
445 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
446
447 return 1;
448 }
449
450int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
451 {
452 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
453 ctx->generate_session_id = cb;
454 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
455 return 1;
456 }
457
458int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
459 {
460 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
461 ssl->generate_session_id = cb;
462 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
463 return 1;
464 }
465
466int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
467 unsigned int id_len)
468 {
469 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
470 * we can "construct" a session to give us the desired check - ie. to
471 * find if there's a session in the hash table that would conflict with
472 * any new session built out of this id/id_len and the ssl_version in
473 * use by this SSL. */
474 SSL_SESSION r, *p;
475
476 if(id_len > sizeof r.session_id)
477 return 0;
478
479 r.ssl_version = ssl->version;
480 r.session_id_length = id_len;
481 memcpy(r.session_id, id, id_len);
482 /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
483 * callback is calling us to check the uniqueness of a shorter ID, it
484 * must be compared as a padded-out ID because that is what it will be
485 * converted to when the callback has finished choosing it. */
486 if((r.ssl_version == SSL2_VERSION) &&
487 (id_len < SSL2_SSL_SESSION_ID_LENGTH))
488 {
489 memset(r.session_id + id_len, 0,
490 SSL2_SSL_SESSION_ID_LENGTH - id_len);
491 r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
492 }
493
494 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
495 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
496 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
497 return (p != NULL);
498 }
499
500int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
501 {
502 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
503 }
504
505int SSL_set_purpose(SSL *s, int purpose)
506 {
507 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
508 }
509
510int SSL_CTX_set_trust(SSL_CTX *s, int trust)
511 {
512 return X509_VERIFY_PARAM_set_trust(s->param, trust);
513 }
514
515int SSL_set_trust(SSL *s, int trust)
516 {
517 return X509_VERIFY_PARAM_set_trust(s->param, trust);
518 }
519
520int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
521 {
522 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
523 }
524
525int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
526 {
527 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
528 }
529
Adam Langley858a88d2014-06-20 12:00:00 -0700530void ssl_cipher_preference_list_free(
531 struct ssl_cipher_preference_list_st *cipher_list)
532 {
533 sk_SSL_CIPHER_free(cipher_list->ciphers);
534 OPENSSL_free(cipher_list->in_group_flags);
535 OPENSSL_free(cipher_list);
536 }
537
538struct ssl_cipher_preference_list_st*
539ssl_cipher_preference_list_dup(
540 struct ssl_cipher_preference_list_st *cipher_list)
541 {
542 struct ssl_cipher_preference_list_st* ret = NULL;
543 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
544
545 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
546 if (!ret)
547 goto err;
548 ret->ciphers = NULL;
549 ret->in_group_flags = NULL;
550 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
551 if (!ret->ciphers)
552 goto err;
David Benjamin072c9532014-07-26 11:44:25 -0400553 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
Adam Langley858a88d2014-06-20 12:00:00 -0700554 if (!ret->in_group_flags)
555 goto err;
Adam Langley858a88d2014-06-20 12:00:00 -0700556 return ret;
557
558err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400559 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700560 sk_SSL_CIPHER_free(ret->ciphers);
561 if (ret)
562 OPENSSL_free(ret);
563 return NULL;
564 }
565
566struct ssl_cipher_preference_list_st*
567ssl_cipher_preference_list_from_ciphers(STACK_OF(SSL_CIPHER) *ciphers)
568 {
569 struct ssl_cipher_preference_list_st* ret = NULL;
570 size_t n = sk_SSL_CIPHER_num(ciphers);
571
572 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
573 if (!ret)
574 goto err;
575 ret->ciphers = NULL;
576 ret->in_group_flags = NULL;
577 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
578 if (!ret->ciphers)
579 goto err;
580 ret->in_group_flags = OPENSSL_malloc(n);
581 if (!ret->in_group_flags)
582 goto err;
583 memset(ret->in_group_flags, 0, n);
584 return ret;
585
586err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400587 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700588 sk_SSL_CIPHER_free(ret->ciphers);
589 if (ret)
590 OPENSSL_free(ret);
591 return NULL;
592 }
593
Adam Langley95c29f32014-06-20 12:00:00 -0700594X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
595 {
596 return ctx->param;
597 }
598
599X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
600 {
601 return ssl->param;
602 }
603
604void SSL_certs_clear(SSL *s)
605 {
606 ssl_cert_clear_certs(s->cert);
607 }
608
609void SSL_free(SSL *s)
610 {
611 int i;
612
613 if(s == NULL)
614 return;
615
616 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
617#ifdef REF_PRINT
618 REF_PRINT("SSL",s);
619#endif
620 if (i > 0) return;
621#ifdef REF_CHECK
622 if (i < 0)
623 {
624 fprintf(stderr,"SSL_free, bad reference count\n");
625 abort(); /* ok */
626 }
627#endif
628
629 if (s->param)
630 X509_VERIFY_PARAM_free(s->param);
631
632 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
633
634 if (s->bbio != NULL)
635 {
636 /* If the buffering BIO is in place, pop it off */
637 if (s->bbio == s->wbio)
638 {
639 s->wbio=BIO_pop(s->wbio);
640 }
641 BIO_free(s->bbio);
642 s->bbio=NULL;
643 }
644 if (s->rbio != NULL)
645 BIO_free_all(s->rbio);
646 if ((s->wbio != NULL) && (s->wbio != s->rbio))
647 BIO_free_all(s->wbio);
648
649 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
650
651 /* add extra stuff */
Adam Langley858a88d2014-06-20 12:00:00 -0700652 if (s->cipher_list != NULL)
653 ssl_cipher_preference_list_free(s->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -0700654 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
655
656 /* Make the next call work :-) */
657 if (s->session != NULL)
658 {
659 ssl_clear_bad_session(s);
660 SSL_SESSION_free(s->session);
661 }
662
663 ssl_clear_cipher_ctx(s);
664 ssl_clear_hash_ctx(&s->read_hash);
665 ssl_clear_hash_ctx(&s->write_hash);
666
667 if (s->cert != NULL) ssl_cert_free(s->cert);
668 /* Free up if allocated */
669
Adam Langley95c29f32014-06-20 12:00:00 -0700670 if (s->tlsext_hostname)
671 OPENSSL_free(s->tlsext_hostname);
672 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
673#ifndef OPENSSL_NO_EC
674 if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
675 if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
676#endif /* OPENSSL_NO_EC */
Adam Langley95c29f32014-06-20 12:00:00 -0700677 if (s->tlsext_ocsp_exts)
678 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
679 X509_EXTENSION_free);
680 /* TODO(fork): OCSP support */
681#if 0
682 if (s->tlsext_ocsp_ids)
683 sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);
684#endif
685 if (s->tlsext_ocsp_resp)
686 OPENSSL_free(s->tlsext_ocsp_resp);
687 if (s->alpn_client_proto_list)
688 OPENSSL_free(s->alpn_client_proto_list);
Adam Langley1258b6a2014-06-20 12:00:00 -0700689 if (s->tlsext_channel_id_private)
690 EVP_PKEY_free(s->tlsext_channel_id_private);
Adam Langley95c29f32014-06-20 12:00:00 -0700691
Adam Langley0289c732014-06-20 12:00:00 -0700692 if (s->psk_identity_hint)
693 OPENSSL_free(s->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -0700694
Adam Langley95c29f32014-06-20 12:00:00 -0700695 if (s->client_CA != NULL)
696 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
697
698 if (s->method != NULL) s->method->ssl_free(s);
699
700 if (s->ctx) SSL_CTX_free(s->ctx);
701
David Benjamin6dbd73d2014-07-03 15:59:49 -0400702#if !defined(OPENSSL_NO_NEXTPROTONEG)
Adam Langley95c29f32014-06-20 12:00:00 -0700703 if (s->next_proto_negotiated)
704 OPENSSL_free(s->next_proto_negotiated);
705#endif
706
707 if (s->srtp_profiles)
708 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
709
David Benjamin1b965262014-08-13 20:06:29 -0400710 if (s->tlsext_session_ticket)
711 {
712 OPENSSL_free(s->tlsext_session_ticket);
713 }
714
Adam Langley95c29f32014-06-20 12:00:00 -0700715 OPENSSL_free(s);
716 }
717
718void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
719 {
720 /* If the output buffering BIO is still in place, remove it
721 */
722 if (s->bbio != NULL)
723 {
724 if (s->wbio == s->bbio)
725 {
726 s->wbio=s->wbio->next_bio;
727 s->bbio->next_bio=NULL;
728 }
729 }
730 if ((s->rbio != NULL) && (s->rbio != rbio))
731 BIO_free_all(s->rbio);
732 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
733 BIO_free_all(s->wbio);
734 s->rbio=rbio;
735 s->wbio=wbio;
736 }
737
738BIO *SSL_get_rbio(const SSL *s)
739 { return(s->rbio); }
740
741BIO *SSL_get_wbio(const SSL *s)
742 { return(s->wbio); }
743
744int SSL_get_fd(const SSL *s)
745 {
746 return(SSL_get_rfd(s));
747 }
748
749int SSL_get_rfd(const SSL *s)
750 {
751 int ret= -1;
752 BIO *b,*r;
753
754 b=SSL_get_rbio(s);
755 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
756 if (r != NULL)
757 BIO_get_fd(r,&ret);
758 return(ret);
759 }
760
761int SSL_get_wfd(const SSL *s)
762 {
763 int ret= -1;
764 BIO *b,*r;
765
766 b=SSL_get_wbio(s);
767 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
768 if (r != NULL)
769 BIO_get_fd(r,&ret);
770 return(ret);
771 }
772
773#ifndef OPENSSL_NO_SOCK
774int SSL_set_fd(SSL *s,int fd)
775 {
776 int ret=0;
777 BIO *bio=NULL;
778
779 bio=BIO_new(BIO_s_fd());
780
781 if (bio == NULL)
782 {
783 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
784 goto err;
785 }
786 BIO_set_fd(bio,fd,BIO_NOCLOSE);
787 SSL_set_bio(s,bio,bio);
788 ret=1;
789err:
790 return(ret);
791 }
792
793int SSL_set_wfd(SSL *s,int fd)
794 {
795 int ret=0;
796 BIO *bio=NULL;
797
798 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_FD)
799 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
800 {
801 bio=BIO_new(BIO_s_fd());
802
803 if (bio == NULL)
804 {
805 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
806 goto err;
807 }
808 BIO_set_fd(bio,fd,BIO_NOCLOSE);
809 SSL_set_bio(s,SSL_get_rbio(s),bio);
810 }
811 else
812 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
813 ret=1;
814err:
815 return(ret);
816 }
817
818int SSL_set_rfd(SSL *s,int fd)
819 {
820 int ret=0;
821 BIO *bio=NULL;
822
823 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_FD)
824 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
825 {
826 bio=BIO_new(BIO_s_fd());
827
828 if (bio == NULL)
829 {
830 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
831 goto err;
832 }
833 BIO_set_fd(bio,fd,BIO_NOCLOSE);
834 SSL_set_bio(s,bio,SSL_get_wbio(s));
835 }
836 else
837 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
838 ret=1;
839err:
840 return(ret);
841 }
842#endif
843
844
845/* return length of latest Finished message we sent, copy to 'buf' */
846size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
847 {
848 size_t ret = 0;
849
850 if (s->s3 != NULL)
851 {
852 ret = s->s3->tmp.finish_md_len;
853 if (count > ret)
854 count = ret;
855 memcpy(buf, s->s3->tmp.finish_md, count);
856 }
857 return ret;
858 }
859
860/* return length of latest Finished message we expected, copy to 'buf' */
861size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
862 {
863 size_t ret = 0;
864
865 if (s->s3 != NULL)
866 {
867 ret = s->s3->tmp.peer_finish_md_len;
868 if (count > ret)
869 count = ret;
870 memcpy(buf, s->s3->tmp.peer_finish_md, count);
871 }
872 return ret;
873 }
874
875
876int SSL_get_verify_mode(const SSL *s)
877 {
878 return(s->verify_mode);
879 }
880
881int SSL_get_verify_depth(const SSL *s)
882 {
883 return X509_VERIFY_PARAM_get_depth(s->param);
884 }
885
886int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
887 {
888 return(s->verify_callback);
889 }
890
891int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
892 {
893 return(ctx->verify_mode);
894 }
895
896int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
897 {
898 return X509_VERIFY_PARAM_get_depth(ctx->param);
899 }
900
901int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
902 {
903 return(ctx->default_verify_callback);
904 }
905
906void SSL_set_verify(SSL *s,int mode,
907 int (*callback)(int ok,X509_STORE_CTX *ctx))
908 {
909 s->verify_mode=mode;
910 if (callback != NULL)
911 s->verify_callback=callback;
912 }
913
914void SSL_set_verify_depth(SSL *s,int depth)
915 {
916 X509_VERIFY_PARAM_set_depth(s->param, depth);
917 }
918
919void SSL_set_read_ahead(SSL *s,int yes)
920 {
921 s->read_ahead=yes;
922 }
923
924int SSL_get_read_ahead(const SSL *s)
925 {
926 return(s->read_ahead);
927 }
928
929int SSL_pending(const SSL *s)
930 {
931 /* SSL_pending cannot work properly if read-ahead is enabled
932 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
933 * and it is impossible to fix since SSL_pending cannot report
934 * errors that may be observed while scanning the new data.
935 * (Note that SSL_pending() is often used as a boolean value,
936 * so we'd better not return -1.)
937 */
938 return(s->method->ssl_pending(s));
939 }
940
941X509 *SSL_get_peer_certificate(const SSL *s)
942 {
943 X509 *r;
944
945 if ((s == NULL) || (s->session == NULL))
946 r=NULL;
947 else
948 r=s->session->peer;
949
David Benjamin150c6172014-08-05 22:22:49 -0400950 if (r == NULL)
951 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700952
David Benjamin150c6172014-08-05 22:22:49 -0400953 return X509_up_ref(r);
Adam Langley95c29f32014-06-20 12:00:00 -0700954 }
955
956STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
957 {
958 STACK_OF(X509) *r;
959
960 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
961 r=NULL;
962 else
963 r=s->session->sess_cert->cert_chain;
964
965 /* If we are a client, cert_chain includes the peer's own
966 * certificate; if we are a server, it does not. */
967
968 return(r);
969 }
970
Adam Langley95c29f32014-06-20 12:00:00 -0700971/* Fix this so it checks all the valid key/cert options */
972int SSL_CTX_check_private_key(const SSL_CTX *ctx)
973 {
974 if ( (ctx == NULL) ||
975 (ctx->cert == NULL) ||
976 (ctx->cert->key->x509 == NULL))
977 {
978 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
979 return(0);
980 }
981 if (ctx->cert->key->privatekey == NULL)
982 {
983 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
984 return(0);
985 }
986 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
987 }
988
989/* Fix this function so that it takes an optional type parameter */
990int SSL_check_private_key(const SSL *ssl)
991 {
992 if (ssl == NULL)
993 {
994 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
995 return(0);
996 }
997 if (ssl->cert == NULL)
998 {
999 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
1000 return 0;
1001 }
1002 if (ssl->cert->key->x509 == NULL)
1003 {
1004 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
1005 return(0);
1006 }
1007 if (ssl->cert->key->privatekey == NULL)
1008 {
1009 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
1010 return(0);
1011 }
1012 return(X509_check_private_key(ssl->cert->key->x509,
1013 ssl->cert->key->privatekey));
1014 }
1015
1016int SSL_accept(SSL *s)
1017 {
1018 if (s->handshake_func == 0)
1019 /* Not properly initialized yet */
1020 SSL_set_accept_state(s);
1021
1022 return(s->method->ssl_accept(s));
1023 }
1024
1025int SSL_connect(SSL *s)
1026 {
1027 if (s->handshake_func == 0)
1028 /* Not properly initialized yet */
1029 SSL_set_connect_state(s);
1030
1031 return(s->method->ssl_connect(s));
1032 }
1033
1034long SSL_get_default_timeout(const SSL *s)
1035 {
David Benjaminf4501342014-08-14 17:24:37 -04001036 return SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001037 }
1038
1039int SSL_read(SSL *s,void *buf,int num)
1040 {
1041 if (s->handshake_func == 0)
1042 {
1043 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
1044 return -1;
1045 }
1046
1047 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1048 {
1049 s->rwstate=SSL_NOTHING;
1050 return(0);
1051 }
1052 return(s->method->ssl_read(s,buf,num));
1053 }
1054
1055int SSL_peek(SSL *s,void *buf,int num)
1056 {
1057 if (s->handshake_func == 0)
1058 {
1059 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
1060 return -1;
1061 }
1062
1063 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1064 {
1065 return(0);
1066 }
1067 return(s->method->ssl_peek(s,buf,num));
1068 }
1069
1070int SSL_write(SSL *s,const void *buf,int num)
1071 {
1072 if (s->handshake_func == 0)
1073 {
1074 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
1075 return -1;
1076 }
1077
1078 if (s->shutdown & SSL_SENT_SHUTDOWN)
1079 {
1080 s->rwstate=SSL_NOTHING;
1081 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
1082 return(-1);
1083 }
1084 return(s->method->ssl_write(s,buf,num));
1085 }
1086
1087int SSL_shutdown(SSL *s)
1088 {
1089 /* Note that this function behaves differently from what one might
1090 * expect. Return values are 0 for no success (yet),
1091 * 1 for success; but calling it once is usually not enough,
1092 * even if blocking I/O is used (see ssl3_shutdown).
1093 */
1094
1095 if (s->handshake_func == 0)
1096 {
1097 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1098 return -1;
1099 }
1100
1101 if ((s != NULL) && !SSL_in_init(s))
1102 return(s->method->ssl_shutdown(s));
1103 else
1104 return(1);
1105 }
1106
1107int SSL_renegotiate(SSL *s)
1108 {
1109 if (s->renegotiate == 0)
1110 s->renegotiate=1;
1111
1112 s->new_session=1;
1113
1114 return(s->method->ssl_renegotiate(s));
1115 }
1116
1117int SSL_renegotiate_abbreviated(SSL *s)
1118 {
1119 if (s->renegotiate == 0)
1120 s->renegotiate=1;
1121
1122 s->new_session=0;
1123
1124 return(s->method->ssl_renegotiate(s));
1125 }
1126
1127int SSL_renegotiate_pending(SSL *s)
1128 {
1129 /* becomes true when negotiation is requested;
1130 * false again once a handshake has finished */
1131 return (s->renegotiate != 0);
1132 }
1133
1134long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1135 {
1136 long l;
1137
1138 switch (cmd)
1139 {
1140 case SSL_CTRL_GET_READ_AHEAD:
1141 return(s->read_ahead);
1142 case SSL_CTRL_SET_READ_AHEAD:
1143 l=s->read_ahead;
1144 s->read_ahead=larg;
1145 return(l);
1146
1147 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1148 s->msg_callback_arg = parg;
1149 return 1;
1150
1151 case SSL_CTRL_OPTIONS:
1152 return(s->options|=larg);
1153 case SSL_CTRL_CLEAR_OPTIONS:
1154 return(s->options&=~larg);
1155 case SSL_CTRL_MODE:
1156 return(s->mode|=larg);
1157 case SSL_CTRL_CLEAR_MODE:
1158 return(s->mode &=~larg);
1159 case SSL_CTRL_GET_MAX_CERT_LIST:
1160 return(s->max_cert_list);
1161 case SSL_CTRL_SET_MAX_CERT_LIST:
1162 l=s->max_cert_list;
1163 s->max_cert_list=larg;
1164 return(l);
1165 case SSL_CTRL_SET_MTU:
1166#ifndef OPENSSL_NO_DTLS1
1167 if (larg < (long)dtls1_min_mtu())
1168 return 0;
1169#endif
1170
1171 if (SSL_IS_DTLS(s))
1172 {
1173 s->d1->mtu = larg;
1174 return larg;
1175 }
1176 return 0;
1177 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1178 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1179 return 0;
1180 s->max_send_fragment = larg;
1181 return 1;
1182 case SSL_CTRL_GET_RI_SUPPORT:
1183 if (s->s3)
1184 return s->s3->send_connection_binding;
1185 else return 0;
1186 case SSL_CTRL_CERT_FLAGS:
1187 return(s->cert->cert_flags|=larg);
1188 case SSL_CTRL_CLEAR_CERT_FLAGS:
1189 return(s->cert->cert_flags &=~larg);
1190
1191 case SSL_CTRL_GET_RAW_CIPHERLIST:
1192 if (parg)
1193 {
1194 if (s->cert->ciphers_raw == NULL)
1195 return 0;
1196 *(unsigned char **)parg = s->cert->ciphers_raw;
1197 return (int)s->cert->ciphers_rawlen;
1198 }
1199 else
David Benjamin39482a12014-07-20 13:30:15 -04001200 {
1201 /* Passing a NULL |parg| returns the size of a single
1202 * cipher suite value. */
1203 return 2;
1204 }
Adam Langley95c29f32014-06-20 12:00:00 -07001205 default:
1206 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1207 }
1208 }
1209
1210long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1211 {
1212 switch(cmd)
1213 {
1214 case SSL_CTRL_SET_MSG_CALLBACK:
1215 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1216 return 1;
1217
1218 default:
1219 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1220 }
1221 }
1222
1223LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1224 {
1225 return ctx->sessions;
1226 }
1227
1228long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1229 {
1230 long l;
1231
1232 switch (cmd)
1233 {
1234 case SSL_CTRL_GET_READ_AHEAD:
1235 return(ctx->read_ahead);
1236 case SSL_CTRL_SET_READ_AHEAD:
1237 l=ctx->read_ahead;
1238 ctx->read_ahead=larg;
1239 return(l);
1240
1241 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1242 ctx->msg_callback_arg = parg;
1243 return 1;
1244
1245 case SSL_CTRL_GET_MAX_CERT_LIST:
1246 return(ctx->max_cert_list);
1247 case SSL_CTRL_SET_MAX_CERT_LIST:
1248 l=ctx->max_cert_list;
1249 ctx->max_cert_list=larg;
1250 return(l);
1251
1252 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1253 l=ctx->session_cache_size;
1254 ctx->session_cache_size=larg;
1255 return(l);
1256 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1257 return(ctx->session_cache_size);
1258 case SSL_CTRL_SET_SESS_CACHE_MODE:
1259 l=ctx->session_cache_mode;
1260 ctx->session_cache_mode=larg;
1261 return(l);
1262 case SSL_CTRL_GET_SESS_CACHE_MODE:
1263 return(ctx->session_cache_mode);
1264
1265 case SSL_CTRL_SESS_NUMBER:
1266 return(lh_SSL_SESSION_num_items(ctx->sessions));
1267 case SSL_CTRL_SESS_CONNECT:
1268 return(ctx->stats.sess_connect);
1269 case SSL_CTRL_SESS_CONNECT_GOOD:
1270 return(ctx->stats.sess_connect_good);
1271 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1272 return(ctx->stats.sess_connect_renegotiate);
1273 case SSL_CTRL_SESS_ACCEPT:
1274 return(ctx->stats.sess_accept);
1275 case SSL_CTRL_SESS_ACCEPT_GOOD:
1276 return(ctx->stats.sess_accept_good);
1277 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1278 return(ctx->stats.sess_accept_renegotiate);
1279 case SSL_CTRL_SESS_HIT:
1280 return(ctx->stats.sess_hit);
1281 case SSL_CTRL_SESS_CB_HIT:
1282 return(ctx->stats.sess_cb_hit);
1283 case SSL_CTRL_SESS_MISSES:
1284 return(ctx->stats.sess_miss);
1285 case SSL_CTRL_SESS_TIMEOUTS:
1286 return(ctx->stats.sess_timeout);
1287 case SSL_CTRL_SESS_CACHE_FULL:
1288 return(ctx->stats.sess_cache_full);
1289 case SSL_CTRL_OPTIONS:
1290 return(ctx->options|=larg);
1291 case SSL_CTRL_CLEAR_OPTIONS:
1292 return(ctx->options&=~larg);
1293 case SSL_CTRL_MODE:
1294 return(ctx->mode|=larg);
1295 case SSL_CTRL_CLEAR_MODE:
1296 return(ctx->mode&=~larg);
1297 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1298 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1299 return 0;
1300 ctx->max_send_fragment = larg;
1301 return 1;
1302 case SSL_CTRL_CERT_FLAGS:
1303 return(ctx->cert->cert_flags|=larg);
1304 case SSL_CTRL_CLEAR_CERT_FLAGS:
1305 return(ctx->cert->cert_flags &=~larg);
1306 default:
1307 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1308 }
1309 }
1310
1311long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1312 {
1313 switch(cmd)
1314 {
1315 case SSL_CTRL_SET_MSG_CALLBACK:
1316 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1317 return 1;
1318
1319 default:
1320 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1321 }
1322 }
1323
1324int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1325 {
1326 long l;
1327 const SSL_CIPHER *a = in_a;
1328 const SSL_CIPHER *b = in_b;
1329 const long a_id = a->id;
1330 const long b_id = b->id;
1331
1332 l = a_id - b_id;
1333 if (l == 0L)
1334 return(0);
1335 else
1336 return((l > 0)?1:-1);
1337 }
1338
1339int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1340 {
1341 long l;
1342 const long a_id = (*ap)->id;
1343 const long b_id = (*bp)->id;
1344
1345 l = a_id - b_id;
1346 if (l == 0)
1347 return(0);
1348 else
1349 return((l > 0)?1:-1);
1350 }
1351
1352/** return a STACK of the ciphers available for the SSL and in order of
1353 * preference */
1354STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1355 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001356 if (s == NULL)
1357 return NULL;
1358
1359 if (s->cipher_list != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001360 {
Adam Langley858a88d2014-06-20 12:00:00 -07001361 return(s->cipher_list->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -07001362 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001363
1364 if (s->version >= TLS1_1_VERSION)
1365 {
1366 if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001367 return s->ctx->cipher_list_tls11->ciphers;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001368 }
1369
1370 if ((s->ctx != NULL) &&
1371 (s->ctx->cipher_list != NULL))
1372 {
Adam Langley858a88d2014-06-20 12:00:00 -07001373 return(s->ctx->cipher_list->ciphers);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001374 }
1375
Adam Langley95c29f32014-06-20 12:00:00 -07001376 return(NULL);
1377 }
1378
1379/** return a STACK of the ciphers available for the SSL and in order of
1380 * algorithm id */
1381STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1382 {
1383 if (s != NULL)
1384 {
1385 if (s->cipher_list_by_id != NULL)
1386 {
1387 return(s->cipher_list_by_id);
1388 }
1389 else if ((s->ctx != NULL) &&
1390 (s->ctx->cipher_list_by_id != NULL))
1391 {
1392 return(s->ctx->cipher_list_by_id);
1393 }
1394 }
1395 return(NULL);
1396 }
1397
1398/** The old interface to get the same thing as SSL_get_ciphers() */
1399const char *SSL_get_cipher_list(const SSL *s,int n)
1400 {
David Benjamin6f260012014-08-15 13:49:12 -04001401 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001402 STACK_OF(SSL_CIPHER) *sk;
1403
1404 if (s == NULL) return(NULL);
1405 sk=SSL_get_ciphers(s);
1406 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
1407 return(NULL);
1408 c=sk_SSL_CIPHER_value(sk,n);
1409 if (c == NULL) return(NULL);
1410 return(c->name);
1411 }
1412
1413/** specify the ciphers to be used by default by the SSL_CTX */
1414int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1415 {
1416 STACK_OF(SSL_CIPHER) *sk;
1417
1418 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1419 &ctx->cipher_list_by_id,str, ctx->cert);
1420 /* ssl_create_cipher_list may return an empty stack if it
1421 * was unable to find a cipher matching the given rule string
1422 * (for example if the rule string specifies a cipher which
1423 * has been disabled). This is not an error as far as
1424 * ssl_create_cipher_list is concerned, and hence
1425 * ctx->cipher_list and ctx->cipher_list_by_id has been
1426 * updated. */
1427 if (sk == NULL)
1428 return 0;
1429 else if (sk_SSL_CIPHER_num(sk) == 0)
1430 {
1431 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1432 return 0;
1433 }
1434 return 1;
1435 }
1436
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001437int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str)
1438 {
1439 STACK_OF(SSL_CIPHER) *sk;
1440
1441 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert);
1442 if (sk == NULL)
1443 return 0;
1444 else if (sk_SSL_CIPHER_num(sk) == 0)
1445 {
1446 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH);
1447 return 0;
1448 }
1449 return 1;
1450 }
1451
Adam Langley95c29f32014-06-20 12:00:00 -07001452/** specify the ciphers to be used by the SSL */
1453int SSL_set_cipher_list(SSL *s,const char *str)
1454 {
1455 STACK_OF(SSL_CIPHER) *sk;
1456
1457 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1458 &s->cipher_list_by_id,str, s->cert);
1459 /* see comment in SSL_CTX_set_cipher_list */
1460 if (sk == NULL)
1461 return 0;
1462 else if (sk_SSL_CIPHER_num(sk) == 0)
1463 {
1464 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1465 return 0;
1466 }
1467 return 1;
1468 }
1469
1470/* works well for SSLv2, not so good for SSLv3 */
1471char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
1472 {
1473 char *p;
1474 STACK_OF(SSL_CIPHER) *sk;
David Benjamin6f260012014-08-15 13:49:12 -04001475 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001476 int i;
1477
1478 if ((s->session == NULL) || (s->session->ciphers == NULL) ||
1479 (len < 2))
1480 return(NULL);
1481
1482 p=buf;
1483 sk=s->session->ciphers;
Adam Langley04124042014-06-20 12:00:00 -07001484
1485 if (sk_SSL_CIPHER_num(sk) == 0)
1486 return NULL;
1487
Adam Langley95c29f32014-06-20 12:00:00 -07001488 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1489 {
1490 int n;
1491
1492 c=sk_SSL_CIPHER_value(sk,i);
1493 n=strlen(c->name);
1494 if (n+1 > len)
1495 {
1496 if (p != buf)
1497 --p;
1498 *p='\0';
1499 return buf;
1500 }
1501 strcpy(p,c->name);
1502 p+=n;
1503 *(p++)=':';
1504 len-=n+1;
1505 }
1506 p[-1]='\0';
1507 return(buf);
1508 }
1509
David Benjamin39482a12014-07-20 13:30:15 -04001510int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p)
Adam Langley95c29f32014-06-20 12:00:00 -07001511 {
David Benjamin39482a12014-07-20 13:30:15 -04001512 int i;
David Benjamin6f260012014-08-15 13:49:12 -04001513 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001514 CERT *ct = s->cert;
1515 unsigned char *q;
1516 int no_scsv = s->renegotiate;
1517 /* Set disabled masks for this session */
1518 ssl_set_client_disabled(s);
1519
1520 if (sk == NULL) return(0);
1521 q=p;
1522
1523 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1524 {
1525 c=sk_SSL_CIPHER_value(sk,i);
1526 /* Skip disabled ciphers */
1527 if (c->algorithm_ssl & ct->mask_ssl ||
1528 c->algorithm_mkey & ct->mask_k ||
1529 c->algorithm_auth & ct->mask_a)
1530 continue;
1531#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
1532 if (c->id == SSL3_CK_SCSV)
1533 {
1534 if (no_scsv)
1535 continue;
1536 else
1537 no_scsv = 1;
1538 }
1539#endif
David Benjamin39482a12014-07-20 13:30:15 -04001540 s2n(ssl3_get_cipher_value(c), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001541 }
1542 /* If p == q, no ciphers and caller indicates an error. Otherwise
1543 * add SCSV if not renegotiating.
1544 */
Adam Langleyac61fa32014-06-23 12:03:11 -07001545 if (p != q)
Adam Langley95c29f32014-06-20 12:00:00 -07001546 {
Adam Langleyac61fa32014-06-23 12:03:11 -07001547 if (!no_scsv)
Adam Langley95c29f32014-06-20 12:00:00 -07001548 {
David Benjamin6f260012014-08-15 13:49:12 -04001549 static const SSL_CIPHER scsv =
Adam Langleyac61fa32014-06-23 12:03:11 -07001550 {
1551 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1552 };
David Benjamin39482a12014-07-20 13:30:15 -04001553 s2n(ssl3_get_cipher_value(&scsv), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001554#ifdef OPENSSL_RI_DEBUG
Adam Langleyac61fa32014-06-23 12:03:11 -07001555 fprintf(stderr, "SCSV sent by client\n");
Adam Langley95c29f32014-06-20 12:00:00 -07001556#endif
Adam Langleyac61fa32014-06-23 12:03:11 -07001557 }
1558 if (s->fallback_scsv)
1559 {
David Benjamin6f260012014-08-15 13:49:12 -04001560 static const SSL_CIPHER fallback_scsv =
Adam Langleyac61fa32014-06-23 12:03:11 -07001561 {
1562 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1563 };
David Benjamin39482a12014-07-20 13:30:15 -04001564 s2n(ssl3_get_cipher_value(&fallback_scsv), p);
Adam Langleyac61fa32014-06-23 12:03:11 -07001565 }
Adam Langley95c29f32014-06-20 12:00:00 -07001566 }
1567
1568 return(p-q);
1569 }
1570
David Benjamin39482a12014-07-20 13:30:15 -04001571STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs,
Adam Langley95c29f32014-06-20 12:00:00 -07001572 STACK_OF(SSL_CIPHER) **skp)
1573 {
David Benjamin39482a12014-07-20 13:30:15 -04001574 CBS cipher_suites = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001575 const SSL_CIPHER *c;
1576 STACK_OF(SSL_CIPHER) *sk;
David Benjamin39482a12014-07-20 13:30:15 -04001577
Adam Langley95c29f32014-06-20 12:00:00 -07001578 if (s->s3)
1579 s->s3->send_connection_binding = 0;
1580
David Benjamin39482a12014-07-20 13:30:15 -04001581 if (CBS_len(&cipher_suites) % 2 != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001582 {
1583 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1584 return(NULL);
1585 }
1586 if ((skp == NULL) || (*skp == NULL))
1587 sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
1588 else
1589 {
1590 sk= *skp;
1591 sk_SSL_CIPHER_zero(sk);
1592 }
1593
David Benjamin39482a12014-07-20 13:30:15 -04001594 if (!CBS_stow(&cipher_suites,
1595 &s->cert->ciphers_raw, &s->cert->ciphers_rawlen))
Adam Langley95c29f32014-06-20 12:00:00 -07001596 {
1597 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1598 goto err;
1599 }
Adam Langley95c29f32014-06-20 12:00:00 -07001600
David Benjamin39482a12014-07-20 13:30:15 -04001601 while (CBS_len(&cipher_suites) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001602 {
David Benjamin39482a12014-07-20 13:30:15 -04001603 uint16_t cipher_suite;
1604
1605 if (!CBS_get_u16(&cipher_suites, &cipher_suite))
1606 {
1607 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1608 goto err;
1609 }
1610
Adam Langley95c29f32014-06-20 12:00:00 -07001611 /* Check for SCSV */
David Benjamin39482a12014-07-20 13:30:15 -04001612 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff))
Adam Langley95c29f32014-06-20 12:00:00 -07001613 {
1614 /* SCSV fatal if renegotiating */
1615 if (s->renegotiate)
1616 {
1617 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1618 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1619 goto err;
1620 }
1621 s->s3->send_connection_binding = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001622#ifdef OPENSSL_RI_DEBUG
1623 fprintf(stderr, "SCSV received by server\n");
1624#endif
1625 continue;
1626 }
1627
Adam Langley29707792014-06-20 12:00:00 -07001628 /* Check for FALLBACK_SCSV */
David Benjamin39482a12014-07-20 13:30:15 -04001629 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff) &&
Adam Langley29707792014-06-20 12:00:00 -07001630 s->version < ssl_get_max_version(s))
1631 {
1632 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1633 ssl3_send_alert(s,SSL3_AL_FATAL,SSL3_AD_INAPPROPRIATE_FALLBACK);
1634 goto err;
1635 }
1636
David Benjamin39482a12014-07-20 13:30:15 -04001637 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -07001638 if (c != NULL)
1639 {
1640 if (!sk_SSL_CIPHER_push(sk,c))
1641 {
1642 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1643 goto err;
1644 }
1645 }
1646 }
1647
1648 if (skp != NULL)
1649 *skp=sk;
1650 return(sk);
1651err:
1652 if ((skp == NULL) || (*skp == NULL))
1653 sk_SSL_CIPHER_free(sk);
1654 return(NULL);
1655 }
1656
1657
Adam Langley95c29f32014-06-20 12:00:00 -07001658/** return a servername extension value if provided in Client Hello, or NULL.
1659 * So far, only host_name types are defined (RFC 3546).
1660 */
1661
1662const char *SSL_get_servername(const SSL *s, const int type)
1663 {
1664 if (type != TLSEXT_NAMETYPE_host_name)
1665 return NULL;
1666
1667 return s->session && !s->tlsext_hostname ?
1668 s->session->tlsext_hostname :
1669 s->tlsext_hostname;
1670 }
1671
1672int SSL_get_servername_type(const SSL *s)
1673 {
1674 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1675 return TLSEXT_NAMETYPE_host_name;
1676 return -1;
1677 }
1678
1679/* SSL_select_next_proto implements the standard protocol selection. It is
1680 * expected that this function is called from the callback set by
1681 * SSL_CTX_set_next_proto_select_cb.
1682 *
1683 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1684 * strings. The length byte itself is not included in the length. A byte
1685 * string of length 0 is invalid. No byte string may be truncated.
1686 *
1687 * The current, but experimental algorithm for selecting the protocol is:
1688 *
1689 * 1) If the server doesn't support NPN then this is indicated to the
1690 * callback. In this case, the client application has to abort the connection
1691 * or have a default application level protocol.
1692 *
1693 * 2) If the server supports NPN, but advertises an empty list then the
1694 * client selects the first protcol in its list, but indicates via the
1695 * API that this fallback case was enacted.
1696 *
1697 * 3) Otherwise, the client finds the first protocol in the server's list
1698 * that it supports and selects this protocol. This is because it's
1699 * assumed that the server has better information about which protocol
1700 * a client should use.
1701 *
1702 * 4) If the client doesn't support any of the server's advertised
1703 * protocols, then this is treated the same as case 2.
1704 *
1705 * It returns either
1706 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1707 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1708 */
1709int 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)
1710 {
1711 unsigned int i, j;
1712 const unsigned char *result;
1713 int status = OPENSSL_NPN_UNSUPPORTED;
1714
1715 /* For each protocol in server preference order, see if we support it. */
1716 for (i = 0; i < server_len; )
1717 {
1718 for (j = 0; j < client_len; )
1719 {
1720 if (server[i] == client[j] &&
1721 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1722 {
1723 /* We found a match */
1724 result = &server[i];
1725 status = OPENSSL_NPN_NEGOTIATED;
1726 goto found;
1727 }
1728 j += client[j];
1729 j++;
1730 }
1731 i += server[i];
1732 i++;
1733 }
1734
1735 /* There's no overlap between our protocols and the server's list. */
1736 result = client;
1737 status = OPENSSL_NPN_NO_OVERLAP;
1738
1739 found:
1740 *out = (unsigned char *) result + 1;
1741 *outlen = result[0];
1742 return status;
1743 }
1744
1745# ifndef OPENSSL_NO_NEXTPROTONEG
1746/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1747 * requested protocol for this connection and returns 0. If the client didn't
1748 * request any protocol, then *data is set to NULL.
1749 *
1750 * Note that the client can request any protocol it chooses. The value returned
1751 * from this function need not be a member of the list of supported protocols
1752 * provided by the callback.
1753 */
David Benjaminced551f2014-07-11 23:16:16 -04001754void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, unsigned *len)
Adam Langley95c29f32014-06-20 12:00:00 -07001755 {
1756 *data = s->next_proto_negotiated;
1757 if (!*data) {
1758 *len = 0;
1759 } else {
1760 *len = s->next_proto_negotiated_len;
1761 }
1762}
1763
1764/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1765 * TLS server needs a list of supported protocols for Next Protocol
1766 * Negotiation. The returned list must be in wire format. The list is returned
1767 * by setting |out| to point to it and |outlen| to its length. This memory will
1768 * not be modified, but one should assume that the SSL* keeps a reference to
1769 * it.
1770 *
1771 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1772 * such extension will be included in the ServerHello. */
1773void 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)
1774 {
1775 ctx->next_protos_advertised_cb = cb;
1776 ctx->next_protos_advertised_cb_arg = arg;
1777 }
1778
1779/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1780 * client needs to select a protocol from the server's provided list. |out|
1781 * must be set to point to the selected protocol (which may be within |in|).
1782 * The length of the protocol name must be written into |outlen|. The server's
1783 * advertised protocols are provided in |in| and |inlen|. The callback can
1784 * assume that |in| is syntactically valid.
1785 *
1786 * The client must select a protocol. It is fatal to the connection if this
1787 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1788 */
1789void 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)
1790 {
1791 ctx->next_proto_select_cb = cb;
1792 ctx->next_proto_select_cb_arg = arg;
1793 }
1794# endif
1795
Adam Langley95c29f32014-06-20 12:00:00 -07001796/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1797 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1798 * length-prefixed strings).
1799 *
1800 * Returns 0 on success. */
1801int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1802 unsigned protos_len)
1803 {
1804 if (ctx->alpn_client_proto_list)
1805 OPENSSL_free(ctx->alpn_client_proto_list);
1806
David Benjamin072c9532014-07-26 11:44:25 -04001807 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001808 if (!ctx->alpn_client_proto_list)
1809 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001810 ctx->alpn_client_proto_list_len = protos_len;
1811
1812 return 0;
1813 }
1814
1815/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1816 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1817 * length-prefixed strings).
1818 *
1819 * Returns 0 on success. */
1820int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1821 unsigned protos_len)
1822 {
1823 if (ssl->alpn_client_proto_list)
1824 OPENSSL_free(ssl->alpn_client_proto_list);
1825
David Benjamin072c9532014-07-26 11:44:25 -04001826 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001827 if (!ssl->alpn_client_proto_list)
1828 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001829 ssl->alpn_client_proto_list_len = protos_len;
1830
1831 return 0;
1832 }
1833
1834/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1835 * during ClientHello processing in order to select an ALPN protocol from the
1836 * client's list of offered protocols. */
1837void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1838 int (*cb) (SSL *ssl,
1839 const unsigned char **out,
1840 unsigned char *outlen,
1841 const unsigned char *in,
1842 unsigned int inlen,
1843 void *arg),
1844 void *arg)
1845 {
1846 ctx->alpn_select_cb = cb;
1847 ctx->alpn_select_cb_arg = arg;
1848 }
1849
1850/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1851 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1852 * including the leading length-prefix byte). If the server didn't respond with
1853 * a negotiated protocol then |*len| will be zero. */
1854void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1855 unsigned *len)
1856 {
1857 *data = NULL;
1858 if (ssl->s3)
1859 *data = ssl->s3->alpn_selected;
1860 if (*data == NULL)
1861 *len = 0;
1862 else
1863 *len = ssl->s3->alpn_selected_len;
1864 }
Adam Langley95c29f32014-06-20 12:00:00 -07001865
1866int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1867 const char *label, size_t llen, const unsigned char *p, size_t plen,
1868 int use_context)
1869 {
1870 if (s->version < TLS1_VERSION)
1871 return -1;
1872
1873 return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1874 llen, p, plen,
1875 use_context);
1876 }
1877
1878static uint32_t ssl_session_hash(const SSL_SESSION *a)
1879 {
1880 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1881 ((uint32_t) a->session_id[1] << 8) ||
1882 ((uint32_t) a->session_id[2] << 16) ||
1883 ((uint32_t) a->session_id[3] << 24);
1884
1885 return hash;
1886 }
1887
1888/* NB: If this function (or indeed the hash function which uses a sort of
1889 * coarser function than this one) is changed, ensure
1890 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1891 * able to construct an SSL_SESSION that will collide with any existing session
1892 * with a matching session ID. */
1893static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1894 {
1895 if (a->ssl_version != b->ssl_version)
1896 return(1);
1897 if (a->session_id_length != b->session_id_length)
1898 return(1);
1899 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1900 }
1901
1902SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1903 {
1904 SSL_CTX *ret=NULL;
1905
1906 if (meth == NULL)
1907 {
1908 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1909 return(NULL);
1910 }
1911
Adam Langley95c29f32014-06-20 12:00:00 -07001912 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1913 {
1914 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1915 goto err;
1916 }
1917 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1918 if (ret == NULL)
1919 goto err;
1920
1921 memset(ret,0,sizeof(SSL_CTX));
1922
1923 ret->method=meth;
1924
1925 ret->cert_store=NULL;
1926 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1927 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1928 ret->session_cache_head=NULL;
1929 ret->session_cache_tail=NULL;
1930
1931 /* We take the system default */
David Benjaminf4501342014-08-14 17:24:37 -04001932 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001933
1934 ret->new_session_cb=0;
1935 ret->remove_session_cb=0;
1936 ret->get_session_cb=0;
1937 ret->generate_session_id=0;
1938
1939 memset((char *)&ret->stats,0,sizeof(ret->stats));
1940
1941 ret->references=1;
1942 ret->quiet_shutdown=0;
1943
Adam Langley95c29f32014-06-20 12:00:00 -07001944 ret->info_callback=NULL;
1945
1946 ret->app_verify_callback=0;
1947 ret->app_verify_arg=NULL;
1948
1949 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1950 ret->read_ahead=0;
1951 ret->msg_callback=0;
1952 ret->msg_callback_arg=NULL;
1953 ret->verify_mode=SSL_VERIFY_NONE;
1954#if 0
1955 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1956#endif
1957 ret->sid_ctx_length=0;
1958 ret->default_verify_callback=NULL;
1959 if ((ret->cert=ssl_cert_new()) == NULL)
1960 goto err;
1961
1962 ret->default_passwd_callback=0;
1963 ret->default_passwd_callback_userdata=NULL;
1964 ret->client_cert_cb=0;
1965 ret->app_gen_cookie_cb=0;
1966 ret->app_verify_cookie_cb=0;
1967
1968 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1969 if (ret->sessions == NULL) goto err;
1970 ret->cert_store=X509_STORE_new();
1971 if (ret->cert_store == NULL) goto err;
1972
1973 ssl_create_cipher_list(ret->method,
1974 &ret->cipher_list,&ret->cipher_list_by_id,
1975 meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST, ret->cert);
1976 if (ret->cipher_list == NULL
Adam Langley858a88d2014-06-20 12:00:00 -07001977 || sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001978 {
1979 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1980 goto err2;
1981 }
1982
1983 ret->param = X509_VERIFY_PARAM_new();
1984 if (!ret->param)
1985 goto err;
1986
Adam Langley95c29f32014-06-20 12:00:00 -07001987 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1988 goto err;
1989
1990 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1991
1992 ret->extra_certs=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001993
1994 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1995
Adam Langley95c29f32014-06-20 12:00:00 -07001996 ret->tlsext_servername_callback = 0;
1997 ret->tlsext_servername_arg = NULL;
1998 /* Setup RFC4507 ticket keys */
1999 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
2000 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
2001 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
2002 ret->options |= SSL_OP_NO_TICKET;
2003
2004 ret->tlsext_status_cb = 0;
2005 ret->tlsext_status_arg = NULL;
2006
2007# ifndef OPENSSL_NO_NEXTPROTONEG
2008 ret->next_protos_advertised_cb = 0;
2009 ret->next_proto_select_cb = 0;
2010# endif
Adam Langley95c29f32014-06-20 12:00:00 -07002011 ret->psk_identity_hint=NULL;
2012 ret->psk_client_callback=NULL;
2013 ret->psk_server_callback=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002014#ifndef OPENSSL_NO_ENGINE
2015 ret->client_cert_engine = NULL;
2016#ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
2017#define eng_strx(x) #x
2018#define eng_str(x) eng_strx(x)
2019 /* Use specific client engine automatically... ignore errors */
2020 {
2021 ENGINE *eng;
2022 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
2023 if (!eng)
2024 {
2025 ERR_clear_error();
2026 ENGINE_load_builtin_engines();
2027 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
2028 }
2029 if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
2030 ERR_clear_error();
2031 }
2032#endif
2033#endif
2034 /* Default is to connect to non-RI servers. When RI is more widely
2035 * deployed might change this.
2036 */
2037 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
2038
2039 return(ret);
2040err:
2041 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
2042err2:
2043 if (ret != NULL) SSL_CTX_free(ret);
2044 return(NULL);
2045 }
2046
Adam Langley95c29f32014-06-20 12:00:00 -07002047
2048void SSL_CTX_free(SSL_CTX *a)
2049 {
2050 int i;
2051
2052 if (a == NULL) return;
2053
2054 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
2055#ifdef REF_PRINT
2056 REF_PRINT("SSL_CTX",a);
2057#endif
2058 if (i > 0) return;
2059#ifdef REF_CHECK
2060 if (i < 0)
2061 {
2062 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
2063 abort(); /* ok */
2064 }
2065#endif
2066
2067 if (a->param)
2068 X509_VERIFY_PARAM_free(a->param);
2069
2070 /*
2071 * Free internal session cache. However: the remove_cb() may reference
2072 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2073 * after the sessions were flushed.
2074 * As the ex_data handling routines might also touch the session cache,
2075 * the most secure solution seems to be: empty (flush) the cache, then
2076 * free ex_data, then finally free the cache.
2077 * (See ticket [openssl.org #212].)
2078 */
2079 if (a->sessions != NULL)
2080 SSL_CTX_flush_sessions(a,0);
2081
2082 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
2083
2084 if (a->sessions != NULL)
2085 lh_SSL_SESSION_free(a->sessions);
2086
2087 if (a->cert_store != NULL)
2088 X509_STORE_free(a->cert_store);
2089 if (a->cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002090 ssl_cipher_preference_list_free(a->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002091 if (a->cipher_list_by_id != NULL)
2092 sk_SSL_CIPHER_free(a->cipher_list_by_id);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07002093 if (a->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002094 ssl_cipher_preference_list_free(a->cipher_list_tls11);
Adam Langley95c29f32014-06-20 12:00:00 -07002095 if (a->cert != NULL)
2096 ssl_cert_free(a->cert);
2097 if (a->client_CA != NULL)
2098 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2099 if (a->extra_certs != NULL)
2100 sk_X509_pop_free(a->extra_certs,X509_free);
Adam Langley95c29f32014-06-20 12:00:00 -07002101
2102 if (a->srtp_profiles)
2103 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2104
Adam Langley95c29f32014-06-20 12:00:00 -07002105 if (a->psk_identity_hint)
2106 OPENSSL_free(a->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07002107
2108 /* TODO(fork): remove. */
2109#if 0
2110#ifndef OPENSSL_NO_ENGINE
2111 if (a->client_cert_engine)
2112 ENGINE_finish(a->client_cert_engine);
2113#endif
2114#endif
2115
Adam Langley95c29f32014-06-20 12:00:00 -07002116# ifndef OPENSSL_NO_EC
2117 if (a->tlsext_ecpointformatlist)
2118 OPENSSL_free(a->tlsext_ecpointformatlist);
2119 if (a->tlsext_ellipticcurvelist)
2120 OPENSSL_free(a->tlsext_ellipticcurvelist);
2121# endif /* OPENSSL_NO_EC */
2122 if (a->alpn_client_proto_list != NULL)
2123 OPENSSL_free(a->alpn_client_proto_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002124
Adam Langley1258b6a2014-06-20 12:00:00 -07002125 if (a->tlsext_channel_id_private)
2126 EVP_PKEY_free(a->tlsext_channel_id_private);
Adam Langley1258b6a2014-06-20 12:00:00 -07002127
Adam Langley95c29f32014-06-20 12:00:00 -07002128 OPENSSL_free(a);
2129 }
2130
2131void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2132 {
2133 ctx->default_passwd_callback=cb;
2134 }
2135
2136void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2137 {
2138 ctx->default_passwd_callback_userdata=u;
2139 }
2140
2141void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2142 {
2143 ctx->app_verify_callback=cb;
2144 ctx->app_verify_arg=arg;
2145 }
2146
2147void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2148 {
2149 ctx->verify_mode=mode;
2150 ctx->default_verify_callback=cb;
2151 }
2152
2153void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2154 {
2155 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2156 }
2157
2158void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2159 {
2160 ssl_cert_set_cert_cb(c->cert, cb, arg);
2161 }
2162
2163void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2164 {
2165 ssl_cert_set_cert_cb(s->cert, cb, arg);
2166 }
2167
2168void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
2169 {
2170 CERT_PKEY *cpk;
David Benjamin0da0e182014-08-19 16:20:28 -04002171 int rsa_enc,rsa_sign,dh_tmp,dsa_sign;
David Benjamin060d9d22014-07-15 00:54:26 -04002172 unsigned long mask_k,mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002173#ifndef OPENSSL_NO_ECDSA
David Benjamin060d9d22014-07-15 00:54:26 -04002174 int have_ecc_cert, ecdsa_ok;
Adam Langley95c29f32014-06-20 12:00:00 -07002175#endif
2176#ifndef OPENSSL_NO_ECDH
David Benjamin0da0e182014-08-19 16:20:28 -04002177 int have_ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002178#endif
2179#ifndef OPENSSL_NO_EC
2180 X509 *x = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002181#endif
2182 if (c == NULL) return;
2183
Adam Langley95c29f32014-06-20 12:00:00 -07002184#ifndef OPENSSL_NO_DH
2185 dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002186#else
David Benjamin060d9d22014-07-15 00:54:26 -04002187 dh_tmp=0;
Adam Langley95c29f32014-06-20 12:00:00 -07002188#endif
2189
2190#ifndef OPENSSL_NO_ECDH
2191 have_ecdh_tmp=(c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
2192#endif
2193 cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
2194 rsa_enc= cpk->valid_flags & CERT_PKEY_VALID;
Adam Langley95c29f32014-06-20 12:00:00 -07002195 cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
2196 rsa_sign= cpk->valid_flags & CERT_PKEY_SIGN;
2197 cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
2198 dsa_sign= cpk->valid_flags & CERT_PKEY_SIGN;
Adam Langley95c29f32014-06-20 12:00:00 -07002199 cpk= &(c->pkeys[SSL_PKEY_ECC]);
2200#ifndef OPENSSL_NO_EC
2201 have_ecc_cert= cpk->valid_flags & CERT_PKEY_VALID;
2202#endif
2203 mask_k=0;
2204 mask_a=0;
Adam Langley95c29f32014-06-20 12:00:00 -07002205
2206#ifdef CIPHER_DEBUG
2207 printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
2208 rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
2209 rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
2210#endif
2211
David Benjamin77a942b2014-07-15 01:22:50 -04002212 if (rsa_enc)
Adam Langley95c29f32014-06-20 12:00:00 -07002213 mask_k|=SSL_kRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002214
Adam Langley95c29f32014-06-20 12:00:00 -07002215 if (dh_tmp)
2216 mask_k|=SSL_kEDH;
2217
Adam Langley95c29f32014-06-20 12:00:00 -07002218 if (rsa_enc || rsa_sign)
2219 {
2220 mask_a|=SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002221 }
2222
2223 if (dsa_sign)
2224 {
2225 mask_a|=SSL_aDSS;
Adam Langley95c29f32014-06-20 12:00:00 -07002226 }
2227
2228 mask_a|=SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002229
David Benjamin0da0e182014-08-19 16:20:28 -04002230 /* An ECC certificate may be usable for ECDSA cipher suites depending on
2231 * the key usage extension. */
Adam Langley95c29f32014-06-20 12:00:00 -07002232#ifndef OPENSSL_NO_EC
2233 if (have_ecc_cert)
2234 {
2235 cpk = &c->pkeys[SSL_PKEY_ECC];
2236 x = cpk->x509;
2237 /* This call populates extension flags (ex_flags) */
2238 X509_check_purpose(x, -1, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07002239 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2240 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
2241 if (!(cpk->valid_flags & CERT_PKEY_SIGN))
2242 ecdsa_ok = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002243#ifndef OPENSSL_NO_ECDSA
2244 if (ecdsa_ok)
2245 {
2246 mask_a|=SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002247 }
2248#endif
2249 }
2250#endif
2251
2252#ifndef OPENSSL_NO_ECDH
2253 if (have_ecdh_tmp)
2254 {
2255 mask_k|=SSL_kEECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002256 }
2257#endif
2258
Adam Langley95c29f32014-06-20 12:00:00 -07002259 mask_k |= SSL_kPSK;
2260 mask_a |= SSL_aPSK;
Adam Langley95c29f32014-06-20 12:00:00 -07002261
2262 c->mask_k=mask_k;
2263 c->mask_a=mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002264 c->valid=1;
2265 }
2266
2267/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2268#define ku_reject(x, usage) \
2269 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2270
2271#ifndef OPENSSL_NO_EC
2272
2273int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2274 {
David Benjamin0da0e182014-08-19 16:20:28 -04002275 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002276 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2277 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2278
Adam Langley95c29f32014-06-20 12:00:00 -07002279 alg_a = cs->algorithm_auth;
2280
Adam Langley95c29f32014-06-20 12:00:00 -07002281 /* This call populates the ex_flags field correctly */
2282 X509_check_purpose(x, -1, 0);
2283 if ((x->sig_alg) && (x->sig_alg->algorithm))
2284 {
2285 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2286 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2287 }
Adam Langley95c29f32014-06-20 12:00:00 -07002288 if (alg_a & SSL_aECDSA)
2289 {
2290 /* key usage, if present, must allow signing */
2291 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2292 {
2293 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2294 return 0;
2295 }
2296 }
2297
2298 return 1; /* all checks are ok */
2299 }
2300
2301#endif
2302
2303static int ssl_get_server_cert_index(const SSL *s)
2304 {
2305 int idx;
2306 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2307 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2308 idx = SSL_PKEY_RSA_SIGN;
2309 if (idx == -1)
2310 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2311 return idx;
2312 }
2313
2314CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2315 {
2316 CERT *c;
2317 int i;
2318
2319 c = s->cert;
2320 ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
2321
2322#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2323 /* Broken protocol test: return last used certificate: which may
2324 * mismatch the one expected.
2325 */
2326 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2327 return c->key;
2328#endif
2329
2330 i = ssl_get_server_cert_index(s);
2331
2332 /* This may or may not be an error. */
2333 if (i < 0)
2334 return NULL;
2335
2336 /* May be NULL. */
2337 return &c->pkeys[i];
2338 }
2339
2340EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd)
2341 {
2342 unsigned long alg_a;
2343 CERT *c;
2344 int idx = -1;
2345
2346 alg_a = cipher->algorithm_auth;
2347 c=s->cert;
2348
2349#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2350 /* Broken protocol test: use last key: which may
2351 * mismatch the one expected.
2352 */
2353 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2354 idx = c->key - c->pkeys;
2355 else
2356#endif
2357
2358 if ((alg_a & SSL_aDSS) &&
2359 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
2360 idx = SSL_PKEY_DSA_SIGN;
2361 else if (alg_a & SSL_aRSA)
2362 {
2363 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2364 idx = SSL_PKEY_RSA_SIGN;
2365 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2366 idx = SSL_PKEY_RSA_ENC;
2367 }
2368 else if ((alg_a & SSL_aECDSA) &&
2369 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2370 idx = SSL_PKEY_ECC;
2371 if (idx == -1)
2372 {
2373 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2374 return(NULL);
2375 }
2376 if (pmd)
2377 *pmd = c->pkeys[idx].digest;
2378 return c->pkeys[idx].privatekey;
2379 }
2380
Adam Langley95c29f32014-06-20 12:00:00 -07002381void ssl_update_cache(SSL *s,int mode)
2382 {
2383 int i;
2384
2385 /* If the session_id_length is 0, we are not supposed to cache it,
2386 * and it would be rather hard to do anyway :-) */
2387 if (s->session->session_id_length == 0) return;
2388
2389 i=s->session_ctx->session_cache_mode;
2390 if ((i & mode) && (!s->hit)
2391 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
2392 || SSL_CTX_add_session(s->session_ctx,s->session))
2393 && (s->session_ctx->new_session_cb != NULL))
2394 {
2395 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
2396 if (!s->session_ctx->new_session_cb(s,s->session))
2397 SSL_SESSION_free(s->session);
2398 }
2399
2400 /* auto flush every 255 connections */
2401 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2402 ((i & mode) == mode))
2403 {
2404 if ( (((mode & SSL_SESS_CACHE_CLIENT)
2405 ?s->session_ctx->stats.sess_connect_good
2406 :s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff)
2407 {
2408 SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL));
2409 }
2410 }
2411 }
2412
2413const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
2414 {
2415 return ctx->method;
2416 }
2417
2418const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2419 {
2420 return(s->method);
2421 }
2422
2423int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2424 {
2425 int conn= -1;
2426 int ret=1;
2427
2428 if (s->method != meth)
2429 {
2430 if (s->handshake_func != NULL)
2431 conn=(s->handshake_func == s->method->ssl_connect);
2432
2433 if (s->method->version == meth->version)
2434 s->method=meth;
2435 else
2436 {
2437 s->method->ssl_free(s);
2438 s->method=meth;
2439 ret=s->method->ssl_new(s);
2440 }
2441
2442 if (conn == 1)
2443 s->handshake_func=meth->ssl_connect;
2444 else if (conn == 0)
2445 s->handshake_func=meth->ssl_accept;
2446 }
2447 return(ret);
2448 }
2449
2450int SSL_get_error(const SSL *s,int i)
2451 {
2452 int reason;
2453 unsigned long l;
2454 BIO *bio;
2455
2456 if (i > 0) return(SSL_ERROR_NONE);
2457
2458 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2459 * etc, where we do encode the error */
2460 if ((l=ERR_peek_error()) != 0)
2461 {
2462 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2463 return(SSL_ERROR_SYSCALL);
2464 else
2465 return(SSL_ERROR_SSL);
2466 }
2467
Adam Langleyb2ce0582014-06-20 12:00:00 -07002468 if ((i < 0) && SSL_want_session(s))
2469 return(SSL_ERROR_PENDING_SESSION);
2470
Adam Langleydc9b1412014-06-20 12:00:00 -07002471 if ((i < 0) && SSL_want_certificate(s))
2472 return(SSL_ERROR_PENDING_CERTIFICATE);
2473
Adam Langley95c29f32014-06-20 12:00:00 -07002474 if ((i < 0) && SSL_want_read(s))
2475 {
2476 bio=SSL_get_rbio(s);
2477 if (BIO_should_read(bio))
2478 return(SSL_ERROR_WANT_READ);
2479 else if (BIO_should_write(bio))
2480 /* This one doesn't make too much sense ... We never try
2481 * to write to the rbio, and an application program where
2482 * rbio and wbio are separate couldn't even know what it
2483 * should wait for.
2484 * However if we ever set s->rwstate incorrectly
2485 * (so that we have SSL_want_read(s) instead of
2486 * SSL_want_write(s)) and rbio and wbio *are* the same,
2487 * this test works around that bug; so it might be safer
2488 * to keep it. */
2489 return(SSL_ERROR_WANT_WRITE);
2490 else if (BIO_should_io_special(bio))
2491 {
2492 reason=BIO_get_retry_reason(bio);
2493 if (reason == BIO_RR_CONNECT)
2494 return(SSL_ERROR_WANT_CONNECT);
2495 else if (reason == BIO_RR_ACCEPT)
2496 return(SSL_ERROR_WANT_ACCEPT);
2497 else
2498 return(SSL_ERROR_SYSCALL); /* unknown */
2499 }
2500 }
2501
2502 if ((i < 0) && SSL_want_write(s))
2503 {
2504 bio=SSL_get_wbio(s);
2505 if (BIO_should_write(bio))
2506 return(SSL_ERROR_WANT_WRITE);
2507 else if (BIO_should_read(bio))
2508 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2509 return(SSL_ERROR_WANT_READ);
2510 else if (BIO_should_io_special(bio))
2511 {
2512 reason=BIO_get_retry_reason(bio);
2513 if (reason == BIO_RR_CONNECT)
2514 return(SSL_ERROR_WANT_CONNECT);
2515 else if (reason == BIO_RR_ACCEPT)
2516 return(SSL_ERROR_WANT_ACCEPT);
2517 else
2518 return(SSL_ERROR_SYSCALL);
2519 }
2520 }
2521 if ((i < 0) && SSL_want_x509_lookup(s))
2522 {
2523 return(SSL_ERROR_WANT_X509_LOOKUP);
2524 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002525 if ((i < 0) && SSL_want_channel_id_lookup(s))
2526 {
2527 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2528 }
Adam Langley95c29f32014-06-20 12:00:00 -07002529
2530 if (i == 0)
2531 {
2532 if (s->version == SSL2_VERSION)
2533 {
2534 /* assume it is the socket being closed */
2535 return(SSL_ERROR_ZERO_RETURN);
2536 }
2537 else
2538 {
2539 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2540 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2541 return(SSL_ERROR_ZERO_RETURN);
2542 }
2543 }
2544 return(SSL_ERROR_SYSCALL);
2545 }
2546
2547int SSL_do_handshake(SSL *s)
2548 {
2549 int ret=1;
2550
2551 if (s->handshake_func == NULL)
2552 {
2553 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2554 return(-1);
2555 }
2556
2557 s->method->ssl_renegotiate_check(s);
2558
2559 if (SSL_in_init(s) || SSL_in_before(s))
2560 {
2561 ret=s->handshake_func(s);
2562 }
2563 return(ret);
2564 }
2565
2566/* For the next 2 functions, SSL_clear() sets shutdown and so
2567 * one of these calls will reset it */
2568void SSL_set_accept_state(SSL *s)
2569 {
2570 s->server=1;
2571 s->shutdown=0;
2572 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2573 s->handshake_func=s->method->ssl_accept;
2574 /* clear the current cipher */
2575 ssl_clear_cipher_ctx(s);
2576 ssl_clear_hash_ctx(&s->read_hash);
2577 ssl_clear_hash_ctx(&s->write_hash);
2578 }
2579
2580void SSL_set_connect_state(SSL *s)
2581 {
2582 s->server=0;
2583 s->shutdown=0;
2584 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2585 s->handshake_func=s->method->ssl_connect;
2586 /* clear the current cipher */
2587 ssl_clear_cipher_ctx(s);
2588 ssl_clear_hash_ctx(&s->read_hash);
2589 ssl_clear_hash_ctx(&s->write_hash);
2590 }
2591
2592int ssl_undefined_function(SSL *s)
2593 {
2594 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2595 return(0);
2596 }
2597
2598int ssl_undefined_void_function(void)
2599 {
2600 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2601 return(0);
2602 }
2603
2604int ssl_undefined_const_function(const SSL *s)
2605 {
2606 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2607 return(0);
2608 }
2609
2610SSL_METHOD *ssl_bad_method(int ver)
2611 {
2612 OPENSSL_PUT_ERROR(SSL, ssl_bad_method, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2613 return(NULL);
2614 }
2615
Adam Langley0f4746e2014-08-13 12:26:32 -07002616static const char *ssl_get_version(int version)
Adam Langley95c29f32014-06-20 12:00:00 -07002617 {
Adam Langley0f4746e2014-08-13 12:26:32 -07002618 if (version == TLS1_2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002619 return("TLSv1.2");
Adam Langley0f4746e2014-08-13 12:26:32 -07002620 else if (version == TLS1_1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002621 return("TLSv1.1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002622 else if (version == TLS1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002623 return("TLSv1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002624 else if (version == SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002625 return("SSLv3");
Adam Langley0f4746e2014-08-13 12:26:32 -07002626 else if (version == SSL2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002627 return("SSLv2");
2628 else
2629 return("unknown");
2630 }
2631
Adam Langley0f4746e2014-08-13 12:26:32 -07002632const char *SSL_get_version(const SSL *s)
2633 {
2634 return ssl_get_version(s->version);
2635 }
2636
2637const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2638 {
2639 return ssl_get_version(sess->ssl_version);
2640 }
2641
Adam Langley95c29f32014-06-20 12:00:00 -07002642void ssl_clear_cipher_ctx(SSL *s)
2643 {
2644 if (s->enc_read_ctx != NULL)
2645 {
2646 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2647 OPENSSL_free(s->enc_read_ctx);
2648 s->enc_read_ctx=NULL;
2649 }
2650 if (s->enc_write_ctx != NULL)
2651 {
2652 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2653 OPENSSL_free(s->enc_write_ctx);
2654 s->enc_write_ctx=NULL;
2655 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002656 if (s->aead_read_ctx != NULL)
2657 {
2658 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2659 OPENSSL_free(s->aead_read_ctx);
2660 s->aead_read_ctx = NULL;
2661 }
2662 if (s->aead_write_ctx != NULL)
2663 {
2664 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2665 OPENSSL_free(s->aead_write_ctx);
2666 s->aead_write_ctx = NULL;
2667 }
Adam Langley95c29f32014-06-20 12:00:00 -07002668 }
2669
2670X509 *SSL_get_certificate(const SSL *s)
2671 {
2672 if (s->cert != NULL)
2673 return(s->cert->key->x509);
2674 else
2675 return(NULL);
2676 }
2677
2678EVP_PKEY *SSL_get_privatekey(const SSL *s)
2679 {
2680 if (s->cert != NULL)
2681 return(s->cert->key->privatekey);
2682 else
2683 return(NULL);
2684 }
2685
2686X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2687 {
2688 if (ctx->cert != NULL)
2689 return ctx->cert->key->x509;
2690 else
2691 return NULL;
2692 }
2693
2694EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2695 {
2696 if (ctx->cert != NULL)
2697 return ctx->cert->key->privatekey;
2698 else
2699 return NULL ;
2700 }
2701
2702const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2703 {
2704 if ((s->session != NULL) && (s->session->cipher != NULL))
2705 return(s->session->cipher);
2706 return(NULL);
2707 }
2708const void *SSL_get_current_compression(SSL *s)
2709 {
2710 return NULL;
2711 }
2712const void *SSL_get_current_expansion(SSL *s)
2713 {
2714 return NULL;
2715 }
2716
2717int ssl_init_wbio_buffer(SSL *s,int push)
2718 {
2719 BIO *bbio;
2720
2721 if (s->bbio == NULL)
2722 {
2723 bbio=BIO_new(BIO_f_buffer());
2724 if (bbio == NULL) return(0);
2725 s->bbio=bbio;
2726 }
2727 else
2728 {
2729 bbio=s->bbio;
2730 if (s->bbio == s->wbio)
2731 s->wbio=BIO_pop(s->wbio);
2732 }
2733 (void)BIO_reset(bbio);
2734/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2735 if (!BIO_set_read_buffer_size(bbio,1))
2736 {
2737 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2738 return(0);
2739 }
2740 if (push)
2741 {
2742 if (s->wbio != bbio)
2743 s->wbio=BIO_push(bbio,s->wbio);
2744 }
2745 else
2746 {
2747 if (s->wbio == bbio)
2748 s->wbio=BIO_pop(bbio);
2749 }
2750 return(1);
2751 }
2752
2753void ssl_free_wbio_buffer(SSL *s)
2754 {
2755 if (s->bbio == NULL) return;
2756
2757 if (s->bbio == s->wbio)
2758 {
2759 /* remove buffering */
2760 s->wbio=BIO_pop(s->wbio);
2761#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2762 assert(s->wbio != NULL);
2763#endif
2764 }
2765 BIO_free(s->bbio);
2766 s->bbio=NULL;
2767 }
2768
2769void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2770 {
2771 ctx->quiet_shutdown=mode;
2772 }
2773
2774int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2775 {
2776 return(ctx->quiet_shutdown);
2777 }
2778
2779void SSL_set_quiet_shutdown(SSL *s,int mode)
2780 {
2781 s->quiet_shutdown=mode;
2782 }
2783
2784int SSL_get_quiet_shutdown(const SSL *s)
2785 {
2786 return(s->quiet_shutdown);
2787 }
2788
2789void SSL_set_shutdown(SSL *s,int mode)
2790 {
2791 s->shutdown=mode;
2792 }
2793
2794int SSL_get_shutdown(const SSL *s)
2795 {
2796 return(s->shutdown);
2797 }
2798
2799int SSL_version(const SSL *s)
2800 {
2801 return(s->version);
2802 }
2803
2804SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2805 {
2806 return(ssl->ctx);
2807 }
2808
2809SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2810 {
2811 if (ssl->ctx == ctx)
2812 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002813 if (ctx == NULL)
2814 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002815 if (ssl->cert != NULL)
2816 ssl_cert_free(ssl->cert);
2817 ssl->cert = ssl_cert_dup(ctx->cert);
2818 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2819 if (ssl->ctx != NULL)
2820 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2821 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07002822
2823 ssl->sid_ctx_length = ctx->sid_ctx_length;
2824 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2825 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2826
Adam Langley95c29f32014-06-20 12:00:00 -07002827 return(ssl->ctx);
2828 }
2829
2830#ifndef OPENSSL_NO_STDIO
2831int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2832 {
2833 return(X509_STORE_set_default_paths(ctx->cert_store));
2834 }
2835
2836int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2837 const char *CApath)
2838 {
2839 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2840 }
2841#endif
2842
2843void SSL_set_info_callback(SSL *ssl,
2844 void (*cb)(const SSL *ssl,int type,int val))
2845 {
2846 ssl->info_callback=cb;
2847 }
2848
2849/* One compiler (Diab DCC) doesn't like argument names in returned
2850 function pointer. */
2851void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2852 {
2853 return ssl->info_callback;
2854 }
2855
2856int SSL_state(const SSL *ssl)
2857 {
2858 return(ssl->state);
2859 }
2860
2861void SSL_set_state(SSL *ssl, int state)
2862 {
2863 ssl->state = state;
2864 }
2865
2866void SSL_set_verify_result(SSL *ssl,long arg)
2867 {
2868 ssl->verify_result=arg;
2869 }
2870
2871long SSL_get_verify_result(const SSL *ssl)
2872 {
2873 return(ssl->verify_result);
2874 }
2875
2876int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2877 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2878 {
2879 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2880 new_func, dup_func, free_func);
2881 }
2882
2883int SSL_set_ex_data(SSL *s,int idx,void *arg)
2884 {
2885 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2886 }
2887
2888void *SSL_get_ex_data(const SSL *s,int idx)
2889 {
2890 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2891 }
2892
2893int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2894 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2895 {
2896 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2897 new_func, dup_func, free_func);
2898 }
2899
2900int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2901 {
2902 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2903 }
2904
2905void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2906 {
2907 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2908 }
2909
2910int ssl_ok(SSL *s)
2911 {
2912 return(1);
2913 }
2914
2915X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2916 {
2917 return(ctx->cert_store);
2918 }
2919
2920void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2921 {
2922 if (ctx->cert_store != NULL)
2923 X509_STORE_free(ctx->cert_store);
2924 ctx->cert_store=store;
2925 }
2926
2927int SSL_want(const SSL *s)
2928 {
2929 return(s->rwstate);
2930 }
2931
2932/*!
2933 * \brief Set the callback for generating temporary RSA keys.
2934 * \param ctx the SSL context.
2935 * \param cb the callback
2936 */
2937
Adam Langley95c29f32014-06-20 12:00:00 -07002938void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2939 int is_export,
2940 int keylength))
2941 {
2942 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2943 }
2944
2945void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2946 int is_export,
2947 int keylength))
2948 {
2949 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2950 }
Adam Langley95c29f32014-06-20 12:00:00 -07002951
2952#ifdef DOXYGEN
2953/*!
2954 * \brief The RSA temporary key callback function.
2955 * \param ssl the SSL session.
2956 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2957 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2958 * of the required key in bits.
2959 * \return the temporary RSA key.
2960 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2961 */
2962
2963RSA *cb(SSL *ssl,int is_export,int keylength)
2964 {}
2965#endif
2966
2967/*!
2968 * \brief Set the callback for generating temporary DH keys.
2969 * \param ctx the SSL context.
2970 * \param dh the callback
2971 */
2972
2973#ifndef OPENSSL_NO_DH
2974void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2975 int keylength))
2976 {
2977 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2978 }
2979
2980void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2981 int keylength))
2982 {
2983 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2984 }
2985#endif
2986
2987#ifndef OPENSSL_NO_ECDH
2988void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2989 int keylength))
2990 {
2991 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2992 }
2993
2994void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2995 int keylength))
2996 {
2997 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2998 }
2999#endif
3000
Adam Langley95c29f32014-06-20 12:00:00 -07003001int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
3002 {
3003 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
3004 {
3005 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
3006 return 0;
3007 }
3008 if (ctx->psk_identity_hint != NULL)
3009 OPENSSL_free(ctx->psk_identity_hint);
3010 if (identity_hint != NULL)
3011 {
3012 ctx->psk_identity_hint = BUF_strdup(identity_hint);
3013 if (ctx->psk_identity_hint == NULL)
3014 return 0;
3015 }
3016 else
3017 ctx->psk_identity_hint = NULL;
3018 return 1;
3019 }
3020
3021int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
3022 {
3023 if (s == NULL)
3024 return 0;
3025
Adam Langley95c29f32014-06-20 12:00:00 -07003026 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
3027 {
3028 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
3029 return 0;
3030 }
Adam Langley0289c732014-06-20 12:00:00 -07003031
3032 /* Clear hint in SSL and associated SSL_SESSION (if any). */
3033 if (s->psk_identity_hint != NULL)
3034 {
3035 OPENSSL_free(s->psk_identity_hint);
3036 s->psk_identity_hint = NULL;
3037 }
3038 if (s->session != NULL && s->session->psk_identity_hint != NULL)
3039 {
Adam Langley95c29f32014-06-20 12:00:00 -07003040 OPENSSL_free(s->session->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -07003041 s->session->psk_identity_hint = NULL;
3042 }
3043
Adam Langley95c29f32014-06-20 12:00:00 -07003044 if (identity_hint != NULL)
3045 {
Adam Langley0289c732014-06-20 12:00:00 -07003046 /* The hint is stored in SSL and SSL_SESSION with the one in
3047 * SSL_SESSION taking precedence. Thus, if SSL_SESSION is avaiable,
3048 * we store the hint there, otherwise we store it in SSL. */
3049 if (s->session != NULL)
3050 {
3051 s->session->psk_identity_hint = BUF_strdup(identity_hint);
3052 if (s->session->psk_identity_hint == NULL)
3053 return 0;
3054 }
3055 else
3056 {
3057 s->psk_identity_hint = BUF_strdup(identity_hint);
3058 if (s->psk_identity_hint == NULL)
3059 return 0;
3060 }
Adam Langley95c29f32014-06-20 12:00:00 -07003061 }
Adam Langley95c29f32014-06-20 12:00:00 -07003062 return 1;
3063 }
3064
3065const char *SSL_get_psk_identity_hint(const SSL *s)
3066 {
Adam Langley0289c732014-06-20 12:00:00 -07003067 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07003068 return NULL;
Adam Langley0289c732014-06-20 12:00:00 -07003069 /* The hint is stored in SSL and SSL_SESSION with the one in SSL_SESSION
3070 * taking precedence. */
3071 if (s->session != NULL)
3072 return(s->session->psk_identity_hint);
3073 return(s->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07003074 }
3075
3076const char *SSL_get_psk_identity(const SSL *s)
3077 {
3078 if (s == NULL || s->session == NULL)
3079 return NULL;
3080 return(s->session->psk_identity);
3081 }
3082
3083void SSL_set_psk_client_callback(SSL *s,
3084 unsigned int (*cb)(SSL *ssl, const char *hint,
3085 char *identity, unsigned int max_identity_len, unsigned char *psk,
3086 unsigned int max_psk_len))
3087 {
3088 s->psk_client_callback = cb;
3089 }
3090
3091void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
3092 unsigned int (*cb)(SSL *ssl, const char *hint,
3093 char *identity, unsigned int max_identity_len, unsigned char *psk,
3094 unsigned int max_psk_len))
3095 {
3096 ctx->psk_client_callback = cb;
3097 }
3098
3099void SSL_set_psk_server_callback(SSL *s,
3100 unsigned int (*cb)(SSL *ssl, const char *identity,
3101 unsigned char *psk, unsigned int max_psk_len))
3102 {
3103 s->psk_server_callback = cb;
3104 }
3105
3106void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
3107 unsigned int (*cb)(SSL *ssl, const char *identity,
3108 unsigned char *psk, unsigned int max_psk_len))
3109 {
3110 ctx->psk_server_callback = cb;
3111 }
Adam Langley95c29f32014-06-20 12:00:00 -07003112
3113void 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))
3114 {
3115 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3116 }
3117void 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))
3118 {
3119 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3120 }
3121
Adam Langleyadb739e2014-06-20 12:00:00 -07003122int SSL_cutthrough_complete(const SSL *s)
3123 {
3124 return (!s->server && /* cutthrough only applies to clients */
3125 !s->hit && /* full-handshake */
3126 s->version >= SSL3_VERSION &&
3127 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3128 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3129 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3130 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3131 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
David Benjamin7e3305e2014-07-28 14:52:32 -04003132 s->state == SSL3_ST_CR_CHANGE ||
Adam Langleyadb739e2014-06-20 12:00:00 -07003133 s->state == SSL3_ST_CR_FINISHED_A));
3134 }
3135
Adam Langley95f22882014-06-20 12:00:00 -07003136void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3137 size_t* ssl_session_size)
3138{
3139 *ssl_size = sizeof(SSL);
3140 *ssl_ctx_size = sizeof(SSL_CTX);
3141 *ssl_session_size = sizeof(SSL_SESSION);
3142}
3143
Adam Langleyadb739e2014-06-20 12:00:00 -07003144int ssl3_can_cutthrough(const SSL *s)
3145 {
3146 const SSL_CIPHER *c;
3147
3148 /* require a strong enough cipher */
3149 if (SSL_get_cipher_bits(s, NULL) < 128)
3150 return 0;
3151
3152 /* require ALPN or NPN extension */
3153 if (!s->s3->alpn_selected
3154#ifndef OPENSSL_NO_NEXTPROTONEG
3155 && !s->s3->next_proto_neg_seen
3156#endif
3157 )
3158 {
3159 return 0;
3160 }
3161
3162 /* require a forward-secret cipher */
3163 c = SSL_get_current_cipher(s);
3164 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3165 c->algorithm_mkey != SSL_kEECDH))
3166 {
3167 return 0;
3168 }
3169
3170 return 1;
3171 }
3172
Adam Langley29707792014-06-20 12:00:00 -07003173/* ssl_get_max_version returns the maximum SSL/TLS version number supported by
3174 * |s|, or zero if all versions are disabled. */
3175int ssl_get_max_version(const SSL *s)
3176 {
3177 /* Only one version supported for DTLS. */
3178 if (s->version == DTLS1_VERSION)
3179 return DTLS1_VERSION;
3180
3181 if (!(s->options & SSL_OP_NO_TLSv1_2))
3182 return TLS1_2_VERSION;
3183 if (!(s->options & SSL_OP_NO_TLSv1_1))
3184 return TLS1_1_VERSION;
3185 if (!(s->options & SSL_OP_NO_TLSv1))
3186 return TLS1_VERSION;
3187 if (!(s->options & SSL_OP_NO_SSLv3))
3188 return SSL3_VERSION;
3189 if (!(s->options & SSL_OP_NO_SSLv2))
3190 return SSL2_VERSION;
3191 return 0;
3192 }
3193
Adam Langley95c29f32014-06-20 12:00:00 -07003194/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3195 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3196 * any. If EVP_MD pointer is passed, initializes ctx with this md
3197 * Returns newly allocated ctx;
3198 */
3199
3200EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3201{
3202 ssl_clear_hash_ctx(hash);
3203 *hash = EVP_MD_CTX_create();
3204 if (md) EVP_DigestInit_ex(*hash,md,NULL);
3205 return *hash;
3206}
3207void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3208{
3209
3210 if (*hash) EVP_MD_CTX_destroy(*hash);
3211 *hash=NULL;
3212}
3213
3214void SSL_set_debug(SSL *s, int debug)
3215 {
3216 s->debug = debug;
3217 }
3218
3219int SSL_cache_hit(SSL *s)
3220 {
3221 return s->hit;
3222 }
3223
3224int SSL_is_server(SSL *s)
3225 {
3226 return s->server;
3227 }