| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
#ifdef HAVE_CONFIG_H |
|---|
| 30 |
# include "config.h" |
|---|
| 31 |
#endif |
|---|
| 32 |
|
|---|
| 33 |
#include <vlc_common.h> |
|---|
| 34 |
#include "libvlc.h" |
|---|
| 35 |
|
|---|
| 36 |
#include <vlc_tls.h> |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
tls_server_t * |
|---|
| 49 |
tls_ServerCreate (vlc_object_t *obj, const char *cert_path, |
|---|
| 50 |
const char *key_path) |
|---|
| 51 |
{ |
|---|
| 52 |
tls_server_t *srv; |
|---|
| 53 |
|
|---|
| 54 |
srv = (tls_server_t *)vlc_custom_create (obj, sizeof (*srv), |
|---|
| 55 |
VLC_OBJECT_GENERIC, |
|---|
| 56 |
"tls server"); |
|---|
| 57 |
if (srv == NULL) |
|---|
| 58 |
return NULL; |
|---|
| 59 |
|
|---|
| 60 |
var_Create (srv, "tls-x509-cert", VLC_VAR_STRING); |
|---|
| 61 |
var_Create (srv, "tls-x509-key", VLC_VAR_STRING); |
|---|
| 62 |
|
|---|
| 63 |
if (cert_path != NULL) |
|---|
| 64 |
{ |
|---|
| 65 |
var_SetString (srv, "tls-x509-cert", cert_path); |
|---|
| 66 |
|
|---|
| 67 |
if (key_path == NULL) |
|---|
| 68 |
key_path = cert_path; |
|---|
| 69 |
var_SetString (srv, "tls-x509-key", key_path); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
srv->p_module = module_Need (srv, "tls server", 0, 0); |
|---|
| 73 |
if (srv->p_module == NULL) |
|---|
| 74 |
{ |
|---|
| 75 |
msg_Err (srv, "TLS server plugin not available"); |
|---|
| 76 |
vlc_object_release (srv); |
|---|
| 77 |
return NULL; |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
vlc_object_attach (srv, obj); |
|---|
| 81 |
msg_Dbg (srv, "TLS server plugin initialized"); |
|---|
| 82 |
return srv; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
void tls_ServerDelete (tls_server_t *srv) |
|---|
| 91 |
{ |
|---|
| 92 |
if (srv == NULL) |
|---|
| 93 |
return; |
|---|
| 94 |
|
|---|
| 95 |
module_Unneed (srv, srv->p_module); |
|---|
| 96 |
vlc_object_detach (srv); |
|---|
| 97 |
vlc_object_release (srv); |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
int tls_ServerAddCA (tls_server_t *srv, const char *path) |
|---|
| 106 |
{ |
|---|
| 107 |
return srv->pf_add_CA (srv, path); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
int tls_ServerAddCRL (tls_server_t *srv, const char *path) |
|---|
| 116 |
{ |
|---|
| 117 |
return srv->pf_add_CRL (srv, path); |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
tls_session_t *tls_ServerSessionPrepare (tls_server_t *srv) |
|---|
| 122 |
{ |
|---|
| 123 |
tls_session_t *ses; |
|---|
| 124 |
|
|---|
| 125 |
ses = srv->pf_open (srv); |
|---|
| 126 |
if (ses == NULL) |
|---|
| 127 |
return NULL; |
|---|
| 128 |
|
|---|
| 129 |
vlc_object_attach (ses, srv); |
|---|
| 130 |
return ses; |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
void tls_ServerSessionClose (tls_session_t *ses) |
|---|
| 135 |
{ |
|---|
| 136 |
tls_server_t *srv = (tls_server_t *)(ses->p_parent); |
|---|
| 137 |
srv->pf_close (srv, ses); |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
int tls_ServerSessionHandshake (tls_session_t *ses, int fd) |
|---|
| 142 |
{ |
|---|
| 143 |
ses->pf_set_fd (ses, fd); |
|---|
| 144 |
return 2; |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
int tls_SessionContinueHandshake (tls_session_t *ses) |
|---|
| 149 |
{ |
|---|
| 150 |
int val = ses->pf_handshake (ses); |
|---|
| 151 |
if (val < 0) |
|---|
| 152 |
tls_ServerSessionClose (ses); |
|---|
| 153 |
return val; |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
tls_session_t * |
|---|
| 168 |
tls_ClientCreate (vlc_object_t *obj, int fd, const char *psz_hostname) |
|---|
| 169 |
{ |
|---|
| 170 |
tls_session_t *cl; |
|---|
| 171 |
int val; |
|---|
| 172 |
|
|---|
| 173 |
cl = (tls_session_t *)vlc_custom_create (obj, sizeof (*cl), |
|---|
| 174 |
VLC_OBJECT_GENERIC, |
|---|
| 175 |
"tls client"); |
|---|
| 176 |
if (cl == NULL) |
|---|
| 177 |
return NULL; |
|---|
| 178 |
|
|---|
| 179 |
var_Create (cl, "tls-server-name", VLC_VAR_STRING); |
|---|
| 180 |
if (psz_hostname != NULL) |
|---|
| 181 |
{ |
|---|
| 182 |
msg_Dbg (cl, "requested server name: %s", psz_hostname); |
|---|
| 183 |
var_SetString (cl, "tls-server-name", psz_hostname); |
|---|
| 184 |
} |
|---|
| 185 |
else |
|---|
| 186 |
msg_Dbg (cl, "requested anonymous server"); |
|---|
| 187 |
|
|---|
| 188 |
cl->p_module = module_Need (cl, "tls client", 0, 0); |
|---|
| 189 |
if (cl->p_module == NULL) |
|---|
| 190 |
{ |
|---|
| 191 |
msg_Err (cl, "TLS client plugin not available"); |
|---|
| 192 |
vlc_object_release (cl); |
|---|
| 193 |
return NULL; |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
cl->pf_set_fd (cl, fd); |
|---|
| 197 |
|
|---|
| 198 |
do |
|---|
| 199 |
val = cl->pf_handshake (cl); |
|---|
| 200 |
while (val > 0); |
|---|
| 201 |
|
|---|
| 202 |
if (val == 0) |
|---|
| 203 |
{ |
|---|
| 204 |
msg_Dbg (cl, "TLS client session initialized"); |
|---|
| 205 |
vlc_object_attach (cl, obj); |
|---|
| 206 |
return cl; |
|---|
| 207 |
} |
|---|
| 208 |
msg_Err (cl, "TLS client session handshake error"); |
|---|
| 209 |
|
|---|
| 210 |
module_Unneed (cl, cl->p_module); |
|---|
| 211 |
vlc_object_release (cl); |
|---|
| 212 |
return NULL; |
|---|
| 213 |
} |
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
void tls_ClientDelete (tls_session_t *cl) |
|---|
| 221 |
{ |
|---|
| 222 |
if (cl == NULL) |
|---|
| 223 |
return; |
|---|
| 224 |
|
|---|
| 225 |
module_Unneed (cl, cl->p_module); |
|---|
| 226 |
vlc_object_detach (cl); |
|---|
| 227 |
vlc_object_release (cl); |
|---|
| 228 |
} |
|---|