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