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