*** empty log message ***
[gnutls.git] / lib / gnutls_global.c
blob35ab5437269730c796aa1ef0f4f8b8b99a38556a
1 /*
2 * Copyright (C) 2001,2002 Nikos Mavroyanopoulos
4 * This file is part of GNUTLS.
6 * The GNUTLS library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <gnutls_int.h>
23 #include <gnutls_errors.h>
24 #include <x509_asn1.h>
25 #include <gnutls_dh.h>
28 /* created by asn1c */
29 extern const static_asn gnutls_asn1_tab[];
30 extern const static_asn pkix_asn1_tab[];
33 typedef void (*LOG_FUNC)( const char*);
34 #define GNUTLS_LOG_FUNC LOG_FUNC
36 LOG_FUNC _gnutls_log_func;
38 static node_asn *PKIX1_ASN;
39 static node_asn *GNUTLS_ASN;
41 node_asn* _gnutls_get_pkix(void) {
42 return PKIX1_ASN;
45 node_asn* _gnutls_get_gnutls_asn(void) {
46 return GNUTLS_ASN;
50 /**
51 * gnutls_global_set_log_func - This function sets the logging function
52 * @log_func: it's a log function
54 * This is the function were you set the logging function gnutls
55 * is going to use. This function only accepts a character array.
56 * Normaly you may not use this function since
57 * it is only used for debugging reasons.
58 * LOG_FUNC is of the form,
59 * void (*LOG_FUNC)( const char*);
60 **/
61 void gnutls_global_set_log_func( GNUTLS_LOG_FUNC log_func) {
62 _gnutls_log_func = log_func;
66 /* default logging function */
67 static void dlog( const char* str) {
68 #ifdef DEBUG
69 fprintf( stderr, "%s", str);
70 #endif
73 extern void* (*gnutls_secure_malloc)(size_t);
74 extern void* (*gnutls_malloc)(size_t);
75 extern void (*gnutls_free)(void*);
76 extern int (*_gnutls_is_secure_memory)(const void*);
77 extern void* (*gnutls_realloc)(void*, size_t);
79 int _gnutls_is_secure_mem_null( const void*);
81 /**
82 * gnutls_global_set_mem_func - This function sets the memory allocation functions
83 * @alloc_func: it's the default memory allocation function. Like malloc().
84 * @secure_alloc_func: This is the memory allocation function that will be used for sensitive data.
85 * @is_secure_func: a function that returns 0 if the memory given is not secure. May be NULL.
86 * @realloc_func: A realloc function
87 * @free_func: The function that frees allocated data.
89 * This is the function were you set the memory allocation functions gnutls
90 * is going to use. By default the libc's allocation functions (malloc(), free()),
91 * are used by gnutls, to allocate both sensitive and not sensitive data.
92 * This function is provided to set the memory allocation functions to
93 * something other than the defaults (ie the gcrypt allocation functions).
95 * This function must be called before gnutls_global_init() is called.
97 **/
98 void gnutls_global_set_mem_func(
99 void *(*gnutls_alloc_func)(size_t), void* (*gnutls_secure_alloc_func)(size_t),
100 int (*gnutls_is_secure_func)(const void*), void *(*gnutls_realloc_func)(void *, size_t),
101 void (*gnutls_free_func)(void*))
103 gnutls_secure_malloc = gnutls_secure_alloc_func;
104 gnutls_malloc = gnutls_alloc_func;
105 gnutls_realloc = gnutls_realloc_func;
106 gnutls_free = gnutls_free_func;
108 if (gnutls_is_secure_func==NULL)
109 _gnutls_is_secure_memory = gnutls_is_secure_func;
110 else
111 _gnutls_is_secure_memory = _gnutls_is_secure_mem_null;
113 return;
116 static int _gnutls_init = 0;
119 * gnutls_global_init - This function initializes the global state to defaults.
121 * This function initializes the global state to defaults.
122 * Every gnutls application has a global state which holds common parameters
123 * shared by gnutls state structures.
124 * You must call gnutls_global_deinit() when gnutls usage is no longer needed
125 * Returns zero on success.
127 * Note that this function will also initialize libgcrypt, if it has not
128 * been initialized before. Thus if you want to manualy initialize libgcrypt
129 * you must do it before calling this function. (useful in cases you want
130 * to disable internal lockings etc.)
133 int gnutls_global_init( void)
135 int result;
137 _gnutls_init++;
139 if (_gnutls_init!=1) {
140 return 0;
143 if (gcry_control( GCRYCTL_ANY_INITIALIZATION_P) == 0) {
144 /* for gcrypt in order to be able to allocate memory */
145 gcry_set_allocation_handler(gnutls_malloc, gnutls_secure_malloc, _gnutls_is_secure_memory, gnutls_realloc, gnutls_free);
147 /* gcry_control (GCRYCTL_DISABLE_INTERNAL_LOCKING, NULL, 0); */
149 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, NULL,0);
150 gcry_control (GCRYCTL_SET_VERBOSITY, (int)0);
153 /* set default recv/send functions
155 gnutls_global_set_log_func( dlog);
157 /* initialize parser
158 * This should not deal with files in the final
159 * version.
162 result=asn1_create_tree( (void*)pkix_asn1_tab, &PKIX1_ASN);
163 if (result != ASN_OK) {
164 return _gnutls_asn2err(result);
167 result=asn1_create_tree( (void*)gnutls_asn1_tab, &GNUTLS_ASN);
168 if (result != ASN_OK) {
169 asn1_delete_structure( PKIX1_ASN);
170 return _gnutls_asn2err(result);
173 result = _gnutls_dh_calc_mpis();
174 if (result < 0) {
175 gnutls_assert();
176 return result;
180 return 0;
184 * gnutls_global_deinit - This function deinitializes the global state
186 * This function deinitializes the global state.
190 void gnutls_global_deinit( void) {
192 _gnutls_init--;
194 if (_gnutls_init==0) {
195 asn1_delete_structure( GNUTLS_ASN);
196 asn1_delete_structure( PKIX1_ASN);
198 _gnutls_dh_clear_mpis();
204 /* These functions should be elsewere. Kept here for
205 * historical reasons.
209 * gnutls_transport_set_pull_func - This function sets a read like function
210 * @pull_func: it's a function like read
211 * @state: gnutls state
213 * This is the function where you set a function for gnutls
214 * to receive data. Normaly, if you use berkeley style sockets,
215 * you may not use this function since the default (recv(2)) will
216 * probably be ok.
217 * This function should be called once and after gnutls_global_init().
218 * PULL_FUNC is of the form,
219 * ssize_t (*GNUTLS_PULL_FUNC)(GNUTLS_TRANSPORT_PTR, const void*, size_t);
221 void gnutls_transport_set_pull_func( GNUTLS_STATE state, GNUTLS_PULL_FUNC pull_func) {
222 state->gnutls_internals._gnutls_pull_func = pull_func;
226 * gnutls_transport_set_push_func - This function sets the function to send data
227 * @push_func: it's a function like write
228 * @state: gnutls state
230 * This is the function where you set a push function for gnutls
231 * to use in order to send data. If you are going to use berkeley style
232 * sockets, you may not use this function since
233 * the default (send(2)) will probably be ok. Otherwise you should
234 * specify this function for gnutls to be able to send data.
236 * This function should be called once and after gnutls_global_init().
237 * PUSH_FUNC is of the form,
238 * ssize_t (*GNUTLS_PUSH_FUNC)(GNUTLS_TRANSPORT_PTR, const void*, size_t);
240 void gnutls_transport_set_push_func( GNUTLS_STATE state, GNUTLS_PUSH_FUNC push_func) {
241 state->gnutls_internals._gnutls_push_func = push_func;