blob: 94383e5949f79a521518aef79b13bbe9a83edd90 [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
David Benjamine1136082014-09-20 12:28:58 -0400175/* Some error codes are special. Ensure the make_errors.go script
176 * never regresses this. */
177OPENSSL_COMPILE_ASSERT(
178 SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
179 SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
180 ssl_alert_reason_code_mismatch);
181
Adam Langley95c29f32014-06-20 12:00:00 -0700182int SSL_clear(SSL *s)
183 {
184
185 if (s->method == NULL)
186 {
187 OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
188 return(0);
189 }
190
191 if (ssl_clear_bad_session(s))
192 {
193 SSL_SESSION_free(s->session);
194 s->session=NULL;
195 }
196
Adam Langley95c29f32014-06-20 12:00:00 -0700197 s->hit=0;
198 s->shutdown=0;
199
200#if 0 /* Disabled since version 1.10 of this file (early return not
201 * needed because SSL_clear is not called when doing renegotiation) */
202 /* This is set if we are doing dynamic renegotiation so keep
203 * the old cipher. It is sort of a SSL_clear_lite :-) */
204 if (s->renegotiate) return(1);
205#else
206 if (s->renegotiate)
207 {
208 OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
209 return 0;
210 }
211#endif
212
213 s->type=0;
214
215 s->state=SSL_ST_BEFORE|((s->server)?SSL_ST_ACCEPT:SSL_ST_CONNECT);
216
217 s->version=s->method->version;
218 s->client_version=s->version;
219 s->rwstate=SSL_NOTHING;
220 s->rstate=SSL_ST_READ_HEADER;
221#if 0
222 s->read_ahead=s->ctx->read_ahead;
223#endif
224
225 if (s->init_buf != NULL)
226 {
227 BUF_MEM_free(s->init_buf);
228 s->init_buf=NULL;
229 }
230
231 ssl_clear_cipher_ctx(s);
232 ssl_clear_hash_ctx(&s->read_hash);
233 ssl_clear_hash_ctx(&s->write_hash);
234
235 s->first_packet=0;
236
237#if 1
238 /* Check to see if we were changed into a different method, if
239 * so, revert back if we are not doing session-id reuse. */
240 if (!s->in_handshake && (s->session == NULL) && (s->method != s->ctx->method))
241 {
242 s->method->ssl_free(s);
243 s->method=s->ctx->method;
244 if (!s->method->ssl_new(s))
245 return(0);
246 }
247 else
248#endif
249 s->method->ssl_clear(s);
250 return(1);
251 }
252
253/** Used to change an SSL_CTXs default SSL method type */
254int SSL_CTX_set_ssl_version(SSL_CTX *ctx,const SSL_METHOD *meth)
255 {
256 STACK_OF(SSL_CIPHER) *sk;
257
258 ctx->method=meth;
259
Adam Langley858a88d2014-06-20 12:00:00 -0700260 sk=ssl_create_cipher_list(
261 ctx->method, &ctx->cipher_list, &ctx->cipher_list_by_id,
David Benjamin5e4f6e92014-11-17 03:23:24 -0500262 SSL_DEFAULT_CIPHER_LIST,
Adam Langley858a88d2014-06-20 12:00:00 -0700263 ctx->cert);
264
Adam Langley95c29f32014-06-20 12:00:00 -0700265 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
266 {
267 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_ssl_version, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
268 return(0);
269 }
270 return(1);
271 }
272
273SSL *SSL_new(SSL_CTX *ctx)
274 {
275 SSL *s;
276
277 if (ctx == NULL)
278 {
279 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
280 return(NULL);
281 }
282 if (ctx->method == NULL)
283 {
284 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
285 return(NULL);
286 }
287
288 s=(SSL *)OPENSSL_malloc(sizeof(SSL));
289 if (s == NULL) goto err;
290 memset(s,0,sizeof(SSL));
291
292 s->options=ctx->options;
293 s->mode=ctx->mode;
294 s->max_cert_list=ctx->max_cert_list;
295
296 if (ctx->cert != NULL)
297 {
298 /* Earlier library versions used to copy the pointer to
299 * the CERT, not its contents; only when setting new
300 * parameters for the per-SSL copy, ssl_cert_new would be
301 * called (and the direct reference to the per-SSL_CTX
302 * settings would be lost, but those still were indirectly
303 * accessed for various purposes, and for that reason they
304 * used to be known as s->ctx->default_cert).
305 * Now we don't look at the SSL_CTX's CERT after having
306 * duplicated it once. */
307
308 s->cert = ssl_cert_dup(ctx->cert);
309 if (s->cert == NULL)
310 goto err;
311 }
312 else
313 s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
314
315 s->read_ahead=ctx->read_ahead;
316 s->msg_callback=ctx->msg_callback;
317 s->msg_callback_arg=ctx->msg_callback_arg;
318 s->verify_mode=ctx->verify_mode;
319#if 0
320 s->verify_depth=ctx->verify_depth;
321#endif
322 s->sid_ctx_length=ctx->sid_ctx_length;
323 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
324 memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
325 s->verify_callback=ctx->default_verify_callback;
326 s->generate_session_id=ctx->generate_session_id;
327
328 s->param = X509_VERIFY_PARAM_new();
329 if (!s->param)
330 goto err;
331 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
332#if 0
333 s->purpose = ctx->purpose;
334 s->trust = ctx->trust;
335#endif
336 s->quiet_shutdown=ctx->quiet_shutdown;
337 s->max_send_fragment = ctx->max_send_fragment;
338
339 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
340 s->ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700341 s->tlsext_debug_cb = 0;
342 s->tlsext_debug_arg = NULL;
343 s->tlsext_ticket_expected = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700344 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
345 s->initial_ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700346 if (ctx->tlsext_ecpointformatlist)
347 {
348 s->tlsext_ecpointformatlist =
349 BUF_memdup(ctx->tlsext_ecpointformatlist,
350 ctx->tlsext_ecpointformatlist_length);
351 if (!s->tlsext_ecpointformatlist)
352 goto err;
353 s->tlsext_ecpointformatlist_length =
354 ctx->tlsext_ecpointformatlist_length;
355 }
356 if (ctx->tlsext_ellipticcurvelist)
357 {
358 s->tlsext_ellipticcurvelist =
359 BUF_memdup(ctx->tlsext_ellipticcurvelist,
David Benjamin072334d2014-07-13 16:24:27 -0400360 ctx->tlsext_ellipticcurvelist_length * 2);
Adam Langley95c29f32014-06-20 12:00:00 -0700361 if (!s->tlsext_ellipticcurvelist)
362 goto err;
363 s->tlsext_ellipticcurvelist_length =
364 ctx->tlsext_ellipticcurvelist_length;
365 }
Adam Langley95c29f32014-06-20 12:00:00 -0700366 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700367
368 if (s->ctx->alpn_client_proto_list)
369 {
David Benjamin072c9532014-07-26 11:44:25 -0400370 s->alpn_client_proto_list = BUF_memdup(
371 s->ctx->alpn_client_proto_list,
372 s->ctx->alpn_client_proto_list_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700373 if (s->alpn_client_proto_list == NULL)
374 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700375 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
376 }
Adam Langley95c29f32014-06-20 12:00:00 -0700377
378 s->verify_result=X509_V_OK;
379
380 s->method=ctx->method;
381
382 if (!s->method->ssl_new(s))
383 goto err;
384
385 s->references=1;
386 s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
387
388 SSL_clear(s);
389
390 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
391
Adam Langley0289c732014-06-20 12:00:00 -0700392 s->psk_identity_hint = NULL;
393 if (ctx->psk_identity_hint)
394 {
395 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
396 if (s->psk_identity_hint == NULL)
397 goto err;
398 }
Adam Langley95c29f32014-06-20 12:00:00 -0700399 s->psk_client_callback=ctx->psk_client_callback;
400 s->psk_server_callback=ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700401
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200402 if (!s->server)
David Benjamin6c7aed02014-08-27 16:42:38 -0400403 {
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200404 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
David Benjamin6c7aed02014-08-27 16:42:38 -0400405 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
406 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200407
Adam Langley95c29f32014-06-20 12:00:00 -0700408 return(s);
409err:
410 if (s != NULL)
411 {
412 if (s->cert != NULL)
413 ssl_cert_free(s->cert);
414 if (s->ctx != NULL)
415 SSL_CTX_free(s->ctx); /* decrement reference count */
416 OPENSSL_free(s);
417 }
418 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
419 return(NULL);
420 }
421
422int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
423 unsigned int sid_ctx_len)
424 {
425 if(sid_ctx_len > sizeof ctx->sid_ctx)
426 {
427 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
428 return 0;
429 }
430 ctx->sid_ctx_length=sid_ctx_len;
431 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
432
433 return 1;
434 }
435
436int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
437 unsigned int sid_ctx_len)
438 {
439 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
440 {
441 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
442 return 0;
443 }
444 ssl->sid_ctx_length=sid_ctx_len;
445 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
446
447 return 1;
448 }
449
450int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
451 {
452 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
453 ctx->generate_session_id = cb;
454 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
455 return 1;
456 }
457
458int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
459 {
460 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
461 ssl->generate_session_id = cb;
462 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
463 return 1;
464 }
465
466int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
467 unsigned int id_len)
468 {
469 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
470 * we can "construct" a session to give us the desired check - ie. to
471 * find if there's a session in the hash table that would conflict with
472 * any new session built out of this id/id_len and the ssl_version in
473 * use by this SSL. */
474 SSL_SESSION r, *p;
475
476 if(id_len > sizeof r.session_id)
477 return 0;
478
479 r.ssl_version = ssl->version;
480 r.session_id_length = id_len;
481 memcpy(r.session_id, id, id_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700482
483 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
484 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
485 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
486 return (p != NULL);
487 }
488
489int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
490 {
491 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
492 }
493
494int SSL_set_purpose(SSL *s, int purpose)
495 {
496 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
497 }
498
499int SSL_CTX_set_trust(SSL_CTX *s, int trust)
500 {
501 return X509_VERIFY_PARAM_set_trust(s->param, trust);
502 }
503
504int SSL_set_trust(SSL *s, int trust)
505 {
506 return X509_VERIFY_PARAM_set_trust(s->param, trust);
507 }
508
509int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
510 {
511 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
512 }
513
514int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
515 {
516 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
517 }
518
Adam Langley858a88d2014-06-20 12:00:00 -0700519void ssl_cipher_preference_list_free(
520 struct ssl_cipher_preference_list_st *cipher_list)
521 {
522 sk_SSL_CIPHER_free(cipher_list->ciphers);
523 OPENSSL_free(cipher_list->in_group_flags);
524 OPENSSL_free(cipher_list);
525 }
526
527struct ssl_cipher_preference_list_st*
528ssl_cipher_preference_list_dup(
529 struct ssl_cipher_preference_list_st *cipher_list)
530 {
531 struct ssl_cipher_preference_list_st* ret = NULL;
532 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
533
534 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
535 if (!ret)
536 goto err;
537 ret->ciphers = NULL;
538 ret->in_group_flags = NULL;
539 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
540 if (!ret->ciphers)
541 goto err;
David Benjamin072c9532014-07-26 11:44:25 -0400542 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
Adam Langley858a88d2014-06-20 12:00:00 -0700543 if (!ret->in_group_flags)
544 goto err;
Adam Langley858a88d2014-06-20 12:00:00 -0700545 return ret;
546
547err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400548 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700549 sk_SSL_CIPHER_free(ret->ciphers);
550 if (ret)
551 OPENSSL_free(ret);
552 return NULL;
553 }
554
555struct ssl_cipher_preference_list_st*
556ssl_cipher_preference_list_from_ciphers(STACK_OF(SSL_CIPHER) *ciphers)
557 {
558 struct ssl_cipher_preference_list_st* ret = NULL;
559 size_t n = sk_SSL_CIPHER_num(ciphers);
560
561 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
562 if (!ret)
563 goto err;
564 ret->ciphers = NULL;
565 ret->in_group_flags = NULL;
566 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
567 if (!ret->ciphers)
568 goto err;
569 ret->in_group_flags = OPENSSL_malloc(n);
570 if (!ret->in_group_flags)
571 goto err;
572 memset(ret->in_group_flags, 0, n);
573 return ret;
574
575err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400576 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700577 sk_SSL_CIPHER_free(ret->ciphers);
578 if (ret)
579 OPENSSL_free(ret);
580 return NULL;
581 }
582
Adam Langley95c29f32014-06-20 12:00:00 -0700583X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
584 {
585 return ctx->param;
586 }
587
588X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
589 {
590 return ssl->param;
591 }
592
593void SSL_certs_clear(SSL *s)
594 {
595 ssl_cert_clear_certs(s->cert);
596 }
597
598void SSL_free(SSL *s)
599 {
600 int i;
601
602 if(s == NULL)
603 return;
604
605 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
606#ifdef REF_PRINT
607 REF_PRINT("SSL",s);
608#endif
609 if (i > 0) return;
610#ifdef REF_CHECK
611 if (i < 0)
612 {
613 fprintf(stderr,"SSL_free, bad reference count\n");
614 abort(); /* ok */
615 }
616#endif
617
618 if (s->param)
619 X509_VERIFY_PARAM_free(s->param);
620
621 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
622
623 if (s->bbio != NULL)
624 {
625 /* If the buffering BIO is in place, pop it off */
626 if (s->bbio == s->wbio)
627 {
628 s->wbio=BIO_pop(s->wbio);
629 }
630 BIO_free(s->bbio);
631 s->bbio=NULL;
632 }
633 if (s->rbio != NULL)
634 BIO_free_all(s->rbio);
635 if ((s->wbio != NULL) && (s->wbio != s->rbio))
636 BIO_free_all(s->wbio);
637
638 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
639
640 /* add extra stuff */
Adam Langley858a88d2014-06-20 12:00:00 -0700641 if (s->cipher_list != NULL)
642 ssl_cipher_preference_list_free(s->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -0700643 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
644
645 /* Make the next call work :-) */
646 if (s->session != NULL)
647 {
648 ssl_clear_bad_session(s);
649 SSL_SESSION_free(s->session);
650 }
651
652 ssl_clear_cipher_ctx(s);
653 ssl_clear_hash_ctx(&s->read_hash);
654 ssl_clear_hash_ctx(&s->write_hash);
655
656 if (s->cert != NULL) ssl_cert_free(s->cert);
657 /* Free up if allocated */
658
Adam Langley95c29f32014-06-20 12:00:00 -0700659 if (s->tlsext_hostname)
660 OPENSSL_free(s->tlsext_hostname);
661 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700662 if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
663 if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -0700664 if (s->alpn_client_proto_list)
665 OPENSSL_free(s->alpn_client_proto_list);
Adam Langley1258b6a2014-06-20 12:00:00 -0700666 if (s->tlsext_channel_id_private)
667 EVP_PKEY_free(s->tlsext_channel_id_private);
Adam Langley95c29f32014-06-20 12:00:00 -0700668
Adam Langley0289c732014-06-20 12:00:00 -0700669 if (s->psk_identity_hint)
670 OPENSSL_free(s->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -0700671
Adam Langley95c29f32014-06-20 12:00:00 -0700672 if (s->client_CA != NULL)
673 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
674
675 if (s->method != NULL) s->method->ssl_free(s);
676
677 if (s->ctx) SSL_CTX_free(s->ctx);
678
Adam Langley95c29f32014-06-20 12:00:00 -0700679 if (s->next_proto_negotiated)
680 OPENSSL_free(s->next_proto_negotiated);
Adam Langley95c29f32014-06-20 12:00:00 -0700681
682 if (s->srtp_profiles)
683 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
684
David Benjamin1b965262014-08-13 20:06:29 -0400685 if (s->tlsext_session_ticket)
686 {
687 OPENSSL_free(s->tlsext_session_ticket);
688 }
689
Adam Langley95c29f32014-06-20 12:00:00 -0700690 OPENSSL_free(s);
691 }
692
693void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
694 {
695 /* If the output buffering BIO is still in place, remove it
696 */
697 if (s->bbio != NULL)
698 {
699 if (s->wbio == s->bbio)
700 {
701 s->wbio=s->wbio->next_bio;
702 s->bbio->next_bio=NULL;
703 }
704 }
705 if ((s->rbio != NULL) && (s->rbio != rbio))
706 BIO_free_all(s->rbio);
707 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
708 BIO_free_all(s->wbio);
709 s->rbio=rbio;
710 s->wbio=wbio;
711 }
712
713BIO *SSL_get_rbio(const SSL *s)
714 { return(s->rbio); }
715
716BIO *SSL_get_wbio(const SSL *s)
717 { return(s->wbio); }
718
719int SSL_get_fd(const SSL *s)
720 {
721 return(SSL_get_rfd(s));
722 }
723
724int SSL_get_rfd(const SSL *s)
725 {
726 int ret= -1;
727 BIO *b,*r;
728
729 b=SSL_get_rbio(s);
730 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
731 if (r != NULL)
732 BIO_get_fd(r,&ret);
733 return(ret);
734 }
735
736int SSL_get_wfd(const SSL *s)
737 {
738 int ret= -1;
739 BIO *b,*r;
740
741 b=SSL_get_wbio(s);
742 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
743 if (r != NULL)
744 BIO_get_fd(r,&ret);
745 return(ret);
746 }
747
748#ifndef OPENSSL_NO_SOCK
749int SSL_set_fd(SSL *s,int fd)
750 {
751 int ret=0;
752 BIO *bio=NULL;
753
754 bio=BIO_new(BIO_s_fd());
755
756 if (bio == NULL)
757 {
758 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
759 goto err;
760 }
761 BIO_set_fd(bio,fd,BIO_NOCLOSE);
762 SSL_set_bio(s,bio,bio);
763 ret=1;
764err:
765 return(ret);
766 }
767
768int SSL_set_wfd(SSL *s,int fd)
769 {
770 int ret=0;
771 BIO *bio=NULL;
772
773 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_FD)
774 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
775 {
776 bio=BIO_new(BIO_s_fd());
777
778 if (bio == NULL)
779 {
780 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
781 goto err;
782 }
783 BIO_set_fd(bio,fd,BIO_NOCLOSE);
784 SSL_set_bio(s,SSL_get_rbio(s),bio);
785 }
786 else
787 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
788 ret=1;
789err:
790 return(ret);
791 }
792
793int SSL_set_rfd(SSL *s,int fd)
794 {
795 int ret=0;
796 BIO *bio=NULL;
797
798 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_FD)
799 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
800 {
801 bio=BIO_new(BIO_s_fd());
802
803 if (bio == NULL)
804 {
805 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
806 goto err;
807 }
808 BIO_set_fd(bio,fd,BIO_NOCLOSE);
809 SSL_set_bio(s,bio,SSL_get_wbio(s));
810 }
811 else
812 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
813 ret=1;
814err:
815 return(ret);
816 }
817#endif
818
819
820/* return length of latest Finished message we sent, copy to 'buf' */
821size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
822 {
823 size_t ret = 0;
824
825 if (s->s3 != NULL)
826 {
827 ret = s->s3->tmp.finish_md_len;
828 if (count > ret)
829 count = ret;
830 memcpy(buf, s->s3->tmp.finish_md, count);
831 }
832 return ret;
833 }
834
835/* return length of latest Finished message we expected, copy to 'buf' */
836size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
837 {
838 size_t ret = 0;
839
840 if (s->s3 != NULL)
841 {
842 ret = s->s3->tmp.peer_finish_md_len;
843 if (count > ret)
844 count = ret;
845 memcpy(buf, s->s3->tmp.peer_finish_md, count);
846 }
847 return ret;
848 }
849
850
851int SSL_get_verify_mode(const SSL *s)
852 {
853 return(s->verify_mode);
854 }
855
856int SSL_get_verify_depth(const SSL *s)
857 {
858 return X509_VERIFY_PARAM_get_depth(s->param);
859 }
860
861int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
862 {
863 return(s->verify_callback);
864 }
865
866int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
867 {
868 return(ctx->verify_mode);
869 }
870
871int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
872 {
873 return X509_VERIFY_PARAM_get_depth(ctx->param);
874 }
875
876int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
877 {
878 return(ctx->default_verify_callback);
879 }
880
881void SSL_set_verify(SSL *s,int mode,
882 int (*callback)(int ok,X509_STORE_CTX *ctx))
883 {
884 s->verify_mode=mode;
885 if (callback != NULL)
886 s->verify_callback=callback;
887 }
888
889void SSL_set_verify_depth(SSL *s,int depth)
890 {
891 X509_VERIFY_PARAM_set_depth(s->param, depth);
892 }
893
894void SSL_set_read_ahead(SSL *s,int yes)
895 {
896 s->read_ahead=yes;
897 }
898
899int SSL_get_read_ahead(const SSL *s)
900 {
901 return(s->read_ahead);
902 }
903
904int SSL_pending(const SSL *s)
905 {
906 /* SSL_pending cannot work properly if read-ahead is enabled
907 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
908 * and it is impossible to fix since SSL_pending cannot report
909 * errors that may be observed while scanning the new data.
910 * (Note that SSL_pending() is often used as a boolean value,
911 * so we'd better not return -1.)
912 */
913 return(s->method->ssl_pending(s));
914 }
915
916X509 *SSL_get_peer_certificate(const SSL *s)
917 {
918 X509 *r;
919
920 if ((s == NULL) || (s->session == NULL))
921 r=NULL;
922 else
923 r=s->session->peer;
924
David Benjamin150c6172014-08-05 22:22:49 -0400925 if (r == NULL)
926 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700927
David Benjamin150c6172014-08-05 22:22:49 -0400928 return X509_up_ref(r);
Adam Langley95c29f32014-06-20 12:00:00 -0700929 }
930
931STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
932 {
933 STACK_OF(X509) *r;
934
935 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
936 r=NULL;
937 else
938 r=s->session->sess_cert->cert_chain;
939
940 /* If we are a client, cert_chain includes the peer's own
941 * certificate; if we are a server, it does not. */
942
943 return(r);
944 }
945
Adam Langley95c29f32014-06-20 12:00:00 -0700946/* Fix this so it checks all the valid key/cert options */
947int SSL_CTX_check_private_key(const SSL_CTX *ctx)
948 {
949 if ( (ctx == NULL) ||
950 (ctx->cert == NULL) ||
951 (ctx->cert->key->x509 == NULL))
952 {
953 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
954 return(0);
955 }
956 if (ctx->cert->key->privatekey == NULL)
957 {
958 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
959 return(0);
960 }
961 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
962 }
963
964/* Fix this function so that it takes an optional type parameter */
965int SSL_check_private_key(const SSL *ssl)
966 {
967 if (ssl == NULL)
968 {
969 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
970 return(0);
971 }
972 if (ssl->cert == NULL)
973 {
974 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
975 return 0;
976 }
977 if (ssl->cert->key->x509 == NULL)
978 {
979 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
980 return(0);
981 }
982 if (ssl->cert->key->privatekey == NULL)
983 {
984 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
985 return(0);
986 }
987 return(X509_check_private_key(ssl->cert->key->x509,
988 ssl->cert->key->privatekey));
989 }
990
991int SSL_accept(SSL *s)
992 {
993 if (s->handshake_func == 0)
994 /* Not properly initialized yet */
995 SSL_set_accept_state(s);
996
997 return(s->method->ssl_accept(s));
998 }
999
1000int SSL_connect(SSL *s)
1001 {
1002 if (s->handshake_func == 0)
1003 /* Not properly initialized yet */
1004 SSL_set_connect_state(s);
1005
1006 return(s->method->ssl_connect(s));
1007 }
1008
1009long SSL_get_default_timeout(const SSL *s)
1010 {
David Benjaminf4501342014-08-14 17:24:37 -04001011 return SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001012 }
1013
1014int SSL_read(SSL *s,void *buf,int num)
1015 {
1016 if (s->handshake_func == 0)
1017 {
1018 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
1019 return -1;
1020 }
1021
1022 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1023 {
1024 s->rwstate=SSL_NOTHING;
1025 return(0);
1026 }
1027 return(s->method->ssl_read(s,buf,num));
1028 }
1029
1030int SSL_peek(SSL *s,void *buf,int num)
1031 {
1032 if (s->handshake_func == 0)
1033 {
1034 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
1035 return -1;
1036 }
1037
1038 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1039 {
1040 return(0);
1041 }
1042 return(s->method->ssl_peek(s,buf,num));
1043 }
1044
1045int SSL_write(SSL *s,const void *buf,int num)
1046 {
1047 if (s->handshake_func == 0)
1048 {
1049 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
1050 return -1;
1051 }
1052
1053 if (s->shutdown & SSL_SENT_SHUTDOWN)
1054 {
1055 s->rwstate=SSL_NOTHING;
1056 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
1057 return(-1);
1058 }
1059 return(s->method->ssl_write(s,buf,num));
1060 }
1061
1062int SSL_shutdown(SSL *s)
1063 {
1064 /* Note that this function behaves differently from what one might
1065 * expect. Return values are 0 for no success (yet),
1066 * 1 for success; but calling it once is usually not enough,
1067 * even if blocking I/O is used (see ssl3_shutdown).
1068 */
1069
1070 if (s->handshake_func == 0)
1071 {
1072 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1073 return -1;
1074 }
1075
Adam Langleyb2cb0ec2014-09-02 14:28:49 -07001076 if (!SSL_in_init(s))
Adam Langley95c29f32014-06-20 12:00:00 -07001077 return(s->method->ssl_shutdown(s));
1078 else
1079 return(1);
1080 }
1081
1082int SSL_renegotiate(SSL *s)
1083 {
1084 if (s->renegotiate == 0)
1085 s->renegotiate=1;
1086
1087 s->new_session=1;
1088
1089 return(s->method->ssl_renegotiate(s));
1090 }
1091
1092int SSL_renegotiate_abbreviated(SSL *s)
1093 {
1094 if (s->renegotiate == 0)
1095 s->renegotiate=1;
1096
1097 s->new_session=0;
1098
1099 return(s->method->ssl_renegotiate(s));
1100 }
1101
1102int SSL_renegotiate_pending(SSL *s)
1103 {
1104 /* becomes true when negotiation is requested;
1105 * false again once a handshake has finished */
1106 return (s->renegotiate != 0);
1107 }
1108
1109long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1110 {
1111 long l;
1112
1113 switch (cmd)
1114 {
1115 case SSL_CTRL_GET_READ_AHEAD:
1116 return(s->read_ahead);
1117 case SSL_CTRL_SET_READ_AHEAD:
1118 l=s->read_ahead;
1119 s->read_ahead=larg;
1120 return(l);
1121
1122 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1123 s->msg_callback_arg = parg;
1124 return 1;
1125
1126 case SSL_CTRL_OPTIONS:
1127 return(s->options|=larg);
1128 case SSL_CTRL_CLEAR_OPTIONS:
1129 return(s->options&=~larg);
1130 case SSL_CTRL_MODE:
1131 return(s->mode|=larg);
1132 case SSL_CTRL_CLEAR_MODE:
1133 return(s->mode &=~larg);
1134 case SSL_CTRL_GET_MAX_CERT_LIST:
1135 return(s->max_cert_list);
1136 case SSL_CTRL_SET_MAX_CERT_LIST:
1137 l=s->max_cert_list;
1138 s->max_cert_list=larg;
1139 return(l);
1140 case SSL_CTRL_SET_MTU:
Adam Langley95c29f32014-06-20 12:00:00 -07001141 if (larg < (long)dtls1_min_mtu())
1142 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001143 if (SSL_IS_DTLS(s))
1144 {
1145 s->d1->mtu = larg;
1146 return larg;
1147 }
1148 return 0;
1149 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1150 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1151 return 0;
1152 s->max_send_fragment = larg;
1153 return 1;
1154 case SSL_CTRL_GET_RI_SUPPORT:
1155 if (s->s3)
1156 return s->s3->send_connection_binding;
1157 else return 0;
1158 case SSL_CTRL_CERT_FLAGS:
1159 return(s->cert->cert_flags|=larg);
1160 case SSL_CTRL_CLEAR_CERT_FLAGS:
1161 return(s->cert->cert_flags &=~larg);
1162
1163 case SSL_CTRL_GET_RAW_CIPHERLIST:
1164 if (parg)
1165 {
1166 if (s->cert->ciphers_raw == NULL)
1167 return 0;
1168 *(unsigned char **)parg = s->cert->ciphers_raw;
1169 return (int)s->cert->ciphers_rawlen;
1170 }
1171 else
David Benjamin39482a12014-07-20 13:30:15 -04001172 {
1173 /* Passing a NULL |parg| returns the size of a single
1174 * cipher suite value. */
1175 return 2;
1176 }
Adam Langley95c29f32014-06-20 12:00:00 -07001177 default:
1178 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1179 }
1180 }
1181
1182long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1183 {
1184 switch(cmd)
1185 {
1186 case SSL_CTRL_SET_MSG_CALLBACK:
1187 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1188 return 1;
1189
1190 default:
1191 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1192 }
1193 }
1194
1195LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1196 {
1197 return ctx->sessions;
1198 }
1199
1200long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1201 {
1202 long l;
1203
1204 switch (cmd)
1205 {
1206 case SSL_CTRL_GET_READ_AHEAD:
1207 return(ctx->read_ahead);
1208 case SSL_CTRL_SET_READ_AHEAD:
1209 l=ctx->read_ahead;
1210 ctx->read_ahead=larg;
1211 return(l);
1212
1213 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1214 ctx->msg_callback_arg = parg;
1215 return 1;
1216
1217 case SSL_CTRL_GET_MAX_CERT_LIST:
1218 return(ctx->max_cert_list);
1219 case SSL_CTRL_SET_MAX_CERT_LIST:
1220 l=ctx->max_cert_list;
1221 ctx->max_cert_list=larg;
1222 return(l);
1223
1224 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1225 l=ctx->session_cache_size;
1226 ctx->session_cache_size=larg;
1227 return(l);
1228 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1229 return(ctx->session_cache_size);
1230 case SSL_CTRL_SET_SESS_CACHE_MODE:
1231 l=ctx->session_cache_mode;
1232 ctx->session_cache_mode=larg;
1233 return(l);
1234 case SSL_CTRL_GET_SESS_CACHE_MODE:
1235 return(ctx->session_cache_mode);
1236
1237 case SSL_CTRL_SESS_NUMBER:
1238 return(lh_SSL_SESSION_num_items(ctx->sessions));
1239 case SSL_CTRL_SESS_CONNECT:
1240 return(ctx->stats.sess_connect);
1241 case SSL_CTRL_SESS_CONNECT_GOOD:
1242 return(ctx->stats.sess_connect_good);
1243 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1244 return(ctx->stats.sess_connect_renegotiate);
1245 case SSL_CTRL_SESS_ACCEPT:
1246 return(ctx->stats.sess_accept);
1247 case SSL_CTRL_SESS_ACCEPT_GOOD:
1248 return(ctx->stats.sess_accept_good);
1249 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1250 return(ctx->stats.sess_accept_renegotiate);
1251 case SSL_CTRL_SESS_HIT:
1252 return(ctx->stats.sess_hit);
1253 case SSL_CTRL_SESS_CB_HIT:
1254 return(ctx->stats.sess_cb_hit);
1255 case SSL_CTRL_SESS_MISSES:
1256 return(ctx->stats.sess_miss);
1257 case SSL_CTRL_SESS_TIMEOUTS:
1258 return(ctx->stats.sess_timeout);
1259 case SSL_CTRL_SESS_CACHE_FULL:
1260 return(ctx->stats.sess_cache_full);
1261 case SSL_CTRL_OPTIONS:
1262 return(ctx->options|=larg);
1263 case SSL_CTRL_CLEAR_OPTIONS:
1264 return(ctx->options&=~larg);
1265 case SSL_CTRL_MODE:
1266 return(ctx->mode|=larg);
1267 case SSL_CTRL_CLEAR_MODE:
1268 return(ctx->mode&=~larg);
1269 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1270 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1271 return 0;
1272 ctx->max_send_fragment = larg;
1273 return 1;
1274 case SSL_CTRL_CERT_FLAGS:
1275 return(ctx->cert->cert_flags|=larg);
1276 case SSL_CTRL_CLEAR_CERT_FLAGS:
1277 return(ctx->cert->cert_flags &=~larg);
1278 default:
1279 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1280 }
1281 }
1282
1283long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1284 {
1285 switch(cmd)
1286 {
1287 case SSL_CTRL_SET_MSG_CALLBACK:
1288 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1289 return 1;
1290
1291 default:
1292 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1293 }
1294 }
1295
1296int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1297 {
1298 long l;
1299 const SSL_CIPHER *a = in_a;
1300 const SSL_CIPHER *b = in_b;
1301 const long a_id = a->id;
1302 const long b_id = b->id;
1303
1304 l = a_id - b_id;
1305 if (l == 0L)
1306 return(0);
1307 else
1308 return((l > 0)?1:-1);
1309 }
1310
1311int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1312 {
1313 long l;
1314 const long a_id = (*ap)->id;
1315 const long b_id = (*bp)->id;
1316
1317 l = a_id - b_id;
1318 if (l == 0)
1319 return(0);
1320 else
1321 return((l > 0)?1:-1);
1322 }
1323
1324/** return a STACK of the ciphers available for the SSL and in order of
1325 * preference */
1326STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1327 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001328 if (s == NULL)
1329 return NULL;
1330
1331 if (s->cipher_list != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001332 {
Adam Langley858a88d2014-06-20 12:00:00 -07001333 return(s->cipher_list->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -07001334 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001335
1336 if (s->version >= TLS1_1_VERSION)
1337 {
1338 if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001339 return s->ctx->cipher_list_tls11->ciphers;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001340 }
1341
1342 if ((s->ctx != NULL) &&
1343 (s->ctx->cipher_list != NULL))
1344 {
Adam Langley858a88d2014-06-20 12:00:00 -07001345 return(s->ctx->cipher_list->ciphers);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001346 }
1347
Adam Langley95c29f32014-06-20 12:00:00 -07001348 return(NULL);
1349 }
1350
1351/** return a STACK of the ciphers available for the SSL and in order of
1352 * algorithm id */
1353STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1354 {
1355 if (s != NULL)
1356 {
1357 if (s->cipher_list_by_id != NULL)
1358 {
1359 return(s->cipher_list_by_id);
1360 }
1361 else if ((s->ctx != NULL) &&
1362 (s->ctx->cipher_list_by_id != NULL))
1363 {
1364 return(s->ctx->cipher_list_by_id);
1365 }
1366 }
1367 return(NULL);
1368 }
1369
1370/** The old interface to get the same thing as SSL_get_ciphers() */
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001371const char *SSL_get_cipher_list(const SSL *s, int n)
Adam Langley95c29f32014-06-20 12:00:00 -07001372 {
David Benjamin6f260012014-08-15 13:49:12 -04001373 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001374 STACK_OF(SSL_CIPHER) *sk;
1375
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001376 if (s == NULL)
1377 return NULL;
1378 sk = SSL_get_ciphers(s);
1379 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk))
1380 return NULL;
1381 c = sk_SSL_CIPHER_value(sk, n);
1382 if (c == NULL)
1383 return NULL;
1384 return c->name;
Adam Langley95c29f32014-06-20 12:00:00 -07001385 }
1386
1387/** specify the ciphers to be used by default by the SSL_CTX */
1388int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1389 {
1390 STACK_OF(SSL_CIPHER) *sk;
1391
1392 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1393 &ctx->cipher_list_by_id,str, ctx->cert);
1394 /* ssl_create_cipher_list may return an empty stack if it
1395 * was unable to find a cipher matching the given rule string
1396 * (for example if the rule string specifies a cipher which
1397 * has been disabled). This is not an error as far as
1398 * ssl_create_cipher_list is concerned, and hence
1399 * ctx->cipher_list and ctx->cipher_list_by_id has been
1400 * updated. */
1401 if (sk == NULL)
1402 return 0;
1403 else if (sk_SSL_CIPHER_num(sk) == 0)
1404 {
1405 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1406 return 0;
1407 }
1408 return 1;
1409 }
1410
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001411int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str)
1412 {
1413 STACK_OF(SSL_CIPHER) *sk;
1414
1415 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert);
1416 if (sk == NULL)
1417 return 0;
1418 else if (sk_SSL_CIPHER_num(sk) == 0)
1419 {
1420 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH);
1421 return 0;
1422 }
1423 return 1;
1424 }
1425
Adam Langley95c29f32014-06-20 12:00:00 -07001426/** specify the ciphers to be used by the SSL */
1427int SSL_set_cipher_list(SSL *s,const char *str)
1428 {
1429 STACK_OF(SSL_CIPHER) *sk;
1430
1431 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1432 &s->cipher_list_by_id,str, s->cert);
1433 /* see comment in SSL_CTX_set_cipher_list */
1434 if (sk == NULL)
1435 return 0;
1436 else if (sk_SSL_CIPHER_num(sk) == 0)
1437 {
1438 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1439 return 0;
1440 }
1441 return 1;
1442 }
1443
David Benjamin5491e3f2014-09-29 19:33:09 -04001444int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p)
Adam Langley95c29f32014-06-20 12:00:00 -07001445 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001446 size_t i;
David Benjamin6f260012014-08-15 13:49:12 -04001447 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001448 CERT *ct = s->cert;
David Benjamin5491e3f2014-09-29 19:33:09 -04001449 uint8_t *q;
Adam Langley95c29f32014-06-20 12:00:00 -07001450 /* Set disabled masks for this session */
1451 ssl_set_client_disabled(s);
1452
1453 if (sk == NULL) return(0);
1454 q=p;
1455
1456 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1457 {
1458 c=sk_SSL_CIPHER_value(sk,i);
1459 /* Skip disabled ciphers */
1460 if (c->algorithm_ssl & ct->mask_ssl ||
1461 c->algorithm_mkey & ct->mask_k ||
1462 c->algorithm_auth & ct->mask_a)
1463 continue;
David Benjamin39482a12014-07-20 13:30:15 -04001464 s2n(ssl3_get_cipher_value(c), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001465 }
David Benjamin5491e3f2014-09-29 19:33:09 -04001466 /* If all ciphers were disabled, return the error to the caller. */
1467 if (p == q)
Adam Langley95c29f32014-06-20 12:00:00 -07001468 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001469 return 0;
1470 }
1471
1472 /* Add SCSVs. */
1473 if (!s->renegotiate)
1474 {
1475 s2n(SSL3_CK_SCSV & 0xffff, p);
1476 }
1477 if (s->fallback_scsv)
1478 {
1479 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001480 }
1481
1482 return(p-q);
1483 }
1484
David Benjamin9f2c0d72014-10-21 22:00:19 -04001485STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001486 {
David Benjamin39482a12014-07-20 13:30:15 -04001487 CBS cipher_suites = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001488 const SSL_CIPHER *c;
1489 STACK_OF(SSL_CIPHER) *sk;
David Benjamin39482a12014-07-20 13:30:15 -04001490
Adam Langley95c29f32014-06-20 12:00:00 -07001491 if (s->s3)
1492 s->s3->send_connection_binding = 0;
1493
David Benjamin39482a12014-07-20 13:30:15 -04001494 if (CBS_len(&cipher_suites) % 2 != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001495 {
1496 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001497 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001498 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001499
1500 sk = sk_SSL_CIPHER_new_null();
1501 if (sk == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001502 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001503 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1504 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001505 }
1506
David Benjamin39482a12014-07-20 13:30:15 -04001507 if (!CBS_stow(&cipher_suites,
1508 &s->cert->ciphers_raw, &s->cert->ciphers_rawlen))
Adam Langley95c29f32014-06-20 12:00:00 -07001509 {
1510 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1511 goto err;
1512 }
Adam Langley95c29f32014-06-20 12:00:00 -07001513
David Benjamin39482a12014-07-20 13:30:15 -04001514 while (CBS_len(&cipher_suites) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001515 {
David Benjamin39482a12014-07-20 13:30:15 -04001516 uint16_t cipher_suite;
1517
1518 if (!CBS_get_u16(&cipher_suites, &cipher_suite))
1519 {
1520 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1521 goto err;
1522 }
1523
David Benjamin9f2c0d72014-10-21 22:00:19 -04001524 /* Check for SCSV. */
David Benjamin39482a12014-07-20 13:30:15 -04001525 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff))
Adam Langley95c29f32014-06-20 12:00:00 -07001526 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001527 /* SCSV is fatal if renegotiating. */
Adam Langley95c29f32014-06-20 12:00:00 -07001528 if (s->renegotiate)
1529 {
1530 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1531 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1532 goto err;
1533 }
1534 s->s3->send_connection_binding = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001535 continue;
1536 }
1537
David Benjamin9f2c0d72014-10-21 22:00:19 -04001538 /* Check for FALLBACK_SCSV. */
1539 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff))
Adam Langley29707792014-06-20 12:00:00 -07001540 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001541 if (s->version < ssl_get_max_version(s))
1542 {
1543 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1544 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1545 goto err;
1546 }
1547 continue;
Adam Langley29707792014-06-20 12:00:00 -07001548 }
1549
David Benjamin39482a12014-07-20 13:30:15 -04001550 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -07001551 if (c != NULL)
1552 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001553 if (!sk_SSL_CIPHER_push(sk, c))
Adam Langley95c29f32014-06-20 12:00:00 -07001554 {
1555 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1556 goto err;
1557 }
1558 }
1559 }
1560
David Benjamin9f2c0d72014-10-21 22:00:19 -04001561 return sk;
1562
Adam Langley95c29f32014-06-20 12:00:00 -07001563err:
David Benjamin9f2c0d72014-10-21 22:00:19 -04001564 if (sk != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001565 sk_SSL_CIPHER_free(sk);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001566 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001567 }
1568
1569
Adam Langley95c29f32014-06-20 12:00:00 -07001570/** return a servername extension value if provided in Client Hello, or NULL.
1571 * So far, only host_name types are defined (RFC 3546).
1572 */
1573
1574const char *SSL_get_servername(const SSL *s, const int type)
1575 {
1576 if (type != TLSEXT_NAMETYPE_host_name)
1577 return NULL;
1578
1579 return s->session && !s->tlsext_hostname ?
1580 s->session->tlsext_hostname :
1581 s->tlsext_hostname;
1582 }
1583
1584int SSL_get_servername_type(const SSL *s)
1585 {
1586 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1587 return TLSEXT_NAMETYPE_host_name;
1588 return -1;
1589 }
1590
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001591void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx)
1592 {
1593 ctx->signed_cert_timestamps_enabled = 1;
1594 }
1595
1596int SSL_enable_signed_cert_timestamps(SSL *ssl)
1597 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001598 /* Currently not implemented server-side. */
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001599 if (ssl->server)
1600 return 0;
1601
1602 ssl->signed_cert_timestamps_enabled = 1;
1603 return 1;
1604 }
1605
David Benjamin6c7aed02014-08-27 16:42:38 -04001606void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx)
1607 {
1608 ctx->ocsp_stapling_enabled = 1;
1609 }
1610
1611int SSL_enable_ocsp_stapling(SSL *ssl)
1612 {
1613 /* Currently not implemented server-side. */
1614 if (ssl->server)
1615 return 0;
1616 ssl->ocsp_stapling_enabled = 1;
1617 return 1;
1618 }
1619
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001620void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, uint8_t **out, size_t *out_len)
1621 {
Adam Langley3cb50e02014-08-26 14:00:31 -07001622 SSL_SESSION *session = ssl->session;
1623
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001624 *out_len = 0;
1625 *out = NULL;
1626 if (ssl->server)
1627 return;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001628 if (!session || !session->tlsext_signed_cert_timestamp_list)
1629 return;
1630 *out = session->tlsext_signed_cert_timestamp_list;
1631 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1632 }
1633
David Benjamin6c7aed02014-08-27 16:42:38 -04001634void SSL_get0_ocsp_response(const SSL *ssl, uint8_t **out, size_t *out_len)
1635 {
1636 SSL_SESSION *session = ssl->session;
1637
1638 *out_len = 0;
1639 *out = NULL;
1640 if (ssl->server)
1641 return;
1642 if (!session || !session->ocsp_response)
1643 return;
1644 *out = session->ocsp_response;
1645 *out_len = session->ocsp_response_length;
1646 }
1647
Adam Langley95c29f32014-06-20 12:00:00 -07001648/* SSL_select_next_proto implements the standard protocol selection. It is
1649 * expected that this function is called from the callback set by
1650 * SSL_CTX_set_next_proto_select_cb.
1651 *
1652 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1653 * strings. The length byte itself is not included in the length. A byte
1654 * string of length 0 is invalid. No byte string may be truncated.
1655 *
1656 * The current, but experimental algorithm for selecting the protocol is:
1657 *
1658 * 1) If the server doesn't support NPN then this is indicated to the
1659 * callback. In this case, the client application has to abort the connection
1660 * or have a default application level protocol.
1661 *
1662 * 2) If the server supports NPN, but advertises an empty list then the
1663 * client selects the first protcol in its list, but indicates via the
1664 * API that this fallback case was enacted.
1665 *
1666 * 3) Otherwise, the client finds the first protocol in the server's list
1667 * that it supports and selects this protocol. This is because it's
1668 * assumed that the server has better information about which protocol
1669 * a client should use.
1670 *
1671 * 4) If the client doesn't support any of the server's advertised
1672 * protocols, then this is treated the same as case 2.
1673 *
1674 * It returns either
1675 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1676 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1677 */
1678int 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)
1679 {
1680 unsigned int i, j;
1681 const unsigned char *result;
1682 int status = OPENSSL_NPN_UNSUPPORTED;
1683
1684 /* For each protocol in server preference order, see if we support it. */
1685 for (i = 0; i < server_len; )
1686 {
1687 for (j = 0; j < client_len; )
1688 {
1689 if (server[i] == client[j] &&
1690 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1691 {
1692 /* We found a match */
1693 result = &server[i];
1694 status = OPENSSL_NPN_NEGOTIATED;
1695 goto found;
1696 }
1697 j += client[j];
1698 j++;
1699 }
1700 i += server[i];
1701 i++;
1702 }
1703
1704 /* There's no overlap between our protocols and the server's list. */
1705 result = client;
1706 status = OPENSSL_NPN_NO_OVERLAP;
1707
1708 found:
1709 *out = (unsigned char *) result + 1;
1710 *outlen = result[0];
1711 return status;
1712 }
1713
Adam Langley95c29f32014-06-20 12:00:00 -07001714/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1715 * requested protocol for this connection and returns 0. If the client didn't
1716 * request any protocol, then *data is set to NULL.
1717 *
1718 * Note that the client can request any protocol it chooses. The value returned
1719 * from this function need not be a member of the list of supported protocols
1720 * provided by the callback.
1721 */
David Benjaminced551f2014-07-11 23:16:16 -04001722void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, unsigned *len)
Adam Langley95c29f32014-06-20 12:00:00 -07001723 {
1724 *data = s->next_proto_negotiated;
1725 if (!*data) {
1726 *len = 0;
1727 } else {
1728 *len = s->next_proto_negotiated_len;
1729 }
1730}
1731
1732/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1733 * TLS server needs a list of supported protocols for Next Protocol
1734 * Negotiation. The returned list must be in wire format. The list is returned
1735 * by setting |out| to point to it and |outlen| to its length. This memory will
1736 * not be modified, but one should assume that the SSL* keeps a reference to
1737 * it.
1738 *
1739 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1740 * such extension will be included in the ServerHello. */
1741void 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)
1742 {
1743 ctx->next_protos_advertised_cb = cb;
1744 ctx->next_protos_advertised_cb_arg = arg;
1745 }
1746
1747/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1748 * client needs to select a protocol from the server's provided list. |out|
1749 * must be set to point to the selected protocol (which may be within |in|).
1750 * The length of the protocol name must be written into |outlen|. The server's
1751 * advertised protocols are provided in |in| and |inlen|. The callback can
1752 * assume that |in| is syntactically valid.
1753 *
1754 * The client must select a protocol. It is fatal to the connection if this
1755 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1756 */
1757void 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)
1758 {
1759 ctx->next_proto_select_cb = cb;
1760 ctx->next_proto_select_cb_arg = arg;
1761 }
Adam Langley95c29f32014-06-20 12:00:00 -07001762
Adam Langley95c29f32014-06-20 12:00:00 -07001763/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1764 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1765 * length-prefixed strings).
1766 *
1767 * Returns 0 on success. */
1768int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1769 unsigned protos_len)
1770 {
1771 if (ctx->alpn_client_proto_list)
1772 OPENSSL_free(ctx->alpn_client_proto_list);
1773
David Benjamin072c9532014-07-26 11:44:25 -04001774 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001775 if (!ctx->alpn_client_proto_list)
1776 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001777 ctx->alpn_client_proto_list_len = protos_len;
1778
1779 return 0;
1780 }
1781
1782/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1783 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1784 * length-prefixed strings).
1785 *
1786 * Returns 0 on success. */
1787int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1788 unsigned protos_len)
1789 {
1790 if (ssl->alpn_client_proto_list)
1791 OPENSSL_free(ssl->alpn_client_proto_list);
1792
David Benjamin072c9532014-07-26 11:44:25 -04001793 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001794 if (!ssl->alpn_client_proto_list)
1795 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001796 ssl->alpn_client_proto_list_len = protos_len;
1797
1798 return 0;
1799 }
1800
1801/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1802 * during ClientHello processing in order to select an ALPN protocol from the
1803 * client's list of offered protocols. */
1804void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1805 int (*cb) (SSL *ssl,
1806 const unsigned char **out,
1807 unsigned char *outlen,
1808 const unsigned char *in,
1809 unsigned int inlen,
1810 void *arg),
1811 void *arg)
1812 {
1813 ctx->alpn_select_cb = cb;
1814 ctx->alpn_select_cb_arg = arg;
1815 }
1816
1817/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1818 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1819 * including the leading length-prefix byte). If the server didn't respond with
1820 * a negotiated protocol then |*len| will be zero. */
1821void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1822 unsigned *len)
1823 {
1824 *data = NULL;
1825 if (ssl->s3)
1826 *data = ssl->s3->alpn_selected;
1827 if (*data == NULL)
1828 *len = 0;
1829 else
1830 *len = ssl->s3->alpn_selected_len;
1831 }
Adam Langley95c29f32014-06-20 12:00:00 -07001832
1833int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1834 const char *label, size_t llen, const unsigned char *p, size_t plen,
1835 int use_context)
1836 {
1837 if (s->version < TLS1_VERSION)
1838 return -1;
1839
1840 return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1841 llen, p, plen,
1842 use_context);
1843 }
1844
1845static uint32_t ssl_session_hash(const SSL_SESSION *a)
1846 {
1847 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1848 ((uint32_t) a->session_id[1] << 8) ||
1849 ((uint32_t) a->session_id[2] << 16) ||
1850 ((uint32_t) a->session_id[3] << 24);
1851
1852 return hash;
1853 }
1854
1855/* NB: If this function (or indeed the hash function which uses a sort of
1856 * coarser function than this one) is changed, ensure
1857 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1858 * able to construct an SSL_SESSION that will collide with any existing session
1859 * with a matching session ID. */
1860static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1861 {
1862 if (a->ssl_version != b->ssl_version)
1863 return(1);
1864 if (a->session_id_length != b->session_id_length)
1865 return(1);
1866 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1867 }
1868
1869SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1870 {
1871 SSL_CTX *ret=NULL;
1872
1873 if (meth == NULL)
1874 {
1875 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1876 return(NULL);
1877 }
1878
Adam Langley95c29f32014-06-20 12:00:00 -07001879 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1880 {
1881 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1882 goto err;
1883 }
1884 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1885 if (ret == NULL)
1886 goto err;
1887
1888 memset(ret,0,sizeof(SSL_CTX));
1889
1890 ret->method=meth;
1891
1892 ret->cert_store=NULL;
1893 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1894 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1895 ret->session_cache_head=NULL;
1896 ret->session_cache_tail=NULL;
1897
1898 /* We take the system default */
David Benjaminf4501342014-08-14 17:24:37 -04001899 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001900
1901 ret->new_session_cb=0;
1902 ret->remove_session_cb=0;
1903 ret->get_session_cb=0;
1904 ret->generate_session_id=0;
1905
1906 memset((char *)&ret->stats,0,sizeof(ret->stats));
1907
1908 ret->references=1;
1909 ret->quiet_shutdown=0;
1910
Adam Langley95c29f32014-06-20 12:00:00 -07001911 ret->info_callback=NULL;
1912
1913 ret->app_verify_callback=0;
1914 ret->app_verify_arg=NULL;
1915
1916 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1917 ret->read_ahead=0;
1918 ret->msg_callback=0;
1919 ret->msg_callback_arg=NULL;
1920 ret->verify_mode=SSL_VERIFY_NONE;
1921#if 0
1922 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1923#endif
1924 ret->sid_ctx_length=0;
1925 ret->default_verify_callback=NULL;
1926 if ((ret->cert=ssl_cert_new()) == NULL)
1927 goto err;
1928
1929 ret->default_passwd_callback=0;
1930 ret->default_passwd_callback_userdata=NULL;
1931 ret->client_cert_cb=0;
1932 ret->app_gen_cookie_cb=0;
1933 ret->app_verify_cookie_cb=0;
1934
1935 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1936 if (ret->sessions == NULL) goto err;
1937 ret->cert_store=X509_STORE_new();
1938 if (ret->cert_store == NULL) goto err;
1939
1940 ssl_create_cipher_list(ret->method,
1941 &ret->cipher_list,&ret->cipher_list_by_id,
David Benjamin5e4f6e92014-11-17 03:23:24 -05001942 SSL_DEFAULT_CIPHER_LIST, ret->cert);
Adam Langley95c29f32014-06-20 12:00:00 -07001943 if (ret->cipher_list == NULL
Adam Langley858a88d2014-06-20 12:00:00 -07001944 || sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001945 {
1946 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1947 goto err2;
1948 }
1949
1950 ret->param = X509_VERIFY_PARAM_new();
1951 if (!ret->param)
1952 goto err;
1953
Adam Langley95c29f32014-06-20 12:00:00 -07001954 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1955 goto err;
1956
1957 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1958
1959 ret->extra_certs=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001960
1961 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1962
Adam Langley95c29f32014-06-20 12:00:00 -07001963 ret->tlsext_servername_callback = 0;
1964 ret->tlsext_servername_arg = NULL;
1965 /* Setup RFC4507 ticket keys */
1966 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
1967 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
1968 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
1969 ret->options |= SSL_OP_NO_TICKET;
1970
1971 ret->tlsext_status_cb = 0;
1972 ret->tlsext_status_arg = NULL;
1973
Adam Langley95c29f32014-06-20 12:00:00 -07001974 ret->next_protos_advertised_cb = 0;
1975 ret->next_proto_select_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001976 ret->psk_identity_hint=NULL;
1977 ret->psk_client_callback=NULL;
1978 ret->psk_server_callback=NULL;
David Benjamin422d3a42014-08-20 11:09:03 -04001979
Adam Langley95c29f32014-06-20 12:00:00 -07001980 /* Default is to connect to non-RI servers. When RI is more widely
1981 * deployed might change this.
1982 */
1983 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1984
1985 return(ret);
1986err:
1987 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
1988err2:
1989 if (ret != NULL) SSL_CTX_free(ret);
1990 return(NULL);
1991 }
1992
Adam Langley95c29f32014-06-20 12:00:00 -07001993
1994void SSL_CTX_free(SSL_CTX *a)
1995 {
1996 int i;
1997
1998 if (a == NULL) return;
1999
2000 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
2001#ifdef REF_PRINT
2002 REF_PRINT("SSL_CTX",a);
2003#endif
2004 if (i > 0) return;
2005#ifdef REF_CHECK
2006 if (i < 0)
2007 {
2008 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
2009 abort(); /* ok */
2010 }
2011#endif
2012
2013 if (a->param)
2014 X509_VERIFY_PARAM_free(a->param);
2015
2016 /*
2017 * Free internal session cache. However: the remove_cb() may reference
2018 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2019 * after the sessions were flushed.
2020 * As the ex_data handling routines might also touch the session cache,
2021 * the most secure solution seems to be: empty (flush) the cache, then
2022 * free ex_data, then finally free the cache.
2023 * (See ticket [openssl.org #212].)
2024 */
2025 if (a->sessions != NULL)
2026 SSL_CTX_flush_sessions(a,0);
2027
2028 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
2029
2030 if (a->sessions != NULL)
2031 lh_SSL_SESSION_free(a->sessions);
2032
2033 if (a->cert_store != NULL)
2034 X509_STORE_free(a->cert_store);
2035 if (a->cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002036 ssl_cipher_preference_list_free(a->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002037 if (a->cipher_list_by_id != NULL)
2038 sk_SSL_CIPHER_free(a->cipher_list_by_id);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07002039 if (a->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002040 ssl_cipher_preference_list_free(a->cipher_list_tls11);
Adam Langley95c29f32014-06-20 12:00:00 -07002041 if (a->cert != NULL)
2042 ssl_cert_free(a->cert);
2043 if (a->client_CA != NULL)
2044 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2045 if (a->extra_certs != NULL)
2046 sk_X509_pop_free(a->extra_certs,X509_free);
Adam Langley95c29f32014-06-20 12:00:00 -07002047
2048 if (a->srtp_profiles)
2049 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2050
Adam Langley95c29f32014-06-20 12:00:00 -07002051 if (a->psk_identity_hint)
2052 OPENSSL_free(a->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07002053
Adam Langley95c29f32014-06-20 12:00:00 -07002054 if (a->tlsext_ecpointformatlist)
2055 OPENSSL_free(a->tlsext_ecpointformatlist);
2056 if (a->tlsext_ellipticcurvelist)
2057 OPENSSL_free(a->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -07002058 if (a->alpn_client_proto_list != NULL)
2059 OPENSSL_free(a->alpn_client_proto_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002060
Adam Langley1258b6a2014-06-20 12:00:00 -07002061 if (a->tlsext_channel_id_private)
2062 EVP_PKEY_free(a->tlsext_channel_id_private);
Adam Langley1258b6a2014-06-20 12:00:00 -07002063
David Benjamin859ec3c2014-09-02 16:29:36 -04002064 if (a->keylog_bio)
2065 BIO_free(a->keylog_bio);
2066
Adam Langley95c29f32014-06-20 12:00:00 -07002067 OPENSSL_free(a);
2068 }
2069
2070void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2071 {
2072 ctx->default_passwd_callback=cb;
2073 }
2074
2075void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2076 {
2077 ctx->default_passwd_callback_userdata=u;
2078 }
2079
2080void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2081 {
2082 ctx->app_verify_callback=cb;
2083 ctx->app_verify_arg=arg;
2084 }
2085
2086void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2087 {
2088 ctx->verify_mode=mode;
2089 ctx->default_verify_callback=cb;
2090 }
2091
2092void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2093 {
2094 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2095 }
2096
2097void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2098 {
2099 ssl_cert_set_cert_cb(c->cert, cb, arg);
2100 }
2101
2102void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2103 {
2104 ssl_cert_set_cert_cb(s->cert, cb, arg);
2105 }
2106
David Benjamin033e5f42014-11-13 18:47:41 -05002107static int ssl_has_key(SSL *s, size_t idx)
2108 {
2109 CERT_PKEY *cpk = &s->cert->pkeys[idx];
2110 return cpk->x509 && cpk->privatekey;
2111 }
2112
David Benjaminf31e6812014-11-13 18:05:55 -05002113void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
2114 unsigned long *out_mask_a)
Adam Langley95c29f32014-06-20 12:00:00 -07002115 {
David Benjaminf31e6812014-11-13 18:05:55 -05002116 CERT *c = s->cert;
David Benjaminf31e6812014-11-13 18:05:55 -05002117 int rsa_enc, rsa_sign, dh_tmp;
2118 unsigned long mask_k, mask_a;
David Benjamin060d9d22014-07-15 00:54:26 -04002119 int have_ecc_cert, ecdsa_ok;
David Benjamin0da0e182014-08-19 16:20:28 -04002120 int have_ecdh_tmp;
David Benjaminf31e6812014-11-13 18:05:55 -05002121 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002122
David Benjaminf31e6812014-11-13 18:05:55 -05002123 if (c == NULL)
2124 {
2125 /* TODO(davidben): Is this codepath possible? */
2126 *out_mask_k = 0;
2127 *out_mask_a = 0;
2128 return;
2129 }
Adam Langley95c29f32014-06-20 12:00:00 -07002130
David Benjaminf31e6812014-11-13 18:05:55 -05002131 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002132
David Benjaminf31e6812014-11-13 18:05:55 -05002133 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
David Benjamin033e5f42014-11-13 18:47:41 -05002134 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2135 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2136 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
David Benjaminf31e6812014-11-13 18:05:55 -05002137 mask_k = 0;
2138 mask_a = 0;
2139
David Benjamin77a942b2014-07-15 01:22:50 -04002140 if (rsa_enc)
David Benjaminf31e6812014-11-13 18:05:55 -05002141 mask_k |= SSL_kRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002142
Adam Langley95c29f32014-06-20 12:00:00 -07002143 if (dh_tmp)
David Benjaminf31e6812014-11-13 18:05:55 -05002144 mask_k |= SSL_kEDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002145
Adam Langley95c29f32014-06-20 12:00:00 -07002146 if (rsa_enc || rsa_sign)
2147 {
David Benjaminf31e6812014-11-13 18:05:55 -05002148 mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002149 }
2150
David Benjaminf31e6812014-11-13 18:05:55 -05002151 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002152
David Benjamin0da0e182014-08-19 16:20:28 -04002153 /* An ECC certificate may be usable for ECDSA cipher suites depending on
David Benjamin033e5f42014-11-13 18:47:41 -05002154 * the key usage extension and on the client's curve preferences. */
Adam Langley95c29f32014-06-20 12:00:00 -07002155 if (have_ecc_cert)
2156 {
David Benjamin033e5f42014-11-13 18:47:41 -05002157 x = c->pkeys[SSL_PKEY_ECC].x509;
Adam Langley95c29f32014-06-20 12:00:00 -07002158 /* This call populates extension flags (ex_flags) */
2159 X509_check_purpose(x, -1, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07002160 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2161 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
David Benjamin033e5f42014-11-13 18:47:41 -05002162 if (!tls1_check_ec_cert(s, x))
Adam Langley95c29f32014-06-20 12:00:00 -07002163 ecdsa_ok = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002164 if (ecdsa_ok)
2165 {
David Benjaminf31e6812014-11-13 18:05:55 -05002166 mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002167 }
Adam Langley95c29f32014-06-20 12:00:00 -07002168 }
Adam Langley95c29f32014-06-20 12:00:00 -07002169
David Benjaminf31e6812014-11-13 18:05:55 -05002170 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2171 * key, check it. */
2172 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002173 {
David Benjaminf31e6812014-11-13 18:05:55 -05002174 mask_k |= SSL_kEECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002175 }
Adam Langley95c29f32014-06-20 12:00:00 -07002176
David Benjaminf31e6812014-11-13 18:05:55 -05002177 /* PSK requires a server callback. */
2178 if (s->psk_server_callback != NULL)
2179 {
2180 mask_k |= SSL_kPSK;
2181 mask_a |= SSL_aPSK;
2182 }
Adam Langley95c29f32014-06-20 12:00:00 -07002183
David Benjaminf31e6812014-11-13 18:05:55 -05002184 *out_mask_k = mask_k;
2185 *out_mask_a = mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002186 }
2187
2188/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2189#define ku_reject(x, usage) \
2190 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2191
Adam Langley95c29f32014-06-20 12:00:00 -07002192
2193int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2194 {
David Benjamin0da0e182014-08-19 16:20:28 -04002195 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002196 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2197 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2198
Adam Langley95c29f32014-06-20 12:00:00 -07002199 alg_a = cs->algorithm_auth;
2200
Adam Langley95c29f32014-06-20 12:00:00 -07002201 /* This call populates the ex_flags field correctly */
2202 X509_check_purpose(x, -1, 0);
2203 if ((x->sig_alg) && (x->sig_alg->algorithm))
2204 {
2205 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2206 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2207 }
Adam Langley95c29f32014-06-20 12:00:00 -07002208 if (alg_a & SSL_aECDSA)
2209 {
2210 /* key usage, if present, must allow signing */
2211 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2212 {
2213 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2214 return 0;
2215 }
2216 }
2217
2218 return 1; /* all checks are ok */
2219 }
2220
Adam Langley95c29f32014-06-20 12:00:00 -07002221
2222static int ssl_get_server_cert_index(const SSL *s)
2223 {
2224 int idx;
2225 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2226 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2227 idx = SSL_PKEY_RSA_SIGN;
2228 if (idx == -1)
2229 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2230 return idx;
2231 }
2232
2233CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2234 {
David Benjaminf31e6812014-11-13 18:05:55 -05002235 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002236
2237 /* This may or may not be an error. */
2238 if (i < 0)
2239 return NULL;
2240
2241 /* May be NULL. */
David Benjaminf31e6812014-11-13 18:05:55 -05002242 return &s->cert->pkeys[i];
Adam Langley95c29f32014-06-20 12:00:00 -07002243 }
2244
David Benjaminec2f27d2014-11-13 19:17:25 -05002245EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher)
Adam Langley95c29f32014-06-20 12:00:00 -07002246 {
2247 unsigned long alg_a;
2248 CERT *c;
2249 int idx = -1;
2250
2251 alg_a = cipher->algorithm_auth;
2252 c=s->cert;
2253
David Benjaminef2116d2014-08-19 20:21:56 -04002254 if (alg_a & SSL_aRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002255 {
2256 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2257 idx = SSL_PKEY_RSA_SIGN;
2258 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2259 idx = SSL_PKEY_RSA_ENC;
2260 }
2261 else if ((alg_a & SSL_aECDSA) &&
2262 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2263 idx = SSL_PKEY_ECC;
2264 if (idx == -1)
2265 {
2266 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2267 return(NULL);
2268 }
Adam Langley95c29f32014-06-20 12:00:00 -07002269 return c->pkeys[idx].privatekey;
2270 }
2271
Adam Langley95c29f32014-06-20 12:00:00 -07002272void ssl_update_cache(SSL *s,int mode)
2273 {
2274 int i;
2275
2276 /* If the session_id_length is 0, we are not supposed to cache it,
2277 * and it would be rather hard to do anyway :-) */
2278 if (s->session->session_id_length == 0) return;
2279
David Benjamin041b58a2014-08-27 15:25:21 -04002280 i=s->initial_ctx->session_cache_mode;
Adam Langley95c29f32014-06-20 12:00:00 -07002281 if ((i & mode) && (!s->hit)
2282 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
David Benjamin041b58a2014-08-27 15:25:21 -04002283 || SSL_CTX_add_session(s->initial_ctx,s->session))
2284 && (s->initial_ctx->new_session_cb != NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07002285 {
2286 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
David Benjamin041b58a2014-08-27 15:25:21 -04002287 if (!s->initial_ctx->new_session_cb(s,s->session))
Adam Langley95c29f32014-06-20 12:00:00 -07002288 SSL_SESSION_free(s->session);
2289 }
2290
2291 /* auto flush every 255 connections */
2292 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2293 ((i & mode) == mode))
2294 {
2295 if ( (((mode & SSL_SESS_CACHE_CLIENT)
David Benjamin041b58a2014-08-27 15:25:21 -04002296 ?s->initial_ctx->stats.sess_connect_good
2297 :s->initial_ctx->stats.sess_accept_good) & 0xff) == 0xff)
Adam Langley95c29f32014-06-20 12:00:00 -07002298 {
David Benjamin041b58a2014-08-27 15:25:21 -04002299 SSL_CTX_flush_sessions(s->initial_ctx,(unsigned long)time(NULL));
Adam Langley95c29f32014-06-20 12:00:00 -07002300 }
2301 }
2302 }
2303
2304const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
2305 {
2306 return ctx->method;
2307 }
2308
2309const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2310 {
2311 return(s->method);
2312 }
2313
2314int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2315 {
2316 int conn= -1;
2317 int ret=1;
2318
2319 if (s->method != meth)
2320 {
2321 if (s->handshake_func != NULL)
2322 conn=(s->handshake_func == s->method->ssl_connect);
2323
2324 if (s->method->version == meth->version)
2325 s->method=meth;
2326 else
2327 {
2328 s->method->ssl_free(s);
2329 s->method=meth;
2330 ret=s->method->ssl_new(s);
2331 }
2332
2333 if (conn == 1)
2334 s->handshake_func=meth->ssl_connect;
2335 else if (conn == 0)
2336 s->handshake_func=meth->ssl_accept;
2337 }
2338 return(ret);
2339 }
2340
2341int SSL_get_error(const SSL *s,int i)
2342 {
2343 int reason;
2344 unsigned long l;
2345 BIO *bio;
2346
2347 if (i > 0) return(SSL_ERROR_NONE);
2348
2349 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2350 * etc, where we do encode the error */
2351 if ((l=ERR_peek_error()) != 0)
2352 {
2353 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2354 return(SSL_ERROR_SYSCALL);
2355 else
2356 return(SSL_ERROR_SSL);
2357 }
2358
Adam Langleyb2ce0582014-06-20 12:00:00 -07002359 if ((i < 0) && SSL_want_session(s))
2360 return(SSL_ERROR_PENDING_SESSION);
2361
Adam Langleydc9b1412014-06-20 12:00:00 -07002362 if ((i < 0) && SSL_want_certificate(s))
2363 return(SSL_ERROR_PENDING_CERTIFICATE);
2364
Adam Langley95c29f32014-06-20 12:00:00 -07002365 if ((i < 0) && SSL_want_read(s))
2366 {
2367 bio=SSL_get_rbio(s);
2368 if (BIO_should_read(bio))
2369 return(SSL_ERROR_WANT_READ);
2370 else if (BIO_should_write(bio))
2371 /* This one doesn't make too much sense ... We never try
2372 * to write to the rbio, and an application program where
2373 * rbio and wbio are separate couldn't even know what it
2374 * should wait for.
2375 * However if we ever set s->rwstate incorrectly
2376 * (so that we have SSL_want_read(s) instead of
2377 * SSL_want_write(s)) and rbio and wbio *are* the same,
2378 * this test works around that bug; so it might be safer
2379 * to keep it. */
2380 return(SSL_ERROR_WANT_WRITE);
2381 else if (BIO_should_io_special(bio))
2382 {
2383 reason=BIO_get_retry_reason(bio);
2384 if (reason == BIO_RR_CONNECT)
2385 return(SSL_ERROR_WANT_CONNECT);
2386 else if (reason == BIO_RR_ACCEPT)
2387 return(SSL_ERROR_WANT_ACCEPT);
2388 else
2389 return(SSL_ERROR_SYSCALL); /* unknown */
2390 }
2391 }
2392
2393 if ((i < 0) && SSL_want_write(s))
2394 {
2395 bio=SSL_get_wbio(s);
2396 if (BIO_should_write(bio))
2397 return(SSL_ERROR_WANT_WRITE);
2398 else if (BIO_should_read(bio))
2399 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2400 return(SSL_ERROR_WANT_READ);
2401 else if (BIO_should_io_special(bio))
2402 {
2403 reason=BIO_get_retry_reason(bio);
2404 if (reason == BIO_RR_CONNECT)
2405 return(SSL_ERROR_WANT_CONNECT);
2406 else if (reason == BIO_RR_ACCEPT)
2407 return(SSL_ERROR_WANT_ACCEPT);
2408 else
2409 return(SSL_ERROR_SYSCALL);
2410 }
2411 }
2412 if ((i < 0) && SSL_want_x509_lookup(s))
2413 {
2414 return(SSL_ERROR_WANT_X509_LOOKUP);
2415 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002416 if ((i < 0) && SSL_want_channel_id_lookup(s))
2417 {
2418 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2419 }
Adam Langley95c29f32014-06-20 12:00:00 -07002420
2421 if (i == 0)
2422 {
David Benjamin5e4f6e92014-11-17 03:23:24 -05002423 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2424 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
Adam Langley95c29f32014-06-20 12:00:00 -07002425 return(SSL_ERROR_ZERO_RETURN);
Adam Langley95c29f32014-06-20 12:00:00 -07002426 }
2427 return(SSL_ERROR_SYSCALL);
2428 }
2429
2430int SSL_do_handshake(SSL *s)
2431 {
2432 int ret=1;
2433
2434 if (s->handshake_func == NULL)
2435 {
2436 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2437 return(-1);
2438 }
2439
2440 s->method->ssl_renegotiate_check(s);
2441
2442 if (SSL_in_init(s) || SSL_in_before(s))
2443 {
2444 ret=s->handshake_func(s);
2445 }
2446 return(ret);
2447 }
2448
2449/* For the next 2 functions, SSL_clear() sets shutdown and so
2450 * one of these calls will reset it */
2451void SSL_set_accept_state(SSL *s)
2452 {
2453 s->server=1;
2454 s->shutdown=0;
2455 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2456 s->handshake_func=s->method->ssl_accept;
2457 /* clear the current cipher */
2458 ssl_clear_cipher_ctx(s);
2459 ssl_clear_hash_ctx(&s->read_hash);
2460 ssl_clear_hash_ctx(&s->write_hash);
2461 }
2462
2463void SSL_set_connect_state(SSL *s)
2464 {
2465 s->server=0;
2466 s->shutdown=0;
2467 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2468 s->handshake_func=s->method->ssl_connect;
2469 /* clear the current cipher */
2470 ssl_clear_cipher_ctx(s);
2471 ssl_clear_hash_ctx(&s->read_hash);
2472 ssl_clear_hash_ctx(&s->write_hash);
2473 }
2474
2475int ssl_undefined_function(SSL *s)
2476 {
2477 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2478 return(0);
2479 }
2480
2481int ssl_undefined_void_function(void)
2482 {
2483 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2484 return(0);
2485 }
2486
2487int ssl_undefined_const_function(const SSL *s)
2488 {
2489 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2490 return(0);
2491 }
2492
2493SSL_METHOD *ssl_bad_method(int ver)
2494 {
2495 OPENSSL_PUT_ERROR(SSL, ssl_bad_method, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2496 return(NULL);
2497 }
2498
Adam Langley0f4746e2014-08-13 12:26:32 -07002499static const char *ssl_get_version(int version)
Adam Langley95c29f32014-06-20 12:00:00 -07002500 {
Adam Langley0f4746e2014-08-13 12:26:32 -07002501 if (version == TLS1_2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002502 return("TLSv1.2");
Adam Langley0f4746e2014-08-13 12:26:32 -07002503 else if (version == TLS1_1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002504 return("TLSv1.1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002505 else if (version == TLS1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002506 return("TLSv1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002507 else if (version == SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002508 return("SSLv3");
Adam Langley95c29f32014-06-20 12:00:00 -07002509 else
2510 return("unknown");
2511 }
2512
Adam Langley0f4746e2014-08-13 12:26:32 -07002513const char *SSL_get_version(const SSL *s)
2514 {
2515 return ssl_get_version(s->version);
2516 }
2517
2518const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2519 {
2520 return ssl_get_version(sess->ssl_version);
2521 }
2522
Adam Langley95c29f32014-06-20 12:00:00 -07002523void ssl_clear_cipher_ctx(SSL *s)
2524 {
2525 if (s->enc_read_ctx != NULL)
2526 {
2527 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2528 OPENSSL_free(s->enc_read_ctx);
2529 s->enc_read_ctx=NULL;
2530 }
2531 if (s->enc_write_ctx != NULL)
2532 {
2533 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2534 OPENSSL_free(s->enc_write_ctx);
2535 s->enc_write_ctx=NULL;
2536 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002537 if (s->aead_read_ctx != NULL)
2538 {
2539 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2540 OPENSSL_free(s->aead_read_ctx);
2541 s->aead_read_ctx = NULL;
2542 }
2543 if (s->aead_write_ctx != NULL)
2544 {
2545 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2546 OPENSSL_free(s->aead_write_ctx);
2547 s->aead_write_ctx = NULL;
2548 }
Adam Langley95c29f32014-06-20 12:00:00 -07002549 }
2550
2551X509 *SSL_get_certificate(const SSL *s)
2552 {
2553 if (s->cert != NULL)
2554 return(s->cert->key->x509);
2555 else
2556 return(NULL);
2557 }
2558
2559EVP_PKEY *SSL_get_privatekey(const SSL *s)
2560 {
2561 if (s->cert != NULL)
2562 return(s->cert->key->privatekey);
2563 else
2564 return(NULL);
2565 }
2566
2567X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2568 {
2569 if (ctx->cert != NULL)
2570 return ctx->cert->key->x509;
2571 else
2572 return NULL;
2573 }
2574
2575EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2576 {
2577 if (ctx->cert != NULL)
2578 return ctx->cert->key->privatekey;
2579 else
2580 return NULL ;
2581 }
2582
2583const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2584 {
2585 if ((s->session != NULL) && (s->session->cipher != NULL))
2586 return(s->session->cipher);
2587 return(NULL);
2588 }
2589const void *SSL_get_current_compression(SSL *s)
2590 {
2591 return NULL;
2592 }
2593const void *SSL_get_current_expansion(SSL *s)
2594 {
2595 return NULL;
2596 }
2597
2598int ssl_init_wbio_buffer(SSL *s,int push)
2599 {
2600 BIO *bbio;
2601
2602 if (s->bbio == NULL)
2603 {
2604 bbio=BIO_new(BIO_f_buffer());
2605 if (bbio == NULL) return(0);
2606 s->bbio=bbio;
2607 }
2608 else
2609 {
2610 bbio=s->bbio;
2611 if (s->bbio == s->wbio)
2612 s->wbio=BIO_pop(s->wbio);
2613 }
2614 (void)BIO_reset(bbio);
2615/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2616 if (!BIO_set_read_buffer_size(bbio,1))
2617 {
2618 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2619 return(0);
2620 }
2621 if (push)
2622 {
2623 if (s->wbio != bbio)
2624 s->wbio=BIO_push(bbio,s->wbio);
2625 }
2626 else
2627 {
2628 if (s->wbio == bbio)
2629 s->wbio=BIO_pop(bbio);
2630 }
2631 return(1);
2632 }
2633
2634void ssl_free_wbio_buffer(SSL *s)
2635 {
2636 if (s->bbio == NULL) return;
2637
2638 if (s->bbio == s->wbio)
2639 {
2640 /* remove buffering */
2641 s->wbio=BIO_pop(s->wbio);
2642#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2643 assert(s->wbio != NULL);
2644#endif
2645 }
2646 BIO_free(s->bbio);
2647 s->bbio=NULL;
2648 }
2649
2650void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2651 {
2652 ctx->quiet_shutdown=mode;
2653 }
2654
2655int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2656 {
2657 return(ctx->quiet_shutdown);
2658 }
2659
2660void SSL_set_quiet_shutdown(SSL *s,int mode)
2661 {
2662 s->quiet_shutdown=mode;
2663 }
2664
2665int SSL_get_quiet_shutdown(const SSL *s)
2666 {
2667 return(s->quiet_shutdown);
2668 }
2669
2670void SSL_set_shutdown(SSL *s,int mode)
2671 {
2672 s->shutdown=mode;
2673 }
2674
2675int SSL_get_shutdown(const SSL *s)
2676 {
2677 return(s->shutdown);
2678 }
2679
2680int SSL_version(const SSL *s)
2681 {
2682 return(s->version);
2683 }
2684
2685SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2686 {
2687 return(ssl->ctx);
2688 }
2689
2690SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2691 {
2692 if (ssl->ctx == ctx)
2693 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002694 if (ctx == NULL)
2695 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002696 if (ssl->cert != NULL)
2697 ssl_cert_free(ssl->cert);
2698 ssl->cert = ssl_cert_dup(ctx->cert);
2699 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2700 if (ssl->ctx != NULL)
2701 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2702 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07002703
2704 ssl->sid_ctx_length = ctx->sid_ctx_length;
2705 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2706 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2707
Adam Langley95c29f32014-06-20 12:00:00 -07002708 return(ssl->ctx);
2709 }
2710
2711#ifndef OPENSSL_NO_STDIO
2712int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2713 {
2714 return(X509_STORE_set_default_paths(ctx->cert_store));
2715 }
2716
2717int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2718 const char *CApath)
2719 {
2720 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2721 }
2722#endif
2723
2724void SSL_set_info_callback(SSL *ssl,
2725 void (*cb)(const SSL *ssl,int type,int val))
2726 {
2727 ssl->info_callback=cb;
2728 }
2729
2730/* One compiler (Diab DCC) doesn't like argument names in returned
2731 function pointer. */
2732void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2733 {
2734 return ssl->info_callback;
2735 }
2736
2737int SSL_state(const SSL *ssl)
2738 {
2739 return(ssl->state);
2740 }
2741
2742void SSL_set_state(SSL *ssl, int state)
2743 {
2744 ssl->state = state;
2745 }
2746
2747void SSL_set_verify_result(SSL *ssl,long arg)
2748 {
2749 ssl->verify_result=arg;
2750 }
2751
2752long SSL_get_verify_result(const SSL *ssl)
2753 {
2754 return(ssl->verify_result);
2755 }
2756
2757int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2758 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2759 {
2760 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2761 new_func, dup_func, free_func);
2762 }
2763
2764int SSL_set_ex_data(SSL *s,int idx,void *arg)
2765 {
2766 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2767 }
2768
2769void *SSL_get_ex_data(const SSL *s,int idx)
2770 {
2771 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2772 }
2773
2774int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2775 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2776 {
2777 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2778 new_func, dup_func, free_func);
2779 }
2780
2781int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2782 {
2783 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2784 }
2785
2786void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2787 {
2788 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2789 }
2790
2791int ssl_ok(SSL *s)
2792 {
2793 return(1);
2794 }
2795
2796X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2797 {
2798 return(ctx->cert_store);
2799 }
2800
2801void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2802 {
2803 if (ctx->cert_store != NULL)
2804 X509_STORE_free(ctx->cert_store);
2805 ctx->cert_store=store;
2806 }
2807
2808int SSL_want(const SSL *s)
2809 {
2810 return(s->rwstate);
2811 }
2812
2813/*!
2814 * \brief Set the callback for generating temporary RSA keys.
2815 * \param ctx the SSL context.
2816 * \param cb the callback
2817 */
2818
Adam Langley95c29f32014-06-20 12:00:00 -07002819void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2820 int is_export,
2821 int keylength))
2822 {
2823 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2824 }
2825
2826void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2827 int is_export,
2828 int keylength))
2829 {
2830 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2831 }
Adam Langley95c29f32014-06-20 12:00:00 -07002832
2833#ifdef DOXYGEN
2834/*!
2835 * \brief The RSA temporary key callback function.
2836 * \param ssl the SSL session.
2837 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2838 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2839 * of the required key in bits.
2840 * \return the temporary RSA key.
2841 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2842 */
2843
2844RSA *cb(SSL *ssl,int is_export,int keylength)
2845 {}
2846#endif
2847
2848/*!
2849 * \brief Set the callback for generating temporary DH keys.
2850 * \param ctx the SSL context.
2851 * \param dh the callback
2852 */
2853
Adam Langley95c29f32014-06-20 12:00:00 -07002854void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2855 int keylength))
2856 {
2857 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2858 }
2859
2860void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2861 int keylength))
2862 {
2863 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2864 }
Adam Langley95c29f32014-06-20 12:00:00 -07002865
Adam Langley95c29f32014-06-20 12:00:00 -07002866void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2867 int keylength))
2868 {
2869 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2870 }
2871
2872void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2873 int keylength))
2874 {
2875 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2876 }
Adam Langley95c29f32014-06-20 12:00:00 -07002877
Adam Langley95c29f32014-06-20 12:00:00 -07002878int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2879 {
2880 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2881 {
2882 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2883 return 0;
2884 }
2885 if (ctx->psk_identity_hint != NULL)
2886 OPENSSL_free(ctx->psk_identity_hint);
2887 if (identity_hint != NULL)
2888 {
2889 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2890 if (ctx->psk_identity_hint == NULL)
2891 return 0;
2892 }
2893 else
2894 ctx->psk_identity_hint = NULL;
2895 return 1;
2896 }
2897
2898int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
2899 {
2900 if (s == NULL)
2901 return 0;
2902
Adam Langley95c29f32014-06-20 12:00:00 -07002903 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2904 {
2905 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2906 return 0;
2907 }
Adam Langley0289c732014-06-20 12:00:00 -07002908
David Benjamin688d8df2014-11-02 23:06:42 -05002909 /* Clear currently configured hint, if any. */
Adam Langley0289c732014-06-20 12:00:00 -07002910 if (s->psk_identity_hint != NULL)
2911 {
2912 OPENSSL_free(s->psk_identity_hint);
2913 s->psk_identity_hint = NULL;
2914 }
Adam Langley0289c732014-06-20 12:00:00 -07002915
Adam Langley95c29f32014-06-20 12:00:00 -07002916 if (identity_hint != NULL)
2917 {
David Benjamin688d8df2014-11-02 23:06:42 -05002918 s->psk_identity_hint = BUF_strdup(identity_hint);
2919 if (s->psk_identity_hint == NULL)
2920 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002921 }
Adam Langley95c29f32014-06-20 12:00:00 -07002922 return 1;
2923 }
2924
2925const char *SSL_get_psk_identity_hint(const SSL *s)
2926 {
Adam Langley0289c732014-06-20 12:00:00 -07002927 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002928 return NULL;
David Benjamin688d8df2014-11-02 23:06:42 -05002929 return s->psk_identity_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07002930 }
2931
2932const char *SSL_get_psk_identity(const SSL *s)
2933 {
2934 if (s == NULL || s->session == NULL)
2935 return NULL;
2936 return(s->session->psk_identity);
2937 }
2938
2939void SSL_set_psk_client_callback(SSL *s,
2940 unsigned int (*cb)(SSL *ssl, const char *hint,
2941 char *identity, unsigned int max_identity_len, unsigned char *psk,
2942 unsigned int max_psk_len))
2943 {
2944 s->psk_client_callback = cb;
2945 }
2946
2947void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
2948 unsigned int (*cb)(SSL *ssl, const char *hint,
2949 char *identity, unsigned int max_identity_len, unsigned char *psk,
2950 unsigned int max_psk_len))
2951 {
2952 ctx->psk_client_callback = cb;
2953 }
2954
2955void SSL_set_psk_server_callback(SSL *s,
2956 unsigned int (*cb)(SSL *ssl, const char *identity,
2957 unsigned char *psk, unsigned int max_psk_len))
2958 {
2959 s->psk_server_callback = cb;
2960 }
2961
2962void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
2963 unsigned int (*cb)(SSL *ssl, const char *identity,
2964 unsigned char *psk, unsigned int max_psk_len))
2965 {
2966 ctx->psk_server_callback = cb;
2967 }
Adam Langley95c29f32014-06-20 12:00:00 -07002968
2969void 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))
2970 {
2971 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2972 }
2973void 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))
2974 {
2975 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2976 }
2977
David Benjamin859ec3c2014-09-02 16:29:36 -04002978void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio)
2979 {
2980 if (ctx->keylog_bio != NULL)
2981 BIO_free(ctx->keylog_bio);
2982 ctx->keylog_bio = keylog_bio;
2983 }
2984
2985static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len)
2986 {
2987 static const char hextable[] = "0123456789abcdef";
2988 uint8_t *out;
2989 size_t i;
2990
2991 if (!CBB_add_space(cbb, &out, in_len * 2))
2992 {
2993 return 0;
2994 }
2995
2996 for (i = 0; i < in_len; i++)
2997 {
2998 *(out++) = (uint8_t)hextable[in[i] >> 4];
2999 *(out++) = (uint8_t)hextable[in[i] & 0xf];
3000 }
3001 return 1;
3002 }
3003
3004int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
3005 const uint8_t *encrypted_premaster, size_t encrypted_premaster_len,
3006 const uint8_t *premaster, size_t premaster_len)
3007 {
3008 BIO *bio = ctx->keylog_bio;
3009 CBB cbb;
3010 uint8_t *out;
3011 size_t out_len;
3012 int ret;
3013
3014 if (bio == NULL)
3015 {
3016 return 1;
3017 }
3018
3019 if (encrypted_premaster_len < 8)
3020 {
3021 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange, ERR_R_INTERNAL_ERROR);
3022 return 0;
3023 }
3024
3025 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len*2 + 1))
3026 {
3027 return 0;
3028 }
3029 if (!CBB_add_bytes(&cbb, (const uint8_t*)"RSA ", 4) ||
3030 /* Only the first 8 bytes of the encrypted premaster secret are
3031 * logged. */
3032 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
3033 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3034 !cbb_add_hex(&cbb, premaster, premaster_len) ||
3035 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3036 !CBB_finish(&cbb, &out, &out_len))
3037 {
3038 CBB_cleanup(&cbb);
3039 return 0;
3040 }
3041
3042 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3043 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3044 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3045
3046 OPENSSL_free(out);
3047 return ret;
3048 }
3049
3050int ssl_ctx_log_master_secret(SSL_CTX *ctx,
3051 const uint8_t *client_random, size_t client_random_len,
3052 const uint8_t *master, size_t master_len)
3053 {
3054 BIO *bio = ctx->keylog_bio;
3055 CBB cbb;
3056 uint8_t *out;
3057 size_t out_len;
3058 int ret;
3059
3060 if (bio == NULL)
3061 {
3062 return 1;
3063 }
3064
3065 if (client_random_len != 32)
3066 {
3067 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
3068 return 0;
3069 }
3070
3071 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len*2 + 1))
3072 {
3073 return 0;
3074 }
3075 if (!CBB_add_bytes(&cbb, (const uint8_t*)"CLIENT_RANDOM ", 14) ||
3076 !cbb_add_hex(&cbb, client_random, 32) ||
3077 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3078 !cbb_add_hex(&cbb, master, master_len) ||
3079 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3080 !CBB_finish(&cbb, &out, &out_len))
3081 {
3082 CBB_cleanup(&cbb);
3083 return 0;
3084 }
3085
3086 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3087 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3088 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3089
3090 OPENSSL_free(out);
3091 return ret;
3092 }
3093
Adam Langleyadb739e2014-06-20 12:00:00 -07003094int SSL_cutthrough_complete(const SSL *s)
3095 {
3096 return (!s->server && /* cutthrough only applies to clients */
3097 !s->hit && /* full-handshake */
3098 s->version >= SSL3_VERSION &&
3099 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3100 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3101 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3102 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3103 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
David Benjamin7e3305e2014-07-28 14:52:32 -04003104 s->state == SSL3_ST_CR_CHANGE ||
Adam Langleyadb739e2014-06-20 12:00:00 -07003105 s->state == SSL3_ST_CR_FINISHED_A));
3106 }
3107
Adam Langley95f22882014-06-20 12:00:00 -07003108void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3109 size_t* ssl_session_size)
3110{
3111 *ssl_size = sizeof(SSL);
3112 *ssl_ctx_size = sizeof(SSL_CTX);
3113 *ssl_session_size = sizeof(SSL_SESSION);
3114}
3115
Adam Langleyadb739e2014-06-20 12:00:00 -07003116int ssl3_can_cutthrough(const SSL *s)
3117 {
3118 const SSL_CIPHER *c;
3119
3120 /* require a strong enough cipher */
3121 if (SSL_get_cipher_bits(s, NULL) < 128)
3122 return 0;
3123
3124 /* require ALPN or NPN extension */
David Benjamin457112e2014-08-27 14:35:09 -04003125 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen)
Adam Langleyadb739e2014-06-20 12:00:00 -07003126 {
3127 return 0;
3128 }
3129
3130 /* require a forward-secret cipher */
3131 c = SSL_get_current_cipher(s);
3132 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3133 c->algorithm_mkey != SSL_kEECDH))
3134 {
3135 return 0;
3136 }
3137
3138 return 1;
3139 }
3140
Adam Langley29707792014-06-20 12:00:00 -07003141/* ssl_get_max_version returns the maximum SSL/TLS version number supported by
3142 * |s|, or zero if all versions are disabled. */
3143int ssl_get_max_version(const SSL *s)
3144 {
3145 /* Only one version supported for DTLS. */
3146 if (s->version == DTLS1_VERSION)
3147 return DTLS1_VERSION;
3148
3149 if (!(s->options & SSL_OP_NO_TLSv1_2))
3150 return TLS1_2_VERSION;
3151 if (!(s->options & SSL_OP_NO_TLSv1_1))
3152 return TLS1_1_VERSION;
3153 if (!(s->options & SSL_OP_NO_TLSv1))
3154 return TLS1_VERSION;
3155 if (!(s->options & SSL_OP_NO_SSLv3))
3156 return SSL3_VERSION;
Adam Langley29707792014-06-20 12:00:00 -07003157 return 0;
3158 }
3159
Adam Langley95c29f32014-06-20 12:00:00 -07003160/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3161 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3162 * any. If EVP_MD pointer is passed, initializes ctx with this md
3163 * Returns newly allocated ctx;
3164 */
3165
3166EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3167{
3168 ssl_clear_hash_ctx(hash);
3169 *hash = EVP_MD_CTX_create();
3170 if (md) EVP_DigestInit_ex(*hash,md,NULL);
3171 return *hash;
3172}
3173void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3174{
3175
3176 if (*hash) EVP_MD_CTX_destroy(*hash);
3177 *hash=NULL;
3178}
3179
3180void SSL_set_debug(SSL *s, int debug)
3181 {
3182 s->debug = debug;
3183 }
3184
3185int SSL_cache_hit(SSL *s)
3186 {
3187 return s->hit;
3188 }
3189
3190int SSL_is_server(SSL *s)
3191 {
3192 return s->server;
3193 }