guile: Fix `priorities' test to use `run-test'.
[gnutls.git] / lib / gnutls_global.c
blobe99f1c1fd4729a5f77d0a27f065f6888b57c7a37
1 /*
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009, 2010, 2011
3 * Free Software Foundation, Inc.
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GnuTLS.
9 * The GnuTLS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 3 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <gnutls_int.h>
25 #include <gnutls_errors.h>
26 #include <libtasn1.h>
27 #include <gnutls_dh.h>
28 #include <random.h>
29 #include <gnutls/pkcs11.h>
31 #include <gnutls_extensions.h> /* for _gnutls_ext_init */
32 #include <locks.h>
33 #include <system.h>
34 #include <accelerated/cryptodev.h>
35 #include <accelerated/accelerated.h>
37 #include "sockets.h"
38 #include "gettext.h"
40 /* Minimum library versions we accept. */
41 #define GNUTLS_MIN_LIBTASN1_VERSION "0.3.4"
43 /* created by asn1c */
44 extern const ASN1_ARRAY_TYPE gnutls_asn1_tab[];
45 extern const ASN1_ARRAY_TYPE pkix_asn1_tab[];
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.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 were 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 (is_secure_func != NULL)
155 _gnutls_is_secure_memory = is_secure_func;
156 else
157 _gnutls_is_secure_memory = _gnutls_is_secure_mem_null;
159 /* if using the libc's default malloc
160 * use libc's calloc as well.
162 if (gnutls_malloc == malloc)
164 gnutls_calloc = calloc;
166 else
167 { /* use the included ones */
168 gnutls_calloc = _gnutls_calloc;
170 gnutls_strdup = _gnutls_strdup;
174 static int _gnutls_init = 0;
177 * gnutls_global_init:
179 * This function initializes the global data to defaults. Every
180 * gnutls application has a global data which holds common parameters
181 * shared by gnutls session structures. You should call
182 * gnutls_global_deinit() when gnutls usage is no longer needed
184 * Note that this function will also initialize the underlying crypto
185 * backend, if it has not been initialized before.
187 * This function increment a global counter, so that
188 * gnutls_global_deinit() only releases resources when it has been
189 * called as many times as gnutls_global_init(). This is useful when
190 * GnuTLS is used by more than one library in an application. This
191 * function can be called many times, but will only do something the
192 * first time.
194 * Note! This function is not thread safe. If two threads call this
195 * function simultaneously, they can cause a race between checking
196 * the global counter and incrementing it, causing both threads to
197 * execute the library initialization code. That would lead to a
198 * memory leak. To handle this, your application could invoke this
199 * function after aquiring a thread mutex. To ignore the potential
200 * memory leak is also an option.
202 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned,
203 * otherwise a negative error code is returned.
206 gnutls_global_init (void)
208 int result = 0;
209 int res;
211 if (_gnutls_init++)
212 goto out;
214 if (gl_sockets_startup (SOCKETS_1_1))
215 return GNUTLS_E_LIBRARY_VERSION_MISMATCH;
217 bindtextdomain (PACKAGE, LOCALEDIR);
219 res = gnutls_crypto_init ();
220 if (res != 0)
222 gnutls_assert ();
223 return GNUTLS_E_CRYPTO_INIT_FAILED;
226 _gnutls_register_accel_crypto();
228 /* initialize ASN.1 parser
229 * This should not deal with files in the final
230 * version.
232 if (asn1_check_version (GNUTLS_MIN_LIBTASN1_VERSION) == NULL)
234 gnutls_assert ();
235 _gnutls_debug_log ("Checking for libtasn1 failed: %s < %s\n",
236 asn1_check_version (NULL),
237 GNUTLS_MIN_LIBTASN1_VERSION);
238 return GNUTLS_E_INCOMPATIBLE_LIBTASN1_LIBRARY;
241 res = asn1_array2tree (pkix_asn1_tab, &_gnutls_pkix1_asn, NULL);
242 if (res != ASN1_SUCCESS)
244 result = _gnutls_asn2err (res);
245 goto out;
248 res = asn1_array2tree (gnutls_asn1_tab, &_gnutls_gnutls_asn, NULL);
249 if (res != ASN1_SUCCESS)
251 asn1_delete_structure (&_gnutls_pkix1_asn);
252 result = _gnutls_asn2err (res);
253 goto out;
256 /* Initialize the random generator */
257 result = _gnutls_rnd_init ();
258 if (result < 0)
260 gnutls_assert ();
261 goto out;
264 /* Initialize the default TLS extensions */
265 result = _gnutls_ext_init ();
266 if (result < 0)
268 gnutls_assert ();
269 goto out;
272 #ifdef ENABLE_PKCS11
273 gnutls_pkcs11_init (GNUTLS_PKCS11_FLAG_AUTO, NULL);
274 #endif
276 _gnutls_cryptodev_init ();
278 out:
279 return result;
283 * gnutls_global_deinit:
285 * This function deinitializes the global data, that were initialized
286 * using gnutls_global_init().
288 * Note! This function is not thread safe. See the discussion for
289 * gnutls_global_init() for more information.
291 void
292 gnutls_global_deinit (void)
294 if (_gnutls_init == 1)
296 gl_sockets_cleanup ();
297 _gnutls_rnd_deinit ();
298 _gnutls_ext_deinit ();
299 asn1_delete_structure (&_gnutls_gnutls_asn);
300 asn1_delete_structure (&_gnutls_pkix1_asn);
301 _gnutls_crypto_deregister ();
302 _gnutls_cryptodev_deinit ();
303 #ifdef ENABLE_PKCS11
304 gnutls_pkcs11_deinit ();
305 #endif
307 _gnutls_init--;
310 /* These functions should be elsewere. Kept here for
311 * historical reasons.
316 * gnutls_check_version:
317 * @req_version: version string to compare with, or %NULL.
319 * Check GnuTLS Library version.
321 * See %GNUTLS_VERSION for a suitable @req_version string.
323 * Returns: Check that the version of the library is at
324 * minimum the one given as a string in @req_version and return the
325 * actual version string of the library; return %NULL if the
326 * condition is not met. If %NULL is passed to this function no
327 * check is done and only the version string is returned.
329 const char *
330 gnutls_check_version (const char *req_version)
332 if (!req_version || strverscmp (req_version, VERSION) <= 0)
333 return VERSION;
335 return NULL;