blob: 1a8457bad3fdc9f52884df088aaaa764eccb0066 [file] [log] [blame]
Paul Bakker9d781402011-05-09 16:17:09 +00001/*
2 * Error message information
3 *
Paul Bakker9a736322012-11-14 12:39:52 +00004 * Copyright (C) 2006-2012, Brainspark B.V.
Paul Bakker9d781402011-05-09 16:17:09 +00005 *
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
26#include "polarssl/config.h"
27
28#if defined(POLARSSL_ERROR_C)
29
30#if defined(POLARSSL_AES_C)
31#include "polarssl/aes.h"
32#endif
33
34#if defined(POLARSSL_BASE64_C)
35#include "polarssl/base64.h"
36#endif
37
38#if defined(POLARSSL_BIGNUM_C)
39#include "polarssl/bignum.h"
40#endif
41
Paul Bakker83f00bb2012-07-04 11:08:50 +000042#if defined(POLARSSL_BLOWFISH_C)
43#include "polarssl/blowfish.h"
44#endif
45
Paul Bakker9d781402011-05-09 16:17:09 +000046#if defined(POLARSSL_CAMELLIA_C)
47#include "polarssl/camellia.h"
48#endif
49
Paul Bakkerff61a782011-06-09 15:42:02 +000050#if defined(POLARSSL_CIPHER_C)
51#include "polarssl/cipher.h"
52#endif
53
Paul Bakker880ac7e2011-11-27 14:50:49 +000054#if defined(POLARSSL_CTR_DRBG_C)
55#include "polarssl/ctr_drbg.h"
56#endif
57
Paul Bakker9d781402011-05-09 16:17:09 +000058#if defined(POLARSSL_DES_C)
59#include "polarssl/des.h"
60#endif
61
62#if defined(POLARSSL_DHM_C)
63#include "polarssl/dhm.h"
64#endif
65
Paul Bakkercf4365f2013-01-16 17:00:43 +010066#if defined(POLARSSL_ECP_C)
67#include "polarssl/ecp.h"
68#endif
69
Paul Bakker6083fd22011-12-03 21:45:14 +000070#if defined(POLARSSL_ENTROPY_C)
71#include "polarssl/entropy.h"
72#endif
73
Paul Bakker030277a2012-04-17 12:24:26 +000074#if defined(POLARSSL_GCM_C)
75#include "polarssl/gcm.h"
76#endif
77
Paul Bakker9d781402011-05-09 16:17:09 +000078#if defined(POLARSSL_MD_C)
79#include "polarssl/md.h"
80#endif
81
Paul Bakker69e095c2011-12-10 21:55:01 +000082#if defined(POLARSSL_MD2_C)
83#include "polarssl/md2.h"
84#endif
85
86#if defined(POLARSSL_MD4_C)
87#include "polarssl/md4.h"
88#endif
89
90#if defined(POLARSSL_MD5_C)
91#include "polarssl/md5.h"
92#endif
93
Paul Bakker9d781402011-05-09 16:17:09 +000094#if defined(POLARSSL_NET_C)
95#include "polarssl/net.h"
96#endif
97
98#if defined(POLARSSL_PADLOCK_C)
99#include "polarssl/padlock.h"
100#endif
101
Paul Bakkerd14277d2012-09-26 15:19:05 +0000102#if defined(POLARSSL_PBKDF2_C)
103#include "polarssl/pbkdf2.h"
104#endif
105
Paul Bakker9d781402011-05-09 16:17:09 +0000106#if defined(POLARSSL_PEM_C)
107#include "polarssl/pem.h"
108#endif
109
110#if defined(POLARSSL_RSA_C)
111#include "polarssl/rsa.h"
112#endif
113
Paul Bakker69e095c2011-12-10 21:55:01 +0000114#if defined(POLARSSL_SHA1_C)
115#include "polarssl/sha1.h"
116#endif
117
118#if defined(POLARSSL_SHA2_C)
119#include "polarssl/sha2.h"
120#endif
121
122#if defined(POLARSSL_SHA4_C)
123#include "polarssl/sha4.h"
124#endif
125
Paul Bakker831a7552011-05-18 13:32:51 +0000126#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker9d781402011-05-09 16:17:09 +0000127#include "polarssl/ssl.h"
128#endif
129
130#if defined(POLARSSL_X509_PARSE_C)
131#include "polarssl/x509.h"
132#endif
133
134#if defined(POLARSSL_XTEA_C)
135#include "polarssl/xtea.h"
136#endif
137
138
139#include <string.h>
140
Paul Bakkerdceecd82011-11-15 16:38:34 +0000141#if defined _MSC_VER && !defined snprintf
142#define snprintf _snprintf
143#endif
144
Paul Bakker9d781402011-05-09 16:17:09 +0000145void error_strerror( int ret, char *buf, size_t buflen )
146{
147 size_t len;
148 int use_ret;
149
150 memset( buf, 0x00, buflen );
151
152 if( ret < 0 )
153 ret = -ret;
154
155 if( ret & 0xFF80 )
156 {
157 use_ret = ret & 0xFF80;
158
159 // High level error codes
160 //
Paul Bakkerff61a782011-06-09 15:42:02 +0000161#if defined(POLARSSL_CIPHER_C)
162 if( use_ret == -(POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE) )
163 snprintf( buf, buflen, "CIPHER - The selected feature is not available" );
164 if( use_ret == -(POLARSSL_ERR_CIPHER_BAD_INPUT_DATA) )
165 snprintf( buf, buflen, "CIPHER - Bad input parameters to function" );
166 if( use_ret == -(POLARSSL_ERR_CIPHER_ALLOC_FAILED) )
167 snprintf( buf, buflen, "CIPHER - Failed to allocate memory" );
168 if( use_ret == -(POLARSSL_ERR_CIPHER_INVALID_PADDING) )
169 snprintf( buf, buflen, "CIPHER - Input data contains invalid padding and is rejected" );
170 if( use_ret == -(POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED) )
171 snprintf( buf, buflen, "CIPHER - Decryption of block requires a full block" );
172#endif /* POLARSSL_CIPHER_C */
173
Paul Bakker9d781402011-05-09 16:17:09 +0000174#if defined(POLARSSL_DHM_C)
175 if( use_ret == -(POLARSSL_ERR_DHM_BAD_INPUT_DATA) )
176 snprintf( buf, buflen, "DHM - Bad input parameters to function" );
177 if( use_ret == -(POLARSSL_ERR_DHM_READ_PARAMS_FAILED) )
178 snprintf( buf, buflen, "DHM - Reading of the DHM parameters failed" );
179 if( use_ret == -(POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED) )
180 snprintf( buf, buflen, "DHM - Making of the DHM parameters failed" );
181 if( use_ret == -(POLARSSL_ERR_DHM_READ_PUBLIC_FAILED) )
182 snprintf( buf, buflen, "DHM - Reading of the public values failed" );
183 if( use_ret == -(POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED) )
Paul Bakker05ef8352012-05-08 09:17:57 +0000184 snprintf( buf, buflen, "DHM - Making of the public value failed" );
Paul Bakker9d781402011-05-09 16:17:09 +0000185 if( use_ret == -(POLARSSL_ERR_DHM_CALC_SECRET_FAILED) )
186 snprintf( buf, buflen, "DHM - Calculation of the DHM secret failed" );
187#endif /* POLARSSL_DHM_C */
188
Paul Bakkercf4365f2013-01-16 17:00:43 +0100189#if defined(POLARSSL_ECP_C)
190 if( use_ret == -(POLARSSL_ERR_ECP_BAD_INPUT_DATA) )
191 snprintf( buf, buflen, "ECP - Bad input parameters to function" );
192 if( use_ret == -(POLARSSL_ERR_ECP_GENERIC) )
193 snprintf( buf, buflen, "ECP - Generic ECP error" );
194#endif /* POLARSSL_ECP_C */
195
Paul Bakker9d781402011-05-09 16:17:09 +0000196#if defined(POLARSSL_MD_C)
197 if( use_ret == -(POLARSSL_ERR_MD_FEATURE_UNAVAILABLE) )
198 snprintf( buf, buflen, "MD - The selected feature is not available" );
Paul Bakker9c021ad2011-06-09 15:55:11 +0000199 if( use_ret == -(POLARSSL_ERR_MD_BAD_INPUT_DATA) )
200 snprintf( buf, buflen, "MD - Bad input parameters to function" );
201 if( use_ret == -(POLARSSL_ERR_MD_ALLOC_FAILED) )
202 snprintf( buf, buflen, "MD - Failed to allocate memory" );
Paul Bakker8913f822012-01-14 18:07:41 +0000203 if( use_ret == -(POLARSSL_ERR_MD_FILE_IO_ERROR) )
204 snprintf( buf, buflen, "MD - Opening or reading of file failed" );
Paul Bakker9d781402011-05-09 16:17:09 +0000205#endif /* POLARSSL_MD_C */
206
207#if defined(POLARSSL_PEM_C)
208 if( use_ret == -(POLARSSL_ERR_PEM_NO_HEADER_PRESENT) )
209 snprintf( buf, buflen, "PEM - No PEM header found" );
210 if( use_ret == -(POLARSSL_ERR_PEM_INVALID_DATA) )
211 snprintf( buf, buflen, "PEM - PEM string is not as expected" );
212 if( use_ret == -(POLARSSL_ERR_PEM_MALLOC_FAILED) )
213 snprintf( buf, buflen, "PEM - Failed to allocate memory" );
214 if( use_ret == -(POLARSSL_ERR_PEM_INVALID_ENC_IV) )
215 snprintf( buf, buflen, "PEM - RSA IV is not in hex-format" );
216 if( use_ret == -(POLARSSL_ERR_PEM_UNKNOWN_ENC_ALG) )
217 snprintf( buf, buflen, "PEM - Unsupported key encryption algorithm" );
218 if( use_ret == -(POLARSSL_ERR_PEM_PASSWORD_REQUIRED) )
219 snprintf( buf, buflen, "PEM - Private key password can't be empty" );
220 if( use_ret == -(POLARSSL_ERR_PEM_PASSWORD_MISMATCH) )
221 snprintf( buf, buflen, "PEM - Given private key password does not allow for correct decryption" );
222 if( use_ret == -(POLARSSL_ERR_PEM_FEATURE_UNAVAILABLE) )
223 snprintf( buf, buflen, "PEM - Unavailable feature, e.g. hashing/encryption combination" );
224#endif /* POLARSSL_PEM_C */
225
226#if defined(POLARSSL_RSA_C)
227 if( use_ret == -(POLARSSL_ERR_RSA_BAD_INPUT_DATA) )
228 snprintf( buf, buflen, "RSA - Bad input parameters to function" );
229 if( use_ret == -(POLARSSL_ERR_RSA_INVALID_PADDING) )
230 snprintf( buf, buflen, "RSA - Input data contains invalid padding and is rejected" );
231 if( use_ret == -(POLARSSL_ERR_RSA_KEY_GEN_FAILED) )
232 snprintf( buf, buflen, "RSA - Something failed during generation of a key" );
233 if( use_ret == -(POLARSSL_ERR_RSA_KEY_CHECK_FAILED) )
234 snprintf( buf, buflen, "RSA - Key failed to pass the libraries validity check" );
235 if( use_ret == -(POLARSSL_ERR_RSA_PUBLIC_FAILED) )
236 snprintf( buf, buflen, "RSA - The public key operation failed" );
237 if( use_ret == -(POLARSSL_ERR_RSA_PRIVATE_FAILED) )
238 snprintf( buf, buflen, "RSA - The private key operation failed" );
239 if( use_ret == -(POLARSSL_ERR_RSA_VERIFY_FAILED) )
240 snprintf( buf, buflen, "RSA - The PKCS#1 verification failed" );
241 if( use_ret == -(POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE) )
242 snprintf( buf, buflen, "RSA - The output buffer for decryption is not large enough" );
243 if( use_ret == -(POLARSSL_ERR_RSA_RNG_FAILED) )
244 snprintf( buf, buflen, "RSA - The random generator failed to generate non-zeros" );
245#endif /* POLARSSL_RSA_C */
246
Paul Bakker831a7552011-05-18 13:32:51 +0000247#if defined(POLARSSL_SSL_TLS_C)
Paul Bakker9d781402011-05-09 16:17:09 +0000248 if( use_ret == -(POLARSSL_ERR_SSL_FEATURE_UNAVAILABLE) )
249 snprintf( buf, buflen, "SSL - The requested feature is not available" );
250 if( use_ret == -(POLARSSL_ERR_SSL_BAD_INPUT_DATA) )
251 snprintf( buf, buflen, "SSL - Bad input parameters to function" );
252 if( use_ret == -(POLARSSL_ERR_SSL_INVALID_MAC) )
253 snprintf( buf, buflen, "SSL - Verification of the message MAC failed" );
254 if( use_ret == -(POLARSSL_ERR_SSL_INVALID_RECORD) )
255 snprintf( buf, buflen, "SSL - An invalid SSL record was received" );
Paul Bakker831a7552011-05-18 13:32:51 +0000256 if( use_ret == -(POLARSSL_ERR_SSL_CONN_EOF) )
257 snprintf( buf, buflen, "SSL - The connection indicated an EOF" );
Paul Bakker9d781402011-05-09 16:17:09 +0000258 if( use_ret == -(POLARSSL_ERR_SSL_UNKNOWN_CIPHER) )
259 snprintf( buf, buflen, "SSL - An unknown cipher was received" );
260 if( use_ret == -(POLARSSL_ERR_SSL_NO_CIPHER_CHOSEN) )
261 snprintf( buf, buflen, "SSL - The server has no ciphersuites in common with the client" );
262 if( use_ret == -(POLARSSL_ERR_SSL_NO_SESSION_FOUND) )
263 snprintf( buf, buflen, "SSL - No session to recover was found" );
264 if( use_ret == -(POLARSSL_ERR_SSL_NO_CLIENT_CERTIFICATE) )
265 snprintf( buf, buflen, "SSL - No client certification received from the client, but required by the authentication mode" );
266 if( use_ret == -(POLARSSL_ERR_SSL_CERTIFICATE_TOO_LARGE) )
267 snprintf( buf, buflen, "SSL - DESCRIPTION MISSING" );
268 if( use_ret == -(POLARSSL_ERR_SSL_CERTIFICATE_REQUIRED) )
269 snprintf( buf, buflen, "SSL - The own certificate is not set, but needed by the server" );
270 if( use_ret == -(POLARSSL_ERR_SSL_PRIVATE_KEY_REQUIRED) )
271 snprintf( buf, buflen, "SSL - The own private key is not set, but needed" );
272 if( use_ret == -(POLARSSL_ERR_SSL_CA_CHAIN_REQUIRED) )
273 snprintf( buf, buflen, "SSL - No CA Chain is set, but required to operate" );
274 if( use_ret == -(POLARSSL_ERR_SSL_UNEXPECTED_MESSAGE) )
275 snprintf( buf, buflen, "SSL - An unexpected message was received from our peer" );
276 if( use_ret == -(POLARSSL_ERR_SSL_FATAL_ALERT_MESSAGE) )
Paul Bakker3aac1da2012-05-08 13:12:27 +0000277 {
Paul Bakker9d781402011-05-09 16:17:09 +0000278 snprintf( buf, buflen, "SSL - A fatal alert message was received from our peer" );
Paul Bakker3aac1da2012-05-08 13:12:27 +0000279 return;
280 }
Paul Bakker9d781402011-05-09 16:17:09 +0000281 if( use_ret == -(POLARSSL_ERR_SSL_PEER_VERIFY_FAILED) )
282 snprintf( buf, buflen, "SSL - Verification of our peer failed" );
283 if( use_ret == -(POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY) )
284 snprintf( buf, buflen, "SSL - The peer notified us that the connection is going to be closed" );
285 if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CLIENT_HELLO) )
286 snprintf( buf, buflen, "SSL - Processing of the ClientHello handshake message failed" );
287 if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO) )
288 snprintf( buf, buflen, "SSL - Processing of the ServerHello handshake message failed" );
289 if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE) )
290 snprintf( buf, buflen, "SSL - Processing of the Certificate handshake message failed" );
291 if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST) )
292 snprintf( buf, buflen, "SSL - Processing of the CertificateRequest handshake message failed" );
293 if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE) )
294 snprintf( buf, buflen, "SSL - Processing of the ServerKeyExchange handshake message failed" );
295 if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_SERVER_HELLO_DONE) )
296 snprintf( buf, buflen, "SSL - Processing of the ServerHelloDone handshake message failed" );
297 if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE) )
298 snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed" );
299 if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_DHM_RP) )
300 snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed in DHM Read Public" );
301 if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_DHM_CS) )
302 snprintf( buf, buflen, "SSL - Processing of the ClientKeyExchange handshake message failed in DHM Calculate Secret" );
303 if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY) )
304 snprintf( buf, buflen, "SSL - Processing of the CertificateVerify handshake message failed" );
305 if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC) )
306 snprintf( buf, buflen, "SSL - Processing of the ChangeCipherSpec handshake message failed" );
307 if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_FINISHED) )
308 snprintf( buf, buflen, "SSL - Processing of the Finished handshake message failed" );
Paul Bakker69e095c2011-12-10 21:55:01 +0000309 if( use_ret == -(POLARSSL_ERR_SSL_MALLOC_FAILED) )
310 snprintf( buf, buflen, "SSL - Memory allocation failed" );
Paul Bakker05ef8352012-05-08 09:17:57 +0000311 if( use_ret == -(POLARSSL_ERR_SSL_HW_ACCEL_FAILED) )
312 snprintf( buf, buflen, "SSL - Hardware acceleration function returned with error" );
313 if( use_ret == -(POLARSSL_ERR_SSL_HW_ACCEL_FALLTHROUGH) )
314 snprintf( buf, buflen, "SSL - Hardware acceleration function skipped / left alone data" );
Paul Bakker83f00bb2012-07-04 11:08:50 +0000315 if( use_ret == -(POLARSSL_ERR_SSL_COMPRESSION_FAILED) )
316 snprintf( buf, buflen, "SSL - Processing of the compression / decompression failed" );
Paul Bakker1d29fb52012-09-28 13:28:45 +0000317 if( use_ret == -(POLARSSL_ERR_SSL_BAD_HS_PROTOCOL_VERSION) )
318 snprintf( buf, buflen, "SSL - Handshake protocol not within min/max boundaries" );
Paul Bakker831a7552011-05-18 13:32:51 +0000319#endif /* POLARSSL_SSL_TLS_C */
Paul Bakker9d781402011-05-09 16:17:09 +0000320
321#if defined(POLARSSL_X509_PARSE_C)
322 if( use_ret == -(POLARSSL_ERR_X509_FEATURE_UNAVAILABLE) )
323 snprintf( buf, buflen, "X509 - Unavailable feature, e.g. RSA hashing/encryption combination" );
324 if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_PEM) )
325 snprintf( buf, buflen, "X509 - The PEM-encoded certificate contains invalid elements, e.g. invalid character" );
326 if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_FORMAT) )
327 snprintf( buf, buflen, "X509 - The certificate format is invalid, e.g. different type expected" );
328 if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_VERSION) )
329 snprintf( buf, buflen, "X509 - The certificate version element is invalid" );
330 if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_SERIAL) )
331 snprintf( buf, buflen, "X509 - The serial tag or value is invalid" );
332 if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_ALG) )
333 snprintf( buf, buflen, "X509 - The algorithm tag or value is invalid" );
334 if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_NAME) )
335 snprintf( buf, buflen, "X509 - The name tag or value is invalid" );
336 if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_DATE) )
337 snprintf( buf, buflen, "X509 - The date tag or value is invalid" );
338 if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_PUBKEY) )
339 snprintf( buf, buflen, "X509 - The pubkey tag or value is invalid (only RSA is supported)" );
340 if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_SIGNATURE) )
341 snprintf( buf, buflen, "X509 - The signature tag or value invalid" );
342 if( use_ret == -(POLARSSL_ERR_X509_CERT_INVALID_EXTENSIONS) )
343 snprintf( buf, buflen, "X509 - The extension tag or value is invalid" );
344 if( use_ret == -(POLARSSL_ERR_X509_CERT_UNKNOWN_VERSION) )
345 snprintf( buf, buflen, "X509 - Certificate or CRL has an unsupported version number" );
346 if( use_ret == -(POLARSSL_ERR_X509_CERT_UNKNOWN_SIG_ALG) )
347 snprintf( buf, buflen, "X509 - Signature algorithm (oid) is unsupported" );
Paul Bakker9db77422011-07-13 11:47:32 +0000348 if( use_ret == -(POLARSSL_ERR_X509_UNKNOWN_PK_ALG) )
349 snprintf( buf, buflen, "X509 - Key algorithm is unsupported (only RSA is supported)" );
Paul Bakker9d781402011-05-09 16:17:09 +0000350 if( use_ret == -(POLARSSL_ERR_X509_CERT_SIG_MISMATCH) )
351 snprintf( buf, buflen, "X509 - Certificate signature algorithms do not match. (see \\c ::x509_cert sig_oid)" );
352 if( use_ret == -(POLARSSL_ERR_X509_CERT_VERIFY_FAILED) )
353 snprintf( buf, buflen, "X509 - Certificate verification failed, e.g. CRL, CA or signature check failed" );
354 if( use_ret == -(POLARSSL_ERR_X509_KEY_INVALID_VERSION) )
355 snprintf( buf, buflen, "X509 - Unsupported RSA key version" );
356 if( use_ret == -(POLARSSL_ERR_X509_KEY_INVALID_FORMAT) )
357 snprintf( buf, buflen, "X509 - Invalid RSA key tag or value" );
Paul Bakker6c0ceb32011-12-04 12:24:18 +0000358 if( use_ret == -(POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT) )
359 snprintf( buf, buflen, "X509 - Format not recognized as DER or PEM" );
Paul Bakker69e095c2011-12-10 21:55:01 +0000360 if( use_ret == -(POLARSSL_ERR_X509_INVALID_INPUT) )
361 snprintf( buf, buflen, "X509 - Input invalid" );
362 if( use_ret == -(POLARSSL_ERR_X509_MALLOC_FAILED) )
363 snprintf( buf, buflen, "X509 - Allocation of memory failed" );
364 if( use_ret == -(POLARSSL_ERR_X509_FILE_IO_ERROR) )
365 snprintf( buf, buflen, "X509 - Read/write of file failed" );
Paul Bakker9d781402011-05-09 16:17:09 +0000366#endif /* POLARSSL_X509_PARSE_C */
367
368 if( strlen( buf ) == 0 )
369 snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
370 }
371
372 use_ret = ret & ~0xFF80;
373
374 if( use_ret == 0 )
375 return;
376
377 // If high level code is present, make a concatenation between both
378 // error strings.
379 //
380 len = strlen( buf );
381
382 if( len > 0 )
383 {
384 if( buflen - len < 5 )
385 return;
386
387 snprintf( buf + len, buflen - len, " : " );
388
389 buf += len + 3;
390 buflen -= len + 3;
391 }
392
393 // Low level error codes
394 //
395#if defined(POLARSSL_AES_C)
396 if( use_ret == -(POLARSSL_ERR_AES_INVALID_KEY_LENGTH) )
397 snprintf( buf, buflen, "AES - Invalid key length" );
398 if( use_ret == -(POLARSSL_ERR_AES_INVALID_INPUT_LENGTH) )
399 snprintf( buf, buflen, "AES - Invalid data input length" );
400#endif /* POLARSSL_AES_C */
401
Paul Bakkerdceecd82011-11-15 16:38:34 +0000402#if defined(POLARSSL_ASN1_PARSE_C)
403 if( use_ret == -(POLARSSL_ERR_ASN1_OUT_OF_DATA) )
404 snprintf( buf, buflen, "ASN1 - Out of data when parsing an ASN1 data structure" );
405 if( use_ret == -(POLARSSL_ERR_ASN1_UNEXPECTED_TAG) )
406 snprintf( buf, buflen, "ASN1 - ASN1 tag was of an unexpected value" );
407 if( use_ret == -(POLARSSL_ERR_ASN1_INVALID_LENGTH) )
408 snprintf( buf, buflen, "ASN1 - Error when trying to determine the length or invalid length" );
409 if( use_ret == -(POLARSSL_ERR_ASN1_LENGTH_MISMATCH) )
410 snprintf( buf, buflen, "ASN1 - Actual length differs from expected length" );
411 if( use_ret == -(POLARSSL_ERR_ASN1_INVALID_DATA) )
412 snprintf( buf, buflen, "ASN1 - Data is invalid. (not used)" );
Paul Bakker69e095c2011-12-10 21:55:01 +0000413 if( use_ret == -(POLARSSL_ERR_ASN1_MALLOC_FAILED) )
414 snprintf( buf, buflen, "ASN1 - Memory allocation failed" );
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000415 if( use_ret == -(POLARSSL_ERR_ASN1_BUF_TOO_SMALL) )
416 snprintf( buf, buflen, "ASN1 - Buffer too small when writing ASN.1 data structure" );
Paul Bakkerdceecd82011-11-15 16:38:34 +0000417#endif /* POLARSSL_ASN1_PARSE_C */
418
Paul Bakker9d781402011-05-09 16:17:09 +0000419#if defined(POLARSSL_BASE64_C)
420 if( use_ret == -(POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL) )
421 snprintf( buf, buflen, "BASE64 - Output buffer too small" );
422 if( use_ret == -(POLARSSL_ERR_BASE64_INVALID_CHARACTER) )
423 snprintf( buf, buflen, "BASE64 - Invalid character in input" );
424#endif /* POLARSSL_BASE64_C */
425
426#if defined(POLARSSL_BIGNUM_C)
427 if( use_ret == -(POLARSSL_ERR_MPI_FILE_IO_ERROR) )
428 snprintf( buf, buflen, "BIGNUM - An error occurred while reading from or writing to a file" );
429 if( use_ret == -(POLARSSL_ERR_MPI_BAD_INPUT_DATA) )
430 snprintf( buf, buflen, "BIGNUM - Bad input parameters to function" );
431 if( use_ret == -(POLARSSL_ERR_MPI_INVALID_CHARACTER) )
432 snprintf( buf, buflen, "BIGNUM - There is an invalid character in the digit string" );
433 if( use_ret == -(POLARSSL_ERR_MPI_BUFFER_TOO_SMALL) )
Paul Bakker69e095c2011-12-10 21:55:01 +0000434 snprintf( buf, buflen, "BIGNUM - The buffer is too small to write to" );
Paul Bakker9d781402011-05-09 16:17:09 +0000435 if( use_ret == -(POLARSSL_ERR_MPI_NEGATIVE_VALUE) )
436 snprintf( buf, buflen, "BIGNUM - The input arguments are negative or result in illegal output" );
437 if( use_ret == -(POLARSSL_ERR_MPI_DIVISION_BY_ZERO) )
438 snprintf( buf, buflen, "BIGNUM - The input argument for division is zero, which is not allowed" );
439 if( use_ret == -(POLARSSL_ERR_MPI_NOT_ACCEPTABLE) )
440 snprintf( buf, buflen, "BIGNUM - The input arguments are not acceptable" );
Paul Bakker69e095c2011-12-10 21:55:01 +0000441 if( use_ret == -(POLARSSL_ERR_MPI_MALLOC_FAILED) )
442 snprintf( buf, buflen, "BIGNUM - Memory allocation failed" );
Paul Bakker9d781402011-05-09 16:17:09 +0000443#endif /* POLARSSL_BIGNUM_C */
444
Paul Bakker83f00bb2012-07-04 11:08:50 +0000445#if defined(POLARSSL_BLOWFISH_C)
446 if( use_ret == -(POLARSSL_ERR_BLOWFISH_INVALID_KEY_LENGTH) )
447 snprintf( buf, buflen, "BLOWFISH - Invalid key length" );
448 if( use_ret == -(POLARSSL_ERR_BLOWFISH_INVALID_INPUT_LENGTH) )
449 snprintf( buf, buflen, "BLOWFISH - Invalid data input length" );
450#endif /* POLARSSL_BLOWFISH_C */
451
Paul Bakker9d781402011-05-09 16:17:09 +0000452#if defined(POLARSSL_CAMELLIA_C)
453 if( use_ret == -(POLARSSL_ERR_CAMELLIA_INVALID_KEY_LENGTH) )
454 snprintf( buf, buflen, "CAMELLIA - Invalid key length" );
455 if( use_ret == -(POLARSSL_ERR_CAMELLIA_INVALID_INPUT_LENGTH) )
456 snprintf( buf, buflen, "CAMELLIA - Invalid data input length" );
457#endif /* POLARSSL_CAMELLIA_C */
458
Paul Bakker880ac7e2011-11-27 14:50:49 +0000459#if defined(POLARSSL_CTR_DRBG_C)
460 if( use_ret == -(POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED) )
461 snprintf( buf, buflen, "CTR_DRBG - The entropy source failed" );
462 if( use_ret == -(POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG) )
463 snprintf( buf, buflen, "CTR_DRBG - Too many random requested in single call" );
464 if( use_ret == -(POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG) )
465 snprintf( buf, buflen, "CTR_DRBG - Input too large (Entropy + additional)" );
Paul Bakker69e095c2011-12-10 21:55:01 +0000466 if( use_ret == -(POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR) )
467 snprintf( buf, buflen, "CTR_DRBG - Read/write error in file" );
Paul Bakker880ac7e2011-11-27 14:50:49 +0000468#endif /* POLARSSL_CTR_DRBG_C */
469
Paul Bakker9d781402011-05-09 16:17:09 +0000470#if defined(POLARSSL_DES_C)
471 if( use_ret == -(POLARSSL_ERR_DES_INVALID_INPUT_LENGTH) )
472 snprintf( buf, buflen, "DES - The data input has an invalid length" );
473#endif /* POLARSSL_DES_C */
474
Paul Bakker6083fd22011-12-03 21:45:14 +0000475#if defined(POLARSSL_ENTROPY_C)
476 if( use_ret == -(POLARSSL_ERR_ENTROPY_SOURCE_FAILED) )
477 snprintf( buf, buflen, "ENTROPY - Critical entropy source failure" );
478 if( use_ret == -(POLARSSL_ERR_ENTROPY_MAX_SOURCES) )
479 snprintf( buf, buflen, "ENTROPY - No more sources can be added" );
Paul Bakker43655f42011-12-15 20:11:16 +0000480 if( use_ret == -(POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED) )
481 snprintf( buf, buflen, "ENTROPY - No sources have been added to poll" );
Paul Bakker6083fd22011-12-03 21:45:14 +0000482#endif /* POLARSSL_ENTROPY_C */
483
Paul Bakker030277a2012-04-17 12:24:26 +0000484#if defined(POLARSSL_GCM_C)
485 if( use_ret == -(POLARSSL_ERR_GCM_AUTH_FAILED) )
486 snprintf( buf, buflen, "GCM - Authenticated decryption failed" );
Paul Bakkerd8ef1672012-04-18 14:17:32 +0000487 if( use_ret == -(POLARSSL_ERR_GCM_BAD_INPUT) )
488 snprintf( buf, buflen, "GCM - Bad input parameters to function" );
Paul Bakker030277a2012-04-17 12:24:26 +0000489#endif /* POLARSSL_GCM_C */
490
Paul Bakker69e095c2011-12-10 21:55:01 +0000491#if defined(POLARSSL_MD2_C)
492 if( use_ret == -(POLARSSL_ERR_MD2_FILE_IO_ERROR) )
493 snprintf( buf, buflen, "MD2 - Read/write error in file" );
494#endif /* POLARSSL_MD2_C */
495
496#if defined(POLARSSL_MD4_C)
497 if( use_ret == -(POLARSSL_ERR_MD4_FILE_IO_ERROR) )
498 snprintf( buf, buflen, "MD4 - Read/write error in file" );
499#endif /* POLARSSL_MD4_C */
500
501#if defined(POLARSSL_MD5_C)
502 if( use_ret == -(POLARSSL_ERR_MD5_FILE_IO_ERROR) )
503 snprintf( buf, buflen, "MD5 - Read/write error in file" );
504#endif /* POLARSSL_MD5_C */
505
Paul Bakker9d781402011-05-09 16:17:09 +0000506#if defined(POLARSSL_NET_C)
507 if( use_ret == -(POLARSSL_ERR_NET_UNKNOWN_HOST) )
508 snprintf( buf, buflen, "NET - Failed to get an IP address for the given hostname" );
509 if( use_ret == -(POLARSSL_ERR_NET_SOCKET_FAILED) )
510 snprintf( buf, buflen, "NET - Failed to open a socket" );
511 if( use_ret == -(POLARSSL_ERR_NET_CONNECT_FAILED) )
512 snprintf( buf, buflen, "NET - The connection to the given server / port failed" );
513 if( use_ret == -(POLARSSL_ERR_NET_BIND_FAILED) )
514 snprintf( buf, buflen, "NET - Binding of the socket failed" );
515 if( use_ret == -(POLARSSL_ERR_NET_LISTEN_FAILED) )
516 snprintf( buf, buflen, "NET - Could not listen on the socket" );
517 if( use_ret == -(POLARSSL_ERR_NET_ACCEPT_FAILED) )
518 snprintf( buf, buflen, "NET - Could not accept the incoming connection" );
519 if( use_ret == -(POLARSSL_ERR_NET_RECV_FAILED) )
520 snprintf( buf, buflen, "NET - Reading information from the socket failed" );
521 if( use_ret == -(POLARSSL_ERR_NET_SEND_FAILED) )
522 snprintf( buf, buflen, "NET - Sending information through the socket failed" );
523 if( use_ret == -(POLARSSL_ERR_NET_CONN_RESET) )
524 snprintf( buf, buflen, "NET - Connection was reset by peer" );
Paul Bakker831a7552011-05-18 13:32:51 +0000525 if( use_ret == -(POLARSSL_ERR_NET_WANT_READ) )
526 snprintf( buf, buflen, "NET - Connection requires a read call" );
527 if( use_ret == -(POLARSSL_ERR_NET_WANT_WRITE) )
528 snprintf( buf, buflen, "NET - Connection requires a write call" );
Paul Bakker9d781402011-05-09 16:17:09 +0000529#endif /* POLARSSL_NET_C */
530
531#if defined(POLARSSL_PADLOCK_C)
532 if( use_ret == -(POLARSSL_ERR_PADLOCK_DATA_MISALIGNED) )
533 snprintf( buf, buflen, "PADLOCK - Input data should be aligned" );
534#endif /* POLARSSL_PADLOCK_C */
535
Paul Bakkerd14277d2012-09-26 15:19:05 +0000536#if defined(POLARSSL_PBKDF2_C)
537 if( use_ret == -(POLARSSL_ERR_PBKDF2_BAD_INPUT_DATA) )
538 snprintf( buf, buflen, "PBKDF2 - Bad input parameters to function" );
539#endif /* POLARSSL_PBKDF2_C */
540
Paul Bakker69e095c2011-12-10 21:55:01 +0000541#if defined(POLARSSL_SHA1_C)
542 if( use_ret == -(POLARSSL_ERR_SHA1_FILE_IO_ERROR) )
543 snprintf( buf, buflen, "SHA1 - Read/write error in file" );
544#endif /* POLARSSL_SHA1_C */
545
546#if defined(POLARSSL_SHA2_C)
547 if( use_ret == -(POLARSSL_ERR_SHA2_FILE_IO_ERROR) )
548 snprintf( buf, buflen, "SHA2 - Read/write error in file" );
549#endif /* POLARSSL_SHA2_C */
550
551#if defined(POLARSSL_SHA4_C)
552 if( use_ret == -(POLARSSL_ERR_SHA4_FILE_IO_ERROR) )
553 snprintf( buf, buflen, "SHA4 - Read/write error in file" );
554#endif /* POLARSSL_SHA4_C */
555
Paul Bakker9d781402011-05-09 16:17:09 +0000556#if defined(POLARSSL_XTEA_C)
557 if( use_ret == -(POLARSSL_ERR_XTEA_INVALID_INPUT_LENGTH) )
558 snprintf( buf, buflen, "XTEA - The data input has an invalid length" );
559#endif /* POLARSSL_XTEA_C */
560
561 if( strlen( buf ) != 0 )
562 return;
563
564 snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
565}
566
Paul Bakker8fe40dc2013-02-02 12:43:08 +0100567#else /* POLARSSL_ERROR_C */
568
569#if defined(POLARSSL_ERROR_STRERROR_DUMMY)
570
571#include <string.h>
572
573/*
574 * Provide an non-function in case POLARSSL_ERROR_C is not defined
575 */
576void error_strerror( int ret, char *buf, size_t buflen )
577{
578 ((void) ret);
579
580 if( buflen > 0 )
581 buf[0] = '\0';
582}
583
584#endif /* POLARSSL_ERROR_STRERROR_DUMMY */
Paul Bakker9a736322012-11-14 12:39:52 +0000585#endif /* POLARSSL_ERROR_C */