s2n_errno.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 "error/s2n_errno.h"
  16. #include <errno.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <strings.h>
  21. #include "api/s2n.h"
  22. #include "utils/s2n_map.h"
  23. #include "utils/s2n_safety.h"
  24. #ifdef S2N_STACKTRACE
  25. #include <execinfo.h>
  26. #endif
  27. __thread int s2n_errno;
  28. __thread struct s2n_debug_info _s2n_debug_info = { .debug_str = "", .source = "" };
  29. /**
  30. * Returns the address of the thread-local `s2n_errno` variable
  31. */
  32. int *s2n_errno_location()
  33. {
  34. return &s2n_errno;
  35. }
  36. static const char *no_such_language = "Language is not supported for error translation";
  37. static const char *no_such_error = "Internal s2n error";
  38. /*
  39. * Define error entries with descriptions in this macro once
  40. * to generate code in next 2 following functions.
  41. */
  42. /* clang-format off */
  43. #define ERR_ENTRIES(ERR_ENTRY) \
  44. ERR_ENTRY(S2N_ERR_OK, "no error") \
  45. ERR_ENTRY(S2N_ERR_IO, "underlying I/O operation failed, check system errno") \
  46. ERR_ENTRY(S2N_ERR_CLOSED, "connection is closed") \
  47. ERR_ENTRY(S2N_ERR_IO_BLOCKED, "underlying I/O operation would block") \
  48. ERR_ENTRY(S2N_ERR_ASYNC_BLOCKED, "blocked on external async function invocation") \
  49. ERR_ENTRY(S2N_ERR_ALERT, "TLS alert received") \
  50. ERR_ENTRY(S2N_ERR_ENCRYPT, "error encrypting data") \
  51. ERR_ENTRY(S2N_ERR_DECRYPT, "error decrypting data") \
  52. ERR_ENTRY(S2N_ERR_BAD_MESSAGE, "Bad message encountered") \
  53. ERR_ENTRY(S2N_ERR_KEY_INIT, "error initializing encryption key") \
  54. ERR_ENTRY(S2N_ERR_KEY_DESTROY, "error destroying encryption key") \
  55. ERR_ENTRY(S2N_ERR_DH_SERIALIZING, "error serializing Diffie-Hellman parameters") \
  56. ERR_ENTRY(S2N_ERR_DH_SHARED_SECRET, "error computing Diffie-Hellman shared secret") \
  57. ERR_ENTRY(S2N_ERR_DH_WRITING_PUBLIC_KEY, "error writing Diffie-Hellman public key") \
  58. ERR_ENTRY(S2N_ERR_DH_FAILED_SIGNING, "error signing Diffie-Hellman values") \
  59. ERR_ENTRY(S2N_ERR_DH_COPYING_PARAMETERS, "error copying Diffie-Hellman parameters") \
  60. ERR_ENTRY(S2N_ERR_DH_GENERATING_PARAMETERS, "error generating Diffie-Hellman parameters") \
  61. ERR_ENTRY(S2N_ERR_CIPHER_NOT_SUPPORTED, "Cipher is not supported") \
  62. ERR_ENTRY(S2N_ERR_NO_APPLICATION_PROTOCOL, "No supported application protocol to negotiate") \
  63. ERR_ENTRY(S2N_ERR_FALLBACK_DETECTED, "TLS fallback detected") \
  64. ERR_ENTRY(S2N_ERR_HASH_DIGEST_FAILED, "failed to create hash digest") \
  65. ERR_ENTRY(S2N_ERR_HASH_INIT_FAILED, "error initializing hash") \
  66. ERR_ENTRY(S2N_ERR_HASH_UPDATE_FAILED, "error updating hash") \
  67. ERR_ENTRY(S2N_ERR_HASH_COPY_FAILED, "error copying hash") \
  68. ERR_ENTRY(S2N_ERR_HASH_WIPE_FAILED, "error wiping hash") \
  69. ERR_ENTRY(S2N_ERR_HASH_NOT_READY, "hash not in a valid state for the attempted operation") \
  70. ERR_ENTRY(S2N_ERR_ALLOW_MD5_FOR_FIPS_FAILED, "error allowing MD5 to be used when in FIPS mode") \
  71. ERR_ENTRY(S2N_ERR_DECODE_CERTIFICATE, "error decoding certificate") \
  72. ERR_ENTRY(S2N_ERR_DECODE_PRIVATE_KEY, "error decoding private key") \
  73. ERR_ENTRY(S2N_ERR_INVALID_SIGNATURE_ALGORITHM, "Invalid signature algorithm") \
  74. ERR_ENTRY(S2N_ERR_INVALID_SIGNATURE_SCHEME, "Invalid signature scheme") \
  75. ERR_ENTRY(S2N_ERR_CBC_VERIFY, "Failed CBC verification") \
  76. ERR_ENTRY(S2N_ERR_DH_COPYING_PUBLIC_KEY, "error copying Diffie-Hellman public key") \
  77. ERR_ENTRY(S2N_ERR_SIGN, "error signing data") \
  78. ERR_ENTRY(S2N_ERR_VERIFY_SIGNATURE, "error verifying signature") \
  79. ERR_ENTRY(S2N_ERR_ECDHE_GEN_KEY, "Failed to generate an ECDHE key") \
  80. ERR_ENTRY(S2N_ERR_ECDHE_SHARED_SECRET, "Error computing ECDHE shared secret") \
  81. ERR_ENTRY(S2N_ERR_ECDHE_UNSUPPORTED_CURVE, "Unsupported EC curve was presented during an ECDHE handshake") \
  82. ERR_ENTRY(S2N_ERR_ECDSA_UNSUPPORTED_CURVE, "Unsupported EC curve was presented during an ECDSA SignatureScheme handshake") \
  83. ERR_ENTRY(S2N_ERR_ECDHE_SERIALIZING, "Error serializing ECDHE public") \
  84. ERR_ENTRY(S2N_ERR_KEM_UNSUPPORTED_PARAMS, "Unsupported KEM params was presented during a handshake that uses a KEM") \
  85. ERR_ENTRY(S2N_ERR_SHUTDOWN_RECORD_TYPE, "Non alert record received during s2n_shutdown()") \
  86. ERR_ENTRY(S2N_ERR_SHUTDOWN_CLOSED, "Peer closed before sending their close_notify") \
  87. ERR_ENTRY(S2N_ERR_NON_EMPTY_RENEGOTIATION_INFO, "renegotiation_info should be empty") \
  88. ERR_ENTRY(S2N_ERR_RECORD_LIMIT, "TLS record limit reached") \
  89. ERR_ENTRY(S2N_ERR_CERT_UNTRUSTED, "Certificate is untrusted") \
  90. ERR_ENTRY(S2N_ERR_CERT_REVOKED, "Certificate has been revoked by the CA") \
  91. ERR_ENTRY(S2N_ERR_CERT_NOT_YET_VALID, "Certificate is not yet valid") \
  92. ERR_ENTRY(S2N_ERR_CERT_EXPIRED, "Certificate has expired") \
  93. ERR_ENTRY(S2N_ERR_CERT_TYPE_UNSUPPORTED, "Certificate Type is unsupported") \
  94. ERR_ENTRY(S2N_ERR_CERT_INVALID, "Certificate is invalid") \
  95. ERR_ENTRY(S2N_ERR_CERT_MAX_CHAIN_DEPTH_EXCEEDED, "The maximum certificate chain depth has been exceeded") \
  96. ERR_ENTRY(S2N_ERR_CERT_REJECTED, "Certificate failed custom application validation") \
  97. ERR_ENTRY(S2N_ERR_CRL_LOOKUP_FAILED, "No CRL could be found for the corresponding certificate") \
  98. ERR_ENTRY(S2N_ERR_CRL_SIGNATURE, "The signature of the CRL is invalid") \
  99. ERR_ENTRY(S2N_ERR_CRL_ISSUER, "Unable to get the CRL issuer certificate") \
  100. ERR_ENTRY(S2N_ERR_CRL_UNHANDLED_CRITICAL_EXTENSION, "Unhandled critical CRL extension") \
  101. ERR_ENTRY(S2N_ERR_CRL_INVALID_THIS_UPDATE, "The CRL contains an invalid thisUpdate field") \
  102. ERR_ENTRY(S2N_ERR_CRL_INVALID_NEXT_UPDATE, "The CRL contains an invalid nextUpdate field") \
  103. ERR_ENTRY(S2N_ERR_CRL_NOT_YET_VALID, "The CRL is not yet valid") \
  104. ERR_ENTRY(S2N_ERR_CRL_EXPIRED, "The CRL has expired") \
  105. ERR_ENTRY(S2N_ERR_INVALID_MAX_FRAG_LEN, "invalid Maximum Fragmentation Length encountered") \
  106. ERR_ENTRY(S2N_ERR_MAX_FRAG_LEN_MISMATCH, "Negotiated Maximum Fragmentation Length from server does not match the requested length by client") \
  107. ERR_ENTRY(S2N_ERR_PROTOCOL_VERSION_UNSUPPORTED, "TLS protocol version is not supported by configuration") \
  108. ERR_ENTRY(S2N_ERR_BAD_KEY_SHARE, "Bad key share received") \
  109. ERR_ENTRY(S2N_ERR_CANCELLED, "handshake was cancelled") \
  110. ERR_ENTRY(S2N_ERR_PROTOCOL_DOWNGRADE_DETECTED, "Protocol downgrade detected by client") \
  111. ERR_ENTRY(S2N_ERR_MADVISE, "error calling madvise") \
  112. ERR_ENTRY(S2N_ERR_ALLOC, "error allocating memory") \
  113. ERR_ENTRY(S2N_ERR_MLOCK, "error calling mlock (Did you run prlimit?)") \
  114. ERR_ENTRY(S2N_ERR_MUNLOCK, "error calling munlock") \
  115. ERR_ENTRY(S2N_ERR_FSTAT, "error calling fstat") \
  116. ERR_ENTRY(S2N_ERR_OPEN, "error calling open") \
  117. ERR_ENTRY(S2N_ERR_MMAP, "error calling mmap") \
  118. ERR_ENTRY(S2N_ERR_ATEXIT, "error calling atexit") \
  119. ERR_ENTRY(S2N_ERR_NOMEM, "no memory") \
  120. ERR_ENTRY(S2N_ERR_NULL, "NULL pointer encountered") \
  121. ERR_ENTRY(S2N_ERR_SAFETY, "a safety check failed") \
  122. ERR_ENTRY(S2N_ERR_INITIALIZED, "s2n is initialized") \
  123. ERR_ENTRY(S2N_ERR_NOT_INITIALIZED, "s2n not initialized") \
  124. ERR_ENTRY(S2N_ERR_RANDOM_UNINITIALIZED, "s2n entropy not initialized") \
  125. ERR_ENTRY(S2N_ERR_OPEN_RANDOM, "error opening urandom") \
  126. ERR_ENTRY(S2N_ERR_RESIZE_STATIC_STUFFER, "cannot resize a static stuffer") \
  127. ERR_ENTRY(S2N_ERR_RESIZE_TAINTED_STUFFER, "cannot resize a tainted stuffer") \
  128. ERR_ENTRY(S2N_ERR_STUFFER_OUT_OF_DATA, "stuffer is out of data") \
  129. ERR_ENTRY(S2N_ERR_STUFFER_IS_FULL, "stuffer is full") \
  130. ERR_ENTRY(S2N_ERR_STUFFER_NOT_FOUND, "stuffer expected bytes were not found") \
  131. ERR_ENTRY(S2N_ERR_STUFFER_HAS_UNPROCESSED_DATA, "stuffer has unprocessed data") \
  132. ERR_ENTRY(S2N_ERR_HASH_INVALID_ALGORITHM, "invalid hash algorithm") \
  133. ERR_ENTRY(S2N_ERR_PRF_INVALID_ALGORITHM, "invalid prf hash algorithm") \
  134. ERR_ENTRY(S2N_ERR_PRF_INVALID_SEED, "invalid prf seeds provided") \
  135. ERR_ENTRY(S2N_ERR_PRF_DERIVE, "error deriving a secret from the PRF") \
  136. ERR_ENTRY(S2N_ERR_P_HASH_INVALID_ALGORITHM, "invalid p_hash algorithm") \
  137. ERR_ENTRY(S2N_ERR_P_HASH_INIT_FAILED, "error initializing p_hash") \
  138. ERR_ENTRY(S2N_ERR_P_HASH_UPDATE_FAILED, "error updating p_hash") \
  139. ERR_ENTRY(S2N_ERR_P_HASH_FINAL_FAILED, "error creating p_hash digest") \
  140. ERR_ENTRY(S2N_ERR_P_HASH_WIPE_FAILED, "error wiping p_hash") \
  141. ERR_ENTRY(S2N_ERR_HMAC_INVALID_ALGORITHM, "invalid HMAC algorithm") \
  142. ERR_ENTRY(S2N_ERR_HKDF_OUTPUT_SIZE, "invalid HKDF output size") \
  143. ERR_ENTRY(S2N_ERR_HKDF, "error generating HKDF output") \
  144. ERR_ENTRY(S2N_ERR_ALERT_PRESENT, "TLS alert is already pending") \
  145. ERR_ENTRY(S2N_ERR_HANDSHAKE_STATE, "Invalid handshake state encountered") \
  146. ERR_ENTRY(S2N_ERR_SHUTDOWN_PAUSED, "s2n_shutdown() called while paused") \
  147. ERR_ENTRY(S2N_ERR_SIZE_MISMATCH, "size mismatch") \
  148. ERR_ENTRY(S2N_ERR_DRBG, "Error using Deterministic Random Bit Generator") \
  149. ERR_ENTRY(S2N_ERR_DRBG_REQUEST_SIZE, "Request for too much entropy") \
  150. ERR_ENTRY(S2N_ERR_KEY_CHECK, "Invalid key") \
  151. ERR_ENTRY(S2N_ERR_CIPHER_TYPE, "Unknown cipher type used") \
  152. ERR_ENTRY(S2N_ERR_MAP_DUPLICATE, "Duplicate map key inserted") \
  153. ERR_ENTRY(S2N_ERR_MAP_IMMUTABLE, "Attempt to update an immutable map") \
  154. ERR_ENTRY(S2N_ERR_MAP_MUTABLE, "Attempt to lookup a mutable map") \
  155. ERR_ENTRY(S2N_ERR_MAP_INVALID_MAP_SIZE, "Attempt to create a map with 0 capacity") \
  156. ERR_ENTRY(S2N_ERR_INITIAL_HMAC, "error calling EVP_CIPHER_CTX_ctrl for composite cbc cipher") \
  157. ERR_ENTRY(S2N_ERR_INVALID_NONCE_TYPE, "Invalid AEAD nonce type") \
  158. ERR_ENTRY(S2N_ERR_UNIMPLEMENTED, "Unimplemented feature") \
  159. ERR_ENTRY(S2N_ERR_HANDSHAKE_UNREACHABLE, "Unreachable handshake state machine handler invoked") \
  160. ERR_ENTRY(S2N_ERR_READ, "error calling read") \
  161. ERR_ENTRY(S2N_ERR_WRITE, "error calling write") \
  162. ERR_ENTRY(S2N_ERR_BAD_FD, "Invalid file descriptor") \
  163. ERR_ENTRY(S2N_ERR_RDRAND_FAILED, "Error executing rdrand instruction") \
  164. ERR_ENTRY(S2N_ERR_FAILED_CACHE_RETRIEVAL, "Failed cache retrieval") \
  165. ERR_ENTRY(S2N_ERR_X509_TRUST_STORE, "Error initializing trust store") \
  166. ERR_ENTRY(S2N_ERR_UNKNOWN_PROTOCOL_VERSION, "Error determining client protocol version") \
  167. ERR_ENTRY(S2N_ERR_NULL_CN_NAME, "Error parsing CN names") \
  168. ERR_ENTRY(S2N_ERR_NULL_SANS, "Error parsing SANS") \
  169. ERR_ENTRY(S2N_ERR_CLIENT_HELLO_VERSION, "Could not get client hello version") \
  170. ERR_ENTRY(S2N_ERR_CLIENT_PROTOCOL_VERSION, "Could not get client protocol version") \
  171. ERR_ENTRY(S2N_ERR_SERVER_PROTOCOL_VERSION, "Could not get server protocol version") \
  172. ERR_ENTRY(S2N_ERR_ACTUAL_PROTOCOL_VERSION, "Could not get actual protocol version") \
  173. ERR_ENTRY(S2N_ERR_POLLING_FROM_SOCKET, "Error polling from socket") \
  174. ERR_ENTRY(S2N_ERR_RECV_STUFFER_FROM_CONN, "Error receiving stuffer from connection") \
  175. ERR_ENTRY(S2N_ERR_SEND_STUFFER_TO_CONN, "Error sending stuffer to connection") \
  176. ERR_ENTRY(S2N_ERR_PRECONDITION_VIOLATION, "Precondition violation") \
  177. ERR_ENTRY(S2N_ERR_POSTCONDITION_VIOLATION, "Postcondition violation") \
  178. ERR_ENTRY(S2N_ERR_INTEGER_OVERFLOW, "Integer overflow violation") \
  179. ERR_ENTRY(S2N_ERR_ARRAY_INDEX_OOB, "Array index out of bounds") \
  180. ERR_ENTRY(S2N_ERR_FREE_STATIC_BLOB, "Cannot free a static blob") \
  181. ERR_ENTRY(S2N_ERR_RESIZE_STATIC_BLOB, "Cannot resize a static blob") \
  182. ERR_ENTRY(S2N_ERR_NO_SUPPORTED_LIBCRYPTO_API, "libcrypto does not support this API") \
  183. ERR_ENTRY(S2N_ERR_RECORD_LENGTH_TOO_LARGE, "Record length exceeds protocol version maximum") \
  184. ERR_ENTRY(S2N_ERR_SET_DUPLICATE_VALUE, "Set already contains the provided value") \
  185. ERR_ENTRY(S2N_ERR_ASYNC_CALLBACK_FAILED, "Callback associated with async private keys function has failed") \
  186. ERR_ENTRY(S2N_ERR_ASYNC_MORE_THAN_ONE, "Only one asynchronous operation can be in-progress at the same time") \
  187. ERR_ENTRY(S2N_ERR_NO_ALERT, "No Alert present") \
  188. ERR_ENTRY(S2N_ERR_SERVER_MODE, "Operation not allowed in server mode") \
  189. ERR_ENTRY(S2N_ERR_CLIENT_MODE, "Operation not allowed in client mode") \
  190. ERR_ENTRY(S2N_ERR_CLIENT_MODE_DISABLED, "client connections not allowed") \
  191. ERR_ENTRY(S2N_ERR_TOO_MANY_CERTIFICATES, "only 1 certificate is supported in client mode") \
  192. ERR_ENTRY(S2N_ERR_TOO_MANY_SIGNATURE_SCHEMES, "Max supported length of SignatureAlgorithms/SignatureSchemes list is 32") \
  193. ERR_ENTRY(S2N_ERR_CLIENT_AUTH_NOT_SUPPORTED_IN_FIPS_MODE, "Client Auth is not supported when in FIPS mode") \
  194. ERR_ENTRY(S2N_ERR_INVALID_BASE64, "invalid base64 encountered") \
  195. ERR_ENTRY(S2N_ERR_INVALID_HEX, "invalid HEX encountered") \
  196. ERR_ENTRY(S2N_ERR_INVALID_PEM, "invalid PEM encountered") \
  197. ERR_ENTRY(S2N_ERR_DH_PARAMS_CREATE, "error creating Diffie-Hellman parameters") \
  198. ERR_ENTRY(S2N_ERR_DH_TOO_SMALL, "Diffie-Hellman parameters are too small") \
  199. ERR_ENTRY(S2N_ERR_DH_PARAMETER_CHECK, "Diffie-Hellman parameter check failed") \
  200. ERR_ENTRY(S2N_ERR_INVALID_PKCS3, "invalid PKCS3 encountered") \
  201. ERR_ENTRY(S2N_ERR_NO_CERTIFICATE_IN_PEM, "No certificate in PEM") \
  202. ERR_ENTRY(S2N_ERR_SERVER_NAME_TOO_LONG, "server name is too long") \
  203. ERR_ENTRY(S2N_ERR_NUM_DEFAULT_CERTIFICATES, "exceeded max default certificates or provided no default") \
  204. ERR_ENTRY(S2N_ERR_MULTIPLE_DEFAULT_CERTIFICATES_PER_AUTH_TYPE, "setting multiple default certificates per auth type is not allowed") \
  205. ERR_ENTRY(S2N_ERR_INVALID_CIPHER_PREFERENCES, "Invalid Cipher Preferences version") \
  206. ERR_ENTRY(S2N_ERR_INVALID_APPLICATION_PROTOCOL, "The supplied application protocol name is invalid") \
  207. ERR_ENTRY(S2N_ERR_KEY_MISMATCH, "public and private key do not match") \
  208. ERR_ENTRY(S2N_ERR_SEND_SIZE, "Retried s2n_send() size is invalid") \
  209. ERR_ENTRY(S2N_ERR_CORK_SET_ON_UNMANAGED, "Attempt to set connection cork management on unmanaged IO") \
  210. ERR_ENTRY(S2N_ERR_UNRECOGNIZED_EXTENSION, "TLS extension not recognized") \
  211. ERR_ENTRY(S2N_ERR_EXTENSION_NOT_RECEIVED, "The TLS extension was not received") \
  212. ERR_ENTRY(S2N_ERR_INVALID_SCT_LIST, "SCT list is invalid") \
  213. ERR_ENTRY(S2N_ERR_INVALID_OCSP_RESPONSE, "OCSP response is invalid") \
  214. ERR_ENTRY(S2N_ERR_UPDATING_EXTENSION, "Updating extension data failed") \
  215. ERR_ENTRY(S2N_ERR_INVALID_SERIALIZED_SESSION_STATE, "Serialized session state is not in valid format") \
  216. ERR_ENTRY(S2N_ERR_SERIALIZED_SESSION_STATE_TOO_LONG, "Serialized session state is too long") \
  217. ERR_ENTRY(S2N_ERR_SESSION_ID_TOO_LONG, "Session id is too long") \
  218. ERR_ENTRY(S2N_ERR_CLIENT_AUTH_NOT_SUPPORTED_IN_SESSION_RESUMPTION_MODE, "Client Auth is not supported in session resumption mode") \
  219. ERR_ENTRY(S2N_ERR_INVALID_TICKET_KEY_LENGTH, "Session ticket key length cannot be zero") \
  220. ERR_ENTRY(S2N_ERR_INVALID_TICKET_KEY_NAME_OR_NAME_LENGTH, "Session ticket key name should be unique and the name length cannot be zero") \
  221. ERR_ENTRY(S2N_ERR_TICKET_KEY_NOT_UNIQUE, "Cannot add session ticket key because it was added before") \
  222. ERR_ENTRY(S2N_ERR_TICKET_KEY_LIMIT, "Limit reached for unexpired session ticket keys") \
  223. ERR_ENTRY(S2N_ERR_NO_TICKET_ENCRYPT_DECRYPT_KEY, "No key in encrypt-decrypt state is available to encrypt session ticket") \
  224. ERR_ENTRY(S2N_ERR_ENCRYPT_DECRYPT_KEY_SELECTION_FAILED, "Failed to select a key from keys in encrypt-decrypt state") \
  225. ERR_ENTRY(S2N_ERR_KEY_USED_IN_SESSION_TICKET_NOT_FOUND, "Key used in already assigned session ticket not found for decryption") \
  226. ERR_ENTRY(S2N_ERR_SENDING_NST, "Error in session ticket status encountered before sending NST") \
  227. ERR_ENTRY(S2N_ERR_INVALID_DYNAMIC_THRESHOLD, "invalid dynamic record threshold") \
  228. ERR_ENTRY(S2N_ERR_INVALID_ARGUMENT, "invalid argument provided into a function call") \
  229. ERR_ENTRY(S2N_ERR_NOT_IN_UNIT_TEST, "Illegal configuration, can only be used during unit tests") \
  230. ERR_ENTRY(S2N_ERR_NOT_IN_TEST, "Illegal configuration, can only be used during unit or integration tests") \
  231. ERR_ENTRY(S2N_ERR_UNSUPPORTED_CPU, "Unsupported CPU architecture") \
  232. ERR_ENTRY(S2N_ERR_SESSION_ID_TOO_SHORT, "Session id is too short") \
  233. ERR_ENTRY(S2N_ERR_CONNECTION_CACHING_DISALLOWED, "This connection is not allowed to be cached") \
  234. ERR_ENTRY(S2N_ERR_SESSION_TICKET_NOT_SUPPORTED, "Session ticket not supported for this connection") \
  235. ERR_ENTRY(S2N_ERR_OCSP_NOT_SUPPORTED, "OCSP stapling was requested, but is not supported") \
  236. ERR_ENTRY(S2N_ERR_INVALID_SIGNATURE_ALGORITHMS_PREFERENCES, "Invalid signature algorithms preferences version") \
  237. ERR_ENTRY(S2N_ERR_RSA_PSS_NOT_SUPPORTED, "RSA-PSS signing not supported by underlying libcrypto implementation") \
  238. ERR_ENTRY(S2N_ERR_MAX_INNER_PLAINTEXT_SIZE, "Inner plaintext size exceeds limit") \
  239. ERR_ENTRY(S2N_ERR_INVALID_ECC_PREFERENCES, "Invalid ecc curves preferences version") \
  240. ERR_ENTRY(S2N_ERR_RECORD_STUFFER_SIZE, "Record stuffer out of space") \
  241. ERR_ENTRY(S2N_ERR_FRAGMENT_LENGTH_TOO_SMALL, "Fragment length is too small") \
  242. ERR_ENTRY(S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE, "Fragment length is too large") \
  243. ERR_ENTRY(S2N_ERR_RECORD_STUFFER_NEEDS_DRAINING, "Record stuffer needs to be drained first") \
  244. ERR_ENTRY(S2N_ERR_UNSUPPORTED_EXTENSION, "Illegal use of a known, supported extension") \
  245. ERR_ENTRY(S2N_ERR_MISSING_EXTENSION, "Mandatory extension not received") \
  246. ERR_ENTRY(S2N_ERR_DUPLICATE_EXTENSION, "Extension block contains two or more extensions of the same type") \
  247. ERR_ENTRY(S2N_ERR_INVALID_SECURITY_POLICY, "Invalid security policy") \
  248. ERR_ENTRY(S2N_ERR_INVALID_KEM_PREFERENCES, "Invalid kem preferences version") \
  249. ERR_ENTRY(S2N_ERR_INVALID_PARSED_EXTENSIONS, "Invalid parsed extension data") \
  250. ERR_ENTRY(S2N_ERR_ASYNC_ALREADY_PERFORMED, "Async operation was already performed, cannot perform it again") \
  251. ERR_ENTRY(S2N_ERR_ASYNC_NOT_PERFORMED, "Async operation is not performed, cannot apply its result") \
  252. ERR_ENTRY(S2N_ERR_ASYNC_WRONG_CONNECTION, "Async private key operation can only be consumed by connection which initiated it") \
  253. ERR_ENTRY(S2N_ERR_ASYNC_ALREADY_APPLIED, "Async operation was already applied to connection, cannot apply it again") \
  254. ERR_ENTRY(S2N_ERR_INVALID_HELLO_RETRY, "Invalid hello retry request") \
  255. ERR_ENTRY(S2N_ERR_INVALID_STATE, "Invalid state, this is the result of invalid use of an API. Check the API documentation for the function that raised this error for more info") \
  256. ERR_ENTRY(S2N_ERR_UNSUPPORTED_WITH_QUIC, "Functionality not supported when running with QUIC support enabled") \
  257. ERR_ENTRY(S2N_ERR_PQ_CRYPTO, "An error occurred in a post-quantum crypto function") \
  258. ERR_ENTRY(S2N_ERR_PQ_DISABLED, "Post-quantum crypto is disabled") \
  259. ERR_ENTRY(S2N_ERR_DUPLICATE_PSK_IDENTITIES, "The list of pre-shared keys provided contains duplicate psk identities") \
  260. ERR_ENTRY(S2N_ERR_OFFERED_PSKS_TOO_LONG, "The total pre-shared key data is too long to send over the wire") \
  261. ERR_ENTRY(S2N_ERR_INVALID_SESSION_TICKET, "Session ticket data is not valid") \
  262. ERR_ENTRY(S2N_ERR_REENTRANCY, "Original execution must complete before method can be called again") \
  263. ERR_ENTRY(S2N_ERR_INVALID_CERT_STATE, "Certificate validation entered an invalid state and is not able to continue") \
  264. ERR_ENTRY(S2N_ERR_INVALID_EARLY_DATA_STATE, "Early data in invalid state") \
  265. ERR_ENTRY(S2N_ERR_EARLY_DATA_NOT_ALLOWED, "Early data is not allowed by the connection") \
  266. ERR_ENTRY(S2N_ERR_NO_CERT_FOUND, "Certificate not found") \
  267. ERR_ENTRY(S2N_ERR_NO_PRIVATE_KEY, "Certificate found, but no corresponding private key") \
  268. ERR_ENTRY(S2N_ERR_CERT_NOT_VALIDATED, "Certificate not validated") \
  269. ERR_ENTRY(S2N_ERR_MAX_EARLY_DATA_SIZE, "Maximum early data bytes exceeded") \
  270. ERR_ENTRY(S2N_ERR_EARLY_DATA_BLOCKED, "Blocked on early data") \
  271. ERR_ENTRY(S2N_ERR_PSK_MODE, "Mixing resumption and external PSKs is not supported") \
  272. ERR_ENTRY(S2N_ERR_X509_EXTENSION_VALUE_NOT_FOUND, "X509 extension value not found") \
  273. ERR_ENTRY(S2N_ERR_INVALID_X509_EXTENSION_TYPE, "Invalid X509 extension type") \
  274. ERR_ENTRY(S2N_ERR_INSUFFICIENT_MEM_SIZE, "The provided buffer size is not large enough to contain the output data. Try increasing the allocation size.") \
  275. ERR_ENTRY(S2N_ERR_KEYING_MATERIAL_EXPIRED, "The lifetime of the connection keying material has exceeded the limit. Perform a new full handshake.") \
  276. ERR_ENTRY(S2N_ERR_EARLY_DATA_TRIAL_DECRYPT, "Unable to decrypt rejected early data") \
  277. ERR_ENTRY(S2N_ERR_PKEY_CTX_INIT, "Unable to initialize the libcrypto pkey context") \
  278. ERR_ENTRY(S2N_ERR_FORK_DETECTION_INIT, "Fork detection initialization failed") \
  279. ERR_ENTRY(S2N_ERR_RETRIEVE_FORK_GENERATION_NUMBER, "Retrieving fork generation number failed") \
  280. ERR_ENTRY(S2N_ERR_SECRET_SCHEDULE_STATE, "Correct inputs to secret calculation not available") \
  281. ERR_ENTRY(S2N_ERR_LIBCRYPTO_VERSION_NUMBER_MISMATCH, "The libcrypto major version number seen at compile-time is different from the major version number seen at run-time") \
  282. ERR_ENTRY(S2N_ERR_LIBCRYPTO_VERSION_NAME_MISMATCH, "The libcrypto major version name seen at compile-time is different from the major version name seen at run-time") \
  283. ERR_ENTRY(S2N_ERR_OSSL_PROVIDER, "Failed to load or unload an openssl provider") \
  284. ERR_ENTRY(S2N_ERR_CERT_OWNERSHIP, "The ownership of the certificate chain is incompatible with the operation") \
  285. ERR_ENTRY(S2N_ERR_INTERNAL_LIBCRYPTO_ERROR, "An internal error has occurred in the libcrypto API") \
  286. ERR_ENTRY(S2N_ERR_NO_RENEGOTIATION, "Only secure, server-initiated renegotiation is supported") \
  287. ERR_ENTRY(S2N_ERR_APP_DATA_BLOCKED, "Blocked on application data during handshake") \
  288. ERR_ENTRY(S2N_ERR_KTLS_MANAGED_IO, "kTLS cannot be enabled while custom I/O is configured for the connection") \
  289. ERR_ENTRY(S2N_ERR_HANDSHAKE_NOT_COMPLETE, "Operation is only allowed after the handshake is complete") \
  290. ERR_ENTRY(S2N_ERR_KTLS_UNSUPPORTED_PLATFORM, "kTLS is unsupported on this platform") \
  291. ERR_ENTRY(S2N_ERR_KTLS_UNSUPPORTED_CONN, "kTLS is unsupported for this connection") \
  292. ERR_ENTRY(S2N_ERR_KTLS_ENABLE, "An error occurred when attempting to enable kTLS on socket. Ensure the 'tls' kernel module is enabled.") \
  293. ERR_ENTRY(S2N_ERR_KTLS_BAD_CMSG, "Error handling cmsghdr.") \
  294. ERR_ENTRY(S2N_ERR_ATOMIC, "Atomic operations in this environment would require locking") \
  295. ERR_ENTRY(S2N_ERR_TEST_ASSERTION, "Test assertion failed") \
  296. ERR_ENTRY(S2N_ERR_KTLS_RENEG, "kTLS does not support secure renegotiation") \
  297. /* clang-format on */
  298. #define ERR_STR_CASE(ERR, str) \
  299. case ERR: \
  300. return str;
  301. #define ERR_NAME_CASE(ERR, str) \
  302. case ERR: \
  303. return #ERR;
  304. const char *s2n_strerror(int error, const char *lang)
  305. {
  306. if (lang == NULL) {
  307. lang = "EN";
  308. }
  309. if (strcasecmp(lang, "EN")) {
  310. return no_such_language;
  311. }
  312. s2n_error err = error;
  313. switch (err) {
  314. ERR_ENTRIES(ERR_STR_CASE)
  315. /* Skip block ends */
  316. case S2N_ERR_T_OK_END:
  317. case S2N_ERR_T_IO_END:
  318. case S2N_ERR_T_CLOSED_END:
  319. case S2N_ERR_T_BLOCKED_END:
  320. case S2N_ERR_T_ALERT_END:
  321. case S2N_ERR_T_PROTO_END:
  322. case S2N_ERR_T_INTERNAL_END:
  323. case S2N_ERR_T_USAGE_END:
  324. break;
  325. /* No default to make compiler fail on missing values */
  326. }
  327. return no_such_error;
  328. }
  329. const char *s2n_strerror_name(int error)
  330. {
  331. s2n_error err = error;
  332. switch (err) {
  333. ERR_ENTRIES(ERR_NAME_CASE)
  334. /* Skip block ends */
  335. case S2N_ERR_T_OK_END:
  336. case S2N_ERR_T_IO_END:
  337. case S2N_ERR_T_CLOSED_END:
  338. case S2N_ERR_T_BLOCKED_END:
  339. case S2N_ERR_T_ALERT_END:
  340. case S2N_ERR_T_PROTO_END:
  341. case S2N_ERR_T_INTERNAL_END:
  342. case S2N_ERR_T_USAGE_END:
  343. break;
  344. /* No default to make compiler fail on missing values */
  345. }
  346. return no_such_error;
  347. }
  348. const char *s2n_strerror_debug(int error, const char *lang)
  349. {
  350. if (lang == NULL) {
  351. lang = "EN";
  352. }
  353. if (strcasecmp(lang, "EN")) {
  354. return no_such_language;
  355. }
  356. /* No error, just return the no error string */
  357. if (error == S2N_ERR_OK) {
  358. return s2n_strerror(error, lang);
  359. }
  360. return _s2n_debug_info.debug_str;
  361. }
  362. const char *s2n_strerror_source(int error)
  363. {
  364. /* No error, just return the no error string */
  365. if (error == S2N_ERR_OK) {
  366. return s2n_strerror(error, "EN");
  367. }
  368. return _s2n_debug_info.source;
  369. }
  370. int s2n_error_get_type(int error)
  371. {
  372. return (error >> S2N_ERR_NUM_VALUE_BITS);
  373. }
  374. /* https://www.gnu.org/software/libc/manual/html_node/Backtraces.html */
  375. static bool s_s2n_stack_traces_enabled = false;
  376. bool s2n_stack_traces_enabled()
  377. {
  378. return s_s2n_stack_traces_enabled;
  379. }
  380. int s2n_stack_traces_enabled_set(bool newval)
  381. {
  382. s_s2n_stack_traces_enabled = newval;
  383. return S2N_SUCCESS;
  384. }
  385. void s2n_debug_info_reset(void)
  386. {
  387. _s2n_debug_info.debug_str = "";
  388. _s2n_debug_info.source = "";
  389. }
  390. #ifdef S2N_STACKTRACE
  391. #define MAX_BACKTRACE_DEPTH 20
  392. __thread struct s2n_stacktrace tl_stacktrace = { 0 };
  393. int s2n_free_stacktrace(void)
  394. {
  395. if (tl_stacktrace.trace != NULL) {
  396. free(tl_stacktrace.trace);
  397. struct s2n_stacktrace zero_stacktrace = { 0 };
  398. tl_stacktrace = zero_stacktrace;
  399. }
  400. return S2N_SUCCESS;
  401. }
  402. int s2n_calculate_stacktrace(void)
  403. {
  404. if (!s_s2n_stack_traces_enabled) {
  405. return S2N_SUCCESS;
  406. }
  407. int old_errno = errno;
  408. POSIX_GUARD(s2n_free_stacktrace());
  409. void *array[MAX_BACKTRACE_DEPTH];
  410. tl_stacktrace.trace_size = backtrace(array, MAX_BACKTRACE_DEPTH);
  411. tl_stacktrace.trace = backtrace_symbols(array, tl_stacktrace.trace_size);
  412. errno = old_errno;
  413. return S2N_SUCCESS;
  414. }
  415. int s2n_get_stacktrace(struct s2n_stacktrace *trace)
  416. {
  417. *trace = tl_stacktrace;
  418. return S2N_SUCCESS;
  419. }
  420. int s2n_print_stacktrace(FILE *fptr)
  421. {
  422. if (!s_s2n_stack_traces_enabled) {
  423. fprintf(fptr, "%s\n%s\n",
  424. "NOTE: Some details are omitted, run with S2N_PRINT_STACKTRACE=1 for a verbose backtrace.",
  425. "See https://github.com/aws/s2n-tls/blob/main/docs/USAGE-GUIDE.md");
  426. return S2N_SUCCESS;
  427. }
  428. fprintf(fptr, "\nStacktrace is:\n");
  429. for (int i = 0; i < tl_stacktrace.trace_size; ++i) {
  430. fprintf(fptr, "%s\n", tl_stacktrace.trace[i]);
  431. }
  432. return S2N_SUCCESS;
  433. }
  434. #else /* !S2N_STACKTRACE */
  435. int s2n_free_stacktrace(void)
  436. {
  437. S2N_ERROR(S2N_ERR_UNIMPLEMENTED);
  438. }
  439. int s2n_calculate_stacktrace(void)
  440. {
  441. if (!s_s2n_stack_traces_enabled) {
  442. return S2N_SUCCESS;
  443. }
  444. S2N_ERROR(S2N_ERR_UNIMPLEMENTED);
  445. }
  446. int s2n_get_stacktrace(struct s2n_stacktrace *trace)
  447. {
  448. S2N_ERROR(S2N_ERR_UNIMPLEMENTED);
  449. }
  450. int s2n_print_stacktrace(FILE *fptr)
  451. {
  452. S2N_ERROR(S2N_ERR_UNIMPLEMENTED);
  453. }
  454. #endif /* S2N_STACKTRACE */