Use the new asn1_read_node_value()
[gnutls.git] / lib / gnutls_global.c
blob3a71eaefa3fa55f1fce9502f737b7155fff7a9eb
1 /*
2 * Copyright (C) 2001-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 #include <gnutls_dh.h>
27 #include <random.h>
28 #include <gnutls/pkcs11.h>
30 #include <gnutls_extensions.h> /* for _gnutls_ext_init */
31 #include <locks.h>
32 #include <system.h>
33 #include <accelerated/cryptodev.h>
34 #include <accelerated/accelerated.h>
36 #include "sockets.h"
37 #include "gettext.h"
39 /* Minimum library versions we accept. */
40 #define GNUTLS_MIN_LIBTASN1_VERSION "0.3.4"
42 /* created by asn1c */
43 extern const ASN1_ARRAY_TYPE gnutls_asn1_tab[];
44 extern const ASN1_ARRAY_TYPE pkix_asn1_tab[];
45 extern void *_gnutls_file_mutex;
47 ASN1_TYPE _gnutls_pkix1_asn;
48 ASN1_TYPE _gnutls_gnutls_asn;
50 gnutls_log_func _gnutls_log_func;
51 gnutls_audit_log_func _gnutls_audit_log_func;
52 int _gnutls_log_level = 0; /* default log level */
54 /**
55 * gnutls_global_set_log_function:
56 * @log_func: it's a log function
58 * This is the function where you set the logging function gnutls is
59 * going to use. This function only accepts a character array.
60 * Normally you may not use this function since it is only used for
61 * debugging purposes.
63 * @gnutls_log_func is of the form,
64 * void (*gnutls_log_func)( int level, const char*);
65 **/
66 void
67 gnutls_global_set_log_function (gnutls_log_func log_func)
69 _gnutls_log_func = log_func;
72 /**
73 * gnutls_global_set_audit_log_function:
74 * @log_func: it is the audit log function
76 * This is the function where you set the logging function gnutls is
77 * going to use. This is different from gnutls_global_set_log_function()
78 * because it will report the session of the event if any. Note that
79 * that session might be null if there is no corresponding TLS session.
81 * @gnutls_audit_log_func is of the form,
82 * void (*gnutls_audit_log_func)( gnutls_session_t, int level, const char*);
84 * Since: 3.0
85 **/
86 void
87 gnutls_global_set_audit_log_function (gnutls_audit_log_func log_func)
89 _gnutls_audit_log_func = log_func;
92 /**
93 * gnutls_global_set_time_function:
94 * @time_func: it's the system time function, a gnutls_time_func() callback.
96 * This is the function where you can override the default system time
97 * function. The application provided function should behave the same
98 * as the standard function.
100 * Since: 2.12.0
102 void
103 gnutls_global_set_time_function (gnutls_time_func time_func)
105 gnutls_time = time_func;
109 * gnutls_global_set_log_level:
110 * @level: it's an integer from 0 to 9.
112 * This is the function that allows you to set the log level. The
113 * level is an integer between 0 and 9. Higher values mean more
114 * verbosity. The default value is 0. Larger values should only be
115 * used with care, since they may reveal sensitive information.
117 * Use a log level over 10 to enable all debugging options.
119 void
120 gnutls_global_set_log_level (int level)
122 _gnutls_log_level = level;
126 * gnutls_global_set_mem_functions:
127 * @alloc_func: it's the default memory allocation function. Like malloc().
128 * @secure_alloc_func: This is the memory allocation function that will be used for sensitive data.
129 * @is_secure_func: a function that returns 0 if the memory given is not secure. May be NULL.
130 * @realloc_func: A realloc function
131 * @free_func: The function that frees allocated data. Must accept a NULL pointer.
133 * This is the function where you set the memory allocation functions
134 * gnutls is going to use. By default the libc's allocation functions
135 * (malloc(), free()), are used by gnutls, to allocate both sensitive
136 * and not sensitive data. This function is provided to set the
137 * memory allocation functions to something other than the defaults
139 * This function must be called before gnutls_global_init() is called.
140 * This function is not thread safe.
142 void
143 gnutls_global_set_mem_functions (gnutls_alloc_function alloc_func,
144 gnutls_alloc_function secure_alloc_func,
145 gnutls_is_secure_function is_secure_func,
146 gnutls_realloc_function realloc_func,
147 gnutls_free_function free_func)
149 gnutls_secure_malloc = secure_alloc_func;
150 gnutls_malloc = alloc_func;
151 gnutls_realloc = realloc_func;
152 gnutls_free = free_func;
154 /* if using the libc's default malloc
155 * use libc's calloc as well.
157 if (gnutls_malloc == malloc)
159 gnutls_calloc = calloc;
161 else
162 { /* use the included ones */
163 gnutls_calloc = _gnutls_calloc;
165 gnutls_strdup = _gnutls_strdup;
169 static int _gnutls_init = 0;
172 * gnutls_global_init:
174 * This function initializes the global data to defaults.
175 * In order to free resources you may call gnutls_global_deinit()
176 * when gnutls usage is no longer needed.
178 * Note that this function will also initialize the underlying crypto
179 * backend, if it has not been initialized before.
181 * This function increments a global counter, so that
182 * gnutls_global_deinit() only releases resources when it has been
183 * called as many times as gnutls_global_init(). This is useful when
184 * GnuTLS is used by more than one library in an application. This
185 * function can be called many times, but will only do something the
186 * first time.
188 * Note! This function is not thread safe. If two threads call this
189 * function simultaneously, they can cause a race between checking
190 * the global counter and incrementing it, causing both threads to
191 * execute the library initialization code. That would lead to a
192 * memory leak. To handle this, your application could invoke this
193 * function after aquiring a thread mutex. To ignore the potential
194 * memory leak is also an option.
196 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,
197 * otherwise a negative error code is returned.
200 gnutls_global_init (void)
202 int result = 0;
203 int res;
205 if (_gnutls_init++)
206 goto out;
208 if (gl_sockets_startup (SOCKETS_1_1))
209 return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
211 bindtextdomain (PACKAGE, LOCALEDIR);
213 res = gnutls_crypto_init ();
214 if (res != 0)
216 gnutls_assert ();
217 return GNUTLS_E_CRYPTO_INIT_FAILED;
220 _gnutls_register_accel_crypto();
222 /* initialize ASN.1 parser
223 * This should not deal with files in the final
224 * version.
226 if (asn1_check_version (GNUTLS_MIN_LIBTASN1_VERSION) == NULL)
228 gnutls_assert ();
229 _gnutls_debug_log ("Checking for libtasn1 failed: %s < %s\n",
230 asn1_check_version (NULL),
231 GNUTLS_MIN_LIBTASN1_VERSION);
232 return GNUTLS_E_INCOMPATIBLE_LIBTASN1_LIBRARY;
235 res = asn1_array2tree (pkix_asn1_tab, &_gnutls_pkix1_asn, NULL);
236 if (res != ASN1_SUCCESS)
238 result = _gnutls_asn2err (res);
239 goto out;
242 res = asn1_array2tree (gnutls_asn1_tab, &_gnutls_gnutls_asn, NULL);
243 if (res != ASN1_SUCCESS)
245 asn1_delete_structure (&_gnutls_pkix1_asn);
246 result = _gnutls_asn2err (res);
247 goto out;
250 /* Initialize the random generator */
251 result = _gnutls_rnd_init ();
252 if (result < 0)
254 gnutls_assert ();
255 goto out;
258 /* Initialize the default TLS extensions */
259 result = _gnutls_ext_init ();
260 if (result < 0)
262 gnutls_assert ();
263 goto out;
266 result = gnutls_mutex_init(&_gnutls_file_mutex);
267 if (result < 0)
269 gnutls_assert();
270 goto out;
273 #ifdef ENABLE_PKCS11
274 gnutls_pkcs11_init (GNUTLS_PKCS11_FLAG_AUTO, NULL);
275 #endif
277 _gnutls_cryptodev_init ();
279 out:
280 return result;
284 * gnutls_global_deinit:
286 * This function deinitializes the global data, that were initialized
287 * using gnutls_global_init().
289 * Note! This function is not thread safe. See the discussion for
290 * gnutls_global_init() for more information.
292 void
293 gnutls_global_deinit (void)
295 if (_gnutls_init == 1)
297 gl_sockets_cleanup ();
298 _gnutls_rnd_deinit ();
299 _gnutls_ext_deinit ();
300 asn1_delete_structure (&_gnutls_gnutls_asn);
301 asn1_delete_structure (&_gnutls_pkix1_asn);
302 _gnutls_crypto_deregister ();
303 _gnutls_cryptodev_deinit ();
304 #ifdef ENABLE_PKCS11
305 gnutls_pkcs11_deinit ();
306 #endif
307 gnutls_mutex_deinit(&_gnutls_file_mutex);
309 _gnutls_init--;
312 /* These functions should be elsewere. Kept here for
313 * historical reasons.
318 * gnutls_check_version:
319 * @req_version: version string to compare with, or %NULL.
321 * Check GnuTLS Library version.
323 * See %GNUTLS_VERSION for a suitable @req_version string.
325 * Returns: Check that the version of the library is at
326 * minimum the one given as a string in @req_version and return the
327 * actual version string of the library; return %NULL if the
328 * condition is not met. If %NULL is passed to this function no
329 * check is done and only the version string is returned.
331 const char *
332 gnutls_check_version (const char *req_version)
334 if (!req_version || strverscmp (req_version, VERSION) <= 0)
335 return VERSION;
337 return NULL;