📄 gnutls.c
字号:
/***************************************************************************** * tls.c ***************************************************************************** * Copyright (C) 2004-2005 VideoLAN * $Id: gnutls.c 10620 2005-04-09 14:54:44Z courmisch $ * * Authors: Remi 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA. *****************************************************************************//* * TODO: * - fix FIXMEs *//***************************************************************************** * Preamble *****************************************************************************/#include <stdlib.h>#include <errno.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 <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_( \ "Allows you to modify the Diffie-Hellman prime's number of bits " \ "(used for TLS or SSL-based server-side encryption)." )#define CACHE_EXPIRATION_TEXT N_("Expiration time for resumed TLS sessions")#define CACHE_EXPIRATION_LONGTEXT N_( \ "Defines the delay before resumed TLS sessions will be expired " \ "(in seconds)." )#define CACHE_SIZE_TEXT N_("Number of resumed TLS sessions")#define CACHE_SIZE_LONGTEXT N_( \ "Allows you to modify 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_( \ "Ensures that server certificate is valid " \ "(ie. signed by an approved Certificate Authority)." )#define CHECK_HOSTNAME_TEXT N_("Check TLS/SSL server hostname in certificate")#define CHECK_HOSTNAME_LONGTEXT N_( \ "Ensures that server hostname in certificate match 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_FALSE, NULL, CHECK_CERT_TEXT, CHECK_CERT_LONGTEXT, VLC_FALSE ); add_bool( "tls-check-hostname", VLC_FALSE, NULL, CHECK_HOSTNAME_TEXT, CHECK_HOSTNAME_LONGTEXT, VLC_FALSE ); add_integer( "dh-bits", DH_BITS, NULL, DH_BITS_TEXT, DH_BITS_LONGTEXT, VLC_TRUE ); add_integer( "tls-cache-expiration", CACHE_EXPIRATION, NULL, CACHE_EXPIRATION_TEXT, CACHE_EXPIRATION_LONGTEXT, VLC_TRUE ); add_integer( "tls-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) )/***************************************************************************** * tls_Send: ***************************************************************************** * 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 ); /* TODO: handle fatal error */ return val < 0 ? -1 : val;}/***************************************************************************** * tls_Recv: ***************************************************************************** * 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 ); /* TODO: handle fatal error */ return val < 0 ? -1 : 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 */ 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 ) { msg_Err( p_session, "TLS handshake failed : %s", gnutls_strerror( val ) ); p_session->pf_close( p_session ); return -1; } p_sys->b_handshaked = VLC_TRUE; return 0;}static intgnutls_HandshakeAndValidate( tls_session_t *p_session ){ int val; val = gnutls_ContinueHandshake( p_session ); if( val == 0 ) { int status; gnutls_x509_crt cert; const gnutls_datum *p_data; tls_session_sys_t *p_sys; p_sys = (tls_session_sys_t *)(p_session->p_sys); /* certificates chain verification */ val = gnutls_certificate_verify_peers2( p_sys->session, &status ); if( val ) { msg_Err( p_session, "TLS certificate verification failed : %s", gnutls_strerror( val ) ); p_session->pf_close( p_session ); return -1; } if( status ) { msg_Warn( p_session, "TLS session : access denied" ); if( status & GNUTLS_CERT_INVALID ) msg_Dbg( p_session, "certificate could not be verified" ); if( status & GNUTLS_CERT_REVOKED ) msg_Dbg( p_session, "certificate was revoked" ); if( status & GNUTLS_CERT_SIGNER_NOT_FOUND ) msg_Dbg( p_session, "certificate's signer was not found" ); if( status & GNUTLS_CERT_SIGNER_NOT_CA ) msg_Dbg( p_session, "certificate's signer is not a CA" ); p_session->pf_close( p_session ); return -1; } msg_Dbg( p_session, "TLS certificate verified" ); if( p_sys->psz_hostname == NULL ) return 0; /* certificate (host)name verification */ p_data = gnutls_certificate_get_peers( p_sys->session, &val ); if( p_data == NULL ) { msg_Err( p_session, "TLS peer certificate not available" ); p_session->pf_close( p_session ); return -1; } val = gnutls_x509_crt_init( &cert ); if( val ) { msg_Err( p_session, "x509 fatal error : %s", gnutls_strerror( val ) ); p_session->pf_close( p_session ); return -1; } val = gnutls_x509_crt_import( cert, p_data, GNUTLS_X509_FMT_DER ); if( val ) { msg_Err( p_session, "x509 certificate import error : %s", gnutls_strerror( val ) ); gnutls_x509_crt_deinit( cert ); p_session->pf_close( p_session ); return -1; } if( gnutls_x509_crt_check_hostname( cert, p_sys->psz_hostname ) == 0 ) { msg_Err( p_session, "x509 certificate does not match \"%s\"", p_sys->psz_hostname ); gnutls_x509_crt_deinit( cert ); p_session->pf_close( p_session ); return -1; } gnutls_x509_crt_deinit( cert ); msg_Dbg( p_session, "x509 hostname verified" ); return 0; } return val;}static intgnutls_BeginHandshake( tls_session_t *p_session, int fd, const char *psz_hostname ){ tls_session_sys_t *p_sys; p_sys = (tls_session_sys_t *)(p_session->p_sys); gnutls_transport_set_ptr (p_sys->session, (gnutls_transport_ptr)fd); if( psz_hostname != NULL ) { gnutls_server_name_set( p_sys->session, GNUTLS_NAME_DNS, psz_hostname, strlen( psz_hostname ) ); if( get_Bool( p_session, "tls-check-hostname" ) ) { p_sys->psz_hostname = strdup( psz_hostname ); if( p_sys->psz_hostname == NULL ) { p_session->pf_close( p_session ); return -1; } } } return p_session->pf_handshake2( p_session );}/***************************************************************************** * tls_SessionClose: ***************************************************************************** * Terminates TLS session and releases session data. *****************************************************************************/static voidgnutls_SessionClose( tls_session_t *p_session )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -