📄 gnutls.c
字号:
/***************************************************************************** * gnutls.c ***************************************************************************** * Copyright (C) 2004-2006 Rémi Denis-Courmont * $Id: gnutls.c 19803 2007-04-15 06:46:21Z courmisch $ * * 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 *****************************************************************************/#include <stdlib.h>#include <errno.h>#include <time.h>#include <vlc/vlc.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># ifdef HAVE_UNISTD_H# include <unistd.h># endif#endif#include "vlc_tls.h"#include "charset.h"#include <gcrypt.h>#include <gnutls/gnutls.h>#include <gnutls/x509.h>#define DH_BITS 1024#define CACHE_EXPIRATION 3600#define CACHE_SIZE 64/***************************************************************************** * Module descriptor *****************************************************************************/static int Open ( vlc_object_t * );static void Close( vlc_object_t * );#define DH_BITS_TEXT N_("Diffie-Hellman prime bits")#define DH_BITS_LONGTEXT N_( \ "This allows you to modify the Diffie-Hellman prime's number of bits, " \ "used for TLS or SSL-based server-side encryption. This is generally " \ "not needed." )#define CACHE_EXPIRATION_TEXT N_("Expiration time for resumed TLS sessions")#define CACHE_EXPIRATION_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." )#define CHECK_CERT_TEXT N_("Check TLS/SSL server certificate validity")#define CHECK_CERT_LONGTEXT N_( \ "This ensures that the server certificate is valid " \ "(i.e. signed by an approved Certification Authority)." )#define CHECK_HOSTNAME_TEXT N_("Check TLS/SSL server hostname in certificate")#define CHECK_HOSTNAME_LONGTEXT N_( \ "This ensures that the server hostname in certificate matches the " \ "requested host name." )vlc_module_begin(); set_shortname( "GnuTLS" ); set_description( _("GnuTLS TLS encryption layer") ); set_capability( "tls", 1 ); set_callbacks( Open, Close ); set_category( CAT_ADVANCED ); set_subcategory( SUBCAT_ADVANCED_MISC ); add_bool( "tls-check-cert", VLC_TRUE, NULL, CHECK_CERT_TEXT, CHECK_CERT_LONGTEXT, VLC_FALSE ); add_bool( "tls-check-hostname", VLC_TRUE, NULL, CHECK_HOSTNAME_TEXT, CHECK_HOSTNAME_LONGTEXT, VLC_FALSE ); add_integer( "gnutls-dh-bits", DH_BITS, NULL, DH_BITS_TEXT, DH_BITS_LONGTEXT, VLC_TRUE ); add_integer( "gnutls-cache-expiration", CACHE_EXPIRATION, NULL, CACHE_EXPIRATION_TEXT, CACHE_EXPIRATION_LONGTEXT, VLC_TRUE ); add_integer( "gnutls-cache-size", CACHE_SIZE, NULL, CACHE_SIZE_TEXT, CACHE_SIZE_LONGTEXT, VLC_TRUE );vlc_module_end();#define MAX_SESSION_ID 32#define MAX_SESSION_DATA 1024typedef struct saved_session_t{ char id[MAX_SESSION_ID]; char data[MAX_SESSION_DATA]; unsigned i_idlen; unsigned i_datalen;} saved_session_t;typedef struct tls_server_sys_t{ gnutls_certificate_credentials x509_cred; gnutls_dh_params dh_params; struct saved_session_t *p_cache; struct saved_session_t *p_store; int i_cache_size; vlc_mutex_t cache_lock; int (*pf_handshake2)( tls_session_t * );} tls_server_sys_t;typedef struct tls_session_sys_t{ gnutls_session session; char *psz_hostname; vlc_bool_t b_handshaked;} tls_session_sys_t;typedef struct tls_client_sys_t{ struct tls_session_sys_t session; gnutls_certificate_credentials x509_cred;} tls_client_sys_t;static int_get_Int( vlc_object_t *p_this, const char *var ){ vlc_value_t value; if( var_Get( p_this, var, &value ) != VLC_SUCCESS ) { var_Create( p_this, var, VLC_VAR_INTEGER | VLC_VAR_DOINHERIT ); var_Get( p_this, var, &value ); } return value.i_int;}static int_get_Bool( vlc_object_t *p_this, const char *var ){ vlc_value_t value; if( var_Get( p_this, var, &value ) != VLC_SUCCESS ) { var_Create( p_this, var, VLC_VAR_BOOL | VLC_VAR_DOINHERIT ); var_Get( p_this, var, &value ); } return value.b_bool;}#define get_Int( a, b ) _get_Int( (vlc_object_t *)(a), (b) )#define get_Bool( a, b ) _get_Bool( (vlc_object_t *)(a), (b) )static int gnutls_Error (vlc_object_t *obj, int val){ switch (val) { case GNUTLS_E_AGAIN:#if defined(WIN32) WSASetLastError(WSAEWOULDBLOCK);#else errno = EAGAIN;#endif break; case GNUTLS_E_INTERRUPTED:#if defined(WIN32) WSASetLastError(WSAEINTR);#else errno = EINTR;#endif break; default: msg_Err (obj, "%s", gnutls_strerror (val));#ifdef DEBUG if (!gnutls_error_is_fatal (val)) msg_Err (obj, "Error above should be handled");#endif#if defined(WIN32) WSASetLastError(WSAECONNRESET);#else errno = ECONNRESET;#endif } return -1;}/** * 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;}/***************************************************************************** * tls_Session(Continue)?Handshake: ***************************************************************************** * Establishes TLS session with a peer through socket <fd>. * Returns -1 on error (you need not and must not call tls_SessionClose) * 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; int val; p_sys = (tls_session_sys_t *)(p_session->p_sys); /* TODO: handle fatal error */#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 ) ); p_session->pf_close( p_session ); return -1; } p_sys->b_handshaked = VLC_TRUE; return 0;}static intgnutls_VerifyHostname( vlc_object_t *p_this, gnutls_session session, const char *psz_hostname ){ const gnutls_datum *p_data; gnutls_x509_crt cert; unsigned status; int val; /* certificate (host)name verification */ p_data = gnutls_certificate_get_peers( session, &status ); if( p_data == NULL ) { msg_Err( p_this, "TLS peer certificate not available" ); return -1; } val = gnutls_x509_crt_init( &cert ); if( val ) { msg_Err( p_this, "x509 fatal error: %s", gnutls_strerror( val ) ); return -1; } val = gnutls_x509_crt_import( cert, p_data, GNUTLS_X509_FMT_DER ); if( val ) { msg_Err( p_this, "x509 certificate import error: %s", gnutls_strerror( val ) ); gnutls_x509_crt_deinit( cert ); return -1; } if( gnutls_x509_crt_check_hostname( cert, psz_hostname ) == 0 ) { msg_Err( p_this, "x509 certificate does not match \"%s\"", psz_hostname ); gnutls_x509_crt_deinit( cert ); return -1; } gnutls_x509_crt_deinit( cert ); msg_Dbg( p_this, "x509 hostname matches %s", psz_hostname ); 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 ) ); goto error; } if( status ) { const error_msg_t *e; msg_Err( session, "TLS session: access denied" ); for( e = cert_errors; e->flag; e++ ) { if( status & e->flag ) { msg_Err( session, e->msg ); status &= ~e->flag; } } if( status ) msg_Err( session, "unknown certificate error (you found a bug in VLC)" ); goto error; } /* certificate (host)name verification */ const gnutls_datum *data = gnutls_certificate_get_peers( p_sys->session, &(size_t){ 0 } ); if( data == NULL ) { msg_Err( session, "Peer certificate not available" ); goto error; } gnutls_x509_crt cert; val = gnutls_x509_crt_init( &cert ); if( val ) { msg_Err( session, "x509 fatal error: %s", gnutls_strerror( val ) ); goto error; } val = gnutls_x509_crt_import( cert, data, GNUTLS_X509_FMT_DER ); if( val ) { msg_Err( session, "Certificate import error: %s", gnutls_strerror( val ) ); goto crt_error; } if( 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 crt_error; } } else msg_Warn( session, "Certificate and hostname were not verified" ); if( gnutls_x509_crt_get_expiration_time( cert ) < time( NULL ) )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -