]> git.sesse.net Git - vlc/blob - modules/misc/gnutls.c
Client-side anonymous TLS/SSL support
[vlc] / modules / misc / gnutls.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 /*
25  * TODO:
26  * - fix FIXMEs,
27  * - client side stuff,
28  * - server-side client cert validation,
29  * - client-side server cert validation (?).
30  */
31
32  
33 /*****************************************************************************
34  * Preamble
35  *****************************************************************************/
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <vlc/vlc.h>
39
40 #include "vlc_tls.h"
41
42 #include <gcrypt.h>
43 #include <gnutls/gnutls.h>
44
45 #define DH_BITS 1024
46
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 static int  Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
53
54 #define DH_BITS_TEXT N_("Diffie-Hellman prime bits")
55 #define DH_BITS_LONGTEXT N_( \
56     "Allows you to modify the Diffie-Hellman prime's number of bits" \
57     "(used for TLS or SSL-based server-side encryption)." )
58
59 vlc_module_begin();
60     set_description( _("GnuTLS TLS encryption layer") );
61     set_capability( "tls", 1 );
62     set_callbacks( Open, Close );
63
64     add_integer( "dh-bits", DH_BITS, NULL, DH_BITS_TEXT,
65                  DH_BITS_LONGTEXT, VLC_TRUE );
66 vlc_module_end();
67
68
69
70 typedef struct tls_server_sys_t
71 {
72     gnutls_certificate_credentials x509_cred;
73     gnutls_dh_params dh_params;
74 } tls_server_sys_t;
75
76
77 /* client-side session private data */
78 typedef struct tls_client_sys_t
79 {
80     gnutls_session session;
81     gnutls_certificate_credentials x509_cred;
82 } tls_client_sys_t;
83
84
85 /*****************************************************************************
86  * tls_Send:
87  *****************************************************************************
88  * Sends data through a TLS session.
89  *****************************************************************************/
90 static int
91 gnutls_Send( tls_session_t *p_session, const char *buf, int i_length )
92 {
93     int val;
94
95     val = gnutls_record_send( *(gnutls_session *)(p_session->p_sys),
96                               buf, i_length );
97     return val < 0 ? -1 : val;
98 }
99
100
101 /*****************************************************************************
102  * tls_Recv:
103  *****************************************************************************
104  * Receives data through a TLS session.
105  *****************************************************************************/
106 static int
107 gnutls_Recv( tls_session_t *p_session, char *buf, int i_length )
108 {
109     int val;
110
111     val = gnutls_record_recv( *(gnutls_session *)(p_session->p_sys),
112                               buf, i_length );
113     return val < 0 ? -1 : val;
114 }
115
116
117 /*****************************************************************************
118  * tls_SessionHandshake:
119  *****************************************************************************
120  * Establishes TLS session with a peer through socket <fd>
121  * Returns NULL on error (do NOT call tls_SessionClose in case of error or
122  * re-use the session structure).
123  *****************************************************************************/
124 static tls_session_t *
125 gnutls_SessionHandshake( tls_session_t *p_session, int fd )
126 {
127     int val;
128     gnutls_session *p_sys;
129
130     p_sys = (gnutls_session *)(p_session->p_sys);
131
132     gnutls_transport_set_ptr( *p_sys, (gnutls_transport_ptr)fd);
133     val = gnutls_handshake( *p_sys );
134     if( val < 0 )
135     {
136         gnutls_deinit( *p_sys );
137         msg_Err( p_session->p_tls, "TLS handshake failed : %s",
138                  gnutls_strerror( val ) );
139         free( p_sys );
140         free( p_session );
141         return NULL;
142     }
143     return p_session;
144 }
145
146
147 /*****************************************************************************
148  * tls_SessionClose:
149  *****************************************************************************
150  * Terminates TLS session and releases session data.
151  *****************************************************************************/
152 static void
153 gnutls_SessionClose( tls_session_t *p_session )
154 {
155     gnutls_session *p_sys;
156
157     p_sys = (gnutls_session *)(p_session->p_sys);
158
159     /* On the client-side, credentials are re-allocated per session */
160     if( p_session->p_server == NULL )
161         gnutls_certificate_free_credentials( ((tls_client_sys_t *)p_sys)
162                                                 ->x509_cred );
163
164     gnutls_bye( *p_sys, GNUTLS_SHUT_WR );
165     gnutls_deinit( *p_sys );
166     free( p_sys );
167     free( p_session );
168 }
169
170
171 /*****************************************************************************
172  * tls_ClientCreate:
173  *****************************************************************************
174  * Initializes client-side TLS session data.
175  *****************************************************************************/
176 static tls_session_t *
177 gnutls_ClientCreate( tls_t *p_tls, const char *psz_ca_path )
178 {
179     tls_session_t *p_session;
180     tls_client_sys_t *p_sys;
181     int i_val;
182     const int cert_type_priority[3] =
183     {
184         GNUTLS_CRT_X509,
185         0
186     };
187
188     p_sys = (tls_client_sys_t *)malloc( sizeof(struct tls_client_sys_t) );
189     if( p_sys == NULL )
190         return NULL;
191
192     i_val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
193     if( i_val != 0 )
194     {
195         msg_Err( p_tls, "Cannot allocate X509 credentials : %s",
196                  gnutls_strerror( i_val ) );
197         free( p_sys );
198         return NULL;
199     }
200
201     if( psz_ca_path != NULL )
202     {
203         i_val = gnutls_certificate_set_x509_trust_file( p_sys->x509_cred,
204                                                         psz_ca_path,
205                                                         GNUTLS_X509_FMT_PEM );
206         if( i_val != 0 )
207         {
208             msg_Err( p_tls, "Cannot add trusted CA (%s) : %s", psz_ca_path,
209                      gnutls_strerror( i_val ) );
210             gnutls_certificate_free_credentials( p_sys->x509_cred );
211             free( p_sys );
212             return NULL;
213         }
214     }
215
216     i_val = gnutls_init( &p_sys->session, GNUTLS_CLIENT );
217     if( i_val != 0 )
218     {
219         msg_Err( p_tls, "Cannot initialize TLS session : %s",
220                  gnutls_strerror( i_val ) );
221         gnutls_certificate_free_credentials( p_sys->x509_cred );
222         free( p_sys );
223         return NULL;
224     }
225
226     i_val = gnutls_set_default_priority( p_sys->session );
227     if( i_val < 0 )
228     {
229         msg_Err( p_tls, "Cannot set ciphers priorities : %s",
230                  gnutls_strerror( i_val ) );
231         gnutls_deinit( p_sys->session );
232         gnutls_certificate_free_credentials( p_sys->x509_cred );
233         free( p_sys );
234         return NULL;
235     }
236
237     i_val = gnutls_certificate_type_set_priority( p_sys->session, cert_type_priority );
238     if( i_val < 0 )
239     {
240         msg_Err( p_tls, "Cannot set certificate type priorities : %s",
241                  gnutls_strerror( i_val ) );
242         gnutls_deinit( p_sys->session );
243         gnutls_certificate_free_credentials( p_sys->x509_cred );
244         free( p_sys );
245         return NULL;
246     }
247
248     i_val = gnutls_credentials_set( p_sys->session, GNUTLS_CRD_CERTIFICATE,
249                                     p_sys->x509_cred );
250     if( i_val < 0 )
251     {
252         msg_Err( p_tls, "Cannot set TLS session credentials : %s",
253                  gnutls_strerror( i_val ) );
254         gnutls_deinit( p_sys->session );
255         gnutls_certificate_free_credentials( p_sys->x509_cred );
256         free( p_sys );
257         return NULL;
258     }
259
260     p_session = malloc( sizeof (struct tls_session_t) );
261     if( p_session == NULL )
262     {
263         gnutls_deinit( p_sys->session );
264         gnutls_certificate_free_credentials( p_sys->x509_cred );
265         free( p_sys );
266         return NULL;
267     }
268
269     p_session->p_tls = p_tls;
270     p_session->p_server = NULL;
271     p_session->p_sys = p_sys;
272     p_session->pf_handshake = gnutls_SessionHandshake;
273     p_session->pf_close = gnutls_SessionClose;
274     p_session->pf_send = gnutls_Send;
275     p_session->pf_recv = gnutls_Recv;
276
277     return p_session;
278 }
279
280
281 /*****************************************************************************
282  * tls_ServerSessionPrepare:
283  *****************************************************************************
284  * Initializes server-side TLS session data.
285  *****************************************************************************/
286 static tls_session_t *
287 gnutls_ServerSessionPrepare( tls_server_t *p_server )
288 {
289     tls_session_t *p_session;
290     gnutls_session *p_sys;
291     int val;
292     vlc_value_t bits;
293
294     p_sys = (gnutls_session *)malloc( sizeof(gnutls_session) );
295     if( p_sys == NULL )
296         return NULL;
297
298     val = gnutls_init( p_sys, GNUTLS_SERVER );
299     if( val != 0 )
300     {
301         msg_Err( p_server->p_tls, "Cannot initialize TLS session : %s",
302                  gnutls_strerror( val ) );
303         free( p_sys );
304         return NULL;
305     }
306    
307     val = gnutls_set_default_priority( *p_sys );
308     if( val < 0 )
309     {
310         msg_Err( p_server->p_tls, "Cannot set ciphers priorities : %s",
311                  gnutls_strerror( val ) );
312         gnutls_deinit( *p_sys );
313         free( p_sys );
314         return NULL;
315     }
316
317     val = gnutls_credentials_set( *p_sys, GNUTLS_CRD_CERTIFICATE,
318                                   ((tls_server_sys_t *)(p_server->p_sys))
319                                   ->x509_cred );
320     if( val < 0 )
321     {
322         msg_Err( p_server->p_tls, "Cannot set TLS session credentials : %s",
323                  gnutls_strerror( val ) );
324         gnutls_deinit( *p_sys );
325         free( p_sys );
326         return NULL;
327     }
328
329     /* TODO: support for client authentication */
330     /*gnutls_certificate_server_set_request( p_session->session,
331                                            GNUTLS_CERT_REQUEST ); */
332
333     if( var_Get( p_server->p_tls, "dh-bits", &bits ) != VLC_SUCCESS )
334     {
335         var_Create( p_server->p_tls, "dh-bits",
336                     VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
337         var_Get( p_server->p_tls, "dh-bits", &bits );
338     }
339
340     gnutls_dh_set_prime_bits( *p_sys, bits.i_int );
341
342     p_session = malloc( sizeof (struct tls_session_t) );
343     if( p_session == NULL )
344     {
345         gnutls_deinit( *p_sys );
346         free( p_sys );
347         return NULL;
348     }
349
350     p_session->p_tls = p_server->p_tls;
351     p_session->p_server = p_server;
352     p_session->p_sys = p_sys;
353     p_session->pf_handshake = gnutls_SessionHandshake;
354     p_session->pf_close = gnutls_SessionClose;
355     p_session->pf_send = gnutls_Send;
356     p_session->pf_recv = gnutls_Recv;
357
358     return p_session;
359 }
360
361
362 /*****************************************************************************
363  * tls_ServerDelete:
364  *****************************************************************************
365  * Releases data allocated with tls_ServerCreate.
366  *****************************************************************************/
367 static void
368 gnutls_ServerDelete( tls_server_t *p_server )
369 {
370     gnutls_certificate_free_credentials(
371                                        ((tls_server_sys_t *)(p_server->p_sys))
372                                          ->x509_cred );
373     free( p_server->p_sys );
374     free( p_server );
375 }
376
377
378 /*****************************************************************************
379  * tls_ServerAddCA:
380  *****************************************************************************
381  * Adds one or more certificate authorities.
382  * TODO: we are not able to check the client credentials yet, so this function
383  * is pretty useless.
384  * Returns -1 on error, 0 on success.
385  *****************************************************************************/
386 static int
387 gnutls_ServerAddCA( tls_server_t *p_server, const char *psz_ca_path )
388 {
389     int val;
390
391     val = gnutls_certificate_set_x509_trust_file( ((tls_server_sys_t *)
392                                                   (p_server->p_sys))
393                                                     ->x509_cred,
394                                                   psz_ca_path,
395                                                   GNUTLS_X509_FMT_PEM );
396     if( val < 0 )
397     {
398         msg_Err( p_server->p_tls, "Cannot add trusted CA (%s) : %s",
399                  psz_ca_path, gnutls_strerror( val ) );
400         gnutls_ServerDelete( p_server );
401         return VLC_EGENERIC;
402     }
403     msg_Dbg( p_server->p_tls, " %d trusted CA added (%s)", val,
404              psz_ca_path );
405     return VLC_SUCCESS;
406 }
407
408
409 /*****************************************************************************
410  * tls_ServerAddCRL:
411  *****************************************************************************
412  * Adds a certificates revocation list to be sent to TLS clients.
413  * Returns -1 on error, 0 on success.
414  *****************************************************************************/
415 static int
416 gnutls_ServerAddCRL( tls_server_t *p_server, const char *psz_crl_path )
417 {
418     int val;
419
420     val = gnutls_certificate_set_x509_crl_file( ((tls_server_sys_t *)
421                                                 (p_server->p_sys))->x509_cred,
422                                                 psz_crl_path,
423                                                 GNUTLS_X509_FMT_PEM );
424     if( val < 0 )
425     {
426         msg_Err( p_server->p_tls, "Cannot add CRL (%s) : %s",
427                  psz_crl_path, gnutls_strerror( val ) );
428         gnutls_ServerDelete( p_server );
429         return VLC_EGENERIC;
430     }
431     msg_Dbg( p_server->p_tls, "%d CRL added (%s)", val, psz_crl_path );
432     return VLC_SUCCESS;
433 }
434     
435
436 /*****************************************************************************
437  * tls_ServerCreate:
438  *****************************************************************************
439  * Allocates a whole server's TLS credentials.
440  * Returns NULL on error.
441  *****************************************************************************/
442 static tls_server_t *
443 gnutls_ServerCreate( tls_t *p_this, const char *psz_cert_path,
444                   const char *psz_key_path )
445 {
446     tls_server_t *p_server;
447     tls_server_sys_t *p_server_sys;
448     int val;
449
450     msg_Dbg( p_this, "Creating TLS server" );
451
452     p_server_sys = (tls_server_sys_t *)malloc( sizeof(struct tls_server_sys_t) );
453     if( p_server_sys == NULL )
454         return NULL;
455
456     /* Sets server's credentials */
457     val = gnutls_certificate_allocate_credentials( &p_server_sys->x509_cred );
458     if( val != 0 )
459     {
460         msg_Err( p_this, "Cannot allocate X509 credentials : %s",
461                  gnutls_strerror( val ) );
462         free( p_server_sys );
463         return NULL;
464     }
465
466     val = gnutls_certificate_set_x509_key_file( p_server_sys->x509_cred,
467                                                 psz_cert_path, psz_key_path,
468                                                 GNUTLS_X509_FMT_PEM );
469     if( val < 0 )
470     {
471         msg_Err( p_this, "Cannot set certificate chain or private key : %s",
472                  gnutls_strerror( val ) );
473         gnutls_certificate_free_credentials( p_server_sys->x509_cred );
474         free( p_server_sys );
475         return NULL;
476     }
477
478     /* FIXME: regenerate these regularly */
479     val = gnutls_dh_params_init( &p_server_sys->dh_params );
480     if( val >= 0 )
481     {
482         vlc_value_t bits;
483
484         if( var_Get( p_this, "dh-bits", &bits ) != VLC_SUCCESS )
485         {
486             var_Create( p_this, "dh-bits",
487                         VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
488             var_Get( p_this, "dh-bits", &bits );
489         }
490
491         msg_Dbg( p_this, "Computing Diffie Hellman ciphers parameters" );
492         val = gnutls_dh_params_generate2( p_server_sys->dh_params,
493                                           bits.i_int );
494     }
495     if( val < 0 )
496     {
497         msg_Err( p_this, "Cannot initialize DH cipher suites : %s",
498                  gnutls_strerror( val ) );
499         gnutls_certificate_free_credentials( p_server_sys->x509_cred );
500         free( p_server_sys );
501         return NULL;
502     }
503     msg_Dbg( p_this, "Ciphers parameters computed" );
504
505     gnutls_certificate_set_dh_params( p_server_sys->x509_cred,
506                                       p_server_sys->dh_params);
507
508     p_server = (tls_server_t *)malloc( sizeof(struct tls_server_t) );
509     if( p_server == NULL )
510     {
511         free( p_server_sys );
512         return NULL;
513     }
514
515     p_server->p_tls = p_this;
516     p_server->p_sys = p_server_sys;
517     p_server->pf_delete = gnutls_ServerDelete;
518     p_server->pf_add_CA = gnutls_ServerAddCA;
519     p_server->pf_add_CRL = gnutls_ServerAddCRL;
520     p_server->pf_session_prepare = gnutls_ServerSessionPrepare;
521
522     return p_server;
523 }
524
525
526 /*****************************************************************************
527  * gcrypt thread option VLC implementation:
528  *****************************************************************************/
529 vlc_object_t *__p_gcry_data;
530
531 static int gcry_vlc_mutex_init (void **p_sys)
532 {
533     int i_val;
534     vlc_mutex_t *p_lock = (vlc_mutex_t *)malloc (sizeof (vlc_mutex_t));
535
536     if( p_lock == NULL)
537         return ENOMEM;
538
539     i_val = vlc_mutex_init( __p_gcry_data, p_lock);
540     if (i_val)
541         free (p_lock);
542     else
543         *p_sys = p_lock;
544     return i_val;
545 }
546
547 static int gcry_vlc_mutex_destroy (void **p_sys)
548 {
549     int i_val;
550     vlc_mutex_t *p_lock = (vlc_mutex_t *)*p_sys;
551
552     i_val = vlc_mutex_destroy (p_lock);
553     free (p_lock);
554     return i_val;
555 }
556
557 static int gcry_vlc_mutex_lock (void **p_sys)
558 {
559     return vlc_mutex_lock ((vlc_mutex_t *)*p_sys);
560 }
561
562 static int gcry_vlc_mutex_unlock (void **lock)
563 {
564     return vlc_mutex_unlock ((vlc_mutex_t *)*lock);
565 }
566
567 static struct gcry_thread_cbs gcry_threads_vlc =                        
568 {
569     GCRY_THREAD_OPTION_USER,
570     NULL,
571     gcry_vlc_mutex_init,
572     gcry_vlc_mutex_destroy,
573     gcry_vlc_mutex_lock,
574     gcry_vlc_mutex_unlock
575 };
576
577
578 /*****************************************************************************
579  * Module initialization
580  *****************************************************************************/
581 static int
582 Open( vlc_object_t *p_this )
583 {
584     tls_t *p_tls = (tls_t *)p_this;
585
586     vlc_value_t lock, count;
587
588     var_Create( p_this->p_libvlc, "tls_mutex", VLC_VAR_MUTEX );
589     var_Get( p_this->p_libvlc, "tls_mutex", &lock );
590     vlc_mutex_lock( lock.p_address );
591
592     /* Initialize GnuTLS only once */
593     var_Create( p_this->p_libvlc, "gnutls_count", VLC_VAR_INTEGER );
594     var_Get( p_this->p_libvlc, "gnutls_count", &count);
595
596     if( count.i_int == 0)
597     {
598         __p_gcry_data = VLC_OBJECT( p_this->p_vlc );
599
600         gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_vlc);
601         if( gnutls_global_init( ) )
602         {
603             msg_Warn( p_this, "cannot initialize GNUTLS" );
604             vlc_mutex_unlock( lock.p_address );
605             return VLC_EGENERIC;
606         }
607         if( gnutls_check_version( "1.0.0" ) == NULL )
608         {
609             gnutls_global_deinit( );
610             vlc_mutex_unlock( lock.p_address );
611             msg_Err( p_this, "unsupported GNUTLS version" );
612             return VLC_EGENERIC;
613         }
614         msg_Dbg( p_this, "GNUTLS initialized" );
615     }
616
617     count.i_int++;
618     var_Set( p_this->p_libvlc, "gnutls_count", count);
619     vlc_mutex_unlock( lock.p_address );
620
621     p_tls->pf_server_create = gnutls_ServerCreate;
622     p_tls->pf_client_create = gnutls_ClientCreate;
623     return VLC_SUCCESS;
624 }
625
626
627 /*****************************************************************************
628  * Module deinitialization
629  *****************************************************************************/
630 static void
631 Close( vlc_object_t *p_this )
632 {
633     /*tls_t *p_tls = (tls_t *)p_this;
634     tls_sys_t *p_sys = (tls_sys_t *)(p_this->p_sys);*/
635
636     vlc_value_t lock, count;
637
638     var_Create( p_this->p_libvlc, "gnutls_mutex", VLC_VAR_MUTEX );
639     var_Get( p_this->p_libvlc, "gnutls_mutex", &lock );
640     vlc_mutex_lock( lock.p_address );
641
642     var_Create( p_this->p_libvlc, "gnutls_count", VLC_VAR_INTEGER );
643     var_Get( p_this->p_libvlc, "gnutls_count", &count);
644     count.i_int--;
645     var_Set( p_this->p_libvlc, "gnutls_count", count);
646
647     if( count.i_int == 0 )
648     {
649         gnutls_global_deinit( );
650         msg_Dbg( p_this, "GNUTLS deinitialized" );
651     }
652
653     vlc_mutex_unlock( lock.p_address);
654 }