Coverage Report

Created: 2022-08-24 06:30

/src/libressl/ssl/ssl_lib.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: ssl_lib.c,v 1.300 2022/07/24 15:05:16 jsing Exp $ */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3
 * All rights reserved.
4
 *
5
 * This package is an SSL implementation written
6
 * by Eric Young (eay@cryptsoft.com).
7
 * The implementation was written so as to conform with Netscapes SSL.
8
 *
9
 * This library is free for commercial and non-commercial use as long as
10
 * the following conditions are aheared to.  The following conditions
11
 * apply to all code found in this distribution, be it the RC4, RSA,
12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13
 * included with this distribution is covered by the same copyright terms
14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15
 *
16
 * Copyright remains Eric Young's, and as such any Copyright notices in
17
 * the code are not to be removed.
18
 * If this package is used in a product, Eric Young should be given attribution
19
 * as the author of the parts of the library used.
20
 * This can be in the form of a textual message at program startup or
21
 * in documentation (online or textual) provided with the package.
22
 *
23
 * Redistribution and use in source and binary forms, with or without
24
 * modification, are permitted provided that the following conditions
25
 * are met:
26
 * 1. Redistributions of source code must retain the copyright
27
 *    notice, this list of conditions and the following disclaimer.
28
 * 2. Redistributions in binary form must reproduce the above copyright
29
 *    notice, this list of conditions and the following disclaimer in the
30
 *    documentation and/or other materials provided with the distribution.
31
 * 3. All advertising materials mentioning features or use of this software
32
 *    must display the following acknowledgement:
33
 *    "This product includes cryptographic software written by
34
 *     Eric Young (eay@cryptsoft.com)"
35
 *    The word 'cryptographic' can be left out if the rouines from the library
36
 *    being used are not cryptographic related :-).
37
 * 4. If you include any Windows specific code (or a derivative thereof) from
38
 *    the apps directory (application code) you must include an acknowledgement:
39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51
 * SUCH DAMAGE.
52
 *
53
 * The licence and distribution terms for any publically available version or
54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55
 * copied and put under another distribution licence
56
 * [including the GNU Public Licence.]
57
 */
58
/* ====================================================================
59
 * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
60
 *
61
 * Redistribution and use in source and binary forms, with or without
62
 * modification, are permitted provided that the following conditions
63
 * are met:
64
 *
65
 * 1. Redistributions of source code must retain the above copyright
66
 *    notice, this list of conditions and the following disclaimer.
67
 *
68
 * 2. Redistributions in binary form must reproduce the above copyright
69
 *    notice, this list of conditions and the following disclaimer in
70
 *    the documentation and/or other materials provided with the
71
 *    distribution.
72
 *
73
 * 3. All advertising materials mentioning features or use of this
74
 *    software must display the following acknowledgment:
75
 *    "This product includes software developed by the OpenSSL Project
76
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77
 *
78
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79
 *    endorse or promote products derived from this software without
80
 *    prior written permission. For written permission, please contact
81
 *    openssl-core@openssl.org.
82
 *
83
 * 5. Products derived from this software may not be called "OpenSSL"
84
 *    nor may "OpenSSL" appear in their names without prior written
85
 *    permission of the OpenSSL Project.
86
 *
87
 * 6. Redistributions of any form whatsoever must retain the following
88
 *    acknowledgment:
89
 *    "This product includes software developed by the OpenSSL Project
90
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91
 *
92
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103
 * OF THE POSSIBILITY OF SUCH DAMAGE.
104
 * ====================================================================
105
 *
106
 * This product includes cryptographic software written by Eric Young
107
 * (eay@cryptsoft.com).  This product includes software written by Tim
108
 * Hudson (tjh@cryptsoft.com).
109
 *
110
 */
111
/* ====================================================================
112
 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113
 * ECC cipher suite support in OpenSSL originally developed by
114
 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
115
 */
116
/* ====================================================================
117
 * Copyright 2005 Nokia. All rights reserved.
118
 *
119
 * The portions of the attached software ("Contribution") is developed by
120
 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
121
 * license.
122
 *
123
 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
124
 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
125
 * support (see RFC 4279) to OpenSSL.
126
 *
127
 * No patent licenses or other rights except those expressly stated in
128
 * the OpenSSL open source license shall be deemed granted or received
129
 * expressly, by implication, estoppel, or otherwise.
130
 *
131
 * No assurances are provided by Nokia that the Contribution does not
132
 * infringe the patent or other intellectual property rights of any third
133
 * party or that the license provides you with all the necessary rights
134
 * to make use of the Contribution.
135
 *
136
 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
137
 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
138
 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
139
 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
140
 * OTHERWISE.
141
 */
142
143
#include <arpa/inet.h>
144
#include <sys/socket.h>
145
#include <netinet/in.h>
146
147
#include <limits.h>
148
#include <stdio.h>
149
150
#include <openssl/dh.h>
151
#include <openssl/lhash.h>
152
#include <openssl/objects.h>
153
#include <openssl/ocsp.h>
154
#include <openssl/opensslconf.h>
155
#include <openssl/x509v3.h>
156
157
#ifndef OPENSSL_NO_ENGINE
158
#include <openssl/engine.h>
159
#endif
160
161
#include "bytestring.h"
162
#include "dtls_locl.h"
163
#include "ssl_locl.h"
164
#include "ssl_sigalgs.h"
165
#include "ssl_tlsext.h"
166
167
const char *SSL_version_str = OPENSSL_VERSION_TEXT;
168
169
int
170
SSL_clear(SSL *s)
171
6.07k
{
172
6.07k
  if (s->method == NULL) {
173
0
    SSLerror(s, SSL_R_NO_METHOD_SPECIFIED);
174
0
    return (0);
175
0
  }
176
177
6.07k
  if (ssl_clear_bad_session(s)) {
178
0
    SSL_SESSION_free(s->session);
179
0
    s->session = NULL;
180
0
  }
181
182
6.07k
  s->error = 0;
183
6.07k
  s->internal->hit = 0;
184
6.07k
  s->internal->shutdown = 0;
185
186
6.07k
  if (s->internal->renegotiate) {
187
0
    SSLerror(s, ERR_R_INTERNAL_ERROR);
188
0
    return (0);
189
0
  }
190
191
6.07k
  s->version = s->method->version;
192
6.07k
  s->client_version = s->version;
193
6.07k
  s->internal->rwstate = SSL_NOTHING;
194
6.07k
  s->internal->rstate = SSL_ST_READ_HEADER;
195
196
6.07k
  tls13_ctx_free(s->internal->tls13);
197
6.07k
  s->internal->tls13 = NULL;
198
199
6.07k
  ssl3_release_init_buffer(s);
200
201
6.07k
  ssl_clear_cipher_state(s);
202
203
6.07k
  s->internal->first_packet = 0;
204
205
  /*
206
   * Check to see if we were changed into a different method, if
207
   * so, revert back if we are not doing session-id reuse.
208
   */
209
6.07k
  if (!s->internal->in_handshake && (s->session == NULL) &&
210
6.07k
      (s->method != s->ctx->method)) {
211
0
    s->method->ssl_free(s);
212
0
    s->method = s->ctx->method;
213
0
    if (!s->method->ssl_new(s))
214
0
      return (0);
215
0
  } else
216
6.07k
    s->method->ssl_clear(s);
217
218
6.07k
  return (1);
219
6.07k
}
220
221
/* Used to change an SSL_CTXs default SSL method type */
222
int
223
SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
224
0
{
225
0
  STACK_OF(SSL_CIPHER) *ciphers;
226
227
0
  ctx->method = meth;
228
229
0
  ciphers = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
230
0
      ctx->internal->cipher_list_tls13, SSL_DEFAULT_CIPHER_LIST,
231
0
      ctx->internal->cert);
232
0
  if (ciphers == NULL || sk_SSL_CIPHER_num(ciphers) <= 0) {
233
0
    SSLerrorx(SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
234
0
    return (0);
235
0
  }
236
0
  return (1);
237
0
}
238
239
SSL *
240
SSL_new(SSL_CTX *ctx)
241
6.07k
{
242
6.07k
  SSL *s;
243
6.07k
  CBS cbs;
244
245
6.07k
  if (ctx == NULL) {
246
0
    SSLerrorx(SSL_R_NULL_SSL_CTX);
247
0
    return (NULL);
248
0
  }
249
6.07k
  if (ctx->method == NULL) {
250
0
    SSLerrorx(SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
251
0
    return (NULL);
252
0
  }
253
254
6.07k
  if ((s = calloc(1, sizeof(*s))) == NULL)
255
0
    goto err;
256
6.07k
  if ((s->internal = calloc(1, sizeof(*s->internal))) == NULL)
257
0
    goto err;
258
259
6.07k
  if ((s->internal->rl = tls12_record_layer_new()) == NULL)
260
0
    goto err;
261
262
6.07k
  s->internal->min_tls_version = ctx->internal->min_tls_version;
263
6.07k
  s->internal->max_tls_version = ctx->internal->max_tls_version;
264
6.07k
  s->internal->min_proto_version = ctx->internal->min_proto_version;
265
6.07k
  s->internal->max_proto_version = ctx->internal->max_proto_version;
266
267
6.07k
  s->internal->options = ctx->internal->options;
268
6.07k
  s->internal->mode = ctx->internal->mode;
269
6.07k
  s->internal->max_cert_list = ctx->internal->max_cert_list;
270
6.07k
  s->internal->num_tickets = ctx->internal->num_tickets;
271
272
6.07k
  if ((s->cert = ssl_cert_dup(ctx->internal->cert)) == NULL)
273
0
    goto err;
274
275
6.07k
  s->internal->read_ahead = ctx->internal->read_ahead;
276
6.07k
  s->internal->msg_callback = ctx->internal->msg_callback;
277
6.07k
  s->internal->msg_callback_arg = ctx->internal->msg_callback_arg;
278
6.07k
  s->verify_mode = ctx->verify_mode;
279
6.07k
  s->sid_ctx_length = ctx->sid_ctx_length;
280
6.07k
  OPENSSL_assert(s->sid_ctx_length <= sizeof s->sid_ctx);
281
6.07k
  memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
282
6.07k
  s->internal->verify_callback = ctx->internal->default_verify_callback;
283
6.07k
  s->internal->generate_session_id = ctx->internal->generate_session_id;
284
285
6.07k
  s->param = X509_VERIFY_PARAM_new();
286
6.07k
  if (!s->param)
287
0
    goto err;
288
6.07k
  X509_VERIFY_PARAM_inherit(s->param, ctx->param);
289
6.07k
  s->internal->quiet_shutdown = ctx->internal->quiet_shutdown;
290
6.07k
  s->max_send_fragment = ctx->internal->max_send_fragment;
291
292
6.07k
  CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
293
6.07k
  s->ctx = ctx;
294
6.07k
  s->internal->tlsext_debug_cb = 0;
295
6.07k
  s->internal->tlsext_debug_arg = NULL;
296
6.07k
  s->internal->tlsext_ticket_expected = 0;
297
6.07k
  s->tlsext_status_type = -1;
298
6.07k
  s->internal->tlsext_status_expected = 0;
299
6.07k
  s->internal->tlsext_ocsp_ids = NULL;
300
6.07k
  s->internal->tlsext_ocsp_exts = NULL;
301
6.07k
  s->internal->tlsext_ocsp_resp = NULL;
302
6.07k
  s->internal->tlsext_ocsp_resp_len = 0;
303
6.07k
  CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
304
6.07k
  s->initial_ctx = ctx;
305
306
6.07k
  if (ctx->internal->tlsext_ecpointformatlist != NULL) {
307
0
    s->internal->tlsext_ecpointformatlist =
308
0
        calloc(ctx->internal->tlsext_ecpointformatlist_length,
309
0
      sizeof(ctx->internal->tlsext_ecpointformatlist[0]));
310
0
    if (s->internal->tlsext_ecpointformatlist == NULL)
311
0
      goto err;
312
0
    memcpy(s->internal->tlsext_ecpointformatlist,
313
0
        ctx->internal->tlsext_ecpointformatlist,
314
0
        ctx->internal->tlsext_ecpointformatlist_length *
315
0
        sizeof(ctx->internal->tlsext_ecpointformatlist[0]));
316
0
    s->internal->tlsext_ecpointformatlist_length =
317
0
        ctx->internal->tlsext_ecpointformatlist_length;
318
0
  }
319
6.07k
  if (ctx->internal->tlsext_supportedgroups != NULL) {
320
0
    s->internal->tlsext_supportedgroups =
321
0
        calloc(ctx->internal->tlsext_supportedgroups_length,
322
0
      sizeof(ctx->internal->tlsext_supportedgroups[0]));
323
0
    if (s->internal->tlsext_supportedgroups == NULL)
324
0
      goto err;
325
0
    memcpy(s->internal->tlsext_supportedgroups,
326
0
        ctx->internal->tlsext_supportedgroups,
327
0
        ctx->internal->tlsext_supportedgroups_length *
328
0
        sizeof(ctx->internal->tlsext_supportedgroups[0]));
329
0
    s->internal->tlsext_supportedgroups_length =
330
0
        ctx->internal->tlsext_supportedgroups_length;
331
0
  }
332
333
6.07k
  CBS_init(&cbs, ctx->internal->alpn_client_proto_list,
334
6.07k
      ctx->internal->alpn_client_proto_list_len);
335
6.07k
  if (!CBS_stow(&cbs, &s->internal->alpn_client_proto_list,
336
6.07k
      &s->internal->alpn_client_proto_list_len))
337
0
    goto err;
338
339
6.07k
  s->verify_result = X509_V_OK;
340
341
6.07k
  s->method = ctx->method;
342
343
6.07k
  if (!s->method->ssl_new(s))
344
0
    goto err;
345
346
6.07k
  s->references = 1;
347
6.07k
  s->server = ctx->method->server;
348
349
6.07k
  SSL_clear(s);
350
351
6.07k
  CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->internal->ex_data);
352
353
6.07k
  return (s);
354
355
0
 err:
356
0
  SSL_free(s);
357
0
  SSLerrorx(ERR_R_MALLOC_FAILURE);
358
0
  return (NULL);
359
6.07k
}
360
361
int
362
SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
363
    unsigned int sid_ctx_len)
364
0
{
365
0
  if (sid_ctx_len > sizeof ctx->sid_ctx) {
366
0
    SSLerrorx(SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
367
0
    return (0);
368
0
  }
369
0
  ctx->sid_ctx_length = sid_ctx_len;
370
0
  memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
371
372
0
  return (1);
373
0
}
374
375
int
376
SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx,
377
    unsigned int sid_ctx_len)
378
0
{
379
0
  if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
380
0
    SSLerror(ssl, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
381
0
    return (0);
382
0
  }
383
0
  ssl->sid_ctx_length = sid_ctx_len;
384
0
  memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
385
386
0
  return (1);
387
0
}
388
389
int
390
SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
391
0
{
392
0
  CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
393
0
  ctx->internal->generate_session_id = cb;
394
0
  CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
395
0
  return (1);
396
0
}
397
398
int
399
SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
400
0
{
401
0
  CRYPTO_w_lock(CRYPTO_LOCK_SSL);
402
0
  ssl->internal->generate_session_id = cb;
403
0
  CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
404
0
  return (1);
405
0
}
406
407
int
408
SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
409
    unsigned int id_len)
