certtool is able to set certificate policies via a template
[gnutls.git] / lib / gnutls_errors.c
blob56ff58561283c5b132cf70323e4eb219f93a2237
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 ERROR_ENTRY (N_("The OCSP response is invalid"),
351 GNUTLS_E_OCSP_RESPONSE_ERROR, 1),
352 ERROR_ENTRY (N_("There is no certificate status (OCSP)."),
353 GNUTLS_E_NO_CERTIFICATE_STATUS, 1),
354 {NULL, NULL, 0, 0}
358 * gnutls_error_is_fatal:
359 * @error: is a GnuTLS error code, a negative error code
361 * If a GnuTLS function returns a negative error code you may feed that
362 * value to this function to see if the error condition is fatal.
363 * Note that you may also want to check the error code manually, since some
364 * non-fatal errors to the protocol (such as a warning alert or
365 * a rehandshake request) may be fatal for your program.
367 * This function is only useful if you are dealing with errors from
368 * the record layer or the handshake layer.
370 * Returns: 1 if the error code is fatal, for positive @error values,
371 * 0 is returned. For unknown @error values, -1 is returned.
374 gnutls_error_is_fatal (int error)
376 int ret = 1;
377 const gnutls_error_entry *p;
379 /* Input sanitzation. Positive values are not errors at all, and
380 definitely not fatal. */
381 if (error > 0)
382 return 0;
384 for (p = error_algorithms; p->desc != NULL; p++)
386 if (p->number == error)
388 ret = p->fatal;
389 break;
393 return ret;
397 * gnutls_perror:
398 * @error: is a GnuTLS error code, a negative error code
400 * This function is like perror(). The only difference is that it
401 * accepts an error number returned by a gnutls function.
403 void
404 gnutls_perror (int error)
406 fprintf (stderr, "GnuTLS error: %s\n", gnutls_strerror (error));
411 * gnutls_strerror:
412 * @error: is a GnuTLS error code, a negative error code
414 * This function is similar to strerror. The difference is that it
415 * accepts an error number returned by a gnutls function; In case of
416 * an unknown error a descriptive string is sent instead of %NULL.
418 * Error codes are always a negative error code.
420 * Returns: A string explaining the GnuTLS error message.
422 const char *
423 gnutls_strerror (int error)
425 const char *ret = NULL;
426 const gnutls_error_entry *p;
428 for (p = error_algorithms; p->desc != NULL; p++)
430 if (p->number == error)
432 ret = p->desc;
433 break;
437 /* avoid prefix */
438 if (ret == NULL)
439 return _("(unknown error code)");
441 return _(ret);
445 * gnutls_strerror_name:
446 * @error: is an error returned by a gnutls function.
448 * Return the GnuTLS error code define as a string. For example,
449 * gnutls_strerror_name (GNUTLS_E_DH_PRIME_UNACCEPTABLE) will return
450 * the string "GNUTLS_E_DH_PRIME_UNACCEPTABLE".
452 * Returns: A string corresponding to the symbol name of the error
453 * code.
455 * Since: 2.6.0
457 const char *
458 gnutls_strerror_name (int error)
460 const char *ret = NULL;
461 const gnutls_error_entry *p;
463 for (p = error_algorithms; p->desc != NULL; p++)
465 if (p->number == error)
467 ret = p->_name;
468 break;
472 return ret;
476 _gnutls_asn2err (int asn_err)
478 switch (asn_err)
480 case ASN1_FILE_NOT_FOUND:
481 return GNUTLS_E_FILE_ERROR;
482 case ASN1_ELEMENT_NOT_FOUND:
483 return GNUTLS_E_ASN1_ELEMENT_NOT_FOUND;
484 case ASN1_IDENTIFIER_NOT_FOUND:
485 return GNUTLS_E_ASN1_IDENTIFIER_NOT_FOUND;
486 case ASN1_DER_ERROR:
487 return GNUTLS_E_ASN1_DER_ERROR;
488 case ASN1_VALUE_NOT_FOUND:
489 return GNUTLS_E_ASN1_VALUE_NOT_FOUND;
490 case ASN1_GENERIC_ERROR:
491 return GNUTLS_E_ASN1_GENERIC_ERROR;
492 case ASN1_VALUE_NOT_VALID:
493 return GNUTLS_E_ASN1_VALUE_NOT_VALID;
494 case ASN1_TAG_ERROR:
495 return GNUTLS_E_ASN1_TAG_ERROR;
496 case ASN1_TAG_IMPLICIT:
497 return GNUTLS_E_ASN1_TAG_IMPLICIT;
498 case ASN1_ERROR_TYPE_ANY:
499 return GNUTLS_E_ASN1_TYPE_ANY_ERROR;
500 case ASN1_SYNTAX_ERROR:
501 return GNUTLS_E_ASN1_SYNTAX_ERROR;
502 case ASN1_MEM_ERROR:
503 return GNUTLS_E_SHORT_MEMORY_BUFFER;
504 case ASN1_MEM_ALLOC_ERROR:
505 return GNUTLS_E_MEMORY_ERROR;
506 case ASN1_DER_OVERFLOW:
507 return GNUTLS_E_ASN1_DER_OVERFLOW;
508 default:
509 return GNUTLS_E_ASN1_GENERIC_ERROR;
513 void
514 _gnutls_mpi_log (const char *prefix, bigint_t a)
516 size_t binlen = 0;
517 void *binbuf;
518 size_t hexlen;
519 char *hexbuf;
520 int res;
522 if (_gnutls_log_level < 2) return;
524 res = _gnutls_mpi_print (a, NULL, &binlen);
525 if (res < 0 && res != GNUTLS_E_SHORT_MEMORY_BUFFER)
527 gnutls_assert ();
528 _gnutls_hard_log ("MPI: %s can't print value (%d/%d)\n", prefix, res,
529 (int) binlen);
530 return;
533 if (binlen > 1024 * 1024)
535 gnutls_assert ();
536 _gnutls_hard_log ("MPI: %s too large mpi (%d)\n", prefix, (int) binlen);
537 return;
540 binbuf = gnutls_malloc (binlen);
541 if (!binbuf)
543 gnutls_assert ();
544 _gnutls_hard_log ("MPI: %s out of memory (%d)\n", prefix, (int) binlen);
545 return;
548 res = _gnutls_mpi_print (a, binbuf, &binlen);
549 if (res != 0)
551 gnutls_assert ();
552 _gnutls_hard_log ("MPI: %s can't print value (%d/%d)\n", prefix, res,
553 (int) binlen);
554 gnutls_free (binbuf);
555 return;
558 hexlen = 2 * binlen + 1;
559 hexbuf = gnutls_malloc (hexlen);
561 if (!hexbuf)
563 gnutls_assert ();
564 _gnutls_hard_log ("MPI: %s out of memory (hex %d)\n", prefix, (int) hexlen);
565 gnutls_free (binbuf);
566 return;
569 _gnutls_bin2hex (binbuf, binlen, hexbuf, hexlen, NULL);
571 _gnutls_hard_log ("MPI: length: %d\n\t%s%s\n", (int) binlen, prefix,
572 hexbuf);
574 gnutls_free (hexbuf);
575 gnutls_free (binbuf);
578 /* this function will output a message using the
579 * caller provided function
581 void
582 _gnutls_log (int level, const char *fmt, ...)
584 va_list args;
585 char *str;
586 int ret;
588 if (_gnutls_log_func == NULL)
589 return;
591 va_start (args, fmt);
592 ret = vasprintf (&str, fmt, args);
593 va_end (args);
595 if (ret >= 0)
597 _gnutls_log_func (level, str);
598 free (str);
602 void
603 _gnutls_audit_log (gnutls_session_t session, const char *fmt, ...)
605 va_list args;
606 char *str;
607 int ret;
609 if (_gnutls_audit_log_func == NULL && _gnutls_log_func == NULL)
610 return;
612 va_start (args, fmt);
613 ret = vasprintf (&str, fmt, args);
614 va_end (args);
616 if (ret >= 0)
618 if (_gnutls_audit_log_func)
619 _gnutls_audit_log_func (session, str);
620 else
621 _gnutls_log_func(1, str);
622 free (str);
626 #ifndef DEBUG
627 #ifndef C99_MACROS
629 /* Without C99 macros these functions have to
630 * be called. This may affect performance.
632 void
633 _gnutls_null_log (void *x, ...)
635 return;
638 #endif /* C99_MACROS */
639 #endif /* DEBUG */