blob: ec054d26350d73eee348227a8075d95aeb270585 [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,
262 meth->version == SSL2_VERSION ?
263 "SSLv2" :
264 SSL_DEFAULT_CIPHER_LIST,
265 ctx->cert);
266
Adam Langley95c29f32014-06-20 12:00:00 -0700267 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0))
268 {
269 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_ssl_version, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
270 return(0);
271 }
272 return(1);
273 }
274
275SSL *SSL_new(SSL_CTX *ctx)
276 {
277 SSL *s;
278
279 if (ctx == NULL)
280 {
281 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
282 return(NULL);
283 }
284 if (ctx->method == NULL)
285 {
286 OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
287 return(NULL);
288 }
289
290 s=(SSL *)OPENSSL_malloc(sizeof(SSL));
291 if (s == NULL) goto err;
292 memset(s,0,sizeof(SSL));
293
294 s->options=ctx->options;
295 s->mode=ctx->mode;
296 s->max_cert_list=ctx->max_cert_list;
297
298 if (ctx->cert != NULL)
299 {
300 /* Earlier library versions used to copy the pointer to
301 * the CERT, not its contents; only when setting new
302 * parameters for the per-SSL copy, ssl_cert_new would be
303 * called (and the direct reference to the per-SSL_CTX
304 * settings would be lost, but those still were indirectly
305 * accessed for various purposes, and for that reason they
306 * used to be known as s->ctx->default_cert).
307 * Now we don't look at the SSL_CTX's CERT after having
308 * duplicated it once. */
309
310 s->cert = ssl_cert_dup(ctx->cert);
311 if (s->cert == NULL)
312 goto err;
313 }
314 else
315 s->cert=NULL; /* Cannot really happen (see SSL_CTX_new) */
316
317 s->read_ahead=ctx->read_ahead;
318 s->msg_callback=ctx->msg_callback;
319 s->msg_callback_arg=ctx->msg_callback_arg;
320 s->verify_mode=ctx->verify_mode;
321#if 0
322 s->verify_depth=ctx->verify_depth;
323#endif
324 s->sid_ctx_length=ctx->sid_ctx_length;
325 assert(s->sid_ctx_length <= sizeof s->sid_ctx);
326 memcpy(&s->sid_ctx,&ctx->sid_ctx,sizeof(s->sid_ctx));
327 s->verify_callback=ctx->default_verify_callback;
328 s->generate_session_id=ctx->generate_session_id;
329
330 s->param = X509_VERIFY_PARAM_new();
331 if (!s->param)
332 goto err;
333 X509_VERIFY_PARAM_inherit(s->param, ctx->param);
334#if 0
335 s->purpose = ctx->purpose;
336 s->trust = ctx->trust;
337#endif
338 s->quiet_shutdown=ctx->quiet_shutdown;
339 s->max_send_fragment = ctx->max_send_fragment;
340
341 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
342 s->ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700343 s->tlsext_debug_cb = 0;
344 s->tlsext_debug_arg = NULL;
345 s->tlsext_ticket_expected = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700346 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
347 s->initial_ctx=ctx;
Adam Langley95c29f32014-06-20 12:00:00 -0700348 if (ctx->tlsext_ecpointformatlist)
349 {
350 s->tlsext_ecpointformatlist =
351 BUF_memdup(ctx->tlsext_ecpointformatlist,
352 ctx->tlsext_ecpointformatlist_length);
353 if (!s->tlsext_ecpointformatlist)
354 goto err;
355 s->tlsext_ecpointformatlist_length =
356 ctx->tlsext_ecpointformatlist_length;
357 }
358 if (ctx->tlsext_ellipticcurvelist)
359 {
360 s->tlsext_ellipticcurvelist =
361 BUF_memdup(ctx->tlsext_ellipticcurvelist,
David Benjamin072334d2014-07-13 16:24:27 -0400362 ctx->tlsext_ellipticcurvelist_length * 2);
Adam Langley95c29f32014-06-20 12:00:00 -0700363 if (!s->tlsext_ellipticcurvelist)
364 goto err;
365 s->tlsext_ellipticcurvelist_length =
366 ctx->tlsext_ellipticcurvelist_length;
367 }
Adam Langley95c29f32014-06-20 12:00:00 -0700368 s->next_proto_negotiated = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700369
370 if (s->ctx->alpn_client_proto_list)
371 {
David Benjamin072c9532014-07-26 11:44:25 -0400372 s->alpn_client_proto_list = BUF_memdup(
373 s->ctx->alpn_client_proto_list,
374 s->ctx->alpn_client_proto_list_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700375 if (s->alpn_client_proto_list == NULL)
376 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -0700377 s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
378 }
Adam Langley95c29f32014-06-20 12:00:00 -0700379
380 s->verify_result=X509_V_OK;
381
382 s->method=ctx->method;
383
384 if (!s->method->ssl_new(s))
385 goto err;
386
387 s->references=1;
388 s->server=(ctx->method->ssl_accept == ssl_undefined_function)?0:1;
389
390 SSL_clear(s);
391
392 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
393
Adam Langley0289c732014-06-20 12:00:00 -0700394 s->psk_identity_hint = NULL;
395 if (ctx->psk_identity_hint)
396 {
397 s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
398 if (s->psk_identity_hint == NULL)
399 goto err;
400 }
Adam Langley95c29f32014-06-20 12:00:00 -0700401 s->psk_client_callback=ctx->psk_client_callback;
402 s->psk_server_callback=ctx->psk_server_callback;
Adam Langley95c29f32014-06-20 12:00:00 -0700403
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200404 if (!s->server)
David Benjamin6c7aed02014-08-27 16:42:38 -0400405 {
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200406 s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
David Benjamin6c7aed02014-08-27 16:42:38 -0400407 s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
408 }
HÃ¥vard Molland9169c962014-08-14 14:42:37 +0200409
Adam Langley95c29f32014-06-20 12:00:00 -0700410 return(s);
411err:
412 if (s != NULL)
413 {
414 if (s->cert != NULL)
415 ssl_cert_free(s->cert);
416 if (s->ctx != NULL)
417 SSL_CTX_free(s->ctx); /* decrement reference count */
418 OPENSSL_free(s);
419 }
420 OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
421 return(NULL);
422 }
423
424int SSL_CTX_set_session_id_context(SSL_CTX *ctx,const unsigned char *sid_ctx,
425 unsigned int sid_ctx_len)
426 {
427 if(sid_ctx_len > sizeof ctx->sid_ctx)
428 {
429 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
430 return 0;
431 }
432 ctx->sid_ctx_length=sid_ctx_len;
433 memcpy(ctx->sid_ctx,sid_ctx,sid_ctx_len);
434
435 return 1;
436 }
437
438int SSL_set_session_id_context(SSL *ssl,const unsigned char *sid_ctx,
439 unsigned int sid_ctx_len)
440 {
441 if(sid_ctx_len > SSL_MAX_SID_CTX_LENGTH)
442 {
443 OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
444 return 0;
445 }
446 ssl->sid_ctx_length=sid_ctx_len;
447 memcpy(ssl->sid_ctx,sid_ctx,sid_ctx_len);
448
449 return 1;
450 }
451
452int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
453 {
454 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
455 ctx->generate_session_id = cb;
456 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
457 return 1;
458 }
459
460int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
461 {
462 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
463 ssl->generate_session_id = cb;
464 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
465 return 1;
466 }
467
468int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
469 unsigned int id_len)
470 {
471 /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
472 * we can "construct" a session to give us the desired check - ie. to
473 * find if there's a session in the hash table that would conflict with
474 * any new session built out of this id/id_len and the ssl_version in
475 * use by this SSL. */
476 SSL_SESSION r, *p;
477
478 if(id_len > sizeof r.session_id)
479 return 0;
480
481 r.ssl_version = ssl->version;
482 r.session_id_length = id_len;
483 memcpy(r.session_id, id, id_len);
484 /* NB: SSLv2 always uses a fixed 16-byte session ID, so even if a
485 * callback is calling us to check the uniqueness of a shorter ID, it
486 * must be compared as a padded-out ID because that is what it will be
487 * converted to when the callback has finished choosing it. */
488 if((r.ssl_version == SSL2_VERSION) &&
489 (id_len < SSL2_SSL_SESSION_ID_LENGTH))
490 {
491 memset(r.session_id + id_len, 0,
492 SSL2_SSL_SESSION_ID_LENGTH - id_len);
493 r.session_id_length = SSL2_SSL_SESSION_ID_LENGTH;
494 }
495
496 CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
497 p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
498 CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
499 return (p != NULL);
500 }
501
502int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
503 {
504 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
505 }
506
507int SSL_set_purpose(SSL *s, int purpose)
508 {
509 return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
510 }
511
512int SSL_CTX_set_trust(SSL_CTX *s, int trust)
513 {
514 return X509_VERIFY_PARAM_set_trust(s->param, trust);
515 }
516
517int SSL_set_trust(SSL *s, int trust)
518 {
519 return X509_VERIFY_PARAM_set_trust(s->param, trust);
520 }
521
522int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
523 {
524 return X509_VERIFY_PARAM_set1(ctx->param, vpm);
525 }
526
527int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
528 {
529 return X509_VERIFY_PARAM_set1(ssl->param, vpm);
530 }
531
Adam Langley858a88d2014-06-20 12:00:00 -0700532void ssl_cipher_preference_list_free(
533 struct ssl_cipher_preference_list_st *cipher_list)
534 {
535 sk_SSL_CIPHER_free(cipher_list->ciphers);
536 OPENSSL_free(cipher_list->in_group_flags);
537 OPENSSL_free(cipher_list);
538 }
539
540struct ssl_cipher_preference_list_st*
541ssl_cipher_preference_list_dup(
542 struct ssl_cipher_preference_list_st *cipher_list)
543 {
544 struct ssl_cipher_preference_list_st* ret = NULL;
545 size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
546
547 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
548 if (!ret)
549 goto err;
550 ret->ciphers = NULL;
551 ret->in_group_flags = NULL;
552 ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
553 if (!ret->ciphers)
554 goto err;
David Benjamin072c9532014-07-26 11:44:25 -0400555 ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
Adam Langley858a88d2014-06-20 12:00:00 -0700556 if (!ret->in_group_flags)
557 goto err;
Adam Langley858a88d2014-06-20 12:00:00 -0700558 return ret;
559
560err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400561 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700562 sk_SSL_CIPHER_free(ret->ciphers);
563 if (ret)
564 OPENSSL_free(ret);
565 return NULL;
566 }
567
568struct ssl_cipher_preference_list_st*
569ssl_cipher_preference_list_from_ciphers(STACK_OF(SSL_CIPHER) *ciphers)
570 {
571 struct ssl_cipher_preference_list_st* ret = NULL;
572 size_t n = sk_SSL_CIPHER_num(ciphers);
573
574 ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
575 if (!ret)
576 goto err;
577 ret->ciphers = NULL;
578 ret->in_group_flags = NULL;
579 ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
580 if (!ret->ciphers)
581 goto err;
582 ret->in_group_flags = OPENSSL_malloc(n);
583 if (!ret->in_group_flags)
584 goto err;
585 memset(ret->in_group_flags, 0, n);
586 return ret;
587
588err:
David Benjamin61b66ff2014-07-25 03:58:53 -0400589 if (ret && ret->ciphers)
Adam Langley858a88d2014-06-20 12:00:00 -0700590 sk_SSL_CIPHER_free(ret->ciphers);
591 if (ret)
592 OPENSSL_free(ret);
593 return NULL;
594 }
595
Adam Langley95c29f32014-06-20 12:00:00 -0700596X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
597 {
598 return ctx->param;
599 }
600
601X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
602 {
603 return ssl->param;
604 }
605
606void SSL_certs_clear(SSL *s)
607 {
608 ssl_cert_clear_certs(s->cert);
609 }
610
611void SSL_free(SSL *s)
612 {
613 int i;
614
615 if(s == NULL)
616 return;
617
618 i=CRYPTO_add(&s->references,-1,CRYPTO_LOCK_SSL);
619#ifdef REF_PRINT
620 REF_PRINT("SSL",s);
621#endif
622 if (i > 0) return;
623#ifdef REF_CHECK
624 if (i < 0)
625 {
626 fprintf(stderr,"SSL_free, bad reference count\n");
627 abort(); /* ok */
628 }
629#endif
630
631 if (s->param)
632 X509_VERIFY_PARAM_free(s->param);
633
634 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
635
636 if (s->bbio != NULL)
637 {
638 /* If the buffering BIO is in place, pop it off */
639 if (s->bbio == s->wbio)
640 {
641 s->wbio=BIO_pop(s->wbio);
642 }
643 BIO_free(s->bbio);
644 s->bbio=NULL;
645 }
646 if (s->rbio != NULL)
647 BIO_free_all(s->rbio);
648 if ((s->wbio != NULL) && (s->wbio != s->rbio))
649 BIO_free_all(s->wbio);
650
651 if (s->init_buf != NULL) BUF_MEM_free(s->init_buf);
652
653 /* add extra stuff */
Adam Langley858a88d2014-06-20 12:00:00 -0700654 if (s->cipher_list != NULL)
655 ssl_cipher_preference_list_free(s->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -0700656 if (s->cipher_list_by_id != NULL) sk_SSL_CIPHER_free(s->cipher_list_by_id);
657
658 /* Make the next call work :-) */
659 if (s->session != NULL)
660 {
661 ssl_clear_bad_session(s);
662 SSL_SESSION_free(s->session);
663 }
664
665 ssl_clear_cipher_ctx(s);
666 ssl_clear_hash_ctx(&s->read_hash);
667 ssl_clear_hash_ctx(&s->write_hash);
668
669 if (s->cert != NULL) ssl_cert_free(s->cert);
670 /* Free up if allocated */
671
Adam Langley95c29f32014-06-20 12:00:00 -0700672 if (s->tlsext_hostname)
673 OPENSSL_free(s->tlsext_hostname);
674 if (s->initial_ctx) SSL_CTX_free(s->initial_ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700675 if (s->tlsext_ecpointformatlist) OPENSSL_free(s->tlsext_ecpointformatlist);
676 if (s->tlsext_ellipticcurvelist) OPENSSL_free(s->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -0700677 if (s->alpn_client_proto_list)
678 OPENSSL_free(s->alpn_client_proto_list);
Adam Langley1258b6a2014-06-20 12:00:00 -0700679 if (s->tlsext_channel_id_private)
680 EVP_PKEY_free(s->tlsext_channel_id_private);
Adam Langley95c29f32014-06-20 12:00:00 -0700681
Adam Langley0289c732014-06-20 12:00:00 -0700682 if (s->psk_identity_hint)
683 OPENSSL_free(s->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -0700684
Adam Langley95c29f32014-06-20 12:00:00 -0700685 if (s->client_CA != NULL)
686 sk_X509_NAME_pop_free(s->client_CA,X509_NAME_free);
687
688 if (s->method != NULL) s->method->ssl_free(s);
689
690 if (s->ctx) SSL_CTX_free(s->ctx);
691
Adam Langley95c29f32014-06-20 12:00:00 -0700692 if (s->next_proto_negotiated)
693 OPENSSL_free(s->next_proto_negotiated);
Adam Langley95c29f32014-06-20 12:00:00 -0700694
695 if (s->srtp_profiles)
696 sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
697
David Benjamin1b965262014-08-13 20:06:29 -0400698 if (s->tlsext_session_ticket)
699 {
700 OPENSSL_free(s->tlsext_session_ticket);
701 }
702
Adam Langley95c29f32014-06-20 12:00:00 -0700703 OPENSSL_free(s);
704 }
705
706void SSL_set_bio(SSL *s,BIO *rbio,BIO *wbio)
707 {
708 /* If the output buffering BIO is still in place, remove it
709 */
710 if (s->bbio != NULL)
711 {
712 if (s->wbio == s->bbio)
713 {
714 s->wbio=s->wbio->next_bio;
715 s->bbio->next_bio=NULL;
716 }
717 }
718 if ((s->rbio != NULL) && (s->rbio != rbio))
719 BIO_free_all(s->rbio);
720 if ((s->wbio != NULL) && (s->wbio != wbio) && (s->rbio != s->wbio))
721 BIO_free_all(s->wbio);
722 s->rbio=rbio;
723 s->wbio=wbio;
724 }
725
726BIO *SSL_get_rbio(const SSL *s)
727 { return(s->rbio); }
728
729BIO *SSL_get_wbio(const SSL *s)
730 { return(s->wbio); }
731
732int SSL_get_fd(const SSL *s)
733 {
734 return(SSL_get_rfd(s));
735 }
736
737int SSL_get_rfd(const SSL *s)
738 {
739 int ret= -1;
740 BIO *b,*r;
741
742 b=SSL_get_rbio(s);
743 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
744 if (r != NULL)
745 BIO_get_fd(r,&ret);
746 return(ret);
747 }
748
749int SSL_get_wfd(const SSL *s)
750 {
751 int ret= -1;
752 BIO *b,*r;
753
754 b=SSL_get_wbio(s);
755 r=BIO_find_type(b,BIO_TYPE_DESCRIPTOR);
756 if (r != NULL)
757 BIO_get_fd(r,&ret);
758 return(ret);
759 }
760
761#ifndef OPENSSL_NO_SOCK
762int SSL_set_fd(SSL *s,int fd)
763 {
764 int ret=0;
765 BIO *bio=NULL;
766
767 bio=BIO_new(BIO_s_fd());
768
769 if (bio == NULL)
770 {
771 OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
772 goto err;
773 }
774 BIO_set_fd(bio,fd,BIO_NOCLOSE);
775 SSL_set_bio(s,bio,bio);
776 ret=1;
777err:
778 return(ret);
779 }
780
781int SSL_set_wfd(SSL *s,int fd)
782 {
783 int ret=0;
784 BIO *bio=NULL;
785
786 if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_FD)
787 || ((int)BIO_get_fd(s->rbio,NULL) != fd))
788 {
789 bio=BIO_new(BIO_s_fd());
790
791 if (bio == NULL)
792 {
793 OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
794 goto err;
795 }
796 BIO_set_fd(bio,fd,BIO_NOCLOSE);
797 SSL_set_bio(s,SSL_get_rbio(s),bio);
798 }
799 else
800 SSL_set_bio(s,SSL_get_rbio(s),SSL_get_rbio(s));
801 ret=1;
802err:
803 return(ret);
804 }
805
806int SSL_set_rfd(SSL *s,int fd)
807 {
808 int ret=0;
809 BIO *bio=NULL;
810
811 if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_FD)
812 || ((int)BIO_get_fd(s->wbio,NULL) != fd))
813 {
814 bio=BIO_new(BIO_s_fd());
815
816 if (bio == NULL)
817 {
818 OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
819 goto err;
820 }
821 BIO_set_fd(bio,fd,BIO_NOCLOSE);
822 SSL_set_bio(s,bio,SSL_get_wbio(s));
823 }
824 else
825 SSL_set_bio(s,SSL_get_wbio(s),SSL_get_wbio(s));
826 ret=1;
827err:
828 return(ret);
829 }
830#endif
831
832
833/* return length of latest Finished message we sent, copy to 'buf' */
834size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
835 {
836 size_t ret = 0;
837
838 if (s->s3 != NULL)
839 {
840 ret = s->s3->tmp.finish_md_len;
841 if (count > ret)
842 count = ret;
843 memcpy(buf, s->s3->tmp.finish_md, count);
844 }
845 return ret;
846 }
847
848/* return length of latest Finished message we expected, copy to 'buf' */
849size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
850 {
851 size_t ret = 0;
852
853 if (s->s3 != NULL)
854 {
855 ret = s->s3->tmp.peer_finish_md_len;
856 if (count > ret)
857 count = ret;
858 memcpy(buf, s->s3->tmp.peer_finish_md, count);
859 }
860 return ret;
861 }
862
863
864int SSL_get_verify_mode(const SSL *s)
865 {
866 return(s->verify_mode);
867 }
868
869int SSL_get_verify_depth(const SSL *s)
870 {
871 return X509_VERIFY_PARAM_get_depth(s->param);
872 }
873
874int (*SSL_get_verify_callback(const SSL *s))(int,X509_STORE_CTX *)
875 {
876 return(s->verify_callback);
877 }
878
879int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
880 {
881 return(ctx->verify_mode);
882 }
883
884int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
885 {
886 return X509_VERIFY_PARAM_get_depth(ctx->param);
887 }
888
889int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int,X509_STORE_CTX *)
890 {
891 return(ctx->default_verify_callback);
892 }
893
894void SSL_set_verify(SSL *s,int mode,
895 int (*callback)(int ok,X509_STORE_CTX *ctx))
896 {
897 s->verify_mode=mode;
898 if (callback != NULL)
899 s->verify_callback=callback;
900 }
901
902void SSL_set_verify_depth(SSL *s,int depth)
903 {
904 X509_VERIFY_PARAM_set_depth(s->param, depth);
905 }
906
907void SSL_set_read_ahead(SSL *s,int yes)
908 {
909 s->read_ahead=yes;
910 }
911
912int SSL_get_read_ahead(const SSL *s)
913 {
914 return(s->read_ahead);
915 }
916
917int SSL_pending(const SSL *s)
918 {
919 /* SSL_pending cannot work properly if read-ahead is enabled
920 * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)),
921 * and it is impossible to fix since SSL_pending cannot report
922 * errors that may be observed while scanning the new data.
923 * (Note that SSL_pending() is often used as a boolean value,
924 * so we'd better not return -1.)
925 */
926 return(s->method->ssl_pending(s));
927 }
928
929X509 *SSL_get_peer_certificate(const SSL *s)
930 {
931 X509 *r;
932
933 if ((s == NULL) || (s->session == NULL))
934 r=NULL;
935 else
936 r=s->session->peer;
937
David Benjamin150c6172014-08-05 22:22:49 -0400938 if (r == NULL)
939 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700940
David Benjamin150c6172014-08-05 22:22:49 -0400941 return X509_up_ref(r);
Adam Langley95c29f32014-06-20 12:00:00 -0700942 }
943
944STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
945 {
946 STACK_OF(X509) *r;
947
948 if ((s == NULL) || (s->session == NULL) || (s->session->sess_cert == NULL))
949 r=NULL;
950 else
951 r=s->session->sess_cert->cert_chain;
952
953 /* If we are a client, cert_chain includes the peer's own
954 * certificate; if we are a server, it does not. */
955
956 return(r);
957 }
958
Adam Langley95c29f32014-06-20 12:00:00 -0700959/* Fix this so it checks all the valid key/cert options */
960int SSL_CTX_check_private_key(const SSL_CTX *ctx)
961 {
962 if ( (ctx == NULL) ||
963 (ctx->cert == NULL) ||
964 (ctx->cert->key->x509 == NULL))
965 {
966 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
967 return(0);
968 }
969 if (ctx->cert->key->privatekey == NULL)
970 {
971 OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
972 return(0);
973 }
974 return(X509_check_private_key(ctx->cert->key->x509, ctx->cert->key->privatekey));
975 }
976
977/* Fix this function so that it takes an optional type parameter */
978int SSL_check_private_key(const SSL *ssl)
979 {
980 if (ssl == NULL)
981 {
982 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
983 return(0);
984 }
985 if (ssl->cert == NULL)
986 {
987 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
988 return 0;
989 }
990 if (ssl->cert->key->x509 == NULL)
991 {
992 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_CERTIFICATE_ASSIGNED);
993 return(0);
994 }
995 if (ssl->cert->key->privatekey == NULL)
996 {
997 OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
998 return(0);
999 }
1000 return(X509_check_private_key(ssl->cert->key->x509,
1001 ssl->cert->key->privatekey));
1002 }
1003
1004int SSL_accept(SSL *s)
1005 {
1006 if (s->handshake_func == 0)
1007 /* Not properly initialized yet */
1008 SSL_set_accept_state(s);
1009
1010 return(s->method->ssl_accept(s));
1011 }
1012
1013int SSL_connect(SSL *s)
1014 {
1015 if (s->handshake_func == 0)
1016 /* Not properly initialized yet */
1017 SSL_set_connect_state(s);
1018
1019 return(s->method->ssl_connect(s));
1020 }
1021
1022long SSL_get_default_timeout(const SSL *s)
1023 {
David Benjaminf4501342014-08-14 17:24:37 -04001024 return SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001025 }
1026
1027int SSL_read(SSL *s,void *buf,int num)
1028 {
1029 if (s->handshake_func == 0)
1030 {
1031 OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
1032 return -1;
1033 }
1034
1035 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1036 {
1037 s->rwstate=SSL_NOTHING;
1038 return(0);
1039 }
1040 return(s->method->ssl_read(s,buf,num));
1041 }
1042
1043int SSL_peek(SSL *s,void *buf,int num)
1044 {
1045 if (s->handshake_func == 0)
1046 {
1047 OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
1048 return -1;
1049 }
1050
1051 if (s->shutdown & SSL_RECEIVED_SHUTDOWN)
1052 {
1053 return(0);
1054 }
1055 return(s->method->ssl_peek(s,buf,num));
1056 }
1057
1058int SSL_write(SSL *s,const void *buf,int num)
1059 {
1060 if (s->handshake_func == 0)
1061 {
1062 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
1063 return -1;
1064 }
1065
1066 if (s->shutdown & SSL_SENT_SHUTDOWN)
1067 {
1068 s->rwstate=SSL_NOTHING;
1069 OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
1070 return(-1);
1071 }
1072 return(s->method->ssl_write(s,buf,num));
1073 }
1074
1075int SSL_shutdown(SSL *s)
1076 {
1077 /* Note that this function behaves differently from what one might
1078 * expect. Return values are 0 for no success (yet),
1079 * 1 for success; but calling it once is usually not enough,
1080 * even if blocking I/O is used (see ssl3_shutdown).
1081 */
1082
1083 if (s->handshake_func == 0)
1084 {
1085 OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
1086 return -1;
1087 }
1088
Adam Langleyb2cb0ec2014-09-02 14:28:49 -07001089 if (!SSL_in_init(s))
Adam Langley95c29f32014-06-20 12:00:00 -07001090 return(s->method->ssl_shutdown(s));
1091 else
1092 return(1);
1093 }
1094
1095int SSL_renegotiate(SSL *s)
1096 {
1097 if (s->renegotiate == 0)
1098 s->renegotiate=1;
1099
1100 s->new_session=1;
1101
1102 return(s->method->ssl_renegotiate(s));
1103 }
1104
1105int SSL_renegotiate_abbreviated(SSL *s)
1106 {
1107 if (s->renegotiate == 0)
1108 s->renegotiate=1;
1109
1110 s->new_session=0;
1111
1112 return(s->method->ssl_renegotiate(s));
1113 }
1114
1115int SSL_renegotiate_pending(SSL *s)
1116 {
1117 /* becomes true when negotiation is requested;
1118 * false again once a handshake has finished */
1119 return (s->renegotiate != 0);
1120 }
1121
1122long SSL_ctrl(SSL *s,int cmd,long larg,void *parg)
1123 {
1124 long l;
1125
1126 switch (cmd)
1127 {
1128 case SSL_CTRL_GET_READ_AHEAD:
1129 return(s->read_ahead);
1130 case SSL_CTRL_SET_READ_AHEAD:
1131 l=s->read_ahead;
1132 s->read_ahead=larg;
1133 return(l);
1134
1135 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1136 s->msg_callback_arg = parg;
1137 return 1;
1138
1139 case SSL_CTRL_OPTIONS:
1140 return(s->options|=larg);
1141 case SSL_CTRL_CLEAR_OPTIONS:
1142 return(s->options&=~larg);
1143 case SSL_CTRL_MODE:
1144 return(s->mode|=larg);
1145 case SSL_CTRL_CLEAR_MODE:
1146 return(s->mode &=~larg);
1147 case SSL_CTRL_GET_MAX_CERT_LIST:
1148 return(s->max_cert_list);
1149 case SSL_CTRL_SET_MAX_CERT_LIST:
1150 l=s->max_cert_list;
1151 s->max_cert_list=larg;
1152 return(l);
1153 case SSL_CTRL_SET_MTU:
1154#ifndef OPENSSL_NO_DTLS1
1155 if (larg < (long)dtls1_min_mtu())
1156 return 0;
1157#endif
1158
1159 if (SSL_IS_DTLS(s))
1160 {
1161 s->d1->mtu = larg;
1162 return larg;
1163 }
1164 return 0;
1165 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1166 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1167 return 0;
1168 s->max_send_fragment = larg;
1169 return 1;
1170 case SSL_CTRL_GET_RI_SUPPORT:
1171 if (s->s3)
1172 return s->s3->send_connection_binding;
1173 else return 0;
1174 case SSL_CTRL_CERT_FLAGS:
1175 return(s->cert->cert_flags|=larg);
1176 case SSL_CTRL_CLEAR_CERT_FLAGS:
1177 return(s->cert->cert_flags &=~larg);
1178
1179 case SSL_CTRL_GET_RAW_CIPHERLIST:
1180 if (parg)
1181 {
1182 if (s->cert->ciphers_raw == NULL)
1183 return 0;
1184 *(unsigned char **)parg = s->cert->ciphers_raw;
1185 return (int)s->cert->ciphers_rawlen;
1186 }
1187 else
David Benjamin39482a12014-07-20 13:30:15 -04001188 {
1189 /* Passing a NULL |parg| returns the size of a single
1190 * cipher suite value. */
1191 return 2;
1192 }
Adam Langley95c29f32014-06-20 12:00:00 -07001193 default:
1194 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1195 }
1196 }
1197
1198long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1199 {
1200 switch(cmd)
1201 {
1202 case SSL_CTRL_SET_MSG_CALLBACK:
1203 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1204 return 1;
1205
1206 default:
1207 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1208 }
1209 }
1210
1211LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1212 {
1213 return ctx->sessions;
1214 }
1215
1216long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1217 {
1218 long l;
1219
1220 switch (cmd)
1221 {
1222 case SSL_CTRL_GET_READ_AHEAD:
1223 return(ctx->read_ahead);
1224 case SSL_CTRL_SET_READ_AHEAD:
1225 l=ctx->read_ahead;
1226 ctx->read_ahead=larg;
1227 return(l);
1228
1229 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1230 ctx->msg_callback_arg = parg;
1231 return 1;
1232
1233 case SSL_CTRL_GET_MAX_CERT_LIST:
1234 return(ctx->max_cert_list);
1235 case SSL_CTRL_SET_MAX_CERT_LIST:
1236 l=ctx->max_cert_list;
1237 ctx->max_cert_list=larg;
1238 return(l);
1239
1240 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1241 l=ctx->session_cache_size;
1242 ctx->session_cache_size=larg;
1243 return(l);
1244 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1245 return(ctx->session_cache_size);
1246 case SSL_CTRL_SET_SESS_CACHE_MODE:
1247 l=ctx->session_cache_mode;
1248 ctx->session_cache_mode=larg;
1249 return(l);
1250 case SSL_CTRL_GET_SESS_CACHE_MODE:
1251 return(ctx->session_cache_mode);
1252
1253 case SSL_CTRL_SESS_NUMBER:
1254 return(lh_SSL_SESSION_num_items(ctx->sessions));
1255 case SSL_CTRL_SESS_CONNECT:
1256 return(ctx->stats.sess_connect);
1257 case SSL_CTRL_SESS_CONNECT_GOOD:
1258 return(ctx->stats.sess_connect_good);
1259 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1260 return(ctx->stats.sess_connect_renegotiate);
1261 case SSL_CTRL_SESS_ACCEPT:
1262 return(ctx->stats.sess_accept);
1263 case SSL_CTRL_SESS_ACCEPT_GOOD:
1264 return(ctx->stats.sess_accept_good);
1265 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1266 return(ctx->stats.sess_accept_renegotiate);
1267 case SSL_CTRL_SESS_HIT:
1268 return(ctx->stats.sess_hit);
1269 case SSL_CTRL_SESS_CB_HIT:
1270 return(ctx->stats.sess_cb_hit);
1271 case SSL_CTRL_SESS_MISSES:
1272 return(ctx->stats.sess_miss);
1273 case SSL_CTRL_SESS_TIMEOUTS:
1274 return(ctx->stats.sess_timeout);
1275 case SSL_CTRL_SESS_CACHE_FULL:
1276 return(ctx->stats.sess_cache_full);
1277 case SSL_CTRL_OPTIONS:
1278 return(ctx->options|=larg);
1279 case SSL_CTRL_CLEAR_OPTIONS:
1280 return(ctx->options&=~larg);
1281 case SSL_CTRL_MODE:
1282 return(ctx->mode|=larg);
1283 case SSL_CTRL_CLEAR_MODE:
1284 return(ctx->mode&=~larg);
1285 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1286 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1287 return 0;
1288 ctx->max_send_fragment = larg;
1289 return 1;
1290 case SSL_CTRL_CERT_FLAGS:
1291 return(ctx->cert->cert_flags|=larg);
1292 case SSL_CTRL_CLEAR_CERT_FLAGS:
1293 return(ctx->cert->cert_flags &=~larg);
1294 default:
1295 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1296 }
1297 }
1298
1299long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1300 {
1301 switch(cmd)
1302 {
1303 case SSL_CTRL_SET_MSG_CALLBACK:
1304 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1305 return 1;
1306
1307 default:
1308 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1309 }
1310 }
1311
1312int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1313 {
1314 long l;
1315 const SSL_CIPHER *a = in_a;
1316 const SSL_CIPHER *b = in_b;
1317 const long a_id = a->id;
1318 const long b_id = b->id;
1319
1320 l = a_id - b_id;
1321 if (l == 0L)
1322 return(0);
1323 else
1324 return((l > 0)?1:-1);
1325 }
1326
1327int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1328 {
1329 long l;
1330 const long a_id = (*ap)->id;
1331 const long b_id = (*bp)->id;
1332
1333 l = a_id - b_id;
1334 if (l == 0)
1335 return(0);
1336 else
1337 return((l > 0)?1:-1);
1338 }
1339
1340/** return a STACK of the ciphers available for the SSL and in order of
1341 * preference */
1342STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1343 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001344 if (s == NULL)
1345 return NULL;
1346
1347 if (s->cipher_list != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001348 {
Adam Langley858a88d2014-06-20 12:00:00 -07001349 return(s->cipher_list->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -07001350 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001351
1352 if (s->version >= TLS1_1_VERSION)
1353 {
1354 if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001355 return s->ctx->cipher_list_tls11->ciphers;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001356 }
1357
1358 if ((s->ctx != NULL) &&
1359 (s->ctx->cipher_list != NULL))
1360 {
Adam Langley858a88d2014-06-20 12:00:00 -07001361 return(s->ctx->cipher_list->ciphers);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001362 }
1363
Adam Langley95c29f32014-06-20 12:00:00 -07001364 return(NULL);
1365 }
1366
1367/** return a STACK of the ciphers available for the SSL and in order of
1368 * algorithm id */
1369STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1370 {
1371 if (s != NULL)
1372 {
1373 if (s->cipher_list_by_id != NULL)
1374 {
1375 return(s->cipher_list_by_id);
1376 }
1377 else if ((s->ctx != NULL) &&
1378 (s->ctx->cipher_list_by_id != NULL))
1379 {
1380 return(s->ctx->cipher_list_by_id);
1381 }
1382 }
1383 return(NULL);
1384 }
1385
1386/** The old interface to get the same thing as SSL_get_ciphers() */
1387const char *SSL_get_cipher_list(const SSL *s,int n)
1388 {
David Benjamin6f260012014-08-15 13:49:12 -04001389 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001390 STACK_OF(SSL_CIPHER) *sk;
1391
1392 if (s == NULL) return(NULL);
1393 sk=SSL_get_ciphers(s);
1394 if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
1395 return(NULL);
1396 c=sk_SSL_CIPHER_value(sk,n);
1397 if (c == NULL) return(NULL);
1398 return(c->name);
1399 }
1400
1401/** specify the ciphers to be used by default by the SSL_CTX */
1402int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1403 {
1404 STACK_OF(SSL_CIPHER) *sk;
1405
1406 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1407 &ctx->cipher_list_by_id,str, ctx->cert);
1408 /* ssl_create_cipher_list may return an empty stack if it
1409 * was unable to find a cipher matching the given rule string
1410 * (for example if the rule string specifies a cipher which
1411 * has been disabled). This is not an error as far as
1412 * ssl_create_cipher_list is concerned, and hence
1413 * ctx->cipher_list and ctx->cipher_list_by_id has been
1414 * updated. */
1415 if (sk == NULL)
1416 return 0;
1417 else if (sk_SSL_CIPHER_num(sk) == 0)
1418 {
1419 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1420 return 0;
1421 }
1422 return 1;
1423 }
1424
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001425int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str)
1426 {
1427 STACK_OF(SSL_CIPHER) *sk;
1428
1429 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert);
1430 if (sk == NULL)
1431 return 0;
1432 else if (sk_SSL_CIPHER_num(sk) == 0)
1433 {
1434 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH);
1435 return 0;
1436 }
1437 return 1;
1438 }
1439
Adam Langley95c29f32014-06-20 12:00:00 -07001440/** specify the ciphers to be used by the SSL */
1441int SSL_set_cipher_list(SSL *s,const char *str)
1442 {
1443 STACK_OF(SSL_CIPHER) *sk;
1444
1445 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1446 &s->cipher_list_by_id,str, s->cert);
1447 /* see comment in SSL_CTX_set_cipher_list */
1448 if (sk == NULL)
1449 return 0;
1450 else if (sk_SSL_CIPHER_num(sk) == 0)
1451 {
1452 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1453 return 0;
1454 }
1455 return 1;
1456 }
1457
1458/* works well for SSLv2, not so good for SSLv3 */
1459char *SSL_get_shared_ciphers(const SSL *s,char *buf,int len)
1460 {
1461 char *p;
1462 STACK_OF(SSL_CIPHER) *sk;
David Benjamin6f260012014-08-15 13:49:12 -04001463 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001464 int i;
1465
1466 if ((s->session == NULL) || (s->session->ciphers == NULL) ||
1467 (len < 2))
1468 return(NULL);
1469
1470 p=buf;
1471 sk=s->session->ciphers;
Adam Langley04124042014-06-20 12:00:00 -07001472
1473 if (sk_SSL_CIPHER_num(sk) == 0)
1474 return NULL;
1475
Adam Langley95c29f32014-06-20 12:00:00 -07001476 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1477 {
1478 int n;
1479
1480 c=sk_SSL_CIPHER_value(sk,i);
1481 n=strlen(c->name);
1482 if (n+1 > len)
1483 {
1484 if (p != buf)
1485 --p;
1486 *p='\0';
1487 return buf;
1488 }
1489 strcpy(p,c->name);
1490 p+=n;
1491 *(p++)=':';
1492 len-=n+1;
1493 }
1494 p[-1]='\0';
1495 return(buf);
1496 }
1497
David Benjamin39482a12014-07-20 13:30:15 -04001498int ssl_cipher_list_to_bytes(SSL *s,STACK_OF(SSL_CIPHER) *sk,unsigned char *p)
Adam Langley95c29f32014-06-20 12:00:00 -07001499 {
David Benjamin39482a12014-07-20 13:30:15 -04001500 int i;
David Benjamin6f260012014-08-15 13:49:12 -04001501 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001502 CERT *ct = s->cert;
1503 unsigned char *q;
1504 int no_scsv = s->renegotiate;
1505 /* Set disabled masks for this session */
1506 ssl_set_client_disabled(s);
1507
1508 if (sk == NULL) return(0);
1509 q=p;
1510
1511 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1512 {
1513 c=sk_SSL_CIPHER_value(sk,i);
1514 /* Skip disabled ciphers */
1515 if (c->algorithm_ssl & ct->mask_ssl ||
1516 c->algorithm_mkey & ct->mask_k ||
1517 c->algorithm_auth & ct->mask_a)
1518 continue;
1519#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
1520 if (c->id == SSL3_CK_SCSV)
1521 {
1522 if (no_scsv)
1523 continue;
1524 else
1525 no_scsv = 1;
1526 }
1527#endif
David Benjamin39482a12014-07-20 13:30:15 -04001528 s2n(ssl3_get_cipher_value(c), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001529 }
1530 /* If p == q, no ciphers and caller indicates an error. Otherwise
1531 * add SCSV if not renegotiating.
1532 */
Adam Langleyac61fa32014-06-23 12:03:11 -07001533 if (p != q)
Adam Langley95c29f32014-06-20 12:00:00 -07001534 {
Adam Langleyac61fa32014-06-23 12:03:11 -07001535 if (!no_scsv)
Adam Langley95c29f32014-06-20 12:00:00 -07001536 {
David Benjamin6f260012014-08-15 13:49:12 -04001537 static const SSL_CIPHER scsv =
Adam Langleyac61fa32014-06-23 12:03:11 -07001538 {
1539 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1540 };
David Benjamin39482a12014-07-20 13:30:15 -04001541 s2n(ssl3_get_cipher_value(&scsv), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001542#ifdef OPENSSL_RI_DEBUG
Adam Langleyac61fa32014-06-23 12:03:11 -07001543 fprintf(stderr, "SCSV sent by client\n");
Adam Langley95c29f32014-06-20 12:00:00 -07001544#endif
Adam Langleyac61fa32014-06-23 12:03:11 -07001545 }
1546 if (s->fallback_scsv)
1547 {
David Benjamin6f260012014-08-15 13:49:12 -04001548 static const SSL_CIPHER fallback_scsv =
Adam Langleyac61fa32014-06-23 12:03:11 -07001549 {
1550 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
1551 };
David Benjamin39482a12014-07-20 13:30:15 -04001552 s2n(ssl3_get_cipher_value(&fallback_scsv), p);
Adam Langleyac61fa32014-06-23 12:03:11 -07001553 }
Adam Langley95c29f32014-06-20 12:00:00 -07001554 }
1555
1556 return(p-q);
1557 }
1558
David Benjamin39482a12014-07-20 13:30:15 -04001559STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs,
Adam Langley95c29f32014-06-20 12:00:00 -07001560 STACK_OF(SSL_CIPHER) **skp)
1561 {
David Benjamin39482a12014-07-20 13:30:15 -04001562 CBS cipher_suites = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001563 const SSL_CIPHER *c;
1564 STACK_OF(SSL_CIPHER) *sk;
David Benjamin39482a12014-07-20 13:30:15 -04001565
Adam Langley95c29f32014-06-20 12:00:00 -07001566 if (s->s3)
1567 s->s3->send_connection_binding = 0;
1568
David Benjamin39482a12014-07-20 13:30:15 -04001569 if (CBS_len(&cipher_suites) % 2 != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001570 {
1571 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
1572 return(NULL);
1573 }
1574 if ((skp == NULL) || (*skp == NULL))
1575 sk=sk_SSL_CIPHER_new_null(); /* change perhaps later */
1576 else
1577 {
1578 sk= *skp;
1579 sk_SSL_CIPHER_zero(sk);
1580 }
1581
David Benjamin39482a12014-07-20 13:30:15 -04001582 if (!CBS_stow(&cipher_suites,
1583 &s->cert->ciphers_raw, &s->cert->ciphers_rawlen))
Adam Langley95c29f32014-06-20 12:00:00 -07001584 {
1585 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1586 goto err;
1587 }
Adam Langley95c29f32014-06-20 12:00:00 -07001588
David Benjamin39482a12014-07-20 13:30:15 -04001589 while (CBS_len(&cipher_suites) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001590 {
David Benjamin39482a12014-07-20 13:30:15 -04001591 uint16_t cipher_suite;
1592
1593 if (!CBS_get_u16(&cipher_suites, &cipher_suite))
1594 {
1595 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1596 goto err;
1597 }
1598
Adam Langley95c29f32014-06-20 12:00:00 -07001599 /* Check for SCSV */
David Benjamin39482a12014-07-20 13:30:15 -04001600 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff))
Adam Langley95c29f32014-06-20 12:00:00 -07001601 {
1602 /* SCSV fatal if renegotiating */
1603 if (s->renegotiate)
1604 {
1605 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1606 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1607 goto err;
1608 }
1609 s->s3->send_connection_binding = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001610#ifdef OPENSSL_RI_DEBUG
1611 fprintf(stderr, "SCSV received by server\n");
1612#endif
1613 continue;
1614 }
1615
Adam Langley29707792014-06-20 12:00:00 -07001616 /* Check for FALLBACK_SCSV */
David Benjamin39482a12014-07-20 13:30:15 -04001617 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff) &&
Adam Langley29707792014-06-20 12:00:00 -07001618 s->version < ssl_get_max_version(s))
1619 {
1620 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1621 ssl3_send_alert(s,SSL3_AL_FATAL,SSL3_AD_INAPPROPRIATE_FALLBACK);
1622 goto err;
1623 }
1624
David Benjamin39482a12014-07-20 13:30:15 -04001625 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -07001626 if (c != NULL)
1627 {
1628 if (!sk_SSL_CIPHER_push(sk,c))
1629 {
1630 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1631 goto err;
1632 }
1633 }
1634 }
1635
1636 if (skp != NULL)
1637 *skp=sk;
1638 return(sk);
1639err:
1640 if ((skp == NULL) || (*skp == NULL))
1641 sk_SSL_CIPHER_free(sk);
1642 return(NULL);
1643 }
1644
1645
Adam Langley95c29f32014-06-20 12:00:00 -07001646/** return a servername extension value if provided in Client Hello, or NULL.
1647 * So far, only host_name types are defined (RFC 3546).
1648 */
1649
1650const char *SSL_get_servername(const SSL *s, const int type)
1651 {
1652 if (type != TLSEXT_NAMETYPE_host_name)
1653 return NULL;
1654
1655 return s->session && !s->tlsext_hostname ?
1656 s->session->tlsext_hostname :
1657 s->tlsext_hostname;
1658 }
1659
1660int SSL_get_servername_type(const SSL *s)
1661 {
1662 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1663 return TLSEXT_NAMETYPE_host_name;
1664 return -1;
1665 }
1666
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001667void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx)
1668 {
1669 ctx->signed_cert_timestamps_enabled = 1;
1670 }
1671
1672int SSL_enable_signed_cert_timestamps(SSL *ssl)
1673 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001674 /* Currently not implemented server-side. */
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001675 if (ssl->server)
1676 return 0;
1677
1678 ssl->signed_cert_timestamps_enabled = 1;
1679 return 1;
1680 }
1681
David Benjamin6c7aed02014-08-27 16:42:38 -04001682void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx)
1683 {
1684 ctx->ocsp_stapling_enabled = 1;
1685 }
1686
1687int SSL_enable_ocsp_stapling(SSL *ssl)
1688 {
1689 /* Currently not implemented server-side. */
1690 if (ssl->server)
1691 return 0;
1692 ssl->ocsp_stapling_enabled = 1;
1693 return 1;
1694 }
1695
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001696void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, uint8_t **out, size_t *out_len)
1697 {
Adam Langley3cb50e02014-08-26 14:00:31 -07001698 SSL_SESSION *session = ssl->session;
1699
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001700 *out_len = 0;
1701 *out = NULL;
1702 if (ssl->server)
1703 return;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001704 if (!session || !session->tlsext_signed_cert_timestamp_list)
1705 return;
1706 *out = session->tlsext_signed_cert_timestamp_list;
1707 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1708 }
1709
David Benjamin6c7aed02014-08-27 16:42:38 -04001710void SSL_get0_ocsp_response(const SSL *ssl, uint8_t **out, size_t *out_len)
1711 {
1712 SSL_SESSION *session = ssl->session;
1713
1714 *out_len = 0;
1715 *out = NULL;
1716 if (ssl->server)
1717 return;
1718 if (!session || !session->ocsp_response)
1719 return;
1720 *out = session->ocsp_response;
1721 *out_len = session->ocsp_response_length;
1722 }
1723
Adam Langley95c29f32014-06-20 12:00:00 -07001724/* SSL_select_next_proto implements the standard protocol selection. It is
1725 * expected that this function is called from the callback set by
1726 * SSL_CTX_set_next_proto_select_cb.
1727 *
1728 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1729 * strings. The length byte itself is not included in the length. A byte
1730 * string of length 0 is invalid. No byte string may be truncated.
1731 *
1732 * The current, but experimental algorithm for selecting the protocol is:
1733 *
1734 * 1) If the server doesn't support NPN then this is indicated to the
1735 * callback. In this case, the client application has to abort the connection
1736 * or have a default application level protocol.
1737 *
1738 * 2) If the server supports NPN, but advertises an empty list then the
1739 * client selects the first protcol in its list, but indicates via the
1740 * API that this fallback case was enacted.
1741 *
1742 * 3) Otherwise, the client finds the first protocol in the server's list
1743 * that it supports and selects this protocol. This is because it's
1744 * assumed that the server has better information about which protocol
1745 * a client should use.
1746 *
1747 * 4) If the client doesn't support any of the server's advertised
1748 * protocols, then this is treated the same as case 2.
1749 *
1750 * It returns either
1751 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1752 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1753 */
1754int 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)
1755 {
1756 unsigned int i, j;
1757 const unsigned char *result;
1758 int status = OPENSSL_NPN_UNSUPPORTED;
1759
1760 /* For each protocol in server preference order, see if we support it. */
1761 for (i = 0; i < server_len; )
1762 {
1763 for (j = 0; j < client_len; )
1764 {
1765 if (server[i] == client[j] &&
1766 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1767 {
1768 /* We found a match */
1769 result = &server[i];
1770 status = OPENSSL_NPN_NEGOTIATED;
1771 goto found;
1772 }
1773 j += client[j];
1774 j++;
1775 }
1776 i += server[i];
1777 i++;
1778 }
1779
1780 /* There's no overlap between our protocols and the server's list. */
1781 result = client;
1782 status = OPENSSL_NPN_NO_OVERLAP;
1783
1784 found:
1785 *out = (unsigned char *) result + 1;
1786 *outlen = result[0];
1787 return status;
1788 }
1789
Adam Langley95c29f32014-06-20 12:00:00 -07001790/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1791 * requested protocol for this connection and returns 0. If the client didn't
1792 * request any protocol, then *data is set to NULL.
1793 *
1794 * Note that the client can request any protocol it chooses. The value returned
1795 * from this function need not be a member of the list of supported protocols
1796 * provided by the callback.
1797 */
David Benjaminced551f2014-07-11 23:16:16 -04001798void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, unsigned *len)
Adam Langley95c29f32014-06-20 12:00:00 -07001799 {
1800 *data = s->next_proto_negotiated;
1801 if (!*data) {
1802 *len = 0;
1803 } else {
1804 *len = s->next_proto_negotiated_len;
1805 }
1806}
1807
1808/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1809 * TLS server needs a list of supported protocols for Next Protocol
1810 * Negotiation. The returned list must be in wire format. The list is returned
1811 * by setting |out| to point to it and |outlen| to its length. This memory will
1812 * not be modified, but one should assume that the SSL* keeps a reference to
1813 * it.
1814 *
1815 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1816 * such extension will be included in the ServerHello. */
1817void 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)
1818 {
1819 ctx->next_protos_advertised_cb = cb;
1820 ctx->next_protos_advertised_cb_arg = arg;
1821 }
1822
1823/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1824 * client needs to select a protocol from the server's provided list. |out|
1825 * must be set to point to the selected protocol (which may be within |in|).
1826 * The length of the protocol name must be written into |outlen|. The server's
1827 * advertised protocols are provided in |in| and |inlen|. The callback can
1828 * assume that |in| is syntactically valid.
1829 *
1830 * The client must select a protocol. It is fatal to the connection if this
1831 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1832 */
1833void 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)
1834 {
1835 ctx->next_proto_select_cb = cb;
1836 ctx->next_proto_select_cb_arg = arg;
1837 }
Adam Langley95c29f32014-06-20 12:00:00 -07001838
Adam Langley95c29f32014-06-20 12:00:00 -07001839/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1840 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1841 * length-prefixed strings).
1842 *
1843 * Returns 0 on success. */
1844int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1845 unsigned protos_len)
1846 {
1847 if (ctx->alpn_client_proto_list)
1848 OPENSSL_free(ctx->alpn_client_proto_list);
1849
David Benjamin072c9532014-07-26 11:44:25 -04001850 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001851 if (!ctx->alpn_client_proto_list)
1852 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001853 ctx->alpn_client_proto_list_len = protos_len;
1854
1855 return 0;
1856 }
1857
1858/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1859 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1860 * length-prefixed strings).
1861 *
1862 * Returns 0 on success. */
1863int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1864 unsigned protos_len)
1865 {
1866 if (ssl->alpn_client_proto_list)
1867 OPENSSL_free(ssl->alpn_client_proto_list);
1868
David Benjamin072c9532014-07-26 11:44:25 -04001869 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001870 if (!ssl->alpn_client_proto_list)
1871 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001872 ssl->alpn_client_proto_list_len = protos_len;
1873
1874 return 0;
1875 }
1876
1877/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1878 * during ClientHello processing in order to select an ALPN protocol from the
1879 * client's list of offered protocols. */
1880void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1881 int (*cb) (SSL *ssl,
1882 const unsigned char **out,
1883 unsigned char *outlen,
1884 const unsigned char *in,
1885 unsigned int inlen,
1886 void *arg),
1887 void *arg)
1888 {
1889 ctx->alpn_select_cb = cb;
1890 ctx->alpn_select_cb_arg = arg;
1891 }
1892
1893/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1894 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1895 * including the leading length-prefix byte). If the server didn't respond with
1896 * a negotiated protocol then |*len| will be zero. */
1897void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1898 unsigned *len)
1899 {
1900 *data = NULL;
1901 if (ssl->s3)
1902 *data = ssl->s3->alpn_selected;
1903 if (*data == NULL)
1904 *len = 0;
1905 else
1906 *len = ssl->s3->alpn_selected_len;
1907 }
Adam Langley95c29f32014-06-20 12:00:00 -07001908
1909int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1910 const char *label, size_t llen, const unsigned char *p, size_t plen,
1911 int use_context)
1912 {
1913 if (s->version < TLS1_VERSION)
1914 return -1;
1915
1916 return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1917 llen, p, plen,
1918 use_context);
1919 }
1920
1921static uint32_t ssl_session_hash(const SSL_SESSION *a)
1922 {
1923 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1924 ((uint32_t) a->session_id[1] << 8) ||
1925 ((uint32_t) a->session_id[2] << 16) ||
1926 ((uint32_t) a->session_id[3] << 24);
1927
1928 return hash;
1929 }
1930
1931/* NB: If this function (or indeed the hash function which uses a sort of
1932 * coarser function than this one) is changed, ensure
1933 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1934 * able to construct an SSL_SESSION that will collide with any existing session
1935 * with a matching session ID. */
1936static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1937 {
1938 if (a->ssl_version != b->ssl_version)
1939 return(1);
1940 if (a->session_id_length != b->session_id_length)
1941 return(1);
1942 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1943 }
1944
1945SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1946 {
1947 SSL_CTX *ret=NULL;
1948
1949 if (meth == NULL)
1950 {
1951 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1952 return(NULL);
1953 }
1954
Adam Langley95c29f32014-06-20 12:00:00 -07001955 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1956 {
1957 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1958 goto err;
1959 }
1960 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1961 if (ret == NULL)
1962 goto err;
1963
1964 memset(ret,0,sizeof(SSL_CTX));
1965
1966 ret->method=meth;
1967
1968 ret->cert_store=NULL;
1969 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1970 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1971 ret->session_cache_head=NULL;
1972 ret->session_cache_tail=NULL;
1973
1974 /* We take the system default */
David Benjaminf4501342014-08-14 17:24:37 -04001975 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001976
1977 ret->new_session_cb=0;
1978 ret->remove_session_cb=0;
1979 ret->get_session_cb=0;
1980 ret->generate_session_id=0;
1981
1982 memset((char *)&ret->stats,0,sizeof(ret->stats));
1983
1984 ret->references=1;
1985 ret->quiet_shutdown=0;
1986
Adam Langley95c29f32014-06-20 12:00:00 -07001987 ret->info_callback=NULL;
1988
1989 ret->app_verify_callback=0;
1990 ret->app_verify_arg=NULL;
1991
1992 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1993 ret->read_ahead=0;
1994 ret->msg_callback=0;
1995 ret->msg_callback_arg=NULL;
1996 ret->verify_mode=SSL_VERIFY_NONE;
1997#if 0
1998 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1999#endif
2000 ret->sid_ctx_length=0;
2001 ret->default_verify_callback=NULL;
2002 if ((ret->cert=ssl_cert_new()) == NULL)
2003 goto err;
2004
2005 ret->default_passwd_callback=0;
2006 ret->default_passwd_callback_userdata=NULL;
2007 ret->client_cert_cb=0;
2008 ret->app_gen_cookie_cb=0;
2009 ret->app_verify_cookie_cb=0;
2010
2011 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
2012 if (ret->sessions == NULL) goto err;
2013 ret->cert_store=X509_STORE_new();
2014 if (ret->cert_store == NULL) goto err;
2015
2016 ssl_create_cipher_list(ret->method,
2017 &ret->cipher_list,&ret->cipher_list_by_id,
2018 meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST, ret->cert);
2019 if (ret->cipher_list == NULL
Adam Langley858a88d2014-06-20 12:00:00 -07002020 || sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07002021 {
2022 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
2023 goto err2;
2024 }
2025
2026 ret->param = X509_VERIFY_PARAM_new();
2027 if (!ret->param)
2028 goto err;
2029
Adam Langley95c29f32014-06-20 12:00:00 -07002030 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
2031 goto err;
2032
2033 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
2034
2035 ret->extra_certs=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002036
2037 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
2038
Adam Langley95c29f32014-06-20 12:00:00 -07002039 ret->tlsext_servername_callback = 0;
2040 ret->tlsext_servername_arg = NULL;
2041 /* Setup RFC4507 ticket keys */
2042 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
2043 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
2044 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
2045 ret->options |= SSL_OP_NO_TICKET;
2046
2047 ret->tlsext_status_cb = 0;
2048 ret->tlsext_status_arg = NULL;
2049
Adam Langley95c29f32014-06-20 12:00:00 -07002050 ret->next_protos_advertised_cb = 0;
2051 ret->next_proto_select_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002052 ret->psk_identity_hint=NULL;
2053 ret->psk_client_callback=NULL;
2054 ret->psk_server_callback=NULL;
David Benjamin422d3a42014-08-20 11:09:03 -04002055
Adam Langley95c29f32014-06-20 12:00:00 -07002056 /* Default is to connect to non-RI servers. When RI is more widely
2057 * deployed might change this.
2058 */
2059 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
2060
2061 return(ret);
2062err:
2063 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
2064err2:
2065 if (ret != NULL) SSL_CTX_free(ret);
2066 return(NULL);
2067 }
2068
Adam Langley95c29f32014-06-20 12:00:00 -07002069
2070void SSL_CTX_free(SSL_CTX *a)
2071 {
2072 int i;
2073
2074 if (a == NULL) return;
2075
2076 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
2077#ifdef REF_PRINT
2078 REF_PRINT("SSL_CTX",a);
2079#endif
2080 if (i > 0) return;
2081#ifdef REF_CHECK
2082 if (i < 0)
2083 {
2084 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
2085 abort(); /* ok */
2086 }
2087#endif
2088
2089 if (a->param)
2090 X509_VERIFY_PARAM_free(a->param);
2091
2092 /*
2093 * Free internal session cache. However: the remove_cb() may reference
2094 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2095 * after the sessions were flushed.
2096 * As the ex_data handling routines might also touch the session cache,
2097 * the most secure solution seems to be: empty (flush) the cache, then
2098 * free ex_data, then finally free the cache.
2099 * (See ticket [openssl.org #212].)
2100 */
2101 if (a->sessions != NULL)
2102 SSL_CTX_flush_sessions(a,0);
2103
2104 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
2105
2106 if (a->sessions != NULL)
2107 lh_SSL_SESSION_free(a->sessions);
2108
2109 if (a->cert_store != NULL)
2110 X509_STORE_free(a->cert_store);
2111 if (a->cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002112 ssl_cipher_preference_list_free(a->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002113 if (a->cipher_list_by_id != NULL)
2114 sk_SSL_CIPHER_free(a->cipher_list_by_id);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07002115 if (a->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002116 ssl_cipher_preference_list_free(a->cipher_list_tls11);
Adam Langley95c29f32014-06-20 12:00:00 -07002117 if (a->cert != NULL)
2118 ssl_cert_free(a->cert);
2119 if (a->client_CA != NULL)
2120 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2121 if (a->extra_certs != NULL)
2122 sk_X509_pop_free(a->extra_certs,X509_free);
Adam Langley95c29f32014-06-20 12:00:00 -07002123
2124 if (a->srtp_profiles)
2125 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2126
Adam Langley95c29f32014-06-20 12:00:00 -07002127 if (a->psk_identity_hint)
2128 OPENSSL_free(a->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07002129
Adam Langley95c29f32014-06-20 12:00:00 -07002130 if (a->tlsext_ecpointformatlist)
2131 OPENSSL_free(a->tlsext_ecpointformatlist);
2132 if (a->tlsext_ellipticcurvelist)
2133 OPENSSL_free(a->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -07002134 if (a->alpn_client_proto_list != NULL)
2135 OPENSSL_free(a->alpn_client_proto_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002136
Adam Langley1258b6a2014-06-20 12:00:00 -07002137 if (a->tlsext_channel_id_private)
2138 EVP_PKEY_free(a->tlsext_channel_id_private);
Adam Langley1258b6a2014-06-20 12:00:00 -07002139
David Benjamin859ec3c2014-09-02 16:29:36 -04002140 if (a->keylog_bio)
2141 BIO_free(a->keylog_bio);
2142
Adam Langley95c29f32014-06-20 12:00:00 -07002143 OPENSSL_free(a);
2144 }
2145
2146void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2147 {
2148 ctx->default_passwd_callback=cb;
2149 }
2150
2151void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2152 {
2153 ctx->default_passwd_callback_userdata=u;
2154 }
2155
2156void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2157 {
2158 ctx->app_verify_callback=cb;
2159 ctx->app_verify_arg=arg;
2160 }
2161
2162void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2163 {
2164 ctx->verify_mode=mode;
2165 ctx->default_verify_callback=cb;
2166 }
2167
2168void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2169 {
2170 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2171 }
2172
2173void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2174 {
2175 ssl_cert_set_cert_cb(c->cert, cb, arg);
2176 }
2177
2178void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2179 {
2180 ssl_cert_set_cert_cb(s->cert, cb, arg);
2181 }
2182
2183void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
2184 {
2185 CERT_PKEY *cpk;
David Benjaminef2116d2014-08-19 20:21:56 -04002186 int rsa_enc,rsa_sign,dh_tmp;
David Benjamin060d9d22014-07-15 00:54:26 -04002187 unsigned long mask_k,mask_a;
David Benjamin060d9d22014-07-15 00:54:26 -04002188 int have_ecc_cert, ecdsa_ok;
David Benjamin0da0e182014-08-19 16:20:28 -04002189 int have_ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002190 X509 *x = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002191 if (c == NULL) return;
2192
Adam Langley95c29f32014-06-20 12:00:00 -07002193 dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002194
Adam Langley95c29f32014-06-20 12:00:00 -07002195 have_ecdh_tmp=(c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
Adam Langley95c29f32014-06-20 12:00:00 -07002196 cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
2197 rsa_enc= cpk->valid_flags & CERT_PKEY_VALID;
Adam Langley95c29f32014-06-20 12:00:00 -07002198 cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
2199 rsa_sign= cpk->valid_flags & CERT_PKEY_SIGN;
Adam Langley95c29f32014-06-20 12:00:00 -07002200 cpk= &(c->pkeys[SSL_PKEY_ECC]);
Adam Langley95c29f32014-06-20 12:00:00 -07002201 have_ecc_cert= cpk->valid_flags & CERT_PKEY_VALID;
Adam Langley95c29f32014-06-20 12:00:00 -07002202 mask_k=0;
2203 mask_a=0;
Adam Langley95c29f32014-06-20 12:00:00 -07002204
2205#ifdef CIPHER_DEBUG
2206 printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
2207 rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
2208 rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
2209#endif
2210
David Benjamin77a942b2014-07-15 01:22:50 -04002211 if (rsa_enc)
Adam Langley95c29f32014-06-20 12:00:00 -07002212 mask_k|=SSL_kRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002213
Adam Langley95c29f32014-06-20 12:00:00 -07002214 if (dh_tmp)
2215 mask_k|=SSL_kEDH;
2216
Adam Langley95c29f32014-06-20 12:00:00 -07002217 if (rsa_enc || rsa_sign)
2218 {
2219 mask_a|=SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002220 }
2221
Adam Langley95c29f32014-06-20 12:00:00 -07002222 mask_a|=SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002223
David Benjamin0da0e182014-08-19 16:20:28 -04002224 /* An ECC certificate may be usable for ECDSA cipher suites depending on
2225 * the key usage extension. */
Adam Langley95c29f32014-06-20 12:00:00 -07002226 if (have_ecc_cert)
2227 {
2228 cpk = &c->pkeys[SSL_PKEY_ECC];
2229 x = cpk->x509;
2230 /* This call populates extension flags (ex_flags) */
2231 X509_check_purpose(x, -1, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07002232 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2233 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
2234 if (!(cpk->valid_flags & CERT_PKEY_SIGN))
2235 ecdsa_ok = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002236 if (ecdsa_ok)
2237 {
2238 mask_a|=SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002239 }
Adam Langley95c29f32014-06-20 12:00:00 -07002240 }
Adam Langley95c29f32014-06-20 12:00:00 -07002241
Adam Langley95c29f32014-06-20 12:00:00 -07002242 if (have_ecdh_tmp)
2243 {
2244 mask_k|=SSL_kEECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002245 }
Adam Langley95c29f32014-06-20 12:00:00 -07002246
Adam Langley95c29f32014-06-20 12:00:00 -07002247 mask_k |= SSL_kPSK;
2248 mask_a |= SSL_aPSK;
Adam Langley95c29f32014-06-20 12:00:00 -07002249
2250 c->mask_k=mask_k;
2251 c->mask_a=mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002252 c->valid=1;
2253 }
2254
2255/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2256#define ku_reject(x, usage) \
2257 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2258
Adam Langley95c29f32014-06-20 12:00:00 -07002259
2260int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2261 {
David Benjamin0da0e182014-08-19 16:20:28 -04002262 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002263 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2264 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2265
Adam Langley95c29f32014-06-20 12:00:00 -07002266 alg_a = cs->algorithm_auth;
2267
Adam Langley95c29f32014-06-20 12:00:00 -07002268 /* This call populates the ex_flags field correctly */
2269 X509_check_purpose(x, -1, 0);
2270 if ((x->sig_alg) && (x->sig_alg->algorithm))
2271 {
2272 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2273 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2274 }
Adam Langley95c29f32014-06-20 12:00:00 -07002275 if (alg_a & SSL_aECDSA)
2276 {
2277 /* key usage, if present, must allow signing */
2278 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2279 {
2280 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2281 return 0;
2282 }
2283 }
2284
2285 return 1; /* all checks are ok */
2286 }
2287
Adam Langley95c29f32014-06-20 12:00:00 -07002288
2289static int ssl_get_server_cert_index(const SSL *s)
2290 {
2291 int idx;
2292 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2293 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2294 idx = SSL_PKEY_RSA_SIGN;
2295 if (idx == -1)
2296 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2297 return idx;
2298 }
2299
2300CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2301 {
2302 CERT *c;
2303 int i;
2304
2305 c = s->cert;
2306 ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
2307
2308#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2309 /* Broken protocol test: return last used certificate: which may
2310 * mismatch the one expected.
2311 */
2312 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2313 return c->key;
2314#endif
2315
2316 i = ssl_get_server_cert_index(s);
2317
2318 /* This may or may not be an error. */
2319 if (i < 0)
2320 return NULL;
2321
2322 /* May be NULL. */
2323 return &c->pkeys[i];
2324 }
2325
2326EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd)
2327 {
2328 unsigned long alg_a;
2329 CERT *c;
2330 int idx = -1;
2331
2332 alg_a = cipher->algorithm_auth;
2333 c=s->cert;
2334
2335#ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2336 /* Broken protocol test: use last key: which may
2337 * mismatch the one expected.
2338 */
2339 if (c->cert_flags & SSL_CERT_FLAG_BROKEN_PROTOCOL)
2340 idx = c->key - c->pkeys;
2341 else
2342#endif
2343
David Benjaminef2116d2014-08-19 20:21:56 -04002344 if (alg_a & SSL_aRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002345 {
2346 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2347 idx = SSL_PKEY_RSA_SIGN;
2348 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2349 idx = SSL_PKEY_RSA_ENC;
2350 }
2351 else if ((alg_a & SSL_aECDSA) &&
2352 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2353 idx = SSL_PKEY_ECC;
2354 if (idx == -1)
2355 {
2356 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2357 return(NULL);
2358 }
2359 if (pmd)
2360 *pmd = c->pkeys[idx].digest;
2361 return c->pkeys[idx].privatekey;
2362 }
2363
Adam Langley95c29f32014-06-20 12:00:00 -07002364void ssl_update_cache(SSL *s,int mode)
2365 {
2366 int i;
2367
2368 /* If the session_id_length is 0, we are not supposed to cache it,
2369 * and it would be rather hard to do anyway :-) */
2370 if (s->session->session_id_length == 0) return;
2371
David Benjamin041b58a2014-08-27 15:25:21 -04002372 i=s->initial_ctx->session_cache_mode;
Adam Langley95c29f32014-06-20 12:00:00 -07002373 if ((i & mode) && (!s->hit)
2374 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
David Benjamin041b58a2014-08-27 15:25:21 -04002375 || SSL_CTX_add_session(s->initial_ctx,s->session))
2376 && (s->initial_ctx->new_session_cb != NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07002377 {
2378 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
David Benjamin041b58a2014-08-27 15:25:21 -04002379 if (!s->initial_ctx->new_session_cb(s,s->session))
Adam Langley95c29f32014-06-20 12:00:00 -07002380 SSL_SESSION_free(s->session);
2381 }
2382
2383 /* auto flush every 255 connections */
2384 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2385 ((i & mode) == mode))
2386 {
2387 if ( (((mode & SSL_SESS_CACHE_CLIENT)
David Benjamin041b58a2014-08-27 15:25:21 -04002388 ?s->initial_ctx->stats.sess_connect_good
2389 :s->initial_ctx->stats.sess_accept_good) & 0xff) == 0xff)
Adam Langley95c29f32014-06-20 12:00:00 -07002390 {
David Benjamin041b58a2014-08-27 15:25:21 -04002391 SSL_CTX_flush_sessions(s->initial_ctx,(unsigned long)time(NULL));
Adam Langley95c29f32014-06-20 12:00:00 -07002392 }
2393 }
2394 }
2395
2396const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
2397 {
2398 return ctx->method;
2399 }
2400
2401const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2402 {
2403 return(s->method);
2404 }
2405
2406int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2407 {
2408 int conn= -1;
2409 int ret=1;
2410
2411 if (s->method != meth)
2412 {
2413 if (s->handshake_func != NULL)
2414 conn=(s->handshake_func == s->method->ssl_connect);
2415
2416 if (s->method->version == meth->version)
2417 s->method=meth;
2418 else
2419 {
2420 s->method->ssl_free(s);
2421 s->method=meth;
2422 ret=s->method->ssl_new(s);
2423 }
2424
2425 if (conn == 1)
2426 s->handshake_func=meth->ssl_connect;
2427 else if (conn == 0)
2428 s->handshake_func=meth->ssl_accept;
2429 }
2430 return(ret);
2431 }
2432
2433int SSL_get_error(const SSL *s,int i)
2434 {
2435 int reason;
2436 unsigned long l;
2437 BIO *bio;
2438
2439 if (i > 0) return(SSL_ERROR_NONE);
2440
2441 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2442 * etc, where we do encode the error */
2443 if ((l=ERR_peek_error()) != 0)
2444 {
2445 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2446 return(SSL_ERROR_SYSCALL);
2447 else
2448 return(SSL_ERROR_SSL);
2449 }
2450
Adam Langleyb2ce0582014-06-20 12:00:00 -07002451 if ((i < 0) && SSL_want_session(s))
2452 return(SSL_ERROR_PENDING_SESSION);
2453
Adam Langleydc9b1412014-06-20 12:00:00 -07002454 if ((i < 0) && SSL_want_certificate(s))
2455 return(SSL_ERROR_PENDING_CERTIFICATE);
2456
Adam Langley95c29f32014-06-20 12:00:00 -07002457 if ((i < 0) && SSL_want_read(s))
2458 {
2459 bio=SSL_get_rbio(s);
2460 if (BIO_should_read(bio))
2461 return(SSL_ERROR_WANT_READ);
2462 else if (BIO_should_write(bio))
2463 /* This one doesn't make too much sense ... We never try
2464 * to write to the rbio, and an application program where
2465 * rbio and wbio are separate couldn't even know what it
2466 * should wait for.
2467 * However if we ever set s->rwstate incorrectly
2468 * (so that we have SSL_want_read(s) instead of
2469 * SSL_want_write(s)) and rbio and wbio *are* the same,
2470 * this test works around that bug; so it might be safer
2471 * to keep it. */
2472 return(SSL_ERROR_WANT_WRITE);
2473 else if (BIO_should_io_special(bio))
2474 {
2475 reason=BIO_get_retry_reason(bio);
2476 if (reason == BIO_RR_CONNECT)
2477 return(SSL_ERROR_WANT_CONNECT);
2478 else if (reason == BIO_RR_ACCEPT)
2479 return(SSL_ERROR_WANT_ACCEPT);
2480 else
2481 return(SSL_ERROR_SYSCALL); /* unknown */
2482 }
2483 }
2484
2485 if ((i < 0) && SSL_want_write(s))
2486 {
2487 bio=SSL_get_wbio(s);
2488 if (BIO_should_write(bio))
2489 return(SSL_ERROR_WANT_WRITE);
2490 else if (BIO_should_read(bio))
2491 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2492 return(SSL_ERROR_WANT_READ);
2493 else if (BIO_should_io_special(bio))
2494 {
2495 reason=BIO_get_retry_reason(bio);
2496 if (reason == BIO_RR_CONNECT)
2497 return(SSL_ERROR_WANT_CONNECT);
2498 else if (reason == BIO_RR_ACCEPT)
2499 return(SSL_ERROR_WANT_ACCEPT);
2500 else
2501 return(SSL_ERROR_SYSCALL);
2502 }
2503 }
2504 if ((i < 0) && SSL_want_x509_lookup(s))
2505 {
2506 return(SSL_ERROR_WANT_X509_LOOKUP);
2507 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002508 if ((i < 0) && SSL_want_channel_id_lookup(s))
2509 {
2510 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2511 }
Adam Langley95c29f32014-06-20 12:00:00 -07002512
2513 if (i == 0)
2514 {
2515 if (s->version == SSL2_VERSION)
2516 {
2517 /* assume it is the socket being closed */
2518 return(SSL_ERROR_ZERO_RETURN);
2519 }
2520 else
2521 {
2522 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2523 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2524 return(SSL_ERROR_ZERO_RETURN);
2525 }
2526 }
2527 return(SSL_ERROR_SYSCALL);
2528 }
2529
2530int SSL_do_handshake(SSL *s)
2531 {
2532 int ret=1;
2533
2534 if (s->handshake_func == NULL)
2535 {
2536 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2537 return(-1);
2538 }
2539
2540 s->method->ssl_renegotiate_check(s);
2541
2542 if (SSL_in_init(s) || SSL_in_before(s))
2543 {
2544 ret=s->handshake_func(s);
2545 }
2546 return(ret);
2547 }
2548
2549/* For the next 2 functions, SSL_clear() sets shutdown and so
2550 * one of these calls will reset it */
2551void SSL_set_accept_state(SSL *s)
2552 {
2553 s->server=1;
2554 s->shutdown=0;
2555 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2556 s->handshake_func=s->method->ssl_accept;
2557 /* clear the current cipher */
2558 ssl_clear_cipher_ctx(s);
2559 ssl_clear_hash_ctx(&s->read_hash);
2560 ssl_clear_hash_ctx(&s->write_hash);
2561 }
2562
2563void SSL_set_connect_state(SSL *s)
2564 {
2565 s->server=0;
2566 s->shutdown=0;
2567 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2568 s->handshake_func=s->method->ssl_connect;
2569 /* clear the current cipher */
2570 ssl_clear_cipher_ctx(s);
2571 ssl_clear_hash_ctx(&s->read_hash);
2572 ssl_clear_hash_ctx(&s->write_hash);
2573 }
2574
2575int ssl_undefined_function(SSL *s)
2576 {
2577 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2578 return(0);
2579 }
2580
2581int ssl_undefined_void_function(void)
2582 {
2583 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2584 return(0);
2585 }
2586
2587int ssl_undefined_const_function(const SSL *s)
2588 {
2589 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2590 return(0);
2591 }
2592
2593SSL_METHOD *ssl_bad_method(int ver)
2594 {
2595 OPENSSL_PUT_ERROR(SSL, ssl_bad_method, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2596 return(NULL);
2597 }
2598
Adam Langley0f4746e2014-08-13 12:26:32 -07002599static const char *ssl_get_version(int version)
Adam Langley95c29f32014-06-20 12:00:00 -07002600 {
Adam Langley0f4746e2014-08-13 12:26:32 -07002601 if (version == TLS1_2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002602 return("TLSv1.2");
Adam Langley0f4746e2014-08-13 12:26:32 -07002603 else if (version == TLS1_1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002604 return("TLSv1.1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002605 else if (version == TLS1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002606 return("TLSv1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002607 else if (version == SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002608 return("SSLv3");
Adam Langley0f4746e2014-08-13 12:26:32 -07002609 else if (version == SSL2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002610 return("SSLv2");
2611 else
2612 return("unknown");
2613 }
2614
Adam Langley0f4746e2014-08-13 12:26:32 -07002615const char *SSL_get_version(const SSL *s)
2616 {
2617 return ssl_get_version(s->version);
2618 }
2619
2620const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2621 {
2622 return ssl_get_version(sess->ssl_version);
2623 }
2624
Adam Langley95c29f32014-06-20 12:00:00 -07002625void ssl_clear_cipher_ctx(SSL *s)
2626 {
2627 if (s->enc_read_ctx != NULL)
2628 {
2629 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2630 OPENSSL_free(s->enc_read_ctx);
2631 s->enc_read_ctx=NULL;
2632 }
2633 if (s->enc_write_ctx != NULL)
2634 {
2635 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2636 OPENSSL_free(s->enc_write_ctx);
2637 s->enc_write_ctx=NULL;
2638 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002639 if (s->aead_read_ctx != NULL)
2640 {
2641 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2642 OPENSSL_free(s->aead_read_ctx);
2643 s->aead_read_ctx = NULL;
2644 }
2645 if (s->aead_write_ctx != NULL)
2646 {
2647 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2648 OPENSSL_free(s->aead_write_ctx);
2649 s->aead_write_ctx = NULL;
2650 }
Adam Langley95c29f32014-06-20 12:00:00 -07002651 }
2652
2653X509 *SSL_get_certificate(const SSL *s)
2654 {
2655 if (s->cert != NULL)
2656 return(s->cert->key->x509);
2657 else
2658 return(NULL);
2659 }
2660
2661EVP_PKEY *SSL_get_privatekey(const SSL *s)
2662 {
2663 if (s->cert != NULL)
2664 return(s->cert->key->privatekey);
2665 else
2666 return(NULL);
2667 }
2668
2669X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2670 {
2671 if (ctx->cert != NULL)
2672 return ctx->cert->key->x509;
2673 else
2674 return NULL;
2675 }
2676
2677EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2678 {
2679 if (ctx->cert != NULL)
2680 return ctx->cert->key->privatekey;
2681 else
2682 return NULL ;
2683 }
2684
2685const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2686 {
2687 if ((s->session != NULL) && (s->session->cipher != NULL))
2688 return(s->session->cipher);
2689 return(NULL);
2690 }
2691const void *SSL_get_current_compression(SSL *s)
2692 {
2693 return NULL;
2694 }
2695const void *SSL_get_current_expansion(SSL *s)
2696 {
2697 return NULL;
2698 }
2699
2700int ssl_init_wbio_buffer(SSL *s,int push)
2701 {
2702 BIO *bbio;
2703
2704 if (s->bbio == NULL)
2705 {
2706 bbio=BIO_new(BIO_f_buffer());
2707 if (bbio == NULL) return(0);
2708 s->bbio=bbio;
2709 }
2710 else
2711 {
2712 bbio=s->bbio;
2713 if (s->bbio == s->wbio)
2714 s->wbio=BIO_pop(s->wbio);
2715 }
2716 (void)BIO_reset(bbio);
2717/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2718 if (!BIO_set_read_buffer_size(bbio,1))
2719 {
2720 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2721 return(0);
2722 }
2723 if (push)
2724 {
2725 if (s->wbio != bbio)
2726 s->wbio=BIO_push(bbio,s->wbio);
2727 }
2728 else
2729 {
2730 if (s->wbio == bbio)
2731 s->wbio=BIO_pop(bbio);
2732 }
2733 return(1);
2734 }
2735
2736void ssl_free_wbio_buffer(SSL *s)
2737 {
2738 if (s->bbio == NULL) return;
2739
2740 if (s->bbio == s->wbio)
2741 {
2742 /* remove buffering */
2743 s->wbio=BIO_pop(s->wbio);
2744#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2745 assert(s->wbio != NULL);
2746#endif
2747 }
2748 BIO_free(s->bbio);
2749 s->bbio=NULL;
2750 }
2751
2752void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2753 {
2754 ctx->quiet_shutdown=mode;
2755 }
2756
2757int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2758 {
2759 return(ctx->quiet_shutdown);
2760 }
2761
2762void SSL_set_quiet_shutdown(SSL *s,int mode)
2763 {
2764 s->quiet_shutdown=mode;
2765 }
2766
2767int SSL_get_quiet_shutdown(const SSL *s)
2768 {
2769 return(s->quiet_shutdown);
2770 }
2771
2772void SSL_set_shutdown(SSL *s,int mode)
2773 {
2774 s->shutdown=mode;
2775 }
2776
2777int SSL_get_shutdown(const SSL *s)
2778 {
2779 return(s->shutdown);
2780 }
2781
2782int SSL_version(const SSL *s)
2783 {
2784 return(s->version);
2785 }
2786
2787SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2788 {
2789 return(ssl->ctx);
2790 }
2791
2792SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2793 {
2794 if (ssl->ctx == ctx)
2795 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002796 if (ctx == NULL)
2797 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002798 if (ssl->cert != NULL)
2799 ssl_cert_free(ssl->cert);
2800 ssl->cert = ssl_cert_dup(ctx->cert);
2801 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2802 if (ssl->ctx != NULL)
2803 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2804 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07002805
2806 ssl->sid_ctx_length = ctx->sid_ctx_length;
2807 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2808 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2809
Adam Langley95c29f32014-06-20 12:00:00 -07002810 return(ssl->ctx);
2811 }
2812
2813#ifndef OPENSSL_NO_STDIO
2814int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2815 {
2816 return(X509_STORE_set_default_paths(ctx->cert_store));
2817 }
2818
2819int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2820 const char *CApath)
2821 {
2822 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2823 }
2824#endif
2825
2826void SSL_set_info_callback(SSL *ssl,
2827 void (*cb)(const SSL *ssl,int type,int val))
2828 {
2829 ssl->info_callback=cb;
2830 }
2831
2832/* One compiler (Diab DCC) doesn't like argument names in returned
2833 function pointer. */
2834void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2835 {
2836 return ssl->info_callback;
2837 }
2838
2839int SSL_state(const SSL *ssl)
2840 {
2841 return(ssl->state);
2842 }
2843
2844void SSL_set_state(SSL *ssl, int state)
2845 {
2846 ssl->state = state;
2847 }
2848
2849void SSL_set_verify_result(SSL *ssl,long arg)
2850 {
2851 ssl->verify_result=arg;
2852 }
2853
2854long SSL_get_verify_result(const SSL *ssl)
2855 {
2856 return(ssl->verify_result);
2857 }
2858
2859int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2860 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2861 {
2862 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2863 new_func, dup_func, free_func);
2864 }
2865
2866int SSL_set_ex_data(SSL *s,int idx,void *arg)
2867 {
2868 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2869 }
2870
2871void *SSL_get_ex_data(const SSL *s,int idx)
2872 {
2873 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2874 }
2875
2876int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2877 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2878 {
2879 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2880 new_func, dup_func, free_func);
2881 }
2882
2883int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2884 {
2885 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2886 }
2887
2888void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2889 {
2890 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2891 }
2892
2893int ssl_ok(SSL *s)
2894 {
2895 return(1);
2896 }
2897
2898X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2899 {
2900 return(ctx->cert_store);
2901 }
2902
2903void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2904 {
2905 if (ctx->cert_store != NULL)
2906 X509_STORE_free(ctx->cert_store);
2907 ctx->cert_store=store;
2908 }
2909
2910int SSL_want(const SSL *s)
2911 {
2912 return(s->rwstate);
2913 }
2914
2915/*!
2916 * \brief Set the callback for generating temporary RSA keys.
2917 * \param ctx the SSL context.
2918 * \param cb the callback
2919 */
2920
Adam Langley95c29f32014-06-20 12:00:00 -07002921void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2922 int is_export,
2923 int keylength))
2924 {
2925 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2926 }
2927
2928void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2929 int is_export,
2930 int keylength))
2931 {
2932 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2933 }
Adam Langley95c29f32014-06-20 12:00:00 -07002934
2935#ifdef DOXYGEN
2936/*!
2937 * \brief The RSA temporary key callback function.
2938 * \param ssl the SSL session.
2939 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2940 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2941 * of the required key in bits.
2942 * \return the temporary RSA key.
2943 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2944 */
2945
2946RSA *cb(SSL *ssl,int is_export,int keylength)
2947 {}
2948#endif
2949
2950/*!
2951 * \brief Set the callback for generating temporary DH keys.
2952 * \param ctx the SSL context.
2953 * \param dh the callback
2954 */
2955
Adam Langley95c29f32014-06-20 12:00:00 -07002956void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2957 int keylength))
2958 {
2959 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2960 }
2961
2962void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2963 int keylength))
2964 {
2965 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2966 }
Adam Langley95c29f32014-06-20 12:00:00 -07002967
Adam Langley95c29f32014-06-20 12:00:00 -07002968void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2969 int keylength))
2970 {
2971 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2972 }
2973
2974void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2975 int keylength))
2976 {
2977 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2978 }
Adam Langley95c29f32014-06-20 12:00:00 -07002979
Adam Langley95c29f32014-06-20 12:00:00 -07002980int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2981 {
2982 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2983 {
2984 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2985 return 0;
2986 }
2987 if (ctx->psk_identity_hint != NULL)
2988 OPENSSL_free(ctx->psk_identity_hint);
2989 if (identity_hint != NULL)
2990 {
2991 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2992 if (ctx->psk_identity_hint == NULL)
2993 return 0;
2994 }
2995 else
2996 ctx->psk_identity_hint = NULL;
2997 return 1;
2998 }
2999
3000int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
3001 {
3002 if (s == NULL)
3003 return 0;
3004
Adam Langley95c29f32014-06-20 12:00:00 -07003005 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
3006 {
3007 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
3008 return 0;
3009 }
Adam Langley0289c732014-06-20 12:00:00 -07003010
3011 /* Clear hint in SSL and associated SSL_SESSION (if any). */
3012 if (s->psk_identity_hint != NULL)
3013 {
3014 OPENSSL_free(s->psk_identity_hint);
3015 s->psk_identity_hint = NULL;
3016 }
3017 if (s->session != NULL && s->session->psk_identity_hint != NULL)
3018 {
Adam Langley95c29f32014-06-20 12:00:00 -07003019 OPENSSL_free(s->session->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -07003020 s->session->psk_identity_hint = NULL;
3021 }
3022
Adam Langley95c29f32014-06-20 12:00:00 -07003023 if (identity_hint != NULL)
3024 {
Adam Langley0289c732014-06-20 12:00:00 -07003025 /* The hint is stored in SSL and SSL_SESSION with the one in
3026 * SSL_SESSION taking precedence. Thus, if SSL_SESSION is avaiable,
3027 * we store the hint there, otherwise we store it in SSL. */
3028 if (s->session != NULL)
3029 {
3030 s->session->psk_identity_hint = BUF_strdup(identity_hint);
3031 if (s->session->psk_identity_hint == NULL)
3032 return 0;
3033 }
3034 else
3035 {
3036 s->psk_identity_hint = BUF_strdup(identity_hint);
3037 if (s->psk_identity_hint == NULL)
3038 return 0;
3039 }
Adam Langley95c29f32014-06-20 12:00:00 -07003040 }
Adam Langley95c29f32014-06-20 12:00:00 -07003041 return 1;
3042 }
3043
3044const char *SSL_get_psk_identity_hint(const SSL *s)
3045 {
Adam Langley0289c732014-06-20 12:00:00 -07003046 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07003047 return NULL;
Adam Langley0289c732014-06-20 12:00:00 -07003048 /* The hint is stored in SSL and SSL_SESSION with the one in SSL_SESSION
3049 * taking precedence. */
3050 if (s->session != NULL)
3051 return(s->session->psk_identity_hint);
3052 return(s->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07003053 }
3054
3055const char *SSL_get_psk_identity(const SSL *s)
3056 {
3057 if (s == NULL || s->session == NULL)
3058 return NULL;
3059 return(s->session->psk_identity);
3060 }
3061
3062void SSL_set_psk_client_callback(SSL *s,
3063 unsigned int (*cb)(SSL *ssl, const char *hint,
3064 char *identity, unsigned int max_identity_len, unsigned char *psk,
3065 unsigned int max_psk_len))
3066 {
3067 s->psk_client_callback = cb;
3068 }
3069
3070void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
3071 unsigned int (*cb)(SSL *ssl, const char *hint,
3072 char *identity, unsigned int max_identity_len, unsigned char *psk,
3073 unsigned int max_psk_len))
3074 {
3075 ctx->psk_client_callback = cb;
3076 }
3077
3078void SSL_set_psk_server_callback(SSL *s,
3079 unsigned int (*cb)(SSL *ssl, const char *identity,
3080 unsigned char *psk, unsigned int max_psk_len))
3081 {
3082 s->psk_server_callback = cb;
3083 }
3084
3085void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
3086 unsigned int (*cb)(SSL *ssl, const char *identity,
3087 unsigned char *psk, unsigned int max_psk_len))
3088 {
3089 ctx->psk_server_callback = cb;
3090 }
Adam Langley95c29f32014-06-20 12:00:00 -07003091
3092void 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))
3093 {
3094 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3095 }
3096void 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))
3097 {
3098 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3099 }
3100
David Benjamin859ec3c2014-09-02 16:29:36 -04003101void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio)
3102 {
3103 if (ctx->keylog_bio != NULL)
3104 BIO_free(ctx->keylog_bio);
3105 ctx->keylog_bio = keylog_bio;
3106 }
3107
3108static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len)
3109 {
3110 static const char hextable[] = "0123456789abcdef";
3111 uint8_t *out;
3112 size_t i;
3113
3114 if (!CBB_add_space(cbb, &out, in_len * 2))
3115 {
3116 return 0;
3117 }
3118
3119 for (i = 0; i < in_len; i++)
3120 {
3121 *(out++) = (uint8_t)hextable[in[i] >> 4];
3122 *(out++) = (uint8_t)hextable[in[i] & 0xf];
3123 }
3124 return 1;
3125 }
3126
3127int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
3128 const uint8_t *encrypted_premaster, size_t encrypted_premaster_len,
3129 const uint8_t *premaster, size_t premaster_len)
3130 {
3131 BIO *bio = ctx->keylog_bio;
3132 CBB cbb;
3133 uint8_t *out;
3134 size_t out_len;
3135 int ret;
3136
3137 if (bio == NULL)
3138 {
3139 return 1;
3140 }
3141
3142 if (encrypted_premaster_len < 8)
3143 {
3144 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange, ERR_R_INTERNAL_ERROR);
3145 return 0;
3146 }
3147
3148 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len*2 + 1))
3149 {
3150 return 0;
3151 }
3152 if (!CBB_add_bytes(&cbb, (const uint8_t*)"RSA ", 4) ||
3153 /* Only the first 8 bytes of the encrypted premaster secret are
3154 * logged. */
3155 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
3156 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3157 !cbb_add_hex(&cbb, premaster, premaster_len) ||
3158 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3159 !CBB_finish(&cbb, &out, &out_len))
3160 {
3161 CBB_cleanup(&cbb);
3162 return 0;
3163 }
3164
3165 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3166 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3167 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3168
3169 OPENSSL_free(out);
3170 return ret;
3171 }
3172
3173int ssl_ctx_log_master_secret(SSL_CTX *ctx,
3174 const uint8_t *client_random, size_t client_random_len,
3175 const uint8_t *master, size_t master_len)
3176 {
3177 BIO *bio = ctx->keylog_bio;
3178 CBB cbb;
3179 uint8_t *out;
3180 size_t out_len;
3181 int ret;
3182
3183 if (bio == NULL)
3184 {
3185 return 1;
3186 }
3187
3188 if (client_random_len != 32)
3189 {
3190 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
3191 return 0;
3192 }
3193
3194 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len*2 + 1))
3195 {
3196 return 0;
3197 }
3198 if (!CBB_add_bytes(&cbb, (const uint8_t*)"CLIENT_RANDOM ", 14) ||
3199 !cbb_add_hex(&cbb, client_random, 32) ||
3200 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3201 !cbb_add_hex(&cbb, master, master_len) ||
3202 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3203 !CBB_finish(&cbb, &out, &out_len))
3204 {
3205 CBB_cleanup(&cbb);
3206 return 0;
3207 }
3208
3209 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3210 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3211 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3212
3213 OPENSSL_free(out);
3214 return ret;
3215 }
3216
Adam Langleyadb739e2014-06-20 12:00:00 -07003217int SSL_cutthrough_complete(const SSL *s)
3218 {
3219 return (!s->server && /* cutthrough only applies to clients */
3220 !s->hit && /* full-handshake */
3221 s->version >= SSL3_VERSION &&
3222 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3223 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3224 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3225 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3226 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
David Benjamin7e3305e2014-07-28 14:52:32 -04003227 s->state == SSL3_ST_CR_CHANGE ||
Adam Langleyadb739e2014-06-20 12:00:00 -07003228 s->state == SSL3_ST_CR_FINISHED_A));
3229 }
3230
Adam Langley95f22882014-06-20 12:00:00 -07003231void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3232 size_t* ssl_session_size)
3233{
3234 *ssl_size = sizeof(SSL);
3235 *ssl_ctx_size = sizeof(SSL_CTX);
3236 *ssl_session_size = sizeof(SSL_SESSION);
3237}
3238
Adam Langleyadb739e2014-06-20 12:00:00 -07003239int ssl3_can_cutthrough(const SSL *s)
3240 {
3241 const SSL_CIPHER *c;
3242
3243 /* require a strong enough cipher */
3244 if (SSL_get_cipher_bits(s, NULL) < 128)
3245 return 0;
3246
3247 /* require ALPN or NPN extension */
David Benjamin457112e2014-08-27 14:35:09 -04003248 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen)
Adam Langleyadb739e2014-06-20 12:00:00 -07003249 {
3250 return 0;
3251 }
3252
3253 /* require a forward-secret cipher */
3254 c = SSL_get_current_cipher(s);
3255 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3256 c->algorithm_mkey != SSL_kEECDH))
3257 {
3258 return 0;
3259 }
3260
3261 return 1;
3262 }
3263
Adam Langley29707792014-06-20 12:00:00 -07003264/* ssl_get_max_version returns the maximum SSL/TLS version number supported by
3265 * |s|, or zero if all versions are disabled. */
3266int ssl_get_max_version(const SSL *s)
3267 {
3268 /* Only one version supported for DTLS. */
3269 if (s->version == DTLS1_VERSION)
3270 return DTLS1_VERSION;
3271
3272 if (!(s->options & SSL_OP_NO_TLSv1_2))
3273 return TLS1_2_VERSION;
3274 if (!(s->options & SSL_OP_NO_TLSv1_1))
3275 return TLS1_1_VERSION;
3276 if (!(s->options & SSL_OP_NO_TLSv1))
3277 return TLS1_VERSION;
3278 if (!(s->options & SSL_OP_NO_SSLv3))
3279 return SSL3_VERSION;
3280 if (!(s->options & SSL_OP_NO_SSLv2))
3281 return SSL2_VERSION;
3282 return 0;
3283 }
3284
Adam Langley95c29f32014-06-20 12:00:00 -07003285/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3286 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3287 * any. If EVP_MD pointer is passed, initializes ctx with this md
3288 * Returns newly allocated ctx;
3289 */
3290
3291EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3292{
3293 ssl_clear_hash_ctx(hash);
3294 *hash = EVP_MD_CTX_create();
3295 if (md) EVP_DigestInit_ex(*hash,md,NULL);
3296 return *hash;
3297}
3298void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3299{
3300
3301 if (*hash) EVP_MD_CTX_destroy(*hash);
3302 *hash=NULL;
3303}
3304
3305void SSL_set_debug(SSL *s, int debug)
3306 {
3307 s->debug = debug;
3308 }
3309
3310int SSL_cache_hit(SSL *s)
3311 {
3312 return s->hit;
3313 }
3314
3315int SSL_is_server(SSL *s)
3316 {
3317 return s->server;
3318 }