]> git.sesse.net Git - vlc/blob - src/misc/tls.c
Initial partial SSL/TLS support (to be continued)
[vlc] / src / misc / tls.c
1 /*****************************************************************************
2  * tls.c
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id: httpd.c 8263 2004-07-24 09:06:58Z courmisch $
6  *
7  * Authors: Remi Denis-Courmont <courmisch@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU 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, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #include <stdlib.h>
25 #include <vlc/vlc.h>
26 #include <assert.h>
27
28 #include "vlc_tls.h"
29
30
31 #define HAVE_GNUTLS 1
32 /*
33  * TODO:
34  * - libgcrypt thread-safety !!!
35  * - fix FIXMEs,
36  * - gnutls version check,
37  * - client side stuff,
38  * - server-side client cert validation,
39  * - client-side server cert validation (?).
40  */
41
42 /* FIXME: proper configure check */
43 //#define HAVE_GNUTLS 1
44
45 #ifdef HAVE_GNUTLS
46 #   include <gnutls/gnutls.h>
47
48 #   define DH_BITS 1024
49
50
51 struct tls_server_t
52 {
53     gnutls_certificate_credentials x509_cred;
54     gnutls_dh_params dh_params;
55     vlc_object_t *p_this;
56 };
57
58 struct tls_session_t
59 {
60     gnutls_session session;
61     vlc_object_t *p_this;
62 };
63
64 /* FIXME: is this legal in the VLC? */
65 unsigned i_servernum = 0;
66
67
68 static int
69 tls_Init( vlc_object_t *p_this )
70 {
71     vlc_value_t lock;
72
73     var_Create( p_this->p_libvlc, "tls_mutex", VLC_VAR_MUTEX );
74     var_Get( p_this->p_libvlc, "tls_mutex", &lock );
75     vlc_mutex_lock( lock.p_address );
76
77     /* Initialize GnuTLS only once */
78     /* FIXME: should check version number */
79     if( i_servernum == 0)
80     {
81         if( gnutls_global_init( ) )
82         {
83             msg_Warn( p_this, "cannot initialize GNUTLS" );
84             vlc_mutex_unlock( lock.p_address);
85             return -1;
86         }
87         msg_Dbg( p_this, "GNUTLS initialized" );
88     }
89
90     i_servernum++;
91     vlc_mutex_unlock( lock.p_address );
92
93     return 0;
94 }
95
96
97 static void
98 tls_CleanUp( vlc_object_t *p_this )
99 {
100     vlc_value_t lock;
101
102     var_Create( p_this->p_libvlc, "tls_mutex", VLC_VAR_MUTEX );
103     var_Get( p_this->p_libvlc, "tls_mutex", &lock );
104     vlc_mutex_lock( lock.p_address );
105
106     i_servernum--;
107     if( i_servernum == 0 )
108     {
109         gnutls_global_deinit( );
110         msg_Dbg( p_this, "GNUTLS deinitialized" );
111     }
112
113     vlc_mutex_unlock( lock.p_address);
114 }
115
116 #endif
117
118
119 /*****************************************************************************
120  * tls_ServerCreate:
121  *****************************************************************************
122  * Allocates a whole server's TLS credentials.
123  * Returns NULL on error.
124  *****************************************************************************/
125 tls_server_t *
126 tls_ServerCreate( vlc_object_t *p_this, const char *psz_cert_path,
127                   const char *psz_key_path )
128 {
129 #if HAVE_GNUTLS
130     tls_server_t *p_server;
131     int val;
132
133     msg_Dbg( p_this, "Creating TLS server" );
134     if( tls_Init( p_this ) )
135         return NULL;
136
137     p_server = (tls_server_t *)malloc( sizeof(struct tls_server_t) );
138
139     /* FIXME: do not hard-code PEM file paths */
140     /* Sets server's credentials */
141     val = gnutls_certificate_allocate_credentials( &p_server->x509_cred );
142     if( val != 0 )
143     {
144         msg_Err( p_this, "Cannot allocate X509 credentials : %s",
145                  gnutls_strerror( val ) );
146         free( p_server );
147         return NULL;
148     }
149
150     val = gnutls_certificate_set_x509_key_file( p_server->x509_cred,
151                                                 psz_cert_path, psz_key_path,
152                                                 GNUTLS_X509_FMT_PEM );
153     if( val < 0 )
154     {
155         msg_Err( p_this, "Cannot set certificate chain or private key : %s",
156                  gnutls_strerror( val ) );
157         gnutls_certificate_free_credentials( p_server->x509_cred );
158         free( p_server );
159         return NULL;
160     }
161
162     /* FIXME: regenerate these regularly */
163     val = gnutls_dh_params_init( &p_server->dh_params );
164     if( val >= 0 )
165     {
166         msg_Dbg( p_this, "Computing Diffie Hellman ciphers parameters" );
167         val = gnutls_dh_params_generate2( p_server->dh_params, DH_BITS );
168     }
169     if( val < 0 )
170     {
171         msg_Err( p_this, "Cannot initialize DH cipher suites : %s",
172                  gnutls_strerror( val ) );
173         gnutls_certificate_free_credentials( p_server->x509_cred );
174         free( p_server );
175         return NULL;
176     }
177     msg_Dbg( p_this, "Ciphers parameters computed" );
178
179     gnutls_certificate_set_dh_params( p_server->x509_cred,
180                                       p_server->dh_params);
181    
182     p_server->p_this = p_this;
183     return p_server;
184 #else
185     return NULL;
186 #endif
187 }
188
189
190 /*****************************************************************************
191  * tls_ServerAddCA:
192  *****************************************************************************
193  * Adds one or more certificate authorities.
194  * TODO: we are not able to check the client credentials yet, so this function
195  * is pretty useless.
196  * Returns -1 on error, 0 on success.
197  *****************************************************************************/
198 int
199 tls_ServerAddCA( tls_server_t *p_server, const char *psz_ca_path )
200 {
201 #if HAVE_GNUTLS
202     int val;
203
204     assert( p_server != NULL);
205     val = gnutls_certificate_set_x509_trust_file( p_server->x509_cred,
206                                                   psz_ca_path,
207                                                   GNUTLS_X509_FMT_PEM );
208     if( val < 0 )
209     {
210         msg_Err( p_server->p_this, "Cannot add trusted CA (%s) : %s",
211                  psz_ca_path, gnutls_strerror( val ) );
212         free( p_server );
213         return -1;
214     }
215     msg_Dbg( p_server->p_this, " %d trusted CA added (%s)", val,
216              psz_ca_path );
217     return 0;
218 #else
219     return -1;
220 #endif
221 }
222
223
224 /*****************************************************************************
225  * tls_ServerAddCRL:
226  *****************************************************************************
227  * Adds a certificates revocation list to be sent to TLS clients.
228  * Returns -1 on error, 0 on success.
229  *****************************************************************************/
230 int
231 tls_ServerAddCRL( tls_server_t *p_server, const char *psz_crl_path )
232 {
233 #if HAVE_GNUTLS
234     int val;
235
236     val = gnutls_certificate_set_x509_crl_file( p_server->x509_cred,
237                                                 psz_crl_path,
238                                                 GNUTLS_X509_FMT_PEM );
239     if( val < 0 )
240     {
241         msg_Err( p_server->p_this, "Cannot add CRL (%s) : %s",
242                  psz_crl_path, gnutls_strerror( val ) );
243         free( p_server );
244         return -1;
245     }
246     msg_Dbg( p_server->p_this, "%d CRL added (%s)", val, psz_crl_path );
247     return 0;
248 #else
249     return -1;
250 #endif
251 }
252     
253
254
255 /*****************************************************************************
256  * tls_ServerDelete:
257  *****************************************************************************
258  * Releases data allocated with tls_ServerCreate
259  *****************************************************************************/
260 void
261 tls_ServerDelete( tls_server_t *p_server )
262 {
263     assert( p_server != NULL );
264
265 #if HAVE_GNUTLS
266     gnutls_certificate_free_credentials( p_server->x509_cred );
267     tls_CleanUp( p_server->p_this );
268     free( p_server );
269 #endif
270 }
271
272
273 /*****************************************************************************
274  * tls_ServerSessionPrepare:
275  *****************************************************************************
276  * Initializes a server-side TLS session data
277  *****************************************************************************/
278 tls_session_t *
279 tls_ServerSessionPrepare( const tls_server_t *p_server )
280 {
281 #if HAVE_GNUTLS
282     tls_session_t *p_session;
283     gnutls_session session;
284     int val;
285
286     assert( p_server != NULL );
287
288     val = gnutls_init( &session, GNUTLS_SERVER );
289     if( val != 0 )
290     {
291         msg_Err( p_server->p_this, "Cannot initialize TLS session : %s",
292                  gnutls_strerror( val ) );
293         return NULL;
294     }
295    
296     val = gnutls_set_default_priority( session );
297     if( val < 0 )
298     {
299         msg_Err( p_server->p_this, "Cannot set ciphers priorities : %s",
300                  gnutls_strerror( val ) );
301         gnutls_deinit( session );
302         return NULL;
303     }
304
305     val = gnutls_credentials_set( session, GNUTLS_CRD_CERTIFICATE,
306                                   p_server->x509_cred );
307     if( val < 0 )
308     {
309         msg_Err( p_server->p_this, "Cannot set TLS session credentials : %s",
310                  gnutls_strerror( val ) );
311         gnutls_deinit( session );
312         return NULL;
313     }
314
315     /* TODO: support for client authentication */
316     /*gnutls_certificate_server_set_request( p_session->session,
317                                            GNUTLS_CERT_REQUEST ); */
318
319     gnutls_dh_set_prime_bits( session, DH_BITS );
320
321     p_session = malloc( sizeof (struct tls_session_t) );
322     p_session->session = session;
323     p_session->p_this = p_server->p_this;
324
325     return p_session;
326 #else
327     return NULL;
328 #endif
329 }
330
331
332 /*****************************************************************************
333  * tls_SessionHandshake:
334  *****************************************************************************
335  * Establishes TLS session with a peer through socket <fd>
336  * Returns NULL on error (do NOT call tls_SessionClose in case of error or
337  * re-use the session structure).
338  *****************************************************************************/
339 tls_session_t *
340 tls_SessionHandshake( tls_session_t *p_session, int fd )
341 {
342 #if HAVE_GNUTLS
343     int val;
344
345     assert( p_session != NULL );
346
347     gnutls_transport_set_ptr( p_session->session, (gnutls_transport_ptr)fd);
348     val = gnutls_handshake( p_session->session);
349     if( val < 0 )
350     {
351         gnutls_deinit( p_session->session );
352         msg_Err( p_session->p_this, "TLS handshake failed : %s",
353                  gnutls_strerror( val ) );
354         free( p_session );
355         return NULL;
356     }
357     return p_session;
358 #else
359     return NULL;
360 #endif
361 }
362
363
364 /*****************************************************************************
365  * tls_ServerCreate:
366  *****************************************************************************
367  * Terminates a TLS session and releases session data.
368  *****************************************************************************/
369 void
370 tls_SessionClose( tls_session_t *p_session )
371 {
372     assert( p_session != NULL );
373
374 #if HAVE_GNUTLS
375     gnutls_bye( p_session->session, GNUTLS_SHUT_WR );
376     gnutls_deinit (p_session->session );
377     free( p_session );
378 #endif
379 }
380
381
382 /*****************************************************************************
383  * tls_Send:
384  *****************************************************************************
385  * Sends data through a TLS session.
386  *****************************************************************************/
387 int
388 tls_Send( tls_session_t *p_session, const char *buf, int i_length )
389 {
390 #if HAVE_GNUTLS
391     int val;
392
393     assert( p_session != NULL );
394
395     val = gnutls_record_send( p_session->session, buf, i_length );
396     if( val < 0 )
397     {
398         /*msg_Warn( p_session->p_this, "TLS problem : %s",
399                   gnutls_strerror( val ) );*/
400         return -1;
401     }
402     return val;
403 #else
404     return -1;
405 #endif
406 }
407
408
409 /*****************************************************************************
410  * tls_Recv:
411  *****************************************************************************
412  * Receives data through a TLS session
413  *****************************************************************************/
414 int
415 tls_Recv( tls_session_t *p_session, char *buf, int i_length )
416 {
417 #if HAVE_GNUTLS
418     int val;
419
420     assert( p_session != NULL );
421
422     val = gnutls_record_recv( p_session->session, buf, i_length );
423     if( val < 0 )
424     {
425         /*msg_Warn( p_session->p_this, "TLS problem : %s",
426                   gnutls_strerror( val ) );*/
427         return -1;
428     }
429     return val;
430 #else
431     return -1;
432 #endif
433 }