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>
52 #define CACHE_TIMEOUT 3600
57 /*****************************************************************************
59 *****************************************************************************/
60 static int OpenClient (vlc_object_t *);
61 static void CloseClient (vlc_object_t *);
62 static int OpenServer (vlc_object_t *);
63 static void CloseServer (vlc_object_t *);
65 #define CACHE_TIMEOUT_TEXT N_("Expiration time for resumed TLS sessions")
66 #define CACHE_TIMEOUT_LONGTEXT N_( \
67 "It is possible to cache the resumed TLS sessions. This is the expiration "\
68 "time of the sessions stored in this cache, in seconds." )
70 #define CACHE_SIZE_TEXT N_("Number of resumed TLS sessions")
71 #define CACHE_SIZE_LONGTEXT N_( \
72 "This is the maximum number of resumed TLS sessions that " \
73 "the cache will hold." )
75 #define CHECK_CERT_TEXT N_("Check TLS/SSL server certificate validity")
76 #define CHECK_CERT_LONGTEXT N_( \
77 "This ensures that the server certificate is valid " \
78 "(i.e. signed by an approved Certification Authority)." )
81 set_shortname( "GnuTLS" );
82 set_description( _("GnuTLS transport layer security") );
83 set_capability( "tls client", 1 );
84 set_callbacks( OpenClient, CloseClient );
85 set_category( CAT_ADVANCED );
86 set_subcategory( SUBCAT_ADVANCED_MISC );
88 add_bool( "tls-check-cert", VLC_TRUE, NULL, CHECK_CERT_TEXT,
89 CHECK_CERT_LONGTEXT, VLC_FALSE );
90 add_obsolete_bool( "tls-check-hostname" );
93 set_description( _("GnuTLS server") );
94 set_capability( "tls server", 1 );
95 set_category( CAT_ADVANCED );
96 set_subcategory( SUBCAT_ADVANCED_MISC );
97 set_callbacks( OpenServer, CloseServer );
99 add_obsolete_integer( "gnutls-dh-bits" );
100 add_integer( "gnutls-cache-timeout", CACHE_TIMEOUT, NULL,
101 CACHE_TIMEOUT_TEXT, CACHE_TIMEOUT_LONGTEXT, VLC_TRUE );
102 add_integer( "gnutls-cache-size", CACHE_SIZE, NULL, CACHE_SIZE_TEXT,
103 CACHE_SIZE_LONGTEXT, VLC_TRUE );
108 #ifdef LIBVLC_USE_PTHREAD
109 GCRY_THREAD_OPTION_PTHREAD_IMPL;
110 # define gcry_threads_vlc gcry_threads_pthread
113 * gcrypt thread option VLC implementation
116 # define NEED_THREAD_CONTEXT 1
117 static vlc_object_t *__p_gcry_data = NULL;
119 static int gcry_vlc_mutex_init( void **p_sys )
122 vlc_mutex_t *p_lock = (vlc_mutex_t *)malloc( sizeof( vlc_mutex_t ) );
127 i_val = vlc_mutex_init( __p_gcry_data, p_lock );
135 static int gcry_vlc_mutex_destroy( void **p_sys )
138 vlc_mutex_t *p_lock = (vlc_mutex_t *)*p_sys;
140 i_val = vlc_mutex_destroy( p_lock );
145 static int gcry_vlc_mutex_lock( void **p_sys )
147 return vlc_mutex_lock( (vlc_mutex_t *)*p_sys );
150 static int gcry_vlc_mutex_unlock( void **lock )
152 return vlc_mutex_unlock( (vlc_mutex_t *)*lock );
155 static struct gcry_thread_cbs gcry_threads_vlc =
157 GCRY_THREAD_OPTION_USER,
160 gcry_vlc_mutex_destroy,
162 gcry_vlc_mutex_unlock
168 * Initializes GnuTLS with proper locking.
169 * @return VLC_SUCCESS on success, a VLC error code otherwise.
171 static int gnutls_Init (vlc_object_t *p_this)
173 int ret = VLC_EGENERIC;
175 vlc_mutex_t *lock = var_AcquireMutex ("gnutls_mutex");
177 /* This should probably be removed/fixed. It will screw up with multiple
178 * LibVLC instances. */
179 #ifdef NEED_THREAD_CONTEXT
180 __p_gcry_data = VLC_OBJECT (p_this->p_libvlc);
183 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_vlc);
184 if (gnutls_global_init ())
186 msg_Err (p_this, "cannot initialize GnuTLS");
190 const char *psz_version = gnutls_check_version ("1.3.3");
191 if (psz_version == NULL)
193 msg_Err (p_this, "unsupported GnuTLS version");
194 gnutls_global_deinit ();
198 msg_Dbg (p_this, "GnuTLS v%s initialized", psz_version);
202 vlc_mutex_unlock (lock);
208 * Deinitializes GnuTLS.
210 static void gnutls_Deinit (vlc_object_t *p_this)
212 vlc_mutex_t *lock = var_AcquireMutex( "gnutls_mutex" );
214 gnutls_global_deinit ();
215 msg_Dbg (p_this, "GnuTLS deinitialized");
216 vlc_mutex_unlock (lock);
220 static int gnutls_Error (vlc_object_t *obj, int val)
229 /* WinSock does not return EAGAIN, return EINTR instead */
231 case GNUTLS_E_INTERRUPTED:
233 WSASetLastError (WSAEINTR);
240 msg_Err (obj, "%s", gnutls_strerror (val));
242 if (!gnutls_error_is_fatal (val))
243 msg_Err (obj, "Error above should be handled");
246 WSASetLastError (WSAECONNRESET);
255 struct tls_session_sys_t
257 gnutls_session_t session;
259 vlc_bool_t b_handshaked;
264 * Sends data through a TLS session.
267 gnutls_Send( void *p_session, const void *buf, int i_length )
270 tls_session_sys_t *p_sys;
272 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
274 val = gnutls_record_send( p_sys->session, buf, i_length );
275 return (val < 0) ? gnutls_Error ((vlc_object_t *)p_session, val) : val;
280 * Receives data through a TLS session.
283 gnutls_Recv( void *p_session, void *buf, int i_length )
286 tls_session_sys_t *p_sys;
288 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
290 val = gnutls_record_recv( p_sys->session, buf, i_length );
291 return (val < 0) ? gnutls_Error ((vlc_object_t *)p_session, val) : val;
296 * Starts or continues the TLS handshake.
298 * @return -1 on fatal error, 0 on succesful handshake completion,
299 * 1 if more would-be blocking recv is needed,
300 * 2 if more would-be blocking send is required.
303 gnutls_ContinueHandshake (tls_session_t *p_session)
305 tls_session_sys_t *p_sys = p_session->p_sys;
309 WSASetLastError( 0 );
311 val = gnutls_handshake( p_sys->session );
312 if( ( val == GNUTLS_E_AGAIN ) || ( val == GNUTLS_E_INTERRUPTED ) )
313 return 1 + gnutls_record_get_direction( p_sys->session );
318 msg_Dbg( p_session, "Winsock error %d", WSAGetLastError( ) );
320 msg_Err( p_session, "TLS handshake error: %s",
321 gnutls_strerror( val ) );
325 p_sys->b_handshaked = VLC_TRUE;
336 static const error_msg_t cert_errors[] =
338 { GNUTLS_CERT_INVALID,
339 "Certificate could not be verified" },
340 { GNUTLS_CERT_REVOKED,
341 "Certificate was revoked" },
342 { GNUTLS_CERT_SIGNER_NOT_FOUND,
343 "Certificate's signer was not found" },
344 { GNUTLS_CERT_SIGNER_NOT_CA,
345 "Certificate's signer is not a CA" },
346 { GNUTLS_CERT_INSECURE_ALGORITHM,
347 "Insecure certificate signature algorithm" },
353 gnutls_HandshakeAndValidate( tls_session_t *session )
355 int val = gnutls_ContinueHandshake( session );
359 tls_session_sys_t *p_sys = (tls_session_sys_t *)(session->p_sys);
361 /* certificates chain verification */
363 val = gnutls_certificate_verify_peers2( p_sys->session, &status );
367 msg_Err( session, "Certificate verification failed: %s",
368 gnutls_strerror( val ) );
374 msg_Err( session, "TLS session: access denied" );
375 for( const error_msg_t *e = cert_errors; e->flag; e++ )
377 if( status & e->flag )
379 msg_Err( session, "%s", e->msg );
386 "unknown certificate error (you found a bug in VLC)" );
391 /* certificate (host)name verification */
392 const gnutls_datum_t *data;
393 data = gnutls_certificate_get_peers (p_sys->session, &(unsigned){0});
396 msg_Err( session, "Peer certificate not available" );
400 gnutls_x509_crt_t cert;
401 val = gnutls_x509_crt_init( &cert );
404 msg_Err( session, "x509 fatal error: %s", gnutls_strerror( val ) );
408 val = gnutls_x509_crt_import( cert, data, GNUTLS_X509_FMT_DER );
411 msg_Err( session, "Certificate import error: %s",
412 gnutls_strerror( val ) );
416 if( p_sys->psz_hostname != NULL )
418 if ( !gnutls_x509_crt_check_hostname( cert, p_sys->psz_hostname ) )
420 msg_Err( session, "Certificate does not match \"%s\"",
421 p_sys->psz_hostname );
426 msg_Warn( session, "Certificate and hostname were not verified" );
428 if( gnutls_x509_crt_get_expiration_time( cert ) < time( NULL ) )
430 msg_Err( session, "Certificate expired" );
434 if( gnutls_x509_crt_get_activation_time( cert ) > time ( NULL ) )
436 msg_Err( session, "Certificate not yet valid" );
440 gnutls_x509_crt_deinit( cert );
441 msg_Dbg( session, "TLS/x509 certificate verified" );
445 gnutls_x509_crt_deinit( cert );
450 * Sets the operating system file descriptor backend for the TLS sesison.
452 * @param fd stream socket already connected with the peer.
455 gnutls_SetFD (tls_session_t *p_session, int fd)
457 gnutls_transport_set_ptr (p_session->p_sys->session,
458 (gnutls_transport_ptr_t)(intptr_t)fd);
461 typedef int (*tls_prio_func) (gnutls_session_t, const int *);
464 gnutls_SetPriority (vlc_object_t *restrict obj, const char *restrict name,
465 tls_prio_func func, gnutls_session_t session,
466 const int *restrict values)
468 int val = func (session, values);
471 msg_Err (obj, "cannot set %s priorities: %s", name,
472 gnutls_strerror (val));
480 gnutls_SessionPrioritize (vlc_object_t *obj, gnutls_session_t session)
482 /* Note that ordering matters (on the client side) */
483 static const int protos[] =
490 static const int comps[] =
496 static const int macs[] =
499 GNUTLS_MAC_RMD160, // RIPEMD
505 static const int ciphers[] =
507 GNUTLS_CIPHER_AES_256_CBC,
508 GNUTLS_CIPHER_AES_128_CBC,
509 GNUTLS_CIPHER_3DES_CBC,
510 GNUTLS_CIPHER_ARCFOUR_128,
511 //GNUTLS_CIPHER_DES_CBC,
512 //GNUTLS_CIPHER_ARCFOUR_40,
513 //GNUTLS_CIPHER_RC2_40_CBC,
514 //GNUTLS_CIPHER_NULL,
517 static const int kx[] =
522 //GNUTLS_KX_RSA_EXPORT,
523 //GNUTLS_KX_DHE_PSK, TODO
524 //GNUTLS_KX_PSK, TODO
525 //GNUTLS_KX_SRP_RSA, TODO
526 //GNUTLS_KX_SRP_DSS, TODO
527 //GNUTLS_KX_SRP, TODO
531 static const int cert_types[] =
534 //GNUTLS_CRT_OPENPGP, TODO
538 int val = gnutls_set_default_priority (session);
541 msg_Err (obj, "cannot set default TLS priorities: %s",
542 gnutls_strerror (val));
546 if (gnutls_SetPriority (obj, "protocols",
547 gnutls_protocol_set_priority, session, protos)
548 || gnutls_SetPriority (obj, "compression algorithms",
549 gnutls_compression_set_priority, session, comps)
550 || gnutls_SetPriority (obj, "MAC algorithms",
551 gnutls_mac_set_priority, session, macs)
552 || gnutls_SetPriority (obj, "ciphers",
553 gnutls_cipher_set_priority, session, ciphers)
554 || gnutls_SetPriority (obj, "key exchange algorithms",
555 gnutls_kx_set_priority, session, kx)
556 || gnutls_SetPriority (obj, "certificate types",
557 gnutls_certificate_type_set_priority, session,
566 gnutls_Addx509File( vlc_object_t *p_this,
567 gnutls_certificate_credentials_t cred,
568 const char *psz_path, vlc_bool_t b_priv );
571 gnutls_Addx509Directory( vlc_object_t *p_this,
572 gnutls_certificate_credentials_t cred,
573 const char *psz_dirname,
578 if( *psz_dirname == '\0' )
581 dir = utf8_opendir( psz_dirname );
586 msg_Err (p_this, "cannot open directory (%s): %m", psz_dirname);
590 msg_Dbg (p_this, "creating empty certificate directory: %s",
592 utf8_mkdir (psz_dirname, b_priv ? 0700 : 0755);
598 struct stat st1, st2;
599 int fd = dirfd( dir );
602 * Gets stats for the directory path, checks that it is not a
603 * symbolic link (to avoid possibly infinite recursion), and verifies
604 * that the inode is still the same, to avoid TOCTOU race condition.
607 || fstat( fd, &st1 ) || utf8_lstat( psz_dirname, &st2 )
608 || S_ISLNK( st2.st_mode ) || ( st1.st_ino != st2.st_ino ) )
618 char *ent = utf8_readdir (dir);
622 if ((strcmp (ent, ".") == 0) || (strcmp (ent, "..") == 0))
625 char path[strlen (psz_dirname) + strlen (ent) + 2];
626 sprintf (path, "%s"DIR_SEP"%s", psz_dirname, ent);
629 gnutls_Addx509File( p_this, cred, path, b_priv );
638 gnutls_Addx509File( vlc_object_t *p_this,
639 gnutls_certificate_credentials cred,
640 const char *psz_path, vlc_bool_t b_priv )
644 if( utf8_stat( psz_path, &st ) == 0 )
646 if( S_ISREG( st.st_mode ) )
648 char *psz_localname = ToLocale( psz_path );
650 ? gnutls_certificate_set_x509_key_file( cred,
651 psz_localname, psz_localname, GNUTLS_X509_FMT_PEM )
652 : gnutls_certificate_set_x509_trust_file( cred,
653 psz_localname, GNUTLS_X509_FMT_PEM );
654 LocaleFree( psz_localname );
658 msg_Warn( p_this, "cannot add x509 credentials (%s): %s",
659 psz_path, gnutls_strerror( i ) );
664 msg_Dbg( p_this, "added x509 credentials (%s)",
669 else if( S_ISDIR( st.st_mode ) )
672 "looking recursively for x509 credentials in %s",
674 return gnutls_Addx509Directory( p_this, cred, psz_path, b_priv);
678 msg_Warn( p_this, "cannot add x509 credentials (%s): %m", psz_path );
683 /** TLS client session data */
684 typedef struct tls_client_sys_t
686 struct tls_session_sys_t session;
687 gnutls_certificate_credentials_t x509_cred;
692 * Initializes a client-side TLS session.
694 static int OpenClient (vlc_object_t *obj)
696 tls_session_t *p_session = (tls_session_t *)obj;
699 if (gnutls_Init (obj))
702 tls_client_sys_t *p_sys = malloc (sizeof (*p_sys));
709 p_session->p_sys = &p_sys->session;
710 p_session->sock.p_sys = p_session;
711 p_session->sock.pf_send = gnutls_Send;
712 p_session->sock.pf_recv = gnutls_Recv;
713 p_session->pf_set_fd = gnutls_SetFD;
715 p_sys->session.b_handshaked = VLC_FALSE;
716 p_sys->session.psz_hostname = NULL;
718 const char *homedir = obj->p_libvlc->psz_datadir,
719 *datadir = config_GetDataDir ();
720 size_t l1 = strlen (homedir), l2 = strlen (datadir);
721 char path[((l1 > l2) ? l1 : l2) + sizeof ("/ca-certificates.crt")];
722 // > sizeof ("/ssl/private")
723 // > sizeof ("/ssl/certs")
725 i_val = gnutls_certificate_allocate_credentials (&p_sys->x509_cred);
728 msg_Err (obj, "cannot allocate X509 credentials: %s",
729 gnutls_strerror (i_val));
733 sprintf (path, "%s/ssl", homedir);
734 utf8_mkdir (path, 0755);
736 if (var_CreateGetBool (obj, "tls-check-cert"))
738 sprintf (path, "%s/ssl/certs", homedir);
739 gnutls_Addx509Directory (VLC_OBJECT (p_session),
740 p_sys->x509_cred, path, VLC_FALSE);
742 sprintf (path, "%s/ca-certificates.crt", datadir);
743 gnutls_Addx509File (VLC_OBJECT (p_session),
744 p_sys->x509_cred, path, VLC_FALSE);
745 p_session->pf_handshake = gnutls_HandshakeAndValidate;
748 p_session->pf_handshake = gnutls_ContinueHandshake;
750 sprintf (path, "%s/ssl/private", homedir);
751 gnutls_Addx509Directory (VLC_OBJECT (p_session), p_sys->x509_cred,
754 i_val = gnutls_init (&p_sys->session.session, GNUTLS_CLIENT);
757 msg_Err (obj, "cannot initialize TLS session: %s",
758 gnutls_strerror (i_val));
759 gnutls_certificate_free_credentials (p_sys->x509_cred);
763 if (gnutls_SessionPrioritize (VLC_OBJECT (p_session),
764 p_sys->session.session))
767 /* minimum DH prime bits */
768 gnutls_dh_set_prime_bits (p_sys->session.session, 1024);
770 i_val = gnutls_credentials_set (p_sys->session.session,
771 GNUTLS_CRD_CERTIFICATE,
775 msg_Err (obj, "cannot set TLS session credentials: %s",
776 gnutls_strerror (i_val));
780 char *servername = var_GetNonEmptyString (p_session, "tls-server-name");
781 if (servername != NULL )
783 p_sys->session.psz_hostname = servername;
784 gnutls_server_name_set (p_sys->session.session, GNUTLS_NAME_DNS,
785 servername, strlen (servername));
791 gnutls_deinit (p_sys->session.session);
792 gnutls_certificate_free_credentials (p_sys->x509_cred);
800 static void CloseClient (vlc_object_t *obj)
802 tls_session_t *client = (tls_session_t *)obj;
803 tls_client_sys_t *p_sys = (tls_client_sys_t *)(client->p_sys);
805 if (p_sys->session.b_handshaked == VLC_TRUE)
806 gnutls_bye (p_sys->session.session, GNUTLS_SHUT_WR);
807 gnutls_deinit (p_sys->session.session);
808 /* credentials must be free'd *after* gnutls_deinit() */
809 gnutls_certificate_free_credentials (p_sys->x509_cred);
812 free (p_sys->session.psz_hostname);
820 struct tls_server_sys_t
822 gnutls_certificate_credentials_t x509_cred;
823 gnutls_dh_params_t dh_params;
825 struct saved_session_t *p_cache;
826 struct saved_session_t *p_store;
828 vlc_mutex_t cache_lock;
830 int (*pf_handshake) (tls_session_t *);
835 * TLS session resumption callbacks (server-side)
837 #define MAX_SESSION_ID 32
838 #define MAX_SESSION_DATA 1024
840 typedef struct saved_session_t
842 char id[MAX_SESSION_ID];
843 char data[MAX_SESSION_DATA];
850 static int cb_store( void *p_server, gnutls_datum key, gnutls_datum data )
852 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
854 if( ( p_sys->i_cache_size == 0 )
855 || ( key.size > MAX_SESSION_ID )
856 || ( data.size > MAX_SESSION_DATA ) )
859 vlc_mutex_lock( &p_sys->cache_lock );
861 memcpy( p_sys->p_store->id, key.data, key.size);
862 memcpy( p_sys->p_store->data, data.data, data.size );
863 p_sys->p_store->i_idlen = key.size;
864 p_sys->p_store->i_datalen = data.size;
867 if( ( p_sys->p_store - p_sys->p_cache ) == p_sys->i_cache_size )
868 p_sys->p_store = p_sys->p_cache;
870 vlc_mutex_unlock( &p_sys->cache_lock );
876 static gnutls_datum cb_fetch( void *p_server, gnutls_datum key )
878 static const gnutls_datum_t err_datum = { NULL, 0 };
879 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
880 saved_session_t *p_session, *p_end;
882 p_session = p_sys->p_cache;
883 p_end = p_session + p_sys->i_cache_size;
885 vlc_mutex_lock( &p_sys->cache_lock );
887 while( p_session < p_end )
889 if( ( p_session->i_idlen == key.size )
890 && !memcmp( p_session->id, key.data, key.size ) )
894 data.size = p_session->i_datalen;
896 data.data = gnutls_malloc( data.size );
897 if( data.data == NULL )
899 vlc_mutex_unlock( &p_sys->cache_lock );
903 memcpy( data.data, p_session->data, data.size );
904 vlc_mutex_unlock( &p_sys->cache_lock );
910 vlc_mutex_unlock( &p_sys->cache_lock );
916 static int cb_delete( void *p_server, gnutls_datum key )
918 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
919 saved_session_t *p_session, *p_end;
921 p_session = p_sys->p_cache;
922 p_end = p_session + p_sys->i_cache_size;
924 vlc_mutex_lock( &p_sys->cache_lock );
926 while( p_session < p_end )
928 if( ( p_session->i_idlen == key.size )
929 && !memcmp( p_session->id, key.data, key.size ) )
931 p_session->i_datalen = p_session->i_idlen = 0;
932 vlc_mutex_unlock( &p_sys->cache_lock );
938 vlc_mutex_unlock( &p_sys->cache_lock );
945 * Terminates TLS session and releases session data.
946 * You still have to close the socket yourself.
949 gnutls_SessionClose (tls_server_t *p_server, tls_session_t *p_session)
951 tls_session_sys_t *p_sys = p_session->p_sys;
954 if( p_sys->b_handshaked == VLC_TRUE )
955 gnutls_bye( p_sys->session, GNUTLS_SHUT_WR );
956 gnutls_deinit( p_sys->session );
958 vlc_object_detach( p_session );
959 vlc_object_destroy( p_session );
966 * Initializes a server-side TLS session.
968 static tls_session_t *
969 gnutls_ServerSessionPrepare( tls_server_t *p_server )
971 tls_session_t *p_session;
972 tls_server_sys_t *p_server_sys;
973 gnutls_session_t session;
976 p_session = vlc_object_create( p_server, sizeof (struct tls_session_t) );
977 if( p_session == NULL )
980 p_session->p_sys = malloc( sizeof(struct tls_session_sys_t) );
981 if( p_session->p_sys == NULL )
983 vlc_object_destroy( p_session );
987 p_server_sys = p_server->p_sys;
988 p_session->sock.p_sys = p_session;
989 p_session->sock.pf_send = gnutls_Send;
990 p_session->sock.pf_recv = gnutls_Recv;
991 p_session->pf_set_fd = gnutls_SetFD;
992 p_session->pf_handshake = p_server_sys->pf_handshake;
994 p_session->p_sys->b_handshaked = VLC_FALSE;
995 p_session->p_sys->psz_hostname = NULL;
997 i_val = gnutls_init( &session, GNUTLS_SERVER );
1000 msg_Err( p_server, "cannot initialize TLS session: %s",
1001 gnutls_strerror( i_val ) );
1005 p_session->p_sys->session = session;
1007 if (gnutls_SessionPrioritize (VLC_OBJECT (p_session), session))
1009 gnutls_deinit( session );
1013 i_val = gnutls_credentials_set( session, GNUTLS_CRD_CERTIFICATE,
1014 p_server_sys->x509_cred );
1017 msg_Err( p_server, "cannot set TLS session credentials: %s",
1018 gnutls_strerror( i_val ) );
1019 gnutls_deinit( session );
1023 if (p_session->pf_handshake == gnutls_HandshakeAndValidate)
1024 gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUIRE);
1026 /* Session resumption support */
1027 i_val = config_GetInt (p_server, "gnutls-cache-timeout");
1028 gnutls_db_set_cache_expiration (session, i_val);
1029 gnutls_db_set_retrieve_function( session, cb_fetch );
1030 gnutls_db_set_remove_function( session, cb_delete );
1031 gnutls_db_set_store_function( session, cb_store );
1032 gnutls_db_set_ptr( session, p_server );
1037 free( p_session->p_sys );
1038 vlc_object_detach( p_session );
1039 vlc_object_destroy( p_session );
1045 * Adds one or more certificate authorities.
1047 * @param psz_ca_path (Unicode) path to an x509 certificates list.
1049 * @return -1 on error, 0 on success.
1052 gnutls_ServerAddCA( tls_server_t *p_server, const char *psz_ca_path )
1054 tls_server_sys_t *p_sys;
1055 char *psz_local_path;
1058 p_sys = (tls_server_sys_t *)(p_server->p_sys);
1060 psz_local_path = ToLocale( psz_ca_path );
1061 val = gnutls_certificate_set_x509_trust_file( p_sys->x509_cred,
1063 GNUTLS_X509_FMT_PEM );
1064 LocaleFree( psz_local_path );
1067 msg_Err( p_server, "cannot add trusted CA (%s): %s", psz_ca_path,
1068 gnutls_strerror( val ) );
1069 return VLC_EGENERIC;
1071 msg_Dbg( p_server, " %d trusted CA added (%s)", val, psz_ca_path );
1073 /* enables peer's certificate verification */
1074 p_sys->pf_handshake = gnutls_HandshakeAndValidate;
1081 * Adds a certificates revocation list to be sent to TLS clients.
1083 * @param psz_crl_path (Unicode) path of the CRL file.
1085 * @return -1 on error, 0 on success.
1088 gnutls_ServerAddCRL( tls_server_t *p_server, const char *psz_crl_path )
1091 char *psz_local_path = ToLocale( psz_crl_path );
1093 val = gnutls_certificate_set_x509_crl_file( ((tls_server_sys_t *)
1094 (p_server->p_sys))->x509_cred,
1096 GNUTLS_X509_FMT_PEM );
1097 LocaleFree( psz_crl_path );
1100 msg_Err( p_server, "cannot add CRL (%s): %s", psz_crl_path,
1101 gnutls_strerror( val ) );
1102 return VLC_EGENERIC;
1104 msg_Dbg( p_server, "%d CRL added (%s)", val, psz_crl_path );
1110 * Allocates a whole server's TLS credentials.
1112 static int OpenServer (vlc_object_t *obj)
1114 tls_server_t *p_server = (tls_server_t *)obj;
1115 tls_server_sys_t *p_sys;
1118 if (gnutls_Init (obj))
1119 return VLC_EGENERIC;
1121 msg_Dbg (obj, "creating TLS server");
1123 p_sys = (tls_server_sys_t *)malloc( sizeof(struct tls_server_sys_t) );
1127 p_sys->i_cache_size = config_GetInt (obj, "gnutls-cache-size");
1128 p_sys->p_cache = calloc (p_sys->i_cache_size,
1129 sizeof (struct saved_session_t));
1130 if (p_sys->p_cache == NULL)
1136 p_sys->p_store = p_sys->p_cache;
1137 p_server->p_sys = p_sys;
1138 p_server->pf_add_CA = gnutls_ServerAddCA;
1139 p_server->pf_add_CRL = gnutls_ServerAddCRL;
1140 p_server->pf_open = gnutls_ServerSessionPrepare;
1141 p_server->pf_close = gnutls_SessionClose;
1143 /* No certificate validation by default */
1144 p_sys->pf_handshake = gnutls_ContinueHandshake;
1146 vlc_mutex_init( p_server, &p_sys->cache_lock );
1148 /* Sets server's credentials */
1149 val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
1152 msg_Err( p_server, "cannot allocate X509 credentials: %s",
1153 gnutls_strerror( val ) );
1157 char *psz_cert_path = var_GetNonEmptyString (obj, "tls-x509-cert");
1158 char *psz_key_path = var_GetNonEmptyString (obj, "tls-x509-key");
1159 const char *psz_local_cert = ToLocale (psz_cert_path);
1160 const char *psz_local_key = ToLocale (psz_key_path);
1161 val = gnutls_certificate_set_x509_key_file (p_sys->x509_cred,
1162 psz_local_cert, psz_local_key,
1163 GNUTLS_X509_FMT_PEM );
1164 LocaleFree (psz_local_key);
1165 free (psz_key_path);
1166 LocaleFree (psz_local_cert);
1167 free (psz_cert_path);
1171 msg_Err( p_server, "cannot set certificate chain or private key: %s",
1172 gnutls_strerror( val ) );
1173 gnutls_certificate_free_credentials( p_sys->x509_cred );
1178 * - support other ciper suites
1180 val = gnutls_dh_params_init (&p_sys->dh_params);
1183 const gnutls_datum_t data = {
1184 .data = (unsigned char *)dh_params,
1185 .size = sizeof (dh_params) - 1,
1188 val = gnutls_dh_params_import_pkcs3 (p_sys->dh_params, &data,
1189 GNUTLS_X509_FMT_PEM);
1191 gnutls_certificate_set_dh_params (p_sys->x509_cred,
1196 msg_Err (p_server, "cannot initialize DHE cipher suites: %s",
1197 gnutls_strerror (val));
1203 vlc_mutex_destroy (&p_sys->cache_lock);
1204 free (p_sys->p_cache);
1206 return VLC_EGENERIC;
1210 * Destroys a TLS server object.
1212 static void CloseServer (vlc_object_t *p_server)
1214 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
1216 vlc_mutex_destroy (&p_sys->cache_lock);
1217 free (p_sys->p_cache);
1219 /* all sessions depending on the server are now deinitialized */
1220 gnutls_certificate_free_credentials (p_sys->x509_cred);
1221 gnutls_dh_params_deinit (p_sys->dh_params);
1224 gnutls_Deinit (p_server);