410
10.4k
{
411
  /*
412
   * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp
413
   * shows how we can "construct" a session to give us the desired
414
   * check - ie. to find if there's a session in the hash table
415
   * that would conflict with any new session built out of this
416
   * id/id_len and the ssl_version in use by this SSL.
417
   */
418
10.4k
  SSL_SESSION r, *p;
419
420
10.4k
  if (id_len > sizeof r.session_id)
421
0
    return (0);
422
423
10.4k
  r.ssl_version = ssl->version;
424
10.4k
  r.session_id_length = id_len;
425
10.4k
  memcpy(r.session_id, id, id_len);
426
427
10.4k
  CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
428
10.4k
  p = lh_SSL_SESSION_retrieve(ssl->ctx->internal->sessions, &r);
429
10.4k
  CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
430
10.4k
  return (p != NULL);
431
10.4k
}
432
433
int
434
SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
435
0
{
436
0
  return (X509_VERIFY_PARAM_set_purpose(s->param, purpose));
437
0
}
438
439
int
440
SSL_set_purpose(SSL *s, int purpose)
441
0
{
442
0
  return (X509_VERIFY_PARAM_set_purpose(s->param, purpose));
443
0
}
444
445
int
446
SSL_CTX_set_trust(SSL_CTX *s, int trust)
447
0
{
448
0
  return (X509_VERIFY_PARAM_set_trust(s->param, trust));
449
0
}
450
451
int
452
SSL_set_trust(SSL *s, int trust)
453
0
{
454
0
  return (X509_VERIFY_PARAM_set_trust(s->param, trust));
455
0
}
456
457
int
458
SSL_set1_host(SSL *s, const char *hostname)
459
0
{
460
0
  struct in_addr ina;
461
0
  struct in6_addr in6a;
462
463
0
  if (hostname != NULL && *hostname != '\0' &&
464
0
      (inet_pton(AF_INET, hostname, &ina) == 1 ||
465
0
      inet_pton(AF_INET6, hostname, &in6a) == 1))
466
0
    return X509_VERIFY_PARAM_set1_ip_asc(s->param, hostname);
467
0
  else
468
0
    return X509_VERIFY_PARAM_set1_host(s->param, hostname, 0);
469
0
}
470
471
void
472
SSL_set_hostflags(SSL *s, unsigned int flags)
473
0
{
474
0
  X509_VERIFY_PARAM_set_hostflags(s->param, flags);
475
0
}
476
477
const char *
478
SSL_get0_peername(SSL *s)
479
0
{
480
0
  return X509_VERIFY_PARAM_get0_peername(s->param);
481
0
}
482
483
X509_VERIFY_PARAM *
484
SSL_CTX_get0_param(SSL_CTX *ctx)
485
0
{
486
0
  return (ctx->param);
487
0
}
488
489
int
490
SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
491
0
{
492
0
  return (X509_VERIFY_PARAM_set1(ctx->param, vpm));
493
0
}
494
495
X509_VERIFY_PARAM *
496
SSL_get0_param(SSL *ssl)
497
0
{
498
0
  return (ssl->param);
499
0
}
500
501
int
502
SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
503
0
{
504
0
  return (X509_VERIFY_PARAM_set1(ssl->param, vpm));
505
0
}
506
507
void
508
SSL_free(SSL *s)
509
6.07k
{
510
6.07k
  int i;
511
512
6.07k
  if (s == NULL)
513
0
    return;
514
515
6.07k
  i = CRYPTO_add(&s->references, -1, CRYPTO_LOCK_SSL);
516
6.07k
  if (i > 0)
517
0
    return;
518
519
6.07k
  X509_VERIFY_PARAM_free(s->param);
520
521
6.07k
  CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->internal->ex_data);
522
523
6.07k
  if (s->bbio != NULL) {
524
    /* If the buffering BIO is in place, pop it off */
525
5.42k
    if (s->bbio == s->wbio) {
526
5.42k
      s->wbio = BIO_pop(s->wbio);
527
5.42k
    }
528
5.42k
    BIO_free(s->bbio);
529
5.42k
    s->bbio = NULL;
530
5.42k
  }
531
532
6.07k
  if (s->rbio != s->wbio)
533
6.07k
    BIO_free_all(s->rbio);
534
6.07k
  BIO_free_all(s->wbio);
535
536
6.07k
  tls13_ctx_free(s->internal->tls13);
537
538
6.07k
  ssl3_release_init_buffer(s);
539
540
6.07k
  sk_SSL_CIPHER_free(s->cipher_list);
541
6.07k
  sk_SSL_CIPHER_free(s->internal->cipher_list_tls13);
542
543
  /* Make the next call work :-) */
544
6.07k
  if (s->session != NULL) {
545
6.07k
    ssl_clear_bad_session(s);
546
6.07k
    SSL_SESSION_free(s->session);
547
6.07k
  }
548
549
6.07k
  ssl_clear_cipher_state(s);
550
551
6.07k
  ssl_cert_free(s->cert);
552
553
6.07k
  free(s->tlsext_hostname);
554
6.07k
  SSL_CTX_free(s->initial_ctx);
555
556
6.07k
  free(s->internal->tlsext_ecpointformatlist);
557
6.07k
  free(s->internal->tlsext_supportedgroups);
558
559
6.07k
  sk_X509_EXTENSION_pop_free(s->internal->tlsext_ocsp_exts,
560
6.07k
      X509_EXTENSION_free);
561
6.07k
  sk_OCSP_RESPID_pop_free(s->internal->tlsext_ocsp_ids, OCSP_RESPID_free);
562
6.07k
  free(s->internal->tlsext_ocsp_resp);
563
564
6.07k
  sk_X509_NAME_pop_free(s->internal->client_CA, X509_NAME_free);
565
566
6.07k
  if (s->method != NULL)
567
6.07k
    s->method->ssl_free(s);
568
569
6.07k
  SSL_CTX_free(s->ctx);
570
571
6.07k
  free(s->internal->alpn_client_proto_list);
572
573
6.07k
  free(s->internal->quic_transport_params);
574
575
6.07k
#ifndef OPENSSL_NO_SRTP
576
6.07k
  sk_SRTP_PROTECTION_PROFILE_free(s->internal->srtp_profiles);
577
6.07k
#endif
578
579
6.07k
  tls12_record_layer_free(s->internal->rl);
580
581
6.07k
  free(s->internal);
582
6.07k
  free(s);
583
6.07k
}
584
585
int
586
SSL_up_ref(SSL *s)
587
0
{
588
0
  int refs = CRYPTO_add(&s->references, 1, CRYPTO_LOCK_SSL);
589
0
  return (refs > 1) ? 1 : 0;
590
0
}
591
592
void
593
SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio)
594
6.07k
{
595
  /* If the output buffering BIO is still in place, remove it */
596
6.07k
  if (s->bbio != NULL) {
597
0
    if (s->wbio == s->bbio) {
598
0
      s->wbio = BIO_next(s->wbio);
599
0
      BIO_set_next(s->bbio, NULL);
600
0
    }
601
0
  }
602
603
6.07k
  if (s->rbio != rbio && s->rbio != s->wbio)
604
0
    BIO_free_all(s->rbio);
605
6.07k
  if (s->wbio != wbio)
606
6.07k
    BIO_free_all(s->wbio);
607
6.07k
  s->rbio = rbio;
608
6.07k
  s->wbio = wbio;
609
6.07k
}
610
611
BIO *
612
SSL_get_rbio(const SSL *s)
613
14
{
614
14
  return (s->rbio);
615
14
}
616
617
void
618
SSL_set0_rbio(SSL *s, BIO *rbio)
619
0
{
620
0
  BIO_free_all(s->rbio);
621
0
  s->rbio = rbio;
622
0
}
623
624
BIO *
625
SSL_get_wbio(const SSL *s)
626
0
{
627
0
  return (s->wbio);
628
0
}
629
630
int
631
SSL_get_fd(const SSL *s)
632
0
{
633
0
  return (SSL_get_rfd(s));
634
0
}
635
636
int
637
SSL_get_rfd(const SSL *s)
638
0
{
639
0
  int  ret = -1;
640
0
  BIO *b, *r;
641
642
0
  b = SSL_get_rbio(s);
643
0
  r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
644
0
  if (r != NULL)
645
0
    BIO_get_fd(r, &ret);
646
0
  return (ret);
647
0
}
648
649
int
650
SSL_get_wfd(const SSL *s)
651
0
{
652
0
  int  ret = -1;
653
0
  BIO *b, *r;
654
655
0
  b = SSL_get_wbio(s);
656
0
  r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
657
0
  if (r != NULL)
658
0
    BIO_get_fd(r, &ret);
659
0
  return (ret);
660
0
}
661
662
int
663
SSL_set_fd(SSL *s, int fd)
664
0
{
665
0
  int  ret = 0;
666
0
  BIO *bio = NULL;
667
668
0
  bio = BIO_new(BIO_s_socket());
669
670
0
  if (bio == NULL) {
671
0
    SSLerror(s, ERR_R_BUF_LIB);
672
0
    goto err;
673
0
  }
674
0
  BIO_set_fd(bio, fd, BIO_NOCLOSE);
675
0
  SSL_set_bio(s, bio, bio);
676
0
  ret = 1;
677
0
 err:
678
0
  return (ret);
679
0
}
680
681
int
682
SSL_set_wfd(SSL *s, int fd)
683
0
{
684
0
  int  ret = 0;
685
0
  BIO *bio = NULL;
686
687
0
  if ((s->rbio == NULL) || (BIO_method_type(s->rbio) != BIO_TYPE_SOCKET)
688
0
      || ((int)BIO_get_fd(s->rbio, NULL) != fd)) {
689
0
    bio = BIO_new(BIO_s_socket());
690
691
0
    if (bio == NULL) {
692
0
      SSLerror(s, ERR_R_BUF_LIB);
693
0
      goto err;
694
0
    }
695
0
    BIO_set_fd(bio, fd, BIO_NOCLOSE);
696
0
    SSL_set_bio(s, SSL_get_rbio(s), bio);
697
0
  } else
698
0
    SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
699
0
  ret = 1;
700
0
 err:
701
0
  return (ret);
702
0
}
703
704
int
705
SSL_set_rfd(SSL *s, int fd)
706
0
{
707
0
  int  ret = 0;
708
0
  BIO *bio = NULL;
709
710
0
  if ((s->wbio == NULL) || (BIO_method_type(s->wbio) != BIO_TYPE_SOCKET)
711
0
      || ((int)BIO_get_fd(s->wbio, NULL) != fd)) {
712
0
    bio = BIO_new(BIO_s_socket());
713
714
0
    if (bio == NULL) {
715
0
      SSLerror(s, ERR_R_BUF_LIB);
716
0
      goto err;
717
0
    }
718
0
    BIO_set_fd(bio, fd, BIO_NOCLOSE);
719
0
    SSL_set_bio(s, bio, SSL_get_wbio(s));
720
0
  } else
721
0
    SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
722
0
  ret = 1;
723
0
 err:
724
0
  return (ret);
725
0
}
726
727
728
/* return length of latest Finished message we sent, copy to 'buf' */
729
size_t
730
SSL_get_finished(const SSL *s, void *buf, size_t count)
731
0
{
732
0
  size_t  ret;
733
734
0
  ret = s->s3->hs.finished_len;
735
0
  if (count > ret)
736
0
    count = ret;
737
0
  memcpy(buf, s->s3->hs.finished, count);
738
0
  return (ret);
739
0
}
740
741
/* return length of latest Finished message we expected, copy to 'buf' */
742
size_t
743
SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
744
0
{
745
0
  size_t  ret;
746
747
0
  ret = s->s3->hs.peer_finished_len;
748
0
  if (count > ret)
749
0
    count = ret;
750
0
  memcpy(buf, s->s3->hs.peer_finished, count);
751
0
  return (ret);
752
0
}
753
754
755
int
756
SSL_get_verify_mode(const SSL *s)
757
86
{
758
86
  return (s->verify_mode);
759
86
}
760
761
int
762
SSL_get_verify_depth(const SSL *s)
763
0
{
764
0
  return (X509_VERIFY_PARAM_get_depth(s->param));
765
0
}
766
767
int
768
(*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *)
769
0
{
770
0
  return (s->internal->verify_callback);
771
0
}
772
773
void
774
SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb)
775
0
{
776
0
  ctx->internal->keylog_callback = cb;
777
0
}
778
779
SSL_CTX_keylog_cb_func
780
SSL_CTX_get_keylog_callback(const SSL_CTX *ctx)
781
0
{
782
0
  return (ctx->internal->keylog_callback);
783
0
}
784
785
int
786
SSL_set_num_tickets(SSL *s, size_t num_tickets)
787
0
{
788
0
  s->internal->num_tickets = num_tickets;
789
790
0
  return 1;
791
0
}
792
793
size_t
794
SSL_get_num_tickets(const SSL *s)
795
0
{
796
0
  return s->internal->num_tickets;
797
0
}
798
799
int
800
SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets)
801
0
{
802
0
  ctx->internal->num_tickets = num_tickets;
803
804
0
  return 1;
805
0
}
806
807
size_t
808
SSL_CTX_get_num_tickets(const SSL_CTX *ctx)
809
0
{
810
0
  return ctx->internal->num_tickets;
811
0
}
812
813
int
814
SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
815
0
{
816
0
  return (ctx->verify_mode);
817
0
}
818
819
int
820
SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
821
0
{
822
0
  return (X509_VERIFY_PARAM_get_depth(ctx->param));
823
0
}
824
825
int
826
(*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *)
827
0
{
828
0
  return (ctx->internal->default_verify_callback);
829
0
}
830
831
void
832
SSL_set_verify(SSL *s, int mode,
833
    int (*callback)(int ok, X509_STORE_CTX *ctx))
834
0
{
835
0
  s->verify_mode = mode;
836
0
  if (callback != NULL)
837
0
    s->internal->verify_callback = callback;
838
0
}
839
840
void
841
SSL_set_verify_depth(SSL *s, int depth)
842
0
{
843
0
  X509_VERIFY_PARAM_set_depth(s->param, depth);
844
0
}
845
846
void
847
SSL_set_read_ahead(SSL *s, int yes)
848
0
{
849
0
  s->internal->read_ahead = yes;
850
0
}
851
852
int
853
SSL_get_read_ahead(const SSL *s)
854
0
{
855
0
  return (s->internal->read_ahead);
856
0
}
857
858
int
859
SSL_pending(const SSL *s)
860
0
{
861
0
  return (s->method->ssl_pending(s));
862
0
}
863
864
X509 *
865
SSL_get_peer_certificate(const SSL *s)
866
0
{
867
0
  X509 *cert;
868
869
0
  if (s == NULL || s->session == NULL)
870
0
    return NULL;
871
872
0
  if ((cert = s->session->peer_cert) == NULL)
873
0
    return NULL;
874
875
0
  X509_up_ref(cert);
876
877
0
  return cert;
878
0
}
879
880
STACK_OF(X509) *
881
SSL_get_peer_cert_chain(const SSL *s)
882
0
{
883
0
  if (s == NULL || s->session == NULL)
884
0
    return NULL;
885
886
  /*
887
   * If we are a client, cert_chain includes the peer's own
888
   * certificate; if we are a server, it does not.
889
   */
890
0
  return s->session->cert_chain;
891
0
}
892
893
STACK_OF(X509) *
894
SSL_get0_verified_chain(const SSL *s)
895
0
{
896
0
  return s->internal->verified_chain;
897
0
}
898
899
/*
900
 * Now in theory, since the calling process own 't' it should be safe to
901
 * modify.  We need to be able to read f without being hassled
902
 */
