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 );
114 * Initializes GnuTLS with proper locking.
115 * @return VLC_SUCCESS on success, a VLC error code otherwise.
117 static int gnutls_Init (vlc_object_t *p_this)
119 int ret = VLC_EGENERIC;
121 vlc_gcrypt_init (); /* GnuTLS depends on gcrypt */
123 vlc_mutex_t *lock = var_AcquireMutex ("gnutls_mutex");
124 if (gnutls_global_init ())
126 msg_Err (p_this, "cannot initialize GnuTLS");
130 const char *psz_version = gnutls_check_version ("1.3.3");
131 if (psz_version == NULL)
133 msg_Err (p_this, "unsupported GnuTLS version");
134 gnutls_global_deinit ();
138 msg_Dbg (p_this, "GnuTLS v%s initialized", psz_version);
142 vlc_mutex_unlock (lock);
148 * Deinitializes GnuTLS.
150 static void gnutls_Deinit (vlc_object_t *p_this)
152 vlc_mutex_t *lock = var_AcquireMutex( "gnutls_mutex" );
154 gnutls_global_deinit ();
155 msg_Dbg (p_this, "GnuTLS deinitialized");
156 vlc_mutex_unlock (lock);
160 static int gnutls_Error (vlc_object_t *obj, int val)
169 /* WinSock does not return EAGAIN, return EINTR instead */
171 case GNUTLS_E_INTERRUPTED:
173 WSASetLastError (WSAEINTR);
180 msg_Err (obj, "%s", gnutls_strerror (val));
182 if (!gnutls_error_is_fatal (val))
183 msg_Err (obj, "Error above should be handled");
186 WSASetLastError (WSAECONNRESET);
195 struct tls_session_sys_t
197 gnutls_session_t session;
204 * Sends data through a TLS session.
207 gnutls_Send( void *p_session, const void *buf, int i_length )
210 tls_session_sys_t *p_sys;
212 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
214 val = gnutls_record_send( p_sys->session, buf, i_length );
215 return (val < 0) ? gnutls_Error ((vlc_object_t *)p_session, val) : val;
220 * Receives data through a TLS session.
223 gnutls_Recv( void *p_session, void *buf, int i_length )
226 tls_session_sys_t *p_sys;
228 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
230 val = gnutls_record_recv( p_sys->session, buf, i_length );
231 return (val < 0) ? gnutls_Error ((vlc_object_t *)p_session, val) : val;
236 * Starts or continues the TLS handshake.
238 * @return -1 on fatal error, 0 on succesful handshake completion,
239 * 1 if more would-be blocking recv is needed,
240 * 2 if more would-be blocking send is required.
243 gnutls_ContinueHandshake (tls_session_t *p_session)
245 tls_session_sys_t *p_sys = p_session->p_sys;
249 WSASetLastError( 0 );
251 val = gnutls_handshake( p_sys->session );
252 if( ( val == GNUTLS_E_AGAIN ) || ( val == GNUTLS_E_INTERRUPTED ) )
253 return 1 + gnutls_record_get_direction( p_sys->session );
258 msg_Dbg( p_session, "Winsock error %d", WSAGetLastError( ) );
260 msg_Err( p_session, "TLS handshake error: %s",
261 gnutls_strerror( val ) );
265 p_sys->b_handshaked = true;
276 static const error_msg_t cert_errors[] =
278 { GNUTLS_CERT_INVALID,
279 "Certificate could not be verified" },
280 { GNUTLS_CERT_REVOKED,
281 "Certificate was revoked" },
282 { GNUTLS_CERT_SIGNER_NOT_FOUND,
283 "Certificate's signer was not found" },
284 { GNUTLS_CERT_SIGNER_NOT_CA,
285 "Certificate's signer is not a CA" },
286 { GNUTLS_CERT_INSECURE_ALGORITHM,
287 "Insecure certificate signature algorithm" },
293 gnutls_HandshakeAndValidate( tls_session_t *session )
295 int val = gnutls_ContinueHandshake( session );
299 tls_session_sys_t *p_sys = (tls_session_sys_t *)(session->p_sys);
301 /* certificates chain verification */
303 val = gnutls_certificate_verify_peers2( p_sys->session, &status );
307 msg_Err( session, "Certificate verification failed: %s",
308 gnutls_strerror( val ) );
314 msg_Err( session, "TLS session: access denied" );
315 for( const error_msg_t *e = cert_errors; e->flag; e++ )
317 if( status & e->flag )
319 msg_Err( session, "%s", e->msg );
326 "unknown certificate error (you found a bug in VLC)" );
331 /* certificate (host)name verification */
332 const gnutls_datum_t *data;
333 data = gnutls_certificate_get_peers (p_sys->session, &(unsigned){0});
336 msg_Err( session, "Peer certificate not available" );
340 gnutls_x509_crt_t cert;
341 val = gnutls_x509_crt_init( &cert );
344 msg_Err( session, "x509 fatal error: %s", gnutls_strerror( val ) );
348 val = gnutls_x509_crt_import( cert, data, GNUTLS_X509_FMT_DER );
351 msg_Err( session, "Certificate import error: %s",
352 gnutls_strerror( val ) );
356 assert( p_sys->psz_hostname != NULL );
357 if ( !gnutls_x509_crt_check_hostname( cert, p_sys->psz_hostname ) )
359 msg_Err( session, "Certificate does not match \"%s\"",
360 p_sys->psz_hostname );
364 if( gnutls_x509_crt_get_expiration_time( cert ) < time( NULL ) )
366 msg_Err( session, "Certificate expired" );
370 if( gnutls_x509_crt_get_activation_time( cert ) > time ( NULL ) )
372 msg_Err( session, "Certificate not yet valid" );
376 gnutls_x509_crt_deinit( cert );
377 msg_Dbg( session, "TLS/x509 certificate verified" );
381 gnutls_x509_crt_deinit( cert );
386 * Sets the operating system file descriptor backend for the TLS sesison.
388 * @param fd stream socket already connected with the peer.
391 gnutls_SetFD (tls_session_t *p_session, int fd)
393 gnutls_transport_set_ptr (p_session->p_sys->session,
394 (gnutls_transport_ptr_t)(intptr_t)fd);
397 typedef int (*tls_prio_func) (gnutls_session_t, const int *);
400 gnutls_SetPriority (vlc_object_t *restrict obj, const char *restrict name,
401 tls_prio_func func, gnutls_session_t session,
402 const int *restrict values)
404 int val = func (session, values);
407 msg_Err (obj, "cannot set %s priorities: %s", name,
408 gnutls_strerror (val));
416 gnutls_SessionPrioritize (vlc_object_t *obj, gnutls_session_t session)
418 /* Note that ordering matters (on the client side) */
419 static const int protos[] =
426 static const int comps[] =
432 static const int macs[] =
435 GNUTLS_MAC_RMD160, // RIPEMD
441 static const int ciphers[] =
443 GNUTLS_CIPHER_AES_256_CBC,
444 GNUTLS_CIPHER_AES_128_CBC,
445 GNUTLS_CIPHER_3DES_CBC,
446 GNUTLS_CIPHER_ARCFOUR_128,
447 //GNUTLS_CIPHER_DES_CBC,
448 //GNUTLS_CIPHER_ARCFOUR_40,
449 //GNUTLS_CIPHER_RC2_40_CBC,
450 //GNUTLS_CIPHER_NULL,
453 static const int kx[] =
458 //GNUTLS_KX_RSA_EXPORT,
459 //GNUTLS_KX_DHE_PSK, TODO
460 //GNUTLS_KX_PSK, TODO
461 //GNUTLS_KX_SRP_RSA, TODO
462 //GNUTLS_KX_SRP_DSS, TODO
463 //GNUTLS_KX_SRP, TODO
467 static const int cert_types[] =
470 //GNUTLS_CRT_OPENPGP, TODO
474 int val = gnutls_set_default_priority (session);
477 msg_Err (obj, "cannot set default TLS priorities: %s",
478 gnutls_strerror (val));
482 if (gnutls_SetPriority (obj, "protocols",
483 gnutls_protocol_set_priority, session, protos)
484 || gnutls_SetPriority (obj, "compression algorithms",
485 gnutls_compression_set_priority, session, comps)
486 || gnutls_SetPriority (obj, "MAC algorithms",
487 gnutls_mac_set_priority, session, macs)
488 || gnutls_SetPriority (obj, "ciphers",
489 gnutls_cipher_set_priority, session, ciphers)
490 || gnutls_SetPriority (obj, "key exchange algorithms",
491 gnutls_kx_set_priority, session, kx)
492 || gnutls_SetPriority (obj, "certificate types",
493 gnutls_certificate_type_set_priority, session,
502 gnutls_Addx509File( vlc_object_t *p_this,
503 gnutls_certificate_credentials_t cred,
504 const char *psz_path, bool b_priv );
507 gnutls_Addx509Directory( vlc_object_t *p_this,
508 gnutls_certificate_credentials_t cred,
509 const char *psz_dirname,
514 if( *psz_dirname == '\0' )
517 dir = utf8_opendir( psz_dirname );
522 msg_Err (p_this, "cannot open directory (%s): %m", psz_dirname);
526 msg_Dbg (p_this, "creating empty certificate directory: %s",
528 utf8_mkdir (psz_dirname, b_priv ? 0700 : 0755);
534 struct stat st1, st2;
535 int fd = dirfd( dir );
538 * Gets stats for the directory path, checks that it is not a
539 * symbolic link (to avoid possibly infinite recursion), and verifies
540 * that the inode is still the same, to avoid TOCTOU race condition.
543 || fstat( fd, &st1 ) || utf8_lstat( psz_dirname, &st2 )
544 || S_ISLNK( st2.st_mode ) || ( st1.st_ino != st2.st_ino ) )
554 char *ent = utf8_readdir (dir);
558 if ((strcmp (ent, ".") == 0) || (strcmp (ent, "..") == 0))
561 char path[strlen (psz_dirname) + strlen (ent) + 2];
562 sprintf (path, "%s"DIR_SEP"%s", psz_dirname, ent);
565 gnutls_Addx509File( p_this, cred, path, b_priv );
574 gnutls_Addx509File( vlc_object_t *p_this,
575 gnutls_certificate_credentials cred,
576 const char *psz_path, bool b_priv )
580 int fd = utf8_open (psz_path, O_RDONLY, 0);
584 block_t *block = block_File (fd);
589 gnutls_datum data = {
590 .data = block->p_buffer,
591 .size = block->i_buffer,
594 ? gnutls_certificate_set_x509_key_mem (cred, &data, &data,
596 : gnutls_certificate_set_x509_trust_mem (cred, &data,
597 GNUTLS_X509_FMT_PEM);
598 block_Release (block);
602 msg_Warn (p_this, "cannot add x509 credentials (%s): %s",
603 psz_path, gnutls_strerror (res));
606 msg_Dbg (p_this, "added x509 credentials (%s)", psz_path);
610 if (!fstat (fd, &st) && S_ISDIR (st.st_mode))
613 msg_Dbg (p_this, "looking recursively for x509 credentials in %s",
615 return gnutls_Addx509Directory (p_this, cred, psz_path, b_priv);
619 msg_Warn (p_this, "cannot add x509 credentials (%s): %m", psz_path);
626 /** TLS client session data */
627 typedef struct tls_client_sys_t
629 struct tls_session_sys_t session;
630 gnutls_certificate_credentials_t x509_cred;
635 * Initializes a client-side TLS session.
637 static int OpenClient (vlc_object_t *obj)
639 tls_session_t *p_session = (tls_session_t *)obj;
642 if (gnutls_Init (obj))
645 tls_client_sys_t *p_sys = malloc (sizeof (*p_sys));
652 p_session->p_sys = &p_sys->session;
653 p_session->sock.p_sys = p_session;
654 p_session->sock.pf_send = gnutls_Send;
655 p_session->sock.pf_recv = gnutls_Recv;
656 p_session->pf_set_fd = gnutls_SetFD;
658 p_sys->session.b_handshaked = false;
660 i_val = gnutls_certificate_allocate_credentials (&p_sys->x509_cred);
663 msg_Err (obj, "cannot allocate X509 credentials: %s",
664 gnutls_strerror (i_val));
668 char *userdir = config_GetUserDataDir ();
671 char path[strlen (userdir) + sizeof ("/ssl/private")];
672 sprintf (path, "%s/ssl", userdir);
673 utf8_mkdir (path, 0755);
675 sprintf (path, "%s/ssl/certs", userdir);
676 gnutls_Addx509Directory (VLC_OBJECT (p_session),
677 p_sys->x509_cred, path, false);
678 sprintf (path, "%s/ssl/private", userdir);
679 gnutls_Addx509Directory (VLC_OBJECT (p_session), p_sys->x509_cred,
684 const char *confdir = config_GetConfDir ();
686 char path[strlen (confdir)
687 + sizeof ("/ssl/certs/ca-certificates.crt")];
688 sprintf (path, "%s/ssl/certs/ca-certificates.crt", confdir);
689 gnutls_Addx509File (VLC_OBJECT (p_session),
690 p_sys->x509_cred, path, false);
692 p_session->pf_handshake = gnutls_HandshakeAndValidate;
693 /*p_session->pf_handshake = gnutls_ContinueHandshake;*/
695 i_val = gnutls_init (&p_sys->session.session, GNUTLS_CLIENT);
698 msg_Err (obj, "cannot initialize TLS session: %s",
699 gnutls_strerror (i_val));
700 gnutls_certificate_free_credentials (p_sys->x509_cred);
704 if (gnutls_SessionPrioritize (VLC_OBJECT (p_session),
705 p_sys->session.session))
708 /* minimum DH prime bits */
709 gnutls_dh_set_prime_bits (p_sys->session.session, 1024);
711 i_val = gnutls_credentials_set (p_sys->session.session,
712 GNUTLS_CRD_CERTIFICATE,
716 msg_Err (obj, "cannot set TLS session credentials: %s",
717 gnutls_strerror (i_val));
721 char *servername = var_GetNonEmptyString (p_session, "tls-server-name");
722 if (servername == NULL )
723 msg_Err (p_session, "server name missing for TLS session");
725 p_sys->session.psz_hostname = servername;
726 gnutls_server_name_set (p_sys->session.session, GNUTLS_NAME_DNS,
727 servername, strlen (servername));
732 gnutls_deinit (p_sys->session.session);
733 gnutls_certificate_free_credentials (p_sys->x509_cred);
741 static void CloseClient (vlc_object_t *obj)
743 tls_session_t *client = (tls_session_t *)obj;
744 tls_client_sys_t *p_sys = (tls_client_sys_t *)(client->p_sys);
746 if (p_sys->session.b_handshaked == true)
747 gnutls_bye (p_sys->session.session, GNUTLS_SHUT_WR);
748 gnutls_deinit (p_sys->session.session);
749 /* credentials must be free'd *after* gnutls_deinit() */
750 gnutls_certificate_free_credentials (p_sys->x509_cred);
753 free (p_sys->session.psz_hostname);
761 struct tls_server_sys_t
763 gnutls_certificate_credentials_t x509_cred;
764 gnutls_dh_params_t dh_params;
766 struct saved_session_t *p_cache;
767 struct saved_session_t *p_store;
769 vlc_mutex_t cache_lock;
771 int (*pf_handshake) (tls_session_t *);
776 * TLS session resumption callbacks (server-side)
778 #define MAX_SESSION_ID 32
779 #define MAX_SESSION_DATA 1024
781 typedef struct saved_session_t
783 char id[MAX_SESSION_ID];
784 char data[MAX_SESSION_DATA];
791 static int cb_store( void *p_server, gnutls_datum key, gnutls_datum data )
793 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
795 if( ( p_sys->i_cache_size == 0 )
796 || ( key.size > MAX_SESSION_ID )
797 || ( data.size > MAX_SESSION_DATA ) )
800 vlc_mutex_lock( &p_sys->cache_lock );
802 memcpy( p_sys->p_store->id, key.data, key.size);
803 memcpy( p_sys->p_store->data, data.data, data.size );
804 p_sys->p_store->i_idlen = key.size;
805 p_sys->p_store->i_datalen = data.size;
808 if( ( p_sys->p_store - p_sys->p_cache ) == p_sys->i_cache_size )
809 p_sys->p_store = p_sys->p_cache;
811 vlc_mutex_unlock( &p_sys->cache_lock );
817 static gnutls_datum cb_fetch( void *p_server, gnutls_datum key )
819 static const gnutls_datum_t err_datum = { NULL, 0 };
820 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
821 saved_session_t *p_session, *p_end;
823 p_session = p_sys->p_cache;
824 p_end = p_session + p_sys->i_cache_size;
826 vlc_mutex_lock( &p_sys->cache_lock );
828 while( p_session < p_end )
830 if( ( p_session->i_idlen == key.size )
831 && !memcmp( p_session->id, key.data, key.size ) )
835 data.size = p_session->i_datalen;
837 data.data = gnutls_malloc( data.size );
838 if( data.data == NULL )
840 vlc_mutex_unlock( &p_sys->cache_lock );
844 memcpy( data.data, p_session->data, data.size );
845 vlc_mutex_unlock( &p_sys->cache_lock );
851 vlc_mutex_unlock( &p_sys->cache_lock );
857 static int cb_delete( void *p_server, gnutls_datum key )
859 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
860 saved_session_t *p_session, *p_end;
862 p_session = p_sys->p_cache;
863 p_end = p_session + p_sys->i_cache_size;
865 vlc_mutex_lock( &p_sys->cache_lock );
867 while( p_session < p_end )
869 if( ( p_session->i_idlen == key.size )
870 && !memcmp( p_session->id, key.data, key.size ) )
872 p_session->i_datalen = p_session->i_idlen = 0;
873 vlc_mutex_unlock( &p_sys->cache_lock );
879 vlc_mutex_unlock( &p_sys->cache_lock );
886 * Terminates TLS session and releases session data.
887 * You still have to close the socket yourself.
890 gnutls_SessionClose (tls_server_t *p_server, tls_session_t *p_session)
892 tls_session_sys_t *p_sys = p_session->p_sys;
895 if( p_sys->b_handshaked == true )
896 gnutls_bye( p_sys->session, GNUTLS_SHUT_WR );
897 gnutls_deinit( p_sys->session );
899 vlc_object_detach( p_session );
900 vlc_object_release( p_session );
907 * Initializes a server-side TLS session.
909 static tls_session_t *
910 gnutls_ServerSessionPrepare( tls_server_t *p_server )
912 tls_session_t *p_session;
913 tls_server_sys_t *p_server_sys;
914 gnutls_session_t session;
917 p_session = vlc_object_create( p_server, sizeof (struct tls_session_t) );
918 if( p_session == NULL )
921 p_session->p_sys = malloc( sizeof(struct tls_session_sys_t) );
922 if( p_session->p_sys == NULL )
924 vlc_object_release( p_session );
928 p_server_sys = p_server->p_sys;
929 p_session->sock.p_sys = p_session;
930 p_session->sock.pf_send = gnutls_Send;
931 p_session->sock.pf_recv = gnutls_Recv;
932 p_session->pf_set_fd = gnutls_SetFD;
933 p_session->pf_handshake = p_server_sys->pf_handshake;
935 p_session->p_sys->b_handshaked = false;
936 p_session->p_sys->psz_hostname = NULL;
938 i_val = gnutls_init( &session, GNUTLS_SERVER );
941 msg_Err( p_server, "cannot initialize TLS session: %s",
942 gnutls_strerror( i_val ) );
946 p_session->p_sys->session = session;
948 if (gnutls_SessionPrioritize (VLC_OBJECT (p_session), session))
950 gnutls_deinit( session );
954 i_val = gnutls_credentials_set( session, GNUTLS_CRD_CERTIFICATE,
955 p_server_sys->x509_cred );
958 msg_Err( p_server, "cannot set TLS session credentials: %s",
959 gnutls_strerror( i_val ) );
960 gnutls_deinit( session );
964 if (p_session->pf_handshake == gnutls_HandshakeAndValidate)
965 gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUIRE);
967 /* Session resumption support */
968 i_val = config_GetInt (p_server, "gnutls-cache-timeout");
969 gnutls_db_set_cache_expiration (session, i_val);
970 gnutls_db_set_retrieve_function( session, cb_fetch );
971 gnutls_db_set_remove_function( session, cb_delete );
972 gnutls_db_set_store_function( session, cb_store );
973 gnutls_db_set_ptr( session, p_server );
978 free( p_session->p_sys );
979 vlc_object_detach( p_session );
980 vlc_object_release( p_session );
986 * Adds one or more certificate authorities.
988 * @param psz_ca_path (Unicode) path to an x509 certificates list.
990 * @return -1 on error, 0 on success.
993 gnutls_ServerAddCA( tls_server_t *p_server, const char *psz_ca_path )
995 tls_server_sys_t *p_sys;
996 char *psz_local_path;
999 p_sys = (tls_server_sys_t *)(p_server->p_sys);
1001 psz_local_path = ToLocale( psz_ca_path );
1002 val = gnutls_certificate_set_x509_trust_file( p_sys->x509_cred,
1004 GNUTLS_X509_FMT_PEM );
1005 LocaleFree( psz_local_path );
1008 msg_Err( p_server, "cannot add trusted CA (%s): %s", psz_ca_path,
1009 gnutls_strerror( val ) );
1010 return VLC_EGENERIC;
1012 msg_Dbg( p_server, " %d trusted CA added (%s)", val, psz_ca_path );
1014 /* enables peer's certificate verification */
1015 p_sys->pf_handshake = gnutls_HandshakeAndValidate;
1022 * Adds a certificates revocation list to be sent to TLS clients.
1024 * @param psz_crl_path (Unicode) path of the CRL file.
1026 * @return -1 on error, 0 on success.
1029 gnutls_ServerAddCRL( tls_server_t *p_server, const char *psz_crl_path )
1032 char *psz_local_path = ToLocale( psz_crl_path );
1034 val = gnutls_certificate_set_x509_crl_file( ((tls_server_sys_t *)
1035 (p_server->p_sys))->x509_cred,
1037 GNUTLS_X509_FMT_PEM );
1038 LocaleFree( psz_crl_path );
1041 msg_Err( p_server, "cannot add CRL (%s): %s", psz_crl_path,
1042 gnutls_strerror( val ) );
1043 return VLC_EGENERIC;
1045 msg_Dbg( p_server, "%d CRL added (%s)", val, psz_crl_path );
1051 * Allocates a whole server's TLS credentials.
1053 static int OpenServer (vlc_object_t *obj)
1055 tls_server_t *p_server = (tls_server_t *)obj;
1056 tls_server_sys_t *p_sys;
1059 if (gnutls_Init (obj))
1060 return VLC_EGENERIC;
1062 msg_Dbg (obj, "creating TLS server");
1064 p_sys = (tls_server_sys_t *)malloc( sizeof(struct tls_server_sys_t) );
1068 p_sys->i_cache_size = config_GetInt (obj, "gnutls-cache-size");
1069 p_sys->p_cache = calloc (p_sys->i_cache_size,
1070 sizeof (struct saved_session_t));
1071 if (p_sys->p_cache == NULL)
1077 p_sys->p_store = p_sys->p_cache;
1078 p_server->p_sys = p_sys;
1079 p_server->pf_add_CA = gnutls_ServerAddCA;
1080 p_server->pf_add_CRL = gnutls_ServerAddCRL;
1081 p_server->pf_open = gnutls_ServerSessionPrepare;
1082 p_server->pf_close = gnutls_SessionClose;
1084 /* No certificate validation by default */
1085 p_sys->pf_handshake = gnutls_ContinueHandshake;
1087 vlc_mutex_init( &p_sys->cache_lock );
1089 /* Sets server's credentials */
1090 val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
1093 msg_Err( p_server, "cannot allocate X509 credentials: %s",
1094 gnutls_strerror( val ) );
1098 char *psz_cert_path = var_GetNonEmptyString (obj, "tls-x509-cert");
1099 char *psz_key_path = var_GetNonEmptyString (obj, "tls-x509-key");
1100 const char *psz_local_cert = ToLocale (psz_cert_path);
1101 const char *psz_local_key = ToLocale (psz_key_path);
1102 val = gnutls_certificate_set_x509_key_file (p_sys->x509_cred,
1103 psz_local_cert, psz_local_key,
1104 GNUTLS_X509_FMT_PEM );
1105 LocaleFree (psz_local_key);
1106 free (psz_key_path);
1107 LocaleFree (psz_local_cert);
1108 free (psz_cert_path);
1112 msg_Err( p_server, "cannot set certificate chain or private key: %s",
1113 gnutls_strerror( val ) );
1114 gnutls_certificate_free_credentials( p_sys->x509_cred );
1119 * - support other ciper suites
1121 val = gnutls_dh_params_init (&p_sys->dh_params);
1124 const gnutls_datum_t data = {
1125 .data = (unsigned char *)dh_params,
1126 .size = sizeof (dh_params) - 1,
1129 val = gnutls_dh_params_import_pkcs3 (p_sys->dh_params, &data,
1130 GNUTLS_X509_FMT_PEM);
1132 gnutls_certificate_set_dh_params (p_sys->x509_cred,
1137 msg_Err (p_server, "cannot initialize DHE cipher suites: %s",
1138 gnutls_strerror (val));
1144 vlc_mutex_destroy (&p_sys->cache_lock);
1145 free (p_sys->p_cache);
1147 return VLC_EGENERIC;
1151 * Destroys a TLS server object.
1153 static void CloseServer (vlc_object_t *p_server)
1155 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
1157 vlc_mutex_destroy (&p_sys->cache_lock);
1158 free (p_sys->p_cache);
1160 /* all sessions depending on the server are now deinitialized */
1161 gnutls_certificate_free_credentials (p_sys->x509_cred);
1162 gnutls_dh_params_deinit (p_sys->dh_params);
1165 gnutls_Deinit (p_server);