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