903
int
904
SSL_copy_session_id(SSL *t, const SSL *f)
905
0
{
906
0
  SSL_CERT *tmp;
907
908
  /* Do we need to do SSL locking? */
909
0
  if (!SSL_set_session(t, SSL_get_session(f)))
910
0
    return 0;
911
912
  /* What if we are set up for one protocol but want to talk another? */
913
0
  if (t->method != f->method) {
914
0
    t->method->ssl_free(t);
915
0
    t->method = f->method;
916
0
    if (!t->method->ssl_new(t))
917
0
      return 0;
918
0
  }
919
920
0
  tmp = t->cert;
921
0
  if (f->cert != NULL) {
922
0
    CRYPTO_add(&f->cert->references, 1, CRYPTO_LOCK_SSL_CERT);
923
0
    t->cert = f->cert;
924
0
  } else
925
0
    t->cert = NULL;
926
0
  ssl_cert_free(tmp);
927
928
0
  if (!SSL_set_session_id_context(t, f->sid_ctx, f->sid_ctx_length))
929
0
    return 0;
930
931
0
  return 1;
932
0
}
933
934
/* Fix this so it checks all the valid key/cert options */
935
int
936
SSL_CTX_check_private_key(const SSL_CTX *ctx)
937
0
{
938
0
  if ((ctx == NULL) || (ctx->internal->cert == NULL) ||
939
0
      (ctx->internal->cert->key->x509 == NULL)) {
940
0
    SSLerrorx(SSL_R_NO_CERTIFICATE_ASSIGNED);
941
0
    return (0);
942
0
  }
943
0
  if (ctx->internal->cert->key->privatekey == NULL) {
944
0
    SSLerrorx(SSL_R_NO_PRIVATE_KEY_ASSIGNED);
945
0
    return (0);
946
0
  }
947
0
  return (X509_check_private_key(ctx->internal->cert->key->x509,
948
0
      ctx->internal->cert->key->privatekey));
949
0
}
950
951
/* Fix this function so that it takes an optional type parameter */
952
int
953
SSL_check_private_key(const SSL *ssl)
954
0
{
955
0
  if (ssl == NULL) {
956
0
    SSLerrorx(ERR_R_PASSED_NULL_PARAMETER);
957
0
    return (0);
958
0
  }
959
0
  if (ssl->cert == NULL) {
960
0
    SSLerror(ssl, SSL_R_NO_CERTIFICATE_ASSIGNED);
961
0
    return (0);
962
0
  }
963
0
  if (ssl->cert->key->x509 == NULL) {
964
0
    SSLerror(ssl, SSL_R_NO_CERTIFICATE_ASSIGNED);
965
0
    return (0);
966
0
  }
967
0
  if (ssl->cert->key->privatekey == NULL) {
968
0
    SSLerror(ssl, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
969
0
    return (0);
970
0
  }
971
0
  return (X509_check_private_key(ssl->cert->key->x509,
972
0
      ssl->cert->key->privatekey));
973
0
}
974
975
int
976
SSL_accept(SSL *s)
977
0
{
978
0
  if (s->internal->handshake_func == NULL)
979
0
    SSL_set_accept_state(s); /* Not properly initialized yet */
980
981
0
  return (s->method->ssl_accept(s));
982
0
}
983
984
int
985
SSL_connect(SSL *s)
986
0
{
987
0
  if (s->internal->handshake_func == NULL)
988
0
    SSL_set_connect_state(s); /* Not properly initialized yet */
989
990
0
  return (s->method->ssl_connect(s));
991
0
}
992
993
int
994
SSL_is_dtls(const SSL *s)
995
203k
{
996
203k
  return s->method->dtls;
997
203k
}
998
999
int
1000
SSL_is_server(const SSL *s)
1001
0
{
1002
0
  return s->server;
1003
0
}
1004
1005
static long
1006
ssl_get_default_timeout()
1007
6.07k
{
1008
  /*
1009
   * 2 hours, the 24 hours mentioned in the TLSv1 spec
1010
   * is way too long for http, the cache would over fill.
1011
   */
1012
6.07k
  return (2 * 60 * 60);
1013
6.07k
}
1014
1015
long
1016
SSL_get_default_timeout(const SSL *s)
1017
0
{
1018
0
  return (ssl_get_default_timeout());
1019
0
}
1020
1021
int
1022
SSL_read(SSL *s, void *buf, int num)
1023
0
{
1024
0
  if (num < 0) {
1025
0
    SSLerror(s, SSL_R_BAD_LENGTH);
1026
0
    return -1;
1027
0
  }
1028
1029
0
  if (s->internal->handshake_func == NULL) {
1030
0
    SSLerror(s, SSL_R_UNINITIALIZED);
1031
0
    return (-1);
1032
0
  }
1033
1034
0
  if (s->internal->shutdown & SSL_RECEIVED_SHUTDOWN) {
1035
0
    s->internal->rwstate = SSL_NOTHING;
1036
0
    return (0);
1037
0
  }
1038
0
  return ssl3_read(s, buf, num);
1039
0
}
1040
1041
int
1042
SSL_read_ex(SSL *s, void *buf, size_t num, size_t *bytes_read)
1043
0
{
1044
0
  int ret;
1045
1046
  /* We simply don't bother supporting enormous reads */
1047
0
  if (num > INT_MAX) {
1048
0
    SSLerror(s, SSL_R_BAD_LENGTH);
1049
0
    return 0;
1050
0
  }
1051
1052
0
  ret = SSL_read(s, buf, (int)num);
1053
0
  if (ret < 0)
1054
0
    ret = 0;
1055
0
  *bytes_read = ret;
1056
1057
0
  return ret > 0;
1058
0
}
1059
1060
int
1061
SSL_peek(SSL *s, void *buf, int num)
1062
0
{
1063
0
  if (num < 0) {
1064
0
    SSLerror(s, SSL_R_BAD_LENGTH);
1065
0
    return -1;
1066
0
  }
1067
1068
0
  if (s->internal->handshake_func == NULL) {
1069
0
    SSLerror(s, SSL_R_UNINITIALIZED);
1070
0
    return (-1);
1071
0
  }
1072
1073
0
  if (s->internal->shutdown & SSL_RECEIVED_SHUTDOWN) {
1074
0
    return (0);
1075
0
  }
1076
0
  return ssl3_peek(s, buf, num);
1077
0
}
1078
1079
int
1080
SSL_peek_ex(SSL *s, void *buf, size_t num, size_t *bytes_peeked)
1081
0
{
1082
0
  int ret;
1083
1084
  /* We simply don't bother supporting enormous peeks */
1085
0
  if (num > INT_MAX) {
1086
0
    SSLerror(s, SSL_R_BAD_LENGTH);
1087
0
    return 0;
1088
0
  }
1089
1090
0
  ret = SSL_peek(s, buf, (int)num);
1091
0
  if (ret < 0)
1092
0
    ret = 0;
1093
0
  *bytes_peeked = ret;
1094
1095
0
  return ret > 0;
1096
0
}
1097
1098
int
1099
SSL_write(SSL *s, const void *buf, int num)
1100
0
{
1101
0
  if (num < 0) {
1102
0
    SSLerror(s, SSL_R_BAD_LENGTH);
1103
0
    return -1;
1104
0
  }
1105
1106
0
  if (s->internal->handshake_func == NULL) {
1107
0
    SSLerror(s, SSL_R_UNINITIALIZED);
1108
0
    return (-1);
1109
0
  }
1110
1111
0
  if (s->internal->shutdown & SSL_SENT_SHUTDOWN) {
1112
0
    s->internal->rwstate = SSL_NOTHING;
1113
0
    SSLerror(s, SSL_R_PROTOCOL_IS_SHUTDOWN);
1114
0
    return (-1);
1115
0
  }
1116
0
  return ssl3_write(s, buf, num);
1117
0
}
1118
1119
int
1120
SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *bytes_written)
1121
0
{
1122
0
  int ret;
1123
1124
  /* We simply don't bother supporting enormous writes */
1125
0
  if (num > INT_MAX) {
1126
0
    SSLerror(s, SSL_R_BAD_LENGTH);
1127
0
    return 0;
1128
0
  }
1129
1130
0
  if (num == 0) {
1131
    /* This API is special */
1132
0
    bytes_written = 0;
1133
0
    return 1;
1134
0
  }
1135
1136
0
  ret = SSL_write(s, buf, (int)num);
1137
0
  if (ret < 0)
1138
0
    ret = 0;
1139
0
  *bytes_written = ret;
1140
1141
0
  return ret > 0;
1142
0
}
1143
1144
uint32_t
1145
SSL_CTX_get_max_early_data(const SSL_CTX *ctx)
1146
0
{
1147
0
  return 0;
1148
0
}
1149
1150
int
1151
SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data)
1152
0
{
1153
0
  return 1;
1154
0
}
1155
1156
uint32_t
1157
SSL_get_max_early_data(const SSL *s)
1158
0
{
1159
0
  return 0;
1160
0
}
1161
1162
int
1163
SSL_set_max_early_data(SSL *s, uint32_t max_early_data)
1164
0
{
1165
0
  return 1;
1166
0
}
1167
1168
int
1169
SSL_get_early_data_status(const SSL *s)
1170
0
{
1171
0
  return SSL_EARLY_DATA_REJECTED;
1172
0
}
1173
1174
int
1175
SSL_read_early_data(SSL *s, void *buf, size_t num, size_t *readbytes)
1176
0
{
1177
0
  *readbytes = 0;
1178
1179
0
  if (!s->server) {
1180
0
    SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1181
0
    return SSL_READ_EARLY_DATA_ERROR;
1182
0
  }
1183
1184
0
  return SSL_READ_EARLY_DATA_FINISH;
1185
0
}
1186
1187
int
1188
SSL_write_early_data(SSL *s, const void *buf, size_t num, size_t *written)
1189
0
{
1190
0
  *written = 0;
1191
0
  SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
1192
0
  return 0;
1193
0
}
1194
1195
int
1196
SSL_shutdown(SSL *s)
1197
0
{
1198
  /*
1199
   * Note that this function behaves differently from what one might
1200
   * expect.  Return values are 0 for no success (yet),
1201
   * 1 for success; but calling it once is usually not enough,
1202
   * even if blocking I/O is used (see ssl3_shutdown).
1203
   */
1204
1205
0
  if (s->internal->handshake_func == NULL) {
1206
0
    SSLerror(s, SSL_R_UNINITIALIZED);
1207
0
    return (-1);
1208
0
  }
1209
1210
0
  if (s != NULL && !SSL_in_init(s))
1211
0
    return (s->method->ssl_shutdown(s));
1212
1213
0
  return (1);
1214
0
}
1215
1216
int
1217
SSL_renegotiate(SSL *s)
1218
0
{
1219
0
  if (s->internal->renegotiate == 0)
1220
0
    s->internal->renegotiate = 1;
1221
1222
0
  s->internal->new_session = 1;
1223
1224
0
  return (s->method->ssl_renegotiate(s));
1225
0
}
1226
1227
int
1228
SSL_renegotiate_abbreviated(SSL *s)
1229
0
{
1230
0
  if (s->internal->renegotiate == 0)
1231
0
    s->internal->renegotiate = 1;
1232
1233
0
  s->internal->new_session = 0;
1234
1235
0
  return (s->method->ssl_renegotiate(s));
1236
0
}
1237
1238
int
1239
SSL_renegotiate_pending(SSL *s)
1240
0
{
1241
  /*
1242
   * Becomes true when negotiation is requested;
1243
   * false again once a handshake has finished.
1244
   */
1245
0
  return (s->internal->renegotiate != 0);
1246
0
}
1247
1248
long
1249
SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
1250
5.47k
{
1251
5.47k
  long  l;
1252
1253
5.47k
  switch (cmd) {
1254
0
  case SSL_CTRL_GET_READ_AHEAD:
1255
0
    return (s->internal->read_ahead);
1256
0
  case SSL_CTRL_SET_READ_AHEAD:
1257
0
    l = s->internal->read_ahead;
1258
0
    s->internal->read_ahead = larg;
1259
0
    return (l);
1260
1261
0
  case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1262
0
    s->internal->msg_callback_arg = parg;
1263
0
    return (1);
1264
1265
5.47k
  case SSL_CTRL_OPTIONS:
1266
5.47k
    return (s->internal->options|=larg);
1267
0
  case SSL_CTRL_CLEAR_OPTIONS:
1268
0
    return (s->internal->options&=~larg);
1269
0
  case SSL_CTRL_MODE:
1270
0
    return (s->internal->mode|=larg);
1271
0
  case SSL_CTRL_CLEAR_MODE:
1272
0
    return (s->internal->mode &=~larg);
1273
0
  case SSL_CTRL_GET_MAX_CERT_LIST:
1274
0
    return (s->internal->max_cert_list);
1275
0
  case SSL_CTRL_SET_MAX_CERT_LIST:
1276
0
    l = s->internal->max_cert_list;
1277
0
    s->internal->max_cert_list = larg;
1278
0
    return (l);
1279
0
  case SSL_CTRL_SET_MTU:
1280
0
#ifndef OPENSSL_NO_DTLS1
1281
0
    if (larg < (long)dtls1_min_mtu())
1282
0
      return (0);
1283
0
#endif
1284
0
    if (SSL_is_dtls(s)) {
1285
0
      s->d1->mtu = larg;
1286
0
      return (larg);
1287
0
    }
1288
0
    return (0);
1289
0
  case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1290
0
    if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1291
0
      return (0);
1292
0
    s->max_send_fragment = larg;
1293
0
    return (1);
1294
0
  case SSL_CTRL_GET_RI_SUPPORT:
1295
0
    if (s->s3)
1296
0
      return (s->s3->send_connection_binding);
1297
0
    else return (0);
1298
0
  default:
1299
0
    if (SSL_is_dtls(s))
1300
0
      return dtls1_ctrl(s, cmd, larg, parg);
1301
0
    return ssl3_ctrl(s, cmd, larg, parg);
1302
5.47k
  }
1303
5.47k
}
1304
1305
long
1306
SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void))
1307
0
{
1308
0
  switch (cmd) {
1309
0
  case SSL_CTRL_SET_MSG_CALLBACK:
1310
0
    s->internal->msg_callback = (ssl_msg_callback_fn *)(fp);
1311
0
    return (1);
1312
1313
0
  default:
1314
0
    return (ssl3_callback_ctrl(s, cmd, fp));
1315
0
  }
1316
0
}
1317
1318
struct lhash_st_SSL_SESSION *
1319
SSL_CTX_sessions(SSL_CTX *ctx)
1320
0
{
1321
0
  return (ctx->internal->sessions);
1322
0
}
1323
1324
long
1325
SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
1326
0
{
1327
0
  long  l;
1328
1329
0
  switch (cmd) {
1330
0
  case SSL_CTRL_GET_READ_AHEAD:
1331
0
    return (ctx->internal->read_ahead);
1332
0
  case SSL_CTRL_SET_READ_AHEAD:
1333
0
    l = ctx->internal->read_ahead;
1334
0
    ctx->internal->read_ahead = larg;
1335
0
    return (l);
1336
1337
0
  case SSL_CTRL_SET_MSG_CALLBACK_ARG:
1338
0
    ctx->internal->msg_callback_arg = parg;
1339
0
    return (1);
1340
1341
0
  case SSL_CTRL_GET_MAX_CERT_LIST:
1342
0
    return (ctx->internal->max_cert_list);
1343
0
  case SSL_CTRL_SET_MAX_CERT_LIST:
1344
0
    l = ctx->internal->max_cert_list;
1345
0
    ctx->internal->max_cert_list = larg;
1346
0
    return (l);
1347
1348
0
  case SSL_CTRL_SET_SESS_CACHE_SIZE:
1349
0
    l = ctx->internal->session_cache_size;
1350
0
    ctx->internal->session_cache_size = larg;
1351
0
    return (l);
1352
0
  case SSL_CTRL_GET_SESS_CACHE_SIZE:
1353
0
    return (ctx->internal->session_cache_size);
1354
0
  case SSL_CTRL_SET_SESS_CACHE_MODE:
1355
0
    l = ctx->internal->session_cache_mode;
1356
0
    ctx->internal->session_cache_mode = larg;
1357
0
    return (l);
1358
0
  case SSL_CTRL_GET_SESS_CACHE_MODE:
1359
0
    return (ctx->internal->session_cache_mode);
1360
1361
0
  case SSL_CTRL_SESS_NUMBER:
1362
0
    return (lh_SSL_SESSION_num_items(ctx->internal->sessions));
1363
0
  case SSL_CTRL_SESS_CONNECT:
1364
0
    return (ctx->internal->stats.sess_connect);
1365
0
  case SSL_CTRL_SESS_CONNECT_GOOD:
1366
0
    return (ctx->internal->stats.sess_connect_good);
1367
0
  case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
1368
0
    return (ctx->internal->stats.sess_connect_renegotiate);
1369
0
  case SSL_CTRL_SESS_ACCEPT:
1370
0
    return (ctx->internal->stats.sess_accept);
1371
0
  case SSL_CTRL_SESS_ACCEPT_GOOD:
1372
0
    return (ctx->internal->stats.sess_accept_good);
1373
0
  case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
1374
0
    return (ctx->internal->stats.sess_accept_renegotiate);
1375
0
  case SSL_CTRL_SESS_HIT:
1376
0
    return (ctx->internal->stats.sess_hit);
1377
0
  case SSL_CTRL_SESS_CB_HIT:
1378
0
    return (ctx->internal->stats.sess_cb_hit);
1379
0
  case SSL_CTRL_SESS_MISSES:
1380
0
    return (ctx->internal->stats.sess_miss);
1381
0
  case SSL_CTRL_SESS_TIMEOUTS:
1382
0
    return (ctx->internal->stats.sess_timeout);
1383
0
  case SSL_CTRL_SESS_CACHE_FULL:
1384
0
    return (ctx->internal->stats.sess_cache_full);
1385
0
  case SSL_CTRL_OPTIONS:
1386
0
    return (ctx->internal->options|=larg);
1387
0
  case SSL_CTRL_CLEAR_OPTIONS:
1388
0
    return (ctx->internal->options&=~larg);
1389
0
  case SSL_CTRL_MODE:
1390
0
    return (ctx->internal->mode|=larg);
1391
0
  case SSL_CTRL_CLEAR_MODE:
1392
0
    return (ctx->internal->mode&=~larg);
1393
0
  case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
1394
0
    if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
1395
0
      return (0);
1396
0
    ctx->internal->max_send_fragment = larg;
1397
0
    return (1);
1398
0
  default:
1399
0
    return (ssl3_ctx_ctrl(ctx, cmd, larg, parg));
1400
0
  }
1401
0
}
1402
1403
long
1404
SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void))
1405
0
{
1406
0
  switch (cmd) {
1407
0
  case SSL_CTRL_SET_MSG_CALLBACK:
1408
0
    ctx->internal->msg_callback = (ssl_msg_callback_fn *)fp;
1409
0
    return (1);
1410
1411
0
  default:
1412
0
    return (ssl3_ctx_callback_ctrl(ctx, cmd, fp));
1413
0
  }
1414
0
}
1415
1416
int
1417
ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
1418
353k
{
1419
353k
  long  l;
1420
1421
353k
  l = a->id - b->id;
1422
353k
  if (l == 0L)
1423
15.2k
    return (0);
1424
338k
  else
1425
338k
    return ((l > 0) ? 1:-1);
1426
353k
}
1427
1428
STACK_OF(SSL_CIPHER) *
1429
SSL_get_ciphers(const SSL *s)
1430
4.51k
{
1431
4.51k
  if (s == NULL)
1432
0
    return (NULL);
1433
4.51k
  if (s->cipher_list != NULL)
1434
0
    return (s->cipher_list);
1435
1436
4.51k
  return (s->ctx->cipher_list);
1437
4.51k
}
1438
1439
STACK_OF(SSL_CIPHER) *
1440
SSL_get_client_ciphers(const SSL *s)
1441
0
{
1442
0
  if (s == NULL || s->session == NULL || !s->server)
1443
0
    return NULL;
1444
0
  return s->session->ciphers;
1445
0
}
1446
1447
STACK_OF(SSL_CIPHER) *
1448
SSL_get1_supported_ciphers(SSL *s)
1449
0
{
1450
0
  STACK_OF(SSL_CIPHER) *supported_ciphers = NULL, *ciphers;
1451
0
  SSL_CIPHER *cipher;
1452
0
  uint16_t min_vers, max_vers;
1453
0
  int i;
1454
1455
0
  if (s == NULL)
1456
0
    return NULL;
1457
0
  if (!ssl_supported_tls_version_range(s, &min_vers, &max_vers))
1458
0
    return NULL;
1459
0
  if ((ciphers = SSL_get_ciphers(s)) == NULL)
1460
0
    return NULL;
1461
0
  if ((supported_ciphers = sk_SSL_CIPHER_new_null()) == NULL)
1462
0
    return NULL;
1463
1464
0
  for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1465
0
    if ((cipher = sk_SSL_CIPHER_value(ciphers, i)) == NULL)
1466
0
      goto err;
1467
0
    if (!ssl_cipher_allowed_in_tls_version_range(cipher, min_vers,
1468
0
        max_vers))
1469
0
      continue;
1470
0
    if (!ssl_security_supported_cipher(s, cipher))
1471
0
      continue;
1472
0
    if (!sk_SSL_CIPHER_push(supported_ciphers, cipher))
1473
0
      goto err;
1474
0
  }
