1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright © 2004-2007 Rémi Denis-Courmont
7 * Authors: Rémi 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
26 * libvlc interface to the Transport Layer Security (TLS) plugins.
34 tls_Init( vlc_object_t *p_this )
39 var_Create( p_this->p_libvlc, "tls_mutex", VLC_VAR_MUTEX );
40 var_Get( p_this->p_libvlc, "tls_mutex", &lockval );
41 vlc_mutex_lock( lockval.p_address );
43 p_tls = vlc_object_find( p_this, VLC_OBJECT_TLS, FIND_ANYWHERE );
47 p_tls = vlc_object_create( p_this, VLC_OBJECT_TLS );
50 vlc_mutex_unlock( lockval.p_address );
54 p_tls->p_module = module_Need( p_tls, "tls", 0, 0 );
55 if( p_tls->p_module == NULL )
57 msg_Err( p_tls, "TLS/SSL provider not found" );
58 vlc_mutex_unlock( lockval.p_address );
59 vlc_object_destroy( p_tls );
63 vlc_object_attach( p_tls, p_this->p_libvlc );
64 vlc_object_yield( p_tls );
65 msg_Dbg( p_tls, "TLS/SSL provider initialized" );
67 vlc_mutex_unlock( lockval.p_address );
73 tls_Deinit( tls_t *p_tls )
78 var_Get( p_tls->p_libvlc, "tls_mutex", &lockval );
79 vlc_mutex_lock( lockval.p_address );
81 vlc_object_release( p_tls );
83 i = p_tls->i_refcount;
85 vlc_object_detach( p_tls );
87 vlc_mutex_unlock( lockval.p_address );
91 module_Unneed( p_tls, p_tls->p_module );
92 msg_Dbg( p_tls, "TLS/SSL provider deinitialized" );
93 vlc_object_destroy( p_tls );
98 * Allocates a whole server's TLS credentials.
100 * @param psz_cert required (Unicode) path to an x509 certificate.
101 * @param psz_key required (Unicode) path to the PKCS private key for
104 * @return NULL on error.
107 tls_ServerCreate( vlc_object_t *p_this, const char *psz_cert,
108 const char *psz_key )
111 tls_server_t *p_server;
113 p_tls = tls_Init( p_this );
117 if( psz_key == NULL )
120 p_server = p_tls->pf_server_create( p_tls, psz_cert, psz_key );
121 if( p_server != NULL )
123 msg_Dbg( p_tls, "TLS/SSL server initialized" );
127 msg_Err( p_tls, "TLS/SSL server error" );
135 * Releases data allocated with tls_ServerCreate.
138 tls_ServerDelete( tls_server_t *p_server )
140 tls_t *p_tls = (tls_t *)p_server->p_parent;
142 p_server->pf_delete( p_server );
149 * Allocates a client's TLS credentials and shakes hands through the network.
150 * This is a blocking network operation.
152 * @param fd stream socket through which to establish the secure communication
154 * @param psz_hostname Server Name Indication to pass to the server, or NULL.
156 * @return NULL on error.
159 tls_ClientCreate( vlc_object_t *p_this, int fd, const char *psz_hostname )
162 tls_session_t *p_session;
164 p_tls = tls_Init( p_this );
168 p_session = p_tls->pf_client_create( p_tls );
169 if( p_session != NULL )
173 for( i_val = tls_ClientSessionHandshake( p_session, fd,
176 i_val = tls_SessionContinueHandshake( p_session ) );
180 msg_Dbg( p_this, "TLS/SSL client initialized" );
183 msg_Err( p_this, "TLS/SSL session handshake error" );
186 msg_Err( p_this, "TLS/SSL client error" );
194 * Releases data allocated with tls_ClientCreate.
195 * It is your job to close the underlying socket.
198 tls_ClientDelete( tls_session_t *p_session )
200 tls_t *p_tls = (tls_t *)p_session->p_parent;
202 p_session->pf_close( p_session );