aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/https/tls/gnutls_global.c
blob: 65ac210a3d3f3c01b5a317cf3c46b11a658f8934 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GNUTLS.
 *
 * The GNUTLS library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA
 *
 */

#include <gnutls_int.h>
#include <gnutls_errors.h>
#include <libtasn1.h>
#include <gnutls_dh.h>

/* this is used in order to make the multi-threaded initialization call to libgcrypt */
#include <pthread.h>
#include <gcrypt.h>

/* used to set the MHD_tls logging function */
#include "internal.h"

/* TODO fix :  needed by GCRY_THREAD_OPTION_PTHREAD_IMPL but missing otherwise */
#define ENOMEM    12            /* Out of memory */

#ifdef HAVE_WINSOCK
# include <winsock2.h>
#endif


GCRY_THREAD_OPTION_PTHREAD_IMPL;

#define MHD_gnutls_log_func LOG_FUNC

/* created by asn1c */
extern const ASN1_ARRAY_TYPE MHD_gnutlsMHD__asn1_tab[];
extern const ASN1_ARRAY_TYPE MHD_pkix_asn1_tab[];

LOG_FUNC MHD__gnutls_log_func;
int MHD__gnutls_log_level = 0;  /* default log level */

ASN1_TYPE MHD__gnutls_pkix1_asn;
ASN1_TYPE MHD__gnutlsMHD__gnutls_asn;

/**
 * MHD_gtls_global_set_log_function - This function sets the logging function
 * @log_func: it's a log function
 *
 * This is the function where you set the logging function gnutls
 * is going to use. This function only accepts a character array.
 * Normally you may not use this function since it is only used
 * for debugging purposes.
 *
 * MHD_gnutls_log_func is of the form,
 * void (*MHD_gnutls_log_func)( int level, const char*);
 **/
void
MHD_gtls_global_set_log_function (MHD_gnutls_log_func log_func)
{
  MHD__gnutls_log_func = log_func;
}

/**
 * MHD_gtls_global_set_log_level - This function sets the logging level
 * @level: it's an integer from 0 to 9.
 *
 * This is the function that allows you to set the log level.
 * The level is an integer between 0 and 9. Higher values mean
 * more verbosity. The default value is 0. Larger values should
 * only be used with care, since they may reveal sensitive information.
 *
 * Use a log level over 10 to enable all debugging options.
 *
 **/
void
MHD_gtls_global_set_log_level (int level)
{
  MHD__gnutls_log_level = level;
}

int MHD__gnutls_is_secure_mem_null (const void *);

static int MHD__gnutls_init_level = 0;

/**
 * MHD__gnutls_global_init - This function initializes the global data to defaults.
 *
 * This function initializes the global data to defaults.
 * Every gnutls application has a global data which holds common parameters
 * shared by gnutls session structures.
 * You must call MHD__gnutls_global_deinit() when gnutls usage is no longer needed
 * Returns zero on success.
 *
 * Note that this function will also initialize libgcrypt, if it has not
 * been initialized before. Thus if you want to manually initialize libgcrypt
 * you must do it before calling this function. This is useful in cases you
 * want to disable libgcrypt's internal lockings etc.
 *
 * This function increment a global counter, so that
 * MHD__gnutls_global_deinit() only releases resources when it has been
 * called as many times as MHD__gnutls_global_init().  This is useful when
 * GnuTLS is used by more than one library in an application.  This
 * function can be called many times, but will only do something the
 * first time.
 *
 * Note!  This function is not thread safe.  If two threads call this
 * function simultaneously, they can cause a race between checking
 * the global counter and incrementing it, causing both threads to
 * execute the library initialization code.  That would lead to a
 * memory leak.  To handle this, your application could invoke this
 * function after aquiring a thread mutex.  To ignore the potential
 * memory leak is also an option.
 *
 **/
