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