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 <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
39 # include <sys/stat.h>
47 #include <vlc_charset.h>
50 #include <gnutls/gnutls.h>
51 #include <gnutls/x509.h>
54 #define CACHE_EXPIRATION 3600
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Open ( vlc_object_t * );
61 static void Close( vlc_object_t * );
63 #define DH_BITS_TEXT N_("Diffie-Hellman prime bits")
64 #define DH_BITS_LONGTEXT N_( \
65 "This allows you to modify the Diffie-Hellman prime's number of bits, " \
66 "used for TLS or SSL-based server-side encryption. This is generally " \
69 #define CACHE_EXPIRATION_TEXT N_("Expiration time for resumed TLS sessions")
70 #define CACHE_EXPIRATION_LONGTEXT N_( \
71 "It is possible to cache the resumed TLS sessions. This is the expiration "\
72 "time of the sessions stored in this cache, in seconds." )
74 #define CACHE_SIZE_TEXT N_("Number of resumed TLS sessions")
75 #define CACHE_SIZE_LONGTEXT N_( \
76 "This is the maximum number of resumed TLS sessions that " \
77 "the cache will hold." )
79 #define CHECK_CERT_TEXT N_("Check TLS/SSL server certificate validity")
80 #define CHECK_CERT_LONGTEXT N_( \
81 "This ensures that the server certificate is valid " \
82 "(i.e. signed by an approved Certification Authority)." )
84 #define CHECK_HOSTNAME_TEXT N_("Check TLS/SSL server hostname in certificate")
85 #define CHECK_HOSTNAME_LONGTEXT N_( \
86 "This ensures that the server hostname in certificate matches the " \
87 "requested host name." )
90 set_shortname( "GnuTLS" );
91 set_description( _("GnuTLS TLS encryption layer") );
92 set_capability( "tls", 1 );
93 set_callbacks( Open, Close );
94 set_category( CAT_ADVANCED );
95 set_subcategory( SUBCAT_ADVANCED_MISC );
97 add_bool( "tls-check-cert", VLC_TRUE, NULL, CHECK_CERT_TEXT,
98 CHECK_CERT_LONGTEXT, VLC_FALSE );
99 add_bool( "tls-check-hostname", VLC_TRUE, NULL, CHECK_HOSTNAME_TEXT,
100 CHECK_HOSTNAME_LONGTEXT, VLC_FALSE );
102 add_integer( "gnutls-dh-bits", DH_BITS, NULL, DH_BITS_TEXT,
103 DH_BITS_LONGTEXT, VLC_TRUE );
104 add_integer( "gnutls-cache-expiration", CACHE_EXPIRATION, NULL,
105 CACHE_EXPIRATION_TEXT, CACHE_EXPIRATION_LONGTEXT, VLC_TRUE );
106 add_integer( "gnutls-cache-size", CACHE_SIZE, NULL, CACHE_SIZE_TEXT,
107 CACHE_SIZE_LONGTEXT, VLC_TRUE );
111 #define MAX_SESSION_ID 32
112 #define MAX_SESSION_DATA 1024
114 typedef struct saved_session_t
116 char id[MAX_SESSION_ID];
117 char data[MAX_SESSION_DATA];
124 typedef struct tls_server_sys_t
126 gnutls_certificate_credentials x509_cred;
127 gnutls_dh_params dh_params;
129 struct saved_session_t *p_cache;
130 struct saved_session_t *p_store;
132 vlc_mutex_t cache_lock;
134 int (*pf_handshake2)( tls_session_t * );
138 typedef struct tls_session_sys_t
140 gnutls_session session;
142 vlc_bool_t b_handshaked;
146 typedef struct tls_client_sys_t
148 struct tls_session_sys_t session;
149 gnutls_certificate_credentials x509_cred;
153 static int gnutls_Error (vlc_object_t *obj, int val)
162 /* WinSock does not return EAGAIN, return EINTR instead */
164 case GNUTLS_E_INTERRUPTED:
166 WSASetLastError(WSAEINTR);
173 msg_Err (obj, "%s", gnutls_strerror (val));
175 if (!gnutls_error_is_fatal (val))
176 msg_Err (obj, "Error above should be handled");
179 WSASetLastError(WSAECONNRESET);
189 * Sends data through a TLS session.
192 gnutls_Send( void *p_session, const void *buf, int i_length )
195 tls_session_sys_t *p_sys;
197 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
199 val = gnutls_record_send( p_sys->session, buf, i_length );
200 return (val < 0) ? gnutls_Error ((vlc_object_t *)p_session, val) : val;
205 * Receives data through a TLS session.
208 gnutls_Recv( void *p_session, void *buf, int i_length )
211 tls_session_sys_t *p_sys;
213 p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys);
215 val = gnutls_record_recv( p_sys->session, buf, i_length );
216 return (val < 0) ? gnutls_Error ((vlc_object_t *)p_session, val) : val;
220 /*****************************************************************************
221 * tls_Session(Continue)?Handshake:
222 *****************************************************************************
223 * Establishes TLS session with a peer through socket <fd>.
224 * Returns -1 on error (you need not and must not call tls_SessionClose)
225 * 0 on succesful handshake completion, 1 if more would-be blocking recv is
226 * needed, 2 if more would-be blocking send is required.
227 *****************************************************************************/
229 gnutls_ContinueHandshake( tls_session_t *p_session)
231 tls_session_sys_t *p_sys;
234 p_sys = (tls_session_sys_t *)(p_session->p_sys);
237 WSASetLastError( 0 );
239 val = gnutls_handshake( p_sys->session );
240 if( ( val == GNUTLS_E_AGAIN ) || ( val == GNUTLS_E_INTERRUPTED ) )
241 return 1 + gnutls_record_get_direction( p_sys->session );
246 msg_Dbg( p_session, "Winsock error %d", WSAGetLastError( ) );
248 msg_Err( p_session, "TLS handshake error: %s",
249 gnutls_strerror( val ) );
250 p_session->pf_close( p_session );
254 p_sys->b_handshaked = VLC_TRUE;
265 static const error_msg_t cert_errors[] =
267 { GNUTLS_CERT_INVALID,
268 "Certificate could not be verified" },
269 { GNUTLS_CERT_REVOKED,
270 "Certificate was revoked" },
271 { GNUTLS_CERT_SIGNER_NOT_FOUND,
272 "Certificate's signer was not found" },
273 { GNUTLS_CERT_SIGNER_NOT_CA,
274 "Certificate's signer is not a CA" },
275 { GNUTLS_CERT_INSECURE_ALGORITHM,
276 "Insecure certificate signature algorithm" },
282 gnutls_HandshakeAndValidate( tls_session_t *session )
284 int val = gnutls_ContinueHandshake( session );
288 tls_session_sys_t *p_sys = (tls_session_sys_t *)(session->p_sys);
290 /* certificates chain verification */
292 val = gnutls_certificate_verify_peers2( p_sys->session, &status );
296 msg_Err( session, "Certificate verification failed: %s",
297 gnutls_strerror( val ) );
303 msg_Err( session, "TLS session: access denied" );
304 for( const error_msg_t *e = cert_errors; e->flag; e++ )
306 if( status & e->flag )
308 msg_Err( session, "%s", e->msg );
315 "unknown certificate error (you found a bug in VLC)" );
320 /* certificate (host)name verification */
321 const gnutls_datum *data = gnutls_certificate_get_peers( p_sys->session,
325 msg_Err( session, "Peer certificate not available" );
329 gnutls_x509_crt cert;
330 val = gnutls_x509_crt_init( &cert );
333 msg_Err( session, "x509 fatal error: %s", gnutls_strerror( val ) );
337 val = gnutls_x509_crt_import( cert, data, GNUTLS_X509_FMT_DER );
340 msg_Err( session, "Certificate import error: %s",
341 gnutls_strerror( val ) );
342 gnutls_x509_crt_deinit( cert );
346 if( p_sys->psz_hostname != NULL )
348 if ( !gnutls_x509_crt_check_hostname( cert, p_sys->psz_hostname ) )
350 msg_Err( session, "Certificate does not match \"%s\"",
351 p_sys->psz_hostname );
356 msg_Warn( session, "Certificate and hostname were not verified" );
358 if( gnutls_x509_crt_get_expiration_time( cert ) < time( NULL ) )
360 msg_Err( session, "Certificate expired" );
364 if( gnutls_x509_crt_get_activation_time( cert ) > time ( NULL ) )
366 msg_Err( session, "Certificate not yet valid" );
370 gnutls_x509_crt_deinit( cert );
371 msg_Dbg( session, "TLS/x509 certificate verified" );
375 gnutls_x509_crt_deinit( cert );
377 session->pf_close( session );
382 * Starts negociation of a TLS session.
384 * @param fd stream socket already connected with the peer.
385 * @param psz_hostname if not NULL, hostname to mention as a Server Name.
387 * @return -1 on error (you need not and must not call tls_SessionClose),
388 * 0 on succesful handshake completion, 1 if more would-be blocking recv is
389 * needed, 2 if more would-be blocking send is required.
392 gnutls_BeginHandshake( tls_session_t *p_session, int fd,
393 const char *psz_hostname )
395 tls_session_sys_t *p_sys;
397 p_sys = (tls_session_sys_t *)(p_session->p_sys);
399 gnutls_transport_set_ptr (p_sys->session, (gnutls_transport_ptr)(intptr_t)fd);
401 if( psz_hostname != NULL )
403 gnutls_server_name_set( p_sys->session, GNUTLS_NAME_DNS, psz_hostname,
404 strlen( psz_hostname ) );
405 if (var_CreateGetBool (p_session, "tls-check-hostname"))
407 p_sys->psz_hostname = strdup( psz_hostname );
408 if( p_sys->psz_hostname == NULL )
410 p_session->pf_close( p_session );
416 return p_session->pf_handshake2( p_session );
420 * Terminates TLS session and releases session data.
421 * You still have to close the socket yourself.
424 gnutls_SessionClose( tls_session_t *p_session )
426 tls_session_sys_t *p_sys;
428 p_sys = (tls_session_sys_t *)(p_session->p_sys);
430 if( p_sys->b_handshaked == VLC_TRUE )
431 gnutls_bye( p_sys->session, GNUTLS_SHUT_WR );
432 gnutls_deinit( p_sys->session );
434 if( p_sys->psz_hostname != NULL )
435 free( p_sys->psz_hostname );
437 vlc_object_detach( p_session );
438 vlc_object_destroy( p_session );
444 typedef int (*tls_prio_func) (gnutls_session_t, const int *);
447 gnutls_SetPriority (vlc_object_t *restrict obj, const char *restrict name,
448 tls_prio_func func, gnutls_session_t session,
449 const int *restrict values)
451 int val = func (session, values);
454 msg_Err (obj, "cannot set %s priorities: %s", name,
455 gnutls_strerror (val));
463 gnutls_SessionPrioritize (vlc_object_t *obj, gnutls_session_t session)
465 /* Note that ordering matters (on the client side) */
466 static const int protos[] =
473 static const int comps[] =
479 static const int macs[] =
482 GNUTLS_MAC_RMD160, // RIPEMD
488 static const int ciphers[] =
490 GNUTLS_CIPHER_AES_256_CBC,
491 GNUTLS_CIPHER_AES_128_CBC,
492 GNUTLS_CIPHER_3DES_CBC,
493 GNUTLS_CIPHER_ARCFOUR_128,
494 //GNUTLS_CIPHER_DES_CBC,
495 //GNUTLS_CIPHER_ARCFOUR_40,
496 //GNUTLS_CIPHER_RC2_40_CBC,
497 //GNUTLS_CIPHER_NULL,
500 static const int kx[] =
505 //GNUTLS_KX_RSA_EXPORT,
506 //GNUTLS_KX_DHE_PSK, TODO
507 //GNUTLS_KX_PSK, TODO
508 //GNUTLS_KX_SRP_RSA, TODO
509 //GNUTLS_KX_SRP_DSS, TODO
510 //GNUTLS_KX_SRP, TODO
514 static const int cert_types[] =
517 //GNUTLS_CRT_OPENPGP, TODO
521 int val = gnutls_set_default_priority (session);
524 msg_Err (obj, "cannot set default TLS priorities: %s",
525 gnutls_strerror (val));
529 if (gnutls_SetPriority (obj, "protocols",
530 gnutls_protocol_set_priority, session, protos)
531 || gnutls_SetPriority (obj, "compression algorithms",
532 gnutls_compression_set_priority, session, comps)
533 || gnutls_SetPriority (obj, "MAC algorithms",
534 gnutls_mac_set_priority, session, macs)
535 || gnutls_SetPriority (obj, "ciphers",
536 gnutls_cipher_set_priority, session, ciphers)
537 || gnutls_SetPriority (obj, "key exchange algorithms",
538 gnutls_kx_set_priority, session, kx)
539 || gnutls_SetPriority (obj, "certificate types",
540 gnutls_certificate_type_set_priority, session,
549 gnutls_ClientDelete( tls_session_t *p_session )
551 /* On the client-side, credentials are re-allocated per session */
552 gnutls_certificate_credentials x509_cred =
553 ((tls_client_sys_t *)(p_session->p_sys))->x509_cred;
555 gnutls_SessionClose( p_session );
557 /* credentials must be free'd *after* gnutls_deinit() */
558 gnutls_certificate_free_credentials( x509_cred );
563 gnutls_Addx509File( vlc_object_t *p_this,
564 gnutls_certificate_credentials cred,
565 const char *psz_path, vlc_bool_t b_priv );
568 gnutls_Addx509Directory( vlc_object_t *p_this,
569 gnutls_certificate_credentials cred,
570 const char *psz_dirname,
575 if( *psz_dirname == '\0' )
578 dir = utf8_opendir( psz_dirname );
581 msg_Warn( p_this, "cannot open directory (%s): %s", psz_dirname,
588 struct stat st1, st2;
589 int fd = dirfd( dir );
592 * Gets stats for the directory path, checks that it is not a
593 * symbolic link (to avoid possibly infinite recursion), and verifies
594 * that the inode is still the same, to avoid TOCTOU race condition.
597 || fstat( fd, &st1 ) || utf8_lstat( psz_dirname, &st2 )
598 || S_ISLNK( st2.st_mode ) || ( st1.st_ino != st2.st_ino ) )
608 char *ent = utf8_readdir (dir);
612 if ((strcmp (ent, ".") == 0) || (strcmp (ent, "..") == 0))
615 char path[strlen (psz_dirname) + strlen (ent) + 2];
616 sprintf (path, "%s"DIR_SEP"%s", psz_dirname, ent);
619 gnutls_Addx509File( p_this, cred, path, b_priv );
628 gnutls_Addx509File( vlc_object_t *p_this,
629 gnutls_certificate_credentials cred,
630 const char *psz_path, vlc_bool_t b_priv )
634 if( utf8_stat( psz_path, &st ) == 0 )
636 if( S_ISREG( st.st_mode ) )
638 char *psz_localname = ToLocale( psz_path );
640 ? gnutls_certificate_set_x509_key_file( cred,
641 psz_localname, psz_localname, GNUTLS_X509_FMT_PEM )
642 : gnutls_certificate_set_x509_trust_file( cred,
643 psz_localname, GNUTLS_X509_FMT_PEM );
644 LocaleFree( psz_localname );
648 msg_Warn( p_this, "cannot add x509 credentials (%s): %s",
649 psz_path, gnutls_strerror( i ) );
654 msg_Dbg( p_this, "added x509 credentials (%s)",
659 else if( S_ISDIR( st.st_mode ) )
662 "looking recursively for x509 credentials in %s",
664 return gnutls_Addx509Directory( p_this, cred, psz_path, b_priv);
668 msg_Warn( p_this, "cannot add x509 credentials (%s): %s",
669 psz_path, strerror( errno ) );
675 * Initializes a client-side TLS session.
677 static tls_session_t *
678 gnutls_ClientCreate( tls_t *p_tls )
680 tls_session_t *p_session = NULL;
681 tls_client_sys_t *p_sys = NULL;
684 p_sys = (tls_client_sys_t *)malloc( sizeof(struct tls_client_sys_t) );
688 p_session = (struct tls_session_t *)vlc_object_create ( p_tls, sizeof(struct tls_session_t) );
689 if( p_session == NULL )
695 p_session->p_sys = p_sys;
696 p_session->sock.p_sys = p_session;
697 p_session->sock.pf_send = gnutls_Send;
698 p_session->sock.pf_recv = gnutls_Recv;
699 p_session->pf_handshake = gnutls_BeginHandshake;
700 p_session->pf_close = gnutls_ClientDelete;
702 p_sys->session.b_handshaked = VLC_FALSE;
703 p_sys->session.psz_hostname = NULL;
705 vlc_object_attach( p_session, p_tls );
707 const char *homedir = p_tls->p_libvlc->psz_homedir,
708 *datadir = config_GetDataDir ((vlc_object_t *)p_session);
709 size_t l1 = strlen (homedir), l2 = strlen (datadir);
710 char path[((l1 > l2) ? l1 : l2) + sizeof ("/"CONFIG_DIR"/ssl/private")];
711 // > sizeof ("/"CONFIG_DIR"/ssl/certs")
712 // > sizeof ("/ca-certificates.crt")
714 i_val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
717 msg_Err( p_tls, "cannot allocate X509 credentials: %s",
718 gnutls_strerror( i_val ) );
722 if (var_CreateGetBool (p_tls, "tls-check-cert"))
724 sprintf (path, "%s/"CONFIG_DIR"/ssl/certs", homedir);
725 gnutls_Addx509Directory ((vlc_object_t *)p_session,
726 p_sys->x509_cred, path, VLC_FALSE);
728 sprintf (path, "%s/ca-certificates.crt", datadir);
729 gnutls_Addx509File ((vlc_object_t *)p_session,
730 p_sys->x509_cred, path, VLC_FALSE);
731 p_session->pf_handshake2 = gnutls_HandshakeAndValidate;
734 p_session->pf_handshake2 = gnutls_ContinueHandshake;
736 sprintf (path, "%s/"CONFIG_DIR"/ssl/private", homedir);
737 gnutls_Addx509Directory ((vlc_object_t *)p_session, p_sys->x509_cred,
740 i_val = gnutls_init( &p_sys->session.session, GNUTLS_CLIENT );
743 msg_Err( p_tls, "cannot initialize TLS session: %s",
744 gnutls_strerror( i_val ) );
745 gnutls_certificate_free_credentials( p_sys->x509_cred );
749 if (gnutls_SessionPrioritize (VLC_OBJECT (p_session),
750 p_sys->session.session))
753 i_val = gnutls_credentials_set( p_sys->session.session,
754 GNUTLS_CRD_CERTIFICATE,
758 msg_Err( p_tls, "cannot set TLS session credentials: %s",
759 gnutls_strerror( i_val ) );
766 gnutls_deinit( p_sys->session.session );
767 gnutls_certificate_free_credentials( p_sys->x509_cred );
770 vlc_object_detach( p_session );
771 vlc_object_destroy( p_session );
779 * TLS session resumption callbacks (server-side)
781 static int cb_store( void *p_server, gnutls_datum key, gnutls_datum data )
783 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
785 if( ( p_sys->i_cache_size == 0 )
786 || ( key.size > MAX_SESSION_ID )
787 || ( data.size > MAX_SESSION_DATA ) )
790 vlc_mutex_lock( &p_sys->cache_lock );
792 memcpy( p_sys->p_store->id, key.data, key.size);
793 memcpy( p_sys->p_store->data, data.data, data.size );
794 p_sys->p_store->i_idlen = key.size;
795 p_sys->p_store->i_datalen = data.size;
798 if( ( p_sys->p_store - p_sys->p_cache ) == p_sys->i_cache_size )
799 p_sys->p_store = p_sys->p_cache;
801 vlc_mutex_unlock( &p_sys->cache_lock );
807 static const gnutls_datum err_datum = { NULL, 0 };
809 static gnutls_datum cb_fetch( void *p_server, gnutls_datum key )
811 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
812 saved_session_t *p_session, *p_end;
814 p_session = p_sys->p_cache;
815 p_end = p_session + p_sys->i_cache_size;
817 vlc_mutex_lock( &p_sys->cache_lock );
819 while( p_session < p_end )
821 if( ( p_session->i_idlen == key.size )
822 && !memcmp( p_session->id, key.data, key.size ) )
826 data.size = p_session->i_datalen;
828 data.data = gnutls_malloc( data.size );
829 if( data.data == NULL )
831 vlc_mutex_unlock( &p_sys->cache_lock );
835 memcpy( data.data, p_session->data, data.size );
836 vlc_mutex_unlock( &p_sys->cache_lock );
842 vlc_mutex_unlock( &p_sys->cache_lock );
848 static int cb_delete( void *p_server, gnutls_datum key )
850 tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys;
851 saved_session_t *p_session, *p_end;
853 p_session = p_sys->p_cache;
854 p_end = p_session + p_sys->i_cache_size;
856 vlc_mutex_lock( &p_sys->cache_lock );
858 while( p_session < p_end )
860 if( ( p_session->i_idlen == key.size )
861 && !memcmp( p_session->id, key.data, key.size ) )
863 p_session->i_datalen = p_session->i_idlen = 0;
864 vlc_mutex_unlock( &p_sys->cache_lock );
870 vlc_mutex_unlock( &p_sys->cache_lock );
877 * Initializes a server-side TLS session.
879 static tls_session_t *
880 gnutls_ServerSessionPrepare( tls_server_t *p_server )
882 tls_session_t *p_session;
883 tls_server_sys_t *p_server_sys;
884 gnutls_session session;
887 p_session = vlc_object_create( p_server, sizeof (struct tls_session_t) );
888 if( p_session == NULL )
891 p_session->p_sys = malloc( sizeof(struct tls_session_sys_t) );
892 if( p_session->p_sys == NULL )
894 vlc_object_destroy( p_session );
898 vlc_object_attach( p_session, p_server );
900 p_server_sys = (tls_server_sys_t *)p_server->p_sys;
901 p_session->sock.p_sys = p_session;
902 p_session->sock.pf_send = gnutls_Send;
903 p_session->sock.pf_recv = gnutls_Recv;
904 p_session->pf_handshake = gnutls_BeginHandshake;
905 p_session->pf_handshake2 = p_server_sys->pf_handshake2;
906 p_session->pf_close = gnutls_SessionClose;
908 ((tls_session_sys_t *)p_session->p_sys)->b_handshaked = VLC_FALSE;
909 ((tls_session_sys_t *)p_session->p_sys)->psz_hostname = NULL;
911 i_val = gnutls_init( &session, GNUTLS_SERVER );
914 msg_Err( p_server, "cannot initialize TLS session: %s",
915 gnutls_strerror( i_val ) );
919 ((tls_session_sys_t *)p_session->p_sys)->session = session;
921 if (gnutls_SessionPrioritize (VLC_OBJECT (p_session), session))
923 gnutls_deinit( session );
927 i_val = gnutls_credentials_set( session, GNUTLS_CRD_CERTIFICATE,
928 p_server_sys->x509_cred );
931 msg_Err( p_server, "cannot set TLS session credentials: %s",
932 gnutls_strerror( i_val ) );
933 gnutls_deinit( session );
937 if( p_session->pf_handshake2 == gnutls_HandshakeAndValidate )
938 gnutls_certificate_server_set_request( session, GNUTLS_CERT_REQUIRE );
940 i_val = config_GetInt (p_server, "gnutls-dh-bits");
941 gnutls_dh_set_prime_bits (session, i_val);
943 /* Session resumption support */
944 i_val = config_GetInt (p_server, "gnutls-cache-expiration");
945 gnutls_db_set_cache_expiration (session, i_val);
946 gnutls_db_set_retrieve_function( session, cb_fetch );
947 gnutls_db_set_remove_function( session, cb_delete );
948 gnutls_db_set_store_function( session, cb_store );
949 gnutls_db_set_ptr( session, p_server );
954 free( p_session->p_sys );
955 vlc_object_detach( p_session );
956 vlc_object_destroy( p_session );
962 * Releases data allocated with tls_ServerCreate().
965 gnutls_ServerDelete( tls_server_t *p_server )
967 tls_server_sys_t *p_sys;
968 p_sys = (tls_server_sys_t *)p_server->p_sys;
970 vlc_mutex_destroy( &p_sys->cache_lock );
971 free( p_sys->p_cache );
973 vlc_object_detach( p_server );
974 vlc_object_destroy( p_server );
976 /* all sessions depending on the server are now deinitialized */
977 gnutls_certificate_free_credentials( p_sys->x509_cred );
978 gnutls_dh_params_deinit( p_sys->dh_params );
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.
989 *****************************************************************************/
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_handshake2 = 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 * @return NULL on error.
1053 static tls_server_t *
1054 gnutls_ServerCreate( tls_t *p_tls, const char *psz_cert_path,
1055 const char *psz_key_path )
1057 tls_server_t *p_server;
1058 tls_server_sys_t *p_sys;
1059 char *psz_local_key, *psz_local_cert;
1062 msg_Dbg( p_tls, "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 (p_tls, "gnutls-cache-size");
1069 p_sys->p_cache = (struct saved_session_t *)calloc( p_sys->i_cache_size,
1070 sizeof( struct saved_session_t ) );
1071 if( p_sys->p_cache == NULL )
1076 p_sys->p_store = p_sys->p_cache;
1078 p_server = vlc_object_create( p_tls, sizeof(struct tls_server_t) );
1079 if( p_server == NULL )
1081 free( p_sys->p_cache );
1086 vlc_object_attach( p_server, p_tls );
1088 p_server->p_sys = p_sys;
1089 p_server->pf_delete = gnutls_ServerDelete;
1090 p_server->pf_add_CA = gnutls_ServerAddCA;
1091 p_server->pf_add_CRL = gnutls_ServerAddCRL;
1092 p_server->pf_session_prepare = gnutls_ServerSessionPrepare;
1094 /* No certificate validation by default */
1095 p_sys->pf_handshake2 = gnutls_ContinueHandshake;
1097 vlc_mutex_init( p_server, &p_sys->cache_lock );
1099 /* Sets server's credentials */
1100 val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred );
1103 msg_Err( p_server, "cannot allocate X509 credentials: %s",
1104 gnutls_strerror( val ) );
1108 psz_local_cert = ToLocale( psz_cert_path );
1109 psz_local_key = ToLocale( psz_key_path );
1110 val = gnutls_certificate_set_x509_key_file( p_sys->x509_cred,
1111 psz_local_cert, psz_local_key,
1112 GNUTLS_X509_FMT_PEM );
1113 LocaleFree( psz_cert_path );
1114 LocaleFree( psz_key_path );
1117 msg_Err( p_server, "cannot set certificate chain or private key: %s",
1118 gnutls_strerror( val ) );
1119 gnutls_certificate_free_credentials( p_sys->x509_cred );
1124 * - regenerate these regularly
1125 * - support other ciper suites
1127 val = gnutls_dh_params_init( &p_sys->dh_params );
1130 msg_Dbg( p_server, "computing Diffie Hellman ciphers parameters" );
1131 val = gnutls_dh_params_generate2( p_sys->dh_params,
1132 config_GetInt( p_tls, "gnutls-dh-bits" ) );
1136 msg_Err( p_server, "cannot initialize DH cipher suites: %s",
1137 gnutls_strerror( val ) );
1138 gnutls_certificate_free_credentials( p_sys->x509_cred );
1141 msg_Dbg( p_server, "ciphers parameters computed" );
1143 gnutls_certificate_set_dh_params( p_sys->x509_cred, p_sys->dh_params);
1148 vlc_mutex_destroy( &p_sys->cache_lock );
1149 vlc_object_detach( p_server );
1150 vlc_object_destroy( p_server );
1156 #ifdef LIBVLC_USE_PTHREAD
1157 GCRY_THREAD_OPTION_PTHREAD_IMPL;
1158 # define gcry_threads_vlc gcry_threads_pthread
1161 * gcrypt thread option VLC implementation
1164 # define NEED_THREAD_CONTEXT 1
1165 static vlc_object_t *__p_gcry_data;
1167 static int gcry_vlc_mutex_init( void **p_sys )
1170 vlc_mutex_t *p_lock = (vlc_mutex_t *)malloc( sizeof( vlc_mutex_t ) );
1175 i_val = vlc_mutex_init( __p_gcry_data, p_lock );
1183 static int gcry_vlc_mutex_destroy( void **p_sys )
1186 vlc_mutex_t *p_lock = (vlc_mutex_t *)*p_sys;
1188 i_val = vlc_mutex_destroy( p_lock );
1193 static int gcry_vlc_mutex_lock( void **p_sys )
1195 return vlc_mutex_lock( (vlc_mutex_t *)*p_sys );
1198 static int gcry_vlc_mutex_unlock( void **lock )
1200 return vlc_mutex_unlock( (vlc_mutex_t *)*lock );
1203 static struct gcry_thread_cbs gcry_threads_vlc =
1205 GCRY_THREAD_OPTION_USER,
1207 gcry_vlc_mutex_init,
1208 gcry_vlc_mutex_destroy,
1209 gcry_vlc_mutex_lock,
1210 gcry_vlc_mutex_unlock
1215 /*****************************************************************************
1216 * Module initialization
1217 *****************************************************************************/
1219 Open( vlc_object_t *p_this )
1221 tls_t *p_tls = (tls_t *)p_this;
1223 vlc_value_t lock, count;
1225 var_Create( p_this->p_libvlc_global, "gnutls_mutex", VLC_VAR_MUTEX );
1226 var_Get( p_this->p_libvlc_global, "gnutls_mutex", &lock );
1227 vlc_mutex_lock( lock.p_address );
1229 /* Initialize GnuTLS only once */
1230 var_Create( p_this->p_libvlc_global, "gnutls_count", VLC_VAR_INTEGER );
1231 var_Get( p_this->p_libvlc_global, "gnutls_count", &count);
1233 if( count.i_int == 0)
1235 #ifdef NEED_THREAD_CONTEXT
1236 __p_gcry_data = VLC_OBJECT( p_this->p_libvlc );
1239 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_vlc);
1240 if( gnutls_global_init( ) )
1242 msg_Warn( p_this, "cannot initialize GnuTLS" );
1243 vlc_mutex_unlock( lock.p_address );
1244 return VLC_EGENERIC;
1247 const char *psz_version = gnutls_check_version( "1.2.9" );
1248 if( psz_version == NULL )
1250 gnutls_global_deinit( );
1251 vlc_mutex_unlock( lock.p_address );
1252 msg_Err( p_this, "unsupported GnuTLS version" );
1253 return VLC_EGENERIC;
1255 msg_Dbg( p_this, "GnuTLS v%s initialized", psz_version );
1259 var_Set( p_this->p_libvlc_global, "gnutls_count", count);
1260 vlc_mutex_unlock( lock.p_address );
1262 p_tls->pf_server_create = gnutls_ServerCreate;
1263 p_tls->pf_client_create = gnutls_ClientCreate;
1268 /*****************************************************************************
1269 * Module deinitialization
1270 *****************************************************************************/
1272 Close( vlc_object_t *p_this )
1274 /*tls_t *p_tls = (tls_t *)p_this;
1275 tls_sys_t *p_sys = (tls_sys_t *)(p_this->p_sys);*/
1277 vlc_value_t lock, count;
1279 var_Create( p_this->p_libvlc_global, "gnutls_mutex", VLC_VAR_MUTEX );
1280 var_Get( p_this->p_libvlc_global, "gnutls_mutex", &lock );
1281 vlc_mutex_lock( lock.p_address );
1283 var_Create( p_this->p_libvlc_global, "gnutls_count", VLC_VAR_INTEGER );
1284 var_Get( p_this->p_libvlc_global, "gnutls_count", &count);
1286 var_Set( p_this->p_libvlc_global, "gnutls_count", count);
1288 if( count.i_int == 0 )
1290 gnutls_global_deinit( );
1291 msg_Dbg( p_this, "GnuTLS deinitialized" );
1294 vlc_mutex_unlock( lock.p_address );