gnutls_global.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 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 #include <gnutls_dh.h>
00029 
00030 /* this is used in order to make the multi-threaded initialization call to libgcrypt */
00031 #include <pthread.h>
00032 #include <gcrypt.h>
00033 
00034 /* used to set the MHD_tls logging function */
00035 #include "internal.h"
00036 
00037 /* TODO fix :  needed by GCRY_THREAD_OPTION_PTHREAD_IMPL but missing otherwise */
00038 #define ENOMEM    12            /* Out of memory */
00039 
00040 #ifdef HAVE_WINSOCK
00041 # include <winsock2.h>
00042 #endif
00043 
00044 
00045 GCRY_THREAD_OPTION_PTHREAD_IMPL;
00046 
00047 #define MHD_gnutls_log_func LOG_FUNC
00048 
00049 /* created by asn1c */
00050 extern const ASN1_ARRAY_TYPE MHD_gnutlsMHD__asn1_tab[];
00051 extern const ASN1_ARRAY_TYPE MHD_pkix_asn1_tab[];
00052 
00053 LOG_FUNC MHD__gnutls_log_func;
00054 int MHD__gnutls_log_level = 0;  /* default log level */
00055 
00056 ASN1_TYPE MHD__gnutls_pkix1_asn;
00057 ASN1_TYPE MHD__gnutlsMHD__gnutls_asn;
00058 
00071 void
00072 MHD_gtls_global_set_log_function (MHD_gnutls_log_func log_func)
00073 {
00074   MHD__gnutls_log_func = log_func;
00075 }
00076 
00089 void
00090 MHD_gtls_global_set_log_level (int level)
00091 {
00092   MHD__gnutls_log_level = level;
00093 }
00094 
00095 int MHD__gnutls_is_secure_mem_null (const void *);
00096 
00097 static int MHD__gnutls_init_level = 0;
00098 
00129 int
00130 MHD__gnutls_global_init ()
00131 {
00132   int result = 0;
00133   int res;
00134 
00135   if (MHD__gnutls_init_level++)
00136     return 0;
00137 
00138 #if HAVE_WINSOCK
00139   {
00140     WORD requested;
00141     WSADATA data;
00142     int err;
00143 
00144     requested = MAKEWORD (1, 1);
00145     err = WSAStartup (requested, &data);
00146     if (err != 0)
00147       {
00148         MHD__gnutls_debug_log ("WSAStartup failed: %d.\n", err);
00149         return GNUTLS_E_LIBRARY_VERSION_MISMATCH;
00150       }
00151 
00152     if (data.wVersion < requested)
00153       {
00154         MHD__gnutls_debug_log ("WSAStartup version check failed (%d < %d).\n",
00155                                data.wVersion, requested);
00156         WSACleanup ();
00157         return GNUTLS_E_LIBRARY_VERSION_MISMATCH;
00158       }
00159   }
00160 #endif
00161 
00162   /* bindtextdomain("mhd", "./"); */
00163 
00164   if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P) == 0)
00165     {
00166       const char *p;
00167 
00168       /* to enable multi-threading this call must precede any other call made to libgcrypt */
00169       gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
00170 
00171       /* set p to point at the required version of gcrypt */
00172       p = strchr (MHD_GCRYPT_VERSION, ':');
00173       if (p == NULL)
00174         p = MHD_GCRYPT_VERSION;
00175       else
00176         p++;
00177 
00178       /* this call initializes libgcrypt */
00179       if (gcry_check_version (p) == NULL)
00180         {
00181           MHD_gnutls_assert ();
00182           MHD__gnutls_debug_log ("Checking for libgcrypt failed '%s'\n", p);
00183           return GNUTLS_E_INCOMPATIBLE_GCRYPT_LIBRARY;
00184         }
00185 
00186       /* for gcrypt in order to be able to allocate memory */
00187       gcry_set_allocation_handler (MHD_gnutls_malloc,
00188                                    MHD_gnutls_secure_malloc,
00189                                    MHD__gnutls_is_secure_memory,
00190                                    MHD_gnutls_realloc, MHD_gnutls_free);
00191 
00192       /* gcry_control (GCRYCTL_DISABLE_INTERNAL_LOCKING, NULL, 0); */
00193 
00194       gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
00195 
00196     }
00197 
00198   if (MHD_gc_init () != GC_OK)
00199     {
00200       MHD_gnutls_assert ();
00201       MHD__gnutls_debug_log ("Initializing crypto backend failed\n");
00202       return GNUTLS_E_INCOMPATIBLE_CRYPTO_LIBRARY;
00203     }
00204 
00205   /* initialize parser
00206    * This should not deal with files in the final
00207    * version.
00208    */
00209   res =
00210     MHD__asn1_array2tree (MHD_pkix_asn1_tab, &MHD__gnutls_pkix1_asn, NULL);
00211   if (res != ASN1_SUCCESS)
00212     {
00213       result = MHD_gtls_asn2err (res);
00214       return result;
00215     }
00216 
00217   res =
00218     MHD__asn1_array2tree (MHD_gnutlsMHD__asn1_tab,
00219                           &MHD__gnutlsMHD__gnutls_asn, NULL);
00220   if (res != ASN1_SUCCESS)
00221     {
00222       MHD__asn1_delete_structure (&MHD__gnutls_pkix1_asn);
00223       result = MHD_gtls_asn2err (res);
00224       return result;
00225     }
00226 
00227   return result;
00228 }
00229 
00240 void
00241 MHD__gnutls_global_deinit ()
00242 {
00243   if (MHD__gnutls_init_level == 1)
00244     {
00245 #if HAVE_WINSOCK
00246       WSACleanup ();
00247 #endif
00248       MHD__asn1_delete_structure (&MHD__gnutlsMHD__gnutls_asn);
00249       MHD__asn1_delete_structure (&MHD__gnutls_pkix1_asn);
00250       MHD_gc_done ();
00251     }
00252   MHD__gnutls_init_level--;
00253 }
00254 
00255 /* These functions should be elsewere. Kept here for
00256  * historical reasons.
00257  */
00258 
00272 void
00273 MHD__gnutls_transport_set_pull_function (MHD_gtls_session_t session,
00274                                          MHD_gtls_pull_func pull_func)
00275 {
00276   session->internals.MHD__gnutls_pull_func = pull_func;
00277 }
00278 
00293 void
00294 MHD__gnutls_transport_set_push_function (MHD_gtls_session_t session,
00295                                          MHD_gtls_push_func push_func)
00296 {
00297   session->internals.MHD__gnutls_push_func = push_func;
00298 }
Generated by  doxygen 1.6.2-20100208