Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

tls.c

00001 /*****************************************************************************
00002  * tls.c
00003  *****************************************************************************
00004  * Copyright (C) 2004-2005 the VideoLAN team
00005  * $Id: tls.c 12172 2005-08-13 16:08:29Z courmisch $
00006  *
00007  * Authors: Remi Denis-Courmont <rem # videolan.org>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
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  * tls_ServerCreate:
00095  *****************************************************************************
00096  * Allocates a whole server's TLS credentials.
00097  * Returns NULL on error.
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  * tls_ServerDelete:
00129  *****************************************************************************
00130  * Releases data allocated with tls_ServerCreate.
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  * tls_ClientCreate:
00145  *****************************************************************************
00146  * Allocates a client's TLS credentials and shakes hands through the network.
00147  * Returns NULL on error. This is a blocking network operation.
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  * tls_ClientDelete:
00186  *****************************************************************************
00187  * Releases data allocated with tls_ClientCreate.
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 }

Generated on Tue Dec 20 10:15:00 2005 for vlc-0.8.4a by  doxygen 1.4.2