int
MHD__gnutls_global_init (void)
{
  int result = 0;
  int res;
  char c;

  if (MHD__gnutls_init_level++)
    return 0;

#if HAVE_WINSOCK
  {
    WORD requested;
    WSADATA data;
    int err;

    requested = MAKEWORD (1, 1);
    err = WSAStartup (requested, &data);
    if (err != 0)
      {
        MHD__gnutls_debug_log ("WSAStartup failed: %d.\n", err);
        return GNUTLS_E_LIBRARY_VERSION_MISMATCH;
      }

    if (data.wVersion < requested)
      {
        MHD__gnutls_debug_log ("WSAStartup version check failed (%d < %d).\n",
                               data.wVersion, requested);
        WSACleanup ();
        return GNUTLS_E_LIBRARY_VERSION_MISMATCH;
      }
  }
#endif

  // bindtextdomain("mhd", "./");

  if (gcry_control (GCRYCTL_ANY_INITIALIZATION_P) == 0)
    {
      const char *p;

      /* to enable multi-threading this call must precede any other call made to libgcrypt */
      gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);

      /* set p to point at the required version of gcrypt */
      p = strchr (MHD_GCRYPT_VERSION, ':');
      if (p == NULL)
        p = MHD_GCRYPT_VERSION;
      else
        p++;

      /* this call initializes libgcrypt */
      if (gcry_check_version (p) == NULL)
        {
          MHD_gnutls_assert ();
          MHD__gnutls_debug_log ("Checking for libgcrypt failed '%s'\n", p);
          return GNUTLS_E_INCOMPATIBLE_GCRYPT_LIBRARY;
        }

      /* for gcrypt in order to be able to allocate memory */
      gcry_set_allocation_handler (MHD_gnutls_malloc,
                                   MHD_gnutls_secure_malloc,
                                   MHD__gnutls_is_secure_memory,
                                   MHD_gnutls_realloc, MHD_gnutls_free);

      /* gcry_control (GCRYCTL_DISABLE_INTERNAL_LOCKING, NULL, 0); */

      gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL, 0);

    }

  if (MHD_gc_init () != GC_OK)
    {
      MHD_gnutls_assert ();
      MHD__gnutls_debug_log ("Initializing crypto backend failed\n");
      return GNUTLS_E_INCOMPATIBLE_CRYPTO_LIBRARY;
    }

  /* initialize parser
   * This should not deal with files in the final
   * version.
   */
  res =
    MHD__asn1_array2tree (MHD_pkix_asn1_tab, &MHD__gnutls_pkix1_asn, NULL);
  if (res != ASN1_SUCCESS)
    {
      result = MHD_gtls_asn2err (res);
      return result;
    }

  res =
    MHD__asn1_array2tree (MHD_gnutlsMHD__asn1_tab,
                          &MHD__gnutlsMHD__gnutls_asn, NULL);
  if (res != ASN1_SUCCESS)
    {
      MHD__asn1_delete_structure (&MHD__gnutls_pkix1_asn);
      result = MHD_gtls_asn2err (res);
      return result;
    }

  /* Initialize the gcrypt (if used random generator) */
  MHD_gc_pseudo_random (&c, 1);

  return result;
}

/**
 * MHD__gnutls_global_deinit - This function deinitializes the global data
 *
 * This function deinitializes the global data, that were initialized
 * using MHD__gnutls_global_init().
 *
 * Note!  This function is not thread safe.  See the discussion for
 * MHD__gnutls_global_init() for more information.
 *
 **/
void
MHD__gnutls_global_deinit (void)
{
  if (MHD__gnutls_init_level == 1)
    {
#if HAVE_WINSOCK
      WSACleanup ();
#endif
      MHD__asn1_delete_structure (&MHD__gnutlsMHD__gnutls_asn);
      MHD__asn1_delete_structure (&MHD__gnutls_pkix1_asn);
      MHD_gc_done ();
    }
  MHD__gnutls_init_level--;
}

/* These functions should be elsewere. Kept here for
 * historical reasons.
 */

/**
 * MHD__gnutls_transport_set_pull_function - This function sets a read like function
 * @pull_func: a callback function similar to read()
 * @session: gnutls session
 *
 * This is the function where you set a function for gnutls
 * to receive data. Normally, if you use berkeley style sockets,
 * do not need to use this function since the default (recv(2)) will
 * probably be ok.
 *
 * PULL_FUNC is of the form,
 * ssize_t (*MHD_gtls_pull_func)(MHD_gnutls_transport_ptr_t, void*, size_t);
 **/
void
MHD__gnutls_transport_set_pull_function (MHD_gtls_session_t session,
                                         MHD_gtls_pull_func pull_func)
{
  session->internals.MHD__gnutls_pull_func = pull_func;
}

/**
 * MHD__gnutls_transport_set_push_function - This function sets the function to send data
 * @push_func: a callback function similar to write()
 * @session: gnutls session
 *
 * This is the function where you set a push function for gnutls
 * to use in order to send data. If you are going to use berkeley style
 * sockets, you do not need to use this function since
 * the default (send(2)) will probably be ok. Otherwise you should
 * specify this function for gnutls to be able to send data.
 *
 * PUSH_FUNC is of the form,
 * ssize_t (*MHD_gtls_push_func)(MHD_gnutls_transport_ptr_t, const void*, size_t);
 **/
void
MHD__gnutls_transport_set_push_function (MHD_gtls_session_t session,
                                         MHD_gtls_push_func push_func)
{
  session->internals.MHD__gnutls_push_func = push_func;
}