blob: 58684b0e2affca65a8ed2dc42fb6406bef3008a3 [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
1082 if ((s != NULL) && !SSL_in_init(s))
1083 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
Adam Langley95c29f32014-06-20 12:00:00 -07002133 OPENSSL_free(a);
2134 }
2135
2136void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2137 {
2138 ctx->default_passwd_callback=cb;
2139 }
2140
2141void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2142 {
2143 ctx->default_passwd_callback_userdata=u;
2144 }
2145
2146void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2147 {
2148 ctx->app_verify_callback=cb;
2149 ctx->app_verify_arg=arg;
2150 }
2151
2152void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2153 {
2154 ctx->verify_mode=mode;
2155 ctx->default_verify_callback=cb;
2156 }
2157
2158void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2159 {
2160 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2161 }
2162
2163void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2164 {
2165 ssl_cert_set_cert_cb(c->cert, cb, arg);
2166 }
2167
2168void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2169 {
2170 ssl_cert_set_cert_cb(s->cert, cb, arg);
2171 }
2172
2173void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
2174 {
2175 CERT_PKEY *cpk;
David Benjaminef2116d2014-08-19 20:21:56 -04002176 int rsa_enc,rsa_sign,dh_tmp;
David Benjamin060d9d22014-07-15 00:54:26 -04002177 unsigned long mask_k,mask_a;
David Benjamin060d9d22014-07-15 00:54:26 -04002178 int have_ecc_cert, ecdsa_ok;
David Benjamin0da0e182014-08-19 16:20:28 -04002179 int have_ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002180 X509 *x = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002181 if (c == NULL) return;
2182
Adam Langley95c29f32014-06-20 12:00:00 -07002183 dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002184
Adam Langley95c29f32014-06-20 12:00:00 -07002185 have_ecdh_tmp=(c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
Adam Langley95c29f32014-06-20 12:00:00 -07002186 cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
2187 rsa_enc= cpk->valid_flags & CERT_PKEY_VALID;
Adam Langley95c29f32014-06-20 12:00:00 -07002188 cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
2189 rsa_sign= cpk->valid_flags & CERT_PKEY_SIGN;
Adam Langley95c29f32014-06-20 12:00:00 -07002190 cpk= &(c->pkeys[SSL_PKEY_ECC]);
Adam Langley95c29f32014-06-20 12:00:00 -07002191 have_ecc_cert= cpk->valid_flags & CERT_PKEY_VALID;
Adam Langley95c29f32014-06-20 12:00:00 -07002192 mask_k=0;
2193 mask_a=0;
Adam Langley95c29f32014-06-20 12:00:00 -07002194
2195#ifdef CIPHER_DEBUG
2196 printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
2197 rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
2198 rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
2199#endif
2200
David Benjamin77a942b2014-07-15 01:22:50 -04002201 if (rsa_enc)
Adam Langley95c29f32014-06-20 12:00:00 -07002202 mask_k|=SSL_kRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002203
Adam Langley95c29f32014-06-20 12:00:00 -07002204 if (dh_tmp)
2205 mask_k|=SSL_kEDH;
2206
Adam Langley95c29f32014-06-20 12:00:00 -07002207 if (rsa_enc || rsa_sign)
2208 {
2209 mask_a|=SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002210 }
2211
Adam Langley95c29f32014-06-20 12:00:00 -07002212 mask_a|=SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002213
David Benjamin0da0e182014-08-19 16:20:28 -04002214 /* An ECC certificate may be usable for ECDSA cipher suites depending on
2215 * the key usage extension. */
Adam Langley95c29f32014-06-20 12:00:00 -07002216 if (have_ecc_cert)
2217 {
2218 cpk = &c->pkeys[SSL_PKEY_ECC];
2219 x = cpk->x509;
2220 /* This call populates extension flags (ex_flags) */
2221 X509_check_purpose(x, -1, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07002222 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2223 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
2224 if (!(cpk->valid_flags & CERT_PKEY_SIGN))
2225 ecdsa_ok = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002226 if (ecdsa_ok)
2227 {
2228 mask_a|=SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002229 }
Adam Langley95c29f32014-06-20 12:00:00 -07002230 }
Adam Langley95c29f32014-06-20 12:00:00 -07002231
Adam Langley95c29f32014-06-20 12:00:00 -07002232 if (have_ecdh_tmp)
2233 {
2234 mask_k|=SSL_kEECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002235 }
Adam Langley95c29f32014-06-20 12:00:00 -07002236
Adam Langley95c29f32014-06-20 12:00:00 -07002237 mask_k |= SSL_kPSK;
2238 mask_a |= SSL_aPSK;
Adam Langley95c29f32014-06-20 12:00:00 -07002239
2240 c->mask_k=mask_k;
2241 c->mask_a=mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002242 c->valid=1;
2243 }
2244
2245/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2246#define ku_reject(x, usage) \
2247 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2248
Adam Langley95c29f32014-06-20 12:00:00 -07002249
2250int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2251 {
David Benjamin0da0e182014-08-19 16:20:28 -04002252 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002253 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2254 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2255
Adam Langley95c29f32014-06-20 12:00:00 -07002256 alg_a = cs->algorithm_auth;
2257
Adam Langley95c29f32014-06-20 12:00:00 -07002258 /* This call populates the ex_flags field correctly */
2259 X509_check_purpose(x, -1, 0);
2260 if ((x->sig_alg) && (x->sig_alg->algorithm))
2261 {
2262 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2263 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2264 }
Adam Langley95c29f32014-06-20 12:00:00 -07002265 if (alg_a & SSL_aECDSA)
2266 {
2267 /* key usage, if present, must allow signing */
2268 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2269 {
2270 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2271 return 0;
2272 }
2273 }
2274
2275 return 1; /* all checks are ok */
2276 }
2277
Adam Langley95c29f32014-06-20 12:00:00 -07002278
2279static int ssl_get_server_cert_index(const SSL *s)
2280 {
2281 int idx;
2282 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2283 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2284 idx = SSL_PKEY_RSA_SIGN;
2285 if (idx == -1)
2286 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2287 return idx;
2288 }
2289
2290CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2291 {
2292 CERT *c;
2293 int i;
2294
2295 c = s->cert;
2296 ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
2297
2298#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2299 /* Broken protocol test: return last used certificate: which may
2300 * mismatch the one expected.
2301 */
2302 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2303 return c->key;
2304#endif
2305
2306 i = ssl_get_server_cert_index(s);
2307
2308 /* This may or may not be an error. */
2309 if (i < 0)
2310 return NULL;
2311
2312 /* May be NULL. */
2313 return &c->pkeys[i];
2314 }
2315
2316EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd)
2317 {
2318 unsigned long alg_a;
2319 CERT *c;
2320 int idx = -1;
2321
2322 alg_a = cipher->algorithm_auth;
2323 c=s->cert;
2324
2325#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2326 /* Broken protocol test: use last key: which may
2327 * mismatch the one expected.
2328 */
2329 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2330 idx = c->key - c->pkeys;
2331 else
2332#endif
2333
David Benjaminef2116d2014-08-19 20:21:56 -04002334 if (alg_a & SSL_aRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002335 {
2336 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2337 idx = SSL_PKEY_RSA_SIGN;
2338 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2339 idx = SSL_PKEY_RSA_ENC;
2340 }
2341 else if ((alg_a & SSL_aECDSA) &&
2342 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2343 idx = SSL_PKEY_ECC;
2344 if (idx == -1)
2345 {
2346 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2347 return(NULL);
2348 }
2349 if (pmd)
2350 *pmd = c->pkeys[idx].digest;
2351 return c->pkeys[idx].privatekey;
2352 }
2353
Adam Langley95c29f32014-06-20 12:00:00 -07002354void ssl_update_cache(SSL *s,int mode)
2355 {
2356 int i;
2357
2358 /* If the session_id_length is 0, we are not supposed to cache it,
2359 * and it would be rather hard to do anyway :-) */
2360 if (s->session->session_id_length == 0) return;
2361
David Benjamin041b58a2014-08-27 15:25:21 -04002362 i=s->initial_ctx->session_cache_mode;
Adam Langley95c29f32014-06-20 12:00:00 -07002363 if ((i & mode) && (!s->hit)
2364 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
David Benjamin041b58a2014-08-27 15:25:21 -04002365 || SSL_CTX_add_session(s->initial_ctx,s->session))
2366 && (s->initial_ctx->new_session_cb != NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07002367 {
2368 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
David Benjamin041b58a2014-08-27 15:25:21 -04002369 if (!s->initial_ctx->new_session_cb(s,s->session))
Adam Langley95c29f32014-06-20 12:00:00 -07002370 SSL_SESSION_free(s->session);
2371 }
2372
2373 /* auto flush every 255 connections */
2374 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2375 ((i & mode) == mode))
2376 {
2377 if ( (((mode & SSL_SESS_CACHE_CLIENT)
David Benjamin041b58a2014-08-27 15:25:21 -04002378 ?s->initial_ctx->stats.sess_connect_good
2379 :s->initial_ctx->stats.sess_accept_good) & 0xff) == 0xff)
Adam Langley95c29f32014-06-20 12:00:00 -07002380 {
David Benjamin041b58a2014-08-27 15:25:21 -04002381 SSL_CTX_flush_sessions(s->initial_ctx,(unsigned long)time(NULL));
Adam Langley95c29f32014-06-20 12:00:00 -07002382 }
2383 }
2384 }
2385
2386const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
2387 {
2388 return ctx->method;
2389 }
2390
2391const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2392 {
2393 return(s->method);
2394 }
2395
2396int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2397 {
2398 int conn= -1;
2399 int ret=1;
2400
2401 if (s->method != meth)
2402 {
2403 if (s->handshake_func != NULL)
2404 conn=(s->handshake_func == s->method->ssl_connect);
2405
2406 if (s->method->version == meth->version)
2407 s->method=meth;
2408 else
2409 {
2410 s->method->ssl_free(s);
2411 s->method=meth;
2412 ret=s->method->ssl_new(s);
2413 }
2414
2415 if (conn == 1)
2416 s->handshake_func=meth->ssl_connect;
2417 else if (conn == 0)
2418 s->handshake_func=meth->ssl_accept;
2419 }
2420 return(ret);
2421 }
2422
2423int SSL_get_error(const SSL *s,int i)
2424 {
2425 int reason;
2426 unsigned long l;
2427 BIO *bio;
2428
2429 if (i > 0) return(SSL_ERROR_NONE);
2430
2431 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2432 * etc, where we do encode the error */
2433 if ((l=ERR_peek_error()) != 0)
2434 {
2435 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2436 return(SSL_ERROR_SYSCALL);
2437 else
2438 return(SSL_ERROR_SSL);
2439 }
2440
Adam Langleyb2ce0582014-06-20 12:00:00 -07002441 if ((i < 0) && SSL_want_session(s))
2442 return(SSL_ERROR_PENDING_SESSION);
2443
Adam Langleydc9b1412014-06-20 12:00:00 -07002444 if ((i < 0) && SSL_want_certificate(s))
2445 return(SSL_ERROR_PENDING_CERTIFICATE);
2446
Adam Langley95c29f32014-06-20 12:00:00 -07002447 if ((i < 0) && SSL_want_read(s))
2448 {
2449 bio=SSL_get_rbio(s);
2450 if (BIO_should_read(bio))
2451 return(SSL_ERROR_WANT_READ);
2452 else if (BIO_should_write(bio))
2453 /* This one doesn't make too much sense ... We never try
2454 * to write to the rbio, and an application program where
2455 * rbio and wbio are separate couldn't even know what it
2456 * should wait for.
2457 * However if we ever set s->rwstate incorrectly
2458 * (so that we have SSL_want_read(s) instead of
2459 * SSL_want_write(s)) and rbio and wbio *are* the same,
2460 * this test works around that bug; so it might be safer
2461 * to keep it. */
2462 return(SSL_ERROR_WANT_WRITE);
2463 else if (BIO_should_io_special(bio))
2464 {
2465 reason=BIO_get_retry_reason(bio);
2466 if (reason == BIO_RR_CONNECT)
2467 return(SSL_ERROR_WANT_CONNECT);
2468 else if (reason == BIO_RR_ACCEPT)
2469 return(SSL_ERROR_WANT_ACCEPT);
2470 else
2471 return(SSL_ERROR_SYSCALL); /* unknown */
2472 }
2473 }
2474
2475 if ((i < 0) && SSL_want_write(s))
2476 {
2477 bio=SSL_get_wbio(s);
2478 if (BIO_should_write(bio))
2479 return(SSL_ERROR_WANT_WRITE);
2480 else if (BIO_should_read(bio))
2481 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2482 return(SSL_ERROR_WANT_READ);
2483 else if (BIO_should_io_special(bio))
2484 {
2485 reason=BIO_get_retry_reason(bio);
2486 if (reason == BIO_RR_CONNECT)
2487 return(SSL_ERROR_WANT_CONNECT);
2488 else if (reason == BIO_RR_ACCEPT)
2489 return(SSL_ERROR_WANT_ACCEPT);
2490 else
2491 return(SSL_ERROR_SYSCALL);
2492 }
2493 }
2494 if ((i < 0) && SSL_want_x509_lookup(s))
2495 {
2496 return(SSL_ERROR_WANT_X509_LOOKUP);
2497 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002498 if ((i < 0) && SSL_want_channel_id_lookup(s))
2499 {
2500 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2501 }
Adam Langley95c29f32014-06-20 12:00:00 -07002502
2503 if (i == 0)
2504 {
2505 if (s->version == SSL2_VERSION)
2506 {
2507 /* assume it is the socket being closed */
2508 return(SSL_ERROR_ZERO_RETURN);
2509 }
2510 else
2511 {
2512 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2513 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2514 return(SSL_ERROR_ZERO_RETURN);
2515 }
2516 }
2517 return(SSL_ERROR_SYSCALL);
2518 }
2519
2520int SSL_do_handshake(SSL *s)
2521 {
2522 int ret=1;
2523
2524 if (s->handshake_func == NULL)
2525 {
2526 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2527 return(-1);
2528 }
2529
2530 s->method->ssl_renegotiate_check(s);
2531
2532 if (SSL_in_init(s) || SSL_in_before(s))
2533 {
2534 ret=s->handshake_func(s);
2535 }
2536 return(ret);
2537 }
2538
2539/* For the next 2 functions, SSL_clear() sets shutdown and so
2540 * one of these calls will reset it */
2541void SSL_set_accept_state(SSL *s)
2542 {
2543 s->server=1;
2544 s->shutdown=0;
2545 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2546 s->handshake_func=s->method->ssl_accept;
2547 /* clear the current cipher */
2548 ssl_clear_cipher_ctx(s);
2549 ssl_clear_hash_ctx(&s->read_hash);
2550 ssl_clear_hash_ctx(&s->write_hash);
2551 }
2552
2553void SSL_set_connect_state(SSL *s)
2554 {
2555 s->server=0;
2556 s->shutdown=0;
2557 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2558 s->handshake_func=s->method->ssl_connect;
2559 /* clear the current cipher */
2560 ssl_clear_cipher_ctx(s);
2561 ssl_clear_hash_ctx(&s->read_hash);
2562 ssl_clear_hash_ctx(&s->write_hash);
2563 }
2564
2565int ssl_undefined_function(SSL *s)
2566 {
2567 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2568 return(0);
2569 }
2570
2571int ssl_undefined_void_function(void)
2572 {
2573 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2574 return(0);
2575 }
2576
2577int ssl_undefined_const_function(const SSL *s)
2578 {
2579 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2580 return(0);
2581 }
2582
2583SSL_METHOD *ssl_bad_method(int ver)
2584 {
2585 OPENSSL_PUT_ERROR(SSL, ssl_bad_method, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2586 return(NULL);
2587 }
2588
Adam Langley0f4746e2014-08-13 12:26:32 -07002589static const char *ssl_get_version(int version)
Adam Langley95c29f32014-06-20 12:00:00 -07002590 {
Adam Langley0f4746e2014-08-13 12:26:32 -07002591 if (version == TLS1_2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002592 return("TLSv1.2");
Adam Langley0f4746e2014-08-13 12:26:32 -07002593 else if (version == TLS1_1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002594 return("TLSv1.1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002595 else if (version == TLS1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002596 return("TLSv1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002597 else if (version == SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002598 return("SSLv3");
Adam Langley0f4746e2014-08-13 12:26:32 -07002599 else if (version == SSL2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002600 return("SSLv2");
2601 else
2602 return("unknown");
2603 }
2604
Adam Langley0f4746e2014-08-13 12:26:32 -07002605const char *SSL_get_version(const SSL *s)
2606 {
2607 return ssl_get_version(s->version);
2608 }
2609
2610const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2611 {
2612 return ssl_get_version(sess->ssl_version);
2613 }
2614
Adam Langley95c29f32014-06-20 12:00:00 -07002615void ssl_clear_cipher_ctx(SSL *s)
2616 {
2617 if (s->enc_read_ctx != NULL)
2618 {
2619 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2620 OPENSSL_free(s->enc_read_ctx);
2621 s->enc_read_ctx=NULL;
2622 }
2623 if (s->enc_write_ctx != NULL)
2624 {
2625 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2626 OPENSSL_free(s->enc_write_ctx);
2627 s->enc_write_ctx=NULL;
2628 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002629 if (s->aead_read_ctx != NULL)
2630 {
2631 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2632 OPENSSL_free(s->aead_read_ctx);
2633 s->aead_read_ctx = NULL;
2634 }
2635 if (s->aead_write_ctx != NULL)
2636 {
2637 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2638 OPENSSL_free(s->aead_write_ctx);
2639 s->aead_write_ctx = NULL;
2640 }
Adam Langley95c29f32014-06-20 12:00:00 -07002641 }
2642
2643X509 *SSL_get_certificate(const SSL *s)
2644 {
2645 if (s->cert != NULL)
2646 return(s->cert->key->x509);
2647 else
2648 return(NULL);
2649 }
2650
2651EVP_PKEY *SSL_get_privatekey(const SSL *s)
2652 {
2653 if (s->cert != NULL)
2654 return(s->cert->key->privatekey);
2655 else
2656 return(NULL);
2657 }
2658
2659X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2660 {
2661 if (ctx->cert != NULL)
2662 return ctx->cert->key->x509;
2663 else
2664 return NULL;
2665 }
2666
2667EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2668 {
2669 if (ctx->cert != NULL)
2670 return ctx->cert->key->privatekey;
2671 else
2672 return NULL ;
2673 }
2674
2675const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2676 {
2677 if ((s->session != NULL) && (s->session->cipher != NULL))
2678 return(s->session->cipher);
2679 return(NULL);
2680 }
2681const void *SSL_get_current_compression(SSL *s)
2682 {
2683 return NULL;
2684 }
2685const void *SSL_get_current_expansion(SSL *s)
2686 {
2687 return NULL;
2688 }
2689
2690int ssl_init_wbio_buffer(SSL *s,int push)
2691 {
2692 BIO *bbio;
2693
2694 if (s->bbio == NULL)
2695 {
2696 bbio=BIO_new(BIO_f_buffer());
2697 if (bbio == NULL) return(0);
2698 s->bbio=bbio;
2699 }
2700 else
2701 {
2702 bbio=s->bbio;
2703 if (s->bbio == s->wbio)
2704 s->wbio=BIO_pop(s->wbio);
2705 }
2706 (void)BIO_reset(bbio);
2707/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2708 if (!BIO_set_read_buffer_size(bbio,1))
2709 {
2710 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2711 return(0);
2712 }
2713 if (push)
2714 {
2715 if (s->wbio != bbio)
2716 s->wbio=BIO_push(bbio,s->wbio);
2717 }
2718 else
2719 {
2720 if (s->wbio == bbio)
2721 s->wbio=BIO_pop(bbio);
2722 }
2723 return(1);
2724 }
2725
2726void ssl_free_wbio_buffer(SSL *s)
2727 {
2728 if (s->bbio == NULL) return;
2729
2730 if (s->bbio == s->wbio)
2731 {
2732 /* remove buffering */
2733 s->wbio=BIO_pop(s->wbio);
2734#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2735 assert(s->wbio != NULL);
2736#endif
2737 }
2738 BIO_free(s->bbio);
2739 s->bbio=NULL;
2740 }
2741
2742void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2743 {
2744 ctx->quiet_shutdown=mode;
2745 }
2746
2747int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2748 {
2749 return(ctx->quiet_shutdown);
2750 }
2751
2752void SSL_set_quiet_shutdown(SSL *s,int mode)
2753 {
2754 s->quiet_shutdown=mode;
2755 }
2756
2757int SSL_get_quiet_shutdown(const SSL *s)
2758 {
2759 return(s->quiet_shutdown);
2760 }
2761
2762void SSL_set_shutdown(SSL *s,int mode)
2763 {
2764 s->shutdown=mode;
2765 }
2766
2767int SSL_get_shutdown(const SSL *s)
2768 {
2769 return(s->shutdown);
2770 }
2771
2772int SSL_version(const SSL *s)
2773 {
2774 return(s->version);
2775 }
2776
2777SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2778 {
2779 return(ssl->ctx);
2780 }
2781
2782SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2783 {
2784 if (ssl->ctx == ctx)
2785 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002786 if (ctx == NULL)
2787 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002788 if (ssl->cert != NULL)
2789 ssl_cert_free(ssl->cert);
2790 ssl->cert = ssl_cert_dup(ctx->cert);
2791 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2792 if (ssl->ctx != NULL)
2793 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2794 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07002795
2796 ssl->sid_ctx_length = ctx->sid_ctx_length;
2797 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2798 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2799
Adam Langley95c29f32014-06-20 12:00:00 -07002800 return(ssl->ctx);
2801 }
2802
2803#ifndef OPENSSL_NO_STDIO
2804int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2805 {
2806 return(X509_STORE_set_default_paths(ctx->cert_store));
2807 }
2808
2809int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2810 const char *CApath)
2811 {
2812 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2813 }
2814#endif
2815
2816void SSL_set_info_callback(SSL *ssl,
2817 void (*cb)(const SSL *ssl,int type,int val))
2818 {
2819 ssl->info_callback=cb;
2820 }
2821
2822/* One compiler (Diab DCC) doesn't like argument names in returned
2823 function pointer. */
2824void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2825 {
2826 return ssl->info_callback;
2827 }
2828
2829int SSL_state(const SSL *ssl)
2830 {
2831 return(ssl->state);
2832 }
2833
2834void SSL_set_state(SSL *ssl, int state)
2835 {
2836 ssl->state = state;
2837 }
2838
2839void SSL_set_verify_result(SSL *ssl,long arg)
2840 {
2841 ssl->verify_result=arg;
2842 }
2843
2844long SSL_get_verify_result(const SSL *ssl)
2845 {
2846 return(ssl->verify_result);
2847 }
2848
2849int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2850 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2851 {
2852 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2853 new_func, dup_func, free_func);
2854 }
2855
2856int SSL_set_ex_data(SSL *s,int idx,void *arg)
2857 {
2858 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2859 }
2860
2861void *SSL_get_ex_data(const SSL *s,int idx)
2862 {
2863 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2864 }
2865
2866int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2867 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2868 {
2869 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2870 new_func, dup_func, free_func);
2871 }
2872
2873int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2874 {
2875 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2876 }
2877
2878void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2879 {
2880 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2881 }
2882
2883int ssl_ok(SSL *s)
2884 {
2885 return(1);
2886 }
2887
2888X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2889 {
2890 return(ctx->cert_store);
2891 }
2892
2893void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2894 {
2895 if (ctx->cert_store != NULL)
2896 X509_STORE_free(ctx->cert_store);
2897 ctx->cert_store=store;
2898 }
2899
2900int SSL_want(const SSL *s)
2901 {
2902 return(s->rwstate);
2903 }
2904
2905/*!
2906 * \brief Set the callback for generating temporary RSA keys.
2907 * \param ctx the SSL context.
2908 * \param cb the callback
2909 */
2910
Adam Langley95c29f32014-06-20 12:00:00 -07002911void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2912 int is_export,
2913 int keylength))
2914 {
2915 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2916 }
2917
2918void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2919 int is_export,
2920 int keylength))
2921 {
2922 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2923 }
Adam Langley95c29f32014-06-20 12:00:00 -07002924
2925#ifdef DOXYGEN
2926/*!
2927 * \brief The RSA temporary key callback function.
2928 * \param ssl the SSL session.
2929 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2930 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2931 * of the required key in bits.
2932 * \return the temporary RSA key.
2933 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2934 */
2935
2936RSA *cb(SSL *ssl,int is_export,int keylength)
2937 {}
2938#endif
2939
2940/*!
2941 * \brief Set the callback for generating temporary DH keys.
2942 * \param ctx the SSL context.
2943 * \param dh the callback
2944 */
2945
Adam Langley95c29f32014-06-20 12:00:00 -07002946void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2947 int keylength))
2948 {
2949 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2950 }
2951
2952void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2953 int keylength))
2954 {
2955 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2956 }
Adam Langley95c29f32014-06-20 12:00:00 -07002957
Adam Langley95c29f32014-06-20 12:00:00 -07002958void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2959 int keylength))
2960 {
2961 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2962 }
2963
2964void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2965 int keylength))
2966 {
2967 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2968 }
Adam Langley95c29f32014-06-20 12:00:00 -07002969
Adam Langley95c29f32014-06-20 12:00:00 -07002970int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2971 {
2972 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2973 {
2974 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2975 return 0;
2976 }
2977 if (ctx->psk_identity_hint != NULL)
2978 OPENSSL_free(ctx->psk_identity_hint);
2979 if (identity_hint != NULL)
2980 {
2981 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2982 if (ctx->psk_identity_hint == NULL)
2983 return 0;
2984 }
2985 else
2986 ctx->psk_identity_hint = NULL;
2987 return 1;
2988 }
2989
2990int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
2991 {
2992 if (s == NULL)
2993 return 0;
2994
Adam Langley95c29f32014-06-20 12:00:00 -07002995 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2996 {
2997 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2998 return 0;
2999 }
Adam Langley0289c732014-06-20 12:00:00 -07003000
3001 /* Clear hint in SSL and associated SSL_SESSION (if any). */
3002 if (s->psk_identity_hint != NULL)
3003 {
3004 OPENSSL_free(s->psk_identity_hint);
3005 s->psk_identity_hint = NULL;
3006 }
3007 if (s->session != NULL && s->session->psk_identity_hint != NULL)
3008 {
Adam Langley95c29f32014-06-20 12:00:00 -07003009 OPENSSL_free(s->session->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -07003010 s->session->psk_identity_hint = NULL;
3011 }
3012
Adam Langley95c29f32014-06-20 12:00:00 -07003013 if (identity_hint != NULL)
3014 {
Adam Langley0289c732014-06-20 12:00:00 -07003015 /* The hint is stored in SSL and SSL_SESSION with the one in
3016 * SSL_SESSION taking precedence. Thus, if SSL_SESSION is avaiable,
3017 * we store the hint there, otherwise we store it in SSL. */
3018 if (s->session != NULL)
3019 {
3020 s->session->psk_identity_hint = BUF_strdup(identity_hint);
3021 if (s->session->psk_identity_hint == NULL)
3022 return 0;
3023 }
3024 else
3025 {
3026 s->psk_identity_hint = BUF_strdup(identity_hint);
3027 if (s->psk_identity_hint == NULL)
3028 return 0;
3029 }
Adam Langley95c29f32014-06-20 12:00:00 -07003030 }
Adam Langley95c29f32014-06-20 12:00:00 -07003031 return 1;
3032 }
3033
3034const char *SSL_get_psk_identity_hint(const SSL *s)
3035 {
Adam Langley0289c732014-06-20 12:00:00 -07003036 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07003037 return NULL;
Adam Langley0289c732014-06-20 12:00:00 -07003038 /* The hint is stored in SSL and SSL_SESSION with the one in SSL_SESSION
3039 * taking precedence. */
3040 if (s->session != NULL)
3041 return(s->session->psk_identity_hint);
3042 return(s->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07003043 }
3044
3045const char *SSL_get_psk_identity(const SSL *s)
3046 {
3047 if (s == NULL || s->session == NULL)
3048 return NULL;
3049 return(s->session->psk_identity);
3050 }
3051
3052void SSL_set_psk_client_callback(SSL *s,
3053 unsigned int (*cb)(SSL *ssl, const char *hint,
3054 char *identity, unsigned int max_identity_len, unsigned char *psk,
3055 unsigned int max_psk_len))
3056 {
3057 s->psk_client_callback = cb;
3058 }
3059
3060void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
3061 unsigned int (*cb)(SSL *ssl, const char *hint,
3062 char *identity, unsigned int max_identity_len, unsigned char *psk,
3063 unsigned int max_psk_len))
3064 {
3065 ctx->psk_client_callback = cb;
3066 }
3067
3068void SSL_set_psk_server_callback(SSL *s,
3069 unsigned int (*cb)(SSL *ssl, const char *identity,
3070 unsigned char *psk, unsigned int max_psk_len))
3071 {
3072 s->psk_server_callback = cb;
3073 }
3074
3075void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
3076 unsigned int (*cb)(SSL *ssl, const char *identity,
3077 unsigned char *psk, unsigned int max_psk_len))
3078 {
3079 ctx->psk_server_callback = cb;
3080 }
Adam Langley95c29f32014-06-20 12:00:00 -07003081
3082void 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))
3083 {
3084 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3085 }
3086void 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))
3087 {
3088 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3089 }
3090
Adam Langleyadb739e2014-06-20 12:00:00 -07003091int SSL_cutthrough_complete(const SSL *s)
3092 {
3093 return (!s->server && /* cutthrough only applies to clients */
3094 !s->hit && /* full-handshake */
3095 s->version >= SSL3_VERSION &&
3096 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3097 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3098 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3099 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3100 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
David Benjamin7e3305e2014-07-28 14:52:32 -04003101 s->state == SSL3_ST_CR_CHANGE ||
Adam Langleyadb739e2014-06-20 12:00:00 -07003102 s->state == SSL3_ST_CR_FINISHED_A));
3103 }
3104
Adam Langley95f22882014-06-20 12:00:00 -07003105void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3106 size_t* ssl_session_size)
3107{
3108 *ssl_size = sizeof(SSL);
3109 *ssl_ctx_size = sizeof(SSL_CTX);
3110 *ssl_session_size = sizeof(SSL_SESSION);
3111}
3112
Adam Langleyadb739e2014-06-20 12:00:00 -07003113int ssl3_can_cutthrough(const SSL *s)
3114 {
3115 const SSL_CIPHER *c;
3116
3117 /* require a strong enough cipher */
3118 if (SSL_get_cipher_bits(s, NULL) < 128)
3119 return 0;
3120
3121 /* require ALPN or NPN extension */
David Benjamin457112e2014-08-27 14:35:09 -04003122 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen)
Adam Langleyadb739e2014-06-20 12:00:00 -07003123 {
3124 return 0;
3125 }
3126
3127 /* require a forward-secret cipher */
3128 c = SSL_get_current_cipher(s);
3129 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3130 c->algorithm_mkey != SSL_kEECDH))
3131 {
3132 return 0;
3133 }
3134
3135 return 1;
3136 }
3137
Adam Langley29707792014-06-20 12:00:00 -07003138/* ssl_get_max_version returns the maximum SSL/TLS version number supported by
3139 * |s|, or zero if all versions are disabled. */
3140int ssl_get_max_version(const SSL *s)
3141 {
3142 /* Only one version supported for DTLS. */
3143 if (s->version == DTLS1_VERSION)
3144 return DTLS1_VERSION;
3145
3146 if (!(s->options & SSL_OP_NO_TLSv1_2))
3147 return TLS1_2_VERSION;
3148 if (!(s->options & SSL_OP_NO_TLSv1_1))
3149 return TLS1_1_VERSION;
3150 if (!(s->options & SSL_OP_NO_TLSv1))
3151 return TLS1_VERSION;
3152 if (!(s->options & SSL_OP_NO_SSLv3))
3153 return SSL3_VERSION;
3154 if (!(s->options & SSL_OP_NO_SSLv2))
3155 return SSL2_VERSION;
3156 return 0;
3157 }
3158
Adam Langley95c29f32014-06-20 12:00:00 -07003159/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3160 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3161 * any. If EVP_MD pointer is passed, initializes ctx with this md
3162 * Returns newly allocated ctx;
3163 */
3164
3165EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3166{
3167 ssl_clear_hash_ctx(hash);
3168 *hash = EVP_MD_CTX_create();
3169 if (md) EVP_DigestInit_ex(*hash,md,NULL);
3170 return *hash;
3171}
3172void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3173{
3174
3175 if (*hash) EVP_MD_CTX_destroy(*hash);
3176 *hash=NULL;
3177}
3178
3179void SSL_set_debug(SSL *s, int debug)
3180 {
3181 s->debug = debug;
3182 }
3183
3184int SSL_cache_hit(SSL *s)
3185 {
3186 return s->hit;
3187 }
3188
3189int SSL_is_server(SSL *s)
3190 {
3191 return s->server;
3192 }