blob: 6b48a0c15c41015947684f9c502183534f46478e [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
Adam Langley95c29f32014-06-20 12:00:00 -0700685 OPENSSL_free(s);
686 }
687
688void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
689 {
690 /* If the output buffering BIO is still in place, remove it
691 */
692 if (s->bbio != NULL)
693 {
694 if (s->wbio == s->bbio)
695 {
696 s->wbio=s->wbio->next_bio;
697 s->bbio->next_bio=NULL;
698 }
699 }
700 if ((s->rbio != NULL) && (s->rbio != rbio))
701 BIO_free_all(s->rbio);
702 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
703 BIO_free_all(s->wbio);
704 s->rbio=rbio;
705 s->wbio=wbio;
706 }
707
708BIO *SSL_get_rbio(const SSL *s)
709 { return(s->rbio); }
710
711BIO *SSL_get_wbio(const SSL *s)
712 { return(s->wbio); }
713
714int SSL_get_fd(const SSL *s)
715 {
716 return(SSL_get_rfd(s));
717 }
718
719int SSL_get_rfd(const SSL *s)
720 {
721 int ret= -1;
722 BIO *b,*r;
723
724 b=SSL_get_rbio(s);
725 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
726 if (r != NULL)
727 BIO_get_fd(r,&ret);
728 return(ret);
729 }
730
731int SSL_get_wfd(const SSL *s)
732 {
733 int ret= -1;
734 BIO *b,*r;
735
736 b=SSL_get_wbio(s);
737 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
738 if (r != NULL)
739 BIO_get_fd(r,&ret);
740 return(ret);
741 }
742
743#ifndef OPENSSL_NO_SOCK
744int SSL_set_fd(SSL *s,int fd)
745 {
746 int ret=0;
747 BIO *bio=NULL;
748
749 bio=BIO_new(BIO_s_fd());
750
751 if (bio == NULL)
752 {
753 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
754 goto err;
755 }
756 BIO_set_fd(bio,fd,BIO_NOCLOSE);
757 SSL_set_bio(s,bio,bio);
758 ret=1;
759err:
760 return(ret);
761 }
762
763int SSL_set_wfd(SSL *s,int fd)
764 {
765 int ret=0;
766 BIO *bio=NULL;
767
768 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_FD)
769 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
770 {
771 bio=BIO_new(BIO_s_fd());
772
773 if (bio == NULL)
774 {
775 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
776 goto err;
777 }
778 BIO_set_fd(bio,fd,BIO_NOCLOSE);
779 SSL_set_bio(s,SSL_get_rbio(s),bio);
780 }
781 else
782 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
783 ret=1;
784err:
785 return(ret);
786 }
787
788int SSL_set_rfd(SSL *s,int fd)
789 {
790 int ret=0;
791 BIO *bio=NULL;
792
793 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_FD)
794 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
795 {
796 bio=BIO_new(BIO_s_fd());
797
798 if (bio == NULL)
799 {
800 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
801 goto err;
802 }
803 BIO_set_fd(bio,fd,BIO_NOCLOSE);
804 SSL_set_bio(s,bio,SSL_get_wbio(s));
805 }
806 else
807 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
808 ret=1;
809err:
810 return(ret);
811 }
812#endif
813
814
815/* return length of latest Finished message we sent, copy to 'buf' */
816size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
817 {
818 size_t ret = 0;
819
820 if (s->s3 != NULL)
821 {
822 ret = s->s3->tmp.finish_md_len;
823 if (count > ret)
824 count = ret;
825 memcpy(buf, s->s3->tmp.finish_md, count);
826 }
827 return ret;
828 }
829
830/* return length of latest Finished message we expected, copy to 'buf' */
831size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
832 {
833 size_t ret = 0;
834
835 if (s->s3 != NULL)
836 {
837 ret = s->s3->tmp.peer_finish_md_len;
838 if (count > ret)
839 count = ret;
840 memcpy(buf, s->s3->tmp.peer_finish_md, count);
841 }
842 return ret;
843 }
844
845
846int SSL_get_verify_mode(const SSL *s)
847 {
848 return(s->verify_mode);
849 }
850
851int SSL_get_verify_depth(const SSL *s)
852 {
853 return X509_VERIFY_PARAM_get_depth(s->param);
854 }
855
856int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
857 {
858 return(s->verify_callback);
859 }
860
861int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
862 {
863 return(ctx->verify_mode);
864 }
865
866int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
867 {
868 return X509_VERIFY_PARAM_get_depth(ctx->param);
869 }
870
871int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
872 {
873 return(ctx->default_verify_callback);
874 }
875
876void SSL_set_verify(SSL *s,int mode,
877 int (*callback)(int ok,X509_STORE_CTX *ctx))
878 {
879 s->verify_mode=mode;
880 if (callback != NULL)
881 s->verify_callback=callback;
882 }
883
884void SSL_set_verify_depth(SSL *s,int depth)
885 {
886 X509_VERIFY_PARAM_set_depth(s->param, depth);
887 }
888
889void SSL_set_read_ahead(SSL *s,int yes)
890 {
891 s->read_ahead=yes;
892 }
893
894int SSL_get_read_ahead(const SSL *s)
895 {
896 return(s->read_ahead);
897 }
898
899int SSL_pending(const SSL *s)
900 {
901 /* SSL_pending cannot work properly if read-ahead is enabled
902 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
903 * and it is impossible to fix since SSL_pending cannot report
904 * errors that may be observed while scanning the new data.
905 * (Note that SSL_pending() is often used as a boolean value,
906 * so we'd better not return -1.)
907 */
908 return(s->method->ssl_pending(s));
909 }
910
911X509 *SSL_get_peer_certificate(const SSL *s)
912 {
913 X509 *r;
914
915 if ((s == NULL) || (s->session == NULL))
916 r=NULL;
917 else
918 r=s->session->peer;
919
David Benjamin150c6172014-08-05 22:22:49 -0400920 if (r == NULL)
921 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700922
David Benjamin150c6172014-08-05 22:22:49 -0400923 return X509_up_ref(r);
Adam Langley95c29f32014-06-20 12:00:00 -0700924 }
925
926STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
927 {
928 STACK_OF(X509) *r;
929
930 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
931 r=NULL;
932 else
933 r=s->session->sess_cert->cert_chain;
934
935 /* If we are a client, cert_chain includes the peer's own
936 * certificate; if we are a server, it does not. */
937
938 return(r);
939 }
940
Adam Langley95c29f32014-06-20 12:00:00 -0700941/* Fix this so it checks all the valid key/cert options */
942int SSL_CTX_check_private_key(const SSL_CTX *ctx)
943 {
944 if ( (ctx == NULL) ||
945 (ctx->cert == NULL) ||
946 (ctx->cert->key->x509 == NULL))
947 {
948 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
949 return(0);
950 }
951 if (ctx->cert->key->privatekey == NULL)
952 {
953 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
954 return(0);
955 }
956 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
957 }
958
959/* Fix this function so that it takes an optional type parameter */
960int SSL_check_private_key(const SSL *ssl)
961 {
962 if (ssl == NULL)
963 {
964 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
965 return(0);
966 }
967 if (ssl->cert == NULL)
968 {
969 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
970 return 0;
971 }
972 if (ssl->cert->key->x509 == 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->privatekey == NULL)
978 {
979 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
980 return(0);
981 }
982 return(X509_check_private_key(ssl->cert->key->x509,
983 ssl->cert->key->privatekey));
984 }
985
986int SSL_accept(SSL *s)
987 {
988 if (s->handshake_func == 0)
989 /* Not properly initialized yet */
990 SSL_set_accept_state(s);
991
992 return(s->method->ssl_accept(s));
993 }
994
995int SSL_connect(SSL *s)
996 {
997 if (s->handshake_func == 0)
998 /* Not properly initialized yet */
999 SSL_set_connect_state(s);
1000
1001 return(s->method->ssl_connect(s));
1002 }
1003
1004long SSL_get_default_timeout(const SSL *s)
1005 {
David Benjaminf4501342014-08-14 17:24:37 -04001006 return SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001007 }
1008
1009int SSL_read(SSL *s,void *buf,int num)
1010 {
1011 if (s->handshake_func == 0)
1012 {
1013 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
1014 return -1;
1015 }
1016
1017 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1018 {
1019 s->rwstate=SSL_NOTHING;
1020 return(0);
1021 }
1022 return(s->method->ssl_read(s,buf,num));
1023 }
1024
1025int SSL_peek(SSL *s,void *buf,int num)
1026 {
1027 if (s->handshake_func == 0)
1028 {
1029 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
1030 return -1;
1031 }
1032
1033 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1034 {
1035 return(0);
1036 }
1037 return(s->method->ssl_peek(s,buf,num));
1038 }
1039
1040int SSL_write(SSL *s,const void *buf,int num)
1041 {
1042 if (s->handshake_func == 0)
1043 {
1044 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
1045 return -1;
1046 }
1047
1048 if (s->shutdown & SSL_SENT_SHUTDOWN)
1049 {
1050 s->rwstate=SSL_NOTHING;
1051 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
1052 return(-1);
1053 }
1054 return(s->method->ssl_write(s,buf,num));
1055 }
1056
1057int SSL_shutdown(SSL *s)
1058 {
1059 /* Note that this function behaves differently from what one might
1060 * expect. Return values are 0 for no success (yet),
1061 * 1 for success; but calling it once is usually not enough,
1062 * even if blocking I/O is used (see ssl3_shutdown).
1063 */
1064
1065 if (s->handshake_func == 0)
1066 {
1067 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1068 return -1;
1069 }
1070
Adam Langleyb2cb0ec2014-09-02 14:28:49 -07001071 if (!SSL_in_init(s))
Adam Langley95c29f32014-06-20 12:00:00 -07001072 return(s->method->ssl_shutdown(s));
1073 else
1074 return(1);
1075 }
1076
1077int SSL_renegotiate(SSL *s)
1078 {
1079 if (s->renegotiate == 0)
1080 s->renegotiate=1;
1081
1082 s->new_session=1;
1083
1084 return(s->method->ssl_renegotiate(s));
1085 }
1086
1087int SSL_renegotiate_abbreviated(SSL *s)
1088 {
1089 if (s->renegotiate == 0)
1090 s->renegotiate=1;
1091
1092 s->new_session=0;
1093
1094 return(s->method->ssl_renegotiate(s));
1095 }
1096
1097int SSL_renegotiate_pending(SSL *s)
1098 {
1099 /* becomes true when negotiation is requested;
1100 * false again once a handshake has finished */
1101 return (s->renegotiate != 0);
1102 }
1103
1104long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1105 {
1106 long l;
1107
1108 switch (cmd)
1109 {
1110 case SSL_CTRL_GET_READ_AHEAD:
1111 return(s->read_ahead);
1112 case SSL_CTRL_SET_READ_AHEAD:
1113 l=s->read_ahead;
1114 s->read_ahead=larg;
1115 return(l);
1116
1117 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1118 s->msg_callback_arg = parg;
1119 return 1;
1120
1121 case SSL_CTRL_OPTIONS:
1122 return(s->options|=larg);
1123 case SSL_CTRL_CLEAR_OPTIONS:
1124 return(s->options&=~larg);
1125 case SSL_CTRL_MODE:
1126 return(s->mode|=larg);
1127 case SSL_CTRL_CLEAR_MODE:
1128 return(s->mode &=~larg);
1129 case SSL_CTRL_GET_MAX_CERT_LIST:
1130 return(s->max_cert_list);
1131 case SSL_CTRL_SET_MAX_CERT_LIST:
1132 l=s->max_cert_list;
1133 s->max_cert_list=larg;
1134 return(l);
1135 case SSL_CTRL_SET_MTU:
Adam Langley95c29f32014-06-20 12:00:00 -07001136 if (larg < (long)dtls1_min_mtu())
1137 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001138 if (SSL_IS_DTLS(s))
1139 {
1140 s->d1->mtu = larg;
1141 return larg;
1142 }
1143 return 0;
1144 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1145 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1146 return 0;
1147 s->max_send_fragment = larg;
1148 return 1;
1149 case SSL_CTRL_GET_RI_SUPPORT:
1150 if (s->s3)
1151 return s->s3->send_connection_binding;
1152 else return 0;
1153 case SSL_CTRL_CERT_FLAGS:
1154 return(s->cert->cert_flags|=larg);
1155 case SSL_CTRL_CLEAR_CERT_FLAGS:
1156 return(s->cert->cert_flags &=~larg);
1157
1158 case SSL_CTRL_GET_RAW_CIPHERLIST:
1159 if (parg)
1160 {
1161 if (s->cert->ciphers_raw == NULL)
1162 return 0;
1163 *(unsigned char **)parg = s->cert->ciphers_raw;
1164 return (int)s->cert->ciphers_rawlen;
1165 }
1166 else
David Benjamin39482a12014-07-20 13:30:15 -04001167 {
1168 /* Passing a NULL |parg| returns the size of a single
1169 * cipher suite value. */
1170 return 2;
1171 }
Adam Langley95c29f32014-06-20 12:00:00 -07001172 default:
1173 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1174 }
1175 }
1176
1177long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1178 {
1179 switch(cmd)
1180 {
1181 case SSL_CTRL_SET_MSG_CALLBACK:
1182 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1183 return 1;
1184
1185 default:
1186 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1187 }
1188 }
1189
1190LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1191 {
1192 return ctx->sessions;
1193 }
1194
1195long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1196 {
1197 long l;
1198
1199 switch (cmd)
1200 {
1201 case SSL_CTRL_GET_READ_AHEAD:
1202 return(ctx->read_ahead);
1203 case SSL_CTRL_SET_READ_AHEAD:
1204 l=ctx->read_ahead;
1205 ctx->read_ahead=larg;
1206 return(l);
1207
1208 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1209 ctx->msg_callback_arg = parg;
1210 return 1;
1211
1212 case SSL_CTRL_GET_MAX_CERT_LIST:
1213 return(ctx->max_cert_list);
1214 case SSL_CTRL_SET_MAX_CERT_LIST:
1215 l=ctx->max_cert_list;
1216 ctx->max_cert_list=larg;
1217 return(l);
1218
1219 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1220 l=ctx->session_cache_size;
1221 ctx->session_cache_size=larg;
1222 return(l);
1223 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1224 return(ctx->session_cache_size);
1225 case SSL_CTRL_SET_SESS_CACHE_MODE:
1226 l=ctx->session_cache_mode;
1227 ctx->session_cache_mode=larg;
1228 return(l);
1229 case SSL_CTRL_GET_SESS_CACHE_MODE:
1230 return(ctx->session_cache_mode);
1231
1232 case SSL_CTRL_SESS_NUMBER:
1233 return(lh_SSL_SESSION_num_items(ctx->sessions));
1234 case SSL_CTRL_SESS_CONNECT:
1235 return(ctx->stats.sess_connect);
1236 case SSL_CTRL_SESS_CONNECT_GOOD:
1237 return(ctx->stats.sess_connect_good);
1238 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1239 return(ctx->stats.sess_connect_renegotiate);
1240 case SSL_CTRL_SESS_ACCEPT:
1241 return(ctx->stats.sess_accept);
1242 case SSL_CTRL_SESS_ACCEPT_GOOD:
1243 return(ctx->stats.sess_accept_good);
1244 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1245 return(ctx->stats.sess_accept_renegotiate);
1246 case SSL_CTRL_SESS_HIT:
1247 return(ctx->stats.sess_hit);
1248 case SSL_CTRL_SESS_CB_HIT:
1249 return(ctx->stats.sess_cb_hit);
1250 case SSL_CTRL_SESS_MISSES:
1251 return(ctx->stats.sess_miss);
1252 case SSL_CTRL_SESS_TIMEOUTS:
1253 return(ctx->stats.sess_timeout);
1254 case SSL_CTRL_SESS_CACHE_FULL:
1255 return(ctx->stats.sess_cache_full);
1256 case SSL_CTRL_OPTIONS:
1257 return(ctx->options|=larg);
1258 case SSL_CTRL_CLEAR_OPTIONS:
1259 return(ctx->options&=~larg);
1260 case SSL_CTRL_MODE:
1261 return(ctx->mode|=larg);
1262 case SSL_CTRL_CLEAR_MODE:
1263 return(ctx->mode&=~larg);
1264 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1265 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1266 return 0;
1267 ctx->max_send_fragment = larg;
1268 return 1;
1269 case SSL_CTRL_CERT_FLAGS:
1270 return(ctx->cert->cert_flags|=larg);
1271 case SSL_CTRL_CLEAR_CERT_FLAGS:
1272 return(ctx->cert->cert_flags &=~larg);
1273 default:
1274 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1275 }
1276 }
1277
1278long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1279 {
1280 switch(cmd)
1281 {
1282 case SSL_CTRL_SET_MSG_CALLBACK:
1283 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1284 return 1;
1285
1286 default:
1287 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1288 }
1289 }
1290
1291int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1292 {
1293 long l;
1294 const SSL_CIPHER *a = in_a;
1295 const SSL_CIPHER *b = in_b;
1296 const long a_id = a->id;
1297 const long b_id = b->id;
1298
1299 l = a_id - b_id;
1300 if (l == 0L)
1301 return(0);
1302 else
1303 return((l > 0)?1:-1);
1304 }
1305
1306int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1307 {
1308 long l;
1309 const long a_id = (*ap)->id;
1310 const long b_id = (*bp)->id;
1311
1312 l = a_id - b_id;
1313 if (l == 0)
1314 return(0);
1315 else
1316 return((l > 0)?1:-1);
1317 }
1318
1319/** return a STACK of the ciphers available for the SSL and in order of
1320 * preference */
1321STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1322 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001323 if (s == NULL)
1324 return NULL;
1325
1326 if (s->cipher_list != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001327 {
Adam Langley858a88d2014-06-20 12:00:00 -07001328 return(s->cipher_list->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -07001329 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001330
1331 if (s->version >= TLS1_1_VERSION)
1332 {
1333 if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001334 return s->ctx->cipher_list_tls11->ciphers;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001335 }
1336
1337 if ((s->ctx != NULL) &&
1338 (s->ctx->cipher_list != NULL))
1339 {
Adam Langley858a88d2014-06-20 12:00:00 -07001340 return(s->ctx->cipher_list->ciphers);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001341 }
1342
Adam Langley95c29f32014-06-20 12:00:00 -07001343 return(NULL);
1344 }
1345
1346/** return a STACK of the ciphers available for the SSL and in order of
1347 * algorithm id */
1348STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1349 {
1350 if (s != NULL)
1351 {
1352 if (s->cipher_list_by_id != NULL)
1353 {
1354 return(s->cipher_list_by_id);
1355 }
1356 else if ((s->ctx != NULL) &&
1357 (s->ctx->cipher_list_by_id != NULL))
1358 {
1359 return(s->ctx->cipher_list_by_id);
1360 }
1361 }
1362 return(NULL);
1363 }
1364
1365/** The old interface to get the same thing as SSL_get_ciphers() */
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001366const char *SSL_get_cipher_list(const SSL *s, int n)
Adam Langley95c29f32014-06-20 12:00:00 -07001367 {
David Benjamin6f260012014-08-15 13:49:12 -04001368 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001369 STACK_OF(SSL_CIPHER) *sk;
1370
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001371 if (s == NULL)
1372 return NULL;
1373 sk = SSL_get_ciphers(s);
1374 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk))
1375 return NULL;
1376 c = sk_SSL_CIPHER_value(sk, n);
1377 if (c == NULL)
1378 return NULL;
1379 return c->name;
Adam Langley95c29f32014-06-20 12:00:00 -07001380 }
1381
1382/** specify the ciphers to be used by default by the SSL_CTX */
1383int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1384 {
1385 STACK_OF(SSL_CIPHER) *sk;
1386
1387 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1388 &ctx->cipher_list_by_id,str, ctx->cert);
1389 /* ssl_create_cipher_list may return an empty stack if it
1390 * was unable to find a cipher matching the given rule string
1391 * (for example if the rule string specifies a cipher which
1392 * has been disabled). This is not an error as far as
1393 * ssl_create_cipher_list is concerned, and hence
1394 * ctx->cipher_list and ctx->cipher_list_by_id has been
1395 * updated. */
1396 if (sk == NULL)
1397 return 0;
1398 else if (sk_SSL_CIPHER_num(sk) == 0)
1399 {
1400 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1401 return 0;
1402 }
1403 return 1;
1404 }
1405
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001406int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str)
1407 {
1408 STACK_OF(SSL_CIPHER) *sk;
1409
1410 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert);
1411 if (sk == NULL)
1412 return 0;
1413 else if (sk_SSL_CIPHER_num(sk) == 0)
1414 {
1415 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH);
1416 return 0;
1417 }
1418 return 1;
1419 }
1420
Adam Langley95c29f32014-06-20 12:00:00 -07001421/** specify the ciphers to be used by the SSL */
1422int SSL_set_cipher_list(SSL *s,const char *str)
1423 {
1424 STACK_OF(SSL_CIPHER) *sk;
1425
1426 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1427 &s->cipher_list_by_id,str, s->cert);
1428 /* see comment in SSL_CTX_set_cipher_list */
1429 if (sk == NULL)
1430 return 0;
1431 else if (sk_SSL_CIPHER_num(sk) == 0)
1432 {
1433 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1434 return 0;
1435 }
1436 return 1;
1437 }
1438
David Benjamin5491e3f2014-09-29 19:33:09 -04001439int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p)
Adam Langley95c29f32014-06-20 12:00:00 -07001440 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001441 size_t i;
David Benjamin6f260012014-08-15 13:49:12 -04001442 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001443 CERT *ct = s->cert;
David Benjamin5491e3f2014-09-29 19:33:09 -04001444 uint8_t *q;
Adam Langley95c29f32014-06-20 12:00:00 -07001445 /* Set disabled masks for this session */
1446 ssl_set_client_disabled(s);
1447
1448 if (sk == NULL) return(0);
1449 q=p;
1450
1451 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1452 {
1453 c=sk_SSL_CIPHER_value(sk,i);
1454 /* Skip disabled ciphers */
1455 if (c->algorithm_ssl & ct->mask_ssl ||
1456 c->algorithm_mkey & ct->mask_k ||
1457 c->algorithm_auth & ct->mask_a)
1458 continue;
David Benjamin39482a12014-07-20 13:30:15 -04001459 s2n(ssl3_get_cipher_value(c), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001460 }
David Benjamin5491e3f2014-09-29 19:33:09 -04001461 /* If all ciphers were disabled, return the error to the caller. */
1462 if (p == q)
Adam Langley95c29f32014-06-20 12:00:00 -07001463 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001464 return 0;
1465 }
1466
1467 /* Add SCSVs. */
1468 if (!s->renegotiate)
1469 {
1470 s2n(SSL3_CK_SCSV & 0xffff, p);
1471 }
1472 if (s->fallback_scsv)
1473 {
1474 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001475 }
1476
1477 return(p-q);
1478 }
1479
David Benjamin9f2c0d72014-10-21 22:00:19 -04001480STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001481 {
David Benjamin39482a12014-07-20 13:30:15 -04001482 CBS cipher_suites = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001483 const SSL_CIPHER *c;
1484 STACK_OF(SSL_CIPHER) *sk;
David Benjamin39482a12014-07-20 13:30:15 -04001485
Adam Langley95c29f32014-06-20 12:00:00 -07001486 if (s->s3)
1487 s->s3->send_connection_binding = 0;
1488
David Benjamin39482a12014-07-20 13:30:15 -04001489 if (CBS_len(&cipher_suites) % 2 != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001490 {
1491 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001492 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001493 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001494
1495 sk = sk_SSL_CIPHER_new_null();
1496 if (sk == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001497 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001498 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1499 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001500 }
1501
David Benjamin39482a12014-07-20 13:30:15 -04001502 if (!CBS_stow(&cipher_suites,
1503 &s->cert->ciphers_raw, &s->cert->ciphers_rawlen))
Adam Langley95c29f32014-06-20 12:00:00 -07001504 {
1505 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1506 goto err;
1507 }
Adam Langley95c29f32014-06-20 12:00:00 -07001508
David Benjamin39482a12014-07-20 13:30:15 -04001509 while (CBS_len(&cipher_suites) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001510 {
David Benjamin39482a12014-07-20 13:30:15 -04001511 uint16_t cipher_suite;
1512
1513 if (!CBS_get_u16(&cipher_suites, &cipher_suite))
1514 {
1515 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1516 goto err;
1517 }
1518
David Benjamin9f2c0d72014-10-21 22:00:19 -04001519 /* Check for SCSV. */
David Benjamin39482a12014-07-20 13:30:15 -04001520 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff))
Adam Langley95c29f32014-06-20 12:00:00 -07001521 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001522 /* SCSV is fatal if renegotiating. */
Adam Langley95c29f32014-06-20 12:00:00 -07001523 if (s->renegotiate)
1524 {
1525 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1526 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1527 goto err;
1528 }
1529 s->s3->send_connection_binding = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001530 continue;
1531 }
1532
David Benjamin9f2c0d72014-10-21 22:00:19 -04001533 /* Check for FALLBACK_SCSV. */
1534 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff))
Adam Langley29707792014-06-20 12:00:00 -07001535 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001536 if (s->version < ssl_get_max_version(s))
1537 {
1538 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1539 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1540 goto err;
1541 }
1542 continue;
Adam Langley29707792014-06-20 12:00:00 -07001543 }
1544
David Benjamin39482a12014-07-20 13:30:15 -04001545 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -07001546 if (c != NULL)
1547 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001548 if (!sk_SSL_CIPHER_push(sk, c))
Adam Langley95c29f32014-06-20 12:00:00 -07001549 {
1550 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1551 goto err;
1552 }
1553 }
1554 }
1555
David Benjamin9f2c0d72014-10-21 22:00:19 -04001556 return sk;
1557
Adam Langley95c29f32014-06-20 12:00:00 -07001558err:
David Benjamin9f2c0d72014-10-21 22:00:19 -04001559 if (sk != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001560 sk_SSL_CIPHER_free(sk);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001561 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001562 }
1563
1564
Adam Langley95c29f32014-06-20 12:00:00 -07001565/** return a servername extension value if provided in Client Hello, or NULL.
1566 * So far, only host_name types are defined (RFC 3546).
1567 */
1568
1569const char *SSL_get_servername(const SSL *s, const int type)
1570 {
1571 if (type != TLSEXT_NAMETYPE_host_name)
1572 return NULL;
1573
1574 return s->session && !s->tlsext_hostname ?
1575 s->session->tlsext_hostname :
1576 s->tlsext_hostname;
1577 }
1578
1579int SSL_get_servername_type(const SSL *s)
1580 {
1581 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1582 return TLSEXT_NAMETYPE_host_name;
1583 return -1;
1584 }
1585
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001586void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx)
1587 {
1588 ctx->signed_cert_timestamps_enabled = 1;
1589 }
1590
1591int SSL_enable_signed_cert_timestamps(SSL *ssl)
1592 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001593 /* Currently not implemented server-side. */
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001594 if (ssl->server)
1595 return 0;
1596
1597 ssl->signed_cert_timestamps_enabled = 1;
1598 return 1;
1599 }
1600
David Benjamin6c7aed02014-08-27 16:42:38 -04001601void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx)
1602 {
1603 ctx->ocsp_stapling_enabled = 1;
1604 }
1605
1606int SSL_enable_ocsp_stapling(SSL *ssl)
1607 {
1608 /* Currently not implemented server-side. */
1609 if (ssl->server)
1610 return 0;
1611 ssl->ocsp_stapling_enabled = 1;
1612 return 1;
1613 }
1614
David Benjamin52d699f2014-11-25 01:35:18 -05001615void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out, size_t *out_len)
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001616 {
Adam Langley3cb50e02014-08-26 14:00:31 -07001617 SSL_SESSION *session = ssl->session;
1618
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001619 *out_len = 0;
1620 *out = NULL;
1621 if (ssl->server)
1622 return;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001623 if (!session || !session->tlsext_signed_cert_timestamp_list)
1624 return;
1625 *out = session->tlsext_signed_cert_timestamp_list;
1626 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1627 }
1628
David Benjamin52d699f2014-11-25 01:35:18 -05001629void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out, size_t *out_len)
David Benjamin6c7aed02014-08-27 16:42:38 -04001630 {
1631 SSL_SESSION *session = ssl->session;
1632
1633 *out_len = 0;
1634 *out = NULL;
1635 if (ssl->server)
1636 return;
1637 if (!session || !session->ocsp_response)
1638 return;
1639 *out = session->ocsp_response;
1640 *out_len = session->ocsp_response_length;
1641 }
1642
Adam Langley95c29f32014-06-20 12:00:00 -07001643/* SSL_select_next_proto implements the standard protocol selection. It is
1644 * expected that this function is called from the callback set by
1645 * SSL_CTX_set_next_proto_select_cb.
1646 *
1647 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1648 * strings. The length byte itself is not included in the length. A byte
1649 * string of length 0 is invalid. No byte string may be truncated.
1650 *
1651 * The current, but experimental algorithm for selecting the protocol is:
1652 *
1653 * 1) If the server doesn't support NPN then this is indicated to the
1654 * callback. In this case, the client application has to abort the connection
1655 * or have a default application level protocol.
1656 *
1657 * 2) If the server supports NPN, but advertises an empty list then the
1658 * client selects the first protcol in its list, but indicates via the
1659 * API that this fallback case was enacted.
1660 *
1661 * 3) Otherwise, the client finds the first protocol in the server's list
1662 * that it supports and selects this protocol. This is because it's
1663 * assumed that the server has better information about which protocol
1664 * a client should use.
1665 *
1666 * 4) If the client doesn't support any of the server's advertised
1667 * protocols, then this is treated the same as case 2.
1668 *
1669 * It returns either
1670 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1671 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1672 */
1673int 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)
1674 {
1675 unsigned int i, j;
1676 const unsigned char *result;
1677 int status = OPENSSL_NPN_UNSUPPORTED;
1678
1679 /* For each protocol in server preference order, see if we support it. */
1680 for (i = 0; i < server_len; )
1681 {
1682 for (j = 0; j < client_len; )
1683 {
1684 if (server[i] == client[j] &&
1685 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1686 {
1687 /* We found a match */
1688 result = &server[i];
1689 status = OPENSSL_NPN_NEGOTIATED;
1690 goto found;
1691 }
1692 j += client[j];
1693 j++;
1694 }
1695 i += server[i];
1696 i++;
1697 }
1698
1699 /* There's no overlap between our protocols and the server's list. */
1700 result = client;
1701 status = OPENSSL_NPN_NO_OVERLAP;
1702
1703 found:
1704 *out = (unsigned char *) result + 1;
1705 *outlen = result[0];
1706 return status;
1707 }
1708
Adam Langley95c29f32014-06-20 12:00:00 -07001709/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1710 * requested protocol for this connection and returns 0. If the client didn't
1711 * request any protocol, then *data is set to NULL.
1712 *
1713 * Note that the client can request any protocol it chooses. The value returned
1714 * from this function need not be a member of the list of supported protocols
1715 * provided by the callback.
1716 */
David Benjaminced551f2014-07-11 23:16:16 -04001717void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, unsigned *len)
Adam Langley95c29f32014-06-20 12:00:00 -07001718 {
1719 *data = s->next_proto_negotiated;
1720 if (!*data) {
1721 *len = 0;
1722 } else {
1723 *len = s->next_proto_negotiated_len;
1724 }
1725}
1726
1727/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1728 * TLS server needs a list of supported protocols for Next Protocol
1729 * Negotiation. The returned list must be in wire format. The list is returned
1730 * by setting |out| to point to it and |outlen| to its length. This memory will
1731 * not be modified, but one should assume that the SSL* keeps a reference to
1732 * it.
1733 *
1734 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1735 * such extension will be included in the ServerHello. */
1736void 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)
1737 {
1738 ctx->next_protos_advertised_cb = cb;
1739 ctx->next_protos_advertised_cb_arg = arg;
1740 }
1741
1742/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1743 * client needs to select a protocol from the server's provided list. |out|
1744 * must be set to point to the selected protocol (which may be within |in|).
1745 * The length of the protocol name must be written into |outlen|. The server's
1746 * advertised protocols are provided in |in| and |inlen|. The callback can
1747 * assume that |in| is syntactically valid.
1748 *
1749 * The client must select a protocol. It is fatal to the connection if this
1750 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1751 */
1752void 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)
1753 {
1754 ctx->next_proto_select_cb = cb;
1755 ctx->next_proto_select_cb_arg = arg;
1756 }
Adam Langley95c29f32014-06-20 12:00:00 -07001757
Adam Langley95c29f32014-06-20 12:00:00 -07001758/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1759 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1760 * length-prefixed strings).
1761 *
1762 * Returns 0 on success. */
1763int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1764 unsigned protos_len)
1765 {
1766 if (ctx->alpn_client_proto_list)
1767 OPENSSL_free(ctx->alpn_client_proto_list);
1768
David Benjamin072c9532014-07-26 11:44:25 -04001769 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001770 if (!ctx->alpn_client_proto_list)
1771 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001772 ctx->alpn_client_proto_list_len = protos_len;
1773
1774 return 0;
1775 }
1776
1777/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1778 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1779 * length-prefixed strings).
1780 *
1781 * Returns 0 on success. */
1782int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1783 unsigned protos_len)
1784 {
1785 if (ssl->alpn_client_proto_list)
1786 OPENSSL_free(ssl->alpn_client_proto_list);
1787
David Benjamin072c9532014-07-26 11:44:25 -04001788 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001789 if (!ssl->alpn_client_proto_list)
1790 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001791 ssl->alpn_client_proto_list_len = protos_len;
1792
1793 return 0;
1794 }
1795
1796/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1797 * during ClientHello processing in order to select an ALPN protocol from the
1798 * client's list of offered protocols. */
1799void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1800 int (*cb) (SSL *ssl,
1801 const unsigned char **out,
1802 unsigned char *outlen,
1803 const unsigned char *in,
1804 unsigned int inlen,
1805 void *arg),
1806 void *arg)
1807 {
1808 ctx->alpn_select_cb = cb;
1809 ctx->alpn_select_cb_arg = arg;
1810 }
1811
1812/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1813 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1814 * including the leading length-prefix byte). If the server didn't respond with
1815 * a negotiated protocol then |*len| will be zero. */
1816void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1817 unsigned *len)
1818 {
1819 *data = NULL;
1820 if (ssl->s3)
1821 *data = ssl->s3->alpn_selected;
1822 if (*data == NULL)
1823 *len = 0;
1824 else
1825 *len = ssl->s3->alpn_selected_len;
1826 }
Adam Langley95c29f32014-06-20 12:00:00 -07001827
1828int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1829 const char *label, size_t llen, const unsigned char *p, size_t plen,
1830 int use_context)
1831 {
1832 if (s->version < TLS1_VERSION)
1833 return -1;
1834
1835 return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1836 llen, p, plen,
1837 use_context);
1838 }
1839
1840static uint32_t ssl_session_hash(const SSL_SESSION *a)
1841 {
1842 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1843 ((uint32_t) a->session_id[1] << 8) ||
1844 ((uint32_t) a->session_id[2] << 16) ||
1845 ((uint32_t) a->session_id[3] << 24);
1846
1847 return hash;
1848 }
1849
1850/* NB: If this function (or indeed the hash function which uses a sort of
1851 * coarser function than this one) is changed, ensure
1852 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1853 * able to construct an SSL_SESSION that will collide with any existing session
1854 * with a matching session ID. */
1855static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1856 {
1857 if (a->ssl_version != b->ssl_version)
1858 return(1);
1859 if (a->session_id_length != b->session_id_length)
1860 return(1);
1861 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1862 }
1863
1864SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1865 {
1866 SSL_CTX *ret=NULL;
1867
1868 if (meth == NULL)
1869 {
1870 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1871 return(NULL);
1872 }
1873
Adam Langley95c29f32014-06-20 12:00:00 -07001874 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1875 {
1876 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1877 goto err;
1878 }
1879 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1880 if (ret == NULL)
1881 goto err;
1882
1883 memset(ret,0,sizeof(SSL_CTX));
1884
1885 ret->method=meth;
1886
1887 ret->cert_store=NULL;
1888 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1889 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1890 ret->session_cache_head=NULL;
1891 ret->session_cache_tail=NULL;
1892
1893 /* We take the system default */
David Benjaminf4501342014-08-14 17:24:37 -04001894 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001895
1896 ret->new_session_cb=0;
1897 ret->remove_session_cb=0;
1898 ret->get_session_cb=0;
1899 ret->generate_session_id=0;
1900
1901 memset((char *)&ret->stats,0,sizeof(ret->stats));
1902
1903 ret->references=1;
1904 ret->quiet_shutdown=0;
1905
Adam Langley95c29f32014-06-20 12:00:00 -07001906 ret->info_callback=NULL;
1907
1908 ret->app_verify_callback=0;
1909 ret->app_verify_arg=NULL;
1910
1911 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1912 ret->read_ahead=0;
1913 ret->msg_callback=0;
1914 ret->msg_callback_arg=NULL;
1915 ret->verify_mode=SSL_VERIFY_NONE;
1916#if 0
1917 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1918#endif
1919 ret->sid_ctx_length=0;
1920 ret->default_verify_callback=NULL;
1921 if ((ret->cert=ssl_cert_new()) == NULL)
1922 goto err;
1923
1924 ret->default_passwd_callback=0;
1925 ret->default_passwd_callback_userdata=NULL;
1926 ret->client_cert_cb=0;
1927 ret->app_gen_cookie_cb=0;
1928 ret->app_verify_cookie_cb=0;
1929
1930 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1931 if (ret->sessions == NULL) goto err;
1932 ret->cert_store=X509_STORE_new();
1933 if (ret->cert_store == NULL) goto err;
1934
1935 ssl_create_cipher_list(ret->method,
1936 &ret->cipher_list,&ret->cipher_list_by_id,
David Benjamin5e4f6e92014-11-17 03:23:24 -05001937 SSL_DEFAULT_CIPHER_LIST, ret->cert);
Adam Langley95c29f32014-06-20 12:00:00 -07001938 if (ret->cipher_list == NULL
Adam Langley858a88d2014-06-20 12:00:00 -07001939 || sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001940 {
1941 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1942 goto err2;
1943 }
1944
1945 ret->param = X509_VERIFY_PARAM_new();
1946 if (!ret->param)
1947 goto err;
1948
Adam Langley95c29f32014-06-20 12:00:00 -07001949 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1950 goto err;
1951
1952 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1953
1954 ret->extra_certs=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001955
1956 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1957
Adam Langley95c29f32014-06-20 12:00:00 -07001958 ret->tlsext_servername_callback = 0;
1959 ret->tlsext_servername_arg = NULL;
1960 /* Setup RFC4507 ticket keys */
1961 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
1962 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
1963 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
1964 ret->options |= SSL_OP_NO_TICKET;
1965
1966 ret->tlsext_status_cb = 0;
1967 ret->tlsext_status_arg = NULL;
1968
Adam Langley95c29f32014-06-20 12:00:00 -07001969 ret->next_protos_advertised_cb = 0;
1970 ret->next_proto_select_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001971 ret->psk_identity_hint=NULL;
1972 ret->psk_client_callback=NULL;
1973 ret->psk_server_callback=NULL;
David Benjamin422d3a42014-08-20 11:09:03 -04001974
Adam Langley95c29f32014-06-20 12:00:00 -07001975 /* Default is to connect to non-RI servers. When RI is more widely
1976 * deployed might change this.
1977 */
1978 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1979
1980 return(ret);
1981err:
1982 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
1983err2:
1984 if (ret != NULL) SSL_CTX_free(ret);
1985 return(NULL);
1986 }
1987
Adam Langley95c29f32014-06-20 12:00:00 -07001988
1989void SSL_CTX_free(SSL_CTX *a)
1990 {
1991 int i;
1992
1993 if (a == NULL) return;
1994
1995 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
1996#ifdef REF_PRINT
1997 REF_PRINT("SSL_CTX",a);
1998#endif
1999 if (i > 0) return;
2000#ifdef REF_CHECK
2001 if (i < 0)
2002 {
2003 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
2004 abort(); /* ok */
2005 }
2006#endif
2007
2008 if (a->param)
2009 X509_VERIFY_PARAM_free(a->param);
2010
2011 /*
2012 * Free internal session cache. However: the remove_cb() may reference
2013 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2014 * after the sessions were flushed.
2015 * As the ex_data handling routines might also touch the session cache,
2016 * the most secure solution seems to be: empty (flush) the cache, then
2017 * free ex_data, then finally free the cache.
2018 * (See ticket [openssl.org #212].)
2019 */
2020 if (a->sessions != NULL)
2021 SSL_CTX_flush_sessions(a,0);
2022
2023 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
2024
2025 if (a->sessions != NULL)
2026 lh_SSL_SESSION_free(a->sessions);
2027
2028 if (a->cert_store != NULL)
2029 X509_STORE_free(a->cert_store);
2030 if (a->cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002031 ssl_cipher_preference_list_free(a->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002032 if (a->cipher_list_by_id != NULL)
2033 sk_SSL_CIPHER_free(a->cipher_list_by_id);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07002034 if (a->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002035 ssl_cipher_preference_list_free(a->cipher_list_tls11);
Adam Langley95c29f32014-06-20 12:00:00 -07002036 if (a->cert != NULL)
2037 ssl_cert_free(a->cert);
2038 if (a->client_CA != NULL)
2039 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2040 if (a->extra_certs != NULL)
2041 sk_X509_pop_free(a->extra_certs,X509_free);
Adam Langley95c29f32014-06-20 12:00:00 -07002042
2043 if (a->srtp_profiles)
2044 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2045
Adam Langley95c29f32014-06-20 12:00:00 -07002046 if (a->psk_identity_hint)
2047 OPENSSL_free(a->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07002048
Adam Langley95c29f32014-06-20 12:00:00 -07002049 if (a->tlsext_ecpointformatlist)
2050 OPENSSL_free(a->tlsext_ecpointformatlist);
2051 if (a->tlsext_ellipticcurvelist)
2052 OPENSSL_free(a->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -07002053 if (a->alpn_client_proto_list != NULL)
2054 OPENSSL_free(a->alpn_client_proto_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002055
Adam Langley1258b6a2014-06-20 12:00:00 -07002056 if (a->tlsext_channel_id_private)
2057 EVP_PKEY_free(a->tlsext_channel_id_private);
Adam Langley1258b6a2014-06-20 12:00:00 -07002058
David Benjamin859ec3c2014-09-02 16:29:36 -04002059 if (a->keylog_bio)
2060 BIO_free(a->keylog_bio);
2061
Adam Langley95c29f32014-06-20 12:00:00 -07002062 OPENSSL_free(a);
2063 }
2064
2065void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2066 {
2067 ctx->default_passwd_callback=cb;
2068 }
2069
2070void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2071 {
2072 ctx->default_passwd_callback_userdata=u;
2073 }
2074
2075void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2076 {
2077 ctx->app_verify_callback=cb;
2078 ctx->app_verify_arg=arg;
2079 }
2080
2081void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2082 {
2083 ctx->verify_mode=mode;
2084 ctx->default_verify_callback=cb;
2085 }
2086
2087void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2088 {
2089 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2090 }
2091
2092void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2093 {
2094 ssl_cert_set_cert_cb(c->cert, cb, arg);
2095 }
2096
2097void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2098 {
2099 ssl_cert_set_cert_cb(s->cert, cb, arg);
2100 }
2101
David Benjamin033e5f42014-11-13 18:47:41 -05002102static int ssl_has_key(SSL *s, size_t idx)
2103 {
2104 CERT_PKEY *cpk = &s->cert->pkeys[idx];
2105 return cpk->x509 && cpk->privatekey;
2106 }
2107
David Benjaminf31e6812014-11-13 18:05:55 -05002108void ssl_get_compatible_server_ciphers(SSL *s, unsigned long *out_mask_k,
2109 unsigned long *out_mask_a)
Adam Langley95c29f32014-06-20 12:00:00 -07002110 {
David Benjaminf31e6812014-11-13 18:05:55 -05002111 CERT *c = s->cert;
David Benjaminf31e6812014-11-13 18:05:55 -05002112 int rsa_enc, rsa_sign, dh_tmp;
2113 unsigned long mask_k, mask_a;
David Benjamin060d9d22014-07-15 00:54:26 -04002114 int have_ecc_cert, ecdsa_ok;
David Benjamin0da0e182014-08-19 16:20:28 -04002115 int have_ecdh_tmp;
David Benjaminf31e6812014-11-13 18:05:55 -05002116 X509 *x;
Adam Langley95c29f32014-06-20 12:00:00 -07002117
David Benjaminf31e6812014-11-13 18:05:55 -05002118 if (c == NULL)
2119 {
2120 /* TODO(davidben): Is this codepath possible? */
2121 *out_mask_k = 0;
2122 *out_mask_a = 0;
2123 return;
2124 }
Adam Langley95c29f32014-06-20 12:00:00 -07002125
David Benjaminf31e6812014-11-13 18:05:55 -05002126 dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002127
David Benjaminf31e6812014-11-13 18:05:55 -05002128 have_ecdh_tmp = (c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
David Benjamin033e5f42014-11-13 18:47:41 -05002129 rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
2130 rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
2131 have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
David Benjaminf31e6812014-11-13 18:05:55 -05002132 mask_k = 0;
2133 mask_a = 0;
2134
David Benjamin77a942b2014-07-15 01:22:50 -04002135 if (rsa_enc)
David Benjaminf31e6812014-11-13 18:05:55 -05002136 mask_k |= SSL_kRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002137
Adam Langley95c29f32014-06-20 12:00:00 -07002138 if (dh_tmp)
David Benjaminf31e6812014-11-13 18:05:55 -05002139 mask_k |= SSL_kEDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002140
Adam Langley95c29f32014-06-20 12:00:00 -07002141 if (rsa_enc || rsa_sign)
2142 {
David Benjaminf31e6812014-11-13 18:05:55 -05002143 mask_a |= SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002144 }
2145
David Benjaminf31e6812014-11-13 18:05:55 -05002146 mask_a |= SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002147
David Benjamin0da0e182014-08-19 16:20:28 -04002148 /* An ECC certificate may be usable for ECDSA cipher suites depending on
David Benjamin033e5f42014-11-13 18:47:41 -05002149 * the key usage extension and on the client's curve preferences. */
Adam Langley95c29f32014-06-20 12:00:00 -07002150 if (have_ecc_cert)
2151 {
David Benjamin033e5f42014-11-13 18:47:41 -05002152 x = c->pkeys[SSL_PKEY_ECC].x509;
Adam Langley95c29f32014-06-20 12:00:00 -07002153 /* This call populates extension flags (ex_flags) */
2154 X509_check_purpose(x, -1, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07002155 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2156 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
David Benjamin033e5f42014-11-13 18:47:41 -05002157 if (!tls1_check_ec_cert(s, x))
Adam Langley95c29f32014-06-20 12:00:00 -07002158 ecdsa_ok = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002159 if (ecdsa_ok)
2160 {
David Benjaminf31e6812014-11-13 18:05:55 -05002161 mask_a |= SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002162 }
Adam Langley95c29f32014-06-20 12:00:00 -07002163 }
Adam Langley95c29f32014-06-20 12:00:00 -07002164
David Benjaminf31e6812014-11-13 18:05:55 -05002165 /* If we are considering an ECC cipher suite that uses an ephemeral EC
2166 * key, check it. */
2167 if (have_ecdh_tmp && tls1_check_ec_tmp_key(s))
Adam Langley95c29f32014-06-20 12:00:00 -07002168 {
David Benjaminf31e6812014-11-13 18:05:55 -05002169 mask_k |= SSL_kEECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002170 }
Adam Langley95c29f32014-06-20 12:00:00 -07002171
David Benjaminf31e6812014-11-13 18:05:55 -05002172 /* PSK requires a server callback. */
2173 if (s->psk_server_callback != NULL)
2174 {
2175 mask_k |= SSL_kPSK;
2176 mask_a |= SSL_aPSK;
2177 }
Adam Langley95c29f32014-06-20 12:00:00 -07002178
David Benjaminf31e6812014-11-13 18:05:55 -05002179 *out_mask_k = mask_k;
2180 *out_mask_a = mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002181 }
2182
2183/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2184#define ku_reject(x, usage) \
2185 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2186
Adam Langley95c29f32014-06-20 12:00:00 -07002187
2188int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2189 {
David Benjamin0da0e182014-08-19 16:20:28 -04002190 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002191 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2192 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2193
Adam Langley95c29f32014-06-20 12:00:00 -07002194 alg_a = cs->algorithm_auth;
2195
Adam Langley95c29f32014-06-20 12:00:00 -07002196 /* This call populates the ex_flags field correctly */
2197 X509_check_purpose(x, -1, 0);
2198 if ((x->sig_alg) && (x->sig_alg->algorithm))
2199 {
2200 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2201 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2202 }
Adam Langley95c29f32014-06-20 12:00:00 -07002203 if (alg_a & SSL_aECDSA)
2204 {
2205 /* key usage, if present, must allow signing */
2206 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2207 {
2208 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2209 return 0;
2210 }
2211 }
2212
2213 return 1; /* all checks are ok */
2214 }
2215
Adam Langley95c29f32014-06-20 12:00:00 -07002216
2217static int ssl_get_server_cert_index(const SSL *s)
2218 {
2219 int idx;
2220 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2221 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2222 idx = SSL_PKEY_RSA_SIGN;
2223 if (idx == -1)
2224 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2225 return idx;
2226 }
2227
2228CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2229 {
David Benjaminf31e6812014-11-13 18:05:55 -05002230 int i = ssl_get_server_cert_index(s);
Adam Langley95c29f32014-06-20 12:00:00 -07002231
2232 /* This may or may not be an error. */
2233 if (i < 0)
2234 return NULL;
2235
2236 /* May be NULL. */
David Benjaminf31e6812014-11-13 18:05:55 -05002237 return &s->cert->pkeys[i];
Adam Langley95c29f32014-06-20 12:00:00 -07002238 }
2239
David Benjaminec2f27d2014-11-13 19:17:25 -05002240EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher)
Adam Langley95c29f32014-06-20 12:00:00 -07002241 {
2242 unsigned long alg_a;
2243 CERT *c;
2244 int idx = -1;
2245
2246 alg_a = cipher->algorithm_auth;
2247 c=s->cert;
2248
David Benjaminef2116d2014-08-19 20:21:56 -04002249 if (alg_a & SSL_aRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002250 {
2251 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2252 idx = SSL_PKEY_RSA_SIGN;
2253 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2254 idx = SSL_PKEY_RSA_ENC;
2255 }
2256 else if ((alg_a & SSL_aECDSA) &&
2257 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2258 idx = SSL_PKEY_ECC;
2259 if (idx == -1)
2260 {
2261 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2262 return(NULL);
2263 }
Adam Langley95c29f32014-06-20 12:00:00 -07002264 return c->pkeys[idx].privatekey;
2265 }
2266
Adam Langley95c29f32014-06-20 12:00:00 -07002267void ssl_update_cache(SSL *s,int mode)
2268 {
2269 int i;
2270
2271 /* If the session_id_length is 0, we are not supposed to cache it,
2272 * and it would be rather hard to do anyway :-) */
2273 if (s->session->session_id_length == 0) return;
2274
David Benjamin041b58a2014-08-27 15:25:21 -04002275 i=s->initial_ctx->session_cache_mode;
Adam Langley95c29f32014-06-20 12:00:00 -07002276 if ((i & mode) && (!s->hit)
2277 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
David Benjamin041b58a2014-08-27 15:25:21 -04002278 || SSL_CTX_add_session(s->initial_ctx,s->session))
2279 && (s->initial_ctx->new_session_cb != NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07002280 {
2281 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
David Benjamin041b58a2014-08-27 15:25:21 -04002282 if (!s->initial_ctx->new_session_cb(s,s->session))
Adam Langley95c29f32014-06-20 12:00:00 -07002283 SSL_SESSION_free(s->session);
2284 }
2285
2286 /* auto flush every 255 connections */
2287 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2288 ((i & mode) == mode))
2289 {
2290 if ( (((mode & SSL_SESS_CACHE_CLIENT)
David Benjamin041b58a2014-08-27 15:25:21 -04002291 ?s->initial_ctx->stats.sess_connect_good
2292 :s->initial_ctx->stats.sess_accept_good) & 0xff) == 0xff)
Adam Langley95c29f32014-06-20 12:00:00 -07002293 {
David Benjamin041b58a2014-08-27 15:25:21 -04002294 SSL_CTX_flush_sessions(s->initial_ctx,(unsigned long)time(NULL));
Adam Langley95c29f32014-06-20 12:00:00 -07002295 }
2296 }
2297 }
2298
2299const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
2300 {
2301 return ctx->method;
2302 }
2303
2304const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2305 {
2306 return(s->method);
2307 }
2308
2309int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2310 {
2311 int conn= -1;
2312 int ret=1;
2313
2314 if (s->method != meth)
2315 {
2316 if (s->handshake_func != NULL)
2317 conn=(s->handshake_func == s->method->ssl_connect);
2318
2319 if (s->method->version == meth->version)
2320 s->method=meth;
2321 else
2322 {
2323 s->method->ssl_free(s);
2324 s->method=meth;
2325 ret=s->method->ssl_new(s);
2326 }
2327
2328 if (conn == 1)
2329 s->handshake_func=meth->ssl_connect;
2330 else if (conn == 0)
2331 s->handshake_func=meth->ssl_accept;
2332 }
2333 return(ret);
2334 }
2335
2336int SSL_get_error(const SSL *s,int i)
2337 {
2338 int reason;
2339 unsigned long l;
2340 BIO *bio;
2341
2342 if (i > 0) return(SSL_ERROR_NONE);
2343
2344 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2345 * etc, where we do encode the error */
2346 if ((l=ERR_peek_error()) != 0)
2347 {
2348 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2349 return(SSL_ERROR_SYSCALL);
2350 else
2351 return(SSL_ERROR_SSL);
2352 }
2353
Adam Langleyb2ce0582014-06-20 12:00:00 -07002354 if ((i < 0) && SSL_want_session(s))
2355 return(SSL_ERROR_PENDING_SESSION);
2356
Adam Langleydc9b1412014-06-20 12:00:00 -07002357 if ((i < 0) && SSL_want_certificate(s))
2358 return(SSL_ERROR_PENDING_CERTIFICATE);
2359
Adam Langley95c29f32014-06-20 12:00:00 -07002360 if ((i < 0) && SSL_want_read(s))
2361 {
2362 bio=SSL_get_rbio(s);
2363 if (BIO_should_read(bio))
2364 return(SSL_ERROR_WANT_READ);
2365 else if (BIO_should_write(bio))
2366 /* This one doesn't make too much sense ... We never try
2367 * to write to the rbio, and an application program where
2368 * rbio and wbio are separate couldn't even know what it
2369 * should wait for.
2370 * However if we ever set s->rwstate incorrectly
2371 * (so that we have SSL_want_read(s) instead of
2372 * SSL_want_write(s)) and rbio and wbio *are* the same,
2373 * this test works around that bug; so it might be safer
2374 * to keep it. */
2375 return(SSL_ERROR_WANT_WRITE);
2376 else if (BIO_should_io_special(bio))
2377 {
2378 reason=BIO_get_retry_reason(bio);
2379 if (reason == BIO_RR_CONNECT)
2380 return(SSL_ERROR_WANT_CONNECT);
2381 else if (reason == BIO_RR_ACCEPT)
2382 return(SSL_ERROR_WANT_ACCEPT);
2383 else
2384 return(SSL_ERROR_SYSCALL); /* unknown */
2385 }
2386 }
2387
2388 if ((i < 0) && SSL_want_write(s))
2389 {
2390 bio=SSL_get_wbio(s);
2391 if (BIO_should_write(bio))
2392 return(SSL_ERROR_WANT_WRITE);
2393 else if (BIO_should_read(bio))
2394 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2395 return(SSL_ERROR_WANT_READ);
2396 else if (BIO_should_io_special(bio))
2397 {
2398 reason=BIO_get_retry_reason(bio);
2399 if (reason == BIO_RR_CONNECT)
2400 return(SSL_ERROR_WANT_CONNECT);
2401 else if (reason == BIO_RR_ACCEPT)
2402 return(SSL_ERROR_WANT_ACCEPT);
2403 else
2404 return(SSL_ERROR_SYSCALL);
2405 }
2406 }
2407 if ((i < 0) && SSL_want_x509_lookup(s))
2408 {
2409 return(SSL_ERROR_WANT_X509_LOOKUP);
2410 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002411 if ((i < 0) && SSL_want_channel_id_lookup(s))
2412 {
2413 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2414 }
Adam Langley95c29f32014-06-20 12:00:00 -07002415
2416 if (i == 0)
2417 {
David Benjamin5e4f6e92014-11-17 03:23:24 -05002418 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2419 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
Adam Langley95c29f32014-06-20 12:00:00 -07002420 return(SSL_ERROR_ZERO_RETURN);
Adam Langley95c29f32014-06-20 12:00:00 -07002421 }
2422 return(SSL_ERROR_SYSCALL);
2423 }
2424
2425int SSL_do_handshake(SSL *s)
2426 {
2427 int ret=1;
2428
2429 if (s->handshake_func == NULL)
2430 {
2431 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2432 return(-1);
2433 }
2434
2435 s->method->ssl_renegotiate_check(s);
2436
2437 if (SSL_in_init(s) || SSL_in_before(s))
2438 {
2439 ret=s->handshake_func(s);
2440 }
2441 return(ret);
2442 }
2443
2444/* For the next 2 functions, SSL_clear() sets shutdown and so
2445 * one of these calls will reset it */
2446void SSL_set_accept_state(SSL *s)
2447 {
2448 s->server=1;
2449 s->shutdown=0;
2450 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2451 s->handshake_func=s->method->ssl_accept;
2452 /* clear the current cipher */
2453 ssl_clear_cipher_ctx(s);
2454 ssl_clear_hash_ctx(&s->read_hash);
2455 ssl_clear_hash_ctx(&s->write_hash);
2456 }
2457
2458void SSL_set_connect_state(SSL *s)
2459 {
2460 s->server=0;
2461 s->shutdown=0;
2462 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2463 s->handshake_func=s->method->ssl_connect;
2464 /* clear the current cipher */
2465 ssl_clear_cipher_ctx(s);
2466 ssl_clear_hash_ctx(&s->read_hash);
2467 ssl_clear_hash_ctx(&s->write_hash);
2468 }
2469
2470int ssl_undefined_function(SSL *s)
2471 {
2472 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2473 return(0);
2474 }
2475
2476int ssl_undefined_void_function(void)
2477 {
2478 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2479 return(0);
2480 }
2481
2482int ssl_undefined_const_function(const SSL *s)
2483 {
2484 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2485 return(0);
2486 }
2487
2488SSL_METHOD *ssl_bad_method(int ver)
2489 {
2490 OPENSSL_PUT_ERROR(SSL, ssl_bad_method, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2491 return(NULL);
2492 }
2493
Adam Langley0f4746e2014-08-13 12:26:32 -07002494static const char *ssl_get_version(int version)
Adam Langley95c29f32014-06-20 12:00:00 -07002495 {
Adam Langley0f4746e2014-08-13 12:26:32 -07002496 if (version == TLS1_2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002497 return("TLSv1.2");
Adam Langley0f4746e2014-08-13 12:26:32 -07002498 else if (version == TLS1_1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002499 return("TLSv1.1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002500 else if (version == TLS1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002501 return("TLSv1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002502 else if (version == SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002503 return("SSLv3");
Adam Langley95c29f32014-06-20 12:00:00 -07002504 else
2505 return("unknown");
2506 }
2507
Adam Langley0f4746e2014-08-13 12:26:32 -07002508const char *SSL_get_version(const SSL *s)
2509 {
2510 return ssl_get_version(s->version);
2511 }
2512
2513const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2514 {
2515 return ssl_get_version(sess->ssl_version);
2516 }
2517
Adam Langley95c29f32014-06-20 12:00:00 -07002518void ssl_clear_cipher_ctx(SSL *s)
2519 {
2520 if (s->enc_read_ctx != NULL)
2521 {
2522 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2523 OPENSSL_free(s->enc_read_ctx);
2524 s->enc_read_ctx=NULL;
2525 }
2526 if (s->enc_write_ctx != NULL)
2527 {
2528 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2529 OPENSSL_free(s->enc_write_ctx);
2530 s->enc_write_ctx=NULL;
2531 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002532 if (s->aead_read_ctx != NULL)
2533 {
2534 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2535 OPENSSL_free(s->aead_read_ctx);
2536 s->aead_read_ctx = NULL;
2537 }
2538 if (s->aead_write_ctx != NULL)
2539 {
2540 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2541 OPENSSL_free(s->aead_write_ctx);
2542 s->aead_write_ctx = NULL;
2543 }
Adam Langley95c29f32014-06-20 12:00:00 -07002544 }
2545
2546X509 *SSL_get_certificate(const SSL *s)
2547 {
2548 if (s->cert != NULL)
2549 return(s->cert->key->x509);
2550 else
2551 return(NULL);
2552 }
2553
2554EVP_PKEY *SSL_get_privatekey(const SSL *s)
2555 {
2556 if (s->cert != NULL)
2557 return(s->cert->key->privatekey);
2558 else
2559 return(NULL);
2560 }
2561
2562X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2563 {
2564 if (ctx->cert != NULL)
2565 return ctx->cert->key->x509;
2566 else
2567 return NULL;
2568 }
2569
2570EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2571 {
2572 if (ctx->cert != NULL)
2573 return ctx->cert->key->privatekey;
2574 else
2575 return NULL ;
2576 }
2577
2578const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2579 {
2580 if ((s->session != NULL) && (s->session->cipher != NULL))
2581 return(s->session->cipher);
2582 return(NULL);
2583 }
2584const void *SSL_get_current_compression(SSL *s)
2585 {
2586 return NULL;
2587 }
2588const void *SSL_get_current_expansion(SSL *s)
2589 {
2590 return NULL;
2591 }
2592
2593int ssl_init_wbio_buffer(SSL *s,int push)
2594 {
2595 BIO *bbio;
2596
2597 if (s->bbio == NULL)
2598 {
2599 bbio=BIO_new(BIO_f_buffer());
2600 if (bbio == NULL) return(0);
2601 s->bbio=bbio;
2602 }
2603 else
2604 {
2605 bbio=s->bbio;
2606 if (s->bbio == s->wbio)
2607 s->wbio=BIO_pop(s->wbio);
2608 }
2609 (void)BIO_reset(bbio);
2610/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2611 if (!BIO_set_read_buffer_size(bbio,1))
2612 {
2613 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2614 return(0);
2615 }
2616 if (push)
2617 {
2618 if (s->wbio != bbio)
2619 s->wbio=BIO_push(bbio,s->wbio);
2620 }
2621 else
2622 {
2623 if (s->wbio == bbio)
2624 s->wbio=BIO_pop(bbio);
2625 }
2626 return(1);
2627 }
2628
2629void ssl_free_wbio_buffer(SSL *s)
2630 {
2631 if (s->bbio == NULL) return;
2632
2633 if (s->bbio == s->wbio)
2634 {
2635 /* remove buffering */
2636 s->wbio=BIO_pop(s->wbio);
2637#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2638 assert(s->wbio != NULL);
2639#endif
2640 }
2641 BIO_free(s->bbio);
2642 s->bbio=NULL;
2643 }
2644
2645void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2646 {
2647 ctx->quiet_shutdown=mode;
2648 }
2649
2650int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2651 {
2652 return(ctx->quiet_shutdown);
2653 }
2654
2655void SSL_set_quiet_shutdown(SSL *s,int mode)
2656 {
2657 s->quiet_shutdown=mode;
2658 }
2659
2660int SSL_get_quiet_shutdown(const SSL *s)
2661 {
2662 return(s->quiet_shutdown);
2663 }
2664
2665void SSL_set_shutdown(SSL *s,int mode)
2666 {
2667 s->shutdown=mode;
2668 }
2669
2670int SSL_get_shutdown(const SSL *s)
2671 {
2672 return(s->shutdown);
2673 }
2674
2675int SSL_version(const SSL *s)
2676 {
2677 return(s->version);
2678 }
2679
2680SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2681 {
2682 return(ssl->ctx);
2683 }
2684
2685SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2686 {
2687 if (ssl->ctx == ctx)
2688 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002689 if (ctx == NULL)
2690 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002691 if (ssl->cert != NULL)
2692 ssl_cert_free(ssl->cert);
2693 ssl->cert = ssl_cert_dup(ctx->cert);
2694 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2695 if (ssl->ctx != NULL)
2696 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2697 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07002698
2699 ssl->sid_ctx_length = ctx->sid_ctx_length;
2700 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2701 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2702
Adam Langley95c29f32014-06-20 12:00:00 -07002703 return(ssl->ctx);
2704 }
2705
2706#ifndef OPENSSL_NO_STDIO
2707int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2708 {
2709 return(X509_STORE_set_default_paths(ctx->cert_store));
2710 }
2711
2712int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2713 const char *CApath)
2714 {
2715 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2716 }
2717#endif
2718
2719void SSL_set_info_callback(SSL *ssl,
2720 void (*cb)(const SSL *ssl,int type,int val))
2721 {
2722 ssl->info_callback=cb;
2723 }
2724
2725/* One compiler (Diab DCC) doesn't like argument names in returned
2726 function pointer. */
2727void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2728 {
2729 return ssl->info_callback;
2730 }
2731
2732int SSL_state(const SSL *ssl)
2733 {
2734 return(ssl->state);
2735 }
2736
2737void SSL_set_state(SSL *ssl, int state)
2738 {
2739 ssl->state = state;
2740 }
2741
2742void SSL_set_verify_result(SSL *ssl,long arg)
2743 {
2744 ssl->verify_result=arg;
2745 }
2746
2747long SSL_get_verify_result(const SSL *ssl)
2748 {
2749 return(ssl->verify_result);
2750 }
2751
2752int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2753 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2754 {
2755 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2756 new_func, dup_func, free_func);
2757 }
2758
2759int SSL_set_ex_data(SSL *s,int idx,void *arg)
2760 {
2761 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2762 }
2763
2764void *SSL_get_ex_data(const SSL *s,int idx)
2765 {
2766 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2767 }
2768
2769int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2770 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2771 {
2772 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2773 new_func, dup_func, free_func);
2774 }
2775
2776int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2777 {
2778 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2779 }
2780
2781void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2782 {
2783 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2784 }
2785
2786int ssl_ok(SSL *s)
2787 {
2788 return(1);
2789 }
2790
2791X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2792 {
2793 return(ctx->cert_store);
2794 }
2795
2796void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2797 {
2798 if (ctx->cert_store != NULL)
2799 X509_STORE_free(ctx->cert_store);
2800 ctx->cert_store=store;
2801 }
2802
2803int SSL_want(const SSL *s)
2804 {
2805 return(s->rwstate);
2806 }
2807
2808/*!
2809 * \brief Set the callback for generating temporary RSA keys.
2810 * \param ctx the SSL context.
2811 * \param cb the callback
2812 */
2813
Adam Langley95c29f32014-06-20 12:00:00 -07002814void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2815 int is_export,
2816 int keylength))
2817 {
2818 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2819 }
2820
2821void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2822 int is_export,
2823 int keylength))
2824 {
2825 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2826 }
Adam Langley95c29f32014-06-20 12:00:00 -07002827
2828#ifdef DOXYGEN
2829/*!
2830 * \brief The RSA temporary key callback function.
2831 * \param ssl the SSL session.
2832 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2833 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2834 * of the required key in bits.
2835 * \return the temporary RSA key.
2836 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2837 */
2838
2839RSA *cb(SSL *ssl,int is_export,int keylength)
2840 {}
2841#endif
2842
2843/*!
2844 * \brief Set the callback for generating temporary DH keys.
2845 * \param ctx the SSL context.
2846 * \param dh the callback
2847 */
2848
Adam Langley95c29f32014-06-20 12:00:00 -07002849void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2850 int keylength))
2851 {
2852 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2853 }
2854
2855void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2856 int keylength))
2857 {
2858 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2859 }
Adam Langley95c29f32014-06-20 12:00:00 -07002860
Adam Langley95c29f32014-06-20 12:00:00 -07002861void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2862 int keylength))
2863 {
2864 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2865 }
2866
2867void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2868 int keylength))
2869 {
2870 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2871 }
Adam Langley95c29f32014-06-20 12:00:00 -07002872
Adam Langley95c29f32014-06-20 12:00:00 -07002873int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2874 {
2875 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2876 {
2877 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2878 return 0;
2879 }
2880 if (ctx->psk_identity_hint != NULL)
2881 OPENSSL_free(ctx->psk_identity_hint);
2882 if (identity_hint != NULL)
2883 {
2884 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2885 if (ctx->psk_identity_hint == NULL)
2886 return 0;
2887 }
2888 else
2889 ctx->psk_identity_hint = NULL;
2890 return 1;
2891 }
2892
2893int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
2894 {
2895 if (s == NULL)
2896 return 0;
2897
Adam Langley95c29f32014-06-20 12:00:00 -07002898 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2899 {
2900 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2901 return 0;
2902 }
Adam Langley0289c732014-06-20 12:00:00 -07002903
David Benjamin688d8df2014-11-02 23:06:42 -05002904 /* Clear currently configured hint, if any. */
Adam Langley0289c732014-06-20 12:00:00 -07002905 if (s->psk_identity_hint != NULL)
2906 {
2907 OPENSSL_free(s->psk_identity_hint);
2908 s->psk_identity_hint = NULL;
2909 }
Adam Langley0289c732014-06-20 12:00:00 -07002910
Adam Langley95c29f32014-06-20 12:00:00 -07002911 if (identity_hint != NULL)
2912 {
David Benjamin688d8df2014-11-02 23:06:42 -05002913 s->psk_identity_hint = BUF_strdup(identity_hint);
2914 if (s->psk_identity_hint == NULL)
2915 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002916 }
Adam Langley95c29f32014-06-20 12:00:00 -07002917 return 1;
2918 }
2919
2920const char *SSL_get_psk_identity_hint(const SSL *s)
2921 {
Adam Langley0289c732014-06-20 12:00:00 -07002922 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002923 return NULL;
David Benjamin688d8df2014-11-02 23:06:42 -05002924 return s->psk_identity_hint;
Adam Langley95c29f32014-06-20 12:00:00 -07002925 }
2926
2927const char *SSL_get_psk_identity(const SSL *s)
2928 {
2929 if (s == NULL || s->session == NULL)
2930 return NULL;
2931 return(s->session->psk_identity);
2932 }
2933
2934void SSL_set_psk_client_callback(SSL *s,
2935 unsigned int (*cb)(SSL *ssl, const char *hint,
2936 char *identity, unsigned int max_identity_len, unsigned char *psk,
2937 unsigned int max_psk_len))
2938 {
2939 s->psk_client_callback = cb;
2940 }
2941
2942void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
2943 unsigned int (*cb)(SSL *ssl, const char *hint,
2944 char *identity, unsigned int max_identity_len, unsigned char *psk,
2945 unsigned int max_psk_len))
2946 {
2947 ctx->psk_client_callback = cb;
2948 }
2949
2950void SSL_set_psk_server_callback(SSL *s,
2951 unsigned int (*cb)(SSL *ssl, const char *identity,
2952 unsigned char *psk, unsigned int max_psk_len))
2953 {
2954 s->psk_server_callback = cb;
2955 }
2956
2957void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
2958 unsigned int (*cb)(SSL *ssl, const char *identity,
2959 unsigned char *psk, unsigned int max_psk_len))
2960 {
2961 ctx->psk_server_callback = cb;
2962 }
Adam Langley95c29f32014-06-20 12:00:00 -07002963
2964void 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))
2965 {
2966 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2967 }
2968void 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))
2969 {
2970 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
2971 }
2972
David Benjamin859ec3c2014-09-02 16:29:36 -04002973void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio)
2974 {
2975 if (ctx->keylog_bio != NULL)
2976 BIO_free(ctx->keylog_bio);
2977 ctx->keylog_bio = keylog_bio;
2978 }
2979
2980static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len)
2981 {
2982 static const char hextable[] = "0123456789abcdef";
2983 uint8_t *out;
2984 size_t i;
2985
2986 if (!CBB_add_space(cbb, &out, in_len * 2))
2987 {
2988 return 0;
2989 }
2990
2991 for (i = 0; i < in_len; i++)
2992 {
2993 *(out++) = (uint8_t)hextable[in[i] >> 4];
2994 *(out++) = (uint8_t)hextable[in[i] & 0xf];
2995 }
2996 return 1;
2997 }
2998
2999int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
3000 const uint8_t *encrypted_premaster, size_t encrypted_premaster_len,
3001 const uint8_t *premaster, size_t premaster_len)
3002 {
3003 BIO *bio = ctx->keylog_bio;
3004 CBB cbb;
3005 uint8_t *out;
3006 size_t out_len;
3007 int ret;
3008
3009 if (bio == NULL)
3010 {
3011 return 1;
3012 }
3013
3014 if (encrypted_premaster_len < 8)
3015 {
3016 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange, ERR_R_INTERNAL_ERROR);
3017 return 0;
3018 }
3019
3020 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len*2 + 1))
3021 {
3022 return 0;
3023 }
3024 if (!CBB_add_bytes(&cbb, (const uint8_t*)"RSA ", 4) ||
3025 /* Only the first 8 bytes of the encrypted premaster secret are
3026 * logged. */
3027 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
3028 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3029 !cbb_add_hex(&cbb, premaster, premaster_len) ||
3030 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3031 !CBB_finish(&cbb, &out, &out_len))
3032 {
3033 CBB_cleanup(&cbb);
3034 return 0;
3035 }
3036
3037 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3038 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3039 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3040
3041 OPENSSL_free(out);
3042 return ret;
3043 }
3044
3045int ssl_ctx_log_master_secret(SSL_CTX *ctx,
3046 const uint8_t *client_random, size_t client_random_len,
3047 const uint8_t *master, size_t master_len)
3048 {
3049 BIO *bio = ctx->keylog_bio;
3050 CBB cbb;
3051 uint8_t *out;
3052 size_t out_len;
3053 int ret;
3054
3055 if (bio == NULL)
3056 {
3057 return 1;
3058 }
3059
3060 if (client_random_len != 32)
3061 {
3062 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
3063 return 0;
3064 }
3065
3066 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len*2 + 1))
3067 {
3068 return 0;
3069 }
3070 if (!CBB_add_bytes(&cbb, (const uint8_t*)"CLIENT_RANDOM ", 14) ||
3071 !cbb_add_hex(&cbb, client_random, 32) ||
3072 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3073 !cbb_add_hex(&cbb, master, master_len) ||
3074 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3075 !CBB_finish(&cbb, &out, &out_len))
3076 {
3077 CBB_cleanup(&cbb);
3078 return 0;
3079 }
3080
3081 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3082 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3083 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3084
3085 OPENSSL_free(out);
3086 return ret;
3087 }
3088
Adam Langleyadb739e2014-06-20 12:00:00 -07003089int SSL_cutthrough_complete(const SSL *s)
3090 {
3091 return (!s->server && /* cutthrough only applies to clients */
3092 !s->hit && /* full-handshake */
3093 s->version >= SSL3_VERSION &&
3094 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3095 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3096 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3097 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3098 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
David Benjamin7e3305e2014-07-28 14:52:32 -04003099 s->state == SSL3_ST_CR_CHANGE ||
Adam Langleyadb739e2014-06-20 12:00:00 -07003100 s->state == SSL3_ST_CR_FINISHED_A));
3101 }
3102
Adam Langley95f22882014-06-20 12:00:00 -07003103void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3104 size_t* ssl_session_size)
3105{
3106 *ssl_size = sizeof(SSL);
3107 *ssl_ctx_size = sizeof(SSL_CTX);
3108 *ssl_session_size = sizeof(SSL_SESSION);
3109}
3110
Adam Langleyadb739e2014-06-20 12:00:00 -07003111int ssl3_can_cutthrough(const SSL *s)
3112 {
3113 const SSL_CIPHER *c;
3114
3115 /* require a strong enough cipher */
3116 if (SSL_get_cipher_bits(s, NULL) < 128)
3117 return 0;
3118
3119 /* require ALPN or NPN extension */
David Benjamin457112e2014-08-27 14:35:09 -04003120 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen)
Adam Langleyadb739e2014-06-20 12:00:00 -07003121 {
3122 return 0;
3123 }
3124
3125 /* require a forward-secret cipher */
3126 c = SSL_get_current_cipher(s);
3127 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3128 c->algorithm_mkey != SSL_kEECDH))
3129 {
3130 return 0;
3131 }
3132
3133 return 1;
3134 }
3135
Adam Langley29707792014-06-20 12:00:00 -07003136/* ssl_get_max_version returns the maximum SSL/TLS version number supported by
3137 * |s|, or zero if all versions are disabled. */
3138int ssl_get_max_version(const SSL *s)
3139 {
3140 /* Only one version supported for DTLS. */
3141 if (s->version == DTLS1_VERSION)
3142 return DTLS1_VERSION;
3143
3144 if (!(s->options & SSL_OP_NO_TLSv1_2))
3145 return TLS1_2_VERSION;
3146 if (!(s->options & SSL_OP_NO_TLSv1_1))
3147 return TLS1_1_VERSION;
3148 if (!(s->options & SSL_OP_NO_TLSv1))
3149 return TLS1_VERSION;
3150 if (!(s->options & SSL_OP_NO_SSLv3))
3151 return SSL3_VERSION;
Adam Langley29707792014-06-20 12:00:00 -07003152 return 0;
3153 }
3154
Adam Langley95c29f32014-06-20 12:00:00 -07003155/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3156 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3157 * any. If EVP_MD pointer is passed, initializes ctx with this md
3158 * Returns newly allocated ctx;
3159 */
3160
3161EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3162{
3163 ssl_clear_hash_ctx(hash);
3164 *hash = EVP_MD_CTX_create();
David Benjaminae3e4872014-11-18 21:52:26 -05003165 if (md != NULL && *hash != NULL &&
3166 !EVP_DigestInit_ex(*hash, md, NULL))
Adam Langley69a01602014-11-17 17:26:55 -08003167 {
David Benjaminae3e4872014-11-18 21:52:26 -05003168 EVP_MD_CTX_destroy(*hash);
3169 *hash = NULL;
Adam Langley69a01602014-11-17 17:26:55 -08003170 }
Adam Langley95c29f32014-06-20 12:00:00 -07003171 return *hash;
3172}
3173void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3174{
3175
3176 if (*hash) EVP_MD_CTX_destroy(*hash);
3177 *hash=NULL;
3178}
3179
3180void SSL_set_debug(SSL *s, int debug)
3181 {
3182 s->debug = debug;
3183 }
3184
3185int SSL_cache_hit(SSL *s)
3186 {
3187 return s->hit;
3188 }
3189
3190int SSL_is_server(SSL *s)
3191 {
3192 return s->server;
3193 }