blob: 1ad825ac42fd383a1490eefdd6a0d9d94606b8e0 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57/* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
59 *
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
62 * are met:
63 *
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
66 *
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76 *
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
81 *
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
85 *
86 * 6. Redistributions of any form whatsoever must retain the following
87 * acknowledgment:
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90 *
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
104 *
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
108 *
109 */
110/* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112 * ECC cipher suite support in OpenSSL originally developed by
113 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114 */
115/* ====================================================================
116 * Copyright 2005 Nokia. All rights reserved.
117 *
118 * The portions of the attached software ("Contribution") is developed by
119 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120 * license.
121 *
122 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124 * support (see RFC 4279) to OpenSSL.
125 *
126 * No patent licenses or other rights except those expressly stated in
127 * the OpenSSL open source license shall be deemed granted or received
128 * expressly, by implication, estoppel, or otherwise.
129 *
130 * No assurances are provided by Nokia that the Contribution does not
131 * infringe the patent or other intellectual property rights of any third
132 * party or that the license provides you with all the necessary rights
133 * to make use of the Contribution.
134 *
135 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139 * OTHERWISE. */
140
141#include <stdio.h>
142#include <assert.h>
143
David Benjamin39482a12014-07-20 13:30:15 -0400144#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700145#include <openssl/dh.h>
146#include <openssl/engine.h>
147#include <openssl/lhash.h>
148#include <openssl/mem.h>
149#include <openssl/obj.h>
150#include <openssl/rand.h>
151#include <openssl/x509v3.h>
152
153#include "ssl_locl.h"
154
155SSL3_ENC_METHOD ssl3_undef_enc_method={
156 /* evil casts, but these functions are only called if there's a library bug */
157 (int (*)(SSL *,int))ssl_undefined_function,
158 (int (*)(SSL *, unsigned char *, int))ssl_undefined_function,
159 ssl_undefined_function,
160 (int (*)(SSL *, unsigned char *, unsigned char *, int))ssl_undefined_function,
161 (int (*)(SSL*, int))ssl_undefined_function,
162 (int (*)(SSL *, const char*, int, unsigned char *))ssl_undefined_function,
163 0, /* finish_mac_length */
164 (int (*)(SSL *, int, unsigned char *))ssl_undefined_function,
165 NULL, /* client_finished_label */
166 0, /* client_finished_label_len */
167 NULL, /* server_finished_label */
168 0, /* server_finished_label_len */
169 (int (*)(int))ssl_undefined_function,
170 (int (*)(SSL *, unsigned char *, size_t, const char *,
171 size_t, const unsigned char *, size_t,
172 int use_context)) ssl_undefined_function,
173 };
174
175int SSL_clear(SSL *s)
176 {
177
178 if (s->method == NULL)
179 {
180 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
181 return(0);
182 }
183
184 if (ssl_clear_bad_session(s))
185 {
186 SSL_SESSION_free(s->session);
187 s->session=NULL;
188 }
189
Adam Langley95c29f32014-06-20 12:00:00 -0700190 s->hit=0;
191 s->shutdown=0;
192
193#if 0 /* Disabled since version 1.10 of this file (early return not
194 * needed because SSL_clear is not called when doing renegotiation) */
195 /* This is set if we are doing dynamic renegotiation so keep
196 * the old cipher. It is sort of a SSL_clear_lite :-) */
197 if (s->renegotiate) return(1);
198#else
199 if (s->renegotiate)
200 {
201 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
202 return 0;
203 }
204#endif
205
206 s->type=0;
207
208 s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
209
210 s->version=s->method->version;
211 s->client_version=s->version;
212 s->rwstate=SSL_NOTHING;
213 s->rstate=SSL_ST_READ_HEADER;
214#if 0
215 s->read_ahead=s->ctx->read_ahead;
216#endif
217
218 if (s->init_buf != NULL)
219 {
220 BUF_MEM_free(s->init_buf);
221 s->init_buf=NULL;
222 }
223
224 ssl_clear_cipher_ctx(s);
225 ssl_clear_hash_ctx(&s->read_hash);
226 ssl_clear_hash_ctx(&s->write_hash);
227
228 s->first_packet=0;
229
230#if 1
231 /* Check to see if we were changed into a different method, if
232 * so, revert back if we are not doing session-id reuse. */
233 if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method))
234 {
235 s->method->ssl_free(s);
236 s->method=s->ctx->method;
237 if (!s->method->ssl_new(s))
238 return(0);
239 }
240 else
241#endif
242 s->method->ssl_clear(s);
243 return(1);
244 }
245
246/** Used to change an SSL_CTXs default SSL method type */
247int SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth)
248 {
249 STACK_OF(SSL_CIPHER) *sk;
250
251 ctx->method=meth;
252
Adam Langley858a88d2014-06-20 12:00:00 -0700253 sk=ssl_create_cipher_list(
254 ctx->method, &ctx->cipher_list, &ctx->cipher_list_by_id,
255 meth->version == SSL2_VERSION ?
256 "SSLv2" :
257 SSL_DEFAULT_CIPHER_LIST,
258 ctx->cert);
259
Adam Langley95c29f32014-06-20 12:00:00 -0700260 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
261 {
262 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_ssl_version, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
263 return(0);
264 }
265 return(1);
266 }
267
268SSL *SSL_new(SSL_CTX *ctx)
269 {
270 SSL *s;
271
272 if (ctx == NULL)
273 {
274 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
275 return(NULL);
276 }
277 if (ctx->method == NULL)
278 {
279 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
280 return(NULL);
281 }
282
283 s=(SSL *)OPENSSL_malloc(sizeof(SSL));
284 if (s == NULL) goto err;
285 memset(s,0,sizeof(SSL));
286
287 s->options=ctx->options;
288 s->mode=ctx->mode;
289 s->max_cert_list=ctx->max_cert_list;
290
291 if (ctx->cert != NULL)
292 {
293 /* Earlier library versions used to copy the pointer to
294 * the CERT, not its contents; only when setting new
295 * parameters for the per-SSL copy, ssl_cert_new would be
296 * called (and the direct reference to the per-SSL_CTX
297 * settings would be lost, but those still were indirectly
298 * accessed for various purposes, and for that reason they
299 * used to be known as s->ctx->default_cert).
300 * Now we don't look at the SSL_CTX's CERT after having
301 * duplicated it once. */
302
303 s->cert = ssl_cert_dup(ctx->cert);
304 if (s->cert == NULL)
305 goto err;
306 }
307 else
308 s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
309
310 s->read_ahead=ctx->read_ahead;
311 s->msg_callback=ctx->msg_callback;
312 s->msg_callback_arg=ctx->msg_callback_arg;
313 s->verify_mode=ctx->verify_mode;
314#if 0
315 s->verify_depth=ctx->verify_depth;
316#endif
317 s->sid_ctx_length=ctx->sid_ctx_length;
318 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
319 memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
320 s->verify_callback=ctx->default_verify_callback;
321 s->generate_session_id=ctx->generate_session_id;
322
323 s->param = X509_VERIFY_PARAM_new();
324 if (!s->param)
325 goto err;
326 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
327#if 0
328 s->purpose = ctx->purpose;
329 s->trust = ctx->trust;
330#endif
331 s->quiet_shutdown=ctx->quiet_shutdown;
332 s->max_send_fragment = ctx->max_send_fragment;
333
334 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
335 s->ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700336 s->tlsext_debug_cb = 0;
337 s->tlsext_debug_arg = NULL;
338 s->tlsext_ticket_expected = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700339 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
340 s->initial_ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700341 if (ctx->tlsext_ecpointformatlist)
342 {
343 s->tlsext_ecpointformatlist =
344 BUF_memdup(ctx->tlsext_ecpointformatlist,
345 ctx->tlsext_ecpointformatlist_length);
346 if (!s->tlsext_ecpointformatlist)
347 goto err;
348 s->tlsext_ecpointformatlist_length =
349 ctx->tlsext_ecpointformatlist_length;
350 }
351 if (ctx->tlsext_ellipticcurvelist)
352 {
353 s->tlsext_ellipticcurvelist =
354 BUF_memdup(ctx->tlsext_ellipticcurvelist,
David Benjamin072334d2014-07-13 16:24:27 -0400355 ctx->tlsext_ellipticcurvelist_length * 2);
Adam Langley95c29f32014-06-20 12:00:00 -0700356 if (!s->tlsext_ellipticcurvelist)
357 goto err;
358 s->tlsext_ellipticcurvelist_length =
359 ctx->tlsext_ellipticcurvelist_length;
360 }
Adam Langley95c29f32014-06-20 12:00:00 -0700361 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700362
363 if (s->ctx->alpn_client_proto_list)
364 {
David Benjamin072c9532014-07-26 11:44:25 -0400365 s->alpn_client_proto_list = BUF_memdup(
366 s->ctx->alpn_client_proto_list,
367 s->ctx->alpn_client_proto_list_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700368 if (s->alpn_client_proto_list == NULL)
369 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700370 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
371 }
Adam Langley95c29f32014-06-20 12:00:00 -0700372
373 s->verify_result=X509_V_OK;
374
375 s->method=ctx->method;
376
377 if (!s->method->ssl_new(s))
378 goto err;
379
380 s->references=1;
381 s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
382
383 SSL_clear(s);
384
385 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
386
Adam Langley0289c732014-06-20 12:00:00 -0700387 s->psk_identity_hint = NULL;
388 if (ctx->psk_identity_hint)
389 {
390 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
391 if (s->psk_identity_hint == NULL)
392 goto err;
393 }
Adam Langley95c29f32014-06-20 12:00:00 -0700394 s->psk_client_callback=ctx->psk_client_callback;
395 s->psk_server_callback=ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700396
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200397 if (!s->server)
David Benjamin6c7aed02014-08-27 16:42:38 -0400398 {
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200399 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
David Benjamin6c7aed02014-08-27 16:42:38 -0400400 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
401 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200402
Adam Langley95c29f32014-06-20 12:00:00 -0700403 return(s);
404err:
405 if (s != NULL)
406 {
407 if (s->cert != NULL)
408 ssl_cert_free(s->cert);
409 if (s->ctx != NULL)
410 SSL_CTX_free(s->ctx); /* decrement reference count */
411 OPENSSL_free(s);
412 }
413 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
414 return(NULL);
415 }
416
417int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
418 unsigned int sid_ctx_len)
419 {
420 if(sid_ctx_len > sizeof ctx->sid_ctx)
421 {
422 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
423 return 0;
424 }
425 ctx->sid_ctx_length=sid_ctx_len;
426 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
427
428 return 1;
429 }
430
431int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
432 unsigned int sid_ctx_len)
433 {
434 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
435 {
436 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
437 return 0;
438 }
439 ssl->sid_ctx_length=sid_ctx_len;
440 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
441
442 return 1;
443 }
444
445int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
446 {
447 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
448 ctx->generate_session_id = cb;
449 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
450 return 1;
451 }
452
453int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
454 {
455 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
456 ssl->generate_session_id = cb;
457 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
458 return 1;
459 }
460
461int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
462 unsigned int id_len)
463 {
464 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
465 * we can "construct" a session to give us the desired check - ie. to
466 * find if there's a session in the hash table that would conflict with
467 * any new session built out of this id/id_len and the ssl_version in
468 * use by this SSL. */
469 SSL_SESSION r, *p;
470
471 if(id_len > sizeof r.session_id)
472 return 0;
473
474 r.ssl_version = ssl->version;
475 r.session_id_length = id_len;
476 memcpy(r.session_id, id, id_len);
477 /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
478 * callback is calling us to check the uniqueness of a shorter ID, it
479 * must be compared as a padded-out ID because that is what it will be
480 * converted to when the callback has finished choosing it. */
481 if((r.ssl_version == SSL2_VERSION) &&
482 (id_len < SSL2_SSL_SESSION_ID_LENGTH))
483 {
484 memset(r.session_id + id_len, 0,
485 SSL2_SSL_SESSION_ID_LENGTH - id_len);
486 r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
487 }
488
489 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
490 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
491 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
492 return (p != NULL);
493 }
494
495int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
496 {
497 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
498 }
499
500int SSL_set_purpose(SSL *s, int purpose)
501 {
502 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
503 }
504
505int SSL_CTX_set_trust(SSL_CTX *s, int trust)
506 {
507 return X509_VERIFY_PARAM_set_trust(s->param, trust);
508 }
509
510int SSL_set_trust(SSL *s, int trust)
511 {
512 return X509_VERIFY_PARAM_set_trust(s->param, trust);
513 }
514
515int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
516 {
517 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
518 }
519
520int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
521 {
522 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
523 }
524
Adam Langley858a88d2014-06-20 12:00:00 -0700525void ssl_cipher_preference_list_free(
526 struct ssl_cipher_preference_list_st *cipher_list)
527 {
528 sk_SSL_CIPHER_free(cipher_list->ciphers);
529 OPENSSL_free(cipher_list->in_group_flags);
530 OPENSSL_free(cipher_list);
531 }
532
533struct ssl_cipher_preference_list_st*
534ssl_cipher_preference_list_dup(
535 struct ssl_cipher_preference_list_st *cipher_list)
536 {
537 struct ssl_cipher_preference_list_st* ret = NULL;
538 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
539
540 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
541 if (!ret)
542 goto err;
543 ret->ciphers = NULL;
544 ret->in_group_flags = NULL;
545 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
546 if (!ret->ciphers)
547 goto err;
David Benjamin072c9532014-07-26 11:44:25 -0400548 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
Adam Langley858a88d2014-06-20 12:00:00 -0700549 if (!ret->in_group_flags)
550 goto err;
Adam Langley858a88d2014-06-20 12:00:00 -0700551 return ret;
552
553err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400554 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700555 sk_SSL_CIPHER_free(ret->ciphers);
556 if (ret)
557 OPENSSL_free(ret);
558 return NULL;
559 }
560
561struct ssl_cipher_preference_list_st*
562ssl_cipher_preference_list_from_ciphers(STACK_OF(SSL_CIPHER) *ciphers)
563 {
564 struct ssl_cipher_preference_list_st* ret = NULL;
565 size_t n = sk_SSL_CIPHER_num(ciphers);
566
567 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
568 if (!ret)
569 goto err;
570 ret->ciphers = NULL;
571 ret->in_group_flags = NULL;
572 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
573 if (!ret->ciphers)
574 goto err;
575 ret->in_group_flags = OPENSSL_malloc(n);
576 if (!ret->in_group_flags)
577 goto err;
578 memset(ret->in_group_flags, 0, n);
579 return ret;
580
581err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400582 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700583 sk_SSL_CIPHER_free(ret->ciphers);
584 if (ret)
585 OPENSSL_free(ret);
586 return NULL;
587 }
588
Adam Langley95c29f32014-06-20 12:00:00 -0700589X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
590 {
591 return ctx->param;
592 }
593
594X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
595 {
596 return ssl->param;
597 }
598
599void SSL_certs_clear(SSL *s)
600 {
601 ssl_cert_clear_certs(s->cert);
602 }
603
604void SSL_free(SSL *s)
605 {
606 int i;
607
608 if(s == NULL)
609 return;
610
611 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
612#ifdef REF_PRINT
613 REF_PRINT("SSL",s);
614#endif
615 if (i > 0) return;
616#ifdef REF_CHECK
617 if (i < 0)
618 {
619 fprintf(stderr,"SSL_free, bad reference count\n");
620 abort(); /* ok */
621 }
622#endif
623
624 if (s->param)
625 X509_VERIFY_PARAM_free(s->param);
626
627 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
628
629 if (s->bbio != NULL)
630 {
631 /* If the buffering BIO is in place, pop it off */
632 if (s->bbio == s->wbio)
633 {
634 s->wbio=BIO_pop(s->wbio);
635 }
636 BIO_free(s->bbio);
637 s->bbio=NULL;
638 }
639 if (s->rbio != NULL)
640 BIO_free_all(s->rbio);
641 if ((s->wbio != NULL) && (s->wbio != s->rbio))
642 BIO_free_all(s->wbio);
643
644 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
645
646 /* add extra stuff */
Adam Langley858a88d2014-06-20 12:00:00 -0700647 if (s->cipher_list != NULL)
648 ssl_cipher_preference_list_free(s->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -0700649 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
650
651 /* Make the next call work :-) */
652 if (s->session != NULL)
653 {
654 ssl_clear_bad_session(s);
655 SSL_SESSION_free(s->session);
656 }
657
658 ssl_clear_cipher_ctx(s);
659 ssl_clear_hash_ctx(&s->read_hash);
660 ssl_clear_hash_ctx(&s->write_hash);
661
662 if (s->cert != NULL) ssl_cert_free(s->cert);
663 /* Free up if allocated */
664
Adam Langley95c29f32014-06-20 12:00:00 -0700665 if (s->tlsext_hostname)
666 OPENSSL_free(s->tlsext_hostname);
667 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700668 if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
669 if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -0700670 if (s->alpn_client_proto_list)
671 OPENSSL_free(s->alpn_client_proto_list);
Adam Langley1258b6a2014-06-20 12:00:00 -0700672 if (s->tlsext_channel_id_private)
673 EVP_PKEY_free(s->tlsext_channel_id_private);
Adam Langley95c29f32014-06-20 12:00:00 -0700674
Adam Langley0289c732014-06-20 12:00:00 -0700675 if (s->psk_identity_hint)
676 OPENSSL_free(s->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -0700677
Adam Langley95c29f32014-06-20 12:00:00 -0700678 if (s->client_CA != NULL)
679 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
680
681 if (s->method != NULL) s->method->ssl_free(s);
682
683 if (s->ctx) SSL_CTX_free(s->ctx);
684
Adam Langley95c29f32014-06-20 12:00:00 -0700685 if (s->next_proto_negotiated)
686 OPENSSL_free(s->next_proto_negotiated);
Adam Langley95c29f32014-06-20 12:00:00 -0700687
688 if (s->srtp_profiles)
689 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
690
David Benjamin1b965262014-08-13 20:06:29 -0400691 if (s->tlsext_session_ticket)
692 {
693 OPENSSL_free(s->tlsext_session_ticket);
694 }
695
Adam Langley95c29f32014-06-20 12:00:00 -0700696 OPENSSL_free(s);
697 }
698
699void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
700 {
701 /* If the output buffering BIO is still in place, remove it
702 */
703 if (s->bbio != NULL)
704 {
705 if (s->wbio == s->bbio)
706 {
707 s->wbio=s->wbio->next_bio;
708 s->bbio->next_bio=NULL;
709 }
710 }
711 if ((s->rbio != NULL) && (s->rbio != rbio))
712 BIO_free_all(s->rbio);
713 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
714 BIO_free_all(s->wbio);
715 s->rbio=rbio;
716 s->wbio=wbio;
717 }
718
719BIO *SSL_get_rbio(const SSL *s)
720 { return(s->rbio); }
721
722BIO *SSL_get_wbio(const SSL *s)
723 { return(s->wbio); }
724
725int SSL_get_fd(const SSL *s)
726 {
727 return(SSL_get_rfd(s));
728 }
729
730int SSL_get_rfd(const SSL *s)
731 {
732 int ret= -1;
733 BIO *b,*r;
734
735 b=SSL_get_rbio(s);
736 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
737 if (r != NULL)
738 BIO_get_fd(r,&ret);
739 return(ret);
740 }
741
742int SSL_get_wfd(const SSL *s)
743 {
744 int ret= -1;
745 BIO *b,*r;
746
747 b=SSL_get_wbio(s);
748 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
749 if (r != NULL)
750 BIO_get_fd(r,&ret);
751 return(ret);
752 }
753
754#ifndef OPENSSL_NO_SOCK
755int SSL_set_fd(SSL *s,int fd)
756 {
757 int ret=0;
758 BIO *bio=NULL;
759
760 bio=BIO_new(BIO_s_fd());
761
762 if (bio == NULL)
763 {
764 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
765 goto err;
766 }
767 BIO_set_fd(bio,fd,BIO_NOCLOSE);
768 SSL_set_bio(s,bio,bio);
769 ret=1;
770err:
771 return(ret);
772 }
773
774int SSL_set_wfd(SSL *s,int fd)
775 {
776 int ret=0;
777 BIO *bio=NULL;
778
779 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_FD)
780 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
781 {
782 bio=BIO_new(BIO_s_fd());
783
784 if (bio == NULL)
785 {
786 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
787 goto err;
788 }
789 BIO_set_fd(bio,fd,BIO_NOCLOSE);
790 SSL_set_bio(s,SSL_get_rbio(s),bio);
791 }
792 else
793 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
794 ret=1;
795err:
796 return(ret);
797 }
798
799int SSL_set_rfd(SSL *s,int fd)
800 {
801 int ret=0;
802 BIO *bio=NULL;
803
804 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_FD)
805 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
806 {
807 bio=BIO_new(BIO_s_fd());
808
809 if (bio == NULL)
810 {
811 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
812 goto err;
813 }
814 BIO_set_fd(bio,fd,BIO_NOCLOSE);
815 SSL_set_bio(s,bio,SSL_get_wbio(s));
816 }
817 else
818 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
819 ret=1;
820err:
821 return(ret);
822 }
823#endif
824
825
826/* return length of latest Finished message we sent, copy to 'buf' */
827size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
828 {
829 size_t ret = 0;
830
831 if (s->s3 != NULL)
832 {
833 ret = s->s3->tmp.finish_md_len;
834 if (count > ret)
835 count = ret;
836 memcpy(buf, s->s3->tmp.finish_md, count);
837 }
838 return ret;
839 }
840
841/* return length of latest Finished message we expected, copy to 'buf' */
842size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
843 {
844 size_t ret = 0;
845
846 if (s->s3 != NULL)
847 {
848 ret = s->s3->tmp.peer_finish_md_len;
849 if (count > ret)
850 count = ret;
851 memcpy(buf, s->s3->tmp.peer_finish_md, count);
852 }
853 return ret;
854 }
855
856
857int SSL_get_verify_mode(const SSL *s)
858 {
859 return(s->verify_mode);
860 }
861
862int SSL_get_verify_depth(const SSL *s)
863 {
864 return X509_VERIFY_PARAM_get_depth(s->param);
865 }
866
867int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
868 {
869 return(s->verify_callback);
870 }
871
872int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
873 {
874 return(ctx->verify_mode);
875 }
876
877int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
878 {
879 return X509_VERIFY_PARAM_get_depth(ctx->param);
880 }
881
882int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
883 {
884 return(ctx->default_verify_callback);
885 }
886
887void SSL_set_verify(SSL *s,int mode,
888 int (*callback)(int ok,X509_STORE_CTX *ctx))
889 {
890 s->verify_mode=mode;
891 if (callback != NULL)
892 s->verify_callback=callback;
893 }
894
895void SSL_set_verify_depth(SSL *s,int depth)
896 {
897 X509_VERIFY_PARAM_set_depth(s->param, depth);
898 }
899
900void SSL_set_read_ahead(SSL *s,int yes)
901 {
902 s->read_ahead=yes;
903 }
904
905int SSL_get_read_ahead(const SSL *s)
906 {
907 return(s->read_ahead);
908 }
909
910int SSL_pending(const SSL *s)
911 {
912 /* SSL_pending cannot work properly if read-ahead is enabled
913 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
914 * and it is impossible to fix since SSL_pending cannot report
915 * errors that may be observed while scanning the new data.
916 * (Note that SSL_pending() is often used as a boolean value,
917 * so we'd better not return -1.)
918 */
919 return(s->method->ssl_pending(s));
920 }
921
922X509 *SSL_get_peer_certificate(const SSL *s)
923 {
924 X509 *r;
925
926 if ((s == NULL) || (s->session == NULL))
927 r=NULL;
928 else
929 r=s->session->peer;
930
David Benjamin150c6172014-08-05 22:22:49 -0400931 if (r == NULL)
932 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700933
David Benjamin150c6172014-08-05 22:22:49 -0400934 return X509_up_ref(r);
Adam Langley95c29f32014-06-20 12:00:00 -0700935 }
936
937STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
938 {
939 STACK_OF(X509) *r;
940
941 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
942 r=NULL;
943 else
944 r=s->session->sess_cert->cert_chain;
945
946 /* If we are a client, cert_chain includes the peer's own
947 * certificate; if we are a server, it does not. */
948
949 return(r);
950 }
951
Adam Langley95c29f32014-06-20 12:00:00 -0700952/* Fix this so it checks all the valid key/cert options */
953int SSL_CTX_check_private_key(const SSL_CTX *ctx)
954 {
955 if ( (ctx == NULL) ||
956 (ctx->cert == NULL) ||
957 (ctx->cert->key->x509 == NULL))
958 {
959 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
960 return(0);
961 }
962 if (ctx->cert->key->privatekey == NULL)
963 {
964 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
965 return(0);
966 }
967 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
968 }
969
970/* Fix this function so that it takes an optional type parameter */
971int SSL_check_private_key(const SSL *ssl)
972 {
973 if (ssl == NULL)
974 {
975 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
976 return(0);
977 }
978 if (ssl->cert == NULL)
979 {
980 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
981 return 0;
982 }
983 if (ssl->cert->key->x509 == NULL)
984 {
985 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
986 return(0);
987 }
988 if (ssl->cert->key->privatekey == NULL)
989 {
990 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
991 return(0);
992 }
993 return(X509_check_private_key(ssl->cert->key->x509,
994 ssl->cert->key->privatekey));
995 }
996
997int SSL_accept(SSL *s)
998 {
999 if (s->handshake_func == 0)
1000 /* Not properly initialized yet */
1001 SSL_set_accept_state(s);
1002
1003 return(s->method->ssl_accept(s));
1004 }
1005
1006int SSL_connect(SSL *s)
1007 {
1008 if (s->handshake_func == 0)
1009 /* Not properly initialized yet */
1010 SSL_set_connect_state(s);
1011
1012 return(s->method->ssl_connect(s));
1013 }
1014
1015long SSL_get_default_timeout(const SSL *s)
1016 {
David Benjaminf4501342014-08-14 17:24:37 -04001017 return SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001018 }
1019
1020int SSL_read(SSL *s,void *buf,int num)
1021 {
1022 if (s->handshake_func == 0)
1023 {
1024 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
1025 return -1;
1026 }
1027
1028 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1029 {
1030 s->rwstate=SSL_NOTHING;
1031 return(0);
1032 }
1033 return(s->method->ssl_read(s,buf,num));
1034 }
1035
1036int SSL_peek(SSL *s,void *buf,int num)
1037 {
1038 if (s->handshake_func == 0)
1039 {
1040 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
1041 return -1;
1042 }
1043
1044 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1045 {
1046 return(0);
1047 }
1048 return(s->method->ssl_peek(s,buf,num));
1049 }
1050
1051int SSL_write(SSL *s,const void *buf,int num)
1052 {
1053 if (s->handshake_func == 0)
1054 {
1055 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
1056 return -1;
1057 }
1058
1059 if (s->shutdown & SSL_SENT_SHUTDOWN)
1060 {
1061 s->rwstate=SSL_NOTHING;
1062 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
1063 return(-1);
1064 }
1065 return(s->method->ssl_write(s,buf,num));
1066 }
1067
1068int SSL_shutdown(SSL *s)
1069 {
1070 /* Note that this function behaves differently from what one might
1071 * expect. Return values are 0 for no success (yet),
1072 * 1 for success; but calling it once is usually not enough,
1073 * even if blocking I/O is used (see ssl3_shutdown).
1074 */
1075
1076 if (s->handshake_func == 0)
1077 {
1078 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1079 return -1;
1080 }
1081
Adam Langleyb2cb0ec2014-09-02 14:28:49 -07001082 if (!SSL_in_init(s))
Adam Langley95c29f32014-06-20 12:00:00 -07001083 return(s->method->ssl_shutdown(s));
1084 else
1085 return(1);
1086 }
1087
1088int SSL_renegotiate(SSL *s)
1089 {
1090 if (s->renegotiate == 0)
1091 s->renegotiate=1;
1092
1093 s->new_session=1;
1094
1095 return(s->method->ssl_renegotiate(s));
1096 }
1097
1098int SSL_renegotiate_abbreviated(SSL *s)
1099 {
1100 if (s->renegotiate == 0)
1101 s->renegotiate=1;
1102
1103 s->new_session=0;
1104
1105 return(s->method->ssl_renegotiate(s));
1106 }
1107
1108int SSL_renegotiate_pending(SSL *s)
1109 {
1110 /* becomes true when negotiation is requested;
1111 * false again once a handshake has finished */
1112 return (s->renegotiate != 0);
1113 }
1114
1115long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1116 {
1117 long l;
1118
1119 switch (cmd)
1120 {
1121 case SSL_CTRL_GET_READ_AHEAD:
1122 return(s->read_ahead);
1123 case SSL_CTRL_SET_READ_AHEAD:
1124 l=s->read_ahead;
1125 s->read_ahead=larg;
1126 return(l);
1127
1128 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1129 s->msg_callback_arg = parg;
1130 return 1;
1131
1132 case SSL_CTRL_OPTIONS:
1133 return(s->options|=larg);
1134 case SSL_CTRL_CLEAR_OPTIONS:
1135 return(s->options&=~larg);
1136 case SSL_CTRL_MODE:
1137 return(s->mode|=larg);
1138 case SSL_CTRL_CLEAR_MODE:
1139 return(s->mode &=~larg);
1140 case SSL_CTRL_GET_MAX_CERT_LIST:
1141 return(s->max_cert_list);
1142 case SSL_CTRL_SET_MAX_CERT_LIST:
1143 l=s->max_cert_list;
1144 s->max_cert_list=larg;
1145 return(l);
1146 case SSL_CTRL_SET_MTU:
1147#ifndef OPENSSL_NO_DTLS1
1148 if (larg < (long)dtls1_min_mtu())
1149 return 0;
1150#endif
1151
1152 if (SSL_IS_DTLS(s))
1153 {
1154 s->d1->mtu = larg;
1155 return larg;
1156 }
1157 return 0;
1158 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1159 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1160 return 0;
1161 s->max_send_fragment = larg;
1162 return 1;
1163 case SSL_CTRL_GET_RI_SUPPORT:
1164 if (s->s3)
1165 return s->s3->send_connection_binding;
1166 else return 0;
1167 case SSL_CTRL_CERT_FLAGS:
1168 return(s->cert->cert_flags|=larg);
1169 case SSL_CTRL_CLEAR_CERT_FLAGS:
1170 return(s->cert->cert_flags &=~larg);
1171
1172 case SSL_CTRL_GET_RAW_CIPHERLIST:
1173 if (parg)
1174 {
1175 if (s->cert->ciphers_raw == NULL)
1176 return 0;
1177 *(unsigned char **)parg = s->cert->ciphers_raw;
1178 return (int)s->cert->ciphers_rawlen;
1179 }
1180 else
David Benjamin39482a12014-07-20 13:30:15 -04001181 {
1182 /* Passing a NULL |parg| returns the size of a single
1183 * cipher suite value. */
1184 return 2;
1185 }
Adam Langley95c29f32014-06-20 12:00:00 -07001186 default:
1187 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1188 }
1189 }
1190
1191long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1192 {
1193 switch(cmd)
1194 {
1195 case SSL_CTRL_SET_MSG_CALLBACK:
1196 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1197 return 1;
1198
1199 default:
1200 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1201 }
1202 }
1203
1204LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1205 {
1206 return ctx->sessions;
1207 }
1208
1209long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1210 {
1211 long l;
1212
1213 switch (cmd)
1214 {
1215 case SSL_CTRL_GET_READ_AHEAD:
1216 return(ctx->read_ahead);
1217 case SSL_CTRL_SET_READ_AHEAD:
1218 l=ctx->read_ahead;
1219 ctx->read_ahead=larg;
1220 return(l);
1221
1222 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1223 ctx->msg_callback_arg = parg;
1224 return 1;
1225
1226 case SSL_CTRL_GET_MAX_CERT_LIST:
1227 return(ctx->max_cert_list);
1228 case SSL_CTRL_SET_MAX_CERT_LIST:
1229 l=ctx->max_cert_list;
1230 ctx->max_cert_list=larg;
1231 return(l);
1232
1233 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1234 l=ctx->session_cache_size;
1235 ctx->session_cache_size=larg;
1236 return(l);
1237 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1238 return(ctx->session_cache_size);
1239 case SSL_CTRL_SET_SESS_CACHE_MODE:
1240 l=ctx->session_cache_mode;
1241 ctx->session_cache_mode=larg;
1242 return(l);
1243 case SSL_CTRL_GET_SESS_CACHE_MODE:
1244 return(ctx->session_cache_mode);
1245
1246 case SSL_CTRL_SESS_NUMBER:
1247 return(lh_SSL_SESSION_num_items(ctx->sessions));
1248 case SSL_CTRL_SESS_CONNECT:
1249 return(ctx->stats.sess_connect);
1250 case SSL_CTRL_SESS_CONNECT_GOOD:
1251 return(ctx->stats.sess_connect_good);
1252 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1253 return(ctx->stats.sess_connect_renegotiate);
1254 case SSL_CTRL_SESS_ACCEPT:
1255 return(ctx->stats.sess_accept);
1256 case SSL_CTRL_SESS_ACCEPT_GOOD:
1257 return(ctx->stats.sess_accept_good);
1258 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1259 return(ctx->stats.sess_accept_renegotiate);
1260 case SSL_CTRL_SESS_HIT:
1261 return(ctx->stats.sess_hit);
1262 case SSL_CTRL_SESS_CB_HIT:
1263 return(ctx->stats.sess_cb_hit);
1264 case SSL_CTRL_SESS_MISSES:
1265 return(ctx->stats.sess_miss);
1266 case SSL_CTRL_SESS_TIMEOUTS:
1267 return(ctx->stats.sess_timeout);
1268 case SSL_CTRL_SESS_CACHE_FULL:
1269 return(ctx->stats.sess_cache_full);
1270 case SSL_CTRL_OPTIONS:
1271 return(ctx->options|=larg);
1272 case SSL_CTRL_CLEAR_OPTIONS:
1273 return(ctx->options&=~larg);
1274 case SSL_CTRL_MODE:
1275 return(ctx->mode|=larg);
1276 case SSL_CTRL_CLEAR_MODE:
1277 return(ctx->mode&=~larg);
1278 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1279 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1280 return 0;
1281 ctx->max_send_fragment = larg;
1282 return 1;
1283 case SSL_CTRL_CERT_FLAGS:
1284 return(ctx->cert->cert_flags|=larg);
1285 case SSL_CTRL_CLEAR_CERT_FLAGS:
1286 return(ctx->cert->cert_flags &=~larg);
1287 default:
1288 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1289 }
1290 }
1291
1292long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1293 {
1294 switch(cmd)
1295 {
1296 case SSL_CTRL_SET_MSG_CALLBACK:
1297 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1298 return 1;
1299
1300 default:
1301 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1302 }
1303 }
1304
1305int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1306 {
1307 long l;
1308 const SSL_CIPHER *a = in_a;
1309 const SSL_CIPHER *b = in_b;
1310 const long a_id = a->id;
1311 const long b_id = b->id;
1312
1313 l = a_id - b_id;
1314 if (l == 0L)
1315 return(0);
1316 else
1317 return((l > 0)?1:-1);
1318 }
1319
1320int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1321 {
1322 long l;
1323 const long a_id = (*ap)->id;
1324 const long b_id = (*bp)->id;
1325
1326 l = a_id - b_id;
1327 if (l == 0)
1328 return(0);
1329 else
1330 return((l > 0)?1:-1);
1331 }
1332
1333/** return a STACK of the ciphers available for the SSL and in order of
1334 * preference */
1335STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1336 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001337 if (s == NULL)
1338 return NULL;
1339
1340 if (s->cipher_list != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001341 {
Adam Langley858a88d2014-06-20 12:00:00 -07001342 return(s->cipher_list->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -07001343 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001344
1345 if (s->version >= TLS1_1_VERSION)
1346 {
1347 if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001348 return s->ctx->cipher_list_tls11->ciphers;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001349 }
1350
1351 if ((s->ctx != NULL) &&
1352 (s->ctx->cipher_list != NULL))
1353 {
Adam Langley858a88d2014-06-20 12:00:00 -07001354 return(s->ctx->cipher_list->ciphers);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001355 }
1356
Adam Langley95c29f32014-06-20 12:00:00 -07001357 return(NULL);
1358 }
1359
1360/** return a STACK of the ciphers available for the SSL and in order of
1361 * algorithm id */
1362STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1363 {
1364 if (s != NULL)
1365 {
1366 if (s->cipher_list_by_id != NULL)
1367 {
1368 return(s->cipher_list_by_id);
1369 }
1370 else if ((s->ctx != NULL) &&
1371 (s->ctx->cipher_list_by_id != NULL))
1372 {
1373 return(s->ctx->cipher_list_by_id);
1374 }
1375 }
1376 return(NULL);
1377 }
1378
1379/** The old interface to get the same thing as SSL_get_ciphers() */
1380const char *SSL_get_cipher_list(const SSL *s,int n)
1381 {
David Benjamin6f260012014-08-15 13:49:12 -04001382 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001383 STACK_OF(SSL_CIPHER) *sk;
1384
1385 if (s == NULL) return(NULL);
1386 sk=SSL_get_ciphers(s);
1387 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
1388 return(NULL);
1389 c=sk_SSL_CIPHER_value(sk,n);
1390 if (c == NULL) return(NULL);
1391 return(c->name);
1392 }
1393
1394/** specify the ciphers to be used by default by the SSL_CTX */
1395int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1396 {
1397 STACK_OF(SSL_CIPHER) *sk;
1398
1399 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1400 &ctx->cipher_list_by_id,str, ctx->cert);
1401 /* ssl_create_cipher_list may return an empty stack if it
1402 * was unable to find a cipher matching the given rule string
1403 * (for example if the rule string specifies a cipher which
1404 * has been disabled). This is not an error as far as
1405 * ssl_create_cipher_list is concerned, and hence
1406 * ctx->cipher_list and ctx->cipher_list_by_id has been
1407 * updated. */
1408 if (sk == NULL)
1409 return 0;
1410 else if (sk_SSL_CIPHER_num(sk) == 0)
1411 {
1412 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1413 return 0;
1414 }
1415 return 1;
1416 }
1417
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001418int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str)
1419 {
1420 STACK_OF(SSL_CIPHER) *sk;
1421
1422 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert);
1423 if (sk == NULL)
1424 return 0;
1425 else if (sk_SSL_CIPHER_num(sk) == 0)
1426 {
1427 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH);
1428 return 0;
1429 }
1430 return 1;
1431 }
1432
Adam Langley95c29f32014-06-20 12:00:00 -07001433/** specify the ciphers to be used by the SSL */
1434int SSL_set_cipher_list(SSL *s,const char *str)
1435 {
1436 STACK_OF(SSL_CIPHER) *sk;
1437
1438 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1439 &s->cipher_list_by_id,str, s->cert);
1440 /* see comment in SSL_CTX_set_cipher_list */
1441 if (sk == NULL)
1442 return 0;
1443 else if (sk_SSL_CIPHER_num(sk) == 0)
1444 {
1445 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1446 return 0;
1447 }
1448 return 1;
1449 }
1450
1451/* works well for SSLv2, not so good for SSLv3 */
1452char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
1453 {
1454 char *p;
1455 STACK_OF(SSL_CIPHER) *sk;
David Benjamin6f260012014-08-15 13:49:12 -04001456 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001457 int i;
1458
1459 if ((s->session == NULL) || (s->session->ciphers == NULL) ||
1460 (len < 2))
1461 return(NULL);
1462
1463 p=buf;
1464 sk=s->session->ciphers;
Adam Langley04124042014-06-20 12:00:00 -07001465
1466 if (sk_SSL_CIPHER_num(sk) == 0)
1467 return NULL;
1468
Adam Langley95c29f32014-06-20 12:00:00 -07001469 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1470 {
1471 int n;
1472
1473 c=sk_SSL_CIPHER_value(sk,i);
1474 n=strlen(c->name);
1475 if (n+1 > len)
1476 {
1477 if (p != buf)
1478 --p;
1479 *p='\0';
1480 return buf;
1481 }
1482 strcpy(p,c->name);
1483 p+=n;
1484 *(p++)=':';
1485 len-=n+1;
1486 }
1487 p[-1]='\0';
1488 return(buf);
1489 }
1490
David Benjamin39482a12014-07-20 13:30:15 -04001491int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p)
Adam Langley95c29f32014-06-20 12:00:00 -07001492 {
David Benjamin39482a12014-07-20 13:30:15 -04001493 int i;
David Benjamin6f260012014-08-15 13:49:12 -04001494 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001495 CERT *ct = s->cert;
1496 unsigned char *q;
1497 int no_scsv = s->renegotiate;
1498 /* Set disabled masks for this session */
1499 ssl_set_client_disabled(s);
1500
1501 if (sk == NULL) return(0);
1502 q=p;
1503
1504 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1505 {
1506 c=sk_SSL_CIPHER_value(sk,i);
1507 /* Skip disabled ciphers */
1508 if (c->algorithm_ssl & ct->mask_ssl ||
1509 c->algorithm_mkey & ct->mask_k ||
1510 c->algorithm_auth & ct->mask_a)
1511 continue;
1512#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
1513 if (c->id == SSL3_CK_SCSV)
1514 {
1515 if (no_scsv)
1516 continue;
1517 else
1518 no_scsv = 1;
1519 }
1520#endif
David Benjamin39482a12014-07-20 13:30:15 -04001521 s2n(ssl3_get_cipher_value(c), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001522 }
1523 /* If p == q, no ciphers and caller indicates an error. Otherwise
1524 * add SCSV if not renegotiating.
1525 */
Adam Langleyac61fa32014-06-23 12:03:11 -07001526 if (p != q)
Adam Langley95c29f32014-06-20 12:00:00 -07001527 {
Adam Langleyac61fa32014-06-23 12:03:11 -07001528 if (!no_scsv)
Adam Langley95c29f32014-06-20 12:00:00 -07001529 {
David Benjamin6f260012014-08-15 13:49:12 -04001530 static const SSL_CIPHER scsv =
Adam Langleyac61fa32014-06-23 12:03:11 -07001531 {
1532 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1533 };
David Benjamin39482a12014-07-20 13:30:15 -04001534 s2n(ssl3_get_cipher_value(&scsv), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001535#ifdef OPENSSL_RI_DEBUG
Adam Langleyac61fa32014-06-23 12:03:11 -07001536 fprintf(stderr, "SCSV sent by client\n");
Adam Langley95c29f32014-06-20 12:00:00 -07001537#endif
Adam Langleyac61fa32014-06-23 12:03:11 -07001538 }
1539 if (s->fallback_scsv)
1540 {
David Benjamin6f260012014-08-15 13:49:12 -04001541 static const SSL_CIPHER fallback_scsv =
Adam Langleyac61fa32014-06-23 12:03:11 -07001542 {
1543 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1544 };
David Benjamin39482a12014-07-20 13:30:15 -04001545 s2n(ssl3_get_cipher_value(&fallback_scsv), p);
Adam Langleyac61fa32014-06-23 12:03:11 -07001546 }
Adam Langley95c29f32014-06-20 12:00:00 -07001547 }
1548
1549 return(p-q);
1550 }
1551
David Benjamin39482a12014-07-20 13:30:15 -04001552STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs,
Adam Langley95c29f32014-06-20 12:00:00 -07001553 STACK_OF(SSL_CIPHER) **skp)
1554 {
David Benjamin39482a12014-07-20 13:30:15 -04001555 CBS cipher_suites = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001556 const SSL_CIPHER *c;
1557 STACK_OF(SSL_CIPHER) *sk;
David Benjamin39482a12014-07-20 13:30:15 -04001558
Adam Langley95c29f32014-06-20 12:00:00 -07001559 if (s->s3)
1560 s->s3->send_connection_binding = 0;
1561
David Benjamin39482a12014-07-20 13:30:15 -04001562 if (CBS_len(&cipher_suites) % 2 != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001563 {
1564 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1565 return(NULL);
1566 }
1567 if ((skp == NULL) || (*skp == NULL))
1568 sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
1569 else
1570 {
1571 sk= *skp;
1572 sk_SSL_CIPHER_zero(sk);
1573 }
1574
David Benjamin39482a12014-07-20 13:30:15 -04001575 if (!CBS_stow(&cipher_suites,
1576 &s->cert->ciphers_raw, &s->cert->ciphers_rawlen))
Adam Langley95c29f32014-06-20 12:00:00 -07001577 {
1578 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1579 goto err;
1580 }
Adam Langley95c29f32014-06-20 12:00:00 -07001581
David Benjamin39482a12014-07-20 13:30:15 -04001582 while (CBS_len(&cipher_suites) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001583 {
David Benjamin39482a12014-07-20 13:30:15 -04001584 uint16_t cipher_suite;
1585
1586 if (!CBS_get_u16(&cipher_suites, &cipher_suite))
1587 {
1588 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1589 goto err;
1590 }
1591
Adam Langley95c29f32014-06-20 12:00:00 -07001592 /* Check for SCSV */
David Benjamin39482a12014-07-20 13:30:15 -04001593 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff))
Adam Langley95c29f32014-06-20 12:00:00 -07001594 {
1595 /* SCSV fatal if renegotiating */
1596 if (s->renegotiate)
1597 {
1598 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1599 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1600 goto err;
1601 }
1602 s->s3->send_connection_binding = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001603#ifdef OPENSSL_RI_DEBUG
1604 fprintf(stderr, "SCSV received by server\n");
1605#endif
1606 continue;
1607 }
1608
Adam Langley29707792014-06-20 12:00:00 -07001609 /* Check for FALLBACK_SCSV */
David Benjamin39482a12014-07-20 13:30:15 -04001610 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff) &&
Adam Langley29707792014-06-20 12:00:00 -07001611 s->version < ssl_get_max_version(s))
1612 {
1613 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1614 ssl3_send_alert(s,SSL3_AL_FATAL,SSL3_AD_INAPPROPRIATE_FALLBACK);
1615 goto err;
1616 }
1617
David Benjamin39482a12014-07-20 13:30:15 -04001618 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -07001619 if (c != NULL)
1620 {
1621 if (!sk_SSL_CIPHER_push(sk,c))
1622 {
1623 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1624 goto err;
1625 }
1626 }
1627 }
1628
1629 if (skp != NULL)
1630 *skp=sk;
1631 return(sk);
1632err:
1633 if ((skp == NULL) || (*skp == NULL))
1634 sk_SSL_CIPHER_free(sk);
1635 return(NULL);
1636 }
1637
1638
Adam Langley95c29f32014-06-20 12:00:00 -07001639/** return a servername extension value if provided in Client Hello, or NULL.
1640 * So far, only host_name types are defined (RFC 3546).
1641 */
1642
1643const char *SSL_get_servername(const SSL *s, const int type)
1644 {
1645 if (type != TLSEXT_NAMETYPE_host_name)
1646 return NULL;
1647
1648 return s->session && !s->tlsext_hostname ?
1649 s->session->tlsext_hostname :
1650 s->tlsext_hostname;
1651 }
1652
1653int SSL_get_servername_type(const SSL *s)
1654 {
1655 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1656 return TLSEXT_NAMETYPE_host_name;
1657 return -1;
1658 }
1659
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001660void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx)
1661 {
1662 ctx->signed_cert_timestamps_enabled = 1;
1663 }
1664
1665int SSL_enable_signed_cert_timestamps(SSL *ssl)
1666 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001667 /* Currently not implemented server-side. */
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001668 if (ssl->server)
1669 return 0;
1670
1671 ssl->signed_cert_timestamps_enabled = 1;
1672 return 1;
1673 }
1674
David Benjamin6c7aed02014-08-27 16:42:38 -04001675void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx)
1676 {
1677 ctx->ocsp_stapling_enabled = 1;
1678 }
1679
1680int SSL_enable_ocsp_stapling(SSL *ssl)
1681 {
1682 /* Currently not implemented server-side. */
1683 if (ssl->server)
1684 return 0;
1685 ssl->ocsp_stapling_enabled = 1;
1686 return 1;
1687 }
1688
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001689void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, uint8_t **out, size_t *out_len)
1690 {
Adam Langley3cb50e02014-08-26 14:00:31 -07001691 SSL_SESSION *session = ssl->session;
1692
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001693 *out_len = 0;
1694 *out = NULL;
1695 if (ssl->server)
1696 return;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001697 if (!session || !session->tlsext_signed_cert_timestamp_list)
1698 return;
1699 *out = session->tlsext_signed_cert_timestamp_list;
1700 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1701 }
1702
David Benjamin6c7aed02014-08-27 16:42:38 -04001703void SSL_get0_ocsp_response(const SSL *ssl, uint8_t **out, size_t *out_len)
1704 {
1705 SSL_SESSION *session = ssl->session;
1706
1707 *out_len = 0;
1708 *out = NULL;
1709 if (ssl->server)
1710 return;
1711 if (!session || !session->ocsp_response)
1712 return;
1713 *out = session->ocsp_response;
1714 *out_len = session->ocsp_response_length;
1715 }
1716
Adam Langley95c29f32014-06-20 12:00:00 -07001717/* SSL_select_next_proto implements the standard protocol selection. It is
1718 * expected that this function is called from the callback set by
1719 * SSL_CTX_set_next_proto_select_cb.
1720 *
1721 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1722 * strings. The length byte itself is not included in the length. A byte
1723 * string of length 0 is invalid. No byte string may be truncated.
1724 *
1725 * The current, but experimental algorithm for selecting the protocol is:
1726 *
1727 * 1) If the server doesn't support NPN then this is indicated to the
1728 * callback. In this case, the client application has to abort the connection
1729 * or have a default application level protocol.
1730 *
1731 * 2) If the server supports NPN, but advertises an empty list then the
1732 * client selects the first protcol in its list, but indicates via the
1733 * API that this fallback case was enacted.
1734 *
1735 * 3) Otherwise, the client finds the first protocol in the server's list
1736 * that it supports and selects this protocol. This is because it's
1737 * assumed that the server has better information about which protocol
1738 * a client should use.
1739 *
1740 * 4) If the client doesn't support any of the server's advertised
1741 * protocols, then this is treated the same as case 2.
1742 *
1743 * It returns either
1744 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1745 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1746 */
1747int 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)
1748 {
1749 unsigned int i, j;
1750 const unsigned char *result;
1751 int status = OPENSSL_NPN_UNSUPPORTED;
1752
1753 /* For each protocol in server preference order, see if we support it. */
1754 for (i = 0; i < server_len; )
1755 {
1756 for (j = 0; j < client_len; )
1757 {
1758 if (server[i] == client[j] &&
1759 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1760 {
1761 /* We found a match */
1762 result = &server[i];
1763 status = OPENSSL_NPN_NEGOTIATED;
1764 goto found;
1765 }
1766 j += client[j];
1767 j++;
1768 }
1769 i += server[i];
1770 i++;
1771 }
1772
1773 /* There's no overlap between our protocols and the server's list. */
1774 result = client;
1775 status = OPENSSL_NPN_NO_OVERLAP;
1776
1777 found:
1778 *out = (unsigned char *) result + 1;
1779 *outlen = result[0];
1780 return status;
1781 }
1782
Adam Langley95c29f32014-06-20 12:00:00 -07001783/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1784 * requested protocol for this connection and returns 0. If the client didn't
1785 * request any protocol, then *data is set to NULL.
1786 *
1787 * Note that the client can request any protocol it chooses. The value returned
1788 * from this function need not be a member of the list of supported protocols
1789 * provided by the callback.
1790 */
David Benjaminced551f2014-07-11 23:16:16 -04001791void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, unsigned *len)
Adam Langley95c29f32014-06-20 12:00:00 -07001792 {
1793 *data = s->next_proto_negotiated;
1794 if (!*data) {
1795 *len = 0;
1796 } else {
1797 *len = s->next_proto_negotiated_len;
1798 }
1799}
1800
1801/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1802 * TLS server needs a list of supported protocols for Next Protocol
1803 * Negotiation. The returned list must be in wire format. The list is returned
1804 * by setting |out| to point to it and |outlen| to its length. This memory will
1805 * not be modified, but one should assume that the SSL* keeps a reference to
1806 * it.
1807 *
1808 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1809 * such extension will be included in the ServerHello. */
1810void 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)
1811 {
1812 ctx->next_protos_advertised_cb = cb;
1813 ctx->next_protos_advertised_cb_arg = arg;
1814 }
1815
1816/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1817 * client needs to select a protocol from the server's provided list. |out|
1818 * must be set to point to the selected protocol (which may be within |in|).
1819 * The length of the protocol name must be written into |outlen|. The server's
1820 * advertised protocols are provided in |in| and |inlen|. The callback can
1821 * assume that |in| is syntactically valid.
1822 *
1823 * The client must select a protocol. It is fatal to the connection if this
1824 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1825 */
1826void 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)
1827 {
1828 ctx->next_proto_select_cb = cb;
1829 ctx->next_proto_select_cb_arg = arg;
1830 }
Adam Langley95c29f32014-06-20 12:00:00 -07001831
Adam Langley95c29f32014-06-20 12:00:00 -07001832/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1833 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1834 * length-prefixed strings).
1835 *
1836 * Returns 0 on success. */
1837int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1838 unsigned protos_len)
1839 {
1840 if (ctx->alpn_client_proto_list)
1841 OPENSSL_free(ctx->alpn_client_proto_list);
1842
David Benjamin072c9532014-07-26 11:44:25 -04001843 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001844 if (!ctx->alpn_client_proto_list)
1845 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001846 ctx->alpn_client_proto_list_len = protos_len;
1847
1848 return 0;
1849 }
1850
1851/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1852 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1853 * length-prefixed strings).
1854 *
1855 * Returns 0 on success. */
1856int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1857 unsigned protos_len)
1858 {
1859 if (ssl->alpn_client_proto_list)
1860 OPENSSL_free(ssl->alpn_client_proto_list);
1861
David Benjamin072c9532014-07-26 11:44:25 -04001862 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001863 if (!ssl->alpn_client_proto_list)
1864 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001865 ssl->alpn_client_proto_list_len = protos_len;
1866
1867 return 0;
1868 }
1869
1870/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1871 * during ClientHello processing in order to select an ALPN protocol from the
1872 * client's list of offered protocols. */
1873void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1874 int (*cb) (SSL *ssl,
1875 const unsigned char **out,
1876 unsigned char *outlen,
1877 const unsigned char *in,
1878 unsigned int inlen,
1879 void *arg),
1880 void *arg)
1881 {
1882 ctx->alpn_select_cb = cb;
1883 ctx->alpn_select_cb_arg = arg;
1884 }
1885
1886/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1887 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1888 * including the leading length-prefix byte). If the server didn't respond with
1889 * a negotiated protocol then |*len| will be zero. */
1890void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1891 unsigned *len)
1892 {
1893 *data = NULL;
1894 if (ssl->s3)
1895 *data = ssl->s3->alpn_selected;
1896 if (*data == NULL)
1897 *len = 0;
1898 else
1899 *len = ssl->s3->alpn_selected_len;
1900 }
Adam Langley95c29f32014-06-20 12:00:00 -07001901
1902int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1903 const char *label, size_t llen, const unsigned char *p, size_t plen,
1904 int use_context)
1905 {
1906 if (s->version < TLS1_VERSION)
1907 return -1;
1908
1909 return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1910 llen, p, plen,
1911 use_context);
1912 }
1913
1914static uint32_t ssl_session_hash(const SSL_SESSION *a)
1915 {
1916 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1917 ((uint32_t) a->session_id[1] << 8) ||
1918 ((uint32_t) a->session_id[2] << 16) ||
1919 ((uint32_t) a->session_id[3] << 24);
1920
1921 return hash;
1922 }
1923
1924/* NB: If this function (or indeed the hash function which uses a sort of
1925 * coarser function than this one) is changed, ensure
1926 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1927 * able to construct an SSL_SESSION that will collide with any existing session
1928 * with a matching session ID. */
1929static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1930 {
1931 if (a->ssl_version != b->ssl_version)
1932 return(1);
1933 if (a->session_id_length != b->session_id_length)
1934 return(1);
1935 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1936 }
1937
1938SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1939 {
1940 SSL_CTX *ret=NULL;
1941
1942 if (meth == NULL)
1943 {
1944 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1945 return(NULL);
1946 }
1947
Adam Langley95c29f32014-06-20 12:00:00 -07001948 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1949 {
1950 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1951 goto err;
1952 }
1953 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1954 if (ret == NULL)
1955 goto err;
1956
1957 memset(ret,0,sizeof(SSL_CTX));
1958
1959 ret->method=meth;
1960
1961 ret->cert_store=NULL;
1962 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1963 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1964 ret->session_cache_head=NULL;
1965 ret->session_cache_tail=NULL;
1966
1967 /* We take the system default */
David Benjaminf4501342014-08-14 17:24:37 -04001968 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001969
1970 ret->new_session_cb=0;
1971 ret->remove_session_cb=0;
1972 ret->get_session_cb=0;
1973 ret->generate_session_id=0;
1974
1975 memset((char *)&ret->stats,0,sizeof(ret->stats));
1976
1977 ret->references=1;
1978 ret->quiet_shutdown=0;
1979
Adam Langley95c29f32014-06-20 12:00:00 -07001980 ret->info_callback=NULL;
1981
1982 ret->app_verify_callback=0;
1983 ret->app_verify_arg=NULL;
1984
1985 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1986 ret->read_ahead=0;
1987 ret->msg_callback=0;
1988 ret->msg_callback_arg=NULL;
1989 ret->verify_mode=SSL_VERIFY_NONE;
1990#if 0
1991 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1992#endif
1993 ret->sid_ctx_length=0;
1994 ret->default_verify_callback=NULL;
1995 if ((ret->cert=ssl_cert_new()) == NULL)
1996 goto err;
1997
1998 ret->default_passwd_callback=0;
1999 ret->default_passwd_callback_userdata=NULL;
2000 ret->client_cert_cb=0;
2001 ret->app_gen_cookie_cb=0;
2002 ret->app_verify_cookie_cb=0;
2003
2004 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
2005 if (ret->sessions == NULL) goto err;
2006 ret->cert_store=X509_STORE_new();
2007 if (ret->cert_store == NULL) goto err;
2008
2009 ssl_create_cipher_list(ret->method,
2010 &ret->cipher_list,&ret->cipher_list_by_id,
2011 meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST, ret->cert);
2012 if (ret->cipher_list == NULL
Adam Langley858a88d2014-06-20 12:00:00 -07002013 || sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002014 {
2015 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
2016 goto err2;
2017 }
2018
2019 ret->param = X509_VERIFY_PARAM_new();
2020 if (!ret->param)
2021 goto err;
2022
Adam Langley95c29f32014-06-20 12:00:00 -07002023 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
2024 goto err;
2025
2026 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
2027
2028 ret->extra_certs=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002029
2030 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
2031
Adam Langley95c29f32014-06-20 12:00:00 -07002032 ret->tlsext_servername_callback = 0;
2033 ret->tlsext_servername_arg = NULL;
2034 /* Setup RFC4507 ticket keys */
2035 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
2036 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
2037 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
2038 ret->options |= SSL_OP_NO_TICKET;
2039
2040 ret->tlsext_status_cb = 0;
2041 ret->tlsext_status_arg = NULL;
2042
Adam Langley95c29f32014-06-20 12:00:00 -07002043 ret->next_protos_advertised_cb = 0;
2044 ret->next_proto_select_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002045 ret->psk_identity_hint=NULL;
2046 ret->psk_client_callback=NULL;
2047 ret->psk_server_callback=NULL;
David Benjamin422d3a42014-08-20 11:09:03 -04002048
Adam Langley95c29f32014-06-20 12:00:00 -07002049 /* Default is to connect to non-RI servers. When RI is more widely
2050 * deployed might change this.
2051 */
2052 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
2053
2054 return(ret);
2055err:
2056 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
2057err2:
2058 if (ret != NULL) SSL_CTX_free(ret);
2059 return(NULL);
2060 }
2061
Adam Langley95c29f32014-06-20 12:00:00 -07002062
2063void SSL_CTX_free(SSL_CTX *a)
2064 {
2065 int i;
2066
2067 if (a == NULL) return;
2068
2069 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
2070#ifdef REF_PRINT
2071 REF_PRINT("SSL_CTX",a);
2072#endif
2073 if (i > 0) return;
2074#ifdef REF_CHECK
2075 if (i < 0)
2076 {
2077 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
2078 abort(); /* ok */
2079 }
2080#endif
2081
2082 if (a->param)
2083 X509_VERIFY_PARAM_free(a->param);
2084
2085 /*
2086 * Free internal session cache. However: the remove_cb() may reference
2087 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2088 * after the sessions were flushed.
2089 * As the ex_data handling routines might also touch the session cache,
2090 * the most secure solution seems to be: empty (flush) the cache, then
2091 * free ex_data, then finally free the cache.
2092 * (See ticket [openssl.org #212].)
2093 */
2094 if (a->sessions != NULL)
2095 SSL_CTX_flush_sessions(a,0);
2096
2097 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
2098
2099 if (a->sessions != NULL)
2100 lh_SSL_SESSION_free(a->sessions);
2101
2102 if (a->cert_store != NULL)
2103 X509_STORE_free(a->cert_store);
2104 if (a->cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002105 ssl_cipher_preference_list_free(a->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002106 if (a->cipher_list_by_id != NULL)
2107 sk_SSL_CIPHER_free(a->cipher_list_by_id);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07002108 if (a->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002109 ssl_cipher_preference_list_free(a->cipher_list_tls11);
Adam Langley95c29f32014-06-20 12:00:00 -07002110 if (a->cert != NULL)
2111 ssl_cert_free(a->cert);
2112 if (a->client_CA != NULL)
2113 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2114 if (a->extra_certs != NULL)
2115 sk_X509_pop_free(a->extra_certs,X509_free);
Adam Langley95c29f32014-06-20 12:00:00 -07002116
2117 if (a->srtp_profiles)
2118 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2119
Adam Langley95c29f32014-06-20 12:00:00 -07002120 if (a->psk_identity_hint)
2121 OPENSSL_free(a->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07002122
Adam Langley95c29f32014-06-20 12:00:00 -07002123 if (a->tlsext_ecpointformatlist)
2124 OPENSSL_free(a->tlsext_ecpointformatlist);
2125 if (a->tlsext_ellipticcurvelist)
2126 OPENSSL_free(a->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -07002127 if (a->alpn_client_proto_list != NULL)
2128 OPENSSL_free(a->alpn_client_proto_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002129
Adam Langley1258b6a2014-06-20 12:00:00 -07002130 if (a->tlsext_channel_id_private)
2131 EVP_PKEY_free(a->tlsext_channel_id_private);
Adam Langley1258b6a2014-06-20 12:00:00 -07002132
David Benjamin859ec3c2014-09-02 16:29:36 -04002133 if (a->keylog_bio)
2134 BIO_free(a->keylog_bio);
2135
Adam Langley95c29f32014-06-20 12:00:00 -07002136 OPENSSL_free(a);
2137 }
2138
2139void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2140 {
2141 ctx->default_passwd_callback=cb;
2142 }
2143
2144void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2145 {
2146 ctx->default_passwd_callback_userdata=u;
2147 }
2148
2149void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2150 {
2151 ctx->app_verify_callback=cb;
2152 ctx->app_verify_arg=arg;
2153 }
2154
2155void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2156 {
2157 ctx->verify_mode=mode;
2158 ctx->default_verify_callback=cb;
2159 }
2160
2161void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2162 {
2163 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2164 }
2165
2166void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2167 {
2168 ssl_cert_set_cert_cb(c->cert, cb, arg);
2169 }
2170
2171void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2172 {
2173 ssl_cert_set_cert_cb(s->cert, cb, arg);
2174 }
2175
2176void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
2177 {
2178 CERT_PKEY *cpk;
David Benjaminef2116d2014-08-19 20:21:56 -04002179 int rsa_enc,rsa_sign,dh_tmp;
David Benjamin060d9d22014-07-15 00:54:26 -04002180 unsigned long mask_k,mask_a;
David Benjamin060d9d22014-07-15 00:54:26 -04002181 int have_ecc_cert, ecdsa_ok;
David Benjamin0da0e182014-08-19 16:20:28 -04002182 int have_ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002183 X509 *x = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002184 if (c == NULL) return;
2185
Adam Langley95c29f32014-06-20 12:00:00 -07002186 dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002187
Adam Langley95c29f32014-06-20 12:00:00 -07002188 have_ecdh_tmp=(c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
Adam Langley95c29f32014-06-20 12:00:00 -07002189 cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
2190 rsa_enc= cpk->valid_flags & CERT_PKEY_VALID;
Adam Langley95c29f32014-06-20 12:00:00 -07002191 cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
2192 rsa_sign= cpk->valid_flags & CERT_PKEY_SIGN;
Adam Langley95c29f32014-06-20 12:00:00 -07002193 cpk= &(c->pkeys[SSL_PKEY_ECC]);
Adam Langley95c29f32014-06-20 12:00:00 -07002194 have_ecc_cert= cpk->valid_flags & CERT_PKEY_VALID;
Adam Langley95c29f32014-06-20 12:00:00 -07002195 mask_k=0;
2196 mask_a=0;
Adam Langley95c29f32014-06-20 12:00:00 -07002197
2198#ifdef CIPHER_DEBUG
2199 printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
2200 rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
2201 rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
2202#endif
2203
David Benjamin77a942b2014-07-15 01:22:50 -04002204 if (rsa_enc)
Adam Langley95c29f32014-06-20 12:00:00 -07002205 mask_k|=SSL_kRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002206
Adam Langley95c29f32014-06-20 12:00:00 -07002207 if (dh_tmp)
2208 mask_k|=SSL_kEDH;
2209
Adam Langley95c29f32014-06-20 12:00:00 -07002210 if (rsa_enc || rsa_sign)
2211 {
2212 mask_a|=SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002213 }
2214
Adam Langley95c29f32014-06-20 12:00:00 -07002215 mask_a|=SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002216
David Benjamin0da0e182014-08-19 16:20:28 -04002217 /* An ECC certificate may be usable for ECDSA cipher suites depending on
2218 * the key usage extension. */
Adam Langley95c29f32014-06-20 12:00:00 -07002219 if (have_ecc_cert)
2220 {
2221 cpk = &c->pkeys[SSL_PKEY_ECC];
2222 x = cpk->x509;
2223 /* This call populates extension flags (ex_flags) */
2224 X509_check_purpose(x, -1, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07002225 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2226 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
2227 if (!(cpk->valid_flags & CERT_PKEY_SIGN))
2228 ecdsa_ok = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002229 if (ecdsa_ok)
2230 {
2231 mask_a|=SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002232 }
Adam Langley95c29f32014-06-20 12:00:00 -07002233 }
Adam Langley95c29f32014-06-20 12:00:00 -07002234
Adam Langley95c29f32014-06-20 12:00:00 -07002235 if (have_ecdh_tmp)
2236 {
2237 mask_k|=SSL_kEECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002238 }
Adam Langley95c29f32014-06-20 12:00:00 -07002239
Adam Langley95c29f32014-06-20 12:00:00 -07002240 mask_k |= SSL_kPSK;
2241 mask_a |= SSL_aPSK;
Adam Langley95c29f32014-06-20 12:00:00 -07002242
2243 c->mask_k=mask_k;
2244 c->mask_a=mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002245 c->valid=1;
2246 }
2247
2248/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2249#define ku_reject(x, usage) \
2250 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2251
Adam Langley95c29f32014-06-20 12:00:00 -07002252
2253int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2254 {
David Benjamin0da0e182014-08-19 16:20:28 -04002255 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002256 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2257 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2258
Adam Langley95c29f32014-06-20 12:00:00 -07002259 alg_a = cs->algorithm_auth;
2260
Adam Langley95c29f32014-06-20 12:00:00 -07002261 /* This call populates the ex_flags field correctly */
2262 X509_check_purpose(x, -1, 0);
2263 if ((x->sig_alg) && (x->sig_alg->algorithm))
2264 {
2265 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2266 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2267 }
Adam Langley95c29f32014-06-20 12:00:00 -07002268 if (alg_a & SSL_aECDSA)
2269 {
2270 /* key usage, if present, must allow signing */
2271 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2272 {
2273 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2274 return 0;
2275 }
2276 }
2277
2278 return 1; /* all checks are ok */
2279 }
2280
Adam Langley95c29f32014-06-20 12:00:00 -07002281
2282static int ssl_get_server_cert_index(const SSL *s)
2283 {
2284 int idx;
2285 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2286 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2287 idx = SSL_PKEY_RSA_SIGN;
2288 if (idx == -1)
2289 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2290 return idx;
2291 }
2292
2293CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2294 {
2295 CERT *c;
2296 int i;
2297
2298 c = s->cert;
2299 ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
2300
2301#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2302 /* Broken protocol test: return last used certificate: which may
2303 * mismatch the one expected.
2304 */
2305 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2306 return c->key;
2307#endif
2308
2309 i = ssl_get_server_cert_index(s);
2310
2311 /* This may or may not be an error. */
2312 if (i < 0)
2313 return NULL;
2314
2315 /* May be NULL. */
2316 return &c->pkeys[i];
2317 }
2318
2319EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd)
2320 {
2321 unsigned long alg_a;
2322 CERT *c;
2323 int idx = -1;
2324
2325 alg_a = cipher->algorithm_auth;
2326 c=s->cert;
2327
2328#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2329 /* Broken protocol test: use last key: which may
2330 * mismatch the one expected.
2331 */
2332 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2333 idx = c->key - c->pkeys;
2334 else
2335#endif
2336
David Benjaminef2116d2014-08-19 20:21:56 -04002337 if (alg_a & SSL_aRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002338 {
2339 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2340 idx = SSL_PKEY_RSA_SIGN;
2341 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2342 idx = SSL_PKEY_RSA_ENC;
2343 }
2344 else if ((alg_a & SSL_aECDSA) &&
2345 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2346 idx = SSL_PKEY_ECC;
2347 if (idx == -1)
2348 {
2349 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2350 return(NULL);
2351 }
2352 if (pmd)
2353 *pmd = c->pkeys[idx].digest;
2354 return c->pkeys[idx].privatekey;
2355 }
2356
Adam Langley95c29f32014-06-20 12:00:00 -07002357void ssl_update_cache(SSL *s,int mode)
2358 {
2359 int i;
2360
2361 /* If the session_id_length is 0, we are not supposed to cache it,
2362 * and it would be rather hard to do anyway :-) */
2363 if (s->session->session_id_length == 0) return;
2364
David Benjamin041b58a2014-08-27 15:25:21 -04002365 i=s->initial_ctx->session_cache_mode;
Adam Langley95c29f32014-06-20 12:00:00 -07002366 if ((i & mode) && (!s->hit)
2367 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
David Benjamin041b58a2014-08-27 15:25:21 -04002368 || SSL_CTX_add_session(s->initial_ctx,s->session))
2369 && (s->initial_ctx->new_session_cb != NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07002370 {
2371 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
David Benjamin041b58a2014-08-27 15:25:21 -04002372 if (!s->initial_ctx->new_session_cb(s,s->session))
Adam Langley95c29f32014-06-20 12:00:00 -07002373 SSL_SESSION_free(s->session);
2374 }
2375
2376 /* auto flush every 255 connections */
2377 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2378 ((i & mode) == mode))
2379 {
2380 if ( (((mode & SSL_SESS_CACHE_CLIENT)
David Benjamin041b58a2014-08-27 15:25:21 -04002381 ?s->initial_ctx->stats.sess_connect_good
2382 :s->initial_ctx->stats.sess_accept_good) & 0xff) == 0xff)
Adam Langley95c29f32014-06-20 12:00:00 -07002383 {
David Benjamin041b58a2014-08-27 15:25:21 -04002384 SSL_CTX_flush_sessions(s->initial_ctx,(unsigned long)time(NULL));
Adam Langley95c29f32014-06-20 12:00:00 -07002385 }
2386 }
2387 }
2388
2389const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
2390 {
2391 return ctx->method;
2392 }
2393
2394const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2395 {
2396 return(s->method);
2397 }
2398
2399int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2400 {
2401 int conn= -1;
2402 int ret=1;
2403
2404 if (s->method != meth)
2405 {
2406 if (s->handshake_func != NULL)
2407 conn=(s->handshake_func == s->method->ssl_connect);
2408
2409 if (s->method->version == meth->version)
2410 s->method=meth;
2411 else
2412 {
2413 s->method->ssl_free(s);
2414 s->method=meth;
2415 ret=s->method->ssl_new(s);
2416 }
2417
2418 if (conn == 1)
2419 s->handshake_func=meth->ssl_connect;
2420 else if (conn == 0)
2421 s->handshake_func=meth->ssl_accept;
2422 }
2423 return(ret);
2424 }
2425
2426int SSL_get_error(const SSL *s,int i)
2427 {
2428 int reason;
2429 unsigned long l;
2430 BIO *bio;
2431
2432 if (i > 0) return(SSL_ERROR_NONE);
2433
2434 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2435 * etc, where we do encode the error */
2436 if ((l=ERR_peek_error()) != 0)
2437 {
2438 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2439 return(SSL_ERROR_SYSCALL);
2440 else
2441 return(SSL_ERROR_SSL);
2442 }
2443
Adam Langleyb2ce0582014-06-20 12:00:00 -07002444 if ((i < 0) && SSL_want_session(s))
2445 return(SSL_ERROR_PENDING_SESSION);
2446
Adam Langleydc9b1412014-06-20 12:00:00 -07002447 if ((i < 0) && SSL_want_certificate(s))
2448 return(SSL_ERROR_PENDING_CERTIFICATE);
2449
Adam Langley95c29f32014-06-20 12:00:00 -07002450 if ((i < 0) && SSL_want_read(s))
2451 {
2452 bio=SSL_get_rbio(s);
2453 if (BIO_should_read(bio))
2454 return(SSL_ERROR_WANT_READ);
2455 else if (BIO_should_write(bio))
2456 /* This one doesn't make too much sense ... We never try
2457 * to write to the rbio, and an application program where
2458 * rbio and wbio are separate couldn't even know what it
2459 * should wait for.
2460 * However if we ever set s->rwstate incorrectly
2461 * (so that we have SSL_want_read(s) instead of
2462 * SSL_want_write(s)) and rbio and wbio *are* the same,
2463 * this test works around that bug; so it might be safer
2464 * to keep it. */
2465 return(SSL_ERROR_WANT_WRITE);
2466 else if (BIO_should_io_special(bio))
2467 {
2468 reason=BIO_get_retry_reason(bio);
2469 if (reason == BIO_RR_CONNECT)
2470 return(SSL_ERROR_WANT_CONNECT);
2471 else if (reason == BIO_RR_ACCEPT)
2472 return(SSL_ERROR_WANT_ACCEPT);
2473 else
2474 return(SSL_ERROR_SYSCALL); /* unknown */
2475 }
2476 }
2477
2478 if ((i < 0) && SSL_want_write(s))
2479 {
2480 bio=SSL_get_wbio(s);
2481 if (BIO_should_write(bio))
2482 return(SSL_ERROR_WANT_WRITE);
2483 else if (BIO_should_read(bio))
2484 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2485 return(SSL_ERROR_WANT_READ);
2486 else if (BIO_should_io_special(bio))
2487 {
2488 reason=BIO_get_retry_reason(bio);
2489 if (reason == BIO_RR_CONNECT)
2490 return(SSL_ERROR_WANT_CONNECT);
2491 else if (reason == BIO_RR_ACCEPT)
2492 return(SSL_ERROR_WANT_ACCEPT);
2493 else
2494 return(SSL_ERROR_SYSCALL);
2495 }
2496 }
2497 if ((i < 0) && SSL_want_x509_lookup(s))
2498 {
2499 return(SSL_ERROR_WANT_X509_LOOKUP);
2500 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002501 if ((i < 0) && SSL_want_channel_id_lookup(s))
2502 {
2503 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2504 }
Adam Langley95c29f32014-06-20 12:00:00 -07002505
2506 if (i == 0)
2507 {
2508 if (s->version == SSL2_VERSION)
2509 {
2510 /* assume it is the socket being closed */
2511 return(SSL_ERROR_ZERO_RETURN);
2512 }
2513 else
2514 {
2515 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2516 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2517 return(SSL_ERROR_ZERO_RETURN);
2518 }
2519 }
2520 return(SSL_ERROR_SYSCALL);
2521 }
2522
2523int SSL_do_handshake(SSL *s)
2524 {
2525 int ret=1;
2526
2527 if (s->handshake_func == NULL)
2528 {
2529 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2530 return(-1);
2531 }
2532
2533 s->method->ssl_renegotiate_check(s);
2534
2535 if (SSL_in_init(s) || SSL_in_before(s))
2536 {
2537 ret=s->handshake_func(s);
2538 }
2539 return(ret);
2540 }
2541
2542/* For the next 2 functions, SSL_clear() sets shutdown and so
2543 * one of these calls will reset it */
2544void SSL_set_accept_state(SSL *s)
2545 {
2546 s->server=1;
2547 s->shutdown=0;
2548 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2549 s->handshake_func=s->method->ssl_accept;
2550 /* clear the current cipher */
2551 ssl_clear_cipher_ctx(s);
2552 ssl_clear_hash_ctx(&s->read_hash);
2553 ssl_clear_hash_ctx(&s->write_hash);
2554 }
2555
2556void SSL_set_connect_state(SSL *s)
2557 {
2558 s->server=0;
2559 s->shutdown=0;
2560 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2561 s->handshake_func=s->method->ssl_connect;
2562 /* clear the current cipher */
2563 ssl_clear_cipher_ctx(s);
2564 ssl_clear_hash_ctx(&s->read_hash);
2565 ssl_clear_hash_ctx(&s->write_hash);
2566 }
2567
2568int ssl_undefined_function(SSL *s)
2569 {
2570 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2571 return(0);
2572 }
2573
2574int ssl_undefined_void_function(void)
2575 {
2576 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2577 return(0);
2578 }
2579
2580int ssl_undefined_const_function(const SSL *s)
2581 {
2582 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2583 return(0);
2584 }
2585
2586SSL_METHOD *ssl_bad_method(int ver)
2587 {
2588 OPENSSL_PUT_ERROR(SSL, ssl_bad_method, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2589 return(NULL);
2590 }
2591
Adam Langley0f4746e2014-08-13 12:26:32 -07002592static const char *ssl_get_version(int version)
Adam Langley95c29f32014-06-20 12:00:00 -07002593 {
Adam Langley0f4746e2014-08-13 12:26:32 -07002594 if (version == TLS1_2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002595 return("TLSv1.2");
Adam Langley0f4746e2014-08-13 12:26:32 -07002596 else if (version == TLS1_1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002597 return("TLSv1.1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002598 else if (version == TLS1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002599 return("TLSv1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002600 else if (version == SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002601 return("SSLv3");
Adam Langley0f4746e2014-08-13 12:26:32 -07002602 else if (version == SSL2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002603 return("SSLv2");
2604 else
2605 return("unknown");
2606 }
2607
Adam Langley0f4746e2014-08-13 12:26:32 -07002608const char *SSL_get_version(const SSL *s)
2609 {
2610 return ssl_get_version(s->version);
2611 }
2612
2613const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2614 {
2615 return ssl_get_version(sess->ssl_version);
2616 }
2617
Adam Langley95c29f32014-06-20 12:00:00 -07002618void ssl_clear_cipher_ctx(SSL *s)
2619 {
2620 if (s->enc_read_ctx != NULL)
2621 {
2622 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2623 OPENSSL_free(s->enc_read_ctx);
2624 s->enc_read_ctx=NULL;
2625 }
2626 if (s->enc_write_ctx != NULL)
2627 {
2628 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2629 OPENSSL_free(s->enc_write_ctx);
2630 s->enc_write_ctx=NULL;
2631 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002632 if (s->aead_read_ctx != NULL)
2633 {
2634 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2635 OPENSSL_free(s->aead_read_ctx);
2636 s->aead_read_ctx = NULL;
2637 }
2638 if (s->aead_write_ctx != NULL)
2639 {
2640 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2641 OPENSSL_free(s->aead_write_ctx);
2642 s->aead_write_ctx = NULL;
2643 }
Adam Langley95c29f32014-06-20 12:00:00 -07002644 }
2645
2646X509 *SSL_get_certificate(const SSL *s)
2647 {
2648 if (s->cert != NULL)
2649 return(s->cert->key->x509);
2650 else
2651 return(NULL);
2652 }
2653
2654EVP_PKEY *SSL_get_privatekey(const SSL *s)
2655 {
2656 if (s->cert != NULL)
2657 return(s->cert->key->privatekey);
2658 else
2659 return(NULL);
2660 }
2661
2662X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2663 {
2664 if (ctx->cert != NULL)
2665 return ctx->cert->key->x509;
2666 else
2667 return NULL;
2668 }
2669
2670EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2671 {
2672 if (ctx->cert != NULL)
2673 return ctx->cert->key->privatekey;
2674 else
2675 return NULL ;
2676 }
2677
2678const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2679 {
2680 if ((s->session != NULL) && (s->session->cipher != NULL))
2681 return(s->session->cipher);
2682 return(NULL);
2683 }
2684const void *SSL_get_current_compression(SSL *s)
2685 {
2686 return NULL;
2687 }
2688const void *SSL_get_current_expansion(SSL *s)
2689 {
2690 return NULL;
2691 }
2692
2693int ssl_init_wbio_buffer(SSL *s,int push)
2694 {
2695 BIO *bbio;
2696
2697 if (s->bbio == NULL)
2698 {
2699 bbio=BIO_new(BIO_f_buffer());
2700 if (bbio == NULL) return(0);
2701 s->bbio=bbio;
2702 }
2703 else
2704 {
2705 bbio=s->bbio;
2706 if (s->bbio == s->wbio)
2707 s->wbio=BIO_pop(s->wbio);
2708 }
2709 (void)BIO_reset(bbio);
2710/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2711 if (!BIO_set_read_buffer_size(bbio,1))
2712 {
2713 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2714 return(0);
2715 }
2716 if (push)
2717 {
2718 if (s->wbio != bbio)
2719 s->wbio=BIO_push(bbio,s->wbio);
2720 }
2721 else
2722 {
2723 if (s->wbio == bbio)
2724 s->wbio=BIO_pop(bbio);
2725 }
2726 return(1);
2727 }
2728
2729void ssl_free_wbio_buffer(SSL *s)
2730 {
2731 if (s->bbio == NULL) return;
2732
2733 if (s->bbio == s->wbio)
2734 {
2735 /* remove buffering */
2736 s->wbio=BIO_pop(s->wbio);
2737#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2738 assert(s->wbio != NULL);
2739#endif
2740 }
2741 BIO_free(s->bbio);
2742 s->bbio=NULL;
2743 }
2744
2745void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2746 {
2747 ctx->quiet_shutdown=mode;
2748 }
2749
2750int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2751 {
2752 return(ctx->quiet_shutdown);
2753 }
2754
2755void SSL_set_quiet_shutdown(SSL *s,int mode)
2756 {
2757 s->quiet_shutdown=mode;
2758 }
2759
2760int SSL_get_quiet_shutdown(const SSL *s)
2761 {
2762 return(s->quiet_shutdown);
2763 }
2764
2765void SSL_set_shutdown(SSL *s,int mode)
2766 {
2767 s->shutdown=mode;
2768 }
2769
2770int SSL_get_shutdown(const SSL *s)
2771 {
2772 return(s->shutdown);
2773 }
2774
2775int SSL_version(const SSL *s)
2776 {
2777 return(s->version);
2778 }
2779
2780SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2781 {
2782 return(ssl->ctx);
2783 }
2784
2785SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2786 {
2787 if (ssl->ctx == ctx)
2788 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002789 if (ctx == NULL)
2790 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002791 if (ssl->cert != NULL)
2792 ssl_cert_free(ssl->cert);
2793 ssl->cert = ssl_cert_dup(ctx->cert);
2794 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2795 if (ssl->ctx != NULL)
2796 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2797 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07002798
2799 ssl->sid_ctx_length = ctx->sid_ctx_length;
2800 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2801 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2802
Adam Langley95c29f32014-06-20 12:00:00 -07002803 return(ssl->ctx);
2804 }
2805
2806#ifndef OPENSSL_NO_STDIO
2807int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2808 {
2809 return(X509_STORE_set_default_paths(ctx->cert_store));
2810 }
2811
2812int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2813 const char *CApath)
2814 {
2815 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2816 }
2817#endif
2818
2819void SSL_set_info_callback(SSL *ssl,
2820 void (*cb)(const SSL *ssl,int type,int val))
2821 {
2822 ssl->info_callback=cb;
2823 }
2824
2825/* One compiler (Diab DCC) doesn't like argument names in returned
2826 function pointer. */
2827void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2828 {
2829 return ssl->info_callback;
2830 }
2831
2832int SSL_state(const SSL *ssl)
2833 {
2834 return(ssl->state);
2835 }
2836
2837void SSL_set_state(SSL *ssl, int state)
2838 {
2839 ssl->state = state;
2840 }
2841
2842void SSL_set_verify_result(SSL *ssl,long arg)
2843 {
2844 ssl->verify_result=arg;
2845 }
2846
2847long SSL_get_verify_result(const SSL *ssl)
2848 {
2849 return(ssl->verify_result);
2850 }
2851
2852int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2853 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2854 {
2855 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2856 new_func, dup_func, free_func);
2857 }
2858
2859int SSL_set_ex_data(SSL *s,int idx,void *arg)
2860 {
2861 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2862 }
2863
2864void *SSL_get_ex_data(const SSL *s,int idx)
2865 {
2866 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2867 }
2868
2869int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2870 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2871 {
2872 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2873 new_func, dup_func, free_func);
2874 }
2875
2876int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2877 {
2878 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2879 }
2880
2881void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2882 {
2883 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2884 }
2885
2886int ssl_ok(SSL *s)
2887 {
2888 return(1);
2889 }
2890
2891X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2892 {
2893 return(ctx->cert_store);
2894 }
2895
2896void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2897 {
2898 if (ctx->cert_store != NULL)
2899 X509_STORE_free(ctx->cert_store);
2900 ctx->cert_store=store;
2901 }
2902
2903int SSL_want(const SSL *s)
2904 {
2905 return(s->rwstate);
2906 }
2907
2908/*!
2909 * \brief Set the callback for generating temporary RSA keys.
2910 * \param ctx the SSL context.
2911 * \param cb the callback
2912 */
2913
Adam Langley95c29f32014-06-20 12:00:00 -07002914void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2915 int is_export,
2916 int keylength))
2917 {
2918 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2919 }
2920
2921void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2922 int is_export,
2923 int keylength))
2924 {
2925 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2926 }
Adam Langley95c29f32014-06-20 12:00:00 -07002927
2928#ifdef DOXYGEN
2929/*!
2930 * \brief The RSA temporary key callback function.
2931 * \param ssl the SSL session.
2932 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2933 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2934 * of the required key in bits.
2935 * \return the temporary RSA key.
2936 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2937 */
2938
2939RSA *cb(SSL *ssl,int is_export,int keylength)
2940 {}
2941#endif
2942
2943/*!
2944 * \brief Set the callback for generating temporary DH keys.
2945 * \param ctx the SSL context.
2946 * \param dh the callback
2947 */
2948
Adam Langley95c29f32014-06-20 12:00:00 -07002949void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2950 int keylength))
2951 {
2952 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2953 }
2954
2955void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2956 int keylength))
2957 {
2958 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2959 }
Adam Langley95c29f32014-06-20 12:00:00 -07002960
Adam Langley95c29f32014-06-20 12:00:00 -07002961void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2962 int keylength))
2963 {
2964 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2965 }
2966
2967void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2968 int keylength))
2969 {
2970 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2971 }
Adam Langley95c29f32014-06-20 12:00:00 -07002972
Adam Langley95c29f32014-06-20 12:00:00 -07002973int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2974 {
2975 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2976 {
2977 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2978 return 0;
2979 }
2980 if (ctx->psk_identity_hint != NULL)
2981 OPENSSL_free(ctx->psk_identity_hint);
2982 if (identity_hint != NULL)
2983 {
2984 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2985 if (ctx->psk_identity_hint == NULL)
2986 return 0;
2987 }
2988 else
2989 ctx->psk_identity_hint = NULL;
2990 return 1;
2991 }
2992
2993int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
2994 {
2995 if (s == NULL)
2996 return 0;
2997
Adam Langley95c29f32014-06-20 12:00:00 -07002998 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2999 {
3000 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
3001 return 0;
3002 }
Adam Langley0289c732014-06-20 12:00:00 -07003003
3004 /* Clear hint in SSL and associated SSL_SESSION (if any). */
3005 if (s->psk_identity_hint != NULL)
3006 {
3007 OPENSSL_free(s->psk_identity_hint);
3008 s->psk_identity_hint = NULL;
3009 }
3010 if (s->session != NULL && s->session->psk_identity_hint != NULL)
3011 {
Adam Langley95c29f32014-06-20 12:00:00 -07003012 OPENSSL_free(s->session->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -07003013 s->session->psk_identity_hint = NULL;
3014 }
3015
Adam Langley95c29f32014-06-20 12:00:00 -07003016 if (identity_hint != NULL)
3017 {
Adam Langley0289c732014-06-20 12:00:00 -07003018 /* The hint is stored in SSL and SSL_SESSION with the one in
3019 * SSL_SESSION taking precedence. Thus, if SSL_SESSION is avaiable,
3020 * we store the hint there, otherwise we store it in SSL. */
3021 if (s->session != NULL)
3022 {
3023 s->session->psk_identity_hint = BUF_strdup(identity_hint);
3024 if (s->session->psk_identity_hint == NULL)
3025 return 0;
3026 }
3027 else
3028 {
3029 s->psk_identity_hint = BUF_strdup(identity_hint);
3030 if (s->psk_identity_hint == NULL)
3031 return 0;
3032 }
Adam Langley95c29f32014-06-20 12:00:00 -07003033 }
Adam Langley95c29f32014-06-20 12:00:00 -07003034 return 1;
3035 }
3036
3037const char *SSL_get_psk_identity_hint(const SSL *s)
3038 {
Adam Langley0289c732014-06-20 12:00:00 -07003039 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07003040 return NULL;
Adam Langley0289c732014-06-20 12:00:00 -07003041 /* The hint is stored in SSL and SSL_SESSION with the one in SSL_SESSION
3042 * taking precedence. */
3043 if (s->session != NULL)
3044 return(s->session->psk_identity_hint);
3045 return(s->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07003046 }
3047
3048const char *SSL_get_psk_identity(const SSL *s)
3049 {
3050 if (s == NULL || s->session == NULL)
3051 return NULL;
3052 return(s->session->psk_identity);
3053 }
3054
3055void SSL_set_psk_client_callback(SSL *s,
3056 unsigned int (*cb)(SSL *ssl, const char *hint,
3057 char *identity, unsigned int max_identity_len, unsigned char *psk,
3058 unsigned int max_psk_len))
3059 {
3060 s->psk_client_callback = cb;
3061 }
3062
3063void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
3064 unsigned int (*cb)(SSL *ssl, const char *hint,
3065 char *identity, unsigned int max_identity_len, unsigned char *psk,
3066 unsigned int max_psk_len))
3067 {
3068 ctx->psk_client_callback = cb;
3069 }
3070
3071void SSL_set_psk_server_callback(SSL *s,
3072 unsigned int (*cb)(SSL *ssl, const char *identity,
3073 unsigned char *psk, unsigned int max_psk_len))
3074 {
3075 s->psk_server_callback = cb;
3076 }
3077
3078void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
3079 unsigned int (*cb)(SSL *ssl, const char *identity,
3080 unsigned char *psk, unsigned int max_psk_len))
3081 {
3082 ctx->psk_server_callback = cb;
3083 }
Adam Langley95c29f32014-06-20 12:00:00 -07003084
3085void 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))
3086 {
3087 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3088 }
3089void 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))
3090 {
3091 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3092 }
3093
David Benjamin859ec3c2014-09-02 16:29:36 -04003094void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio)
3095 {
3096 if (ctx->keylog_bio != NULL)
3097 BIO_free(ctx->keylog_bio);
3098 ctx->keylog_bio = keylog_bio;
3099 }
3100
3101static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len)
3102 {
3103 static const char hextable[] = "0123456789abcdef";
3104 uint8_t *out;
3105 size_t i;
3106
3107 if (!CBB_add_space(cbb, &out, in_len * 2))
3108 {
3109 return 0;
3110 }
3111
3112 for (i = 0; i < in_len; i++)
3113 {
3114 *(out++) = (uint8_t)hextable[in[i] >> 4];
3115 *(out++) = (uint8_t)hextable[in[i] & 0xf];
3116 }
3117 return 1;
3118 }
3119
3120int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
3121 const uint8_t *encrypted_premaster, size_t encrypted_premaster_len,
3122 const uint8_t *premaster, size_t premaster_len)
3123 {
3124 BIO *bio = ctx->keylog_bio;
3125 CBB cbb;
3126 uint8_t *out;
3127 size_t out_len;
3128 int ret;
3129
3130 if (bio == NULL)
3131 {
3132 return 1;
3133 }
3134
3135 if (encrypted_premaster_len < 8)
3136 {
3137 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange, ERR_R_INTERNAL_ERROR);
3138 return 0;
3139 }
3140
3141 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len*2 + 1))
3142 {
3143 return 0;
3144 }
3145 if (!CBB_add_bytes(&cbb, (const uint8_t*)"RSA ", 4) ||
3146 /* Only the first 8 bytes of the encrypted premaster secret are
3147 * logged. */
3148 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
3149 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3150 !cbb_add_hex(&cbb, premaster, premaster_len) ||
3151 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3152 !CBB_finish(&cbb, &out, &out_len))
3153 {
3154 CBB_cleanup(&cbb);
3155 return 0;
3156 }
3157
3158 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3159 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3160 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3161
3162 OPENSSL_free(out);
3163 return ret;
3164 }
3165
3166int ssl_ctx_log_master_secret(SSL_CTX *ctx,
3167 const uint8_t *client_random, size_t client_random_len,
3168 const uint8_t *master, size_t master_len)
3169 {
3170 BIO *bio = ctx->keylog_bio;
3171 CBB cbb;
3172 uint8_t *out;
3173 size_t out_len;
3174 int ret;
3175
3176 if (bio == NULL)
3177 {
3178 return 1;
3179 }
3180
3181 if (client_random_len != 32)
3182 {
3183 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
3184 return 0;
3185 }
3186
3187 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len*2 + 1))
3188 {
3189 return 0;
3190 }
3191 if (!CBB_add_bytes(&cbb, (const uint8_t*)"CLIENT_RANDOM ", 14) ||
3192 !cbb_add_hex(&cbb, client_random, 32) ||
3193 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3194 !cbb_add_hex(&cbb, master, master_len) ||
3195 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3196 !CBB_finish(&cbb, &out, &out_len))
3197 {
3198 CBB_cleanup(&cbb);
3199 return 0;
3200 }
3201
3202 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3203 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3204 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3205
3206 OPENSSL_free(out);
3207 return ret;
3208 }
3209
Adam Langleyadb739e2014-06-20 12:00:00 -07003210int SSL_cutthrough_complete(const SSL *s)
3211 {
3212 return (!s->server && /* cutthrough only applies to clients */
3213 !s->hit && /* full-handshake */
3214 s->version >= SSL3_VERSION &&
3215 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3216 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3217 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3218 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3219 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
David Benjamin7e3305e2014-07-28 14:52:32 -04003220 s->state == SSL3_ST_CR_CHANGE ||
Adam Langleyadb739e2014-06-20 12:00:00 -07003221 s->state == SSL3_ST_CR_FINISHED_A));
3222 }
3223
Adam Langley95f22882014-06-20 12:00:00 -07003224void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3225 size_t* ssl_session_size)
3226{
3227 *ssl_size = sizeof(SSL);
3228 *ssl_ctx_size = sizeof(SSL_CTX);
3229 *ssl_session_size = sizeof(SSL_SESSION);
3230}
3231
Adam Langleyadb739e2014-06-20 12:00:00 -07003232int ssl3_can_cutthrough(const SSL *s)
3233 {
3234 const SSL_CIPHER *c;
3235
3236 /* require a strong enough cipher */
3237 if (SSL_get_cipher_bits(s, NULL) < 128)
3238 return 0;
3239
3240 /* require ALPN or NPN extension */
David Benjamin457112e2014-08-27 14:35:09 -04003241 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen)
Adam Langleyadb739e2014-06-20 12:00:00 -07003242 {
3243 return 0;
3244 }
3245
3246 /* require a forward-secret cipher */
3247 c = SSL_get_current_cipher(s);
3248 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3249 c->algorithm_mkey != SSL_kEECDH))
3250 {
3251 return 0;
3252 }
3253
3254 return 1;
3255 }
3256
Adam Langley29707792014-06-20 12:00:00 -07003257/* ssl_get_max_version returns the maximum SSL/TLS version number supported by
3258 * |s|, or zero if all versions are disabled. */
3259int ssl_get_max_version(const SSL *s)
3260 {
3261 /* Only one version supported for DTLS. */
3262 if (s->version == DTLS1_VERSION)
3263 return DTLS1_VERSION;
3264
3265 if (!(s->options & SSL_OP_NO_TLSv1_2))
3266 return TLS1_2_VERSION;
3267 if (!(s->options & SSL_OP_NO_TLSv1_1))
3268 return TLS1_1_VERSION;
3269 if (!(s->options & SSL_OP_NO_TLSv1))
3270 return TLS1_VERSION;
3271 if (!(s->options & SSL_OP_NO_SSLv3))
3272 return SSL3_VERSION;
3273 if (!(s->options & SSL_OP_NO_SSLv2))
3274 return SSL2_VERSION;
3275 return 0;
3276 }
3277
Adam Langley95c29f32014-06-20 12:00:00 -07003278/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3279 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3280 * any. If EVP_MD pointer is passed, initializes ctx with this md
3281 * Returns newly allocated ctx;
3282 */
3283
3284EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3285{
3286 ssl_clear_hash_ctx(hash);
3287 *hash = EVP_MD_CTX_create();
3288 if (md) EVP_DigestInit_ex(*hash,md,NULL);
3289 return *hash;
3290}
3291void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3292{
3293
3294 if (*hash) EVP_MD_CTX_destroy(*hash);
3295 *hash=NULL;
3296}
3297
3298void SSL_set_debug(SSL *s, int debug)
3299 {
3300 s->debug = debug;
3301 }
3302
3303int SSL_cache_hit(SSL *s)
3304 {
3305 return s->hit;
3306 }
3307
3308int SSL_is_server(SSL *s)
3309 {
3310 return s->server;
3311 }