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>
46 #include <vlc_charset.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)." )
84 set_shortname( "GnuTLS" );
85 set_description( _("GnuTLS TLS encryption layer") );
86 set_capability( "tls", 1 );
87 set_callbacks( Open, Close );
88 set_category( CAT_ADVANCED );
89 set_subcategory( SUBCAT_ADVANCED_MISC );
91 add_bool( "tls-check-cert", VLC_TRUE, NULL, CHECK_CERT_TEXT,
92 CHECK_CERT_LONGTEXT, VLC_FALSE );
93 add_obsolete_bool( "tls-check-hostname" );
95 add_integer( "gnutls-dh-bits", DH_BITS, NULL, DH_BITS_TEXT,
96 DH_BITS_LONGTEXT, VLC_TRUE );
97 add_integer( "gnutls-cache-expiration", CACHE_EXPIRATION, NULL,
98 CACHE_EXPIRATION_TEXT, CACHE_EXPIRATION_LONGTEXT, VLC_TRUE );
99 add_integer( "gnutls-cache-size", CACHE_SIZE, NULL, CACHE_SIZE_TEXT,
100 CACHE_SIZE_LONGTEXT, VLC_TRUE );
104 #define MAX_SESSION_ID 32
105 #define MAX_SESSION_DATA 1024
107 typedef struct saved_session_t
109 char id[MAX_SESSION_ID];
110 char data[MAX_SESSION_DATA];
117 typedef struct tls_server_sys_t
119 gnutls_certificate_credentials x509_cred;
120 gnutls_dh_params dh_params;
122 struct saved_session_t *p_cache;
123 struct saved_session_t *p_store;
125 vlc_mutex_t cache_lock;
127 int (*pf_handshake2)( tls_session_t * );
131 typedef struct tls_session_sys_t
133 gnutls_session session;
135 vlc_bool_t b_handshaked;
139 typedef struct tls_client_sys_t
141 struct tls_session_sys_t session;
142 gnutls_certificate_credentials x509_cred;
146 static int gnutls_Error (vlc_object_t *obj, int val)
155 /* WinSock does not return EAGAIN, return EINTR instead */
157 case GNUTLS_E_INTERRUPTED:
159 WSASetLastError(WSAEINTR);
166 msg_Err (obj, "%s", gnutls_strerror (val));
168 if (!gnutls_error_is_fatal (val))
169 msg_Err (obj, "Error above should be handled");
172 WSASetLastError(WSAECONNRESET);
182 * Sends data through a TLS session.
185 gnutls_Send( void *p_session, const void *buf, int i_length )
188 tls_session_sys_t *p_sys;
190 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
192 val = gnutls_record_send( p_sys->session, buf, i_length );
193 return (val < 0) ? gnutls_Error ((vlc_object_t *)p_session, val) : val;
198 * Receives data through a TLS session.
201 gnutls_Recv( void *p_session, void *buf, int i_length )
204 tls_session_sys_t *p_sys;
206 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
208 val = gnutls_record_recv( p_sys->session, buf, i_length );
209 return (val < 0) ? gnutls_Error ((vlc_object_t *)p_session, val) : val;
213 /*****************************************************************************
214 * tls_Session(Continue)?Handshake:
215 *****************************************************************************
216 * Establishes TLS session with a peer through socket <fd>.
217 * Returns -1 on error (you need not and must not call tls_SessionClose)
218 * 0 on succesful handshake completion, 1 if more would-be blocking recv is
219 * needed, 2 if more would-be blocking send is required.
220 *****************************************************************************/
222 gnutls_ContinueHandshake( tls_session_t *p_session)
224 tls_session_sys_t *p_sys;
227 p_sys = (tls_session_sys_t *)(p_session->p_sys);
230 WSASetLastError( 0 );
232 val = gnutls_handshake( p_sys->session );
233 if( ( val == GNUTLS_E_AGAIN ) || ( val == GNUTLS_E_INTERRUPTED ) )
234 return 1 + gnutls_record_get_direction( p_sys->session );
239 msg_Dbg( p_session, "Winsock error %d", WSAGetLastError( ) );
241 msg_Err( p_session, "TLS handshake error: %s",
242 gnutls_strerror( val ) );
243 p_session->pf_close( p_session );
247 p_sys->b_handshaked = VLC_TRUE;
258 static const error_msg_t cert_errors[] =
260 { GNUTLS_CERT_INVALID,
261 "Certificate could not be verified" },
262 { GNUTLS_CERT_REVOKED,
263 "Certificate was revoked" },
264 { GNUTLS_CERT_SIGNER_NOT_FOUND,
265 "Certificate's signer was not found" },
266 { GNUTLS_CERT_SIGNER_NOT_CA,
267 "Certificate's signer is not a CA" },
268 { GNUTLS_CERT_INSECURE_ALGORITHM,
269 "Insecure certificate signature algorithm" },
275 gnutls_HandshakeAndValidate( tls_session_t *session )
277 int val = gnutls_ContinueHandshake( session );
281 tls_session_sys_t *p_sys = (tls_session_sys_t *)(session->p_sys);
283 /* certificates chain verification */
285 val = gnutls_certificate_verify_peers2( p_sys->session, &status );
289 msg_Err( session, "Certificate verification failed: %s",
290 gnutls_strerror( val ) );
296 msg_Err( session, "TLS session: access denied" );
297 for( const error_msg_t *e = cert_errors; e->flag; e++ )
299 if( status & e->flag )
301 msg_Err( session, "%s", e->msg );
308 "unknown certificate error (you found a bug in VLC)" );
313 /* certificate (host)name verification */
314 const gnutls_datum *data = gnutls_certificate_get_peers( p_sys->session,
318 msg_Err( session, "Peer certificate not available" );
322 gnutls_x509_crt cert;
323 val = gnutls_x509_crt_init( &cert );
326 msg_Err( session, "x509 fatal error: %s", gnutls_strerror( val ) );
330 val = gnutls_x509_crt_import( cert, data, GNUTLS_X509_FMT_DER );
333 msg_Err( session, "Certificate import error: %s",
334 gnutls_strerror( val ) );
338 if( p_sys->psz_hostname != NULL )
340 if ( !gnutls_x509_crt_check_hostname( cert, p_sys->psz_hostname ) )
342 msg_Err( session, "Certificate does not match \"%s\"",
343 p_sys->psz_hostname );
348 msg_Warn( session, "Certificate and hostname were not verified" );
350 if( gnutls_x509_crt_get_expiration_time( cert ) < time( NULL ) )
352 msg_Err( session, "Certificate expired" );
356 if( gnutls_x509_crt_get_activation_time( cert ) > time ( NULL ) )
358 msg_Err( session, "Certificate not yet valid" );
362 gnutls_x509_crt_deinit( cert );
363 msg_Dbg( session, "TLS/x509 certificate verified" );
367 gnutls_x509_crt_deinit( cert );
369 session->pf_close( session );
374 * Starts negociation of a TLS session.
376 * @param fd stream socket already connected with the peer.
377 * @param psz_hostname if not NULL, hostname to mention as a Server Name,
378 * and to be found in the server's certificate.
380 * @return -1 on error (you need not and must not call tls_SessionClose),
381 * 0 on succesful handshake completion, 1 if more would-be blocking recv is
382 * needed, 2 if more would-be blocking send is required.
385 gnutls_BeginHandshake( tls_session_t *p_session, int fd,
386 const char *psz_hostname )
388 tls_session_sys_t *p_sys;
390 p_sys = (tls_session_sys_t *)(p_session->p_sys);
392 gnutls_transport_set_ptr (p_sys->session, (gnutls_transport_ptr)(intptr_t)fd);
394 if( psz_hostname != NULL )
396 gnutls_server_name_set (p_sys->session, GNUTLS_NAME_DNS, psz_hostname,
397 strlen (psz_hostname));
398 p_sys->psz_hostname = strdup (psz_hostname);
399 if (p_sys->psz_hostname == NULL)
401 p_session->pf_close (p_session);
406 return p_session->pf_handshake2( p_session );
410 * Terminates TLS session and releases session data.
411 * You still have to close the socket yourself.
414 gnutls_SessionClose( tls_session_t *p_session )
416 tls_session_sys_t *p_sys;
418 p_sys = (tls_session_sys_t *)(p_session->p_sys);
420 if( p_sys->b_handshaked == VLC_TRUE )
421 gnutls_bye( p_sys->session, GNUTLS_SHUT_WR );
422 gnutls_deinit( p_sys->session );
424 if( p_sys->psz_hostname != NULL )
425 free( p_sys->psz_hostname );
427 vlc_object_detach( p_session );
428 vlc_object_destroy( p_session );
434 typedef int (*tls_prio_func) (gnutls_session_t, const int *);
437 gnutls_SetPriority (vlc_object_t *restrict obj, const char *restrict name,
438 tls_prio_func func, gnutls_session_t session,
439 const int *restrict values)
441 int val = func (session, values);
444 msg_Err (obj, "cannot set %s priorities: %s", name,
445 gnutls_strerror (val));
453 gnutls_SessionPrioritize (vlc_object_t *obj, gnutls_session_t session)
455 /* Note that ordering matters (on the client side) */
456 static const int protos[] =
463 static const int comps[] =
469 static const int macs[] =
472 GNUTLS_MAC_RMD160, // RIPEMD
478 static const int ciphers[] =
480 GNUTLS_CIPHER_AES_256_CBC,
481 GNUTLS_CIPHER_AES_128_CBC,
482 GNUTLS_CIPHER_3DES_CBC,
483 GNUTLS_CIPHER_ARCFOUR_128,
484 //GNUTLS_CIPHER_DES_CBC,
485 //GNUTLS_CIPHER_ARCFOUR_40,
486 //GNUTLS_CIPHER_RC2_40_CBC,
487 //GNUTLS_CIPHER_NULL,
490 static const int kx[] =
495 //GNUTLS_KX_RSA_EXPORT,
496 //GNUTLS_KX_DHE_PSK, TODO
497 //GNUTLS_KX_PSK, TODO
498 //GNUTLS_KX_SRP_RSA, TODO
499 //GNUTLS_KX_SRP_DSS, TODO
500 //GNUTLS_KX_SRP, TODO
504 static const int cert_types[] =
507 //GNUTLS_CRT_OPENPGP, TODO
511 int val = gnutls_set_default_priority (session);
514 msg_Err (obj, "cannot set default TLS priorities: %s",
515 gnutls_strerror (val));
519 if (gnutls_SetPriority (obj, "protocols",
520 gnutls_protocol_set_priority, session, protos)
521 || gnutls_SetPriority (obj, "compression algorithms",
522 gnutls_compression_set_priority, session, comps)
523 || gnutls_SetPriority (obj, "MAC algorithms",
524 gnutls_mac_set_priority, session, macs)
525 || gnutls_SetPriority (obj, "ciphers",
526 gnutls_cipher_set_priority, session, ciphers)
527 || gnutls_SetPriority (obj, "key exchange algorithms",
528 gnutls_kx_set_priority, session, kx)
529 || gnutls_SetPriority (obj, "certificate types",
530 gnutls_certificate_type_set_priority, session,
539 gnutls_ClientDelete( tls_session_t *p_session )
541 /* On the client-side, credentials are re-allocated per session */
542 gnutls_certificate_credentials x509_cred =
543 ((tls_client_sys_t *)(p_session->p_sys))->x509_cred;
545 gnutls_SessionClose( p_session );
547 /* credentials must be free'd *after* gnutls_deinit() */
548 gnutls_certificate_free_credentials( x509_cred );
553 gnutls_Addx509File( vlc_object_t *p_this,
554 gnutls_certificate_credentials cred,
555 const char *psz_path, vlc_bool_t b_priv );
558 gnutls_Addx509Directory( vlc_object_t *p_this,
559 gnutls_certificate_credentials cred,
560 const char *psz_dirname,
565 if( *psz_dirname == '\0' )
568 dir = utf8_opendir( psz_dirname );
571 msg_Warn( p_this, "cannot open directory (%s): %m", psz_dirname );
577 struct stat st1, st2;
578 int fd = dirfd( dir );
581 * Gets stats for the directory path, checks that it is not a
582 * symbolic link (to avoid possibly infinite recursion), and verifies
583 * that the inode is still the same, to avoid TOCTOU race condition.
586 || fstat( fd, &st1 ) || utf8_lstat( psz_dirname, &st2 )
587 || S_ISLNK( st2.st_mode ) || ( st1.st_ino != st2.st_ino ) )
597 char *ent = utf8_readdir (dir);
601 if ((strcmp (ent, ".") == 0) || (strcmp (ent, "..") == 0))
604 char path[strlen (psz_dirname) + strlen (ent) + 2];
605 sprintf (path, "%s"DIR_SEP"%s", psz_dirname, ent);
608 gnutls_Addx509File( p_this, cred, path, b_priv );
617 gnutls_Addx509File( vlc_object_t *p_this,
618 gnutls_certificate_credentials cred,
619 const char *psz_path, vlc_bool_t b_priv )
623 if( utf8_stat( psz_path, &st ) == 0 )
625 if( S_ISREG( st.st_mode ) )
627 char *psz_localname = ToLocale( psz_path );
629 ? gnutls_certificate_set_x509_key_file( cred,
630 psz_localname, psz_localname, GNUTLS_X509_FMT_PEM )
631 : gnutls_certificate_set_x509_trust_file( cred,
632 psz_localname, GNUTLS_X509_FMT_PEM );
633 LocaleFree( psz_localname );
637 msg_Warn( p_this, "cannot add x509 credentials (%s): %s",
638 psz_path, gnutls_strerror( i ) );
643 msg_Dbg( p_this, "added x509 credentials (%s)",
648 else if( S_ISDIR( st.st_mode ) )
651 "looking recursively for x509 credentials in %s",
653 return gnutls_Addx509Directory( p_this, cred, psz_path, b_priv);
657 msg_Warn( p_this, "cannot add x509 credentials (%s): %m", psz_path );
663 * Initializes a client-side TLS session.
665 static tls_session_t *
666 gnutls_ClientCreate( tls_t *p_tls )
668 tls_session_t *p_session = NULL;
669 tls_client_sys_t *p_sys = NULL;
672 p_sys = (tls_client_sys_t *)malloc( sizeof(struct tls_client_sys_t) );
676 p_session = (struct tls_session_t *)vlc_object_create ( p_tls, sizeof(struct tls_session_t) );
677 if( p_session == NULL )
683 p_session->p_sys = p_sys;
684 p_session->sock.p_sys = p_session;
685 p_session->sock.pf_send = gnutls_Send;
686 p_session->sock.pf_recv = gnutls_Recv;
687 p_session->pf_handshake = gnutls_BeginHandshake;
688 p_session->pf_close = gnutls_ClientDelete;
690 p_sys->session.b_handshaked = VLC_FALSE;
691 p_sys->session.psz_hostname = NULL;
693 vlc_object_attach( p_session, p_tls );
695 const char *homedir = p_tls->p_libvlc->psz_datadir,
696 *datadir = config_GetDataDir ();
697 size_t l1 = strlen (homedir), l2 = strlen (datadir);
698 char path[((l1 > l2) ? l1 : l2) + sizeof ("/ssl/private")];
699 // > sizeof ("/ssl/certs")
700 // > sizeof ("/ca-certificates.crt")
702 i_val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
705 msg_Err( p_tls, "cannot allocate X509 credentials: %s",
706 gnutls_strerror( i_val ) );
710 if (var_CreateGetBool (p_tls, "tls-check-cert"))
712 sprintf (path, "%s/ssl/certs", homedir);
713 gnutls_Addx509Directory ((vlc_object_t *)p_session,
714 p_sys->x509_cred, path, VLC_FALSE);
716 sprintf (path, "%s/ca-certificates.crt", datadir);
717 gnutls_Addx509File ((vlc_object_t *)p_session,
718 p_sys->x509_cred, path, VLC_FALSE);
719 p_session->pf_handshake2 = gnutls_HandshakeAndValidate;
722 p_session->pf_handshake2 = gnutls_ContinueHandshake;
724 sprintf (path, "%s/ssl/private", homedir);
725 gnutls_Addx509Directory ((vlc_object_t *)p_session, p_sys->x509_cred,
728 i_val = gnutls_init( &p_sys->session.session, GNUTLS_CLIENT );
731 msg_Err( p_tls, "cannot initialize TLS session: %s",
732 gnutls_strerror( i_val ) );
733 gnutls_certificate_free_credentials( p_sys->x509_cred );
737 if (gnutls_SessionPrioritize (VLC_OBJECT (p_session),
738 p_sys->session.session))
741 i_val = gnutls_credentials_set( p_sys->session.session,
742 GNUTLS_CRD_CERTIFICATE,
746 msg_Err( p_tls, "cannot set TLS session credentials: %s",
747 gnutls_strerror( i_val ) );
754 gnutls_deinit( p_sys->session.session );
755 gnutls_certificate_free_credentials( p_sys->x509_cred );
758 vlc_object_detach( p_session );
759 vlc_object_destroy( p_session );
767 * TLS session resumption callbacks (server-side)
769 static int cb_store( void *p_server, gnutls_datum key, gnutls_datum data )
771 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
773 if( ( p_sys->i_cache_size == 0 )
774 || ( key.size > MAX_SESSION_ID )
775 || ( data.size > MAX_SESSION_DATA ) )
778 vlc_mutex_lock( &p_sys->cache_lock );
780 memcpy( p_sys->p_store->id, key.data, key.size);
781 memcpy( p_sys->p_store->data, data.data, data.size );
782 p_sys->p_store->i_idlen = key.size;
783 p_sys->p_store->i_datalen = data.size;
786 if( ( p_sys->p_store - p_sys->p_cache ) == p_sys->i_cache_size )
787 p_sys->p_store = p_sys->p_cache;
789 vlc_mutex_unlock( &p_sys->cache_lock );
795 static const gnutls_datum err_datum = { NULL, 0 };
797 static gnutls_datum cb_fetch( void *p_server, gnutls_datum key )
799 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
800 saved_session_t *p_session, *p_end;
802 p_session = p_sys->p_cache;
803 p_end = p_session + p_sys->i_cache_size;
805 vlc_mutex_lock( &p_sys->cache_lock );
807 while( p_session < p_end )
809 if( ( p_session->i_idlen == key.size )
810 && !memcmp( p_session->id, key.data, key.size ) )
814 data.size = p_session->i_datalen;
816 data.data = gnutls_malloc( data.size );
817 if( data.data == NULL )
819 vlc_mutex_unlock( &p_sys->cache_lock );
823 memcpy( data.data, p_session->data, data.size );
824 vlc_mutex_unlock( &p_sys->cache_lock );
830 vlc_mutex_unlock( &p_sys->cache_lock );
836 static int cb_delete( void *p_server, gnutls_datum key )
838 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
839 saved_session_t *p_session, *p_end;
841 p_session = p_sys->p_cache;
842 p_end = p_session + p_sys->i_cache_size;
844 vlc_mutex_lock( &p_sys->cache_lock );
846 while( p_session < p_end )
848 if( ( p_session->i_idlen == key.size )
849 && !memcmp( p_session->id, key.data, key.size ) )
851 p_session->i_datalen = p_session->i_idlen = 0;
852 vlc_mutex_unlock( &p_sys->cache_lock );
858 vlc_mutex_unlock( &p_sys->cache_lock );
865 * Initializes a server-side TLS session.
867 static tls_session_t *
868 gnutls_ServerSessionPrepare( tls_server_t *p_server )
870 tls_session_t *p_session;
871 tls_server_sys_t *p_server_sys;
872 gnutls_session session;
875 p_session = vlc_object_create( p_server, sizeof (struct tls_session_t) );
876 if( p_session == NULL )
879 p_session->p_sys = malloc( sizeof(struct tls_session_sys_t) );
880 if( p_session->p_sys == NULL )
882 vlc_object_destroy( p_session );
886 vlc_object_attach( p_session, p_server );
888 p_server_sys = (tls_server_sys_t *)p_server->p_sys;
889 p_session->sock.p_sys = p_session;
890 p_session->sock.pf_send = gnutls_Send;
891 p_session->sock.pf_recv = gnutls_Recv;
892 p_session->pf_handshake = gnutls_BeginHandshake;
893 p_session->pf_handshake2 = p_server_sys->pf_handshake2;
894 p_session->pf_close = gnutls_SessionClose;
896 ((tls_session_sys_t *)p_session->p_sys)->b_handshaked = VLC_FALSE;
897 ((tls_session_sys_t *)p_session->p_sys)->psz_hostname = NULL;
899 i_val = gnutls_init( &session, GNUTLS_SERVER );
902 msg_Err( p_server, "cannot initialize TLS session: %s",
903 gnutls_strerror( i_val ) );
907 ((tls_session_sys_t *)p_session->p_sys)->session = session;
909 if (gnutls_SessionPrioritize (VLC_OBJECT (p_session), session))
911 gnutls_deinit( session );
915 i_val = gnutls_credentials_set( session, GNUTLS_CRD_CERTIFICATE,
916 p_server_sys->x509_cred );
919 msg_Err( p_server, "cannot set TLS session credentials: %s",
920 gnutls_strerror( i_val ) );
921 gnutls_deinit( session );
925 if( p_session->pf_handshake2 == gnutls_HandshakeAndValidate )
926 gnutls_certificate_server_set_request( session, GNUTLS_CERT_REQUIRE );
928 i_val = config_GetInt (p_server, "gnutls-dh-bits");
929 gnutls_dh_set_prime_bits (session, i_val);
931 /* Session resumption support */
932 i_val = config_GetInt (p_server, "gnutls-cache-expiration");
933 gnutls_db_set_cache_expiration (session, i_val);
934 gnutls_db_set_retrieve_function( session, cb_fetch );
935 gnutls_db_set_remove_function( session, cb_delete );
936 gnutls_db_set_store_function( session, cb_store );
937 gnutls_db_set_ptr( session, p_server );
942 free( p_session->p_sys );
943 vlc_object_detach( p_session );
944 vlc_object_destroy( p_session );
950 * Releases data allocated with tls_ServerCreate().
953 gnutls_ServerDelete( tls_server_t *p_server )
955 tls_server_sys_t *p_sys;
956 p_sys = (tls_server_sys_t *)p_server->p_sys;
958 vlc_mutex_destroy( &p_sys->cache_lock );
959 free( p_sys->p_cache );
961 vlc_object_detach( p_server );
962 vlc_object_destroy( p_server );
964 /* all sessions depending on the server are now deinitialized */
965 gnutls_certificate_free_credentials( p_sys->x509_cred );
966 gnutls_dh_params_deinit( p_sys->dh_params );
972 * Adds one or more certificate authorities.
974 * @param psz_ca_path (Unicode) path to an x509 certificates list.
976 * @return -1 on error, 0 on success.
977 *****************************************************************************/
979 gnutls_ServerAddCA( tls_server_t *p_server, const char *psz_ca_path )
981 tls_server_sys_t *p_sys;
982 char *psz_local_path;
985 p_sys = (tls_server_sys_t *)(p_server->p_sys);
987 psz_local_path = ToLocale( psz_ca_path );
988 val = gnutls_certificate_set_x509_trust_file( p_sys->x509_cred,
990 GNUTLS_X509_FMT_PEM );
991 LocaleFree( psz_local_path );
994 msg_Err( p_server, "cannot add trusted CA (%s): %s", psz_ca_path,
995 gnutls_strerror( val ) );
998 msg_Dbg( p_server, " %d trusted CA added (%s)", val, psz_ca_path );
1000 /* enables peer's certificate verification */
1001 p_sys->pf_handshake2 = gnutls_HandshakeAndValidate;
1008 * Adds a certificates revocation list to be sent to TLS clients.
1010 * @param psz_crl_path (Unicode) path of the CRL file.
1012 * @return -1 on error, 0 on success.
1015 gnutls_ServerAddCRL( tls_server_t *p_server, const char *psz_crl_path )
1018 char *psz_local_path = ToLocale( psz_crl_path );
1020 val = gnutls_certificate_set_x509_crl_file( ((tls_server_sys_t *)
1021 (p_server->p_sys))->x509_cred,
1023 GNUTLS_X509_FMT_PEM );
1024 LocaleFree( psz_crl_path );
1027 msg_Err( p_server, "cannot add CRL (%s): %s", psz_crl_path,
1028 gnutls_strerror( val ) );
1029 return VLC_EGENERIC;
1031 msg_Dbg( p_server, "%d CRL added (%s)", val, psz_crl_path );
1037 * Allocates a whole server's TLS credentials.
1039 * @return NULL on error.
1041 static tls_server_t *
1042 gnutls_ServerCreate( tls_t *p_tls, const char *psz_cert_path,
1043 const char *psz_key_path )
1045 tls_server_t *p_server;
1046 tls_server_sys_t *p_sys;
1047 char *psz_local_key, *psz_local_cert;
1050 msg_Dbg( p_tls, "creating TLS server" );
1052 p_sys = (tls_server_sys_t *)malloc( sizeof(struct tls_server_sys_t) );
1056 p_sys->i_cache_size = config_GetInt (p_tls, "gnutls-cache-size");
1057 p_sys->p_cache = (struct saved_session_t *)calloc( p_sys->i_cache_size,
1058 sizeof( struct saved_session_t ) );
1059 if( p_sys->p_cache == NULL )
1064 p_sys->p_store = p_sys->p_cache;
1066 p_server = vlc_object_create( p_tls, sizeof(struct tls_server_t) );
1067 if( p_server == NULL )
1069 free( p_sys->p_cache );
1074 vlc_object_attach( p_server, p_tls );
1076 p_server->p_sys = p_sys;
1077 p_server->pf_delete = gnutls_ServerDelete;
1078 p_server->pf_add_CA = gnutls_ServerAddCA;
1079 p_server->pf_add_CRL = gnutls_ServerAddCRL;
1080 p_server->pf_session_prepare = gnutls_ServerSessionPrepare;
1082 /* No certificate validation by default */
1083 p_sys->pf_handshake2 = gnutls_ContinueHandshake;
1085 vlc_mutex_init( p_server, &p_sys->cache_lock );
1087 /* Sets server's credentials */
1088 val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
1091 msg_Err( p_server, "cannot allocate X509 credentials: %s",
1092 gnutls_strerror( val ) );
1096 psz_local_cert = ToLocale( psz_cert_path );
1097 psz_local_key = ToLocale( psz_key_path );
1098 val = gnutls_certificate_set_x509_key_file( p_sys->x509_cred,
1099 psz_local_cert, psz_local_key,
1100 GNUTLS_X509_FMT_PEM );
1101 LocaleFree( psz_cert_path );
1102 LocaleFree( psz_key_path );
1105 msg_Err( p_server, "cannot set certificate chain or private key: %s",
1106 gnutls_strerror( val ) );
1107 gnutls_certificate_free_credentials( p_sys->x509_cred );
1112 * - regenerate these regularly
1113 * - support other ciper suites
1115 val = gnutls_dh_params_init( &p_sys->dh_params );
1118 msg_Dbg( p_server, "computing Diffie Hellman ciphers parameters" );
1119 val = gnutls_dh_params_generate2( p_sys->dh_params,
1120 config_GetInt( p_tls, "gnutls-dh-bits" ) );
1124 msg_Err( p_server, "cannot initialize DH cipher suites: %s",
1125 gnutls_strerror( val ) );
1126 gnutls_certificate_free_credentials( p_sys->x509_cred );
1129 msg_Dbg( p_server, "ciphers parameters computed" );
1131 gnutls_certificate_set_dh_params( p_sys->x509_cred, p_sys->dh_params);
1136 vlc_mutex_destroy( &p_sys->cache_lock );
1137 vlc_object_detach( p_server );
1138 vlc_object_destroy( p_server );
1144 #ifdef LIBVLC_USE_PTHREAD
1145 GCRY_THREAD_OPTION_PTHREAD_IMPL;
1146 # define gcry_threads_vlc gcry_threads_pthread
1149 * gcrypt thread option VLC implementation
1152 # define NEED_THREAD_CONTEXT 1
1153 static vlc_object_t *__p_gcry_data;
1155 static int gcry_vlc_mutex_init( void **p_sys )
1158 vlc_mutex_t *p_lock = (vlc_mutex_t *)malloc( sizeof( vlc_mutex_t ) );
1163 i_val = vlc_mutex_init( __p_gcry_data, p_lock );
1171 static int gcry_vlc_mutex_destroy( void **p_sys )
1174 vlc_mutex_t *p_lock = (vlc_mutex_t *)*p_sys;
1176 i_val = vlc_mutex_destroy( p_lock );
1181 static int gcry_vlc_mutex_lock( void **p_sys )
1183 return vlc_mutex_lock( (vlc_mutex_t *)*p_sys );
1186 static int gcry_vlc_mutex_unlock( void **lock )
1188 return vlc_mutex_unlock( (vlc_mutex_t *)*lock );
1191 static struct gcry_thread_cbs gcry_threads_vlc =
1193 GCRY_THREAD_OPTION_USER,
1195 gcry_vlc_mutex_init,
1196 gcry_vlc_mutex_destroy,
1197 gcry_vlc_mutex_lock,
1198 gcry_vlc_mutex_unlock
1203 /*****************************************************************************
1204 * Module initialization
1205 *****************************************************************************/
1206 static unsigned refs = 0;
1209 Open( vlc_object_t *p_this )
1211 tls_t *p_tls = (tls_t *)p_this;
1214 lock = var_GetGlobalMutex( "gnutls_mutex" );
1215 vlc_mutex_lock( lock );
1217 /* Initialize GnuTLS only once */
1220 #ifdef NEED_THREAD_CONTEXT
1221 __p_gcry_data = VLC_OBJECT( p_this->p_libvlc );
1224 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_vlc);
1225 if( gnutls_global_init( ) )
1227 msg_Warn( p_this, "cannot initialize GnuTLS" );
1228 vlc_mutex_unlock( lock );
1229 return VLC_EGENERIC;
1232 const char *psz_version = gnutls_check_version( "1.2.9" );
1233 if( psz_version == NULL )
1235 gnutls_global_deinit( );
1236 vlc_mutex_unlock( lock );
1237 msg_Err( p_this, "unsupported GnuTLS version" );
1238 return VLC_EGENERIC;
1240 msg_Dbg( p_this, "GnuTLS v%s initialized", psz_version );
1244 vlc_mutex_unlock( lock );
1246 p_tls->pf_server_create = gnutls_ServerCreate;
1247 p_tls->pf_client_create = gnutls_ClientCreate;
1252 /*****************************************************************************
1253 * Module deinitialization
1254 *****************************************************************************/
1256 Close( vlc_object_t *p_this )
1258 /*tls_t *p_tls = (tls_t *)p_this;
1259 tls_sys_t *p_sys = (tls_sys_t *)(p_this->p_sys);*/
1263 lock = var_GetGlobalMutex( "gnutls_mutex" );
1264 vlc_mutex_lock( lock );
1268 gnutls_global_deinit( );
1269 msg_Dbg( p_this, "GnuTLS deinitialized" );
1272 vlc_mutex_unlock( lock );