1475
1476
0
  if (sk_SSL_CIPHER_num(supported_ciphers) > 0)
1477
0
    return supported_ciphers;
1478
1479
0
 err:
1480
0
  sk_SSL_CIPHER_free(supported_ciphers);
1481
0
  return NULL;
1482
0
}
1483
1484
/* See if we have any ECC cipher suites. */
1485
int
1486
ssl_has_ecc_ciphers(SSL *s)
1487
0
{
1488
0
  STACK_OF(SSL_CIPHER) *ciphers;
1489
0
  unsigned long alg_k, alg_a;
1490
0
  SSL_CIPHER *cipher;
1491
0
  int i;
1492
1493
0
  if ((ciphers = SSL_get_ciphers(s)) == NULL)
1494
0
    return 0;
1495
1496
0
  for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1497
0
    cipher = sk_SSL_CIPHER_value(ciphers, i);
1498
1499
0
    alg_k = cipher->algorithm_mkey;
1500
0
    alg_a = cipher->algorithm_auth;
1501
1502
0
    if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))
1503
0
      return 1;
1504
0
  }
1505
1506
0
  return 0;
1507
0
}
1508
1509
/* The old interface to get the same thing as SSL_get_ciphers(). */
1510
const char *
1511
SSL_get_cipher_list(const SSL *s, int n)
1512
0
{
1513
0
  STACK_OF(SSL_CIPHER) *ciphers;
1514
0
  const SSL_CIPHER *cipher;
1515
1516
0
  if ((ciphers = SSL_get_ciphers(s)) == NULL)
1517
0
    return (NULL);
1518
0
  if ((cipher = sk_SSL_CIPHER_value(ciphers, n)) == NULL)
1519
0
    return (NULL);
1520
1521
0
  return (cipher->name);
1522
0
}
1523
1524
STACK_OF(SSL_CIPHER) *
1525
SSL_CTX_get_ciphers(const SSL_CTX *ctx)
1526
0
{
1527
0
  if (ctx == NULL)
1528
0
    return NULL;
1529
0
  return ctx->cipher_list;
1530
0
}
1531
1532
/* Specify the ciphers to be used by default by the SSL_CTX. */
1533
int
1534
SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
1535
6.07k
{
1536
6.07k
  STACK_OF(SSL_CIPHER) *ciphers;
1537
1538
  /*
1539
   * ssl_create_cipher_list may return an empty stack if it was unable to
1540
   * find a cipher matching the given rule string (for example if the
1541
   * rule string specifies a cipher which has been disabled). This is not
1542
   * an error as far as ssl_create_cipher_list is concerned, and hence
1543
   * ctx->cipher_list has been updated.
1544
   */
1545
6.07k
  ciphers = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
1546
6.07k
      ctx->internal->cipher_list_tls13, str, ctx->internal->cert);
1547
6.07k
  if (ciphers == NULL) {
1548
0
    return (0);
1549
6.07k
  } else if (sk_SSL_CIPHER_num(ciphers) == 0) {
1550
0
    SSLerrorx(SSL_R_NO_CIPHER_MATCH);
1551
0
    return (0);
1552
0
  }
1553
6.07k
  return (1);
1554
6.07k
}
1555
1556
int
1557
SSL_CTX_set_ciphersuites(SSL_CTX *ctx, const char *str)
1558
0
{
1559
0
  if (!ssl_parse_ciphersuites(&ctx->internal->cipher_list_tls13, str)) {
1560
0
    SSLerrorx(SSL_R_NO_CIPHER_MATCH);
1561
0
    return 0;
1562
0
  }
1563
0
  if (!ssl_merge_cipherlists(ctx->cipher_list,
1564
0
      ctx->internal->cipher_list_tls13, &ctx->cipher_list))
1565
0
    return 0;
1566
1567
0
  return 1;
1568
0
}
1569
1570
/* Specify the ciphers to be used by the SSL. */
1571
int
1572
SSL_set_cipher_list(SSL *s, const char *str)
1573
0
{
1574
0
  STACK_OF(SSL_CIPHER) *ciphers, *ciphers_tls13;
1575
1576
0
  if ((ciphers_tls13 = s->internal->cipher_list_tls13) == NULL)
1577
0
    ciphers_tls13 = s->ctx->internal->cipher_list_tls13;
1578
1579
  /* See comment in SSL_CTX_set_cipher_list. */
1580
0
  ciphers = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
1581
0
      ciphers_tls13, str, s->cert);
1582
0
  if (ciphers == NULL) {
1583
0
    return (0);
1584
0
  } else if (sk_SSL_CIPHER_num(ciphers) == 0) {
1585
0
    SSLerror(s, SSL_R_NO_CIPHER_MATCH);
1586
0
    return (0);
1587
0
  }
1588
0
  return (1);
1589
0
}
1590
1591
int
1592
SSL_set_ciphersuites(SSL *s, const char *str)
1593
0
{
1594
0
  STACK_OF(SSL_CIPHER) *ciphers;
1595
1596
0
  if ((ciphers = s->cipher_list) == NULL)
1597
0
    ciphers = s->ctx->cipher_list;
1598
1599
0
  if (!ssl_parse_ciphersuites(&s->internal->cipher_list_tls13, str)) {
1600
0
    SSLerrorx(SSL_R_NO_CIPHER_MATCH);
1601
0
    return (0);
1602
0
  }
1603
0
  if (!ssl_merge_cipherlists(ciphers, s->internal->cipher_list_tls13,
1604
0
      &s->cipher_list))
1605
0
    return 0;
1606
1607
0
  return 1;
1608
0
}
1609
1610
char *
1611
SSL_get_shared_ciphers(const SSL *s, char *buf, int len)
1612
0
{
1613
0
  STACK_OF(SSL_CIPHER) *client_ciphers, *server_ciphers;
1614
0
  const SSL_CIPHER *cipher;
1615
0
  size_t curlen = 0;
1616
0
  char *end;
1617
0
  int i;
1618
1619
0
  if (!s->server || s->session == NULL || len < 2)
1620
0
    return NULL;
1621
1622
0
  if ((client_ciphers = s->session->ciphers) == NULL)
1623
0
    return NULL;
1624
0
  if ((server_ciphers = SSL_get_ciphers(s)) == NULL)
1625
0
    return NULL;
1626
0
  if (sk_SSL_CIPHER_num(client_ciphers) == 0 ||
1627
0
      sk_SSL_CIPHER_num(server_ciphers) == 0)
1628
0
    return NULL;
1629
1630
0
  buf[0] = '\0';
1631
0
  for (i = 0; i < sk_SSL_CIPHER_num(client_ciphers); i++) {
1632
0
    cipher = sk_SSL_CIPHER_value(client_ciphers, i);
1633
1634
0
    if (sk_SSL_CIPHER_find(server_ciphers, cipher) < 0)
1635
0
      continue;
1636
1637
0
    end = buf + curlen;
1638
0
    if (strlcat(buf, cipher->name, len) >= len ||
1639
0
        (curlen = strlcat(buf, ":", len)) >= len) {
1640
      /* remove truncated cipher from list */
1641
0
      *end = '\0';
1642
0
      break;
1643
0
    }
1644
0
  }
1645
  /* remove trailing colon */
1646
0
  if ((end = strrchr(buf, ':')) != NULL)
1647
0
    *end = '\0';
1648
0
  return buf;
1649
0
}
1650
1651
/*
1652
 * Return a servername extension value if provided in Client Hello, or NULL.
1653
 * So far, only host_name types are defined (RFC 3546).
1654
 */
1655
const char *
1656
SSL_get_servername(const SSL *s, const int type)
1657
0
{
1658
0
  if (type != TLSEXT_NAMETYPE_host_name)
1659
0
    return (NULL);
1660
1661
0
  return (s->session && !s->tlsext_hostname ?
1662
0
      s->session->tlsext_hostname :
1663
0
      s->tlsext_hostname);
1664
0
}
1665
1666
int
1667
SSL_get_servername_type(const SSL *s)
1668
0
{
1669
0
  if (s->session &&
1670
0
      (!s->tlsext_hostname ?
1671
0
      s->session->tlsext_hostname : s->tlsext_hostname))
1672
0
    return (TLSEXT_NAMETYPE_host_name);
1673
0
  return (-1);
1674
0
}
1675
1676
/*
1677
 * SSL_select_next_proto implements standard protocol selection. It is
1678
 * expected that this function is called from the callback set by
1679
 * SSL_CTX_set_alpn_select_cb.
1680
 *
1681
 * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
1682
 * strings. The length byte itself is not included in the length. A byte
1683
 * string of length 0 is invalid. No byte string may be truncated.
1684
 *
1685
 * It returns either:
1686
 * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
1687
 * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
1688
 */
1689
int
1690
SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
1691
    const unsigned char *server, unsigned int server_len,
1692
    const unsigned char *client, unsigned int client_len)
