make outgoing SSL_connect() non-blocking
[freeradius.git] / src / include / tls-h
1 /*
2  *   This program is free software; you can redistribute it and/or modify
3  *   it under the terms of the GNU General Public License as published by
4  *   the Free Software Foundation; either version 2 of the License, or
5  *   (at your option) any later version.
6  *
7  *   This program is distributed in the hope that it will be useful,
8  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *   GNU General Public License for more details.
11  *
12  *   You should have received a copy of the GNU General Public License
13  *   along with this program; if not, write to the Free Software
14  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 #ifndef FR_TLS_H
17 #define FR_TLS_H
18
19 #ifdef WITH_TLS
20 /**
21  * $Id$
22  *
23  * @file tls.h
24  * @brief Structures and prototypes for TLS wrappers
25  *
26  * @copyright 2010 Network RADIUS SARL <info@networkradius.com>
27  */
28
29 RCSIDH(tls_h, "$Id$")
30
31 #include <freeradius-devel/conffile.h>
32
33 /*
34  *      For RH 9, which apparently needs this.
35  */
36 #ifndef OPENSSL_NO_KRB5
37 #  define OPENSSL_NO_KRB5
38 #endif
39 #include <openssl/err.h>
40 #ifdef HAVE_OPENSSL_ENGINE_H
41 #  include <openssl/engine.h>
42 #endif
43 #include <openssl/ssl.h>
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 typedef struct fr_tls_server_conf_t fr_tls_server_conf_t;
50
51 typedef enum {
52         FR_TLS_INVALID = 0,                     //!< Invalid, don't reply.
53         FR_TLS_REQUEST,                         //!< Request, ok to send, invalid to receive.
54         FR_TLS_RESPONSE,                        //!< Response, ok to receive, invalid to send.
55         FR_TLS_SUCCESS,                         //!< Success, send success.
56         FR_TLS_FAIL,                            //!< Fail, send fail.
57         FR_TLS_NOOP,                            //!< Noop, continue.
58
59         FR_TLS_START,                           //!< Start, ok to send, invalid to receive.
60         FR_TLS_OK,                              //!< Ok, continue.
61         FR_TLS_ACK,                             //!< Acknowledge, continue.
62         FR_TLS_FIRST_FRAGMENT,                  //!< First fragment.
63         FR_TLS_MORE_FRAGMENTS,                  //!< More fragments, to send/receive.
64         FR_TLS_LENGTH_INCLUDED,                 //!< Length included.
65         FR_TLS_MORE_FRAGMENTS_WITH_LENGTH,      //!< More fragments with length.
66         FR_TLS_HANDLED                          //!< TLS code has handled it.
67 } fr_tls_status_t;
68 extern FR_NAME_NUMBER const fr_tls_status_table[];
69
70 #define MAX_RECORD_SIZE 16384
71
72 /*
73  *      A single TLS record may be up to 16384 octets in length, but a
74  *      TLS message may span multiple TLS records, and a TLS
75  *      certificate message may in principle be as long as 16MB.
76  *
77  *      However, note that in order to protect against reassembly
78  *      lockup and denial of service attacks, it may be desirable for
79  *      an implementation to set a maximum size for one such group of
80  *      TLS messages.
81  *
82  *      The TLS Message Length field is four octets, and provides the
83  *      total length of the TLS message or set of messages that is
84  *      being fragmented; this simplifies buffer allocation.
85  */
86
87 /*
88  * FIXME: Dynamic allocation of buffer to overcome MAX_RECORD_SIZE overflows.
89  *      or configure TLS not to exceed MAX_RECORD_SIZE.
90  */
91 typedef struct _record_t {
92         uint8_t data[MAX_RECORD_SIZE];
93         size_t  used;
94 } record_t;
95
96 typedef struct _tls_info_t {
97         int             origin;
98         int             content_type;
99         uint8_t         handshake_type;
100         uint8_t         alert_level;
101         uint8_t         alert_description;
102         bool            initialized;
103
104         char            info_description[256];
105         size_t          record_len;
106         int             version;
107 } tls_info_t;
108
109 #if OPENSSL_VERSION_NUMBER < 0x10001000L
110 #define ssl_session ssl->session
111 #else
112 #define ssl_session session
113 #endif
114
115 /** Contains EAP-REQUEST specific data (ie FR_TLS_DATA(fragment), EAPTLS-ALERT, EAPTLS-REQUEST ...)
116  *
117  * The tls_session_t Structure gets stored as opaque in eap_handler_t
118  */
119 typedef struct _tls_session_t {
120         SSL_CTX         *ctx;
121         SSL             *ssl;
122 #if OPENSSL_VERSION_NUMBER >= 0x10001000L
123         SSL_SESSION     *session;
124 #endif
125         tls_info_t      info;
126
127         BIO             *into_ssl;
128         BIO             *from_ssl;
129         record_t        clean_in;                       //!< Data that needs to be sent but only after it is soiled.
130         record_t        clean_out;                      //!< Data that is cleaned after receiving.
131         record_t        dirty_in;                       //!< Data EAP server receives.
132         record_t        dirty_out;                      //!< Data EAP server sends.
133
134         void            (*record_init)(record_t *buf);
135         void            (*record_close)(record_t *buf);
136         unsigned int    (*record_plus)(record_t *buf, void const *ptr, unsigned int size);
137         unsigned int    (*record_minus)(record_t *buf, void *ptr, unsigned int size);
138
139         bool            invalid_hb_used;                //!< Whether heartbleed attack was detected.
140         bool            connected;                      //!< whether the outgoing socket is connected
141
142         /*
143          *      Framed-MTU attribute in RADIUS, if present, can also be used to set this
144          */
145         size_t          mtu;                            //!< Current fragment size transmitted.
146         size_t          tls_msg_len;                    //!< Actual/Total TLS message length.
147         bool            fragment;                       //!< Flag, In fragment mode or not.
148         bool            length_flag;                    //!< A flag to include length in every TLS Data/Alert packet.
149                                                         //!< If set to no then only the first fragment contains length.
150         int             peap_flag;
151
152         size_t          tls_record_in_total_len;        //!< How long the peer indicated the complete tls record
153                                                         //!< would be.
154         size_t          tls_record_in_recvd_len;        //!< How much of the record we've received so far.
155
156         /*
157          *      Used by TTLS & PEAP to keep track of other per-session data.
158          */
159         void            *opaque;
160         void            (*free_opaque)(void *opaque);
161
162         char const      *prf_label;
163         bool            allow_session_resumption;       //!< Whether session resumption is allowed.
164 } tls_session_t;
165
166 /*
167  *      RFC 2716, Section 4.2:
168  *
169  *         Flags
170  *
171  *      0 1 2 3 4 5 6 7 8
172  *      +-+-+-+-+-+-+-+-+
173  *      |L M S R R R R R|
174  *      +-+-+-+-+-+-+-+-+
175  *
176  *      L = Length included
177  *      M = More fragments
178  *      S = EAP-TLS start
179  *      R = Reserved
180  */
181 #define TLS_START(x)            (((x) & 0x20) != 0)
182 #define TLS_MORE_FRAGMENTS(x)   (((x) & 0x40) != 0)
183 #define TLS_LENGTH_INCLUDED(x)  (((x) & 0x80) != 0)
184
185 #define TLS_CHANGE_CIPHER_SPEC(x)       (((x) & 0x0014) == 0x0014)
186 #define TLS_ALERT(x)                    (((x) & 0x0015) == 0x0015)
187 #define TLS_HANDSHAKE(x)                (((x) & 0x0016) == 0x0016)
188
189 #define SET_START(x)            ((x) | (0x20))
190 #define SET_MORE_FRAGMENTS(x)   ((x) | (0x40))
191 #define SET_LENGTH_INCLUDED(x)  ((x) | (0x80))
192
193 /*
194  *      Following enums from rfc2246
195  *
196  *      Hmm... since we dpeend on OpenSSL, it would be smarter to
197  *      use the OpenSSL names for these.
198  */
199 enum ContentType {
200         change_cipher_spec = 20,
201         alert = 21,
202         handshake = 22,
203         application_data = 23
204 };
205
206 enum AlertLevel {
207         warning = 1,
208         fatal = 2
209 };
210
211 enum AlertDescription {
212         close_notify = 0,
213         unexpected_message = 10,
214         bad_record_mac = 20,
215         decryption_failed = 21,
216         record_overflow = 22,
217         decompression_failure = 30,
218         handshake_failure = 40,
219         bad_certificate = 42,
220         unsupported_certificate = 43,
221         certificate_revoked = 44,
222         certificate_expired = 45,
223         certificate_unknown = 46,
224         illegal_parameter = 47,
225         unknown_ca = 48,
226         access_denied = 49,
227         decode_error = 50,
228         decrypt_error = 51,
229         export_restriction = 60,
230         protocol_version = 70,
231         insufficient_security = 71,
232         internal_error = 80,
233         user_canceled = 90,
234         no_renegotiation = 100
235 };
236
237 enum HandshakeType {
238         hello_request = 0,
239         client_hello = 1,
240         server_hello = 2,
241         certificate = 11,
242         server_key_exchange  = 12,
243         certificate_request = 13,
244         server_hello_done = 14,
245         certificate_verify = 15,
246         client_key_exchange = 16,
247         handshake_finished = 20
248 };
249
250
251 /*
252  * From rfc
253    Flags
254
255       0 1 2 3 4 5 6 7 8
256       +-+-+-+-+-+-+-+-+
257       |L M S R R R R R|
258       +-+-+-+-+-+-+-+-+
259
260       L = Length included
261       M = More fragments
262       S = EAP-TLS start
263       R = Reserved
264
265       The L bit (length included) is set to indicate the presence of the
266       four octet TLS Message Length field, and MUST be set for the first
267       fragment of a fragmented TLS message or set of messages. The M bit
268       (more fragments) is set on all but the last fragment. The S bit
269       (EAP-TLS start) is set in an EAP-TLS Start message.  This
270       differentiates the EAP-TLS Start message from a fragment
271       acknowledgement.
272
273    TLS Message Length
274
275       The TLS Message Length field is four octets, and is present only
276       if the L bit is set. This field provides the total length of the
277       TLS message or set of messages that is being fragmented.
278
279    TLS data
280
281       The TLS data consists of the encapsulated TLS packet in TLS record
282       format.
283  *
284  * The data structures present here
285  * maps only to the typedata in the EAP packet
286  *
287  * Based on the L bit flag, first 4 bytes of data indicate the length
288  */
289
290 /* Callbacks */
291 int             cbtls_password(char *buf, int num, int rwflag, void *userdata);
292 void            cbtls_info(SSL const *s, int where, int ret);
293 void            cbtls_msg(int write_p, int msg_version, int content_type, void const *buf, size_t len, SSL *ssl,
294                           void *arg);
295 int             cbtls_verify(int ok, X509_STORE_CTX *ctx);
296
297 /* TLS */
298 void            tls_global_init(void);
299 #ifdef ENABLE_OPENSSL_VERSION_CHECK
300 int             tls_global_version_check(char const *acknowledged);
301 #endif
302
303 int             tls_error_log(REQUEST *request, char const *msg, ...) CC_HINT(format (printf, 2, 3));
304 int             tls_error_io_log(REQUEST *request, tls_session_t *session, int ret, char const *msg, ...)
305                                  CC_HINT(format (printf, 4, 5));
306
307 void            tls_global_cleanup(void);
308 tls_session_t   *tls_new_session(TALLOC_CTX *ctx, fr_tls_server_conf_t *conf, REQUEST *request, bool client_cert);
309 tls_session_t   *tls_new_client_session(TALLOC_CTX *ctx, fr_tls_server_conf_t *conf, int fd);
310 fr_tls_server_conf_t *tls_server_conf_parse(CONF_SECTION *cs);
311 fr_tls_server_conf_t *tls_client_conf_parse(CONF_SECTION *cs);
312 fr_tls_server_conf_t *tls_server_conf_alloc(TALLOC_CTX *ctx);
313 SSL_CTX         *tls_init_ctx(fr_tls_server_conf_t *conf, int client);
314 int             tls_handshake_recv(REQUEST *, tls_session_t *ssn);
315 int             tls_handshake_send(REQUEST *, tls_session_t *ssn);
316 void            tls_session_information(tls_session_t *ssn);
317 void            tls_session_id(SSL_SESSION *ssn, char *buffer, size_t bufsize);
318
319 /*
320  *      Low-level TLS stuff
321  */
322 int tls_success(tls_session_t *ssn, REQUEST *request);
323 void tls_fail(tls_session_t *ssn);
324 fr_tls_status_t tls_ack_handler(tls_session_t *tls_session, REQUEST *request);
325 fr_tls_status_t tls_application_data(tls_session_t *ssn, REQUEST *request);
326
327 #define FR_TLS_EX_INDEX_HANDLER  (10)
328 #define FR_TLS_EX_INDEX_CONF     (11)
329 #define FR_TLS_EX_INDEX_REQUEST  (12)
330 #define FR_TLS_EX_INDEX_IDENTITY (13)
331 #define FR_TLS_EX_INDEX_STORE    (14)
332 #define FR_TLS_EX_INDEX_SSN      (15)
333 #define FR_TLS_EX_INDEX_TALLOC   (16)
334
335 extern int fr_tls_ex_index_certs;
336 extern int fr_tls_ex_index_vps;
337
338 /* configured values goes right here */
339 struct fr_tls_server_conf_t {
340         SSL_CTX         *ctx;
341         CONF_SECTION    *cs;
342
343         char const      *private_key_password;
344         char const      *private_key_file;
345         char const      *certificate_file;
346         char const      *random_file;
347         char const      *ca_path;
348         char const      *ca_file;
349         char const      *dh_file;
350         char const      *rsa_file;
351         uint32_t        verify_depth;
352         bool            file_type;
353         bool            include_length;
354         bool            auto_chain;
355         bool            disable_single_dh_use;
356         bool            disable_tlsv1;
357         bool            disable_tlsv1_1;
358         bool            disable_tlsv1_2;
359
360         /*
361          *      Always < 4096 (due to radius limit), 0 by default = 1024
362          */
363         uint32_t        fragment_size;
364         bool            check_crl;
365         bool            check_all_crl;
366         bool            allow_expired_crl;
367         char const      *check_cert_cn;
368         char const      *cipher_list;
369         bool            cipher_server_preference;
370         char const      *check_cert_issuer;
371
372         bool            session_cache_enable;
373         uint32_t        session_timeout;
374         uint32_t        session_cache_size;
375         char const      *session_id_name;
376         char const      *session_cache_path;
377         char            session_context_id[SSL_MAX_SSL_SESSION_ID_LENGTH];
378         time_t          session_last_flushed;
379
380         bool            verify_skip_if_ocsp_ok;
381         char const      *verify_tmp_dir;
382         char const      *verify_client_cert_cmd;
383         bool            require_client_cert;
384
385 #ifdef HAVE_OPENSSL_OCSP_H
386         /*
387          * OCSP Configuration
388          */
389         bool            ocsp_enable;
390         bool            ocsp_override_url;
391         char const      *ocsp_url;
392         bool            ocsp_use_nonce;
393         X509_STORE      *ocsp_store;
394         uint32_t        ocsp_timeout;
395         bool            ocsp_softfail;
396 #endif
397
398 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
399 #ifndef OPENSSL_NO_ECDH
400         char const      *ecdh_curve;
401 #endif
402 #endif
403
404 #ifdef PSK_MAX_IDENTITY_LEN
405         char const      *psk_identity;
406         char const      *psk_password;
407         char const      *psk_query;
408 #endif
409
410 };
411
412 #ifdef __cplusplus
413 }
414 #endif
415
416 #endif  /* WITH_TLS */
417 #endif /* FR_TLS_H */