blob: 2059032fb2ebd8bbf87565e6f7564b3518bd936d [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
2245EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd)
2246 {
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 }
2269 if (pmd)
2270 *pmd = c->pkeys[idx].digest;
2271 return c->pkeys[idx].privatekey;
2272 }
2273
Adam Langley95c29f32014-06-20 12:00:00 -07002274void ssl_update_cache(SSL *s,int mode)
2275 {
2276 int i;
2277
2278 /* If the session_id_length is 0, we are not supposed to cache it,
2279 * and it would be rather hard to do anyway :-) */
2280 if (s->session->session_id_length == 0) return;
2281
David Benjamin041b58a2014-08-27 15:25:21 -04002282 i=s->initial_ctx->session_cache_mode;
Adam Langley95c29f32014-06-20 12:00:00 -07002283 if ((i & mode) && (!s->hit)
2284 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
David Benjamin041b58a2014-08-27 15:25:21 -04002285 || SSL_CTX_add_session(s->initial_ctx,s->session))
2286 && (s->initial_ctx->new_session_cb != NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07002287 {
2288 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
David Benjamin041b58a2014-08-27 15:25:21 -04002289 if (!s->initial_ctx->new_session_cb(s,s->session))
Adam Langley95c29f32014-06-20 12:00:00 -07002290 SSL_SESSION_free(s->session);
2291 }
2292
2293 /* auto flush every 255 connections */
2294 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2295 ((i & mode) == mode))
2296 {
2297 if ( (((mode & SSL_SESS_CACHE_CLIENT)
David Benjamin041b58a2014-08-27 15:25:21 -04002298 ?s->initial_ctx->stats.sess_connect_good
2299 :s->initial_ctx->stats.sess_accept_good) & 0xff) == 0xff)
Adam Langley95c29f32014-06-20 12:00:00 -07002300 {
David Benjamin041b58a2014-08-27 15:25:21 -04002301 SSL_CTX_flush_sessions(s->initial_ctx,(unsigned long)time(NULL));
Adam Langley95c29f32014-06-20 12:00:00 -07002302 }
2303 }
2304 }
2305
2306const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
2307 {
2308 return ctx->method;
2309 }
2310
2311const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2312 {
2313 return(s->method);
2314 }
2315
2316int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2317 {
2318 int conn= -1;
2319 int ret=1;
2320
2321 if (s->method != meth)
2322 {
2323 if (s->handshake_func != NULL)
2324 conn=(s->handshake_func == s->method->ssl_connect);
2325
2326 if (s->method->version == meth->version)
2327 s->method=meth;
2328 else
2329 {
2330 s->method->ssl_free(s);
2331 s->method=meth;
2332 ret=s->method->ssl_new(s);
2333 }
2334
2335 if (conn == 1)
2336 s->handshake_func=meth->ssl_connect;
2337 else if (conn == 0)
2338 s->handshake_func=meth->ssl_accept;
2339 }
2340 return(ret);
2341 }
2342
2343int SSL_get_error(const SSL *s,int i)
2344 {
2345 int reason;
2346 unsigned long l;
2347 BIO *bio;
2348
2349 if (i > 0) return(SSL_ERROR_NONE);
2350
2351 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2352 * etc, where we do encode the error */
2353 if ((l=ERR_peek_error()) != 0)
2354 {
2355 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2356 return(SSL_ERROR_SYSCALL);
2357 else
2358 return(SSL_ERROR_SSL);
2359 }
2360
Adam Langleyb2ce0582014-06-20 12:00:00 -07002361 if ((i < 0) && SSL_want_session(s))
2362 return(SSL_ERROR_PENDING_SESSION);
2363
Adam Langleydc9b1412014-06-20 12:00:00 -07002364 if ((i < 0) && SSL_want_certificate(s))
2365 return(SSL_ERROR_PENDING_CERTIFICATE);
2366
Adam Langley95c29f32014-06-20 12:00:00 -07002367 if ((i < 0) && SSL_want_read(s))
2368 {
2369 bio=SSL_get_rbio(s);
2370 if (BIO_should_read(bio))
2371 return(SSL_ERROR_WANT_READ);
2372 else if (BIO_should_write(bio))
2373 /* This one doesn't make too much sense ... We never try
2374 * to write to the rbio, and an application program where
2375 * rbio and wbio are separate couldn't even know what it
2376 * should wait for.
2377 * However if we ever set s->rwstate incorrectly
2378 * (so that we have SSL_want_read(s) instead of
2379 * SSL_want_write(s)) and rbio and wbio *are* the same,
2380 * this test works around that bug; so it might be safer
2381 * to keep it. */
2382 return(SSL_ERROR_WANT_WRITE);
2383 else if (BIO_should_io_special(bio))
2384 {
2385 reason=BIO_get_retry_reason(bio);
2386 if (reason == BIO_RR_CONNECT)
2387 return(SSL_ERROR_WANT_CONNECT);
2388 else if (reason == BIO_RR_ACCEPT)
2389 return(SSL_ERROR_WANT_ACCEPT);
2390 else
2391 return(SSL_ERROR_SYSCALL); /* unknown */
2392 }
2393 }
2394
2395 if ((i < 0) && SSL_want_write(s))
2396 {
2397 bio=SSL_get_wbio(s);
2398 if (BIO_should_write(bio))
2399 return(SSL_ERROR_WANT_WRITE);
2400 else if (BIO_should_read(bio))
2401 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2402 return(SSL_ERROR_WANT_READ);
2403 else if (BIO_should_io_special(bio))
2404 {
2405 reason=BIO_get_retry_reason(bio);
2406 if (reason == BIO_RR_CONNECT)
2407 return(SSL_ERROR_WANT_CONNECT);
2408 else if (reason == BIO_RR_ACCEPT)
2409 return(SSL_ERROR_WANT_ACCEPT);
2410 else
2411 return(SSL_ERROR_SYSCALL);
2412 }
2413 }
2414 if ((i < 0) && SSL_want_x509_lookup(s))
2415 {
2416 return(SSL_ERROR_WANT_X509_LOOKUP);
2417 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002418 if ((i < 0) && SSL_want_channel_id_lookup(s))
2419 {
2420 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2421 }
Adam Langley95c29f32014-06-20 12:00:00 -07002422
2423 if (i == 0)
2424 {
David Benjamin5e4f6e92014-11-17 03:23:24 -05002425 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2426 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
Adam Langley95c29f32014-06-20 12:00:00 -07002427 return(SSL_ERROR_ZERO_RETURN);
Adam Langley95c29f32014-06-20 12:00:00 -07002428 }
2429 return(SSL_ERROR_SYSCALL);
2430 }
2431
2432int SSL_do_handshake(SSL *s)
2433 {
2434 int ret=1;
2435
2436 if (s->handshake_func == NULL)
2437 {
2438 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2439 return(-1);
2440 }
2441
2442 s->method->ssl_renegotiate_check(s);
2443
2444 if (SSL_in_init(s) || SSL_in_before(s))
2445 {
2446 ret=s->handshake_func(s);
2447 }
2448 return(ret);
2449 }
2450
2451/* For the next 2 functions, SSL_clear() sets shutdown and so
2452 * one of these calls will reset it */
2453void SSL_set_accept_state(SSL *s)
2454 {
2455 s->server=1;
2456 s->shutdown=0;
2457 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2458 s->handshake_func=s->method->ssl_accept;
2459 /* clear the current cipher */
2460 ssl_clear_cipher_ctx(s);
2461 ssl_clear_hash_ctx(&s->read_hash);
2462 ssl_clear_hash_ctx(&s->write_hash);
2463 }
2464
2465void SSL_set_connect_state(SSL *s)
2466 {
2467 s->server=0;
2468 s->shutdown=0;
2469 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2470 s->handshake_func=s->method->ssl_connect;
2471 /* clear the current cipher */
2472 ssl_clear_cipher_ctx(s);
2473 ssl_clear_hash_ctx(&s->read_hash);
2474 ssl_clear_hash_ctx(&s->write_hash);
2475 }
2476
2477int ssl_undefined_function(SSL *s)
2478 {
2479 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2480 return(0);
2481 }
2482
2483int ssl_undefined_void_function(void)
2484 {
2485 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2486 return(0);
2487 }
2488
2489int ssl_undefined_const_function(const SSL *s)
2490 {
2491 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2492 return(0);
2493 }
2494
2495SSL_METHOD *ssl_bad_method(int ver)
2496 {
2497 OPENSSL_PUT_ERROR(SSL, ssl_bad_method, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2498 return(NULL);
2499 }
2500
Adam Langley0f4746e2014-08-13 12:26:32 -07002501static const char *ssl_get_version(int version)
Adam Langley95c29f32014-06-20 12:00:00 -07002502 {
Adam Langley0f4746e2014-08-13 12:26:32 -07002503 if (version == TLS1_2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002504 return("TLSv1.2");
Adam Langley0f4746e2014-08-13 12:26:32 -07002505 else if (version == TLS1_1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002506 return("TLSv1.1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002507 else if (version == TLS1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002508 return("TLSv1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002509 else if (version == SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002510 return("SSLv3");
Adam Langley95c29f32014-06-20 12:00:00 -07002511 else
2512 return("unknown");
2513 }
2514
Adam Langley0f4746e2014-08-13 12:26:32 -07002515const char *SSL_get_version(const SSL *s)
2516 {
2517 return ssl_get_version(s->version);
2518 }
2519
2520const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2521 {
2522 return ssl_get_version(sess->ssl_version);
2523 }
2524
Adam Langley95c29f32014-06-20 12:00:00 -07002525void ssl_clear_cipher_ctx(SSL *s)
2526 {
2527 if (s->enc_read_ctx != NULL)
2528 {
2529 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2530 OPENSSL_free(s->enc_read_ctx);
2531 s->enc_read_ctx=NULL;
2532 }
2533 if (s->enc_write_ctx != NULL)
2534 {
2535 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2536 OPENSSL_free(s->enc_write_ctx);
2537 s->enc_write_ctx=NULL;
2538 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002539 if (s->aead_read_ctx != NULL)
2540 {
2541 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2542 OPENSSL_free(s->aead_read_ctx);
2543 s->aead_read_ctx = NULL;
2544 }
2545 if (s->aead_write_ctx != NULL)
2546 {
2547 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2548 OPENSSL_free(s->aead_write_ctx);
2549 s->aead_write_ctx = NULL;
2550 }
Adam Langley95c29f32014-06-20 12:00:00 -07002551 }
2552
2553X509 *SSL_get_certificate(const SSL *s)
2554 {
2555 if (s->cert != NULL)
2556 return(s->cert->key->x509);
2557 else
2558 return(NULL);
2559 }
2560
2561EVP_PKEY *SSL_get_privatekey(const SSL *s)
2562 {
2563 if (s->cert != NULL)
2564 return(s->cert->key->privatekey);
2565 else
2566 return(NULL);
2567 }
2568
2569X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2570 {
2571 if (ctx->cert != NULL)
2572 return ctx->cert->key->x509;
2573 else
2574 return NULL;
2575 }
2576
2577EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2578 {
2579 if (ctx->cert != NULL)
2580 return ctx->cert->key->privatekey;
2581 else
2582 return NULL ;
2583 }
2584
2585const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2586 {
2587 if ((s->session != NULL) && (s->session->cipher != NULL))
2588 return(s->session->cipher);
2589 return(NULL);
2590 }
2591const void *SSL_get_current_compression(SSL *s)
2592 {
2593 return NULL;
2594 }
2595const void *SSL_get_current_expansion(SSL *s)
2596 {
2597 return NULL;
2598 }
2599
2600int ssl_init_wbio_buffer(SSL *s,int push)
2601 {
2602 BIO *bbio;
2603
2604 if (s->bbio == NULL)
2605 {
2606 bbio=BIO_new(BIO_f_buffer());
2607 if (bbio == NULL) return(0);
2608 s->bbio=bbio;
2609 }
2610 else
2611 {
2612 bbio=s->bbio;
2613 if (s->bbio == s->wbio)
2614 s->wbio=BIO_pop(s->wbio);
2615 }
2616 (void)BIO_reset(bbio);
2617/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2618 if (!BIO_set_read_buffer_size(bbio,1))
2619 {
2620 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2621 return(0);
2622 }
2623 if (push)
2624 {
2625 if (s->wbio != bbio)
2626 s->wbio=BIO_push(bbio,s->wbio);
2627 }
2628 else
2629 {
2630 if (s->wbio == bbio)
2631 s->wbio=BIO_pop(bbio);
2632 }
2633 return(1);
2634 }
2635
2636void ssl_free_wbio_buffer(SSL *s)
2637 {
2638 if (s->bbio == NULL) return;
2639
2640 if (s->bbio == s->wbio)
2641 {
2642 /* remove buffering */
2643 s->wbio=BIO_pop(s->wbio);
2644#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2645 assert(s->wbio != NULL);
2646#endif
2647 }
2648 BIO_free(s->bbio);
2649 s->bbio=NULL;
2650 }
2651
2652void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2653 {
2654 ctx->quiet_shutdown=mode;
2655 }
2656
2657int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2658 {
2659 return(ctx->quiet_shutdown);
2660 }
2661
2662void SSL_set_quiet_shutdown(SSL *s,int mode)
2663 {
2664 s->quiet_shutdown=mode;
2665 }
2666
2667int SSL_get_quiet_shutdown(const SSL *s)
2668 {
2669 return(s->quiet_shutdown);
2670 }
2671
2672void SSL_set_shutdown(SSL *s,int mode)
2673 {
2674 s->shutdown=mode;
2675 }
2676
2677int SSL_get_shutdown(const SSL *s)
2678 {
2679 return(s->shutdown);
2680 }
2681
2682int SSL_version(const SSL *s)
2683 {
2684 return(s->version);
2685 }
2686
2687SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2688 {
2689 return(ssl->ctx);
2690 }
2691
2692SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2693 {
2694 if (ssl->ctx == ctx)
2695 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002696 if (ctx == NULL)
2697 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002698 if (ssl->cert != NULL)
2699 ssl_cert_free(ssl->cert);
2700 ssl->cert = ssl_cert_dup(ctx->cert);
2701 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2702 if (ssl->ctx != NULL)
2703 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2704 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07002705
2706 ssl->sid_ctx_length = ctx->sid_ctx_length;
2707 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2708 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2709
Adam Langley95c29f32014-06-20 12:00:00 -07002710 return(ssl->ctx);
2711 }
2712
2713#ifndef OPENSSL_NO_STDIO
2714int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2715 {
2716 return(X509_STORE_set_default_paths(ctx->cert_store));
2717 }
2718
2719int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2720 const char *CApath)
2721 {
2722 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2723 }
2724#endif
2725
2726void SSL_set_info_callback(SSL *ssl,
2727 void (*cb)(const SSL *ssl,int type,int val))
2728 {
2729 ssl->info_callback=cb;
2730 }
2731
2732/* One compiler (Diab DCC) doesn't like argument names in returned
2733 function pointer. */
2734void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2735 {
2736 return ssl->info_callback;
2737 }
2738
2739int SSL_state(const SSL *ssl)
2740 {
2741 return(ssl->state);
2742 }
2743
2744void SSL_set_state(SSL *ssl, int state)
2745 {
2746 ssl->state = state;
2747 }
2748
2749void SSL_set_verify_result(SSL *ssl,long arg)
2750 {
2751 ssl->verify_result=arg;
2752 }
2753
2754long SSL_get_verify_result(const SSL *ssl)
2755 {
2756 return(ssl->verify_result);
2757 }
2758
2759int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2760 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2761 {
2762 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2763 new_func, dup_func, free_func);
2764 }
2765
2766int SSL_set_ex_data(SSL *s,int idx,void *arg)
2767 {
2768 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2769 }
2770
2771void *SSL_get_ex_data(const SSL *s,int idx)
2772 {
2773 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2774 }
2775
2776int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2777 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2778 {
2779 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2780 new_func, dup_func, free_func);
2781 }
2782
2783int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2784 {
2785 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2786 }
2787
2788void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2789 {
2790 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2791 }
2792
2793int ssl_ok(SSL *s)
2794 {
2795 return(1);
2796 }
2797
2798X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2799 {
2800 return(ctx->cert_store);
2801 }
2802
2803void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2804 {
2805 if (ctx->cert_store != NULL)
2806 X509_STORE_free(ctx->cert_store);
2807 ctx->cert_store=store;
2808 }
2809
2810int SSL_want(const SSL *s)
2811 {
2812 return(s->rwstate);
2813 }
2814
2815/*!
2816 * \brief Set the callback for generating temporary RSA keys.
2817 * \param ctx the SSL context.
2818 * \param cb the callback
2819 */
2820
Adam Langley95c29f32014-06-20 12:00:00 -07002821void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2822 int is_export,
2823 int keylength))
2824 {
2825 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2826 }
2827
2828void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2829 int is_export,
2830 int keylength))
2831 {
2832 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2833 }
Adam Langley95c29f32014-06-20 12:00:00 -07002834
2835#ifdef DOXYGEN
2836/*!
2837 * \brief The RSA temporary key callback function.
2838 * \param ssl the SSL session.
2839 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2840 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2841 * of the required key in bits.
2842 * \return the temporary RSA key.
2843 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2844 */
2845
2846RSA *cb(SSL *ssl,int is_export,int keylength)
2847 {}
2848#endif
2849
2850/*!
2851 * \brief Set the callback for generating temporary DH keys.
2852 * \param ctx the SSL context.
2853 * \param dh the callback
2854 */
2855
Adam Langley95c29f32014-06-20 12:00:00 -07002856void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2857 int keylength))
2858 {
2859 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2860 }
2861
2862void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2863 int keylength))
2864 {
2865 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2866 }
Adam Langley95c29f32014-06-20 12:00:00 -07002867
Adam Langley95c29f32014-06-20 12:00:00 -07002868void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2869 int keylength))
2870 {
2871 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2872 }
2873
2874void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2875 int keylength))
2876 {
2877 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2878 }
Adam Langley95c29f32014-06-20 12:00:00 -07002879
Adam Langley95c29f32014-06-20 12:00:00 -07002880int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2881 {
2882 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2883 {
2884 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2885 return 0;
2886 }
2887 if (ctx->psk_identity_hint != NULL)
2888 OPENSSL_free(ctx->psk_identity_hint);
2889 if (identity_hint != NULL)
2890 {
2891 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2892 if (ctx->psk_identity_hint == NULL)
2893 return 0;
2894 }
2895 else
2896 ctx->psk_identity_hint = NULL;
2897 return 1;
2898 }
2899
2900int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
2901 {
2902 if (s == NULL)
2903 return 0;
2904
Adam Langley95c29f32014-06-20 12:00:00 -07002905 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2906 {
2907 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2908 return 0;
2909 }
Adam Langley0289c732014-06-20 12:00:00 -07002910
David Benjamin688d8df2014-11-02 23:06:42 -05002911 /* Clear currently configured hint, if any. */
Adam Langley0289c732014-06-20 12:00:00 -07002912 if (s->psk_identity_hint != NULL)
2913 {
2914 OPENSSL_free(s->psk_identity_hint);
2915 s->psk_identity_hint = NULL;
2916 }
Adam Langley0289c732014-06-20 12:00:00 -07002917
Adam Langley95c29f32014-06-20 12:00:00 -07002918 if (identity_hint != NULL)
2919 {
David Benjamin688d8df2014-11-02 23:06:42 -05002920 s->psk_identity_hint = BUF_strdup(identity_hint);
2921 if (s->psk_identity_hint == NULL)
2922 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002923 }
Adam Langley95c29f32014-06-20 12:00:00 -07002924 return 1;
2925 }
2926
2927const char *SSL_get_psk_identity_hint(const SSL *s)
2928 {
Adam Langley0289c732014-06-20 12:00:00 -07002929 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002930 return NULL;
David Benjamin688d8df2014-11-02 23:06:42 -05002931 return s->psk_identity_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07002932 }
2933
2934const char *SSL_get_psk_identity(const SSL *s)
2935 {
2936 if (s == NULL || s->session == NULL)
2937 return NULL;
2938 return(s->session->psk_identity);
2939 }
2940
2941void SSL_set_psk_client_callback(SSL *s,
2942 unsigned int (*cb)(SSL *ssl, const char *hint,
2943 char *identity, unsigned int max_identity_len, unsigned char *psk,
2944 unsigned int max_psk_len))
2945 {
2946 s->psk_client_callback = cb;
2947 }
2948
2949void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
2950 unsigned int (*cb)(SSL *ssl, const char *hint,
2951 char *identity, unsigned int max_identity_len, unsigned char *psk,
2952 unsigned int max_psk_len))
2953 {
2954 ctx->psk_client_callback = cb;
2955 }
2956
2957void SSL_set_psk_server_callback(SSL *s,
2958 unsigned int (*cb)(SSL *ssl, const char *identity,
2959 unsigned char *psk, unsigned int max_psk_len))
2960 {
2961 s->psk_server_callback = cb;
2962 }
2963
2964void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
2965 unsigned int (*cb)(SSL *ssl, const char *identity,
2966 unsigned char *psk, unsigned int max_psk_len))
2967 {
2968 ctx->psk_server_callback = cb;
2969 }
Adam Langley95c29f32014-06-20 12:00:00 -07002970
2971void 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))
2972 {
2973 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2974 }
2975void 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))
2976 {
2977 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2978 }
2979
David Benjamin859ec3c2014-09-02 16:29:36 -04002980void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio)
2981 {
2982 if (ctx->keylog_bio != NULL)
2983 BIO_free(ctx->keylog_bio);
2984 ctx->keylog_bio = keylog_bio;
2985 }
2986
2987static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len)
2988 {
2989 static const char hextable[] = "0123456789abcdef";
2990 uint8_t *out;
2991 size_t i;
2992
2993 if (!CBB_add_space(cbb, &out, in_len * 2))
2994 {
2995 return 0;
2996 }
2997
2998 for (i = 0; i < in_len; i++)
2999 {
3000 *(out++) = (uint8_t)hextable[in[i] >> 4];
3001 *(out++) = (uint8_t)hextable[in[i] & 0xf];
3002 }
3003 return 1;
3004 }
3005
3006int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
3007 const uint8_t *encrypted_premaster, size_t encrypted_premaster_len,
3008 const uint8_t *premaster, size_t premaster_len)
3009 {
3010 BIO *bio = ctx->keylog_bio;
3011 CBB cbb;
3012 uint8_t *out;
3013 size_t out_len;
3014 int ret;
3015
3016 if (bio == NULL)
3017 {
3018 return 1;
3019 }
3020
3021 if (encrypted_premaster_len < 8)
3022 {
3023 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange, ERR_R_INTERNAL_ERROR);
3024 return 0;
3025 }
3026
3027 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len*2 + 1))
3028 {
3029 return 0;
3030 }
3031 if (!CBB_add_bytes(&cbb, (const uint8_t*)"RSA ", 4) ||
3032 /* Only the first 8 bytes of the encrypted premaster secret are
3033 * logged. */
3034 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
3035 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3036 !cbb_add_hex(&cbb, premaster, premaster_len) ||
3037 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3038 !CBB_finish(&cbb, &out, &out_len))
3039 {
3040 CBB_cleanup(&cbb);
3041 return 0;
3042 }
3043
3044 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3045 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3046 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3047
3048 OPENSSL_free(out);
3049 return ret;
3050 }
3051
3052int ssl_ctx_log_master_secret(SSL_CTX *ctx,
3053 const uint8_t *client_random, size_t client_random_len,
3054 const uint8_t *master, size_t master_len)
3055 {
3056 BIO *bio = ctx->keylog_bio;
3057 CBB cbb;
3058 uint8_t *out;
3059 size_t out_len;
3060 int ret;
3061
3062 if (bio == NULL)
3063 {
3064 return 1;
3065 }
3066
3067 if (client_random_len != 32)
3068 {
3069 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
3070 return 0;
3071 }
3072
3073 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len*2 + 1))
3074 {
3075 return 0;
3076 }
3077 if (!CBB_add_bytes(&cbb, (const uint8_t*)"CLIENT_RANDOM ", 14) ||
3078 !cbb_add_hex(&cbb, client_random, 32) ||
3079 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3080 !cbb_add_hex(&cbb, master, master_len) ||
3081 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3082 !CBB_finish(&cbb, &out, &out_len))
3083 {
3084 CBB_cleanup(&cbb);
3085 return 0;
3086 }
3087
3088 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3089 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3090 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3091
3092 OPENSSL_free(out);
3093 return ret;
3094 }
3095
Adam Langleyadb739e2014-06-20 12:00:00 -07003096int SSL_cutthrough_complete(const SSL *s)
3097 {
3098 return (!s->server && /* cutthrough only applies to clients */
3099 !s->hit && /* full-handshake */
3100 s->version >= SSL3_VERSION &&
3101 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3102 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3103 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3104 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3105 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
David Benjamin7e3305e2014-07-28 14:52:32 -04003106 s->state == SSL3_ST_CR_CHANGE ||
Adam Langleyadb739e2014-06-20 12:00:00 -07003107 s->state == SSL3_ST_CR_FINISHED_A));
3108 }
3109
Adam Langley95f22882014-06-20 12:00:00 -07003110void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3111 size_t* ssl_session_size)
3112{
3113 *ssl_size = sizeof(SSL);
3114 *ssl_ctx_size = sizeof(SSL_CTX);
3115 *ssl_session_size = sizeof(SSL_SESSION);
3116}
3117
Adam Langleyadb739e2014-06-20 12:00:00 -07003118int ssl3_can_cutthrough(const SSL *s)
3119 {
3120 const SSL_CIPHER *c;
3121
3122 /* require a strong enough cipher */
3123 if (SSL_get_cipher_bits(s, NULL) < 128)
3124 return 0;
3125
3126 /* require ALPN or NPN extension */
David Benjamin457112e2014-08-27 14:35:09 -04003127 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen)
Adam Langleyadb739e2014-06-20 12:00:00 -07003128 {
3129 return 0;
3130 }
3131
3132 /* require a forward-secret cipher */
3133 c = SSL_get_current_cipher(s);
3134 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3135 c->algorithm_mkey != SSL_kEECDH))
3136 {
3137 return 0;
3138 }
3139
3140 return 1;
3141 }
3142
Adam Langley29707792014-06-20 12:00:00 -07003143/* ssl_get_max_version returns the maximum SSL/TLS version number supported by
3144 * |s|, or zero if all versions are disabled. */
3145int ssl_get_max_version(const SSL *s)
3146 {
3147 /* Only one version supported for DTLS. */
3148 if (s->version == DTLS1_VERSION)
3149 return DTLS1_VERSION;
3150
3151 if (!(s->options & SSL_OP_NO_TLSv1_2))
3152 return TLS1_2_VERSION;
3153 if (!(s->options & SSL_OP_NO_TLSv1_1))
3154 return TLS1_1_VERSION;
3155 if (!(s->options & SSL_OP_NO_TLSv1))
3156 return TLS1_VERSION;
3157 if (!(s->options & SSL_OP_NO_SSLv3))
3158 return SSL3_VERSION;
Adam Langley29707792014-06-20 12:00:00 -07003159 return 0;
3160 }
3161
Adam Langley95c29f32014-06-20 12:00:00 -07003162/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3163 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3164 * any. If EVP_MD pointer is passed, initializes ctx with this md
3165 * Returns newly allocated ctx;
3166 */
3167
3168EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3169{
3170 ssl_clear_hash_ctx(hash);
3171 *hash = EVP_MD_CTX_create();
3172 if (md) EVP_DigestInit_ex(*hash,md,NULL);
3173 return *hash;
3174}
3175void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3176{
3177
3178 if (*hash) EVP_MD_CTX_destroy(*hash);
3179 *hash=NULL;
3180}
3181
3182void SSL_set_debug(SSL *s, int debug)
3183 {
3184 s->debug = debug;
3185 }
3186
3187int SSL_cache_hit(SSL *s)
3188 {
3189 return s->hit;
3190 }
3191
3192int SSL_is_server(SSL *s)
3193 {
3194 return s->server;
3195 }