1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2006 Rémi Denis-Courmont
7 * Authors: Rémi Denis-Courmont <rem # videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_plugin.h>
37 #include <sys/types.h>
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
54 #include <vlc_charset.h>
55 #include <vlc_block.h>
58 #include <gnutls/gnutls.h>
59 #include <gnutls/x509.h>
61 #include <vlc_gcrypt.h>
63 #define CACHE_TIMEOUT 3600
68 /*****************************************************************************
70 *****************************************************************************/
71 static int OpenClient (vlc_object_t *);
72 static void CloseClient (vlc_object_t *);
73 static int OpenServer (vlc_object_t *);
74 static void CloseServer (vlc_object_t *);
76 #define CACHE_TIMEOUT_TEXT N_("Expiration time for resumed TLS sessions")
77 #define CACHE_TIMEOUT_LONGTEXT N_( \
78 "It is possible to cache the resumed TLS sessions. This is the expiration "\
79 "time of the sessions stored in this cache, in seconds." )
81 #define CACHE_SIZE_TEXT N_("Number of resumed TLS sessions")
82 #define CACHE_SIZE_LONGTEXT N_( \
83 "This is the maximum number of resumed TLS sessions that " \
84 "the cache will hold." )
87 set_shortname( "GnuTLS" );
88 set_description( N_("GnuTLS transport layer security") );
89 set_capability( "tls client", 1 );
90 set_callbacks( OpenClient, CloseClient );
91 set_category( CAT_ADVANCED );
92 set_subcategory( SUBCAT_ADVANCED_MISC );
94 add_obsolete_bool( "tls-check-cert" );
95 add_obsolete_bool( "tls-check-hostname" );
98 set_description( N_("GnuTLS server") );
99 set_capability( "tls server", 1 );
100 set_category( CAT_ADVANCED );
101 set_subcategory( SUBCAT_ADVANCED_MISC );
102 set_callbacks( OpenServer, CloseServer );
104 add_obsolete_integer( "gnutls-dh-bits" );
105 add_integer( "gnutls-cache-timeout", CACHE_TIMEOUT, NULL,
106 CACHE_TIMEOUT_TEXT, CACHE_TIMEOUT_LONGTEXT, true );
107 add_integer( "gnutls-cache-size", CACHE_SIZE, NULL, CACHE_SIZE_TEXT,
108 CACHE_SIZE_LONGTEXT, true );
112 * Initializes GnuTLS with proper locking.
113 * @return VLC_SUCCESS on success, a VLC error code otherwise.
115 static int gnutls_Init (vlc_object_t *p_this)
117 int ret = VLC_EGENERIC;
119 vlc_gcrypt_init (); /* GnuTLS depends on gcrypt */
121 vlc_mutex_t *lock = var_AcquireMutex ("gnutls_mutex");
122 if (gnutls_global_init ())
124 msg_Err (p_this, "cannot initialize GnuTLS");
128 const char *psz_version = gnutls_check_version ("1.3.3");
129 if (psz_version == NULL)
131 msg_Err (p_this, "unsupported GnuTLS version");
132 gnutls_global_deinit ();
136 msg_Dbg (p_this, "GnuTLS v%s initialized", psz_version);
140 vlc_mutex_unlock (lock);
146 * Deinitializes GnuTLS.
148 static void gnutls_Deinit (vlc_object_t *p_this)
150 vlc_mutex_t *lock = var_AcquireMutex( "gnutls_mutex" );
152 gnutls_global_deinit ();
153 msg_Dbg (p_this, "GnuTLS deinitialized");
154 vlc_mutex_unlock (lock);
158 static int gnutls_Error (vlc_object_t *obj, int val)
167 /* WinSock does not return EAGAIN, return EINTR instead */
169 case GNUTLS_E_INTERRUPTED:
171 WSASetLastError (WSAEINTR);
178 msg_Err (obj, "%s", gnutls_strerror (val));
180 if (!gnutls_error_is_fatal (val))
181 msg_Err (obj, "Error above should be handled");
184 WSASetLastError (WSAECONNRESET);
193 struct tls_session_sys_t
195 gnutls_session_t session;
202 * Sends data through a TLS session.
205 gnutls_Send( void *p_session, const 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_send( p_sys->session, buf, i_length );
213 return (val < 0) ? gnutls_Error ((vlc_object_t *)p_session, val) : val;
218 * Receives data through a TLS session.
221 gnutls_Recv( void *p_session, void *buf, int i_length )
224 tls_session_sys_t *p_sys;
226 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
228 val = gnutls_record_recv( p_sys->session, buf, i_length );
229 return (val < 0) ? gnutls_Error ((vlc_object_t *)p_session, val) : val;
234 * Starts or continues the TLS handshake.
236 * @return -1 on fatal error, 0 on succesful handshake completion,
237 * 1 if more would-be blocking recv is needed,
238 * 2 if more would-be blocking send is required.
241 gnutls_ContinueHandshake (tls_session_t *p_session)
243 tls_session_sys_t *p_sys = p_session->p_sys;
247 WSASetLastError( 0 );
249 val = gnutls_handshake( p_sys->session );
250 if( ( val == GNUTLS_E_AGAIN ) || ( val == GNUTLS_E_INTERRUPTED ) )
251 return 1 + gnutls_record_get_direction( p_sys->session );
256 msg_Dbg( p_session, "Winsock error %d", WSAGetLastError( ) );
258 msg_Err( p_session, "TLS handshake error: %s",
259 gnutls_strerror( val ) );
263 p_sys->b_handshaked = true;
274 static const error_msg_t cert_errors[] =
276 { GNUTLS_CERT_INVALID,
277 "Certificate could not be verified" },
278 { GNUTLS_CERT_REVOKED,
279 "Certificate was revoked" },
280 { GNUTLS_CERT_SIGNER_NOT_FOUND,
281 "Certificate's signer was not found" },
282 { GNUTLS_CERT_SIGNER_NOT_CA,
283 "Certificate's signer is not a CA" },
284 { GNUTLS_CERT_INSECURE_ALGORITHM,
285 "Insecure certificate signature algorithm" },
291 gnutls_HandshakeAndValidate( tls_session_t *session )
293 int val = gnutls_ContinueHandshake( session );
297 tls_session_sys_t *p_sys = (tls_session_sys_t *)(session->p_sys);
299 /* certificates chain verification */
301 val = gnutls_certificate_verify_peers2( p_sys->session, &status );
305 msg_Err( session, "Certificate verification failed: %s",
306 gnutls_strerror( val ) );
312 msg_Err( session, "TLS session: access denied" );
313 for( const error_msg_t *e = cert_errors; e->flag; e++ )
315 if( status & e->flag )
317 msg_Err( session, "%s", e->msg );
324 "unknown certificate error (you found a bug in VLC)" );
329 /* certificate (host)name verification */
330 const gnutls_datum_t *data;
331 data = gnutls_certificate_get_peers (p_sys->session, &(unsigned){0});
334 msg_Err( session, "Peer certificate not available" );
338 gnutls_x509_crt_t cert;
339 val = gnutls_x509_crt_init( &cert );
342 msg_Err( session, "x509 fatal error: %s", gnutls_strerror( val ) );
346 val = gnutls_x509_crt_import( cert, data, GNUTLS_X509_FMT_DER );
349 msg_Err( session, "Certificate import error: %s",
350 gnutls_strerror( val ) );
354 assert( p_sys->psz_hostname != NULL );
355 if ( !gnutls_x509_crt_check_hostname( cert, p_sys->psz_hostname ) )
357 msg_Err( session, "Certificate does not match \"%s\"",
358 p_sys->psz_hostname );
362 if( gnutls_x509_crt_get_expiration_time( cert ) < time( NULL ) )
364 msg_Err( session, "Certificate expired" );
368 if( gnutls_x509_crt_get_activation_time( cert ) > time ( NULL ) )
370 msg_Err( session, "Certificate not yet valid" );
374 gnutls_x509_crt_deinit( cert );
375 msg_Dbg( session, "TLS/x509 certificate verified" );
379 gnutls_x509_crt_deinit( cert );
384 * Sets the operating system file descriptor backend for the TLS sesison.
386 * @param fd stream socket already connected with the peer.
389 gnutls_SetFD (tls_session_t *p_session, int fd)
391 gnutls_transport_set_ptr (p_session->p_sys->session,
392 (gnutls_transport_ptr_t)(intptr_t)fd);
395 typedef int (*tls_prio_func) (gnutls_session_t, const int *);
398 gnutls_SetPriority (vlc_object_t *restrict obj, const char *restrict name,
399 tls_prio_func func, gnutls_session_t session,
400 const int *restrict values)
402 int val = func (session, values);
405 msg_Err (obj, "cannot set %s priorities: %s", name,
406 gnutls_strerror (val));
414 gnutls_SessionPrioritize (vlc_object_t *obj, gnutls_session_t session)
416 /* Note that ordering matters (on the client side) */
417 static const int protos[] =
424 static const int comps[] =
430 static const int macs[] =
433 GNUTLS_MAC_RMD160, // RIPEMD
439 static const int ciphers[] =
441 GNUTLS_CIPHER_AES_256_CBC,
442 GNUTLS_CIPHER_AES_128_CBC,
443 GNUTLS_CIPHER_3DES_CBC,
444 GNUTLS_CIPHER_ARCFOUR_128,
445 //GNUTLS_CIPHER_DES_CBC,
446 //GNUTLS_CIPHER_ARCFOUR_40,
447 //GNUTLS_CIPHER_RC2_40_CBC,
448 //GNUTLS_CIPHER_NULL,
451 static const int kx[] =
456 //GNUTLS_KX_RSA_EXPORT,
457 //GNUTLS_KX_DHE_PSK, TODO
458 //GNUTLS_KX_PSK, TODO
459 //GNUTLS_KX_SRP_RSA, TODO
460 //GNUTLS_KX_SRP_DSS, TODO
461 //GNUTLS_KX_SRP, TODO
465 static const int cert_types[] =
468 //GNUTLS_CRT_OPENPGP, TODO
472 int val = gnutls_set_default_priority (session);
475 msg_Err (obj, "cannot set default TLS priorities: %s",
476 gnutls_strerror (val));
480 if (gnutls_SetPriority (obj, "protocols",
481 gnutls_protocol_set_priority, session, protos)
482 || gnutls_SetPriority (obj, "compression algorithms",
483 gnutls_compression_set_priority, session, comps)
484 || gnutls_SetPriority (obj, "MAC algorithms",
485 gnutls_mac_set_priority, session, macs)
486 || gnutls_SetPriority (obj, "ciphers",
487 gnutls_cipher_set_priority, session, ciphers)
488 || gnutls_SetPriority (obj, "key exchange algorithms",
489 gnutls_kx_set_priority, session, kx)
490 || gnutls_SetPriority (obj, "certificate types",
491 gnutls_certificate_type_set_priority, session,
500 gnutls_Addx509File( vlc_object_t *p_this,
501 gnutls_certificate_credentials_t cred,
502 const char *psz_path, bool b_priv );
505 gnutls_Addx509Directory( vlc_object_t *p_this,
506 gnutls_certificate_credentials_t cred,
507 const char *psz_dirname,
512 if( *psz_dirname == '\0' )
515 dir = utf8_opendir( psz_dirname );
520 msg_Err (p_this, "cannot open directory (%s): %m", psz_dirname);
524 msg_Dbg (p_this, "creating empty certificate directory: %s",
526 utf8_mkdir (psz_dirname, b_priv ? 0700 : 0755);
532 struct stat st1, st2;
533 int fd = dirfd( dir );
536 * Gets stats for the directory path, checks that it is not a
537 * symbolic link (to avoid possibly infinite recursion), and verifies
538 * that the inode is still the same, to avoid TOCTOU race condition.
541 || fstat( fd, &st1 ) || utf8_lstat( psz_dirname, &st2 )
542 || S_ISLNK( st2.st_mode ) || ( st1.st_ino != st2.st_ino ) )
552 char *ent = utf8_readdir (dir);
556 if ((strcmp (ent, ".") == 0) || (strcmp (ent, "..") == 0))
559 char path[strlen (psz_dirname) + strlen (ent) + 2];
560 sprintf (path, "%s"DIR_SEP"%s", psz_dirname, ent);
563 gnutls_Addx509File( p_this, cred, path, b_priv );
572 gnutls_Addx509File( vlc_object_t *p_this,
573 gnutls_certificate_credentials cred,
574 const char *psz_path, bool b_priv )
578 int fd = utf8_open (psz_path, O_RDONLY, 0);
582 block_t *block = block_File (fd);
587 gnutls_datum data = {
588 .data = block->p_buffer,
589 .size = block->i_buffer,
592 ? gnutls_certificate_set_x509_key_mem (cred, &data, &data,
594 : gnutls_certificate_set_x509_trust_mem (cred, &data,
595 GNUTLS_X509_FMT_PEM);
596 block_Release (block);
600 msg_Warn (p_this, "cannot add x509 credentials (%s): %s",
601 psz_path, gnutls_strerror (res));
604 msg_Dbg (p_this, "added x509 credentials (%s)", psz_path);
608 if (!fstat (fd, &st) && S_ISDIR (st.st_mode))
611 msg_Dbg (p_this, "looking recursively for x509 credentials in %s",
613 return gnutls_Addx509Directory (p_this, cred, psz_path, b_priv);
617 msg_Warn (p_this, "cannot add x509 credentials (%s): %m", psz_path);
624 /** TLS client session data */
625 typedef struct tls_client_sys_t
627 struct tls_session_sys_t session;
628 gnutls_certificate_credentials_t x509_cred;
633 * Initializes a client-side TLS session.
635 static int OpenClient (vlc_object_t *obj)
637 tls_session_t *p_session = (tls_session_t *)obj;
640 if (gnutls_Init (obj))
643 tls_client_sys_t *p_sys = malloc (sizeof (*p_sys));
650 p_session->p_sys = &p_sys->session;
651 p_session->sock.p_sys = p_session;
652 p_session->sock.pf_send = gnutls_Send;
653 p_session->sock.pf_recv = gnutls_Recv;
654 p_session->pf_set_fd = gnutls_SetFD;
656 p_sys->session.b_handshaked = false;
658 i_val = gnutls_certificate_allocate_credentials (&p_sys->x509_cred);
661 msg_Err (obj, "cannot allocate X509 credentials: %s",
662 gnutls_strerror (i_val));
666 char *userdir = config_GetUserDataDir ();
669 char path[strlen (userdir) + sizeof ("/ssl/private")];
670 sprintf (path, "%s/ssl", userdir);
671 utf8_mkdir (path, 0755);
673 sprintf (path, "%s/ssl/certs", userdir);
674 gnutls_Addx509Directory (VLC_OBJECT (p_session),
675 p_sys->x509_cred, path, false);
676 sprintf (path, "%s/ssl/private", userdir);
677 gnutls_Addx509Directory (VLC_OBJECT (p_session), p_sys->x509_cred,
682 const char *confdir = config_GetConfDir ();
684 char path[strlen (confdir)
685 + sizeof ("/ssl/certs/ca-certificates.crt")];
686 sprintf (path, "%s/ssl/certs/ca-certificates.crt", confdir);
687 gnutls_Addx509File (VLC_OBJECT (p_session),
688 p_sys->x509_cred, path, false);
690 p_session->pf_handshake = gnutls_HandshakeAndValidate;
691 /*p_session->pf_handshake = gnutls_ContinueHandshake;*/
693 i_val = gnutls_init (&p_sys->session.session, GNUTLS_CLIENT);
696 msg_Err (obj, "cannot initialize TLS session: %s",
697 gnutls_strerror (i_val));
698 gnutls_certificate_free_credentials (p_sys->x509_cred);
702 if (gnutls_SessionPrioritize (VLC_OBJECT (p_session),
703 p_sys->session.session))
706 /* minimum DH prime bits */
707 gnutls_dh_set_prime_bits (p_sys->session.session, 1024);
709 i_val = gnutls_credentials_set (p_sys->session.session,
710 GNUTLS_CRD_CERTIFICATE,
714 msg_Err (obj, "cannot set TLS session credentials: %s",
715 gnutls_strerror (i_val));
719 char *servername = var_GetNonEmptyString (p_session, "tls-server-name");
720 if (servername == NULL )
721 msg_Err (p_session, "server name missing for TLS session");
723 p_sys->session.psz_hostname = servername;
724 gnutls_server_name_set (p_sys->session.session, GNUTLS_NAME_DNS,
725 servername, strlen (servername));
730 gnutls_deinit (p_sys->session.session);
731 gnutls_certificate_free_credentials (p_sys->x509_cred);
739 static void CloseClient (vlc_object_t *obj)
741 tls_session_t *client = (tls_session_t *)obj;
742 tls_client_sys_t *p_sys = (tls_client_sys_t *)(client->p_sys);
744 if (p_sys->session.b_handshaked == true)
745 gnutls_bye (p_sys->session.session, GNUTLS_SHUT_WR);
746 gnutls_deinit (p_sys->session.session);
747 /* credentials must be free'd *after* gnutls_deinit() */
748 gnutls_certificate_free_credentials (p_sys->x509_cred);
751 free (p_sys->session.psz_hostname);
759 struct tls_server_sys_t
761 gnutls_certificate_credentials_t x509_cred;
762 gnutls_dh_params_t dh_params;
764 struct saved_session_t *p_cache;
765 struct saved_session_t *p_store;
767 vlc_mutex_t cache_lock;
769 int (*pf_handshake) (tls_session_t *);
774 * TLS session resumption callbacks (server-side)
776 #define MAX_SESSION_ID 32
777 #define MAX_SESSION_DATA 1024
779 typedef struct saved_session_t
781 char id[MAX_SESSION_ID];
782 char data[MAX_SESSION_DATA];
789 static int cb_store( void *p_server, gnutls_datum key, gnutls_datum data )
791 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
793 if( ( p_sys->i_cache_size == 0 )
794 || ( key.size > MAX_SESSION_ID )
795 || ( data.size > MAX_SESSION_DATA ) )
798 vlc_mutex_lock( &p_sys->cache_lock );
800 memcpy( p_sys->p_store->id, key.data, key.size);
801 memcpy( p_sys->p_store->data, data.data, data.size );
802 p_sys->p_store->i_idlen = key.size;
803 p_sys->p_store->i_datalen = data.size;
806 if( ( p_sys->p_store - p_sys->p_cache ) == p_sys->i_cache_size )
807 p_sys->p_store = p_sys->p_cache;
809 vlc_mutex_unlock( &p_sys->cache_lock );
815 static gnutls_datum cb_fetch( void *p_server, gnutls_datum key )
817 static const gnutls_datum_t err_datum = { NULL, 0 };
818 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
819 saved_session_t *p_session, *p_end;
821 p_session = p_sys->p_cache;
822 p_end = p_session + p_sys->i_cache_size;
824 vlc_mutex_lock( &p_sys->cache_lock );
826 while( p_session < p_end )
828 if( ( p_session->i_idlen == key.size )
829 && !memcmp( p_session->id, key.data, key.size ) )
833 data.size = p_session->i_datalen;
835 data.data = gnutls_malloc( data.size );
836 if( data.data == NULL )
838 vlc_mutex_unlock( &p_sys->cache_lock );
842 memcpy( data.data, p_session->data, data.size );
843 vlc_mutex_unlock( &p_sys->cache_lock );
849 vlc_mutex_unlock( &p_sys->cache_lock );
855 static int cb_delete( void *p_server, gnutls_datum key )
857 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
858 saved_session_t *p_session, *p_end;
860 p_session = p_sys->p_cache;
861 p_end = p_session + p_sys->i_cache_size;
863 vlc_mutex_lock( &p_sys->cache_lock );
865 while( p_session < p_end )
867 if( ( p_session->i_idlen == key.size )
868 && !memcmp( p_session->id, key.data, key.size ) )
870 p_session->i_datalen = p_session->i_idlen = 0;
871 vlc_mutex_unlock( &p_sys->cache_lock );
877 vlc_mutex_unlock( &p_sys->cache_lock );
884 * Terminates TLS session and releases session data.
885 * You still have to close the socket yourself.
888 gnutls_SessionClose (tls_server_t *p_server, tls_session_t *p_session)
890 tls_session_sys_t *p_sys = p_session->p_sys;
893 if( p_sys->b_handshaked == true )
894 gnutls_bye( p_sys->session, GNUTLS_SHUT_WR );
895 gnutls_deinit( p_sys->session );
897 vlc_object_detach( p_session );
898 vlc_object_release( p_session );
905 * Initializes a server-side TLS session.
907 static tls_session_t *
908 gnutls_ServerSessionPrepare( tls_server_t *p_server )
910 tls_session_t *p_session;
911 tls_server_sys_t *p_server_sys;
912 gnutls_session_t session;
915 p_session = vlc_object_create( p_server, sizeof (struct tls_session_t) );
916 if( p_session == NULL )
919 p_session->p_sys = malloc( sizeof(struct tls_session_sys_t) );
920 if( p_session->p_sys == NULL )
922 vlc_object_release( p_session );
926 p_server_sys = p_server->p_sys;
927 p_session->sock.p_sys = p_session;
928 p_session->sock.pf_send = gnutls_Send;
929 p_session->sock.pf_recv = gnutls_Recv;
930 p_session->pf_set_fd = gnutls_SetFD;
931 p_session->pf_handshake = p_server_sys->pf_handshake;
933 p_session->p_sys->b_handshaked = false;
934 p_session->p_sys->psz_hostname = NULL;
936 i_val = gnutls_init( &session, GNUTLS_SERVER );
939 msg_Err( p_server, "cannot initialize TLS session: %s",
940 gnutls_strerror( i_val ) );
944 p_session->p_sys->session = session;
946 if (gnutls_SessionPrioritize (VLC_OBJECT (p_session), session))
948 gnutls_deinit( session );
952 i_val = gnutls_credentials_set( session, GNUTLS_CRD_CERTIFICATE,
953 p_server_sys->x509_cred );
956 msg_Err( p_server, "cannot set TLS session credentials: %s",
957 gnutls_strerror( i_val ) );
958 gnutls_deinit( session );
962 if (p_session->pf_handshake == gnutls_HandshakeAndValidate)
963 gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUIRE);
965 /* Session resumption support */
966 i_val = config_GetInt (p_server, "gnutls-cache-timeout");
967 gnutls_db_set_cache_expiration (session, i_val);
968 gnutls_db_set_retrieve_function( session, cb_fetch );
969 gnutls_db_set_remove_function( session, cb_delete );
970 gnutls_db_set_store_function( session, cb_store );
971 gnutls_db_set_ptr( session, p_server );
976 free( p_session->p_sys );
977 vlc_object_detach( p_session );
978 vlc_object_release( p_session );
984 * Adds one or more certificate authorities.
986 * @param psz_ca_path (Unicode) path to an x509 certificates list.
988 * @return -1 on error, 0 on success.
991 gnutls_ServerAddCA( tls_server_t *p_server, const char *psz_ca_path )
993 tls_server_sys_t *p_sys;
994 char *psz_local_path;
997 p_sys = (tls_server_sys_t *)(p_server->p_sys);
999 psz_local_path = ToLocale( psz_ca_path );
1000 val = gnutls_certificate_set_x509_trust_file( p_sys->x509_cred,
1002 GNUTLS_X509_FMT_PEM );
1003 LocaleFree( psz_local_path );
1006 msg_Err( p_server, "cannot add trusted CA (%s): %s", psz_ca_path,
1007 gnutls_strerror( val ) );
1008 return VLC_EGENERIC;
1010 msg_Dbg( p_server, " %d trusted CA added (%s)", val, psz_ca_path );
1012 /* enables peer's certificate verification */
1013 p_sys->pf_handshake = gnutls_HandshakeAndValidate;
1020 * Adds a certificates revocation list to be sent to TLS clients.
1022 * @param psz_crl_path (Unicode) path of the CRL file.
1024 * @return -1 on error, 0 on success.
1027 gnutls_ServerAddCRL( tls_server_t *p_server, const char *psz_crl_path )
1030 char *psz_local_path = ToLocale( psz_crl_path );
1032 val = gnutls_certificate_set_x509_crl_file( ((tls_server_sys_t *)
1033 (p_server->p_sys))->x509_cred,
1035 GNUTLS_X509_FMT_PEM );
1036 LocaleFree( psz_crl_path );
1039 msg_Err( p_server, "cannot add CRL (%s): %s", psz_crl_path,
1040 gnutls_strerror( val ) );
1041 return VLC_EGENERIC;
1043 msg_Dbg( p_server, "%d CRL added (%s)", val, psz_crl_path );
1049 * Allocates a whole server's TLS credentials.
1051 static int OpenServer (vlc_object_t *obj)
1053 tls_server_t *p_server = (tls_server_t *)obj;
1054 tls_server_sys_t *p_sys;
1057 if (gnutls_Init (obj))
1058 return VLC_EGENERIC;
1060 msg_Dbg (obj, "creating TLS server");
1062 p_sys = (tls_server_sys_t *)malloc( sizeof(struct tls_server_sys_t) );
1066 p_sys->i_cache_size = config_GetInt (obj, "gnutls-cache-size");
1067 p_sys->p_cache = calloc (p_sys->i_cache_size,
1068 sizeof (struct saved_session_t));
1069 if (p_sys->p_cache == NULL)
1075 p_sys->p_store = p_sys->p_cache;
1076 p_server->p_sys = p_sys;
1077 p_server->pf_add_CA = gnutls_ServerAddCA;
1078 p_server->pf_add_CRL = gnutls_ServerAddCRL;
1079 p_server->pf_open = gnutls_ServerSessionPrepare;
1080 p_server->pf_close = gnutls_SessionClose;
1082 /* No certificate validation by default */
1083 p_sys->pf_handshake = gnutls_ContinueHandshake;
1085 vlc_mutex_init( &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 char *psz_cert_path = var_GetNonEmptyString (obj, "tls-x509-cert");
1097 char *psz_key_path = var_GetNonEmptyString (obj, "tls-x509-key");
1098 const char *psz_local_cert = ToLocale (psz_cert_path);
1099 const char *psz_local_key = ToLocale (psz_key_path);
1100 val = gnutls_certificate_set_x509_key_file (p_sys->x509_cred,
1101 psz_local_cert, psz_local_key,
1102 GNUTLS_X509_FMT_PEM );
1103 LocaleFree (psz_local_key);
1104 free (psz_key_path);
1105 LocaleFree (psz_local_cert);
1106 free (psz_cert_path);
1110 msg_Err( p_server, "cannot set certificate chain or private key: %s",
1111 gnutls_strerror( val ) );
1112 gnutls_certificate_free_credentials( p_sys->x509_cred );
1117 * - support other ciper suites
1119 val = gnutls_dh_params_init (&p_sys->dh_params);
1122 const gnutls_datum_t data = {
1123 .data = (unsigned char *)dh_params,
1124 .size = sizeof (dh_params) - 1,
1127 val = gnutls_dh_params_import_pkcs3 (p_sys->dh_params, &data,
1128 GNUTLS_X509_FMT_PEM);
1130 gnutls_certificate_set_dh_params (p_sys->x509_cred,
1135 msg_Err (p_server, "cannot initialize DHE cipher suites: %s",
1136 gnutls_strerror (val));
1142 vlc_mutex_destroy (&p_sys->cache_lock);
1143 free (p_sys->p_cache);
1145 return VLC_EGENERIC;
1149 * Destroys a TLS server object.
1151 static void CloseServer (vlc_object_t *p_server)
1153 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
1155 vlc_mutex_destroy (&p_sys->cache_lock);
1156 free (p_sys->p_cache);
1158 /* all sessions depending on the server are now deinitialized */
1159 gnutls_certificate_free_credentials (p_sys->x509_cred);
1160 gnutls_dh_params_deinit (p_sys->dh_params);
1163 gnutls_Deinit (p_server);