blob: 8357ff9a2890c1f471ef48bdc10849fdbfbd72bf [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:
Adam Langley95c29f32014-06-20 12:00:00 -07001154 if (larg < (long)dtls1_min_mtu())
1155 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001156 if (SSL_IS_DTLS(s))
1157 {
1158 s->d1->mtu = larg;
1159 return larg;
1160 }
1161 return 0;
1162 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1163 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1164 return 0;
1165 s->max_send_fragment = larg;
1166 return 1;
1167 case SSL_CTRL_GET_RI_SUPPORT:
1168 if (s->s3)
1169 return s->s3->send_connection_binding;
1170 else return 0;
1171 case SSL_CTRL_CERT_FLAGS:
1172 return(s->cert->cert_flags|=larg);
1173 case SSL_CTRL_CLEAR_CERT_FLAGS:
1174 return(s->cert->cert_flags &=~larg);
1175
1176 case SSL_CTRL_GET_RAW_CIPHERLIST:
1177 if (parg)
1178 {
1179 if (s->cert->ciphers_raw == NULL)
1180 return 0;
1181 *(unsigned char **)parg = s->cert->ciphers_raw;
1182 return (int)s->cert->ciphers_rawlen;
1183 }
1184 else
David Benjamin39482a12014-07-20 13:30:15 -04001185 {
1186 /* Passing a NULL |parg| returns the size of a single
1187 * cipher suite value. */
1188 return 2;
1189 }
Adam Langley95c29f32014-06-20 12:00:00 -07001190 default:
1191 return(s->method->ssl_ctrl(s,cmd,larg,parg));
1192 }
1193 }
1194
1195long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1196 {
1197 switch(cmd)
1198 {
1199 case SSL_CTRL_SET_MSG_CALLBACK:
1200 s->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1201 return 1;
1202
1203 default:
1204 return(s->method->ssl_callback_ctrl(s,cmd,fp));
1205 }
1206 }
1207
1208LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
1209 {
1210 return ctx->sessions;
1211 }
1212
1213long SSL_CTX_ctrl(SSL_CTX *ctx,int cmd,long larg,void *parg)
1214 {
1215 long l;
1216
1217 switch (cmd)
1218 {
1219 case SSL_CTRL_GET_READ_AHEAD:
1220 return(ctx->read_ahead);
1221 case SSL_CTRL_SET_READ_AHEAD:
1222 l=ctx->read_ahead;
1223 ctx->read_ahead=larg;
1224 return(l);
1225
1226 case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1227 ctx->msg_callback_arg = parg;
1228 return 1;
1229
1230 case SSL_CTRL_GET_MAX_CERT_LIST:
1231 return(ctx->max_cert_list);
1232 case SSL_CTRL_SET_MAX_CERT_LIST:
1233 l=ctx->max_cert_list;
1234 ctx->max_cert_list=larg;
1235 return(l);
1236
1237 case SSL_CTRL_SET_SESS_CACHE_SIZE:
1238 l=ctx->session_cache_size;
1239 ctx->session_cache_size=larg;
1240 return(l);
1241 case SSL_CTRL_GET_SESS_CACHE_SIZE:
1242 return(ctx->session_cache_size);
1243 case SSL_CTRL_SET_SESS_CACHE_MODE:
1244 l=ctx->session_cache_mode;
1245 ctx->session_cache_mode=larg;
1246 return(l);
1247 case SSL_CTRL_GET_SESS_CACHE_MODE:
1248 return(ctx->session_cache_mode);
1249
1250 case SSL_CTRL_SESS_NUMBER:
1251 return(lh_SSL_SESSION_num_items(ctx->sessions));
1252 case SSL_CTRL_SESS_CONNECT:
1253 return(ctx->stats.sess_connect);
1254 case SSL_CTRL_SESS_CONNECT_GOOD:
1255 return(ctx->stats.sess_connect_good);
1256 case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1257 return(ctx->stats.sess_connect_renegotiate);
1258 case SSL_CTRL_SESS_ACCEPT:
1259 return(ctx->stats.sess_accept);
1260 case SSL_CTRL_SESS_ACCEPT_GOOD:
1261 return(ctx->stats.sess_accept_good);
1262 case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1263 return(ctx->stats.sess_accept_renegotiate);
1264 case SSL_CTRL_SESS_HIT:
1265 return(ctx->stats.sess_hit);
1266 case SSL_CTRL_SESS_CB_HIT:
1267 return(ctx->stats.sess_cb_hit);
1268 case SSL_CTRL_SESS_MISSES:
1269 return(ctx->stats.sess_miss);
1270 case SSL_CTRL_SESS_TIMEOUTS:
1271 return(ctx->stats.sess_timeout);
1272 case SSL_CTRL_SESS_CACHE_FULL:
1273 return(ctx->stats.sess_cache_full);
1274 case SSL_CTRL_OPTIONS:
1275 return(ctx->options|=larg);
1276 case SSL_CTRL_CLEAR_OPTIONS:
1277 return(ctx->options&=~larg);
1278 case SSL_CTRL_MODE:
1279 return(ctx->mode|=larg);
1280 case SSL_CTRL_CLEAR_MODE:
1281 return(ctx->mode&=~larg);
1282 case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1283 if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1284 return 0;
1285 ctx->max_send_fragment = larg;
1286 return 1;
1287 case SSL_CTRL_CERT_FLAGS:
1288 return(ctx->cert->cert_flags|=larg);
1289 case SSL_CTRL_CLEAR_CERT_FLAGS:
1290 return(ctx->cert->cert_flags &=~larg);
1291 default:
1292 return(ctx->method->ssl_ctx_ctrl(ctx,cmd,larg,parg));
1293 }
1294 }
1295
1296long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1297 {
1298 switch(cmd)
1299 {
1300 case SSL_CTRL_SET_MSG_CALLBACK:
1301 ctx->msg_callback = (void (*)(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg))(fp);
1302 return 1;
1303
1304 default:
1305 return(ctx->method->ssl_ctx_callback_ctrl(ctx,cmd,fp));
1306 }
1307 }
1308
1309int ssl_cipher_id_cmp(const void *in_a, const void *in_b)
1310 {
1311 long l;
1312 const SSL_CIPHER *a = in_a;
1313 const SSL_CIPHER *b = in_b;
1314 const long a_id = a->id;
1315 const long b_id = b->id;
1316
1317 l = a_id - b_id;
1318 if (l == 0L)
1319 return(0);
1320 else
1321 return((l > 0)?1:-1);
1322 }
1323
1324int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp)
1325 {
1326 long l;
1327 const long a_id = (*ap)->id;
1328 const long b_id = (*bp)->id;
1329
1330 l = a_id - b_id;
1331 if (l == 0)
1332 return(0);
1333 else
1334 return((l > 0)?1:-1);
1335 }
1336
1337/** return a STACK of the ciphers available for the SSL and in order of
1338 * preference */
1339STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
1340 {
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001341 if (s == NULL)
1342 return NULL;
1343
1344 if (s->cipher_list != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001345 {
Adam Langley858a88d2014-06-20 12:00:00 -07001346 return(s->cipher_list->ciphers);
Adam Langley95c29f32014-06-20 12:00:00 -07001347 }
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001348
1349 if (s->version >= TLS1_1_VERSION)
1350 {
1351 if (s->ctx != NULL && s->ctx->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07001352 return s->ctx->cipher_list_tls11->ciphers;
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001353 }
1354
1355 if ((s->ctx != NULL) &&
1356 (s->ctx->cipher_list != NULL))
1357 {
Adam Langley858a88d2014-06-20 12:00:00 -07001358 return(s->ctx->cipher_list->ciphers);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001359 }
1360
Adam Langley95c29f32014-06-20 12:00:00 -07001361 return(NULL);
1362 }
1363
1364/** return a STACK of the ciphers available for the SSL and in order of
1365 * algorithm id */
1366STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
1367 {
1368 if (s != NULL)
1369 {
1370 if (s->cipher_list_by_id != NULL)
1371 {
1372 return(s->cipher_list_by_id);
1373 }
1374 else if ((s->ctx != NULL) &&
1375 (s->ctx->cipher_list_by_id != NULL))
1376 {
1377 return(s->ctx->cipher_list_by_id);
1378 }
1379 }
1380 return(NULL);
1381 }
1382
1383/** The old interface to get the same thing as SSL_get_ciphers() */
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001384const char *SSL_get_cipher_list(const SSL *s, int n)
Adam Langley95c29f32014-06-20 12:00:00 -07001385 {
David Benjamin6f260012014-08-15 13:49:12 -04001386 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001387 STACK_OF(SSL_CIPHER) *sk;
1388
David Benjaminfb3ff2c2014-09-30 21:00:38 -04001389 if (s == NULL)
1390 return NULL;
1391 sk = SSL_get_ciphers(s);
1392 if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk))
1393 return NULL;
1394 c = sk_SSL_CIPHER_value(sk, n);
1395 if (c == NULL)
1396 return NULL;
1397 return c->name;
Adam Langley95c29f32014-06-20 12:00:00 -07001398 }
1399
1400/** specify the ciphers to be used by default by the SSL_CTX */
1401int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1402 {
1403 STACK_OF(SSL_CIPHER) *sk;
1404
1405 sk=ssl_create_cipher_list(ctx->method,&ctx->cipher_list,
1406 &ctx->cipher_list_by_id,str, ctx->cert);
1407 /* ssl_create_cipher_list may return an empty stack if it
1408 * was unable to find a cipher matching the given rule string
1409 * (for example if the rule string specifies a cipher which
1410 * has been disabled). This is not an error as far as
1411 * ssl_create_cipher_list is concerned, and hence
1412 * ctx->cipher_list and ctx->cipher_list_by_id has been
1413 * updated. */
1414 if (sk == NULL)
1415 return 0;
1416 else if (sk_SSL_CIPHER_num(sk) == 0)
1417 {
1418 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1419 return 0;
1420 }
1421 return 1;
1422 }
1423
Adam Langley0b5c1ac2014-06-20 12:00:00 -07001424int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str)
1425 {
1426 STACK_OF(SSL_CIPHER) *sk;
1427
1428 sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str, ctx->cert);
1429 if (sk == NULL)
1430 return 0;
1431 else if (sk_SSL_CIPHER_num(sk) == 0)
1432 {
1433 OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11, SSL_R_NO_CIPHER_MATCH);
1434 return 0;
1435 }
1436 return 1;
1437 }
1438
Adam Langley95c29f32014-06-20 12:00:00 -07001439/** specify the ciphers to be used by the SSL */
1440int SSL_set_cipher_list(SSL *s,const char *str)
1441 {
1442 STACK_OF(SSL_CIPHER) *sk;
1443
1444 sk=ssl_create_cipher_list(s->ctx->method,&s->cipher_list,
1445 &s->cipher_list_by_id,str, s->cert);
1446 /* see comment in SSL_CTX_set_cipher_list */
1447 if (sk == NULL)
1448 return 0;
1449 else if (sk_SSL_CIPHER_num(sk) == 0)
1450 {
1451 OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
1452 return 0;
1453 }
1454 return 1;
1455 }
1456
David Benjamin5491e3f2014-09-29 19:33:09 -04001457int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p)
Adam Langley95c29f32014-06-20 12:00:00 -07001458 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001459 size_t i;
David Benjamin6f260012014-08-15 13:49:12 -04001460 const SSL_CIPHER *c;
Adam Langley95c29f32014-06-20 12:00:00 -07001461 CERT *ct = s->cert;
David Benjamin5491e3f2014-09-29 19:33:09 -04001462 uint8_t *q;
Adam Langley95c29f32014-06-20 12:00:00 -07001463 /* Set disabled masks for this session */
1464 ssl_set_client_disabled(s);
1465
1466 if (sk == NULL) return(0);
1467 q=p;
1468
1469 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
1470 {
1471 c=sk_SSL_CIPHER_value(sk,i);
1472 /* Skip disabled ciphers */
1473 if (c->algorithm_ssl & ct->mask_ssl ||
1474 c->algorithm_mkey & ct->mask_k ||
1475 c->algorithm_auth & ct->mask_a)
1476 continue;
David Benjamin39482a12014-07-20 13:30:15 -04001477 s2n(ssl3_get_cipher_value(c), p);
Adam Langley95c29f32014-06-20 12:00:00 -07001478 }
David Benjamin5491e3f2014-09-29 19:33:09 -04001479 /* If all ciphers were disabled, return the error to the caller. */
1480 if (p == q)
Adam Langley95c29f32014-06-20 12:00:00 -07001481 {
David Benjamin5491e3f2014-09-29 19:33:09 -04001482 return 0;
1483 }
1484
1485 /* Add SCSVs. */
1486 if (!s->renegotiate)
1487 {
1488 s2n(SSL3_CK_SCSV & 0xffff, p);
1489 }
1490 if (s->fallback_scsv)
1491 {
1492 s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
Adam Langley95c29f32014-06-20 12:00:00 -07001493 }
1494
1495 return(p-q);
1496 }
1497
David Benjamin9f2c0d72014-10-21 22:00:19 -04001498STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs)
Adam Langley95c29f32014-06-20 12:00:00 -07001499 {
David Benjamin39482a12014-07-20 13:30:15 -04001500 CBS cipher_suites = *cbs;
Adam Langley95c29f32014-06-20 12:00:00 -07001501 const SSL_CIPHER *c;
1502 STACK_OF(SSL_CIPHER) *sk;
David Benjamin39482a12014-07-20 13:30:15 -04001503
Adam Langley95c29f32014-06-20 12:00:00 -07001504 if (s->s3)
1505 s->s3->send_connection_binding = 0;
1506
David Benjamin39482a12014-07-20 13:30:15 -04001507 if (CBS_len(&cipher_suites) % 2 != 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001508 {
1509 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001510 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001511 }
David Benjamin9f2c0d72014-10-21 22:00:19 -04001512
1513 sk = sk_SSL_CIPHER_new_null();
1514 if (sk == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001515 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001516 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1517 goto err;
Adam Langley95c29f32014-06-20 12:00:00 -07001518 }
1519
David Benjamin39482a12014-07-20 13:30:15 -04001520 if (!CBS_stow(&cipher_suites,
1521 &s->cert->ciphers_raw, &s->cert->ciphers_rawlen))
Adam Langley95c29f32014-06-20 12:00:00 -07001522 {
1523 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1524 goto err;
1525 }
Adam Langley95c29f32014-06-20 12:00:00 -07001526
David Benjamin39482a12014-07-20 13:30:15 -04001527 while (CBS_len(&cipher_suites) > 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001528 {
David Benjamin39482a12014-07-20 13:30:15 -04001529 uint16_t cipher_suite;
1530
1531 if (!CBS_get_u16(&cipher_suites, &cipher_suite))
1532 {
1533 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
1534 goto err;
1535 }
1536
David Benjamin9f2c0d72014-10-21 22:00:19 -04001537 /* Check for SCSV. */
David Benjamin39482a12014-07-20 13:30:15 -04001538 if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff))
Adam Langley95c29f32014-06-20 12:00:00 -07001539 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001540 /* SCSV is fatal if renegotiating. */
Adam Langley95c29f32014-06-20 12:00:00 -07001541 if (s->renegotiate)
1542 {
1543 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
1544 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
1545 goto err;
1546 }
1547 s->s3->send_connection_binding = 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001548 continue;
1549 }
1550
David Benjamin9f2c0d72014-10-21 22:00:19 -04001551 /* Check for FALLBACK_SCSV. */
1552 if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff))
Adam Langley29707792014-06-20 12:00:00 -07001553 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001554 if (s->version < ssl_get_max_version(s))
1555 {
1556 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, SSL_R_INAPPROPRIATE_FALLBACK);
1557 ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
1558 goto err;
1559 }
1560 continue;
Adam Langley29707792014-06-20 12:00:00 -07001561 }
1562
David Benjamin39482a12014-07-20 13:30:15 -04001563 c = ssl3_get_cipher_by_value(cipher_suite);
Adam Langley95c29f32014-06-20 12:00:00 -07001564 if (c != NULL)
1565 {
David Benjamin9f2c0d72014-10-21 22:00:19 -04001566 if (!sk_SSL_CIPHER_push(sk, c))
Adam Langley95c29f32014-06-20 12:00:00 -07001567 {
1568 OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
1569 goto err;
1570 }
1571 }
1572 }
1573
David Benjamin9f2c0d72014-10-21 22:00:19 -04001574 return sk;
1575
Adam Langley95c29f32014-06-20 12:00:00 -07001576err:
David Benjamin9f2c0d72014-10-21 22:00:19 -04001577 if (sk != NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07001578 sk_SSL_CIPHER_free(sk);
David Benjamin9f2c0d72014-10-21 22:00:19 -04001579 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001580 }
1581
1582
Adam Langley95c29f32014-06-20 12:00:00 -07001583/** return a servername extension value if provided in Client Hello, or NULL.
1584 * So far, only host_name types are defined (RFC 3546).
1585 */
1586
1587const char *SSL_get_servername(const SSL *s, const int type)
1588 {
1589 if (type != TLSEXT_NAMETYPE_host_name)
1590 return NULL;
1591
1592 return s->session && !s->tlsext_hostname ?
1593 s->session->tlsext_hostname :
1594 s->tlsext_hostname;
1595 }
1596
1597int SSL_get_servername_type(const SSL *s)
1598 {
1599 if (s->session && (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname))
1600 return TLSEXT_NAMETYPE_host_name;
1601 return -1;
1602 }
1603
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001604void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx)
1605 {
1606 ctx->signed_cert_timestamps_enabled = 1;
1607 }
1608
1609int SSL_enable_signed_cert_timestamps(SSL *ssl)
1610 {
David Benjamin6c7aed02014-08-27 16:42:38 -04001611 /* Currently not implemented server-side. */
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001612 if (ssl->server)
1613 return 0;
1614
1615 ssl->signed_cert_timestamps_enabled = 1;
1616 return 1;
1617 }
1618
David Benjamin6c7aed02014-08-27 16:42:38 -04001619void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx)
1620 {
1621 ctx->ocsp_stapling_enabled = 1;
1622 }
1623
1624int SSL_enable_ocsp_stapling(SSL *ssl)
1625 {
1626 /* Currently not implemented server-side. */
1627 if (ssl->server)
1628 return 0;
1629 ssl->ocsp_stapling_enabled = 1;
1630 return 1;
1631 }
1632
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001633void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, uint8_t **out, size_t *out_len)
1634 {
Adam Langley3cb50e02014-08-26 14:00:31 -07001635 SSL_SESSION *session = ssl->session;
1636
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001637 *out_len = 0;
1638 *out = NULL;
1639 if (ssl->server)
1640 return;
HÃ¥vard Molland9169c962014-08-14 14:42:37 +02001641 if (!session || !session->tlsext_signed_cert_timestamp_list)
1642 return;
1643 *out = session->tlsext_signed_cert_timestamp_list;
1644 *out_len = session->tlsext_signed_cert_timestamp_list_length;
1645 }
1646
David Benjamin6c7aed02014-08-27 16:42:38 -04001647void SSL_get0_ocsp_response(const SSL *ssl, uint8_t **out, size_t *out_len)
1648 {
1649 SSL_SESSION *session = ssl->session;
1650
1651 *out_len = 0;
1652 *out = NULL;
1653 if (ssl->server)
1654 return;
1655 if (!session || !session->ocsp_response)
1656 return;
1657 *out = session->ocsp_response;
1658 *out_len = session->ocsp_response_length;
1659 }
1660
Adam Langley95c29f32014-06-20 12:00:00 -07001661/* SSL_select_next_proto implements the standard protocol selection. It is
1662 * expected that this function is called from the callback set by
1663 * SSL_CTX_set_next_proto_select_cb.
1664 *
1665 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1666 * strings. The length byte itself is not included in the length. A byte
1667 * string of length 0 is invalid. No byte string may be truncated.
1668 *
1669 * The current, but experimental algorithm for selecting the protocol is:
1670 *
1671 * 1) If the server doesn't support NPN then this is indicated to the
1672 * callback. In this case, the client application has to abort the connection
1673 * or have a default application level protocol.
1674 *
1675 * 2) If the server supports NPN, but advertises an empty list then the
1676 * client selects the first protcol in its list, but indicates via the
1677 * API that this fallback case was enacted.
1678 *
1679 * 3) Otherwise, the client finds the first protocol in the server's list
1680 * that it supports and selects this protocol. This is because it's
1681 * assumed that the server has better information about which protocol
1682 * a client should use.
1683 *
1684 * 4) If the client doesn't support any of the server's advertised
1685 * protocols, then this is treated the same as case 2.
1686 *
1687 * It returns either
1688 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1689 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1690 */
1691int 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)
1692 {
1693 unsigned int i, j;
1694 const unsigned char *result;
1695 int status = OPENSSL_NPN_UNSUPPORTED;
1696
1697 /* For each protocol in server preference order, see if we support it. */
1698 for (i = 0; i < server_len; )
1699 {
1700 for (j = 0; j < client_len; )
1701 {
1702 if (server[i] == client[j] &&
1703 memcmp(&server[i+1], &client[j+1], server[i]) == 0)
1704 {
1705 /* We found a match */
1706 result = &server[i];
1707 status = OPENSSL_NPN_NEGOTIATED;
1708 goto found;
1709 }
1710 j += client[j];
1711 j++;
1712 }
1713 i += server[i];
1714 i++;
1715 }
1716
1717 /* There's no overlap between our protocols and the server's list. */
1718 result = client;
1719 status = OPENSSL_NPN_NO_OVERLAP;
1720
1721 found:
1722 *out = (unsigned char *) result + 1;
1723 *outlen = result[0];
1724 return status;
1725 }
1726
Adam Langley95c29f32014-06-20 12:00:00 -07001727/* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
1728 * requested protocol for this connection and returns 0. If the client didn't
1729 * request any protocol, then *data is set to NULL.
1730 *
1731 * Note that the client can request any protocol it chooses. The value returned
1732 * from this function need not be a member of the list of supported protocols
1733 * provided by the callback.
1734 */
David Benjaminced551f2014-07-11 23:16:16 -04001735void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data, unsigned *len)
Adam Langley95c29f32014-06-20 12:00:00 -07001736 {
1737 *data = s->next_proto_negotiated;
1738 if (!*data) {
1739 *len = 0;
1740 } else {
1741 *len = s->next_proto_negotiated_len;
1742 }
1743}
1744
1745/* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
1746 * TLS server needs a list of supported protocols for Next Protocol
1747 * Negotiation. The returned list must be in wire format. The list is returned
1748 * by setting |out| to point to it and |outlen| to its length. This memory will
1749 * not be modified, but one should assume that the SSL* keeps a reference to
1750 * it.
1751 *
1752 * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise. Otherwise, no
1753 * such extension will be included in the ServerHello. */
1754void 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)
1755 {
1756 ctx->next_protos_advertised_cb = cb;
1757 ctx->next_protos_advertised_cb_arg = arg;
1758 }
1759
1760/* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
1761 * client needs to select a protocol from the server's provided list. |out|
1762 * must be set to point to the selected protocol (which may be within |in|).
1763 * The length of the protocol name must be written into |outlen|. The server's
1764 * advertised protocols are provided in |in| and |inlen|. The callback can
1765 * assume that |in| is syntactically valid.
1766 *
1767 * The client must select a protocol. It is fatal to the connection if this
1768 * callback returns a value other than SSL_TLSEXT_ERR_OK.
1769 */
1770void 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)
1771 {
1772 ctx->next_proto_select_cb = cb;
1773 ctx->next_proto_select_cb_arg = arg;
1774 }
Adam Langley95c29f32014-06-20 12:00:00 -07001775
Adam Langley95c29f32014-06-20 12:00:00 -07001776/* SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
1777 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1778 * length-prefixed strings).
1779 *
1780 * Returns 0 on success. */
1781int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char* protos,
1782 unsigned protos_len)
1783 {
1784 if (ctx->alpn_client_proto_list)
1785 OPENSSL_free(ctx->alpn_client_proto_list);
1786
David Benjamin072c9532014-07-26 11:44:25 -04001787 ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001788 if (!ctx->alpn_client_proto_list)
1789 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001790 ctx->alpn_client_proto_list_len = protos_len;
1791
1792 return 0;
1793 }
1794
1795/* SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
1796 * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
1797 * length-prefixed strings).
1798 *
1799 * Returns 0 on success. */
1800int SSL_set_alpn_protos(SSL *ssl, const unsigned char* protos,
1801 unsigned protos_len)
1802 {
1803 if (ssl->alpn_client_proto_list)
1804 OPENSSL_free(ssl->alpn_client_proto_list);
1805
David Benjamin072c9532014-07-26 11:44:25 -04001806 ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
Adam Langley95c29f32014-06-20 12:00:00 -07001807 if (!ssl->alpn_client_proto_list)
1808 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -07001809 ssl->alpn_client_proto_list_len = protos_len;
1810
1811 return 0;
1812 }
1813
1814/* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
1815 * during ClientHello processing in order to select an ALPN protocol from the
1816 * client's list of offered protocols. */
1817void SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1818 int (*cb) (SSL *ssl,
1819 const unsigned char **out,
1820 unsigned char *outlen,
1821 const unsigned char *in,
1822 unsigned int inlen,
1823 void *arg),
1824 void *arg)
1825 {
1826 ctx->alpn_select_cb = cb;
1827 ctx->alpn_select_cb_arg = arg;
1828 }
1829
1830/* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
1831 * On return it sets |*data| to point to |*len| bytes of protocol name (not
1832 * including the leading length-prefix byte). If the server didn't respond with
1833 * a negotiated protocol then |*len| will be zero. */
1834void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1835 unsigned *len)
1836 {
1837 *data = NULL;
1838 if (ssl->s3)
1839 *data = ssl->s3->alpn_selected;
1840 if (*data == NULL)
1841 *len = 0;
1842 else
1843 *len = ssl->s3->alpn_selected_len;
1844 }
Adam Langley95c29f32014-06-20 12:00:00 -07001845
1846int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1847 const char *label, size_t llen, const unsigned char *p, size_t plen,
1848 int use_context)
1849 {
1850 if (s->version < TLS1_VERSION)
1851 return -1;
1852
1853 return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
1854 llen, p, plen,
1855 use_context);
1856 }
1857
1858static uint32_t ssl_session_hash(const SSL_SESSION *a)
1859 {
1860 uint32_t hash = ((uint32_t) a->session_id[0]) ||
1861 ((uint32_t) a->session_id[1] << 8) ||
1862 ((uint32_t) a->session_id[2] << 16) ||
1863 ((uint32_t) a->session_id[3] << 24);
1864
1865 return hash;
1866 }
1867
1868/* NB: If this function (or indeed the hash function which uses a sort of
1869 * coarser function than this one) is changed, ensure
1870 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1871 * able to construct an SSL_SESSION that will collide with any existing session
1872 * with a matching session ID. */
1873static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1874 {
1875 if (a->ssl_version != b->ssl_version)
1876 return(1);
1877 if (a->session_id_length != b->session_id_length)
1878 return(1);
1879 return(memcmp(a->session_id,b->session_id,a->session_id_length));
1880 }
1881
1882SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
1883 {
1884 SSL_CTX *ret=NULL;
1885
1886 if (meth == NULL)
1887 {
1888 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
1889 return(NULL);
1890 }
1891
Adam Langley95c29f32014-06-20 12:00:00 -07001892 if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0)
1893 {
1894 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1895 goto err;
1896 }
1897 ret=(SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
1898 if (ret == NULL)
1899 goto err;
1900
1901 memset(ret,0,sizeof(SSL_CTX));
1902
1903 ret->method=meth;
1904
1905 ret->cert_store=NULL;
1906 ret->session_cache_mode=SSL_SESS_CACHE_SERVER;
1907 ret->session_cache_size=SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1908 ret->session_cache_head=NULL;
1909 ret->session_cache_tail=NULL;
1910
1911 /* We take the system default */
David Benjaminf4501342014-08-14 17:24:37 -04001912 ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
Adam Langley95c29f32014-06-20 12:00:00 -07001913
1914 ret->new_session_cb=0;
1915 ret->remove_session_cb=0;
1916 ret->get_session_cb=0;
1917 ret->generate_session_id=0;
1918
1919 memset((char *)&ret->stats,0,sizeof(ret->stats));
1920
1921 ret->references=1;
1922 ret->quiet_shutdown=0;
1923
Adam Langley95c29f32014-06-20 12:00:00 -07001924 ret->info_callback=NULL;
1925
1926 ret->app_verify_callback=0;
1927 ret->app_verify_arg=NULL;
1928
1929 ret->max_cert_list=SSL_MAX_CERT_LIST_DEFAULT;
1930 ret->read_ahead=0;
1931 ret->msg_callback=0;
1932 ret->msg_callback_arg=NULL;
1933 ret->verify_mode=SSL_VERIFY_NONE;
1934#if 0
1935 ret->verify_depth=-1; /* Don't impose a limit (but x509_lu.c does) */
1936#endif
1937 ret->sid_ctx_length=0;
1938 ret->default_verify_callback=NULL;
1939 if ((ret->cert=ssl_cert_new()) == NULL)
1940 goto err;
1941
1942 ret->default_passwd_callback=0;
1943 ret->default_passwd_callback_userdata=NULL;
1944 ret->client_cert_cb=0;
1945 ret->app_gen_cookie_cb=0;
1946 ret->app_verify_cookie_cb=0;
1947
1948 ret->sessions=lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
1949 if (ret->sessions == NULL) goto err;
1950 ret->cert_store=X509_STORE_new();
1951 if (ret->cert_store == NULL) goto err;
1952
1953 ssl_create_cipher_list(ret->method,
1954 &ret->cipher_list,&ret->cipher_list_by_id,
1955 meth->version == SSL2_VERSION ? "SSLv2" : SSL_DEFAULT_CIPHER_LIST, ret->cert);
1956 if (ret->cipher_list == NULL
Adam Langley858a88d2014-06-20 12:00:00 -07001957 || sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0)
Adam Langley95c29f32014-06-20 12:00:00 -07001958 {
1959 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
1960 goto err2;
1961 }
1962
1963 ret->param = X509_VERIFY_PARAM_new();
1964 if (!ret->param)
1965 goto err;
1966
Adam Langley95c29f32014-06-20 12:00:00 -07001967 if ((ret->client_CA=sk_X509_NAME_new_null()) == NULL)
1968 goto err;
1969
1970 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data);
1971
1972 ret->extra_certs=NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001973
1974 ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
1975
Adam Langley95c29f32014-06-20 12:00:00 -07001976 ret->tlsext_servername_callback = 0;
1977 ret->tlsext_servername_arg = NULL;
1978 /* Setup RFC4507 ticket keys */
1979 if ((RAND_pseudo_bytes(ret->tlsext_tick_key_name, 16) <= 0)
1980 || (RAND_bytes(ret->tlsext_tick_hmac_key, 16) <= 0)
1981 || (RAND_bytes(ret->tlsext_tick_aes_key, 16) <= 0))
1982 ret->options |= SSL_OP_NO_TICKET;
1983
1984 ret->tlsext_status_cb = 0;
1985 ret->tlsext_status_arg = NULL;
1986
Adam Langley95c29f32014-06-20 12:00:00 -07001987 ret->next_protos_advertised_cb = 0;
1988 ret->next_proto_select_cb = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001989 ret->psk_identity_hint=NULL;
1990 ret->psk_client_callback=NULL;
1991 ret->psk_server_callback=NULL;
David Benjamin422d3a42014-08-20 11:09:03 -04001992
Adam Langley95c29f32014-06-20 12:00:00 -07001993 /* Default is to connect to non-RI servers. When RI is more widely
1994 * deployed might change this.
1995 */
1996 ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
1997
1998 return(ret);
1999err:
2000 OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
2001err2:
2002 if (ret != NULL) SSL_CTX_free(ret);
2003 return(NULL);
2004 }
2005
Adam Langley95c29f32014-06-20 12:00:00 -07002006
2007void SSL_CTX_free(SSL_CTX *a)
2008 {
2009 int i;
2010
2011 if (a == NULL) return;
2012
2013 i=CRYPTO_add(&a->references,-1,CRYPTO_LOCK_SSL_CTX);
2014#ifdef REF_PRINT
2015 REF_PRINT("SSL_CTX",a);
2016#endif
2017 if (i > 0) return;
2018#ifdef REF_CHECK
2019 if (i < 0)
2020 {
2021 fprintf(stderr,"SSL_CTX_free, bad reference count\n");
2022 abort(); /* ok */
2023 }
2024#endif
2025
2026 if (a->param)
2027 X509_VERIFY_PARAM_free(a->param);
2028
2029 /*
2030 * Free internal session cache. However: the remove_cb() may reference
2031 * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2032 * after the sessions were flushed.
2033 * As the ex_data handling routines might also touch the session cache,
2034 * the most secure solution seems to be: empty (flush) the cache, then
2035 * free ex_data, then finally free the cache.
2036 * (See ticket [openssl.org #212].)
2037 */
2038 if (a->sessions != NULL)
2039 SSL_CTX_flush_sessions(a,0);
2040
2041 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
2042
2043 if (a->sessions != NULL)
2044 lh_SSL_SESSION_free(a->sessions);
2045
2046 if (a->cert_store != NULL)
2047 X509_STORE_free(a->cert_store);
2048 if (a->cipher_list != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002049 ssl_cipher_preference_list_free(a->cipher_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002050 if (a->cipher_list_by_id != NULL)
2051 sk_SSL_CIPHER_free(a->cipher_list_by_id);
Adam Langley0b5c1ac2014-06-20 12:00:00 -07002052 if (a->cipher_list_tls11 != NULL)
Adam Langley858a88d2014-06-20 12:00:00 -07002053 ssl_cipher_preference_list_free(a->cipher_list_tls11);
Adam Langley95c29f32014-06-20 12:00:00 -07002054 if (a->cert != NULL)
2055 ssl_cert_free(a->cert);
2056 if (a->client_CA != NULL)
2057 sk_X509_NAME_pop_free(a->client_CA,X509_NAME_free);
2058 if (a->extra_certs != NULL)
2059 sk_X509_pop_free(a->extra_certs,X509_free);
Adam Langley95c29f32014-06-20 12:00:00 -07002060
2061 if (a->srtp_profiles)
2062 sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
2063
Adam Langley95c29f32014-06-20 12:00:00 -07002064 if (a->psk_identity_hint)
2065 OPENSSL_free(a->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07002066
Adam Langley95c29f32014-06-20 12:00:00 -07002067 if (a->tlsext_ecpointformatlist)
2068 OPENSSL_free(a->tlsext_ecpointformatlist);
2069 if (a->tlsext_ellipticcurvelist)
2070 OPENSSL_free(a->tlsext_ellipticcurvelist);
Adam Langley95c29f32014-06-20 12:00:00 -07002071 if (a->alpn_client_proto_list != NULL)
2072 OPENSSL_free(a->alpn_client_proto_list);
Adam Langley95c29f32014-06-20 12:00:00 -07002073
Adam Langley1258b6a2014-06-20 12:00:00 -07002074 if (a->tlsext_channel_id_private)
2075 EVP_PKEY_free(a->tlsext_channel_id_private);
Adam Langley1258b6a2014-06-20 12:00:00 -07002076
David Benjamin859ec3c2014-09-02 16:29:36 -04002077 if (a->keylog_bio)
2078 BIO_free(a->keylog_bio);
2079
Adam Langley95c29f32014-06-20 12:00:00 -07002080 OPENSSL_free(a);
2081 }
2082
2083void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2084 {
2085 ctx->default_passwd_callback=cb;
2086 }
2087
2088void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx,void *u)
2089 {
2090 ctx->default_passwd_callback_userdata=u;
2091 }
2092
2093void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *,void *), void *arg)
2094 {
2095 ctx->app_verify_callback=cb;
2096 ctx->app_verify_arg=arg;
2097 }
2098
2099void SSL_CTX_set_verify(SSL_CTX *ctx,int mode,int (*cb)(int, X509_STORE_CTX *))
2100 {
2101 ctx->verify_mode=mode;
2102 ctx->default_verify_callback=cb;
2103 }
2104
2105void SSL_CTX_set_verify_depth(SSL_CTX *ctx,int depth)
2106 {
2107 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2108 }
2109
2110void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg), void *arg)
2111 {
2112 ssl_cert_set_cert_cb(c->cert, cb, arg);
2113 }
2114
2115void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg)
2116 {
2117 ssl_cert_set_cert_cb(s->cert, cb, arg);
2118 }
2119
2120void ssl_set_cert_masks(CERT *c, const SSL_CIPHER *cipher)
2121 {
2122 CERT_PKEY *cpk;
David Benjaminef2116d2014-08-19 20:21:56 -04002123 int rsa_enc,rsa_sign,dh_tmp;
David Benjamin060d9d22014-07-15 00:54:26 -04002124 unsigned long mask_k,mask_a;
David Benjamin060d9d22014-07-15 00:54:26 -04002125 int have_ecc_cert, ecdsa_ok;
David Benjamin0da0e182014-08-19 16:20:28 -04002126 int have_ecdh_tmp;
Adam Langley95c29f32014-06-20 12:00:00 -07002127 X509 *x = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002128 if (c == NULL) return;
2129
Adam Langley95c29f32014-06-20 12:00:00 -07002130 dh_tmp=(c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
Adam Langley95c29f32014-06-20 12:00:00 -07002131
Adam Langley95c29f32014-06-20 12:00:00 -07002132 have_ecdh_tmp=(c->ecdh_tmp || c->ecdh_tmp_cb || c->ecdh_tmp_auto);
Adam Langley95c29f32014-06-20 12:00:00 -07002133 cpk= &(c->pkeys[SSL_PKEY_RSA_ENC]);
2134 rsa_enc= cpk->valid_flags & CERT_PKEY_VALID;
Adam Langley95c29f32014-06-20 12:00:00 -07002135 cpk= &(c->pkeys[SSL_PKEY_RSA_SIGN]);
2136 rsa_sign= cpk->valid_flags & CERT_PKEY_SIGN;
Adam Langley95c29f32014-06-20 12:00:00 -07002137 cpk= &(c->pkeys[SSL_PKEY_ECC]);
Adam Langley95c29f32014-06-20 12:00:00 -07002138 have_ecc_cert= cpk->valid_flags & CERT_PKEY_VALID;
Adam Langley95c29f32014-06-20 12:00:00 -07002139 mask_k=0;
2140 mask_a=0;
Adam Langley95c29f32014-06-20 12:00:00 -07002141
2142#ifdef CIPHER_DEBUG
2143 printf("rt=%d rte=%d dht=%d ecdht=%d re=%d ree=%d rs=%d ds=%d dhr=%d dhd=%d\n",
2144 rsa_tmp,rsa_tmp_export,dh_tmp,have_ecdh_tmp,
2145 rsa_enc,rsa_enc_export,rsa_sign,dsa_sign,dh_rsa,dh_dsa);
2146#endif
2147
David Benjamin77a942b2014-07-15 01:22:50 -04002148 if (rsa_enc)
Adam Langley95c29f32014-06-20 12:00:00 -07002149 mask_k|=SSL_kRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002150
Adam Langley95c29f32014-06-20 12:00:00 -07002151 if (dh_tmp)
2152 mask_k|=SSL_kEDH;
2153
Adam Langley95c29f32014-06-20 12:00:00 -07002154 if (rsa_enc || rsa_sign)
2155 {
2156 mask_a|=SSL_aRSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002157 }
2158
Adam Langley95c29f32014-06-20 12:00:00 -07002159 mask_a|=SSL_aNULL;
Adam Langley95c29f32014-06-20 12:00:00 -07002160
David Benjamin0da0e182014-08-19 16:20:28 -04002161 /* An ECC certificate may be usable for ECDSA cipher suites depending on
2162 * the key usage extension. */
Adam Langley95c29f32014-06-20 12:00:00 -07002163 if (have_ecc_cert)
2164 {
2165 cpk = &c->pkeys[SSL_PKEY_ECC];
2166 x = cpk->x509;
2167 /* This call populates extension flags (ex_flags) */
2168 X509_check_purpose(x, -1, 0);
Adam Langley95c29f32014-06-20 12:00:00 -07002169 ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE) ?
2170 (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE) : 1;
2171 if (!(cpk->valid_flags & CERT_PKEY_SIGN))
2172 ecdsa_ok = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07002173 if (ecdsa_ok)
2174 {
2175 mask_a|=SSL_aECDSA;
Adam Langley95c29f32014-06-20 12:00:00 -07002176 }
Adam Langley95c29f32014-06-20 12:00:00 -07002177 }
Adam Langley95c29f32014-06-20 12:00:00 -07002178
Adam Langley95c29f32014-06-20 12:00:00 -07002179 if (have_ecdh_tmp)
2180 {
2181 mask_k|=SSL_kEECDH;
Adam Langley95c29f32014-06-20 12:00:00 -07002182 }
Adam Langley95c29f32014-06-20 12:00:00 -07002183
Adam Langley95c29f32014-06-20 12:00:00 -07002184 mask_k |= SSL_kPSK;
2185 mask_a |= SSL_aPSK;
Adam Langley95c29f32014-06-20 12:00:00 -07002186
2187 c->mask_k=mask_k;
2188 c->mask_a=mask_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002189 c->valid=1;
2190 }
2191
2192/* This handy macro borrowed from crypto/x509v3/v3_purp.c */
2193#define ku_reject(x, usage) \
2194 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
2195
Adam Langley95c29f32014-06-20 12:00:00 -07002196
2197int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
2198 {
David Benjamin0da0e182014-08-19 16:20:28 -04002199 unsigned long alg_a;
Adam Langley95c29f32014-06-20 12:00:00 -07002200 int signature_nid = 0, md_nid = 0, pk_nid = 0;
2201 const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
2202
Adam Langley95c29f32014-06-20 12:00:00 -07002203 alg_a = cs->algorithm_auth;
2204
Adam Langley95c29f32014-06-20 12:00:00 -07002205 /* This call populates the ex_flags field correctly */
2206 X509_check_purpose(x, -1, 0);
2207 if ((x->sig_alg) && (x->sig_alg->algorithm))
2208 {
2209 signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
2210 OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
2211 }
Adam Langley95c29f32014-06-20 12:00:00 -07002212 if (alg_a & SSL_aECDSA)
2213 {
2214 /* key usage, if present, must allow signing */
2215 if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE))
2216 {
2217 OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2218 return 0;
2219 }
2220 }
2221
2222 return 1; /* all checks are ok */
2223 }
2224
Adam Langley95c29f32014-06-20 12:00:00 -07002225
2226static int ssl_get_server_cert_index(const SSL *s)
2227 {
2228 int idx;
2229 idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
2230 if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509)
2231 idx = SSL_PKEY_RSA_SIGN;
2232 if (idx == -1)
2233 OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
2234 return idx;
2235 }
2236
2237CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
2238 {
2239 CERT *c;
2240 int i;
2241
2242 c = s->cert;
2243 ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
2244
Adam Langley95c29f32014-06-20 12:00:00 -07002245 i = ssl_get_server_cert_index(s);
2246
2247 /* This may or may not be an error. */
2248 if (i < 0)
2249 return NULL;
2250
2251 /* May be NULL. */
2252 return &c->pkeys[i];
2253 }
2254
2255EVP_PKEY *ssl_get_sign_pkey(SSL *s,const SSL_CIPHER *cipher, const EVP_MD **pmd)
2256 {
2257 unsigned long alg_a;
2258 CERT *c;
2259 int idx = -1;
2260
2261 alg_a = cipher->algorithm_auth;
2262 c=s->cert;
2263
David Benjaminef2116d2014-08-19 20:21:56 -04002264 if (alg_a & SSL_aRSA)
Adam Langley95c29f32014-06-20 12:00:00 -07002265 {
2266 if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL)
2267 idx = SSL_PKEY_RSA_SIGN;
2268 else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL)
2269 idx = SSL_PKEY_RSA_ENC;
2270 }
2271 else if ((alg_a & SSL_aECDSA) &&
2272 (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2273 idx = SSL_PKEY_ECC;
2274 if (idx == -1)
2275 {
2276 OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
2277 return(NULL);
2278 }
2279 if (pmd)
2280 *pmd = c->pkeys[idx].digest;
2281 return c->pkeys[idx].privatekey;
2282 }
2283
Adam Langley95c29f32014-06-20 12:00:00 -07002284void ssl_update_cache(SSL *s,int mode)
2285 {
2286 int i;
2287
2288 /* If the session_id_length is 0, we are not supposed to cache it,
2289 * and it would be rather hard to do anyway :-) */
2290 if (s->session->session_id_length == 0) return;
2291
David Benjamin041b58a2014-08-27 15:25:21 -04002292 i=s->initial_ctx->session_cache_mode;
Adam Langley95c29f32014-06-20 12:00:00 -07002293 if ((i & mode) && (!s->hit)
2294 && ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE)
David Benjamin041b58a2014-08-27 15:25:21 -04002295 || SSL_CTX_add_session(s->initial_ctx,s->session))
2296 && (s->initial_ctx->new_session_cb != NULL))
Adam Langley95c29f32014-06-20 12:00:00 -07002297 {
2298 CRYPTO_add(&s->session->references,1,CRYPTO_LOCK_SSL_SESSION);
David Benjamin041b58a2014-08-27 15:25:21 -04002299 if (!s->initial_ctx->new_session_cb(s,s->session))
Adam Langley95c29f32014-06-20 12:00:00 -07002300 SSL_SESSION_free(s->session);
2301 }
2302
2303 /* auto flush every 255 connections */
2304 if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) &&
2305 ((i & mode) == mode))
2306 {
2307 if ( (((mode & SSL_SESS_CACHE_CLIENT)
David Benjamin041b58a2014-08-27 15:25:21 -04002308 ?s->initial_ctx->stats.sess_connect_good
2309 :s->initial_ctx->stats.sess_accept_good) & 0xff) == 0xff)
Adam Langley95c29f32014-06-20 12:00:00 -07002310 {
David Benjamin041b58a2014-08-27 15:25:21 -04002311 SSL_CTX_flush_sessions(s->initial_ctx,(unsigned long)time(NULL));
Adam Langley95c29f32014-06-20 12:00:00 -07002312 }
2313 }
2314 }
2315
2316const SSL_METHOD *SSL_CTX_get_ssl_method(SSL_CTX *ctx)
2317 {
2318 return ctx->method;
2319 }
2320
2321const SSL_METHOD *SSL_get_ssl_method(SSL *s)
2322 {
2323 return(s->method);
2324 }
2325
2326int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
2327 {
2328 int conn= -1;
2329 int ret=1;
2330
2331 if (s->method != meth)
2332 {
2333 if (s->handshake_func != NULL)
2334 conn=(s->handshake_func == s->method->ssl_connect);
2335
2336 if (s->method->version == meth->version)
2337 s->method=meth;
2338 else
2339 {
2340 s->method->ssl_free(s);
2341 s->method=meth;
2342 ret=s->method->ssl_new(s);
2343 }
2344
2345 if (conn == 1)
2346 s->handshake_func=meth->ssl_connect;
2347 else if (conn == 0)
2348 s->handshake_func=meth->ssl_accept;
2349 }
2350 return(ret);
2351 }
2352
2353int SSL_get_error(const SSL *s,int i)
2354 {
2355 int reason;
2356 unsigned long l;
2357 BIO *bio;
2358
2359 if (i > 0) return(SSL_ERROR_NONE);
2360
2361 /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2362 * etc, where we do encode the error */
2363 if ((l=ERR_peek_error()) != 0)
2364 {
2365 if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2366 return(SSL_ERROR_SYSCALL);
2367 else
2368 return(SSL_ERROR_SSL);
2369 }
2370
Adam Langleyb2ce0582014-06-20 12:00:00 -07002371 if ((i < 0) && SSL_want_session(s))
2372 return(SSL_ERROR_PENDING_SESSION);
2373
Adam Langleydc9b1412014-06-20 12:00:00 -07002374 if ((i < 0) && SSL_want_certificate(s))
2375 return(SSL_ERROR_PENDING_CERTIFICATE);
2376
Adam Langley95c29f32014-06-20 12:00:00 -07002377 if ((i < 0) && SSL_want_read(s))
2378 {
2379 bio=SSL_get_rbio(s);
2380 if (BIO_should_read(bio))
2381 return(SSL_ERROR_WANT_READ);
2382 else if (BIO_should_write(bio))
2383 /* This one doesn't make too much sense ... We never try
2384 * to write to the rbio, and an application program where
2385 * rbio and wbio are separate couldn't even know what it
2386 * should wait for.
2387 * However if we ever set s->rwstate incorrectly
2388 * (so that we have SSL_want_read(s) instead of
2389 * SSL_want_write(s)) and rbio and wbio *are* the same,
2390 * this test works around that bug; so it might be safer
2391 * to keep it. */
2392 return(SSL_ERROR_WANT_WRITE);
2393 else if (BIO_should_io_special(bio))
2394 {
2395 reason=BIO_get_retry_reason(bio);
2396 if (reason == BIO_RR_CONNECT)
2397 return(SSL_ERROR_WANT_CONNECT);
2398 else if (reason == BIO_RR_ACCEPT)
2399 return(SSL_ERROR_WANT_ACCEPT);
2400 else
2401 return(SSL_ERROR_SYSCALL); /* unknown */
2402 }
2403 }
2404
2405 if ((i < 0) && SSL_want_write(s))
2406 {
2407 bio=SSL_get_wbio(s);
2408 if (BIO_should_write(bio))
2409 return(SSL_ERROR_WANT_WRITE);
2410 else if (BIO_should_read(bio))
2411 /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
2412 return(SSL_ERROR_WANT_READ);
2413 else if (BIO_should_io_special(bio))
2414 {
2415 reason=BIO_get_retry_reason(bio);
2416 if (reason == BIO_RR_CONNECT)
2417 return(SSL_ERROR_WANT_CONNECT);
2418 else if (reason == BIO_RR_ACCEPT)
2419 return(SSL_ERROR_WANT_ACCEPT);
2420 else
2421 return(SSL_ERROR_SYSCALL);
2422 }
2423 }
2424 if ((i < 0) && SSL_want_x509_lookup(s))
2425 {
2426 return(SSL_ERROR_WANT_X509_LOOKUP);
2427 }
Adam Langley1258b6a2014-06-20 12:00:00 -07002428 if ((i < 0) && SSL_want_channel_id_lookup(s))
2429 {
2430 return(SSL_ERROR_WANT_CHANNEL_ID_LOOKUP);
2431 }
Adam Langley95c29f32014-06-20 12:00:00 -07002432
2433 if (i == 0)
2434 {
2435 if (s->version == SSL2_VERSION)
2436 {
2437 /* assume it is the socket being closed */
2438 return(SSL_ERROR_ZERO_RETURN);
2439 }
2440 else
2441 {
2442 if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2443 (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2444 return(SSL_ERROR_ZERO_RETURN);
2445 }
2446 }
2447 return(SSL_ERROR_SYSCALL);
2448 }
2449
2450int SSL_do_handshake(SSL *s)
2451 {
2452 int ret=1;
2453
2454 if (s->handshake_func == NULL)
2455 {
2456 OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
2457 return(-1);
2458 }
2459
2460 s->method->ssl_renegotiate_check(s);
2461
2462 if (SSL_in_init(s) || SSL_in_before(s))
2463 {
2464 ret=s->handshake_func(s);
2465 }
2466 return(ret);
2467 }
2468
2469/* For the next 2 functions, SSL_clear() sets shutdown and so
2470 * one of these calls will reset it */
2471void SSL_set_accept_state(SSL *s)
2472 {
2473 s->server=1;
2474 s->shutdown=0;
2475 s->state=SSL_ST_ACCEPT|SSL_ST_BEFORE;
2476 s->handshake_func=s->method->ssl_accept;
2477 /* clear the current cipher */
2478 ssl_clear_cipher_ctx(s);
2479 ssl_clear_hash_ctx(&s->read_hash);
2480 ssl_clear_hash_ctx(&s->write_hash);
2481 }
2482
2483void SSL_set_connect_state(SSL *s)
2484 {
2485 s->server=0;
2486 s->shutdown=0;
2487 s->state=SSL_ST_CONNECT|SSL_ST_BEFORE;
2488 s->handshake_func=s->method->ssl_connect;
2489 /* clear the current cipher */
2490 ssl_clear_cipher_ctx(s);
2491 ssl_clear_hash_ctx(&s->read_hash);
2492 ssl_clear_hash_ctx(&s->write_hash);
2493 }
2494
2495int ssl_undefined_function(SSL *s)
2496 {
2497 OPENSSL_PUT_ERROR(SSL, ssl_undefined_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2498 return(0);
2499 }
2500
2501int ssl_undefined_void_function(void)
2502 {
2503 OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2504 return(0);
2505 }
2506
2507int ssl_undefined_const_function(const SSL *s)
2508 {
2509 OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2510 return(0);
2511 }
2512
2513SSL_METHOD *ssl_bad_method(int ver)
2514 {
2515 OPENSSL_PUT_ERROR(SSL, ssl_bad_method, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2516 return(NULL);
2517 }
2518
Adam Langley0f4746e2014-08-13 12:26:32 -07002519static const char *ssl_get_version(int version)
Adam Langley95c29f32014-06-20 12:00:00 -07002520 {
Adam Langley0f4746e2014-08-13 12:26:32 -07002521 if (version == TLS1_2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002522 return("TLSv1.2");
Adam Langley0f4746e2014-08-13 12:26:32 -07002523 else if (version == TLS1_1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002524 return("TLSv1.1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002525 else if (version == TLS1_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002526 return("TLSv1");
Adam Langley0f4746e2014-08-13 12:26:32 -07002527 else if (version == SSL3_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002528 return("SSLv3");
Adam Langley0f4746e2014-08-13 12:26:32 -07002529 else if (version == SSL2_VERSION)
Adam Langley95c29f32014-06-20 12:00:00 -07002530 return("SSLv2");
2531 else
2532 return("unknown");
2533 }
2534
Adam Langley0f4746e2014-08-13 12:26:32 -07002535const char *SSL_get_version(const SSL *s)
2536 {
2537 return ssl_get_version(s->version);
2538 }
2539
2540const char *SSL_SESSION_get_version(const SSL_SESSION *sess)
2541 {
2542 return ssl_get_version(sess->ssl_version);
2543 }
2544
Adam Langley95c29f32014-06-20 12:00:00 -07002545void ssl_clear_cipher_ctx(SSL *s)
2546 {
2547 if (s->enc_read_ctx != NULL)
2548 {
2549 EVP_CIPHER_CTX_cleanup(s->enc_read_ctx);
2550 OPENSSL_free(s->enc_read_ctx);
2551 s->enc_read_ctx=NULL;
2552 }
2553 if (s->enc_write_ctx != NULL)
2554 {
2555 EVP_CIPHER_CTX_cleanup(s->enc_write_ctx);
2556 OPENSSL_free(s->enc_write_ctx);
2557 s->enc_write_ctx=NULL;
2558 }
Adam Langleyc9fb3752014-06-20 12:00:00 -07002559 if (s->aead_read_ctx != NULL)
2560 {
2561 EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
2562 OPENSSL_free(s->aead_read_ctx);
2563 s->aead_read_ctx = NULL;
2564 }
2565 if (s->aead_write_ctx != NULL)
2566 {
2567 EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
2568 OPENSSL_free(s->aead_write_ctx);
2569 s->aead_write_ctx = NULL;
2570 }
Adam Langley95c29f32014-06-20 12:00:00 -07002571 }
2572
2573X509 *SSL_get_certificate(const SSL *s)
2574 {
2575 if (s->cert != NULL)
2576 return(s->cert->key->x509);
2577 else
2578 return(NULL);
2579 }
2580
2581EVP_PKEY *SSL_get_privatekey(const SSL *s)
2582 {
2583 if (s->cert != NULL)
2584 return(s->cert->key->privatekey);
2585 else
2586 return(NULL);
2587 }
2588
2589X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
2590 {
2591 if (ctx->cert != NULL)
2592 return ctx->cert->key->x509;
2593 else
2594 return NULL;
2595 }
2596
2597EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
2598 {
2599 if (ctx->cert != NULL)
2600 return ctx->cert->key->privatekey;
2601 else
2602 return NULL ;
2603 }
2604
2605const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
2606 {
2607 if ((s->session != NULL) && (s->session->cipher != NULL))
2608 return(s->session->cipher);
2609 return(NULL);
2610 }
2611const void *SSL_get_current_compression(SSL *s)
2612 {
2613 return NULL;
2614 }
2615const void *SSL_get_current_expansion(SSL *s)
2616 {
2617 return NULL;
2618 }
2619
2620int ssl_init_wbio_buffer(SSL *s,int push)
2621 {
2622 BIO *bbio;
2623
2624 if (s->bbio == NULL)
2625 {
2626 bbio=BIO_new(BIO_f_buffer());
2627 if (bbio == NULL) return(0);
2628 s->bbio=bbio;
2629 }
2630 else
2631 {
2632 bbio=s->bbio;
2633 if (s->bbio == s->wbio)
2634 s->wbio=BIO_pop(s->wbio);
2635 }
2636 (void)BIO_reset(bbio);
2637/* if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2638 if (!BIO_set_read_buffer_size(bbio,1))
2639 {
2640 OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
2641 return(0);
2642 }
2643 if (push)
2644 {
2645 if (s->wbio != bbio)
2646 s->wbio=BIO_push(bbio,s->wbio);
2647 }
2648 else
2649 {
2650 if (s->wbio == bbio)
2651 s->wbio=BIO_pop(bbio);
2652 }
2653 return(1);
2654 }
2655
2656void ssl_free_wbio_buffer(SSL *s)
2657 {
2658 if (s->bbio == NULL) return;
2659
2660 if (s->bbio == s->wbio)
2661 {
2662 /* remove buffering */
2663 s->wbio=BIO_pop(s->wbio);
2664#ifdef REF_CHECK /* not the usual REF_CHECK, but this avoids adding one more preprocessor symbol */
2665 assert(s->wbio != NULL);
2666#endif
2667 }
2668 BIO_free(s->bbio);
2669 s->bbio=NULL;
2670 }
2671
2672void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx,int mode)
2673 {
2674 ctx->quiet_shutdown=mode;
2675 }
2676
2677int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2678 {
2679 return(ctx->quiet_shutdown);
2680 }
2681
2682void SSL_set_quiet_shutdown(SSL *s,int mode)
2683 {
2684 s->quiet_shutdown=mode;
2685 }
2686
2687int SSL_get_quiet_shutdown(const SSL *s)
2688 {
2689 return(s->quiet_shutdown);
2690 }
2691
2692void SSL_set_shutdown(SSL *s,int mode)
2693 {
2694 s->shutdown=mode;
2695 }
2696
2697int SSL_get_shutdown(const SSL *s)
2698 {
2699 return(s->shutdown);
2700 }
2701
2702int SSL_version(const SSL *s)
2703 {
2704 return(s->version);
2705 }
2706
2707SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
2708 {
2709 return(ssl->ctx);
2710 }
2711
2712SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2713 {
2714 if (ssl->ctx == ctx)
2715 return ssl->ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002716 if (ctx == NULL)
2717 ctx = ssl->initial_ctx;
Adam Langley95c29f32014-06-20 12:00:00 -07002718 if (ssl->cert != NULL)
2719 ssl_cert_free(ssl->cert);
2720 ssl->cert = ssl_cert_dup(ctx->cert);
2721 CRYPTO_add(&ctx->references,1,CRYPTO_LOCK_SSL_CTX);
2722 if (ssl->ctx != NULL)
2723 SSL_CTX_free(ssl->ctx); /* decrement reference count */
2724 ssl->ctx = ctx;
Adam Langleya5dc5452014-06-20 12:00:00 -07002725
2726 ssl->sid_ctx_length = ctx->sid_ctx_length;
2727 assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
2728 memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
2729
Adam Langley95c29f32014-06-20 12:00:00 -07002730 return(ssl->ctx);
2731 }
2732
2733#ifndef OPENSSL_NO_STDIO
2734int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2735 {
2736 return(X509_STORE_set_default_paths(ctx->cert_store));
2737 }
2738
2739int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2740 const char *CApath)
2741 {
2742 return(X509_STORE_load_locations(ctx->cert_store,CAfile,CApath));
2743 }
2744#endif
2745
2746void SSL_set_info_callback(SSL *ssl,
2747 void (*cb)(const SSL *ssl,int type,int val))
2748 {
2749 ssl->info_callback=cb;
2750 }
2751
2752/* One compiler (Diab DCC) doesn't like argument names in returned
2753 function pointer. */
2754void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/,int /*type*/,int /*val*/)
2755 {
2756 return ssl->info_callback;
2757 }
2758
2759int SSL_state(const SSL *ssl)
2760 {
2761 return(ssl->state);
2762 }
2763
2764void SSL_set_state(SSL *ssl, int state)
2765 {
2766 ssl->state = state;
2767 }
2768
2769void SSL_set_verify_result(SSL *ssl,long arg)
2770 {
2771 ssl->verify_result=arg;
2772 }
2773
2774long SSL_get_verify_result(const SSL *ssl)
2775 {
2776 return(ssl->verify_result);
2777 }
2778
2779int SSL_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2780 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2781 {
2782 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
2783 new_func, dup_func, free_func);
2784 }
2785
2786int SSL_set_ex_data(SSL *s,int idx,void *arg)
2787 {
2788 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2789 }
2790
2791void *SSL_get_ex_data(const SSL *s,int idx)
2792 {
2793 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2794 }
2795
2796int SSL_CTX_get_ex_new_index(long argl,void *argp,CRYPTO_EX_new *new_func,
2797 CRYPTO_EX_dup *dup_func,CRYPTO_EX_free *free_func)
2798 {
2799 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
2800 new_func, dup_func, free_func);
2801 }
2802
2803int SSL_CTX_set_ex_data(SSL_CTX *s,int idx,void *arg)
2804 {
2805 return(CRYPTO_set_ex_data(&s->ex_data,idx,arg));
2806 }
2807
2808void *SSL_CTX_get_ex_data(const SSL_CTX *s,int idx)
2809 {
2810 return(CRYPTO_get_ex_data(&s->ex_data,idx));
2811 }
2812
2813int ssl_ok(SSL *s)
2814 {
2815 return(1);
2816 }
2817
2818X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
2819 {
2820 return(ctx->cert_store);
2821 }
2822
2823void SSL_CTX_set_cert_store(SSL_CTX *ctx,X509_STORE *store)
2824 {
2825 if (ctx->cert_store != NULL)
2826 X509_STORE_free(ctx->cert_store);
2827 ctx->cert_store=store;
2828 }
2829
2830int SSL_want(const SSL *s)
2831 {
2832 return(s->rwstate);
2833 }
2834
2835/*!
2836 * \brief Set the callback for generating temporary RSA keys.
2837 * \param ctx the SSL context.
2838 * \param cb the callback
2839 */
2840
Adam Langley95c29f32014-06-20 12:00:00 -07002841void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,RSA *(*cb)(SSL *ssl,
2842 int is_export,
2843 int keylength))
2844 {
2845 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2846 }
2847
2848void SSL_set_tmp_rsa_callback(SSL *ssl,RSA *(*cb)(SSL *ssl,
2849 int is_export,
2850 int keylength))
2851 {
2852 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
2853 }
Adam Langley95c29f32014-06-20 12:00:00 -07002854
2855#ifdef DOXYGEN
2856/*!
2857 * \brief The RSA temporary key callback function.
2858 * \param ssl the SSL session.
2859 * \param is_export \c TRUE if the temp RSA key is for an export ciphersuite.
2860 * \param keylength if \c is_export is \c TRUE, then \c keylength is the size
2861 * of the required key in bits.
2862 * \return the temporary RSA key.
2863 * \sa SSL_CTX_set_tmp_rsa_callback, SSL_set_tmp_rsa_callback
2864 */
2865
2866RSA *cb(SSL *ssl,int is_export,int keylength)
2867 {}
2868#endif
2869
2870/*!
2871 * \brief Set the callback for generating temporary DH keys.
2872 * \param ctx the SSL context.
2873 * \param dh the callback
2874 */
2875
Adam Langley95c29f32014-06-20 12:00:00 -07002876void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,DH *(*dh)(SSL *ssl,int is_export,
2877 int keylength))
2878 {
2879 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2880 }
2881
2882void SSL_set_tmp_dh_callback(SSL *ssl,DH *(*dh)(SSL *ssl,int is_export,
2883 int keylength))
2884 {
2885 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
2886 }
Adam Langley95c29f32014-06-20 12:00:00 -07002887
Adam Langley95c29f32014-06-20 12:00:00 -07002888void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2889 int keylength))
2890 {
2891 SSL_CTX_callback_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2892 }
2893
2894void SSL_set_tmp_ecdh_callback(SSL *ssl,EC_KEY *(*ecdh)(SSL *ssl,int is_export,
2895 int keylength))
2896 {
2897 SSL_callback_ctrl(ssl,SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
2898 }
Adam Langley95c29f32014-06-20 12:00:00 -07002899
Adam Langley95c29f32014-06-20 12:00:00 -07002900int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
2901 {
2902 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2903 {
2904 OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2905 return 0;
2906 }
2907 if (ctx->psk_identity_hint != NULL)
2908 OPENSSL_free(ctx->psk_identity_hint);
2909 if (identity_hint != NULL)
2910 {
2911 ctx->psk_identity_hint = BUF_strdup(identity_hint);
2912 if (ctx->psk_identity_hint == NULL)
2913 return 0;
2914 }
2915 else
2916 ctx->psk_identity_hint = NULL;
2917 return 1;
2918 }
2919
2920int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
2921 {
2922 if (s == NULL)
2923 return 0;
2924
Adam Langley95c29f32014-06-20 12:00:00 -07002925 if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN)
2926 {
2927 OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint, SSL_R_DATA_LENGTH_TOO_LONG);
2928 return 0;
2929 }
Adam Langley0289c732014-06-20 12:00:00 -07002930
2931 /* Clear hint in SSL and associated SSL_SESSION (if any). */
2932 if (s->psk_identity_hint != NULL)
2933 {
2934 OPENSSL_free(s->psk_identity_hint);
2935 s->psk_identity_hint = NULL;
2936 }
2937 if (s->session != NULL && s->session->psk_identity_hint != NULL)
2938 {
Adam Langley95c29f32014-06-20 12:00:00 -07002939 OPENSSL_free(s->session->psk_identity_hint);
Adam Langley0289c732014-06-20 12:00:00 -07002940 s->session->psk_identity_hint = NULL;
2941 }
2942
Adam Langley95c29f32014-06-20 12:00:00 -07002943 if (identity_hint != NULL)
2944 {
Adam Langley0289c732014-06-20 12:00:00 -07002945 /* The hint is stored in SSL and SSL_SESSION with the one in
2946 * SSL_SESSION taking precedence. Thus, if SSL_SESSION is avaiable,
2947 * we store the hint there, otherwise we store it in SSL. */
2948 if (s->session != NULL)
2949 {
2950 s->session->psk_identity_hint = BUF_strdup(identity_hint);
2951 if (s->session->psk_identity_hint == NULL)
2952 return 0;
2953 }
2954 else
2955 {
2956 s->psk_identity_hint = BUF_strdup(identity_hint);
2957 if (s->psk_identity_hint == NULL)
2958 return 0;
2959 }
Adam Langley95c29f32014-06-20 12:00:00 -07002960 }
Adam Langley95c29f32014-06-20 12:00:00 -07002961 return 1;
2962 }
2963
2964const char *SSL_get_psk_identity_hint(const SSL *s)
2965 {
Adam Langley0289c732014-06-20 12:00:00 -07002966 if (s == NULL)
Adam Langley95c29f32014-06-20 12:00:00 -07002967 return NULL;
Adam Langley0289c732014-06-20 12:00:00 -07002968 /* The hint is stored in SSL and SSL_SESSION with the one in SSL_SESSION
2969 * taking precedence. */
2970 if (s->session != NULL)
2971 return(s->session->psk_identity_hint);
2972 return(s->psk_identity_hint);
Adam Langley95c29f32014-06-20 12:00:00 -07002973 }
2974
2975const char *SSL_get_psk_identity(const SSL *s)
2976 {
2977 if (s == NULL || s->session == NULL)
2978 return NULL;
2979 return(s->session->psk_identity);
2980 }
2981
2982void SSL_set_psk_client_callback(SSL *s,
2983 unsigned int (*cb)(SSL *ssl, const char *hint,
2984 char *identity, unsigned int max_identity_len, unsigned char *psk,
2985 unsigned int max_psk_len))
2986 {
2987 s->psk_client_callback = cb;
2988 }
2989
2990void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx,
2991 unsigned int (*cb)(SSL *ssl, const char *hint,
2992 char *identity, unsigned int max_identity_len, unsigned char *psk,
2993 unsigned int max_psk_len))
2994 {
2995 ctx->psk_client_callback = cb;
2996 }
2997
2998void SSL_set_psk_server_callback(SSL *s,
2999 unsigned int (*cb)(SSL *ssl, const char *identity,
3000 unsigned char *psk, unsigned int max_psk_len))
3001 {
3002 s->psk_server_callback = cb;
3003 }
3004
3005void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx,
3006 unsigned int (*cb)(SSL *ssl, const char *identity,
3007 unsigned char *psk, unsigned int max_psk_len))
3008 {
3009 ctx->psk_server_callback = cb;
3010 }
Adam Langley95c29f32014-06-20 12:00:00 -07003011
3012void 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))
3013 {
3014 SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3015 }
3016void 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))
3017 {
3018 SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3019 }
3020
David Benjamin859ec3c2014-09-02 16:29:36 -04003021void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio)
3022 {
3023 if (ctx->keylog_bio != NULL)
3024 BIO_free(ctx->keylog_bio);
3025 ctx->keylog_bio = keylog_bio;
3026 }
3027
3028static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len)
3029 {
3030 static const char hextable[] = "0123456789abcdef";
3031 uint8_t *out;
3032 size_t i;
3033
3034 if (!CBB_add_space(cbb, &out, in_len * 2))
3035 {
3036 return 0;
3037 }
3038
3039 for (i = 0; i < in_len; i++)
3040 {
3041 *(out++) = (uint8_t)hextable[in[i] >> 4];
3042 *(out++) = (uint8_t)hextable[in[i] & 0xf];
3043 }
3044 return 1;
3045 }
3046
3047int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
3048 const uint8_t *encrypted_premaster, size_t encrypted_premaster_len,
3049 const uint8_t *premaster, size_t premaster_len)
3050 {
3051 BIO *bio = ctx->keylog_bio;
3052 CBB cbb;
3053 uint8_t *out;
3054 size_t out_len;
3055 int ret;
3056
3057 if (bio == NULL)
3058 {
3059 return 1;
3060 }
3061
3062 if (encrypted_premaster_len < 8)
3063 {
3064 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange, ERR_R_INTERNAL_ERROR);
3065 return 0;
3066 }
3067
3068 if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len*2 + 1))
3069 {
3070 return 0;
3071 }
3072 if (!CBB_add_bytes(&cbb, (const uint8_t*)"RSA ", 4) ||
3073 /* Only the first 8 bytes of the encrypted premaster secret are
3074 * logged. */
3075 !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
3076 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3077 !cbb_add_hex(&cbb, premaster, premaster_len) ||
3078 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3079 !CBB_finish(&cbb, &out, &out_len))
3080 {
3081 CBB_cleanup(&cbb);
3082 return 0;
3083 }
3084
3085 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3086 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3087 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3088
3089 OPENSSL_free(out);
3090 return ret;
3091 }
3092
3093int ssl_ctx_log_master_secret(SSL_CTX *ctx,
3094 const uint8_t *client_random, size_t client_random_len,
3095 const uint8_t *master, size_t master_len)
3096 {
3097 BIO *bio = ctx->keylog_bio;
3098 CBB cbb;
3099 uint8_t *out;
3100 size_t out_len;
3101 int ret;
3102
3103 if (bio == NULL)
3104 {
3105 return 1;
3106 }
3107
3108 if (client_random_len != 32)
3109 {
3110 OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
3111 return 0;
3112 }
3113
3114 if (!CBB_init(&cbb, 14 + 64 + 1 + master_len*2 + 1))
3115 {
3116 return 0;
3117 }
3118 if (!CBB_add_bytes(&cbb, (const uint8_t*)"CLIENT_RANDOM ", 14) ||
3119 !cbb_add_hex(&cbb, client_random, 32) ||
3120 !CBB_add_bytes(&cbb, (const uint8_t*)" ", 1) ||
3121 !cbb_add_hex(&cbb, master, master_len) ||
3122 !CBB_add_bytes(&cbb, (const uint8_t*)"\n", 1) ||
3123 !CBB_finish(&cbb, &out, &out_len))
3124 {
3125 CBB_cleanup(&cbb);
3126 return 0;
3127 }
3128
3129 CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
3130 ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
3131 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
3132
3133 OPENSSL_free(out);
3134 return ret;
3135 }
3136
Adam Langleyadb739e2014-06-20 12:00:00 -07003137int SSL_cutthrough_complete(const SSL *s)
3138 {
3139 return (!s->server && /* cutthrough only applies to clients */
3140 !s->hit && /* full-handshake */
3141 s->version >= SSL3_VERSION &&
3142 s->s3->in_read_app_data == 0 && /* cutthrough only applies to write() */
3143 (SSL_get_mode((SSL*)s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && /* cutthrough enabled */
3144 ssl3_can_cutthrough(s) && /* cutthrough allowed */
3145 s->s3->previous_server_finished_len == 0 && /* not a renegotiation handshake */
3146 (s->state == SSL3_ST_CR_SESSION_TICKET_A || /* ready to write app-data*/
David Benjamin7e3305e2014-07-28 14:52:32 -04003147 s->state == SSL3_ST_CR_CHANGE ||
Adam Langleyadb739e2014-06-20 12:00:00 -07003148 s->state == SSL3_ST_CR_FINISHED_A));
3149 }
3150
Adam Langley95f22882014-06-20 12:00:00 -07003151void SSL_get_structure_sizes(size_t* ssl_size, size_t* ssl_ctx_size,
3152 size_t* ssl_session_size)
3153{
3154 *ssl_size = sizeof(SSL);
3155 *ssl_ctx_size = sizeof(SSL_CTX);
3156 *ssl_session_size = sizeof(SSL_SESSION);
3157}
3158
Adam Langleyadb739e2014-06-20 12:00:00 -07003159int ssl3_can_cutthrough(const SSL *s)
3160 {
3161 const SSL_CIPHER *c;
3162
3163 /* require a strong enough cipher */
3164 if (SSL_get_cipher_bits(s, NULL) < 128)
3165 return 0;
3166
3167 /* require ALPN or NPN extension */
David Benjamin457112e2014-08-27 14:35:09 -04003168 if (!s->s3->alpn_selected && !s->s3->next_proto_neg_seen)
Adam Langleyadb739e2014-06-20 12:00:00 -07003169 {
3170 return 0;
3171 }
3172
3173 /* require a forward-secret cipher */
3174 c = SSL_get_current_cipher(s);
3175 if (!c || (c->algorithm_mkey != SSL_kEDH &&
3176 c->algorithm_mkey != SSL_kEECDH))
3177 {
3178 return 0;
3179 }
3180
3181 return 1;
3182 }
3183
Adam Langley29707792014-06-20 12:00:00 -07003184/* ssl_get_max_version returns the maximum SSL/TLS version number supported by
3185 * |s|, or zero if all versions are disabled. */
3186int ssl_get_max_version(const SSL *s)
3187 {
3188 /* Only one version supported for DTLS. */
3189 if (s->version == DTLS1_VERSION)
3190 return DTLS1_VERSION;
3191
3192 if (!(s->options & SSL_OP_NO_TLSv1_2))
3193 return TLS1_2_VERSION;
3194 if (!(s->options & SSL_OP_NO_TLSv1_1))
3195 return TLS1_1_VERSION;
3196 if (!(s->options & SSL_OP_NO_TLSv1))
3197 return TLS1_VERSION;
3198 if (!(s->options & SSL_OP_NO_SSLv3))
3199 return SSL3_VERSION;
3200 if (!(s->options & SSL_OP_NO_SSLv2))
3201 return SSL2_VERSION;
3202 return 0;
3203 }
3204
Adam Langley95c29f32014-06-20 12:00:00 -07003205/* Allocates new EVP_MD_CTX and sets pointer to it into given pointer
3206 * vairable, freeing EVP_MD_CTX previously stored in that variable, if
3207 * any. If EVP_MD pointer is passed, initializes ctx with this md
3208 * Returns newly allocated ctx;
3209 */
3210
3211EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash,const EVP_MD *md)
3212{
3213 ssl_clear_hash_ctx(hash);
3214 *hash = EVP_MD_CTX_create();
3215 if (md) EVP_DigestInit_ex(*hash,md,NULL);
3216 return *hash;
3217}
3218void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
3219{
3220
3221 if (*hash) EVP_MD_CTX_destroy(*hash);
3222 *hash=NULL;
3223}
3224
3225void SSL_set_debug(SSL *s, int debug)
3226 {
3227 s->debug = debug;
3228 }
3229
3230int SSL_cache_hit(SSL *s)
3231 {
3232 return s->hit;
3233 }
3234
3235int SSL_is_server(SSL *s)
3236 {
3237 return s->server;
3238 }