Tizen 2.0 Release
[external/libgnutls26.git] / lib / gnutls_errors.c
1 /*
2  * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3  * 2009, 2010 Free Software Foundation, Inc.
4  *
5  * Author: Nikos Mavrogiannopoulos
6  *
7  * This file is part of GnuTLS.
8  *
9  * The GnuTLS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1 of
12  * the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA
23  *
24  */
25
26 #include <gnutls_int.h>
27 #include "gnutls_errors.h"
28 #include <libtasn1.h>
29 #ifdef STDC_HEADERS
30 #include <stdarg.h>
31 #endif
32
33 /* I18n of error codes. */
34 #include "gettext.h"
35 #define _(String) dgettext (PACKAGE, String)
36 #define N_(String) gettext_noop (String)
37
38 #define ERROR_ENTRY(desc, name, fatal) \
39         { desc, #name, name, fatal}
40
41 struct gnutls_error_entry
42 {
43   const char *desc;
44   const char *_name;
45   int number;
46   int fatal;                    /* whether this error is fatal and the session for handshake 
47                                  * should be terminated.
48                                  */
49 };
50 typedef struct gnutls_error_entry gnutls_error_entry;
51
52 static const gnutls_error_entry error_algorithms[] = {
53   /* "Short Description", Error code define, critical (0,1) -- 1 in most cases */
54   ERROR_ENTRY (N_("Success."), GNUTLS_E_SUCCESS, 0),
55   ERROR_ENTRY (N_("Could not negotiate a supported cipher suite."),
56                GNUTLS_E_UNKNOWN_CIPHER_SUITE, 1),
57   ERROR_ENTRY (N_("The cipher type is unsupported."),
58                GNUTLS_E_UNKNOWN_CIPHER_TYPE, 1),
59   ERROR_ENTRY (N_("The certificate and the given key do not match."),
60                GNUTLS_E_CERTIFICATE_KEY_MISMATCH, 1),
61   ERROR_ENTRY (N_("Could not negotiate a supported compression method."),
62                GNUTLS_E_UNKNOWN_COMPRESSION_ALGORITHM, 1),
63   ERROR_ENTRY (N_("An unknown public key algorithm was encountered."),
64                GNUTLS_E_UNKNOWN_PK_ALGORITHM, 1),
65
66   ERROR_ENTRY (N_("An algorithm that is not enabled was negotiated."),
67                GNUTLS_E_UNWANTED_ALGORITHM, 1),
68   ERROR_ENTRY (N_("A large TLS record packet was received."),
69                GNUTLS_E_LARGE_PACKET, 1),
70   ERROR_ENTRY (N_("A record packet with illegal version was received."),
71                GNUTLS_E_UNSUPPORTED_VERSION_PACKET, 1),
72   ERROR_ENTRY (N_
73                ("The Diffie-Hellman prime sent by the server is not acceptable (not long enough)."),
74                GNUTLS_E_DH_PRIME_UNACCEPTABLE, 1),
75   ERROR_ENTRY (N_("A TLS packet with unexpected length was received."),
76                GNUTLS_E_UNEXPECTED_PACKET_LENGTH, 1),
77   ERROR_ENTRY (N_
78                ("The specified session has been invalidated for some reason."),
79                GNUTLS_E_INVALID_SESSION, 1),
80
81   ERROR_ENTRY (N_("GnuTLS internal error."), GNUTLS_E_INTERNAL_ERROR, 1),
82   ERROR_ENTRY (N_("An illegal TLS extension was received."),
83                GNUTLS_E_RECEIVED_ILLEGAL_EXTENSION, 1),
84   ERROR_ENTRY (N_("A TLS fatal alert has been received."),
85                GNUTLS_E_FATAL_ALERT_RECEIVED, 1),
86   ERROR_ENTRY (N_("An unexpected TLS packet was received."),
87                GNUTLS_E_UNEXPECTED_PACKET, 1),
88   ERROR_ENTRY (N_("A TLS warning alert has been received."),
89                GNUTLS_E_WARNING_ALERT_RECEIVED, 0),
90   ERROR_ENTRY (N_
91                ("An error was encountered at the TLS Finished packet calculation."),
92                GNUTLS_E_ERROR_IN_FINISHED_PACKET, 1),
93   ERROR_ENTRY (N_("The peer did not send any certificate."),
94                GNUTLS_E_NO_CERTIFICATE_FOUND, 1),
95   ERROR_ENTRY (N_("The given DSA key is incompatible with the selected TLS protocol."),
96                GNUTLS_E_INCOMPAT_DSA_KEY_WITH_TLS_PROTOCOL, 1),
97
98   ERROR_ENTRY (N_("There is already a crypto algorithm with lower priority."),
99                GNUTLS_E_CRYPTO_ALREADY_REGISTERED, 1),
100
101   ERROR_ENTRY (N_("No temporary RSA parameters were found."),
102                GNUTLS_E_NO_TEMPORARY_RSA_PARAMS, 1),
103   ERROR_ENTRY (N_("No temporary DH parameters were found."),
104                GNUTLS_E_NO_TEMPORARY_DH_PARAMS, 1),
105   ERROR_ENTRY (N_("An unexpected TLS handshake packet was received."),
106                GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET, 1),
107   ERROR_ENTRY (N_("The scanning of a large integer has failed."),
108                GNUTLS_E_MPI_SCAN_FAILED, 1),
109   ERROR_ENTRY (N_("Could not export a large integer."),
110                GNUTLS_E_MPI_PRINT_FAILED, 1),
111   ERROR_ENTRY (N_("Decryption has failed."), GNUTLS_E_DECRYPTION_FAILED, 1),
112   ERROR_ENTRY (N_("Encryption has failed."), GNUTLS_E_ENCRYPTION_FAILED, 1),
113   ERROR_ENTRY (N_("Public key decryption has failed."),
114                GNUTLS_E_PK_DECRYPTION_FAILED, 1),
115   ERROR_ENTRY (N_("Public key encryption has failed."),
116                GNUTLS_E_PK_ENCRYPTION_FAILED, 1),
117   ERROR_ENTRY (N_("Public key signing has failed."), GNUTLS_E_PK_SIGN_FAILED,
118                1),
119   ERROR_ENTRY (N_("Public key signature verification has failed."),
120                GNUTLS_E_PK_SIG_VERIFY_FAILED, 1),
121   ERROR_ENTRY (N_("Decompression of the TLS record packet has failed."),
122                GNUTLS_E_DECOMPRESSION_FAILED, 1),
123   ERROR_ENTRY (N_("Compression of the TLS record packet has failed."),
124                GNUTLS_E_COMPRESSION_FAILED, 1),
125
126   ERROR_ENTRY (N_("Internal error in memory allocation."),
127                GNUTLS_E_MEMORY_ERROR, 1),
128   ERROR_ENTRY (N_("An unimplemented or disabled feature has been requested."),
129                GNUTLS_E_UNIMPLEMENTED_FEATURE, 1),
130   ERROR_ENTRY (N_("Insufficient credentials for that request."),
131                GNUTLS_E_INSUFFICIENT_CREDENTIALS, 1),
132   ERROR_ENTRY (N_("Error in password file."), GNUTLS_E_SRP_PWD_ERROR, 1),
133   ERROR_ENTRY (N_("Wrong padding in PKCS1 packet."), GNUTLS_E_PKCS1_WRONG_PAD,
134                1),
135   ERROR_ENTRY (N_("The requested session has expired."), GNUTLS_E_EXPIRED, 1),
136   ERROR_ENTRY (N_("Hashing has failed."), GNUTLS_E_HASH_FAILED, 1),
137   ERROR_ENTRY (N_("Base64 decoding error."), GNUTLS_E_BASE64_DECODING_ERROR,
138                1),
139   ERROR_ENTRY (N_("Base64 unexpected header error."),
140                GNUTLS_E_BASE64_UNEXPECTED_HEADER_ERROR,
141                1),
142   ERROR_ENTRY (N_("Base64 encoding error."), GNUTLS_E_BASE64_ENCODING_ERROR,
143                1),
144   ERROR_ENTRY (N_("Parsing error in password file."),
145                GNUTLS_E_SRP_PWD_PARSING_ERROR, 1),
146   ERROR_ENTRY (N_("The requested data were not available."),
147                GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE, 1),
148   ERROR_ENTRY (N_("Error in the pull function."), GNUTLS_E_PULL_ERROR, 1),
149   ERROR_ENTRY (N_("Error in the push function."), GNUTLS_E_PUSH_ERROR, 1),
150   ERROR_ENTRY (N_
151                ("The upper limit of record packet sequence numbers has been reached. Wow!"),
152                GNUTLS_E_RECORD_LIMIT_REACHED, 1),
153   ERROR_ENTRY (N_("Error in the certificate."), GNUTLS_E_CERTIFICATE_ERROR,
154                1),
155   ERROR_ENTRY (N_("Unknown Subject Alternative name in X.509 certificate."),
156                GNUTLS_E_X509_UNKNOWN_SAN, 1),
157
158   ERROR_ENTRY (N_("Unsupported critical extension in X.509 certificate."),
159                GNUTLS_E_X509_UNSUPPORTED_CRITICAL_EXTENSION, 1),
160   ERROR_ENTRY (N_("Key usage violation in certificate has been detected."),
161                GNUTLS_E_KEY_USAGE_VIOLATION, 1),
162   ERROR_ENTRY (N_("Resource temporarily unavailable, try again."),
163                GNUTLS_E_AGAIN, 0),
164   ERROR_ENTRY (N_("Function was interrupted."), GNUTLS_E_INTERRUPTED, 0),
165   ERROR_ENTRY (N_("Rehandshake was requested by the peer."),
166                GNUTLS_E_REHANDSHAKE, 0),
167   ERROR_ENTRY (N_
168                ("TLS Application data were received, while expecting handshake data."),
169                GNUTLS_E_GOT_APPLICATION_DATA, 1),
170   ERROR_ENTRY (N_("Error in Database backend."), GNUTLS_E_DB_ERROR, 1),
171   ERROR_ENTRY (N_("The certificate type is not supported."),
172                GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE, 1),
173   ERROR_ENTRY (N_("The given memory buffer is too short to hold parameters."),
174                GNUTLS_E_SHORT_MEMORY_BUFFER, 1),
175   ERROR_ENTRY (N_("The request is invalid."), GNUTLS_E_INVALID_REQUEST, 1),
176   ERROR_ENTRY (N_("An illegal parameter has been received."),
177                GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER, 1),
178   ERROR_ENTRY (N_("Error while reading file."), GNUTLS_E_FILE_ERROR, 1),
179
180   ERROR_ENTRY (N_("ASN1 parser: Element was not found."),
181                GNUTLS_E_ASN1_ELEMENT_NOT_FOUND, 1),
182   ERROR_ENTRY (N_("ASN1 parser: Identifier was not found"),
183                GNUTLS_E_ASN1_IDENTIFIER_NOT_FOUND, 1),
184   ERROR_ENTRY (N_("ASN1 parser: Error in DER parsing."),
185                GNUTLS_E_ASN1_DER_ERROR, 1),
186   ERROR_ENTRY (N_("ASN1 parser: Value was not found."),
187                GNUTLS_E_ASN1_VALUE_NOT_FOUND, 1),
188   ERROR_ENTRY (N_("ASN1 parser: Generic parsing error."),
189                GNUTLS_E_ASN1_GENERIC_ERROR, 1),
190   ERROR_ENTRY (N_("ASN1 parser: Value is not valid."),
191                GNUTLS_E_ASN1_VALUE_NOT_VALID, 1),
192   ERROR_ENTRY (N_("ASN1 parser: Error in TAG."), GNUTLS_E_ASN1_TAG_ERROR, 1),
193   ERROR_ENTRY (N_("ASN1 parser: error in implicit tag"),
194                GNUTLS_E_ASN1_TAG_IMPLICIT, 1),
195   ERROR_ENTRY (N_("ASN1 parser: Error in type 'ANY'."),
196                GNUTLS_E_ASN1_TYPE_ANY_ERROR, 1),
197   ERROR_ENTRY (N_("ASN1 parser: Syntax error."), GNUTLS_E_ASN1_SYNTAX_ERROR,
198                1),
199   ERROR_ENTRY (N_("ASN1 parser: Overflow in DER parsing."),
200                GNUTLS_E_ASN1_DER_OVERFLOW, 1),
201
202   ERROR_ENTRY (N_("Too many empty record packets have been received."),
203                GNUTLS_E_TOO_MANY_EMPTY_PACKETS, 1),
204   ERROR_ENTRY (N_("The initialization of GnuTLS-extra has failed."),
205                GNUTLS_E_INIT_LIBEXTRA, 1),
206   ERROR_ENTRY (N_
207                ("The GnuTLS library version does not match the GnuTLS-extra library version."),
208                GNUTLS_E_LIBRARY_VERSION_MISMATCH, 1),
209   ERROR_ENTRY (N_("The gcrypt library version is too old."),
210                GNUTLS_E_INCOMPATIBLE_GCRYPT_LIBRARY, 1),
211
212   ERROR_ENTRY (N_("The tasn1 library version is too old."),
213                GNUTLS_E_INCOMPATIBLE_LIBTASN1_LIBRARY, 1),
214   ERROR_ENTRY (N_("The OpenPGP User ID is revoked."),
215                GNUTLS_E_OPENPGP_UID_REVOKED, 1),
216   ERROR_ENTRY (N_("The OpenPGP key has not a preferred key set."),
217                GNUTLS_E_OPENPGP_PREFERRED_KEY_ERROR, 1),
218   ERROR_ENTRY (N_("Error loading the keyring."),
219                GNUTLS_E_OPENPGP_KEYRING_ERROR, 1),
220   ERROR_ENTRY (N_("The initialization of crypto backend has failed."),
221                GNUTLS_E_CRYPTO_INIT_FAILED, 1),
222   ERROR_ENTRY (N_("The initialization of LZO has failed."),
223                GNUTLS_E_LZO_INIT_FAILED, 1),
224   ERROR_ENTRY (N_("No supported compression algorithms have been found."),
225                GNUTLS_E_NO_COMPRESSION_ALGORITHMS, 1),
226   ERROR_ENTRY (N_("No supported cipher suites have been found."),
227                GNUTLS_E_NO_CIPHER_SUITES, 1),
228   ERROR_ENTRY (N_("Could not get OpenPGP key."),
229                GNUTLS_E_OPENPGP_GETKEY_FAILED, 1),
230   ERROR_ENTRY (N_("Could not find OpenPGP subkey."),
231                GNUTLS_E_OPENPGP_SUBKEY_ERROR, 1),
232   ERROR_ENTRY (N_("Safe renegotiation failed."),
233                GNUTLS_E_SAFE_RENEGOTIATION_FAILED, 1),
234   ERROR_ENTRY (N_("Unsafe renegotiation denied."),
235                GNUTLS_E_UNSAFE_RENEGOTIATION_DENIED, 1),
236
237   ERROR_ENTRY (N_("The SRP username supplied is illegal."),
238                GNUTLS_E_ILLEGAL_SRP_USERNAME, 1),
239   ERROR_ENTRY (N_("The SRP username supplied is unknown."),
240                GNUTLS_E_UNKNOWN_SRP_USERNAME, 1),
241
242   ERROR_ENTRY (N_("The OpenPGP fingerprint is not supported."),
243                GNUTLS_E_OPENPGP_FINGERPRINT_UNSUPPORTED, 1),
244   ERROR_ENTRY (N_("The signature algorithm is not supported."),
245                GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM, 1),
246   ERROR_ENTRY (N_("The certificate has unsupported attributes."),
247                GNUTLS_E_X509_UNSUPPORTED_ATTRIBUTE, 1),
248   ERROR_ENTRY (N_("The OID is not supported."), GNUTLS_E_X509_UNSUPPORTED_OID,
249                1),
250   ERROR_ENTRY (N_("The hash algorithm is unknown."),
251                GNUTLS_E_UNKNOWN_HASH_ALGORITHM, 1),
252   ERROR_ENTRY (N_("The PKCS structure's content type is unknown."),
253                GNUTLS_E_UNKNOWN_PKCS_CONTENT_TYPE, 1),
254   ERROR_ENTRY (N_("The PKCS structure's bag type is unknown."),
255                GNUTLS_E_UNKNOWN_PKCS_BAG_TYPE, 1),
256   ERROR_ENTRY (N_("The given password contains invalid characters."),
257                GNUTLS_E_INVALID_PASSWORD, 1),
258   ERROR_ENTRY (N_("The Message Authentication Code verification failed."),
259                GNUTLS_E_MAC_VERIFY_FAILED, 1),
260   ERROR_ENTRY (N_("Some constraint limits were reached."),
261                GNUTLS_E_CONSTRAINT_ERROR, 1),
262   ERROR_ENTRY (N_("Failed to acquire random data."), GNUTLS_E_RANDOM_FAILED,
263                1),
264
265   ERROR_ENTRY (N_("Received a TLS/IA Intermediate Phase Finished message"),
266                GNUTLS_E_WARNING_IA_IPHF_RECEIVED, 0),
267   ERROR_ENTRY (N_("Received a TLS/IA Final Phase Finished message"),
268                GNUTLS_E_WARNING_IA_FPHF_RECEIVED, 0),
269   ERROR_ENTRY (N_("Verifying TLS/IA phase checksum failed"),
270                GNUTLS_E_IA_VERIFY_FAILED, 1),
271
272   ERROR_ENTRY (N_("The specified algorithm or protocol is unknown."),
273                GNUTLS_E_UNKNOWN_ALGORITHM, 1),
274
275   ERROR_ENTRY (N_("The handshake data size is too large (DoS?), "
276                   "check gnutls_handshake_set_max_packet_length()."),
277                GNUTLS_E_HANDSHAKE_TOO_LARGE, 1),
278
279   ERROR_ENTRY (N_("Error opening /dev/crypto"),
280                GNUTLS_E_CRYPTODEV_DEVICE_ERROR, 1),
281
282   ERROR_ENTRY (N_("Error interfacing with /dev/crypto"),
283                GNUTLS_E_CRYPTODEV_IOCTL_ERROR, 1),
284
285   ERROR_ENTRY (N_("Channel binding data not available"),
286                GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE, 1),
287
288   ERROR_ENTRY (N_("PKCS #11 error."),
289                GNUTLS_E_PKCS11_ERROR, 1),
290   ERROR_ENTRY (N_("PKCS #11 initialization error."),
291                GNUTLS_E_PKCS11_LOAD_ERROR, 1),
292   ERROR_ENTRY (N_("Error in parsing."),
293                GNUTLS_E_PARSING_ERROR, 1),
294   ERROR_ENTRY (N_("PKCS #11 error in PIN."),
295                GNUTLS_E_PKCS11_PIN_ERROR, 1),
296   ERROR_ENTRY (N_("PKCS #11 PIN should be saved."),
297                GNUTLS_E_PKCS11_ERROR, 1),
298   ERROR_ENTRY (N_("PKCS #11 error in slot"),
299                GNUTLS_E_PKCS11_SLOT_ERROR, 1),
300   ERROR_ENTRY (N_("Thread locking error"),
301                GNUTLS_E_LOCKING_ERROR, 1),
302   ERROR_ENTRY (N_("PKCS #11 error in attribute"),
303                GNUTLS_E_PKCS11_ATTRIBUTE_ERROR, 1),
304   ERROR_ENTRY (N_("PKCS #11 error in device"),
305                GNUTLS_E_PKCS11_DEVICE_ERROR, 1),
306   ERROR_ENTRY (N_("PKCS #11 error in data"),
307                GNUTLS_E_PKCS11_DATA_ERROR, 1),
308   ERROR_ENTRY (N_("PKCS #11 unsupported feature"),
309                GNUTLS_E_PKCS11_UNSUPPORTED_FEATURE_ERROR, 1),
310   ERROR_ENTRY (N_("PKCS #11 error in key"),
311                GNUTLS_E_PKCS11_KEY_ERROR, 1),
312   ERROR_ENTRY (N_("PKCS #11 PIN expired"),
313                GNUTLS_E_PKCS11_PIN_EXPIRED, 1),
314   ERROR_ENTRY (N_("PKCS #11 PIN locked"),
315                GNUTLS_E_PKCS11_PIN_LOCKED, 1),
316   ERROR_ENTRY (N_("PKCS #11 error in session"),
317                GNUTLS_E_PKCS11_SESSION_ERROR, 1),
318   ERROR_ENTRY (N_("PKCS #11 error in signature"),
319                GNUTLS_E_PKCS11_SIGNATURE_ERROR, 1),
320   ERROR_ENTRY (N_("PKCS #11 error in token"),
321                GNUTLS_E_PKCS11_TOKEN_ERROR, 1),
322   ERROR_ENTRY (N_("PKCS #11 user error"),
323                GNUTLS_E_PKCS11_USER_ERROR, 1),
324   ERROR_ENTRY (N_("The provided X.509 certificate list is not sorted (in subject to issuer order)"),
325                GNUTLS_E_CERTIFICATE_LIST_UNSORTED, 1),
326   {NULL, NULL, 0, 0}
327 };
328
329 /**
330  * gnutls_error_is_fatal:
331  * @error: is a GnuTLS error code, a negative value
332  *
333  * If a GnuTLS function returns a negative value you may feed that
334  * value to this function to see if the error condition is fatal.
335  *
336  * Note that you may want to check the error code manually, since some
337  * non-fatal errors to the protocol may be fatal for you program.
338  *
339  * This function is only useful if you are dealing with errors from
340  * the record layer or the handshake layer.
341  *
342  * Returns: 1 if the error code is fatal, for positive @error values,
343  *   0 is returned.  For unknown @error values, -1 is returned.
344  **/
345 int
346 gnutls_error_is_fatal (int error)
347 {
348   int ret = 1;
349   const gnutls_error_entry *p;
350
351   /* Input sanitzation.  Positive values are not errors at all, and
352      definitely not fatal. */
353   if (error > 0)
354     return 0;
355
356   for (p = error_algorithms; p->desc != NULL; p++)
357     {
358       if (p->number == error)
359         {
360           ret = p->fatal;
361           break;
362         }
363     }
364
365   return ret;
366 }
367
368 /**
369  * gnutls_perror:
370  * @error: is a GnuTLS error code, a negative value
371  *
372  * This function is like perror(). The only difference is that it
373  * accepts an error number returned by a gnutls function.
374  **/
375 void
376 gnutls_perror (int error)
377 {
378   fprintf (stderr, "GnuTLS error: %s\n", gnutls_strerror (error));
379 }
380
381
382 /**
383  * gnutls_strerror:
384  * @error: is a GnuTLS error code, a negative value
385  *
386  * This function is similar to strerror.  The difference is that it
387  * accepts an error number returned by a gnutls function; In case of
388  * an unknown error a descriptive string is sent instead of %NULL.
389  *
390  * Error codes are always a negative value.
391  *
392  * Returns: A string explaining the GnuTLS error message.
393  **/
394 const char *
395 gnutls_strerror (int error)
396 {
397   const char *ret = NULL;
398   const gnutls_error_entry *p;
399
400   for (p = error_algorithms; p->desc != NULL; p++)
401     {
402       if (p->number == error)
403         {
404           ret = p->desc;
405           break;
406         }
407     }
408
409   /* avoid prefix */
410   if (ret == NULL)
411     return _("(unknown error code)");
412
413   return _(ret);
414 }
415
416 /**
417  * gnutls_strerror_name:
418  * @error: is an error returned by a gnutls function.
419  *
420  * Return the GnuTLS error code define as a string.  For example,
421  * gnutls_strerror_name (GNUTLS_E_DH_PRIME_UNACCEPTABLE) will return
422  * the string "GNUTLS_E_DH_PRIME_UNACCEPTABLE".
423  *
424  * Returns: A string corresponding to the symbol name of the error
425  * code.
426  *
427  * Since: 2.6.0
428  **/
429 const char *
430 gnutls_strerror_name (int error)
431 {
432   const char *ret = NULL;
433   const gnutls_error_entry *p;
434
435   for (p = error_algorithms; p->desc != NULL; p++)
436     {
437       if (p->number == error)
438         {
439           ret = p->_name;
440           break;
441         }
442     }
443
444   return ret;
445 }
446
447 int
448 _gnutls_asn2err (int asn_err)
449 {
450   switch (asn_err)
451     {
452     case ASN1_FILE_NOT_FOUND:
453       return GNUTLS_E_FILE_ERROR;
454     case ASN1_ELEMENT_NOT_FOUND:
455       return GNUTLS_E_ASN1_ELEMENT_NOT_FOUND;
456     case ASN1_IDENTIFIER_NOT_FOUND:
457       return GNUTLS_E_ASN1_IDENTIFIER_NOT_FOUND;
458     case ASN1_DER_ERROR:
459       return GNUTLS_E_ASN1_DER_ERROR;
460     case ASN1_VALUE_NOT_FOUND:
461       return GNUTLS_E_ASN1_VALUE_NOT_FOUND;
462     case ASN1_GENERIC_ERROR:
463       return GNUTLS_E_ASN1_GENERIC_ERROR;
464     case ASN1_VALUE_NOT_VALID:
465       return GNUTLS_E_ASN1_VALUE_NOT_VALID;
466     case ASN1_TAG_ERROR:
467       return GNUTLS_E_ASN1_TAG_ERROR;
468     case ASN1_TAG_IMPLICIT:
469       return GNUTLS_E_ASN1_TAG_IMPLICIT;
470     case ASN1_ERROR_TYPE_ANY:
471       return GNUTLS_E_ASN1_TYPE_ANY_ERROR;
472     case ASN1_SYNTAX_ERROR:
473       return GNUTLS_E_ASN1_SYNTAX_ERROR;
474     case ASN1_MEM_ERROR:
475       return GNUTLS_E_SHORT_MEMORY_BUFFER;
476     case ASN1_MEM_ALLOC_ERROR:
477       return GNUTLS_E_MEMORY_ERROR;
478     case ASN1_DER_OVERFLOW:
479       return GNUTLS_E_ASN1_DER_OVERFLOW;
480     default:
481       return GNUTLS_E_ASN1_GENERIC_ERROR;
482     }
483 }
484
485 void
486 _gnutls_mpi_log (const char *prefix, bigint_t a)
487 {
488   size_t binlen = 0;
489   void *binbuf;
490   size_t hexlen;
491   char *hexbuf;
492   int res;
493
494   if (_gnutls_log_level < 2) return;
495
496   res = _gnutls_mpi_print (a, NULL, &binlen);
497   if (res < 0 && res != GNUTLS_E_SHORT_MEMORY_BUFFER)
498     {
499       gnutls_assert ();
500       _gnutls_hard_log ("MPI: %s can't print value (%d/%d)\n", prefix, res,
501                         (int) binlen);
502       return;
503     }
504
505   if (binlen > 1024 * 1024)
506     {
507       gnutls_assert ();
508       _gnutls_hard_log ("MPI: %s too large mpi (%d)\n", prefix, (int) binlen);
509       return;
510     }
511
512   binbuf = gnutls_malloc (binlen);
513   if (!binbuf)
514     {
515       gnutls_assert ();
516       _gnutls_hard_log ("MPI: %s out of memory (%d)\n", prefix, (int) binlen);
517       return;
518     }
519
520   res = _gnutls_mpi_print (a, binbuf, &binlen);
521   if (res != 0)
522     {
523       gnutls_assert ();
524       _gnutls_hard_log ("MPI: %s can't print value (%d/%d)\n", prefix, res,
525                         (int) binlen);
526       gnutls_free (binbuf);
527       return;
528     }
529
530   hexlen = 2 * binlen + 1;
531   hexbuf = gnutls_malloc (hexlen);
532
533   if (!hexbuf)
534     {
535       gnutls_assert ();
536       _gnutls_hard_log ("MPI: %s out of memory (hex %d)\n", prefix, (int) hexlen);
537       gnutls_free (binbuf);
538       return;
539     }
540
541   _gnutls_bin2hex (binbuf, binlen, hexbuf, hexlen, NULL);
542
543   _gnutls_hard_log ("MPI: length: %d\n\t%s%s\n", (int) binlen, prefix,
544                     hexbuf);
545
546   gnutls_free (hexbuf);
547   gnutls_free (binbuf);
548 }
549
550 /* this function will output a message using the
551  * caller provided function
552  */
553 void
554 _gnutls_log (int level, const char *fmt, ...)
555 {
556   va_list args;
557   char *str;
558   int ret;
559
560   if (_gnutls_log_func == NULL)
561     return;
562
563   va_start (args, fmt);
564   ret = vasprintf (&str, fmt, args);
565   va_end (args);
566
567   if (ret >= 0)
568     {
569       _gnutls_log_func (level, str);
570       free (str);
571     }
572 }
573
574 #ifndef DEBUG
575 #ifndef C99_MACROS
576
577 /* Without C99 macros these functions have to
578  * be called. This may affect performance.
579  */
580 void
581 _gnutls_null_log (void *x, ...)
582 {
583   return;
584 }
585
586 #endif /* C99_MACROS */
587 #endif /* DEBUG */