]> git.sesse.net Git - vlc/blob - src/misc/tls.c
Generic client SSL/TLS support
[vlc] / src / misc / tls.c
1 /*****************************************************************************
2  * tls.c
3  *****************************************************************************
4  * Copyright (C) 2004 VideoLAN
5  * $Id: httpd.c 8263 2004-07-24 09:06:58Z courmisch $
6  *
7  * Authors: Remi Denis-Courmont <courmisch@via.ecp.fr>
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 = __tls_ServerCreate( p_tls, psz_cert, psz_key );
59         if( p_server != NULL )
60         {
61             msg_Dbg( p_this, "TLS/SSL provider initialized" );
62             return p_server;
63         }
64         else
65             msg_Err( p_this, "TLS/SSL provider error" );
66         module_Unneed( p_tls, p_tls->p_module );
67     }
68     else
69         msg_Err( p_this, "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 = p_server->p_tls;
86
87     __tls_ServerDelete( 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, const char *psz_ca, int fd )
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 = __tls_ClientCreate( p_tls, psz_ca );
114         if( p_session != NULL )
115         {
116             int i_val;
117
118             for( i_val = tls_SessionHandshake( p_session, fd ); i_val > 0;
119                  i_val = tls_SessionContinueHandshake( p_session ) );
120             
121             if( i_val == 0 )
122             {
123                 msg_Dbg( p_this, "TLS/SSL provider initialized" );
124                 return p_session;
125             }
126             msg_Err( p_this, "TLS/SSL session handshake error" );
127         }
128         else
129             msg_Err( p_this, "TLS/SSL provider error" );
130         module_Unneed( p_tls, p_tls->p_module );
131     }
132     else
133         msg_Err( p_this, "TLS/SSL provider not found" );
134
135     vlc_object_detach( p_tls );
136     vlc_object_destroy( p_tls );
137     return NULL;
138 }
139
140
141 /*****************************************************************************
142  * tls_ClientDelete:
143  *****************************************************************************
144  * Releases data allocated with tls_ClientCreate.
145  *****************************************************************************/
146 void
147 tls_ClientDelete( tls_session_t *p_session )
148 {
149     tls_t *p_tls = p_session->p_tls;
150
151     tls_SessionClose( p_session );
152
153     module_Unneed( p_tls, p_tls->p_module );
154     vlc_object_detach( p_tls );
155     vlc_object_destroy( p_tls );
156 }