| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "tls/s2n_alerts.h" |
| 17 |
|
| 18 |
#include <stdint.h> |
| 19 |
|
| 20 |
#include "error/s2n_errno.h" |
| 21 |
#include "tls/s2n_connection.h" |
| 22 |
#include "tls/s2n_record.h" |
| 23 |
#include "tls/s2n_resume.h" |
| 24 |
#include "tls/s2n_tls_parameters.h" |
| 25 |
#include "utils/s2n_atomic.h" |
| 26 |
#include "utils/s2n_blob.h" |
| 27 |
#include "utils/s2n_safety.h" |
| 28 |
|
| 29 |
#define S2N_ALERT_CASE(error, alert_code) \ |
| 30 |
case (error): \ |
| 31 |
*alert = (alert_code); \ |
| 32 |
return S2N_RESULT_OK |
| 33 |
|
| 34 |
#define S2N_NO_ALERT(error) \ |
| 35 |
case (error): \ |
| 36 |
RESULT_BAIL(S2N_ERR_NO_ALERT) |
| 37 |
|
| 38 |
static S2N_RESULT s2n_translate_protocol_error_to_alert(int error_code, uint8_t *alert) |
| 39 |
{ |
| 40 |
RESULT_ENSURE_REF(alert); |
| 41 |
|
| 42 |
switch (error_code) { |
| 43 |
S2N_ALERT_CASE(S2N_ERR_MISSING_EXTENSION, S2N_TLS_ALERT_MISSING_EXTENSION); |
| 44 |
S2N_ALERT_CASE(S2N_ERR_NO_VALID_SIGNATURE_SCHEME, S2N_TLS_ALERT_HANDSHAKE_FAILURE); |
| 45 |
S2N_ALERT_CASE(S2N_ERR_MISSING_CLIENT_CERT, S2N_TLS_ALERT_CERTIFICATE_REQUIRED); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
S2N_ALERT_CASE(S2N_ERR_BAD_MESSAGE, S2N_TLS_ALERT_UNEXPECTED_MESSAGE); |
| 54 |
S2N_ALERT_CASE(S2N_ERR_UNEXPECTED_CERT_REQUEST, S2N_TLS_ALERT_UNEXPECTED_MESSAGE); |
| 55 |
S2N_ALERT_CASE(S2N_ERR_MISSING_CERT_REQUEST, S2N_TLS_ALERT_UNEXPECTED_MESSAGE); |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
S2N_ALERT_CASE(S2N_ERR_NO_RENEGOTIATION, S2N_TLS_ALERT_HANDSHAKE_FAILURE); |
| 64 |
|
| 65 |
S2N_ALERT_CASE(S2N_ERR_KTLS_KEYUPDATE, S2N_TLS_ALERT_UNEXPECTED_MESSAGE); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
S2N_ALERT_CASE(S2N_ERR_CERT_UNTRUSTED, S2N_TLS_ALERT_CERTIFICATE_UNKNOWN); |
| 75 |
S2N_ALERT_CASE(S2N_ERR_CERT_UNHANDLED_CRITICAL_EXTENSION, S2N_TLS_ALERT_CERTIFICATE_UNKNOWN); |
| 76 |
S2N_ALERT_CASE(S2N_ERR_CERT_INVALID_HOSTNAME, S2N_TLS_ALERT_CERTIFICATE_UNKNOWN); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
S2N_ALERT_CASE(S2N_ERR_CERT_REVOKED, S2N_TLS_ALERT_CERTIFICATE_REVOKED); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
S2N_ALERT_CASE(S2N_ERR_CERT_NOT_YET_VALID, S2N_TLS_ALERT_CERTIFICATE_EXPIRED); |
| 90 |
S2N_ALERT_CASE(S2N_ERR_CERT_EXPIRED, S2N_TLS_ALERT_CERTIFICATE_EXPIRED); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
S2N_ALERT_CASE(S2N_ERR_CERT_TYPE_UNSUPPORTED, S2N_TLS_ALERT_UNSUPPORTED_CERTIFICATE); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
S2N_ALERT_CASE(S2N_ERR_CERT_REJECTED, S2N_TLS_ALERT_ACCESS_DENIED); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
S2N_ALERT_CASE(S2N_ERR_CERT_MAX_CHAIN_DEPTH_EXCEEDED, S2N_TLS_ALERT_BAD_CERTIFICATE); |
| 112 |
S2N_ALERT_CASE(S2N_ERR_CERT_INVALID, S2N_TLS_ALERT_BAD_CERTIFICATE); |
| 113 |
S2N_ALERT_CASE(S2N_ERR_DECODE_CERTIFICATE, S2N_TLS_ALERT_BAD_CERTIFICATE); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
S2N_NO_ALERT(S2N_ERR_ENCRYPT); |
| 118 |
S2N_NO_ALERT(S2N_ERR_DECRYPT); |
| 119 |
S2N_NO_ALERT(S2N_ERR_KEY_INIT); |
| 120 |
S2N_NO_ALERT(S2N_ERR_KEY_DESTROY); |
| 121 |
S2N_NO_ALERT(S2N_ERR_DH_SERIALIZING); |
| 122 |
S2N_NO_ALERT(S2N_ERR_DH_SHARED_SECRET); |
| 123 |
S2N_NO_ALERT(S2N_ERR_DH_WRITING_PUBLIC_KEY); |
| 124 |
S2N_NO_ALERT(S2N_ERR_DH_FAILED_SIGNING); |
| 125 |
S2N_NO_ALERT(S2N_ERR_DH_COPYING_PARAMETERS); |
| 126 |
S2N_NO_ALERT(S2N_ERR_DH_GENERATING_PARAMETERS); |
| 127 |
S2N_NO_ALERT(S2N_ERR_CIPHER_NOT_SUPPORTED); |
| 128 |
S2N_NO_ALERT(S2N_ERR_NO_APPLICATION_PROTOCOL); |
| 129 |
S2N_NO_ALERT(S2N_ERR_FALLBACK_DETECTED); |
| 130 |
S2N_NO_ALERT(S2N_ERR_HASH_DIGEST_FAILED); |
| 131 |
S2N_NO_ALERT(S2N_ERR_HASH_INIT_FAILED); |
| 132 |
S2N_NO_ALERT(S2N_ERR_HASH_UPDATE_FAILED); |
| 133 |
S2N_NO_ALERT(S2N_ERR_HASH_COPY_FAILED); |
| 134 |
S2N_NO_ALERT(S2N_ERR_HASH_WIPE_FAILED); |
| 135 |
S2N_NO_ALERT(S2N_ERR_HASH_NOT_READY); |
| 136 |
S2N_NO_ALERT(S2N_ERR_ALLOW_MD5_FOR_FIPS_FAILED); |
| 137 |
S2N_NO_ALERT(S2N_ERR_DECODE_PRIVATE_KEY); |
| 138 |
S2N_NO_ALERT(S2N_ERR_INVALID_HELLO_RETRY); |
| 139 |
S2N_NO_ALERT(S2N_ERR_INVALID_SIGNATURE_ALGORITHM); |
| 140 |
S2N_NO_ALERT(S2N_ERR_INVALID_SIGNATURE_SCHEME); |
| 141 |
S2N_NO_ALERT(S2N_ERR_CBC_VERIFY); |
| 142 |
S2N_NO_ALERT(S2N_ERR_DH_COPYING_PUBLIC_KEY); |
| 143 |
S2N_NO_ALERT(S2N_ERR_SIGN); |
| 144 |
S2N_NO_ALERT(S2N_ERR_VERIFY_SIGNATURE); |
| 145 |
S2N_NO_ALERT(S2N_ERR_ECDHE_GEN_KEY); |
| 146 |
S2N_NO_ALERT(S2N_ERR_ECDHE_SHARED_SECRET); |
| 147 |
S2N_NO_ALERT(S2N_ERR_ECDHE_UNSUPPORTED_CURVE); |
| 148 |
S2N_NO_ALERT(S2N_ERR_ECDHE_INVALID_PUBLIC_KEY); |
| 149 |
S2N_NO_ALERT(S2N_ERR_ECDHE_INVALID_PUBLIC_KEY_FIPS); |
| 150 |
S2N_NO_ALERT(S2N_ERR_ECDSA_UNSUPPORTED_CURVE); |
| 151 |
S2N_NO_ALERT(S2N_ERR_ECDHE_SERIALIZING); |
| 152 |
S2N_NO_ALERT(S2N_ERR_KEM_UNSUPPORTED_PARAMS); |
| 153 |
S2N_NO_ALERT(S2N_ERR_SHUTDOWN_RECORD_TYPE); |
| 154 |
S2N_NO_ALERT(S2N_ERR_SHUTDOWN_CLOSED); |
| 155 |
S2N_NO_ALERT(S2N_ERR_NON_EMPTY_RENEGOTIATION_INFO); |
| 156 |
S2N_NO_ALERT(S2N_ERR_RECORD_LIMIT); |
| 157 |
S2N_NO_ALERT(S2N_ERR_CERT_INTENT_INVALID); |
| 158 |
S2N_NO_ALERT(S2N_ERR_CRL_LOOKUP_FAILED); |
| 159 |
S2N_NO_ALERT(S2N_ERR_CRL_SIGNATURE); |
| 160 |
S2N_NO_ALERT(S2N_ERR_CRL_ISSUER); |
| 161 |
S2N_NO_ALERT(S2N_ERR_CRL_UNHANDLED_CRITICAL_EXTENSION); |
| 162 |
S2N_NO_ALERT(S2N_ERR_CRL_INVALID_THIS_UPDATE); |
| 163 |
S2N_NO_ALERT(S2N_ERR_CRL_INVALID_NEXT_UPDATE); |
| 164 |
S2N_NO_ALERT(S2N_ERR_CRL_NOT_YET_VALID); |
| 165 |
S2N_NO_ALERT(S2N_ERR_CRL_EXPIRED); |
| 166 |
S2N_NO_ALERT(S2N_ERR_INVALID_MAX_FRAG_LEN); |
| 167 |
S2N_NO_ALERT(S2N_ERR_MAX_FRAG_LEN_MISMATCH); |
| 168 |
S2N_NO_ALERT(S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED); |
| 169 |
S2N_NO_ALERT(S2N_ERR_BAD_KEY_SHARE); |
| 170 |
S2N_NO_ALERT(S2N_ERR_CANCELLED); |
| 171 |
S2N_NO_ALERT(S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED); |
| 172 |
S2N_NO_ALERT(S2N_ERR_MAX_INNER_PLAINTEXT_SIZE); |
| 173 |
S2N_NO_ALERT(S2N_ERR_RECORD_STUFFER_SIZE); |
| 174 |
S2N_NO_ALERT(S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE); |
| 175 |
S2N_NO_ALERT(S2N_ERR_FRAGMENT_LENGTH_TOO_SMALL); |
| 176 |
S2N_NO_ALERT(S2N_ERR_RECORD_STUFFER_NEEDS_DRAINING); |
| 177 |
S2N_NO_ALERT(S2N_ERR_UNSUPPORTED_EXTENSION); |
| 178 |
S2N_NO_ALERT(S2N_ERR_DUPLICATE_EXTENSION); |
| 179 |
S2N_NO_ALERT(S2N_ERR_MAX_EARLY_DATA_SIZE); |
| 180 |
S2N_NO_ALERT(S2N_ERR_EARLY_DATA_TRIAL_DECRYPT); |
| 181 |
} |
| 182 |
|
| 183 |
RESULT_BAIL(S2N_ERR_UNIMPLEMENTED); |
| 184 |
} |
| 185 |
|
| 186 |
static bool s2n_alerts_supported(struct s2n_connection *conn) |
| 187 |
{ |
| 188 |
|
| 189 |
|
| 190 |
return !s2n_connection_is_quic_enabled(conn); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
static bool s2n_process_as_warning(struct s2n_connection *conn, uint8_t level, uint8_t type) |
| 199 |
{ |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
if (s2n_connection_get_protocol_version(conn) < S2N_TLS13 || !conn->actual_protocol_version_established) { |
| 205 |
return level == S2N_TLS_ALERT_LEVEL_WARNING |
| 206 |
&& conn->config->alert_behavior == S2N_ALERT_IGNORE_WARNINGS; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
return type == S2N_TLS_ALERT_USER_CANCELED; |
| 213 |
} |
| 214 |
|
| 215 |
int s2n_error_get_alert(int error, uint8_t *alert) |
| 216 |
{ |
| 217 |
int error_type = s2n_error_get_type(error); |
| 218 |
|
| 219 |
POSIX_ENSURE_REF(alert); |
| 220 |
|
| 221 |
switch (error_type) { |
| 222 |
case S2N_ERR_T_OK: |
| 223 |
case S2N_ERR_T_CLOSED: |
| 224 |
case S2N_ERR_T_BLOCKED: |
| 225 |
case S2N_ERR_T_USAGE: |
| 226 |
case S2N_ERR_T_ALERT: |
| 227 |
POSIX_BAIL(S2N_ERR_NO_ALERT); |
| 228 |
break; |
| 229 |
case S2N_ERR_T_PROTO: |
| 230 |
POSIX_GUARD_RESULT(s2n_translate_protocol_error_to_alert(error, alert)); |
| 231 |
break; |
| 232 |
case S2N_ERR_T_IO: |
| 233 |
case S2N_ERR_T_INTERNAL: |
| 234 |
*alert = S2N_TLS_ALERT_INTERNAL_ERROR; |
| 235 |
break; |
| 236 |
} |
| 237 |
|
| 238 |
return S2N_SUCCESS; |
| 239 |
} |
| 240 |
|
| 241 |
int s2n_process_alert_fragment(struct s2n_connection *conn) |
| 242 |
{ |
| 243 |
POSIX_ENSURE_REF(conn); |
| 244 |
S2N_ERROR_IF(s2n_stuffer_data_available(&conn->in) == 0, S2N_ERR_BAD_MESSAGE); |
| 245 |
S2N_ERROR_IF(s2n_stuffer_data_available(&conn->alert_in) == 2, S2N_ERR_ALERT_PRESENT); |
| 246 |
POSIX_ENSURE(s2n_alerts_supported(conn), S2N_ERR_BAD_MESSAGE); |
| 247 |
|
| 248 |
while (s2n_stuffer_data_available(&conn->in)) { |
| 249 |
uint8_t bytes_required = 2; |
| 250 |
|
| 251 |
|
| 252 |
if (s2n_stuffer_data_available(&conn->alert_in) == 1) { |
| 253 |
bytes_required = 1; |
| 254 |
} |
| 255 |
|
| 256 |
int bytes_to_read = S2N_MIN(bytes_required, s2n_stuffer_data_available(&conn->in)); |
| 257 |
|
| 258 |
POSIX_GUARD(s2n_stuffer_copy(&conn->in, &conn->alert_in, bytes_to_read)); |
| 259 |
|
| 260 |
if (s2n_stuffer_data_available(&conn->alert_in) == 2) { |
| 261 |
|
| 262 |
if (conn->alert_in_data[1] == S2N_TLS_ALERT_CLOSE_NOTIFY) { |
| 263 |
s2n_atomic_flag_set(&conn->read_closed); |
| 264 |
s2n_atomic_flag_set(&conn->close_notify_received); |
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
if (s2n_process_as_warning(conn, conn->alert_in_data[0], conn->alert_in_data[1])) { |
| 270 |
POSIX_GUARD(s2n_stuffer_wipe(&conn->alert_in)); |
| 271 |
return 0; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
if (s2n_allowed_to_cache_connection(conn) && conn->session_id_len) { |
| 276 |
conn->config->cache_delete(conn, conn->config->cache_delete_data, conn->session_id, conn->session_id_len); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
POSIX_GUARD_RESULT(s2n_connection_set_closed(conn)); |
| 285 |
s2n_atomic_flag_set(&conn->error_alert_received); |
| 286 |
POSIX_BAIL(S2N_ERR_ALERT); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
return 0; |
| 291 |
} |
| 292 |
|
| 293 |
static S2N_RESULT s2n_queue_reader_alert(struct s2n_connection *conn, s2n_tls_alert_code code) |
| 294 |
{ |
| 295 |
RESULT_ENSURE_REF(conn); |
| 296 |
if (!conn->reader_alert_out) { |
| 297 |
conn->reader_alert_out = code; |
| 298 |
} |
| 299 |
return S2N_RESULT_OK; |
| 300 |
} |
| 301 |
|
| 302 |
int s2n_queue_reader_unsupported_protocol_version_alert(struct s2n_connection *conn) |
| 303 |
{ |
| 304 |
POSIX_GUARD_RESULT(s2n_queue_reader_alert(conn, S2N_TLS_ALERT_PROTOCOL_VERSION)); |
| 305 |
return S2N_SUCCESS; |
| 306 |
} |
| 307 |
|
| 308 |
int s2n_queue_reader_handshake_failure_alert(struct s2n_connection *conn) |
| 309 |
{ |
| 310 |
POSIX_GUARD_RESULT(s2n_queue_reader_alert(conn, S2N_TLS_ALERT_HANDSHAKE_FAILURE)); |
| 311 |
return S2N_SUCCESS; |
| 312 |
} |
| 313 |
|
| 314 |
S2N_RESULT s2n_queue_reader_no_renegotiation_alert(struct s2n_connection *conn) |
| 315 |
{ |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
if (s2n_connection_get_protocol_version(conn) == S2N_SSLv3) { |
| 324 |
RESULT_GUARD_POSIX(s2n_queue_reader_handshake_failure_alert(conn)); |
| 325 |
RESULT_BAIL(S2N_ERR_BAD_MESSAGE); |
| 326 |
} |
| 327 |
|
| 328 |
if (!conn->reader_warning_out) { |
| 329 |
conn->reader_warning_out = S2N_TLS_ALERT_NO_RENEGOTIATION; |
| 330 |
} |
| 331 |
return S2N_RESULT_OK; |
| 332 |
} |
| 333 |
|
| 334 |
S2N_RESULT s2n_alerts_write_error_or_close_notify(struct s2n_connection *conn) |
| 335 |
{ |
| 336 |
if (!s2n_alerts_supported(conn)) { |
| 337 |
return S2N_RESULT_OK; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
uint8_t code = S2N_TLS_ALERT_CLOSE_NOTIFY; |
| 354 |
uint8_t level = S2N_TLS_ALERT_LEVEL_WARNING; |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
if (conn->writer_alert_out) { |
| 361 |
code = conn->writer_alert_out; |
| 362 |
level = S2N_TLS_ALERT_LEVEL_FATAL; |
| 363 |
} else if (conn->reader_alert_out) { |
| 364 |
code = conn->reader_alert_out; |
| 365 |
level = S2N_TLS_ALERT_LEVEL_FATAL; |
| 366 |
} |
| 367 |
|
| 368 |
struct s2n_blob alert = { 0 }; |
| 369 |
uint8_t alert_bytes[] = { level, code }; |
| 370 |
RESULT_GUARD_POSIX(s2n_blob_init(&alert, alert_bytes, sizeof(alert_bytes))); |
| 371 |
|
| 372 |
RESULT_GUARD(s2n_record_write(conn, TLS_ALERT, &alert)); |
| 373 |
conn->alert_sent = true; |
| 374 |
return S2N_RESULT_OK; |
| 375 |
} |
| 376 |
|
| 377 |
S2N_RESULT s2n_alerts_write_warning(struct s2n_connection *conn) |
| 378 |
{ |
| 379 |
if (!s2n_alerts_supported(conn)) { |
| 380 |
return S2N_RESULT_OK; |
| 381 |
} |
| 382 |
|
| 383 |
uint8_t code = conn->reader_warning_out; |
| 384 |
uint8_t level = S2N_TLS_ALERT_LEVEL_WARNING; |
| 385 |
|
| 386 |
struct s2n_blob alert = { 0 }; |
| 387 |
uint8_t alert_bytes[] = { level, code }; |
| 388 |
RESULT_GUARD_POSIX(s2n_blob_init(&alert, alert_bytes, sizeof(alert_bytes))); |
| 389 |
|
| 390 |
RESULT_GUARD(s2n_record_write(conn, TLS_ALERT, &alert)); |
| 391 |
return S2N_RESULT_OK; |
| 392 |
} |