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