tizen 2.3.1 release
[external/libgnutls26.git] / libextra / gnutls_extra.c
1 /*
2  * Copyright (C) 2001, 2004, 2005, 2007, 2008, 2009, 2010 Free Software
3  * Foundation, Inc.
4  *
5  * Author: Nikos Mavrogiannopoulos
6  *
7  * This file is part of GnuTLS-EXTRA.
8  *
9  * GnuTLS-extra is free software: you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation, either version 3 of the
12  * License, or (at your option) any later version.
13  *
14  * GnuTLS-extra 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  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see
21  * <http://www.gnu.org/licenses/>.
22  */
23
24 #include <gnutls_int.h>
25 #include <gnutls_errors.h>
26 #include <gnutls_extensions.h>
27 #include <gnutls_algorithms.h>
28 #include <ext_inner_application.h>
29
30 #ifdef HAVE_GCRYPT
31 #include <gcrypt.h>
32 #endif
33
34 #ifdef USE_LZO
35 #ifdef USE_MINILZO
36 #include "minilzo/minilzo.h"
37 #elif HAVE_LZO_LZO1X_H
38 #include <lzo/lzo1x.h>
39 #elif HAVE_LZO1X_H
40 #include <lzo1x.h>
41 #endif
42 #endif
43 #include <gnutls/extra.h>
44
45 #ifdef USE_LZO
46 #include <gnutls_compress.h>
47
48 /* the number of the compression algorithms available in the compression
49  * structure.
50  */
51 extern int _gnutls_comp_algorithms_size;
52
53 typedef int (*LZO_FUNC) ();
54 extern LZO_FUNC _gnutls_lzo1x_decompress_safe;
55 extern LZO_FUNC _gnutls_lzo1x_1_compress;
56
57 extern gnutls_compression_entry _gnutls_compression_algorithms[];
58
59 static int
60 _gnutls_add_lzo_comp (void)
61 {
62   int i;
63
64   /* find the last element */
65   for (i = 0; i < _gnutls_comp_algorithms_size; i++)
66     {
67       if (_gnutls_compression_algorithms[i].name == NULL)
68         break;
69     }
70
71   if (_gnutls_compression_algorithms[i].name == NULL
72       && (i < _gnutls_comp_algorithms_size - 1))
73     {
74       _gnutls_compression_algorithms[i].name = "GNUTLS_COMP_LZO";
75       _gnutls_compression_algorithms[i].id = GNUTLS_COMP_LZO;
76       _gnutls_compression_algorithms[i].num = 0xf2;
77
78       _gnutls_compression_algorithms[i + 1].name = 0;
79
80       /* Now enable the lzo functions: */
81       _gnutls_lzo1x_decompress_safe = lzo1x_decompress_safe;
82       _gnutls_lzo1x_1_compress = lzo1x_1_compress;
83
84       return 0;                 /* ok */
85     }
86
87
88   return GNUTLS_E_MEMORY_ERROR;
89 }
90 #endif
91
92 static int _gnutls_init_extra = 0;
93
94 /**
95  * gnutls_global_init_extra:
96  *
97  * This function initializes the global state of gnutls-extra library
98  * to defaults.
99  *
100  * Note that gnutls_global_init() has to be called before this
101  * function.  If this function is not called then the gnutls-extra
102  * library will not be usable.
103  *
104  * This function is not thread safe, see the discussion for
105  * gnutls_global_init() on how to deal with that.
106  *
107  * Returns: On success, %GNUTLS_E_SUCCESS (zero) is returned,
108  *   otherwise an error code is returned.
109  **/
110 int
111 gnutls_global_init_extra (void)
112 {
113   int ret;
114
115   /* If the version of libgnutls != version of
116    * libextra, then do not initialize the library.
117    * This is because it may break things.
118    */
119   if (strcmp (gnutls_check_version (NULL), VERSION) != 0)
120     {
121       return GNUTLS_E_LIBRARY_VERSION_MISMATCH;
122     }
123
124   _gnutls_init_extra++;
125
126   if (_gnutls_init_extra != 1)
127     return 0;
128
129   ret = _gnutls_ext_register (&ext_mod_ia);
130   if (ret != GNUTLS_E_SUCCESS)
131     return ret;
132
133   /* Initialize the LZO library
134    */
135 #ifdef USE_LZO
136   if (lzo_init () != LZO_E_OK)
137     return GNUTLS_E_LZO_INIT_FAILED;
138
139   /* Add the LZO compression method in the list of compression
140    * methods.
141    */
142   ret = _gnutls_add_lzo_comp ();
143   if (ret < 0)
144     {
145       gnutls_assert ();
146       return ret;
147     }
148 #endif
149
150
151 #ifdef HAVE_GCRYPT
152 #ifdef gcry_fips_mode_active
153   /* Libgcrypt manual says that gcry_version_check must be called
154      before calling gcry_fips_mode_active. */
155   gcry_check_version (NULL);
156   if (gcry_fips_mode_active ())
157     {
158       ret = gnutls_register_md5_handler ();
159       if (ret)
160         fprintf (stderr, "gnutls_register_md5_handler: %s\n",
161                  gnutls_strerror (ret));
162     }
163 #endif
164 #endif
165
166   return 0;
167 }
168
169 /**
170  * gnutls_extra_check_version:
171  * @req_version: version string to compare with, or %NULL.
172  *
173  * Check GnuTLS Extra Library version.
174  *
175  * See %GNUTLS_EXTRA_VERSION for a suitable @req_version string.
176  *
177  * Return value: Check that the version of the library is at
178  *   minimum the one given as a string in @req_version and return the
179  *   actual version string of the library; return %NULL if the
180  *   condition is not met.  If %NULL is passed to this function no
181  *   check is done and only the version string is returned.
182  **/
183 const char *
184 gnutls_extra_check_version (const char *req_version)
185 {
186   if (!req_version || strverscmp (req_version, VERSION) <= 0)
187     return VERSION;
188
189   return NULL;
190 }