diff options
| author | tb <> | 2022-06-28 20:40:24 +0000 |
|---|---|---|
| committer | tb <> | 2022-06-28 20:40:24 +0000 |
| commit | 155ade5b4877933ef80a8ed009a2750254d9dec8 (patch) | |
| tree | 03da3cce0daceebde86bd02a2c0a4cf117bd10c9 /src/lib/libssl/ssl_seclevel.c | |
| parent | 3628f027e307db44ceb406c2ef4c3852af79ba09 (diff) | |
| download | openbsd-155ade5b4877933ef80a8ed009a2750254d9dec8.tar.gz openbsd-155ade5b4877933ef80a8ed009a2750254d9dec8.tar.bz2 openbsd-155ade5b4877933ef80a8ed009a2750254d9dec8.zip | |
Implement the default security level callback
And here is where the fun starts. The tentacles will grow everywhere.
ok beck jsing sthen
Diffstat (limited to 'src/lib/libssl/ssl_seclevel.c')
| -rw-r--r-- | src/lib/libssl/ssl_seclevel.c | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/src/lib/libssl/ssl_seclevel.c b/src/lib/libssl/ssl_seclevel.c new file mode 100644 index 0000000000..3da78c65b7 --- /dev/null +++ b/src/lib/libssl/ssl_seclevel.c | |||
| @@ -0,0 +1,194 @@ | |||
| 1 | /* $OpenBSD: ssl_seclevel.c,v 1.1 2022/06/28 20:40:24 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/ossl_typ.h> | ||
| 21 | #include <openssl/ssl.h> | ||
| 22 | #include <openssl/tls1.h> | ||
| 23 | |||
| 24 | #include "ssl_locl.h" | ||
| 25 | |||
| 26 | static int | ||
| 27 | ssl_security_normalize_level(const SSL_CTX *ctx, const SSL *ssl, int *out_level) | ||
| 28 | { | ||
| 29 | int security_level; | ||
| 30 | |||
| 31 | if (ctx != NULL) | ||
| 32 | security_level = SSL_CTX_get_security_level(ctx); | ||
| 33 | else | ||
| 34 | security_level = SSL_get_security_level(ssl); | ||
| 35 | |||
| 36 | if (security_level < 0) | ||
| 37 | security_level = 0; | ||
| 38 | if (security_level > 5) | ||
| 39 | security_level = 5; | ||
| 40 | |||
| 41 | *out_level = security_level; | ||
| 42 | |||
| 43 | return 1; | ||
| 44 | } | ||
| 45 | |||
| 46 | static int | ||
| 47 | ssl_security_level_to_minimum_bits(int security_level, int *out_minimum_bits) | ||
| 48 | { | ||
| 49 | if (security_level < 0) | ||
| 50 | return 0; | ||
| 51 | |||
| 52 | if (security_level == 0) | ||
| 53 | *out_minimum_bits = 0; | ||
| 54 | else if (security_level == 1) | ||
| 55 | *out_minimum_bits = 80; | ||
| 56 | else if (security_level == 2) | ||
| 57 | *out_minimum_bits = 112; | ||
| 58 | else if (security_level == 3) | ||
| 59 | *out_minimum_bits = 128; | ||
| 60 | else if (security_level == 4) | ||
| 61 | *out_minimum_bits = 192; | ||
| 62 | else if (security_level >= 5) | ||
| 63 | *out_minimum_bits = 256; | ||
| 64 | |||
| 65 | return 1; | ||
| 66 | } | ||
| 67 | |||
| 68 | static int | ||
| 69 | ssl_security_level_and_minimum_bits(const SSL_CTX *ctx, const SSL *ssl, | ||
| 70 | int *out_level, int *out_minimum_bits) | ||
| 71 | { | ||
| 72 | int security_level = 0, minimum_bits = 0; | ||
| 73 | |||
| 74 | if (!ssl_security_normalize_level(ctx, ssl, &security_level)) | ||
| 75 | return 0; | ||
| 76 | if (!ssl_security_level_to_minimum_bits(security_level, &minimum_bits)) | ||
| 77 | return 0; | ||
| 78 | |||
| 79 | if (out_level != NULL) | ||
| 80 | *out_level = security_level; | ||
| 81 | if (out_minimum_bits != NULL) | ||
| 82 | *out_minimum_bits = minimum_bits; | ||
| 83 | |||
| 84 | return 1; | ||
| 85 | } | ||
| 86 | |||
| 87 | static int | ||
| 88 | ssl_security_secop_cipher(const SSL_CTX *ctx, const SSL *ssl, int bits, | ||
| 89 | void *arg) | ||
| 90 | { | ||
| 91 | const SSL_CIPHER *cipher = arg; | ||
| 92 | int security_level, minimum_bits; | ||
| 93 | |||
| 94 | if (!ssl_security_level_and_minimum_bits(ctx, ssl, &security_level, | ||
| 95 | &minimum_bits)) | ||
| 96 | return 0; | ||
| 97 | |||
| 98 | if (security_level <= 0) | ||
| 99 | return 1; | ||
| 100 | |||
| 101 | if (bits < minimum_bits) | ||
| 102 | return 0; | ||
| 103 | |||
| 104 | /* No unauthenticated ciphersuites */ | ||
| 105 | if (cipher->algorithm_auth & SSL_aNULL) | ||
| 106 | return 0; | ||
| 107 | |||
| 108 | if (security_level <= 1) | ||
| 109 | return 1; | ||
| 110 | |||
| 111 | if (cipher->algorithm_enc == SSL_RC4) | ||
| 112 | return 0; | ||
| 113 | |||
| 114 | if (security_level <= 2) | ||
| 115 | return 1; | ||
| 116 | |||
| 117 | /* XXX TLSv1.3 */ | ||
| 118 | if ((cipher->algorithm_mkey & (SSL_kDHE | SSL_kECDHE)) != 0) | ||
| 119 | return 0; | ||
| 120 | |||
| 121 | return 1; | ||
| 122 | } | ||
| 123 | |||
| 124 | static int | ||
| 125 | ssl_security_secop_version(const SSL_CTX *ctx, const SSL *ssl, int version) | ||
| 126 | { | ||
| 127 | int min_version = TLS1_2_VERSION; | ||
| 128 | int security_level; | ||
| 129 | |||
| 130 | if (!ssl_security_level_and_minimum_bits(ctx, ssl, &security_level, NULL)) | ||
| 131 | return 0; | ||
| 132 | |||
| 133 | if (security_level < 4) | ||
| 134 | min_version = TLS1_1_VERSION; | ||
| 135 | if (security_level < 3) | ||
| 136 | min_version = TLS1_VERSION; | ||
| 137 | |||
| 138 | return ssl_tls_version(version) >= min_version; | ||
| 139 | } | ||
| 140 | |||
| 141 | static int | ||
| 142 | ssl_security_secop_compression(const SSL_CTX *ctx, const SSL *ssl) | ||
| 143 | { | ||
| 144 | return 0; | ||
| 145 | } | ||
| 146 | |||
| 147 | static int | ||
| 148 | ssl_security_secop_tickets(const SSL_CTX *ctx, const SSL *ssl) | ||
| 149 | { | ||
| 150 | int security_level; | ||
| 151 | |||
| 152 | if (!ssl_security_level_and_minimum_bits(ctx, ssl, &security_level, NULL)) | ||
| 153 | return 0; | ||
| 154 | |||
| 155 | return security_level < 3; | ||
| 156 | } | ||
| 157 | |||
| 158 | static int | ||
| 159 | ssl_security_secop_default(const SSL_CTX *ctx, const SSL *ssl, int bits) | ||
| 160 | { | ||
| 161 | int minimum_bits; | ||
| 162 | |||
| 163 | if (!ssl_security_level_and_minimum_bits(ctx, ssl, NULL, &minimum_bits)) | ||
| 164 | return 0; | ||
| 165 | |||
| 166 | return bits >= minimum_bits; | ||
| 167 | } | ||
| 168 | |||
| 169 | int | ||
| 170 | ssl_security_default_cb(const SSL *ssl, const SSL_CTX *ctx, int op, int bits, | ||
| 171 | int version, void *cipher, void *ex_data) | ||
| 172 | { | ||
| 173 | switch (op) { | ||
| 174 | case SSL_SECOP_CIPHER_SUPPORTED: | ||
| 175 | case SSL_SECOP_CIPHER_SHARED: | ||
| 176 | case SSL_SECOP_CIPHER_CHECK: | ||
| 177 | return ssl_security_secop_cipher(ctx, ssl, bits, cipher); | ||
| 178 | case SSL_SECOP_VERSION: | ||
| 179 | return ssl_security_secop_version(ctx, ssl, version); | ||
| 180 | case SSL_SECOP_COMPRESSION: | ||
| 181 | return ssl_security_secop_compression(ctx, ssl); | ||
| 182 | case SSL_SECOP_TICKET: | ||
| 183 | return ssl_security_secop_tickets(ctx, ssl); | ||
| 184 | default: | ||
| 185 | return ssl_security_secop_default(ctx, ssl, bits); | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | int | ||
| 190 | ssl_security_dummy_cb(const SSL *ssl, const SSL_CTX *ctx, int op, int bits, | ||
| 191 | int version, void *cipher, void *ex_data) | ||
| 192 | { | ||
| 193 | return 1; | ||
| 194 | } | ||
