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 "This allows you to modify the Diffie-Hellman prime's number of bits, " \
70 "used for TLS or SSL-based server-side encryption. This is generally " \
73 #define CACHE_EXPIRATION_TEXT N_("Expiration time for resumed TLS sessions")
74 #define CACHE_EXPIRATION_LONGTEXT N_( \
75 "It is possible to cache the resumed TLS sessions. This is the expiration "\
76 "time of the sessions stored in this cache, in seconds." )
78 #define CACHE_SIZE_TEXT N_("Number of resumed TLS sessions")
79 #define CACHE_SIZE_LONGTEXT N_( \
80 "This is the maximum number of resumed TLS sessions that " \
81 "the cache will hold." )
83 #define CHECK_CERT_TEXT N_("Check TLS/SSL server certificate validity")
84 #define CHECK_CERT_LONGTEXT N_( \
85 "This ensures that the server certificate is valid " \
86 "(i.e. signed by an approved Certification Authority)." )
88 #define CHECK_HOSTNAME_TEXT N_("Check TLS/SSL server hostname in certificate")
89 #define CHECK_HOSTNAME_LONGTEXT N_( \
90 "This ensures that the server hostname in certificate matches the " \
91 "requested host name." )
94 set_shortname( "GnuTLS" );
95 set_description( _("GnuTLS TLS encryption layer") );
96 set_capability( "tls", 1 );
97 set_callbacks( Open, Close );
98 set_category( CAT_ADVANCED );
99 set_subcategory( SUBCAT_ADVANCED_MISC );
101 add_bool( "tls-check-cert", VLC_TRUE, NULL, CHECK_CERT_TEXT,
102 CHECK_CERT_LONGTEXT, VLC_FALSE );
103 add_bool( "tls-check-hostname", VLC_TRUE, NULL, CHECK_HOSTNAME_TEXT,
104 CHECK_HOSTNAME_LONGTEXT, VLC_FALSE );
106 add_integer( "gnutls-dh-bits", DH_BITS, NULL, DH_BITS_TEXT,
107 DH_BITS_LONGTEXT, VLC_TRUE );
108 add_integer( "gnutls-cache-expiration", CACHE_EXPIRATION, NULL,
109 CACHE_EXPIRATION_TEXT, CACHE_EXPIRATION_LONGTEXT, VLC_TRUE );
110 add_integer( "gnutls-cache-size", CACHE_SIZE, NULL, CACHE_SIZE_TEXT,
111 CACHE_SIZE_LONGTEXT, VLC_TRUE );
115 #define MAX_SESSION_ID 32
116 #define MAX_SESSION_DATA 1024
118 typedef struct saved_session_t
120 char id[MAX_SESSION_ID];
121 char data[MAX_SESSION_DATA];
128 typedef struct tls_server_sys_t
130 gnutls_certificate_credentials x509_cred;
131 gnutls_dh_params dh_params;
133 struct saved_session_t *p_cache;
134 struct saved_session_t *p_store;
136 vlc_mutex_t cache_lock;
138 int (*pf_handshake2)( tls_session_t * );
142 typedef struct tls_session_sys_t
144 gnutls_session session;
146 vlc_bool_t b_handshaked;
150 typedef struct tls_client_sys_t
152 struct tls_session_sys_t session;
153 gnutls_certificate_credentials x509_cred;
158 _get_Int( vlc_object_t *p_this, const char *var )
162 if( var_Get( p_this, var, &value ) != VLC_SUCCESS )
164 var_Create( p_this, var, VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
165 var_Get( p_this, var, &value );
172 _get_Bool( vlc_object_t *p_this, const char *var )
176 if( var_Get( p_this, var, &value ) != VLC_SUCCESS )
178 var_Create( p_this, var, VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
179 var_Get( p_this, var, &value );
185 #define get_Int( a, b ) _get_Int( (vlc_object_t *)(a), (b) )
186 #define get_Bool( a, b ) _get_Bool( (vlc_object_t *)(a), (b) )
190 * Sends data through a TLS session.
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;
207 * Receives data through a TLS session.
210 gnutls_Recv( void *p_session, void *buf, int i_length )
213 tls_session_sys_t *p_sys;
215 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
217 val = gnutls_record_recv( p_sys->session, buf, i_length );
218 /* TODO: handle fatal error */
219 return val < 0 ? -1 : val;
223 /*****************************************************************************
224 * tls_Session(Continue)?Handshake:
225 *****************************************************************************
226 * Establishes TLS session with a peer through socket <fd>.
227 * Returns -1 on error (you need not and must not call tls_SessionClose)
228 * 0 on succesful handshake completion, 1 if more would-be blocking recv is
229 * needed, 2 if more would-be blocking send is required.
230 *****************************************************************************/
232 gnutls_ContinueHandshake( tls_session_t *p_session)
234 tls_session_sys_t *p_sys;
237 p_sys = (tls_session_sys_t *)(p_session->p_sys);
239 /* TODO: handle fatal error */
241 WSASetLastError( 0 );
243 val = gnutls_handshake( p_sys->session );
244 if( ( val == GNUTLS_E_AGAIN ) || ( val == GNUTLS_E_INTERRUPTED ) )
245 return 1 + gnutls_record_get_direction( p_sys->session );
250 msg_Dbg( p_session, "Winsock error %d", WSAGetLastError( ) );
252 msg_Err( p_session, "TLS handshake failed: %s",
253 gnutls_strerror( val ) );
254 p_session->pf_close( p_session );
258 p_sys->b_handshaked = VLC_TRUE;
263 gnutls_VerifyHostname( vlc_object_t *p_this, gnutls_session session,
264 const char *psz_hostname )
266 const gnutls_datum *p_data;
267 gnutls_x509_crt cert;
271 /* certificate (host)name verification */
272 p_data = gnutls_certificate_get_peers( session, &status );
275 msg_Err( p_this, "TLS peer certificate not available" );
279 val = gnutls_x509_crt_init( &cert );
282 msg_Err( p_this, "x509 fatal error: %s", gnutls_strerror( val ) );
286 val = gnutls_x509_crt_import( cert, p_data, GNUTLS_X509_FMT_DER );
289 msg_Err( p_this, "x509 certificate import error: %s",
290 gnutls_strerror( val ) );
291 gnutls_x509_crt_deinit( cert );
295 if( gnutls_x509_crt_check_hostname( cert, psz_hostname ) == 0 )
297 msg_Err( p_this, "x509 certificate does not match \"%s\"",
299 gnutls_x509_crt_deinit( cert );
303 gnutls_x509_crt_deinit( cert );
304 msg_Dbg( p_this, "x509 hostname matches %s", psz_hostname );
309 gnutls_HandshakeAndValidate( tls_session_t *p_session )
313 val = gnutls_ContinueHandshake( p_session );
317 tls_session_sys_t *p_sys;
319 p_sys = (tls_session_sys_t *)(p_session->p_sys);
320 /* certificates chain verification */
321 val = gnutls_certificate_verify_peers2( p_sys->session, &status );
325 msg_Err( p_session, "TLS certificate verification failed: %s",
326 gnutls_strerror( val ) );
327 p_session->pf_close( p_session );
333 msg_Warn( p_session, "TLS session: access denied" );
334 if( status & GNUTLS_CERT_INVALID )
335 msg_Dbg( p_session, "certificate could not be verified" );
336 if( status & GNUTLS_CERT_REVOKED )
337 msg_Dbg( p_session, "certificate was revoked" );
338 if( status & GNUTLS_CERT_SIGNER_NOT_FOUND )
339 msg_Dbg( p_session, "certificate's signer was not found" );
340 if( status & GNUTLS_CERT_SIGNER_NOT_CA )
341 msg_Dbg( p_session, "certificate's signer is not a CA" );
342 p_session->pf_close( p_session );
346 msg_Dbg( p_session, "TLS certificate verified" );
347 if( ( p_sys->psz_hostname != NULL )
348 && gnutls_VerifyHostname( (vlc_object_t *)p_session, p_sys->session,
349 p_sys->psz_hostname ) )
351 p_session->pf_close( p_session );
361 * Starts negociation of a TLS session.
363 * @param fd stream socket already connected with the peer.
364 * @param psz_hostname if not NULL, hostname to mention as a Server Name.
366 * @return -1 on error (you need not and must not call tls_SessionClose),
367 * 0 on succesful handshake completion, 1 if more would-be blocking recv is
368 * needed, 2 if more would-be blocking send is required.
371 gnutls_BeginHandshake( tls_session_t *p_session, int fd,
372 const char *psz_hostname )
374 tls_session_sys_t *p_sys;
376 p_sys = (tls_session_sys_t *)(p_session->p_sys);
378 gnutls_transport_set_ptr (p_sys->session, (gnutls_transport_ptr)(intptr_t)fd);
380 if( psz_hostname != NULL )
382 gnutls_server_name_set( p_sys->session, GNUTLS_NAME_DNS, psz_hostname,
383 strlen( psz_hostname ) );
384 if( get_Bool( p_session, "tls-check-hostname" ) )
386 p_sys->psz_hostname = strdup( psz_hostname );
387 if( p_sys->psz_hostname == NULL )
389 p_session->pf_close( p_session );
395 return p_session->pf_handshake2( p_session );
399 * Terminates TLS session and releases session data.
400 * You still have to close the socket yourself.
403 gnutls_SessionClose( tls_session_t *p_session )
405 tls_session_sys_t *p_sys;
407 p_sys = (tls_session_sys_t *)(p_session->p_sys);
409 if( p_sys->b_handshaked == VLC_TRUE )
410 gnutls_bye( p_sys->session, GNUTLS_SHUT_WR );
411 gnutls_deinit( p_sys->session );
413 if( p_sys->psz_hostname != NULL )
414 free( p_sys->psz_hostname );
416 vlc_object_detach( p_session );
417 vlc_object_destroy( p_session );
423 gnutls_ClientDelete( tls_session_t *p_session )
425 /* On the client-side, credentials are re-allocated per session */
426 gnutls_certificate_credentials x509_cred =
427 ((tls_client_sys_t *)(p_session->p_sys))->x509_cred;
429 gnutls_SessionClose( p_session );
431 /* credentials must be free'd *after* gnutls_deinit() */
432 gnutls_certificate_free_credentials( x509_cred );
437 gnutls_Addx509File( vlc_object_t *p_this,
438 gnutls_certificate_credentials cred,
439 const char *psz_path, vlc_bool_t b_priv );
442 gnutls_Addx509Directory( vlc_object_t *p_this,
443 gnutls_certificate_credentials cred,
444 const char *psz_dirname,
448 const char *psz_dirent;
450 if( *psz_dirname == '\0' )
453 dir = utf8_opendir( psz_dirname );
456 msg_Warn( p_this, "cannot open directory (%s): %s", psz_dirname,
463 struct stat st1, st2;
464 int fd = dirfd( dir );
467 * Gets stats for the directory path, checks that it is not a
468 * symbolic link (to avoid possibly infinite recursion), and verifies
469 * that the inode is still the same, to avoid TOCTOU race condition.
472 || fstat( fd, &st1 ) || utf8_lstat( psz_dirname, &st2 )
473 || S_ISLNK( st2.st_mode ) || ( st1.st_ino != st2.st_ino ) )
481 while( ( psz_dirent = utf8_readdir( dir ) ) != NULL )
486 if( ( strcmp( ".", psz_dirent ) == 0 )
487 || ( strcmp( "..", psz_dirent ) == 0 ) )
490 check = asprintf( &psz_filename, "%s/%s", psz_dirname,
492 LocaleFree( psz_dirent );
496 gnutls_Addx509File( p_this, cred, psz_filename, b_priv );
497 free( psz_filename );
506 gnutls_Addx509File( vlc_object_t *p_this,
507 gnutls_certificate_credentials cred,
508 const char *psz_path, vlc_bool_t b_priv )
512 if( utf8_stat( psz_path, &st ) == 0 )
514 if( S_ISREG( st.st_mode ) )
516 char *psz_localname = ToLocale( psz_path );
518 ? gnutls_certificate_set_x509_key_file( cred,
519 psz_localname, psz_localname, GNUTLS_X509_FMT_PEM )
520 : gnutls_certificate_set_x509_trust_file( cred,
521 psz_localname, GNUTLS_X509_FMT_PEM );
522 LocaleFree( psz_localname );
526 msg_Warn( p_this, "cannot add x509 credentials (%s): %s",
527 psz_path, gnutls_strerror( i ) );
532 msg_Dbg( p_this, "added x509 credentials (%s)",
537 else if( S_ISDIR( st.st_mode ) )
540 "looking recursively for x509 credentials in %s",
542 return gnutls_Addx509Directory( p_this, cred, psz_path, b_priv);
546 msg_Warn( p_this, "cannot add x509 credentials (%s): %s",
547 psz_path, strerror( errno ) );
553 * Initializes a client-side TLS session.
555 static tls_session_t *
556 gnutls_ClientCreate( tls_t *p_tls )
558 tls_session_t *p_session = NULL;
559 tls_client_sys_t *p_sys = NULL;
561 const int cert_type_priority[3] =
567 p_sys = (tls_client_sys_t *)malloc( sizeof(struct tls_client_sys_t) );
571 p_session = (struct tls_session_t *)vlc_object_create ( p_tls, sizeof(struct tls_session_t) );
572 if( p_session == NULL )
578 p_session->p_sys = p_sys;
579 p_session->sock.p_sys = p_session;
580 p_session->sock.pf_send = gnutls_Send;
581 p_session->sock.pf_recv = gnutls_Recv;
582 p_session->pf_handshake = gnutls_BeginHandshake;
583 p_session->pf_close = gnutls_ClientDelete;
585 p_sys->session.b_handshaked = VLC_FALSE;
586 p_sys->session.psz_hostname = NULL;
588 vlc_object_attach( p_session, p_tls );
590 i_val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
593 msg_Err( p_tls, "cannot allocate X509 credentials: %s",
594 gnutls_strerror( i_val ) );
598 if( get_Bool( p_tls, "tls-check-cert" ) )
602 if( asprintf( &psz_path, "%s/"CONFIG_DIR"/ssl/certs",
603 p_tls->p_vlc->psz_homedir ) != -1 )
605 gnutls_Addx509Directory( (vlc_object_t *)p_session,
606 p_sys->x509_cred, psz_path, VLC_FALSE );
610 if( asprintf( &psz_path, "%s/ca-certificates.crt",
611 config_GetDataDir ( (vlc_object_t *)p_session) ) != -1 )
613 gnutls_Addx509File( (vlc_object_t *)p_session,
614 p_sys->x509_cred, psz_path, VLC_FALSE );
617 p_session->pf_handshake2 = gnutls_HandshakeAndValidate;
620 p_session->pf_handshake2 = gnutls_ContinueHandshake;
625 if( asprintf( &psz_path, "%s/"CONFIG_DIR"/ssl/private",
626 p_tls->p_vlc->psz_homedir ) == -1 )
628 gnutls_certificate_free_credentials( p_sys->x509_cred );
632 gnutls_Addx509Directory( (vlc_object_t *)p_session, p_sys->x509_cred,
633 psz_path, VLC_TRUE );
638 i_val = gnutls_init( &p_sys->session.session, GNUTLS_CLIENT );
641 msg_Err( p_tls, "cannot initialize TLS session: %s",
642 gnutls_strerror( i_val ) );
643 gnutls_certificate_free_credentials( p_sys->x509_cred );
647 i_val = gnutls_set_default_priority( p_sys->session.session );
650 msg_Err( p_tls, "cannot set ciphers priorities: %s",
651 gnutls_strerror( i_val ) );
652 gnutls_deinit( p_sys->session.session );
653 gnutls_certificate_free_credentials( p_sys->x509_cred );
657 i_val = gnutls_certificate_type_set_priority( p_sys->session.session,
658 cert_type_priority );
661 msg_Err( p_tls, "cannot set certificate type priorities: %s",
662 gnutls_strerror( i_val ) );
663 gnutls_deinit( p_sys->session.session );
664 gnutls_certificate_free_credentials( p_sys->x509_cred );
668 i_val = gnutls_credentials_set( p_sys->session.session,
669 GNUTLS_CRD_CERTIFICATE,
673 msg_Err( p_tls, "cannot set TLS session credentials: %s",
674 gnutls_strerror( i_val ) );
675 gnutls_deinit( p_sys->session.session );
676 gnutls_certificate_free_credentials( p_sys->x509_cred );
683 vlc_object_detach( p_session );
684 vlc_object_destroy( p_session );
692 * TLS session resumption callbacks (server-side)
694 static int cb_store( void *p_server, gnutls_datum key, gnutls_datum data )
696 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
698 if( ( p_sys->i_cache_size == 0 )
699 || ( key.size > MAX_SESSION_ID )
700 || ( data.size > MAX_SESSION_DATA ) )
703 vlc_mutex_lock( &p_sys->cache_lock );
705 memcpy( p_sys->p_store->id, key.data, key.size);
706 memcpy( p_sys->p_store->data, data.data, data.size );
707 p_sys->p_store->i_idlen = key.size;
708 p_sys->p_store->i_datalen = data.size;
711 if( ( p_sys->p_store - p_sys->p_cache ) == p_sys->i_cache_size )
712 p_sys->p_store = p_sys->p_cache;
714 vlc_mutex_unlock( &p_sys->cache_lock );
720 static const gnutls_datum err_datum = { NULL, 0 };
722 static gnutls_datum cb_fetch( void *p_server, gnutls_datum key )
724 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
725 saved_session_t *p_session, *p_end;
727 p_session = p_sys->p_cache;
728 p_end = p_session + p_sys->i_cache_size;
730 vlc_mutex_lock( &p_sys->cache_lock );
732 while( p_session < p_end )
734 if( ( p_session->i_idlen == key.size )
735 && !memcmp( p_session->id, key.data, key.size ) )
739 data.size = p_session->i_datalen;
741 data.data = gnutls_malloc( data.size );
742 if( data.data == NULL )
744 vlc_mutex_unlock( &p_sys->cache_lock );
748 memcpy( data.data, p_session->data, data.size );
749 vlc_mutex_unlock( &p_sys->cache_lock );
755 vlc_mutex_unlock( &p_sys->cache_lock );
761 static int cb_delete( void *p_server, gnutls_datum key )
763 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
764 saved_session_t *p_session, *p_end;
766 p_session = p_sys->p_cache;
767 p_end = p_session + p_sys->i_cache_size;
769 vlc_mutex_lock( &p_sys->cache_lock );
771 while( p_session < p_end )
773 if( ( p_session->i_idlen == key.size )
774 && !memcmp( p_session->id, key.data, key.size ) )
776 p_session->i_datalen = p_session->i_idlen = 0;
777 vlc_mutex_unlock( &p_sys->cache_lock );
783 vlc_mutex_unlock( &p_sys->cache_lock );
790 * Initializes a server-side TLS session.
792 static tls_session_t *
793 gnutls_ServerSessionPrepare( tls_server_t *p_server )
795 tls_session_t *p_session;
796 tls_server_sys_t *p_server_sys;
797 gnutls_session session;
800 p_session = vlc_object_create( p_server, sizeof (struct tls_session_t) );
801 if( p_session == NULL )
804 p_session->p_sys = malloc( sizeof(struct tls_session_sys_t) );
805 if( p_session->p_sys == NULL )
807 vlc_object_destroy( p_session );
811 vlc_object_attach( p_session, p_server );
813 p_server_sys = (tls_server_sys_t *)p_server->p_sys;
814 p_session->sock.p_sys = p_session;
815 p_session->sock.pf_send = gnutls_Send;
816 p_session->sock.pf_recv = gnutls_Recv;
817 p_session->pf_handshake = gnutls_BeginHandshake;
818 p_session->pf_handshake2 = p_server_sys->pf_handshake2;
819 p_session->pf_close = gnutls_SessionClose;
821 ((tls_session_sys_t *)p_session->p_sys)->b_handshaked = VLC_FALSE;
822 ((tls_session_sys_t *)p_session->p_sys)->psz_hostname = NULL;
824 i_val = gnutls_init( &session, GNUTLS_SERVER );
827 msg_Err( p_server, "cannot initialize TLS session: %s",
828 gnutls_strerror( i_val ) );
832 ((tls_session_sys_t *)p_session->p_sys)->session = session;
834 i_val = gnutls_set_default_priority( session );
837 msg_Err( p_server, "cannot set ciphers priorities: %s",
838 gnutls_strerror( i_val ) );
839 gnutls_deinit( session );
843 i_val = gnutls_credentials_set( session, GNUTLS_CRD_CERTIFICATE,
844 p_server_sys->x509_cred );
847 msg_Err( p_server, "cannot set TLS session credentials: %s",
848 gnutls_strerror( i_val ) );
849 gnutls_deinit( session );
853 if( p_session->pf_handshake2 == gnutls_HandshakeAndValidate )
854 gnutls_certificate_server_set_request( session, GNUTLS_CERT_REQUIRE );
856 gnutls_dh_set_prime_bits( session, get_Int( p_server, "gnutls-dh-bits" ) );
858 /* Session resumption support */
859 gnutls_db_set_cache_expiration( session, get_Int( p_server,
860 "gnutls-cache-expiration" ) );
861 gnutls_db_set_retrieve_function( session, cb_fetch );
862 gnutls_db_set_remove_function( session, cb_delete );
863 gnutls_db_set_store_function( session, cb_store );
864 gnutls_db_set_ptr( session, p_server );
869 free( p_session->p_sys );
870 vlc_object_detach( p_session );
871 vlc_object_destroy( p_session );
877 * Releases data allocated with tls_ServerCreate().
880 gnutls_ServerDelete( tls_server_t *p_server )
882 tls_server_sys_t *p_sys;
883 p_sys = (tls_server_sys_t *)p_server->p_sys;
885 vlc_mutex_destroy( &p_sys->cache_lock );
886 free( p_sys->p_cache );
888 vlc_object_detach( p_server );
889 vlc_object_destroy( p_server );
891 /* all sessions depending on the server are now deinitialized */
892 gnutls_certificate_free_credentials( p_sys->x509_cred );
893 gnutls_dh_params_deinit( p_sys->dh_params );
899 * Adds one or more certificate authorities.
901 * @param psz_ca_path (Unicode) path to an x509 certificates list.
903 * @return -1 on error, 0 on success.
904 *****************************************************************************/
906 gnutls_ServerAddCA( tls_server_t *p_server, const char *psz_ca_path )
908 tls_server_sys_t *p_sys;
909 char *psz_local_path;
912 p_sys = (tls_server_sys_t *)(p_server->p_sys);
914 psz_local_path = ToLocale( psz_ca_path );
915 val = gnutls_certificate_set_x509_trust_file( p_sys->x509_cred,
917 GNUTLS_X509_FMT_PEM );
918 LocaleFree( psz_local_path );
921 msg_Err( p_server, "cannot add trusted CA (%s): %s", psz_ca_path,
922 gnutls_strerror( val ) );
925 msg_Dbg( p_server, " %d trusted CA added (%s)", val, psz_ca_path );
927 /* enables peer's certificate verification */
928 p_sys->pf_handshake2 = gnutls_HandshakeAndValidate;
935 * Adds a certificates revocation list to be sent to TLS clients.
937 * @param psz_crl_path (Unicode) path of the CRL file.
939 * @return -1 on error, 0 on success.
942 gnutls_ServerAddCRL( tls_server_t *p_server, const char *psz_crl_path )
945 char *psz_local_path = ToLocale( psz_crl_path );
947 val = gnutls_certificate_set_x509_crl_file( ((tls_server_sys_t *)
948 (p_server->p_sys))->x509_cred,
950 GNUTLS_X509_FMT_PEM );
951 LocaleFree( psz_crl_path );
954 msg_Err( p_server, "cannot add CRL (%s): %s", psz_crl_path,
955 gnutls_strerror( val ) );
958 msg_Dbg( p_server, "%d CRL added (%s)", val, psz_crl_path );
964 * Allocates a whole server's TLS credentials.
966 * @return NULL on error.
968 static tls_server_t *
969 gnutls_ServerCreate( tls_t *p_tls, const char *psz_cert_path,
970 const char *psz_key_path )
972 tls_server_t *p_server;
973 tls_server_sys_t *p_sys;
974 char *psz_local_key, *psz_local_cert;
977 msg_Dbg( p_tls, "creating TLS server" );
979 p_sys = (tls_server_sys_t *)malloc( sizeof(struct tls_server_sys_t) );
983 p_sys->i_cache_size = get_Int( p_tls, "gnutls-cache-size" );
984 p_sys->p_cache = (struct saved_session_t *)calloc( p_sys->i_cache_size,
985 sizeof( struct saved_session_t ) );
986 if( p_sys->p_cache == NULL )
991 p_sys->p_store = p_sys->p_cache;
993 p_server = vlc_object_create( p_tls, sizeof(struct tls_server_t) );
994 if( p_server == NULL )
996 free( p_sys->p_cache );
1001 vlc_object_attach( p_server, p_tls );
1003 p_server->p_sys = p_sys;
1004 p_server->pf_delete = gnutls_ServerDelete;
1005 p_server->pf_add_CA = gnutls_ServerAddCA;
1006 p_server->pf_add_CRL = gnutls_ServerAddCRL;
1007 p_server->pf_session_prepare = gnutls_ServerSessionPrepare;
1009 /* No certificate validation by default */
1010 p_sys->pf_handshake2 = gnutls_ContinueHandshake;
1012 /* FIXME: check for errors */
1013 vlc_mutex_init( p_server, &p_sys->cache_lock );
1015 /* Sets server's credentials */
1016 val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
1019 msg_Err( p_server, "cannot allocate X509 credentials: %s",
1020 gnutls_strerror( val ) );
1024 psz_local_cert = ToLocale( psz_cert_path );
1025 psz_local_key = ToLocale( psz_key_path );
1026 val = gnutls_certificate_set_x509_key_file( p_sys->x509_cred,
1027 psz_local_cert, psz_local_key,
1028 GNUTLS_X509_FMT_PEM );
1029 LocaleFree( psz_cert_path );
1030 LocaleFree( psz_key_path );
1033 msg_Err( p_server, "cannot set certificate chain or private key: %s",
1034 gnutls_strerror( val ) );
1035 gnutls_certificate_free_credentials( p_sys->x509_cred );
1040 * - regenerate these regularly
1041 * - support other ciper suites
1043 val = gnutls_dh_params_init( &p_sys->dh_params );
1046 msg_Dbg( p_server, "computing Diffie Hellman ciphers parameters" );
1047 val = gnutls_dh_params_generate2( p_sys->dh_params,
1048 get_Int( p_tls, "gnutls-dh-bits" ) );
1052 msg_Err( p_server, "cannot initialize DH cipher suites: %s",
1053 gnutls_strerror( val ) );
1054 gnutls_certificate_free_credentials( p_sys->x509_cred );
1057 msg_Dbg( p_server, "ciphers parameters computed" );
1059 gnutls_certificate_set_dh_params( p_sys->x509_cred, p_sys->dh_params);
1064 vlc_mutex_destroy( &p_sys->cache_lock );
1065 vlc_object_detach( p_server );
1066 vlc_object_destroy( p_server );
1073 * gcrypt thread option VLC implementation
1075 vlc_object_t *__p_gcry_data;
1077 static int gcry_vlc_mutex_init( void **p_sys )
1080 vlc_mutex_t *p_lock = (vlc_mutex_t *)malloc( sizeof( vlc_mutex_t ) );
1085 i_val = vlc_mutex_init( __p_gcry_data, p_lock );
1093 static int gcry_vlc_mutex_destroy( void **p_sys )
1096 vlc_mutex_t *p_lock = (vlc_mutex_t *)*p_sys;
1098 i_val = vlc_mutex_destroy( p_lock );
1103 static int gcry_vlc_mutex_lock( void **p_sys )
1105 return vlc_mutex_lock( (vlc_mutex_t *)*p_sys );
1108 static int gcry_vlc_mutex_unlock( void **lock )
1110 return vlc_mutex_unlock( (vlc_mutex_t *)*lock );
1113 static struct gcry_thread_cbs gcry_threads_vlc =
1115 GCRY_THREAD_OPTION_USER,
1117 gcry_vlc_mutex_init,
1118 gcry_vlc_mutex_destroy,
1119 gcry_vlc_mutex_lock,
1120 gcry_vlc_mutex_unlock
1124 /*****************************************************************************
1125 * Module initialization
1126 *****************************************************************************/
1128 Open( vlc_object_t *p_this )
1130 tls_t *p_tls = (tls_t *)p_this;
1132 vlc_value_t lock, count;
1134 var_Create( p_this->p_libvlc, "gnutls_mutex", VLC_VAR_MUTEX );
1135 var_Get( p_this->p_libvlc, "gnutls_mutex", &lock );
1136 vlc_mutex_lock( lock.p_address );
1138 /* Initialize GnuTLS only once */
1139 var_Create( p_this->p_libvlc, "gnutls_count", VLC_VAR_INTEGER );
1140 var_Get( p_this->p_libvlc, "gnutls_count", &count);
1142 if( count.i_int == 0)
1144 const char *psz_version;
1146 __p_gcry_data = VLC_OBJECT( p_this->p_vlc );
1148 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_vlc);
1149 if( gnutls_global_init( ) )
1151 msg_Warn( p_this, "cannot initialize GnuTLS" );
1152 vlc_mutex_unlock( lock.p_address );
1153 return VLC_EGENERIC;
1156 * FIXME: in fact, we currently depends on 1.0.17, but it breaks on
1157 * Debian which as a patched 1.0.16 (which we can use).
1159 psz_version = gnutls_check_version( "1.0.16" );
1160 if( psz_version == NULL )
1162 gnutls_global_deinit( );
1163 vlc_mutex_unlock( lock.p_address );
1164 msg_Err( p_this, "unsupported GnuTLS version" );
1165 return VLC_EGENERIC;
1167 msg_Dbg( p_this, "GnuTLS v%s initialized", psz_version );
1171 var_Set( p_this->p_libvlc, "gnutls_count", count);
1172 vlc_mutex_unlock( lock.p_address );
1174 p_tls->pf_server_create = gnutls_ServerCreate;
1175 p_tls->pf_client_create = gnutls_ClientCreate;
1180 /*****************************************************************************
1181 * Module deinitialization
1182 *****************************************************************************/
1184 Close( vlc_object_t *p_this )
1186 /*tls_t *p_tls = (tls_t *)p_this;
1187 tls_sys_t *p_sys = (tls_sys_t *)(p_this->p_sys);*/
1189 vlc_value_t lock, count;
1191 var_Create( p_this->p_libvlc, "gnutls_mutex", VLC_VAR_MUTEX );
1192 var_Get( p_this->p_libvlc, "gnutls_mutex", &lock );
1193 vlc_mutex_lock( lock.p_address );
1195 var_Create( p_this->p_libvlc, "gnutls_count", VLC_VAR_INTEGER );
1196 var_Get( p_this->p_libvlc, "gnutls_count", &count);
1198 var_Set( p_this->p_libvlc, "gnutls_count", count);
1200 if( count.i_int == 0 )
1202 gnutls_global_deinit( );
1203 msg_Dbg( p_this, "GnuTLS deinitialized" );
1206 vlc_mutex_unlock( lock.p_address );