blob: 85e33e76c8128777c220dae92a98d80375267c8e [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
144#include <openssl/dh.h>
145#include <openssl/engine.h>
146#include <openssl/lhash.h>
147#include <openssl/mem.h>
148#include <openssl/obj.h>
149#include <openssl/rand.h>
150#include <openssl/x509v3.h>
151
152#include "ssl_locl.h"
153
154SSL3_ENC_METHOD ssl3_undef_enc_method={
155 /* evil casts, but these functions are only called if there's a library bug */
156 (int (*)(SSL *,int))ssl_undefined_function,
157 (int (*)(SSL *, unsigned char *, int))ssl_undefined_function,
158 ssl_undefined_function,
159 (int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function,
160 (int (*)(SSL*, int))ssl_undefined_function,
161 (int (*)(SSL *, const char*, int, unsigned char *))ssl_undefined_function,
162 0, /* finish_mac_length */
163 (int (*)(SSL *, int, unsigned char *))ssl_undefined_function,
164 NULL, /* client_finished_label */
165 0, /* client_finished_label_len */
166 NULL, /* server_finished_label */
167 0, /* server_finished_label_len */
168 (int (*)(int))ssl_undefined_function,
169 (int (*)(SSL *, unsigned char *, size_t, const char *,
170 size_t, const unsigned char *, size_t,
171 int use_context)) ssl_undefined_function,
172 };
173
174int SSL_clear(SSL *s)
175 {
176
177 if (s->method == NULL)
178 {
179 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
180 return(0);
181 }
182
183 if (ssl_clear_bad_session(s))
184 {
185 SSL_SESSION_free(s->session);
186 s->session=NULL;
187 }
188
189 s->error=0;
190 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
253 sk=ssl_create_cipher_list(ctx->method,&(ctx->cipher_list),
254 &(ctx->cipher_list_by_id),
255 meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST, ctx->cert);
256 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
257 {
258 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_ssl_version, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
259 return(0);
260 }
261 return(1);
262 }
263
264SSL *SSL_new(SSL_CTX *ctx)
265 {
266 SSL *s;
267
268 if (ctx == NULL)
269 {
270 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
271 return(NULL);
272 }
273 if (ctx->method == NULL)
274 {
275 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
276 return(NULL);
277 }
278
279 s=(SSL *)OPENSSL_malloc(sizeof(SSL));
280 if (s == NULL) goto err;
281 memset(s,0,sizeof(SSL));
282
283 s->options=ctx->options;
284 s->mode=ctx->mode;
285 s->max_cert_list=ctx->max_cert_list;
286
287 if (ctx->cert != NULL)
288 {
289 /* Earlier library versions used to copy the pointer to
290 * the CERT, not its contents; only when setting new
291 * parameters for the per-SSL copy, ssl_cert_new would be
292 * called (and the direct reference to the per-SSL_CTX
293 * settings would be lost, but those still were indirectly
294 * accessed for various purposes, and for that reason they
295 * used to be known as s->ctx->default_cert).
296 * Now we don't look at the SSL_CTX's CERT after having
297 * duplicated it once. */
298
299 s->cert = ssl_cert_dup(ctx->cert);
300 if (s->cert == NULL)
301 goto err;
302 }
303 else
304 s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
305
306 s->read_ahead=ctx->read_ahead;
307 s->msg_callback=ctx->msg_callback;
308 s->msg_callback_arg=ctx->msg_callback_arg;
309 s->verify_mode=ctx->verify_mode;
310#if 0
311 s->verify_depth=ctx->verify_depth;
312#endif
313 s->sid_ctx_length=ctx->sid_ctx_length;
314 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
315 memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
316 s->verify_callback=ctx->default_verify_callback;
317 s->generate_session_id=ctx->generate_session_id;
318
319 s->param = X509_VERIFY_PARAM_new();
320 if (!s->param)
321 goto err;
322 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
323#if 0
324 s->purpose = ctx->purpose;
325 s->trust = ctx->trust;
326#endif
327 s->quiet_shutdown=ctx->quiet_shutdown;
328 s->max_send_fragment = ctx->max_send_fragment;
329
330 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
331 s->ctx=ctx;
332#ifndef OPENSSL_NO_TLSEXT
333 s->tlsext_debug_cb = 0;
334 s->tlsext_debug_arg = NULL;
335 s->tlsext_ticket_expected = 0;
336 s->tlsext_status_type = -1;
337 s->tlsext_status_expected = 0;
338 s->tlsext_ocsp_ids = NULL;
339 s->tlsext_ocsp_exts = NULL;
340 s->tlsext_ocsp_resp = NULL;
341 s->tlsext_ocsp_resplen = -1;
342 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
343 s->initial_ctx=ctx;
344#ifndef OPENSSL_NO_EC
345 if (ctx->tlsext_ecpointformatlist)
346 {
347 s->tlsext_ecpointformatlist =
348 BUF_memdup(ctx->tlsext_ecpointformatlist,
349 ctx->tlsext_ecpointformatlist_length);
350 if (!s->tlsext_ecpointformatlist)
351 goto err;
352 s->tlsext_ecpointformatlist_length =
353 ctx->tlsext_ecpointformatlist_length;
354 }
355 if (ctx->tlsext_ellipticcurvelist)
356 {
357 s->tlsext_ellipticcurvelist =
358 BUF_memdup(ctx->tlsext_ellipticcurvelist,
359 ctx->tlsext_ellipticcurvelist_length);
360 if (!s->tlsext_ellipticcurvelist)
361 goto err;
362 s->tlsext_ellipticcurvelist_length =
363 ctx->tlsext_ellipticcurvelist_length;
364 }
365#endif
366# ifndef OPENSSL_NO_NEXTPROTONEG
367 s->next_proto_negotiated = NULL;
368# endif
369
370 if (s->ctx->alpn_client_proto_list)
371 {
372 s->alpn_client_proto_list =
373 OPENSSL_malloc(s->ctx->alpn_client_proto_list_len);
374 if (s->alpn_client_proto_list == NULL)
375 goto err;
376 memcpy(s->alpn_client_proto_list, s->ctx->alpn_client_proto_list,
377 s->ctx->alpn_client_proto_list_len);
378 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
379 }
380#endif
381
382 s->verify_result=X509_V_OK;
383
384 s->method=ctx->method;
385
386 if (!s->method->ssl_new(s))
387 goto err;
388
389 s->references=1;
390 s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
391
392 SSL_clear(s);
393
394 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
395
396#ifndef OPENSSL_NO_PSK
397 s->psk_client_callback=ctx->psk_client_callback;
398 s->psk_server_callback=ctx->psk_server_callback;
399#endif
400
401 return(s);
402err:
403 if (s != NULL)
404 {
405 if (s->cert != NULL)
406 ssl_cert_free(s->cert);
407 if (s->ctx != NULL)
408 SSL_CTX_free(s->ctx); /* decrement reference count */
409 OPENSSL_free(s);
410 }
411 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
412 return(NULL);
413 }
414
415int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
416 unsigned int sid_ctx_len)
417 {
418 if(sid_ctx_len > sizeof ctx->sid_ctx)
419 {
420 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
421 return 0;
422 }
423 ctx->sid_ctx_length=sid_ctx_len;
424 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
425
426 return 1;
427 }
428
429int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
430 unsigned int sid_ctx_len)
431 {
432 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
433 {
434 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
435 return 0;
436 }
437 ssl->sid_ctx_length=sid_ctx_len;
438 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
439
440 return 1;
441 }
442
443int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
444 {
445 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
446 ctx->generate_session_id = cb;
447 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
448 return 1;
449 }
450
451int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
452 {
453 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
454 ssl->generate_session_id = cb;
455 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
456 return 1;
457 }
458
459int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
460 unsigned int id_len)
461 {
462 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
463 * we can "construct" a session to give us the desired check - ie. to
464 * find if there's a session in the hash table that would conflict with
465 * any new session built out of this id/id_len and the ssl_version in
466 * use by this SSL. */
467 SSL_SESSION r, *p;
468
469 if(id_len > sizeof r.session_id)
470 return 0;
471
472 r.ssl_version = ssl->version;
473 r.session_id_length = id_len;
474 memcpy(r.session_id, id, id_len);
475 /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
476 * callback is calling us to check the uniqueness of a shorter ID, it
477 * must be compared as a padded-out ID because that is what it will be
478 * converted to when the callback has finished choosing it. */
479 if((r.ssl_version == SSL2_VERSION) &&
480 (id_len < SSL2_SSL_SESSION_ID_LENGTH))
481 {
482 memset(r.session_id + id_len, 0,
483 SSL2_SSL_SESSION_ID_LENGTH - id_len);
484 r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
485 }
486
487 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
488 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
489 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
490 return (p != NULL);
491 }
492
493int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
494 {
495 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
496 }
497
498int SSL_set_purpose(SSL *s, int purpose)
499 {
500 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
501 }
502
503int SSL_CTX_set_trust(SSL_CTX *s, int trust)
504 {
505 return X509_VERIFY_PARAM_set_trust(s->param, trust);
506 }
507
508int SSL_set_trust(SSL *s, int trust)
509 {
510 return X509_VERIFY_PARAM_set_trust(s->param, trust);
511 }
512
513int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
514 {
515 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
516 }
517
518int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
519 {
520 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
521 }
522
523X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
524 {
525 return ctx->param;
526 }
527
528X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
529 {
530 return ssl->param;
531 }
532
533void SSL_certs_clear(SSL *s)
534 {
535 ssl_cert_clear_certs(s->cert);
536 }
537
538void SSL_free(SSL *s)
539 {
540 int i;
541
542 if(s == NULL)
543 return;
544
545 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
546#ifdef REF_PRINT
547 REF_PRINT("SSL",s);
548#endif
549 if (i > 0) return;
550#ifdef REF_CHECK
551 if (i < 0)
552 {
553 fprintf(stderr,"SSL_free, bad reference count\n");
554 abort(); /* ok */
555 }
556#endif
557
558 if (s->param)
559 X509_VERIFY_PARAM_free(s->param);
560
561 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
562
563 if (s->bbio != NULL)
564 {
565 /* If the buffering BIO is in place, pop it off */
566 if (s->bbio == s->wbio)
567 {
568 s->wbio=BIO_pop(s->wbio);
569 }
570 BIO_free(s->bbio);
571 s->bbio=NULL;
572 }
573 if (s->rbio != NULL)
574 BIO_free_all(s->rbio);
575 if ((s->wbio != NULL) && (s->wbio != s->rbio))
576 BIO_free_all(s->wbio);
577
578 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
579
580 /* add extra stuff */
581 if (s->cipher_list != NULL) sk_SSL_CIPHER_free(s->cipher_list);
582 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
583
584 /* Make the next call work :-) */
585 if (s->session != NULL)
586 {
587 ssl_clear_bad_session(s);
588 SSL_SESSION_free(s->session);
589 }
590
591 ssl_clear_cipher_ctx(s);
592 ssl_clear_hash_ctx(&s->read_hash);
593 ssl_clear_hash_ctx(&s->write_hash);
594
595 if (s->cert != NULL) ssl_cert_free(s->cert);
596 /* Free up if allocated */
597
598#ifndef OPENSSL_NO_TLSEXT
599 if (s->tlsext_hostname)
600 OPENSSL_free(s->tlsext_hostname);
601 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
602#ifndef OPENSSL_NO_EC
603 if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
604 if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
605#endif /* OPENSSL_NO_EC */
606 if (s->tlsext_opaque_prf_input) OPENSSL_free(s->tlsext_opaque_prf_input);
607 if (s->tlsext_ocsp_exts)
608 sk_X509_EXTENSION_pop_free(s->tlsext_ocsp_exts,
609 X509_EXTENSION_free);
610 /* TODO(fork): OCSP support */
611#if 0
612 if (s->tlsext_ocsp_ids)
613 sk_OCSP_RESPID_pop_free(s->tlsext_ocsp_ids, OCSP_RESPID_free);
614#endif
615 if (s->tlsext_ocsp_resp)
616 OPENSSL_free(s->tlsext_ocsp_resp);
617 if (s->alpn_client_proto_list)
618 OPENSSL_free(s->alpn_client_proto_list);
Adam Langley1258b6a2014-06-20 12:00:00 -0700619 if (s->tlsext_channel_id_private)
620 EVP_PKEY_free(s->tlsext_channel_id_private);
Adam Langley95c29f32014-06-20 12:00:00 -0700621#endif
622
623 if (s->client_CA != NULL)
624 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
625
626 if (s->method != NULL) s->method->ssl_free(s);
627
628 if (s->ctx) SSL_CTX_free(s->ctx);
629
630#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
631 if (s->next_proto_negotiated)
632 OPENSSL_free(s->next_proto_negotiated);
633#endif
634
635 if (s->srtp_profiles)
636 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
637
638#ifndef OPENSSL_NO_DANE
639 if (s->tlsa_record && s->tlsa_record!=(void *)-1)
640 OPENSSL_free(s->tlsa_record);
641#endif
642
643 OPENSSL_free(s);
644 }
645
646void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
647 {
648 /* If the output buffering BIO is still in place, remove it
649 */
650 if (s->bbio != NULL)
651 {
652 if (s->wbio == s->bbio)
653 {
654 s->wbio=s->wbio->next_bio;
655 s->bbio->next_bio=NULL;
656 }
657 }
658 if ((s->rbio != NULL) && (s->rbio != rbio))
659 BIO_free_all(s->rbio);
660 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
661 BIO_free_all(s->wbio);
662 s->rbio=rbio;
663 s->wbio=wbio;
664 }
665
666BIO *SSL_get_rbio(const SSL *s)
667 { return(s->rbio); }
668
669BIO *SSL_get_wbio(const SSL *s)
670 { return(s->wbio); }
671
672int SSL_get_fd(const SSL *s)
673 {
674 return(SSL_get_rfd(s));
675 }
676
677int SSL_get_rfd(const SSL *s)
678 {
679 int ret= -1;
680 BIO *b,*r;
681
682 b=SSL_get_rbio(s);
683 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
684 if (r != NULL)
685 BIO_get_fd(r,&ret);
686 return(ret);
687 }
688
689int SSL_get_wfd(const SSL *s)
690 {
691 int ret= -1;
692 BIO *b,*r;
693
694 b=SSL_get_wbio(s);
695 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
696 if (r != NULL)
697 BIO_get_fd(r,&ret);
698 return(ret);
699 }
700
701#ifndef OPENSSL_NO_SOCK
702int SSL_set_fd(SSL *s,int fd)
703 {
704 int ret=0;
705 BIO *bio=NULL;
706
707 bio=BIO_new(BIO_s_fd());
708
709 if (bio == NULL)
710 {
711 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
712 goto err;
713 }
714 BIO_set_fd(bio,fd,BIO_NOCLOSE);
715 SSL_set_bio(s,bio,bio);
716 ret=1;
717err:
718 return(ret);
719 }
720
721int SSL_set_wfd(SSL *s,int fd)
722 {
723 int ret=0;
724 BIO *bio=NULL;
725
726 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_FD)
727 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
728 {
729 bio=BIO_new(BIO_s_fd());
730
731 if (bio == NULL)
732 {
733 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
734 goto err;
735 }
736 BIO_set_fd(bio,fd,BIO_NOCLOSE);
737 SSL_set_bio(s,SSL_get_rbio(s),bio);
738 }
739 else
740 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
741 ret=1;
742err:
743 return(ret);
744 }
745
746int SSL_set_rfd(SSL *s,int fd)
747 {
748 int ret=0;
749 BIO *bio=NULL;
750
751 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_FD)
752 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
753 {
754 bio=BIO_new(BIO_s_fd());
755
756 if (bio == NULL)
757 {
758 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
759 goto err;
760 }
761 BIO_set_fd(bio,fd,BIO_NOCLOSE);
762 SSL_set_bio(s,bio,SSL_get_wbio(s));
763 }
764 else
765 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
766 ret=1;
767err:
768 return(ret);
769 }
770#endif
771
772
773/* return length of latest Finished message we sent, copy to 'buf' */
774size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
775 {
776 size_t ret = 0;
777
778 if (s->s3 != NULL)
779 {
780 ret = s->s3->tmp.finish_md_len;
781 if (count > ret)
782 count = ret;
783 memcpy(buf, s->s3->tmp.finish_md, count);
784 }
785 return ret;
786 }
787
788/* return length of latest Finished message we expected, copy to 'buf' */
789size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
790 {
791 size_t ret = 0;
792
793 if (s->s3 != NULL)
794 {
795 ret = s->s3->tmp.peer_finish_md_len;
796 if (count > ret)
797 count = ret;
798 memcpy(buf, s->s3->tmp.peer_finish_md, count);
799 }
800 return ret;
801 }
802
803
804int SSL_get_verify_mode(const SSL *s)
805 {
806 return(s->verify_mode);
807 }
808
809int SSL_get_verify_depth(const SSL *s)
810 {
811 return X509_VERIFY_PARAM_get_depth(s->param);
812 }
813
814int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
815 {
816 return(s->verify_callback);
817 }
818
819int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
820 {
821 return(ctx->verify_mode);
822 }
823
824int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
825 {
826 return X509_VERIFY_PARAM_get_depth(ctx->param);
827 }
828
829int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
830 {
831 return(ctx->default_verify_callback);
832 }
833
834void SSL_set_verify(SSL *s,int mode,
835 int (*callback)(int ok,X509_STORE_CTX *ctx))
836 {
837 s->verify_mode=mode;
838 if (callback != NULL)
839 s->verify_callback=callback;
840 }
841
842void SSL_set_verify_depth(SSL *s,int depth)
843 {
844 X509_VERIFY_PARAM_set_depth(s->param, depth);
845 }
846
847void SSL_set_read_ahead(SSL *s,int yes)
848 {
849 s->read_ahead=yes;
850 }
851
852int SSL_get_read_ahead(const SSL *s)
853 {
854 return(s->read_ahead);
855 }
856
857int SSL_pending(const SSL *s)
858 {
859 /* SSL_pending cannot work properly if read-ahead is enabled
860 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
861 * and it is impossible to fix since SSL_pending cannot report
862 * errors that may be observed while scanning the new data.
863 * (Note that SSL_pending() is often used as a boolean value,
864 * so we'd better not return -1.)
865 */
866 return(s->method->ssl_pending(s));
867 }
868
869X509 *SSL_get_peer_certificate(const SSL *s)
870 {
871 X509 *r;
872
873 if ((s == NULL) || (s->session == NULL))
874 r=NULL;
875 else
876 r=s->session->peer;
877
878 if (r == NULL) return(r);
879
880 CRYPTO_add(&r->references,1,CRYPTO_LOCK_X509);
881
882 return(r);
883 }
884
885STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
886 {
887 STACK_OF(X509) *r;
888
889 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
890 r=NULL;
891 else
892 r=s->session->sess_cert->cert_chain;
893
894 /* If we are a client, cert_chain includes the peer's own
895 * certificate; if we are a server, it does not. */
896
897 return(r);
898 }
899
900/* Now in theory, since the calling process own 't' it should be safe to
901 * modify. We need to be able to read f without being hassled */
902void SSL_copy_session_id(SSL *t,const SSL *f)
903 {
904 CERT *tmp;
905
906 /* Do we need to to SSL locking? */
907 SSL_set_session(t,SSL_get_session(f));
908
909 /* what if we are setup as SSLv2 but want to talk SSLv3 or
910 * vice-versa */
911 if (t->method != f->method)
912 {
913 t->method->ssl_free(t); /* cleanup current */
914 t->method=f->method; /* change method */
915 t->method->ssl_new(t); /* setup new */
916 }
917
918 tmp=t->cert;
919 if (f->cert != NULL)
920 {
921 CRYPTO_add(&f->cert->references,1,CRYPTO_LOCK_SSL_CERT);
922 t->cert=f->cert;
923 }
924 else
925 t->cert=NULL;
926 if (tmp != NULL) ssl_cert_free(tmp);
927 SSL_set_session_id_context(t,f->sid_ctx,f->sid_ctx_length);
928 }
929
930/* Fix this so it checks all the valid key/cert options */
931int SSL_CTX_check_private_key(const SSL_CTX *ctx)
932 {
933 if ( (ctx == NULL) ||
934 (ctx->cert == NULL) ||
935 (ctx->cert->key->x509 == NULL))
936 {
937 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
938 return(0);
939 }
940 if (ctx->cert->key->privatekey == NULL)
941 {
942 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
943 return(0);
944 }
945 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
946 }
947
948/* Fix this function so that it takes an optional type parameter */
949int SSL_check_private_key(const SSL *ssl)
950 {
951 if (ssl == NULL)
952 {
953 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
954 return(0);
955 }
956 if (ssl->cert == NULL)
957 {
958 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
959 return 0;
960 }
961 if (ssl->cert->key->x509 == NULL)
962 {
963 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
964 return(0);
965 }
966 if (ssl->cert->key->privatekey == NULL)
967 {
968 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
969 return(0);
970 }
971 return(X509_check_private_key(ssl->cert->key->x509,
972 ssl->cert->key->privatekey));
973 }
974
975int SSL_accept(SSL *s)
976 {
977 if (s->handshake_func == 0)
978 /* Not properly initialized yet */
979 SSL_set_accept_state(s);
980
981 return(s->method->ssl_accept(s));
982 }
983
984int SSL_connect(SSL *s)
985 {
986 if (s->handshake_func == 0)
987 /* Not properly initialized yet */
988 SSL_set_connect_state(s);
989
990 return(s->method->ssl_connect(s));
991 }
992
993long SSL_get_default_timeout(const SSL *s)
994 {
995 return(s->method->get_timeout());
996 }
997
998int SSL_read(SSL *s,void *buf,int num)
999 {
1000 if (s->handshake_func == 0)
1001 {
1002 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
1003 return -1;
1004 }
1005
1006 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1007 {
1008 s->rwstate=SSL_NOTHING;
1009 return(0);
1010 }
1011 return(s->method->ssl_read(s,buf,num));
1012 }
1013
1014int SSL_peek(SSL *s,void *buf,int num)
1015 {
1016 if (s->handshake_func == 0)
1017 {
1018 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
1019 return -1;
1020 }
1021
1022 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1023 {
1024 return(0);
1025 }
1026 return(s->method->ssl_peek(s,buf,num));
1027 }
1028
1029int SSL_write(SSL *s,const void *buf,int num)
1030 {
1031 if (s->handshake_func == 0)
1032 {
1033 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
1034 return -1;
1035 }
1036
1037 if (s->shutdown & SSL_SENT_SHUTDOWN)
1038 {
1039 s->rwstate=SSL_NOTHING;
1040 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
1041 return(-1);
1042 }
1043 return(s->method->ssl_write(s,buf,num));
1044 }
1045
1046int SSL_shutdown(SSL *s)
1047 {
1048 /* Note that this function behaves differently from what one might
1049 * expect. Return values are 0 for no success (yet),
1050 * 1 for success; but calling it once is usually not enough,
1051 * even if blocking I/O is used (see ssl3_shutdown).
1052 */
1053
1054 if (s->handshake_func == 0)
1055 {
1056 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1057 return -1;
1058 }
1059
1060 if ((s != NULL) && !SSL_in_init(s))
1061 return(s->method->ssl_shutdown(s));
1062 else
1063 return(1);
1064 }
1065
1066int SSL_renegotiate(SSL *s)
1067 {
1068 if (s->renegotiate == 0)
1069 s->renegotiate=1;
1070
1071 s->new_session=1;
1072
1073 return(s->method->ssl_renegotiate(s));
1074 }
1075
1076int SSL_renegotiate_abbreviated(SSL *s)
1077 {
1078 if (s->renegotiate == 0)
1079 s->renegotiate=1;
1080
1081 s->new_session=0;
1082
1083 return(s->method->ssl_renegotiate(s));
1084 }
1085
1086int SSL_renegotiate_pending(SSL *s)
1087 {
1088 /* becomes true when negotiation is requested;
1089 * false again once a handshake has finished */
1090 return (s->renegotiate != 0);
1091 }
1092
1093long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1094 {
1095 long l;
1096
1097 switch (cmd)
1098 {
1099 case SSL_CTRL_GET_READ_AHEAD:
1100 return(s->read_ahead);
1101 case SSL_CTRL_SET_READ_AHEAD:
1102 l=s->read_ahead;
1103 s->read_ahead=larg;
1104 return(l);
1105
1106 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1107 s->msg_callback_arg = parg;
1108 return 1;
1109
1110 case SSL_CTRL_OPTIONS:
1111 return(s->options|=larg);
1112 case SSL_CTRL_CLEAR_OPTIONS:
1113 return(s->options&=~larg);
1114 case SSL_CTRL_MODE:
1115 return(s->mode|=larg);
1116 case SSL_CTRL_CLEAR_MODE:
1117 return(s->mode &=~larg);
1118 case SSL_CTRL_GET_MAX_CERT_LIST:
1119 return(s->max_cert_list);
1120 case SSL_CTRL_SET_MAX_CERT_LIST:
1121 l=s->max_cert_list;
1122 s->max_cert_list=larg;
1123 return(l);
1124 case SSL_CTRL_SET_MTU:
1125#ifndef OPENSSL_NO_DTLS1
1126 if (larg < (long)dtls1_min_mtu())
1127 return 0;
1128#endif
1129
1130 if (SSL_IS_DTLS(s))
1131 {
1132 s->d1->mtu = larg;
1133 return larg;
1134 }
1135 return 0;
1136 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1137 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1138 return 0;
1139 s->max_send_fragment = larg;
1140 return 1;
1141 case SSL_CTRL_GET_RI_SUPPORT:
1142 if (s->s3)
1143 return s->s3->send_connection_binding;
1144 else return 0;
1145 case SSL_CTRL_CERT_FLAGS:
1146 return(s->cert->cert_flags|=larg);
1147 case SSL_CTRL_CLEAR_CERT_FLAGS:
1148 return(s->cert->cert_flags &=~larg);
1149
1150 case SSL_CTRL_GET_RAW_CIPHERLIST:
1151 if (parg)
1152 {
1153 if (s->cert->ciphers_raw == NULL)
1154 return 0;
1155 *(unsigned char **)parg = s->cert->ciphers_raw;
1156 return (int)s->cert->ciphers_rawlen;
1157 }
1158 else
1159 return ssl_put_cipher_by_char(s,NULL,NULL);
1160 default:
1161 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1162 }
1163 }
1164
1165long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1166 {
1167 switch(cmd)
1168 {
1169 case SSL_CTRL_SET_MSG_CALLBACK:
1170 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1171 return 1;
1172
1173 default:
1174 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1175 }
1176 }
1177
1178LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1179 {
1180 return ctx->sessions;
1181 }
1182
1183long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1184 {
1185 long l;
1186
1187 switch (cmd)
1188 {
1189 case SSL_CTRL_GET_READ_AHEAD:
1190 return(ctx->read_ahead);
1191 case SSL_CTRL_SET_READ_AHEAD:
1192 l=ctx->read_ahead;
1193 ctx->read_ahead=larg;
1194 return(l);
1195
1196 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1197 ctx->msg_callback_arg = parg;
1198 return 1;
1199
1200 case SSL_CTRL_GET_MAX_CERT_LIST:
1201 return(ctx->max_cert_list);
1202 case SSL_CTRL_SET_MAX_CERT_LIST:
1203 l=ctx->max_cert_list;
1204 ctx->max_cert_list=larg;
1205 return(l);
1206
1207 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1208 l=ctx->session_cache_size;
1209 ctx->session_cache_size=larg;
1210 return(l);
1211 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1212 return(ctx->session_cache_size);
1213 case SSL_CTRL_SET_SESS_CACHE_MODE:
1214 l=ctx->session_cache_mode;
1215 ctx->session_cache_mode=larg;
1216 return(l);
1217 case SSL_CTRL_GET_SESS_CACHE_MODE:
1218 return(ctx->session_cache_mode);
1219
1220 case SSL_CTRL_SESS_NUMBER:
1221 return(lh_SSL_SESSION_num_items(ctx->sessions));
1222 case SSL_CTRL_SESS_CONNECT:
1223 return(ctx->stats.sess_connect);
1224 case SSL_CTRL_SESS_CONNECT_GOOD:
1225 return(ctx->stats.sess_connect_good);
1226 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1227 return(ctx->stats.sess_connect_renegotiate);
1228 case SSL_CTRL_SESS_ACCEPT:
1229 return(ctx->stats.sess_accept);
1230 case SSL_CTRL_SESS_ACCEPT_GOOD:
1231 return(ctx->stats.sess_accept_good);
1232 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1233 return(ctx->stats.sess_accept_renegotiate);
1234 case SSL_CTRL_SESS_HIT:
1235 return(ctx->stats.sess_hit);
1236 case SSL_CTRL_SESS_CB_HIT:
1237 return(ctx->stats.sess_cb_hit);
1238 case SSL_CTRL_SESS_MISSES:
1239 return(ctx->stats.sess_miss);
1240 case SSL_CTRL_SESS_TIMEOUTS:
1241 return(ctx->stats.sess_timeout);
1242 case SSL_CTRL_SESS_CACHE_FULL:
1243 return(ctx->stats.sess_cache_full);
1244 case SSL_CTRL_OPTIONS:
1245 return(ctx->options|=larg);
1246 case SSL_CTRL_CLEAR_OPTIONS:
1247 return(ctx->options&=~larg);
1248 case SSL_CTRL_MODE:
1249 return(ctx->mode|=larg);
1250 case SSL_CTRL_CLEAR_MODE:
1251 return(ctx->mode&=~larg);
1252 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1253 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1254 return 0;
1255 ctx->max_send_fragment = larg;
1256 return 1;
1257 case SSL_CTRL_CERT_FLAGS:
1258 return(ctx->cert->cert_flags|=larg);
1259 case SSL_CTRL_CLEAR_CERT_FLAGS:
1260 return(ctx->cert->cert_flags &=~larg);
1261 default:
1262 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1263 }
1264 }
1265
1266long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1267 {
1268 switch(cmd)
1269 {
1270 case SSL_CTRL_SET_MSG_CALLBACK:
1271 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1272 return 1;
1273
1274 default:
1275 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1276 }
1277 }
1278
1279int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1280 {
1281 long l;
1282 const SSL_CIPHER *a = in_a;
1283 const SSL_CIPHER *b = in_b;
1284 const long a_id = a->id;
1285 const long b_id = b->id;
1286
1287 l = a_id - b_id;
1288 if (l == 0L)
1289 return(0);
1290 else
1291 return((l > 0)?1:-1);
1292 }
1293
1294int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1295 {
1296 long l;
1297 const long a_id = (*ap)->id;
1298 const long b_id = (*bp)->id;
1299
1300 l = a_id - b_id;
1301 if (l == 0)
1302 return(0);
1303 else
1304 return((l > 0)?1:-1);
1305 }
1306
1307/** return a STACK of the ciphers available for the SSL and in order of
1308 * preference */
1309STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1310 {
1311 if (s != NULL)
1312 {
1313 if (s->cipher_list != NULL)
1314 {
1315 return(s->cipher_list);
1316 }
1317 else if ((s->ctx != NULL) &&
1318 (s->ctx->cipher_list != NULL))
1319 {
1320 return(s->ctx->cipher_list);
1321 }
1322 }
1323 return(NULL);
1324 }
1325
1326/** return a STACK of the ciphers available for the SSL and in order of
1327 * algorithm id */
1328STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1329 {
1330 if (s != NULL)
1331 {
1332 if (s->cipher_list_by_id != NULL)
1333 {
1334 return(s->cipher_list_by_id);
1335 }
1336 else if ((s->ctx != NULL) &&
1337 (s->ctx->cipher_list_by_id != NULL))
1338 {
1339 return(s->ctx->cipher_list_by_id);
1340 }
1341 }
1342 return(NULL);
1343 }
1344
1345/** The old interface to get the same thing as SSL_get_ciphers() */
1346const char *SSL_get_cipher_list(const SSL *s,int n)
1347 {
1348 SSL_CIPHER *c;
1349 STACK_OF(SSL_CIPHER) *sk;
1350
1351 if (s == NULL) return(NULL);
1352 sk=SSL_get_ciphers(s);
1353 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
1354 return(NULL);
1355 c=sk_SSL_CIPHER_value(sk,n);
1356 if (c == NULL) return(NULL);
1357 return(c->name);
1358 }
1359
1360/** specify the ciphers to be used by default by the SSL_CTX */
1361int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1362 {
1363 STACK_OF(SSL_CIPHER) *sk;
1364
1365 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1366 &ctx->cipher_list_by_id,str, ctx->cert);
1367 /* ssl_create_cipher_list may return an empty stack if it
1368 * was unable to find a cipher matching the given rule string
1369 * (for example if the rule string specifies a cipher which
1370 * has been disabled). This is not an error as far as
1371 * ssl_create_cipher_list is concerned, and hence
1372 * ctx->cipher_list and ctx->cipher_list_by_id has been
1373 * updated. */
1374 if (sk == NULL)
1375 return 0;
1376 else if (sk_SSL_CIPHER_num(sk) == 0)
1377 {
1378 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1379 return 0;
1380 }
1381 return 1;
1382 }
1383
1384/** specify the ciphers to be used by the SSL */
1385int SSL_set_cipher_list(SSL *s,const char *str)
1386 {
1387 STACK_OF(SSL_CIPHER) *sk;
1388
1389 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1390 &s->cipher_list_by_id,str, s->cert);
1391 /* see comment in SSL_CTX_set_cipher_list */
1392 if (sk == NULL)
1393 return 0;
1394 else if (sk_SSL_CIPHER_num(sk) == 0)
1395 {
1396 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1397 return 0;
1398 }
1399 return 1;
1400 }
1401
1402/* works well for SSLv2, not so good for SSLv3 */
1403char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
1404 {
1405 char *p;
1406 STACK_OF(SSL_CIPHER) *sk;
1407 SSL_CIPHER *c;
1408 int i;
1409
1410 if ((s->session == NULL) || (s->session->ciphers == NULL) ||
1411 (len < 2))
1412 return(NULL);
1413
1414 p=buf;
1415 sk=s->session->ciphers;
1416 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1417 {
1418 int n;
1419
1420 c=sk_SSL_CIPHER_value(sk,i);
1421 n=strlen(c->name);
1422 if (n+1 > len)
1423 {
1424 if (p != buf)
1425 --p;
1426 *p='\0';
1427 return buf;
1428 }
1429 strcpy(p,c->name);
1430 p+=n;
1431 *(p++)=':';
1432 len-=n+1;
1433 }
1434 p[-1]='\0';
1435 return(buf);
1436 }
1437
1438int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p,
1439 int (*put_cb)(const SSL_CIPHER *, unsigned char *))
1440 {
1441 int i,j=0;
1442 SSL_CIPHER *c;
1443 CERT *ct = s->cert;
1444 unsigned char *q;
1445 int no_scsv = s->renegotiate;
1446 /* Set disabled masks for this session */
1447 ssl_set_client_disabled(s);
1448
1449 if (sk == NULL) return(0);
1450 q=p;
1451
1452 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1453 {
1454 c=sk_SSL_CIPHER_value(sk,i);
1455 /* Skip disabled ciphers */
1456 if (c->algorithm_ssl & ct->mask_ssl ||
1457 c->algorithm_mkey & ct->mask_k ||
1458 c->algorithm_auth & ct->mask_a)
1459 continue;
1460#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
1461 if (c->id == SSL3_CK_SCSV)
1462 {
1463 if (no_scsv)
1464 continue;
1465 else
1466 no_scsv = 1;
1467 }
1468#endif
1469 j = put_cb ? put_cb(c,p) : ssl_put_cipher_by_char(s,c,p);
1470 p+=j;
1471 }
1472 /* If p == q, no ciphers and caller indicates an error. Otherwise
1473 * add SCSV if not renegotiating.
1474 */
1475 if (p != q && !no_scsv)
1476 {
1477 static SSL_CIPHER scsv =
1478 {
1479 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1480 };
1481 j = put_cb ? put_cb(&scsv,p) : ssl_put_cipher_by_char(s,&scsv,p);
1482 p+=j;
1483#ifdef OPENSSL_RI_DEBUG
1484 fprintf(stderr, "SCSV sent by client\n");
1485#endif
1486 }
1487
1488 return(p-q);
1489 }
1490
1491STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s,unsigned char *p,int num,
1492 STACK_OF(SSL_CIPHER) **skp)
1493 {
1494 const SSL_CIPHER *c;
1495 STACK_OF(SSL_CIPHER) *sk;
1496 int i,n;
1497 if (s->s3)
1498 s->s3->send_connection_binding = 0;
1499
1500 n=ssl_put_cipher_by_char(s,NULL,NULL);
1501 if ((num%n) != 0)
1502 {
1503 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1504 return(NULL);
1505 }
1506 if ((skp == NULL) || (*skp == NULL))
1507 sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
1508 else
1509 {
1510 sk= *skp;
1511 sk_SSL_CIPHER_zero(sk);
1512 }
1513
1514 if (s->cert->ciphers_raw)
1515 OPENSSL_free(s->cert->ciphers_raw);
1516 s->cert->ciphers_raw = BUF_memdup(p, num);
1517 if (s->cert->ciphers_raw == NULL)
1518 {
1519 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1520 goto err;
1521 }
1522 s->cert->ciphers_rawlen = (size_t)num;
1523
1524 for (i=0; i<num; i+=n)
1525 {
1526 /* Check for SCSV */
1527 if (s->s3 && (n != 3 || !p[0]) &&
1528 (p[n-2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
1529 (p[n-1] == (SSL3_CK_SCSV & 0xff)))
1530 {
1531 /* SCSV fatal if renegotiating */
1532 if (s->renegotiate)
1533 {
1534 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1535 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1536 goto err;
1537 }
1538 s->s3->send_connection_binding = 1;
1539 p += n;
1540#ifdef OPENSSL_RI_DEBUG
1541 fprintf(stderr, "SCSV received by server\n");
1542#endif
1543 continue;
1544 }
1545
1546 c=ssl_get_cipher_by_char(s,p);
1547 p+=n;
1548 if (c != NULL)
1549 {
1550 if (!sk_SSL_CIPHER_push(sk,c))
1551 {
1552 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1553 goto err;
1554 }
1555 }
1556 }
1557
1558 if (skp != NULL)
1559 *skp=sk;
1560 return(sk);
1561err:
1562 if ((skp == NULL) || (*skp == NULL))
1563 sk_SSL_CIPHER_free(sk);
1564 return(NULL);
1565 }
1566
1567
1568#ifndef OPENSSL_NO_TLSEXT
1569/** return a servername extension value if provided in Client Hello, or NULL.
1570 * So far, only host_name types are defined (RFC 3546).
1571 */
1572
1573const char *SSL_get_servername(const SSL *s, const int type)
1574 {
1575 if (type != TLSEXT_NAMETYPE_host_name)
1576 return NULL;
1577
1578 return s->session && !s->tlsext_hostname ?
1579 s->session->tlsext_hostname :
1580 s->tlsext_hostname;
1581 }
1582
1583int SSL_get_servername_type(const SSL *s)
1584 {
1585 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1586 return TLSEXT_NAMETYPE_host_name;
1587 return -1;
1588 }
1589
1590/* SSL_select_next_proto implements the standard protocol selection. It is
1591 * expected that this function is called from the callback set by
1592 * SSL_CTX_set_next_proto_select_cb.
1593 *
1594 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1595 * strings. The length byte itself is not included in the length. A byte
1596 * string of length 0 is invalid. No byte string may be truncated.
1597 *
1598 * The current, but experimental algorithm for selecting the protocol is:
1599 *
1600 * 1) If the server doesn't support NPN then this is indicated to the
1601 * callback. In this case, the client application has to abort the connection
1602 * or have a default application level protocol.
1603 *
1604 * 2) If the server supports NPN, but advertises an empty list then the
1605 * client selects the first protcol in its list, but indicates via the
1606 * API that this fallback case was enacted.
1607 *
1608 * 3) Otherwise, the client finds the first protocol in the server's list
1609 * that it supports and selects this protocol. This is because it's
1610 * assumed that the server has better information about which protocol
1611 * a client should use.
1612 *
1613 * 4) If the client doesn't support any of the server's advertised
1614 * protocols, then this is treated the same as case 2.
1615 *
1616 * It returns either
1617 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1618 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1619 */
1620int 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)
1621 {
1622 unsigned int i, j;
1623 const unsigned char *result;
1624 int status = OPENSSL_NPN_UNSUPPORTED;
1625
1626 /* For each protocol in server preference order, see if we support it. */
1627 for (i = 0; i < server_len; )
1628 {
1629 for (j = 0; j < client_len; )
1630 {
1631 if (server[i] == client[j] &&
1632 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1633 {
1634 /* We found a match */
1635 result = &server[i];
1636 status = OPENSSL_NPN_NEGOTIATED;
1637 goto found;
1638 }
1639 j += client[j];
1640 j++;
1641 }
1642 i += server[i];
1643 i++;
1644 }
1645
1646 /* There's no overlap between our protocols and the server's list. */
1647 result = client;
1648 status = OPENSSL_NPN_NO_OVERLAP;
1649
1650 found:
1651 *out = (unsigned char *) result + 1;
1652 *outlen = result[0];
1653 return status;
1654 }
1655
1656# ifndef OPENSSL_NO_NEXTPROTONEG
1657/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1658 * requested protocol for this connection and returns 0. If the client didn't
1659 * request any protocol, then *data is set to NULL.
1660 *
1661 * Note that the client can request any protocol it chooses. The value returned
1662 * from this function need not be a member of the list of supported protocols
1663 * provided by the callback.
1664 */
1665void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data, unsigned *len)
1666 {
1667 *data = s->next_proto_negotiated;
1668 if (!*data) {
1669 *len = 0;
1670 } else {
1671 *len = s->next_proto_negotiated_len;
1672 }
1673}
1674
1675/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1676 * TLS server needs a list of supported protocols for Next Protocol
1677 * Negotiation. The returned list must be in wire format. The list is returned
1678 * by setting |out| to point to it and |outlen| to its length. This memory will
1679 * not be modified, but one should assume that the SSL* keeps a reference to
1680 * it.
1681 *
1682 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1683 * such extension will be included in the ServerHello. */
1684void 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)
1685 {
1686 ctx->next_protos_advertised_cb = cb;
1687 ctx->next_protos_advertised_cb_arg = arg;
1688 }
1689
1690/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1691 * client needs to select a protocol from the server's provided list. |out|
1692 * must be set to point to the selected protocol (which may be within |in|).
1693 * The length of the protocol name must be written into |outlen|. The server's
1694 * advertised protocols are provided in |in| and |inlen|. The callback can
1695 * assume that |in| is syntactically valid.
1696 *
1697 * The client must select a protocol. It is fatal to the connection if this
1698 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1699 */
1700void 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)
1701 {
1702 ctx->next_proto_select_cb = cb;
1703 ctx->next_proto_select_cb_arg = arg;
1704 }
1705# endif
1706
1707int SSL_CTX_set_custom_cli_ext(SSL_CTX *ctx, unsigned short ext_type,
1708 custom_cli_ext_first_cb_fn fn1,
1709 custom_cli_ext_second_cb_fn fn2, void* arg)
1710 {
1711 size_t i;
1712 custom_cli_ext_record* record;
1713
1714 /* Check for duplicates */
1715 for (i=0; i < ctx->custom_cli_ext_records_count; i++)
1716 if (ext_type == ctx->custom_cli_ext_records[i].ext_type)
1717 return 0;
1718
1719 ctx->custom_cli_ext_records = OPENSSL_realloc(ctx->custom_cli_ext_records,
1720 (ctx->custom_cli_ext_records_count + 1) *
1721 sizeof(custom_cli_ext_record));
1722 if (!ctx->custom_cli_ext_records) {
1723 ctx->custom_cli_ext_records_count = 0;
1724 return 0;
1725 }
1726 ctx->custom_cli_ext_records_count++;
1727 record = &ctx->custom_cli_ext_records[ctx->custom_cli_ext_records_count - 1];
1728 record->ext_type = ext_type;
1729 record->fn1 = fn1;
1730 record->fn2 = fn2;
1731 record->arg = arg;
1732 return 1;
1733 }
1734
1735int SSL_CTX_set_custom_srv_ext(SSL_CTX *ctx, unsigned short ext_type,
1736 custom_srv_ext_first_cb_fn fn1,
1737 custom_srv_ext_second_cb_fn fn2, void* arg)
1738 {
1739 size_t i;
1740 custom_srv_ext_record* record;
1741
1742 /* Check for duplicates */
1743 for (i=0; i < ctx->custom_srv_ext_records_count; i++)
1744 if (ext_type == ctx->custom_srv_ext_records[i].ext_type)
1745 return 0;
1746
1747 ctx->custom_srv_ext_records = OPENSSL_realloc(ctx->custom_srv_ext_records,
1748 (ctx->custom_srv_ext_records_count + 1) *
1749 sizeof(custom_srv_ext_record));
1750 if (!ctx->custom_srv_ext_records) {
1751 ctx->custom_srv_ext_records_count = 0;
1752 return 0;
1753 }
1754 ctx->custom_srv_ext_records_count++;
1755 record = &ctx->custom_srv_ext_records[ctx->custom_srv_ext_records_count - 1];
1756 record->ext_type = ext_type;
1757 record->fn1 = fn1;
1758 record->fn2 = fn2;
1759 record->arg = arg;
1760 return 1;
1761 }
1762
1763/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1764 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1765 * length-prefixed strings).
1766 *
1767 * Returns 0 on success. */
1768int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1769 unsigned protos_len)
1770 {
1771 if (ctx->alpn_client_proto_list)
1772 OPENSSL_free(ctx->alpn_client_proto_list);
1773
1774 ctx->alpn_client_proto_list = OPENSSL_malloc(protos_len);
1775 if (!ctx->alpn_client_proto_list)
1776 return 1;
1777 memcpy(ctx->alpn_client_proto_list, protos, protos_len);
1778 ctx->alpn_client_proto_list_len = protos_len;
1779
1780 return 0;
1781 }
1782
1783/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1784 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1785 * length-prefixed strings).
1786 *
1787 * Returns 0 on success. */
1788int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1789 unsigned protos_len)
1790 {
1791 if (ssl->alpn_client_proto_list)
1792 OPENSSL_free(ssl->alpn_client_proto_list);
1793
1794 ssl->alpn_client_proto_list = OPENSSL_malloc(protos_len);
1795 if (!ssl->alpn_client_proto_list)
1796 return 1;
1797 memcpy(ssl->alpn_client_proto_list, protos, protos_len);
1798 ssl->alpn_client_proto_list_len = protos_len;
1799
1800 return 0;
1801 }
1802
1803/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1804 * during ClientHello processing in order to select an ALPN protocol from the
1805 * client's list of offered protocols. */
1806void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1807 int (*cb) (SSL *ssl,
1808 const unsigned char **out,
1809 unsigned char *outlen,
1810 const unsigned char *in,
1811 unsigned int inlen,
1812 void *arg),
1813 void *arg)
1814 {
1815 ctx->alpn_select_cb = cb;
1816 ctx->alpn_select_cb_arg = arg;
1817 }
1818
1819/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1820 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1821 * including the leading length-prefix byte). If the server didn't respond with
1822 * a negotiated protocol then |*len| will be zero. */
1823void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1824 unsigned *len)
1825 {
1826 *data = NULL;
1827 if (ssl->s3)
1828 *data = ssl->s3->alpn_selected;
1829 if (*data == NULL)
1830 *len = 0;
1831 else
1832 *len = ssl->s3->alpn_selected_len;
1833 }
1834#endif /* !OPENSSL_NO_TLSEXT */
1835
1836int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1837 const char *label, size_t llen, const unsigned char *p, size_t plen,
1838 int use_context)
1839 {
1840 if (s->version < TLS1_VERSION)
1841 return -1;
1842
1843 return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1844 llen, p, plen,
1845 use_context);
1846 }
1847
1848static uint32_t ssl_session_hash(const SSL_SESSION *a)
1849 {
1850 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1851 ((uint32_t) a->session_id[1] << 8) ||
1852 ((uint32_t) a->session_id[2] << 16) ||
1853 ((uint32_t) a->session_id[3] << 24);
1854
1855 return hash;
1856 }
1857
1858/* NB: If this function (or indeed the hash function which uses a sort of
1859 * coarser function than this one) is changed, ensure
1860 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1861 * able to construct an SSL_SESSION that will collide with any existing session
1862 * with a matching session ID. */
1863static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1864 {
1865 if (a->ssl_version != b->ssl_version)
1866 return(1);
1867 if (a->session_id_length != b->session_id_length)
1868 return(1);
1869 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1870 }
1871
1872SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1873 {
1874 SSL_CTX *ret=NULL;
1875
1876 if (meth == NULL)
1877 {
1878 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1879 return(NULL);
1880 }
1881
1882#ifdef OPENSSL_FIPS
1883 if (FIPS_mode() && (meth->version < TLS1_VERSION))
1884 {
1885 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
1886 return NULL;
1887 }
1888#endif
1889
1890 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1891 {
1892 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1893 goto err;
1894 }
1895 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1896 if (ret == NULL)
1897 goto err;
1898
1899 memset(ret,0,sizeof(SSL_CTX));
1900
1901 ret->method=meth;
1902
1903 ret->cert_store=NULL;
1904 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1905 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1906 ret->session_cache_head=NULL;
1907 ret->session_cache_tail=NULL;
1908
1909 /* We take the system default */
1910 ret->session_timeout=meth->get_timeout();
1911
1912 ret->new_session_cb=0;
1913 ret->remove_session_cb=0;
1914 ret->get_session_cb=0;
1915 ret->generate_session_id=0;
1916
1917 memset((char *)&ret->stats,0,sizeof(ret->stats));
1918
1919 ret->references=1;
1920 ret->quiet_shutdown=0;
1921
1922/* ret->cipher=NULL;*/
1923/* ret->s2->challenge=NULL;
1924 ret->master_key=NULL;
1925 ret->key_arg=NULL;
1926 ret->s2->conn_id=NULL; */
1927
1928 ret->info_callback=NULL;
1929
1930 ret->app_verify_callback=0;
1931 ret->app_verify_arg=NULL;
1932
1933 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1934 ret->read_ahead=0;
1935 ret->msg_callback=0;
1936 ret->msg_callback_arg=NULL;
1937 ret->verify_mode=SSL_VERIFY_NONE;
1938#if 0
1939 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1940#endif
1941 ret->sid_ctx_length=0;
1942 ret->default_verify_callback=NULL;
1943 if ((ret->cert=ssl_cert_new()) == NULL)
1944 goto err;
1945
1946 ret->default_passwd_callback=0;
1947 ret->default_passwd_callback_userdata=NULL;
1948 ret->client_cert_cb=0;
1949 ret->app_gen_cookie_cb=0;
1950 ret->app_verify_cookie_cb=0;
1951
1952 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1953 if (ret->sessions == NULL) goto err;
1954 ret->cert_store=X509_STORE_new();
1955 if (ret->cert_store == NULL) goto err;
1956
1957 ssl_create_cipher_list(ret->method,
1958 &ret->cipher_list,&ret->cipher_list_by_id,
1959 meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST, ret->cert);
1960 if (ret->cipher_list == NULL
1961 || sk_SSL_CIPHER_num(ret->cipher_list) <= 0)
1962 {
1963 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1964 goto err2;
1965 }
1966
1967 ret->param = X509_VERIFY_PARAM_new();
1968 if (!ret->param)
1969 goto err;
1970
1971 ret->rsa_md5 = EVP_md5();
1972 ret->md5 = EVP_md5();
1973 ret->sha1 = EVP_sha1();
1974
1975 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1976 goto err;
1977
1978 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1979
1980 ret->extra_certs=NULL;
1981 /* No compression for DTLS */
1982 if (meth->version != DTLS1_VERSION)
1983 ret->comp_methods=SSL_COMP_get_compression_methods();
1984
1985 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1986
1987#ifndef OPENSSL_NO_TLSEXT
1988 ret->tlsext_servername_callback = 0;
1989 ret->tlsext_servername_arg = NULL;
1990 /* Setup RFC4507 ticket keys */
1991 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
1992 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
1993 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
1994 ret->options |= SSL_OP_NO_TICKET;
1995
1996 ret->tlsext_status_cb = 0;
1997 ret->tlsext_status_arg = NULL;
1998
1999# ifndef OPENSSL_NO_NEXTPROTONEG
2000 ret->next_protos_advertised_cb = 0;
2001 ret->next_proto_select_cb = 0;
2002# endif
2003#endif
2004#ifndef OPENSSL_NO_PSK
2005 ret->psk_identity_hint=NULL;
2006 ret->psk_client_callback=NULL;
2007 ret->psk_server_callback=NULL;
2008#endif
2009 ret->custom_cli_ext_records = NULL;
2010 ret->custom_cli_ext_records_count = 0;
2011 ret->custom_srv_ext_records = NULL;
2012 ret->custom_srv_ext_records_count = 0;
2013#ifndef OPENSSL_NO_BUF_FREELISTS
2014 ret->freelist_max_len = SSL_MAX_BUF_FREELIST_LEN_DEFAULT;
2015 ret->rbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));
2016 if (!ret->rbuf_freelist)
2017 goto err;
2018 ret->rbuf_freelist->chunklen = 0;
2019 ret->rbuf_freelist->len = 0;
2020 ret->rbuf_freelist->head = NULL;
2021 ret->wbuf_freelist = OPENSSL_malloc(sizeof(SSL3_BUF_FREELIST));
2022 if (!ret->wbuf_freelist)
2023 {
2024 OPENSSL_free(ret->rbuf_freelist);
2025 goto err;
2026 }
2027 ret->wbuf_freelist->chunklen = 0;
2028 ret->wbuf_freelist->len = 0;
2029 ret->wbuf_freelist->head = NULL;
2030#endif
2031#ifndef OPENSSL_NO_ENGINE
2032 ret->client_cert_engine = NULL;
2033#ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
2034#define eng_strx(x) #x
2035#define eng_str(x) eng_strx(x)
2036 /* Use specific client engine automatically... ignore errors */
2037 {
2038 ENGINE *eng;
2039 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
2040 if (!eng)
2041 {
2042 ERR_clear_error();
2043 ENGINE_load_builtin_engines();
2044 eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
2045 }
2046 if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
2047 ERR_clear_error();
2048 }
2049#endif
2050#endif
2051 /* Default is to connect to non-RI servers. When RI is more widely
2052 * deployed might change this.
2053 */
2054 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
2055
2056 return(ret);
2057err:
2058 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
2059err2:
2060 if (ret != NULL) SSL_CTX_free(ret);
2061 return(NULL);
2062 }
2063
2064#if 0
2065static void SSL_COMP_free(SSL_COMP *comp)
2066 { OPENSSL_free(comp); }
2067#endif
2068
2069#ifndef OPENSSL_NO_BUF_FREELISTS
2070static void
2071ssl_buf_freelist_free(SSL3_BUF_FREELIST *list)
2072 {
2073 SSL3_BUF_FREELIST_ENTRY *ent, *next;
2074 for (ent = list->head; ent; ent = next)
2075 {
2076 next = ent->next;
2077 OPENSSL_free(ent);
2078 }
2079 OPENSSL_free(list);
2080 }
2081#endif
2082
2083void SSL_CTX_free(SSL_CTX *a)
2084 {
2085 int i;
2086
2087 if (a == NULL) return;
2088
2089 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
2090#ifdef REF_PRINT
2091 REF_PRINT("SSL_CTX",a);
2092#endif
2093 if (i > 0) return;
2094#ifdef REF_CHECK
2095 if (i < 0)
2096 {
2097 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
2098 abort(); /* ok */
2099 }
2100#endif
2101
2102 if (a->param)
2103 X509_VERIFY_PARAM_free(a->param);
2104
2105 /*
2106 * Free internal session cache. However: the remove_cb() may reference
2107 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2108 * after the sessions were flushed.
2109 * As the ex_data handling routines might also touch the session cache,
2110 * the most secure solution seems to be: empty (flush) the cache, then
2111 * free ex_data, then finally free the cache.
2112 * (See ticket [openssl.org #212].)
2113 */
2114 if (a->sessions != NULL)
2115 SSL_CTX_flush_sessions(a,0);
2116
2117 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
2118
2119 if (a->sessions != NULL)
2120 lh_SSL_SESSION_free(a->sessions);
2121
2122 if (a->cert_store != NULL)
2123 X509_STORE_free(a->cert_store);
2124 if (a->cipher_list != NULL)
2125 sk_SSL_CIPHER_free(a->cipher_list);
2126 if (a->cipher_list_by_id != NULL)
2127 sk_SSL_CIPHER_free(a->cipher_list_by_id);
2128 if (a->cert != NULL)
2129 ssl_cert_free(a->cert);
2130 if (a->client_CA != NULL)
2131 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2132 if (a->extra_certs != NULL)
2133 sk_X509_pop_free(a->extra_certs,X509_free);
2134#if 0 /* This should never be done, since it removes a global database */
2135 if (a->comp_methods != NULL)
2136 sk_SSL_COMP_pop_free(a->comp_methods,SSL_COMP_free);
2137#else
2138 a->comp_methods = NULL;
2139#endif
2140
2141 if (a->srtp_profiles)
2142 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2143
2144#ifndef OPENSSL_NO_PSK
2145 if (a->psk_identity_hint)
2146 OPENSSL_free(a->psk_identity_hint);
2147#endif
2148#ifndef OPENSSL_NO_TLSEXT
2149 OPENSSL_free(a->custom_cli_ext_records);
2150 OPENSSL_free(a->custom_srv_ext_records);
2151#endif
2152
2153 /* TODO(fork): remove. */
2154#if 0
2155#ifndef OPENSSL_NO_ENGINE
2156 if (a->client_cert_engine)
2157 ENGINE_finish(a->client_cert_engine);
2158#endif
2159#endif
2160
2161#ifndef OPENSSL_NO_BUF_FREELISTS
2162 if (a->wbuf_freelist)
2163 ssl_buf_freelist_free(a->wbuf_freelist);
2164 if (a->rbuf_freelist)
2165 ssl_buf_freelist_free(a->rbuf_freelist);
2166#endif
2167#ifndef OPENSSL_NO_TLSEXT
2168# ifndef OPENSSL_NO_EC
2169 if (a->tlsext_ecpointformatlist)
2170 OPENSSL_free(a->tlsext_ecpointformatlist);
2171 if (a->tlsext_ellipticcurvelist)
2172 OPENSSL_free(a->tlsext_ellipticcurvelist);
2173# endif /* OPENSSL_NO_EC */
2174 if (a->alpn_client_proto_list != NULL)
2175 OPENSSL_free(a->alpn_client_proto_list);
2176#endif
2177
Adam Langley1258b6a2014-06-20 12:00:00 -07002178#ifndef OPENSSL_NO_TLSEXT
2179 if (a->tlsext_channel_id_private)
2180 EVP_PKEY_free(a->tlsext_channel_id_private);
2181#endif
2182
Adam Langley95c29f32014-06-20 12:00:00 -07002183 OPENSSL_free(a);
2184 }
2185
2186void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2187 {
2188 ctx->default_passwd_callback=cb;
2189 }
2190
2191void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2192 {
2193 ctx->default_passwd_callback_userdata=u;
2194 }
2195
2196void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2197 {
2198 ctx->app_verify_callback=cb;
2199 ctx->app_verify_arg=arg;
2200 }
2201
2202void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2203 {
2204 ctx->verify_mode=mode;
2205 ctx->default_verify_callback=cb;
2206 }
2207
2208void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2209 {
2210 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2211 }
2212
2213void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2214 {
2215 ssl_cert_set_cert_cb(c->cert, cb, arg);
2216 }
2217
2218void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2219 {
2220 ssl_cert_set_cert_cb(s->cert, cb, arg);
2221 }
2222
2223void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
2224 {
2225 CERT_PKEY *cpk;
2226 int rsa_enc,rsa_tmp,rsa_sign,dh_tmp,dh_rsa,dh_dsa,dsa_sign;
2227 int rsa_enc_export,dh_rsa_export,dh_dsa_export;
2228 int rsa_tmp_export,dh_tmp_export,kl;
2229 unsigned long mask_k,mask_a,emask_k,emask_a;
2230#ifndef OPENSSL_NO_ECDSA
2231 int have_ecc_cert, ecdsa_ok, ecc_pkey_size;
2232#endif
2233#ifndef OPENSSL_NO_ECDH
2234 int have_ecdh_tmp, ecdh_ok;
2235#endif
2236#ifndef OPENSSL_NO_EC
2237 X509 *x = NULL;
2238 EVP_PKEY *ecc_pkey = NULL;
2239 int signature_nid = 0, pk_nid = 0, md_nid = 0;
2240#endif
2241 if (c == NULL) return;
2242
2243 kl=SSL_C_EXPORT_PKEYLENGTH(cipher);
2244
2245#ifndef OPENSSL_NO_RSA
2246 rsa_tmp=(c->rsa_tmp != NULL || c->rsa_tmp_cb != NULL);
2247 rsa_tmp_export=(c->rsa_tmp_cb != NULL ||
2248 (rsa_tmp && RSA_size(c->rsa_tmp)*8 <= kl));
2249#else
2250 rsa_tmp=rsa_tmp_export=0;
2251#endif
2252#ifndef OPENSSL_NO_DH
2253 dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
2254 dh_tmp_export=(c->dh_tmp_cb != NULL ||
2255 (dh_tmp && DH_size(c->dh_tmp)*8 <= kl));
2256#else
2257 dh_tmp=dh_tmp_export=0;
2258#endif
2259
2260#ifndef OPENSSL_NO_ECDH
2261 have_ecdh_tmp=(c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
2262#endif
2263 cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
2264 rsa_enc= cpk->valid_flags & CERT_PKEY_VALID;
2265 rsa_enc_export=(rsa_enc && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
2266 cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
2267 rsa_sign= cpk->valid_flags & CERT_PKEY_SIGN;
2268 cpk= &(c->pkeys[SSL_PKEY_DSA_SIGN]);
2269 dsa_sign= cpk->valid_flags & CERT_PKEY_SIGN;
2270 cpk= &(c->pkeys[SSL_PKEY_DH_RSA]);
2271 dh_rsa= cpk->valid_flags & CERT_PKEY_VALID;
2272 dh_rsa_export=(dh_rsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
2273 cpk= &(c->pkeys[SSL_PKEY_DH_DSA]);
2274/* FIX THIS EAY EAY EAY */
2275 dh_dsa= cpk->valid_flags & CERT_PKEY_VALID;
2276 dh_dsa_export=(dh_dsa && EVP_PKEY_size(cpk->privatekey)*8 <= kl);
2277 cpk= &(c->pkeys[SSL_PKEY_ECC]);
2278#ifndef OPENSSL_NO_EC
2279 have_ecc_cert= cpk->valid_flags & CERT_PKEY_VALID;
2280#endif
2281 mask_k=0;
2282 mask_a=0;
2283 emask_k=0;
2284 emask_a=0;
2285
2286
2287
2288#ifdef CIPHER_DEBUG
2289 printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
2290 rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
2291 rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
2292#endif
2293
2294 cpk = &(c->pkeys[SSL_PKEY_GOST01]);
2295 if (cpk->x509 != NULL && cpk->privatekey !=NULL) {
2296 mask_k |= SSL_kGOST;
2297 mask_a |= SSL_aGOST01;
2298 }
2299 cpk = &(c->pkeys[SSL_PKEY_GOST94]);
2300 if (cpk->x509 != NULL && cpk->privatekey !=NULL) {
2301 mask_k |= SSL_kGOST;
2302 mask_a |= SSL_aGOST94;
2303 }
2304
2305 if (rsa_enc || (rsa_tmp && rsa_sign))
2306 mask_k|=SSL_kRSA;
2307 if (rsa_enc_export || (rsa_tmp_export && (rsa_sign || rsa_enc)))
2308 emask_k|=SSL_kRSA;
2309
2310#if 0
2311 /* The match needs to be both kEDH and aRSA or aDSA, so don't worry */
2312 if ( (dh_tmp || dh_rsa || dh_dsa) &&
2313 (rsa_enc || rsa_sign || dsa_sign))
2314 mask_k|=SSL_kEDH;
2315 if ((dh_tmp_export || dh_rsa_export || dh_dsa_export) &&
2316 (rsa_enc || rsa_sign || dsa_sign))
2317 emask_k|=SSL_kEDH;
2318#endif
2319
2320 if (dh_tmp_export)
2321 emask_k|=SSL_kEDH;
2322
2323 if (dh_tmp)
2324 mask_k|=SSL_kEDH;
2325
2326 if (dh_rsa) mask_k|=SSL_kDHr;
2327 if (dh_rsa_export) emask_k|=SSL_kDHr;
2328
2329 if (dh_dsa) mask_k|=SSL_kDHd;
2330 if (dh_dsa_export) emask_k|=SSL_kDHd;
2331
2332 if (emask_k & (SSL_kDHr|SSL_kDHd))
2333 mask_a |= SSL_aDH;
2334
2335 if (rsa_enc || rsa_sign)
2336 {
2337 mask_a|=SSL_aRSA;
2338 emask_a|=SSL_aRSA;
2339 }
2340
2341 if (dsa_sign)
2342 {
2343 mask_a|=SSL_aDSS;
2344 emask_a|=SSL_aDSS;
2345 }
2346
2347 mask_a|=SSL_aNULL;
2348 emask_a|=SSL_aNULL;
2349
2350 /* An ECC certificate may be usable for ECDH and/or
2351 * ECDSA cipher suites depending on the key usage extension.
2352 */
2353#ifndef OPENSSL_NO_EC
2354 if (have_ecc_cert)
2355 {
2356 cpk = &c->pkeys[SSL_PKEY_ECC];
2357 x = cpk->x509;
2358 /* This call populates extension flags (ex_flags) */
2359 X509_check_purpose(x, -1, 0);
2360 ecdh_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2361 (x->ex_kusage & X509v3_KU_KEY_AGREEMENT) : 1;
2362 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2363 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
2364 if (!(cpk->valid_flags & CERT_PKEY_SIGN))
2365 ecdsa_ok = 0;
2366 ecc_pkey = X509_get_pubkey(x);
2367 ecc_pkey_size = (ecc_pkey != NULL) ?
2368 EVP_PKEY_bits(ecc_pkey) : 0;
2369 EVP_PKEY_free(ecc_pkey);
2370 if ((x->sig_alg) && (x->sig_alg->algorithm))
2371 {
2372 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2373 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2374 }
2375#ifndef OPENSSL_NO_ECDH
2376 if (ecdh_ok)
2377 {
2378
2379 if (pk_nid == NID_rsaEncryption || pk_nid == NID_rsa)
2380 {
2381 mask_k|=SSL_kECDHr;
2382 mask_a|=SSL_aECDH;
2383 if (ecc_pkey_size <= 163)
2384 {
2385 emask_k|=SSL_kECDHr;
2386 emask_a|=SSL_aECDH;
2387 }
2388 }
2389
2390 if (pk_nid == NID_X9_62_id_ecPublicKey)
2391 {
2392 mask_k|=SSL_kECDHe;
2393 mask_a|=SSL_aECDH;
2394 if (ecc_pkey_size <= 163)
2395 {
2396 emask_k|=SSL_kECDHe;
2397 emask_a|=SSL_aECDH;
2398 }
2399 }
2400 }
2401#endif
2402#ifndef OPENSSL_NO_ECDSA
2403 if (ecdsa_ok)
2404 {
2405 mask_a|=SSL_aECDSA;
2406 emask_a|=SSL_aECDSA;
2407 }
2408#endif
2409 }
2410#endif
2411
2412#ifndef OPENSSL_NO_ECDH
2413 if (have_ecdh_tmp)
2414 {
2415 mask_k|=SSL_kEECDH;
2416 emask_k|=SSL_kEECDH;
2417 }
2418#endif
2419
2420#ifndef OPENSSL_NO_PSK
2421 mask_k |= SSL_kPSK;
2422 mask_a |= SSL_aPSK;
2423 emask_k |= SSL_kPSK;
2424 emask_a |= SSL_aPSK;
2425#endif
2426
2427 c->mask_k=mask_k;
2428 c->mask_a=mask_a;
2429 c->export_mask_k=emask_k;
2430 c->export_mask_a=emask_a;
2431 c->valid=1;
2432 }
2433
2434/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2435#define ku_reject(x, usage) \
2436 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2437
2438#ifndef OPENSSL_NO_EC
2439
2440int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2441 {
2442 unsigned long alg_k, alg_a;
2443 EVP_PKEY *pkey = NULL;
2444 int keysize = 0;
2445 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2446 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2447
2448 alg_k = cs->algorithm_mkey;
2449 alg_a = cs->algorithm_auth;
2450
2451 if (SSL_C_IS_EXPORT(cs))
2452 {
2453 /* ECDH key length in export ciphers must be <= 163 bits */
2454 pkey = X509_get_pubkey(x);
2455 if (pkey == NULL) return 0;
2456 keysize = EVP_PKEY_bits(pkey);
2457 EVP_PKEY_free(pkey);
2458 if (keysize > 163) return 0;
2459 }
2460
2461 /* This call populates the ex_flags field correctly */
2462 X509_check_purpose(x, -1, 0);
2463 if ((x->sig_alg) && (x->sig_alg->algorithm))
2464 {
2465 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2466 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2467 }
2468 if (alg_k & SSL_kECDHe || alg_k & SSL_kECDHr)
2469 {
2470 /* key usage, if present, must allow key agreement */
2471 if (ku_reject(x, X509v3_KU_KEY_AGREEMENT))
2472 {
2473 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_KEY_AGREEMENT);
2474 return 0;
2475 }
2476 if ((alg_k & SSL_kECDHe) && TLS1_get_version(s) < TLS1_2_VERSION)
2477 {
2478 /* signature alg must be ECDSA */
2479 if (pk_nid != NID_X9_62_id_ecPublicKey)
2480 {
2481 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_SHOULD_HAVE_SHA1_SIGNATURE);
2482 return 0;
2483 }
2484 }
2485 if ((alg_k & SSL_kECDHr) && TLS1_get_version(s) < TLS1_2_VERSION)
2486 {
2487 /* signature alg must be RSA */
2488
2489 if (pk_nid != NID_rsaEncryption && pk_nid != NID_rsa)
2490 {
2491 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_SHOULD_HAVE_RSA_SIGNATURE);
2492 return 0;
2493 }
2494 }
2495 }
2496 if (alg_a & SSL_aECDSA)
2497 {
2498 /* key usage, if present, must allow signing */
2499 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2500 {
2501 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2502 return 0;
2503 }
2504 }
2505
2506 return 1; /* all checks are ok */
2507 }
2508
2509#endif
2510
2511static int ssl_get_server_cert_index(const SSL *s)
2512 {
2513 int idx;
2514 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2515 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2516 idx = SSL_PKEY_RSA_SIGN;
2517 if (idx == -1)
2518 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2519 return idx;
2520 }
2521
2522CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2523 {
2524 CERT *c;
2525 int i;
2526
2527 c = s->cert;
2528 ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
2529
2530#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2531 /* Broken protocol test: return last used certificate: which may
2532 * mismatch the one expected.
2533 */
2534 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2535 return c->key;
2536#endif
2537
2538 i = ssl_get_server_cert_index(s);
2539
2540 /* This may or may not be an error. */
2541 if (i < 0)
2542 return NULL;
2543
2544 /* May be NULL. */
2545 return &c->pkeys[i];
2546 }
2547
2548EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd)
2549 {
2550 unsigned long alg_a;
2551 CERT *c;
2552 int idx = -1;
2553
2554 alg_a = cipher->algorithm_auth;
2555 c=s->cert;
2556
2557#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2558 /* Broken protocol test: use last key: which may
2559 * mismatch the one expected.
2560 */
2561 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2562 idx = c->key - c->pkeys;
2563 else
2564#endif
2565
2566 if ((alg_a & SSL_aDSS) &&
2567 (c->pkeys[SSL_PKEY_DSA_SIGN].privatekey != NULL))
2568 idx = SSL_PKEY_DSA_SIGN;
2569 else if (alg_a & SSL_aRSA)
2570 {
2571 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2572 idx = SSL_PKEY_RSA_SIGN;
2573 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2574 idx = SSL_PKEY_RSA_ENC;
2575 }
2576 else if ((alg_a & SSL_aECDSA) &&
2577 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2578 idx = SSL_PKEY_ECC;
2579 if (idx == -1)
2580 {
2581 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2582 return(NULL);
2583 }
2584 if (pmd)
2585 *pmd = c->pkeys[idx].digest;
2586 return c->pkeys[idx].privatekey;
2587 }
2588
2589#ifndef OPENSSL_NO_TLSEXT
2590unsigned char *ssl_get_authz_data(SSL *s, size_t *authz_length)
2591 {
2592 CERT *c;
2593 int i;
2594
2595 c = s->cert;
2596 i = ssl_get_server_cert_index(s);
2597
2598 if (i == -1)
2599 return NULL;
2600
2601 *authz_length = 0;
2602 if (c->pkeys[i].authz == NULL)
2603 return(NULL);
2604 *authz_length = c->pkeys[i].authz_length;
2605
2606 return c->pkeys[i].authz;
2607 }
2608
2609int ssl_get_server_cert_serverinfo(SSL *s, const unsigned char **serverinfo,
2610 size_t *serverinfo_length)
2611 {
2612 CERT *c = NULL;
2613 int i = 0;
2614 *serverinfo_length = 0;
2615
2616 c = s->cert;
2617 i = ssl_get_server_cert_index(s);
2618
2619 if (i == -1)
2620 return 0;
2621 if (c->pkeys[i].serverinfo == NULL)
2622 return 0;
2623
2624 *serverinfo = c->pkeys[i].serverinfo;
2625 *serverinfo_length = c->pkeys[i].serverinfo_length;
2626 return 1;
2627 }
2628#endif
2629
2630void ssl_update_cache(SSL *s,int mode)
2631 {
2632 int i;
2633
2634 /* If the session_id_length is 0, we are not supposed to cache it,
2635 * and it would be rather hard to do anyway :-) */
2636 if (s->session->session_id_length == 0) return;
2637
2638 i=s->session_ctx->session_cache_mode;
2639 if ((i & mode) && (!s->hit)
2640 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
2641 || SSL_CTX_add_session(s->session_ctx,s->session))
2642 && (s->session_ctx->new_session_cb != NULL))
2643 {
2644 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
2645 if (!s->session_ctx->new_session_cb(s,s->session))
2646 SSL_SESSION_free(s->session);
2647 }
2648
2649 /* auto flush every 255 connections */
2650 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2651 ((i & mode) == mode))
2652 {
2653 if ( (((mode & SSL_SESS_CACHE_CLIENT)
2654 ?s->session_ctx->stats.sess_connect_good
2655 :s->session_ctx->stats.sess_accept_good) & 0xff) == 0xff)
2656 {
2657 SSL_CTX_flush_sessions(s->session_ctx,(unsigned long)time(NULL));
2658 }
2659 }
2660 }
2661
2662const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
2663 {
2664 return ctx->method;
2665 }
2666
2667const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2668 {
2669 return(s->method);
2670 }
2671
2672int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2673 {
2674 int conn= -1;
2675 int ret=1;
2676
2677 if (s->method != meth)
2678 {
2679 if (s->handshake_func != NULL)
2680 conn=(s->handshake_func == s->method->ssl_connect);
2681
2682 if (s->method->version == meth->version)
2683 s->method=meth;
2684 else
2685 {
2686 s->method->ssl_free(s);
2687 s->method=meth;
2688 ret=s->method->ssl_new(s);
2689 }
2690
2691 if (conn == 1)
2692 s->handshake_func=meth->ssl_connect;
2693 else if (conn == 0)
2694 s->handshake_func=meth->ssl_accept;
2695 }
2696 return(ret);
2697 }
2698
2699int SSL_get_error(const SSL *s,int i)
2700 {
2701 int reason;
2702 unsigned long l;
2703 BIO *bio;
2704
2705 if (i > 0) return(SSL_ERROR_NONE);
2706
2707 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2708 * etc, where we do encode the error */
2709 if ((l=ERR_peek_error()) != 0)
2710 {
2711 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2712 return(SSL_ERROR_SYSCALL);
2713 else
2714 return(SSL_ERROR_SSL);
2715 }
2716
Adam Langleyb2ce0582014-06-20 12:00:00 -07002717 if ((i < 0) && SSL_want_session(s))
2718 return(SSL_ERROR_PENDING_SESSION);
2719
Adam Langley95c29f32014-06-20 12:00:00 -07002720 if ((i < 0) && SSL_want_read(s))
2721 {
2722 bio=SSL_get_rbio(s);
2723 if (BIO_should_read(bio))
2724 return(SSL_ERROR_WANT_READ);
2725 else if (BIO_should_write(bio))
2726 /* This one doesn't make too much sense ... We never try
2727 * to write to the rbio, and an application program where
2728 * rbio and wbio are separate couldn't even know what it
2729 * should wait for.
2730 * However if we ever set s->rwstate incorrectly
2731 * (so that we have SSL_want_read(s) instead of
2732 * SSL_want_write(s)) and rbio and wbio *are* the same,
2733 * this test works around that bug; so it might be safer
2734 * to keep it. */
2735 return(SSL_ERROR_WANT_WRITE);
2736 else if (BIO_should_io_special(bio))
2737 {
2738 reason=BIO_get_retry_reason(bio);
2739 if (reason == BIO_RR_CONNECT)
2740 return(SSL_ERROR_WANT_CONNECT);
2741 else if (reason == BIO_RR_ACCEPT)
2742 return(SSL_ERROR_WANT_ACCEPT);
2743 else
2744 return(SSL_ERROR_SYSCALL); /* unknown */
2745 }
2746 }
2747
2748 if ((i < 0) && SSL_want_write(s))
2749 {
2750 bio=SSL_get_wbio(s);
2751 if (BIO_should_write(bio))
2752 return(SSL_ERROR_WANT_WRITE);
2753 else if (BIO_should_read(bio))
2754 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2755 return(SSL_ERROR_WANT_READ);
2756 else if (BIO_should_io_special(bio))
2757 {
2758 reason=BIO_get_retry_reason(bio);
2759 if (reason == BIO_RR_CONNECT)
2760 return(SSL_ERROR_WANT_CONNECT);
2761 else if (reason == BIO_RR_ACCEPT)
2762 return(SSL_ERROR_WANT_ACCEPT);
2763 else
2764 return(SSL_ERROR_SYSCALL);
2765 }
2766 }
2767 if ((i < 0) && SSL_want_x509_lookup(s))
2768 {
2769 return(SSL_ERROR_WANT_X509_LOOKUP);
2770 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002771 if ((i < 0) && SSL_want_channel_id_lookup(s))
2772 {
2773 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2774 }
Adam Langley95c29f32014-06-20 12:00:00 -07002775
2776 if (i == 0)
2777 {
2778 if (s->version == SSL2_VERSION)
2779 {
2780 /* assume it is the socket being closed */
2781 return(SSL_ERROR_ZERO_RETURN);
2782 }
2783 else
2784 {
2785 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2786 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2787 return(SSL_ERROR_ZERO_RETURN);
2788 }
2789 }
2790 return(SSL_ERROR_SYSCALL);
2791 }
2792
2793int SSL_do_handshake(SSL *s)
2794 {
2795 int ret=1;
2796
2797 if (s->handshake_func == NULL)
2798 {
2799 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2800 return(-1);
2801 }
2802
2803 s->method->ssl_renegotiate_check(s);
2804
2805 if (SSL_in_init(s) || SSL_in_before(s))
2806 {
2807 ret=s->handshake_func(s);
2808 }
2809 return(ret);
2810 }
2811
2812/* For the next 2 functions, SSL_clear() sets shutdown and so
2813 * one of these calls will reset it */
2814void SSL_set_accept_state(SSL *s)
2815 {
2816 s->server=1;
2817 s->shutdown=0;
2818 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2819 s->handshake_func=s->method->ssl_accept;
2820 /* clear the current cipher */
2821 ssl_clear_cipher_ctx(s);
2822 ssl_clear_hash_ctx(&s->read_hash);
2823 ssl_clear_hash_ctx(&s->write_hash);
2824 }
2825
2826void SSL_set_connect_state(SSL *s)
2827 {
2828 s->server=0;
2829 s->shutdown=0;
2830 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2831 s->handshake_func=s->method->ssl_connect;
2832 /* clear the current cipher */
2833 ssl_clear_cipher_ctx(s);
2834 ssl_clear_hash_ctx(&s->read_hash);
2835 ssl_clear_hash_ctx(&s->write_hash);
2836 }
2837
2838int ssl_undefined_function(SSL *s)
2839 {
2840 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2841 return(0);
2842 }
2843
2844int ssl_undefined_void_function(void)
2845 {
2846 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2847 return(0);
2848 }
2849
2850int ssl_undefined_const_function(const SSL *s)
2851 {
2852 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2853 return(0);
2854 }
2855
2856SSL_METHOD *ssl_bad_method(int ver)
2857 {
2858 OPENSSL_PUT_ERROR(SSL, ssl_bad_method, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2859 return(NULL);
2860 }
2861
2862const char *SSL_get_version(const SSL *s)
2863 {
2864 if (s->version == TLS1_2_VERSION)
2865 return("TLSv1.2");
2866 else if (s->version == TLS1_1_VERSION)
2867 return("TLSv1.1");
2868 else if (s->version == TLS1_VERSION)
2869 return("TLSv1");
2870 else if (s->version == SSL3_VERSION)
2871 return("SSLv3");
2872 else if (s->version == SSL2_VERSION)
2873 return("SSLv2");
2874 else
2875 return("unknown");
2876 }
2877
2878void ssl_clear_cipher_ctx(SSL *s)
2879 {
2880 if (s->enc_read_ctx != NULL)
2881 {
2882 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2883 OPENSSL_free(s->enc_read_ctx);
2884 s->enc_read_ctx=NULL;
2885 }
2886 if (s->enc_write_ctx != NULL)
2887 {
2888 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2889 OPENSSL_free(s->enc_write_ctx);
2890 s->enc_write_ctx=NULL;
2891 }
2892 }
2893
2894X509 *SSL_get_certificate(const SSL *s)
2895 {
2896 if (s->cert != NULL)
2897 return(s->cert->key->x509);
2898 else
2899 return(NULL);
2900 }
2901
2902EVP_PKEY *SSL_get_privatekey(const SSL *s)
2903 {
2904 if (s->cert != NULL)
2905 return(s->cert->key->privatekey);
2906 else
2907 return(NULL);
2908 }
2909
2910X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2911 {
2912 if (ctx->cert != NULL)
2913 return ctx->cert->key->x509;
2914 else
2915 return NULL;
2916 }
2917
2918EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2919 {
2920 if (ctx->cert != NULL)
2921 return ctx->cert->key->privatekey;
2922 else
2923 return NULL ;
2924 }
2925
2926const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2927 {
2928 if ((s->session != NULL) && (s->session->cipher != NULL))
2929 return(s->session->cipher);
2930 return(NULL);
2931 }
2932const void *SSL_get_current_compression(SSL *s)
2933 {
2934 return NULL;
2935 }
2936const void *SSL_get_current_expansion(SSL *s)
2937 {
2938 return NULL;
2939 }
2940
2941int ssl_init_wbio_buffer(SSL *s,int push)
2942 {
2943 BIO *bbio;
2944
2945 if (s->bbio == NULL)
2946 {
2947 bbio=BIO_new(BIO_f_buffer());
2948 if (bbio == NULL) return(0);
2949 s->bbio=bbio;
2950 }
2951 else
2952 {
2953 bbio=s->bbio;
2954 if (s->bbio == s->wbio)
2955 s->wbio=BIO_pop(s->wbio);
2956 }
2957 (void)BIO_reset(bbio);
2958/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2959 if (!BIO_set_read_buffer_size(bbio,1))
2960 {
2961 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2962 return(0);
2963 }
2964 if (push)
2965 {
2966 if (s->wbio != bbio)
2967 s->wbio=BIO_push(bbio,s->wbio);
2968 }
2969 else
2970 {
2971 if (s->wbio == bbio)
2972 s->wbio=BIO_pop(bbio);
2973 }
2974 return(1);
2975 }
2976
2977void ssl_free_wbio_buffer(SSL *s)
2978 {
2979 if (s->bbio == NULL) return;
2980
2981 if (s->bbio == s->wbio)
2982 {
2983 /* remove buffering */
2984 s->wbio=BIO_pop(s->wbio);
2985#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2986 assert(s->wbio != NULL);
2987#endif
2988 }
2989 BIO_free(s->bbio);
2990 s->bbio=NULL;
2991 }
2992
2993void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2994 {
2995 ctx->quiet_shutdown=mode;
2996 }
2997
2998int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2999 {
3000 return(ctx->quiet_shutdown);
3001 }
3002
3003void SSL_set_quiet_shutdown(SSL *s,int mode)
3004 {
3005 s->quiet_shutdown=mode;
3006 }
3007
3008int SSL_get_quiet_shutdown(const SSL *s)
3009 {
3010 return(s->quiet_shutdown);
3011 }
3012
3013void SSL_set_shutdown(SSL *s,int mode)
3014 {
3015 s->shutdown=mode;
3016 }
3017
3018int SSL_get_shutdown(const SSL *s)
3019 {
3020 return(s->shutdown);
3021 }
3022
3023int SSL_version(const SSL *s)
3024 {
3025 return(s->version);
3026 }
3027
3028SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
3029 {
3030 return(ssl->ctx);
3031 }
3032
3033SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
3034 {
3035 if (ssl->ctx == ctx)
3036 return ssl->ctx;
3037#ifndef OPENSSL_NO_TLSEXT
3038 if (ctx == NULL)
3039 ctx = ssl->initial_ctx;
3040#endif
3041 if (ssl->cert != NULL)
3042 ssl_cert_free(ssl->cert);
3043 ssl->cert = ssl_cert_dup(ctx->cert);
3044 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
3045 if (ssl->ctx != NULL)
3046 SSL_CTX_free(ssl->ctx); /* decrement reference count */
3047 ssl->ctx = ctx;
3048 return(ssl->ctx);
3049 }
3050
3051#ifndef OPENSSL_NO_STDIO
3052int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
3053 {
3054 return(X509_STORE_set_default_paths(ctx->cert_store));
3055 }
3056
3057int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
3058 const char *CApath)
3059 {
3060 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
3061 }
3062#endif
3063
3064void SSL_set_info_callback(SSL *ssl,
3065 void (*cb)(const SSL *ssl,int type,int val))
3066 {
3067 ssl->info_callback=cb;
3068 }
3069
3070/* One compiler (Diab DCC) doesn't like argument names in returned
3071 function pointer. */
3072void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
3073 {
3074 return ssl->info_callback;
3075 }
3076
3077int SSL_state(const SSL *ssl)
3078 {
3079 return(ssl->state);
3080 }
3081
3082void SSL_set_state(SSL *ssl, int state)
3083 {
3084 ssl->state = state;
3085 }
3086
3087void SSL_set_verify_result(SSL *ssl,long arg)
3088 {
3089 ssl->verify_result=arg;
3090 }
3091
3092long SSL_get_verify_result(const SSL *ssl)
3093 {
3094 return(ssl->verify_result);
3095 }
3096
3097int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
3098 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
3099 {
3100 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
3101 new_func, dup_func, free_func);
3102 }
3103
3104int SSL_set_ex_data(SSL *s,int idx,void *arg)
3105 {
3106 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
3107 }
3108
3109void *SSL_get_ex_data(const SSL *s,int idx)
3110 {
3111 return(CRYPTO_get_ex_data(&s->ex_data,idx));
3112 }
3113
3114int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
3115 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
3116 {
3117 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
3118 new_func, dup_func, free_func);
3119 }
3120
3121int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
3122 {
3123 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
3124 }
3125
3126void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
3127 {
3128 return(CRYPTO_get_ex_data(&s->ex_data,idx));
3129 }
3130
3131int ssl_ok(SSL *s)
3132 {
3133 return(1);
3134 }
3135
3136X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
3137 {
3138 return(ctx->cert_store);
3139 }
3140
3141void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
3142 {
3143 if (ctx->cert_store != NULL)
3144 X509_STORE_free(ctx->cert_store);
3145 ctx->cert_store=store;
3146 }
3147
3148int SSL_want(const SSL *s)
3149 {
3150 return(s->rwstate);
3151 }
3152
3153/*!
3154 * \brief Set the callback for generating temporary RSA keys.
3155 * \param ctx the SSL context.
3156 * \param cb the callback
3157 */
3158
3159#ifndef OPENSSL_NO_RSA
3160void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
3161 int is_export,
3162 int keylength))
3163 {
3164 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3165 }
3166
3167void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
3168 int is_export,
3169 int keylength))
3170 {
3171 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3172 }
3173#endif
3174
3175#ifdef DOXYGEN
3176/*!
3177 * \brief The RSA temporary key callback function.
3178 * \param ssl the SSL session.
3179 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
3180 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
3181 * of the required key in bits.
3182 * \return the temporary RSA key.
3183 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
3184 */
3185
3186RSA *cb(SSL *ssl,int is_export,int keylength)
3187 {}
3188#endif
3189
3190/*!
3191 * \brief Set the callback for generating temporary DH keys.
3192 * \param ctx the SSL context.
3193 * \param dh the callback
3194 */
3195
3196#ifndef OPENSSL_NO_DH
3197void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
3198 int keylength))
3199 {
3200 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3201 }
3202
3203void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
3204 int keylength))
3205 {
3206 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3207 }
3208#endif
3209
3210#ifndef OPENSSL_NO_ECDH
3211void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
3212 int keylength))
3213 {
3214 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
3215 }
3216
3217void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
3218 int keylength))
3219 {
3220 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
3221 }
3222#endif
3223
3224#ifndef OPENSSL_NO_PSK
3225int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
3226 {
3227 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
3228 {
3229 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
3230 return 0;
3231 }
3232 if (ctx->psk_identity_hint != NULL)
3233 OPENSSL_free(ctx->psk_identity_hint);
3234 if (identity_hint != NULL)
3235 {
3236 ctx->psk_identity_hint = BUF_strdup(identity_hint);
3237 if (ctx->psk_identity_hint == NULL)
3238 return 0;
3239 }
3240 else
3241 ctx->psk_identity_hint = NULL;
3242 return 1;
3243 }
3244
3245int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
3246 {
3247 if (s == NULL)
3248 return 0;
3249
3250 if (s->session == NULL)
3251 return 1; /* session not created yet, ignored */
3252
3253 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
3254 {
3255 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
3256 return 0;
3257 }
3258 if (s->session->psk_identity_hint != NULL)
3259 OPENSSL_free(s->session->psk_identity_hint);
3260 if (identity_hint != NULL)
3261 {
3262 s->session->psk_identity_hint = BUF_strdup(identity_hint);
3263 if (s->session->psk_identity_hint == NULL)
3264 return 0;
3265 }
3266 else
3267 s->session->psk_identity_hint = NULL;
3268 return 1;
3269 }
3270
3271const char *SSL_get_psk_identity_hint(const SSL *s)
3272 {
3273 if (s == NULL || s->session == NULL)
3274 return NULL;
3275 return(s->session->psk_identity_hint);
3276 }
3277
3278const char *SSL_get_psk_identity(const SSL *s)
3279 {
3280 if (s == NULL || s->session == NULL)
3281 return NULL;
3282 return(s->session->psk_identity);
3283 }
3284
3285void SSL_set_psk_client_callback(SSL *s,
3286 unsigned int (*cb)(SSL *ssl, const char *hint,
3287 char *identity, unsigned int max_identity_len, unsigned char *psk,
3288 unsigned int max_psk_len))
3289 {
3290 s->psk_client_callback = cb;
3291 }
3292
3293void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
3294 unsigned int (*cb)(SSL *ssl, const char *hint,
3295 char *identity, unsigned int max_identity_len, unsigned char *psk,
3296 unsigned int max_psk_len))
3297 {
3298 ctx->psk_client_callback = cb;
3299 }
3300
3301void SSL_set_psk_server_callback(SSL *s,
3302 unsigned int (*cb)(SSL *ssl, const char *identity,
3303 unsigned char *psk, unsigned int max_psk_len))
3304 {
3305 s->psk_server_callback = cb;
3306 }
3307
3308void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
3309 unsigned int (*cb)(SSL *ssl, const char *identity,
3310 unsigned char *psk, unsigned int max_psk_len))
3311 {
3312 ctx->psk_server_callback = cb;
3313 }
3314#endif
3315
3316void 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))
3317 {
3318 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3319 }
3320void 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))
3321 {
3322 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3323 }
3324
Adam Langleyadb739e2014-06-20 12:00:00 -07003325int SSL_cutthrough_complete(const SSL *s)
3326 {
3327 return (!s->server && /* cutthrough only applies to clients */
3328 !s->hit && /* full-handshake */
3329 s->version >= SSL3_VERSION &&
3330 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3331 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3332 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3333 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3334 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
3335 s->state == SSL3_ST_CR_FINISHED_A));
3336 }
3337
Adam Langley95f22882014-06-20 12:00:00 -07003338void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3339 size_t* ssl_session_size)
3340{
3341 *ssl_size = sizeof(SSL);
3342 *ssl_ctx_size = sizeof(SSL_CTX);
3343 *ssl_session_size = sizeof(SSL_SESSION);
3344}
3345
Adam Langleyadb739e2014-06-20 12:00:00 -07003346int ssl3_can_cutthrough(const SSL *s)
3347 {
3348 const SSL_CIPHER *c;
3349
3350 /* require a strong enough cipher */
3351 if (SSL_get_cipher_bits(s, NULL) < 128)
3352 return 0;
3353
3354 /* require ALPN or NPN extension */
3355 if (!s->s3->alpn_selected
3356#ifndef OPENSSL_NO_NEXTPROTONEG
3357 && !s->s3->next_proto_neg_seen
3358#endif
3359 )
3360 {
3361 return 0;
3362 }
3363
3364 /* require a forward-secret cipher */
3365 c = SSL_get_current_cipher(s);
3366 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3367 c->algorithm_mkey != SSL_kEECDH))
3368 {
3369 return 0;
3370 }
3371
3372 return 1;
3373 }
3374
Adam Langley95c29f32014-06-20 12:00:00 -07003375/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3376 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3377 * any. If EVP_MD pointer is passed, initializes ctx with this md
3378 * Returns newly allocated ctx;
3379 */
3380
3381EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3382{
3383 ssl_clear_hash_ctx(hash);
3384 *hash = EVP_MD_CTX_create();
3385 if (md) EVP_DigestInit_ex(*hash,md,NULL);
3386 return *hash;
3387}
3388void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3389{
3390
3391 if (*hash) EVP_MD_CTX_destroy(*hash);
3392 *hash=NULL;
3393}
3394
3395void SSL_set_debug(SSL *s, int debug)
3396 {
3397 s->debug = debug;
3398 }
3399
3400int SSL_cache_hit(SSL *s)
3401 {
3402 return s->hit;
3403 }
3404
3405int SSL_is_server(SSL *s)
3406 {
3407 return s->server;
3408 }