| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/s2n_auth_selection.h" |
| 17 |
|
| 18 |
#include "crypto/s2n_certificate.h" |
| 19 |
#include "crypto/s2n_signature.h" |
| 20 |
#include "tls/s2n_cipher_suites.h" |
| 21 |
#include "tls/s2n_kex.h" |
| 22 |
#include "tls/s2n_signature_algorithms.h" |
| 23 |
#include "utils/s2n_safety.h" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
int s2n_get_auth_method_for_cert_type(s2n_pkey_type cert_type, s2n_authentication_method *auth_method) |
| 43 |
{ |
| 44 |
switch (cert_type) { |
| 45 |
case S2N_PKEY_TYPE_RSA: |
| 46 |
case S2N_PKEY_TYPE_RSA_PSS: |
| 47 |
*auth_method = S2N_AUTHENTICATION_RSA; |
| 48 |
return S2N_SUCCESS; |
| 49 |
case S2N_PKEY_TYPE_ECDSA: |
| 50 |
*auth_method = S2N_AUTHENTICATION_ECDSA; |
| 51 |
return S2N_SUCCESS; |
| 52 |
case S2N_PKEY_TYPE_MLDSA: |
| 53 |
case S2N_PKEY_TYPE_UNKNOWN: |
| 54 |
case S2N_PKEY_TYPE_SENTINEL: |
| 55 |
POSIX_BAIL(S2N_ERR_CERT_TYPE_UNSUPPORTED); |
| 56 |
} |
| 57 |
POSIX_BAIL(S2N_ERR_CERT_TYPE_UNSUPPORTED); |
| 58 |
} |
| 59 |
|
| 60 |
static int s2n_is_sig_alg_valid_for_cipher_suite(s2n_signature_algorithm sig_alg, struct s2n_cipher_suite *cipher_suite) |
| 61 |
{ |
| 62 |
POSIX_ENSURE_REF(cipher_suite); |
| 63 |
|
| 64 |
s2n_pkey_type cert_type_for_sig_alg = S2N_PKEY_TYPE_UNKNOWN; |
| 65 |
POSIX_GUARD_RESULT(s2n_signature_algorithm_get_pkey_type(sig_alg, &cert_type_for_sig_alg)); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
const struct s2n_kex *kex = cipher_suite->key_exchange_alg; |
| 74 |
POSIX_ENSURE_REF(kex); |
| 75 |
if (!kex->is_ephemeral) { |
| 76 |
POSIX_ENSURE_NE(cert_type_for_sig_alg, S2N_PKEY_TYPE_RSA_PSS); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
if (cipher_suite->auth_method != S2N_AUTHENTICATION_METHOD_SENTINEL) { |
| 83 |
s2n_authentication_method auth_method_for_sig_alg; |
| 84 |
POSIX_GUARD(s2n_get_auth_method_for_cert_type(cert_type_for_sig_alg, &auth_method_for_sig_alg)); |
| 85 |
POSIX_ENSURE_EQ(cipher_suite->auth_method, auth_method_for_sig_alg); |
| 86 |
} |
| 87 |
|
| 88 |
return S2N_SUCCESS; |
| 89 |
} |
| 90 |
|
| 91 |
static int s2n_certs_exist_for_sig_scheme(struct s2n_connection *conn, const struct s2n_signature_scheme *sig_scheme) |
| 92 |
{ |
| 93 |
POSIX_ENSURE_REF(sig_scheme); |
| 94 |
|
| 95 |
s2n_pkey_type cert_type = S2N_PKEY_TYPE_UNKNOWN; |
| 96 |
POSIX_GUARD_RESULT(s2n_signature_algorithm_get_pkey_type(sig_scheme->sig_alg, &cert_type)); |
| 97 |
|
| 98 |
struct s2n_cert_chain_and_key *cert = s2n_get_compatible_cert_chain_and_key(conn, cert_type); |
| 99 |
POSIX_ENSURE_REF(cert); |
| 100 |
|
| 101 |
|
| 102 |
if (conn->actual_protocol_version >= S2N_TLS13) { |
| 103 |
if (cert_type == S2N_PKEY_TYPE_RSA || cert_type == S2N_PKEY_TYPE_RSA_PSS) { |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
return S2N_SUCCESS; |
| 108 |
} else if (cert_type == S2N_PKEY_TYPE_ECDSA) { |
| 109 |
|
| 110 |
|
| 111 |
POSIX_ENSURE_REF(cert->private_key); |
| 112 |
POSIX_ENSURE_REF(cert->cert_chain); |
| 113 |
POSIX_ENSURE_REF(cert->cert_chain->head); |
| 114 |
POSIX_ENSURE_EQ(cert->cert_chain->head->pkey_type, S2N_PKEY_TYPE_ECDSA); |
| 115 |
POSIX_ENSURE_EQ(cert->cert_chain->head->info.public_key_nid, sig_scheme->signature_curve->libcrypto_nid); |
| 116 |
} else if (cert_type == S2N_PKEY_TYPE_MLDSA) { |
| 117 |
|
| 118 |
|
| 119 |
POSIX_ENSURE_REF(cert->private_key); |
| 120 |
POSIX_ENSURE_REF(cert->cert_chain); |
| 121 |
POSIX_ENSURE_REF(cert->cert_chain->head); |
| 122 |
POSIX_ENSURE_EQ(cert->cert_chain->head->pkey_type, S2N_PKEY_TYPE_MLDSA); |
| 123 |
POSIX_ENSURE_EQ(cert->cert_chain->head->info.signature_nid, sig_scheme->libcrypto_nid); |
| 124 |
} else { |
| 125 |
|
| 126 |
|
| 127 |
POSIX_BAIL(S2N_ERR_UNIMPLEMENTED); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return S2N_SUCCESS; |
| 132 |
} |
| 133 |
|
| 134 |
static int s2n_certs_exist_for_auth_method(struct s2n_connection *conn, s2n_authentication_method auth_method) |
| 135 |
{ |
| 136 |
if (auth_method == S2N_AUTHENTICATION_METHOD_SENTINEL) { |
| 137 |
return S2N_SUCCESS; |
| 138 |
} |
| 139 |
|
| 140 |
s2n_authentication_method auth_method_for_cert_type; |
| 141 |
for (int i = 0; i < S2N_CERT_TYPE_COUNT; i++) { |
| 142 |
POSIX_GUARD(s2n_get_auth_method_for_cert_type(i, &auth_method_for_cert_type)); |
| 143 |
|
| 144 |
if (auth_method != auth_method_for_cert_type) { |
| 145 |
continue; |
| 146 |
} |
| 147 |
|
| 148 |
if (s2n_get_compatible_cert_chain_and_key(conn, i) != NULL) { |
| 149 |
return S2N_SUCCESS; |
| 150 |
} |
| 151 |
} |
| 152 |
POSIX_BAIL(S2N_ERR_CERT_TYPE_UNSUPPORTED); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
int s2n_is_cipher_suite_valid_for_auth(struct s2n_connection *conn, struct s2n_cipher_suite *cipher_suite) |
| 163 |
{ |
| 164 |
POSIX_ENSURE_REF(cipher_suite); |
| 165 |
|
| 166 |
POSIX_GUARD(s2n_certs_exist_for_auth_method(conn, cipher_suite->auth_method)); |
| 167 |
|
| 168 |
return S2N_SUCCESS; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
int s2n_is_sig_scheme_valid_for_auth(struct s2n_connection *conn, const struct s2n_signature_scheme *sig_scheme) |
| 179 |
{ |
| 180 |
POSIX_ENSURE_REF(conn); |
| 181 |
POSIX_ENSURE_REF(conn->secure); |
| 182 |
POSIX_ENSURE_REF(sig_scheme); |
| 183 |
|
| 184 |
struct s2n_cipher_suite *cipher_suite = conn->secure->cipher_suite; |
| 185 |
POSIX_ENSURE_REF(cipher_suite); |
| 186 |
|
| 187 |
POSIX_GUARD(s2n_certs_exist_for_sig_scheme(conn, sig_scheme)); |
| 188 |
|
| 189 |
|
| 190 |
if (conn->mode == S2N_SERVER) { |
| 191 |
POSIX_GUARD(s2n_is_sig_alg_valid_for_cipher_suite(sig_scheme->sig_alg, cipher_suite)); |
| 192 |
} |
| 193 |
return S2N_SUCCESS; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
int s2n_is_cert_type_valid_for_auth(struct s2n_connection *conn, s2n_pkey_type cert_type) |
| 206 |
{ |
| 207 |
POSIX_ENSURE_REF(conn); |
| 208 |
POSIX_ENSURE_REF(conn->secure); |
| 209 |
POSIX_ENSURE_REF(conn->secure->cipher_suite); |
| 210 |
s2n_authentication_method conn_auth_method = conn->secure->cipher_suite->auth_method; |
| 211 |
|
| 212 |
|
| 213 |
if (conn_auth_method == S2N_AUTHENTICATION_METHOD_SENTINEL) { |
| 214 |
return S2N_SUCCESS; |
| 215 |
} |
| 216 |
|
| 217 |
s2n_authentication_method cert_auth_method = 0; |
| 218 |
POSIX_GUARD(s2n_get_auth_method_for_cert_type(cert_type, &cert_auth_method)); |
| 219 |
POSIX_ENSURE(cert_auth_method == conn_auth_method, S2N_ERR_CERT_TYPE_UNSUPPORTED); |
| 220 |
return S2N_SUCCESS; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
int s2n_select_certs_for_server_auth(struct s2n_connection *conn, struct s2n_cert_chain_and_key **chosen_certs) |
| 228 |
{ |
| 229 |
POSIX_ENSURE_REF(conn); |
| 230 |
const struct s2n_signature_scheme *sig_scheme = conn->handshake_params.server_cert_sig_scheme; |
| 231 |
POSIX_ENSURE_REF(sig_scheme); |
| 232 |
s2n_signature_algorithm sig_alg = sig_scheme->sig_alg; |
| 233 |
|
| 234 |
s2n_pkey_type cert_type = S2N_PKEY_TYPE_UNKNOWN; |
| 235 |
if (sig_scheme == &s2n_null_sig_scheme) { |
| 236 |
|
| 237 |
cert_type = S2N_PKEY_TYPE_RSA; |
| 238 |
} else { |
| 239 |
POSIX_GUARD_RESULT(s2n_signature_algorithm_get_pkey_type(sig_alg, &cert_type)); |
| 240 |
} |
| 241 |
|
| 242 |
*chosen_certs = s2n_get_compatible_cert_chain_and_key(conn, cert_type); |
| 243 |
S2N_ERROR_IF(*chosen_certs == NULL, S2N_ERR_CERT_TYPE_UNSUPPORTED); |
| 244 |
|
| 245 |
return S2N_SUCCESS; |
| 246 |
} |