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 (void)
00131 {
00132   int result = 0;
00133   int res;
00134   char c;
00135 
00136   if (MHD__gnutls_init_level++)
00137     return 0;
00138 
00139 #if HAVE_WINSOCK
00140   {
00141     WORD requested;
00142     WSADATA data;
00143     int err;
00144 
00145     requested = MAKEWORD (1, 1);
00146     err = WSAStartup (requested, &data);
00147     if (err != 0)
00148       {
00149         MHD__gnutls_debug_log ("WSAStartup failed: %d.\n", err);
00150         return GNUTLS_E_LIBRARY_VERSION_MISMATCH;
00151       }
00152 
00153     if (data.wVersion < requested)
00154       {
00155         MHD__gnutls_debug_log ("WSAStartup version check failed (%d < %d).\n",
00156                                data.wVersion, requested);
00157         WSACleanup ();
00158         return GNUTLS_E_LIBRARY_VERSION_MISMATCH;
00159       }
00160   }
00161 #endif
00162 
00163   // bindtextdomain("mhd", "./");
00164 
00165   if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P) == 0)
00166     {
00167       const char *p;
00168 
00169       /* to enable multi-threading this call must precede any other call made to libgcrypt */
00170       gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
00171 
00172       /* set p to point at the required version of gcrypt */
00173       p = strchr (MHD_GCRYPT_VERSION, ':');
00174       if (p == NULL)
00175         p = MHD_GCRYPT_VERSION;
00176       else
00177         p++;
00178 
00179       /* this call initializes libgcrypt */
00180       if (gcry_check_version (p) == NULL)
00181         {
00182           MHD_gnutls_assert ();
00183           MHD__gnutls_debug_log ("Checking for libgcrypt failed '%s'\n", p);
00184           return GNUTLS_E_INCOMPATIBLE_GCRYPT_LIBRARY;
00185         }
00186 
00187       /* for gcrypt in order to be able to allocate memory */
00188       gcry_set_allocation_handler (MHD_gnutls_malloc,
00189                                    MHD_gnutls_secure_malloc,
00190                                    MHD__gnutls_is_secure_memory,
00191                                    MHD_gnutls_realloc, MHD_gnutls_free);
00192 
00193       /* gcry_control (GCRYCTL_DISABLE_INTERNAL_LOCKING, NULL, 0); */
00194 
00195       gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);
00196 
00197     }
00198 
00199   if (MHD_gc_init () != GC_OK)
00200     {
00201       MHD_gnutls_assert ();
00202       MHD__gnutls_debug_log ("Initializing crypto backend failed\n");
00203       return GNUTLS_E_INCOMPATIBLE_CRYPTO_LIBRARY;
00204     }
00205 
00206   /* initialize parser
00207    * This should not deal with files in the final
00208    * version.
00209    */
00210   res =
00211     MHD__asn1_array2tree (MHD_pkix_asn1_tab, &MHD__gnutls_pkix1_asn, NULL);
00212   if (res != ASN1_SUCCESS)
00213     {
00214       result = MHD_gtls_asn2err (res);
00215       return result;
00216     }
00217 
00218   res =
00219     MHD__asn1_array2tree (MHD_gnutlsMHD__asn1_tab,
00220                           &MHD__gnutlsMHD__gnutls_asn, NULL);
00221   if (res != ASN1_SUCCESS)
00222     {
00223       MHD__asn1_delete_structure (&MHD__gnutls_pkix1_asn);
00224       result = MHD_gtls_asn2err (res);
00225       return result;
00226     }
00227 
00228   /* Initialize the gcrypt (if used random generator) */
00229   MHD_gc_pseudo_random (&c, 1);
00230 
00231   return result;
00232 }
00233 
00244 void
00245 MHD__gnutls_global_deinit (void)
00246 {
00247   if (MHD__gnutls_init_level == 1)
00248     {
00249 #if HAVE_WINSOCK
00250       WSACleanup ();
00251 #endif
00252       MHD__asn1_delete_structure (&MHD__gnutlsMHD__gnutls_asn);
00253       MHD__asn1_delete_structure (&MHD__gnutls_pkix1_asn);
00254       MHD_gc_done ();
00255     }
00256   MHD__gnutls_init_level--;
00257 }
00258 
00259 /* These functions should be elsewere. Kept here for
00260  * historical reasons.
00261  */
00262 
00276 void
00277 MHD__gnutls_transport_set_pull_function (MHD_gtls_session_t session,
00278                                          MHD_gtls_pull_func pull_func)
00279 {
00280   session->internals.MHD__gnutls_pull_func = pull_func;
00281 }
00282 
00297 void
00298 MHD__gnutls_transport_set_push_function (MHD_gtls_session_t session,
00299                                          MHD_gtls_push_func push_func)
00300 {
00301   session->internals.MHD__gnutls_push_func = push_func;
00302 }

Generated on Tue May 19 23:21:08 2009 for GNU libmicrohttpd by  doxygen 1.5.8