PageRenderTime 59ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/library/error.c

https://github.com/leg0/polarssl
C | 574 lines | 463 code | 77 blank | 34 comment | 294 complexity | 1d0aedf4503a9fac8a12818afe914ddb MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * Error message information
  3. *
  4. * Copyright (C) 2006-2012, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #include "polarssl/config.h"
  26. #if defined(POLARSSL_ERROR_C)
  27. #if defined(POLARSSL_AES_C)
  28. #include "polarssl/aes.h"
  29. #endif
  30. #if defined(POLARSSL_BASE64_C)
  31. #include "polarssl/base64.h"
  32. #endif
  33. #if defined(POLARSSL_BIGNUM_C)
  34. #include "polarssl/bignum.h"
  35. #endif
  36. #if defined(POLARSSL_BLOWFISH_C)
  37. #include "polarssl/blowfish.h"
  38. #endif
  39. #if defined(POLARSSL_CAMELLIA_C)
  40. #include "polarssl/camellia.h"
  41. #endif
  42. #if defined(POLARSSL_CIPHER_C)
  43. #include "polarssl/cipher.h"
  44. #endif
  45. #if defined(POLARSSL_CTR_DRBG_C)
  46. #include "polarssl/ctr_drbg.h"
  47. #endif
  48. #if defined(POLARSSL_DES_C)
  49. #include "polarssl/des.h"
  50. #endif
  51. #if defined(POLARSSL_DHM_C)
  52. #include "polarssl/dhm.h"
  53. #endif
  54. #if defined(POLARSSL_ENTROPY_C)
  55. #include "polarssl/entropy.h"
  56. #endif
  57. #if defined(POLARSSL_GCM_C)
  58. #include "polarssl/gcm.h"
  59. #endif
  60. #if defined(POLARSSL_MD_C)
  61. #include "polarssl/md.h"
  62. #endif
  63. #if defined(POLARSSL_MD2_C)
  64. #include "polarssl/md2.h"
  65. #endif
  66. #if defined(POLARSSL_MD4_C)
  67. #include "polarssl/md4.h"
  68. #endif
  69. #if defined(POLARSSL_MD5_C)
  70. #include "polarssl/md5.h"
  71. #endif
  72. #if defined(POLARSSL_NET_C)
  73. #include "polarssl/net.h"
  74. #endif
  75. #if defined(POLARSSL_PADLOCK_C)
  76. #include "polarssl/padlock.h"
  77. #endif
  78. #if defined(POLARSSL_PBKDF2_C)
  79. #include "polarssl/pbkdf2.h"
  80. #endif
  81. #if defined(POLARSSL_PEM_C)
  82. #include "polarssl/pem.h"
  83. #endif
  84. #if defined(POLARSSL_RSA_C)
  85. #include "polarssl/rsa.h"
  86. #endif
  87. #if defined(POLARSSL_SHA1_C)
  88. #include "polarssl/sha1.h"
  89. #endif
  90. #if defined(POLARSSL_SHA2_C)
  91. #include "polarssl/sha2.h"
  92. #endif
  93. #if defined(POLARSSL_SHA4_C)
  94. #include "polarssl/sha4.h"
  95. #endif
  96. #if defined(POLARSSL_SSL_TLS_C)
  97. #include "polarssl/ssl.h"
  98. #endif
  99. #if defined(POLARSSL_X509_PARSE_C)
  100. #include "polarssl/x509.h"
  101. #endif
  102. #if defined(POLARSSL_XTEA_C)
  103. #include "polarssl/xtea.h"
  104. #endif
  105. #include <string.h>
  106. #if defined _MSC_VER && !defined snprintf
  107. #define snprintf _snprintf
  108. #endif
  109. void error_strerror( int ret, char *buf, size_t buflen )
  110. {
  111. size_t len;
  112. int use_ret;
  113. memset( buf, 0x00, buflen );
  114. if( ret < 0 )
  115. ret = -ret;
  116. if( ret & 0xFF80 )
  117. {
  118. use_ret = ret & 0xFF80;
  119. // High level error codes
  120. //
  121. #if defined(POLARSSL_CIPHER_C)
  122. if( use_ret == -(POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE) )
  123. snprintf( buf, buflen, "CIPHER - The selected feature is not available" );
  124. if( use_ret == -(POLARSSL_ERR_CIPHER_BAD_INPUT_DATA) )
  125. snprintf( buf, buflen, "CIPHER - Bad input parameters to function" );
  126. if( use_ret == -(POLARSSL_ERR_CIPHER_ALLOC_FAILED) )
  127. snprintf( buf, buflen, "CIPHER - Failed to allocate memory" );
  128. if( use_ret == -(POLARSSL_ERR_CIPHER_INVALID_PADDING) )
  129. snprintf( buf, buflen, "CIPHER - Input data contains invalid padding and is rejected" );
  130. if( use_ret == -(POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED) )
  131. snprintf( buf, buflen, "CIPHER - Decryption of block requires a full block" );
  132. #endif /* POLARSSL_CIPHER_C */
  133. #if defined(POLARSSL_DHM_C)
  134. if( use_ret == -(POLARSSL_ERR_DHM_BAD_INPUT_DATA) )
  135. snprintf( buf, buflen, "DHM - Bad input parameters to function" );
  136. if( use_ret == -(POLARSSL_ERR_DHM_READ_PARAMS_FAILED) )
  137. snprintf( buf, buflen, "DHM - Reading of the DHM parameters failed" );
  138. if( use_ret == -(POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED) )
  139. snprintf( buf, buflen, "DHM - Making of the DHM parameters failed" );
  140. if( use_ret == -(POLARSSL_ERR_DHM_READ_PUBLIC_FAILED) )
  141. snprintf( buf, buflen, "DHM - Reading of the public values failed" );
  142. if( use_ret == -(POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED) )
  143. snprintf( buf, buflen, "DHM - Making of the public value failed" );
  144. if( use_ret == -(POLARSSL_ERR_DHM_CALC_SECRET_FAILED) )
  145. snprintf( buf, buflen, "DHM - Calculation of the DHM secret failed" );
  146. #endif /* POLARSSL_DHM_C */
  147. #if defined(POLARSSL_MD_C)
  148. if( use_ret == -(POLARSSL_ERR_MD_FEATURE_UNAVAILABLE) )
  149. snprintf( buf, buflen, "MD - The selected feature is not available" );
  150. if( use_ret == -(POLARSSL_ERR_MD_BAD_INPUT_DATA) )
  151. snprintf( buf, buflen, "MD - Bad input parameters to function" );
  152. if( use_ret == -(POLARSSL_ERR_MD_ALLOC_FAILED) )
  153. snprintf( buf, buflen, "MD - Failed to allocate memory" );
  154. if( use_ret == -(POLARSSL_ERR_MD_FILE_IO_ERROR) )
  155. snprintf( buf, buflen, "MD - Opening or reading of file failed" );
  156. #endif /* POLARSSL_MD_C */
  157. #if defined(POLARSSL_PEM_C)
  158. if( use_ret == -(POLARSSL_ERR_PEM_NO_HEADER_PRESENT) )
  159. snprintf( buf, buflen, "PEM - No PEM header found" );
  160. if( use_ret == -(POLARSSL_ERR_PEM_INVALID_DATA) )
  161. snprintf( buf, buflen, "PEM - PEM string is not as expected" );
  162. if( use_ret == -(POLARSSL_ERR_PEM_MALLOC_FAILED) )
  163. snprintf( buf, buflen, "PEM - Failed to allocate memory" );
  164. if( use_ret == -(POLARSSL_ERR_PEM_INVALID_ENC_IV) )
  165. snprintf( buf, buflen, "PEM - RSA IV is not in hex-format" );
  166. if( use_ret == -(POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG) )
  167. snprintf( buf, buflen, "PEM - Unsupported key encryption algorithm" );
  168. if( use_ret == -(POLARSSL_ERR_PEM_PASSWORD_REQUIRED) )
  169. snprintf( buf, buflen, "PEM - Private key password can't be empty" );
  170. if( use_ret == -(POLARSSL_ERR_PEM_PASSWORD_MISMATCH) )
  171. snprintf( buf, buflen, "PEM - Given private key password does not allow for correct decryption" );
  172. if( use_ret == -(POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE) )
  173. snprintf( buf, buflen, "PEM - Unavailable feature, e.g. hashing/encryption combination" );
  174. #endif /* POLARSSL_PEM_C */
  175. #if defined(POLARSSL_RSA_C)
  176. if( use_ret == -(POLARSSL_ERR_RSA_BAD_INPUT_DATA) )
  177. snprintf( buf, buflen, "RSA - Bad input parameters to function" );
  178. if( use_ret == -(POLARSSL_ERR_RSA_INVALID_PADDING) )
  179. snprintf( buf, buflen, "RSA - Input data contains invalid padding and is rejected" );
  180. if( use_ret == -(POLARSSL_ERR_RSA_KEY_GEN_FAILED) )
  181. snprintf( buf, buflen, "RSA - Something failed during generation of a key" );
  182. if( use_ret == -(POLARSSL_ERR_RSA_KEY_CHECK_FAILED) )
  183. snprintf( buf, buflen, "RSA - Key failed to pass the libraries validity check" );
  184. if( use_ret == -(POLARSSL_ERR_RSA_PUBLIC_FAILED) )
  185. snprintf( buf, buflen, "RSA - The public key operation failed" );
  186. if( use_ret == -(POLARSSL_ERR_RSA_PRIVATE_FAILED) )
  187. snprintf( buf, buflen, "RSA - The private key operation failed" );
  188. if( use_ret == -(POLARSSL_ERR_RSA_VERIFY_FAILED) )
  189. snprintf( buf, buflen, "RSA - The PKCS#1 verification failed" );
  190. if( use_ret == -(POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE) )
  191. snprintf( buf, buflen, "RSA - The output buffer for decryption is not large enough" );
  192. if( use_ret == -(POLARSSL_ERR_RSA_RNG_FAILED) )
  193. snprintf( buf, buflen, "RSA - The random generator failed to generate non-zeros" );
  194. #endif /* POLARSSL_RSA_C */
  195. #if defined(POLARSSL_SSL_TLS_C)
  196. if( use_ret == -(POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE) )
  197. snprintf( buf, buflen, "SSL - The requested feature is not available" );
  198. if( use_ret == -(POLARSSL_ERR_SSL_BAD_INPUT_DATA) )
  199. snprintf( buf, buflen, "SSL - Bad input parameters to function" );
  200. if( use_ret == -(POLARSSL_ERR_SSL_INVALID_MAC) )
  201. snprintf( buf, buflen, "SSL - Verification of the message MAC failed" );
  202. if( use_ret == -(POLARSSL_ERR_SSL_INVALID_RECORD) )
  203. snprintf( buf, buflen, "SSL - An invalid SSL record was received" );
  204. if( use_ret == -(POLARSSL_ERR_SSL_CONN_EOF) )
  205. snprintf( buf, buflen, "SSL - The connection indicated an EOF" );
  206. if( use_ret == -(POLARSSL_ERR_SSL_UNKNOWN_CIPHER) )
  207. snprintf( buf, buflen, "SSL - An unknown cipher was received" );
  208. if( use_ret == -(POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN) )
  209. snprintf( buf, buflen, "SSL - The server has no ciphersuites in common with the client" );
  210. if( use_ret == -(POLARSSL_ERR_SSL_NO_SESSION_FOUND) )
  211. snprintf( buf, buflen, "SSL - No session to recover was found" );
  212. if( use_ret == -(POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE) )
  213. snprintf( buf, buflen, "SSL - No client certification received from the client, but required by the authentication mode" );
  214. if( use_ret == -(POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE) )
  215. snprintf( buf, buflen, "SSL - DESCRIPTION MISSING" );
  216. if( use_ret == -(POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED) )
  217. snprintf( buf, buflen, "SSL - The own certificate is not set, but needed by the server" );
  218. if( use_ret == -(POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED) )
  219. snprintf( buf, buflen, "SSL - The own private key is not set, but needed" );
  220. if( use_ret == -(POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED) )
  221. snprintf( buf, buflen, "SSL - No CA Chain is set, but required to operate" );
  222. if( use_ret == -(POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE) )
  223. snprintf( buf, buflen, "SSL - An unexpected message was received from our peer" );
  224. if( use_ret == -(POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE) )
  225. {
  226. snprintf( buf, buflen, "SSL - A fatal alert message was received from our peer" );
  227. return;
  228. }
  229. if( use_ret == -(POLARSSL_ERR_SSL_PEER_VERIFY_FAILED) )
  230. snprintf( buf, buflen, "SSL - Verification of our peer failed" );
  231. if( use_ret == -(POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY) )
  232. snprintf( buf, buflen, "SSL - The peer notified us that the connection is going to be closed" );
  233. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO) )
  234. snprintf( buf, buflen, "SSL - Processing of the ClientHello handshake message failed" );
  235. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO) )
  236. snprintf( buf, buflen, "SSL - Processing of the ServerHello handshake message failed" );
  237. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE) )
  238. snprintf( buf, buflen, "SSL - Processing of the Certificate handshake message failed" );
  239. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST) )
  240. snprintf( buf, buflen, "SSL - Processing of the CertificateRequest handshake message failed" );
  241. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE) )
  242. snprintf( buf, buflen, "SSL - Processing of the ServerKeyExchange handshake message failed" );
  243. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE) )
  244. snprintf( buf, buflen, "SSL - Processing of the ServerHelloDone handshake message failed" );
  245. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE) )
  246. snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed" );
  247. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_DHM_RP) )
  248. snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed in DHM Read Public" );
  249. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_DHM_CS) )
  250. snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed in DHM Calculate Secret" );
  251. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY) )
  252. snprintf( buf, buflen, "SSL - Processing of the CertificateVerify handshake message failed" );
  253. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC) )
  254. snprintf( buf, buflen, "SSL - Processing of the ChangeCipherSpec handshake message failed" );
  255. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_FINISHED) )
  256. snprintf( buf, buflen, "SSL - Processing of the Finished handshake message failed" );
  257. if( use_ret == -(POLARSSL_ERR_SSL_MALLOC_FAILED) )
  258. snprintf( buf, buflen, "SSL - Memory allocation failed" );
  259. if( use_ret == -(POLARSSL_ERR_SSL_HW_ACCEL_FAILED) )
  260. snprintf( buf, buflen, "SSL - Hardware acceleration function returned with error" );
  261. if( use_ret == -(POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH) )
  262. snprintf( buf, buflen, "SSL - Hardware acceleration function skipped / left alone data" );
  263. if( use_ret == -(POLARSSL_ERR_SSL_COMPRESSION_FAILED) )
  264. snprintf( buf, buflen, "SSL - Processing of the compression / decompression failed" );
  265. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION) )
  266. snprintf( buf, buflen, "SSL - Handshake protocol not within min/max boundaries" );
  267. #endif /* POLARSSL_SSL_TLS_C */
  268. #if defined(POLARSSL_X509_PARSE_C)
  269. if( use_ret == -(POLARSSL_ERR_X509_FEATURE_UNAVAILABLE) )
  270. snprintf( buf, buflen, "X509 - Unavailable feature, e.g. RSA hashing/encryption combination" );
  271. if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_PEM) )
  272. snprintf( buf, buflen, "X509 - The PEM-encoded certificate contains invalid elements, e.g. invalid character" );
  273. if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_FORMAT) )
  274. snprintf( buf, buflen, "X509 - The certificate format is invalid, e.g. different type expected" );
  275. if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_VERSION) )
  276. snprintf( buf, buflen, "X509 - The certificate version element is invalid" );
  277. if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_SERIAL) )
  278. snprintf( buf, buflen, "X509 - The serial tag or value is invalid" );
  279. if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_ALG) )
  280. snprintf( buf, buflen, "X509 - The algorithm tag or value is invalid" );
  281. if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_NAME) )
  282. snprintf( buf, buflen, "X509 - The name tag or value is invalid" );
  283. if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_DATE) )
  284. snprintf( buf, buflen, "X509 - The date tag or value is invalid" );
  285. if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_PUBKEY) )
  286. snprintf( buf, buflen, "X509 - The pubkey tag or value is invalid (only RSA is supported)" );
  287. if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE) )
  288. snprintf( buf, buflen, "X509 - The signature tag or value invalid" );
  289. if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS) )
  290. snprintf( buf, buflen, "X509 - The extension tag or value is invalid" );
  291. if( use_ret == -(POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION) )
  292. snprintf( buf, buflen, "X509 - Certificate or CRL has an unsupported version number" );
  293. if( use_ret == -(POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG) )
  294. snprintf( buf, buflen, "X509 - Signature algorithm (oid) is unsupported" );
  295. if( use_ret == -(POLARSSL_ERR_X509_UNKNOWN_PK_ALG) )
  296. snprintf( buf, buflen, "X509 - Key algorithm is unsupported (only RSA is supported)" );
  297. if( use_ret == -(POLARSSL_ERR_X509_CERT_SIG_MISMATCH) )
  298. snprintf( buf, buflen, "X509 - Certificate signature algorithms do not match. (see \\c ::x509_cert sig_oid)" );
  299. if( use_ret == -(POLARSSL_ERR_X509_CERT_VERIFY_FAILED) )
  300. snprintf( buf, buflen, "X509 - Certificate verification failed, e.g. CRL, CA or signature check failed" );
  301. if( use_ret == -(POLARSSL_ERR_X509_KEY_INVALID_VERSION) )
  302. snprintf( buf, buflen, "X509 - Unsupported RSA key version" );
  303. if( use_ret == -(POLARSSL_ERR_X509_KEY_INVALID_FORMAT) )
  304. snprintf( buf, buflen, "X509 - Invalid RSA key tag or value" );
  305. if( use_ret == -(POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT) )
  306. snprintf( buf, buflen, "X509 - Format not recognized as DER or PEM" );
  307. if( use_ret == -(POLARSSL_ERR_X509_INVALID_INPUT) )
  308. snprintf( buf, buflen, "X509 - Input invalid" );
  309. if( use_ret == -(POLARSSL_ERR_X509_MALLOC_FAILED) )
  310. snprintf( buf, buflen, "X509 - Allocation of memory failed" );
  311. if( use_ret == -(POLARSSL_ERR_X509_FILE_IO_ERROR) )
  312. snprintf( buf, buflen, "X509 - Read/write of file failed" );
  313. #endif /* POLARSSL_X509_PARSE_C */
  314. if( strlen( buf ) == 0 )
  315. snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
  316. }
  317. use_ret = ret & ~0xFF80;
  318. if( use_ret == 0 )
  319. return;
  320. // If high level code is present, make a concatenation between both
  321. // error strings.
  322. //
  323. len = strlen( buf );
  324. if( len > 0 )
  325. {
  326. if( buflen - len < 5 )
  327. return;
  328. snprintf( buf + len, buflen - len, " : " );
  329. buf += len + 3;
  330. buflen -= len + 3;
  331. }
  332. // Low level error codes
  333. //
  334. #if defined(POLARSSL_AES_C)
  335. if( use_ret == -(POLARSSL_ERR_AES_INVALID_KEY_LENGTH) )
  336. snprintf( buf, buflen, "AES - Invalid key length" );
  337. if( use_ret == -(POLARSSL_ERR_AES_INVALID_INPUT_LENGTH) )
  338. snprintf( buf, buflen, "AES - Invalid data input length" );
  339. #endif /* POLARSSL_AES_C */
  340. #if defined(POLARSSL_ASN1_PARSE_C)
  341. if( use_ret == -(POLARSSL_ERR_ASN1_OUT_OF_DATA) )
  342. snprintf( buf, buflen, "ASN1 - Out of data when parsing an ASN1 data structure" );
  343. if( use_ret == -(POLARSSL_ERR_ASN1_UNEXPECTED_TAG) )
  344. snprintf( buf, buflen, "ASN1 - ASN1 tag was of an unexpected value" );
  345. if( use_ret == -(POLARSSL_ERR_ASN1_INVALID_LENGTH) )
  346. snprintf( buf, buflen, "ASN1 - Error when trying to determine the length or invalid length" );
  347. if( use_ret == -(POLARSSL_ERR_ASN1_LENGTH_MISMATCH) )
  348. snprintf( buf, buflen, "ASN1 - Actual length differs from expected length" );
  349. if( use_ret == -(POLARSSL_ERR_ASN1_INVALID_DATA) )
  350. snprintf( buf, buflen, "ASN1 - Data is invalid. (not used)" );
  351. if( use_ret == -(POLARSSL_ERR_ASN1_MALLOC_FAILED) )
  352. snprintf( buf, buflen, "ASN1 - Memory allocation failed" );
  353. if( use_ret == -(POLARSSL_ERR_ASN1_BUF_TOO_SMALL) )
  354. snprintf( buf, buflen, "ASN1 - Buffer too small when writing ASN.1 data structure" );
  355. #endif /* POLARSSL_ASN1_PARSE_C */
  356. #if defined(POLARSSL_BASE64_C)
  357. if( use_ret == -(POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL) )
  358. snprintf( buf, buflen, "BASE64 - Output buffer too small" );
  359. if( use_ret == -(POLARSSL_ERR_BASE64_INVALID_CHARACTER) )
  360. snprintf( buf, buflen, "BASE64 - Invalid character in input" );
  361. #endif /* POLARSSL_BASE64_C */
  362. #if defined(POLARSSL_BIGNUM_C)
  363. if( use_ret == -(POLARSSL_ERR_MPI_FILE_IO_ERROR) )
  364. snprintf( buf, buflen, "BIGNUM - An error occurred while reading from or writing to a file" );
  365. if( use_ret == -(POLARSSL_ERR_MPI_BAD_INPUT_DATA) )
  366. snprintf( buf, buflen, "BIGNUM - Bad input parameters to function" );
  367. if( use_ret == -(POLARSSL_ERR_MPI_INVALID_CHARACTER) )
  368. snprintf( buf, buflen, "BIGNUM - There is an invalid character in the digit string" );
  369. if( use_ret == -(POLARSSL_ERR_MPI_BUFFER_TOO_SMALL) )
  370. snprintf( buf, buflen, "BIGNUM - The buffer is too small to write to" );
  371. if( use_ret == -(POLARSSL_ERR_MPI_NEGATIVE_VALUE) )
  372. snprintf( buf, buflen, "BIGNUM - The input arguments are negative or result in illegal output" );
  373. if( use_ret == -(POLARSSL_ERR_MPI_DIVISION_BY_ZERO) )
  374. snprintf( buf, buflen, "BIGNUM - The input argument for division is zero, which is not allowed" );
  375. if( use_ret == -(POLARSSL_ERR_MPI_NOT_ACCEPTABLE) )
  376. snprintf( buf, buflen, "BIGNUM - The input arguments are not acceptable" );
  377. if( use_ret == -(POLARSSL_ERR_MPI_MALLOC_FAILED) )
  378. snprintf( buf, buflen, "BIGNUM - Memory allocation failed" );
  379. #endif /* POLARSSL_BIGNUM_C */
  380. #if defined(POLARSSL_BLOWFISH_C)
  381. if( use_ret == -(POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH) )
  382. snprintf( buf, buflen, "BLOWFISH - Invalid key length" );
  383. if( use_ret == -(POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH) )
  384. snprintf( buf, buflen, "BLOWFISH - Invalid data input length" );
  385. #endif /* POLARSSL_BLOWFISH_C */
  386. #if defined(POLARSSL_CAMELLIA_C)
  387. if( use_ret == -(POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH) )
  388. snprintf( buf, buflen, "CAMELLIA - Invalid key length" );
  389. if( use_ret == -(POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH) )
  390. snprintf( buf, buflen, "CAMELLIA - Invalid data input length" );
  391. #endif /* POLARSSL_CAMELLIA_C */
  392. #if defined(POLARSSL_CTR_DRBG_C)
  393. if( use_ret == -(POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED) )
  394. snprintf( buf, buflen, "CTR_DRBG - The entropy source failed" );
  395. if( use_ret == -(POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG) )
  396. snprintf( buf, buflen, "CTR_DRBG - Too many random requested in single call" );
  397. if( use_ret == -(POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG) )
  398. snprintf( buf, buflen, "CTR_DRBG - Input too large (Entropy + additional)" );
  399. if( use_ret == -(POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR) )
  400. snprintf( buf, buflen, "CTR_DRBG - Read/write error in file" );
  401. #endif /* POLARSSL_CTR_DRBG_C */
  402. #if defined(POLARSSL_DES_C)
  403. if( use_ret == -(POLARSSL_ERR_DES_INVALID_INPUT_LENGTH) )
  404. snprintf( buf, buflen, "DES - The data input has an invalid length" );
  405. #endif /* POLARSSL_DES_C */
  406. #if defined(POLARSSL_ENTROPY_C)
  407. if( use_ret == -(POLARSSL_ERR_ENTROPY_SOURCE_FAILED) )
  408. snprintf( buf, buflen, "ENTROPY - Critical entropy source failure" );
  409. if( use_ret == -(POLARSSL_ERR_ENTROPY_MAX_SOURCES) )
  410. snprintf( buf, buflen, "ENTROPY - No more sources can be added" );
  411. if( use_ret == -(POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED) )
  412. snprintf( buf, buflen, "ENTROPY - No sources have been added to poll" );
  413. #endif /* POLARSSL_ENTROPY_C */
  414. #if defined(POLARSSL_GCM_C)
  415. if( use_ret == -(POLARSSL_ERR_GCM_AUTH_FAILED) )
  416. snprintf( buf, buflen, "GCM - Authenticated decryption failed" );
  417. if( use_ret == -(POLARSSL_ERR_GCM_BAD_INPUT) )
  418. snprintf( buf, buflen, "GCM - Bad input parameters to function" );
  419. #endif /* POLARSSL_GCM_C */
  420. #if defined(POLARSSL_MD2_C)
  421. if( use_ret == -(POLARSSL_ERR_MD2_FILE_IO_ERROR) )
  422. snprintf( buf, buflen, "MD2 - Read/write error in file" );
  423. #endif /* POLARSSL_MD2_C */
  424. #if defined(POLARSSL_MD4_C)
  425. if( use_ret == -(POLARSSL_ERR_MD4_FILE_IO_ERROR) )
  426. snprintf( buf, buflen, "MD4 - Read/write error in file" );
  427. #endif /* POLARSSL_MD4_C */
  428. #if defined(POLARSSL_MD5_C)
  429. if( use_ret == -(POLARSSL_ERR_MD5_FILE_IO_ERROR) )
  430. snprintf( buf, buflen, "MD5 - Read/write error in file" );
  431. #endif /* POLARSSL_MD5_C */
  432. #if defined(POLARSSL_NET_C)
  433. if( use_ret == -(POLARSSL_ERR_NET_UNKNOWN_HOST) )
  434. snprintf( buf, buflen, "NET - Failed to get an IP address for the given hostname" );
  435. if( use_ret == -(POLARSSL_ERR_NET_SOCKET_FAILED) )
  436. snprintf( buf, buflen, "NET - Failed to open a socket" );
  437. if( use_ret == -(POLARSSL_ERR_NET_CONNECT_FAILED) )
  438. snprintf( buf, buflen, "NET - The connection to the given server / port failed" );
  439. if( use_ret == -(POLARSSL_ERR_NET_BIND_FAILED) )
  440. snprintf( buf, buflen, "NET - Binding of the socket failed" );
  441. if( use_ret == -(POLARSSL_ERR_NET_LISTEN_FAILED) )
  442. snprintf( buf, buflen, "NET - Could not listen on the socket" );
  443. if( use_ret == -(POLARSSL_ERR_NET_ACCEPT_FAILED) )
  444. snprintf( buf, buflen, "NET - Could not accept the incoming connection" );
  445. if( use_ret == -(POLARSSL_ERR_NET_RECV_FAILED) )
  446. snprintf( buf, buflen, "NET - Reading information from the socket failed" );
  447. if( use_ret == -(POLARSSL_ERR_NET_SEND_FAILED) )
  448. snprintf( buf, buflen, "NET - Sending information through the socket failed" );
  449. if( use_ret == -(POLARSSL_ERR_NET_CONN_RESET) )
  450. snprintf( buf, buflen, "NET - Connection was reset by peer" );
  451. if( use_ret == -(POLARSSL_ERR_NET_WANT_READ) )
  452. snprintf( buf, buflen, "NET - Connection requires a read call" );
  453. if( use_ret == -(POLARSSL_ERR_NET_WANT_WRITE) )
  454. snprintf( buf, buflen, "NET - Connection requires a write call" );
  455. #endif /* POLARSSL_NET_C */
  456. #if defined(POLARSSL_PADLOCK_C)
  457. if( use_ret == -(POLARSSL_ERR_PADLOCK_DATA_MISALIGNED) )
  458. snprintf( buf, buflen, "PADLOCK - Input data should be aligned" );
  459. #endif /* POLARSSL_PADLOCK_C */
  460. #if defined(POLARSSL_PBKDF2_C)
  461. if( use_ret == -(POLARSSL_ERR_PBKDF2_BAD_INPUT_DATA) )
  462. snprintf( buf, buflen, "PBKDF2 - Bad input parameters to function" );
  463. #endif /* POLARSSL_PBKDF2_C */
  464. #if defined(POLARSSL_SHA1_C)
  465. if( use_ret == -(POLARSSL_ERR_SHA1_FILE_IO_ERROR) )
  466. snprintf( buf, buflen, "SHA1 - Read/write error in file" );
  467. #endif /* POLARSSL_SHA1_C */
  468. #if defined(POLARSSL_SHA2_C)
  469. if( use_ret == -(POLARSSL_ERR_SHA2_FILE_IO_ERROR) )
  470. snprintf( buf, buflen, "SHA2 - Read/write error in file" );
  471. #endif /* POLARSSL_SHA2_C */
  472. #if defined(POLARSSL_SHA4_C)
  473. if( use_ret == -(POLARSSL_ERR_SHA4_FILE_IO_ERROR) )
  474. snprintf( buf, buflen, "SHA4 - Read/write error in file" );
  475. #endif /* POLARSSL_SHA4_C */
  476. #if defined(POLARSSL_XTEA_C)
  477. if( use_ret == -(POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH) )
  478. snprintf( buf, buflen, "XTEA - The data input has an invalid length" );
  479. #endif /* POLARSSL_XTEA_C */
  480. if( strlen( buf ) != 0 )
  481. return;
  482. snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
  483. }
  484. #else /* POLARSSL_ERROR_C */
  485. #if defined(POLARSSL_ERROR_STRERROR_DUMMY)
  486. #include <string.h>
  487. /*
  488. * Provide an non-function in case POLARSSL_ERROR_C is not defined
  489. */
  490. void error_strerror( int ret, char *buf, size_t buflen )
  491. {
  492. ((void) ret);
  493. if( buflen > 0 )
  494. buf[0] = '\0';
  495. }
  496. #endif /* POLARSSL_ERROR_STRERROR_DUMMY */
  497. #endif /* POLARSSL_ERROR_C */