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 *****************************************************************************/
32 #include <sys/types.h>
37 #ifdef HAVE_SYS_STAT_H
38 # include <sys/stat.h>
49 #include <gnutls/gnutls.h>
50 #include <gnutls/x509.h>
53 #define CACHE_EXPIRATION 3600
56 /*****************************************************************************
58 *****************************************************************************/
59 static int Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
62 #define DH_BITS_TEXT N_("Diffie-Hellman prime bits")
63 #define DH_BITS_LONGTEXT N_( \
64 "This allows you to modify the Diffie-Hellman prime's number of bits, " \
65 "used for TLS or SSL-based server-side encryption. This is generally " \
68 #define CACHE_EXPIRATION_TEXT N_("Expiration time for resumed TLS sessions")
69 #define CACHE_EXPIRATION_LONGTEXT N_( \
70 "It is possible to cache the resumed TLS sessions. This is the expiration "\
71 "time of the sessions stored in this cache, in seconds." )
73 #define CACHE_SIZE_TEXT N_("Number of resumed TLS sessions")
74 #define CACHE_SIZE_LONGTEXT N_( \
75 "This is the maximum number of resumed TLS sessions that " \
76 "the cache will hold." )
78 #define CHECK_CERT_TEXT N_("Check TLS/SSL server certificate validity")
79 #define CHECK_CERT_LONGTEXT N_( \
80 "This ensures that the server certificate is valid " \
81 "(i.e. signed by an approved Certification Authority)." )
83 #define CHECK_HOSTNAME_TEXT N_("Check TLS/SSL server hostname in certificate")
84 #define CHECK_HOSTNAME_LONGTEXT N_( \
85 "This ensures that the server hostname in certificate matches the " \
86 "requested host name." )
89 set_shortname( "GnuTLS" );
90 set_description( _("GnuTLS TLS encryption layer") );
91 set_capability( "tls", 1 );
92 set_callbacks( Open, Close );
93 set_category( CAT_ADVANCED );
94 set_subcategory( SUBCAT_ADVANCED_MISC );
96 add_bool( "tls-check-cert", VLC_TRUE, NULL, CHECK_CERT_TEXT,
97 CHECK_CERT_LONGTEXT, VLC_FALSE );
98 add_bool( "tls-check-hostname", VLC_TRUE, NULL, CHECK_HOSTNAME_TEXT,
99 CHECK_HOSTNAME_LONGTEXT, VLC_FALSE );
101 add_integer( "gnutls-dh-bits", DH_BITS, NULL, DH_BITS_TEXT,
102 DH_BITS_LONGTEXT, VLC_TRUE );
103 add_integer( "gnutls-cache-expiration", CACHE_EXPIRATION, NULL,
104 CACHE_EXPIRATION_TEXT, CACHE_EXPIRATION_LONGTEXT, VLC_TRUE );
105 add_integer( "gnutls-cache-size", CACHE_SIZE, NULL, CACHE_SIZE_TEXT,
106 CACHE_SIZE_LONGTEXT, VLC_TRUE );
110 #define MAX_SESSION_ID 32
111 #define MAX_SESSION_DATA 1024
113 typedef struct saved_session_t
115 char id[MAX_SESSION_ID];
116 char data[MAX_SESSION_DATA];
123 typedef struct tls_server_sys_t
125 gnutls_certificate_credentials x509_cred;
126 gnutls_dh_params dh_params;
128 struct saved_session_t *p_cache;
129 struct saved_session_t *p_store;
131 vlc_mutex_t cache_lock;
133 int (*pf_handshake2)( tls_session_t * );
137 typedef struct tls_session_sys_t
139 gnutls_session session;
141 vlc_bool_t b_handshaked;
145 typedef struct tls_client_sys_t
147 struct tls_session_sys_t session;
148 gnutls_certificate_credentials x509_cred;
153 _get_Int( vlc_object_t *p_this, const char *var )
157 if( var_Get( p_this, var, &value ) != VLC_SUCCESS )
159 var_Create( p_this, var, VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
160 var_Get( p_this, var, &value );
167 _get_Bool( vlc_object_t *p_this, const char *var )
171 if( var_Get( p_this, var, &value ) != VLC_SUCCESS )
173 var_Create( p_this, var, VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
174 var_Get( p_this, var, &value );
180 #define get_Int( a, b ) _get_Int( (vlc_object_t *)(a), (b) )
181 #define get_Bool( a, b ) _get_Bool( (vlc_object_t *)(a), (b) )
185 * Sends data through a TLS session.
188 gnutls_Send( void *p_session, const void *buf, int i_length )
191 tls_session_sys_t *p_sys;
193 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
195 val = gnutls_record_send( p_sys->session, buf, i_length );
196 /* TODO: handle fatal error */
197 return val < 0 ? -1 : val;
202 * Receives data through a TLS session.
205 gnutls_Recv( void *p_session, void *buf, int i_length )
208 tls_session_sys_t *p_sys;
210 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
212 val = gnutls_record_recv( p_sys->session, buf, i_length );
213 /* TODO: handle fatal error */
214 return val < 0 ? -1 : val;
218 /*****************************************************************************
219 * tls_Session(Continue)?Handshake:
220 *****************************************************************************
221 * Establishes TLS session with a peer through socket <fd>.
222 * Returns -1 on error (you need not and must not call tls_SessionClose)
223 * 0 on succesful handshake completion, 1 if more would-be blocking recv is
224 * needed, 2 if more would-be blocking send is required.
225 *****************************************************************************/
227 gnutls_ContinueHandshake( tls_session_t *p_session)
229 tls_session_sys_t *p_sys;
232 p_sys = (tls_session_sys_t *)(p_session->p_sys);
234 /* TODO: handle fatal error */
236 WSASetLastError( 0 );
238 val = gnutls_handshake( p_sys->session );
239 if( ( val == GNUTLS_E_AGAIN ) || ( val == GNUTLS_E_INTERRUPTED ) )
240 return 1 + gnutls_record_get_direction( p_sys->session );
245 msg_Dbg( p_session, "Winsock error %d", WSAGetLastError( ) );
247 msg_Err( p_session, "TLS handshake failed: %s",
248 gnutls_strerror( val ) );
249 p_session->pf_close( p_session );
253 p_sys->b_handshaked = VLC_TRUE;
258 gnutls_VerifyHostname( vlc_object_t *p_this, gnutls_session session,
259 const char *psz_hostname )
261 const gnutls_datum *p_data;
262 gnutls_x509_crt cert;
266 /* certificate (host)name verification */
267 p_data = gnutls_certificate_get_peers( session, &status );
270 msg_Err( p_this, "TLS peer certificate not available" );
274 val = gnutls_x509_crt_init( &cert );
277 msg_Err( p_this, "x509 fatal error: %s", gnutls_strerror( val ) );
281 val = gnutls_x509_crt_import( cert, p_data, GNUTLS_X509_FMT_DER );
284 msg_Err( p_this, "x509 certificate import error: %s",
285 gnutls_strerror( val ) );
286 gnutls_x509_crt_deinit( cert );
290 if( gnutls_x509_crt_check_hostname( cert, psz_hostname ) == 0 )
292 msg_Err( p_this, "x509 certificate does not match \"%s\"",
294 gnutls_x509_crt_deinit( cert );
298 gnutls_x509_crt_deinit( cert );
299 msg_Dbg( p_this, "x509 hostname matches %s", psz_hostname );
310 static const error_msg_t cert_errors[] =
312 { GNUTLS_CERT_INVALID,
313 "Certificate could not be verified" },
314 { GNUTLS_CERT_REVOKED,
315 "Certificate was revoked" },
316 { GNUTLS_CERT_SIGNER_NOT_FOUND,
317 "Certificate's signer was not found" },
318 { GNUTLS_CERT_SIGNER_NOT_CA,
319 "Certificate's signer is not a CA" },
320 { GNUTLS_CERT_INSECURE_ALGORITHM,
321 "Insecure certificate signature algorithm" },
327 gnutls_HandshakeAndValidate( tls_session_t *session )
329 int val = gnutls_ContinueHandshake( session );
333 tls_session_sys_t *p_sys = (tls_session_sys_t *)(session->p_sys);
335 /* certificates chain verification */
337 val = gnutls_certificate_verify_peers2( p_sys->session, &status );
341 msg_Err( session, "Certificate verification failed: %s",
342 gnutls_strerror( val ) );
348 msg_Err( session, "TLS session: access denied" );
349 for( const error_msg_t *e = cert_errors; e->flag; e++ )
351 if( status & e->flag )
353 msg_Err( session, e->msg );
360 "unknown certificate error (you found a bug in VLC)" );
365 /* certificate (host)name verification */
366 const gnutls_datum *data = gnutls_certificate_get_peers( p_sys->session,
370 msg_Err( session, "Peer certificate not available" );
374 gnutls_x509_crt cert;
375 val = gnutls_x509_crt_init( &cert );
378 msg_Err( session, "x509 fatal error: %s", gnutls_strerror( val ) );
382 val = gnutls_x509_crt_import( cert, data, GNUTLS_X509_FMT_DER );
385 msg_Err( session, "Certificate import error: %s",
386 gnutls_strerror( val ) );
387 gnutls_x509_crt_deinit( cert );
391 if( p_sys->psz_hostname != NULL )
393 if ( !gnutls_x509_crt_check_hostname( cert, p_sys->psz_hostname ) )
395 msg_Err( session, "Certificate does not match \"%s\"",
396 p_sys->psz_hostname );
401 msg_Warn( session, "Certificate and hostname were not verified" );
403 if( gnutls_x509_crt_get_expiration_time( cert ) < time( NULL ) )
405 msg_Err( session, "Certificate expired" );
409 if( gnutls_x509_crt_get_activation_time( cert ) > time ( NULL ) )
411 msg_Err( session, "Certificate not yet valid" );
415 gnutls_x509_crt_deinit( cert );
416 msg_Dbg( session, "TLS/x509 certificate verified" );
420 gnutls_x509_crt_deinit( cert );
422 session->pf_close( session );
427 * Starts negociation of a TLS session.
429 * @param fd stream socket already connected with the peer.
430 * @param psz_hostname if not NULL, hostname to mention as a Server Name.
432 * @return -1 on error (you need not and must not call tls_SessionClose),
433 * 0 on succesful handshake completion, 1 if more would-be blocking recv is
434 * needed, 2 if more would-be blocking send is required.
437 gnutls_BeginHandshake( tls_session_t *p_session, int fd,
438 const char *psz_hostname )
440 tls_session_sys_t *p_sys;
442 p_sys = (tls_session_sys_t *)(p_session->p_sys);
444 gnutls_transport_set_ptr (p_sys->session, (gnutls_transport_ptr)(intptr_t)fd);
446 if( psz_hostname != NULL )
448 gnutls_server_name_set( p_sys->session, GNUTLS_NAME_DNS, psz_hostname,
449 strlen( psz_hostname ) );
450 if( get_Bool( p_session, "tls-check-hostname" ) )
452 p_sys->psz_hostname = strdup( psz_hostname );
453 if( p_sys->psz_hostname == NULL )
455 p_session->pf_close( p_session );
461 return p_session->pf_handshake2( p_session );
465 * Terminates TLS session and releases session data.
466 * You still have to close the socket yourself.
469 gnutls_SessionClose( tls_session_t *p_session )
471 tls_session_sys_t *p_sys;
473 p_sys = (tls_session_sys_t *)(p_session->p_sys);
475 if( p_sys->b_handshaked == VLC_TRUE )
476 gnutls_bye( p_sys->session, GNUTLS_SHUT_WR );
477 gnutls_deinit( p_sys->session );
479 if( p_sys->psz_hostname != NULL )
480 free( p_sys->psz_hostname );
482 vlc_object_detach( p_session );
483 vlc_object_destroy( p_session );
489 gnutls_ClientDelete( tls_session_t *p_session )
491 /* On the client-side, credentials are re-allocated per session */
492 gnutls_certificate_credentials x509_cred =
493 ((tls_client_sys_t *)(p_session->p_sys))->x509_cred;
495 gnutls_SessionClose( p_session );
497 /* credentials must be free'd *after* gnutls_deinit() */
498 gnutls_certificate_free_credentials( x509_cred );
503 gnutls_Addx509File( vlc_object_t *p_this,
504 gnutls_certificate_credentials cred,
505 const char *psz_path, vlc_bool_t b_priv );
508 gnutls_Addx509Directory( vlc_object_t *p_this,
509 gnutls_certificate_credentials cred,
510 const char *psz_dirname,
514 const char *psz_dirent;
516 if( *psz_dirname == '\0' )
519 dir = utf8_opendir( psz_dirname );
522 msg_Warn( p_this, "cannot open directory (%s): %s", psz_dirname,
529 struct stat st1, st2;
530 int fd = dirfd( dir );
533 * Gets stats for the directory path, checks that it is not a
534 * symbolic link (to avoid possibly infinite recursion), and verifies
535 * that the inode is still the same, to avoid TOCTOU race condition.
538 || fstat( fd, &st1 ) || utf8_lstat( psz_dirname, &st2 )
539 || S_ISLNK( st2.st_mode ) || ( st1.st_ino != st2.st_ino ) )
547 while( ( psz_dirent = utf8_readdir( dir ) ) != NULL )
552 if( ( strcmp( ".", psz_dirent ) == 0 )
553 || ( strcmp( "..", psz_dirent ) == 0 ) )
556 check = asprintf( &psz_filename, "%s/%s", psz_dirname,
558 LocaleFree( psz_dirent );
562 gnutls_Addx509File( p_this, cred, psz_filename, b_priv );
563 free( psz_filename );
572 gnutls_Addx509File( vlc_object_t *p_this,
573 gnutls_certificate_credentials cred,
574 const char *psz_path, vlc_bool_t b_priv )
578 if( utf8_stat( psz_path, &st ) == 0 )
580 if( S_ISREG( st.st_mode ) )
582 char *psz_localname = ToLocale( psz_path );
584 ? gnutls_certificate_set_x509_key_file( cred,
585 psz_localname, psz_localname, GNUTLS_X509_FMT_PEM )
586 : gnutls_certificate_set_x509_trust_file( cred,
587 psz_localname, GNUTLS_X509_FMT_PEM );
588 LocaleFree( psz_localname );
592 msg_Warn( p_this, "cannot add x509 credentials (%s): %s",
593 psz_path, gnutls_strerror( i ) );
598 msg_Dbg( p_this, "added x509 credentials (%s)",
603 else if( S_ISDIR( st.st_mode ) )
606 "looking recursively for x509 credentials in %s",
608 return gnutls_Addx509Directory( p_this, cred, psz_path, b_priv);
612 msg_Warn( p_this, "cannot add x509 credentials (%s): %s",
613 psz_path, strerror( errno ) );
619 * Initializes a client-side TLS session.
621 static tls_session_t *
622 gnutls_ClientCreate( tls_t *p_tls )
624 tls_session_t *p_session = NULL;
625 tls_client_sys_t *p_sys = NULL;
627 const int cert_type_priority[3] =
633 p_sys = (tls_client_sys_t *)malloc( sizeof(struct tls_client_sys_t) );
637 p_session = (struct tls_session_t *)vlc_object_create ( p_tls, sizeof(struct tls_session_t) );
638 if( p_session == NULL )
644 p_session->p_sys = p_sys;
645 p_session->sock.p_sys = p_session;
646 p_session->sock.pf_send = gnutls_Send;
647 p_session->sock.pf_recv = gnutls_Recv;
648 p_session->pf_handshake = gnutls_BeginHandshake;
649 p_session->pf_close = gnutls_ClientDelete;
651 p_sys->session.b_handshaked = VLC_FALSE;
652 p_sys->session.psz_hostname = NULL;
654 vlc_object_attach( p_session, p_tls );
656 i_val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
659 msg_Err( p_tls, "cannot allocate X509 credentials: %s",
660 gnutls_strerror( i_val ) );
664 if( get_Bool( p_tls, "tls-check-cert" ) )
668 if( asprintf( &psz_path, "%s/"CONFIG_DIR"/ssl/certs",
669 p_tls->p_libvlc->psz_homedir ) != -1 )
671 gnutls_Addx509Directory( (vlc_object_t *)p_session,
672 p_sys->x509_cred, psz_path, VLC_FALSE );
676 if( asprintf( &psz_path, "%s/ca-certificates.crt",
677 config_GetDataDir ( (vlc_object_t *)p_session) ) != -1 )
679 gnutls_Addx509File( (vlc_object_t *)p_session,
680 p_sys->x509_cred, psz_path, VLC_FALSE );
683 p_session->pf_handshake2 = gnutls_HandshakeAndValidate;
686 p_session->pf_handshake2 = gnutls_ContinueHandshake;
691 if( asprintf( &psz_path, "%s/"CONFIG_DIR"/ssl/private",
692 p_tls->p_libvlc->psz_homedir ) == -1 )
694 gnutls_certificate_free_credentials( p_sys->x509_cred );
698 gnutls_Addx509Directory( (vlc_object_t *)p_session, p_sys->x509_cred,
699 psz_path, VLC_TRUE );
704 i_val = gnutls_init( &p_sys->session.session, GNUTLS_CLIENT );
707 msg_Err( p_tls, "cannot initialize TLS session: %s",
708 gnutls_strerror( i_val ) );
709 gnutls_certificate_free_credentials( p_sys->x509_cred );
713 i_val = gnutls_set_default_priority( p_sys->session.session );
716 msg_Err( p_tls, "cannot set ciphers priorities: %s",
717 gnutls_strerror( i_val ) );
718 gnutls_deinit( p_sys->session.session );
719 gnutls_certificate_free_credentials( p_sys->x509_cred );
723 i_val = gnutls_certificate_type_set_priority( p_sys->session.session,
724 cert_type_priority );
727 msg_Err( p_tls, "cannot set certificate type priorities: %s",
728 gnutls_strerror( i_val ) );
729 gnutls_deinit( p_sys->session.session );
730 gnutls_certificate_free_credentials( p_sys->x509_cred );
734 i_val = gnutls_credentials_set( p_sys->session.session,
735 GNUTLS_CRD_CERTIFICATE,
739 msg_Err( p_tls, "cannot set TLS session credentials: %s",
740 gnutls_strerror( i_val ) );
741 gnutls_deinit( p_sys->session.session );
742 gnutls_certificate_free_credentials( p_sys->x509_cred );
749 vlc_object_detach( p_session );
750 vlc_object_destroy( p_session );
758 * TLS session resumption callbacks (server-side)
760 static int cb_store( void *p_server, gnutls_datum key, gnutls_datum data )
762 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
764 if( ( p_sys->i_cache_size == 0 )
765 || ( key.size > MAX_SESSION_ID )
766 || ( data.size > MAX_SESSION_DATA ) )
769 vlc_mutex_lock( &p_sys->cache_lock );
771 memcpy( p_sys->p_store->id, key.data, key.size);
772 memcpy( p_sys->p_store->data, data.data, data.size );
773 p_sys->p_store->i_idlen = key.size;
774 p_sys->p_store->i_datalen = data.size;
777 if( ( p_sys->p_store - p_sys->p_cache ) == p_sys->i_cache_size )
778 p_sys->p_store = p_sys->p_cache;
780 vlc_mutex_unlock( &p_sys->cache_lock );
786 static const gnutls_datum err_datum = { NULL, 0 };
788 static gnutls_datum cb_fetch( void *p_server, gnutls_datum key )
790 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
791 saved_session_t *p_session, *p_end;
793 p_session = p_sys->p_cache;
794 p_end = p_session + p_sys->i_cache_size;
796 vlc_mutex_lock( &p_sys->cache_lock );
798 while( p_session < p_end )
800 if( ( p_session->i_idlen == key.size )
801 && !memcmp( p_session->id, key.data, key.size ) )
805 data.size = p_session->i_datalen;
807 data.data = gnutls_malloc( data.size );
808 if( data.data == NULL )
810 vlc_mutex_unlock( &p_sys->cache_lock );
814 memcpy( data.data, p_session->data, data.size );
815 vlc_mutex_unlock( &p_sys->cache_lock );
821 vlc_mutex_unlock( &p_sys->cache_lock );
827 static int cb_delete( void *p_server, gnutls_datum key )
829 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
830 saved_session_t *p_session, *p_end;
832 p_session = p_sys->p_cache;
833 p_end = p_session + p_sys->i_cache_size;
835 vlc_mutex_lock( &p_sys->cache_lock );
837 while( p_session < p_end )
839 if( ( p_session->i_idlen == key.size )
840 && !memcmp( p_session->id, key.data, key.size ) )
842 p_session->i_datalen = p_session->i_idlen = 0;
843 vlc_mutex_unlock( &p_sys->cache_lock );
849 vlc_mutex_unlock( &p_sys->cache_lock );
856 * Initializes a server-side TLS session.
858 static tls_session_t *
859 gnutls_ServerSessionPrepare( tls_server_t *p_server )
861 tls_session_t *p_session;
862 tls_server_sys_t *p_server_sys;
863 gnutls_session session;
866 p_session = vlc_object_create( p_server, sizeof (struct tls_session_t) );
867 if( p_session == NULL )
870 p_session->p_sys = malloc( sizeof(struct tls_session_sys_t) );
871 if( p_session->p_sys == NULL )
873 vlc_object_destroy( p_session );
877 vlc_object_attach( p_session, p_server );
879 p_server_sys = (tls_server_sys_t *)p_server->p_sys;
880 p_session->sock.p_sys = p_session;
881 p_session->sock.pf_send = gnutls_Send;
882 p_session->sock.pf_recv = gnutls_Recv;
883 p_session->pf_handshake = gnutls_BeginHandshake;
884 p_session->pf_handshake2 = p_server_sys->pf_handshake2;
885 p_session->pf_close = gnutls_SessionClose;
887 ((tls_session_sys_t *)p_session->p_sys)->b_handshaked = VLC_FALSE;
888 ((tls_session_sys_t *)p_session->p_sys)->psz_hostname = NULL;
890 i_val = gnutls_init( &session, GNUTLS_SERVER );
893 msg_Err( p_server, "cannot initialize TLS session: %s",
894 gnutls_strerror( i_val ) );
898 ((tls_session_sys_t *)p_session->p_sys)->session = session;
900 i_val = gnutls_set_default_priority( session );
903 msg_Err( p_server, "cannot set ciphers priorities: %s",
904 gnutls_strerror( i_val ) );
905 gnutls_deinit( session );
909 i_val = gnutls_credentials_set( session, GNUTLS_CRD_CERTIFICATE,
910 p_server_sys->x509_cred );
913 msg_Err( p_server, "cannot set TLS session credentials: %s",
914 gnutls_strerror( i_val ) );
915 gnutls_deinit( session );
919 if( p_session->pf_handshake2 == gnutls_HandshakeAndValidate )
920 gnutls_certificate_server_set_request( session, GNUTLS_CERT_REQUIRE );
922 gnutls_dh_set_prime_bits( session, get_Int( p_server, "gnutls-dh-bits" ) );
924 /* Session resumption support */
925 gnutls_db_set_cache_expiration( session, get_Int( p_server,
926 "gnutls-cache-expiration" ) );
927 gnutls_db_set_retrieve_function( session, cb_fetch );
928 gnutls_db_set_remove_function( session, cb_delete );
929 gnutls_db_set_store_function( session, cb_store );
930 gnutls_db_set_ptr( session, p_server );
935 free( p_session->p_sys );
936 vlc_object_detach( p_session );
937 vlc_object_destroy( p_session );
943 * Releases data allocated with tls_ServerCreate().
946 gnutls_ServerDelete( tls_server_t *p_server )
948 tls_server_sys_t *p_sys;
949 p_sys = (tls_server_sys_t *)p_server->p_sys;
951 vlc_mutex_destroy( &p_sys->cache_lock );
952 free( p_sys->p_cache );
954 vlc_object_detach( p_server );
955 vlc_object_destroy( p_server );
957 /* all sessions depending on the server are now deinitialized */
958 gnutls_certificate_free_credentials( p_sys->x509_cred );
959 gnutls_dh_params_deinit( p_sys->dh_params );
965 * Adds one or more certificate authorities.
967 * @param psz_ca_path (Unicode) path to an x509 certificates list.
969 * @return -1 on error, 0 on success.
970 *****************************************************************************/
972 gnutls_ServerAddCA( tls_server_t *p_server, const char *psz_ca_path )
974 tls_server_sys_t *p_sys;
975 char *psz_local_path;
978 p_sys = (tls_server_sys_t *)(p_server->p_sys);
980 psz_local_path = ToLocale( psz_ca_path );
981 val = gnutls_certificate_set_x509_trust_file( p_sys->x509_cred,
983 GNUTLS_X509_FMT_PEM );
984 LocaleFree( psz_local_path );
987 msg_Err( p_server, "cannot add trusted CA (%s): %s", psz_ca_path,
988 gnutls_strerror( val ) );
991 msg_Dbg( p_server, " %d trusted CA added (%s)", val, psz_ca_path );
993 /* enables peer's certificate verification */
994 p_sys->pf_handshake2 = gnutls_HandshakeAndValidate;
1001 * Adds a certificates revocation list to be sent to TLS clients.
1003 * @param psz_crl_path (Unicode) path of the CRL file.
1005 * @return -1 on error, 0 on success.
1008 gnutls_ServerAddCRL( tls_server_t *p_server, const char *psz_crl_path )
1011 char *psz_local_path = ToLocale( psz_crl_path );
1013 val = gnutls_certificate_set_x509_crl_file( ((tls_server_sys_t *)
1014 (p_server->p_sys))->x509_cred,
1016 GNUTLS_X509_FMT_PEM );
1017 LocaleFree( psz_crl_path );
1020 msg_Err( p_server, "cannot add CRL (%s): %s", psz_crl_path,
1021 gnutls_strerror( val ) );
1022 return VLC_EGENERIC;
1024 msg_Dbg( p_server, "%d CRL added (%s)", val, psz_crl_path );
1030 * Allocates a whole server's TLS credentials.
1032 * @return NULL on error.
1034 static tls_server_t *
1035 gnutls_ServerCreate( tls_t *p_tls, const char *psz_cert_path,
1036 const char *psz_key_path )
1038 tls_server_t *p_server;
1039 tls_server_sys_t *p_sys;
1040 char *psz_local_key, *psz_local_cert;
1043 msg_Dbg( p_tls, "creating TLS server" );
1045 p_sys = (tls_server_sys_t *)malloc( sizeof(struct tls_server_sys_t) );
1049 p_sys->i_cache_size = get_Int( p_tls, "gnutls-cache-size" );
1050 p_sys->p_cache = (struct saved_session_t *)calloc( p_sys->i_cache_size,
1051 sizeof( struct saved_session_t ) );
1052 if( p_sys->p_cache == NULL )
1057 p_sys->p_store = p_sys->p_cache;
1059 p_server = vlc_object_create( p_tls, sizeof(struct tls_server_t) );
1060 if( p_server == NULL )
1062 free( p_sys->p_cache );
1067 vlc_object_attach( p_server, p_tls );
1069 p_server->p_sys = p_sys;
1070 p_server->pf_delete = gnutls_ServerDelete;
1071 p_server->pf_add_CA = gnutls_ServerAddCA;
1072 p_server->pf_add_CRL = gnutls_ServerAddCRL;
1073 p_server->pf_session_prepare = gnutls_ServerSessionPrepare;
1075 /* No certificate validation by default */
1076 p_sys->pf_handshake2 = gnutls_ContinueHandshake;
1078 vlc_mutex_init( p_server, &p_sys->cache_lock );
1080 /* Sets server's credentials */
1081 val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
1084 msg_Err( p_server, "cannot allocate X509 credentials: %s",
1085 gnutls_strerror( val ) );
1089 psz_local_cert = ToLocale( psz_cert_path );
1090 psz_local_key = ToLocale( psz_key_path );
1091 val = gnutls_certificate_set_x509_key_file( p_sys->x509_cred,
1092 psz_local_cert, psz_local_key,
1093 GNUTLS_X509_FMT_PEM );
1094 LocaleFree( psz_cert_path );
1095 LocaleFree( psz_key_path );
1098 msg_Err( p_server, "cannot set certificate chain or private key: %s",
1099 gnutls_strerror( val ) );
1100 gnutls_certificate_free_credentials( p_sys->x509_cred );
1105 * - regenerate these regularly
1106 * - support other ciper suites
1108 val = gnutls_dh_params_init( &p_sys->dh_params );
1111 msg_Dbg( p_server, "computing Diffie Hellman ciphers parameters" );
1112 val = gnutls_dh_params_generate2( p_sys->dh_params,
1113 get_Int( p_tls, "gnutls-dh-bits" ) );
1117 msg_Err( p_server, "cannot initialize DH cipher suites: %s",
1118 gnutls_strerror( val ) );
1119 gnutls_certificate_free_credentials( p_sys->x509_cred );
1122 msg_Dbg( p_server, "ciphers parameters computed" );
1124 gnutls_certificate_set_dh_params( p_sys->x509_cred, p_sys->dh_params);
1129 vlc_mutex_destroy( &p_sys->cache_lock );
1130 vlc_object_detach( p_server );
1131 vlc_object_destroy( p_server );
1137 #ifdef LIBVLC_USE_PTHREAD
1138 GCRY_THREAD_OPTION_PTHREAD_IMPL;
1139 # define gcry_threads_vlc gcry_threads_pthread
1142 * gcrypt thread option VLC implementation
1145 # define NEED_THREAD_CONTEXT 1
1146 static vlc_object_t *__p_gcry_data;
1148 static int gcry_vlc_mutex_init( void **p_sys )
1151 vlc_mutex_t *p_lock = (vlc_mutex_t *)malloc( sizeof( vlc_mutex_t ) );
1156 i_val = vlc_mutex_init( __p_gcry_data, p_lock );
1164 static int gcry_vlc_mutex_destroy( void **p_sys )
1167 vlc_mutex_t *p_lock = (vlc_mutex_t *)*p_sys;
1169 i_val = vlc_mutex_destroy( p_lock );
1174 static int gcry_vlc_mutex_lock( void **p_sys )
1176 return vlc_mutex_lock( (vlc_mutex_t *)*p_sys );
1179 static int gcry_vlc_mutex_unlock( void **lock )
1181 return vlc_mutex_unlock( (vlc_mutex_t *)*lock );
1184 static struct gcry_thread_cbs gcry_threads_vlc =
1186 GCRY_THREAD_OPTION_USER,
1188 gcry_vlc_mutex_init,
1189 gcry_vlc_mutex_destroy,
1190 gcry_vlc_mutex_lock,
1191 gcry_vlc_mutex_unlock
1196 /*****************************************************************************
1197 * Module initialization
1198 *****************************************************************************/
1200 Open( vlc_object_t *p_this )
1202 tls_t *p_tls = (tls_t *)p_this;
1204 vlc_value_t lock, count;
1206 var_Create( p_this->p_libvlc_global, "gnutls_mutex", VLC_VAR_MUTEX );
1207 var_Get( p_this->p_libvlc_global, "gnutls_mutex", &lock );
1208 vlc_mutex_lock( lock.p_address );
1210 /* Initialize GnuTLS only once */
1211 var_Create( p_this->p_libvlc_global, "gnutls_count", VLC_VAR_INTEGER );
1212 var_Get( p_this->p_libvlc_global, "gnutls_count", &count);
1214 if( count.i_int == 0)
1216 #ifdef NEED_THREAD_CONTEXT
1217 __p_gcry_data = VLC_OBJECT( p_this->p_libvlc );
1220 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_vlc);
1221 if( gnutls_global_init( ) )
1223 msg_Warn( p_this, "cannot initialize GnuTLS" );
1224 vlc_mutex_unlock( lock.p_address );
1225 return VLC_EGENERIC;
1228 const char *psz_version = gnutls_check_version( "1.2.9" );
1229 if( psz_version == NULL )
1231 gnutls_global_deinit( );
1232 vlc_mutex_unlock( lock.p_address );
1233 msg_Err( p_this, "unsupported GnuTLS version" );
1234 return VLC_EGENERIC;
1236 msg_Dbg( p_this, "GnuTLS v%s initialized", psz_version );
1240 var_Set( p_this->p_libvlc_global, "gnutls_count", count);
1241 vlc_mutex_unlock( lock.p_address );
1243 p_tls->pf_server_create = gnutls_ServerCreate;
1244 p_tls->pf_client_create = gnutls_ClientCreate;
1249 /*****************************************************************************
1250 * Module deinitialization
1251 *****************************************************************************/
1253 Close( vlc_object_t *p_this )
1255 /*tls_t *p_tls = (tls_t *)p_this;
1256 tls_sys_t *p_sys = (tls_sys_t *)(p_this->p_sys);*/
1258 vlc_value_t lock, count;
1260 var_Create( p_this->p_libvlc_global, "gnutls_mutex", VLC_VAR_MUTEX );
1261 var_Get( p_this->p_libvlc_global, "gnutls_mutex", &lock );
1262 vlc_mutex_lock( lock.p_address );
1264 var_Create( p_this->p_libvlc_global, "gnutls_count", VLC_VAR_INTEGER );
1265 var_Get( p_this->p_libvlc_global, "gnutls_count", &count);
1267 var_Set( p_this->p_libvlc_global, "gnutls_count", count);
1269 if( count.i_int == 0 )
1271 gnutls_global_deinit( );
1272 msg_Dbg( p_this, "GnuTLS deinitialized" );
1275 vlc_mutex_unlock( lock.p_address );