1693
0
{
1694
0
  unsigned int     i, j;
1695
0
  const unsigned char *result;
1696
0
  int      status = OPENSSL_NPN_UNSUPPORTED;
1697
1698
  /*
1699
   * For each protocol in server preference order,
1700
   * see if we support it.
1701
   */
1702
0
  for (i = 0; i < server_len; ) {
1703
0
    for (j = 0; j < client_len; ) {
1704
0
      if (server[i] == client[j] &&
1705
0
          memcmp(&server[i + 1],
1706
0
          &client[j + 1], server[i]) == 0) {
1707
        /* We found a match */
1708
0
        result = &server[i];
1709
0
        status = OPENSSL_NPN_NEGOTIATED;
1710
0
        goto found;
1711
0
      }
1712
0
      j += client[j];
1713
0
      j++;
1714
0
    }
1715
0
    i += server[i];
1716
0
    i++;
1717
0
  }
1718
1719
  /* There's no overlap between our protocols and the server's list. */
1720
0
  result = client;
1721
0
  status = OPENSSL_NPN_NO_OVERLAP;
1722
1723
0
 found:
1724
0
  *out = (unsigned char *) result + 1;
1725
0
  *outlen = result[0];
1726
0
  return (status);
1727
0
}
1728
1729
/* SSL_get0_next_proto_negotiated is deprecated. */
1730
void
1731
SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
1732
    unsigned int *len)
1733
0
{
1734
0
  *data = NULL;
1735
0
  *len = 0;
1736
0
}
1737
1738
/* SSL_CTX_set_next_protos_advertised_cb is deprecated. */
1739
void
1740
SSL_CTX_set_next_protos_advertised_cb(SSL_CTX *ctx, int (*cb) (SSL *ssl,
1741
    const unsigned char **out, unsigned int *outlen, void *arg), void *arg)
1742
0
{
1743
0
}
1744
1745
/* SSL_CTX_set_next_proto_select_cb is deprecated. */
1746
void
1747
SSL_CTX_set_next_proto_select_cb(SSL_CTX *ctx, int (*cb) (SSL *s,
1748
    unsigned char **out, unsigned char *outlen, const unsigned char *in,
1749
    unsigned int inlen, void *arg), void *arg)
1750
0
{
1751
0
}
1752
1753
/*
1754
 * SSL_CTX_set_alpn_protos sets the ALPN protocol list to the specified
1755
 * protocols, which must be in wire-format (i.e. a series of non-empty,
1756
 * 8-bit length-prefixed strings). Returns 0 on success.
1757
 */
1758
int
1759
SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
1760
    unsigned int protos_len)
1761
0
{
1762
0
  CBS cbs;
1763
0
  int failed = 1;
1764
1765
0
  if (protos == NULL)
1766
0
    protos_len = 0;
1767
1768
0
  CBS_init(&cbs, protos, protos_len);
1769
1770
0
  if (protos_len > 0) {
1771
0
    if (!tlsext_alpn_check_format(&cbs))
1772
0
      goto err;
1773
0
  }
1774
1775
0
  if (!CBS_stow(&cbs, &ctx->internal->alpn_client_proto_list,
1776
0
      &ctx->internal->alpn_client_proto_list_len))
1777
0
    goto err;
1778
1779
0
  failed = 0;
1780
1781
0
 err:
1782
  /* NOTE: Return values are the reverse of what you expect. */
1783
0
  return failed;
1784
0
}
1785
1786
/*
1787
 * SSL_set_alpn_protos sets the ALPN protocol list to the specified
1788
 * protocols, which must be in wire-format (i.e. a series of non-empty,
1789
 * 8-bit length-prefixed strings). Returns 0 on success.
1790
 */
1791
int
1792
SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
1793
    unsigned int protos_len)
1794
0
{
1795
0
  CBS cbs;
1796
0
  int failed = 1;
1797
1798
0
  if (protos == NULL)
1799
0
    protos_len = 0;
1800
1801
0
  CBS_init(&cbs, protos, protos_len);
1802
1803
0
  if (protos_len > 0) {
1804
0
    if (!tlsext_alpn_check_format(&cbs))
1805
0
      goto err;
1806
0
  }
1807
1808
0
  if (!CBS_stow(&cbs, &ssl->internal->alpn_client_proto_list,
1809
0
      &ssl->internal->alpn_client_proto_list_len))
1810
0
    goto err;
1811
1812
0
  failed = 0;
1813
1814
0
 err:
1815
  /* NOTE: Return values are the reverse of what you expect. */
1816
0
  return failed;
1817
0
}
1818
1819
/*
1820
 * SSL_CTX_set_alpn_select_cb sets a callback function that is called during
1821
 * ClientHello processing in order to select an ALPN protocol from the
1822
 * client's list of offered protocols.
1823
 */
1824
void
1825
SSL_CTX_set_alpn_select_cb(SSL_CTX* ctx,
1826
    int (*cb) (SSL *ssl, const unsigned char **out, unsigned char *outlen,
1827
    const unsigned char *in, unsigned int inlen, void *arg), void *arg)
1828
0
{
1829
0
  ctx->internal->alpn_select_cb = cb;
1830
0
  ctx->internal->alpn_select_cb_arg = arg;
1831
0
}
1832
1833
/*
1834
 * SSL_get0_alpn_selected gets the selected ALPN protocol (if any). On return
1835
 * it sets data to point to len bytes of protocol name (not including the
1836
 * leading length-prefix byte). If the server didn't respond with* a negotiated
1837
 * protocol then len will be zero.
1838
 */
1839
void
1840
SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
1841
    unsigned int *len)
1842
0
{
1843
0
  *data = ssl->s3->alpn_selected;
1844
0
  *len = ssl->s3->alpn_selected_len;
1845
0
}
1846
1847
void
1848
SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb)
1849
0
{
1850
0
  return;
1851
0
}
1852
1853
int
1854
SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1855
    const char *label, size_t llen, const unsigned char *p, size_t plen,
1856
    int use_context)
1857
0
{
1858
0
  if (s->internal->tls13 != NULL && s->version == TLS1_3_VERSION) {
1859
0
    if (!use_context) {
1860
0
      p = NULL;
1861
0
      plen = 0;
1862
0
    }
1863
0
    return tls13_exporter(s->internal->tls13, label, llen, p, plen,
1864
0
        out, olen);
1865
0
  }
1866
1867
0
  return (tls1_export_keying_material(s, out, olen, label, llen, p, plen,
1868
0
      use_context));
1869
0
}
1870
1871
static unsigned long
1872
ssl_session_hash(const SSL_SESSION *a)
1873
12.2k
{
1874
12.2k
  unsigned long l;
1875
1876
12.2k
  l = (unsigned long)
1877
12.2k
      ((unsigned int) a->session_id[0]     )|
1878
12.2k
      ((unsigned int) a->session_id[1]<< 8L)|
1879
12.2k
      ((unsigned long)a->session_id[2]<<16L)|
1880
12.2k
      ((unsigned long)a->session_id[3]<<24L);
1881
12.2k
  return (l);
1882
12.2k
}
1883
1884
/*
1885
 * NB: If this function (or indeed the hash function which uses a sort of
1886
 * coarser function than this one) is changed, ensure
1887
 * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
1888
 * able to construct an SSL_SESSION that will collide with any existing session
1889
 * with a matching session ID.
1890
 */
1891
static int
1892
ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
1893
0
{
1894
0
  if (a->ssl_version != b->ssl_version)
1895
0
    return (1);
1896
0
  if (a->session_id_length != b->session_id_length)
1897
0
    return (1);
1898
0
  if (timingsafe_memcmp(a->session_id, b->session_id, a->session_id_length) != 0)
1899
0
    return (1);
1900
0
  return (0);
1901
0
}
1902
1903
/*
1904
 * These wrapper functions should remain rather than redeclaring
1905
 * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
1906
 * variable. The reason is that the functions aren't static, they're exposed via
1907
 * ssl.h.
1908
 */
1909
static unsigned long
1910
ssl_session_LHASH_HASH(const void *arg)
1911
12.2k
{
1912
12.2k
  const SSL_SESSION *a = arg;
1913
1914
12.2k
  return ssl_session_hash(a);
1915
12.2k
}
1916
1917
static int
1918
ssl_session_LHASH_COMP(const void *arg1, const void *arg2)
1919
0
{
1920
0
  const SSL_SESSION *a = arg1;
1921
0
  const SSL_SESSION *b = arg2;
1922
1923
0
  return ssl_session_cmp(a, b);
1924
0
}
1925
1926
SSL_CTX *
1927
SSL_CTX_new(const SSL_METHOD *meth)
1928
6.07k
{
1929
6.07k
  SSL_CTX *ret;
1930
1931
6.07k
  if (!OPENSSL_init_ssl(0, NULL)) {
1932
0
    SSLerrorx(SSL_R_LIBRARY_BUG);
1933
0
    return (NULL);
1934
0
  }
1935
1936
6.07k
  if (meth == NULL) {
1937
0
    SSLerrorx(SSL_R_NULL_SSL_METHOD_PASSED);
1938
0
    return (NULL);
1939
0
  }
1940
1941
6.07k
  if ((ret = calloc(1, sizeof(*ret))) == NULL) {
1942
0
    SSLerrorx(ERR_R_MALLOC_FAILURE);
1943
0
    return (NULL);
1944
0
  }
1945
6.07k
  if ((ret->internal = calloc(1, sizeof(*ret->internal))) == NULL) {
1946
0
    free(ret);
1947
0
    SSLerrorx(ERR_R_MALLOC_FAILURE);
1948
0
    return (NULL);
1949
0
  }
1950
1951
6.07k
  if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
1952
0
    SSLerrorx(SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
1953
0
    goto err;
1954
0
  }
1955
1956
6.07k
  ret->method = meth;
1957
6.07k
  ret->internal->min_tls_version = meth->min_tls_version;
1958
6.07k
  ret->internal->max_tls_version = meth->max_tls_version;
1959
6.07k
  ret->internal->min_proto_version = 0;
1960
6.07k
  ret->internal->max_proto_version = 0;
1961
6.07k
  ret->internal->mode = SSL_MODE_AUTO_RETRY;
1962
1963
6.07k
  ret->cert_store = NULL;
1964
6.07k
  ret->internal->session_cache_mode = SSL_SESS_CACHE_SERVER;
1965
6.07k
  ret->internal->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
1966
6.07k
  ret->internal->session_cache_head = NULL;
1967
6.07k
  ret->internal->session_cache_tail = NULL;
1968
1969
  /* We take the system default */
1970
6.07k
  ret->session_timeout = ssl_get_default_timeout();
1971
1972
6.07k
  ret->internal->new_session_cb = 0;
1973
6.07k
  ret->internal->remove_session_cb = 0;
1974
6.07k
  ret->internal->get_session_cb = 0;
1975
6.07k
  ret->internal->generate_session_id = 0;
1976
1977
6.07k
  memset((char *)&ret->internal->stats, 0, sizeof(ret->internal->stats));
1978
1979
6.07k
  ret->references = 1;
1980
6.07k
  ret->internal->quiet_shutdown = 0;
1981
1982
6.07k
  ret->internal->info_callback = NULL;
1983
1984
6.07k
  ret->internal->app_verify_callback = 0;
1985
6.07k
  ret->internal->app_verify_arg = NULL;
1986
1987
6.07k
  ret->internal->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
1988
6.07k
  ret->internal->read_ahead = 0;
1989
6.07k
  ret->internal->msg_callback = 0;
1990
6.07k
  ret->internal->msg_callback_arg = NULL;
1991
6.07k
  ret->verify_mode = SSL_VERIFY_NONE;
1992
6.07k
  ret->sid_ctx_length = 0;
1993
6.07k
  ret->internal->default_verify_callback = NULL;
1994
1995
6.07k
  if ((ret->internal->cert = ssl_cert_new()) == NULL)
1996
0
    goto err;
1997
1998
6.07k
  ret->default_passwd_callback = 0;
1999
6.07k
  ret->default_passwd_callback_userdata = NULL;
2000
6.07k
  ret->internal->client_cert_cb = 0;
2001
6.07k
  ret->internal->app_gen_cookie_cb = 0;
2002
6.07k
  ret->internal->app_verify_cookie_cb = 0;
2003
2004
6.07k
  ret->internal->sessions = lh_SSL_SESSION_new();
2005
6.07k
  if (ret->internal->sessions == NULL)
2006
0
    goto err;
2007
6.07k
  ret->cert_store = X509_STORE_new();
2008
6.07k
  if (ret->cert_store == NULL)
2009
0
    goto err;
2010
2011
6.07k
  ssl_create_cipher_list(ret->method, &ret->cipher_list,
2012
6.07k
      NULL, SSL_DEFAULT_CIPHER_LIST, ret->internal->cert);
2013
6.07k
  if (ret->cipher_list == NULL ||
2014
6.07k
      sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
2015
0
    SSLerrorx(SSL_R_LIBRARY_HAS_NO_CIPHERS);
2016
0
    goto err2;
2017
0
  }
2018
2019
6.07k
  ret->param = X509_VERIFY_PARAM_new();
2020
6.07k
  if (!ret->param)
2021
0
    goto err;
2022
2023
6.07k
  if ((ret->internal->client_CA = sk_X509_NAME_new_null()) == NULL)
2024
0
    goto err;
2025
2026
6.07k
  CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->internal->ex_data);
2027
2028
6.07k
  ret->extra_certs = NULL;
2029
2030
6.07k
  ret->internal->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
2031
2032
6.07k
  ret->internal->tlsext_servername_callback = 0;
2033
6.07k
  ret->internal->tlsext_servername_arg = NULL;
2034
2035
  /* Setup RFC4507 ticket keys */
2036
6.07k
  arc4random_buf(ret->internal->tlsext_tick_key_name, 16);
2037
6.07k
  arc4random_buf(ret->internal->tlsext_tick_hmac_key, 16);
2038
6.07k
  arc4random_buf(ret->internal->tlsext_tick_aes_key, 16);
2039
2040
6.07k
  ret->internal->tlsext_status_cb = 0;
2041
6.07k
  ret->internal->tlsext_status_arg = NULL;
2042
2043
6.07k
#ifndef OPENSSL_NO_ENGINE
2044
6.07k
  ret->internal->client_cert_engine = NULL;
2045
#ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
2046
#define eng_strx(x) #x
2047
#define eng_str(x)  eng_strx(x)
2048
  /* Use specific client engine automatically... ignore errors */
2049
  {
2050
    ENGINE *eng;
2051
    eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
2052
    if (!eng) {
2053
      ERR_clear_error();
2054
      ENGINE_load_builtin_engines();
2055
      eng = ENGINE_by_id(eng_str(
2056
          OPENSSL_SSL_CLIENT_ENGINE_AUTO));
2057
    }
2058
    if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
2059
      ERR_clear_error();
2060
  }
2061
#endif
2062
6.07k
#endif
2063
  /*
2064
   * Default is to connect to non-RI servers. When RI is more widely
2065
   * deployed might change this.
2066
   */
2067
6.07k
  ret->internal->options |= SSL_OP_LEGACY_SERVER_CONNECT;
2068
2069
6.07k
  return (ret);
2070
0
 err:
2071
0
  SSLerrorx(ERR_R_MALLOC_FAILURE);
2072
0
 err2:
2073
0
  SSL_CTX_free(ret);
2074
0
  return (NULL);
