00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <stdlib.h>
00025 #include <vlc/vlc.h>
00026
00027 #include "vlc_tls.h"
00028
00029 static tls_t *
00030 tls_Init( vlc_object_t *p_this )
00031 {
00032 tls_t *p_tls;
00033 vlc_value_t lockval;
00034
00035 var_Create( p_this->p_libvlc, "tls_mutex", VLC_VAR_MUTEX );
00036 var_Get( p_this->p_libvlc, "tls_mutex", &lockval );
00037 vlc_mutex_lock( lockval.p_address );
00038
00039 p_tls = vlc_object_find( p_this, VLC_OBJECT_TLS, FIND_ANYWHERE );
00040
00041 if( p_tls == NULL )
00042 {
00043 p_tls = vlc_object_create( p_this, VLC_OBJECT_TLS );
00044 if( p_tls == NULL )
00045 {
00046 vlc_mutex_unlock( lockval.p_address );
00047 return NULL;
00048 }
00049
00050 p_tls->p_module = module_Need( p_tls, "tls", 0, 0 );
00051 if( p_tls->p_module == NULL )
00052 {
00053 msg_Err( p_tls, "TLS/SSL provider not found" );
00054 vlc_mutex_unlock( lockval.p_address );
00055 vlc_object_destroy( p_tls );
00056 return NULL;
00057 }
00058
00059 vlc_object_attach( p_tls, p_this->p_vlc );
00060 vlc_object_yield( p_tls );
00061 msg_Dbg( p_tls, "TLS/SSL provider initialized" );
00062 }
00063 vlc_mutex_unlock( lockval.p_address );
00064
00065 return p_tls;
00066 }
00067
00068 static void
00069 tls_Deinit( tls_t *p_tls )
00070 {
00071 int i;
00072 vlc_value_t lockval;
00073
00074 var_Get( p_tls->p_libvlc, "tls_mutex", &lockval );
00075 vlc_mutex_lock( lockval.p_address );
00076
00077 vlc_object_release( p_tls );
00078
00079 i = p_tls->i_refcount;
00080 if( i == 0 )
00081 vlc_object_detach( p_tls );
00082
00083 vlc_mutex_unlock( lockval.p_address );
00084
00085 if( i == 0 )
00086 {
00087 module_Unneed( p_tls, p_tls->p_module );
00088 msg_Dbg( p_tls, "TLS/SSL provider deinitialized" );
00089 vlc_object_destroy( p_tls );
00090 }
00091 }
00092
00093
00094
00095
00096
00097
00098
00099 tls_server_t *
00100 tls_ServerCreate( vlc_object_t *p_this, const char *psz_cert,
00101 const char *psz_key )
00102 {
00103 tls_t *p_tls;
00104 tls_server_t *p_server;
00105
00106 p_tls = tls_Init( p_this );
00107 if( p_tls == NULL )
00108 return NULL;
00109
00110 if( psz_key == NULL )
00111 psz_key = psz_cert;
00112
00113 p_server = p_tls->pf_server_create( p_tls, psz_cert, psz_key );
00114 if( p_server != NULL )
00115 {
00116 msg_Dbg( p_tls, "TLS/SSL server initialized" );
00117 return p_server;
00118 }
00119 else
00120 msg_Err( p_tls, "TLS/SSL server error" );
00121
00122 tls_Deinit( p_tls );
00123 return NULL;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132 void
00133 tls_ServerDelete( tls_server_t *p_server )
00134 {
00135 tls_t *p_tls = (tls_t *)p_server->p_parent;
00136
00137 p_server->pf_delete( p_server );
00138
00139 tls_Deinit( p_tls );
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149 tls_session_t *
00150 tls_ClientCreate( vlc_object_t *p_this, int fd, const char *psz_hostname )
00151 {
00152 tls_t *p_tls;
00153 tls_session_t *p_session;
00154
00155 p_tls = tls_Init( p_this );
00156 if( p_tls == NULL )
00157 return NULL;
00158
00159 p_session = p_tls->pf_client_create( p_tls );
00160 if( p_session != NULL )
00161 {
00162 int i_val;
00163
00164 for( i_val = tls_ClientSessionHandshake( p_session, fd,
00165 psz_hostname );
00166 i_val > 0;
00167 i_val = tls_SessionContinueHandshake( p_session ) );
00168
00169 if( i_val == 0 )
00170 {
00171 msg_Dbg( p_this, "TLS/SSL client initialized" );
00172 return p_session;
00173 }
00174 msg_Err( p_this, "TLS/SSL session handshake error" );
00175 }
00176 else
00177 msg_Err( p_this, "TLS/SSL client error" );
00178
00179 tls_Deinit( p_tls );
00180 return NULL;
00181 }
00182
00183
00184
00185
00186
00187
00188
00189 void
00190 tls_ClientDelete( tls_session_t *p_session )
00191 {
00192 tls_t *p_tls = (tls_t *)p_session->p_parent;
00193
00194 p_session->pf_close( p_session );
00195
00196 tls_Deinit( p_tls );
00197 }