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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
50 #include <gnutls/gnutls.h>
51 #include <gnutls/x509.h>
54 #define CACHE_EXPIRATION 3600
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
63 #define DH_BITS_TEXT N_("Diffie-Hellman prime bits")
64 #define DH_BITS_LONGTEXT N_( \
65 "This allows you to modify the Diffie-Hellman prime's number of bits, " \
66 "used for TLS or SSL-based server-side encryption. This is generally " \
69 #define CACHE_EXPIRATION_TEXT N_("Expiration time for resumed TLS sessions")
70 #define CACHE_EXPIRATION_LONGTEXT N_( \
71 "It is possible to cache the resumed TLS sessions. This is the expiration "\
72 "time of the sessions stored in this cache, in seconds." )
74 #define CACHE_SIZE_TEXT N_("Number of resumed TLS sessions")
75 #define CACHE_SIZE_LONGTEXT N_( \
76 "This is the maximum number of resumed TLS sessions that " \
77 "the cache will hold." )
79 #define CHECK_CERT_TEXT N_("Check TLS/SSL server certificate validity")
80 #define CHECK_CERT_LONGTEXT N_( \
81 "This ensures that the server certificate is valid " \
82 "(i.e. signed by an approved Certification Authority)." )
84 #define CHECK_HOSTNAME_TEXT N_("Check TLS/SSL server hostname in certificate")
85 #define CHECK_HOSTNAME_LONGTEXT N_( \
86 "This ensures that the server hostname in certificate matches the " \
87 "requested host name." )
90 set_shortname( "GnuTLS" );
91 set_description( _("GnuTLS TLS encryption layer") );
92 set_capability( "tls", 1 );
93 set_callbacks( Open, Close );
94 set_category( CAT_ADVANCED );
95 set_subcategory( SUBCAT_ADVANCED_MISC );
97 add_bool( "tls-check-cert", VLC_TRUE, NULL, CHECK_CERT_TEXT,
98 CHECK_CERT_LONGTEXT, VLC_FALSE );
99 add_bool( "tls-check-hostname", VLC_TRUE, NULL, CHECK_HOSTNAME_TEXT,
100 CHECK_HOSTNAME_LONGTEXT, VLC_FALSE );
102 add_integer( "gnutls-dh-bits", DH_BITS, NULL, DH_BITS_TEXT,
103 DH_BITS_LONGTEXT, VLC_TRUE );
104 add_integer( "gnutls-cache-expiration", CACHE_EXPIRATION, NULL,
105 CACHE_EXPIRATION_TEXT, CACHE_EXPIRATION_LONGTEXT, VLC_TRUE );
106 add_integer( "gnutls-cache-size", CACHE_SIZE, NULL, CACHE_SIZE_TEXT,
107 CACHE_SIZE_LONGTEXT, VLC_TRUE );
111 #define MAX_SESSION_ID 32
112 #define MAX_SESSION_DATA 1024
114 typedef struct saved_session_t
116 char id[MAX_SESSION_ID];
117 char data[MAX_SESSION_DATA];
124 typedef struct tls_server_sys_t
126 gnutls_certificate_credentials x509_cred;
127 gnutls_dh_params dh_params;
129 struct saved_session_t *p_cache;
130 struct saved_session_t *p_store;
132 vlc_mutex_t cache_lock;
134 int (*pf_handshake2)( tls_session_t * );
138 typedef struct tls_session_sys_t
140 gnutls_session session;
142 vlc_bool_t b_handshaked;
146 typedef struct tls_client_sys_t
148 struct tls_session_sys_t session;
149 gnutls_certificate_credentials x509_cred;
154 _get_Int( vlc_object_t *p_this, const char *var )
158 if( var_Get( p_this, var, &value ) != VLC_SUCCESS )
160 var_Create( p_this, var, VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
161 var_Get( p_this, var, &value );
168 _get_Bool( vlc_object_t *p_this, const char *var )
172 if( var_Get( p_this, var, &value ) != VLC_SUCCESS )
174 var_Create( p_this, var, VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
175 var_Get( p_this, var, &value );
181 #define get_Int( a, b ) _get_Int( (vlc_object_t *)(a), (b) )
182 #define get_Bool( a, b ) _get_Bool( (vlc_object_t *)(a), (b) )
186 * Sends data through a TLS session.
189 gnutls_Send( void *p_session, const void *buf, int i_length )
192 tls_session_sys_t *p_sys;
194 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
196 val = gnutls_record_send( p_sys->session, buf, i_length );
197 /* TODO: handle fatal error */
198 return val < 0 ? -1 : val;
203 * Receives data through a TLS session.
206 gnutls_Recv( void *p_session, void *buf, int i_length )
209 tls_session_sys_t *p_sys;
211 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
213 val = gnutls_record_recv( p_sys->session, buf, i_length );
214 /* TODO: handle fatal error */
215 return val < 0 ? -1 : val;
219 /*****************************************************************************
220 * tls_Session(Continue)?Handshake:
221 *****************************************************************************
222 * Establishes TLS session with a peer through socket <fd>.
223 * Returns -1 on error (you need not and must not call tls_SessionClose)
224 * 0 on succesful handshake completion, 1 if more would-be blocking recv is
225 * needed, 2 if more would-be blocking send is required.
226 *****************************************************************************/
228 gnutls_ContinueHandshake( tls_session_t *p_session)
230 tls_session_sys_t *p_sys;
233 p_sys = (tls_session_sys_t *)(p_session->p_sys);
235 /* TODO: handle fatal error */
237 WSASetLastError( 0 );
239 val = gnutls_handshake( p_sys->session );
240 if( ( val == GNUTLS_E_AGAIN ) || ( val == GNUTLS_E_INTERRUPTED ) )
241 return 1 + gnutls_record_get_direction( p_sys->session );
246 msg_Dbg( p_session, "Winsock error %d", WSAGetLastError( ) );
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_VerifyHostname( vlc_object_t *p_this, gnutls_session session,
260 const char *psz_hostname )
262 const gnutls_datum *p_data;
263 gnutls_x509_crt cert;
267 /* certificate (host)name verification */
268 p_data = gnutls_certificate_get_peers( session, &status );
271 msg_Err( p_this, "TLS peer certificate not available" );
275 val = gnutls_x509_crt_init( &cert );
278 msg_Err( p_this, "x509 fatal error: %s", gnutls_strerror( val ) );
282 val = gnutls_x509_crt_import( cert, p_data, GNUTLS_X509_FMT_DER );
285 msg_Err( p_this, "x509 certificate import error: %s",
286 gnutls_strerror( val ) );
287 gnutls_x509_crt_deinit( cert );
291 if( gnutls_x509_crt_check_hostname( cert, psz_hostname ) == 0 )
293 msg_Err( p_this, "x509 certificate does not match \"%s\"",
295 gnutls_x509_crt_deinit( cert );
299 gnutls_x509_crt_deinit( cert );
300 msg_Dbg( p_this, "x509 hostname matches %s", psz_hostname );
311 static const error_msg_t cert_errors[] =
313 { GNUTLS_CERT_INVALID,
314 "Certificate could not be verified" },
315 { GNUTLS_CERT_REVOKED,
316 "Certificate was revoked" },
317 { GNUTLS_CERT_SIGNER_NOT_FOUND,
318 "Certificate's signer was not found" },
319 { GNUTLS_CERT_SIGNER_NOT_CA,
320 "Certificate's signer is not a CA" },
321 { GNUTLS_CERT_INSECURE_ALGORITHM,
322 "Insecure certificate signature algorithm" },
328 gnutls_HandshakeAndValidate( tls_session_t *session )
330 int val = gnutls_ContinueHandshake( session );
334 tls_session_sys_t *p_sys = (tls_session_sys_t *)(session->p_sys);
336 /* certificates chain verification */
338 val = gnutls_certificate_verify_peers2( p_sys->session, &status );
342 msg_Err( session, "Certificate verification failed: %s",
343 gnutls_strerror( val ) );
349 msg_Err( session, "TLS session: access denied" );
350 for( const error_msg_t *e = cert_errors; e->flag; e++ )
352 if( status & e->flag )
354 msg_Err( session, e->msg );
361 "unknown certificate error (you found a bug in VLC)" );
366 /* certificate (host)name verification */
367 const gnutls_datum *data = gnutls_certificate_get_peers( p_sys->session,
371 msg_Err( session, "Peer certificate not available" );
375 gnutls_x509_crt cert;
376 val = gnutls_x509_crt_init( &cert );
379 msg_Err( session, "x509 fatal error: %s", gnutls_strerror( val ) );
383 val = gnutls_x509_crt_import( cert, data, GNUTLS_X509_FMT_DER );
386 msg_Err( session, "Certificate import error: %s",
387 gnutls_strerror( val ) );
388 gnutls_x509_crt_deinit( cert );
392 if( p_sys->psz_hostname != NULL )
394 if ( !gnutls_x509_crt_check_hostname( cert, p_sys->psz_hostname ) )
396 msg_Err( session, "Certificate does not match \"%s\"",
397 p_sys->psz_hostname );
402 msg_Warn( session, "Certificate and hostname were not verified" );
404 if( gnutls_x509_crt_get_expiration_time( cert ) < time( NULL ) )
406 msg_Err( session, "Certificate expired" );
410 if( gnutls_x509_crt_get_activation_time( cert ) > time ( NULL ) )
412 msg_Err( session, "Certificate not yet valid" );
416 gnutls_x509_crt_deinit( cert );
417 msg_Dbg( session, "TLS/x509 certificate verified" );
421 gnutls_x509_crt_deinit( cert );
423 session->pf_close( session );
428 * Starts negociation of a TLS session.
430 * @param fd stream socket already connected with the peer.
431 * @param psz_hostname if not NULL, hostname to mention as a Server Name.
433 * @return -1 on error (you need not and must not call tls_SessionClose),
434 * 0 on succesful handshake completion, 1 if more would-be blocking recv is
435 * needed, 2 if more would-be blocking send is required.
438 gnutls_BeginHandshake( tls_session_t *p_session, int fd,
439 const char *psz_hostname )
441 tls_session_sys_t *p_sys;
443 p_sys = (tls_session_sys_t *)(p_session->p_sys);
445 gnutls_transport_set_ptr (p_sys->session, (gnutls_transport_ptr)(intptr_t)fd);
447 if( psz_hostname != NULL )
449 gnutls_server_name_set( p_sys->session, GNUTLS_NAME_DNS, psz_hostname,
450 strlen( psz_hostname ) );
451 if( get_Bool( p_session, "tls-check-hostname" ) )
453 p_sys->psz_hostname = strdup( psz_hostname );
454 if( p_sys->psz_hostname == NULL )
456 p_session->pf_close( p_session );
462 return p_session->pf_handshake2( p_session );
466 * Terminates TLS session and releases session data.
467 * You still have to close the socket yourself.
470 gnutls_SessionClose( tls_session_t *p_session )
472 tls_session_sys_t *p_sys;
474 p_sys = (tls_session_sys_t *)(p_session->p_sys);
476 if( p_sys->b_handshaked == VLC_TRUE )
477 gnutls_bye( p_sys->session, GNUTLS_SHUT_WR );
478 gnutls_deinit( p_sys->session );
480 if( p_sys->psz_hostname != NULL )
481 free( p_sys->psz_hostname );
483 vlc_object_detach( p_session );
484 vlc_object_destroy( p_session );
490 gnutls_ClientDelete( tls_session_t *p_session )
492 /* On the client-side, credentials are re-allocated per session */
493 gnutls_certificate_credentials x509_cred =
494 ((tls_client_sys_t *)(p_session->p_sys))->x509_cred;
496 gnutls_SessionClose( p_session );
498 /* credentials must be free'd *after* gnutls_deinit() */
499 gnutls_certificate_free_credentials( x509_cred );
504 gnutls_Addx509File( vlc_object_t *p_this,
505 gnutls_certificate_credentials cred,
506 const char *psz_path, vlc_bool_t b_priv );
509 gnutls_Addx509Directory( vlc_object_t *p_this,
510 gnutls_certificate_credentials cred,
511 const char *psz_dirname,
515 const char *psz_dirent;
517 if( *psz_dirname == '\0' )
520 dir = utf8_opendir( psz_dirname );
523 msg_Warn( p_this, "cannot open directory (%s): %s", psz_dirname,
530 struct stat st1, st2;
531 int fd = dirfd( dir );
534 * Gets stats for the directory path, checks that it is not a
535 * symbolic link (to avoid possibly infinite recursion), and verifies
536 * that the inode is still the same, to avoid TOCTOU race condition.
539 || fstat( fd, &st1 ) || utf8_lstat( psz_dirname, &st2 )
540 || S_ISLNK( st2.st_mode ) || ( st1.st_ino != st2.st_ino ) )
548 while( ( psz_dirent = utf8_readdir( dir ) ) != NULL )
553 if( ( strcmp( ".", psz_dirent ) == 0 )
554 || ( strcmp( "..", psz_dirent ) == 0 ) )
557 check = asprintf( &psz_filename, "%s/%s", psz_dirname,
559 LocaleFree( psz_dirent );
563 gnutls_Addx509File( p_this, cred, psz_filename, b_priv );
564 free( psz_filename );
573 gnutls_Addx509File( vlc_object_t *p_this,
574 gnutls_certificate_credentials cred,
575 const char *psz_path, vlc_bool_t b_priv )
579 if( utf8_stat( psz_path, &st ) == 0 )
581 if( S_ISREG( st.st_mode ) )
583 char *psz_localname = ToLocale( psz_path );
585 ? gnutls_certificate_set_x509_key_file( cred,
586 psz_localname, psz_localname, GNUTLS_X509_FMT_PEM )
587 : gnutls_certificate_set_x509_trust_file( cred,
588 psz_localname, GNUTLS_X509_FMT_PEM );
589 LocaleFree( psz_localname );
593 msg_Warn( p_this, "cannot add x509 credentials (%s): %s",
594 psz_path, gnutls_strerror( i ) );
599 msg_Dbg( p_this, "added x509 credentials (%s)",
604 else if( S_ISDIR( st.st_mode ) )
607 "looking recursively for x509 credentials in %s",
609 return gnutls_Addx509Directory( p_this, cred, psz_path, b_priv);
613 msg_Warn( p_this, "cannot add x509 credentials (%s): %s",
614 psz_path, strerror( errno ) );
620 * Initializes a client-side TLS session.
622 static tls_session_t *
623 gnutls_ClientCreate( tls_t *p_tls )
625 tls_session_t *p_session = NULL;
626 tls_client_sys_t *p_sys = NULL;
628 const int cert_type_priority[3] =
634 p_sys = (tls_client_sys_t *)malloc( sizeof(struct tls_client_sys_t) );
638 p_session = (struct tls_session_t *)vlc_object_create ( p_tls, sizeof(struct tls_session_t) );
639 if( p_session == NULL )
645 p_session->p_sys = p_sys;
646 p_session->sock.p_sys = p_session;
647 p_session->sock.pf_send = gnutls_Send;
648 p_session->sock.pf_recv = gnutls_Recv;
649 p_session->pf_handshake = gnutls_BeginHandshake;
650 p_session->pf_close = gnutls_ClientDelete;
652 p_sys->session.b_handshaked = VLC_FALSE;
653 p_sys->session.psz_hostname = NULL;
655 vlc_object_attach( p_session, p_tls );
657 i_val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
660 msg_Err( p_tls, "cannot allocate X509 credentials: %s",
661 gnutls_strerror( i_val ) );
665 if( get_Bool( p_tls, "tls-check-cert" ) )
669 if( asprintf( &psz_path, "%s/"CONFIG_DIR"/ssl/certs",
670 p_tls->p_libvlc->psz_homedir ) != -1 )
672 gnutls_Addx509Directory( (vlc_object_t *)p_session,
673 p_sys->x509_cred, psz_path, VLC_FALSE );
677 if( asprintf( &psz_path, "%s/ca-certificates.crt",
678 config_GetDataDir ( (vlc_object_t *)p_session) ) != -1 )
680 gnutls_Addx509File( (vlc_object_t *)p_session,
681 p_sys->x509_cred, psz_path, VLC_FALSE );
684 p_session->pf_handshake2 = gnutls_HandshakeAndValidate;
687 p_session->pf_handshake2 = gnutls_ContinueHandshake;
692 if( asprintf( &psz_path, "%s/"CONFIG_DIR"/ssl/private",
693 p_tls->p_libvlc->psz_homedir ) == -1 )
695 gnutls_certificate_free_credentials( p_sys->x509_cred );
699 gnutls_Addx509Directory( (vlc_object_t *)p_session, p_sys->x509_cred,
700 psz_path, VLC_TRUE );
705 i_val = gnutls_init( &p_sys->session.session, GNUTLS_CLIENT );
708 msg_Err( p_tls, "cannot initialize TLS session: %s",
709 gnutls_strerror( i_val ) );
710 gnutls_certificate_free_credentials( p_sys->x509_cred );
714 i_val = gnutls_set_default_priority( p_sys->session.session );
717 msg_Err( p_tls, "cannot set ciphers priorities: %s",
718 gnutls_strerror( i_val ) );
719 gnutls_deinit( p_sys->session.session );
720 gnutls_certificate_free_credentials( p_sys->x509_cred );
724 i_val = gnutls_certificate_type_set_priority( p_sys->session.session,
725 cert_type_priority );
728 msg_Err( p_tls, "cannot set certificate type priorities: %s",
729 gnutls_strerror( i_val ) );
730 gnutls_deinit( p_sys->session.session );
731 gnutls_certificate_free_credentials( p_sys->x509_cred );
735 i_val = gnutls_credentials_set( p_sys->session.session,
736 GNUTLS_CRD_CERTIFICATE,
740 msg_Err( p_tls, "cannot set TLS session credentials: %s",
741 gnutls_strerror( i_val ) );
742 gnutls_deinit( p_sys->session.session );
743 gnutls_certificate_free_credentials( p_sys->x509_cred );
750 vlc_object_detach( p_session );
751 vlc_object_destroy( p_session );
759 * TLS session resumption callbacks (server-side)
761 static int cb_store( void *p_server, gnutls_datum key, gnutls_datum data )
763 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
765 if( ( p_sys->i_cache_size == 0 )
766 || ( key.size > MAX_SESSION_ID )
767 || ( data.size > MAX_SESSION_DATA ) )
770 vlc_mutex_lock( &p_sys->cache_lock );
772 memcpy( p_sys->p_store->id, key.data, key.size);
773 memcpy( p_sys->p_store->data, data.data, data.size );
774 p_sys->p_store->i_idlen = key.size;
775 p_sys->p_store->i_datalen = data.size;
778 if( ( p_sys->p_store - p_sys->p_cache ) == p_sys->i_cache_size )
779 p_sys->p_store = p_sys->p_cache;
781 vlc_mutex_unlock( &p_sys->cache_lock );
787 static const gnutls_datum err_datum = { NULL, 0 };
789 static gnutls_datum cb_fetch( void *p_server, gnutls_datum key )
791 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
792 saved_session_t *p_session, *p_end;
794 p_session = p_sys->p_cache;
795 p_end = p_session + p_sys->i_cache_size;
797 vlc_mutex_lock( &p_sys->cache_lock );
799 while( p_session < p_end )
801 if( ( p_session->i_idlen == key.size )
802 && !memcmp( p_session->id, key.data, key.size ) )
806 data.size = p_session->i_datalen;
808 data.data = gnutls_malloc( data.size );
809 if( data.data == NULL )
811 vlc_mutex_unlock( &p_sys->cache_lock );
815 memcpy( data.data, p_session->data, data.size );
816 vlc_mutex_unlock( &p_sys->cache_lock );
822 vlc_mutex_unlock( &p_sys->cache_lock );
828 static int cb_delete( void *p_server, gnutls_datum key )
830 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
831 saved_session_t *p_session, *p_end;
833 p_session = p_sys->p_cache;
834 p_end = p_session + p_sys->i_cache_size;
836 vlc_mutex_lock( &p_sys->cache_lock );
838 while( p_session < p_end )
840 if( ( p_session->i_idlen == key.size )
841 && !memcmp( p_session->id, key.data, key.size ) )
843 p_session->i_datalen = p_session->i_idlen = 0;
844 vlc_mutex_unlock( &p_sys->cache_lock );
850 vlc_mutex_unlock( &p_sys->cache_lock );
857 * Initializes a server-side TLS session.
859 static tls_session_t *
860 gnutls_ServerSessionPrepare( tls_server_t *p_server )
862 tls_session_t *p_session;
863 tls_server_sys_t *p_server_sys;
864 gnutls_session session;
867 p_session = vlc_object_create( p_server, sizeof (struct tls_session_t) );
868 if( p_session == NULL )
871 p_session->p_sys = malloc( sizeof(struct tls_session_sys_t) );
872 if( p_session->p_sys == NULL )
874 vlc_object_destroy( p_session );
878 vlc_object_attach( p_session, p_server );
880 p_server_sys = (tls_server_sys_t *)p_server->p_sys;
881 p_session->sock.p_sys = p_session;
882 p_session->sock.pf_send = gnutls_Send;
883 p_session->sock.pf_recv = gnutls_Recv;
884 p_session->pf_handshake = gnutls_BeginHandshake;
885 p_session->pf_handshake2 = p_server_sys->pf_handshake2;
886 p_session->pf_close = gnutls_SessionClose;
888 ((tls_session_sys_t *)p_session->p_sys)->b_handshaked = VLC_FALSE;
889 ((tls_session_sys_t *)p_session->p_sys)->psz_hostname = NULL;
891 i_val = gnutls_init( &session, GNUTLS_SERVER );
894 msg_Err( p_server, "cannot initialize TLS session: %s",
895 gnutls_strerror( i_val ) );
899 ((tls_session_sys_t *)p_session->p_sys)->session = session;
901 i_val = gnutls_set_default_priority( session );
904 msg_Err( p_server, "cannot set ciphers priorities: %s",
905 gnutls_strerror( i_val ) );
906 gnutls_deinit( session );
910 i_val = gnutls_credentials_set( session, GNUTLS_CRD_CERTIFICATE,
911 p_server_sys->x509_cred );
914 msg_Err( p_server, "cannot set TLS session credentials: %s",
915 gnutls_strerror( i_val ) );
916 gnutls_deinit( session );
920 if( p_session->pf_handshake2 == gnutls_HandshakeAndValidate )
921 gnutls_certificate_server_set_request( session, GNUTLS_CERT_REQUIRE );
923 gnutls_dh_set_prime_bits( session, get_Int( p_server, "gnutls-dh-bits" ) );
925 /* Session resumption support */
926 gnutls_db_set_cache_expiration( session, get_Int( p_server,
927 "gnutls-cache-expiration" ) );
928 gnutls_db_set_retrieve_function( session, cb_fetch );
929 gnutls_db_set_remove_function( session, cb_delete );
930 gnutls_db_set_store_function( session, cb_store );
931 gnutls_db_set_ptr( session, p_server );
936 free( p_session->p_sys );
937 vlc_object_detach( p_session );
938 vlc_object_destroy( p_session );
944 * Releases data allocated with tls_ServerCreate().
947 gnutls_ServerDelete( tls_server_t *p_server )
949 tls_server_sys_t *p_sys;
950 p_sys = (tls_server_sys_t *)p_server->p_sys;
952 vlc_mutex_destroy( &p_sys->cache_lock );
953 free( p_sys->p_cache );
955 vlc_object_detach( p_server );
956 vlc_object_destroy( p_server );
958 /* all sessions depending on the server are now deinitialized */
959 gnutls_certificate_free_credentials( p_sys->x509_cred );
960 gnutls_dh_params_deinit( p_sys->dh_params );
966 * Adds one or more certificate authorities.
968 * @param psz_ca_path (Unicode) path to an x509 certificates list.
970 * @return -1 on error, 0 on success.
971 *****************************************************************************/
973 gnutls_ServerAddCA( tls_server_t *p_server, const char *psz_ca_path )
975 tls_server_sys_t *p_sys;
976 char *psz_local_path;
979 p_sys = (tls_server_sys_t *)(p_server->p_sys);
981 psz_local_path = ToLocale( psz_ca_path );
982 val = gnutls_certificate_set_x509_trust_file( p_sys->x509_cred,
984 GNUTLS_X509_FMT_PEM );
985 LocaleFree( psz_local_path );
988 msg_Err( p_server, "cannot add trusted CA (%s): %s", psz_ca_path,
989 gnutls_strerror( val ) );
992 msg_Dbg( p_server, " %d trusted CA added (%s)", val, psz_ca_path );
994 /* enables peer's certificate verification */
995 p_sys->pf_handshake2 = gnutls_HandshakeAndValidate;
1002 * Adds a certificates revocation list to be sent to TLS clients.
1004 * @param psz_crl_path (Unicode) path of the CRL file.
1006 * @return -1 on error, 0 on success.
1009 gnutls_ServerAddCRL( tls_server_t *p_server, const char *psz_crl_path )
1012 char *psz_local_path = ToLocale( psz_crl_path );
1014 val = gnutls_certificate_set_x509_crl_file( ((tls_server_sys_t *)
1015 (p_server->p_sys))->x509_cred,
1017 GNUTLS_X509_FMT_PEM );
1018 LocaleFree( psz_crl_path );
1021 msg_Err( p_server, "cannot add CRL (%s): %s", psz_crl_path,
1022 gnutls_strerror( val ) );
1023 return VLC_EGENERIC;
1025 msg_Dbg( p_server, "%d CRL added (%s)", val, psz_crl_path );
1031 * Allocates a whole server's TLS credentials.
1033 * @return NULL on error.
1035 static tls_server_t *
1036 gnutls_ServerCreate( tls_t *p_tls, const char *psz_cert_path,
1037 const char *psz_key_path )
1039 tls_server_t *p_server;
1040 tls_server_sys_t *p_sys;
1041 char *psz_local_key, *psz_local_cert;
1044 msg_Dbg( p_tls, "creating TLS server" );
1046 p_sys = (tls_server_sys_t *)malloc( sizeof(struct tls_server_sys_t) );
1050 p_sys->i_cache_size = get_Int( p_tls, "gnutls-cache-size" );
1051 p_sys->p_cache = (struct saved_session_t *)calloc( p_sys->i_cache_size,
1052 sizeof( struct saved_session_t ) );
1053 if( p_sys->p_cache == NULL )
1058 p_sys->p_store = p_sys->p_cache;
1060 p_server = vlc_object_create( p_tls, sizeof(struct tls_server_t) );
1061 if( p_server == NULL )
1063 free( p_sys->p_cache );
1068 vlc_object_attach( p_server, p_tls );
1070 p_server->p_sys = p_sys;
1071 p_server->pf_delete = gnutls_ServerDelete;
1072 p_server->pf_add_CA = gnutls_ServerAddCA;
1073 p_server->pf_add_CRL = gnutls_ServerAddCRL;
1074 p_server->pf_session_prepare = gnutls_ServerSessionPrepare;
1076 /* No certificate validation by default */
1077 p_sys->pf_handshake2 = gnutls_ContinueHandshake;
1079 vlc_mutex_init( p_server, &p_sys->cache_lock );
1081 /* Sets server's credentials */
1082 val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
1085 msg_Err( p_server, "cannot allocate X509 credentials: %s",
1086 gnutls_strerror( val ) );
1090 psz_local_cert = ToLocale( psz_cert_path );
1091 psz_local_key = ToLocale( psz_key_path );
1092 val = gnutls_certificate_set_x509_key_file( p_sys->x509_cred,
1093 psz_local_cert, psz_local_key,
1094 GNUTLS_X509_FMT_PEM );
1095 LocaleFree( psz_cert_path );
1096 LocaleFree( psz_key_path );
1099 msg_Err( p_server, "cannot set certificate chain or private key: %s",
1100 gnutls_strerror( val ) );
1101 gnutls_certificate_free_credentials( p_sys->x509_cred );
1106 * - regenerate these regularly
1107 * - support other ciper suites
1109 val = gnutls_dh_params_init( &p_sys->dh_params );
1112 msg_Dbg( p_server, "computing Diffie Hellman ciphers parameters" );
1113 val = gnutls_dh_params_generate2( p_sys->dh_params,
1114 get_Int( p_tls, "gnutls-dh-bits" ) );
1118 msg_Err( p_server, "cannot initialize DH cipher suites: %s",
1119 gnutls_strerror( val ) );
1120 gnutls_certificate_free_credentials( p_sys->x509_cred );
1123 msg_Dbg( p_server, "ciphers parameters computed" );
1125 gnutls_certificate_set_dh_params( p_sys->x509_cred, p_sys->dh_params);
1130 vlc_mutex_destroy( &p_sys->cache_lock );
1131 vlc_object_detach( p_server );
1132 vlc_object_destroy( p_server );
1138 #ifdef LIBVLC_USE_PTHREAD
1139 GCRY_THREAD_OPTION_PTHREAD_IMPL;
1140 # define gcry_threads_vlc gcry_threads_pthread
1143 * gcrypt thread option VLC implementation
1146 # define NEED_THREAD_CONTEXT 1
1147 static vlc_object_t *__p_gcry_data;
1149 static int gcry_vlc_mutex_init( void **p_sys )
1152 vlc_mutex_t *p_lock = (vlc_mutex_t *)malloc( sizeof( vlc_mutex_t ) );
1157 i_val = vlc_mutex_init( __p_gcry_data, p_lock );
1165 static int gcry_vlc_mutex_destroy( void **p_sys )
1168 vlc_mutex_t *p_lock = (vlc_mutex_t *)*p_sys;
1170 i_val = vlc_mutex_destroy( p_lock );
1175 static int gcry_vlc_mutex_lock( void **p_sys )
1177 return vlc_mutex_lock( (vlc_mutex_t *)*p_sys );
1180 static int gcry_vlc_mutex_unlock( void **lock )
1182 return vlc_mutex_unlock( (vlc_mutex_t *)*lock );
1185 static struct gcry_thread_cbs gcry_threads_vlc =
1187 GCRY_THREAD_OPTION_USER,
1189 gcry_vlc_mutex_init,
1190 gcry_vlc_mutex_destroy,
1191 gcry_vlc_mutex_lock,
1192 gcry_vlc_mutex_unlock
1197 /*****************************************************************************
1198 * Module initialization
1199 *****************************************************************************/
1201 Open( vlc_object_t *p_this )
1203 tls_t *p_tls = (tls_t *)p_this;
1205 vlc_value_t lock, count;
1207 var_Create( p_this->p_libvlc_global, "gnutls_mutex", VLC_VAR_MUTEX );
1208 var_Get( p_this->p_libvlc_global, "gnutls_mutex", &lock );
1209 vlc_mutex_lock( lock.p_address );
1211 /* Initialize GnuTLS only once */
1212 var_Create( p_this->p_libvlc_global, "gnutls_count", VLC_VAR_INTEGER );
1213 var_Get( p_this->p_libvlc_global, "gnutls_count", &count);
1215 if( count.i_int == 0)
1217 #ifdef NEED_THREAD_CONTEXT
1218 __p_gcry_data = VLC_OBJECT( p_this->p_libvlc );
1221 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_vlc);
1222 if( gnutls_global_init( ) )
1224 msg_Warn( p_this, "cannot initialize GnuTLS" );
1225 vlc_mutex_unlock( lock.p_address );
1226 return VLC_EGENERIC;
1229 const char *psz_version = gnutls_check_version( "1.2.9" );
1230 if( psz_version == NULL )
1232 gnutls_global_deinit( );
1233 vlc_mutex_unlock( lock.p_address );
1234 msg_Err( p_this, "unsupported GnuTLS version" );
1235 return VLC_EGENERIC;
1237 msg_Dbg( p_this, "GnuTLS v%s initialized", psz_version );
1241 var_Set( p_this->p_libvlc_global, "gnutls_count", count);
1242 vlc_mutex_unlock( lock.p_address );
1244 p_tls->pf_server_create = gnutls_ServerCreate;
1245 p_tls->pf_client_create = gnutls_ClientCreate;
1250 /*****************************************************************************
1251 * Module deinitialization
1252 *****************************************************************************/
1254 Close( vlc_object_t *p_this )
1256 /*tls_t *p_tls = (tls_t *)p_this;
1257 tls_sys_t *p_sys = (tls_sys_t *)(p_this->p_sys);*/
1259 vlc_value_t lock, count;
1261 var_Create( p_this->p_libvlc_global, "gnutls_mutex", VLC_VAR_MUTEX );
1262 var_Get( p_this->p_libvlc_global, "gnutls_mutex", &lock );
1263 vlc_mutex_lock( lock.p_address );
1265 var_Create( p_this->p_libvlc_global, "gnutls_count", VLC_VAR_INTEGER );
1266 var_Get( p_this->p_libvlc_global, "gnutls_count", &count);
1268 var_Set( p_this->p_libvlc_global, "gnutls_count", count);
1270 if( count.i_int == 0 )
1272 gnutls_global_deinit( );
1273 msg_Dbg( p_this, "GnuTLS deinitialized" );
1276 vlc_mutex_unlock( lock.p_address );