documented fix
[gnutls.git] / lib / gnutls_errors.c
blob2a14f74a13acb3aa358f097a5fc22308a57dd0fd
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),
96 ERROR_ENTRY (N_("A heartbeat pong message was received."),
97 GNUTLS_E_HEARTBEAT_PONG_RECEIVED, 0),
98 ERROR_ENTRY (N_("A heartbeat ping message was received."),
99 GNUTLS_E_HEARTBEAT_PING_RECEIVED, 0),
100 ERROR_ENTRY (N_("There is already a crypto algorithm with lower priority."),
101 GNUTLS_E_CRYPTO_ALREADY_REGISTERED, 1),
103 ERROR_ENTRY (N_("No temporary RSA parameters were found."),
104 GNUTLS_E_NO_TEMPORARY_RSA_PARAMS, 1),
105 ERROR_ENTRY (N_("No temporary DH parameters were found."),
106 GNUTLS_E_NO_TEMPORARY_DH_PARAMS, 1),
107 ERROR_ENTRY (N_("An unexpected TLS handshake packet was received."),
108 GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET, 1),
109 ERROR_ENTRY (N_("The scanning of a large integer has failed."),
110 GNUTLS_E_MPI_SCAN_FAILED, 1),
111 ERROR_ENTRY (N_("Could not export a large integer."),
112 GNUTLS_E_MPI_PRINT_FAILED, 1),
113 ERROR_ENTRY (N_("Decryption has failed."), GNUTLS_E_DECRYPTION_FAILED, 1),
114 ERROR_ENTRY (N_("Encryption has failed."), GNUTLS_E_ENCRYPTION_FAILED, 1),
115 ERROR_ENTRY (N_("Public key decryption has failed."),
116 GNUTLS_E_PK_DECRYPTION_FAILED, 1),
117 ERROR_ENTRY (N_("Public key encryption has failed."),
118 GNUTLS_E_PK_ENCRYPTION_FAILED, 1),
119 ERROR_ENTRY (N_("Public key signing has failed."), GNUTLS_E_PK_SIGN_FAILED,
121 ERROR_ENTRY (N_("Public key signature verification has failed."),
122 GNUTLS_E_PK_SIG_VERIFY_FAILED, 1),
123 ERROR_ENTRY (N_("Decompression of the TLS record packet has failed."),
124 GNUTLS_E_DECOMPRESSION_FAILED, 1),
125 ERROR_ENTRY (N_("Compression of the TLS record packet has failed."),
126 GNUTLS_E_COMPRESSION_FAILED, 1),
128 ERROR_ENTRY (N_("Internal error in memory allocation."),
129 GNUTLS_E_MEMORY_ERROR, 1),
130 ERROR_ENTRY (N_("An unimplemented or disabled feature has been requested."),
131 GNUTLS_E_UNIMPLEMENTED_FEATURE, 1),
132 ERROR_ENTRY (N_("Insufficient credentials for that request."),
133 GNUTLS_E_INSUFFICIENT_CREDENTIALS, 1),
134 ERROR_ENTRY (N_("Error in password file."), GNUTLS_E_SRP_PWD_ERROR, 1),
135 ERROR_ENTRY (N_("Wrong padding in PKCS1 packet."), GNUTLS_E_PKCS1_WRONG_PAD,
137 ERROR_ENTRY (N_("The requested session has expired."), GNUTLS_E_EXPIRED, 1),
138 ERROR_ENTRY (N_("Hashing has failed."), GNUTLS_E_HASH_FAILED, 1),
139 ERROR_ENTRY (N_("Base64 decoding error."), GNUTLS_E_BASE64_DECODING_ERROR,
141 ERROR_ENTRY (N_("Base64 unexpected header error."),
142 GNUTLS_E_BASE64_UNEXPECTED_HEADER_ERROR,
144 ERROR_ENTRY (N_("Base64 encoding error."), GNUTLS_E_BASE64_ENCODING_ERROR,
146 ERROR_ENTRY (N_("Parsing error in password file."),
147 GNUTLS_E_SRP_PWD_PARSING_ERROR, 1),
148 ERROR_ENTRY (N_("The requested data were not available."),
149 GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE, 1),
150 ERROR_ENTRY (N_("Error in the pull function."), GNUTLS_E_PULL_ERROR, 1),
151 ERROR_ENTRY (N_("Error in the push function."), GNUTLS_E_PUSH_ERROR, 1),
152 ERROR_ENTRY (N_
153 ("The upper limit of record packet sequence numbers has been reached. Wow!"),
154 GNUTLS_E_RECORD_LIMIT_REACHED, 1),
155 ERROR_ENTRY (N_("Error in the certificate."), GNUTLS_E_CERTIFICATE_ERROR,
157 ERROR_ENTRY (N_("Unknown Subject Alternative name in X.509 certificate."),
158 GNUTLS_E_X509_UNKNOWN_SAN, 1),
160 ERROR_ENTRY (N_("Unsupported critical extension in X.509 certificate."),
161 GNUTLS_E_X509_UNSUPPORTED_CRITICAL_EXTENSION, 1),
162 ERROR_ENTRY (N_("Unsupported extension in X.509 certificate."),
163 GNUTLS_E_X509_UNSUPPORTED_EXTENSION, 1),
164 ERROR_ENTRY (N_("Key usage violation in certificate has been detected."),
165 GNUTLS_E_KEY_USAGE_VIOLATION, 1),
166 ERROR_ENTRY (N_("Resource temporarily unavailable, try again."),
167 GNUTLS_E_AGAIN, 0),
168 ERROR_ENTRY (N_("The transmitted packet is too large (EMSGSIZE)."),
169 GNUTLS_E_LARGE_PACKET, 0),
170 ERROR_ENTRY (N_("Function was interrupted."), GNUTLS_E_INTERRUPTED, 0),
171 ERROR_ENTRY (N_("Rehandshake was requested by the peer."),
172 GNUTLS_E_REHANDSHAKE, 0),
173 ERROR_ENTRY (N_
174 ("TLS Application data were received, while expecting handshake data."),
175 GNUTLS_E_GOT_APPLICATION_DATA, 1),
176 ERROR_ENTRY (N_("Error in Database backend."), GNUTLS_E_DB_ERROR, 1),
177 ERROR_ENTRY (N_("The certificate type is not supported."),
178 GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE, 1),
179 ERROR_ENTRY (N_("The given memory buffer is too short to hold parameters."),
180 GNUTLS_E_SHORT_MEMORY_BUFFER, 1),
181 ERROR_ENTRY (N_("The request is invalid."), GNUTLS_E_INVALID_REQUEST, 1),
182 ERROR_ENTRY (N_("The cookie was bad."), GNUTLS_E_BAD_COOKIE, 1),
183 ERROR_ENTRY (N_("An illegal parameter has been received."),
184 GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER, 1),
185 ERROR_ENTRY (N_("An illegal parameter was found."),
186 GNUTLS_E_ILLEGAL_PARAMETER, 1),
187 ERROR_ENTRY (N_("Error while reading file."), GNUTLS_E_FILE_ERROR, 1),
189 ERROR_ENTRY (N_("ASN1 parser: Element was not found."),
190 GNUTLS_E_ASN1_ELEMENT_NOT_FOUND, 1),
191 ERROR_ENTRY (N_("ASN1 parser: Identifier was not found"),
192 GNUTLS_E_ASN1_IDENTIFIER_NOT_FOUND, 1),
193 ERROR_ENTRY (N_("ASN1 parser: Error in DER parsing."),
194 GNUTLS_E_ASN1_DER_ERROR, 1),
195 ERROR_ENTRY (N_("ASN1 parser: Value was not found."),
196 GNUTLS_E_ASN1_VALUE_NOT_FOUND, 1),
197 ERROR_ENTRY (N_("ASN1 parser: Generic parsing error."),
198 GNUTLS_E_ASN1_GENERIC_ERROR, 1),
199 ERROR_ENTRY (N_("ASN1 parser: Value is not valid."),
200 GNUTLS_E_ASN1_VALUE_NOT_VALID, 1),
201 ERROR_ENTRY (N_("ASN1 parser: Error in TAG."), GNUTLS_E_ASN1_TAG_ERROR, 1),
202 ERROR_ENTRY (N_("ASN1 parser: error in implicit tag"),
203 GNUTLS_E_ASN1_TAG_IMPLICIT, 1),
204 ERROR_ENTRY (N_("ASN1 parser: Error in type 'ANY'."),
205 GNUTLS_E_ASN1_TYPE_ANY_ERROR, 1),
206 ERROR_ENTRY (N_("ASN1 parser: Syntax error."), GNUTLS_E_ASN1_SYNTAX_ERROR,
208 ERROR_ENTRY (N_("ASN1 parser: Overflow in DER parsing."),
209 GNUTLS_E_ASN1_DER_OVERFLOW, 1),
211 ERROR_ENTRY (N_("Too many empty record packets have been received."),
212 GNUTLS_E_TOO_MANY_EMPTY_PACKETS, 1),
213 ERROR_ENTRY (N_("Too many handshake packets have been received."),
214 GNUTLS_E_TOO_MANY_HANDSHAKE_PACKETS, 1),
215 ERROR_ENTRY (N_("The crypto library version is too old."),
216 GNUTLS_E_INCOMPATIBLE_GCRYPT_LIBRARY, 1),
218 ERROR_ENTRY (N_("The tasn1 library version is too old."),
219 GNUTLS_E_INCOMPATIBLE_LIBTASN1_LIBRARY, 1),
220 ERROR_ENTRY (N_("The OpenPGP User ID is revoked."),
221 GNUTLS_E_OPENPGP_UID_REVOKED, 1),
222 ERROR_ENTRY (N_("The OpenPGP key has not a preferred key set."),
223 GNUTLS_E_OPENPGP_PREFERRED_KEY_ERROR, 1),
224 ERROR_ENTRY (N_("Error loading the keyring."),
225 GNUTLS_E_OPENPGP_KEYRING_ERROR, 1),
226 ERROR_ENTRY (N_("The initialization of crypto backend has failed."),
227 GNUTLS_E_CRYPTO_INIT_FAILED, 1),
228 ERROR_ENTRY (N_("No supported compression algorithms have been found."),
229 GNUTLS_E_NO_COMPRESSION_ALGORITHMS, 1),
230 ERROR_ENTRY (N_("No supported cipher suites have been found."),
231 GNUTLS_E_NO_CIPHER_SUITES, 1),
232 ERROR_ENTRY (N_("Could not get OpenPGP key."),
233 GNUTLS_E_OPENPGP_GETKEY_FAILED, 1),
234 ERROR_ENTRY (N_("Could not find OpenPGP subkey."),
235 GNUTLS_E_OPENPGP_SUBKEY_ERROR, 1),
236 ERROR_ENTRY (N_("Safe renegotiation failed."),
237 GNUTLS_E_SAFE_RENEGOTIATION_FAILED, 1),
238 ERROR_ENTRY (N_("Unsafe renegotiation denied."),
239 GNUTLS_E_UNSAFE_RENEGOTIATION_DENIED, 1),
241 ERROR_ENTRY (N_("The SRP username supplied is illegal."),
242 GNUTLS_E_ILLEGAL_SRP_USERNAME, 1),
243 ERROR_ENTRY (N_("The SRP username supplied is unknown."),
244 GNUTLS_E_UNKNOWN_SRP_USERNAME, 1),
246 ERROR_ENTRY (N_("The OpenPGP fingerprint is not supported."),
247 GNUTLS_E_OPENPGP_FINGERPRINT_UNSUPPORTED, 1),
248 ERROR_ENTRY (N_("The signature algorithm is not supported."),
249 GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM, 1),
250 ERROR_ENTRY (N_("The certificate has unsupported attributes."),
251 GNUTLS_E_X509_UNSUPPORTED_ATTRIBUTE, 1),
252 ERROR_ENTRY (N_("The OID is not supported."), GNUTLS_E_X509_UNSUPPORTED_OID,
254 ERROR_ENTRY (N_("The hash algorithm is unknown."),
255 GNUTLS_E_UNKNOWN_HASH_ALGORITHM, 1),
256 ERROR_ENTRY (N_("The PKCS structure's content type is unknown."),
257 GNUTLS_E_UNKNOWN_PKCS_CONTENT_TYPE, 1),
258 ERROR_ENTRY (N_("The PKCS structure's bag type is unknown."),
259 GNUTLS_E_UNKNOWN_PKCS_BAG_TYPE, 1),
260 ERROR_ENTRY (N_("The given password contains invalid characters."),
261 GNUTLS_E_INVALID_PASSWORD, 1),
262 ERROR_ENTRY (N_("The Message Authentication Code verification failed."),
263 GNUTLS_E_MAC_VERIFY_FAILED, 1),
264 ERROR_ENTRY (N_("Some constraint limits were reached."),
265 GNUTLS_E_CONSTRAINT_ERROR, 1),
266 ERROR_ENTRY (N_("Failed to acquire random data."), GNUTLS_E_RANDOM_FAILED,
269 ERROR_ENTRY (N_("Received a TLS/IA Intermediate Phase Finished message"),
270 GNUTLS_E_WARNING_IA_IPHF_RECEIVED, 0),
271 ERROR_ENTRY (N_("Received a TLS/IA Final Phase Finished message"),
272 GNUTLS_E_WARNING_IA_FPHF_RECEIVED, 0),
273 ERROR_ENTRY (N_("Verifying TLS/IA phase checksum failed"),
274 GNUTLS_E_IA_VERIFY_FAILED, 1),
276 ERROR_ENTRY (N_("The specified algorithm or protocol is unknown."),
277 GNUTLS_E_UNKNOWN_ALGORITHM, 1),
279 ERROR_ENTRY (N_("The handshake data size is too large."),
280 GNUTLS_E_HANDSHAKE_TOO_LARGE, 1),
282 ERROR_ENTRY (N_("Error opening /dev/crypto"),
283 GNUTLS_E_CRYPTODEV_DEVICE_ERROR, 1),
285 ERROR_ENTRY (N_("Error interfacing with /dev/crypto"),
286 GNUTLS_E_CRYPTODEV_IOCTL_ERROR, 1),
287 ERROR_ENTRY (N_("Peer has terminated the connection"),
288 GNUTLS_E_SESSION_EOF, 1),
289 ERROR_ENTRY (N_("Channel binding data not available"),
290 GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE, 1),
292 ERROR_ENTRY (N_("TPM error."),
293 GNUTLS_E_TPM_ERROR, 1),
294 ERROR_ENTRY (N_("TPM is not initialized."),
295 GNUTLS_E_TPM_UNINITIALIZED, 1),
296 ERROR_ENTRY (N_("TPM key was not found in persistent storage."),
297 GNUTLS_E_TPM_KEY_NOT_FOUND, 1),
298 ERROR_ENTRY (N_("Cannot initialize a session with the TPM."),
299 GNUTLS_E_TPM_SESSION_ERROR, 1),
300 ERROR_ENTRY (N_("PKCS #11 error."),
301 GNUTLS_E_PKCS11_ERROR, 1),
302 ERROR_ENTRY (N_("PKCS #11 initialization error."),
303 GNUTLS_E_PKCS11_LOAD_ERROR, 1),
304 ERROR_ENTRY (N_("Error in parsing."),
305 GNUTLS_E_PARSING_ERROR, 1),
306 ERROR_ENTRY (N_("Error in provided PIN."),
307 GNUTLS_E_PKCS11_PIN_ERROR, 1),
308 ERROR_ENTRY (N_("Error in provided SRK password for TPM."),
309 GNUTLS_E_TPM_SRK_PASSWORD_ERROR, 1),
310 ERROR_ENTRY (N_("Error in provided password for key to be loaded in TPM."),
311 GNUTLS_E_TPM_KEY_PASSWORD_ERROR, 1),
312 ERROR_ENTRY (N_("PKCS #11 error in slot"),
313 GNUTLS_E_PKCS11_SLOT_ERROR, 1),
314 ERROR_ENTRY (N_("Thread locking error"),
315 GNUTLS_E_LOCKING_ERROR, 1),
316 ERROR_ENTRY (N_("PKCS #11 error in attribute"),
317 GNUTLS_E_PKCS11_ATTRIBUTE_ERROR, 1),
318 ERROR_ENTRY (N_("PKCS #11 error in device"),
319 GNUTLS_E_PKCS11_DEVICE_ERROR, 1),
320 ERROR_ENTRY (N_("PKCS #11 error in data"),
321 GNUTLS_E_PKCS11_DATA_ERROR, 1),
322 ERROR_ENTRY (N_("PKCS #11 unsupported feature"),
323 GNUTLS_E_PKCS11_UNSUPPORTED_FEATURE_ERROR, 1),
324 ERROR_ENTRY (N_("PKCS #11 error in key"),
325 GNUTLS_E_PKCS11_KEY_ERROR, 1),
326 ERROR_ENTRY (N_("PKCS #11 PIN expired"),
327 GNUTLS_E_PKCS11_PIN_EXPIRED, 1),
328 ERROR_ENTRY (N_("PKCS #11 PIN locked"),
329 GNUTLS_E_PKCS11_PIN_LOCKED, 1),
330 ERROR_ENTRY (N_("PKCS #11 error in session"),
331 GNUTLS_E_PKCS11_SESSION_ERROR, 1),
332 ERROR_ENTRY (N_("PKCS #11 error in signature"),
333 GNUTLS_E_PKCS11_SIGNATURE_ERROR, 1),
334 ERROR_ENTRY (N_("PKCS #11 error in token"),
335 GNUTLS_E_PKCS11_TOKEN_ERROR, 1),
336 ERROR_ENTRY (N_("PKCS #11 user error"),
337 GNUTLS_E_PKCS11_USER_ERROR, 1),
338 ERROR_ENTRY (N_("The operation timed out"),
339 GNUTLS_E_TIMEDOUT, 1),
340 ERROR_ENTRY (N_("The operation was cancelled due to user error"),
341 GNUTLS_E_USER_ERROR, 1),
342 ERROR_ENTRY (N_("No supported ECC curves were found"),
343 GNUTLS_E_ECC_NO_SUPPORTED_CURVES, 1),
344 ERROR_ENTRY (N_("The curve is unsupported"),
345 GNUTLS_E_ECC_UNSUPPORTED_CURVE, 1),
346 ERROR_ENTRY (N_("The requested PKCS #11 object is not available"),
347 GNUTLS_E_PKCS11_REQUESTED_OBJECT_NOT_AVAILBLE, 1),
348 ERROR_ENTRY (N_("The provided X.509 certificate list is not sorted (in subject to issuer order)"),
349 GNUTLS_E_CERTIFICATE_LIST_UNSORTED, 1),
350 {NULL, NULL, 0, 0}
354 * gnutls_error_is_fatal:
355 * @error: is a GnuTLS error code, a negative error code
357 * If a GnuTLS function returns a negative error code you may feed that
358 * value to this function to see if the error condition is fatal.
359 * Note that you may also want to check the error code manually, since some
360 * non-fatal errors to the protocol (such as a warning alert or
361 * a rehandshake request) may be fatal for your program.
363 * This function is only useful if you are dealing with errors from
364 * the record layer or the handshake layer.
366 * Returns: 1 if the error code is fatal, for positive @error values,
367 * 0 is returned. For unknown @error values, -1 is returned.
370 gnutls_error_is_fatal (int error)
372 int ret = 1;
373 const gnutls_error_entry *p;
375 /* Input sanitzation. Positive values are not errors at all, and
376 definitely not fatal. */
377 if (error > 0)
378 return 0;
380 for (p = error_algorithms; p->desc != NULL; p++)
382 if (p->number == error)
384 ret = p->fatal;
385 break;
389 return ret;
393 * gnutls_perror:
394 * @error: is a GnuTLS error code, a negative error code
396 * This function is like perror(). The only difference is that it
397 * accepts an error number returned by a gnutls function.
399 void
400 gnutls_perror (int error)
402 fprintf (stderr, "GnuTLS error: %s\n", gnutls_strerror (error));
407 * gnutls_strerror:
408 * @error: is a GnuTLS error code, a negative error code
410 * This function is similar to strerror. The difference is that it
411 * accepts an error number returned by a gnutls function; In case of
412 * an unknown error a descriptive string is sent instead of %NULL.
414 * Error codes are always a negative error code.
416 * Returns: A string explaining the GnuTLS error message.
418 const char *
419 gnutls_strerror (int error)
421 const char *ret = NULL;
422 const gnutls_error_entry *p;
424 for (p = error_algorithms; p->desc != NULL; p++)
426 if (p->number == error)
428 ret = p->desc;
429 break;
433 /* avoid prefix */
434 if (ret == NULL)
435 return _("(unknown error code)");
437 return _(ret);
441 * gnutls_strerror_name:
442 * @error: is an error returned by a gnutls function.
444 * Return the GnuTLS error code define as a string. For example,
445 * gnutls_strerror_name (GNUTLS_E_DH_PRIME_UNACCEPTABLE) will return
446 * the string "GNUTLS_E_DH_PRIME_UNACCEPTABLE".
448 * Returns: A string corresponding to the symbol name of the error
449 * code.
451 * Since: 2.6.0
453 const char *
454 gnutls_strerror_name (int error)
456 const char *ret = NULL;
457 const gnutls_error_entry *p;
459 for (p = error_algorithms; p->desc != NULL; p++)
461 if (p->number == error)
463 ret = p->_name;
464 break;
468 return ret;
472 _gnutls_asn2err (int asn_err)
474 switch (asn_err)
476 case ASN1_FILE_NOT_FOUND:
477 return GNUTLS_E_FILE_ERROR;
478 case ASN1_ELEMENT_NOT_FOUND:
479 return GNUTLS_E_ASN1_ELEMENT_NOT_FOUND;
480 case ASN1_IDENTIFIER_NOT_FOUND:
481 return GNUTLS_E_ASN1_IDENTIFIER_NOT_FOUND;
482 case ASN1_DER_ERROR:
483 return GNUTLS_E_ASN1_DER_ERROR;
484 case ASN1_VALUE_NOT_FOUND:
485 return GNUTLS_E_ASN1_VALUE_NOT_FOUND;
486 case ASN1_GENERIC_ERROR:
487 return GNUTLS_E_ASN1_GENERIC_ERROR;
488 case ASN1_VALUE_NOT_VALID:
489 return GNUTLS_E_ASN1_VALUE_NOT_VALID;
490 case ASN1_TAG_ERROR:
491 return GNUTLS_E_ASN1_TAG_ERROR;
492 case ASN1_TAG_IMPLICIT:
493 return GNUTLS_E_ASN1_TAG_IMPLICIT;
494 case ASN1_ERROR_TYPE_ANY:
495 return GNUTLS_E_ASN1_TYPE_ANY_ERROR;
496 case ASN1_SYNTAX_ERROR:
497 return GNUTLS_E_ASN1_SYNTAX_ERROR;
498 case ASN1_MEM_ERROR:
499 return GNUTLS_E_SHORT_MEMORY_BUFFER;
500 case ASN1_MEM_ALLOC_ERROR:
501 return GNUTLS_E_MEMORY_ERROR;
502 case ASN1_DER_OVERFLOW:
503 return GNUTLS_E_ASN1_DER_OVERFLOW;
504 default:
505 return GNUTLS_E_ASN1_GENERIC_ERROR;
509 void
510 _gnutls_mpi_log (const char *prefix, bigint_t a)
512 size_t binlen = 0;
513 void *binbuf;
514 size_t hexlen;
515 char *hexbuf;
516 int res;
518 if (_gnutls_log_level < 2) return;
520 res = _gnutls_mpi_print (a, NULL, &binlen);
521 if (res < 0 && res != GNUTLS_E_SHORT_MEMORY_BUFFER)
523 gnutls_assert ();
524 _gnutls_hard_log ("MPI: %s can't print value (%d/%d)\n", prefix, res,
525 (int) binlen);
526 return;
529 if (binlen > 1024 * 1024)
531 gnutls_assert ();
532 _gnutls_hard_log ("MPI: %s too large mpi (%d)\n", prefix, (int) binlen);
533 return;
536 binbuf = gnutls_malloc (binlen);
537 if (!binbuf)
539 gnutls_assert ();
540 _gnutls_hard_log ("MPI: %s out of memory (%d)\n", prefix, (int) binlen);
541 return;
544 res = _gnutls_mpi_print (a, binbuf, &binlen);
545 if (res != 0)
547 gnutls_assert ();
548 _gnutls_hard_log ("MPI: %s can't print value (%d/%d)\n", prefix, res,
549 (int) binlen);
550 gnutls_free (binbuf);
551 return;
554 hexlen = 2 * binlen + 1;
555 hexbuf = gnutls_malloc (hexlen);
557 if (!hexbuf)
559 gnutls_assert ();
560 _gnutls_hard_log ("MPI: %s out of memory (hex %d)\n", prefix, (int) hexlen);
561 gnutls_free (binbuf);
562 return;
565 _gnutls_bin2hex (binbuf, binlen, hexbuf, hexlen, NULL);
567 _gnutls_hard_log ("MPI: length: %d\n\t%s%s\n", (int) binlen, prefix,
568 hexbuf);
570 gnutls_free (hexbuf);
571 gnutls_free (binbuf);
574 /* this function will output a message using the
575 * caller provided function
577 void
578 _gnutls_log (int level, const char *fmt, ...)
580 va_list args;
581 char *str;
582 int ret;
584 if (_gnutls_log_func == NULL)
585 return;
587 va_start (args, fmt);
588 ret = vasprintf (&str, fmt, args);
589 va_end (args);
591 if (ret >= 0)
593 _gnutls_log_func (level, str);
594 free (str);
598 void
599 _gnutls_audit_log (gnutls_session_t session, const char *fmt, ...)
601 va_list args;
602 char *str;
603 int ret;
605 if (_gnutls_audit_log_func == NULL && _gnutls_log_func == NULL)
606 return;
608 va_start (args, fmt);
609 ret = vasprintf (&str, fmt, args);
610 va_end (args);
612 if (ret >= 0)
614 if (_gnutls_audit_log_func)
615 _gnutls_audit_log_func (session, str);
616 else
617 _gnutls_log_func(1, str);
618 free (str);
622 #ifndef DEBUG
623 #ifndef C99_MACROS
625 /* Without C99 macros these functions have to
626 * be called. This may affect performance.
628 void
629 _gnutls_null_log (void *x, ...)
631 return;
634 #endif /* C99_MACROS */
635 #endif /* DEBUG */