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 <vlc_common.h>
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>
56 #include <vlc_block.h>
59 #include <gnutls/gnutls.h>
60 #include <gnutls/x509.h>
62 #include <vlc_gcrypt.h>
64 #define CACHE_TIMEOUT 3600
71 /*****************************************************************************
73 *****************************************************************************/
74 static int OpenClient (vlc_object_t *);
75 static void CloseClient (vlc_object_t *);
76 static int OpenServer (vlc_object_t *);
77 static void CloseServer (vlc_object_t *);
79 #define CACHE_TIMEOUT_TEXT N_("Expiration time for resumed TLS sessions")
80 #define CACHE_TIMEOUT_LONGTEXT N_( \
81 "It is possible to cache the resumed TLS sessions. This is the expiration "\
82 "time of the sessions stored in this cache, in seconds." )
84 #define CACHE_SIZE_TEXT N_("Number of resumed TLS sessions")
85 #define CACHE_SIZE_LONGTEXT N_( \
86 "This is the maximum number of resumed TLS sessions that " \
87 "the cache will hold." )
90 set_shortname( "GnuTLS" )
91 set_description( N_("GnuTLS transport layer security") )
92 set_capability( "tls client", 1 )
93 set_callbacks( OpenClient, CloseClient )
94 set_category( CAT_ADVANCED )
95 set_subcategory( SUBCAT_ADVANCED_MISC )
97 add_obsolete_bool( "tls-check-cert" )
98 add_obsolete_bool( "tls-check-hostname" )
101 set_description( N_("GnuTLS server") )
102 set_capability( "tls server", 1 )
103 set_category( CAT_ADVANCED )
104 set_subcategory( SUBCAT_ADVANCED_MISC )
105 set_callbacks( OpenServer, CloseServer )
107 add_obsolete_integer( "gnutls-dh-bits" )
108 add_integer( "gnutls-cache-timeout", CACHE_TIMEOUT, NULL,
109 CACHE_TIMEOUT_TEXT, CACHE_TIMEOUT_LONGTEXT, true )
110 add_integer( "gnutls-cache-size", CACHE_SIZE, NULL, CACHE_SIZE_TEXT,
111 CACHE_SIZE_LONGTEXT, true )
114 static vlc_mutex_t gnutls_mutex = VLC_STATIC_MUTEX;
117 * Initializes GnuTLS with proper locking.
118 * @return VLC_SUCCESS on success, a VLC error code otherwise.
120 static int gnutls_Init (vlc_object_t *p_this)
122 int ret = VLC_EGENERIC;
124 vlc_gcrypt_init (); /* GnuTLS depends on gcrypt */
126 vlc_mutex_lock (&gnutls_mutex);
127 if (gnutls_global_init ())
129 msg_Err (p_this, "cannot initialize GnuTLS");
133 const char *psz_version = gnutls_check_version ("1.3.3");
134 if (psz_version == NULL)
136 msg_Err (p_this, "unsupported GnuTLS version");
137 gnutls_global_deinit ();
141 msg_Dbg (p_this, "GnuTLS v%s initialized", psz_version);
145 vlc_mutex_unlock (&gnutls_mutex);
151 * Deinitializes GnuTLS.
153 static void gnutls_Deinit (vlc_object_t *p_this)
155 vlc_mutex_lock (&gnutls_mutex);
157 gnutls_global_deinit ();
158 msg_Dbg (p_this, "GnuTLS deinitialized");
159 vlc_mutex_unlock (&gnutls_mutex);
163 static int gnutls_Error (vlc_object_t *obj, int val)
172 /* WinSock does not return EAGAIN, return EINTR instead */
174 case GNUTLS_E_INTERRUPTED:
176 WSASetLastError (WSAEINTR);
183 msg_Err (obj, "%s", gnutls_strerror (val));
185 if (!gnutls_error_is_fatal (val))
186 msg_Err (obj, "Error above should be handled");
189 WSASetLastError (WSAECONNRESET);
198 struct tls_session_sys_t
200 gnutls_session_t session;
207 * Sends data through a TLS session.
210 gnutls_Send( void *p_session, const 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_send( p_sys->session, buf, i_length );
218 return (val < 0) ? gnutls_Error ((vlc_object_t *)p_session, val) : val;
223 * Receives data through a TLS session.
226 gnutls_Recv( void *p_session, void *buf, int i_length )
229 tls_session_sys_t *p_sys;
231 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
233 val = gnutls_record_recv( p_sys->session, buf, i_length );
234 return (val < 0) ? gnutls_Error ((vlc_object_t *)p_session, val) : val;
239 * Starts or continues the TLS handshake.
241 * @return -1 on fatal error, 0 on succesful handshake completion,
242 * 1 if more would-be blocking recv is needed,
243 * 2 if more would-be blocking send is required.
246 gnutls_ContinueHandshake (tls_session_t *p_session)
248 tls_session_sys_t *p_sys = p_session->p_sys;
252 WSASetLastError( 0 );
254 val = gnutls_handshake( p_sys->session );
255 if( ( val == GNUTLS_E_AGAIN ) || ( val == GNUTLS_E_INTERRUPTED ) )
256 return 1 + gnutls_record_get_direction( p_sys->session );
261 msg_Dbg( p_session, "Winsock error %d", WSAGetLastError( ) );
263 msg_Err( p_session, "TLS handshake error: %s",
264 gnutls_strerror( val ) );
268 p_sys->b_handshaked = true;
279 static const error_msg_t cert_errors[] =
281 { GNUTLS_CERT_INVALID,
282 "Certificate could not be verified" },
283 { GNUTLS_CERT_REVOKED,
284 "Certificate was revoked" },
285 { GNUTLS_CERT_SIGNER_NOT_FOUND,
286 "Certificate's signer was not found" },
287 { GNUTLS_CERT_SIGNER_NOT_CA,
288 "Certificate's signer is not a CA" },
289 { GNUTLS_CERT_INSECURE_ALGORITHM,
290 "Insecure certificate signature algorithm" },
296 gnutls_HandshakeAndValidate( tls_session_t *session )
298 int val = gnutls_ContinueHandshake( session );
302 tls_session_sys_t *p_sys = (tls_session_sys_t *)(session->p_sys);
304 /* certificates chain verification */
306 val = gnutls_certificate_verify_peers2( p_sys->session, &status );
310 msg_Err( session, "Certificate verification failed: %s",
311 gnutls_strerror( val ) );
317 msg_Err( session, "TLS session: access denied" );
318 for( const error_msg_t *e = cert_errors; e->flag; e++ )
320 if( status & e->flag )
322 msg_Err( session, "%s", e->msg );
329 "unknown certificate error (you found a bug in VLC)" );
334 /* certificate (host)name verification */
335 const gnutls_datum_t *data;
336 data = gnutls_certificate_get_peers (p_sys->session, &(unsigned){0});
339 msg_Err( session, "Peer certificate not available" );
343 gnutls_x509_crt_t cert;
344 val = gnutls_x509_crt_init( &cert );
347 msg_Err( session, "x509 fatal error: %s", gnutls_strerror( val ) );
351 val = gnutls_x509_crt_import( cert, data, GNUTLS_X509_FMT_DER );
354 msg_Err( session, "Certificate import error: %s",
355 gnutls_strerror( val ) );
359 assert( p_sys->psz_hostname != NULL );
360 if ( !gnutls_x509_crt_check_hostname( cert, p_sys->psz_hostname ) )
362 msg_Err( session, "Certificate does not match \"%s\"",
363 p_sys->psz_hostname );
367 if( gnutls_x509_crt_get_expiration_time( cert ) < time( NULL ) )
369 msg_Err( session, "Certificate expired" );
373 if( gnutls_x509_crt_get_activation_time( cert ) > time ( NULL ) )
375 msg_Err( session, "Certificate not yet valid" );
379 gnutls_x509_crt_deinit( cert );
380 msg_Dbg( session, "TLS/x509 certificate verified" );
384 gnutls_x509_crt_deinit( cert );
389 * Sets the operating system file descriptor backend for the TLS sesison.
391 * @param fd stream socket already connected with the peer.
394 gnutls_SetFD (tls_session_t *p_session, int fd)
396 gnutls_transport_set_ptr (p_session->p_sys->session,
397 (gnutls_transport_ptr_t)(intptr_t)fd);
400 typedef int (*tls_prio_func) (gnutls_session_t, const int *);
403 gnutls_SetPriority (vlc_object_t *restrict obj, const char *restrict name,
404 tls_prio_func func, gnutls_session_t session,
405 const int *restrict values)
407 int val = func (session, values);
410 msg_Err (obj, "cannot set %s priorities: %s", name,
411 gnutls_strerror (val));
419 gnutls_SessionPrioritize (vlc_object_t *obj, gnutls_session_t session)
421 /* Note that ordering matters (on the client side) */
422 static const int protos[] =
424 /*GNUTLS_TLS1_2, as of GnuTLS 2.6.5, still not ratified */
430 static const int comps[] =
436 static const int macs[] =
442 GNUTLS_MAC_RMD160, // RIPEMD
448 static const int ciphers[] =
450 GNUTLS_CIPHER_AES_256_CBC,
451 GNUTLS_CIPHER_AES_128_CBC,
452 GNUTLS_CIPHER_3DES_CBC,
453 GNUTLS_CIPHER_ARCFOUR_128,
454 // TODO? Camellia ciphers?
455 //GNUTLS_CIPHER_DES_CBC,
456 //GNUTLS_CIPHER_ARCFOUR_40,
457 //GNUTLS_CIPHER_RC2_40_CBC,
458 //GNUTLS_CIPHER_NULL,
461 static const int kx[] =
466 //GNUTLS_KX_RSA_EXPORT,
467 //GNUTLS_KX_DHE_PSK, TODO
468 //GNUTLS_KX_PSK, TODO
469 //GNUTLS_KX_SRP_RSA, TODO
470 //GNUTLS_KX_SRP_DSS, TODO
471 //GNUTLS_KX_SRP, TODO
475 static const int cert_types[] =
478 //GNUTLS_CRT_OPENPGP, TODO
482 int val = gnutls_set_default_priority (session);
485 msg_Err (obj, "cannot set default TLS priorities: %s",
486 gnutls_strerror (val));
490 if (gnutls_SetPriority (obj, "protocols",
491 gnutls_protocol_set_priority, session, protos)
492 || gnutls_SetPriority (obj, "compression algorithms",
493 gnutls_compression_set_priority, session, comps)
494 || gnutls_SetPriority (obj, "MAC algorithms",
495 gnutls_mac_set_priority, session, macs)
496 || gnutls_SetPriority (obj, "ciphers",
497 gnutls_cipher_set_priority, session, ciphers)
498 || gnutls_SetPriority (obj, "key exchange algorithms",
499 gnutls_kx_set_priority, session, kx)
500 || gnutls_SetPriority (obj, "certificate types",
501 gnutls_certificate_type_set_priority, session,
510 gnutls_Addx509File( vlc_object_t *p_this,
511 gnutls_certificate_credentials_t cred,
512 const char *psz_path, bool b_priv );
515 gnutls_Addx509Directory( vlc_object_t *p_this,
516 gnutls_certificate_credentials_t cred,
517 const char *psz_dirname,
522 if( *psz_dirname == '\0' )
525 dir = vlc_opendir( psz_dirname );
530 msg_Err (p_this, "cannot open directory (%s): %m", psz_dirname);
534 msg_Dbg (p_this, "creating empty certificate directory: %s",
536 vlc_mkdir (psz_dirname, b_priv ? 0700 : 0755);
542 struct stat st1, st2;
543 int fd = dirfd( dir );
546 * Gets stats for the directory path, checks that it is not a
547 * symbolic link (to avoid possibly infinite recursion), and verifies
548 * that the inode is still the same, to avoid TOCTOU race condition.
551 || fstat( fd, &st1 ) || vlc_lstat( psz_dirname, &st2 )
552 || S_ISLNK( st2.st_mode ) || ( st1.st_ino != st2.st_ino ) )
562 char *ent = vlc_readdir (dir);
566 if ((strcmp (ent, ".") == 0) || (strcmp (ent, "..") == 0))
572 char path[strlen (psz_dirname) + strlen (ent) + 2];
573 sprintf (path, "%s"DIR_SEP"%s", psz_dirname, ent);
576 gnutls_Addx509File( p_this, cred, path, b_priv );
585 gnutls_Addx509File( vlc_object_t *p_this,
586 gnutls_certificate_credentials cred,
587 const char *psz_path, bool b_priv )
591 int fd = vlc_open (psz_path, O_RDONLY);
595 block_t *block = block_File (fd);
600 gnutls_datum data = {
601 .data = block->p_buffer,
602 .size = block->i_buffer,
605 ? gnutls_certificate_set_x509_key_mem (cred, &data, &data,
607 : gnutls_certificate_set_x509_trust_mem (cred, &data,
608 GNUTLS_X509_FMT_PEM);
609 block_Release (block);
613 msg_Warn (p_this, "cannot add x509 credentials (%s): %s",
614 psz_path, gnutls_strerror (res));
617 msg_Dbg (p_this, "added x509 credentials (%s)", psz_path);
621 if (!fstat (fd, &st) && S_ISDIR (st.st_mode))
624 msg_Dbg (p_this, "looking recursively for x509 credentials in %s",
626 return gnutls_Addx509Directory (p_this, cred, psz_path, b_priv);
630 msg_Warn (p_this, "cannot add x509 credentials (%s): %m", psz_path);
637 /** TLS client session data */
638 typedef struct tls_client_sys_t
640 struct tls_session_sys_t session;
641 gnutls_certificate_credentials_t x509_cred;
646 * Initializes a client-side TLS session.
648 static int OpenClient (vlc_object_t *obj)
650 tls_session_t *p_session = (tls_session_t *)obj;
653 if (gnutls_Init (obj))
656 tls_client_sys_t *p_sys = malloc (sizeof (*p_sys));
663 p_session->p_sys = &p_sys->session;
664 p_session->sock.p_sys = p_session;
665 p_session->sock.pf_send = gnutls_Send;
666 p_session->sock.pf_recv = gnutls_Recv;
667 p_session->pf_set_fd = gnutls_SetFD;
669 p_sys->session.b_handshaked = false;
671 i_val = gnutls_certificate_allocate_credentials (&p_sys->x509_cred);
674 msg_Err (obj, "cannot allocate X509 credentials: %s",
675 gnutls_strerror (i_val));
679 char *userdir = config_GetUserDir ( VLC_DATA_DIR );
682 char path[strlen (userdir) + sizeof ("/ssl/private")];
683 sprintf (path, "%s/ssl", userdir);
684 vlc_mkdir (path, 0755);
686 sprintf (path, "%s/ssl/certs", userdir);
687 gnutls_Addx509Directory (VLC_OBJECT (p_session),
688 p_sys->x509_cred, path, false);
689 sprintf (path, "%s/ssl/private", userdir);
690 gnutls_Addx509Directory (VLC_OBJECT (p_session), p_sys->x509_cred,
695 const char *confdir = config_GetConfDir ();
697 char path[strlen (confdir)
698 + sizeof ("/ssl/certs/ca-certificates.crt")];
699 sprintf (path, "%s/ssl/certs/ca-certificates.crt", confdir);
700 gnutls_Addx509File (VLC_OBJECT (p_session),
701 p_sys->x509_cred, path, false);
703 p_session->pf_handshake = gnutls_HandshakeAndValidate;
704 /*p_session->pf_handshake = gnutls_ContinueHandshake;*/
706 i_val = gnutls_init (&p_sys->session.session, GNUTLS_CLIENT);
709 msg_Err (obj, "cannot initialize TLS session: %s",
710 gnutls_strerror (i_val));
711 gnutls_certificate_free_credentials (p_sys->x509_cred);
715 if (gnutls_SessionPrioritize (VLC_OBJECT (p_session),
716 p_sys->session.session))
719 /* minimum DH prime bits */
720 gnutls_dh_set_prime_bits (p_sys->session.session, 1024);
722 i_val = gnutls_credentials_set (p_sys->session.session,
723 GNUTLS_CRD_CERTIFICATE,
727 msg_Err (obj, "cannot set TLS session credentials: %s",
728 gnutls_strerror (i_val));
732 char *servername = var_GetNonEmptyString (p_session, "tls-server-name");
733 if (servername == NULL )
734 msg_Err (p_session, "server name missing for TLS session");
736 gnutls_server_name_set (p_sys->session.session, GNUTLS_NAME_DNS,
737 servername, strlen (servername));
739 p_sys->session.psz_hostname = servername;
744 gnutls_deinit (p_sys->session.session);
745 gnutls_certificate_free_credentials (p_sys->x509_cred);
753 static void CloseClient (vlc_object_t *obj)
755 tls_session_t *client = (tls_session_t *)obj;
756 tls_client_sys_t *p_sys = (tls_client_sys_t *)(client->p_sys);
758 if (p_sys->session.b_handshaked == true)
759 gnutls_bye (p_sys->session.session, GNUTLS_SHUT_WR);
760 gnutls_deinit (p_sys->session.session);
761 /* credentials must be free'd *after* gnutls_deinit() */
762 gnutls_certificate_free_credentials (p_sys->x509_cred);
765 free (p_sys->session.psz_hostname);
773 struct tls_server_sys_t
775 gnutls_certificate_credentials_t x509_cred;
776 gnutls_dh_params_t dh_params;
778 struct saved_session_t *p_cache;
779 struct saved_session_t *p_store;
781 vlc_mutex_t cache_lock;
783 int (*pf_handshake) (tls_session_t *);
788 * TLS session resumption callbacks (server-side)
790 #define MAX_SESSION_ID 32
791 #define MAX_SESSION_DATA 1024
793 typedef struct saved_session_t
795 char id[MAX_SESSION_ID];
796 char data[MAX_SESSION_DATA];
803 static int cb_store( void *p_server, gnutls_datum key, gnutls_datum data )
805 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
807 if( ( p_sys->i_cache_size == 0 )
808 || ( key.size > MAX_SESSION_ID )
809 || ( data.size > MAX_SESSION_DATA ) )
812 vlc_mutex_lock( &p_sys->cache_lock );
814 memcpy( p_sys->p_store->id, key.data, key.size);
815 memcpy( p_sys->p_store->data, data.data, data.size );
816 p_sys->p_store->i_idlen = key.size;
817 p_sys->p_store->i_datalen = data.size;
820 if( ( p_sys->p_store - p_sys->p_cache ) == p_sys->i_cache_size )
821 p_sys->p_store = p_sys->p_cache;
823 vlc_mutex_unlock( &p_sys->cache_lock );
829 static gnutls_datum cb_fetch( void *p_server, gnutls_datum key )
831 static const gnutls_datum_t err_datum = { NULL, 0 };
832 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
833 saved_session_t *p_session, *p_end;
835 p_session = p_sys->p_cache;
836 p_end = p_session + p_sys->i_cache_size;
838 vlc_mutex_lock( &p_sys->cache_lock );
840 while( p_session < p_end )
842 if( ( p_session->i_idlen == key.size )
843 && !memcmp( p_session->id, key.data, key.size ) )
847 data.size = p_session->i_datalen;
849 data.data = gnutls_malloc( data.size );
850 if( data.data == NULL )
852 vlc_mutex_unlock( &p_sys->cache_lock );
856 memcpy( data.data, p_session->data, data.size );
857 vlc_mutex_unlock( &p_sys->cache_lock );
863 vlc_mutex_unlock( &p_sys->cache_lock );
869 static int cb_delete( void *p_server, gnutls_datum key )
871 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
872 saved_session_t *p_session, *p_end;
874 p_session = p_sys->p_cache;
875 p_end = p_session + p_sys->i_cache_size;
877 vlc_mutex_lock( &p_sys->cache_lock );
879 while( p_session < p_end )
881 if( ( p_session->i_idlen == key.size )
882 && !memcmp( p_session->id, key.data, key.size ) )
884 p_session->i_datalen = p_session->i_idlen = 0;
885 vlc_mutex_unlock( &p_sys->cache_lock );
891 vlc_mutex_unlock( &p_sys->cache_lock );
898 * Terminates TLS session and releases session data.
899 * You still have to close the socket yourself.
902 gnutls_SessionClose (tls_server_t *p_server, tls_session_t *p_session)
904 tls_session_sys_t *p_sys = p_session->p_sys;
907 if( p_sys->b_handshaked == true )
908 gnutls_bye( p_sys->session, GNUTLS_SHUT_WR );
909 gnutls_deinit( p_sys->session );
911 vlc_object_release( p_session );
918 * Initializes a server-side TLS session.
920 static tls_session_t *
921 gnutls_ServerSessionPrepare( tls_server_t *p_server )
923 tls_session_t *p_session;
924 tls_server_sys_t *p_server_sys;
925 gnutls_session_t session;
928 p_session = vlc_object_create( p_server, sizeof (struct tls_session_t) );
929 if( p_session == NULL )
932 p_session->p_sys = malloc( sizeof(struct tls_session_sys_t) );
933 if( p_session->p_sys == NULL )
935 vlc_object_release( p_session );
939 p_server_sys = p_server->p_sys;
940 p_session->sock.p_sys = p_session;
941 p_session->sock.pf_send = gnutls_Send;
942 p_session->sock.pf_recv = gnutls_Recv;
943 p_session->pf_set_fd = gnutls_SetFD;
944 p_session->pf_handshake = p_server_sys->pf_handshake;
946 p_session->p_sys->b_handshaked = false;
947 p_session->p_sys->psz_hostname = NULL;
949 i_val = gnutls_init( &session, GNUTLS_SERVER );
952 msg_Err( p_server, "cannot initialize TLS session: %s",
953 gnutls_strerror( i_val ) );
957 p_session->p_sys->session = session;
959 if (gnutls_SessionPrioritize (VLC_OBJECT (p_session), session))
961 gnutls_deinit( session );
965 i_val = gnutls_credentials_set( session, GNUTLS_CRD_CERTIFICATE,
966 p_server_sys->x509_cred );
969 msg_Err( p_server, "cannot set TLS session credentials: %s",
970 gnutls_strerror( i_val ) );
971 gnutls_deinit( session );
975 if (p_session->pf_handshake == gnutls_HandshakeAndValidate)
976 gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUIRE);
978 /* Session resumption support */
979 i_val = var_InheritInteger (p_server, "gnutls-cache-timeout");
981 gnutls_db_set_cache_expiration (session, i_val);
982 gnutls_db_set_retrieve_function( session, cb_fetch );
983 gnutls_db_set_remove_function( session, cb_delete );
984 gnutls_db_set_store_function( session, cb_store );
985 gnutls_db_set_ptr( session, p_server );
990 free( p_session->p_sys );
991 vlc_object_release( p_session );
997 * Adds one or more certificate authorities.
999 * @param psz_ca_path (Unicode) path to an x509 certificates list.
1001 * @return -1 on error, 0 on success.
1004 gnutls_ServerAddCA( tls_server_t *p_server, const char *psz_ca_path )
1006 tls_server_sys_t *p_sys;
1007 char *psz_local_path;
1010 p_sys = (tls_server_sys_t *)(p_server->p_sys);
1012 psz_local_path = ToLocale( psz_ca_path );
1013 val = gnutls_certificate_set_x509_trust_file( p_sys->x509_cred,
1015 GNUTLS_X509_FMT_PEM );
1016 LocaleFree( psz_local_path );
1019 msg_Err( p_server, "cannot add trusted CA (%s): %s", psz_ca_path,
1020 gnutls_strerror( val ) );
1021 return VLC_EGENERIC;
1023 msg_Dbg( p_server, " %d trusted CA added (%s)", val, psz_ca_path );
1025 /* enables peer's certificate verification */
1026 p_sys->pf_handshake = gnutls_HandshakeAndValidate;
1033 * Adds a certificates revocation list to be sent to TLS clients.
1035 * @param psz_crl_path (Unicode) path of the CRL file.
1037 * @return -1 on error, 0 on success.
1040 gnutls_ServerAddCRL( tls_server_t *p_server, const char *psz_crl_path )
1043 char *psz_local_path = ToLocale( psz_crl_path );
1045 val = gnutls_certificate_set_x509_crl_file( ((tls_server_sys_t *)
1046 (p_server->p_sys))->x509_cred,
1048 GNUTLS_X509_FMT_PEM );
1049 LocaleFree( psz_crl_path );
1052 msg_Err( p_server, "cannot add CRL (%s): %s", psz_crl_path,
1053 gnutls_strerror( val ) );
1054 return VLC_EGENERIC;
1056 msg_Dbg( p_server, "%d CRL added (%s)", val, psz_crl_path );
1062 * Allocates a whole server's TLS credentials.
1064 static int OpenServer (vlc_object_t *obj)
1066 tls_server_t *p_server = (tls_server_t *)obj;
1067 tls_server_sys_t *p_sys;
1070 if (gnutls_Init (obj))
1071 return VLC_EGENERIC;
1073 msg_Dbg (obj, "creating TLS server");
1075 p_sys = (tls_server_sys_t *)malloc( sizeof(struct tls_server_sys_t) );
1079 p_sys->i_cache_size = var_InheritInteger (obj, "gnutls-cache-size");
1080 if (p_sys->i_cache_size == -1) /* Duh, config subsystem exploded?! */
1081 p_sys->i_cache_size = 0;
1082 p_sys->p_cache = calloc (p_sys->i_cache_size,
1083 sizeof (struct saved_session_t));
1084 if (p_sys->p_cache == NULL)
1090 p_sys->p_store = p_sys->p_cache;
1091 p_server->p_sys = p_sys;
1092 p_server->pf_add_CA = gnutls_ServerAddCA;
1093 p_server->pf_add_CRL = gnutls_ServerAddCRL;
1094 p_server->pf_open = gnutls_ServerSessionPrepare;
1095 p_server->pf_close = gnutls_SessionClose;
1097 /* No certificate validation by default */
1098 p_sys->pf_handshake = gnutls_ContinueHandshake;
1100 vlc_mutex_init( &p_sys->cache_lock );
1102 /* Sets server's credentials */
1103 val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
1106 msg_Err( p_server, "cannot allocate X509 credentials: %s",
1107 gnutls_strerror( val ) );
1111 char *psz_cert_path = var_GetNonEmptyString (obj, "tls-x509-cert");
1112 char *psz_key_path = var_GetNonEmptyString (obj, "tls-x509-key");
1113 const char *psz_local_cert = ToLocale (psz_cert_path);
1114 const char *psz_local_key = ToLocale (psz_key_path);
1115 val = gnutls_certificate_set_x509_key_file (p_sys->x509_cred,
1116 psz_local_cert, psz_local_key,
1117 GNUTLS_X509_FMT_PEM );
1118 LocaleFree (psz_local_key);
1119 free (psz_key_path);
1120 LocaleFree (psz_local_cert);
1121 free (psz_cert_path);
1125 msg_Err( p_server, "cannot set certificate chain or private key: %s",
1126 gnutls_strerror( val ) );
1127 gnutls_certificate_free_credentials( p_sys->x509_cred );
1132 * - support other ciper suites
1134 val = gnutls_dh_params_init (&p_sys->dh_params);
1137 const gnutls_datum_t data = {
1138 .data = (unsigned char *)dh_params,
1139 .size = sizeof (dh_params) - 1,
1142 val = gnutls_dh_params_import_pkcs3 (p_sys->dh_params, &data,
1143 GNUTLS_X509_FMT_PEM);
1145 gnutls_certificate_set_dh_params (p_sys->x509_cred,
1150 msg_Err (p_server, "cannot initialize DHE cipher suites: %s",
1151 gnutls_strerror (val));
1157 vlc_mutex_destroy (&p_sys->cache_lock);
1158 free (p_sys->p_cache);
1160 return VLC_EGENERIC;
1164 * Destroys a TLS server object.
1166 static void CloseServer (vlc_object_t *p_server)
1168 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
1170 vlc_mutex_destroy (&p_sys->cache_lock);
1171 free (p_sys->p_cache);
1173 /* all sessions depending on the server are now deinitialized */
1174 gnutls_certificate_free_credentials (p_sys->x509_cred);
1175 gnutls_dh_params_deinit (p_sys->dh_params);
1178 gnutls_Deinit (p_server);