s2n_errno.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. #pragma once
  16. #include <stdbool.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "api/s2n.h"
  20. #include "utils/s2n_ensure.h"
  21. /*
  22. * To easily retrieve error types, we split error values into two parts.
  23. * The upper 6 bits describe the error type and the lower bits describe the value within the category.
  24. * [ Error Type Bits(31-26) ][ Value Bits(25-0) ]
  25. */
  26. #define S2N_ERR_NUM_VALUE_BITS 26
  27. /* Start value for each error type. */
  28. #define S2N_ERR_T_OK_START (S2N_ERR_T_OK << S2N_ERR_NUM_VALUE_BITS)
  29. #define S2N_ERR_T_IO_START (S2N_ERR_T_IO << S2N_ERR_NUM_VALUE_BITS)
  30. #define S2N_ERR_T_CLOSED_START (S2N_ERR_T_CLOSED << S2N_ERR_NUM_VALUE_BITS)
  31. #define S2N_ERR_T_BLOCKED_START (S2N_ERR_T_BLOCKED << S2N_ERR_NUM_VALUE_BITS)
  32. #define S2N_ERR_T_ALERT_START (S2N_ERR_T_ALERT << S2N_ERR_NUM_VALUE_BITS)
  33. #define S2N_ERR_T_PROTO_START (S2N_ERR_T_PROTO << S2N_ERR_NUM_VALUE_BITS)
  34. #define S2N_ERR_T_INTERNAL_START (S2N_ERR_T_INTERNAL << S2N_ERR_NUM_VALUE_BITS)
  35. #define S2N_ERR_T_USAGE_START (S2N_ERR_T_USAGE << S2N_ERR_NUM_VALUE_BITS)
  36. /* Order of values in this enum is important. New error values should be placed at the end of their respective category.
  37. * For example, a new TLS protocol related error belongs in the S2N_ERR_T_PROTO category. It should be placed
  38. * immediately before S2N_ERR_T_INTERNAL_START(the first value of he next category).
  39. */
  40. typedef enum {
  41. /* S2N_ERR_T_OK */
  42. S2N_ERR_OK = S2N_ERR_T_OK_START,
  43. S2N_ERR_T_OK_END,
  44. /* S2N_ERR_T_IO */
  45. S2N_ERR_IO = S2N_ERR_T_IO_START,
  46. S2N_ERR_T_IO_END,
  47. /* S2N_ERR_T_CLOSED */
  48. S2N_ERR_CLOSED = S2N_ERR_T_CLOSED_START,
  49. S2N_ERR_T_CLOSED_END,
  50. /* S2N_ERR_T_BLOCKED */
  51. S2N_ERR_IO_BLOCKED = S2N_ERR_T_BLOCKED_START,
  52. S2N_ERR_ASYNC_BLOCKED,
  53. S2N_ERR_EARLY_DATA_BLOCKED,
  54. S2N_ERR_APP_DATA_BLOCKED,
  55. S2N_ERR_T_BLOCKED_END,
  56. /* S2N_ERR_T_ALERT */
  57. S2N_ERR_ALERT = S2N_ERR_T_ALERT_START,
  58. S2N_ERR_T_ALERT_END,
  59. /* S2N_ERR_T_PROTO */
  60. S2N_ERR_ENCRYPT = S2N_ERR_T_PROTO_START,
  61. S2N_ERR_DECRYPT,
  62. S2N_ERR_BAD_MESSAGE,
  63. S2N_ERR_KEY_INIT,
  64. S2N_ERR_KEY_DESTROY,
  65. S2N_ERR_DH_SERIALIZING,
  66. S2N_ERR_DH_SHARED_SECRET,
  67. S2N_ERR_DH_WRITING_PUBLIC_KEY,
  68. S2N_ERR_DH_FAILED_SIGNING,
  69. S2N_ERR_DH_COPYING_PARAMETERS,
  70. S2N_ERR_DH_GENERATING_PARAMETERS,
  71. S2N_ERR_CIPHER_NOT_SUPPORTED,
  72. S2N_ERR_NO_APPLICATION_PROTOCOL,
  73. S2N_ERR_FALLBACK_DETECTED,
  74. S2N_ERR_HASH_DIGEST_FAILED,
  75. S2N_ERR_HASH_INIT_FAILED,
  76. S2N_ERR_HASH_UPDATE_FAILED,
  77. S2N_ERR_HASH_COPY_FAILED,
  78. S2N_ERR_HASH_WIPE_FAILED,
  79. S2N_ERR_HASH_NOT_READY,
  80. S2N_ERR_ALLOW_MD5_FOR_FIPS_FAILED,
  81. S2N_ERR_DECODE_CERTIFICATE,
  82. S2N_ERR_DECODE_PRIVATE_KEY,
  83. S2N_ERR_INVALID_HELLO_RETRY,
  84. S2N_ERR_INVALID_SIGNATURE_ALGORITHM,
  85. S2N_ERR_INVALID_SIGNATURE_SCHEME,
  86. S2N_ERR_CBC_VERIFY,
  87. S2N_ERR_DH_COPYING_PUBLIC_KEY,
  88. S2N_ERR_SIGN,
  89. S2N_ERR_VERIFY_SIGNATURE,
  90. S2N_ERR_ECDHE_GEN_KEY,
  91. S2N_ERR_ECDHE_SHARED_SECRET,
  92. S2N_ERR_ECDHE_UNSUPPORTED_CURVE,
  93. S2N_ERR_ECDSA_UNSUPPORTED_CURVE,
  94. S2N_ERR_ECDHE_SERIALIZING,
  95. S2N_ERR_KEM_UNSUPPORTED_PARAMS,
  96. S2N_ERR_SHUTDOWN_RECORD_TYPE,
  97. S2N_ERR_SHUTDOWN_CLOSED,
  98. S2N_ERR_NON_EMPTY_RENEGOTIATION_INFO,
  99. S2N_ERR_RECORD_LIMIT,
  100. S2N_ERR_CERT_UNTRUSTED,
  101. S2N_ERR_CERT_REVOKED,
  102. S2N_ERR_CERT_NOT_YET_VALID,
  103. S2N_ERR_CERT_EXPIRED,
  104. S2N_ERR_CERT_TYPE_UNSUPPORTED,
  105. S2N_ERR_CERT_INVALID,
  106. S2N_ERR_CERT_MAX_CHAIN_DEPTH_EXCEEDED,
  107. S2N_ERR_CERT_REJECTED,
  108. S2N_ERR_CRL_LOOKUP_FAILED,
  109. S2N_ERR_CRL_SIGNATURE,
  110. S2N_ERR_CRL_ISSUER,
  111. S2N_ERR_CRL_UNHANDLED_CRITICAL_EXTENSION,
  112. S2N_ERR_CRL_INVALID_THIS_UPDATE,
  113. S2N_ERR_CRL_INVALID_NEXT_UPDATE,
  114. S2N_ERR_CRL_NOT_YET_VALID,
  115. S2N_ERR_CRL_EXPIRED,
  116. S2N_ERR_INVALID_MAX_FRAG_LEN,
  117. S2N_ERR_MAX_FRAG_LEN_MISMATCH,
  118. S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED,
  119. S2N_ERR_BAD_KEY_SHARE,
  120. S2N_ERR_CANCELLED,
  121. S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED,
  122. S2N_ERR_MAX_INNER_PLAINTEXT_SIZE,
  123. S2N_ERR_RECORD_STUFFER_SIZE,
  124. S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE,
  125. S2N_ERR_FRAGMENT_LENGTH_TOO_SMALL,
  126. S2N_ERR_RECORD_STUFFER_NEEDS_DRAINING,
  127. S2N_ERR_MISSING_EXTENSION,
  128. S2N_ERR_UNSUPPORTED_EXTENSION,
  129. S2N_ERR_DUPLICATE_EXTENSION,
  130. S2N_ERR_MAX_EARLY_DATA_SIZE,
  131. S2N_ERR_EARLY_DATA_TRIAL_DECRYPT,
  132. S2N_ERR_NO_RENEGOTIATION,
  133. S2N_ERR_T_PROTO_END,
  134. /* S2N_ERR_T_INTERNAL */
  135. S2N_ERR_MADVISE = S2N_ERR_T_INTERNAL_START,
  136. S2N_ERR_ALLOC,
  137. S2N_ERR_MLOCK,
  138. S2N_ERR_MUNLOCK,
  139. S2N_ERR_FSTAT,
  140. S2N_ERR_OPEN,
  141. S2N_ERR_MMAP,
  142. S2N_ERR_ATEXIT,
  143. S2N_ERR_NOMEM,
  144. S2N_ERR_NULL,
  145. S2N_ERR_SAFETY,
  146. S2N_ERR_INITIALIZED,
  147. S2N_ERR_NOT_INITIALIZED,
  148. S2N_ERR_RANDOM_UNINITIALIZED,
  149. S2N_ERR_OPEN_RANDOM,
  150. S2N_ERR_RESIZE_STATIC_STUFFER,
  151. S2N_ERR_RESIZE_TAINTED_STUFFER,
  152. S2N_ERR_STUFFER_OUT_OF_DATA,
  153. S2N_ERR_STUFFER_IS_FULL,
  154. S2N_ERR_STUFFER_NOT_FOUND,
  155. S2N_ERR_STUFFER_HAS_UNPROCESSED_DATA,
  156. S2N_ERR_HASH_INVALID_ALGORITHM,
  157. S2N_ERR_PRF_INVALID_ALGORITHM,
  158. S2N_ERR_PRF_INVALID_SEED,
  159. S2N_ERR_PRF_DERIVE,
  160. S2N_ERR_P_HASH_INVALID_ALGORITHM,
  161. S2N_ERR_P_HASH_INIT_FAILED,
  162. S2N_ERR_P_HASH_UPDATE_FAILED,
  163. S2N_ERR_P_HASH_FINAL_FAILED,
  164. S2N_ERR_P_HASH_WIPE_FAILED,
  165. S2N_ERR_HMAC_INVALID_ALGORITHM,
  166. S2N_ERR_HKDF_OUTPUT_SIZE,
  167. S2N_ERR_HKDF,
  168. S2N_ERR_ALERT_PRESENT,
  169. S2N_ERR_HANDSHAKE_STATE,
  170. S2N_ERR_SHUTDOWN_PAUSED,
  171. S2N_ERR_SIZE_MISMATCH,
  172. S2N_ERR_DRBG,
  173. S2N_ERR_DRBG_REQUEST_SIZE,
  174. S2N_ERR_KEY_CHECK,
  175. S2N_ERR_CIPHER_TYPE,
  176. S2N_ERR_MAP_DUPLICATE,
  177. S2N_ERR_MAP_IMMUTABLE,
  178. S2N_ERR_MAP_MUTABLE,
  179. S2N_ERR_MAP_INVALID_MAP_SIZE,
  180. S2N_ERR_INITIAL_HMAC,
  181. S2N_ERR_INVALID_NONCE_TYPE,
  182. S2N_ERR_UNIMPLEMENTED,
  183. S2N_ERR_HANDSHAKE_UNREACHABLE,
  184. S2N_ERR_READ,
  185. S2N_ERR_WRITE,
  186. S2N_ERR_BAD_FD,
  187. S2N_ERR_RDRAND_FAILED,
  188. S2N_ERR_FAILED_CACHE_RETRIEVAL,
  189. S2N_ERR_X509_TRUST_STORE,
  190. S2N_ERR_UNKNOWN_PROTOCOL_VERSION,
  191. S2N_ERR_NULL_CN_NAME,
  192. S2N_ERR_NULL_SANS,
  193. S2N_ERR_CLIENT_HELLO_VERSION,
  194. S2N_ERR_CLIENT_PROTOCOL_VERSION,
  195. S2N_ERR_SERVER_PROTOCOL_VERSION,
  196. S2N_ERR_ACTUAL_PROTOCOL_VERSION,
  197. S2N_ERR_POLLING_FROM_SOCKET,
  198. S2N_ERR_RECV_STUFFER_FROM_CONN,
  199. S2N_ERR_SEND_STUFFER_TO_CONN,
  200. S2N_ERR_PRECONDITION_VIOLATION,
  201. S2N_ERR_POSTCONDITION_VIOLATION,
  202. S2N_ERR_INTEGER_OVERFLOW,
  203. S2N_ERR_ARRAY_INDEX_OOB,
  204. S2N_ERR_FREE_STATIC_BLOB,
  205. S2N_ERR_RESIZE_STATIC_BLOB,
  206. S2N_ERR_NO_SUPPORTED_LIBCRYPTO_API,
  207. S2N_ERR_RECORD_LENGTH_TOO_LARGE,
  208. S2N_ERR_SET_DUPLICATE_VALUE,
  209. S2N_ERR_INVALID_PARSED_EXTENSIONS,
  210. S2N_ERR_ASYNC_CALLBACK_FAILED,
  211. S2N_ERR_ASYNC_MORE_THAN_ONE,
  212. S2N_ERR_PQ_CRYPTO,
  213. S2N_ERR_PQ_DISABLED,
  214. S2N_ERR_INVALID_CERT_STATE,
  215. S2N_ERR_INVALID_EARLY_DATA_STATE,
  216. S2N_ERR_PKEY_CTX_INIT,
  217. S2N_ERR_FORK_DETECTION_INIT,
  218. S2N_ERR_RETRIEVE_FORK_GENERATION_NUMBER,
  219. S2N_ERR_LIBCRYPTO_VERSION_NUMBER_MISMATCH,
  220. S2N_ERR_LIBCRYPTO_VERSION_NAME_MISMATCH,
  221. S2N_ERR_OSSL_PROVIDER,
  222. S2N_ERR_TEST_ASSERTION,
  223. S2N_ERR_T_INTERNAL_END,
  224. /* S2N_ERR_T_USAGE */
  225. S2N_ERR_NO_ALERT = S2N_ERR_T_USAGE_START,
  226. S2N_ERR_SERVER_MODE,
  227. S2N_ERR_CLIENT_MODE,
  228. S2N_ERR_CLIENT_MODE_DISABLED,
  229. S2N_ERR_TOO_MANY_CERTIFICATES,
  230. S2N_ERR_TOO_MANY_SIGNATURE_SCHEMES,
  231. S2N_ERR_CLIENT_AUTH_NOT_SUPPORTED_IN_FIPS_MODE,
  232. S2N_ERR_INVALID_BASE64,
  233. S2N_ERR_INVALID_HEX,
  234. S2N_ERR_INVALID_PEM,
  235. S2N_ERR_DH_PARAMS_CREATE,
  236. S2N_ERR_DH_TOO_SMALL,
  237. S2N_ERR_DH_PARAMETER_CHECK,
  238. S2N_ERR_INVALID_PKCS3,
  239. S2N_ERR_NO_CERTIFICATE_IN_PEM,
  240. S2N_ERR_SERVER_NAME_TOO_LONG,
  241. S2N_ERR_NUM_DEFAULT_CERTIFICATES,
  242. S2N_ERR_MULTIPLE_DEFAULT_CERTIFICATES_PER_AUTH_TYPE,
  243. S2N_ERR_INVALID_CIPHER_PREFERENCES,
  244. S2N_ERR_INVALID_APPLICATION_PROTOCOL,
  245. S2N_ERR_KEY_MISMATCH,
  246. S2N_ERR_SEND_SIZE,
  247. S2N_ERR_CORK_SET_ON_UNMANAGED,
  248. S2N_ERR_UNRECOGNIZED_EXTENSION,
  249. S2N_ERR_EXTENSION_NOT_RECEIVED,
  250. S2N_ERR_INVALID_SCT_LIST,
  251. S2N_ERR_INVALID_OCSP_RESPONSE,
  252. S2N_ERR_UPDATING_EXTENSION,
  253. S2N_ERR_INVALID_SERIALIZED_SESSION_STATE,
  254. S2N_ERR_SERIALIZED_SESSION_STATE_TOO_LONG,
  255. S2N_ERR_SESSION_ID_TOO_LONG,
  256. S2N_ERR_CLIENT_AUTH_NOT_SUPPORTED_IN_SESSION_RESUMPTION_MODE,
  257. S2N_ERR_INVALID_TICKET_KEY_LENGTH,
  258. S2N_ERR_INVALID_TICKET_KEY_NAME_OR_NAME_LENGTH,
  259. S2N_ERR_TICKET_KEY_NOT_UNIQUE,
  260. S2N_ERR_TICKET_KEY_LIMIT,
  261. S2N_ERR_NO_TICKET_ENCRYPT_DECRYPT_KEY,
  262. S2N_ERR_ENCRYPT_DECRYPT_KEY_SELECTION_FAILED,
  263. S2N_ERR_KEY_USED_IN_SESSION_TICKET_NOT_FOUND,
  264. S2N_ERR_SENDING_NST,
  265. S2N_ERR_INVALID_DYNAMIC_THRESHOLD,
  266. S2N_ERR_INVALID_ARGUMENT,
  267. S2N_ERR_NOT_IN_UNIT_TEST,
  268. S2N_ERR_NOT_IN_TEST,
  269. S2N_ERR_UNSUPPORTED_CPU,
  270. S2N_ERR_SESSION_ID_TOO_SHORT,
  271. S2N_ERR_CONNECTION_CACHING_DISALLOWED,
  272. S2N_ERR_SESSION_TICKET_NOT_SUPPORTED,
  273. S2N_ERR_OCSP_NOT_SUPPORTED,
  274. S2N_ERR_INVALID_SIGNATURE_ALGORITHMS_PREFERENCES,
  275. S2N_ERR_RSA_PSS_NOT_SUPPORTED,
  276. S2N_ERR_INVALID_ECC_PREFERENCES,
  277. S2N_ERR_INVALID_SECURITY_POLICY,
  278. S2N_ERR_INVALID_KEM_PREFERENCES,
  279. S2N_ERR_ASYNC_ALREADY_PERFORMED,
  280. S2N_ERR_ASYNC_NOT_PERFORMED,
  281. S2N_ERR_ASYNC_WRONG_CONNECTION,
  282. S2N_ERR_ASYNC_ALREADY_APPLIED,
  283. S2N_ERR_UNSUPPORTED_WITH_QUIC,
  284. S2N_ERR_DUPLICATE_PSK_IDENTITIES,
  285. S2N_ERR_OFFERED_PSKS_TOO_LONG,
  286. S2N_ERR_INVALID_SESSION_TICKET,
  287. S2N_ERR_REENTRANCY,
  288. S2N_ERR_INVALID_STATE,
  289. S2N_ERR_EARLY_DATA_NOT_ALLOWED,
  290. S2N_ERR_NO_CERT_FOUND,
  291. S2N_ERR_CERT_NOT_VALIDATED,
  292. S2N_ERR_NO_PRIVATE_KEY,
  293. S2N_ERR_PSK_MODE,
  294. S2N_ERR_X509_EXTENSION_VALUE_NOT_FOUND,
  295. S2N_ERR_INVALID_X509_EXTENSION_TYPE,
  296. S2N_ERR_INSUFFICIENT_MEM_SIZE,
  297. S2N_ERR_KEYING_MATERIAL_EXPIRED,
  298. S2N_ERR_SECRET_SCHEDULE_STATE,
  299. S2N_ERR_CERT_OWNERSHIP,
  300. S2N_ERR_INTERNAL_LIBCRYPTO_ERROR,
  301. S2N_ERR_HANDSHAKE_NOT_COMPLETE,
  302. S2N_ERR_KTLS_MANAGED_IO,
  303. S2N_ERR_KTLS_UNSUPPORTED_PLATFORM,
  304. S2N_ERR_KTLS_UNSUPPORTED_CONN,
  305. S2N_ERR_KTLS_ENABLE,
  306. S2N_ERR_KTLS_BAD_CMSG,
  307. S2N_ERR_KTLS_RENEG,
  308. S2N_ERR_ATOMIC,
  309. S2N_ERR_T_USAGE_END,
  310. } s2n_error;
  311. #define S2N_DEBUG_STR_LEN 128
  312. struct s2n_debug_info {
  313. const char *debug_str;
  314. const char *source;
  315. };
  316. extern __thread struct s2n_debug_info _s2n_debug_info;
  317. #define TO_STRING(s) #s
  318. #define STRING_(s) TO_STRING(s)
  319. #define STRING__LINE__ STRING_(__LINE__)
  320. /* gets the basename of a file path */
  321. /* _S2N_EXTRACT_BASENAME("Error encountered in /path/to/my/file.c") -> "file.c" */
  322. #if !(defined(CBMC) || defined(__TIMING_CONTRACTS__))
  323. #define _S2N_RSPLIT(subject, c) (strrchr((subject), c) ? strrchr((subject), c) + 1 : (subject))
  324. #define _S2N_EXTRACT_BASENAME(path) _S2N_RSPLIT((path) + strlen(_S2N_DEBUG_LINE_PREFIX), '/')
  325. #else
  326. #define _S2N_EXTRACT_BASENAME(path) path
  327. #endif
  328. #define _S2N_DEBUG_LINE_PREFIX "Error encountered in "
  329. #define _S2N_DEBUG_LINE _S2N_DEBUG_LINE_PREFIX __FILE__ ":" STRING__LINE__
  330. #define _S2N_ERROR(x) \
  331. do { \
  332. _s2n_debug_info.debug_str = _S2N_DEBUG_LINE; \
  333. _s2n_debug_info.source = _S2N_EXTRACT_BASENAME(_s2n_debug_info.debug_str); \
  334. s2n_errno = (x); \
  335. s2n_calculate_stacktrace(); \
  336. } while (0)
  337. #define S2N_ERROR_PRESERVE_ERRNO() \
  338. do { \
  339. return -1; \
  340. } while (0)
  341. #define S2N_ERROR_IS_BLOCKING(x) (s2n_error_get_type(x) == S2N_ERR_T_BLOCKED)
  342. /* DEPRECATED: use POSIX_BAIL instead */
  343. #define S2N_ERROR(x) \
  344. do { \
  345. _S2N_ERROR((x)); \
  346. return -1; \
  347. } while (0)
  348. /* DEPRECATED: use PTR_BAIL instead */
  349. #define S2N_ERROR_PTR(x) \
  350. do { \
  351. _S2N_ERROR((x)); \
  352. return NULL; \
  353. } while (0)
  354. /* DEPRECATED: use POSIX_ENSURE instead */
  355. #define S2N_ERROR_IF(cond, x) \
  356. do { \
  357. if (cond) { \
  358. S2N_ERROR(x); \
  359. } \
  360. } while (0)
  361. /** Calculate and print stacktraces */
  362. struct s2n_stacktrace {
  363. char **trace;
  364. int trace_size;
  365. };
  366. bool s2n_stack_traces_enabled();
  367. int s2n_stack_traces_enabled_set(bool newval);
  368. int s2n_calculate_stacktrace(void);
  369. int s2n_print_stacktrace(FILE *fptr);
  370. int s2n_free_stacktrace(void);
  371. int s2n_get_stacktrace(struct s2n_stacktrace *trace);
  372. void s2n_debug_info_reset(void);