📄 gnutls.c
字号:
#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;static int cb_store( void *p_server, gnutls_datum key, gnutls_datum data ){ tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys; if( ( p_sys->i_cache_size == 0 ) || ( key.size > MAX_SESSION_ID ) || ( data.size > MAX_SESSION_DATA ) ) return -1; vlc_mutex_lock( &p_sys->cache_lock ); memcpy( p_sys->p_store->id, key.data, key.size); memcpy( p_sys->p_store->data, data.data, data.size ); p_sys->p_store->i_idlen = key.size; p_sys->p_store->i_datalen = data.size; p_sys->p_store++; if( ( p_sys->p_store - p_sys->p_cache ) == p_sys->i_cache_size ) p_sys->p_store = p_sys->p_cache; vlc_mutex_unlock( &p_sys->cache_lock ); return 0;}static gnutls_datum cb_fetch( void *p_server, gnutls_datum key ){ static const gnutls_datum_t err_datum = { NULL, 0 }; tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys; saved_session_t *p_session, *p_end; p_session = p_sys->p_cache; p_end = p_session + p_sys->i_cache_size; vlc_mutex_lock( &p_sys->cache_lock ); while( p_session < p_end ) { if( ( p_session->i_idlen == key.size ) && !memcmp( p_session->id, key.data, key.size ) ) { gnutls_datum_t data; data.size = p_session->i_datalen; data.data = gnutls_malloc( data.size ); if( data.data == NULL ) { vlc_mutex_unlock( &p_sys->cache_lock ); return err_datum; } memcpy( data.data, p_session->data, data.size ); vlc_mutex_unlock( &p_sys->cache_lock ); return data; } p_session++; } vlc_mutex_unlock( &p_sys->cache_lock ); return err_datum;}static int cb_delete( void *p_server, gnutls_datum key ){ tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys; saved_session_t *p_session, *p_end; p_session = p_sys->p_cache; p_end = p_session + p_sys->i_cache_size; vlc_mutex_lock( &p_sys->cache_lock ); while( p_session < p_end ) { if( ( p_session->i_idlen == key.size ) && !memcmp( p_session->id, key.data, key.size ) ) { p_session->i_datalen = p_session->i_idlen = 0; vlc_mutex_unlock( &p_sys->cache_lock ); return 0; } p_session++; } vlc_mutex_unlock( &p_sys->cache_lock ); return -1;}/** * Terminates TLS session and releases session data. * You still have to close the socket yourself. */static voidgnutls_SessionClose (tls_server_t *p_server, tls_session_t *p_session){ tls_session_sys_t *p_sys = p_session->p_sys; (void)p_server; if( p_sys->b_handshaked == true ) gnutls_bye( p_sys->session, GNUTLS_SHUT_WR ); gnutls_deinit( p_sys->session ); vlc_object_detach( p_session ); vlc_object_release( p_session ); free( p_sys );}/** * Initializes a server-side TLS session. */static tls_session_t *gnutls_ServerSessionPrepare( tls_server_t *p_server ){ tls_session_t *p_session; tls_server_sys_t *p_server_sys; gnutls_session_t session; int i_val; p_session = vlc_object_create( p_server, sizeof (struct tls_session_t) ); if( p_session == NULL ) return NULL; p_session->p_sys = malloc( sizeof(struct tls_session_sys_t) ); if( p_session->p_sys == NULL ) { vlc_object_release( p_session ); return NULL; } p_server_sys = p_server->p_sys; p_session->sock.p_sys = p_session; p_session->sock.pf_send = gnutls_Send; p_session->sock.pf_recv = gnutls_Recv; p_session->pf_set_fd = gnutls_SetFD; p_session->pf_handshake = p_server_sys->pf_handshake; p_session->p_sys->b_handshaked = false; p_session->p_sys->psz_hostname = NULL; i_val = gnutls_init( &session, GNUTLS_SERVER ); if( i_val != 0 ) { msg_Err( p_server, "cannot initialize TLS session: %s", gnutls_strerror( i_val ) ); goto error; } p_session->p_sys->session = session; if (gnutls_SessionPrioritize (VLC_OBJECT (p_session), session)) { gnutls_deinit( session ); goto error; } i_val = gnutls_credentials_set( session, GNUTLS_CRD_CERTIFICATE, p_server_sys->x509_cred ); if( i_val < 0 ) { msg_Err( p_server, "cannot set TLS session credentials: %s", gnutls_strerror( i_val ) ); gnutls_deinit( session ); goto error; } if (p_session->pf_handshake == gnutls_HandshakeAndValidate) gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUIRE); /* Session resumption support */ i_val = config_GetInt (p_server, "gnutls-cache-timeout"); gnutls_db_set_cache_expiration (session, i_val); gnutls_db_set_retrieve_function( session, cb_fetch ); gnutls_db_set_remove_function( session, cb_delete ); gnutls_db_set_store_function( session, cb_store ); gnutls_db_set_ptr( session, p_server ); return p_session;error: free( p_session->p_sys ); vlc_object_detach( p_session ); vlc_object_release( p_session ); return NULL;}/** * Adds one or more certificate authorities. * * @param psz_ca_path (Unicode) path to an x509 certificates list. * * @return -1 on error, 0 on success. */static intgnutls_ServerAddCA( tls_server_t *p_server, const char *psz_ca_path ){ tls_server_sys_t *p_sys; char *psz_local_path; int val; p_sys = (tls_server_sys_t *)(p_server->p_sys); psz_local_path = ToLocale( psz_ca_path ); val = gnutls_certificate_set_x509_trust_file( p_sys->x509_cred, psz_local_path, GNUTLS_X509_FMT_PEM ); LocaleFree( psz_local_path ); if( val < 0 ) { msg_Err( p_server, "cannot add trusted CA (%s): %s", psz_ca_path, gnutls_strerror( val ) ); return VLC_EGENERIC; } msg_Dbg( p_server, " %d trusted CA added (%s)", val, psz_ca_path ); /* enables peer's certificate verification */ p_sys->pf_handshake = gnutls_HandshakeAndValidate; return VLC_SUCCESS;}/** * Adds a certificates revocation list to be sent to TLS clients. * * @param psz_crl_path (Unicode) path of the CRL file. * * @return -1 on error, 0 on success. */static intgnutls_ServerAddCRL( tls_server_t *p_server, const char *psz_crl_path ){ int val; char *psz_local_path = ToLocale( psz_crl_path ); val = gnutls_certificate_set_x509_crl_file( ((tls_server_sys_t *) (p_server->p_sys))->x509_cred, psz_local_path, GNUTLS_X509_FMT_PEM ); LocaleFree( psz_crl_path ); if( val < 0 ) { msg_Err( p_server, "cannot add CRL (%s): %s", psz_crl_path, gnutls_strerror( val ) ); return VLC_EGENERIC; } msg_Dbg( p_server, "%d CRL added (%s)", val, psz_crl_path ); return VLC_SUCCESS;}/** * Allocates a whole server's TLS credentials. */static int OpenServer (vlc_object_t *obj){ tls_server_t *p_server = (tls_server_t *)obj; tls_server_sys_t *p_sys; int val; if (gnutls_Init (obj)) return VLC_EGENERIC; msg_Dbg (obj, "creating TLS server"); p_sys = (tls_server_sys_t *)malloc( sizeof(struct tls_server_sys_t) ); if( p_sys == NULL ) return VLC_ENOMEM; p_sys->i_cache_size = config_GetInt (obj, "gnutls-cache-size"); p_sys->p_cache = calloc (p_sys->i_cache_size, sizeof (struct saved_session_t)); if (p_sys->p_cache == NULL) { free (p_sys); return VLC_ENOMEM; } p_sys->p_store = p_sys->p_cache; p_server->p_sys = p_sys; p_server->pf_add_CA = gnutls_ServerAddCA; p_server->pf_add_CRL = gnutls_ServerAddCRL; p_server->pf_open = gnutls_ServerSessionPrepare; p_server->pf_close = gnutls_SessionClose; /* No certificate validation by default */ p_sys->pf_handshake = gnutls_ContinueHandshake; vlc_mutex_init( &p_sys->cache_lock ); /* Sets server's credentials */ val = gnutls_certificate_allocate_credentials( &p_sys->x509_cred ); if( val != 0 ) { msg_Err( p_server, "cannot allocate X509 credentials: %s", gnutls_strerror( val ) ); goto error; } char *psz_cert_path = var_GetNonEmptyString (obj, "tls-x509-cert"); char *psz_key_path = var_GetNonEmptyString (obj, "tls-x509-key"); const char *psz_local_cert = ToLocale (psz_cert_path); const char *psz_local_key = ToLocale (psz_key_path); val = gnutls_certificate_set_x509_key_file (p_sys->x509_cred, psz_local_cert, psz_local_key, GNUTLS_X509_FMT_PEM ); LocaleFree (psz_local_key); free (psz_key_path); LocaleFree (psz_local_cert); free (psz_cert_path); if( val < 0 ) { msg_Err( p_server, "cannot set certificate chain or private key: %s", gnutls_strerror( val ) ); gnutls_certificate_free_credentials( p_sys->x509_cred ); goto error; } /* FIXME: * - support other ciper suites */ val = gnutls_dh_params_init (&p_sys->dh_params); if (val >= 0) { const gnutls_datum_t data = { .data = (unsigned char *)dh_params, .size = sizeof (dh_params) - 1, }; val = gnutls_dh_params_import_pkcs3 (p_sys->dh_params, &data, GNUTLS_X509_FMT_PEM); if (val == 0) gnutls_certificate_set_dh_params (p_sys->x509_cred, p_sys->dh_params); } if (val < 0) { msg_Err (p_server, "cannot initialize DHE cipher suites: %s", gnutls_strerror (val)); } return VLC_SUCCESS;error: vlc_mutex_destroy (&p_sys->cache_lock); free (p_sys->p_cache); free (p_sys); return VLC_EGENERIC;}/** * Destroys a TLS server object. */static void CloseServer (vlc_object_t *p_server){ tls_server_sys_t *p_sys = ((tls_server_t *)p_server)->p_sys; vlc_mutex_destroy (&p_sys->cache_lock); free (p_sys->p_cache); /* all sessions depending on the server are now deinitialized */ gnutls_certificate_free_credentials (p_sys->x509_cred); gnutls_dh_params_deinit (p_sys->dh_params); free (p_sys); gnutls_Deinit (p_server);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -