gnutls_errors.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation
00003  *
00004  * Author: Nikos Mavrogiannopoulos
00005  *
00006  * This file is part of GNUTLS.
00007  *
00008  * The GNUTLS library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation; either version 2.1 of
00011  * the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00021  * USA
00022  *
00023  */
00024 
00025 #include <gnutls_int.h>
00026 #include "gnutls_errors.h"
00027 #include <libtasn1.h>
00028 #ifdef STDC_HEADERS
00029 # include <stdarg.h>
00030 #endif
00031 
00032 /* I18n of error codes. */
00033 #define _(String) (String)
00034 #define N_(String) (String)
00035 
00036 extern LOG_FUNC MHD__gnutls_log_func;
00037 
00038 #define ERROR_ENTRY(desc, name, fatal) \
00039         { desc, #name, name, fatal}
00040 
00041 struct MHD_gnutls_error_entry
00042 {
00043   const char *desc;
00044   const char *_name;
00045   int number;
00046   int fatal;
00047 };
00048 typedef struct MHD_gnutls_error_entry MHD_gnutls_error_entry;
00049 
00050 static const MHD_gnutls_error_entry MHD_gtls_error_algorithms[] = {
00051   /* "Short Description", Error code define, critical (0,1) -- 1 in most cases */
00052   ERROR_ENTRY (N_("Success."), GNUTLS_E_SUCCESS, 0),
00053   ERROR_ENTRY (N_("Could not negotiate a supported cipher suite."),
00054                GNUTLS_E_UNKNOWN_CIPHER_SUITE, 1),
00055   ERROR_ENTRY (N_("The cipher type is unsupported."),
00056                GNUTLS_E_UNKNOWN_CIPHER_TYPE, 1),
00057   ERROR_ENTRY (N_("The certificate and the given key do not match."),
00058                GNUTLS_E_CERTIFICATE_KEY_MISMATCH, 1),
00059   ERROR_ENTRY (N_("Could not negotiate a supported compression method."),
00060                GNUTLS_E_UNKNOWN_COMPRESSION_ALGORITHM, 1),
00061   ERROR_ENTRY (N_("An unknown public key algorithm was encountered."),
00062                GNUTLS_E_UNKNOWN_PK_ALGORITHM, 1),
00063 
00064   ERROR_ENTRY (N_("An algorithm that is not enabled was negotiated."),
00065                GNUTLS_E_UNWANTED_ALGORITHM, 1),
00066   ERROR_ENTRY (N_("A large TLS record packet was received."),
00067                GNUTLS_E_LARGE_PACKET, 1),
00068   ERROR_ENTRY (N_("A record packet with illegal version was received."),
00069                GNUTLS_E_UNSUPPORTED_VERSION_PACKET, 1),
00070   ERROR_ENTRY (N_
00071                ("The Diffie Hellman prime sent by the server is not acceptable (not long enough)."),
00072                GNUTLS_E_DH_PRIME_UNACCEPTABLE, 1),
00073   ERROR_ENTRY (N_("A TLS packet with unexpected length was received."),
00074                GNUTLS_E_UNEXPECTED_PACKET_LENGTH, 1),
00075   ERROR_ENTRY (N_
00076                ("The specified session has been invalidated for some reason."),
00077                GNUTLS_E_INVALID_SESSION, 1),
00078 
00079   ERROR_ENTRY (N_("GnuTLS internal error."), GNUTLS_E_INTERNAL_ERROR, 1),
00080   ERROR_ENTRY (N_("An illegal TLS extension was received."),
00081                GNUTLS_E_RECEIVED_ILLEGAL_EXTENSION, 1),
00082   ERROR_ENTRY (N_("A TLS fatal alert has been received."),
00083                GNUTLS_E_FATAL_ALERT_RECEIVED, 1),
00084   ERROR_ENTRY (N_("An unexpected TLS packet was received."),
00085                GNUTLS_E_UNEXPECTED_PACKET, 1),
00086   ERROR_ENTRY (N_("A TLS warning alert has been received."),
00087                GNUTLS_E_WARNING_ALERT_RECEIVED, 0),
00088   ERROR_ENTRY (N_
00089                ("An error was encountered at the TLS Finished packet calculation."),
00090                GNUTLS_E_ERROR_IN_FINISHED_PACKET, 1),
00091   ERROR_ENTRY (N_("The peer did not send any certificate."),
00092                GNUTLS_E_NO_CERTIFICATE_FOUND, 1),
00093 
00094   ERROR_ENTRY (N_("No temporary RSA parameters were found."),
00095                GNUTLS_E_NO_TEMPORARY_RSA_PARAMS, 1),
00096   ERROR_ENTRY (N_("No temporary DH parameters were found."),
00097                GNUTLS_E_NO_TEMPORARY_DH_PARAMS, 1),
00098   ERROR_ENTRY (N_("An unexpected TLS handshake packet was received."),
00099                GNUTLS_E_UNEXPECTED_HANDSHAKE_PACKET, 1),
00100   ERROR_ENTRY (N_("The scanning of a large integer has failed."),
00101                GNUTLS_E_MPI_SCAN_FAILED, 1),
00102   ERROR_ENTRY (N_("Could not export a large integer."),
00103                GNUTLS_E_MPI_PRINT_FAILED, 1),
00104   ERROR_ENTRY (N_("Decryption has failed."), GNUTLS_E_DECRYPTION_FAILED, 1),
00105   ERROR_ENTRY (N_("Encryption has failed."), GNUTLS_E_ENCRYPTION_FAILED, 1),
00106   ERROR_ENTRY (N_("Public key decryption has failed."),
00107                GNUTLS_E_PK_DECRYPTION_FAILED, 1),
00108   ERROR_ENTRY (N_("Public key encryption has failed."),
00109                GNUTLS_E_PK_ENCRYPTION_FAILED, 1),
00110   ERROR_ENTRY (N_("Public key signing has failed."), GNUTLS_E_PK_SIGN_FAILED,
00111                1),
00112   ERROR_ENTRY (N_("Public key signature verification has failed."),
00113                GNUTLS_E_PK_SIG_VERIFY_FAILED, 1),
00114   ERROR_ENTRY (N_("Decompression of the TLS record packet has failed."),
00115                GNUTLS_E_DECOMPRESSION_FAILED, 1),
00116   ERROR_ENTRY (N_("Compression of the TLS record packet has failed."),
00117                GNUTLS_E_COMPRESSION_FAILED, 1),
00118 
00119   ERROR_ENTRY (N_("Internal error in memory allocation."),
00120                GNUTLS_E_MEMORY_ERROR, 1),
00121   ERROR_ENTRY (N_("An unimplemented or disabled feature has been requested."),
00122                GNUTLS_E_UNIMPLEMENTED_FEATURE, 1),
00123   ERROR_ENTRY (N_("Insufficient credentials for that request."),
00124                GNUTLS_E_INSUFFICIENT_CREDENTIALS, 1),
00125   ERROR_ENTRY (N_("Error in password file."), GNUTLS_E_SRP_PWD_ERROR, 1),
00126   ERROR_ENTRY (N_("Wrong padding in PKCS1 packet."), GNUTLS_E_PKCS1_WRONG_PAD,
00127                1),
00128   ERROR_ENTRY (N_("The requested session has expired."), GNUTLS_E_EXPIRED, 1),
00129   ERROR_ENTRY (N_("Hashing has failed."), GNUTLS_E_HASH_FAILED, 1),
00130   ERROR_ENTRY (N_("Base64 decoding error."), GNUTLS_E_BASE64_DECODING_ERROR,
00131                1),
00132   ERROR_ENTRY (N_("Base64 unexpected header error."),
00133                GNUTLS_E_BASE64_UNEXPECTED_HEADER_ERROR,
00134                1),
00135   ERROR_ENTRY (N_("Base64 encoding error."), GNUTLS_E_BASE64_ENCODING_ERROR,
00136                1),
00137   ERROR_ENTRY (N_("Parsing error in password file."),
00138                GNUTLS_E_SRP_PWD_PARSING_ERROR, 1),
00139   ERROR_ENTRY (N_("The requested data were not available."),
00140                GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE, 0),
00141   ERROR_ENTRY (N_("Error in the pull function."), GNUTLS_E_PULL_ERROR, 1),
00142   ERROR_ENTRY (N_("Error in the push function."), GNUTLS_E_PUSH_ERROR, 1),
00143   ERROR_ENTRY (N_
00144                ("The upper limit of record packet sequence numbers has been reached. Wow!"),
00145                GNUTLS_E_RECORD_LIMIT_REACHED, 1),
00146   ERROR_ENTRY (N_("Error in the certificate."), GNUTLS_E_CERTIFICATE_ERROR,
00147                1),
00148   ERROR_ENTRY (N_("Unknown Subject Alternative name in X.509 certificate."),
00149                GNUTLS_E_X509_UNKNOWN_SAN, 1),
00150 
00151   ERROR_ENTRY (N_("Unsupported critical extension in X.509 certificate."),
00152                GNUTLS_E_X509_UNSUPPORTED_CRITICAL_EXTENSION, 1),
00153   ERROR_ENTRY (N_("Key usage violation in certificate has been detected."),
00154                GNUTLS_E_KEY_USAGE_VIOLATION, 1),
00155   ERROR_ENTRY (N_("Function was interrupted."), GNUTLS_E_AGAIN, 0),
00156   ERROR_ENTRY (N_("Function was interrupted."), GNUTLS_E_INTERRUPTED, 0),
00157   ERROR_ENTRY (N_("Rehandshake was requested by the peer."),
00158                GNUTLS_E_REHANDSHAKE, 0),
00159   ERROR_ENTRY (N_
00160                ("TLS Application data were received, while expecting handshake data."),
00161                GNUTLS_E_GOT_APPLICATION_DATA, 1),
00162   ERROR_ENTRY (N_("Error in Database backend."), GNUTLS_E_DB_ERROR, 1),
00163   ERROR_ENTRY (N_("The certificate type is not supported."),
00164                GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE, 1),
00165   ERROR_ENTRY (N_("The given memory buffer is too short to hold parameters."),
00166                GNUTLS_E_SHORT_MEMORY_BUFFER, 1),
00167   ERROR_ENTRY (N_("The request is invalid."), GNUTLS_E_INVALID_REQUEST, 1),
00168   ERROR_ENTRY (N_("An illegal parameter has been received."),
00169                GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER, 1),
00170   ERROR_ENTRY (N_("Error while reading file."), GNUTLS_E_FILE_ERROR, 1),
00171 
00172   ERROR_ENTRY (N_("ASN1 parser: Element was not found."),
00173                GNUTLS_E_ASN1_ELEMENT_NOT_FOUND, 1),
00174   ERROR_ENTRY (N_("ASN1 parser: Identifier was not found"),
00175                GNUTLS_E_ASN1_IDENTIFIER_NOT_FOUND, 1),
00176   ERROR_ENTRY (N_("ASN1 parser: Error in DER parsing."),
00177                GNUTLS_E_ASN1_DER_ERROR, 1),
00178   ERROR_ENTRY (N_("ASN1 parser: Value was not found."),
00179                GNUTLS_E_ASN1_VALUE_NOT_FOUND, 1),
00180   ERROR_ENTRY (N_("ASN1 parser: Generic parsing error."),
00181                GNUTLS_E_ASN1_GENERIC_ERROR, 1),
00182   ERROR_ENTRY (N_("ASN1 parser: Value is not valid."),
00183                GNUTLS_E_ASN1_VALUE_NOT_VALID, 1),
00184   ERROR_ENTRY (N_("ASN1 parser: Error in TAG."), GNUTLS_E_ASN1_TAG_ERROR, 1),
00185   ERROR_ENTRY (N_("ASN1 parser: error in implicit tag"),
00186                GNUTLS_E_ASN1_TAG_IMPLICIT, 1),
00187   ERROR_ENTRY (N_("ASN1 parser: Error in type 'ANY'."),
00188                GNUTLS_E_ASN1_TYPE_ANY_ERROR, 1),
00189   ERROR_ENTRY (N_("ASN1 parser: Syntax error."), GNUTLS_E_ASN1_SYNTAX_ERROR,
00190                1),
00191   ERROR_ENTRY (N_("ASN1 parser: Overflow in DER parsing."),
00192                GNUTLS_E_ASN1_DER_OVERFLOW, 1),
00193 
00194   ERROR_ENTRY (N_("Too many empty record packets have been received."),
00195                GNUTLS_E_TOO_MANY_EMPTY_PACKETS, 1),
00196   ERROR_ENTRY (N_("The initialization of GnuTLS-extra has failed."),
00197                GNUTLS_E_INIT_LIBEXTRA, 1),
00198   ERROR_ENTRY (N_
00199                ("The GnuTLS library version does not match the GnuTLS-extra library version."),
00200                GNUTLS_E_LIBRARY_VERSION_MISMATCH, 1),
00201   ERROR_ENTRY (N_("The gcrypt library version is too old."),
00202                GNUTLS_E_INCOMPATIBLE_GCRYPT_LIBRARY, 1),
00203 
00204   ERROR_ENTRY (N_("The tasn1 library version is too old."),
00205                GNUTLS_E_INCOMPATIBLE_LIBTASN1_LIBRARY, 1),
00206 
00207   ERROR_ENTRY (N_("The initialization of LZO has failed."),
00208                GNUTLS_E_LZO_INIT_FAILED, 1),
00209   ERROR_ENTRY (N_("No supported compression algorithms have been found."),
00210                GNUTLS_E_NO_COMPRESSION_ALGORITHMS, 1),
00211   ERROR_ENTRY (N_("No supported cipher suites have been found."),
00212                GNUTLS_E_NO_CIPHER_SUITES, 1),
00213   ERROR_ENTRY (N_("The SRP username supplied is illegal."),
00214                GNUTLS_E_ILLEGAL_SRP_USERNAME, 1),
00215 
00216   ERROR_ENTRY (N_("The certificate has unsupported attributes."),
00217                GNUTLS_E_X509_UNSUPPORTED_ATTRIBUTE, 1),
00218   ERROR_ENTRY (N_("The OID is not supported."), GNUTLS_E_X509_UNSUPPORTED_OID,
00219                1),
00220   ERROR_ENTRY (N_("The hash algorithm is unknown."),
00221                GNUTLS_E_UNKNOWN_HASH_ALGORITHM, 1),
00222   ERROR_ENTRY (N_("The PKCS structure's content type is unknown."),
00223                GNUTLS_E_UNKNOWN_PKCS_CONTENT_TYPE, 1),
00224   ERROR_ENTRY (N_("The PKCS structure's bag type is unknown."),
00225                GNUTLS_E_UNKNOWN_PKCS_BAG_TYPE, 1),
00226   ERROR_ENTRY (N_("The given password contains invalid characters."),
00227                GNUTLS_E_INVALID_PASSWORD, 1),
00228   ERROR_ENTRY (N_("The Message Authentication Code verification failed."),
00229                GNUTLS_E_MAC_VERIFY_FAILED, 1),
00230   ERROR_ENTRY (N_("Some constraint limits were reached."),
00231                GNUTLS_E_CONSTRAINT_ERROR, 1),
00232   ERROR_ENTRY (N_("Failed to acquire random data."), GNUTLS_E_RANDOM_FAILED,
00233                1),
00234 
00235   ERROR_ENTRY (N_("Received a TLS/IA Intermediate Phase Finished message"),
00236                GNUTLS_E_WARNING_IA_IPHF_RECEIVED, 0),
00237   ERROR_ENTRY (N_("Received a TLS/IA Final Phase Finished message"),
00238                GNUTLS_E_WARNING_IA_FPHF_RECEIVED, 0),
00239   ERROR_ENTRY (N_("Verifying TLS/IA phase checksum failed"),
00240                GNUTLS_E_IA_VERIFY_FAILED, 1),
00241 
00242   ERROR_ENTRY (N_("The specified algorithm or protocol is unknown."),
00243                GNUTLS_E_UNKNOWN_ALGORITHM, 1),
00244 
00245   {NULL, NULL, 0, 0}
00246 };
00247 
00248 #define GNUTLS_ERROR_LOOP(b) \
00249         const MHD_gnutls_error_entry *p; \
00250                 for(p = MHD_gtls_error_algorithms; p->desc != NULL; p++) { b ; }
00251 
00252 #define GNUTLS_ERROR_ALG_LOOP(a) \
00253                         GNUTLS_ERROR_LOOP( if(p->number == error) { a; break; } )
00254 
00255 
00256 
00273 int
00274 MHD_gtls_error_is_fatal (int error)
00275 {
00276   int ret = 1;
00277 
00278   /* Input sanitzation.  Positive values are not errors at all, and
00279      definitely not fatal. */
00280   if (error > 0)
00281     return 0;
00282 
00283   GNUTLS_ERROR_ALG_LOOP (ret = p->fatal);
00284 
00285   return ret;
00286 }
00287 
00295 void
00296 MHD_gtls_perror (int error)
00297 {
00298   const char *ret = NULL;
00299 
00300   /* avoid prefix */
00301   GNUTLS_ERROR_ALG_LOOP (ret = p->desc);
00302   if (ret == NULL)
00303     ret = "(unknown)";
00304   fprintf (stderr, "GNUTLS ERROR: %s\n", _(ret));
00305 }
00306 
00307 
00316 const char *
00317 MHD_gtls_strerror (int error)
00318 {
00319   const char *ret = NULL;
00320 
00321   /* avoid prefix */
00322   GNUTLS_ERROR_ALG_LOOP (ret = p->desc);
00323   if (ret == NULL)
00324     return "(unknown error code)";
00325   return _(ret);
00326 }
00327 
00328 /* This will print the actual define of the
00329  * given error code.
00330  */
00331 const char *
00332 MHD__gnutls_strerror (int error)
00333 {
00334   const char *ret = NULL;
00335 
00336   /* avoid prefix */
00337   GNUTLS_ERROR_ALG_LOOP (ret = p->_name);
00338 
00339   return _(ret);
00340 }
00341 
00342 int
00343 MHD_gtls_asn2err (int asn_err)
00344 {
00345   switch (asn_err)
00346     {
00347     case ASN1_FILE_NOT_FOUND:
00348       return GNUTLS_E_FILE_ERROR;
00349     case ASN1_ELEMENT_NOT_FOUND:
00350       return GNUTLS_E_ASN1_ELEMENT_NOT_FOUND;
00351     case ASN1_IDENTIFIER_NOT_FOUND:
00352       return GNUTLS_E_ASN1_IDENTIFIER_NOT_FOUND;
00353     case ASN1_DER_ERROR:
00354       return GNUTLS_E_ASN1_DER_ERROR;
00355     case ASN1_VALUE_NOT_FOUND:
00356       return GNUTLS_E_ASN1_VALUE_NOT_FOUND;
00357     case ASN1_GENERIC_ERROR:
00358       return GNUTLS_E_ASN1_GENERIC_ERROR;
00359     case ASN1_VALUE_NOT_VALID:
00360       return GNUTLS_E_ASN1_VALUE_NOT_VALID;
00361     case ASN1_TAG_ERROR:
00362       return GNUTLS_E_ASN1_TAG_ERROR;
00363     case ASN1_TAG_IMPLICIT:
00364       return GNUTLS_E_ASN1_TAG_IMPLICIT;
00365     case ASN1_ERROR_TYPE_ANY:
00366       return GNUTLS_E_ASN1_TYPE_ANY_ERROR;
00367     case ASN1_SYNTAX_ERROR:
00368       return GNUTLS_E_ASN1_SYNTAX_ERROR;
00369     case ASN1_MEM_ERROR:
00370       return GNUTLS_E_SHORT_MEMORY_BUFFER;
00371     case ASN1_MEM_ALLOC_ERROR:
00372       return GNUTLS_E_MEMORY_ERROR;
00373     case ASN1_DER_OVERFLOW:
00374       return GNUTLS_E_ASN1_DER_OVERFLOW;
00375     default:
00376       return GNUTLS_E_ASN1_GENERIC_ERROR;
00377     }
00378 }
00379 
00380 
00381 /* this function will output a message using the
00382  * caller provided function
00383  */
00384 void
00385 MHD_gtls_log (int level, const char *fmt, ...)
00386 {
00387   va_list args;
00388   char str[MAX_LOG_SIZE];
00389   void (*log_func) (int, const char *) = MHD__gnutls_log_func;
00390 
00391   if (MHD__gnutls_log_func == NULL)
00392     return;
00393 
00394   va_start (args, fmt);
00395   vsnprintf (str, MAX_LOG_SIZE - 1, fmt, args); /* Flawfinder: ignore */
00396   va_end (args);
00397 
00398   log_func (level, str);
00399 }
00400 
00401 void
00402 MHD__gnutls_null_log (void *n, ...)
00403 {
00404 }

Generated on Fri Feb 27 18:18:40 2009 for GNU libmicrohttpd by  doxygen 1.5.8