2075
0
}
2076
2077
void
2078
SSL_CTX_free(SSL_CTX *ctx)
2079
18.2k
{
2080
18.2k
  int i;
2081
2082
18.2k
  if (ctx == NULL)
2083
0
    return;
2084
2085
18.2k
  i = CRYPTO_add(&ctx->references, -1, CRYPTO_LOCK_SSL_CTX);
2086
18.2k
  if (i > 0)
2087
12.1k
    return;
2088
2089
6.07k
  X509_VERIFY_PARAM_free(ctx->param);
2090
2091
  /*
2092
   * Free internal session cache. However: the remove_cb() may reference
2093
   * the ex_data of SSL_CTX, thus the ex_data store can only be removed
2094
   * after the sessions were flushed.
2095
   * As the ex_data handling routines might also touch the session cache,
2096
   * the most secure solution seems to be: empty (flush) the cache, then
2097
   * free ex_data, then finally free the cache.
2098
   * (See ticket [openssl.org #212].)
2099
   */
2100
6.07k
  if (ctx->internal->sessions != NULL)
2101
6.07k
    SSL_CTX_flush_sessions(ctx, 0);
2102
2103
6.07k
  CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ctx, &ctx->internal->ex_data);
2104
2105
6.07k
  lh_SSL_SESSION_free(ctx->internal->sessions);
2106
2107
6.07k
  X509_STORE_free(ctx->cert_store);
2108
6.07k
  sk_SSL_CIPHER_free(ctx->cipher_list);
2109
6.07k
  sk_SSL_CIPHER_free(ctx->internal->cipher_list_tls13);
2110
6.07k
  ssl_cert_free(ctx->internal->cert);
2111
6.07k
  sk_X509_NAME_pop_free(ctx->internal->client_CA, X509_NAME_free);
2112
6.07k
  sk_X509_pop_free(ctx->extra_certs, X509_free);
2113
2114
6.07k
#ifndef OPENSSL_NO_SRTP
2115
6.07k
  if (ctx->internal->srtp_profiles)
2116
6.07k
    sk_SRTP_PROTECTION_PROFILE_free(ctx->internal->srtp_profiles);
2117
6.07k
#endif
2118
2119
6.07k
#ifndef OPENSSL_NO_ENGINE
2120
6.07k
  ENGINE_finish(ctx->internal->client_cert_engine);
2121
6.07k
#endif
2122
2123
6.07k
  free(ctx->internal->tlsext_ecpointformatlist);
2124
6.07k
  free(ctx->internal->tlsext_supportedgroups);
2125
2126
6.07k
  free(ctx->internal->alpn_client_proto_list);
2127
2128
6.07k
  free(ctx->internal);
2129
6.07k
  free(ctx);
2130
6.07k
}
2131
2132
int
2133
SSL_CTX_up_ref(SSL_CTX *ctx)
2134
0
{
2135
0
  int refs = CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
2136
0
  return ((refs > 1) ? 1 : 0);
2137
0
}
2138
2139
pem_password_cb *
2140
SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
2141
0
{
2142
0
  return (ctx->default_passwd_callback);
2143
0
}
2144
2145
void
2146
SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
2147
0
{
2148
0
  ctx->default_passwd_callback = cb;
2149
0
}
2150
2151
void *
2152
SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
2153
0
{
2154
0
  return ctx->default_passwd_callback_userdata;
2155
0
}
2156
2157
void
2158
SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
2159
0
{
2160
0
  ctx->default_passwd_callback_userdata = u;
2161
0
}
2162
2163
void
2164
SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
2165
    int (*cb)(X509_STORE_CTX *, void *), void *arg)
2166
0
{
2167
0
  ctx->internal->app_verify_callback = cb;
2168
0
  ctx->internal->app_verify_arg = arg;
2169
0
}
2170
2171
void
2172
SSL_CTX_set_verify(SSL_CTX *ctx, int mode, int (*cb)(int, X509_STORE_CTX *))
2173
0
{
2174
0
  ctx->verify_mode = mode;
2175
0
  ctx->internal->default_verify_callback = cb;
2176
0
}
2177
2178
void
2179
SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
2180
0
{
2181
0
  X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2182
0
}
2183
2184
void
2185
ssl_set_cert_masks(SSL_CERT *c, const SSL_CIPHER *cipher)
2186
14.0k
{
2187
14.0k
  unsigned long mask_a, mask_k;
2188
14.0k
  SSL_CERT_PKEY *cpk;
2189
2190
14.0k
  if (c == NULL)
2191
0
    return;
2192
2193
14.0k
  mask_a = SSL_aNULL | SSL_aTLS1_3;
2194
14.0k
  mask_k = SSL_kECDHE | SSL_kTLS1_3;
2195
2196
14.0k
  if (c->dhe_params != NULL || c->dhe_params_cb != NULL ||
2197
14.0k
      c->dhe_params_auto != 0)
2198
0
    mask_k |= SSL_kDHE;
2199
2200
14.0k
  cpk = &(c->pkeys[SSL_PKEY_ECC]);
2201
14.0k
  if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2202
    /* Key usage, if present, must allow signing. */
2203
14.0k
    if (X509_get_key_usage(cpk->x509) & X509v3_KU_DIGITAL_SIGNATURE)
2204
14.0k
      mask_a |= SSL_aECDSA;
2205
14.0k
  }
2206
2207
14.0k
  cpk = &(c->pkeys[SSL_PKEY_GOST01]);
2208
14.0k
  if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2209
0
    mask_k |= SSL_kGOST;
2210
0
    mask_a |= SSL_aGOST01;
2211
0
  }
2212
2213
14.0k
  cpk = &(c->pkeys[SSL_PKEY_RSA]);
2214
14.0k
  if (cpk->x509 != NULL && cpk->privatekey != NULL) {
2215
14.0k
    mask_a |= SSL_aRSA;
2216
14.0k
    mask_k |= SSL_kRSA;
2217
14.0k
  }
2218
2219
14.0k
  c->mask_k = mask_k;
2220
14.0k
  c->mask_a = mask_a;
2221
14.0k
  c->valid = 1;
2222
14.0k
}
2223
2224
/* See if this handshake is using an ECC cipher suite. */
2225
int
2226
ssl_using_ecc_cipher(SSL *s)
2227
4.24k
{
2228
4.24k
  unsigned long alg_a, alg_k;
2229
2230
4.24k
  alg_a = s->s3->hs.cipher->algorithm_auth;
2231
4.24k
  alg_k = s->s3->hs.cipher->algorithm_mkey;
2232
2233
4.24k
  return s->session->tlsext_ecpointformatlist != NULL &&
2234
4.24k
      s->session->tlsext_ecpointformatlist_length > 0 &&
2235
4.24k
      ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA));
2236
4.24k
}
2237
2238
int
2239
ssl_check_srvr_ecc_cert_and_alg(SSL *s, X509 *x)
2240
0
{
2241
0
  const SSL_CIPHER *cs = s->s3->hs.cipher;
2242
0
  unsigned long alg_a;
2243
2244
0
  alg_a = cs->algorithm_auth;
2245
2246
0
  if (alg_a & SSL_aECDSA) {
2247
    /* Key usage, if present, must allow signing. */
2248
0
    if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) {
2249
0
      SSLerror(s, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
2250
0
      return (0);
2251
0
    }
2252
0
  }
2253
2254
0
  return (1);
2255
0
}
2256
2257
SSL_CERT_PKEY *
2258
ssl_get_server_send_pkey(const SSL *s)
2259
4.15k
{
2260
4.15k
  unsigned long alg_a;
2261
4.15k
  SSL_CERT *c;
2262
4.15k
  int i;
2263
2264
4.15k
  c = s->cert;
2265
4.15k
  ssl_set_cert_masks(c, s->s3->hs.cipher);
2266
2267
4.15k
  alg_a = s->s3->hs.cipher->algorithm_auth;
2268
2269
4.15k
  if (alg_a & SSL_aECDSA) {
2270
426
    i = SSL_PKEY_ECC;
2271
3.73k
  } else if (alg_a & SSL_aRSA) {
2272
3.73k
    i = SSL_PKEY_RSA;
2273
3.73k
  } else if (alg_a & SSL_aGOST01) {
2274
0
    i = SSL_PKEY_GOST01;
2275
0
  } else { /* if (alg_a & SSL_aNULL) */
2276
0
    SSLerror(s, ERR_R_INTERNAL_ERROR);
2277
0
    return (NULL);
2278
0
  }
2279
2280
4.15k
  return (c->pkeys + i);
2281
4.15k
}
2282
2283
EVP_PKEY *
2284
ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher, const EVP_MD **pmd,
2285
    const struct ssl_sigalg **sap)
