]> git.sesse.net Git - vlc/blob - modules/misc/gnutls.c
gnutls.c: add shortname
[vlc] / modules / misc / gnutls.c
1 /*****************************************************************************
2  * tls.c
3  *****************************************************************************
4  * Copyright (C) 2004-2005 VideoLAN
5  * $Id$
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 server cert validation (?).
28  */
29
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <vlc/vlc.h>
37
38 #include "vlc_tls.h"
39
40 #include <gcrypt.h>
41 #include <gnutls/gnutls.h>
42
43 #define DH_BITS             1024
44 #define CACHE_EXPIRATION    3600
45 #define CACHE_SIZE          64
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50 static int  Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
52
53 #define DH_BITS_TEXT N_("Diffie-Hellman prime bits")
54 #define DH_BITS_LONGTEXT N_( \
55     "Allows you to modify the Diffie-Hellman prime's number of bits " \
56     "(used for TLS or SSL-based server-side encryption)." )
57
58 #define CACHE_EXPIRATION_TEXT N_("Expiration time for resumed TLS sessions")
59 #define CACHE_EXPIRATION_LONGTEXT N_( \
60     "Defines the delay before resumed TLS sessions will be expired " \
61     "(in seconds)." )
62
63 #define CACHE_SIZE_TEXT N_("Number of resumed TLS sessions")
64 #define CACHE_SIZE_LONGTEXT N_( \
65     "Allows you to modify the maximum number of resumed TLS sessions that " \
66     "the cache will hold." )
67
68 #define CHECK_CERT_TEXT N_("Check TLS/SSL server certificate validity")
69 #define CHECK_CERT_LONGTEXT N_( \
70     "Ensures that server certificate is valid " \
71     "(ie. signed by an approved Certificate Authority)." )
72
73 #define CHECK_HOSTNAME_TEXT N_("Check TLS/SSL server hostname in certificate")
74 #define CHECK_HOSTNAME_LONGTEXT N_( \
75     "Ensures that server hostname in certificate match requested host name." )
76
77 vlc_module_begin();
78     set_shortname( "GnuTLS" );
79     set_description( _("GnuTLS TLS encryption layer") );
80     set_capability( "tls", 1 );
81     set_callbacks( Open, Close );
82     set_category( CAT_ADVANCED );
83     set_subcategory( SUBCAT_ADVANCED_MISC );
84
85 #if 0
86     add_bool( "tls-check-cert", VLC_FALSE, NULL, CHECK_CERT_TEXT,
87               CHECK_CERT_LONGTEXT, VLC_FALSE );
88     add_bool( "tls-check-hostname", VLC_FALSE, NULL, CHECK_HOSTNAME_TEXT,
89               CHECK_HOSTNAME_LONGTEXT, VLC_FALSE );
90 #endif
91
92     add_integer( "dh-bits", DH_BITS, NULL, DH_BITS_TEXT,
93                  DH_BITS_LONGTEXT, VLC_TRUE );
94     add_integer( "tls-cache-expiration", CACHE_EXPIRATION, NULL,
95                  CACHE_EXPIRATION_TEXT, CACHE_EXPIRATION_LONGTEXT, VLC_TRUE );
96     add_integer( "tls-cache-size", CACHE_SIZE, NULL, CACHE_SIZE_TEXT,
97                  CACHE_SIZE_LONGTEXT, VLC_TRUE );
98 vlc_module_end();
99
100
101 #define MAX_SESSION_ID    32
102 #define MAX_SESSION_DATA  1024
103
104 typedef struct saved_session_t
105 {
106     char id[MAX_SESSION_ID];
107     char data[MAX_SESSION_DATA];
108
109     unsigned i_idlen;
110     unsigned i_datalen;
111 } saved_session_t;
112
113
114 typedef struct tls_server_sys_t
115 {
116     gnutls_certificate_credentials  x509_cred;
117     gnutls_dh_params                dh_params;
118
119     struct saved_session_t          *p_cache;
120     struct saved_session_t          *p_store;
121     int                             i_cache_size;
122     vlc_mutex_t                     cache_lock;
123
124     int                             (*pf_handshake2)( tls_session_t * );
125 } tls_server_sys_t;
126
127
128 typedef struct tls_session_sys_t
129 {
130     gnutls_session  session;
131     vlc_bool_t      b_handshaked;
132 } tls_session_sys_t;
133
134
135 typedef struct tls_client_sys_t
136 {
137     struct tls_session_sys_t       session;
138     gnutls_certificate_credentials x509_cred;
139 } tls_client_sys_t;
140
141
142 static int
143 _get_Int (vlc_object_t *p_this, const char *var)
144 {
145     vlc_value_t value;
146
147     if( var_Get( p_this, var, &value ) != VLC_SUCCESS )
148     {
149         var_Create( p_this, var, VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
150         var_Get( p_this, var, &value );
151     }
152
153     return value.i_int;
154 }
155
156 #define get_Int( a, b ) _get_Int( (vlc_object_t *)(a), (b) )
157
158
159 /*****************************************************************************
160  * tls_Send:
161  *****************************************************************************
162  * Sends data through a TLS session.
163  *****************************************************************************/
164 static int
165 gnutls_Send( void *p_session, const void *buf, int i_length )
166 {
167     int val;
168     tls_session_sys_t *p_sys;
169
170     p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
171
172     val = gnutls_record_send( p_sys->session, buf, i_length );
173     /* TODO: handle fatal error */
174     return val < 0 ? -1 : val;
175 }
176
177
178 /*****************************************************************************
179  * tls_Recv:
180  *****************************************************************************
181  * Receives data through a TLS session.
182  *****************************************************************************/
183 static int
184 gnutls_Recv( void *p_session, void *buf, int i_length )
185 {
186     int val;
187     tls_session_sys_t *p_sys;
188
189     p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
190
191     val = gnutls_record_recv( p_sys->session, buf, i_length );
192     /* TODO: handle fatal error */
193     return val < 0 ? -1 : val;
194 }
195
196
197 /*****************************************************************************
198  * tls_Session(Continue)?Handshake:
199  *****************************************************************************
200  * Establishes TLS session with a peer through socket <fd>.
201  * Returns -1 on error (you need not and must not call tls_SessionClose)
202  * 0 on succesful handshake completion, 1 if more would-be blocking recv is
203  * needed, 2 if more would-be blocking send is required.
204  *****************************************************************************/
205 static int
206 gnutls_ContinueHandshake( tls_session_t *p_session)
207 {
208     tls_session_sys_t *p_sys;
209     int val;
210
211     p_sys = (tls_session_sys_t *)(p_session->p_sys);
212
213      /* TODO: handle fatal error */
214     val = gnutls_handshake( p_sys->session );
215     if( ( val == GNUTLS_E_AGAIN ) || ( val == GNUTLS_E_INTERRUPTED ) )
216         return 1 + gnutls_record_get_direction( p_sys->session );
217
218     if( val < 0 )
219     {
220         msg_Err( p_session, "TLS handshake failed : %s",
221                  gnutls_strerror( val ) );
222         p_session->pf_close( p_session );
223         return -1;
224     }
225
226     p_sys->b_handshaked = VLC_TRUE;
227     return 0;
228 }
229
230 static int
231 gnutls_HandshakeAndValidate( tls_session_t *p_session )
232 {
233     int val;
234
235     val = gnutls_ContinueHandshake( p_session );
236     if( val == 0 )
237     {
238         int status;
239
240         val = gnutls_certificate_verify_peers2( ((tls_session_sys_t *)
241                                                 (p_session->p_sys))->session,
242                                                 &status );
243
244         if( val )
245         {
246             msg_Err( p_session, "TLS certificate verification failed : %s",
247                      gnutls_strerror( val ) );
248             p_session->pf_close( p_session );
249             return -1;
250         }
251
252         if( status )
253         {
254             msg_Warn( p_session, "TLS session : access denied" );
255             if( status & GNUTLS_CERT_INVALID )
256                 msg_Dbg( p_session, "certificate could not be verified" );
257             if( status & GNUTLS_CERT_REVOKED )
258                 msg_Dbg( p_session, "certificate was revoked" );
259             if( status & GNUTLS_CERT_SIGNER_NOT_FOUND )
260                 msg_Dbg( p_session, "certificate's signer was not found" );
261             if( status & GNUTLS_CERT_SIGNER_NOT_CA )
262                 msg_Dbg( p_session, "certificate's signer is not a CA" );
263             p_session->pf_close( p_session );
264             return -1;
265         }
266
267         msg_Dbg( p_session, "TLS certificate verified" );
268         return 0;
269     }
270
271     return val;
272 }
273
274 static int
275 gnutls_BeginHandshake( tls_session_t *p_session, int fd,
276                          const char *psz_hostname )
277 {
278     tls_session_sys_t *p_sys;
279
280     p_sys = (tls_session_sys_t *)(p_session->p_sys);
281
282     gnutls_transport_set_ptr (p_sys->session, (gnutls_transport_ptr)fd);
283     if( psz_hostname != NULL )
284         gnutls_server_name_set( p_sys->session, GNUTLS_NAME_DNS, psz_hostname,
285                                 strlen( psz_hostname ) );
286
287     return p_session->pf_handshake2( p_session );
288 }
289
290 /*****************************************************************************
291  * tls_SessionClose:
292  *****************************************************************************
293  * Terminates TLS session and releases session data.
294  *****************************************************************************/
295 static void
296 gnutls_SessionClose( tls_session_t *p_session )
297 {
298     tls_session_sys_t *p_sys;
299
300     p_sys = (tls_session_sys_t *)(p_session->p_sys);
301
302     if( p_sys->b_handshaked == VLC_TRUE )
303         gnutls_bye( p_sys->session, GNUTLS_SHUT_WR );
304     gnutls_deinit( p_sys->session );
305
306     vlc_object_detach( p_session );
307     vlc_object_destroy( p_session );
308
309     free( p_sys );
310 }
311
312 static void
313 gnutls_ClientDelete( tls_session_t *p_session )
314 {
315     /* On the client-side, credentials are re-allocated per session */
316     gnutls_certificate_free_credentials( ((tls_client_sys_t *)
317                                           (p_session->p_sys))->x509_cred );
318     gnutls_SessionClose( p_session );
319 }
320
321
322 /*****************************************************************************
323  * tls_ClientCreate:
324  *****************************************************************************
325  * Initializes client-side TLS session data.
326  *****************************************************************************/
327 static tls_session_t *
328 gnutls_ClientCreate( tls_t *p_tls )
329 {
330     tls_session_t *p_session = NULL;
331     tls_client_sys_t *p_sys = NULL;
332     int i_val;
333     const int cert_type_priority[3] =
334     {
335         GNUTLS_CRT_X509,
336         0
337     };
338
339     p_sys = (tls_client_sys_t *)malloc( sizeof(struct tls_client_sys_t) );
340     if( p_sys == NULL )
341         return NULL;
342    
343     p_session = (struct tls_session_t *)vlc_object_create ( p_tls, sizeof(struct tls_session_t) );
344     if( p_session == NULL )
345     {
346         free( p_sys );
347         return NULL;
348     }
349
350     p_session->p_sys = p_sys;
351     p_session->sock.p_sys = p_session;
352     p_session->sock.pf_send = gnutls_Send;
353     p_session->sock.pf_recv = gnutls_Recv;
354     p_session->pf_handshake = gnutls_BeginHandshake;
355     p_session->pf_handshake2 = gnutls_ContinueHandshake;
356     p_session->pf_close = gnutls_ClientDelete;
357
358     p_sys->session.b_handshaked = VLC_FALSE;
359
360     vlc_object_attach( p_session, p_tls );
361
362     i_val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
363     if( i_val != 0 )
364     {
365         msg_Err( p_tls, "Cannot allocate X509 credentials : %s",
366                  gnutls_strerror( i_val ) );
367         goto error;
368     }
369
370 #if 0
371     if( psz_ca_path != NULL )
372     {
373         i_val = gnutls_certificate_set_x509_trust_file( p_sys->x509_cred,
374                                                         psz_ca_path,
375                                                         GNUTLS_X509_FMT_PEM );
376         if( i_val != 0 )
377         {
378             msg_Err( p_tls, "Cannot add trusted CA (%s) : %s", psz_ca_path,
379                      gnutls_strerror( i_val ) );
380             gnutls_certificate_free_credentials( p_sys->x509_cred );
381             goto error;
382         }
383     }
384 #endif
385     i_val = gnutls_init( &p_sys->session.session, GNUTLS_CLIENT );
386     if( i_val != 0 )
387     {
388         msg_Err( p_tls, "Cannot initialize TLS session : %s",
389                  gnutls_strerror( i_val ) );
390         gnutls_certificate_free_credentials( p_sys->x509_cred );
391         goto error;
392     }
393
394     i_val = gnutls_set_default_priority( p_sys->session.session );
395     if( i_val < 0 )
396     {
397         msg_Err( p_tls, "Cannot set ciphers priorities : %s",
398                  gnutls_strerror( i_val ) );
399         gnutls_deinit( p_sys->session.session );
400         gnutls_certificate_free_credentials( p_sys->x509_cred );
401         goto error;
402     }
403
404     i_val = gnutls_certificate_type_set_priority( p_sys->session.session,
405                                                   cert_type_priority );
406     if( i_val < 0 )
407     {
408         msg_Err( p_tls, "Cannot set certificate type priorities : %s",
409                  gnutls_strerror( i_val ) );
410         gnutls_deinit( p_sys->session.session );
411         gnutls_certificate_free_credentials( p_sys->x509_cred );
412         goto error;
413     }
414
415     i_val = gnutls_credentials_set( p_sys->session.session,
416                                     GNUTLS_CRD_CERTIFICATE,
417                                     p_sys->x509_cred );
418     if( i_val < 0 )
419     {
420         msg_Err( p_tls, "Cannot set TLS session credentials : %s",
421                  gnutls_strerror( i_val ) );
422         gnutls_deinit( p_sys->session.session );
423         gnutls_certificate_free_credentials( p_sys->x509_cred );
424         goto error;
425     }
426
427     return p_session;
428
429 error:
430     vlc_object_detach( p_session );
431     vlc_object_destroy( p_session );
432     free( p_sys );
433
434     return NULL;
435 }
436
437
438 /*****************************************************************************
439  * TLS session resumption callbacks
440  *****************************************************************************/
441 static int cb_store( void *p_server, gnutls_datum key, gnutls_datum data )
442 {
443     tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
444
445     if( ( p_sys->i_cache_size == 0 )
446      || ( key.size > MAX_SESSION_ID )
447      || ( data.size > MAX_SESSION_DATA ) )
448         return -1;
449
450     vlc_mutex_lock( &p_sys->cache_lock );
451
452     memcpy( p_sys->p_store->id, key.data, key.size);
453     memcpy( p_sys->p_store->data, data.data, data.size );
454     p_sys->p_store->i_idlen = key.size;
455     p_sys->p_store->i_datalen = data.size;
456
457     p_sys->p_store++;
458     if( ( p_sys->p_store - p_sys->p_cache ) == p_sys->i_cache_size )
459         p_sys->p_store = p_sys->p_cache;
460
461     vlc_mutex_unlock( &p_sys->cache_lock );
462
463     return 0;
464 }
465
466
467 static const gnutls_datum err_datum = { NULL, 0 };
468
469 static gnutls_datum cb_fetch( void *p_server, gnutls_datum key )
470 {
471     tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
472     saved_session_t *p_session, *p_end;
473
474     p_session = p_sys->p_cache;
475     p_end = p_session + p_sys->i_cache_size;
476
477     vlc_mutex_lock( &p_sys->cache_lock );
478
479     while( p_session < p_end )
480     {
481         if( ( p_session->i_idlen == key.size )
482          && !memcmp( p_session->id, key.data, key.size ) )
483         {
484             gnutls_datum data;
485
486             data.size = p_session->i_datalen;
487
488             data.data = gnutls_malloc( data.size );
489             if( data.data == NULL )
490             {
491                 vlc_mutex_unlock( &p_sys->cache_lock );
492                 return err_datum;
493             }
494
495             memcpy( data.data, p_session->data, data.size );
496             vlc_mutex_unlock( &p_sys->cache_lock );
497             return data;
498         }
499         p_session++;
500     }
501
502     vlc_mutex_unlock( &p_sys->cache_lock );
503
504     return err_datum;
505 }
506
507
508 static int cb_delete( void *p_server, gnutls_datum key )
509 {
510     tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
511     saved_session_t *p_session, *p_end;
512
513     p_session = p_sys->p_cache;
514     p_end = p_session + p_sys->i_cache_size;
515
516     vlc_mutex_lock( &p_sys->cache_lock );
517
518     while( p_session < p_end )
519     {
520         if( ( p_session->i_idlen == key.size )
521          && !memcmp( p_session->id, key.data, key.size ) )
522         {
523             p_session->i_datalen = p_session->i_idlen = 0;
524             vlc_mutex_unlock( &p_sys->cache_lock );
525             return 0;
526         }
527         p_session++;
528     }
529
530     vlc_mutex_unlock( &p_sys->cache_lock );
531
532     return -1;
533 }
534
535
536 /*****************************************************************************
537  * tls_ServerSessionPrepare:
538  *****************************************************************************
539  * Initializes server-side TLS session data.
540  *****************************************************************************/
541 static tls_session_t *
542 gnutls_ServerSessionPrepare( tls_server_t *p_server )
543 {
544     tls_session_t *p_session;
545     gnutls_session session;
546     int i_val;
547
548     p_session = vlc_object_create( p_server, sizeof (struct tls_session_t) );
549     if( p_session == NULL )
550         return NULL;
551     
552     p_session->p_sys = malloc( sizeof(struct tls_session_sys_t) );
553     if( p_session->p_sys == NULL )
554     {
555         vlc_object_destroy( p_session );
556         return NULL;
557     }
558
559     vlc_object_attach( p_session, p_server );
560
561     p_session->sock.p_sys = p_session;
562     p_session->sock.pf_send = gnutls_Send;
563     p_session->sock.pf_recv = gnutls_Recv;
564     p_session->pf_handshake = gnutls_BeginHandshake;
565     p_session->pf_handshake2 = ((tls_server_sys_t *)
566                                (p_server->p_sys))->pf_handshake2;
567     p_session->pf_close = gnutls_SessionClose;
568
569     ((tls_session_sys_t *)p_session->p_sys)->b_handshaked = VLC_FALSE;
570
571     i_val = gnutls_init( &session, GNUTLS_SERVER );
572     if( i_val != 0 )
573     {
574         msg_Err( p_server, "Cannot initialize TLS session : %s",
575                  gnutls_strerror( i_val ) );
576         goto error;
577     }
578
579     ((tls_session_sys_t *)p_session->p_sys)->session = session;
580
581     i_val = gnutls_set_default_priority( session );
582     if( i_val < 0 )
583     {
584         msg_Err( p_server, "Cannot set ciphers priorities : %s",
585                  gnutls_strerror( i_val ) );
586         gnutls_deinit( session );
587         goto error;
588     }
589
590     i_val = gnutls_credentials_set( session, GNUTLS_CRD_CERTIFICATE,
591                                     ((tls_server_sys_t *)(p_server->p_sys))
592                                     ->x509_cred );
593     if( i_val < 0 )
594     {
595         msg_Err( p_server, "Cannot set TLS session credentials : %s",
596                  gnutls_strerror( i_val ) );
597         gnutls_deinit( session );
598         goto error;
599     }
600
601     /* TODO: support for client authentication */
602     /*gnutls_certificate_server_set_request( p_session->session,
603                                            GNUTLS_CERT_REQUEST ); */
604
605     gnutls_dh_set_prime_bits( session, get_Int( p_server, "dh-bits" ) );
606
607     /* Session resumption support */
608     gnutls_db_set_cache_expiration( session, get_Int( p_server,
609                                     "tls-cache-expiration" ) );
610     gnutls_db_set_retrieve_function( session, cb_fetch );
611     gnutls_db_set_remove_function( session, cb_delete );
612     gnutls_db_set_store_function( session, cb_store );
613     gnutls_db_set_ptr( session, p_server );
614
615     return p_session;
616
617 error:
618     free( p_session->p_sys );
619     vlc_object_detach( p_session );
620     vlc_object_destroy( p_session );
621     return NULL;
622 }
623
624
625 /*****************************************************************************
626  * tls_ServerDelete:
627  *****************************************************************************
628  * Releases data allocated with tls_ServerCreate.
629  *****************************************************************************/
630 static void
631 gnutls_ServerDelete( tls_server_t *p_server )
632 {
633     tls_server_sys_t *p_sys;
634
635     p_sys = (tls_server_sys_t *)p_server->p_sys;
636
637     gnutls_certificate_free_credentials( p_sys->x509_cred );
638     gnutls_dh_params_deinit( p_sys->dh_params );
639     vlc_mutex_destroy( &p_sys->cache_lock );
640
641     vlc_object_detach( p_server );
642     vlc_object_destroy( p_server );
643
644     free( p_sys->p_cache );
645     free( p_sys );
646 }
647
648
649 /*****************************************************************************
650  * tls_ServerAddCA:
651  *****************************************************************************
652  * Adds one or more certificate authorities.
653  * Returns -1 on error, 0 on success.
654  *****************************************************************************/
655 static int
656 gnutls_ServerAddCA( tls_server_t *p_server, const char *psz_ca_path )
657 {
658     int val;
659     tls_server_sys_t *p_sys;
660
661     p_sys = (tls_server_sys_t *)(p_server->p_sys);
662
663     val = gnutls_certificate_set_x509_trust_file( p_sys->x509_cred,
664                                                   psz_ca_path,
665                                                   GNUTLS_X509_FMT_PEM );
666     if( val < 0 )
667     {
668         msg_Err( p_server, "Cannot add trusted CA (%s) : %s", psz_ca_path,
669                  gnutls_strerror( val ) );
670         return VLC_EGENERIC;
671     }
672     msg_Dbg( p_server, " %d trusted CA added (%s)", val, psz_ca_path );
673
674     /* enables peer's certificate verification */
675     p_sys->pf_handshake2 = gnutls_HandshakeAndValidate;
676
677     return VLC_SUCCESS;
678 }
679
680
681 /*****************************************************************************
682  * tls_ServerAddCRL:
683  *****************************************************************************
684  * Adds a certificates revocation list to be sent to TLS clients.
685  * Returns -1 on error, 0 on success.
686  *****************************************************************************/
687 static int
688 gnutls_ServerAddCRL( tls_server_t *p_server, const char *psz_crl_path )
689 {
690     int val;
691
692     val = gnutls_certificate_set_x509_crl_file( ((tls_server_sys_t *)
693                                                 (p_server->p_sys))->x509_cred,
694                                                 psz_crl_path,
695                                                 GNUTLS_X509_FMT_PEM );
696     if( val < 0 )
697     {
698         msg_Err( p_server, "Cannot add CRL (%s) : %s", psz_crl_path,
699                  gnutls_strerror( val ) );
700         return VLC_EGENERIC;
701     }
702     msg_Dbg( p_server, "%d CRL added (%s)", val, psz_crl_path );
703     return VLC_SUCCESS;
704 }
705     
706
707 /*****************************************************************************
708  * tls_ServerCreate:
709  *****************************************************************************
710  * Allocates a whole server's TLS credentials.
711  * Returns NULL on error.
712  *****************************************************************************/
713 static tls_server_t *
714 gnutls_ServerCreate( tls_t *p_tls, const char *psz_cert_path,
715                      const char *psz_key_path )
716 {
717     tls_server_t *p_server;
718     tls_server_sys_t *p_sys;
719     int val;
720
721     msg_Dbg( p_tls, "Creating TLS server" );
722
723     p_sys = (tls_server_sys_t *)malloc( sizeof(struct tls_server_sys_t) );
724     if( p_sys == NULL )
725         return NULL;
726
727     p_sys->i_cache_size = get_Int( p_tls, "tls-cache-size" );
728     p_sys->p_cache = (struct saved_session_t *)calloc( p_sys->i_cache_size,
729                                            sizeof( struct saved_session_t ) );
730     if( p_sys->p_cache == NULL )
731     {
732         free( p_sys );
733         return NULL;
734     }
735     p_sys->p_store = p_sys->p_cache;
736
737     p_server = vlc_object_create( p_tls, sizeof(struct tls_server_t) );
738     if( p_server == NULL )
739     {
740         free( p_sys->p_cache );
741         free( p_sys );
742         return NULL;
743     }
744
745     vlc_object_attach( p_server, p_tls );
746
747     p_server->p_sys = p_sys;
748     p_server->pf_delete = gnutls_ServerDelete;
749     p_server->pf_add_CA = gnutls_ServerAddCA;
750     p_server->pf_add_CRL = gnutls_ServerAddCRL;
751     p_server->pf_session_prepare = gnutls_ServerSessionPrepare;
752
753     /* No certificate validation by default */
754     p_sys->pf_handshake2 = gnutls_ContinueHandshake;
755
756     /* FIXME: check for errors */
757     vlc_mutex_init( p_server, &p_sys->cache_lock );
758
759     /* Sets server's credentials */
760     val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
761     if( val != 0 )
762     {
763         msg_Err( p_server, "Cannot allocate X509 credentials : %s",
764                  gnutls_strerror( val ) );
765         goto error;
766     }
767
768     val = gnutls_certificate_set_x509_key_file( p_sys->x509_cred,
769                                                 psz_cert_path, psz_key_path,
770                                                 GNUTLS_X509_FMT_PEM );
771     if( val < 0 )
772     {
773         msg_Err( p_server, "Cannot set certificate chain or private key : %s",
774                  gnutls_strerror( val ) );
775         gnutls_certificate_free_credentials( p_sys->x509_cred );
776         goto error;
777     }
778
779     /* FIXME:
780      * - regenerate these regularly
781      * - support other ciper suites
782      */
783     val = gnutls_dh_params_init( &p_sys->dh_params );
784     if( val >= 0 )
785     {
786         msg_Dbg( p_server, "Computing Diffie Hellman ciphers parameters" );
787         val = gnutls_dh_params_generate2( p_sys->dh_params,
788                                           get_Int( p_tls, "dh-bits" ) );
789     }
790     if( val < 0 )
791     {
792         msg_Err( p_server, "Cannot initialize DH cipher suites : %s",
793                  gnutls_strerror( val ) );
794         gnutls_certificate_free_credentials( p_sys->x509_cred );
795         goto error;
796     }
797     msg_Dbg( p_server, "Ciphers parameters computed" );
798
799     gnutls_certificate_set_dh_params( p_sys->x509_cred, p_sys->dh_params);
800
801     return p_server;
802
803 error:
804     vlc_mutex_destroy( &p_sys->cache_lock );
805     vlc_object_detach( p_server );
806     vlc_object_destroy( p_server );
807     free( p_sys );
808     return NULL;
809 }
810
811
812 /*****************************************************************************
813  * gcrypt thread option VLC implementation:
814  *****************************************************************************/
815 vlc_object_t *__p_gcry_data;
816
817 static int gcry_vlc_mutex_init( void **p_sys )
818 {
819     int i_val;
820     vlc_mutex_t *p_lock = (vlc_mutex_t *)malloc( sizeof( vlc_mutex_t ) );
821
822     if( p_lock == NULL)
823         return ENOMEM;
824
825     i_val = vlc_mutex_init( __p_gcry_data, p_lock );
826     if( i_val )
827         free( p_lock );
828     else
829         *p_sys = p_lock;
830     return i_val;
831 }
832
833 static int gcry_vlc_mutex_destroy( void **p_sys )
834 {
835     int i_val;
836     vlc_mutex_t *p_lock = (vlc_mutex_t *)*p_sys;
837
838     i_val = vlc_mutex_destroy( p_lock );
839     free( p_lock );
840     return i_val;
841 }
842
843 static int gcry_vlc_mutex_lock( void **p_sys )
844 {
845     return vlc_mutex_lock( (vlc_mutex_t *)*p_sys );
846 }
847
848 static int gcry_vlc_mutex_unlock( void **lock )
849 {
850     return vlc_mutex_unlock( (vlc_mutex_t *)*lock );
851 }
852
853 static struct gcry_thread_cbs gcry_threads_vlc =
854 {
855     GCRY_THREAD_OPTION_USER,
856     NULL,
857     gcry_vlc_mutex_init,
858     gcry_vlc_mutex_destroy,
859     gcry_vlc_mutex_lock,
860     gcry_vlc_mutex_unlock
861 };
862
863
864 /*****************************************************************************
865  * Module initialization
866  *****************************************************************************/
867 static int
868 Open( vlc_object_t *p_this )
869 {
870     tls_t *p_tls = (tls_t *)p_this;
871
872     vlc_value_t lock, count;
873
874     var_Create( p_this->p_libvlc, "gnutls_mutex", VLC_VAR_MUTEX );
875     var_Get( p_this->p_libvlc, "gnutls_mutex", &lock );
876     vlc_mutex_lock( lock.p_address );
877
878     /* Initialize GnuTLS only once */
879     var_Create( p_this->p_libvlc, "gnutls_count", VLC_VAR_INTEGER );
880     var_Get( p_this->p_libvlc, "gnutls_count", &count);
881
882     if( count.i_int == 0)
883     {
884         const char *psz_version;
885
886         __p_gcry_data = VLC_OBJECT( p_this->p_vlc );
887
888         gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_vlc);
889         if( gnutls_global_init( ) )
890         {
891             msg_Warn( p_this, "cannot initialize GnuTLS" );
892             vlc_mutex_unlock( lock.p_address );
893             return VLC_EGENERIC;
894         }
895         /*
896          * FIXME: in fact, we currently depends on 1.0.17, but it breaks on
897          * Debian which as a patched 1.0.16 (which we can use).
898          */
899         psz_version = gnutls_check_version( "1.0.16" );
900         if( psz_version == NULL )
901         {
902             gnutls_global_deinit( );
903             vlc_mutex_unlock( lock.p_address );
904             msg_Err( p_this, "unsupported GnuTLS version" );
905             return VLC_EGENERIC;
906         }
907         msg_Dbg( p_this, "GnuTLS v%s initialized", psz_version );
908     }
909
910     count.i_int++;
911     var_Set( p_this->p_libvlc, "gnutls_count", count);
912     vlc_mutex_unlock( lock.p_address );
913
914     p_tls->pf_server_create = gnutls_ServerCreate;
915     p_tls->pf_client_create = gnutls_ClientCreate;
916     return VLC_SUCCESS;
917 }
918
919
920 /*****************************************************************************
921  * Module deinitialization
922  *****************************************************************************/
923 static void
924 Close( vlc_object_t *p_this )
925 {
926     /*tls_t *p_tls = (tls_t *)p_this;
927     tls_sys_t *p_sys = (tls_sys_t *)(p_this->p_sys);*/
928
929     vlc_value_t lock, count;
930
931     var_Create( p_this->p_libvlc, "gnutls_mutex", VLC_VAR_MUTEX );
932     var_Get( p_this->p_libvlc, "gnutls_mutex", &lock );
933     vlc_mutex_lock( lock.p_address );
934
935     var_Create( p_this->p_libvlc, "gnutls_count", VLC_VAR_INTEGER );
936     var_Get( p_this->p_libvlc, "gnutls_count", &count);
937     count.i_int--;
938     var_Set( p_this->p_libvlc, "gnutls_count", count);
939
940     if( count.i_int == 0 )
941     {
942         gnutls_global_deinit( );
943         msg_Dbg( p_this, "GnuTLS deinitialized" );
944     }
945
946     vlc_mutex_unlock( lock.p_address );
947 }