1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2004-2005 the VideoLAN team
7 * Authors: Remi Denis-Courmont <rem # videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
30 tls_Init( vlc_object_t *p_this )
35 var_Create( p_this->p_libvlc, "tls_mutex", VLC_VAR_MUTEX );
36 var_Get( p_this->p_libvlc, "tls_mutex", &lockval );
37 vlc_mutex_lock( lockval.p_address );
39 p_tls = vlc_object_find( p_this, VLC_OBJECT_TLS, FIND_ANYWHERE );
43 p_tls = vlc_object_create( p_this, VLC_OBJECT_TLS );
46 vlc_mutex_unlock( lockval.p_address );
50 p_tls->p_module = module_Need( p_tls, "tls", 0, 0 );
51 if( p_tls->p_module == NULL )
53 msg_Err( p_tls, "TLS/SSL provider not found" );
54 vlc_mutex_unlock( lockval.p_address );
55 vlc_object_destroy( p_tls );
59 vlc_object_attach( p_tls, p_this->p_vlc );
60 vlc_object_yield( p_tls );
61 msg_Dbg( p_tls, "TLS/SSL provider initialized" );
63 vlc_mutex_unlock( lockval.p_address );
69 tls_Deinit( tls_t *p_tls )
74 var_Get( p_tls->p_libvlc, "tls_mutex", &lockval );
75 vlc_mutex_lock( lockval.p_address );
77 vlc_object_release( p_tls );
79 i = p_tls->i_refcount;
81 vlc_object_detach( p_tls );
83 vlc_mutex_unlock( lockval.p_address );
87 module_Unneed( p_tls, p_tls->p_module );
88 msg_Dbg( p_tls, "TLS/SSL provider deinitialized" );
89 vlc_object_destroy( p_tls );
93 /*****************************************************************************
95 *****************************************************************************
96 * Allocates a whole server's TLS credentials.
97 * Returns NULL on error.
98 *****************************************************************************/
100 tls_ServerCreate( vlc_object_t *p_this, const char *psz_cert,
101 const char *psz_key )
104 tls_server_t *p_server;
106 p_tls = tls_Init( p_this );
110 if( psz_key == NULL )
113 p_server = p_tls->pf_server_create( p_tls, psz_cert, psz_key );
114 if( p_server != NULL )
116 msg_Dbg( p_tls, "TLS/SSL server initialized" );
120 msg_Err( p_tls, "TLS/SSL server error" );
127 /*****************************************************************************
129 *****************************************************************************
130 * Releases data allocated with tls_ServerCreate.
131 *****************************************************************************/
133 tls_ServerDelete( tls_server_t *p_server )
135 tls_t *p_tls = (tls_t *)p_server->p_parent;
137 p_server->pf_delete( p_server );
143 /*****************************************************************************
145 *****************************************************************************
146 * Allocates a client's TLS credentials and shakes hands through the network.
147 * Returns NULL on error. This is a blocking network operation.
148 *****************************************************************************/
150 tls_ClientCreate( vlc_object_t *p_this, int fd, const char *psz_hostname )
153 tls_session_t *p_session;
155 p_tls = tls_Init( p_this );
159 p_session = p_tls->pf_client_create( p_tls );
160 if( p_session != NULL )
164 for( i_val = tls_ClientSessionHandshake( p_session, fd,
167 i_val = tls_SessionContinueHandshake( p_session ) );
171 msg_Dbg( p_this, "TLS/SSL client initialized" );
174 msg_Err( p_this, "TLS/SSL session handshake error" );
177 msg_Err( p_this, "TLS/SSL client error" );
184 /*****************************************************************************
186 *****************************************************************************
187 * Releases data allocated with tls_ClientCreate.
188 *****************************************************************************/
190 tls_ClientDelete( tls_session_t *p_session )
192 tls_t *p_tls = (tls_t *)p_session->p_parent;
194 p_session->pf_close( p_session );