2286
3.30k
{
2287
3.30k
  const struct ssl_sigalg *sigalg = NULL;
2288
3.30k
  EVP_PKEY *pkey = NULL;
2289
3.30k
  unsigned long alg_a;
2290
3.30k
  SSL_CERT *c;
2291
3.30k
  int idx = -1;
2292
2293
3.30k
  alg_a = cipher->algorithm_auth;
2294
3.30k
  c = s->cert;
2295
2296
3.30k
  if (alg_a & SSL_aRSA) {
2297
2.87k
    idx = SSL_PKEY_RSA;
2298
2.87k
  } else if ((alg_a & SSL_aECDSA) &&
2299
426
      (c->pkeys[SSL_PKEY_ECC].privatekey != NULL))
2300
426
    idx = SSL_PKEY_ECC;
2301
3.30k
  if (idx == -1) {
2302
0
    SSLerror(s, ERR_R_INTERNAL_ERROR);
2303
0
    return (NULL);
2304
0
  }
2305
2306
3.30k
  pkey = c->pkeys[idx].privatekey;
2307
3.30k
  if ((sigalg = ssl_sigalg_select(s, pkey)) == NULL) {
2308
33
    SSLerror(s, SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2309
33
    return (NULL);
2310
33
  }
2311
3.27k
  *pmd = sigalg->md();
2312
3.27k
  *sap = sigalg;
2313
2314
3.27k
  return (pkey);
2315
3.30k
}
2316
2317
size_t
2318
ssl_dhe_params_auto_key_bits(SSL *s)
2319
0
{
2320
0
  SSL_CERT_PKEY *cpk;
2321
0
  int key_bits;
2322
2323
0
  if (s->cert->dhe_params_auto == 2) {
2324
0
    key_bits = 1024;
2325
0
  } else if (s->s3->hs.cipher->algorithm_auth & SSL_aNULL) {
2326
0
    key_bits = 1024;
2327
0
    if (s->s3->hs.cipher->strength_bits == 256)
2328
0
      key_bits = 3072;
2329
0
  } else {
2330
0
    if ((cpk = ssl_get_server_send_pkey(s)) == NULL)
2331
0
      return 0;
2332
0
    if (cpk->privatekey == NULL ||
2333
0
        EVP_PKEY_get0_RSA(cpk->privatekey) == NULL)
2334
0
      return 0;
2335
0
    if ((key_bits = EVP_PKEY_bits(cpk->privatekey)) <= 0)
2336
0
      return 0;
2337
0
  }
2338
2339
0
  return key_bits;
2340
0
}
2341
2342
static int
2343
ssl_should_update_external_cache(SSL *s, int mode)
2344
0
{
2345
0
  int cache_mode;
2346
2347
0
  cache_mode = s->session_ctx->internal->session_cache_mode;
2348
2349
  /* Don't cache if mode says not to */
2350
0
  if ((cache_mode & mode) == 0)
2351
0
    return 0;
2352
2353
  /* if it is not already cached, cache it */
2354
0
  if (!s->internal->hit)
2355
0
    return 1;
2356
2357
  /* If it's TLS 1.3, do it to match OpenSSL */
2358
0
  if (s->s3->hs.negotiated_tls_version >= TLS1_3_VERSION)
2359
0
    return 1;
2360
2361
0
  return 0;
2362
0
}
2363
2364
static int
2365
ssl_should_update_internal_cache(SSL *s, int mode)
2366
0
{
2367
0
  int cache_mode;
2368
2369
0
  cache_mode = s->session_ctx->internal->session_cache_mode;
2370
2371
  /* Don't cache if mode says not to */
2372
0
  if ((cache_mode & mode) == 0)
2373
0
    return 0;
2374
2375
  /* If it is already cached, don't cache it again */
2376
0
  if (s->internal->hit)
2377
0
    return 0;
2378
2379
0
  if ((cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) != 0)
2380
0
    return 0;
2381
2382
  /* If we are lesser than TLS 1.3, Cache it. */
2383
0
  if (s->s3->hs.negotiated_tls_version < TLS1_3_VERSION)
2384
0
    return 1;
2385
2386
  /* Below this we consider TLS 1.3 or later */
2387
2388
  /* If it's not a server, add it? OpenSSL does this. */
2389
0
  if (!s->server)
2390
0
    return 1;
2391
2392
  /* XXX if we support early data / PSK need to add */
2393
2394
  /*
2395
   * If we have the remove session callback, we will want
2396
   * to know about this even if it's a stateless ticket
2397
   * from 1.3 so we can know when it is removed.
2398
   */
2399
0
  if (s->session_ctx->internal->remove_session_cb != NULL)
2400
0
    return 1;
2401
2402
  /* If we have set OP_NO_TICKET, cache it. */
2403
0
  if ((s->internal->options & SSL_OP_NO_TICKET) != 0)
2404
0
    return 1;
2405
2406
  /* Otherwise do not cache */
2407
0
  return 0;
2408
0
}
2409
2410
void
2411
ssl_update_cache(SSL *s, int mode)
2412
0
{
2413
0
  int cache_mode, do_callback;
2414
2415
0
  if (s->session->session_id_length == 0)
2416
0
    return;
2417
2418
0
  cache_mode = s->session_ctx->internal->session_cache_mode;
2419
0
  do_callback = ssl_should_update_external_cache(s, mode);
2420
2421
0
  if (ssl_should_update_internal_cache(s, mode)) {
2422
    /*
2423
     * XXX should we fail if the add to the internal cache
2424
     * fails? OpenSSL doesn't care..
2425
     */
2426
0
    (void) SSL_CTX_add_session(s->session_ctx, s->session);
2427
0
  }
2428
2429
  /*
2430
   * Update the "external cache" by calling the new session
2431
   * callback if present, even with TLS 1.3 without early data
2432
   * "because some application just want to know about the
2433
   * creation of a session and aren't doing a full cache".
2434
   * Apparently, if they are doing a full cache, they'll have
2435
   * some fun, but we endeavour to give application writers the
2436
   * same glorious experience they expect from OpenSSL which
2437
   * does it this way.
2438
   */
2439
0
  if (do_callback && s->session_ctx->internal->new_session_cb != NULL) {
2440
0
        CRYPTO_add(&s->session->references, 1, CRYPTO_LOCK_SSL_SESSION);
2441
0
        if (!s->session_ctx->internal->new_session_cb(s, s->session))
2442
0
          SSL_SESSION_free(s->session);
2443
0
  }
2444
2445
  /* Auto flush every 255 connections. */
2446
0
  if (!(cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
2447
0
      (cache_mode & mode) != 0) {
2448
0
    int connections;
2449
0
    if (mode & SSL_SESS_CACHE_CLIENT)
2450
0
      connections = s->session_ctx->internal->stats.sess_connect_good;
2451
0
    else
2452
0
      connections = s->session_ctx->internal->stats.sess_accept_good;
2453
0
    if ((connections & 0xff) == 0xff)
2454
0
      SSL_CTX_flush_sessions(s->session_ctx, time(NULL));
2455
0
  }
2456
0
}
2457
2458
const SSL_METHOD *
2459
SSL_get_ssl_method(SSL *s)
2460
0
{
2461
0
  return (s->method);
2462
0
}
2463
2464
int
2465
SSL_set_ssl_method(SSL *s, const SSL_METHOD *method)
2466
0
{
2467
0
  int (*handshake_func)(SSL *) = NULL;
2468
0
  int ret = 1;
2469
2470
0
  if (s->method == method)
2471
0
    return (ret);
2472
2473
0
  if (s->internal->handshake_func == s->method->ssl_connect)
2474
0
    handshake_func = method->ssl_connect;
2475
0
  else if (s->internal->handshake_func == s->method->ssl_accept)
2476
0
    handshake_func = method->ssl_accept;
2477
2478
0
  if (s->method->version == method->version) {
2479
0
    s->method = method;
2480
0
  } else {
2481
0
    s->method->ssl_free(s);
2482
0
    s->method = method;
2483
0
    ret = s->method->ssl_new(s);
2484
0
  }
2485
0
  s->internal->handshake_func = handshake_func;
2486
2487
0
  return (ret);
2488
0
}
2489
2490
int
2491
SSL_get_error(const SSL *s, int i)
2492
0
{
2493
0
  unsigned long l;
2494
0
  int reason;
2495
0
  BIO *bio;
2496
2497
0
  if (i > 0)
2498
0
    return (SSL_ERROR_NONE);
2499
2500
  /*
2501
   * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake
2502
   * etc, where we do encode the error.
2503
   */
2504
0
  if ((l = ERR_peek_error()) != 0) {
2505
0
    if (ERR_GET_LIB(l) == ERR_LIB_SYS)
2506
0
      return (SSL_ERROR_SYSCALL);
2507
0
    else
2508
0
      return (SSL_ERROR_SSL);
2509
0
  }
2510
2511
0
  if (SSL_want_read(s)) {
2512
0
    bio = SSL_get_rbio(s);
2513
0
    if (BIO_should_read(bio)) {
2514
0
      return (SSL_ERROR_WANT_READ);
2515
0
    } else if (BIO_should_write(bio)) {
2516
      /*
2517
       * This one doesn't make too much sense...  We never
2518
       * try to write to the rbio, and an application
2519
       * program where rbio and wbio are separate couldn't
2520
       * even know what it should wait for.  However if we
2521
       * ever set s->internal->rwstate incorrectly (so that we have
2522
       * SSL_want_read(s) instead of SSL_want_write(s))
2523
       * and rbio and wbio *are* the same, this test works
2524
       * around that bug; so it might be safer to keep it.
2525
       */
2526
0
      return (SSL_ERROR_WANT_WRITE);
2527
0
    } else if (BIO_should_io_special(bio)) {
2528
0
      reason = BIO_get_retry_reason(bio);
2529
0
      if (reason == BIO_RR_CONNECT)
2530
0
        return (SSL_ERROR_WANT_CONNECT);
2531
0
      else if (reason == BIO_RR_ACCEPT)
2532
0
        return (SSL_ERROR_WANT_ACCEPT);
2533
0
      else
2534
0
        return (SSL_ERROR_SYSCALL); /* unknown */
2535
0
    }
2536
0
  }
2537
2538
0
  if (SSL_want_write(s)) {
2539
0
    bio = SSL_get_wbio(s);
2540
0
    if (BIO_should_write(bio)) {
2541
0
      return (SSL_ERROR_WANT_WRITE);
2542
0
    } else if (BIO_should_read(bio)) {
2543
      /*
2544
       * See above (SSL_want_read(s) with
2545
       * BIO_should_write(bio))
2546
       */
2547
0
      return (SSL_ERROR_WANT_READ);
2548
0
    } else if (BIO_should_io_special(bio)) {
2549
0
      reason = BIO_get_retry_reason(bio);
2550
0
      if (reason == BIO_RR_CONNECT)
2551
0
        return (SSL_ERROR_WANT_CONNECT);
2552
0
      else if (reason == BIO_RR_ACCEPT)
2553
0
        return (SSL_ERROR_WANT_ACCEPT);
2554
0
      else
2555
0
        return (SSL_ERROR_SYSCALL);
2556
0
    }
2557
0
  }
2558
2559
0
  if (SSL_want_x509_lookup(s))
2560
0
    return (SSL_ERROR_WANT_X509_LOOKUP);
2561
2562
0
  if ((s->internal->shutdown & SSL_RECEIVED_SHUTDOWN) &&
2563
0
      (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY))
2564
0
    return (SSL_ERROR_ZERO_RETURN);
2565
2566
0
  return (SSL_ERROR_SYSCALL);
2567
0
}
2568
2569
int
2570
SSL_do_handshake(SSL *s)
2571
6.07k
{
2572
6.07k
  if (s->internal->handshake_func == NULL) {
2573
0
    SSLerror(s, SSL_R_CONNECTION_TYPE_NOT_SET);
2574
0
    return (-1);
2575
0
  }
2576
2577
6.07k
  s->method->ssl_renegotiate_check(s);
2578
2579
6.07k
  if (!SSL_in_init(s) && !SSL_in_before(s))
2580
0
    return 1;
2581
2582
6.07k
  return s->internal->handshake_func(s);
2583
6.07k
}
2584
2585
/*
2586
 * For the next 2 functions, SSL_clear() sets shutdown and so
2587
 * one of these calls will reset it
2588
 */
2589
void
2590
SSL_set_accept_state(SSL *s)
2591
6.07k
{
2592
6.07k
  s->server = 1;
2593
6.07k
  s->internal->shutdown = 0;
2594
6.07k
  s->s3->hs.state = SSL_ST_ACCEPT|SSL_ST_BEFORE;
2595
6.07k
  s->internal->handshake_func = s->method->ssl_accept;
2596
6.07k
  ssl_clear_cipher_state(s);
2597
6.07k
}
2598
2599
void
2600
SSL_set_connect_state(SSL *s)
2601
0
{
2602
0
  s->server = 0;
2603
0
  s->internal->shutdown = 0;
2604
0
  s->s3->hs.state = SSL_ST_CONNECT|SSL_ST_BEFORE;
2605
0
  s->internal->handshake_func = s->method->ssl_connect;
2606
0
  ssl_clear_cipher_state(s);
2607
0
}
2608
2609
int
2610
ssl_undefined_function(SSL *s)
2611
0
{
2612
0
  SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2613
0
  return (0);
2614
0
}
2615
2616
int
2617
ssl_undefined_void_function(void)
2618
0
{
2619
0
  SSLerrorx(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2620
0
  return (0);
2621
0
}
2622
2623
int
2624
ssl_undefined_const_function(const SSL *s)
2625
0
{
2626
0
  SSLerror(s, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
2627
0
  return (0);
2628
0
}
2629
2630
const char *
2631
ssl_version_string(int ver)
2632
0
{
2633
0
  switch (ver) {
2634
0
  case TLS1_VERSION:
2635
0
    return (SSL_TXT_TLSV1);
2636
0
  case TLS1_1_VERSION:
2637
0
    return (SSL_TXT_TLSV1_1);
2638
0
  case TLS1_2_VERSION:
2639
0
    return (SSL_TXT_TLSV1_2);
2640
0
  case TLS1_3_VERSION:
2641
0
    return (SSL_TXT_TLSV1_3);
2642
0
  case DTLS1_VERSION:
2643
0
    return (SSL_TXT_DTLS1);
2644
0
  case DTLS1_2_VERSION:
2645
0
    return (SSL_TXT_DTLS1_2);
2646
0
  default:
2647
0
    return ("unknown");
2648
0
  }
2649
0
}
2650
2651
const char *
2652
SSL_get_version(const SSL *s)
2653
0
{
2654
0
  return ssl_version_string(s->version);
2655
0
}
2656
2657
SSL *
2658
SSL_dup(SSL *s)
2659
0
{
2660
0
  STACK_OF(X509_NAME) *sk;
2661
0
  X509_NAME *xn;
2662
0
  SSL *ret;
2663
0
  int i;
2664
2665
0
  if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
2666
0
    goto err;
2667
2668
0
  ret->version = s->version;
2669
0
  ret->method = s->method;
2670
2671
0
  if (s->session != NULL) {
2672
0
    if (!SSL_copy_session_id(ret, s))
2673
0
      goto err;
2674
0
  } else {
2675
    /*
2676
     * No session has been established yet, so we have to expect
2677
     * that s->cert or ret->cert will be changed later --
2678
     * they should not both point to the same object,
2679
     * and thus we can't use SSL_copy_session_id.
2680
     */
2681
2682
0
    ret->method->ssl_free(ret);
2683
0
    ret->method = s->method;
2684
0
    ret->method->ssl_new(ret);
2685
2686
0
    ssl_cert_free(ret->cert);
2687
0
    if ((ret->cert = ssl_cert_dup(s->cert)) == NULL)
2688
0
      goto err;
2689
2690
0
    if (!SSL_set_session_id_context(ret, s->sid_ctx,
2691
0
        s->sid_ctx_length))
2692
0
      goto err;
2693
0
  }
2694
2695
0
  ret->internal->options = s->internal->options;
2696
0
  ret->internal->mode = s->internal->mode;
2697
0
  SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
2698
0
  SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
2699
0
  ret->internal->msg_callback = s->internal->msg_callback;
2700
0
  ret->internal->msg_callback_arg = s->internal->msg_callback_arg;
2701
0
  SSL_set_verify(ret, SSL_get_verify_mode(s),
2702
0
  SSL_get_verify_callback(s));
2703
0
  SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
2704
0
  ret->internal->generate_session_id = s->internal->generate_session_id;
2705
2706
0
  SSL_set_info_callback(ret, SSL_get_info_callback(s));
2707
2708
0
  ret->internal->debug = s->internal->debug;
2709
2710
  /* copy app data, a little dangerous perhaps */
2711
0
  if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL,
2712
0
      &ret->internal->ex_data, &s->internal->ex_data))
2713
0
    goto err;
2714
2715
  /* setup rbio, and wbio */
2716
0
  if (s->rbio != NULL) {
2717
0
    if (!BIO_dup_state(s->rbio,(char *)&ret->rbio))
2718
0
      goto err;
2719
0
  }
2720
0
  if (s->wbio != NULL) {
2721
0
    if (s->wbio != s->rbio) {
2722
0
      if (!BIO_dup_state(s->wbio,(char *)&ret->wbio))
2723
0
        goto err;
2724
0
    } else
2725
0
      ret->wbio = ret->rbio;
2726
0
  }
2727
0
  ret->internal->rwstate = s->internal->rwstate;
2728
0
  ret->internal->in_handshake = s->internal->in_handshake;
2729
0
  ret->internal->handshake_func = s->internal->handshake_func;
2730
0
  ret->server = s->server;
2731
0
  ret->internal->renegotiate = s->internal->renegotiate;
2732
0
  ret->internal->new_session = s->internal->new_session;
2733
0
  ret->internal->quiet_shutdown = s->internal->quiet_shutdown;
2734
0
  ret->internal->shutdown = s->internal->shutdown;
2735
  /* SSL_dup does not really work at any state, though */
2736
0
  ret->s3->hs.state = s->s3->hs.state;
2737
0
  ret->internal->rstate = s->internal->rstate;
2738
2739
  /*
2740
   * Would have to copy ret->init_buf, ret->init_msg, ret->init_num,
2741
   * ret->init_off
2742
   */
2743
0
  ret->internal->init_num = 0;
2744
2745
0
  ret->internal->hit = s->internal->hit;
2746
2747
0
  X509_VERIFY_PARAM_inherit(ret->param, s->param);
2748
2749
0
  if (s->cipher_list != NULL) {
2750
0
    if ((ret->cipher_list =
2751
0
        sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
2752
0
      goto err;
2753
0
  }
2754
0
  if (s->internal->cipher_list_tls13 != NULL) {
2755
0
    if ((ret->internal->cipher_list_tls13 =
2756
0
        sk_SSL_CIPHER_dup(s->internal->cipher_list_tls13)) == NULL)
2757
0
      goto err;
2758
0
  }
2759
2760
  /* Dup the client_CA list */
2761
0
  if (s->internal->client_CA != NULL) {
2762
0
    if ((sk = sk_X509_NAME_dup(s->internal->client_CA)) == NULL) goto err;
2763
0
      ret->internal->client_CA = sk;
2764
0
    for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2765
0
      xn = sk_X509_NAME_value(sk, i);
2766
0
      if (sk_X509_NAME_set(sk, i,
2767
0
          X509_NAME_dup(xn)) == NULL) {
2768
0
        X509_NAME_free(xn);
2769
0
        goto err;
2770
0
      }
2771
0
    }
2772
0
  }
2773
2774
0
  return ret;
2775
0
 err:
2776
0
  SSL_free(ret);
2777
0
  return NULL;
2778
0
}
2779
2780
void
2781
ssl_clear_cipher_state(SSL *s)
2782
18.2k
{
2783
18.2k
  tls12_record_layer_clear_read_state(s->internal->rl);
2784
18.2k
  tls12_record_layer_clear_write_state(s->internal->rl);
2785
18.2k
}
2786
2787
void
2788
ssl_info_callback(const SSL *s, int type, int value)
2789
45.6k
{
2790
45.6k
  ssl_info_callback_fn *cb;
2791
2792
45.6k
  if ((cb = s->internal->info_callback) == NULL)
2793
45.6k
    cb = s->ctx->internal->info_callback;
2794
45.6k
  if (cb != NULL)
2795
0
    cb(s, type, value);
2796
45.6k
}
2797
2798
void
2799
ssl_msg_callback(SSL *s, int is_write, int content_type,
2800
    const void *msg_buf, size_t msg_len)
2801
21.7k
{
2802
21.7k
  if (s->internal->msg_callback != NULL)
2803
0
    s->internal->msg_callback(is_write, s->version, content_type,
2804
0
        msg_buf, msg_len, s, s->internal->msg_callback_arg);
2805
21.7k
}
2806
2807
/* Fix this function so that it takes an optional type parameter */
2808
X509 *
2809
SSL_get_certificate(const SSL *s)
2810
0
{
2811
0
  return (s->cert->key->x509);
2812
0
}
2813
2814
/* Fix this function so that it takes an optional type parameter */
2815
EVP_PKEY *
2816
SSL_get_privatekey(const SSL *s)
2817
0
{
2818
0
  return (s->cert->key->privatekey);
2819
0
}
2820
2821
const SSL_CIPHER *
2822
SSL_get_current_cipher(const SSL *s)
2823
0
{
2824
0
  if ((s->session != NULL) && (s->session->cipher != NULL))
2825
0
    return (s->session->cipher);
2826
0
  return (NULL);
2827
0
}
2828
const void *
2829
SSL_get_current_compression(SSL *s)
2830
0
{
2831
0
  return (NULL);
2832
0
}
2833
2834
const void *
2835
SSL_get_current_expansion(SSL *s)
2836
0
{
2837
0
  return (NULL);
2838
0
}
2839
2840
size_t
2841
SSL_get_client_random(const SSL *s, unsigned char *out, size_t max_out)
2842
0
{
2843
0
  size_t len = sizeof(s->s3->client_random);
2844
2845
0
  if (out == NULL)
2846
0
    return len;
2847
2848
0
  if (len > max_out)
2849
0
    len = max_out;
2850
2851
0
  memcpy(out, s->s3->client_random, len);
2852
2853
0
  return len;
2854
0
}
2855
2856
size_t
2857
SSL_get_server_random(const SSL *s, unsigned char *out, size_t max_out)
2858
0
{
2859
0
  size_t len = sizeof(s->s3->server_random);
2860
2861
0
  if (out == NULL)
2862
0
    return len;
2863
2864
0
  if (len > max_out)
2865
0
    len = max_out;
2866
2867
0
  memcpy(out, s->s3->server_random, len);
2868
2869
0
  return len;
2870
0
}
2871
2872
int
2873
ssl_init_wbio_buffer(SSL *s, int push)
2874
5.42k
{
2875
5.42k
  BIO *bbio;
2876
2877
5.42k
  if (s->bbio == NULL) {
2878
5.42k
    bbio = BIO_new(BIO_f_buffer());
2879
5.42k
    if (bbio == NULL)
2880
0
      return (0);
2881
5.42k
    s->bbio = bbio;
2882
5.42k
  } else {
2883
0
    bbio = s->bbio;
2884
0
    if (s->bbio == s->wbio)
2885
0
      s->wbio = BIO_pop(s->wbio);
2886
0
  }
2887
5.42k
  (void)BIO_reset(bbio);
2888
/*  if (!BIO_set_write_buffer_size(bbio,16*1024)) */
2889
5.42k
  if (!BIO_set_read_buffer_size(bbio, 1)) {
2890
0
    SSLerror(s, ERR_R_BUF_LIB);
2891
0
    return (0);
2892
0
  }
2893
5.42k
  if (push) {
2894
5.42k
    if (s->wbio != bbio)
2895
5.42k
      s->wbio = BIO_push(bbio, s->wbio);
2896
5.42k
  } else {
2897
0
    if (s->wbio == bbio)
2898
0
      s->wbio = BIO_pop(bbio);
2899
0
  }
2900
5.42k
  return (1);
2901
5.42k
}
2902
2903
void
2904
ssl_free_wbio_buffer(SSL *s)
2905
18.2k
{
2906
18.2k
  if (s == NULL)
2907
0
    return;
2908
2909
18.2k
  if (s->bbio == NULL)
2910
18.2k
    return;
2911
2912
0
  if (s->bbio == s->wbio) {
2913
    /* remove buffering */
2914
0
    s->wbio = BIO_pop(s->wbio);
2915
0
  }
2916
0
  BIO_free(s->bbio);
2917
0
  s->bbio = NULL;
2918
0
}
2919
2920
void
2921
SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
2922
0
{
2923
0
  ctx->internal->quiet_shutdown = mode;
2924
0
}
2925
2926
int
2927
SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
2928
0
{
2929
0
  return (ctx->internal->quiet_shutdown);
2930
0
}
2931
2932
void
2933
SSL_set_quiet_shutdown(SSL *s, int mode)
2934
0
{
2935
0
  s->internal->quiet_shutdown = mode;
2936
0
}
2937
2938
int
2939
SSL_get_quiet_shutdown(const SSL *s)
2940
0
{
2941
0
  return (s->internal->quiet_shutdown);
2942
0
}
2943
2944
void
2945
SSL_set_shutdown(SSL *s, int mode)
2946
0
{
2947
0
  s->internal->shutdown = mode;
2948
0
}
2949
2950
int
2951
SSL_get_shutdown(const SSL *s)
2952
0
{
2953
0
  return (s->internal->shutdown);
2954
0
}
2955
2956
int
2957
SSL_version(const SSL *s)
2958
0
{
2959
0
  return (s->version);
2960
0
}
2961
2962
SSL_CTX *
2963
SSL_get_SSL_CTX(const SSL *ssl)
2964
0
{
2965
0
  return (ssl->ctx);
2966
0
}
2967
2968
SSL_CTX *
2969
SSL_set_SSL_CTX(SSL *ssl, SSL_CTX* ctx)
2970
0
{
2971
0
  SSL_CERT *new_cert;
2972
2973
0
  if (ctx == NULL)
2974
0
    ctx = ssl->initial_ctx;
2975
0
  if (ssl->ctx == ctx)
2976
0
    return (ssl->ctx);
2977
2978
0
  if ((new_cert = ssl_cert_dup(ctx->internal->cert)) == NULL)
2979
0
    return NULL;
2980
0
  ssl_cert_free(ssl->cert);
2981
0
  ssl->cert = new_cert;
2982
2983
0
  SSL_CTX_up_ref(ctx);
2984
0
  SSL_CTX_free(ssl->ctx); /* decrement reference count */
2985
0
  ssl->ctx = ctx;
2986
2987
0
  return (ssl->ctx);
2988
0
}
2989
2990
int
2991
SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
2992
0
{
2993
0
  return (X509_STORE_set_default_paths(ctx->cert_store));
2994
0
}
2995
2996
int
2997
SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
2998
    const char *CApath)
2999
0
{
3000
0
  return (X509_STORE_load_locations(ctx->cert_store, CAfile, CApath));
3001
0
}
3002
3003
int
3004
SSL_CTX_load_verify_mem(SSL_CTX *ctx, void *buf, int len)
3005
0
{
3006
0
  return (X509_STORE_load_mem(ctx->cert_store, buf, len));
3007
0
}
3008
3009
void
3010
SSL_set_info_callback(SSL *ssl, void (*cb)(const SSL *ssl, int type, int val))
3011
0
{
3012
0
  ssl->internal->info_callback = cb;
3013
0
}
3014
3015
void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type, int val)
3016
0
{
3017
0
  return (ssl->internal->info_callback);
3018
0
}
3019
3020
int
3021
SSL_state(const SSL *ssl)
3022
59.3k
{
3023
59.3k
  return (ssl->s3->hs.state);
3024
59.3k
}
3025
3026
void
3027
SSL_set_state(SSL *ssl, int state)
3028
0
{
3029
0
  ssl->s3->hs.state = state;
3030
0
}
3031
3032
void
3033
SSL_set_verify_result(SSL *ssl, long arg)
3034
0
{
3035
0
  ssl->verify_result = arg;
3036
0
}
3037
3038
long
3039
SSL_get_verify_result(const SSL *ssl)
3040
0
{
3041
0
  return (ssl->verify_result);
3042
0
}
3043
3044
int
3045
SSL_verify_client_post_handshake(SSL *ssl)
3046
0
{
3047
0
  return 0;
3048
0
}
3049
3050
void
3051
SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val)
3052
0
{
3053
0
  return;
3054
0
}
3055
3056
void
3057
SSL_set_post_handshake_auth(SSL *ssl, int val)
3058
0
{
3059
0
  return;
3060
0
}
3061
3062
int
3063
SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
3064
    CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
