1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2006 Rémi Denis-Courmont
7 * Authors: Rémi Denis-Courmont <rem # videolan.org>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
30 /*****************************************************************************
32 *****************************************************************************/
37 #include <sys/types.h>
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
54 #include <gnutls/gnutls.h>
55 #include <gnutls/x509.h>
58 #define CACHE_EXPIRATION 3600
61 /*****************************************************************************
63 *****************************************************************************/
64 static int Open ( vlc_object_t * );
65 static void Close( vlc_object_t * );
67 #define DH_BITS_TEXT N_("Diffie-Hellman prime bits")
68 #define DH_BITS_LONGTEXT N_( \
69 "Allows you to modify the Diffie-Hellman prime's number of bits " \
70 "(used for TLS or SSL-based server-side encryption)." )
72 #define CACHE_EXPIRATION_TEXT N_("Expiration time for resumed TLS sessions")
73 #define CACHE_EXPIRATION_LONGTEXT N_( \
74 "Defines the delay before resumed TLS sessions will be expired " \
77 #define CACHE_SIZE_TEXT N_("Number of resumed TLS sessions")
78 #define CACHE_SIZE_LONGTEXT N_( \
79 "Allows you to modify the maximum number of resumed TLS sessions that " \
80 "the cache will hold." )
82 #define CHECK_CERT_TEXT N_("Check TLS/SSL server certificate validity")
83 #define CHECK_CERT_LONGTEXT N_( \
84 "Ensures that server certificate is valid " \
85 "(i.e. signed by an approved Certificate Authority)." )
87 #define CHECK_HOSTNAME_TEXT N_("Check TLS/SSL server hostname in certificate")
88 #define CHECK_HOSTNAME_LONGTEXT N_( \
89 "Ensures that server hostname in certificate match requested host name." )
92 set_shortname( "GnuTLS" );
93 set_description( _("GnuTLS TLS encryption layer") );
94 set_capability( "tls", 1 );
95 set_callbacks( Open, Close );
96 set_category( CAT_ADVANCED );
97 set_subcategory( SUBCAT_ADVANCED_MISC );
99 add_bool( "tls-check-cert", VLC_FALSE, NULL, CHECK_CERT_TEXT,
100 CHECK_CERT_LONGTEXT, VLC_FALSE );
101 add_bool( "tls-check-hostname", VLC_FALSE, NULL, CHECK_HOSTNAME_TEXT,
102 CHECK_HOSTNAME_LONGTEXT, VLC_FALSE );
104 add_integer( "dh-bits", DH_BITS, NULL, DH_BITS_TEXT,
105 DH_BITS_LONGTEXT, VLC_TRUE );
106 add_integer( "tls-cache-expiration", CACHE_EXPIRATION, NULL,
107 CACHE_EXPIRATION_TEXT, CACHE_EXPIRATION_LONGTEXT, VLC_TRUE );
108 add_integer( "tls-cache-size", CACHE_SIZE, NULL, CACHE_SIZE_TEXT,
109 CACHE_SIZE_LONGTEXT, VLC_TRUE );
113 #define MAX_SESSION_ID 32
114 #define MAX_SESSION_DATA 1024
116 typedef struct saved_session_t
118 char id[MAX_SESSION_ID];
119 char data[MAX_SESSION_DATA];
126 typedef struct tls_server_sys_t
128 gnutls_certificate_credentials x509_cred;
129 gnutls_dh_params dh_params;
131 struct saved_session_t *p_cache;
132 struct saved_session_t *p_store;
134 vlc_mutex_t cache_lock;
136 int (*pf_handshake2)( tls_session_t * );
140 typedef struct tls_session_sys_t
142 gnutls_session session;
144 vlc_bool_t b_handshaked;
148 typedef struct tls_client_sys_t
150 struct tls_session_sys_t session;
151 gnutls_certificate_credentials x509_cred;
156 _get_Int( vlc_object_t *p_this, const char *var )
160 if( var_Get( p_this, var, &value ) != VLC_SUCCESS )
162 var_Create( p_this, var, VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
163 var_Get( p_this, var, &value );
170 _get_Bool( vlc_object_t *p_this, const char *var )
174 if( var_Get( p_this, var, &value ) != VLC_SUCCESS )
176 var_Create( p_this, var, VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
177 var_Get( p_this, var, &value );
183 #define get_Int( a, b ) _get_Int( (vlc_object_t *)(a), (b) )
184 #define get_Bool( a, b ) _get_Bool( (vlc_object_t *)(a), (b) )
187 /*****************************************************************************
189 *****************************************************************************
190 * Sends data through a TLS session.
191 *****************************************************************************/
193 gnutls_Send( void *p_session, const void *buf, int i_length )
196 tls_session_sys_t *p_sys;
198 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
200 val = gnutls_record_send( p_sys->session, buf, i_length );
201 /* TODO: handle fatal error */
202 return val < 0 ? -1 : val;
206 /*****************************************************************************
208 *****************************************************************************
209 * Receives data through a TLS session.
210 *****************************************************************************/
212 gnutls_Recv( void *p_session, void *buf, int i_length )
215 tls_session_sys_t *p_sys;
217 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
219 val = gnutls_record_recv( p_sys->session, buf, i_length );
220 /* TODO: handle fatal error */
221 return val < 0 ? -1 : val;
225 /*****************************************************************************
226 * tls_Session(Continue)?Handshake:
227 *****************************************************************************
228 * Establishes TLS session with a peer through socket <fd>.
229 * Returns -1 on error (you need not and must not call tls_SessionClose)
230 * 0 on succesful handshake completion, 1 if more would-be blocking recv is
231 * needed, 2 if more would-be blocking send is required.
232 *****************************************************************************/
234 gnutls_ContinueHandshake( tls_session_t *p_session)
236 tls_session_sys_t *p_sys;
239 p_sys = (tls_session_sys_t *)(p_session->p_sys);
241 /* TODO: handle fatal error */
242 val = gnutls_handshake( p_sys->session );
243 if( ( val == GNUTLS_E_AGAIN ) || ( val == GNUTLS_E_INTERRUPTED ) )
244 return 1 + gnutls_record_get_direction( p_sys->session );
248 msg_Err( p_session, "TLS handshake failed : %s",
249 gnutls_strerror( val ) );
250 p_session->pf_close( p_session );
254 p_sys->b_handshaked = VLC_TRUE;
259 gnutls_HandshakeAndValidate( tls_session_t *p_session )
263 val = gnutls_ContinueHandshake( p_session );
267 gnutls_x509_crt cert;
268 const gnutls_datum *p_data;
269 tls_session_sys_t *p_sys;
271 p_sys = (tls_session_sys_t *)(p_session->p_sys);
272 /* certificates chain verification */
273 val = gnutls_certificate_verify_peers2( p_sys->session, &status );
277 msg_Err( p_session, "TLS certificate verification failed : %s",
278 gnutls_strerror( val ) );
279 p_session->pf_close( p_session );
285 msg_Warn( p_session, "TLS session : access denied" );
286 if( status & GNUTLS_CERT_INVALID )
287 msg_Dbg( p_session, "certificate could not be verified" );
288 if( status & GNUTLS_CERT_REVOKED )
289 msg_Dbg( p_session, "certificate was revoked" );
290 if( status & GNUTLS_CERT_SIGNER_NOT_FOUND )
291 msg_Dbg( p_session, "certificate's signer was not found" );
292 if( status & GNUTLS_CERT_SIGNER_NOT_CA )
293 msg_Dbg( p_session, "certificate's signer is not a CA" );
294 p_session->pf_close( p_session );
298 msg_Dbg( p_session, "TLS certificate verified" );
299 if( p_sys->psz_hostname == NULL )
302 /* certificate (host)name verification */
303 p_data = gnutls_certificate_get_peers( p_sys->session, &status );
306 msg_Err( p_session, "TLS peer certificate not available" );
307 p_session->pf_close( p_session );
311 val = gnutls_x509_crt_init( &cert );
314 msg_Err( p_session, "x509 fatal error : %s",
315 gnutls_strerror( val ) );
316 p_session->pf_close( p_session );
320 val = gnutls_x509_crt_import( cert, p_data, GNUTLS_X509_FMT_DER );
323 msg_Err( p_session, "x509 certificate import error : %s",
324 gnutls_strerror( val ) );
325 gnutls_x509_crt_deinit( cert );
326 p_session->pf_close( p_session );
330 if( gnutls_x509_crt_check_hostname( cert, p_sys->psz_hostname ) == 0 )
332 msg_Err( p_session, "x509 certificate does not match \"%s\"",
333 p_sys->psz_hostname );
334 gnutls_x509_crt_deinit( cert );
335 p_session->pf_close( p_session );
339 gnutls_x509_crt_deinit( cert );
341 msg_Dbg( p_session, "x509 hostname verified" );
349 gnutls_BeginHandshake( tls_session_t *p_session, int fd,
350 const char *psz_hostname )
352 tls_session_sys_t *p_sys;
354 p_sys = (tls_session_sys_t *)(p_session->p_sys);
356 gnutls_transport_set_ptr (p_sys->session, (gnutls_transport_ptr)(intptr_t)fd);
358 if( psz_hostname != NULL )
360 gnutls_server_name_set( p_sys->session, GNUTLS_NAME_DNS, psz_hostname,
361 strlen( psz_hostname ) );
362 if( get_Bool( p_session, "tls-check-hostname" ) )
364 p_sys->psz_hostname = strdup( psz_hostname );
365 if( p_sys->psz_hostname == NULL )
367 p_session->pf_close( p_session );
373 return p_session->pf_handshake2( p_session );
376 /*****************************************************************************
378 *****************************************************************************
379 * Terminates TLS session and releases session data.
380 *****************************************************************************/
382 gnutls_SessionClose( tls_session_t *p_session )
384 tls_session_sys_t *p_sys;
386 p_sys = (tls_session_sys_t *)(p_session->p_sys);
388 if( p_sys->b_handshaked == VLC_TRUE )
389 gnutls_bye( p_sys->session, GNUTLS_SHUT_WR );
390 gnutls_deinit( p_sys->session );
392 if( p_sys->psz_hostname != NULL )
393 free( p_sys->psz_hostname );
395 vlc_object_detach( p_session );
396 vlc_object_destroy( p_session );
402 gnutls_ClientDelete( tls_session_t *p_session )
404 /* On the client-side, credentials are re-allocated per session */
405 gnutls_certificate_credentials x509_cred =
406 ((tls_client_sys_t *)(p_session->p_sys))->x509_cred;
408 gnutls_SessionClose( p_session );
410 /* credentials must be free'd *after* gnutls_deinit() */
411 gnutls_certificate_free_credentials( x509_cred );
415 is_regular( const char *psz_filename )
417 #ifdef HAVE_SYS_STAT_H
420 return ( utf8_stat( psz_filename, &st ) == 0 )
421 && S_ISREG( st.st_mode );
428 gnutls_Addx509Directory( vlc_object_t *p_this,
429 gnutls_certificate_credentials cred,
430 const char *psz_dirname,
434 const char *psz_dirent;
436 if( *psz_dirname == '\0' )
439 dir = utf8_opendir( psz_dirname );
442 msg_Warn( p_this, "Cannot open directory (%s) : %s", psz_dirname,
447 while( ( psz_dirent = utf8_readdir( dir ) ) != NULL )
450 int check = asprintf( &psz_filename, "%s/%s", psz_dirname,
452 LocaleFree( psz_dirent );
459 /* we neglect the race condition here - not security sensitive */
460 if( is_regular( psz_filename ) )
463 char *psz_localname = ToLocale( psz_filename );
466 ? gnutls_certificate_set_x509_key_file( cred, psz_localname,
468 GNUTLS_X509_FMT_PEM )
469 : gnutls_certificate_set_x509_trust_file( cred, psz_localname,
472 LocaleFree( psz_localname );
475 msg_Warn( p_this, "Cannot add x509 certificate (%s) : %s",
476 psz_filename, gnutls_strerror( i ) );
479 free( psz_filename );
486 /*****************************************************************************
488 *****************************************************************************
489 * Initializes client-side TLS session data.
490 *****************************************************************************/
491 static tls_session_t *
492 gnutls_ClientCreate( tls_t *p_tls )
494 tls_session_t *p_session = NULL;
495 tls_client_sys_t *p_sys = NULL;
497 const int cert_type_priority[3] =
503 p_sys = (tls_client_sys_t *)malloc( sizeof(struct tls_client_sys_t) );
507 p_session = (struct tls_session_t *)vlc_object_create ( p_tls, sizeof(struct tls_session_t) );
508 if( p_session == NULL )
514 p_session->p_sys = p_sys;
515 p_session->sock.p_sys = p_session;
516 p_session->sock.pf_send = gnutls_Send;
517 p_session->sock.pf_recv = gnutls_Recv;
518 p_session->pf_handshake = gnutls_BeginHandshake;
519 p_session->pf_close = gnutls_ClientDelete;
521 p_sys->session.b_handshaked = VLC_FALSE;
522 p_sys->session.psz_hostname = NULL;
524 vlc_object_attach( p_session, p_tls );
526 i_val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
529 msg_Err( p_tls, "Cannot allocate X509 credentials : %s",
530 gnutls_strerror( i_val ) );
534 if( get_Bool( p_tls, "tls-check-cert" ) )
536 /* FIXME: support for changing path/using multiple paths */
539 if( asprintf( &psz_path, "%s/"CONFIG_DIR"/ssl/certs",
540 p_tls->p_vlc->psz_homedir ) == -1 )
542 gnutls_certificate_free_credentials( p_sys->x509_cred );
546 gnutls_Addx509Directory( (vlc_object_t *)p_session, p_sys->x509_cred,
547 psz_path, VLC_FALSE );
550 p_session->pf_handshake2 = gnutls_HandshakeAndValidate;
553 p_session->pf_handshake2 = gnutls_ContinueHandshake;
556 /* FIXME: support for changing path/using multiple paths */
559 if( asprintf( &psz_path, "%s/"CONFIG_DIR"/ssl/private",
560 p_tls->p_vlc->psz_homedir ) == -1 )
562 gnutls_certificate_free_credentials( p_sys->x509_cred );
566 gnutls_Addx509Directory( (vlc_object_t *)p_session, p_sys->x509_cred,
567 psz_path, VLC_TRUE );
572 i_val = gnutls_init( &p_sys->session.session, GNUTLS_CLIENT );
575 msg_Err( p_tls, "Cannot initialize TLS session : %s",
576 gnutls_strerror( i_val ) );
577 gnutls_certificate_free_credentials( p_sys->x509_cred );
581 i_val = gnutls_set_default_priority( p_sys->session.session );
584 msg_Err( p_tls, "Cannot set ciphers priorities : %s",
585 gnutls_strerror( i_val ) );
586 gnutls_deinit( p_sys->session.session );
587 gnutls_certificate_free_credentials( p_sys->x509_cred );
591 i_val = gnutls_certificate_type_set_priority( p_sys->session.session,
592 cert_type_priority );
595 msg_Err( p_tls, "Cannot set certificate type priorities : %s",
596 gnutls_strerror( i_val ) );
597 gnutls_deinit( p_sys->session.session );
598 gnutls_certificate_free_credentials( p_sys->x509_cred );
602 i_val = gnutls_credentials_set( p_sys->session.session,
603 GNUTLS_CRD_CERTIFICATE,
607 msg_Err( p_tls, "Cannot set TLS session credentials : %s",
608 gnutls_strerror( i_val ) );
609 gnutls_deinit( p_sys->session.session );
610 gnutls_certificate_free_credentials( p_sys->x509_cred );
617 vlc_object_detach( p_session );
618 vlc_object_destroy( p_session );
625 /*****************************************************************************
626 * TLS session resumption callbacks
627 *****************************************************************************/
628 static int cb_store( void *p_server, gnutls_datum key, gnutls_datum data )
630 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
632 if( ( p_sys->i_cache_size == 0 )
633 || ( key.size > MAX_SESSION_ID )
634 || ( data.size > MAX_SESSION_DATA ) )
637 vlc_mutex_lock( &p_sys->cache_lock );
639 memcpy( p_sys->p_store->id, key.data, key.size);
640 memcpy( p_sys->p_store->data, data.data, data.size );
641 p_sys->p_store->i_idlen = key.size;
642 p_sys->p_store->i_datalen = data.size;
645 if( ( p_sys->p_store - p_sys->p_cache ) == p_sys->i_cache_size )
646 p_sys->p_store = p_sys->p_cache;
648 vlc_mutex_unlock( &p_sys->cache_lock );
654 static const gnutls_datum err_datum = { NULL, 0 };
656 static gnutls_datum cb_fetch( void *p_server, gnutls_datum key )
658 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
659 saved_session_t *p_session, *p_end;
661 p_session = p_sys->p_cache;
662 p_end = p_session + p_sys->i_cache_size;
664 vlc_mutex_lock( &p_sys->cache_lock );
666 while( p_session < p_end )
668 if( ( p_session->i_idlen == key.size )
669 && !memcmp( p_session->id, key.data, key.size ) )
673 data.size = p_session->i_datalen;
675 data.data = gnutls_malloc( data.size );
676 if( data.data == NULL )
678 vlc_mutex_unlock( &p_sys->cache_lock );
682 memcpy( data.data, p_session->data, data.size );
683 vlc_mutex_unlock( &p_sys->cache_lock );
689 vlc_mutex_unlock( &p_sys->cache_lock );
695 static int cb_delete( void *p_server, gnutls_datum key )
697 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
698 saved_session_t *p_session, *p_end;
700 p_session = p_sys->p_cache;
701 p_end = p_session + p_sys->i_cache_size;
703 vlc_mutex_lock( &p_sys->cache_lock );
705 while( p_session < p_end )
707 if( ( p_session->i_idlen == key.size )
708 && !memcmp( p_session->id, key.data, key.size ) )
710 p_session->i_datalen = p_session->i_idlen = 0;
711 vlc_mutex_unlock( &p_sys->cache_lock );
717 vlc_mutex_unlock( &p_sys->cache_lock );
723 /*****************************************************************************
724 * tls_ServerSessionPrepare:
725 *****************************************************************************
726 * Initializes server-side TLS session data.
727 *****************************************************************************/
728 static tls_session_t *
729 gnutls_ServerSessionPrepare( tls_server_t *p_server )
731 tls_session_t *p_session;
732 tls_server_sys_t *p_server_sys;
733 gnutls_session session;
736 p_session = vlc_object_create( p_server, sizeof (struct tls_session_t) );
737 if( p_session == NULL )
740 p_session->p_sys = malloc( sizeof(struct tls_session_sys_t) );
741 if( p_session->p_sys == NULL )
743 vlc_object_destroy( p_session );
747 vlc_object_attach( p_session, p_server );
749 p_server_sys = (tls_server_sys_t *)p_server->p_sys;
750 p_session->sock.p_sys = p_session;
751 p_session->sock.pf_send = gnutls_Send;
752 p_session->sock.pf_recv = gnutls_Recv;
753 p_session->pf_handshake = gnutls_BeginHandshake;
754 p_session->pf_handshake2 = p_server_sys->pf_handshake2;
755 p_session->pf_close = gnutls_SessionClose;
757 ((tls_session_sys_t *)p_session->p_sys)->b_handshaked = VLC_FALSE;
758 ((tls_session_sys_t *)p_session->p_sys)->psz_hostname = NULL;
760 i_val = gnutls_init( &session, GNUTLS_SERVER );
763 msg_Err( p_server, "Cannot initialize TLS session : %s",
764 gnutls_strerror( i_val ) );
768 ((tls_session_sys_t *)p_session->p_sys)->session = session;
770 i_val = gnutls_set_default_priority( session );
773 msg_Err( p_server, "Cannot set ciphers priorities : %s",
774 gnutls_strerror( i_val ) );
775 gnutls_deinit( session );
779 i_val = gnutls_credentials_set( session, GNUTLS_CRD_CERTIFICATE,
780 p_server_sys->x509_cred );
783 msg_Err( p_server, "Cannot set TLS session credentials : %s",
784 gnutls_strerror( i_val ) );
785 gnutls_deinit( session );
789 if( p_session->pf_handshake2 == gnutls_HandshakeAndValidate )
790 gnutls_certificate_server_set_request( session, GNUTLS_CERT_REQUIRE );
792 gnutls_dh_set_prime_bits( session, get_Int( p_server, "dh-bits" ) );
794 /* Session resumption support */
795 gnutls_db_set_cache_expiration( session, get_Int( p_server,
796 "tls-cache-expiration" ) );
797 gnutls_db_set_retrieve_function( session, cb_fetch );
798 gnutls_db_set_remove_function( session, cb_delete );
799 gnutls_db_set_store_function( session, cb_store );
800 gnutls_db_set_ptr( session, p_server );
805 free( p_session->p_sys );
806 vlc_object_detach( p_session );
807 vlc_object_destroy( p_session );
812 /*****************************************************************************
814 *****************************************************************************
815 * Releases data allocated with tls_ServerCreate.
816 *****************************************************************************/
818 gnutls_ServerDelete( tls_server_t *p_server )
820 tls_server_sys_t *p_sys;
821 p_sys = (tls_server_sys_t *)p_server->p_sys;
823 vlc_mutex_destroy( &p_sys->cache_lock );
824 free( p_sys->p_cache );
826 vlc_object_detach( p_server );
827 vlc_object_destroy( p_server );
829 /* all sessions depending on the server are now deinitialized */
830 gnutls_certificate_free_credentials( p_sys->x509_cred );
831 gnutls_dh_params_deinit( p_sys->dh_params );
836 /*****************************************************************************
838 *****************************************************************************
839 * Adds one or more certificate authorities.
840 * Returns -1 on error, 0 on success.
841 *****************************************************************************/
843 gnutls_ServerAddCA( tls_server_t *p_server, const char *psz_ca_path )
845 tls_server_sys_t *p_sys;
846 char *psz_local_path;
849 p_sys = (tls_server_sys_t *)(p_server->p_sys);
851 psz_local_path = ToLocale( psz_ca_path );
852 val = gnutls_certificate_set_x509_trust_file( p_sys->x509_cred,
854 GNUTLS_X509_FMT_PEM );
855 LocaleFree( psz_local_path );
858 msg_Err( p_server, "Cannot add trusted CA (%s) : %s", psz_ca_path,
859 gnutls_strerror( val ) );
862 msg_Dbg( p_server, " %d trusted CA added (%s)", val, psz_ca_path );
864 /* enables peer's certificate verification */
865 p_sys->pf_handshake2 = gnutls_HandshakeAndValidate;
871 /*****************************************************************************
873 *****************************************************************************
874 * Adds a certificates revocation list to be sent to TLS clients.
875 * Returns -1 on error, 0 on success.
876 *****************************************************************************/
878 gnutls_ServerAddCRL( tls_server_t *p_server, const char *psz_crl_path )
881 char *psz_local_path = ToLocale( psz_crl_path );
883 val = gnutls_certificate_set_x509_crl_file( ((tls_server_sys_t *)
884 (p_server->p_sys))->x509_cred,
886 GNUTLS_X509_FMT_PEM );
887 LocaleFree( psz_crl_path );
890 msg_Err( p_server, "Cannot add CRL (%s) : %s", psz_crl_path,
891 gnutls_strerror( val ) );
894 msg_Dbg( p_server, "%d CRL added (%s)", val, psz_crl_path );
899 /*****************************************************************************
901 *****************************************************************************
902 * Allocates a whole server's TLS credentials.
903 * Returns NULL on error.
904 *****************************************************************************/
905 static tls_server_t *
906 gnutls_ServerCreate( tls_t *p_tls, const char *psz_cert_path,
907 const char *psz_key_path )
909 tls_server_t *p_server;
910 tls_server_sys_t *p_sys;
911 char *psz_local_key, *psz_local_cert;
914 msg_Dbg( p_tls, "Creating TLS server" );
916 p_sys = (tls_server_sys_t *)malloc( sizeof(struct tls_server_sys_t) );
920 p_sys->i_cache_size = get_Int( p_tls, "tls-cache-size" );
921 p_sys->p_cache = (struct saved_session_t *)calloc( p_sys->i_cache_size,
922 sizeof( struct saved_session_t ) );
923 if( p_sys->p_cache == NULL )
928 p_sys->p_store = p_sys->p_cache;
930 p_server = vlc_object_create( p_tls, sizeof(struct tls_server_t) );
931 if( p_server == NULL )
933 free( p_sys->p_cache );
938 vlc_object_attach( p_server, p_tls );
940 p_server->p_sys = p_sys;
941 p_server->pf_delete = gnutls_ServerDelete;
942 p_server->pf_add_CA = gnutls_ServerAddCA;
943 p_server->pf_add_CRL = gnutls_ServerAddCRL;
944 p_server->pf_session_prepare = gnutls_ServerSessionPrepare;
946 /* No certificate validation by default */
947 p_sys->pf_handshake2 = gnutls_ContinueHandshake;
949 /* FIXME: check for errors */
950 vlc_mutex_init( p_server, &p_sys->cache_lock );
952 /* Sets server's credentials */
953 val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
956 msg_Err( p_server, "Cannot allocate X509 credentials : %s",
957 gnutls_strerror( val ) );
961 psz_local_cert = ToLocale( psz_cert_path );
962 psz_local_key = ToLocale( psz_key_path );
963 val = gnutls_certificate_set_x509_key_file( p_sys->x509_cred,
964 psz_local_cert, psz_local_key,
965 GNUTLS_X509_FMT_PEM );
966 LocaleFree( psz_cert_path );
967 LocaleFree( psz_key_path );
970 msg_Err( p_server, "Cannot set certificate chain or private key : %s",
971 gnutls_strerror( val ) );
972 gnutls_certificate_free_credentials( p_sys->x509_cred );
977 * - regenerate these regularly
978 * - support other ciper suites
980 val = gnutls_dh_params_init( &p_sys->dh_params );
983 msg_Dbg( p_server, "Computing Diffie Hellman ciphers parameters" );
984 val = gnutls_dh_params_generate2( p_sys->dh_params,
985 get_Int( p_tls, "dh-bits" ) );
989 msg_Err( p_server, "Cannot initialize DH cipher suites : %s",
990 gnutls_strerror( val ) );
991 gnutls_certificate_free_credentials( p_sys->x509_cred );
994 msg_Dbg( p_server, "Ciphers parameters computed" );
996 gnutls_certificate_set_dh_params( p_sys->x509_cred, p_sys->dh_params);
1001 vlc_mutex_destroy( &p_sys->cache_lock );
1002 vlc_object_detach( p_server );
1003 vlc_object_destroy( p_server );
1009 /*****************************************************************************
1010 * gcrypt thread option VLC implementation:
1011 *****************************************************************************/
1012 vlc_object_t *__p_gcry_data;
1014 static int gcry_vlc_mutex_init( void **p_sys )
1017 vlc_mutex_t *p_lock = (vlc_mutex_t *)malloc( sizeof( vlc_mutex_t ) );
1022 i_val = vlc_mutex_init( __p_gcry_data, p_lock );
1030 static int gcry_vlc_mutex_destroy( void **p_sys )
1033 vlc_mutex_t *p_lock = (vlc_mutex_t *)*p_sys;
1035 i_val = vlc_mutex_destroy( p_lock );
1040 static int gcry_vlc_mutex_lock( void **p_sys )
1042 return vlc_mutex_lock( (vlc_mutex_t *)*p_sys );
1045 static int gcry_vlc_mutex_unlock( void **lock )
1047 return vlc_mutex_unlock( (vlc_mutex_t *)*lock );
1050 static struct gcry_thread_cbs gcry_threads_vlc =
1052 GCRY_THREAD_OPTION_USER,
1054 gcry_vlc_mutex_init,
1055 gcry_vlc_mutex_destroy,
1056 gcry_vlc_mutex_lock,
1057 gcry_vlc_mutex_unlock
1061 /*****************************************************************************
1062 * Module initialization
1063 *****************************************************************************/
1065 Open( vlc_object_t *p_this )
1067 tls_t *p_tls = (tls_t *)p_this;
1069 vlc_value_t lock, count;
1071 var_Create( p_this->p_libvlc, "gnutls_mutex", VLC_VAR_MUTEX );
1072 var_Get( p_this->p_libvlc, "gnutls_mutex", &lock );
1073 vlc_mutex_lock( lock.p_address );
1075 /* Initialize GnuTLS only once */
1076 var_Create( p_this->p_libvlc, "gnutls_count", VLC_VAR_INTEGER );
1077 var_Get( p_this->p_libvlc, "gnutls_count", &count);
1079 if( count.i_int == 0)
1081 const char *psz_version;
1083 __p_gcry_data = VLC_OBJECT( p_this->p_vlc );
1085 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_vlc);
1086 if( gnutls_global_init( ) )
1088 msg_Warn( p_this, "cannot initialize GnuTLS" );
1089 vlc_mutex_unlock( lock.p_address );
1090 return VLC_EGENERIC;
1093 * FIXME: in fact, we currently depends on 1.0.17, but it breaks on
1094 * Debian which as a patched 1.0.16 (which we can use).
1096 psz_version = gnutls_check_version( "1.0.16" );
1097 if( psz_version == NULL )
1099 gnutls_global_deinit( );
1100 vlc_mutex_unlock( lock.p_address );
1101 msg_Err( p_this, "unsupported GnuTLS version" );
1102 return VLC_EGENERIC;
1104 msg_Dbg( p_this, "GnuTLS v%s initialized", psz_version );
1108 var_Set( p_this->p_libvlc, "gnutls_count", count);
1109 vlc_mutex_unlock( lock.p_address );
1111 p_tls->pf_server_create = gnutls_ServerCreate;
1112 p_tls->pf_client_create = gnutls_ClientCreate;
1117 /*****************************************************************************
1118 * Module deinitialization
1119 *****************************************************************************/
1121 Close( vlc_object_t *p_this )
1123 /*tls_t *p_tls = (tls_t *)p_this;
1124 tls_sys_t *p_sys = (tls_sys_t *)(p_this->p_sys);*/
1126 vlc_value_t lock, count;
1128 var_Create( p_this->p_libvlc, "gnutls_mutex", VLC_VAR_MUTEX );
1129 var_Get( p_this->p_libvlc, "gnutls_mutex", &lock );
1130 vlc_mutex_lock( lock.p_address );
1132 var_Create( p_this->p_libvlc, "gnutls_count", VLC_VAR_INTEGER );
1133 var_Get( p_this->p_libvlc, "gnutls_count", &count);
1135 var_Set( p_this->p_libvlc, "gnutls_count", count);
1137 if( count.i_int == 0 )
1139 gnutls_global_deinit( );
1140 msg_Dbg( p_this, "GnuTLS deinitialized" );
1143 vlc_mutex_unlock( lock.p_address );