Coverage Report

Created: 2022-08-24 06:30

/src/libressl/ssl/ssl_ciph.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: ssl_ciph.c,v 1.129 2022/06/29 20:06:55 tb 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 <stdio.h>
144
145
#include <openssl/objects.h>
146
#include <openssl/opensslconf.h>
147
148
#ifndef OPENSSL_NO_ENGINE
149
#include <openssl/engine.h>
150
#endif
151
152
#include "ssl_locl.h"
153
154
5.35M
#define CIPHER_ADD  1
155
164k
#define CIPHER_KILL 2
156
1.62M
#define CIPHER_DEL  3
157
2.84M
#define CIPHER_ORD  4
158
72.9k
#define CIPHER_SPECIAL  5
159
160
typedef struct cipher_order_st {
161
  const SSL_CIPHER *cipher;
162
  int active;
163
  int dead;
164
  struct cipher_order_st *next, *prev;
165
} CIPHER_ORDER;
166
167
static const SSL_CIPHER cipher_aliases[] = {
168
169
  /* "ALL" doesn't include eNULL (must be specifically enabled) */
170
  {
171
    .name = SSL_TXT_ALL,
172
    .algorithm_enc = ~SSL_eNULL,
173
  },
174
175
  /* "COMPLEMENTOFALL" */
176
  {
177
    .name = SSL_TXT_CMPALL,
178
    .algorithm_enc = SSL_eNULL,
179
  },
180
181
  /*
182
   * "COMPLEMENTOFDEFAULT"
183
   * (does *not* include ciphersuites not found in ALL!)
184
   */
185
  {
186
    .name = SSL_TXT_CMPDEF,
187
    .algorithm_mkey = SSL_kDHE|SSL_kECDHE,
188
    .algorithm_auth = SSL_aNULL,
189
    .algorithm_enc = ~SSL_eNULL,
190
  },
191
192
  /*
193
   * key exchange aliases
194
   * (some of those using only a single bit here combine multiple key
195
   * exchange algs according to the RFCs, e.g. kEDH combines DHE_DSS
196
   * and DHE_RSA)
197
   */
198
  {
199
    .name = SSL_TXT_kRSA,
200
    .algorithm_mkey = SSL_kRSA,
201
  },
202
  {
203
    .name = SSL_TXT_kEDH,
204
    .algorithm_mkey = SSL_kDHE,
205
  },
206
  {
207
    .name = SSL_TXT_DH,
208
    .algorithm_mkey = SSL_kDHE,
209
  },
210
  {
211
    .name = SSL_TXT_kEECDH,
212
    .algorithm_mkey = SSL_kECDHE,
213
  },
214
  {
215
    .name = SSL_TXT_ECDH,
216
    .algorithm_mkey = SSL_kECDHE,
217
  },
218
  {
219
    .name = SSL_TXT_kGOST,
220
    .algorithm_mkey = SSL_kGOST,
221
  },
222
223
  /* server authentication aliases */
224
  {
225
    .name = SSL_TXT_aRSA,
226
    .algorithm_auth = SSL_aRSA,
227
  },
228
  {
229
    .name = SSL_TXT_aDSS,
230
    .algorithm_auth = SSL_aDSS,
231
  },
232
  {
233
    .name = SSL_TXT_DSS,
234
    .algorithm_auth = SSL_aDSS,
235
  },
236
  {
237
    .name = SSL_TXT_aNULL,
238
    .algorithm_auth = SSL_aNULL,
239
  },
240
  {
241
    .name = SSL_TXT_aECDSA,
242
    .algorithm_auth = SSL_aECDSA,
243
  },
244
  {
245
    .name = SSL_TXT_ECDSA,
246
    .algorithm_auth = SSL_aECDSA,
247
  },
248
  {
249
    .name = SSL_TXT_aGOST01,
250
    .algorithm_auth = SSL_aGOST01,
251
  },
252
  {
253
    .name = SSL_TXT_aGOST,
254
    .algorithm_auth = SSL_aGOST01,
255
  },
256
257
  /* aliases combining key exchange and server authentication */
258
  {
259
    .name = SSL_TXT_DHE,
260
    .algorithm_mkey = SSL_kDHE,
261
    .algorithm_auth = ~SSL_aNULL,
262
  },
263
  {
264
    .name = SSL_TXT_EDH,
265
    .algorithm_mkey = SSL_kDHE,
266
    .algorithm_auth = ~SSL_aNULL,
267
  },
268
  {
269
    .name = SSL_TXT_ECDHE,
270
    .algorithm_mkey = SSL_kECDHE,
271
    .algorithm_auth = ~SSL_aNULL,
272
  },
273
  {
274
    .name = SSL_TXT_EECDH,
275
    .algorithm_mkey = SSL_kECDHE,
276
    .algorithm_auth = ~SSL_aNULL,
277
  },
278
  {
279
    .name = SSL_TXT_NULL,
280
    .algorithm_enc = SSL_eNULL,
281
  },
282
  {
283
    .name = SSL_TXT_RSA,
284
    .algorithm_mkey = SSL_kRSA,
285
    .algorithm_auth = SSL_aRSA,
286
  },
287
  {
288
    .name = SSL_TXT_ADH,
289
    .algorithm_mkey = SSL_kDHE,
290
    .algorithm_auth = SSL_aNULL,
291
  },
292
  {
293
    .name = SSL_TXT_AECDH,
294
    .algorithm_mkey = SSL_kECDHE,
295
    .algorithm_auth = SSL_aNULL,
296
  },
297
298
  /* symmetric encryption aliases */
299
  {
300
    .name = SSL_TXT_3DES,
301
    .algorithm_enc = SSL_3DES,
302
  },
303
  {
304
    .name = SSL_TXT_RC4,
305
    .algorithm_enc = SSL_RC4,
306
  },
307
  {
308
    .name = SSL_TXT_eNULL,
309
    .algorithm_enc = SSL_eNULL,
310
  },
311
  {
312
    .name = SSL_TXT_AES128,
313
    .algorithm_enc = SSL_AES128|SSL_AES128GCM,
314
  },
315
  {
316
    .name = SSL_TXT_AES256,
317
    .algorithm_enc = SSL_AES256|SSL_AES256GCM,
318
  },
319
  {
320
    .name = SSL_TXT_AES,
321
    .algorithm_enc = SSL_AES,
322
  },
323
  {
324
    .name = SSL_TXT_AES_GCM,
325
    .algorithm_enc = SSL_AES128GCM|SSL_AES256GCM,
326
  },
327
  {
328
    .name = SSL_TXT_CAMELLIA128,
329
    .algorithm_enc = SSL_CAMELLIA128,
330
  },
331
  {
332
    .name = SSL_TXT_CAMELLIA256,
333
    .algorithm_enc = SSL_CAMELLIA256,
334
  },
335
  {
336
    .name = SSL_TXT_CAMELLIA,
337
    .algorithm_enc = SSL_CAMELLIA128|SSL_CAMELLIA256,
338
  },
339
  {
340
    .name = SSL_TXT_CHACHA20,
341
    .algorithm_enc = SSL_CHACHA20POLY1305,
342
  },
343
344
  /* MAC aliases */
345
  {
346
    .name = SSL_TXT_AEAD,
347
    .algorithm_mac = SSL_AEAD,
348
  },
349
  {
350
    .name = SSL_TXT_MD5,
351
    .algorithm_mac = SSL_MD5,
352
  },
353
  {
354
    .name = SSL_TXT_SHA1,
355
    .algorithm_mac = SSL_SHA1,
356
  },
357
  {
358
    .name = SSL_TXT_SHA,
359
    .algorithm_mac = SSL_SHA1,
360
  },
361
  {
362
    .name = SSL_TXT_GOST94,
363
    .algorithm_mac = SSL_GOST94,
364
  },
365
  {
366
    .name = SSL_TXT_GOST89MAC,
367
    .algorithm_mac = SSL_GOST89MAC,
368
  },
369
  {
370
    .name = SSL_TXT_SHA256,
371
    .algorithm_mac = SSL_SHA256,
372
  },
373
  {
374
    .name = SSL_TXT_SHA384,
375
    .algorithm_mac = SSL_SHA384,
376
  },
377
  {
378
    .name = SSL_TXT_STREEBOG256,
379
    .algorithm_mac = SSL_STREEBOG256,
380
  },
381
382
  /* protocol version aliases */
383
  {
384
    .name = SSL_TXT_SSLV3,
385
    .algorithm_ssl = SSL_SSLV3,
386
  },
387
  {
388
    .name = SSL_TXT_TLSV1,
389
    .algorithm_ssl = SSL_TLSV1,
390
  },
391
  {
392
    .name = SSL_TXT_TLSV1_2,
393
    .algorithm_ssl = SSL_TLSV1_2,
394
  },
395
  {
396
    .name = SSL_TXT_TLSV1_3,
397
    .algorithm_ssl = SSL_TLSV1_3,
398
  },
399
400
  /* cipher suite aliases */
401
#ifdef LIBRESSL_HAS_TLS1_3
402
  {
403
    .valid = 1,
404
    .name = "TLS_AES_128_GCM_SHA256",
405
    .id = TLS1_3_CK_AES_128_GCM_SHA256,
406
    .algorithm_ssl = SSL_TLSV1_3,
407
  },
408
  {
409
    .valid = 1,
410
    .name = "TLS_AES_256_GCM_SHA384",
411
    .id = TLS1_3_CK_AES_256_GCM_SHA384,
412
    .algorithm_ssl = SSL_TLSV1_3,
413
  },
414
  {
415
    .valid = 1,
416
    .name = "TLS_CHACHA20_POLY1305_SHA256",
417
    .id = TLS1_3_CK_CHACHA20_POLY1305_SHA256,
418
    .algorithm_ssl = SSL_TLSV1_3,
419
  },
420
#endif
421
422
  /* strength classes */
423
  {
424
    .name = SSL_TXT_LOW,
425
    .algo_strength = SSL_LOW,
426
  },
427
  {
428
    .name = SSL_TXT_MEDIUM,
429
    .algo_strength = SSL_MEDIUM,
430
  },
431
  {
432
    .name = SSL_TXT_HIGH,
433
    .algo_strength = SSL_HIGH,
434
  },
435
};
436
437
int
438
ssl_cipher_get_evp(const SSL_SESSION *ss, const EVP_CIPHER **enc,
439
    const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
440
148
{
441
148
  *enc = NULL;
442
148
  *md = NULL;
443
148
  *mac_pkey_type = NID_undef;
444
148
  *mac_secret_size = 0;
445
446
148
  if (ss->cipher == NULL)
447
0
    return 0;
448
449
  /*
450
   * This function does not handle EVP_AEAD.
451
   * See ssl_cipher_get_evp_aead instead.
452
   */
453
148
  if (ss->cipher->algorithm_mac & SSL_AEAD)
454
0
    return 0;
455
456
148
  switch (ss->cipher->algorithm_enc) {
457
64
  case SSL_3DES:
458
64
    *enc = EVP_des_ede3_cbc();
459
64
    break;
460
13
  case SSL_RC4:
461
13
    *enc = EVP_rc4();
462
13
    break;
463
0
  case SSL_eNULL:
464
0
    *enc = EVP_enc_null();
465
0
    break;
466
24
  case SSL_AES128:
467
24
    *enc = EVP_aes_128_cbc();
468
24
    break;
469
36
  case SSL_AES256:
470
36
    *enc = EVP_aes_256_cbc();
471
36
    break;
472
5
  case SSL_CAMELLIA128:
473
5
    *enc = EVP_camellia_128_cbc();
474
5
    break;
475
6
  case SSL_CAMELLIA256:
476
6
    *enc = EVP_camellia_256_cbc();
477
6
    break;
478
0
  case SSL_eGOST2814789CNT:
479
0
    *enc = EVP_gost2814789_cnt();
480
0
    break;
481
148
  }
482
483
148
  switch (ss->cipher->algorithm_mac) {
484
0
  case SSL_MD5:
485
0
    *md = EVP_md5();
486
0
    break;
487
101
  case SSL_SHA1:
488
101
    *md = EVP_sha1();
489
101
    break;
490
29
  case SSL_SHA256:
491
29
    *md = EVP_sha256();
492
29
    break;
493
18
  case SSL_SHA384:
494
18
    *md = EVP_sha384();
495
18
    break;
496
0
  case SSL_GOST89MAC:
497
0
    *md = EVP_gost2814789imit();
498
0
    break;
499
0
  case SSL_GOST94:
500
0
    *md = EVP_gostr341194();
501
0
    break;
502
0
  case SSL_STREEBOG256:
503
0
    *md = EVP_streebog256();
504
0
    break;
505
148
  }
506
507
148
  if (*enc == NULL || *md == NULL)
508
0
    return 0;
509
510
  /*
511
   * EVP_CIPH_FLAG_AEAD_CIPHER and EVP_CIPH_GCM_MODE ciphers are not
512
   * supported via EVP_CIPHER (they should be using EVP_AEAD instead).
513
   */
514
148
  if (EVP_CIPHER_flags(*enc) & EVP_CIPH_FLAG_AEAD_CIPHER)
515
0
    return 0;
516
148
  if (EVP_CIPHER_mode(*enc) == EVP_CIPH_GCM_MODE)
517
0
    return 0;
518
519
148
  if (ss->cipher->algorithm_mac == SSL_GOST89MAC) {
520
0
    *mac_pkey_type = EVP_PKEY_GOSTIMIT;
521
0
    *mac_secret_size = 32; /* XXX */
522
148
  } else {
523
148
    *mac_pkey_type = EVP_PKEY_HMAC;
524
148
    *mac_secret_size = EVP_MD_size(*md);
525
148
  }
526
527
148
  return 1;
528
148
}
529
530
/*
531
 * ssl_cipher_get_evp_aead sets aead to point to the correct EVP_AEAD object
532
 * for s->cipher. It returns 1 on success and 0 on error.
533
 */
534
int
535
ssl_cipher_get_evp_aead(const SSL_SESSION *ss, const EVP_AEAD **aead)
536
91
{
537
91
  *aead = NULL;
538
539
91
  if (ss->cipher == NULL)
540
0
    return 0;
541
91
  if ((ss->cipher->algorithm_mac & SSL_AEAD) == 0)
542
0
    return 0;
543
544
91
  switch (ss->cipher->algorithm_enc) {
545
15
  case SSL_AES128GCM:
546
15
    *aead = EVP_aead_aes_128_gcm();
547
15
    return 1;
548
44
  case SSL_AES256GCM:
549
44
    *aead = EVP_aead_aes_256_gcm();
550
44
    return 1;
551
32
  case SSL_CHACHA20POLY1305:
552
32
    *aead = EVP_aead_chacha20_poly1305();
553
32
    return 1;
554
0
  default:
555
0
    break;
556
91
  }
557
0
  return 0;
558
91
}
559
560
int
561
ssl_get_handshake_evp_md(SSL *s, const EVP_MD **md)
562
6.91k
{
563
6.91k
  unsigned long handshake_mac;
564
565
6.91k
  *md = NULL;
566
567
6.91k
  if (s->s3->hs.cipher == NULL)
568
0
    return 0;
569
570
6.91k
  handshake_mac = s->s3->hs.cipher->algorithm2 &
571
6.91k
      SSL_HANDSHAKE_MAC_MASK;
572
573
  /* For TLSv1.2 we upgrade the default MD5+SHA1 MAC to SHA256. */
574
6.91k
  if (SSL_USE_SHA256_PRF(s) && handshake_mac == SSL_HANDSHAKE_MAC_DEFAULT)
575
1.87k
    handshake_mac = SSL_HANDSHAKE_MAC_SHA256;
576
577
6.91k
  switch (handshake_mac) {
578
652
  case SSL_HANDSHAKE_MAC_DEFAULT:
579
652
    *md = EVP_md5_sha1();
580
652
    return 1;
581
0
  case SSL_HANDSHAKE_MAC_GOST94:
582
0
    *md = EVP_gostr341194();
583
0
    return 1;
584
5.27k
  case SSL_HANDSHAKE_MAC_SHA256:
585
5.27k
    *md = EVP_sha256();
586
5.27k
    return 1;
587
989
  case SSL_HANDSHAKE_MAC_SHA384:
588
989
    *md = EVP_sha384();
589
989
    return 1;
590
0
  case SSL_HANDSHAKE_MAC_STREEBOG256:
591
0
    *md = EVP_streebog256();
592
0
    return 1;
593
0
  default:
594
0
    break;
595
6.91k
  }
596
597
0
  return 0;
598
6.91k
}
599
600
#define ITEM_SEP(a) \
601
60.7k
  (((a) == ':') || ((a) == ' ') || ((a) == ';') || ((a) == ','))
602
603
static void
604
ll_append_tail(CIPHER_ORDER **head, CIPHER_ORDER *curr,
605
    CIPHER_ORDER **tail)
606
3.42M
{
607
3.42M
  if (curr == *tail)
608
0
    return;
609
3.42M
  if (curr == *head)
610
1.50M
    *head = curr->next;
611
3.42M
  if (curr->prev != NULL)
612
1.91M
    curr->prev->next = curr->next;
613
3.42M
  if (curr->next != NULL)
614
3.42M
    curr->next->prev = curr->prev;
615
3.42M
  (*tail)->next = curr;
616
3.42M
  curr->prev= *tail;
617
3.42M
  curr->next = NULL;
618
3.42M
  *tail = curr;
619
3.42M
}
620
621
static void
622
ll_append_head(CIPHER_ORDER **head, CIPHER_ORDER *curr,
623
    CIPHER_ORDER **tail)
624
1.21M
{
625
1.21M
  if (curr == *head)
626
0
    return;
627
1.21M
  if (curr == *tail)
628
1.21M
    *tail = curr->prev;
629
1.21M
  if (curr->next != NULL)
630
0
    curr->next->prev = curr->prev;
631
1.21M
  if (curr->prev != NULL)
632
1.21M
    curr->prev->next = curr->next;
633
1.21M
  (*head)->prev = curr;
634
1.21M
  curr->next= *head;
635
1.21M
  curr->prev = NULL;
636
1.21M
  *head = curr;
637
1.21M
}
638
639
static void
640
ssl_cipher_get_disabled(unsigned long *mkey, unsigned long *auth,
641
    unsigned long *enc, unsigned long *mac, unsigned long *ssl)
642
12.1k
{
643
12.1k
  *mkey = 0;
644
12.1k
  *auth = 0;
645
12.1k
  *enc = 0;
646
12.1k
  *mac = 0;
647
12.1k
  *ssl = 0;
648
649
  /*
650
   * Check for the availability of GOST 34.10 public/private key
651
   * algorithms. If they are not available disable the associated
652
   * authentication and key exchange algorithms.
653
   */
654
12.1k
  if (EVP_PKEY_meth_find(NID_id_GostR3410_2001) == NULL) {
655
0
    *auth |= SSL_aGOST01;
656
0
    *mkey |= SSL_kGOST;
657
0
  }
658
659
#ifdef SSL_FORBID_ENULL
660
  *enc |= SSL_eNULL;
661
#endif
662
12.1k
}
663
664
static void
665
ssl_cipher_collect_ciphers(const SSL_METHOD *ssl_method, int num_of_ciphers,
666
    unsigned long disabled_mkey, unsigned long disabled_auth,
667
    unsigned long disabled_enc, unsigned long disabled_mac,
668
    unsigned long disabled_ssl, CIPHER_ORDER *co_list,
669
    CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
670
12.1k
{
671
12.1k
  int i, co_list_num;
672
12.1k
  const SSL_CIPHER *c;
673
674
  /*
675
   * We have num_of_ciphers descriptions compiled in, depending on the
676
   * method selected (SSLv3, TLSv1, etc). These will later be sorted in
677
   * a linked list with at most num entries.
678
   */
679
680
  /* Get the initial list of ciphers */
681
12.1k
  co_list_num = 0;  /* actual count of ciphers */
682
886k
  for (i = 0; i < num_of_ciphers; i++) {
683
874k
    c = ssl_method->get_cipher(i);
684
    /*
685
     * Drop any invalid ciphers and any which use unavailable
686
     * algorithms.
687
     */
688
874k
    if ((c != NULL) && c->valid &&
689
874k
        !(c->algorithm_mkey & disabled_mkey) &&
690
874k
        !(c->algorithm_auth & disabled_auth) &&
691
874k
        !(c->algorithm_enc & disabled_enc) &&
692
874k
        !(c->algorithm_mac & disabled_mac) &&
693
874k
        !(c->algorithm_ssl & disabled_ssl)) {
694
874k
      co_list[co_list_num].cipher = c;
695
874k
      co_list[co_list_num].next = NULL;
696
874k
      co_list[co_list_num].prev = NULL;
697
874k
      co_list[co_list_num].active = 0;
698
874k
      co_list_num++;
699
      /*
700
      if (!sk_push(ca_list,(char *)c)) goto err;
701
      */
702
874k
    }
703
874k
  }
704
705
  /*
706
   * Prepare linked list from list entries
707
   */
708
12.1k
  if (co_list_num > 0) {
709
12.1k
    co_list[0].prev = NULL;
710
711
12.1k
    if (co_list_num > 1) {
712
12.1k
      co_list[0].next = &co_list[1];
713
714
862k
      for (i = 1; i < co_list_num - 1; i++) {
715
850k
        co_list[i].prev = &co_list[i - 1];
716
850k
        co_list[i].next = &co_list[i + 1];
717
850k
      }
718
719
12.1k
      co_list[co_list_num - 1].prev =
720
12.1k
          &co_list[co_list_num - 2];
721
12.1k
    }
722
723
12.1k
    co_list[co_list_num - 1].next = NULL;
724
725
12.1k
    *head_p = &co_list[0];
726
12.1k
    *tail_p = &co_list[co_list_num - 1];
727
12.1k
  }
728
12.1k
}
729
730
static void
731
ssl_cipher_collect_aliases(const SSL_CIPHER **ca_list, int num_of_group_aliases,
732
    unsigned long disabled_mkey, unsigned long disabled_auth,
733
    unsigned long disabled_enc, unsigned long disabled_mac,
734
    unsigned long disabled_ssl, CIPHER_ORDER *head)
735
12.1k
{
736
12.1k
  CIPHER_ORDER *ciph_curr;
737
12.1k
  const SSL_CIPHER **ca_curr;
738
12.1k
  int i;
739
12.1k
  unsigned long mask_mkey = ~disabled_mkey;
740
12.1k
  unsigned long mask_auth = ~disabled_auth;
741
12.1k
  unsigned long mask_enc = ~disabled_enc;
742
12.1k
  unsigned long mask_mac = ~disabled_mac;
743
12.1k
  unsigned long mask_ssl = ~disabled_ssl;
744
745
  /*
746
   * First, add the real ciphers as already collected
747
   */
748
12.1k
  ciph_curr = head;
749
12.1k
  ca_curr = ca_list;
750
886k
  while (ciph_curr != NULL) {
751
874k
    *ca_curr = ciph_curr->cipher;
752
874k
    ca_curr++;
753
874k
    ciph_curr = ciph_curr->next;
754
874k
  }
755
756
  /*
757
   * Now we add the available ones from the cipher_aliases[] table.
758
   * They represent either one or more algorithms, some of which
759
   * in any affected category must be supported (set in enabled_mask),
760
   * or represent a cipher strength value (will be added in any case because algorithms=0).
761
   */
762
680k
  for (i = 0; i < num_of_group_aliases; i++) {
763
668k
    unsigned long algorithm_mkey = cipher_aliases[i].algorithm_mkey;
764
668k
    unsigned long algorithm_auth = cipher_aliases[i].algorithm_auth;
765
668k
    unsigned long algorithm_enc = cipher_aliases[i].algorithm_enc;
766
668k
    unsigned long algorithm_mac = cipher_aliases[i].algorithm_mac;
767
668k
    unsigned long algorithm_ssl = cipher_aliases[i].algorithm_ssl;
768
769
668k
    if (algorithm_mkey)
770
170k
      if ((algorithm_mkey & mask_mkey) == 0)
771
0
        continue;
772
773
668k
    if (algorithm_auth)
774
194k
      if ((algorithm_auth & mask_auth) == 0)
775
0
        continue;
776
777
668k
    if (algorithm_enc)
778
182k
      if ((algorithm_enc & mask_enc) == 0)
779
0
        continue;
780
781
668k
    if (algorithm_mac)
782
109k
      if ((algorithm_mac & mask_mac) == 0)
783
0
        continue;
784
785
668k
    if (algorithm_ssl)
786
85.0k
      if ((algorithm_ssl & mask_ssl) == 0)
787
0
        continue;
788
789
668k
    *ca_curr = (SSL_CIPHER *)(cipher_aliases + i);
790
668k
    ca_curr++;
791
668k
  }
792
793
12.1k
  *ca_curr = NULL;  /* end of list */
794
12.1k
}
795
796
static void
797
ssl_cipher_apply_rule(unsigned long cipher_id, unsigned long alg_mkey,
798
    unsigned long alg_auth, unsigned long alg_enc, unsigned long alg_mac,
799
    unsigned long alg_ssl, unsigned long algo_strength, int rule,
800
    int strength_bits, CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
801
224k
{
802
224k
  CIPHER_ORDER *head, *tail, *curr, *next, *last;
803
224k
  const SSL_CIPHER *cp;
804
224k
  int reverse = 0;
805
806
224k
  if (rule == CIPHER_DEL)
807
36.4k
    reverse = 1; /* needed to maintain sorting between currently deleted ciphers */
808
809
224k
  head = *head_p;
810
224k
  tail = *tail_p;
811
812
224k
  if (reverse) {
813
36.4k
    next = tail;
814
36.4k
    last = head;
815
188k
  } else {
816
188k
    next = head;
817
188k
    last = tail;
818
188k
  }
819
820
224k
  curr = NULL;
821
16.3M
  for (;;) {
822
16.3M
    if (curr == last)
823
224k
      break;
824
16.0M
    curr = next;
825
16.0M
    next = reverse ? curr->prev : curr->next;
826
827
16.0M
    cp = curr->cipher;
828
829
16.0M
    if (cipher_id && cp->id != cipher_id)
830
0
      continue;
831
832
    /*
833
     * Selection criteria is either the value of strength_bits
834
     * or the algorithms used.
835
     */
836
16.0M
    if (strength_bits >= 0) {
837
3.49M
      if (strength_bits != cp->strength_bits)
838
2.62M
        continue;
839
12.5M
    } else {
840
12.5M
      if (alg_mkey && !(alg_mkey & cp->algorithm_mkey))
841
1.82M
        continue;
842
10.7M
      if (alg_auth && !(alg_auth & cp->algorithm_auth))
843
1.00M
        continue;
844
9.75M
      if (alg_enc && !(alg_enc & cp->algorithm_enc))
845
2.86M
        continue;
846
6.88M
      if (alg_mac && !(alg_mac & cp->algorithm_mac))
847
838k
        continue;
848
6.05M
      if (alg_ssl && !(alg_ssl & cp->algorithm_ssl))
849
1.67M
        continue;
850
4.37M
      if ((algo_strength & SSL_STRONG_MASK) && !(algo_strength & SSL_STRONG_MASK & cp->algo_strength))
851
0
        continue;
852
4.37M
    }
853
854
    /* add the cipher if it has not been added yet. */
855
5.24M
    if (rule == CIPHER_ADD) {
856
      /* reverse == 0 */
857
2.50M
      if (!curr->active) {
858
2.04M
        ll_append_tail(&head, curr, &tail);
859
2.04M
        curr->active = 1;
860
2.04M
      }
861
2.50M
    }
862
    /* Move the added cipher to this location */
863
2.74M
    else if (rule == CIPHER_ORD) {
864
      /* reverse == 0 */
865
1.38M
      if (curr->active) {
866
1.38M
        ll_append_tail(&head, curr, &tail);
867
1.38M
      }
868
1.38M
    } else if (rule == CIPHER_DEL) {
869
      /* reverse == 1 */
870
1.21M
      if (curr->active) {
871
        /* most recently deleted ciphersuites get best positions
872
         * for any future CIPHER_ADD (note that the CIPHER_DEL loop
873
         * works in reverse to maintain the order) */
874
1.21M
        ll_append_head(&head, curr, &tail);
875
1.21M
        curr->active = 0;
876
1.21M
      }
877
1.21M
    } else if (rule == CIPHER_KILL) {
878
      /* reverse == 0 */
879
145k
      if (head == curr)
880
42.5k
        head = curr->next;
881
103k
      else
882
103k
        curr->prev->next = curr->next;
883
145k
      if (tail == curr)
884
0
        tail = curr->prev;
885
145k
      curr->active = 0;
886
145k
      if (curr->next != NULL)
887
145k
        curr->next->prev = curr->prev;
888
145k
      if (curr->prev != NULL)
889
103k
        curr->prev->next = curr->next;
890
145k
      curr->next = NULL;
891
145k
      curr->prev = NULL;
892
145k
    }
893
5.24M
  }
894
895
224k
  *head_p = head;
896
224k
  *tail_p = tail;
897
224k
}
898
899
static int
900
ssl_cipher_strength_sort(CIPHER_ORDER **head_p, CIPHER_ORDER **tail_p)
901
12.1k
{
902
12.1k
  int max_strength_bits, i, *number_uses;
903
12.1k
  CIPHER_ORDER *curr;
904
905
  /*
906
   * This routine sorts the ciphers with descending strength. The sorting
907
   * must keep the pre-sorted sequence, so we apply the normal sorting
908
   * routine as '+' movement to the end of the list.
909
   */
910
12.1k
  max_strength_bits = 0;
911
12.1k
  curr = *head_p;
912
886k
  while (curr != NULL) {
913
874k
    if (curr->active &&
914
874k
        (curr->cipher->strength_bits > max_strength_bits))
915
12.1k
      max_strength_bits = curr->cipher->strength_bits;
916
874k
    curr = curr->next;
917
874k
  }
918
919
12.1k
  number_uses = calloc((max_strength_bits + 1), sizeof(int));
920
12.1k
  if (!number_uses) {
921
0
    SSLerrorx(ERR_R_MALLOC_FAILURE);
922
0
    return (0);
923
0
  }
924
925
  /*
926
   * Now find the strength_bits values actually used
927
   */
928
12.1k
  curr = *head_p;
929
886k
  while (curr != NULL) {
930
874k
    if (curr->active)
931
874k
      number_uses[curr->cipher->strength_bits]++;
932
874k
    curr = curr->next;
933
874k
  }
934
  /*
935
   * Go through the list of used strength_bits values in descending
936
   * order.
937
   */
938
3.13M
  for (i = max_strength_bits; i >= 0; i--)
939
3.12M
    if (number_uses[i] > 0)
940
48.6k
      ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ORD, i, head_p, tail_p);
941
942
12.1k
  free(number_uses);
943
12.1k
  return (1);
944
12.1k
}
945
946
static int
947
ssl_cipher_process_rulestr(const char *rule_str, CIPHER_ORDER **head_p,
948
    CIPHER_ORDER **tail_p, const SSL_CIPHER **ca_list, SSL_CERT *cert,
949
    int *tls13_seen)
950
12.1k
{
951
12.1k
  unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl;
952
12.1k
  unsigned long algo_strength;
953
12.1k
  int j, multi, found, rule, retval, ok, buflen;
954
12.1k
  unsigned long cipher_id = 0;
955
12.1k
  const char *l, *buf;
956
12.1k
  char ch;
957
958
12.1k
  *tls13_seen = 0;
959
960
12.1k
  retval = 1;
961
12.1k
  l = rule_str;
962
60.7k
  for (;;) {
963
60.7k
    ch = *l;
964
965
60.7k
    if (ch == '\0')
966
0
      break;
967
968
60.7k
    if (ch == '-') {
969
0
      rule = CIPHER_DEL;
970
0
      l++;
971
60.7k
    } else if (ch == '+') {
972
0
      rule = CIPHER_ORD;
973
0
      l++;
974
60.7k
    } else if (ch == '!') {
975
18.2k
      rule = CIPHER_KILL;
976
18.2k
      l++;
977
42.5k
    } else if (ch == '@') {
978
0
      rule = CIPHER_SPECIAL;
979
0
      l++;
980
42.5k
    } else {
981
42.5k
      rule = CIPHER_ADD;
982
42.5k
    }
983
984
60.7k
    if (ITEM_SEP(ch)) {
985
24.3k
      l++;
986
24.3k
      continue;
987
24.3k
    }
988
989
36.4k
    alg_mkey = 0;
990
36.4k
    alg_auth = 0;
991
36.4k
    alg_enc = 0;
992
36.4k
    alg_mac = 0;
993
36.4k
    alg_ssl = 0;
994
36.4k
    algo_strength = 0;
995
996
36.4k
    for (;;) {
997
36.4k
      ch = *l;
998
36.4k
      buf = l;
999
36.4k
      buflen = 0;
1000
194k
      while (((ch >= 'A') && (ch <= 'Z')) ||
1001
194k
          ((ch >= '0') && (ch <= '9')) ||
1002
194k
          ((ch >= 'a') && (ch <= 'z')) ||
1003
194k
          (ch == '-') || (ch == '.') ||
1004
194k
          (ch == '_') || (ch == '=')) {
1005
157k
        ch = *(++l);
1006
157k
        buflen++;
1007
157k
      }
1008
1009
36.4k
      if (buflen == 0) {
1010
        /*
1011
         * We hit something we cannot deal with,
1012
         * it is no command or separator nor
1013
         * alphanumeric, so we call this an error.
1014
         */
1015
0
        SSLerrorx(SSL_R_INVALID_COMMAND);
1016
0
        retval = found = 0;
1017
0
        l++;
1018
0
        break;
1019
0
      }
1020
1021
36.4k
      if (rule == CIPHER_SPECIAL) {
1022
         /* unused -- avoid compiler warning */
1023
0
        found = 0;
1024
        /* special treatment */
1025
0
        break;
1026
0
      }
1027
1028
      /* check for multi-part specification */
1029
36.4k
      if (ch == '+') {
1030
0
        multi = 1;
1031
0
        l++;
1032
0
      } else
1033
36.4k
        multi = 0;
1034
1035
      /*
1036
       * Now search for the cipher alias in the ca_list.
1037
       * Be careful with the strncmp, because the "buflen"
1038
       * limitation will make the rule "ADH:SOME" and the
1039
       * cipher "ADH-MY-CIPHER" look like a match for
1040
       * buflen=3. So additionally check whether the cipher
1041
       * name found has the correct length. We can save a
1042
       * strlen() call: just checking for the '\0' at the
1043
       * right place is sufficient, we have to strncmp()
1044
       * anyway (we cannot use strcmp(), because buf is not
1045
       * '\0' terminated.)
1046
       */
1047
36.4k
      j = found = 0;
1048
36.4k
      cipher_id = 0;
1049
3.39M
      while (ca_list[j]) {
1050
3.38M
        if (!strncmp(buf, ca_list[j]->name, buflen) &&
1051
3.38M
            (ca_list[j]->name[buflen] == '\0')) {
1052
30.3k
          found = 1;
1053
30.3k
          break;
1054
30.3k
        } else
1055
3.35M
          j++;
1056
3.38M
      }
1057
1058
36.4k
      if (!found)
1059
6.07k
        break;  /* ignore this entry */
1060
1061
30.3k
      if (ca_list[j]->algorithm_mkey) {
1062
0
        if (alg_mkey) {
1063
0
          alg_mkey &= ca_list[j]->algorithm_mkey;
1064
0
          if (!alg_mkey) {
1065
0
            found = 0;
1066
0
            break;
1067
0
          }
1068
0
        } else
1069
0
          alg_mkey = ca_list[j]->algorithm_mkey;
1070
0
      }
1071
1072
30.3k
      if (ca_list[j]->algorithm_auth) {
1073
6.07k
        if (alg_auth) {
1074
0
          alg_auth &= ca_list[j]->algorithm_auth;
1075
0
          if (!alg_auth) {
1076
0
            found = 0;
1077
0
            break;
1078
0
          }
1079
0
        } else
1080
6.07k
          alg_auth = ca_list[j]->algorithm_auth;
1081
6.07k
      }
1082
1083
30.3k
      if (ca_list[j]->algorithm_enc) {
1084
24.3k
        if (alg_enc) {
1085
0
          alg_enc &= ca_list[j]->algorithm_enc;
1086
0
          if (!alg_enc) {
1087
0
            found = 0;
1088
0
            break;
1089
0
          }
1090
0
        } else
1091
24.3k
          alg_enc = ca_list[j]->algorithm_enc;
1092
24.3k
      }
1093
1094
30.3k
      if (ca_list[j]->algorithm_mac) {
1095
0
        if (alg_mac) {
1096
0
          alg_mac &= ca_list[j]->algorithm_mac;
1097
0
          if (!alg_mac) {
1098
0
            found = 0;
1099
0
            break;
1100
0
          }
1101
0
        } else
1102
0
          alg_mac = ca_list[j]->algorithm_mac;
1103
0
      }
1104
1105
30.3k
      if (ca_list[j]->algo_strength & SSL_STRONG_MASK) {
1106
0
        if (algo_strength & SSL_STRONG_MASK) {
1107
0
          algo_strength &=
1108
0
              (ca_list[j]->algo_strength &
1109
0
              SSL_STRONG_MASK) | ~SSL_STRONG_MASK;
1110
0
          if (!(algo_strength &
1111
0
              SSL_STRONG_MASK)) {
1112
0
            found = 0;
1113
0
            break;
1114
0
          }
1115
0
        } else
1116
0
          algo_strength |=
1117
0
              ca_list[j]->algo_strength &
1118
0
              SSL_STRONG_MASK;
1119
0
      }
1120
1121
30.3k
      if (ca_list[j]->valid) {
1122
        /*
1123
         * explicit ciphersuite found; its protocol
1124
         * version does not become part of the search
1125
         * pattern!
1126
         */
1127
0
        cipher_id = ca_list[j]->id;
1128
0
        if (ca_list[j]->algorithm_ssl == SSL_TLSV1_3)
1129
0
          *tls13_seen = 1;
1130
30.3k
      } else {
1131
        /*
1132
         * not an explicit ciphersuite; only in this
1133
         * case, the protocol version is considered
1134
         * part of the search pattern
1135
         */
1136
30.3k
        if (ca_list[j]->algorithm_ssl) {
1137
0
          if (alg_ssl) {
1138
0
            alg_ssl &=
1139
0
                ca_list[j]->algorithm_ssl;
1140
0
            if (!alg_ssl) {
1141
0
              found = 0;
1142
0
              break;
1143
0
            }
1144
0
          } else
1145
0
            alg_ssl =
1146
0
                ca_list[j]->algorithm_ssl;
1147
0
        }
1148
30.3k
      }
1149
1150
30.3k
      if (!multi)
1151
30.3k
        break;
1152
30.3k
    }
1153
1154
    /*
1155
     * Ok, we have the rule, now apply it
1156
     */
1157
36.4k
    if (rule == CIPHER_SPECIAL) {
1158
      /* special command */
1159
0
      ok = 0;
1160
0
      if (buflen == 8 && strncmp(buf, "STRENGTH", 8) == 0) {
1161
0
        ok = ssl_cipher_strength_sort(head_p, tail_p);
1162
0
      } else if (buflen == 10 &&
1163
0
          strncmp(buf, "SECLEVEL=", 9) == 0) {
1164
0
        int level = buf[9] - '0';
1165
1166
0
        if (level >= 0 && level <= 5) {
1167
0
          cert->security_level = level;
1168
0
          ok = 1;
1169
0
        } else {
1170
0
          SSLerrorx(SSL_R_INVALID_COMMAND);
1171
0
        }
1172
0
      } else {
1173
0
        SSLerrorx(SSL_R_INVALID_COMMAND);
1174
0
      }
1175
0
      if (ok == 0)
1176
0
        retval = 0;
1177
1178
0
      while ((*l != '\0') && !ITEM_SEP(*l))
1179
0
        l++;
1180
36.4k
    } else if (found) {
1181
30.3k
      if (alg_ssl == SSL_TLSV1_3)
1182
0
        *tls13_seen = 1;
1183
30.3k
      ssl_cipher_apply_rule(cipher_id, alg_mkey, alg_auth,
1184
30.3k
          alg_enc, alg_mac, alg_ssl, algo_strength, rule,
1185
30.3k
          -1, head_p, tail_p);
1186
30.3k
    } else {
1187
6.07k
      while ((*l != '\0') && !ITEM_SEP(*l))
1188
0
        l++;
1189
6.07k
    }
1190
36.4k
    if (*l == '\0')
1191
12.1k
      break; /* done */
1192
36.4k
  }
1193
1194
12.1k
  return (retval);
1195
12.1k
}
1196
1197
static inline int
1198
ssl_aes_is_accelerated(void)
1199
12.1k
{
1200
12.1k
#if defined(__i386__) || defined(__x86_64__)
1201
12.1k
  return ((OPENSSL_cpu_caps() & (1ULL << 57)) != 0);
1202
#else
1203
  return (0);
1204
#endif
1205
12.1k
}
1206
1207
STACK_OF(SSL_CIPHER) *
1208
ssl_create_cipher_list(const SSL_METHOD *ssl_method,
1209
    STACK_OF(SSL_CIPHER) **cipher_list,
1210
    STACK_OF(SSL_CIPHER) *cipher_list_tls13,
1211
    const char *rule_str, SSL_CERT *cert)
1212
12.1k
{
1213
12.1k
  int ok, num_of_ciphers, num_of_alias_max, num_of_group_aliases;
1214
12.1k
  unsigned long disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl;
1215
12.1k
  STACK_OF(SSL_CIPHER) *cipherstack;
1216
12.1k
  const char *rule_p;
1217
12.1k
  CIPHER_ORDER *co_list = NULL, *head = NULL, *tail = NULL, *curr;
1218
12.1k
  const SSL_CIPHER **ca_list = NULL;
1219
12.1k
  const SSL_CIPHER *cipher;
1220
12.1k
  int tls13_seen = 0;
1221
12.1k
  int any_active;
1222
12.1k
  int i;
1223
1224
  /*
1225
   * Return with error if nothing to do.
1226
   */
1227
12.1k
  if (rule_str == NULL || cipher_list == NULL)
1228
0
    return NULL;
1229
1230
  /*
1231
   * To reduce the work to do we only want to process the compiled
1232
   * in algorithms, so we first get the mask of disabled ciphers.
1233
   */
1234
12.1k
  ssl_cipher_get_disabled(&disabled_mkey, &disabled_auth, &disabled_enc, &disabled_mac, &disabled_ssl);
1235
1236
  /*
1237
   * Now we have to collect the available ciphers from the compiled
1238
   * in ciphers. We cannot get more than the number compiled in, so
1239
   * it is used for allocation.
1240
   */
1241
12.1k
  num_of_ciphers = ssl3_num_ciphers();
1242
12.1k
  co_list = reallocarray(NULL, num_of_ciphers, sizeof(CIPHER_ORDER));
1243
12.1k
  if (co_list == NULL) {
1244
0
    SSLerrorx(ERR_R_MALLOC_FAILURE);
1245
0
    return(NULL); /* Failure */
1246
0
  }
1247
1248
12.1k
  ssl_cipher_collect_ciphers(ssl_method, num_of_ciphers,
1249
12.1k
      disabled_mkey, disabled_auth, disabled_enc, disabled_mac, disabled_ssl,
1250
12.1k
      co_list, &head, &tail);
1251
1252
1253
  /* Now arrange all ciphers by preference: */
1254
1255
  /* Everything else being equal, prefer ephemeral ECDH over other key exchange mechanisms */
1256
12.1k
  ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1257
12.1k
  ssl_cipher_apply_rule(0, SSL_kECDHE, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
1258
1259
12.1k
  if (ssl_aes_is_accelerated()) {
1260
    /*
1261
     * We have hardware assisted AES - prefer AES as a symmetric
1262
     * cipher, with CHACHA20 second.
1263
     */
1264
12.1k
    ssl_cipher_apply_rule(0, 0, 0, SSL_AES, 0, 0, 0,
1265
12.1k
        CIPHER_ADD, -1, &head, &tail);
1266
12.1k
    ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305,
1267
12.1k
        0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1268
12.1k
  } else {
1269
    /*
1270
     * CHACHA20 is fast and safe on all hardware and is thus our
1271
     * preferred symmetric cipher, with AES second.
1272
     */
1273
0
    ssl_cipher_apply_rule(0, 0, 0, SSL_CHACHA20POLY1305,
1274
0
        0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1275
0
    ssl_cipher_apply_rule(0, 0, 0, SSL_AES, 0, 0, 0,
1276
0
        CIPHER_ADD, -1, &head, &tail);
1277
0
  }
1278
1279
  /* Temporarily enable everything else for sorting */
1280
12.1k
  ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_ADD, -1, &head, &tail);
1281
1282
  /* Low priority for MD5 */
1283
12.1k
  ssl_cipher_apply_rule(0, 0, 0, 0, SSL_MD5, 0, 0, CIPHER_ORD, -1, &head, &tail);
1284
1285
  /* Move anonymous ciphers to the end.  Usually, these will remain disabled.
1286
   * (For applications that allow them, they aren't too bad, but we prefer
1287
   * authenticated ciphers.) */
1288
12.1k
  ssl_cipher_apply_rule(0, 0, SSL_aNULL, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
1289
1290
  /* Move ciphers without forward secrecy to the end */
1291
12.1k
  ssl_cipher_apply_rule(0, SSL_kRSA, 0, 0, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
1292
1293
  /* RC4 is sort of broken - move it to the end */
1294
12.1k
  ssl_cipher_apply_rule(0, 0, 0, SSL_RC4, 0, 0, 0, CIPHER_ORD, -1, &head, &tail);
1295
1296
  /* Now sort by symmetric encryption strength.  The above ordering remains
1297
   * in force within each class */
1298
12.1k
  if (!ssl_cipher_strength_sort(&head, &tail)) {
1299
0
    free(co_list);
1300
0
    return NULL;
1301
0
  }
1302
1303
  /* Now disable everything (maintaining the ordering!) */
1304
12.1k
  ssl_cipher_apply_rule(0, 0, 0, 0, 0, 0, 0, CIPHER_DEL, -1, &head, &tail);
1305
1306
  /* TLSv1.3 first. */
1307
12.1k
  ssl_cipher_apply_rule(0, 0, 0, 0, 0, SSL_TLSV1_3, 0, CIPHER_ADD, -1, &head, &tail);
1308
12.1k
  ssl_cipher_apply_rule(0, 0, 0, 0, 0, SSL_TLSV1_3, 0, CIPHER_DEL, -1, &head, &tail);
1309
1310
  /*
1311
   * We also need cipher aliases for selecting based on the rule_str.
1312
   * There might be two types of entries in the rule_str: 1) names
1313
   * of ciphers themselves 2) aliases for groups of ciphers.
1314
   * For 1) we need the available ciphers and for 2) the cipher
1315
   * groups of cipher_aliases added together in one list (otherwise
1316
   * we would be happy with just the cipher_aliases table).
1317
   */
1318
12.1k
  num_of_group_aliases = sizeof(cipher_aliases) / sizeof(SSL_CIPHER);
1319
12.1k
  num_of_alias_max = num_of_ciphers + num_of_group_aliases + 1;
1320
12.1k
  ca_list = reallocarray(NULL, num_of_alias_max, sizeof(SSL_CIPHER *));
1321
12.1k
  if (ca_list == NULL) {
1322
0
    free(co_list);
1323
0
    SSLerrorx(ERR_R_MALLOC_FAILURE);
1324
0
    return(NULL); /* Failure */
1325
0
  }
1326
12.1k
  ssl_cipher_collect_aliases(ca_list, num_of_group_aliases, disabled_mkey,
1327
12.1k
      disabled_auth, disabled_enc, disabled_mac, disabled_ssl, head);
1328
1329
  /*
1330
   * If the rule_string begins with DEFAULT, apply the default rule
1331
   * before using the (possibly available) additional rules.
1332
   */
1333
12.1k
  ok = 1;
1334
12.1k
  rule_p = rule_str;
1335
12.1k
  if (strncmp(rule_str, "DEFAULT", 7) == 0) {
1336
0
    ok = ssl_cipher_process_rulestr(SSL_DEFAULT_CIPHER_LIST,
1337
0
        &head, &tail, ca_list, cert, &tls13_seen);
1338
0
    rule_p += 7;
1339
0
    if (*rule_p == ':')
1340
0
      rule_p++;
1341
0
  }
1342
1343
12.1k
  if (ok && (strlen(rule_p) > 0))
1344
12.1k
    ok = ssl_cipher_process_rulestr(rule_p, &head, &tail, ca_list,
1345
12.1k
        cert, &tls13_seen);
1346
1347
12.1k
  free((void *)ca_list);  /* Not needed anymore */
1348
1349
12.1k
  if (!ok) {
1350
    /* Rule processing failure */
1351
0
    free(co_list);
1352
0
    return (NULL);
1353
0
  }
1354
1355
  /*
1356
   * Allocate new "cipherstack" for the result, return with error
1357
   * if we cannot get one.
1358
   */
1359
12.1k
  if ((cipherstack = sk_SSL_CIPHER_new_null()) == NULL) {
1360
0
    free(co_list);
1361
0
    return (NULL);
1362
0
  }
1363
1364
  /* Prefer TLSv1.3 cipher suites. */
1365
12.1k
  if (cipher_list_tls13 != NULL) {
1366
0
    for (i = 0; i < sk_SSL_CIPHER_num(cipher_list_tls13); i++) {
1367
0
      cipher = sk_SSL_CIPHER_value(cipher_list_tls13, i);
1368
0
      sk_SSL_CIPHER_push(cipherstack, cipher);
1369
0
    }
1370
0
    tls13_seen = 1;
1371
0
  }
1372
1373
  /*
1374
   * The cipher selection for the list is done. The ciphers are added
1375
   * to the resulting precedence to the STACK_OF(SSL_CIPHER).
1376
   *
1377
   * If the rule string did not contain any references to TLSv1.3 and
1378
   * TLSv1.3 cipher suites have not been configured separately,
1379
   * include inactive TLSv1.3 cipher suites. This avoids attempts to
1380
   * use TLSv1.3 with an older rule string that does not include
1381
   * TLSv1.3 cipher suites. If the rule string resulted in no active
1382
   * cipher suites then we return an empty stack.
1383
   */
1384
12.1k
  any_active = 0;
1385
741k
  for (curr = head; curr != NULL; curr = curr->next) {
1386
729k
    if (curr->active ||
1387
729k
        (!tls13_seen && curr->cipher->algorithm_ssl == SSL_TLSV1_3))
1388
729k
      sk_SSL_CIPHER_push(cipherstack, curr->cipher);
1389
729k
    any_active |= curr->active;
1390
729k
  }
1391
12.1k
  if (!any_active)
1392
12.1k
    sk_SSL_CIPHER_zero(cipherstack);
1393
1394
12.1k
  free(co_list);  /* Not needed any longer */
1395
1396
12.1k
  sk_SSL_CIPHER_free(*cipher_list);
1397
12.1k
  *cipher_list = cipherstack;
1398
1399
12.1k
  return (cipherstack);
1400
12.1k
}
1401
1402
const SSL_CIPHER *
1403
SSL_CIPHER_get_by_id(unsigned int id)
1404
0
{
1405
0
  return ssl3_get_cipher_by_id(id);
1406
0
}
1407
1408
const SSL_CIPHER *
1409
SSL_CIPHER_get_by_value(uint16_t value)
1410
0
{
1411
0
  return ssl3_get_cipher_by_value(value);
1412
0
}
1413
1414
char *
1415
SSL_CIPHER_description(const SSL_CIPHER *cipher, char *buf, int len)
1416
0
{
1417
0
  unsigned long alg_mkey, alg_auth, alg_enc, alg_mac, alg_ssl, alg2;
1418
0
  const char *ver, *kx, *au, *enc, *mac;
1419
0
  char *ret;
1420
0
  int l;
1421
1422
0
  alg_mkey = cipher->algorithm_mkey;
1423
0
  alg_auth = cipher->algorithm_auth;
1424
0
  alg_enc = cipher->algorithm_enc;
1425
0
  alg_mac = cipher->algorithm_mac;
1426
0
  alg_ssl = cipher->algorithm_ssl;
1427
1428
0
  alg2 = cipher->algorithm2;
1429
1430
0
  if (alg_ssl & SSL_SSLV3)
1431
0
    ver = "SSLv3";
1432
0
  else if (alg_ssl & SSL_TLSV1_2)
1433
0
    ver = "TLSv1.2";
1434
0
  else if (alg_ssl & SSL_TLSV1_3)
1435
0
    ver = "TLSv1.3";
1436
0
  else
1437
0
    ver = "unknown";
1438
1439
0
  switch (alg_mkey) {
1440
0
  case SSL_kRSA:
1441
0
    kx = "RSA";
1442
0
    break;
1443
0
  case SSL_kDHE:
1444
0
    kx = "DH";
1445
0
    break;
1446
0
  case SSL_kECDHE:
1447
0
    kx = "ECDH";
1448
0
    break;
1449
0
  case SSL_kGOST:
1450
0
    kx = "GOST";
1451
0
    break;
1452
0
  case SSL_kTLS1_3:
1453
0
    kx = "TLSv1.3";
1454
0
    break;
1455
0
  default:
1456
0
    kx = "unknown";
1457
0
  }
1458
1459
0
  switch (alg_auth) {
1460
0
  case SSL_aRSA:
1461
0
    au = "RSA";
1462
0
    break;
1463
0
  case SSL_aDSS:
1464
0
    au = "DSS";
1465
0
    break;
1466
0
  case SSL_aNULL:
1467
0
    au = "None";
1468
0
    break;
1469
0
  case SSL_aECDSA:
1470
0
    au = "ECDSA";
1471
0
    break;
1472
0
  case SSL_aGOST01:
1473
0
    au = "GOST01";
1474
0
    break;
1475
0
  case SSL_aTLS1_3:
1476
0
    au = "TLSv1.3";
1477
0
    break;
1478
0
  default:
1479
0
    au = "unknown";
1480
0
    break;
1481
0
  }
1482
1483
0
  switch (alg_enc) {
1484
0
  case SSL_3DES:
1485
0
    enc = "3DES(168)";
1486
0
    break;
1487
0
  case SSL_RC4:
1488
0
    enc = alg2 & SSL2_CF_8_BYTE_ENC ? "RC4(64)" : "RC4(128)";
1489
0
    break;
1490
0
  case SSL_eNULL:
1491
0
    enc = "None";
1492
0
    break;
1493
0
  case SSL_AES128:
1494
0
    enc = "AES(128)";
1495
0
    break;
1496
0
  case SSL_AES256:
1497
0
    enc = "AES(256)";
1498
0
    break;
1499
0
  case SSL_AES128GCM:
1500
0
    enc = "AESGCM(128)";
1501
0
    break;
1502
0
  case SSL_AES256GCM:
1503
0
    enc = "AESGCM(256)";
1504
0
    break;
1505
0
  case SSL_CAMELLIA128:
1506
0
    enc = "Camellia(128)";
1507
0
    break;
1508
0
  case SSL_CAMELLIA256:
1509
0
    enc = "Camellia(256)";
1510
0
    break;
1511
0
  case SSL_CHACHA20POLY1305:
1512
0
    enc = "ChaCha20-Poly1305";
1513
0
    break;
1514
0
  case SSL_eGOST2814789CNT:
1515
0
    enc = "GOST-28178-89-CNT";
1516
0
    break;
1517
0
  default:
1518
0
    enc = "unknown";
1519
0
    break;
1520
0
  }
1521
1522
0
  switch (alg_mac) {
1523
0
  case SSL_MD5:
1524
0
    mac = "MD5";
1525
0
    break;
1526
0
  case SSL_SHA1:
1527
0
    mac = "SHA1";
1528
0
    break;
1529
0
  case SSL_SHA256:
1530
0
    mac = "SHA256";
1531
0
    break;
1532
0
  case SSL_SHA384:
1533
0
    mac = "SHA384";
1534
0
    break;
1535
0
  case SSL_AEAD:
1536
0
    mac = "AEAD";
1537
0
    break;
1538
0
  case SSL_GOST94:
1539
0
    mac = "GOST94";
1540
0
    break;
1541
0
  case SSL_GOST89MAC:
1542
0
    mac = "GOST89IMIT";
1543
0
    break;
1544
0
  case SSL_STREEBOG256:
1545
0
    mac = "STREEBOG256";
1546
0
    break;
1547
0
  default:
1548
0
    mac = "unknown";
1549
0
    break;
1550
0
  }
1551
1552
0
  if (asprintf(&ret, "%-23s %s Kx=%-8s Au=%-4s Enc=%-9s Mac=%-4s\n",
1553
0
      cipher->name, ver, kx, au, enc, mac) == -1)
1554
0
    return "OPENSSL_malloc Error";
1555
1556
0
  if (buf != NULL) {
1557
0
    l = strlcpy(buf, ret, len);
1558
0
    free(ret);
1559
0
    ret = buf;
1560
0
    if (l >= len)
1561
0
      ret = "Buffer too small";
1562
0
  }
1563
1564
0
  return (ret);
1565
0
}
1566
1567
const char *
1568
SSL_CIPHER_get_version(const SSL_CIPHER *c)
1569
0
{
1570
0
  if (c == NULL)
1571
0
    return("(NONE)");
1572
0
  if ((c->id >> 24) == 3)
1573
0
    return("TLSv1/SSLv3");
1574
0
  else
1575
0
    return("unknown");
1576
0
}
1577
1578
/* return the actual cipher being used */
1579
const char *
1580
SSL_CIPHER_get_name(const SSL_CIPHER *c)
1581
0
{
1582
0
  if (c != NULL)
1583
0
    return (c->name);
1584
0
  return("(NONE)");
1585
0
}
1586
1587
/* number of bits for symmetric cipher */
1588
int
1589
SSL_CIPHER_get_bits(const SSL_CIPHER *c, int *alg_bits)
1590
0
{
1591
0
  int ret = 0;
1592
1593
0
  if (c != NULL) {
1594
0
    if (alg_bits != NULL)
1595
0
      *alg_bits = c->alg_bits;
1596
0
    ret = c->strength_bits;
1597
0
  }
1598
0
  return (ret);
1599
0
}
1600
1601
unsigned long
1602
SSL_CIPHER_get_id(const SSL_CIPHER *c)
1603
0
{
1604
0
  return c->id;
1605
0
}
1606
1607
uint16_t
1608
SSL_CIPHER_get_value(const SSL_CIPHER *c)
1609
158
{
1610
158
  return ssl3_cipher_get_value(c);
1611
158
}
1612
1613
const SSL_CIPHER *
1614
SSL_CIPHER_find(SSL *ssl, const unsigned char *ptr)
1615
0
{
1616
0
  uint16_t cipher_value;
1617
0
  CBS cbs;
1618
1619
  /* This API is documented with ptr being an array of length two. */
1620
0
  CBS_init(&cbs, ptr, 2);
1621
0
  if (!CBS_get_u16(&cbs, &cipher_value))
1622
0
    return NULL;
1623
1624
0
  return ssl3_get_cipher_by_value(cipher_value);
1625
0
}
1626
1627
int
1628
SSL_CIPHER_get_cipher_nid(const SSL_CIPHER *c)
1629
0
{
1630
0
  switch (c->algorithm_enc) {
1631
0
  case SSL_eNULL:
1632
0
    return NID_undef;
1633
0
  case SSL_3DES:
1634
0
    return NID_des_ede3_cbc;
1635
0
  case SSL_AES128:
1636
0
    return NID_aes_128_cbc;
1637
0
  case SSL_AES128GCM:
1638
0
    return NID_aes_128_gcm;
1639
0
  case SSL_AES256:
1640
0
    return NID_aes_256_cbc;
1641
0
  case SSL_AES256GCM:
1642
0
    return NID_aes_256_gcm;
1643
0
  case SSL_CAMELLIA128:
1644
0
    return NID_camellia_128_cbc;
1645
0
  case SSL_CAMELLIA256:
1646
0
    return NID_camellia_256_cbc;
1647
0
  case SSL_CHACHA20POLY1305:
1648
0
    return NID_chacha20_poly1305;
1649
0
  case SSL_DES:
1650
0
    return NID_des_cbc;
1651
0
  case SSL_RC4:
1652
0
    return NID_rc4;
1653
0
  case SSL_eGOST2814789CNT:
1654
0
    return NID_gost89_cnt;
1655
0
  default:
1656
0
    return NID_undef;
1657
0
  }
1658
0
}
1659
1660
int
1661
SSL_CIPHER_get_digest_nid(const SSL_CIPHER *c)
1662
0
{
1663
0
  switch (c->algorithm_mac) {
1664
0
  case SSL_AEAD:
1665
0
    return NID_undef;
1666
0
  case SSL_GOST89MAC:
1667
0
    return NID_id_Gost28147_89_MAC;
1668
0
  case SSL_GOST94:
1669
0
    return NID_id_GostR3411_94;
1670
0
  case SSL_MD5:
1671
0
    return NID_md5;
1672
0
  case SSL_SHA1:
1673
0
    return NID_sha1;
1674
0
  case SSL_SHA256:
1675
0
    return NID_sha256;
1676
0
  case SSL_SHA384:
1677
0
    return NID_sha384;
1678
0
  case SSL_STREEBOG256:
1679
0
    return NID_id_tc26_gost3411_2012_256;
1680
0
  default:
1681
0
    return NID_undef;
1682
0
  }
1683
0
}
1684
1685
int
1686
SSL_CIPHER_get_kx_nid(const SSL_CIPHER *c)
1687
0
{
1688
0
  switch (c->algorithm_mkey) {
1689
0
  case SSL_kDHE:
1690
0
    return NID_kx_dhe;
1691
0
  case SSL_kECDHE:
1692
0
    return NID_kx_ecdhe;
1693
0
  case SSL_kGOST:
1694
0
    return NID_kx_gost;
1695
0
  case SSL_kRSA:
1696
0
    return NID_kx_rsa;
1697
0
  default:
1698
0
    return NID_undef;
1699
0
  }
1700
0
}
1701
1702
int
1703
SSL_CIPHER_get_auth_nid(const SSL_CIPHER *c)
1704
0
{
1705
0
  switch (c->algorithm_auth) {
1706
0
  case SSL_aNULL:
1707
0
    return NID_auth_null;
1708
0
  case SSL_aECDSA:
1709
0
    return NID_auth_ecdsa;
1710
0
  case SSL_aGOST01:
1711
0
    return NID_auth_gost01;
1712
0
  case SSL_aRSA:
1713
0
    return NID_auth_rsa;
1714
0
  default:
1715
0
    return NID_undef;
1716
0
  }
1717
0
}
1718
1719
int
1720
SSL_CIPHER_is_aead(const SSL_CIPHER *c)
1721
0
{
1722
0
  return (c->algorithm_mac & SSL_AEAD) == SSL_AEAD;
1723
0
}
1724
1725
void *
1726
SSL_COMP_get_compression_methods(void)
1727
2
{
1728
2
  return NULL;
1729
2
}
1730
1731
int
1732
SSL_COMP_add_compression_method(int id, void *cm)
1733
0
{
1734
0
  return 1;
1735
0
}
1736
1737
const char *
1738
SSL_COMP_get_name(const void *comp)
1739
0
{
1740
0
  return NULL;
1741
0
}