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>
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
70 /*****************************************************************************
72 *****************************************************************************/
73 static int OpenClient (vlc_object_t *);
74 static void CloseClient (vlc_object_t *);
75 static int OpenServer (vlc_object_t *);
76 static void CloseServer (vlc_object_t *);
78 #define CACHE_TIMEOUT_TEXT N_("Expiration time for resumed TLS sessions")
79 #define CACHE_TIMEOUT_LONGTEXT N_( \
80 "It is possible to cache the resumed TLS sessions. This is the expiration "\
81 "time of the sessions stored in this cache, in seconds." )
83 #define CACHE_SIZE_TEXT N_("Number of resumed TLS sessions")
84 #define CACHE_SIZE_LONGTEXT N_( \
85 "This is the maximum number of resumed TLS sessions that " \
86 "the cache will hold." )
89 set_shortname( "GnuTLS" )
90 set_description( N_("GnuTLS transport layer security") )
91 set_capability( "tls client", 1 )
92 set_callbacks( OpenClient, CloseClient )
93 set_category( CAT_ADVANCED )
94 set_subcategory( SUBCAT_ADVANCED_MISC )
96 add_obsolete_bool( "tls-check-cert" )
97 add_obsolete_bool( "tls-check-hostname" )
100 set_description( N_("GnuTLS server") )
101 set_capability( "tls server", 1 )
102 set_category( CAT_ADVANCED )
103 set_subcategory( SUBCAT_ADVANCED_MISC )
104 set_callbacks( OpenServer, CloseServer )
106 add_obsolete_integer( "gnutls-dh-bits" )
107 add_integer( "gnutls-cache-timeout", CACHE_TIMEOUT, NULL,
108 CACHE_TIMEOUT_TEXT, CACHE_TIMEOUT_LONGTEXT, true )
109 add_integer( "gnutls-cache-size", CACHE_SIZE, NULL, CACHE_SIZE_TEXT,
110 CACHE_SIZE_LONGTEXT, true )
113 static vlc_mutex_t gnutls_mutex = VLC_STATIC_MUTEX;
116 * Initializes GnuTLS with proper locking.
117 * @return VLC_SUCCESS on success, a VLC error code otherwise.
119 static int gnutls_Init (vlc_object_t *p_this)
121 int ret = VLC_EGENERIC;
123 vlc_gcrypt_init (); /* GnuTLS depends on gcrypt */
125 vlc_mutex_lock (&gnutls_mutex);
126 if (gnutls_global_init ())
128 msg_Err (p_this, "cannot initialize GnuTLS");
132 const char *psz_version = gnutls_check_version ("1.3.3");
133 if (psz_version == NULL)
135 msg_Err (p_this, "unsupported GnuTLS version");
136 gnutls_global_deinit ();
140 msg_Dbg (p_this, "GnuTLS v%s initialized", psz_version);
144 vlc_mutex_unlock (&gnutls_mutex);
150 * Deinitializes GnuTLS.
152 static void gnutls_Deinit (vlc_object_t *p_this)
154 vlc_mutex_lock (&gnutls_mutex);
156 gnutls_global_deinit ();
157 msg_Dbg (p_this, "GnuTLS deinitialized");
158 vlc_mutex_unlock (&gnutls_mutex);
162 static int gnutls_Error (vlc_object_t *obj, int val)
171 /* WinSock does not return EAGAIN, return EINTR instead */
173 case GNUTLS_E_INTERRUPTED:
175 WSASetLastError (WSAEINTR);
182 msg_Err (obj, "%s", gnutls_strerror (val));
184 if (!gnutls_error_is_fatal (val))
185 msg_Err (obj, "Error above should be handled");
188 WSASetLastError (WSAECONNRESET);
197 struct tls_session_sys_t
199 gnutls_session_t session;
206 * Sends data through a TLS session.
209 gnutls_Send( void *p_session, const void *buf, int i_length )
212 tls_session_sys_t *p_sys;
214 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
216 val = gnutls_record_send( p_sys->session, buf, i_length );
217 return (val < 0) ? gnutls_Error ((vlc_object_t *)p_session, val) : val;
222 * Receives data through a TLS session.
225 gnutls_Recv( void *p_session, void *buf, int i_length )
228 tls_session_sys_t *p_sys;
230 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
232 val = gnutls_record_recv( p_sys->session, buf, i_length );
233 return (val < 0) ? gnutls_Error ((vlc_object_t *)p_session, val) : val;
238 * Starts or continues the TLS handshake.
240 * @return -1 on fatal error, 0 on succesful handshake completion,
241 * 1 if more would-be blocking recv is needed,
242 * 2 if more would-be blocking send is required.
245 gnutls_ContinueHandshake (tls_session_t *p_session)
247 tls_session_sys_t *p_sys = p_session->p_sys;
251 WSASetLastError( 0 );
253 val = gnutls_handshake( p_sys->session );
254 if( ( val == GNUTLS_E_AGAIN ) || ( val == GNUTLS_E_INTERRUPTED ) )
255 return 1 + gnutls_record_get_direction( p_sys->session );
260 msg_Dbg( p_session, "Winsock error %d", WSAGetLastError( ) );
262 msg_Err( p_session, "TLS handshake error: %s",
263 gnutls_strerror( val ) );
267 p_sys->b_handshaked = true;
278 static const error_msg_t cert_errors[] =
280 { GNUTLS_CERT_INVALID,
281 "Certificate could not be verified" },
282 { GNUTLS_CERT_REVOKED,
283 "Certificate was revoked" },
284 { GNUTLS_CERT_SIGNER_NOT_FOUND,
285 "Certificate's signer was not found" },
286 { GNUTLS_CERT_SIGNER_NOT_CA,
287 "Certificate's signer is not a CA" },
288 { GNUTLS_CERT_INSECURE_ALGORITHM,
289 "Insecure certificate signature algorithm" },
295 gnutls_HandshakeAndValidate( tls_session_t *session )
297 int val = gnutls_ContinueHandshake( session );
301 tls_session_sys_t *p_sys = (tls_session_sys_t *)(session->p_sys);
303 /* certificates chain verification */
305 val = gnutls_certificate_verify_peers2( p_sys->session, &status );
309 msg_Err( session, "Certificate verification failed: %s",
310 gnutls_strerror( val ) );
316 msg_Err( session, "TLS session: access denied" );
317 for( const error_msg_t *e = cert_errors; e->flag; e++ )
319 if( status & e->flag )
321 msg_Err( session, "%s", e->msg );
328 "unknown certificate error (you found a bug in VLC)" );
333 /* certificate (host)name verification */
334 const gnutls_datum_t *data;
335 data = gnutls_certificate_get_peers (p_sys->session, &(unsigned){0});
338 msg_Err( session, "Peer certificate not available" );
342 gnutls_x509_crt_t cert;
343 val = gnutls_x509_crt_init( &cert );
346 msg_Err( session, "x509 fatal error: %s", gnutls_strerror( val ) );
350 val = gnutls_x509_crt_import( cert, data, GNUTLS_X509_FMT_DER );
353 msg_Err( session, "Certificate import error: %s",
354 gnutls_strerror( val ) );
358 assert( p_sys->psz_hostname != NULL );
359 if ( !gnutls_x509_crt_check_hostname( cert, p_sys->psz_hostname ) )
361 msg_Err( session, "Certificate does not match \"%s\"",
362 p_sys->psz_hostname );
366 if( gnutls_x509_crt_get_expiration_time( cert ) < time( NULL ) )
368 msg_Err( session, "Certificate expired" );
372 if( gnutls_x509_crt_get_activation_time( cert ) > time ( NULL ) )
374 msg_Err( session, "Certificate not yet valid" );
378 gnutls_x509_crt_deinit( cert );
379 msg_Dbg( session, "TLS/x509 certificate verified" );
383 gnutls_x509_crt_deinit( cert );
388 * Sets the operating system file descriptor backend for the TLS sesison.
390 * @param fd stream socket already connected with the peer.
393 gnutls_SetFD (tls_session_t *p_session, int fd)
395 gnutls_transport_set_ptr (p_session->p_sys->session,
396 (gnutls_transport_ptr_t)(intptr_t)fd);
399 typedef int (*tls_prio_func) (gnutls_session_t, const int *);
402 gnutls_SetPriority (vlc_object_t *restrict obj, const char *restrict name,
403 tls_prio_func func, gnutls_session_t session,
404 const int *restrict values)
406 int val = func (session, values);
409 msg_Err (obj, "cannot set %s priorities: %s", name,
410 gnutls_strerror (val));
418 gnutls_SessionPrioritize (vlc_object_t *obj, gnutls_session_t session)
420 /* Note that ordering matters (on the client side) */
421 static const int protos[] =
423 /*GNUTLS_TLS1_2, as of GnuTLS 2.6.5, still not ratified */
429 static const int comps[] =
435 static const int macs[] =
441 GNUTLS_MAC_RMD160, // RIPEMD
447 static const int ciphers[] =
449 GNUTLS_CIPHER_AES_256_CBC,
450 GNUTLS_CIPHER_AES_128_CBC,
451 GNUTLS_CIPHER_3DES_CBC,
452 GNUTLS_CIPHER_ARCFOUR_128,
453 // TODO? Camellia ciphers?
454 //GNUTLS_CIPHER_DES_CBC,
455 //GNUTLS_CIPHER_ARCFOUR_40,
456 //GNUTLS_CIPHER_RC2_40_CBC,
457 //GNUTLS_CIPHER_NULL,
460 static const int kx[] =
465 //GNUTLS_KX_RSA_EXPORT,
466 //GNUTLS_KX_DHE_PSK, TODO
467 //GNUTLS_KX_PSK, TODO
468 //GNUTLS_KX_SRP_RSA, TODO
469 //GNUTLS_KX_SRP_DSS, TODO
470 //GNUTLS_KX_SRP, TODO
474 static const int cert_types[] =
477 //GNUTLS_CRT_OPENPGP, TODO
481 int val = gnutls_set_default_priority (session);
484 msg_Err (obj, "cannot set default TLS priorities: %s",
485 gnutls_strerror (val));
489 if (gnutls_SetPriority (obj, "protocols",
490 gnutls_protocol_set_priority, session, protos)
491 || gnutls_SetPriority (obj, "compression algorithms",
492 gnutls_compression_set_priority, session, comps)
493 || gnutls_SetPriority (obj, "MAC algorithms",
494 gnutls_mac_set_priority, session, macs)
495 || gnutls_SetPriority (obj, "ciphers",
496 gnutls_cipher_set_priority, session, ciphers)
497 || gnutls_SetPriority (obj, "key exchange algorithms",
498 gnutls_kx_set_priority, session, kx)
499 || gnutls_SetPriority (obj, "certificate types",
500 gnutls_certificate_type_set_priority, session,
509 gnutls_Addx509File( vlc_object_t *p_this,
510 gnutls_certificate_credentials_t cred,
511 const char *psz_path, bool b_priv );
514 gnutls_Addx509Directory( vlc_object_t *p_this,
515 gnutls_certificate_credentials_t cred,
516 const char *psz_dirname,
521 if( *psz_dirname == '\0' )
524 dir = utf8_opendir( psz_dirname );
529 msg_Err (p_this, "cannot open directory (%s): %m", psz_dirname);
533 msg_Dbg (p_this, "creating empty certificate directory: %s",
535 utf8_mkdir (psz_dirname, b_priv ? 0700 : 0755);
541 struct stat st1, st2;
542 int fd = dirfd( dir );
545 * Gets stats for the directory path, checks that it is not a
546 * symbolic link (to avoid possibly infinite recursion), and verifies
547 * that the inode is still the same, to avoid TOCTOU race condition.
550 || fstat( fd, &st1 ) || utf8_lstat( psz_dirname, &st2 )
551 || S_ISLNK( st2.st_mode ) || ( st1.st_ino != st2.st_ino ) )
561 char *ent = utf8_readdir (dir);
565 if ((strcmp (ent, ".") == 0) || (strcmp (ent, "..") == 0))
568 char path[strlen (psz_dirname) + strlen (ent) + 2];
569 sprintf (path, "%s"DIR_SEP"%s", psz_dirname, ent);
572 gnutls_Addx509File( p_this, cred, path, b_priv );
581 gnutls_Addx509File( vlc_object_t *p_this,
582 gnutls_certificate_credentials cred,
583 const char *psz_path, bool b_priv )
587 int fd = utf8_open (psz_path, O_RDONLY, 0);
591 block_t *block = block_File (fd);
596 gnutls_datum data = {
597 .data = block->p_buffer,
598 .size = block->i_buffer,
601 ? gnutls_certificate_set_x509_key_mem (cred, &data, &data,
603 : gnutls_certificate_set_x509_trust_mem (cred, &data,
604 GNUTLS_X509_FMT_PEM);
605 block_Release (block);
609 msg_Warn (p_this, "cannot add x509 credentials (%s): %s",
610 psz_path, gnutls_strerror (res));
613 msg_Dbg (p_this, "added x509 credentials (%s)", psz_path);
617 if (!fstat (fd, &st) && S_ISDIR (st.st_mode))
620 msg_Dbg (p_this, "looking recursively for x509 credentials in %s",
622 return gnutls_Addx509Directory (p_this, cred, psz_path, b_priv);
626 msg_Warn (p_this, "cannot add x509 credentials (%s): %m", psz_path);
633 /** TLS client session data */
634 typedef struct tls_client_sys_t
636 struct tls_session_sys_t session;
637 gnutls_certificate_credentials_t x509_cred;
642 * Initializes a client-side TLS session.
644 static int OpenClient (vlc_object_t *obj)
646 tls_session_t *p_session = (tls_session_t *)obj;
649 if (gnutls_Init (obj))
652 tls_client_sys_t *p_sys = malloc (sizeof (*p_sys));
659 p_session->p_sys = &p_sys->session;
660 p_session->sock.p_sys = p_session;
661 p_session->sock.pf_send = gnutls_Send;
662 p_session->sock.pf_recv = gnutls_Recv;
663 p_session->pf_set_fd = gnutls_SetFD;
665 p_sys->session.b_handshaked = false;
667 i_val = gnutls_certificate_allocate_credentials (&p_sys->x509_cred);
670 msg_Err (obj, "cannot allocate X509 credentials: %s",
671 gnutls_strerror (i_val));
675 char *userdir = config_GetUserDataDir ();
678 char path[strlen (userdir) + sizeof ("/ssl/private")];
679 sprintf (path, "%s/ssl", userdir);
680 utf8_mkdir (path, 0755);
682 sprintf (path, "%s/ssl/certs", userdir);
683 gnutls_Addx509Directory (VLC_OBJECT (p_session),
684 p_sys->x509_cred, path, false);
685 sprintf (path, "%s/ssl/private", userdir);
686 gnutls_Addx509Directory (VLC_OBJECT (p_session), p_sys->x509_cred,
691 const char *confdir = config_GetConfDir ();
693 char path[strlen (confdir)
694 + sizeof ("/ssl/certs/ca-certificates.crt")];
695 sprintf (path, "%s/ssl/certs/ca-certificates.crt", confdir);
696 gnutls_Addx509File (VLC_OBJECT (p_session),
697 p_sys->x509_cred, path, false);
699 p_session->pf_handshake = gnutls_HandshakeAndValidate;
700 /*p_session->pf_handshake = gnutls_ContinueHandshake;*/
702 i_val = gnutls_init (&p_sys->session.session, GNUTLS_CLIENT);
705 msg_Err (obj, "cannot initialize TLS session: %s",
706 gnutls_strerror (i_val));
707 gnutls_certificate_free_credentials (p_sys->x509_cred);
711 if (gnutls_SessionPrioritize (VLC_OBJECT (p_session),
712 p_sys->session.session))
715 /* minimum DH prime bits */
716 gnutls_dh_set_prime_bits (p_sys->session.session, 1024);
718 i_val = gnutls_credentials_set (p_sys->session.session,
719 GNUTLS_CRD_CERTIFICATE,
723 msg_Err (obj, "cannot set TLS session credentials: %s",
724 gnutls_strerror (i_val));
728 char *servername = var_GetNonEmptyString (p_session, "tls-server-name");
729 if (servername == NULL )
730 msg_Err (p_session, "server name missing for TLS session");
732 gnutls_server_name_set (p_sys->session.session, GNUTLS_NAME_DNS,
733 servername, strlen (servername));
735 p_sys->session.psz_hostname = servername;
740 gnutls_deinit (p_sys->session.session);
741 gnutls_certificate_free_credentials (p_sys->x509_cred);
749 static void CloseClient (vlc_object_t *obj)
751 tls_session_t *client = (tls_session_t *)obj;
752 tls_client_sys_t *p_sys = (tls_client_sys_t *)(client->p_sys);
754 if (p_sys->session.b_handshaked == true)
755 gnutls_bye (p_sys->session.session, GNUTLS_SHUT_WR);
756 gnutls_deinit (p_sys->session.session);
757 /* credentials must be free'd *after* gnutls_deinit() */
758 gnutls_certificate_free_credentials (p_sys->x509_cred);
761 free (p_sys->session.psz_hostname);
769 struct tls_server_sys_t
771 gnutls_certificate_credentials_t x509_cred;
772 gnutls_dh_params_t dh_params;
774 struct saved_session_t *p_cache;
775 struct saved_session_t *p_store;
777 vlc_mutex_t cache_lock;
779 int (*pf_handshake) (tls_session_t *);
784 * TLS session resumption callbacks (server-side)
786 #define MAX_SESSION_ID 32
787 #define MAX_SESSION_DATA 1024
789 typedef struct saved_session_t
791 char id[MAX_SESSION_ID];
792 char data[MAX_SESSION_DATA];
799 static int cb_store( void *p_server, gnutls_datum key, gnutls_datum data )
801 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
803 if( ( p_sys->i_cache_size == 0 )
804 || ( key.size > MAX_SESSION_ID )
805 || ( data.size > MAX_SESSION_DATA ) )
808 vlc_mutex_lock( &p_sys->cache_lock );
810 memcpy( p_sys->p_store->id, key.data, key.size);
811 memcpy( p_sys->p_store->data, data.data, data.size );
812 p_sys->p_store->i_idlen = key.size;
813 p_sys->p_store->i_datalen = data.size;
816 if( ( p_sys->p_store - p_sys->p_cache ) == p_sys->i_cache_size )
817 p_sys->p_store = p_sys->p_cache;
819 vlc_mutex_unlock( &p_sys->cache_lock );
825 static gnutls_datum cb_fetch( void *p_server, gnutls_datum key )
827 static const gnutls_datum_t err_datum = { NULL, 0 };
828 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
829 saved_session_t *p_session, *p_end;
831 p_session = p_sys->p_cache;
832 p_end = p_session + p_sys->i_cache_size;
834 vlc_mutex_lock( &p_sys->cache_lock );
836 while( p_session < p_end )
838 if( ( p_session->i_idlen == key.size )
839 && !memcmp( p_session->id, key.data, key.size ) )
843 data.size = p_session->i_datalen;
845 data.data = gnutls_malloc( data.size );
846 if( data.data == NULL )
848 vlc_mutex_unlock( &p_sys->cache_lock );
852 memcpy( data.data, p_session->data, data.size );
853 vlc_mutex_unlock( &p_sys->cache_lock );
859 vlc_mutex_unlock( &p_sys->cache_lock );
865 static int cb_delete( void *p_server, gnutls_datum key )
867 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
868 saved_session_t *p_session, *p_end;
870 p_session = p_sys->p_cache;
871 p_end = p_session + p_sys->i_cache_size;
873 vlc_mutex_lock( &p_sys->cache_lock );
875 while( p_session < p_end )
877 if( ( p_session->i_idlen == key.size )
878 && !memcmp( p_session->id, key.data, key.size ) )
880 p_session->i_datalen = p_session->i_idlen = 0;
881 vlc_mutex_unlock( &p_sys->cache_lock );
887 vlc_mutex_unlock( &p_sys->cache_lock );
894 * Terminates TLS session and releases session data.
895 * You still have to close the socket yourself.
898 gnutls_SessionClose (tls_server_t *p_server, tls_session_t *p_session)
900 tls_session_sys_t *p_sys = p_session->p_sys;
903 if( p_sys->b_handshaked == true )
904 gnutls_bye( p_sys->session, GNUTLS_SHUT_WR );
905 gnutls_deinit( p_sys->session );
907 vlc_object_detach( p_session );
908 vlc_object_release( p_session );
915 * Initializes a server-side TLS session.
917 static tls_session_t *
918 gnutls_ServerSessionPrepare( tls_server_t *p_server )
920 tls_session_t *p_session;
921 tls_server_sys_t *p_server_sys;
922 gnutls_session_t session;
925 p_session = vlc_object_create( p_server, sizeof (struct tls_session_t) );
926 if( p_session == NULL )
929 p_session->p_sys = malloc( sizeof(struct tls_session_sys_t) );
930 if( p_session->p_sys == NULL )
932 vlc_object_release( p_session );
936 p_server_sys = p_server->p_sys;
937 p_session->sock.p_sys = p_session;
938 p_session->sock.pf_send = gnutls_Send;
939 p_session->sock.pf_recv = gnutls_Recv;
940 p_session->pf_set_fd = gnutls_SetFD;
941 p_session->pf_handshake = p_server_sys->pf_handshake;
943 p_session->p_sys->b_handshaked = false;
944 p_session->p_sys->psz_hostname = NULL;
946 i_val = gnutls_init( &session, GNUTLS_SERVER );
949 msg_Err( p_server, "cannot initialize TLS session: %s",
950 gnutls_strerror( i_val ) );
954 p_session->p_sys->session = session;
956 if (gnutls_SessionPrioritize (VLC_OBJECT (p_session), session))
958 gnutls_deinit( session );
962 i_val = gnutls_credentials_set( session, GNUTLS_CRD_CERTIFICATE,
963 p_server_sys->x509_cred );
966 msg_Err( p_server, "cannot set TLS session credentials: %s",
967 gnutls_strerror( i_val ) );
968 gnutls_deinit( session );
972 if (p_session->pf_handshake == gnutls_HandshakeAndValidate)
973 gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUIRE);
975 /* Session resumption support */
976 i_val = config_GetInt (p_server, "gnutls-cache-timeout");
978 gnutls_db_set_cache_expiration (session, i_val);
979 gnutls_db_set_retrieve_function( session, cb_fetch );
980 gnutls_db_set_remove_function( session, cb_delete );
981 gnutls_db_set_store_function( session, cb_store );
982 gnutls_db_set_ptr( session, p_server );
987 free( p_session->p_sys );
988 vlc_object_detach( p_session );
989 vlc_object_release( p_session );
995 * Adds one or more certificate authorities.
997 * @param psz_ca_path (Unicode) path to an x509 certificates list.
999 * @return -1 on error, 0 on success.
1002 gnutls_ServerAddCA( tls_server_t *p_server, const char *psz_ca_path )
1004 tls_server_sys_t *p_sys;
1005 char *psz_local_path;
1008 p_sys = (tls_server_sys_t *)(p_server->p_sys);
1010 psz_local_path = ToLocale( psz_ca_path );
1011 val = gnutls_certificate_set_x509_trust_file( p_sys->x509_cred,
1013 GNUTLS_X509_FMT_PEM );
1014 LocaleFree( psz_local_path );
1017 msg_Err( p_server, "cannot add trusted CA (%s): %s", psz_ca_path,
1018 gnutls_strerror( val ) );
1019 return VLC_EGENERIC;
1021 msg_Dbg( p_server, " %d trusted CA added (%s)", val, psz_ca_path );
1023 /* enables peer's certificate verification */
1024 p_sys->pf_handshake = gnutls_HandshakeAndValidate;
1031 * Adds a certificates revocation list to be sent to TLS clients.
1033 * @param psz_crl_path (Unicode) path of the CRL file.
1035 * @return -1 on error, 0 on success.
1038 gnutls_ServerAddCRL( tls_server_t *p_server, const char *psz_crl_path )
1041 char *psz_local_path = ToLocale( psz_crl_path );
1043 val = gnutls_certificate_set_x509_crl_file( ((tls_server_sys_t *)
1044 (p_server->p_sys))->x509_cred,
1046 GNUTLS_X509_FMT_PEM );
1047 LocaleFree( psz_crl_path );
1050 msg_Err( p_server, "cannot add CRL (%s): %s", psz_crl_path,
1051 gnutls_strerror( val ) );
1052 return VLC_EGENERIC;
1054 msg_Dbg( p_server, "%d CRL added (%s)", val, psz_crl_path );
1060 * Allocates a whole server's TLS credentials.
1062 static int OpenServer (vlc_object_t *obj)
1064 tls_server_t *p_server = (tls_server_t *)obj;
1065 tls_server_sys_t *p_sys;
1068 if (gnutls_Init (obj))
1069 return VLC_EGENERIC;
1071 msg_Dbg (obj, "creating TLS server");
1073 p_sys = (tls_server_sys_t *)malloc( sizeof(struct tls_server_sys_t) );
1077 p_sys->i_cache_size = config_GetInt (obj, "gnutls-cache-size");
1078 if (p_sys->i_cache_size == -1) /* Duh, config subsystem exploded?! */
1079 p_sys->i_cache_size = 0;
1080 p_sys->p_cache = calloc (p_sys->i_cache_size,
1081 sizeof (struct saved_session_t));
1082 if (p_sys->p_cache == NULL)
1088 p_sys->p_store = p_sys->p_cache;
1089 p_server->p_sys = p_sys;
1090 p_server->pf_add_CA = gnutls_ServerAddCA;
1091 p_server->pf_add_CRL = gnutls_ServerAddCRL;
1092 p_server->pf_open = gnutls_ServerSessionPrepare;
1093 p_server->pf_close = gnutls_SessionClose;
1095 /* No certificate validation by default */
1096 p_sys->pf_handshake = gnutls_ContinueHandshake;
1098 vlc_mutex_init( &p_sys->cache_lock );
1100 /* Sets server's credentials */
1101 val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
1104 msg_Err( p_server, "cannot allocate X509 credentials: %s",
1105 gnutls_strerror( val ) );
1109 char *psz_cert_path = var_GetNonEmptyString (obj, "tls-x509-cert");
1110 char *psz_key_path = var_GetNonEmptyString (obj, "tls-x509-key");
1111 const char *psz_local_cert = ToLocale (psz_cert_path);
1112 const char *psz_local_key = ToLocale (psz_key_path);
1113 val = gnutls_certificate_set_x509_key_file (p_sys->x509_cred,
1114 psz_local_cert, psz_local_key,
1115 GNUTLS_X509_FMT_PEM );
1116 LocaleFree (psz_local_key);
1117 free (psz_key_path);
1118 LocaleFree (psz_local_cert);
1119 free (psz_cert_path);
1123 msg_Err( p_server, "cannot set certificate chain or private key: %s",
1124 gnutls_strerror( val ) );
1125 gnutls_certificate_free_credentials( p_sys->x509_cred );
1130 * - support other ciper suites
1132 val = gnutls_dh_params_init (&p_sys->dh_params);
1135 const gnutls_datum_t data = {
1136 .data = (unsigned char *)dh_params,
1137 .size = sizeof (dh_params) - 1,
1140 val = gnutls_dh_params_import_pkcs3 (p_sys->dh_params, &data,
1141 GNUTLS_X509_FMT_PEM);
1143 gnutls_certificate_set_dh_params (p_sys->x509_cred,
1148 msg_Err (p_server, "cannot initialize DHE cipher suites: %s",
1149 gnutls_strerror (val));
1155 vlc_mutex_destroy (&p_sys->cache_lock);
1156 free (p_sys->p_cache);
1158 return VLC_EGENERIC;
1162 * Destroys a TLS server object.
1164 static void CloseServer (vlc_object_t *p_server)
1166 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
1168 vlc_mutex_destroy (&p_sys->cache_lock);
1169 free (p_sys->p_cache);
1171 /* all sessions depending on the server are now deinitialized */
1172 gnutls_certificate_free_credentials (p_sys->x509_cred);
1173 gnutls_dh_params_deinit (p_sys->dh_params);
1176 gnutls_Deinit (p_server);