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.
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.
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
24 * @brief Structures and prototypes for TLS wrappers
26 * @copyright 2010 Network RADIUS SARL <info@networkradius.com>
31 #include <freeradius-devel/conffile.h>
34 * For RH 9, which apparently needs this.
36 #ifndef OPENSSL_NO_KRB5
37 # define OPENSSL_NO_KRB5
39 #include <openssl/err.h>
40 #ifdef HAVE_OPENSSL_ENGINE_H
41 # include <openssl/engine.h>
43 #include <openssl/ssl.h>
49 typedef struct fr_tls_server_conf_t fr_tls_server_conf_t;
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.
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.
68 extern FR_NAME_NUMBER const fr_tls_status_table[];
70 #define MAX_RECORD_SIZE 16384
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.
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
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.
88 * FIXME: Dynamic allocation of buffer to overcome MAX_RECORD_SIZE overflows.
89 * or configure TLS not to exceed MAX_RECORD_SIZE.
91 typedef struct _record_t {
92 uint8_t data[MAX_RECORD_SIZE];
96 typedef struct _tls_info_t {
99 uint8_t handshake_type;
101 uint8_t alert_description;
104 char info_description[256];
109 #if OPENSSL_VERSION_NUMBER < 0x10001000L
110 #define ssl_session ssl->session
112 #define ssl_session session
115 /** Contains EAP-REQUEST specific data (ie FR_TLS_DATA(fragment), EAPTLS-ALERT, EAPTLS-REQUEST ...)
117 * The tls_session_t Structure gets stored as opaque in eap_handler_t
119 typedef struct _tls_session_t {
122 #if OPENSSL_VERSION_NUMBER >= 0x10001000L
123 SSL_SESSION *session;
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.
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);
139 bool invalid_hb_used; //!< Whether heartbleed attack was detected.
142 * Framed-MTU attribute in RADIUS, if present, can also be used to set this
144 size_t mtu; //!< Current fragment size transmitted.
145 size_t tls_msg_len; //!< Actual/Total TLS message length.
146 bool fragment; //!< Flag, In fragment mode or not.
147 bool length_flag; //!< A flag to include length in every TLS Data/Alert packet.
148 //!< If set to no then only the first fragment contains length.
151 size_t tls_record_in_total_len; //!< How long the peer indicated the complete tls record
153 size_t tls_record_in_recvd_len; //!< How much of the record we've received so far.
156 * Used by TTLS & PEAP to keep track of other per-session data.
159 void (*free_opaque)(void *opaque);
161 char const *prf_label;
162 bool allow_session_resumption; //!< Whether session resumption is allowed.
166 * RFC 2716, Section 4.2:
175 * L = Length included
180 #define TLS_START(x) (((x) & 0x20) != 0)
181 #define TLS_MORE_FRAGMENTS(x) (((x) & 0x40) != 0)
182 #define TLS_LENGTH_INCLUDED(x) (((x) & 0x80) != 0)
184 #define TLS_CHANGE_CIPHER_SPEC(x) (((x) & 0x0014) == 0x0014)
185 #define TLS_ALERT(x) (((x) & 0x0015) == 0x0015)
186 #define TLS_HANDSHAKE(x) (((x) & 0x0016) == 0x0016)
188 #define SET_START(x) ((x) | (0x20))
189 #define SET_MORE_FRAGMENTS(x) ((x) | (0x40))
190 #define SET_LENGTH_INCLUDED(x) ((x) | (0x80))
193 * Following enums from rfc2246
195 * Hmm... since we dpeend on OpenSSL, it would be smarter to
196 * use the OpenSSL names for these.
199 change_cipher_spec = 20,
202 application_data = 23
210 enum AlertDescription {
212 unexpected_message = 10,
214 decryption_failed = 21,
215 record_overflow = 22,
216 decompression_failure = 30,
217 handshake_failure = 40,
218 bad_certificate = 42,
219 unsupported_certificate = 43,
220 certificate_revoked = 44,
221 certificate_expired = 45,
222 certificate_unknown = 46,
223 illegal_parameter = 47,
228 export_restriction = 60,
229 protocol_version = 70,
230 insufficient_security = 71,
233 no_renegotiation = 100
241 server_key_exchange = 12,
242 certificate_request = 13,
243 server_hello_done = 14,
244 certificate_verify = 15,
245 client_key_exchange = 16,
246 handshake_finished = 20
264 The L bit (length included) is set to indicate the presence of the
265 four octet TLS Message Length field, and MUST be set for the first
266 fragment of a fragmented TLS message or set of messages. The M bit
267 (more fragments) is set on all but the last fragment. The S bit
268 (EAP-TLS start) is set in an EAP-TLS Start message. This
269 differentiates the EAP-TLS Start message from a fragment
274 The TLS Message Length field is four octets, and is present only
275 if the L bit is set. This field provides the total length of the
276 TLS message or set of messages that is being fragmented.
280 The TLS data consists of the encapsulated TLS packet in TLS record
283 * The data structures present here
284 * maps only to the typedata in the EAP packet
286 * Based on the L bit flag, first 4 bytes of data indicate the length
290 int cbtls_password(char *buf, int num, int rwflag, void *userdata);
291 void cbtls_info(SSL const *s, int where, int ret);
292 void cbtls_msg(int write_p, int msg_version, int content_type, void const *buf, size_t len, SSL *ssl,
294 int cbtls_verify(int ok, X509_STORE_CTX *ctx);
297 void tls_global_init(void);
298 #ifdef ENABLE_OPENSSL_VERSION_CHECK
299 int tls_global_version_check(char const *acknowledged);
301 void tls_global_cleanup(void);
302 tls_session_t *tls_new_session(TALLOC_CTX *ctx, fr_tls_server_conf_t *conf, REQUEST *request, bool client_cert);
303 tls_session_t *tls_new_client_session(TALLOC_CTX *ctx, fr_tls_server_conf_t *conf, int fd);
304 fr_tls_server_conf_t *tls_server_conf_parse(CONF_SECTION *cs);
305 fr_tls_server_conf_t *tls_client_conf_parse(CONF_SECTION *cs);
306 SSL_CTX *tls_init_ctx(fr_tls_server_conf_t *conf, int client);
307 int tls_handshake_recv(REQUEST *, tls_session_t *ssn);
308 int tls_handshake_send(REQUEST *, tls_session_t *ssn);
309 void tls_session_information(tls_session_t *ssn);
310 void tls_session_id(SSL_SESSION *ssn, char *buffer, size_t bufsize);
313 * Low-level TLS stuff
315 int tls_success(tls_session_t *ssn, REQUEST *request);
316 void tls_fail(tls_session_t *ssn);
317 fr_tls_status_t tls_ack_handler(tls_session_t *tls_session, REQUEST *request);
318 fr_tls_status_t tls_application_data(tls_session_t *ssn, REQUEST *request);
320 #define FR_TLS_EX_INDEX_HANDLER (10)
321 #define FR_TLS_EX_INDEX_CONF (11)
322 #define FR_TLS_EX_INDEX_REQUEST (12)
323 #define FR_TLS_EX_INDEX_IDENTITY (13)
324 #define FR_TLS_EX_INDEX_STORE (14)
325 #define FR_TLS_EX_INDEX_SSN (15)
326 #define FR_TLS_EX_INDEX_TALLOC (16)
328 extern int fr_tls_ex_index_certs;
329 extern int fr_tls_ex_index_vps;
331 /* configured values goes right here */
332 struct fr_tls_server_conf_t {
336 char const *private_key_password;
337 char const *private_key_file;
338 char const *certificate_file;
339 char const *random_file;
343 char const *rsa_file;
346 uint32_t rsa_key_length;
347 uint32_t dh_key_length;
348 uint32_t verify_depth;
352 bool disable_tlsv1_1;
353 bool disable_tlsv1_2;
356 * Always < 4096 (due to radius limit), 0 by default = 2048
358 uint32_t fragment_size;
361 bool allow_expired_crl;
362 char const *check_cert_cn;
363 char const *cipher_list;
364 char const *check_cert_issuer;
366 bool session_cache_enable;
367 uint32_t session_timeout;
368 uint32_t session_cache_size;
369 char const *session_id_name;
370 char const *session_cache_path;
371 char session_context_id[SSL_MAX_SSL_SESSION_ID_LENGTH];
372 time_t session_last_flushed;
374 char const *verify_tmp_dir;
375 char const *verify_client_cert_cmd;
376 bool require_client_cert;
378 #ifdef HAVE_OPENSSL_OCSP_H
383 bool ocsp_override_url;
384 char const *ocsp_url;
386 X509_STORE *ocsp_store;
387 uint32_t ocsp_timeout;
391 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
392 #ifndef OPENSSL_NO_ECDH
393 char const *ecdh_curve;
397 #ifdef PSK_MAX_IDENTITY_LEN
398 char const *psk_identity;
399 char const *psk_password;
400 char const *psk_query;
409 #endif /* WITH_TLS */
410 #endif /* FR_TLS_H */