1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
/* $OpenBSD: ssl_seclevel.c,v 1.28 2024/05/09 07:12:03 tb Exp $ */
/*
* Copyright (c) 2020-2022 Theo Buehler <tb@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stddef.h>
#include <openssl/asn1.h>
#include <openssl/dh.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/ossl_typ.h>
#include <openssl/ssl.h>
#include <openssl/tls1.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include "bytestring.h"
#include "ssl_local.h"
static int
ssl_security_normalize_level(const SSL_CTX *ctx, const SSL *ssl, int *out_level)
{
int security_level;
if (ctx != NULL)
security_level = SSL_CTX_get_security_level(ctx);
else
security_level = SSL_get_security_level(ssl);
if (security_level < 0)
security_level = 0;
if (security_level > 5)
security_level = 5;
*out_level = security_level;
return 1;
}
static int
ssl_security_level_to_minimum_bits(int security_level, int *out_minimum_bits)
{
if (security_level < 0)
return 0;
if (security_level == 0)
*out_minimum_bits = 0;
else if (security_level == 1)
*out_minimum_bits = 80;
else if (security_level == 2)
*out_minimum_bits = 112;
else if (security_level == 3)
*out_minimum_bits = 128;
else if (security_level == 4)
*out_minimum_bits = 192;
else if (security_level >= 5)
*out_minimum_bits = 256;
return 1;
}
static int
ssl_security_level_and_minimum_bits(const SSL_CTX *ctx, const SSL *ssl,
int *out_level, int *out_minimum_bits)
{
int security_level = 0, minimum_bits = 0;
if (!ssl_security_normalize_level(ctx, ssl, &security_level))
return 0;
if (!ssl_security_level_to_minimum_bits(security_level, &minimum_bits))
return 0;
if (out_level != NULL)
*out_level = security_level;
if (out_minimum_bits != NULL)
*out_minimum_bits = minimum_bits;
return 1;
}
static int
ssl_security_secop_cipher(const SSL_CTX *ctx, const SSL *ssl, int bits,
void *arg)
{
const SSL_CIPHER *cipher = arg;
int security_level, minimum_bits;
if (!ssl_security_level_and_minimum_bits(ctx, ssl, &security_level,
&minimum_bits))
return 0;
if (security_level <= 0)
return 1;
if (bits < minimum_bits)
return 0;
/* No unauthenticated ciphersuites. */
if (cipher->algorithm_auth & SSL_aNULL)
return 0;
if (cipher->algorithm_mac & SSL_MD5)
return 0;
if (security_level <= 1)
return 1;
if (cipher->algorithm_enc & SSL_RC4)
return 0;
if (security_level <= 2)
return 1;
/* Security level >= 3 requires a cipher with forward secrecy. */
if ((cipher->algorithm_mkey & (SSL_kDHE | SSL_kECDHE)) == 0 &&
cipher->algorithm_ssl != SSL_TLSV1_3)
return 0;
if (security_level <= 3)
return 1;
if (cipher->algorithm_mac & SSL_SHA1)
return 0;
return 1;
}
static int
ssl_security_secop_version(const SSL_CTX *ctx, const SSL *ssl, int version)
{
int min_version = TLS1_2_VERSION;
int security_level;
if (!ssl_security_level_and_minimum_bits(ctx, ssl, &security_level, NULL))
return 0;
if (security_level < 4)
min_version = TLS1_1_VERSION;
if (security_level < 3)
min_version = TLS1_VERSION;
return ssl_tls_version(version) >= min_version;
}
static int
ssl_security_secop_compression(const SSL_CTX *ctx, const SSL *ssl)
{
return 0;
}
static int
ssl_security_secop_tickets(const SSL_CTX *ctx, const SSL *ssl)
{
int security_level;
if (!ssl_security_level_and_minimum_bits(ctx, ssl, &security_level, NULL))
return 0;
return security_level < 3;
}
static int
ssl_security_secop_tmp_dh(const SSL_CTX *ctx, const SSL *ssl, int bits)
{
int security_level, minimum_bits;
if (!ssl_security_level_and_minimum_bits(ctx, ssl, &security_level,
&minimum_bits))
return 0;
/* Disallow DHE keys weaker than 1024 bits even at security level 0. */
if (security_level <= 0 && bits < 80)
return 0;
return bits >= minimum_bits;
}
static int
ssl_security_secop_default(const SSL_CTX *ctx, const SSL *ssl, int bits)
{
int minimum_bits;
if (!ssl_security_level_and_minimum_bits(ctx, ssl, NULL, &minimum_bits))
return 0;
return bits >= minimum_bits;
}
int
ssl_security_default_cb(const SSL *ssl, const SSL_CTX *ctx, int secop, int bits,
int version, void *cipher, void *ex_data)
{
switch (secop) {
case SSL_SECOP_CIPHER_SUPPORTED:
case SSL_SECOP_CIPHER_SHARED:
case SSL_SECOP_CIPHER_CHECK:
return ssl_security_secop_cipher(ctx, ssl, bits, cipher);
case SSL_SECOP_VERSION:
return ssl_security_secop_version(ctx, ssl, version);
case SSL_SECOP_COMPRESSION:
return ssl_security_secop_compression(ctx, ssl);
case SSL_SECOP_TICKET:
return ssl_security_secop_tickets(ctx, ssl);
case SSL_SECOP_TMP_DH:
return ssl_security_secop_tmp_dh(ctx, ssl, bits);
default:
return ssl_security_secop_default(ctx, ssl, bits);
}
}
static int
ssl_ctx_security(const SSL_CTX *ctx, int secop, int bits, int nid, void *other)
{
return ctx->cert->security_cb(NULL, ctx, secop, bits, nid,
other, ctx->cert->security_ex_data);
}
static int
ssl_security(const SSL *ssl, int secop, int bits, int nid, void *other)
{
return ssl->cert->security_cb(ssl, NULL, secop, bits, nid, other,
ssl->cert->security_ex_data);
}
int
ssl_security_sigalg_check(const SSL *ssl, const EVP_PKEY *pkey)
{
int bits;
bits = EVP_PKEY_security_bits(pkey);
return ssl_security(ssl, SSL_SECOP_SIGALG_CHECK, bits, 0, NULL);
}
int
ssl_security_tickets(const SSL *ssl)
{
return ssl_security(ssl, SSL_SECOP_TICKET, 0, 0, NULL);
}
int
ssl_security_version(const SSL *ssl, int version)
{
return ssl_security(ssl, SSL_SECOP_VERSION, 0, version, NULL);
}
static int
ssl_security_cipher(const SSL *ssl, SSL_CIPHER *cipher, int secop)
{
return ssl_security(ssl, secop, cipher->strength_bits, 0, cipher);
}
int
ssl_security_cipher_check(const SSL *ssl, SSL_CIPHER *cipher)
{
return ssl_security_cipher(ssl, cipher, SSL_SECOP_CIPHER_CHECK);
}
int
ssl_security_shared_cipher(const SSL *ssl, SSL_CIPHER *cipher)
{
return ssl_security_cipher(ssl, cipher, SSL_SECOP_CIPHER_SHARED);
}
int
ssl_security_supported_cipher(const SSL *ssl, SSL_CIPHER *cipher)
{
return ssl_security_cipher(ssl, cipher, SSL_SECOP_CIPHER_SUPPORTED);
}
int
ssl_ctx_security_dh(const SSL_CTX *ctx, DH *dh)
{
int bits;
bits = DH_security_bits(dh);
return ssl_ctx_security(ctx, SSL_SECOP_TMP_DH, bits, 0, dh);
}
int
ssl_security_dh(const SSL *ssl, DH *dh)
{
int bits;
bits = DH_security_bits(dh);
return ssl_security(ssl, SSL_SECOP_TMP_DH, bits, 0, dh);
}
static int
ssl_cert_pubkey_security_bits(const X509 *x509)
{
EVP_PKEY *pkey;
if ((pkey = X509_get0_pubkey(x509)) == NULL)
return -1;
/*
* XXX: DSA_security_bits() returns -1 on keys without parameters and
* makes the default security callback fail.
*/
return EVP_PKEY_security_bits(pkey);
}
static int
ssl_security_cert_key(const SSL_CTX *ctx, const SSL *ssl, X509 *x509, int secop)
{
int security_bits;
security_bits = ssl_cert_pubkey_security_bits(x509);
if (ssl != NULL)
return ssl_security(ssl, secop, security_bits, 0, x509);
return ssl_ctx_security(ctx, secop, security_bits, 0, x509);
}
static int
ssl_cert_signature_md_nid(X509 *x509)
{
int md_nid, signature_nid;
if ((signature_nid = X509_get_signature_nid(x509)) == NID_undef)
return NID_undef;
if (!OBJ_find_sigid_algs(signature_nid, &md_nid, NULL))
return NID_undef;
return md_nid;
}
static int
ssl_cert_md_nid_security_bits(int md_nid)
{
const EVP_MD *md;
if (md_nid == NID_undef)
return -1;
if ((md = EVP_get_digestbynid(md_nid)) == NULL)
return -1;
/* Assume 4 bits of collision resistance for each hash octet. */
return EVP_MD_size(md) * 4;
}
static int
ssl_security_cert_sig(const SSL_CTX *ctx, const SSL *ssl, X509 *x509, int secop)
{
int md_nid, security_bits;
/* Don't check signature if self signed. */
if ((X509_get_extension_flags(x509) & EXFLAG_SS) != 0)
return 1;
md_nid = ssl_cert_signature_md_nid(x509);
security_bits = ssl_cert_md_nid_security_bits(md_nid);
if (ssl != NULL)
return ssl_security(ssl, secop, security_bits, md_nid, x509);
return ssl_ctx_security(ctx, secop, security_bits, md_nid, x509);
}
int
ssl_security_cert(const SSL_CTX *ctx, const SSL *ssl, X509 *x509,
int is_ee, int *out_error)
{
int key_error, operation;
*out_error = 0;
if (is_ee) {
operation = SSL_SECOP_EE_KEY;
key_error = SSL_R_EE_KEY_TOO_SMALL;
} else {
operation = SSL_SECOP_CA_KEY;
key_error = SSL_R_CA_KEY_TOO_SMALL;
}
if (!ssl_security_cert_key(ctx, ssl, x509, operation)) {
*out_error = key_error;
return 0;
}
if (!ssl_security_cert_sig(ctx, ssl, x509, SSL_SECOP_CA_MD)) {
*out_error = SSL_R_CA_MD_TOO_WEAK;
return 0;
}
return 1;
}
/*
* Check security of a chain. If |sk| includes the end entity certificate
* then |x509| must be NULL.
*/
int
ssl_security_cert_chain(const SSL *ssl, STACK_OF(X509) *sk, X509 *x509,
int *out_error)
{
int start_idx = 0;
int is_ee;
int i;
if (x509 == NULL) {
x509 = sk_X509_value(sk, 0);
start_idx = 1;
}
is_ee = 1;
if (!ssl_security_cert(NULL, ssl, x509, is_ee, out_error))
return 0;
is_ee = 0;
for (i = start_idx; i < sk_X509_num(sk); i++) {
x509 = sk_X509_value(sk, i);
if (!ssl_security_cert(NULL, ssl, x509, is_ee, out_error))
return 0;
}
return 1;
}
static int
ssl_security_group(const SSL *ssl, uint16_t group_id, int secop)
{
CBB cbb;
int bits, nid;
uint8_t group[2];
memset(&cbb, 0, sizeof(cbb));
if (!tls1_ec_group_id2bits(group_id, &bits))
goto err;
if (!tls1_ec_group_id2nid(group_id, &nid))
goto err;
if (!CBB_init_fixed(&cbb, group, sizeof(group)))
goto err;
if (!CBB_add_u16(&cbb, group_id))
goto err;
if (!CBB_finish(&cbb, NULL, NULL))
goto err;
return ssl_security(ssl, secop, bits, nid, group);
err:
CBB_cleanup(&cbb);
return 0;
}
int
ssl_security_shared_group(const SSL *ssl, uint16_t group_id)
{
return ssl_security_group(ssl, group_id, SSL_SECOP_CURVE_SHARED);
}
int
ssl_security_supported_group(const SSL *ssl, uint16_t group_id)
{
return ssl_security_group(ssl, group_id, SSL_SECOP_CURVE_SUPPORTED);
}
|