1 /*****************************************************************************
2 * vlc_tls.h: Transport Layer Security API
3 *****************************************************************************
4 * Copyright (C) 2004-2016 Rémi Denis-Courmont
5 * Copyright (C) 2005-2006 VLC authors and VideoLAN
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
27 * \defgroup transport Transport layer sockets
28 * Network stream abstraction
30 * Originally intended for the TLS protocol (Transport Layer Security),
31 * the Transport Layer Sockets now provides a generic abstraction
32 * for full-duplex I/O byte streams.
36 * Transport layer functions
39 # include <vlc_network.h>
41 /** Transport layer socket */
42 typedef struct vlc_tls
44 int (*get_fd)(struct vlc_tls *);
45 ssize_t (*readv)(struct vlc_tls *, struct iovec *, unsigned);
46 ssize_t (*writev)(struct vlc_tls *, const struct iovec *, unsigned);
47 int (*shutdown)(struct vlc_tls *, bool duplex);
48 void (*close)(struct vlc_tls *);
54 * \defgroup tls Transport Layer Security
61 * This structure contains the credentials for establishing TLS sessions.
62 * This includes root Certificate Authorities (on client side),
63 * trust and cryptographic parameters,
64 * public certificates and private keys.
66 typedef struct vlc_tls_creds
73 vlc_tls_t *(*open)(struct vlc_tls_creds *, vlc_tls_t *sock,
74 const char *host, const char *const *alpn);
75 int (*handshake)(struct vlc_tls_creds *, vlc_tls_t *session,
76 const char *hostname, const char *service,
77 char ** /*restrict*/ alp);
81 * Allocates TLS credentials for a client.
82 * Credentials can be cached and reused across multiple TLS sessions.
84 * @return TLS credentials object, or NULL on error.
86 VLC_API vlc_tls_creds_t *vlc_tls_ClientCreate(vlc_object_t *);
89 * Allocates server TLS credentials.
91 * @param cert path to an x509 certificate (required)
92 * @param key path to the PKCS private key for the certificate,
93 * or NULL to use cert path
95 * @return TLS credentials object, or NULL on error.
97 VLC_API vlc_tls_creds_t *vlc_tls_ServerCreate(vlc_object_t *, const char *cert,
100 static inline int vlc_tls_SessionHandshake (vlc_tls_creds_t *crd,
103 return crd->handshake(crd, tls, NULL, NULL, NULL);
107 * Releases TLS credentials.
109 * Releases data allocated with vlc_tls_ClientCreate() or
110 * vlc_tls_ServerCreate().
112 * @param srv object to be destroyed (or NULL)
114 VLC_API void vlc_tls_Delete(vlc_tls_creds_t *);
117 * Initiates a client TLS session.
119 * Initiates a Transport Layer Security (TLS) session as the client side, using
120 * trusted root CAs previously loaded with vlc_tls_ClientCreate().
122 * This is a blocking network operation and may be a thread cancellation point.
124 * @param creds X.509 credentials, i.e. set of root certificates of trusted
125 * certificate authorities
126 * @param sock socket through which to establish the secure channel
127 * @param hostname expected server name, used both as Server Name Indication
128 * and as expected Common Name of the peer certificate [IN]
129 * @param service unique identifier for the service to connect to
130 * (only used locally for certificates database) [IN]
131 * @param alpn NULL-terminated list of Application Layer Protocols
132 * to negotiate, or NULL to not negotiate protocols [IN]
133 * @param alp storage space for the negotiated Application Layer
134 * Protocol or NULL if negotiation was not performed [OUT]
136 * @note The credentials must remain valid until the session is finished.
138 * @return TLS session, or NULL on error.
140 VLC_API vlc_tls_t *vlc_tls_ClientSessionCreate(vlc_tls_creds_t *creds,
144 const char *const *alpn,
148 * Creates a TLS server session.
150 * Allocates a Transport Layer Security (TLS) session as the server side, using
151 * cryptographic keys pair and X.509 certificates chain already loaded with
152 * vlc_tls_ServerCreate().
154 * Unlike vlc_tls_ClientSessionCreate(), this function does not perform any
155 * actual network I/O. vlc_tls_SessionHandshake() must be used to perform the
156 * TLS handshake before sending and receiving data through the TLS session.
158 * This function is non-blocking and is not a cancellation point.
160 * @param creds server credentials, i.e. keys pair and X.509 certificates chain
161 * @param alpn NULL-terminated list of Application Layer Protocols
162 * to negotiate, or NULL to not negotiate protocols
164 * @return TLS session, or NULL on error.
166 VLC_API vlc_tls_t *vlc_tls_ServerSessionCreate(vlc_tls_creds_t *creds, int fd,
167 const char *const *alpn);
172 * Destroys a TLS session down.
174 * All resources associated with the TLS session are released.
176 * If the session was established successfully, then shutdown cleanly, the
177 * underlying socket can be reused. Otherwise, it must be closed. Either way,
178 * this function does not close the underlying socket: Use vlc_tls_Close()
179 * instead to close it at the same.
181 * This function is non-blocking and is not a cancellation point.
183 VLC_API void vlc_tls_SessionDelete (vlc_tls_t *);
185 static inline int vlc_tls_GetFD(vlc_tls_t *tls)
187 return tls->get_fd(tls);
191 * Receives data through a socket.
193 * This dequeues incoming data from a transport layer socket.
195 * @param buf received buffer start address [OUT]
196 * @param len buffer length (in bytes)
197 * @param waitall whether to wait for the exact buffer length (true),
198 * or for any amount of data (false)
200 * @note At end of stream, the number of bytes returned may be shorter than
201 * requested regardless of the "waitall" flag.
203 * @return the number of bytes actually dequeued, or -1 on error.
205 VLC_API ssize_t vlc_tls_Read(vlc_tls_t *, void *buf, size_t len, bool waitall);
208 * Receives a text line through a socket.
210 * This dequeues one line of text from a transport layer socket.
211 * @return a heap-allocated nul-terminated string, or NULL on error
213 VLC_API char *vlc_tls_GetLine(vlc_tls_t *);
216 * Sends data through a socket.
218 VLC_API ssize_t vlc_tls_Write(vlc_tls_t *, const void *buf, size_t len);
221 * Shuts a connection down.
223 * This sends the connection close notification.
225 * If the TLS protocol is used, this provides a secure indication to the other
226 * end that no further data will be sent. If using plain TCP/IP, this sets the
229 * Data can still be received until a close notification is received from the
232 * @param duplex whether to stop receiving data as well
233 * @retval 0 the session was terminated securely and cleanly
234 * (the underlying socket can be reused for other purposes)
235 * @return -1 the session was terminated locally, but either a notification
236 * could not be sent or received (the underlying socket cannot be
237 * reused and must be closed)
239 static inline int vlc_tls_Shutdown(vlc_tls_t *tls, bool duplex)
241 return tls->shutdown(tls, duplex);
244 # define tls_Recv(a,b,c) vlc_tls_Read(a,b,c,false)
245 # define tls_Send(a,b,c) vlc_tls_Write(a,b,c)
248 * Closes a connection and its underlying resources.
250 * This function closes the transport layer socket, and terminates any
251 * underlying connection. For instance, if the TLS protocol is used over a TCP
252 * stream, this function terminates both the TLS session, and then underlying
255 * To close a connection but retain any underlying resources, use
256 * vlc_tls_SessionDelete() instead.
258 static inline void vlc_tls_Close(vlc_tls_t *session)
262 vlc_tls_t *p = session->p;
264 vlc_tls_SessionDelete(session);
267 while (session != NULL);
271 * Creates a transport-layer stream from a socket.
273 * Creates a transport-layer I/O stream from a socket file descriptor.
274 * Data will be sent and received directly through the socket. This can be used
275 * either to share common code between non-TLS and TLS cases, or for testing
278 * This function is not a cancellation point.
280 * @deprecated This function is transitional. Do not use it directly.
282 VLC_API vlc_tls_t *vlc_tls_SocketOpen(int fd);
285 * Creates a connected pair of transport-layer sockets.
287 VLC_API int vlc_tls_SocketPair(int family, int protocol, vlc_tls_t *[2]);
292 * Creates a transport-layer stream from a struct addrinfo.
294 * \note The function currently iterates through the addrinfo linked list.
295 * Future versions may implement different behaviour (e.g. RFC6555).
297 vlc_tls_t *vlc_tls_SocketOpenAddrInfo(vlc_object_t *obj,
298 const struct addrinfo *);
301 * Creates a transport-layer TCP stream from a name and port.
303 * This function resolves a hostname, and attempts to establish a TCP/IP
304 * connection to the specified host and port number.
306 * @return a transport layer socket on success or NULL on error
308 VLC_API vlc_tls_t *vlc_tls_SocketOpenTCP(vlc_object_t *obj,
309 const char *hostname, unsigned port);
312 * Initiates a TLS session over TCP.
314 * This function resolves a hostname, attempts to establish a TCP/IP
315 * connection to the specified host and port number, and finally attempts to
316 * establish a TLS session over the TCP/IP stream.
318 * See also vlc_tls_SocketOpenTCP() and vlc_tls_SessionCreate().
320 VLC_API vlc_tls_t *vlc_tls_SocketOpenTLS(vlc_tls_creds_t *crd,
321 const char *hostname, unsigned port,
323 const char *const *alpn, char **alp);
326 static inline vlc_tls_t *
327 vlc_tls_ClientSessionCreateFD(vlc_tls_creds_t *crd, int fd, const char *host,
328 const char *srv, const char *const *lp, char **p)
330 vlc_tls_t *sock = vlc_tls_SocketOpen(fd);
331 if (unlikely(sock == NULL))
334 vlc_tls_t *tls = vlc_tls_ClientSessionCreate(crd, sock, host, srv, lp, p);
335 if (unlikely(tls == NULL))