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