s2n_alerts.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include "tls/s2n_alerts.h"
  16. #include <stdint.h>
  17. #include <sys/param.h>
  18. #include "error/s2n_errno.h"
  19. #include "tls/s2n_connection.h"
  20. #include "tls/s2n_record.h"
  21. #include "tls/s2n_resume.h"
  22. #include "tls/s2n_tls_parameters.h"
  23. #include "utils/s2n_atomic.h"
  24. #include "utils/s2n_blob.h"
  25. #include "utils/s2n_safety.h"
  26. #define S2N_ALERT_CASE(error, alert_code) \
  27. case (error): \
  28. *alert = (alert_code); \
  29. return S2N_RESULT_OK
  30. #define S2N_NO_ALERT(error) \
  31. case (error): \
  32. RESULT_BAIL(S2N_ERR_NO_ALERT)
  33. static S2N_RESULT s2n_translate_protocol_error_to_alert(int error_code, uint8_t *alert)
  34. {
  35. RESULT_ENSURE_REF(alert);
  36. switch (error_code) {
  37. S2N_ALERT_CASE(S2N_ERR_MISSING_EXTENSION, S2N_TLS_ALERT_MISSING_EXTENSION);
  38. /* TODO: The ERR_BAD_MESSAGE -> ALERT_UNEXPECTED_MESSAGE mapping
  39. * isn't always correct. Sometimes s2n-tls uses ERR_BAD_MESSAGE
  40. * to indicate S2N_TLS_ALERT_ILLEGAL_PARAMETER instead.
  41. * We'll want to add a new error to distinguish between the two usages:
  42. * our errors should be equally or more specific than alerts, not less.
  43. */
  44. S2N_ALERT_CASE(S2N_ERR_BAD_MESSAGE, S2N_TLS_ALERT_UNEXPECTED_MESSAGE);
  45. /* For errors involving secure renegotiation:
  46. *= https://tools.ietf.org/rfc/rfc5746#3.4
  47. *# Note: later in Section 3, "abort the handshake" is used as
  48. *# shorthand for "send a fatal handshake_failure alert and
  49. *# terminate the connection".
  50. */
  51. S2N_ALERT_CASE(S2N_ERR_NO_RENEGOTIATION, S2N_TLS_ALERT_HANDSHAKE_FAILURE);
  52. /* TODO: Add mappings for other protocol errors.
  53. */
  54. S2N_NO_ALERT(S2N_ERR_ENCRYPT);
  55. S2N_NO_ALERT(S2N_ERR_DECRYPT);
  56. S2N_NO_ALERT(S2N_ERR_KEY_INIT);
  57. S2N_NO_ALERT(S2N_ERR_KEY_DESTROY);
  58. S2N_NO_ALERT(S2N_ERR_DH_SERIALIZING);
  59. S2N_NO_ALERT(S2N_ERR_DH_SHARED_SECRET);
  60. S2N_NO_ALERT(S2N_ERR_DH_WRITING_PUBLIC_KEY);
  61. S2N_NO_ALERT(S2N_ERR_DH_FAILED_SIGNING);
  62. S2N_NO_ALERT(S2N_ERR_DH_COPYING_PARAMETERS);
  63. S2N_NO_ALERT(S2N_ERR_DH_GENERATING_PARAMETERS);
  64. S2N_NO_ALERT(S2N_ERR_CIPHER_NOT_SUPPORTED);
  65. S2N_NO_ALERT(S2N_ERR_NO_APPLICATION_PROTOCOL);
  66. S2N_NO_ALERT(S2N_ERR_FALLBACK_DETECTED);
  67. S2N_NO_ALERT(S2N_ERR_HASH_DIGEST_FAILED);
  68. S2N_NO_ALERT(S2N_ERR_HASH_INIT_FAILED);
  69. S2N_NO_ALERT(S2N_ERR_HASH_UPDATE_FAILED);
  70. S2N_NO_ALERT(S2N_ERR_HASH_COPY_FAILED);
  71. S2N_NO_ALERT(S2N_ERR_HASH_WIPE_FAILED);
  72. S2N_NO_ALERT(S2N_ERR_HASH_NOT_READY);
  73. S2N_NO_ALERT(S2N_ERR_ALLOW_MD5_FOR_FIPS_FAILED);
  74. S2N_NO_ALERT(S2N_ERR_DECODE_CERTIFICATE);
  75. S2N_NO_ALERT(S2N_ERR_DECODE_PRIVATE_KEY);
  76. S2N_NO_ALERT(S2N_ERR_INVALID_HELLO_RETRY);
  77. S2N_NO_ALERT(S2N_ERR_INVALID_SIGNATURE_ALGORITHM);
  78. S2N_NO_ALERT(S2N_ERR_INVALID_SIGNATURE_SCHEME);
  79. S2N_NO_ALERT(S2N_ERR_CBC_VERIFY);
  80. S2N_NO_ALERT(S2N_ERR_DH_COPYING_PUBLIC_KEY);
  81. S2N_NO_ALERT(S2N_ERR_SIGN);
  82. S2N_NO_ALERT(S2N_ERR_VERIFY_SIGNATURE);
  83. S2N_NO_ALERT(S2N_ERR_ECDHE_GEN_KEY);
  84. S2N_NO_ALERT(S2N_ERR_ECDHE_SHARED_SECRET);
  85. S2N_NO_ALERT(S2N_ERR_ECDHE_UNSUPPORTED_CURVE);
  86. S2N_NO_ALERT(S2N_ERR_ECDSA_UNSUPPORTED_CURVE);
  87. S2N_NO_ALERT(S2N_ERR_ECDHE_SERIALIZING);
  88. S2N_NO_ALERT(S2N_ERR_KEM_UNSUPPORTED_PARAMS);
  89. S2N_NO_ALERT(S2N_ERR_SHUTDOWN_RECORD_TYPE);
  90. S2N_NO_ALERT(S2N_ERR_SHUTDOWN_CLOSED);
  91. S2N_NO_ALERT(S2N_ERR_NON_EMPTY_RENEGOTIATION_INFO);
  92. S2N_NO_ALERT(S2N_ERR_RECORD_LIMIT);
  93. S2N_NO_ALERT(S2N_ERR_CERT_UNTRUSTED);
  94. S2N_NO_ALERT(S2N_ERR_CERT_REVOKED);
  95. S2N_NO_ALERT(S2N_ERR_CERT_NOT_YET_VALID);
  96. S2N_NO_ALERT(S2N_ERR_CERT_EXPIRED);
  97. S2N_NO_ALERT(S2N_ERR_CERT_TYPE_UNSUPPORTED);
  98. S2N_NO_ALERT(S2N_ERR_CERT_INVALID);
  99. S2N_NO_ALERT(S2N_ERR_CERT_MAX_CHAIN_DEPTH_EXCEEDED);
  100. S2N_NO_ALERT(S2N_ERR_CERT_REJECTED);
  101. S2N_NO_ALERT(S2N_ERR_CRL_LOOKUP_FAILED);
  102. S2N_NO_ALERT(S2N_ERR_CRL_SIGNATURE);
  103. S2N_NO_ALERT(S2N_ERR_CRL_ISSUER);
  104. S2N_NO_ALERT(S2N_ERR_CRL_UNHANDLED_CRITICAL_EXTENSION);
  105. S2N_NO_ALERT(S2N_ERR_CRL_INVALID_THIS_UPDATE);
  106. S2N_NO_ALERT(S2N_ERR_CRL_INVALID_NEXT_UPDATE);
  107. S2N_NO_ALERT(S2N_ERR_CRL_NOT_YET_VALID);
  108. S2N_NO_ALERT(S2N_ERR_CRL_EXPIRED);
  109. S2N_NO_ALERT(S2N_ERR_INVALID_MAX_FRAG_LEN);
  110. S2N_NO_ALERT(S2N_ERR_MAX_FRAG_LEN_MISMATCH);
  111. S2N_NO_ALERT(S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED);
  112. S2N_NO_ALERT(S2N_ERR_BAD_KEY_SHARE);
  113. S2N_NO_ALERT(S2N_ERR_CANCELLED);
  114. S2N_NO_ALERT(S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED);
  115. S2N_NO_ALERT(S2N_ERR_MAX_INNER_PLAINTEXT_SIZE);
  116. S2N_NO_ALERT(S2N_ERR_RECORD_STUFFER_SIZE);
  117. S2N_NO_ALERT(S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE);
  118. S2N_NO_ALERT(S2N_ERR_FRAGMENT_LENGTH_TOO_SMALL);
  119. S2N_NO_ALERT(S2N_ERR_RECORD_STUFFER_NEEDS_DRAINING);
  120. S2N_NO_ALERT(S2N_ERR_UNSUPPORTED_EXTENSION);
  121. S2N_NO_ALERT(S2N_ERR_DUPLICATE_EXTENSION);
  122. S2N_NO_ALERT(S2N_ERR_MAX_EARLY_DATA_SIZE);
  123. S2N_NO_ALERT(S2N_ERR_EARLY_DATA_TRIAL_DECRYPT);
  124. }
  125. RESULT_BAIL(S2N_ERR_UNIMPLEMENTED);
  126. }
  127. static bool s2n_alerts_supported(struct s2n_connection *conn)
  128. {
  129. /* If running in QUIC mode, QUIC handles alerting.
  130. * S2N should not send or receive alerts. */
  131. return !s2n_connection_is_quic_enabled(conn);
  132. }
  133. /* In TLS1.3 all Alerts
  134. *= https://tools.ietf.org/rfc/rfc8446#section-6
  135. *# MUST be treated as error alerts when received
  136. *# regardless of the AlertLevel in the message.
  137. */
  138. static bool s2n_process_as_warning(struct s2n_connection *conn, uint8_t level, uint8_t type)
  139. {
  140. /* Only TLS1.2 considers the alert level. The alert level field is
  141. * considered deprecated in TLS1.3. */
  142. if (s2n_connection_get_protocol_version(conn) < S2N_TLS13) {
  143. return level == S2N_TLS_ALERT_LEVEL_WARNING
  144. && conn->config->alert_behavior == S2N_ALERT_IGNORE_WARNINGS;
  145. }
  146. /* user_canceled is the only alert currently treated as a warning in TLS1.3.
  147. * We need to treat it as a warning regardless of alert_behavior to avoid marking
  148. * correctly-closed connections as failed. */
  149. return type == S2N_TLS_ALERT_USER_CANCELED;
  150. }
  151. int s2n_error_get_alert(int error, uint8_t *alert)
  152. {
  153. int error_type = s2n_error_get_type(error);
  154. POSIX_ENSURE_REF(alert);
  155. switch (error_type) {
  156. case S2N_ERR_T_OK:
  157. case S2N_ERR_T_CLOSED:
  158. case S2N_ERR_T_BLOCKED:
  159. case S2N_ERR_T_USAGE:
  160. case S2N_ERR_T_ALERT:
  161. POSIX_BAIL(S2N_ERR_NO_ALERT);
  162. break;
  163. case S2N_ERR_T_PROTO:
  164. POSIX_GUARD_RESULT(s2n_translate_protocol_error_to_alert(error, alert));
  165. break;
  166. case S2N_ERR_T_IO:
  167. case S2N_ERR_T_INTERNAL:
  168. *alert = S2N_TLS_ALERT_INTERNAL_ERROR;
  169. break;
  170. }
  171. return S2N_SUCCESS;
  172. }
  173. int s2n_process_alert_fragment(struct s2n_connection *conn)
  174. {
  175. POSIX_ENSURE_REF(conn);
  176. S2N_ERROR_IF(s2n_stuffer_data_available(&conn->in) == 0, S2N_ERR_BAD_MESSAGE);
  177. S2N_ERROR_IF(s2n_stuffer_data_available(&conn->alert_in) == 2, S2N_ERR_ALERT_PRESENT);
  178. POSIX_ENSURE(s2n_alerts_supported(conn), S2N_ERR_BAD_MESSAGE);
  179. while (s2n_stuffer_data_available(&conn->in)) {
  180. uint8_t bytes_required = 2;
  181. /* Alerts are two bytes long, but can still be fragmented or coalesced */
  182. if (s2n_stuffer_data_available(&conn->alert_in) == 1) {
  183. bytes_required = 1;
  184. }
  185. int bytes_to_read = MIN(bytes_required, s2n_stuffer_data_available(&conn->in));
  186. POSIX_GUARD(s2n_stuffer_copy(&conn->in, &conn->alert_in, bytes_to_read));
  187. if (s2n_stuffer_data_available(&conn->alert_in) == 2) {
  188. /* Close notifications are handled as shutdowns */
  189. if (conn->alert_in_data[1] == S2N_TLS_ALERT_CLOSE_NOTIFY) {
  190. s2n_atomic_flag_set(&conn->read_closed);
  191. s2n_atomic_flag_set(&conn->close_notify_received);
  192. return 0;
  193. }
  194. /* Ignore warning-level alerts if we're in warning-tolerant mode */
  195. if (s2n_process_as_warning(conn, conn->alert_in_data[0], conn->alert_in_data[1])) {
  196. POSIX_GUARD(s2n_stuffer_wipe(&conn->alert_in));
  197. return 0;
  198. }
  199. /* RFC 5077 5.1 - Expire any cached session on an error alert */
  200. if (s2n_allowed_to_cache_connection(conn) && conn->session_id_len) {
  201. conn->config->cache_delete(conn, conn->config->cache_delete_data, conn->session_id, conn->session_id_len);
  202. }
  203. /* All other alerts are treated as fatal errors.
  204. *
  205. *= https://tools.ietf.org/rfc/rfc8446#section-6
  206. *# Unknown Alert types MUST be treated as error alerts.
  207. */
  208. POSIX_GUARD_RESULT(s2n_connection_set_closed(conn));
  209. s2n_atomic_flag_set(&conn->error_alert_received);
  210. POSIX_BAIL(S2N_ERR_ALERT);
  211. }
  212. }
  213. return 0;
  214. }
  215. static S2N_RESULT s2n_queue_reader_alert(struct s2n_connection *conn, s2n_tls_alert_code code)
  216. {
  217. RESULT_ENSURE_REF(conn);
  218. if (!conn->reader_alert_out) {
  219. conn->reader_alert_out = code;
  220. }
  221. return S2N_RESULT_OK;
  222. }
  223. int s2n_queue_reader_unsupported_protocol_version_alert(struct s2n_connection *conn)
  224. {
  225. POSIX_GUARD_RESULT(s2n_queue_reader_alert(conn, S2N_TLS_ALERT_PROTOCOL_VERSION));
  226. return S2N_SUCCESS;
  227. }
  228. int s2n_queue_reader_handshake_failure_alert(struct s2n_connection *conn)
  229. {
  230. POSIX_GUARD_RESULT(s2n_queue_reader_alert(conn, S2N_TLS_ALERT_HANDSHAKE_FAILURE));
  231. return S2N_SUCCESS;
  232. }
  233. S2N_RESULT s2n_queue_reader_no_renegotiation_alert(struct s2n_connection *conn)
  234. {
  235. /**
  236. *= https://tools.ietf.org/rfc/rfc5746#4.5
  237. *# SSLv3 does not define the "no_renegotiation" alert (and does
  238. *# not offer a way to indicate a refusal to renegotiate at a "warning"
  239. *# level). SSLv3 clients that refuse renegotiation SHOULD use a fatal
  240. *# handshake_failure alert.
  241. **/
  242. if (s2n_connection_get_protocol_version(conn) == S2N_SSLv3) {
  243. RESULT_GUARD_POSIX(s2n_queue_reader_handshake_failure_alert(conn));
  244. RESULT_BAIL(S2N_ERR_BAD_MESSAGE);
  245. }
  246. if (!conn->reader_warning_out) {
  247. conn->reader_warning_out = S2N_TLS_ALERT_NO_RENEGOTIATION;
  248. }
  249. return S2N_RESULT_OK;
  250. }
  251. S2N_RESULT s2n_alerts_write_error_or_close_notify(struct s2n_connection *conn)
  252. {
  253. if (!s2n_alerts_supported(conn)) {
  254. return S2N_RESULT_OK;
  255. }
  256. /*
  257. *= https://tools.ietf.org/rfc/rfc8446#section-6.2
  258. *= type=exception
  259. *= reason=Specific alerts could expose a side-channel attack vector.
  260. *# The phrases "terminate the connection with an X
  261. *# alert" and "abort the handshake with an X alert" mean that the
  262. *# implementation MUST send alert X if it sends any alert.
  263. *
  264. * By default, s2n-tls sends a generic close_notify alert, even in
  265. * response to fatal errors. This is done to avoid potential
  266. * side-channel attacks since specific alerts could reveal information
  267. * about why the error occured.
  268. */
  269. uint8_t code = S2N_TLS_ALERT_CLOSE_NOTIFY;
  270. uint8_t level = S2N_TLS_ALERT_LEVEL_WARNING;
  271. /* s2n-tls sends a very small subset of more specific error alerts.
  272. * Since either the reader or the writer can produce one of these alerts,
  273. * but only a single alert can be reported, we prioritize writer alerts.
  274. */
  275. if (conn->writer_alert_out) {
  276. code = conn->writer_alert_out;
  277. level = S2N_TLS_ALERT_LEVEL_FATAL;
  278. } else if (conn->reader_alert_out) {
  279. code = conn->reader_alert_out;
  280. level = S2N_TLS_ALERT_LEVEL_FATAL;
  281. }
  282. struct s2n_blob alert = { 0 };
  283. uint8_t alert_bytes[] = { level, code };
  284. RESULT_GUARD_POSIX(s2n_blob_init(&alert, alert_bytes, sizeof(alert_bytes)));
  285. RESULT_GUARD(s2n_record_write(conn, TLS_ALERT, &alert));
  286. conn->alert_sent = true;
  287. return S2N_RESULT_OK;
  288. }
  289. S2N_RESULT s2n_alerts_write_warning(struct s2n_connection *conn)
  290. {
  291. if (!s2n_alerts_supported(conn)) {
  292. return S2N_RESULT_OK;
  293. }
  294. uint8_t code = conn->reader_warning_out;
  295. uint8_t level = S2N_TLS_ALERT_LEVEL_WARNING;
  296. struct s2n_blob alert = { 0 };
  297. uint8_t alert_bytes[] = { level, code };
  298. RESULT_GUARD_POSIX(s2n_blob_init(&alert, alert_bytes, sizeof(alert_bytes)));
  299. RESULT_GUARD(s2n_record_write(conn, TLS_ALERT, &alert));
  300. return S2N_RESULT_OK;
  301. }