]> git.sesse.net Git - vlc/blob - src/misc/tls.c
Copyright fixes
[vlc] / src / misc / tls.c
1 /*****************************************************************************
2  * tls.c
3  *****************************************************************************
4  * Copyright (C) 2004-2005 VideoLAN (Centrale Réseaux) and its contributors
5  * $Id$
6  *
7  * Authors: Remi Denis-Courmont <rem # videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #include <stdlib.h>
25 #include <vlc/vlc.h>
26
27 #include "vlc_tls.h"
28
29
30 /*****************************************************************************
31  * tls_ServerCreate:
32  *****************************************************************************
33  * Allocates a whole server's TLS credentials.
34  * Returns NULL on error.
35  *****************************************************************************/
36 tls_server_t *
37 tls_ServerCreate( vlc_object_t *p_this, const char *psz_cert,
38                   const char *psz_key )
39 {
40     tls_t *p_tls;
41     tls_server_t *p_server;
42
43     p_tls = vlc_object_create( p_this, VLC_OBJECT_TLS );
44     vlc_object_attach( p_tls, p_this );
45
46     p_tls->p_module = module_Need( p_tls, "tls", 0, 0 );
47     if( p_tls->p_module != NULL )
48     {
49         if( psz_key == NULL )
50             psz_key = psz_cert;
51
52         p_server = p_tls->pf_server_create( p_tls, psz_cert, psz_key );
53         if( p_server != NULL )
54         {
55             msg_Dbg( p_tls, "TLS/SSL provider initialized" );
56             return p_server;
57         }
58         else
59             msg_Err( p_tls, "TLS/SSL provider error" );
60         module_Unneed( p_tls, p_tls->p_module );
61     }
62     else
63         msg_Err( p_tls, "TLS/SSL provider not found" );
64
65     vlc_object_detach( p_tls );
66     vlc_object_destroy( p_tls );
67     return NULL;
68 }
69
70
71 /*****************************************************************************
72  * tls_ServerDelete:
73  *****************************************************************************
74  * Releases data allocated with tls_ServerCreate.
75  *****************************************************************************/
76 void
77 tls_ServerDelete( tls_server_t *p_server )
78 {
79     tls_t *p_tls = (tls_t *)p_server->p_parent;
80
81     p_server->pf_delete( p_server );
82
83     module_Unneed( p_tls, p_tls->p_module );
84     vlc_object_detach( p_tls );
85     vlc_object_destroy( p_tls );
86 }
87
88
89 /*****************************************************************************
90  * tls_ClientCreate:
91  *****************************************************************************
92  * Allocates a client's TLS credentials and shakes hands through the network.
93  * Returns NULL on error. This is a blocking network operation.
94  *****************************************************************************/
95 tls_session_t *
96 tls_ClientCreate( vlc_object_t *p_this, int fd, const char *psz_hostname )
97 {
98     tls_t *p_tls;
99     tls_session_t *p_session;
100
101     p_tls = vlc_object_create( p_this, VLC_OBJECT_TLS );
102     vlc_object_attach( p_tls, p_this );
103
104     p_tls->p_module = module_Need( p_tls, "tls", 0, 0 );
105     if( p_tls->p_module != NULL )
106     {
107         p_session = p_tls->pf_client_create( p_tls );
108         if( p_session != NULL )
109         {
110             int i_val;
111
112             for( i_val = tls_ClientSessionHandshake( p_session, fd,
113                                                      psz_hostname );
114                  i_val > 0;
115                  i_val = tls_SessionContinueHandshake( p_session ) );
116             
117             if( i_val == 0 )
118             {
119                 msg_Dbg( p_this, "TLS/SSL provider initialized" );
120                 return p_session;
121             }
122             msg_Err( p_this, "TLS/SSL session handshake error" );
123         }
124         else
125             msg_Err( p_this, "TLS/SSL provider error" );
126         module_Unneed( p_tls, p_tls->p_module );
127     }
128     else
129         msg_Err( p_this, "TLS/SSL provider not found" );
130
131     vlc_object_detach( p_tls );
132     vlc_object_destroy( p_tls );
133     return NULL;
134 }
135
136
137 /*****************************************************************************
138  * tls_ClientDelete:
139  *****************************************************************************
140  * Releases data allocated with tls_ClientCreate.
141  *****************************************************************************/
142 void
143 tls_ClientDelete( tls_session_t *p_session )
144 {
145     tls_t *p_tls = (tls_t *)p_session->p_parent;
146
147     p_session->pf_close( p_session );
148
149     module_Unneed( p_tls, p_tls->p_module );
150     vlc_object_detach( p_tls );
151     vlc_object_destroy( p_tls );
152 }