gnutls-cli: Fix uninitialized variable when PKCS#11 uris in use.
[gnutls.git] / lib / gnutls_errors.c
blob37e5b1e4e472507ff5116501b04ae4ee47099f6e
1 /*
2 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 * 2009, 2010 Free Software Foundation, Inc.
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GnuTLS.
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.
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.
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
26 #include <gnutls_int.h>
27 #include "gnutls_errors.h"
28 #include <libtasn1.h>
29 #ifdef STDC_HEADERS
30 #include <stdarg.h>
31 #endif
33 /* I18n of error codes. */
34 #include "gettext.h"
35 #define _(String) dgettext (PACKAGE, String)
36 #define N_(String) gettext_noop (String)
38 #define ERROR_ENTRY(desc, name, fatal) \
39 { desc, #name, name, fatal}
41 struct gnutls_error_entry
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.
50 typedef struct gnutls_error_entry gnutls_error_entry;
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),
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),
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),
98 ERROR_ENTRY (N_("There is already a crypto algorithm with lower priority."),
99 GNUTLS_E_CRYPTO_ALREADY_REGISTERED, 1),
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,
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),
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,
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,
139 ERROR_ENTRY (N_("Base64 unexpected header error."),
140 GNUTLS_E_BASE64_UNEXPECTED_HEADER_ERROR,
142 ERROR_ENTRY (N_("Base64 encoding error."), GNUTLS_E_BASE64_ENCODING_ERROR,
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,
155 ERROR_ENTRY (N_("Unknown Subject Alternative name in X.509 certificate."),
156 GNUTLS_E_X509_UNKNOWN_SAN, 1),
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),
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,
199 ERROR_ENTRY (N_("ASN1 parser: Overflow in DER parsing."),
200 GNUTLS_E_ASN1_DER_OVERFLOW, 1),
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),
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),
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),
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,
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,
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),
272 ERROR_ENTRY (N_("The specified algorithm or protocol is unknown."),
273 GNUTLS_E_UNKNOWN_ALGORITHM, 1),
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),
279 ERROR_ENTRY (N_("Error opening /dev/crypto"),
280 GNUTLS_E_CRYPTODEV_DEVICE_ERROR, 1),
282 ERROR_ENTRY (N_("Error interfacing with /dev/crypto"),
283 GNUTLS_E_CRYPTODEV_IOCTL_ERROR, 1),
285 ERROR_ENTRY (N_("Channel binding data not available"),
286 GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE, 1),
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}
330 * gnutls_error_is_fatal:
331 * @error: is a GnuTLS error code, a negative value
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.
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.
339 * This function is only useful if you are dealing with errors from
340 * the record layer or the handshake layer.
342 * Returns: 1 if the error code is fatal, for positive @error values,
343 * 0 is returned. For unknown @error values, -1 is returned.
346 gnutls_error_is_fatal (int error)
348 int ret = 1;
349 const gnutls_error_entry *p;
351 /* Input sanitzation. Positive values are not errors at all, and
352 definitely not fatal. */
353 if (error > 0)
354 return 0;
356 for (p = error_algorithms; p->desc != NULL; p++)
358 if (p->number == error)
360 ret = p->fatal;
361 break;
365 return ret;
369 * gnutls_perror:
370 * @error: is a GnuTLS error code, a negative value
372 * This function is like perror(). The only difference is that it
373 * accepts an error number returned by a gnutls function.
375 void
376 gnutls_perror (int error)
378 fprintf (stderr, "GnuTLS error: %s\n", gnutls_strerror (error));
383 * gnutls_strerror:
384 * @error: is a GnuTLS error code, a negative value
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.
390 * Error codes are always a negative value.
392 * Returns: A string explaining the GnuTLS error message.
394 const char *
395 gnutls_strerror (int error)
397 const char *ret = NULL;
398 const gnutls_error_entry *p;
400 for (p = error_algorithms; p->desc != NULL; p++)
402 if (p->number == error)
404 ret = p->desc;
405 break;
409 /* avoid prefix */
410 if (ret == NULL)
411 return _("(unknown error code)");
413 return _(ret);
417 * gnutls_strerror_name:
418 * @error: is an error returned by a gnutls function.
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".
424 * Returns: A string corresponding to the symbol name of the error
425 * code.
427 * Since: 2.6.0
429 const char *
430 gnutls_strerror_name (int error)
432 const char *ret = NULL;
433 const gnutls_error_entry *p;
435 for (p = error_algorithms; p->desc != NULL; p++)
437 if (p->number == error)
439 ret = p->_name;
440 break;
444 return ret;
448 _gnutls_asn2err (int asn_err)
450 switch (asn_err)
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;
485 void
486 _gnutls_mpi_log (const char *prefix, bigint_t a)
488 size_t binlen = 0;
489 void *binbuf;
490 size_t hexlen;
491 char *hexbuf;
492 int res;
494 res = _gnutls_mpi_print (a, NULL, &binlen);
495 if (res != 0)
497 gnutls_assert ();
498 _gnutls_hard_log ("MPI: can't print value (%d/%d)\n", res,
499 (int) binlen);
500 return;
503 if (binlen > 1024 * 1024)
505 gnutls_assert ();
506 _gnutls_hard_log ("MPI: too large mpi (%d)\n", (int) binlen);
507 return;
510 binbuf = gnutls_malloc (binlen);
511 if (!binbuf)
513 gnutls_assert ();
514 _gnutls_hard_log ("MPI: out of memory (%d)\n", (int) binlen);
515 return;
518 res = _gnutls_mpi_print (a, binbuf, &binlen);
519 if (res != 0)
521 gnutls_assert ();
522 _gnutls_hard_log ("MPI: can't print value (%d/%d)\n", res,
523 (int) binlen);
524 gnutls_free (binbuf);
525 return;
528 hexlen = 2 * binlen + 1;
529 hexbuf = gnutls_malloc (hexlen);
531 if (!hexbuf)
533 gnutls_assert ();
534 _gnutls_hard_log ("MPI: out of memory (hex %d)\n", (int) hexlen);
535 gnutls_free (binbuf);
536 return;
539 _gnutls_bin2hex (binbuf, binlen, hexbuf, hexlen, NULL);
541 _gnutls_hard_log ("MPI: length: %d\n\t%s%s\n", (int) binlen, prefix,
542 hexbuf);
544 gnutls_free (hexbuf);
545 gnutls_free (binbuf);
548 /* this function will output a message using the
549 * caller provided function
551 void
552 _gnutls_log (int level, const char *fmt, ...)
554 va_list args;
555 char *str;
556 int ret;
558 if (_gnutls_log_func == NULL)
559 return;
561 va_start (args, fmt);
562 ret = vasprintf (&str, fmt, args);
563 va_end (args);
565 if (ret >= 0)
567 _gnutls_log_func (level, str);
568 free (str);
572 #ifndef DEBUG
573 #ifndef C99_MACROS
575 /* Without C99 macros these functions have to
576 * be called. This may affect performance.
578 void
579 _gnutls_null_log (void *x, ...)
581 return;
584 #endif /* C99_MACROS */
585 #endif /* DEBUG */