error.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. * Error message information
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "common.h"
  20. #include "mbedtls/error.h"
  21. #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
  22. #if defined(MBEDTLS_ERROR_C)
  23. #if defined(MBEDTLS_PLATFORM_C)
  24. #include "mbedtls/platform.h"
  25. #else
  26. #define mbedtls_snprintf snprintf
  27. #endif
  28. #include <stdio.h>
  29. #include <string.h>
  30. #if defined(MBEDTLS_AES_C)
  31. #include "mbedtls/aes.h"
  32. #endif
  33. #if defined(MBEDTLS_ARIA_C)
  34. #include "mbedtls/aria.h"
  35. #endif
  36. #if defined(MBEDTLS_ASN1_PARSE_C)
  37. #include "mbedtls/asn1.h"
  38. #endif
  39. #if defined(MBEDTLS_BASE64_C)
  40. #include "mbedtls/base64.h"
  41. #endif
  42. #if defined(MBEDTLS_BIGNUM_C)
  43. #include "mbedtls/bignum.h"
  44. #endif
  45. #if defined(MBEDTLS_CAMELLIA_C)
  46. #include "mbedtls/camellia.h"
  47. #endif
  48. #if defined(MBEDTLS_CCM_C)
  49. #include "mbedtls/ccm.h"
  50. #endif
  51. #if defined(MBEDTLS_CHACHA20_C)
  52. #include "mbedtls/chacha20.h"
  53. #endif
  54. #if defined(MBEDTLS_CHACHAPOLY_C)
  55. #include "mbedtls/chachapoly.h"
  56. #endif
  57. #if defined(MBEDTLS_CIPHER_C)
  58. #include "mbedtls/cipher.h"
  59. #endif
  60. #if defined(MBEDTLS_CTR_DRBG_C)
  61. #include "mbedtls/ctr_drbg.h"
  62. #endif
  63. #if defined(MBEDTLS_DES_C)
  64. #include "mbedtls/des.h"
  65. #endif
  66. #if defined(MBEDTLS_DHM_C)
  67. #include "mbedtls/dhm.h"
  68. #endif
  69. #if defined(MBEDTLS_ECP_C)
  70. #include "mbedtls/ecp.h"
  71. #endif
  72. #if defined(MBEDTLS_ENTROPY_C)
  73. #include "mbedtls/entropy.h"
  74. #endif
  75. #if defined(MBEDTLS_ERROR_C)
  76. #include "mbedtls/error.h"
  77. #endif
  78. #if defined(MBEDTLS_PLATFORM_C)
  79. #include "mbedtls/platform.h"
  80. #endif
  81. #if defined(MBEDTLS_GCM_C)
  82. #include "mbedtls/gcm.h"
  83. #endif
  84. #if defined(MBEDTLS_HKDF_C)
  85. #include "mbedtls/hkdf.h"
  86. #endif
  87. #if defined(MBEDTLS_HMAC_DRBG_C)
  88. #include "mbedtls/hmac_drbg.h"
  89. #endif
  90. #if defined(MBEDTLS_MD_C)
  91. #include "mbedtls/md.h"
  92. #endif
  93. #if defined(MBEDTLS_NET_C)
  94. #include "mbedtls/net_sockets.h"
  95. #endif
  96. #if defined(MBEDTLS_OID_C)
  97. #include "mbedtls/oid.h"
  98. #endif
  99. #if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
  100. #include "mbedtls/pem.h"
  101. #endif
  102. #if defined(MBEDTLS_PK_C)
  103. #include "mbedtls/pk.h"
  104. #endif
  105. #if defined(MBEDTLS_PKCS12_C)
  106. #include "mbedtls/pkcs12.h"
  107. #endif
  108. #if defined(MBEDTLS_PKCS5_C)
  109. #include "mbedtls/pkcs5.h"
  110. #endif
  111. #if defined(MBEDTLS_POLY1305_C)
  112. #include "mbedtls/poly1305.h"
  113. #endif
  114. #if defined(MBEDTLS_RSA_C)
  115. #include "mbedtls/rsa.h"
  116. #endif
  117. #if defined(MBEDTLS_SHA1_C)
  118. #include "mbedtls/sha1.h"
  119. #endif
  120. #if defined(MBEDTLS_SHA256_C)
  121. #include "mbedtls/sha256.h"
  122. #endif
  123. #if defined(MBEDTLS_SHA512_C)
  124. #include "mbedtls/sha512.h"
  125. #endif
  126. #if defined(MBEDTLS_SSL_TLS_C)
  127. #include "mbedtls/ssl.h"
  128. #endif
  129. #if defined(MBEDTLS_THREADING_C)
  130. #include "mbedtls/threading.h"
  131. #endif
  132. #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
  133. #include "mbedtls/x509.h"
  134. #endif
  135. const char * mbedtls_high_level_strerr( int error_code )
  136. {
  137. int high_level_error_code;
  138. if( error_code < 0 )
  139. error_code = -error_code;
  140. /* Extract the high-level part from the error code. */
  141. high_level_error_code = error_code & 0xFF80;
  142. switch( high_level_error_code )
  143. {
  144. /* Begin Auto-Generated Code. */
  145. #if defined(MBEDTLS_CIPHER_C)
  146. case -(MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE):
  147. return( "CIPHER - The selected feature is not available" );
  148. case -(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA):
  149. return( "CIPHER - Bad input parameters" );
  150. case -(MBEDTLS_ERR_CIPHER_ALLOC_FAILED):
  151. return( "CIPHER - Failed to allocate memory" );
  152. case -(MBEDTLS_ERR_CIPHER_INVALID_PADDING):
  153. return( "CIPHER - Input data contains invalid padding and is rejected" );
  154. case -(MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED):
  155. return( "CIPHER - Decryption of block requires a full block" );
  156. case -(MBEDTLS_ERR_CIPHER_AUTH_FAILED):
  157. return( "CIPHER - Authentication failed (for AEAD modes)" );
  158. case -(MBEDTLS_ERR_CIPHER_INVALID_CONTEXT):
  159. return( "CIPHER - The context is invalid. For example, because it was freed" );
  160. #endif /* MBEDTLS_CIPHER_C */
  161. #if defined(MBEDTLS_DHM_C)
  162. case -(MBEDTLS_ERR_DHM_BAD_INPUT_DATA):
  163. return( "DHM - Bad input parameters" );
  164. case -(MBEDTLS_ERR_DHM_READ_PARAMS_FAILED):
  165. return( "DHM - Reading of the DHM parameters failed" );
  166. case -(MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED):
  167. return( "DHM - Making of the DHM parameters failed" );
  168. case -(MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED):
  169. return( "DHM - Reading of the public values failed" );
  170. case -(MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED):
  171. return( "DHM - Making of the public value failed" );
  172. case -(MBEDTLS_ERR_DHM_CALC_SECRET_FAILED):
  173. return( "DHM - Calculation of the DHM secret failed" );
  174. case -(MBEDTLS_ERR_DHM_INVALID_FORMAT):
  175. return( "DHM - The ASN.1 data is not formatted correctly" );
  176. case -(MBEDTLS_ERR_DHM_ALLOC_FAILED):
  177. return( "DHM - Allocation of memory failed" );
  178. case -(MBEDTLS_ERR_DHM_FILE_IO_ERROR):
  179. return( "DHM - Read or write of file failed" );
  180. case -(MBEDTLS_ERR_DHM_SET_GROUP_FAILED):
  181. return( "DHM - Setting the modulus and generator failed" );
  182. #endif /* MBEDTLS_DHM_C */
  183. #if defined(MBEDTLS_ECP_C)
  184. case -(MBEDTLS_ERR_ECP_BAD_INPUT_DATA):
  185. return( "ECP - Bad input parameters to function" );
  186. case -(MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL):
  187. return( "ECP - The buffer is too small to write to" );
  188. case -(MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE):
  189. return( "ECP - The requested feature is not available, for example, the requested curve is not supported" );
  190. case -(MBEDTLS_ERR_ECP_VERIFY_FAILED):
  191. return( "ECP - The signature is not valid" );
  192. case -(MBEDTLS_ERR_ECP_ALLOC_FAILED):
  193. return( "ECP - Memory allocation failed" );
  194. case -(MBEDTLS_ERR_ECP_RANDOM_FAILED):
  195. return( "ECP - Generation of random value, such as ephemeral key, failed" );
  196. case -(MBEDTLS_ERR_ECP_INVALID_KEY):
  197. return( "ECP - Invalid private or public key" );
  198. case -(MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH):
  199. return( "ECP - The buffer contains a valid signature followed by more data" );
  200. case -(MBEDTLS_ERR_ECP_IN_PROGRESS):
  201. return( "ECP - Operation in progress, call again with the same parameters to continue" );
  202. #endif /* MBEDTLS_ECP_C */
  203. #if defined(MBEDTLS_MD_C)
  204. case -(MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE):
  205. return( "MD - The selected feature is not available" );
  206. case -(MBEDTLS_ERR_MD_BAD_INPUT_DATA):
  207. return( "MD - Bad input parameters to function" );
  208. case -(MBEDTLS_ERR_MD_ALLOC_FAILED):
  209. return( "MD - Failed to allocate memory" );
  210. case -(MBEDTLS_ERR_MD_FILE_IO_ERROR):
  211. return( "MD - Opening or reading of file failed" );
  212. #endif /* MBEDTLS_MD_C */
  213. #if defined(MBEDTLS_PEM_PARSE_C) || defined(MBEDTLS_PEM_WRITE_C)
  214. case -(MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT):
  215. return( "PEM - No PEM header or footer found" );
  216. case -(MBEDTLS_ERR_PEM_INVALID_DATA):
  217. return( "PEM - PEM string is not as expected" );
  218. case -(MBEDTLS_ERR_PEM_ALLOC_FAILED):
  219. return( "PEM - Failed to allocate memory" );
  220. case -(MBEDTLS_ERR_PEM_INVALID_ENC_IV):
  221. return( "PEM - RSA IV is not in hex-format" );
  222. case -(MBEDTLS_ERR_PEM_UNKNOWN_ENC_ALG):
  223. return( "PEM - Unsupported key encryption algorithm" );
  224. case -(MBEDTLS_ERR_PEM_PASSWORD_REQUIRED):
  225. return( "PEM - Private key password can't be empty" );
  226. case -(MBEDTLS_ERR_PEM_PASSWORD_MISMATCH):
  227. return( "PEM - Given private key password does not allow for correct decryption" );
  228. case -(MBEDTLS_ERR_PEM_FEATURE_UNAVAILABLE):
  229. return( "PEM - Unavailable feature, e.g. hashing/encryption combination" );
  230. case -(MBEDTLS_ERR_PEM_BAD_INPUT_DATA):
  231. return( "PEM - Bad input parameters to function" );
  232. #endif /* MBEDTLS_PEM_PARSE_C || MBEDTLS_PEM_WRITE_C */
  233. #if defined(MBEDTLS_PK_C)
  234. case -(MBEDTLS_ERR_PK_ALLOC_FAILED):
  235. return( "PK - Memory allocation failed" );
  236. case -(MBEDTLS_ERR_PK_TYPE_MISMATCH):
  237. return( "PK - Type mismatch, eg attempt to encrypt with an ECDSA key" );
  238. case -(MBEDTLS_ERR_PK_BAD_INPUT_DATA):
  239. return( "PK - Bad input parameters to function" );
  240. case -(MBEDTLS_ERR_PK_FILE_IO_ERROR):
  241. return( "PK - Read/write of file failed" );
  242. case -(MBEDTLS_ERR_PK_KEY_INVALID_VERSION):
  243. return( "PK - Unsupported key version" );
  244. case -(MBEDTLS_ERR_PK_KEY_INVALID_FORMAT):
  245. return( "PK - Invalid key tag or value" );
  246. case -(MBEDTLS_ERR_PK_UNKNOWN_PK_ALG):
  247. return( "PK - Key algorithm is unsupported (only RSA and EC are supported)" );
  248. case -(MBEDTLS_ERR_PK_PASSWORD_REQUIRED):
  249. return( "PK - Private key password can't be empty" );
  250. case -(MBEDTLS_ERR_PK_PASSWORD_MISMATCH):
  251. return( "PK - Given private key password does not allow for correct decryption" );
  252. case -(MBEDTLS_ERR_PK_INVALID_PUBKEY):
  253. return( "PK - The pubkey tag or value is invalid (only RSA and EC are supported)" );
  254. case -(MBEDTLS_ERR_PK_INVALID_ALG):
  255. return( "PK - The algorithm tag or value is invalid" );
  256. case -(MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE):
  257. return( "PK - Elliptic curve is unsupported (only NIST curves are supported)" );
  258. case -(MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE):
  259. return( "PK - Unavailable feature, e.g. RSA disabled for RSA key" );
  260. case -(MBEDTLS_ERR_PK_SIG_LEN_MISMATCH):
  261. return( "PK - The buffer contains a valid signature followed by more data" );
  262. case -(MBEDTLS_ERR_PK_BUFFER_TOO_SMALL):
  263. return( "PK - The output buffer is too small" );
  264. #endif /* MBEDTLS_PK_C */
  265. #if defined(MBEDTLS_PKCS12_C)
  266. case -(MBEDTLS_ERR_PKCS12_BAD_INPUT_DATA):
  267. return( "PKCS12 - Bad input parameters to function" );
  268. case -(MBEDTLS_ERR_PKCS12_FEATURE_UNAVAILABLE):
  269. return( "PKCS12 - Feature not available, e.g. unsupported encryption scheme" );
  270. case -(MBEDTLS_ERR_PKCS12_PBE_INVALID_FORMAT):
  271. return( "PKCS12 - PBE ASN.1 data not as expected" );
  272. case -(MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH):
  273. return( "PKCS12 - Given private key password does not allow for correct decryption" );
  274. #endif /* MBEDTLS_PKCS12_C */
  275. #if defined(MBEDTLS_PKCS5_C)
  276. case -(MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA):
  277. return( "PKCS5 - Bad input parameters to function" );
  278. case -(MBEDTLS_ERR_PKCS5_INVALID_FORMAT):
  279. return( "PKCS5 - Unexpected ASN.1 data" );
  280. case -(MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE):
  281. return( "PKCS5 - Requested encryption or digest alg not available" );
  282. case -(MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH):
  283. return( "PKCS5 - Given private key password does not allow for correct decryption" );
  284. #endif /* MBEDTLS_PKCS5_C */
  285. #if defined(MBEDTLS_RSA_C)
  286. case -(MBEDTLS_ERR_RSA_BAD_INPUT_DATA):
  287. return( "RSA - Bad input parameters to function" );
  288. case -(MBEDTLS_ERR_RSA_INVALID_PADDING):
  289. return( "RSA - Input data contains invalid padding and is rejected" );
  290. case -(MBEDTLS_ERR_RSA_KEY_GEN_FAILED):
  291. return( "RSA - Something failed during generation of a key" );
  292. case -(MBEDTLS_ERR_RSA_KEY_CHECK_FAILED):
  293. return( "RSA - Key failed to pass the validity check of the library" );
  294. case -(MBEDTLS_ERR_RSA_PUBLIC_FAILED):
  295. return( "RSA - The public key operation failed" );
  296. case -(MBEDTLS_ERR_RSA_PRIVATE_FAILED):
  297. return( "RSA - The private key operation failed" );
  298. case -(MBEDTLS_ERR_RSA_VERIFY_FAILED):
  299. return( "RSA - The PKCS#1 verification failed" );
  300. case -(MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE):
  301. return( "RSA - The output buffer for decryption is not large enough" );
  302. case -(MBEDTLS_ERR_RSA_RNG_FAILED):
  303. return( "RSA - The random generator failed to generate non-zeros" );
  304. #endif /* MBEDTLS_RSA_C */
  305. #if defined(MBEDTLS_SSL_TLS_C)
  306. case -(MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS):
  307. return( "SSL - A cryptographic operation is in progress. Try again later" );
  308. case -(MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE):
  309. return( "SSL - The requested feature is not available" );
  310. case -(MBEDTLS_ERR_SSL_BAD_INPUT_DATA):
  311. return( "SSL - Bad input parameters to function" );
  312. case -(MBEDTLS_ERR_SSL_INVALID_MAC):
  313. return( "SSL - Verification of the message MAC failed" );
  314. case -(MBEDTLS_ERR_SSL_INVALID_RECORD):
  315. return( "SSL - An invalid SSL record was received" );
  316. case -(MBEDTLS_ERR_SSL_CONN_EOF):
  317. return( "SSL - The connection indicated an EOF" );
  318. case -(MBEDTLS_ERR_SSL_DECODE_ERROR):
  319. return( "SSL - A message could not be parsed due to a syntactic error" );
  320. case -(MBEDTLS_ERR_SSL_NO_RNG):
  321. return( "SSL - No RNG was provided to the SSL module" );
  322. case -(MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE):
  323. return( "SSL - No client certification received from the client, but required by the authentication mode" );
  324. case -(MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION):
  325. return( "SSL - Client received an extended server hello containing an unsupported extension" );
  326. case -(MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL):
  327. return( "SSL - No ALPN protocols supported that the client advertises" );
  328. case -(MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED):
  329. return( "SSL - The own private key or pre-shared key is not set, but needed" );
  330. case -(MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED):
  331. return( "SSL - No CA Chain is set, but required to operate" );
  332. case -(MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE):
  333. return( "SSL - An unexpected message was received from our peer" );
  334. case -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE):
  335. return( "SSL - A fatal alert message was received from our peer" );
  336. case -(MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME):
  337. return( "SSL - No server could be identified matching the client's SNI" );
  338. case -(MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY):
  339. return( "SSL - The peer notified us that the connection is going to be closed" );
  340. case -(MBEDTLS_ERR_SSL_BAD_CERTIFICATE):
  341. return( "SSL - Processing of the Certificate handshake message failed" );
  342. case -(MBEDTLS_ERR_SSL_ALLOC_FAILED):
  343. return( "SSL - Memory allocation failed" );
  344. case -(MBEDTLS_ERR_SSL_HW_ACCEL_FAILED):
  345. return( "SSL - Hardware acceleration function returned with error" );
  346. case -(MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH):
  347. return( "SSL - Hardware acceleration function skipped / left alone data" );
  348. case -(MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION):
  349. return( "SSL - Handshake protocol not within min/max boundaries" );
  350. case -(MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE):
  351. return( "SSL - The handshake negotiation failed" );
  352. case -(MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED):
  353. return( "SSL - Session ticket has expired" );
  354. case -(MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH):
  355. return( "SSL - Public key type mismatch (eg, asked for RSA key exchange and presented EC key)" );
  356. case -(MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY):
  357. return( "SSL - Unknown identity received (eg, PSK identity)" );
  358. case -(MBEDTLS_ERR_SSL_INTERNAL_ERROR):
  359. return( "SSL - Internal error (eg, unexpected failure in lower-level module)" );
  360. case -(MBEDTLS_ERR_SSL_COUNTER_WRAPPING):
  361. return( "SSL - A counter would wrap (eg, too many messages exchanged)" );
  362. case -(MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO):
  363. return( "SSL - Unexpected message at ServerHello in renegotiation" );
  364. case -(MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED):
  365. return( "SSL - DTLS client must retry for hello verification" );
  366. case -(MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL):
  367. return( "SSL - A buffer is too small to receive or write a message" );
  368. case -(MBEDTLS_ERR_SSL_WANT_READ):
  369. return( "SSL - No data of requested type currently available on underlying transport" );
  370. case -(MBEDTLS_ERR_SSL_WANT_WRITE):
  371. return( "SSL - Connection requires a write call" );
  372. case -(MBEDTLS_ERR_SSL_TIMEOUT):
  373. return( "SSL - The operation timed out" );
  374. case -(MBEDTLS_ERR_SSL_CLIENT_RECONNECT):
  375. return( "SSL - The client initiated a reconnect from the same port" );
  376. case -(MBEDTLS_ERR_SSL_UNEXPECTED_RECORD):
  377. return( "SSL - Record header looks valid but is not expected" );
  378. case -(MBEDTLS_ERR_SSL_NON_FATAL):
  379. return( "SSL - The alert message received indicates a non-fatal error" );
  380. case -(MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER):
  381. return( "SSL - A field in a message was incorrect or inconsistent with other fields" );
  382. case -(MBEDTLS_ERR_SSL_CONTINUE_PROCESSING):
  383. return( "SSL - Internal-only message signaling that further message-processing should be done" );
  384. case -(MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS):
  385. return( "SSL - The asynchronous operation is not completed yet" );
  386. case -(MBEDTLS_ERR_SSL_EARLY_MESSAGE):
  387. return( "SSL - Internal-only message signaling that a message arrived early" );
  388. case -(MBEDTLS_ERR_SSL_UNEXPECTED_CID):
  389. return( "SSL - An encrypted DTLS-frame with an unexpected CID was received" );
  390. case -(MBEDTLS_ERR_SSL_VERSION_MISMATCH):
  391. return( "SSL - An operation failed due to an unexpected version or configuration" );
  392. case -(MBEDTLS_ERR_SSL_BAD_CONFIG):
  393. return( "SSL - Invalid value in SSL config" );
  394. #endif /* MBEDTLS_SSL_TLS_C */
  395. #if defined(MBEDTLS_X509_USE_C) || defined(MBEDTLS_X509_CREATE_C)
  396. case -(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE):
  397. return( "X509 - Unavailable feature, e.g. RSA hashing/encryption combination" );
  398. case -(MBEDTLS_ERR_X509_UNKNOWN_OID):
  399. return( "X509 - Requested OID is unknown" );
  400. case -(MBEDTLS_ERR_X509_INVALID_FORMAT):
  401. return( "X509 - The CRT/CRL/CSR format is invalid, e.g. different type expected" );
  402. case -(MBEDTLS_ERR_X509_INVALID_VERSION):
  403. return( "X509 - The CRT/CRL/CSR version element is invalid" );
  404. case -(MBEDTLS_ERR_X509_INVALID_SERIAL):
  405. return( "X509 - The serial tag or value is invalid" );
  406. case -(MBEDTLS_ERR_X509_INVALID_ALG):
  407. return( "X509 - The algorithm tag or value is invalid" );
  408. case -(MBEDTLS_ERR_X509_INVALID_NAME):
  409. return( "X509 - The name tag or value is invalid" );
  410. case -(MBEDTLS_ERR_X509_INVALID_DATE):
  411. return( "X509 - The date tag or value is invalid" );
  412. case -(MBEDTLS_ERR_X509_INVALID_SIGNATURE):
  413. return( "X509 - The signature tag or value invalid" );
  414. case -(MBEDTLS_ERR_X509_INVALID_EXTENSIONS):
  415. return( "X509 - The extension tag or value is invalid" );
  416. case -(MBEDTLS_ERR_X509_UNKNOWN_VERSION):
  417. return( "X509 - CRT/CRL/CSR has an unsupported version number" );
  418. case -(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG):
  419. return( "X509 - Signature algorithm (oid) is unsupported" );
  420. case -(MBEDTLS_ERR_X509_SIG_MISMATCH):
  421. return( "X509 - Signature algorithms do not match. (see \\c ::mbedtls_x509_crt sig_oid)" );
  422. case -(MBEDTLS_ERR_X509_CERT_VERIFY_FAILED):
  423. return( "X509 - Certificate verification failed, e.g. CRL, CA or signature check failed" );
  424. case -(MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT):
  425. return( "X509 - Format not recognized as DER or PEM" );
  426. case -(MBEDTLS_ERR_X509_BAD_INPUT_DATA):
  427. return( "X509 - Input invalid" );
  428. case -(MBEDTLS_ERR_X509_ALLOC_FAILED):
  429. return( "X509 - Allocation of memory failed" );
  430. case -(MBEDTLS_ERR_X509_FILE_IO_ERROR):
  431. return( "X509 - Read/write of file failed" );
  432. case -(MBEDTLS_ERR_X509_BUFFER_TOO_SMALL):
  433. return( "X509 - Destination buffer is too small" );
  434. case -(MBEDTLS_ERR_X509_FATAL_ERROR):
  435. return( "X509 - A fatal error occurred, eg the chain is too long or the vrfy callback failed" );
  436. #endif /* MBEDTLS_X509_USE_C || MBEDTLS_X509_CREATE_C */
  437. /* End Auto-Generated Code. */
  438. default:
  439. break;
  440. }
  441. return( NULL );
  442. }
  443. const char * mbedtls_low_level_strerr( int error_code )
  444. {
  445. int low_level_error_code;
  446. if( error_code < 0 )
  447. error_code = -error_code;
  448. /* Extract the low-level part from the error code. */
  449. low_level_error_code = error_code & ~0xFF80;
  450. switch( low_level_error_code )
  451. {
  452. /* Begin Auto-Generated Code. */
  453. #if defined(MBEDTLS_AES_C)
  454. case -(MBEDTLS_ERR_AES_INVALID_KEY_LENGTH):
  455. return( "AES - Invalid key length" );
  456. case -(MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH):
  457. return( "AES - Invalid data input length" );
  458. case -(MBEDTLS_ERR_AES_BAD_INPUT_DATA):
  459. return( "AES - Invalid input data" );
  460. #endif /* MBEDTLS_AES_C */
  461. #if defined(MBEDTLS_ARIA_C)
  462. case -(MBEDTLS_ERR_ARIA_BAD_INPUT_DATA):
  463. return( "ARIA - Bad input data" );
  464. case -(MBEDTLS_ERR_ARIA_INVALID_INPUT_LENGTH):
  465. return( "ARIA - Invalid data input length" );
  466. #endif /* MBEDTLS_ARIA_C */
  467. #if defined(MBEDTLS_ASN1_PARSE_C)
  468. case -(MBEDTLS_ERR_ASN1_OUT_OF_DATA):
  469. return( "ASN1 - Out of data when parsing an ASN1 data structure" );
  470. case -(MBEDTLS_ERR_ASN1_UNEXPECTED_TAG):
  471. return( "ASN1 - ASN1 tag was of an unexpected value" );
  472. case -(MBEDTLS_ERR_ASN1_INVALID_LENGTH):
  473. return( "ASN1 - Error when trying to determine the length or invalid length" );
  474. case -(MBEDTLS_ERR_ASN1_LENGTH_MISMATCH):
  475. return( "ASN1 - Actual length differs from expected length" );
  476. case -(MBEDTLS_ERR_ASN1_INVALID_DATA):
  477. return( "ASN1 - Data is invalid" );
  478. case -(MBEDTLS_ERR_ASN1_ALLOC_FAILED):
  479. return( "ASN1 - Memory allocation failed" );
  480. case -(MBEDTLS_ERR_ASN1_BUF_TOO_SMALL):
  481. return( "ASN1 - Buffer too small when writing ASN.1 data structure" );
  482. #endif /* MBEDTLS_ASN1_PARSE_C */
  483. #if defined(MBEDTLS_BASE64_C)
  484. case -(MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL):
  485. return( "BASE64 - Output buffer too small" );
  486. case -(MBEDTLS_ERR_BASE64_INVALID_CHARACTER):
  487. return( "BASE64 - Invalid character in input" );
  488. #endif /* MBEDTLS_BASE64_C */
  489. #if defined(MBEDTLS_BIGNUM_C)
  490. case -(MBEDTLS_ERR_MPI_FILE_IO_ERROR):
  491. return( "BIGNUM - An error occurred while reading from or writing to a file" );
  492. case -(MBEDTLS_ERR_MPI_BAD_INPUT_DATA):
  493. return( "BIGNUM - Bad input parameters to function" );
  494. case -(MBEDTLS_ERR_MPI_INVALID_CHARACTER):
  495. return( "BIGNUM - There is an invalid character in the digit string" );
  496. case -(MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL):
  497. return( "BIGNUM - The buffer is too small to write to" );
  498. case -(MBEDTLS_ERR_MPI_NEGATIVE_VALUE):
  499. return( "BIGNUM - The input arguments are negative or result in illegal output" );
  500. case -(MBEDTLS_ERR_MPI_DIVISION_BY_ZERO):
  501. return( "BIGNUM - The input argument for division is zero, which is not allowed" );
  502. case -(MBEDTLS_ERR_MPI_NOT_ACCEPTABLE):
  503. return( "BIGNUM - The input arguments are not acceptable" );
  504. case -(MBEDTLS_ERR_MPI_ALLOC_FAILED):
  505. return( "BIGNUM - Memory allocation failed" );
  506. #endif /* MBEDTLS_BIGNUM_C */
  507. #if defined(MBEDTLS_CAMELLIA_C)
  508. case -(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA):
  509. return( "CAMELLIA - Bad input data" );
  510. case -(MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH):
  511. return( "CAMELLIA - Invalid data input length" );
  512. #endif /* MBEDTLS_CAMELLIA_C */
  513. #if defined(MBEDTLS_CCM_C)
  514. case -(MBEDTLS_ERR_CCM_BAD_INPUT):
  515. return( "CCM - Bad input parameters to the function" );
  516. case -(MBEDTLS_ERR_CCM_AUTH_FAILED):
  517. return( "CCM - Authenticated decryption failed" );
  518. #endif /* MBEDTLS_CCM_C */
  519. #if defined(MBEDTLS_CHACHA20_C)
  520. case -(MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA):
  521. return( "CHACHA20 - Invalid input parameter(s)" );
  522. #endif /* MBEDTLS_CHACHA20_C */
  523. #if defined(MBEDTLS_CHACHAPOLY_C)
  524. case -(MBEDTLS_ERR_CHACHAPOLY_BAD_STATE):
  525. return( "CHACHAPOLY - The requested operation is not permitted in the current state" );
  526. case -(MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED):
  527. return( "CHACHAPOLY - Authenticated decryption failed: data was not authentic" );
  528. #endif /* MBEDTLS_CHACHAPOLY_C */
  529. #if defined(MBEDTLS_CTR_DRBG_C)
  530. case -(MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED):
  531. return( "CTR_DRBG - The entropy source failed" );
  532. case -(MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG):
  533. return( "CTR_DRBG - The requested random buffer length is too big" );
  534. case -(MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG):
  535. return( "CTR_DRBG - The input (entropy + additional data) is too large" );
  536. case -(MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR):
  537. return( "CTR_DRBG - Read or write error in file" );
  538. #endif /* MBEDTLS_CTR_DRBG_C */
  539. #if defined(MBEDTLS_DES_C)
  540. case -(MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH):
  541. return( "DES - The data input has an invalid length" );
  542. #endif /* MBEDTLS_DES_C */
  543. #if defined(MBEDTLS_ENTROPY_C)
  544. case -(MBEDTLS_ERR_ENTROPY_SOURCE_FAILED):
  545. return( "ENTROPY - Critical entropy source failure" );
  546. case -(MBEDTLS_ERR_ENTROPY_MAX_SOURCES):
  547. return( "ENTROPY - No more sources can be added" );
  548. case -(MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED):
  549. return( "ENTROPY - No sources have been added to poll" );
  550. case -(MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE):
  551. return( "ENTROPY - No strong sources have been added to poll" );
  552. case -(MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR):
  553. return( "ENTROPY - Read/write error in file" );
  554. #endif /* MBEDTLS_ENTROPY_C */
  555. #if defined(MBEDTLS_ERROR_C)
  556. case -(MBEDTLS_ERR_ERROR_GENERIC_ERROR):
  557. return( "ERROR - Generic error" );
  558. case -(MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED):
  559. return( "ERROR - This is a bug in the library" );
  560. #endif /* MBEDTLS_ERROR_C */
  561. #if defined(MBEDTLS_PLATFORM_C)
  562. case -(MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED):
  563. return( "PLATFORM - Hardware accelerator failed" );
  564. case -(MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED):
  565. return( "PLATFORM - The requested feature is not supported by the platform" );
  566. #endif /* MBEDTLS_PLATFORM_C */
  567. #if defined(MBEDTLS_GCM_C)
  568. case -(MBEDTLS_ERR_GCM_AUTH_FAILED):
  569. return( "GCM - Authenticated decryption failed" );
  570. case -(MBEDTLS_ERR_GCM_BAD_INPUT):
  571. return( "GCM - Bad input parameters to function" );
  572. case -(MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL):
  573. return( "GCM - An output buffer is too small" );
  574. #endif /* MBEDTLS_GCM_C */
  575. #if defined(MBEDTLS_HKDF_C)
  576. case -(MBEDTLS_ERR_HKDF_BAD_INPUT_DATA):
  577. return( "HKDF - Bad input parameters to function" );
  578. #endif /* MBEDTLS_HKDF_C */
  579. #if defined(MBEDTLS_HMAC_DRBG_C)
  580. case -(MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG):
  581. return( "HMAC_DRBG - Too many random requested in single call" );
  582. case -(MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG):
  583. return( "HMAC_DRBG - Input too large (Entropy + additional)" );
  584. case -(MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR):
  585. return( "HMAC_DRBG - Read/write error in file" );
  586. case -(MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED):
  587. return( "HMAC_DRBG - The entropy source failed" );
  588. #endif /* MBEDTLS_HMAC_DRBG_C */
  589. #if defined(MBEDTLS_NET_C)
  590. case -(MBEDTLS_ERR_NET_SOCKET_FAILED):
  591. return( "NET - Failed to open a socket" );
  592. case -(MBEDTLS_ERR_NET_CONNECT_FAILED):
  593. return( "NET - The connection to the given server / port failed" );
  594. case -(MBEDTLS_ERR_NET_BIND_FAILED):
  595. return( "NET - Binding of the socket failed" );
  596. case -(MBEDTLS_ERR_NET_LISTEN_FAILED):
  597. return( "NET - Could not listen on the socket" );
  598. case -(MBEDTLS_ERR_NET_ACCEPT_FAILED):
  599. return( "NET - Could not accept the incoming connection" );
  600. case -(MBEDTLS_ERR_NET_RECV_FAILED):
  601. return( "NET - Reading information from the socket failed" );
  602. case -(MBEDTLS_ERR_NET_SEND_FAILED):
  603. return( "NET - Sending information through the socket failed" );
  604. case -(MBEDTLS_ERR_NET_CONN_RESET):
  605. return( "NET - Connection was reset by peer" );
  606. case -(MBEDTLS_ERR_NET_UNKNOWN_HOST):
  607. return( "NET - Failed to get an IP address for the given hostname" );
  608. case -(MBEDTLS_ERR_NET_BUFFER_TOO_SMALL):
  609. return( "NET - Buffer is too small to hold the data" );
  610. case -(MBEDTLS_ERR_NET_INVALID_CONTEXT):
  611. return( "NET - The context is invalid, eg because it was free()ed" );
  612. case -(MBEDTLS_ERR_NET_POLL_FAILED):
  613. return( "NET - Polling the net context failed" );
  614. case -(MBEDTLS_ERR_NET_BAD_INPUT_DATA):
  615. return( "NET - Input invalid" );
  616. #endif /* MBEDTLS_NET_C */
  617. #if defined(MBEDTLS_OID_C)
  618. case -(MBEDTLS_ERR_OID_NOT_FOUND):
  619. return( "OID - OID is not found" );
  620. case -(MBEDTLS_ERR_OID_BUF_TOO_SMALL):
  621. return( "OID - output buffer is too small" );
  622. #endif /* MBEDTLS_OID_C */
  623. #if defined(MBEDTLS_POLY1305_C)
  624. case -(MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA):
  625. return( "POLY1305 - Invalid input parameter(s)" );
  626. #endif /* MBEDTLS_POLY1305_C */
  627. #if defined(MBEDTLS_SHA1_C)
  628. case -(MBEDTLS_ERR_SHA1_BAD_INPUT_DATA):
  629. return( "SHA1 - SHA-1 input data was malformed" );
  630. #endif /* MBEDTLS_SHA1_C */
  631. #if defined(MBEDTLS_SHA256_C)
  632. case -(MBEDTLS_ERR_SHA256_BAD_INPUT_DATA):
  633. return( "SHA256 - SHA-256 input data was malformed" );
  634. #endif /* MBEDTLS_SHA256_C */
  635. #if defined(MBEDTLS_SHA512_C)
  636. case -(MBEDTLS_ERR_SHA512_BAD_INPUT_DATA):
  637. return( "SHA512 - SHA-512 input data was malformed" );
  638. #endif /* MBEDTLS_SHA512_C */
  639. #if defined(MBEDTLS_THREADING_C)
  640. case -(MBEDTLS_ERR_THREADING_BAD_INPUT_DATA):
  641. return( "THREADING - Bad input parameters to function" );
  642. case -(MBEDTLS_ERR_THREADING_MUTEX_ERROR):
  643. return( "THREADING - Locking / unlocking / free failed with error code" );
  644. #endif /* MBEDTLS_THREADING_C */
  645. /* End Auto-Generated Code. */
  646. default:
  647. break;
  648. }
  649. return( NULL );
  650. }
  651. void mbedtls_strerror( int ret, char *buf, size_t buflen )
  652. {
  653. size_t len;
  654. int use_ret;
  655. const char * high_level_error_description = NULL;
  656. const char * low_level_error_description = NULL;
  657. if( buflen == 0 )
  658. return;
  659. memset( buf, 0x00, buflen );
  660. if( ret < 0 )
  661. ret = -ret;
  662. if( ret & 0xFF80 )
  663. {
  664. use_ret = ret & 0xFF80;
  665. // Translate high level error code.
  666. high_level_error_description = mbedtls_high_level_strerr( ret );
  667. if( high_level_error_description == NULL )
  668. mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret );
  669. else
  670. mbedtls_snprintf( buf, buflen, "%s", high_level_error_description );
  671. #if defined(MBEDTLS_SSL_TLS_C)
  672. // Early return in case of a fatal error - do not try to translate low
  673. // level code.
  674. if(use_ret == -(MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE))
  675. return;
  676. #endif /* MBEDTLS_SSL_TLS_C */
  677. }
  678. use_ret = ret & ~0xFF80;
  679. if( use_ret == 0 )
  680. return;
  681. // If high level code is present, make a concatenation between both
  682. // error strings.
  683. //
  684. len = strlen( buf );
  685. if( len > 0 )
  686. {
  687. if( buflen - len < 5 )
  688. return;
  689. mbedtls_snprintf( buf + len, buflen - len, " : " );
  690. buf += len + 3;
  691. buflen -= len + 3;
  692. }
  693. // Translate low level error code.
  694. low_level_error_description = mbedtls_low_level_strerr( ret );
  695. if( low_level_error_description == NULL )
  696. mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", (unsigned int) use_ret );
  697. else
  698. mbedtls_snprintf( buf, buflen, "%s", low_level_error_description );
  699. }
  700. #else /* MBEDTLS_ERROR_C */
  701. /*
  702. * Provide an non-function in case MBEDTLS_ERROR_C is not defined
  703. */
  704. void mbedtls_strerror( int ret, char *buf, size_t buflen )
  705. {
  706. ((void) ret);
  707. if( buflen > 0 )
  708. buf[0] = '\0';
  709. }
  710. #endif /* MBEDTLS_ERROR_C */
  711. #if defined(MBEDTLS_TEST_HOOKS)
  712. void (*mbedtls_test_hook_error_add)( int, int, const char *, int );
  713. #endif
  714. #endif /* MBEDTLS_ERROR_C || MBEDTLS_ERROR_STRERROR_DUMMY */