📄 gnutls.c
字号:
/***************************************************************************** * gnutls.c ***************************************************************************** * Copyright (C) 2004-2006 Rémi Denis-Courmont * $Id$ * * Authors: Rémi Denis-Courmont <rem # videolan.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************//***************************************************************************** * Preamble *****************************************************************************/#ifdef HAVE_CONFIG_H# include "config.h"#endif#include <vlc_common.h>#include <vlc_plugin.h>#include <errno.h>#include <time.h>#include <sys/types.h>#include <errno.h>#ifdef HAVE_DIRENT_H# include <dirent.h>#endif#ifdef HAVE_SYS_STAT_H# include <sys/stat.h>#endif#ifdef WIN32# include <io.h>#else# include <unistd.h>#endif# include <fcntl.h>#include <vlc_tls.h>#include <vlc_charset.h>#include <vlc_block.h>#include <gcrypt.h>#include <gnutls/gnutls.h>#include <gnutls/x509.h>#include <vlc_gcrypt.h>#define CACHE_TIMEOUT 3600#define CACHE_SIZE 64#include "dhparams.h"#include <assert.h>/***************************************************************************** * Module descriptor *****************************************************************************/static int OpenClient (vlc_object_t *);static void CloseClient (vlc_object_t *);static int OpenServer (vlc_object_t *);static void CloseServer (vlc_object_t *);#define CACHE_TIMEOUT_TEXT N_("Expiration time for resumed TLS sessions")#define CACHE_TIMEOUT_LONGTEXT N_( \ "It is possible to cache the resumed TLS sessions. This is the expiration "\ "time of the sessions stored in this cache, in seconds." )#define CACHE_SIZE_TEXT N_("Number of resumed TLS sessions")#define CACHE_SIZE_LONGTEXT N_( \ "This is the maximum number of resumed TLS sessions that " \ "the cache will hold." )vlc_module_begin(); set_shortname( "GnuTLS" ); set_description( N_("GnuTLS transport layer security") ); set_capability( "tls client", 1 ); set_callbacks( OpenClient, CloseClient ); set_category( CAT_ADVANCED ); set_subcategory( SUBCAT_ADVANCED_MISC ); add_obsolete_bool( "tls-check-cert" ); add_obsolete_bool( "tls-check-hostname" ); add_submodule(); set_description( N_("GnuTLS server") ); set_capability( "tls server", 1 ); set_category( CAT_ADVANCED ); set_subcategory( SUBCAT_ADVANCED_MISC ); set_callbacks( OpenServer, CloseServer ); add_obsolete_integer( "gnutls-dh-bits" ); add_integer( "gnutls-cache-timeout", CACHE_TIMEOUT, NULL, CACHE_TIMEOUT_TEXT, CACHE_TIMEOUT_LONGTEXT, true ); add_integer( "gnutls-cache-size", CACHE_SIZE, NULL, CACHE_SIZE_TEXT, CACHE_SIZE_LONGTEXT, true );vlc_module_end();/** * Initializes GnuTLS with proper locking. * @return VLC_SUCCESS on success, a VLC error code otherwise. */static int gnutls_Init (vlc_object_t *p_this){ int ret = VLC_EGENERIC; vlc_gcrypt_init (); /* GnuTLS depends on gcrypt */ vlc_mutex_t *lock = var_AcquireMutex ("gnutls_mutex"); if (gnutls_global_init ()) { msg_Err (p_this, "cannot initialize GnuTLS"); goto error; } const char *psz_version = gnutls_check_version ("1.3.3"); if (psz_version == NULL) { msg_Err (p_this, "unsupported GnuTLS version"); gnutls_global_deinit (); goto error; } msg_Dbg (p_this, "GnuTLS v%s initialized", psz_version); ret = VLC_SUCCESS;error: vlc_mutex_unlock (lock); return ret;}/** * Deinitializes GnuTLS. */static void gnutls_Deinit (vlc_object_t *p_this){ vlc_mutex_t *lock = var_AcquireMutex( "gnutls_mutex" ); gnutls_global_deinit (); msg_Dbg (p_this, "GnuTLS deinitialized"); vlc_mutex_unlock (lock);}static int gnutls_Error (vlc_object_t *obj, int val){ switch (val) { case GNUTLS_E_AGAIN:#ifndef WIN32 errno = EAGAIN; break;#endif /* WinSock does not return EAGAIN, return EINTR instead */ case GNUTLS_E_INTERRUPTED:#ifdef WIN32 WSASetLastError (WSAEINTR);#else errno = EINTR;#endif break; default: msg_Err (obj, "%s", gnutls_strerror (val));#ifndef NDEBUG if (!gnutls_error_is_fatal (val)) msg_Err (obj, "Error above should be handled");#endif#ifdef WIN32 WSASetLastError (WSAECONNRESET);#else errno = ECONNRESET;#endif } return -1;}struct tls_session_sys_t{ gnutls_session_t session; char *psz_hostname; bool b_handshaked;};/** * Sends data through a TLS session. */static intgnutls_Send( void *p_session, const void *buf, int i_length ){ int val; tls_session_sys_t *p_sys; p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys); val = gnutls_record_send( p_sys->session, buf, i_length ); return (val < 0) ? gnutls_Error ((vlc_object_t *)p_session, val) : val;}/** * Receives data through a TLS session. */static intgnutls_Recv( void *p_session, void *buf, int i_length ){ int val; tls_session_sys_t *p_sys; p_sys = (tls_session_sys_t *)(((tls_session_t *)p_session)->p_sys); val = gnutls_record_recv( p_sys->session, buf, i_length ); return (val < 0) ? gnutls_Error ((vlc_object_t *)p_session, val) : val;}/** * Starts or continues the TLS handshake. * * @return -1 on fatal error, 0 on succesful handshake completion, * 1 if more would-be blocking recv is needed, * 2 if more would-be blocking send is required. */static intgnutls_ContinueHandshake (tls_session_t *p_session){ tls_session_sys_t *p_sys = p_session->p_sys; int val;#ifdef WIN32 WSASetLastError( 0 );#endif val = gnutls_handshake( p_sys->session ); if( ( val == GNUTLS_E_AGAIN ) || ( val == GNUTLS_E_INTERRUPTED ) ) return 1 + gnutls_record_get_direction( p_sys->session ); if( val < 0 ) {#ifdef WIN32 msg_Dbg( p_session, "Winsock error %d", WSAGetLastError( ) );#endif msg_Err( p_session, "TLS handshake error: %s", gnutls_strerror( val ) ); return -1; } p_sys->b_handshaked = true; return 0;}typedef struct{ int flag; const char *msg;} error_msg_t;static const error_msg_t cert_errors[] ={ { GNUTLS_CERT_INVALID, "Certificate could not be verified" }, { GNUTLS_CERT_REVOKED, "Certificate was revoked" }, { GNUTLS_CERT_SIGNER_NOT_FOUND, "Certificate's signer was not found" }, { GNUTLS_CERT_SIGNER_NOT_CA, "Certificate's signer is not a CA" }, { GNUTLS_CERT_INSECURE_ALGORITHM, "Insecure certificate signature algorithm" }, { 0, NULL }};static intgnutls_HandshakeAndValidate( tls_session_t *session ){ int val = gnutls_ContinueHandshake( session ); if( val ) return val; tls_session_sys_t *p_sys = (tls_session_sys_t *)(session->p_sys); /* certificates chain verification */ unsigned status; val = gnutls_certificate_verify_peers2( p_sys->session, &status ); if( val ) { msg_Err( session, "Certificate verification failed: %s", gnutls_strerror( val ) ); return -1; } if( status ) { msg_Err( session, "TLS session: access denied" ); for( const error_msg_t *e = cert_errors; e->flag; e++ ) { if( status & e->flag ) { msg_Err( session, "%s", e->msg ); status &= ~e->flag; } } if( status ) msg_Err( session, "unknown certificate error (you found a bug in VLC)" ); return -1; } /* certificate (host)name verification */ const gnutls_datum_t *data; data = gnutls_certificate_get_peers (p_sys->session, &(unsigned){0}); if( data == NULL ) { msg_Err( session, "Peer certificate not available" ); return -1; } gnutls_x509_crt_t cert; val = gnutls_x509_crt_init( &cert ); if( val ) { msg_Err( session, "x509 fatal error: %s", gnutls_strerror( val ) ); return -1; } val = gnutls_x509_crt_import( cert, data, GNUTLS_X509_FMT_DER ); if( val ) { msg_Err( session, "Certificate import error: %s", gnutls_strerror( val ) ); goto error; } assert( p_sys->psz_hostname != NULL ); if ( !gnutls_x509_crt_check_hostname( cert, p_sys->psz_hostname ) ) { msg_Err( session, "Certificate does not match \"%s\"", p_sys->psz_hostname ); goto error; } if( gnutls_x509_crt_get_expiration_time( cert ) < time( NULL ) ) { msg_Err( session, "Certificate expired" ); goto error; } if( gnutls_x509_crt_get_activation_time( cert ) > time ( NULL ) ) { msg_Err( session, "Certificate not yet valid" ); goto error; } gnutls_x509_crt_deinit( cert ); msg_Dbg( session, "TLS/x509 certificate verified" ); return 0;error: gnutls_x509_crt_deinit( cert ); return -1;}/** * Sets the operating system file descriptor backend for the TLS sesison. * * @param fd stream socket already connected with the peer. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -