PageRenderTime 63ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/source/polarssl/library/error.c

https://bitbucket.org/nqminh/filagree
C | 706 lines | 580 code | 92 blank | 34 comment | 372 complexity | a1a204f7cbb81d84e791ec211471ccaf 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. #include "polarssl/error.h"
  28. #if defined(POLARSSL_AES_C)
  29. #include "polarssl/aes.h"
  30. #endif
  31. #if defined(POLARSSL_BASE64_C)
  32. #include "polarssl/base64.h"
  33. #endif
  34. #if defined(POLARSSL_BIGNUM_C)
  35. #include "polarssl/bignum.h"
  36. #endif
  37. #if defined(POLARSSL_BLOWFISH_C)
  38. #include "polarssl/blowfish.h"
  39. #endif
  40. #if defined(POLARSSL_CAMELLIA_C)
  41. #include "polarssl/camellia.h"
  42. #endif
  43. #if defined(POLARSSL_CIPHER_C)
  44. #include "polarssl/cipher.h"
  45. #endif
  46. #if defined(POLARSSL_CTR_DRBG_C)
  47. #include "polarssl/ctr_drbg.h"
  48. #endif
  49. #if defined(POLARSSL_DES_C)
  50. #include "polarssl/des.h"
  51. #endif
  52. #if defined(POLARSSL_DHM_C)
  53. #include "polarssl/dhm.h"
  54. #endif
  55. #if defined(POLARSSL_ECP_C)
  56. #include "polarssl/ecp.h"
  57. #endif
  58. #if defined(POLARSSL_ENTROPY_C)
  59. #include "polarssl/entropy.h"
  60. #endif
  61. #if defined(POLARSSL_GCM_C)
  62. #include "polarssl/gcm.h"
  63. #endif
  64. #if defined(POLARSSL_MD_C)
  65. #include "polarssl/md.h"
  66. #endif
  67. #if defined(POLARSSL_MD2_C)
  68. #include "polarssl/md2.h"
  69. #endif
  70. #if defined(POLARSSL_MD4_C)
  71. #include "polarssl/md4.h"
  72. #endif
  73. #if defined(POLARSSL_MD5_C)
  74. #include "polarssl/md5.h"
  75. #endif
  76. #if defined(POLARSSL_NET_C)
  77. #include "polarssl/net.h"
  78. #endif
  79. #if defined(POLARSSL_OID_C)
  80. #include "polarssl/oid.h"
  81. #endif
  82. #if defined(POLARSSL_PADLOCK_C)
  83. #include "polarssl/padlock.h"
  84. #endif
  85. #if defined(POLARSSL_PBKDF2_C)
  86. #include "polarssl/pbkdf2.h"
  87. #endif
  88. #if defined(POLARSSL_PEM_PARSE_C) || defined(POLARSSL_PEM_WRITE_C)
  89. #include "polarssl/pem.h"
  90. #endif
  91. #if defined(POLARSSL_PK_C)
  92. #include "polarssl/pk.h"
  93. #endif
  94. #if defined(POLARSSL_PKCS12_C)
  95. #include "polarssl/pkcs12.h"
  96. #endif
  97. #if defined(POLARSSL_PKCS5_C)
  98. #include "polarssl/pkcs5.h"
  99. #endif
  100. #if defined(POLARSSL_RSA_C)
  101. #include "polarssl/rsa.h"
  102. #endif
  103. #if defined(POLARSSL_SHA1_C)
  104. #include "polarssl/sha1.h"
  105. #endif
  106. #if defined(POLARSSL_SHA256_C)
  107. #include "polarssl/sha256.h"
  108. #endif
  109. #if defined(POLARSSL_SHA512_C)
  110. #include "polarssl/sha512.h"
  111. #endif
  112. #if defined(POLARSSL_SSL_TLS_C)
  113. #include "polarssl/ssl.h"
  114. #endif
  115. #if defined(POLARSSL_THREADING_C)
  116. #include "polarssl/threading.h"
  117. #endif
  118. #if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
  119. #include "polarssl/x509.h"
  120. #endif
  121. #if defined(POLARSSL_XTEA_C)
  122. #include "polarssl/xtea.h"
  123. #endif
  124. #include <string.h>
  125. #if defined _MSC_VER && !defined snprintf
  126. #define snprintf _snprintf
  127. #endif
  128. void polarssl_strerror( int ret, char *buf, size_t buflen )
  129. {
  130. size_t len;
  131. int use_ret;
  132. memset( buf, 0x00, buflen );
  133. if( ret < 0 )
  134. ret = -ret;
  135. if( ret & 0xFF80 )
  136. {
  137. use_ret = ret & 0xFF80;
  138. // High level error codes
  139. //
  140. #if defined(POLARSSL_CIPHER_C)
  141. if( use_ret == -(POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE) )
  142. snprintf( buf, buflen, "CIPHER - The selected feature is not available" );
  143. if( use_ret == -(POLARSSL_ERR_CIPHER_BAD_INPUT_DATA) )
  144. snprintf( buf, buflen, "CIPHER - Bad input parameters to function" );
  145. if( use_ret == -(POLARSSL_ERR_CIPHER_ALLOC_FAILED) )
  146. snprintf( buf, buflen, "CIPHER - Failed to allocate memory" );
  147. if( use_ret == -(POLARSSL_ERR_CIPHER_INVALID_PADDING) )
  148. snprintf( buf, buflen, "CIPHER - Input data contains invalid padding and is rejected" );
  149. if( use_ret == -(POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED) )
  150. snprintf( buf, buflen, "CIPHER - Decryption of block requires a full block" );
  151. if( use_ret == -(POLARSSL_ERR_CIPHER_AUTH_FAILED) )
  152. snprintf( buf, buflen, "CIPHER - Authentication failed (for AEAD modes)" );
  153. #endif /* POLARSSL_CIPHER_C */
  154. #if defined(POLARSSL_DHM_C)
  155. if( use_ret == -(POLARSSL_ERR_DHM_BAD_INPUT_DATA) )
  156. snprintf( buf, buflen, "DHM - Bad input parameters to function" );
  157. if( use_ret == -(POLARSSL_ERR_DHM_READ_PARAMS_FAILED) )
  158. snprintf( buf, buflen, "DHM - Reading of the DHM parameters failed" );
  159. if( use_ret == -(POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED) )
  160. snprintf( buf, buflen, "DHM - Making of the DHM parameters failed" );
  161. if( use_ret == -(POLARSSL_ERR_DHM_READ_PUBLIC_FAILED) )
  162. snprintf( buf, buflen, "DHM - Reading of the public values failed" );
  163. if( use_ret == -(POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED) )
  164. snprintf( buf, buflen, "DHM - Making of the public value failed" );
  165. if( use_ret == -(POLARSSL_ERR_DHM_CALC_SECRET_FAILED) )
  166. snprintf( buf, buflen, "DHM - Calculation of the DHM secret failed" );
  167. if( use_ret == -(POLARSSL_ERR_DHM_INVALID_FORMAT) )
  168. snprintf( buf, buflen, "DHM - The ASN.1 data is not formatted correctly" );
  169. if( use_ret == -(POLARSSL_ERR_DHM_MALLOC_FAILED) )
  170. snprintf( buf, buflen, "DHM - Allocation of memory failed" );
  171. if( use_ret == -(POLARSSL_ERR_DHM_FILE_IO_ERROR) )
  172. snprintf( buf, buflen, "DHM - Read/write of file failed" );
  173. #endif /* POLARSSL_DHM_C */
  174. #if defined(POLARSSL_ECP_C)
  175. if( use_ret == -(POLARSSL_ERR_ECP_BAD_INPUT_DATA) )
  176. snprintf( buf, buflen, "ECP - Bad input parameters to function" );
  177. if( use_ret == -(POLARSSL_ERR_ECP_BUFFER_TOO_SMALL) )
  178. snprintf( buf, buflen, "ECP - The buffer is too small to write to" );
  179. if( use_ret == -(POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE) )
  180. snprintf( buf, buflen, "ECP - Requested curve not available" );
  181. if( use_ret == -(POLARSSL_ERR_ECP_VERIFY_FAILED) )
  182. snprintf( buf, buflen, "ECP - The signature is not valid" );
  183. if( use_ret == -(POLARSSL_ERR_ECP_MALLOC_FAILED) )
  184. snprintf( buf, buflen, "ECP - Memory allocation failed" );
  185. if( use_ret == -(POLARSSL_ERR_ECP_RANDOM_FAILED) )
  186. snprintf( buf, buflen, "ECP - Generation of random value, such as (ephemeral) key, failed" );
  187. if( use_ret == -(POLARSSL_ERR_ECP_INVALID_KEY) )
  188. snprintf( buf, buflen, "ECP - Invalid private or public key" );
  189. #endif /* POLARSSL_ECP_C */
  190. #if defined(POLARSSL_MD_C)
  191. if( use_ret == -(POLARSSL_ERR_MD_FEATURE_UNAVAILABLE) )
  192. snprintf( buf, buflen, "MD - The selected feature is not available" );
  193. if( use_ret == -(POLARSSL_ERR_MD_BAD_INPUT_DATA) )
  194. snprintf( buf, buflen, "MD - Bad input parameters to function" );
  195. if( use_ret == -(POLARSSL_ERR_MD_ALLOC_FAILED) )
  196. snprintf( buf, buflen, "MD - Failed to allocate memory" );
  197. if( use_ret == -(POLARSSL_ERR_MD_FILE_IO_ERROR) )
  198. snprintf( buf, buflen, "MD - Opening or reading of file failed" );
  199. #endif /* POLARSSL_MD_C */
  200. #if defined(POLARSSL_PEM_PARSE_C) || defined(POLARSSL_PEM_WRITE_C)
  201. if( use_ret == -(POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT) )
  202. snprintf( buf, buflen, "PEM - No PEM header or footer found" );
  203. if( use_ret == -(POLARSSL_ERR_PEM_INVALID_DATA) )
  204. snprintf( buf, buflen, "PEM - PEM string is not as expected" );
  205. if( use_ret == -(POLARSSL_ERR_PEM_MALLOC_FAILED) )
  206. snprintf( buf, buflen, "PEM - Failed to allocate memory" );
  207. if( use_ret == -(POLARSSL_ERR_PEM_INVALID_ENC_IV) )
  208. snprintf( buf, buflen, "PEM - RSA IV is not in hex-format" );
  209. if( use_ret == -(POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG) )
  210. snprintf( buf, buflen, "PEM - Unsupported key encryption algorithm" );
  211. if( use_ret == -(POLARSSL_ERR_PEM_PASSWORD_REQUIRED) )
  212. snprintf( buf, buflen, "PEM - Private key password can't be empty" );
  213. if( use_ret == -(POLARSSL_ERR_PEM_PASSWORD_MISMATCH) )
  214. snprintf( buf, buflen, "PEM - Given private key password does not allow for correct decryption" );
  215. if( use_ret == -(POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE) )
  216. snprintf( buf, buflen, "PEM - Unavailable feature, e.g. hashing/encryption combination" );
  217. if( use_ret == -(POLARSSL_ERR_PEM_BAD_INPUT_DATA) )
  218. snprintf( buf, buflen, "PEM - Bad input parameters to function" );
  219. #endif /* POLARSSL_PEM_PARSE_C || POLARSSL_PEM_WRITE_C */
  220. #if defined(POLARSSL_PK_C)
  221. if( use_ret == -(POLARSSL_ERR_PK_MALLOC_FAILED) )
  222. snprintf( buf, buflen, "PK - Memory alloation failed" );
  223. if( use_ret == -(POLARSSL_ERR_PK_TYPE_MISMATCH) )
  224. snprintf( buf, buflen, "PK - Type mismatch, eg attempt to encrypt with an ECDSA key" );
  225. if( use_ret == -(POLARSSL_ERR_PK_BAD_INPUT_DATA) )
  226. snprintf( buf, buflen, "PK - Bad input parameters to function" );
  227. if( use_ret == -(POLARSSL_ERR_PK_FILE_IO_ERROR) )
  228. snprintf( buf, buflen, "PK - Read/write of file failed" );
  229. if( use_ret == -(POLARSSL_ERR_PK_KEY_INVALID_VERSION) )
  230. snprintf( buf, buflen, "PK - Unsupported key version" );
  231. if( use_ret == -(POLARSSL_ERR_PK_KEY_INVALID_FORMAT) )
  232. snprintf( buf, buflen, "PK - Invalid key tag or value" );
  233. if( use_ret == -(POLARSSL_ERR_PK_UNKNOWN_PK_ALG) )
  234. snprintf( buf, buflen, "PK - Key algorithm is unsupported (only RSA and EC are supported)" );
  235. if( use_ret == -(POLARSSL_ERR_PK_PASSWORD_REQUIRED) )
  236. snprintf( buf, buflen, "PK - Private key password can't be empty" );
  237. if( use_ret == -(POLARSSL_ERR_PK_PASSWORD_MISMATCH) )
  238. snprintf( buf, buflen, "PK - Given private key password does not allow for correct decryption" );
  239. if( use_ret == -(POLARSSL_ERR_PK_INVALID_PUBKEY) )
  240. snprintf( buf, buflen, "PK - The pubkey tag or value is invalid (only RSA and EC are supported)" );
  241. if( use_ret == -(POLARSSL_ERR_PK_INVALID_ALG) )
  242. snprintf( buf, buflen, "PK - The algorithm tag or value is invalid" );
  243. if( use_ret == -(POLARSSL_ERR_PK_UNKNOWN_NAMED_CURVE) )
  244. snprintf( buf, buflen, "PK - Elliptic curve is unsupported (only NIST curves are supported)" );
  245. if( use_ret == -(POLARSSL_ERR_PK_FEATURE_UNAVAILABLE) )
  246. snprintf( buf, buflen, "PK - Unavailable feature, e.g. RSA disabled for RSA key" );
  247. #endif /* POLARSSL_PK_C */
  248. #if defined(POLARSSL_PKCS12_C)
  249. if( use_ret == -(POLARSSL_ERR_PKCS12_BAD_INPUT_DATA) )
  250. snprintf( buf, buflen, "PKCS12 - Bad input parameters to function" );
  251. if( use_ret == -(POLARSSL_ERR_PKCS12_FEATURE_UNAVAILABLE) )
  252. snprintf( buf, buflen, "PKCS12 - Feature not available, e.g. unsupported encryption scheme" );
  253. if( use_ret == -(POLARSSL_ERR_PKCS12_PBE_INVALID_FORMAT) )
  254. snprintf( buf, buflen, "PKCS12 - PBE ASN.1 data not as expected" );
  255. if( use_ret == -(POLARSSL_ERR_PKCS12_PASSWORD_MISMATCH) )
  256. snprintf( buf, buflen, "PKCS12 - Given private key password does not allow for correct decryption" );
  257. #endif /* POLARSSL_PKCS12_C */
  258. #if defined(POLARSSL_PKCS5_C)
  259. if( use_ret == -(POLARSSL_ERR_PKCS5_BAD_INPUT_DATA) )
  260. snprintf( buf, buflen, "PKCS5 - Bad input parameters to function" );
  261. if( use_ret == -(POLARSSL_ERR_PKCS5_INVALID_FORMAT) )
  262. snprintf( buf, buflen, "PKCS5 - Unexpected ASN.1 data" );
  263. if( use_ret == -(POLARSSL_ERR_PKCS5_FEATURE_UNAVAILABLE) )
  264. snprintf( buf, buflen, "PKCS5 - Requested encryption or digest alg not available" );
  265. if( use_ret == -(POLARSSL_ERR_PKCS5_PASSWORD_MISMATCH) )
  266. snprintf( buf, buflen, "PKCS5 - Given private key password does not allow for correct decryption" );
  267. #endif /* POLARSSL_PKCS5_C */
  268. #if defined(POLARSSL_RSA_C)
  269. if( use_ret == -(POLARSSL_ERR_RSA_BAD_INPUT_DATA) )
  270. snprintf( buf, buflen, "RSA - Bad input parameters to function" );
  271. if( use_ret == -(POLARSSL_ERR_RSA_INVALID_PADDING) )
  272. snprintf( buf, buflen, "RSA - Input data contains invalid padding and is rejected" );
  273. if( use_ret == -(POLARSSL_ERR_RSA_KEY_GEN_FAILED) )
  274. snprintf( buf, buflen, "RSA - Something failed during generation of a key" );
  275. if( use_ret == -(POLARSSL_ERR_RSA_KEY_CHECK_FAILED) )
  276. snprintf( buf, buflen, "RSA - Key failed to pass the libraries validity check" );
  277. if( use_ret == -(POLARSSL_ERR_RSA_PUBLIC_FAILED) )
  278. snprintf( buf, buflen, "RSA - The public key operation failed" );
  279. if( use_ret == -(POLARSSL_ERR_RSA_PRIVATE_FAILED) )
  280. snprintf( buf, buflen, "RSA - The private key operation failed" );
  281. if( use_ret == -(POLARSSL_ERR_RSA_VERIFY_FAILED) )
  282. snprintf( buf, buflen, "RSA - The PKCS#1 verification failed" );
  283. if( use_ret == -(POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE) )
  284. snprintf( buf, buflen, "RSA - The output buffer for decryption is not large enough" );
  285. if( use_ret == -(POLARSSL_ERR_RSA_RNG_FAILED) )
  286. snprintf( buf, buflen, "RSA - The random generator failed to generate non-zeros" );
  287. #endif /* POLARSSL_RSA_C */
  288. #if defined(POLARSSL_SSL_TLS_C)
  289. if( use_ret == -(POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE) )
  290. snprintf( buf, buflen, "SSL - The requested feature is not available" );
  291. if( use_ret == -(POLARSSL_ERR_SSL_BAD_INPUT_DATA) )
  292. snprintf( buf, buflen, "SSL - Bad input parameters to function" );
  293. if( use_ret == -(POLARSSL_ERR_SSL_INVALID_MAC) )
  294. snprintf( buf, buflen, "SSL - Verification of the message MAC failed" );
  295. if( use_ret == -(POLARSSL_ERR_SSL_INVALID_RECORD) )
  296. snprintf( buf, buflen, "SSL - An invalid SSL record was received" );
  297. if( use_ret == -(POLARSSL_ERR_SSL_CONN_EOF) )
  298. snprintf( buf, buflen, "SSL - The connection indicated an EOF" );
  299. if( use_ret == -(POLARSSL_ERR_SSL_UNKNOWN_CIPHER) )
  300. snprintf( buf, buflen, "SSL - An unknown cipher was received" );
  301. if( use_ret == -(POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN) )
  302. snprintf( buf, buflen, "SSL - The server has no ciphersuites in common with the client" );
  303. if( use_ret == -(POLARSSL_ERR_SSL_NO_SESSION_FOUND) )
  304. snprintf( buf, buflen, "SSL - No session to recover was found" );
  305. if( use_ret == -(POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE) )
  306. snprintf( buf, buflen, "SSL - No client certification received from the client, but required by the authentication mode" );
  307. if( use_ret == -(POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE) )
  308. snprintf( buf, buflen, "SSL - DESCRIPTION MISSING" );
  309. if( use_ret == -(POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED) )
  310. snprintf( buf, buflen, "SSL - The own certificate is not set, but needed by the server" );
  311. if( use_ret == -(POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED) )
  312. snprintf( buf, buflen, "SSL - The own private key or pre-shared key is not set, but needed" );
  313. if( use_ret == -(POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED) )
  314. snprintf( buf, buflen, "SSL - No CA Chain is set, but required to operate" );
  315. if( use_ret == -(POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE) )
  316. snprintf( buf, buflen, "SSL - An unexpected message was received from our peer" );
  317. if( use_ret == -(POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE) )
  318. {
  319. snprintf( buf, buflen, "SSL - A fatal alert message was received from our peer" );
  320. return;
  321. }
  322. if( use_ret == -(POLARSSL_ERR_SSL_PEER_VERIFY_FAILED) )
  323. snprintf( buf, buflen, "SSL - Verification of our peer failed" );
  324. if( use_ret == -(POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY) )
  325. snprintf( buf, buflen, "SSL - The peer notified us that the connection is going to be closed" );
  326. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO) )
  327. snprintf( buf, buflen, "SSL - Processing of the ClientHello handshake message failed" );
  328. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO) )
  329. snprintf( buf, buflen, "SSL - Processing of the ServerHello handshake message failed" );
  330. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE) )
  331. snprintf( buf, buflen, "SSL - Processing of the Certificate handshake message failed" );
  332. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST) )
  333. snprintf( buf, buflen, "SSL - Processing of the CertificateRequest handshake message failed" );
  334. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE) )
  335. snprintf( buf, buflen, "SSL - Processing of the ServerKeyExchange handshake message failed" );
  336. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE) )
  337. snprintf( buf, buflen, "SSL - Processing of the ServerHelloDone handshake message failed" );
  338. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE) )
  339. snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed" );
  340. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP) )
  341. snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Read Public" );
  342. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS) )
  343. snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Calculate Secret" );
  344. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY) )
  345. snprintf( buf, buflen, "SSL - Processing of the CertificateVerify handshake message failed" );
  346. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC) )
  347. snprintf( buf, buflen, "SSL - Processing of the ChangeCipherSpec handshake message failed" );
  348. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_FINISHED) )
  349. snprintf( buf, buflen, "SSL - Processing of the Finished handshake message failed" );
  350. if( use_ret == -(POLARSSL_ERR_SSL_MALLOC_FAILED) )
  351. snprintf( buf, buflen, "SSL - Memory allocation failed" );
  352. if( use_ret == -(POLARSSL_ERR_SSL_HW_ACCEL_FAILED) )
  353. snprintf( buf, buflen, "SSL - Hardware acceleration function returned with error" );
  354. if( use_ret == -(POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH) )
  355. snprintf( buf, buflen, "SSL - Hardware acceleration function skipped / left alone data" );
  356. if( use_ret == -(POLARSSL_ERR_SSL_COMPRESSION_FAILED) )
  357. snprintf( buf, buflen, "SSL - Processing of the compression / decompression failed" );
  358. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION) )
  359. snprintf( buf, buflen, "SSL - Handshake protocol not within min/max boundaries" );
  360. if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_NEW_SESSION_TICKET) )
  361. snprintf( buf, buflen, "SSL - Processing of the NewSessionTicket handshake message failed" );
  362. if( use_ret == -(POLARSSL_ERR_SSL_SESSION_TICKET_EXPIRED) )
  363. snprintf( buf, buflen, "SSL - Session ticket has expired" );
  364. if( use_ret == -(POLARSSL_ERR_SSL_PK_TYPE_MISMATCH) )
  365. snprintf( buf, buflen, "SSL - Public key type mismatch (eg, asked for RSA key exchange and presented EC key)" );
  366. if( use_ret == -(POLARSSL_ERR_SSL_UNKNOWN_IDENTITY) )
  367. snprintf( buf, buflen, "SSL - Unkown identity received (eg, PSK identity)" );
  368. #endif /* POLARSSL_SSL_TLS_C */
  369. #if defined(POLARSSL_X509_USE_C) || defined(POLARSSL_X509_CREATE_C)
  370. if( use_ret == -(POLARSSL_ERR_X509_FEATURE_UNAVAILABLE) )
  371. snprintf( buf, buflen, "X509 - Unavailable feature, e.g. RSA hashing/encryption combination" );
  372. if( use_ret == -(POLARSSL_ERR_X509_UNKNOWN_OID) )
  373. snprintf( buf, buflen, "X509 - Requested OID is unknown" );
  374. if( use_ret == -(POLARSSL_ERR_X509_INVALID_FORMAT) )
  375. snprintf( buf, buflen, "X509 - The CRT/CRL/CSR format is invalid, e.g. different type expected" );
  376. if( use_ret == -(POLARSSL_ERR_X509_INVALID_VERSION) )
  377. snprintf( buf, buflen, "X509 - The CRT/CRL/CSR version element is invalid" );
  378. if( use_ret == -(POLARSSL_ERR_X509_INVALID_SERIAL) )
  379. snprintf( buf, buflen, "X509 - The serial tag or value is invalid" );
  380. if( use_ret == -(POLARSSL_ERR_X509_INVALID_ALG) )
  381. snprintf( buf, buflen, "X509 - The algorithm tag or value is invalid" );
  382. if( use_ret == -(POLARSSL_ERR_X509_INVALID_NAME) )
  383. snprintf( buf, buflen, "X509 - The name tag or value is invalid" );
  384. if( use_ret == -(POLARSSL_ERR_X509_INVALID_DATE) )
  385. snprintf( buf, buflen, "X509 - The date tag or value is invalid" );
  386. if( use_ret == -(POLARSSL_ERR_X509_INVALID_SIGNATURE) )
  387. snprintf( buf, buflen, "X509 - The signature tag or value invalid" );
  388. if( use_ret == -(POLARSSL_ERR_X509_INVALID_EXTENSIONS) )
  389. snprintf( buf, buflen, "X509 - The extension tag or value is invalid" );
  390. if( use_ret == -(POLARSSL_ERR_X509_UNKNOWN_VERSION) )
  391. snprintf( buf, buflen, "X509 - CRT/CRL/CSR has an unsupported version number" );
  392. if( use_ret == -(POLARSSL_ERR_X509_UNKNOWN_SIG_ALG) )
  393. snprintf( buf, buflen, "X509 - Signature algorithm (oid) is unsupported" );
  394. if( use_ret == -(POLARSSL_ERR_X509_SIG_MISMATCH) )
  395. snprintf( buf, buflen, "X509 - Signature algorithms do not match. (see \\c ::x509_crt sig_oid)" );
  396. if( use_ret == -(POLARSSL_ERR_X509_CERT_VERIFY_FAILED) )
  397. snprintf( buf, buflen, "X509 - Certificate verification failed, e.g. CRL, CA or signature check failed" );
  398. if( use_ret == -(POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT) )
  399. snprintf( buf, buflen, "X509 - Format not recognized as DER or PEM" );
  400. if( use_ret == -(POLARSSL_ERR_X509_BAD_INPUT_DATA) )
  401. snprintf( buf, buflen, "X509 - Input invalid" );
  402. if( use_ret == -(POLARSSL_ERR_X509_MALLOC_FAILED) )
  403. snprintf( buf, buflen, "X509 - Allocation of memory failed" );
  404. if( use_ret == -(POLARSSL_ERR_X509_FILE_IO_ERROR) )
  405. snprintf( buf, buflen, "X509 - Read/write of file failed" );
  406. #endif /* POLARSSL_X509_USE,X509_CREATE_C */
  407. if( strlen( buf ) == 0 )
  408. snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
  409. }
  410. use_ret = ret & ~0xFF80;
  411. if( use_ret == 0 )
  412. return;
  413. // If high level code is present, make a concatenation between both
  414. // error strings.
  415. //
  416. len = strlen( buf );
  417. if( len > 0 )
  418. {
  419. if( buflen - len < 5 )
  420. return;
  421. snprintf( buf + len, buflen - len, " : " );
  422. buf += len + 3;
  423. buflen -= len + 3;
  424. }
  425. // Low level error codes
  426. //
  427. #if defined(POLARSSL_AES_C)
  428. if( use_ret == -(POLARSSL_ERR_AES_INVALID_KEY_LENGTH) )
  429. snprintf( buf, buflen, "AES - Invalid key length" );
  430. if( use_ret == -(POLARSSL_ERR_AES_INVALID_INPUT_LENGTH) )
  431. snprintf( buf, buflen, "AES - Invalid data input length" );
  432. #endif /* POLARSSL_AES_C */
  433. #if defined(POLARSSL_ASN1_PARSE_C)
  434. if( use_ret == -(POLARSSL_ERR_ASN1_OUT_OF_DATA) )
  435. snprintf( buf, buflen, "ASN1 - Out of data when parsing an ASN1 data structure" );
  436. if( use_ret == -(POLARSSL_ERR_ASN1_UNEXPECTED_TAG) )
  437. snprintf( buf, buflen, "ASN1 - ASN1 tag was of an unexpected value" );
  438. if( use_ret == -(POLARSSL_ERR_ASN1_INVALID_LENGTH) )
  439. snprintf( buf, buflen, "ASN1 - Error when trying to determine the length or invalid length" );
  440. if( use_ret == -(POLARSSL_ERR_ASN1_LENGTH_MISMATCH) )
  441. snprintf( buf, buflen, "ASN1 - Actual length differs from expected length" );
  442. if( use_ret == -(POLARSSL_ERR_ASN1_INVALID_DATA) )
  443. snprintf( buf, buflen, "ASN1 - Data is invalid. (not used)" );
  444. if( use_ret == -(POLARSSL_ERR_ASN1_MALLOC_FAILED) )
  445. snprintf( buf, buflen, "ASN1 - Memory allocation failed" );
  446. if( use_ret == -(POLARSSL_ERR_ASN1_BUF_TOO_SMALL) )
  447. snprintf( buf, buflen, "ASN1 - Buffer too small when writing ASN.1 data structure" );
  448. #endif /* POLARSSL_ASN1_PARSE_C */
  449. #if defined(POLARSSL_BASE64_C)
  450. if( use_ret == -(POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL) )
  451. snprintf( buf, buflen, "BASE64 - Output buffer too small" );
  452. if( use_ret == -(POLARSSL_ERR_BASE64_INVALID_CHARACTER) )
  453. snprintf( buf, buflen, "BASE64 - Invalid character in input" );
  454. #endif /* POLARSSL_BASE64_C */
  455. #if defined(POLARSSL_BIGNUM_C)
  456. if( use_ret == -(POLARSSL_ERR_MPI_FILE_IO_ERROR) )
  457. snprintf( buf, buflen, "BIGNUM - An error occurred while reading from or writing to a file" );
  458. if( use_ret == -(POLARSSL_ERR_MPI_BAD_INPUT_DATA) )
  459. snprintf( buf, buflen, "BIGNUM - Bad input parameters to function" );
  460. if( use_ret == -(POLARSSL_ERR_MPI_INVALID_CHARACTER) )
  461. snprintf( buf, buflen, "BIGNUM - There is an invalid character in the digit string" );
  462. if( use_ret == -(POLARSSL_ERR_MPI_BUFFER_TOO_SMALL) )
  463. snprintf( buf, buflen, "BIGNUM - The buffer is too small to write to" );
  464. if( use_ret == -(POLARSSL_ERR_MPI_NEGATIVE_VALUE) )
  465. snprintf( buf, buflen, "BIGNUM - The input arguments are negative or result in illegal output" );
  466. if( use_ret == -(POLARSSL_ERR_MPI_DIVISION_BY_ZERO) )
  467. snprintf( buf, buflen, "BIGNUM - The input argument for division is zero, which is not allowed" );
  468. if( use_ret == -(POLARSSL_ERR_MPI_NOT_ACCEPTABLE) )
  469. snprintf( buf, buflen, "BIGNUM - The input arguments are not acceptable" );
  470. if( use_ret == -(POLARSSL_ERR_MPI_MALLOC_FAILED) )
  471. snprintf( buf, buflen, "BIGNUM - Memory allocation failed" );
  472. #endif /* POLARSSL_BIGNUM_C */
  473. #if defined(POLARSSL_BLOWFISH_C)
  474. if( use_ret == -(POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH) )
  475. snprintf( buf, buflen, "BLOWFISH - Invalid key length" );
  476. if( use_ret == -(POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH) )
  477. snprintf( buf, buflen, "BLOWFISH - Invalid data input length" );
  478. #endif /* POLARSSL_BLOWFISH_C */
  479. #if defined(POLARSSL_CAMELLIA_C)
  480. if( use_ret == -(POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH) )
  481. snprintf( buf, buflen, "CAMELLIA - Invalid key length" );
  482. if( use_ret == -(POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH) )
  483. snprintf( buf, buflen, "CAMELLIA - Invalid data input length" );
  484. #endif /* POLARSSL_CAMELLIA_C */
  485. #if defined(POLARSSL_CTR_DRBG_C)
  486. if( use_ret == -(POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED) )
  487. snprintf( buf, buflen, "CTR_DRBG - The entropy source failed" );
  488. if( use_ret == -(POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG) )
  489. snprintf( buf, buflen, "CTR_DRBG - Too many random requested in single call" );
  490. if( use_ret == -(POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG) )
  491. snprintf( buf, buflen, "CTR_DRBG - Input too large (Entropy + additional)" );
  492. if( use_ret == -(POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR) )
  493. snprintf( buf, buflen, "CTR_DRBG - Read/write error in file" );
  494. #endif /* POLARSSL_CTR_DRBG_C */
  495. #if defined(POLARSSL_DES_C)
  496. if( use_ret == -(POLARSSL_ERR_DES_INVALID_INPUT_LENGTH) )
  497. snprintf( buf, buflen, "DES - The data input has an invalid length" );
  498. #endif /* POLARSSL_DES_C */
  499. #if defined(POLARSSL_ENTROPY_C)
  500. if( use_ret == -(POLARSSL_ERR_ENTROPY_SOURCE_FAILED) )
  501. snprintf( buf, buflen, "ENTROPY - Critical entropy source failure" );
  502. if( use_ret == -(POLARSSL_ERR_ENTROPY_MAX_SOURCES) )
  503. snprintf( buf, buflen, "ENTROPY - No more sources can be added" );
  504. if( use_ret == -(POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED) )
  505. snprintf( buf, buflen, "ENTROPY - No sources have been added to poll" );
  506. #endif /* POLARSSL_ENTROPY_C */
  507. #if defined(POLARSSL_GCM_C)
  508. if( use_ret == -(POLARSSL_ERR_GCM_AUTH_FAILED) )
  509. snprintf( buf, buflen, "GCM - Authenticated decryption failed" );
  510. if( use_ret == -(POLARSSL_ERR_GCM_BAD_INPUT) )
  511. snprintf( buf, buflen, "GCM - Bad input parameters to function" );
  512. #endif /* POLARSSL_GCM_C */
  513. #if defined(POLARSSL_MD2_C)
  514. if( use_ret == -(POLARSSL_ERR_MD2_FILE_IO_ERROR) )
  515. snprintf( buf, buflen, "MD2 - Read/write error in file" );
  516. #endif /* POLARSSL_MD2_C */
  517. #if defined(POLARSSL_MD4_C)
  518. if( use_ret == -(POLARSSL_ERR_MD4_FILE_IO_ERROR) )
  519. snprintf( buf, buflen, "MD4 - Read/write error in file" );
  520. #endif /* POLARSSL_MD4_C */
  521. #if defined(POLARSSL_MD5_C)
  522. if( use_ret == -(POLARSSL_ERR_MD5_FILE_IO_ERROR) )
  523. snprintf( buf, buflen, "MD5 - Read/write error in file" );
  524. #endif /* POLARSSL_MD5_C */
  525. #if defined(POLARSSL_NET_C)
  526. if( use_ret == -(POLARSSL_ERR_NET_UNKNOWN_HOST) )
  527. snprintf( buf, buflen, "NET - Failed to get an IP address for the given hostname" );
  528. if( use_ret == -(POLARSSL_ERR_NET_SOCKET_FAILED) )
  529. snprintf( buf, buflen, "NET - Failed to open a socket" );
  530. if( use_ret == -(POLARSSL_ERR_NET_CONNECT_FAILED) )
  531. snprintf( buf, buflen, "NET - The connection to the given server / port failed" );
  532. if( use_ret == -(POLARSSL_ERR_NET_BIND_FAILED) )
  533. snprintf( buf, buflen, "NET - Binding of the socket failed" );
  534. if( use_ret == -(POLARSSL_ERR_NET_LISTEN_FAILED) )
  535. snprintf( buf, buflen, "NET - Could not listen on the socket" );
  536. if( use_ret == -(POLARSSL_ERR_NET_ACCEPT_FAILED) )
  537. snprintf( buf, buflen, "NET - Could not accept the incoming connection" );
  538. if( use_ret == -(POLARSSL_ERR_NET_RECV_FAILED) )
  539. snprintf( buf, buflen, "NET - Reading information from the socket failed" );
  540. if( use_ret == -(POLARSSL_ERR_NET_SEND_FAILED) )
  541. snprintf( buf, buflen, "NET - Sending information through the socket failed" );
  542. if( use_ret == -(POLARSSL_ERR_NET_CONN_RESET) )
  543. snprintf( buf, buflen, "NET - Connection was reset by peer" );
  544. if( use_ret == -(POLARSSL_ERR_NET_WANT_READ) )
  545. snprintf( buf, buflen, "NET - Connection requires a read call" );
  546. if( use_ret == -(POLARSSL_ERR_NET_WANT_WRITE) )
  547. snprintf( buf, buflen, "NET - Connection requires a write call" );
  548. #endif /* POLARSSL_NET_C */
  549. #if defined(POLARSSL_OID_C)
  550. if( use_ret == -(POLARSSL_ERR_OID_NOT_FOUND) )
  551. snprintf( buf, buflen, "OID - OID is not found" );
  552. #endif /* POLARSSL_OID_C */
  553. #if defined(POLARSSL_PADLOCK_C)
  554. if( use_ret == -(POLARSSL_ERR_PADLOCK_DATA_MISALIGNED) )
  555. snprintf( buf, buflen, "PADLOCK - Input data should be aligned" );
  556. #endif /* POLARSSL_PADLOCK_C */
  557. #if defined(POLARSSL_PBKDF2_C)
  558. if( use_ret == -(POLARSSL_ERR_PBKDF2_BAD_INPUT_DATA) )
  559. snprintf( buf, buflen, "PBKDF2 - Bad input parameters to function" );
  560. #endif /* POLARSSL_PBKDF2_C */
  561. #if defined(POLARSSL_SHA1_C)
  562. if( use_ret == -(POLARSSL_ERR_SHA1_FILE_IO_ERROR) )
  563. snprintf( buf, buflen, "SHA1 - Read/write error in file" );
  564. #endif /* POLARSSL_SHA1_C */
  565. #if defined(POLARSSL_SHA256_C)
  566. if( use_ret == -(POLARSSL_ERR_SHA256_FILE_IO_ERROR) )
  567. snprintf( buf, buflen, "SHA256 - Read/write error in file" );
  568. #endif /* POLARSSL_SHA256_C */
  569. #if defined(POLARSSL_SHA512_C)
  570. if( use_ret == -(POLARSSL_ERR_SHA512_FILE_IO_ERROR) )
  571. snprintf( buf, buflen, "SHA512 - Read/write error in file" );
  572. #endif /* POLARSSL_SHA512_C */
  573. #if defined(POLARSSL_THREADING_C)
  574. if( use_ret == -(POLARSSL_ERR_THREADING_FEATURE_UNAVAILABLE) )
  575. snprintf( buf, buflen, "THREADING - The selected feature is not available" );
  576. if( use_ret == -(POLARSSL_ERR_THREADING_BAD_INPUT_DATA) )
  577. snprintf( buf, buflen, "THREADING - Bad input parameters to function" );
  578. if( use_ret == -(POLARSSL_ERR_THREADING_MUTEX_ERROR) )
  579. snprintf( buf, buflen, "THREADING - Locking / unlocking / free failed with error code" );
  580. #endif /* POLARSSL_THREADING_C */
  581. #if defined(POLARSSL_XTEA_C)
  582. if( use_ret == -(POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH) )
  583. snprintf( buf, buflen, "XTEA - The data input has an invalid length" );
  584. #endif /* POLARSSL_XTEA_C */
  585. if( strlen( buf ) != 0 )
  586. return;
  587. snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
  588. }
  589. #if defined(POLARSSL_ERROR_STRERROR_BC)
  590. void error_strerror( int ret, char *buf, size_t buflen )
  591. {
  592. return polarssl_strerror( ret, buf, buflen );
  593. }
  594. #endif /* POLARSSL_ERROR_STRERROR_BC */
  595. #else /* POLARSSL_ERROR_C */
  596. #if defined(POLARSSL_ERROR_STRERROR_DUMMY)
  597. #include <string.h>
  598. /*
  599. * Provide an non-function in case POLARSSL_ERROR_C is not defined
  600. */
  601. void polarssl_strerror( int ret, char *buf, size_t buflen )
  602. {
  603. ((void) ret);
  604. if( buflen > 0 )
  605. buf[0] = '\0';
  606. }
  607. #if defined(POLARSSL_ERROR_STRERROR_BC)
  608. void error_strerror( int ret, char *buf, size_t buflen )
  609. {
  610. return polarssl_strerror( ret, buf, buflen );
  611. }
  612. #endif /* POLARSSL_ERROR_STRERROR_BC */
  613. #endif /* POLARSSL_ERROR_STRERROR_DUMMY */
  614. #endif /* POLARSSL_ERROR_C */