]> git.sesse.net Git - vlc/blob - modules/misc/gnutls.c
Implement net_* virtualization with TLS module
[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( void *p_session, const void *buf, int i_length )
92 {
93     int val;
94
95     val = gnutls_record_send( *(gnutls_session *)(((tls_session_t *)p_session)
96                                 ->p_sys), 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( void *p_session, void *buf, int i_length )
108 {
109     int val;
110
111     val = gnutls_record_recv( *(gnutls_session *)(((tls_session_t *)p_session)
112                                 ->p_sys), 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->sock.p_sys = p_session;
273     p_session->sock.pf_send = gnutls_Send;
274     p_session->sock.pf_recv = gnutls_Recv;
275     p_session->pf_handshake = gnutls_SessionHandshake;
276     p_session->pf_close = gnutls_SessionClose;
277
278     return p_session;
279 }
280
281
282 /*****************************************************************************
283  * tls_ServerSessionPrepare:
284  *****************************************************************************
285  * Initializes server-side TLS session data.
286  *****************************************************************************/
287 static tls_session_t *
288 gnutls_ServerSessionPrepare( tls_server_t *p_server )
289 {
290     tls_session_t *p_session;
291     gnutls_session *p_sys;
292     int val;
293     vlc_value_t bits;
294
295     p_sys = (gnutls_session *)malloc( sizeof(gnutls_session) );
296     if( p_sys == NULL )
297         return NULL;
298
299     val = gnutls_init( p_sys, GNUTLS_SERVER );
300     if( val != 0 )
301     {
302         msg_Err( p_server->p_tls, "Cannot initialize TLS session : %s",
303                  gnutls_strerror( val ) );
304         free( p_sys );
305         return NULL;
306     }
307    
308     val = gnutls_set_default_priority( *p_sys );
309     if( val < 0 )
310     {
311         msg_Err( p_server->p_tls, "Cannot set ciphers priorities : %s",
312                  gnutls_strerror( val ) );
313         gnutls_deinit( *p_sys );
314         free( p_sys );
315         return NULL;
316     }
317
318     val = gnutls_credentials_set( *p_sys, GNUTLS_CRD_CERTIFICATE,
319                                   ((tls_server_sys_t *)(p_server->p_sys))
320                                   ->x509_cred );
321     if( val < 0 )
322     {
323         msg_Err( p_server->p_tls, "Cannot set TLS session credentials : %s",
324                  gnutls_strerror( val ) );
325         gnutls_deinit( *p_sys );
326         free( p_sys );
327         return NULL;
328     }
329
330     /* TODO: support for client authentication */
331     /*gnutls_certificate_server_set_request( p_session->session,
332                                            GNUTLS_CERT_REQUEST ); */
333
334     if( var_Get( p_server->p_tls, "dh-bits", &bits ) != VLC_SUCCESS )
335     {
336         var_Create( p_server->p_tls, "dh-bits",
337                     VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
338         var_Get( p_server->p_tls, "dh-bits", &bits );
339     }
340
341     gnutls_dh_set_prime_bits( *p_sys, bits.i_int );
342
343     p_session = malloc( sizeof (struct tls_session_t) );
344     if( p_session == NULL )
345     {
346         gnutls_deinit( *p_sys );
347         free( p_sys );
348         return NULL;
349     }
350
351     p_session->p_tls = p_server->p_tls;
352     p_session->p_server = p_server;
353     p_session->p_sys = p_sys;
354     p_session->sock.p_sys = p_session;
355     p_session->sock.pf_send = gnutls_Send;
356     p_session->sock.pf_recv = gnutls_Recv;
357     p_session->pf_handshake = gnutls_SessionHandshake;
358     p_session->pf_close = gnutls_SessionClose;
359
360     return p_session;
361 }
362
363
364 /*****************************************************************************
365  * tls_ServerDelete:
366  *****************************************************************************
367  * Releases data allocated with tls_ServerCreate.
368  *****************************************************************************/
369 static void
370 gnutls_ServerDelete( tls_server_t *p_server )
371 {
372     gnutls_certificate_free_credentials(
373                                        ((tls_server_sys_t *)(p_server->p_sys))
374                                          ->x509_cred );
375     free( p_server->p_sys );
376     free( p_server );
377 }
378
379
380 /*****************************************************************************
381  * tls_ServerAddCA:
382  *****************************************************************************
383  * Adds one or more certificate authorities.
384  * TODO: we are not able to check the client credentials yet, so this function
385  * is pretty useless.
386  * Returns -1 on error, 0 on success.
387  *****************************************************************************/
388 static int
389 gnutls_ServerAddCA( tls_server_t *p_server, const char *psz_ca_path )
390 {
391     int val;
392
393     val = gnutls_certificate_set_x509_trust_file( ((tls_server_sys_t *)
394                                                   (p_server->p_sys))
395                                                     ->x509_cred,
396                                                   psz_ca_path,
397                                                   GNUTLS_X509_FMT_PEM );
398     if( val < 0 )
399     {
400         msg_Err( p_server->p_tls, "Cannot add trusted CA (%s) : %s",
401                  psz_ca_path, gnutls_strerror( val ) );
402         gnutls_ServerDelete( p_server );
403         return VLC_EGENERIC;
404     }
405     msg_Dbg( p_server->p_tls, " %d trusted CA added (%s)", val,
406              psz_ca_path );
407     return VLC_SUCCESS;
408 }
409
410
411 /*****************************************************************************
412  * tls_ServerAddCRL:
413  *****************************************************************************
414  * Adds a certificates revocation list to be sent to TLS clients.
415  * Returns -1 on error, 0 on success.
416  *****************************************************************************/
417 static int
418 gnutls_ServerAddCRL( tls_server_t *p_server, const char *psz_crl_path )
419 {
420     int val;
421
422     val = gnutls_certificate_set_x509_crl_file( ((tls_server_sys_t *)
423                                                 (p_server->p_sys))->x509_cred,
424                                                 psz_crl_path,
425                                                 GNUTLS_X509_FMT_PEM );
426     if( val < 0 )
427     {
428         msg_Err( p_server->p_tls, "Cannot add CRL (%s) : %s",
429                  psz_crl_path, gnutls_strerror( val ) );
430         gnutls_ServerDelete( p_server );
431         return VLC_EGENERIC;
432     }
433     msg_Dbg( p_server->p_tls, "%d CRL added (%s)", val, psz_crl_path );
434     return VLC_SUCCESS;
435 }
436     
437
438 /*****************************************************************************
439  * tls_ServerCreate:
440  *****************************************************************************
441  * Allocates a whole server's TLS credentials.
442  * Returns NULL on error.
443  *****************************************************************************/
444 static tls_server_t *
445 gnutls_ServerCreate( tls_t *p_this, const char *psz_cert_path,
446                   const char *psz_key_path )
447 {
448     tls_server_t *p_server;
449     tls_server_sys_t *p_server_sys;
450     int val;
451
452     msg_Dbg( p_this, "Creating TLS server" );
453
454     p_server_sys = (tls_server_sys_t *)malloc( sizeof(struct tls_server_sys_t) );
455     if( p_server_sys == NULL )
456         return NULL;
457
458     /* Sets server's credentials */
459     val = gnutls_certificate_allocate_credentials( &p_server_sys->x509_cred );
460     if( val != 0 )
461     {
462         msg_Err( p_this, "Cannot allocate X509 credentials : %s",
463                  gnutls_strerror( val ) );
464         free( p_server_sys );
465         return NULL;
466     }
467
468     val = gnutls_certificate_set_x509_key_file( p_server_sys->x509_cred,
469                                                 psz_cert_path, psz_key_path,
470                                                 GNUTLS_X509_FMT_PEM );
471     if( val < 0 )
472     {
473         msg_Err( p_this, "Cannot set certificate chain or private key : %s",
474                  gnutls_strerror( val ) );
475         gnutls_certificate_free_credentials( p_server_sys->x509_cred );
476         free( p_server_sys );
477         return NULL;
478     }
479
480     /* FIXME: regenerate these regularly */
481     val = gnutls_dh_params_init( &p_server_sys->dh_params );
482     if( val >= 0 )
483     {
484         vlc_value_t bits;
485
486         if( var_Get( p_this, "dh-bits", &bits ) != VLC_SUCCESS )
487         {
488             var_Create( p_this, "dh-bits",
489                         VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
490             var_Get( p_this, "dh-bits", &bits );
491         }
492
493         msg_Dbg( p_this, "Computing Diffie Hellman ciphers parameters" );
494         val = gnutls_dh_params_generate2( p_server_sys->dh_params,
495                                           bits.i_int );
496     }
497     if( val < 0 )
498     {
499         msg_Err( p_this, "Cannot initialize DH cipher suites : %s",
500                  gnutls_strerror( val ) );
501         gnutls_certificate_free_credentials( p_server_sys->x509_cred );
502         free( p_server_sys );
503         return NULL;
504     }
505     msg_Dbg( p_this, "Ciphers parameters computed" );
506
507     gnutls_certificate_set_dh_params( p_server_sys->x509_cred,
508                                       p_server_sys->dh_params);
509
510     p_server = (tls_server_t *)malloc( sizeof(struct tls_server_t) );
511     if( p_server == NULL )
512     {
513         free( p_server_sys );
514         return NULL;
515     }
516
517     p_server->p_tls = p_this;
518     p_server->p_sys = p_server_sys;
519     p_server->pf_delete = gnutls_ServerDelete;
520     p_server->pf_add_CA = gnutls_ServerAddCA;
521     p_server->pf_add_CRL = gnutls_ServerAddCRL;
522     p_server->pf_session_prepare = gnutls_ServerSessionPrepare;
523
524     return p_server;
525 }
526
527
528 /*****************************************************************************
529  * gcrypt thread option VLC implementation:
530  *****************************************************************************/
531 vlc_object_t *__p_gcry_data;
532
533 static int gcry_vlc_mutex_init (void **p_sys)
534 {
535     int i_val;
536     vlc_mutex_t *p_lock = (vlc_mutex_t *)malloc (sizeof (vlc_mutex_t));
537
538     if( p_lock == NULL)
539         return ENOMEM;
540
541     i_val = vlc_mutex_init( __p_gcry_data, p_lock);
542     if (i_val)
543         free (p_lock);
544     else
545         *p_sys = p_lock;
546     return i_val;
547 }
548
549 static int gcry_vlc_mutex_destroy (void **p_sys)
550 {
551     int i_val;
552     vlc_mutex_t *p_lock = (vlc_mutex_t *)*p_sys;
553
554     i_val = vlc_mutex_destroy (p_lock);
555     free (p_lock);
556     return i_val;
557 }
558
559 static int gcry_vlc_mutex_lock (void **p_sys)
560 {
561     return vlc_mutex_lock ((vlc_mutex_t *)*p_sys);
562 }
563
564 static int gcry_vlc_mutex_unlock (void **lock)
565 {
566     return vlc_mutex_unlock ((vlc_mutex_t *)*lock);
567 }
568
569 static struct gcry_thread_cbs gcry_threads_vlc =                        
570 {
571     GCRY_THREAD_OPTION_USER,
572     NULL,
573     gcry_vlc_mutex_init,
574     gcry_vlc_mutex_destroy,
575     gcry_vlc_mutex_lock,
576     gcry_vlc_mutex_unlock
577 };
578
579
580 /*****************************************************************************
581  * Module initialization
582  *****************************************************************************/
583 static int
584 Open( vlc_object_t *p_this )
585 {
586     tls_t *p_tls = (tls_t *)p_this;
587
588     vlc_value_t lock, count;
589
590     var_Create( p_this->p_libvlc, "tls_mutex", VLC_VAR_MUTEX );
591     var_Get( p_this->p_libvlc, "tls_mutex", &lock );
592     vlc_mutex_lock( lock.p_address );
593
594     /* Initialize GnuTLS only once */
595     var_Create( p_this->p_libvlc, "gnutls_count", VLC_VAR_INTEGER );
596     var_Get( p_this->p_libvlc, "gnutls_count", &count);
597
598     if( count.i_int == 0)
599     {
600         __p_gcry_data = VLC_OBJECT( p_this->p_vlc );
601
602         gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_vlc);
603         if( gnutls_global_init( ) )
604         {
605             msg_Warn( p_this, "cannot initialize GNUTLS" );
606             vlc_mutex_unlock( lock.p_address );
607             return VLC_EGENERIC;
608         }
609         if( gnutls_check_version( "1.0.0" ) == NULL )
610         {
611             gnutls_global_deinit( );
612             vlc_mutex_unlock( lock.p_address );
613             msg_Err( p_this, "unsupported GNUTLS version" );
614             return VLC_EGENERIC;
615         }
616         msg_Dbg( p_this, "GNUTLS initialized" );
617     }
618
619     count.i_int++;
620     var_Set( p_this->p_libvlc, "gnutls_count", count);
621     vlc_mutex_unlock( lock.p_address );
622
623     p_tls->pf_server_create = gnutls_ServerCreate;
624     p_tls->pf_client_create = gnutls_ClientCreate;
625     return VLC_SUCCESS;
626 }
627
628
629 /*****************************************************************************
630  * Module deinitialization
631  *****************************************************************************/
632 static void
633 Close( vlc_object_t *p_this )
634 {
635     /*tls_t *p_tls = (tls_t *)p_this;
636     tls_sys_t *p_sys = (tls_sys_t *)(p_this->p_sys);*/
637
638     vlc_value_t lock, count;
639
640     var_Create( p_this->p_libvlc, "gnutls_mutex", VLC_VAR_MUTEX );
641     var_Get( p_this->p_libvlc, "gnutls_mutex", &lock );
642     vlc_mutex_lock( lock.p_address );
643
644     var_Create( p_this->p_libvlc, "gnutls_count", VLC_VAR_INTEGER );
645     var_Get( p_this->p_libvlc, "gnutls_count", &count);
646     count.i_int--;
647     var_Set( p_this->p_libvlc, "gnutls_count", count);
648
649     if( count.i_int == 0 )
650     {
651         gnutls_global_deinit( );
652         msg_Dbg( p_this, "GNUTLS deinitialized" );
653     }
654
655     vlc_mutex_unlock( lock.p_address);
656 }