3065
0
{
3066
0
  return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL, argl, argp,
3067
0
      new_func, dup_func, free_func));
3068
0
}
3069
3070
int
3071
SSL_set_ex_data(SSL *s, int idx, void *arg)
3072
0
{
3073
0
  return (CRYPTO_set_ex_data(&s->internal->ex_data, idx, arg));
3074
0
}
3075
3076
void *
3077
SSL_get_ex_data(const SSL *s, int idx)
3078
0
{
3079
0
  return (CRYPTO_get_ex_data(&s->internal->ex_data, idx));
3080
0
}
3081
3082
int
3083
SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
3084
    CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
3085
0
{
3086
0
  return (CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_SSL_CTX, argl, argp,
3087
0
      new_func, dup_func, free_func));
3088
0
}
3089
3090
int
3091
SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
3092
0
{
3093
0
  return (CRYPTO_set_ex_data(&s->internal->ex_data, idx, arg));
3094
0
}
3095
3096
void *
3097
SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
3098
0
{
3099
0
  return (CRYPTO_get_ex_data(&s->internal->ex_data, idx));
3100
0
}
3101
3102
int
3103
ssl_ok(SSL *s)
3104
6.07k
{
3105
6.07k
  return (1);
3106
6.07k
}
3107
3108
X509_STORE *
3109
SSL_CTX_get_cert_store(const SSL_CTX *ctx)
3110
0
{
3111
0
  return (ctx->cert_store);
3112
0
}
3113
3114
void
3115
SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
3116
0
{
3117
0
  X509_STORE_free(ctx->cert_store);
3118
0
  ctx->cert_store = store;
3119
0
}
3120
3121
X509 *
3122
SSL_CTX_get0_certificate(const SSL_CTX *ctx)
3123
0
{
3124
0
  if (ctx->internal->cert == NULL)
3125
0
    return NULL;
3126
3127
0
  return ctx->internal->cert->key->x509;
3128
0
}
3129
3130
EVP_PKEY *
3131
SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
3132
0
{
3133
0
  if (ctx->internal->cert == NULL)
3134
0
    return NULL;
3135
3136
0
  return ctx->internal->cert->key->privatekey;
3137
0
}
3138
3139
int
3140
SSL_want(const SSL *s)
3141
0
{
3142
0
  return (s->internal->rwstate);
3143
0
}
3144
3145
void
3146
SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx, RSA *(*cb)(SSL *ssl, int is_export,
3147
    int keylength))
3148
0
{
3149
0
  SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3150
0
}
3151
3152
void
3153
SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
3154
    int keylength))
3155
0
{
3156
0
  SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB,(void (*)(void))cb);
3157
0
}
3158
3159
void
3160
SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx, DH *(*dh)(SSL *ssl, int is_export,
3161
    int keylength))
3162
0
{
3163
0
  SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3164
0
}
3165
3166
void
3167
SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
3168
    int keylength))
3169
0
{
3170
0
  SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB,(void (*)(void))dh);
3171
0
}
3172
3173
void
3174
SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx, EC_KEY *(*ecdh)(SSL *ssl,
3175
    int is_export, int keylength))
3176
0
{
3177
0
  SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB,
3178
0
      (void (*)(void))ecdh);
3179
0
}
3180
3181
void
3182
SSL_set_tmp_ecdh_callback(SSL *ssl, EC_KEY *(*ecdh)(SSL *ssl, int is_export,
3183
    int keylength))
3184
0
{
3185
0
  SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB,(void (*)(void))ecdh);
3186
0
}
3187
3188
3189
void
3190
SSL_CTX_set_msg_callback(SSL_CTX *ctx, void (*cb)(int write_p, int version,
3191
    int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
3192
0
{
3193
0
  SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK,
3194
0
      (void (*)(void))cb);
3195
0
}
3196
3197
void
3198
SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version,
3199
    int content_type, const void *buf, size_t len, SSL *ssl, void *arg))
3200
0
{
3201
0
  SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
3202
0
}
3203
3204
void
3205
SSL_set_debug(SSL *s, int debug)
3206
0
{
3207
0
  s->internal->debug = debug;
3208
0
}
3209
3210
int
3211
SSL_cache_hit(SSL *s)
3212
0
{
3213
0
  return (s->internal->hit);
3214
0
}
3215
3216
int
3217
SSL_CTX_get_min_proto_version(SSL_CTX *ctx)
3218
0
{
3219
0
  return ctx->internal->min_proto_version;
3220
0
}
3221
3222
int
3223
SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version)
3224
6.07k
{
3225
6.07k
  return ssl_version_set_min(ctx->method, version,
3226
6.07k
      ctx->internal->max_tls_version, &ctx->internal->min_tls_version,
3227
6.07k
      &ctx->internal->min_proto_version);
3228
6.07k
}
3229
3230
int
3231
SSL_CTX_get_max_proto_version(SSL_CTX *ctx)
3232
0
{
3233
0
  return ctx->internal->max_proto_version;
3234
0
}
3235
3236
int
3237
SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version)
3238
0
{
3239
0
  return ssl_version_set_max(ctx->method, version,
3240
0
      ctx->internal->min_tls_version, &ctx->internal->max_tls_version,
3241
0
      &ctx->internal->max_proto_version);
3242
0
}
3243
3244
int
3245
SSL_get_min_proto_version(SSL *ssl)
3246
0
{
3247
0
  return ssl->internal->min_proto_version;
3248
0
}
3249
3250
int
3251
SSL_set_min_proto_version(SSL *ssl, uint16_t version)
3252
0
{
3253
0
  return ssl_version_set_min(ssl->method, version,
3254
0
      ssl->internal->max_tls_version, &ssl->internal->min_tls_version,
3255
0
      &ssl->internal->min_proto_version);
3256
0
}
3257
int
3258
SSL_get_max_proto_version(SSL *ssl)
3259
0
{
3260
0
  return ssl->internal->max_proto_version;
3261
0
}
3262
3263
int
3264
SSL_set_max_proto_version(SSL *ssl, uint16_t version)
3265
0
{
3266
0
  return ssl_version_set_max(ssl->method, version,
3267
0
      ssl->internal->min_tls_version, &ssl->internal->max_tls_version,
3268
0
      &ssl->internal->max_proto_version);
3269
0
}
3270
3271
const SSL_METHOD *
3272
SSL_CTX_get_ssl_method(const SSL_CTX *ctx)
3273
0
{
3274
0
  return ctx->method;
3275
0
}
3276
3277
int
3278
SSL_CTX_get_security_level(const SSL_CTX *ctx)
3279
24.3k
{
3280
24.3k
  return ctx->internal->cert->security_level;
3281
24.3k
}
3282
3283
void
3284
SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
3285
0
{
3286
0
  ctx->internal->cert->security_level = level;
3287
0
}
3288
3289
int
3290
SSL_get_security_level(const SSL *ssl)
3291
29.7k
{
3292
29.7k
  return ssl->cert->security_level;
3293
29.7k
}
3294
3295
void
3296
SSL_set_security_level(SSL *ssl, int level)
3297
0
{
3298
0
  ssl->cert->security_level = level;
3299
0
}
3300
3301
int
3302
SSL_is_quic(const SSL *ssl)
3303
10.3k
{
3304
10.3k
  return ssl->quic_method != NULL;
3305
10.3k
}
3306
3307
int
3308
SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params,
3309
    size_t params_len)
3310
0
{
3311
0
  freezero(ssl->internal->quic_transport_params,
3312
0
      ssl->internal->quic_transport_params_len);
3313
0
  ssl->internal->quic_transport_params = NULL;
3314
0
  ssl->internal->quic_transport_params_len = 0;
3315
3316
0
  if ((ssl->internal->quic_transport_params = malloc(params_len)) == NULL)
3317
0
    return 0;
3318
3319
0
  memcpy(ssl->internal->quic_transport_params, params, params_len);
3320
0
  ssl->internal->quic_transport_params_len = params_len;
3321
3322
0
  return 1;
3323
0
}
3324
3325
void
3326
SSL_get_peer_quic_transport_params(const SSL *ssl, const uint8_t **out_params,
3327
    size_t *out_params_len)
3328
0
{
3329
0
  *out_params = ssl->s3->peer_quic_transport_params;
3330
0
  *out_params_len = ssl->s3->peer_quic_transport_params_len;
3331
0
}
3332
3333
static int
3334
ssl_cipher_id_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
3335
353k
{
3336
353k
  SSL_CIPHER const *a = a_;
3337
353k
  SSL_CIPHER const *b = b_;
3338
353k
  return ssl_cipher_id_cmp(a, b);
3339
353k
}
3340
3341
SSL_CIPHER *
3342
OBJ_bsearch_ssl_cipher_id(SSL_CIPHER *key, SSL_CIPHER const *base, int num)
3343
58.6k
{
3344
58.6k
  return (SSL_CIPHER *)OBJ_bsearch_(key, base, num, sizeof(SSL_CIPHER),
3345
58.6k
      ssl_cipher_id_cmp_BSEARCH_CMP_FN);
3346
58.6k
}