The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
rlm_eap_tls.c
Go to the documentation of this file.
1/*
2 * rlm_eap_tls.c contains the interfaces that are called from eap
3 *
4 * Version: $Id: 312db816fe2b7a1e857b9f58b179101d728c1191 $
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 *
20 * @copyright 2001 hereUare Communications, Inc. (raghud@hereuare.com)
21 * @copyright 2003 Alan DeKok (aland@freeradius.org)
22 * @copyright 2006 The FreeRADIUS server project
23 *
24 */
25RCSID("$Id: 312db816fe2b7a1e857b9f58b179101d728c1191 $")
26USES_APPLE_DEPRECATED_API /* OpenSSL API has been deprecated by Apple */
27
28#include <freeradius-devel/tls/openssl_user_macros.h>
29#ifdef HAVE_OPENSSL_RAND_H
30# include <openssl/rand.h>
31#endif
32#ifdef HAVE_OPENSSL_EVP_H
33# include <openssl/evp.h>
34#endif
35#ifdef HAVE_SYS_STAT_H
36# include <sys/stat.h>
37#endif
38
39#include "rlm_eap_tls.h"
40
41typedef struct {
42 SSL_CTX *ssl_ctx; //!< Thread local SSL_CTX.
44
46 { FR_CONF_SUBSECTION_ALLOC("tls", CONF_FLAG_REQUIRED, rlm_eap_tls_t, tls_conf, fr_tls_server_config),
47 .subcs_type = "fr_tls_conf_t" },
48
49 { FR_CONF_OFFSET("require_client_cert", rlm_eap_tls_t, req_client_cert), .dflt = "yes" },
50 { FR_CONF_OFFSET("include_length", rlm_eap_tls_t, include_length), .dflt = "yes" },
52};
53
55
58 { .out = &dict_freeradius, .proto = "freeradius" },
60};
61
63
66 { .out = &attr_eap_tls_require_client_cert, .name = "EAP-TLS-Require-Client-Cert", .type = FR_TYPE_UINT32, .dict = &dict_freeradius },
68};
69
75
77{
78 eap_session_t *eap_session = talloc_get_type_abort(mctx->rctx, eap_session_t);
79 eap_tls_session_t *eap_tls_session = talloc_get_type_abort(eap_session->opaque, eap_tls_session_t);
80 fr_tls_session_t *tls_session = eap_tls_session->tls_session;
81
82 if ((eap_tls_session->state == EAP_TLS_INVALID) || (eap_tls_session->state == EAP_TLS_FAIL)) {
83 REDEBUG("[eap-tls process] = %s", fr_table_str_by_value(eap_tls_status_table, eap_tls_session->state, "<INVALID>"));
84 } else {
85 RDEBUG2("[eap-tls process] = %s", fr_table_str_by_value(eap_tls_status_table, eap_tls_session->state, "<INVALID>"));
86 }
87
88 switch (eap_tls_session->state) {
89 /*
90 * EAP-TLS handshake was successful, return an
91 * EAP-TLS-Success packet here.
92 *
93 * If a virtual server was configured, check that
94 * it accepts the certificates, too.
95 */
97 {
98 eap_tls_prf_label_t prf_label;
99
100 eap_crypto_prf_label_init(&prf_label, eap_session,
101 "client EAP encryption",
102 sizeof("client EAP encryption") - 1);
103
104 if (eap_tls_success(request, eap_session, &prf_label) < 0) RETURN_UNLANG_FAIL;
105
106 /*
107 * Result is always OK, even if we fail to persist the
108 * session data.
109 */
111 /*
112 * Write the session to the session cache
113 *
114 * We do this here (instead of relying on OpenSSL to call the
115 * session caching callback), because we only want to write
116 * session data to the cache if all phases were successful.
117 *
118 * If we wrote out the cache data earlier, and the server
119 * exited whilst the session was in progress, the supplicant
120 * could resume the session (and get access) even if phase2
121 * never completed.
122 */
123 return fr_tls_ticket_stateful_store_session(request, tls_session);
124 }
125
126 /*
127 * The TLS code is still working on the TLS
128 * exchange, and it's a valid TLS request.
129 * do nothing.
130 */
131 case EAP_TLS_HANDLED:
133
134 /*
135 * Handshake is done, proceed with decoding tunneled
136 * data.
137 */
139 REDEBUG("Received unexpected tunneled data after successful handshake");
140 p_result->rcode = RLM_MODULE_INVALID;
141 return eap_tls_fail(request, eap_session);
142
143 /*
144 * Anything else: fail.
145 */
146 default:
147 p_result->rcode = RLM_MODULE_REJECT;
148
149 /*
150 * eap_tls_fail() clears the cached session for us, and
151 * composes the EAP-Failure. We'll jump back to the
152 * caller in the unlang stack if that pushes a child.
153 */
154 return eap_tls_fail(request, eap_session);
155 }
156}
157
158/*
159 * Do authentication, by letting EAP-TLS do most of the work.
160 */
163{
164 eap_session_t *eap_session = eap_session_get(request->parent);
165
166 /*
167 * Setup the resumption frame to process the result
168 */
170
171 /*
172 * Process TLS layer until done.
173 */
174 return eap_tls_process(request, eap_session);
175}
176
178{
179 rlm_eap_tls_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_eap_tls_t);
180 rlm_eap_tls_thread_t *t = talloc_get_type_abort(mctx->thread, rlm_eap_tls_thread_t);
181 eap_session_t *eap_session = eap_session_get(request->parent);
182 eap_tls_session_t *eap_tls_session;
183
184 fr_pair_t *vp;
185 bool client_cert;
186
187 /*
188 * EAP-TLS-Require-Client-Cert attribute will override
189 * the require_client_cert configuration option.
190 */
192 if (vp) {
193 client_cert = vp->vp_uint32 ? true : false;
194 } else {
195 client_cert = inst->req_client_cert;
196 }
197
198 /*
199 * EAP-TLS always requires a client certificate.
200 */
201 eap_session->opaque = eap_tls_session = eap_tls_session_init(request, eap_session, t->ssl_ctx, client_cert);
202 if (!eap_tls_session) RETURN_UNLANG_FAIL;
203
204 eap_tls_session->include_length = inst->include_length;
205
206 /*
207 * TLS session initialization is over. Now handle TLS
208 * related handshaking or application data.
209 */
210 if (eap_tls_start(request, eap_session) < 0) {
211 talloc_free(eap_tls_session);
213 }
214
215 eap_session->process = mod_handshake_process;
216
218}
219
220/*
221 * Send an initial eap-tls request to the peer, using the libeap functions.
222 */
224{
225 rlm_eap_tls_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_eap_tls_t);
226 eap_session_t *eap_session = eap_session_get(request->parent);
227
228 eap_session->tls = true;
229
231
232 if (inst->tls_conf->new_session) return fr_tls_new_session_push(request, inst->tls_conf);
233
235}
236
238{
239 rlm_eap_tls_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_eap_tls_t);
240 rlm_eap_tls_thread_t *t = talloc_get_type_abort(mctx->thread, rlm_eap_tls_thread_t);
241
242 t->ssl_ctx = fr_tls_ctx_alloc(inst->tls_conf, false);
243 if (!t->ssl_ctx) return -1;
244
245 return 0;
246}
247
249{
250 rlm_eap_tls_thread_t *t = talloc_get_type_abort(mctx->thread, rlm_eap_tls_thread_t);
251
252 if (likely(t->ssl_ctx != NULL)) SSL_CTX_free(t->ssl_ctx);
253 t->ssl_ctx = NULL;
254
255 return 0;
256}
257
258/*
259 * Attach the EAP-TLS module.
260 */
261static int mod_instantiate(module_inst_ctx_t const *mctx)
262{
263 rlm_eap_tls_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_eap_tls_t);
264 CONF_SECTION *conf = mctx->mi->conf;
265
266 eap_tls_conf_check(conf, inst->tls_conf);
267
268 return 0;
269}
270
271/*
272 * The module name should be the only globally exported symbol.
273 * That is, everything else should be 'static'.
274 */
277 .common = {
278 .magic = MODULE_MAGIC_INIT,
279 .name = "eap_tls",
280 .inst_size = sizeof(rlm_eap_tls_t),
282 .instantiate = mod_instantiate, /* Create new submodule instance */
283
284 .thread_inst_size = sizeof(rlm_eap_tls_thread_t),
285 .thread_instantiate = mod_thread_instantiate,
286 .thread_detach = mod_thread_detach,
287 },
288 .provides = { FR_EAP_METHOD_TLS },
289 .session_init = mod_session_init, /* Initialise a new EAP session */
290};
unlang_action_t
Returned by unlang_op_t calls, determine the next action of the interpreter.
Definition action.h:35
@ UNLANG_ACTION_CALCULATE_RESULT
Calculate a new section rlm_rcode_t value.
Definition action.h:38
static uint8_t request[4]
#define USES_APPLE_DEPRECATED_API
Definition build.h:590
#define RCSID(id)
Definition build.h:603
#define UNUSED
Definition build.h:415
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:712
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Definition cf_parse.h:280
@ CONF_FLAG_REQUIRED
Error out if no matching CONF_PAIR is found, and no dflt value is set.
Definition cf_parse.h:466
#define FR_CONF_SUBSECTION_ALLOC(_name, _flags, _struct, _field, _subcs)
conf_parser_t which allocates a struct and populates it from a subsection
Definition cf_parse.h:384
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:649
A section grouping multiple CONF_PAIR.
Definition cf_priv.h:106
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:316
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:329
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:335
Specifies an attribute which must be present for the module to function.
Definition dict.h:315
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:328
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
@ FR_EAP_METHOD_TLS
Definition types.h:58
talloc_free(hp)
rlm_rcode_t rcode
The current rcode, from executing the instruction or merging the result from a frame.
Definition interpret.h:140
void eap_crypto_prf_label_init(eap_tls_prf_label_t *prf_label, eap_session_t *eap_session, char const *keying_prf_label, size_t keying_prf_label_len)
Initialize the PRF label fields.
Definition crypto.c:48
static eap_session_t * eap_session_get(request_t *request)
Definition session.h:85
void * opaque
Opaque data used by EAP methods.
Definition session.h:63
bool tls
Whether EAP method uses TLS.
Definition session.h:71
module_method_t process
Callback that should be used to process the next round.
Definition session.h:65
Tracks the progress of a single session of any EAP method.
Definition session.h:41
@ FR_TYPE_UINT32
32 Bit unsigned integer.
module_instance_t const * mi
Instance of the module being instantiated.
Definition module_ctx.h:42
void * thread
Thread specific instance data.
Definition module_ctx.h:43
void * rctx
Resume ctx that a module previously set.
Definition module_ctx.h:45
void * thread
Thread instance data.
Definition module_ctx.h:67
module_instance_t const * mi
Instance of the module being instantiated.
Definition module_ctx.h:64
module_instance_t * mi
Instance of the module being instantiated.
Definition module_ctx.h:51
Temporary structure to hold arguments for module calls.
Definition module_ctx.h:41
Temporary structure to hold arguments for instantiation calls.
Definition module_ctx.h:50
Temporary structure to hold arguments for thread_instantiation calls.
Definition module_ctx.h:63
fr_pair_t * fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, fr_dict_attr_t const *da)
Find the first pair with a matching da.
Definition pair.c:707
static const conf_parser_t config[]
Definition base.c:162
#define REDEBUG(fmt,...)
#define RDEBUG2(fmt,...)
static rs_t * conf
Definition radsniff.c:52
@ RLM_MODULE_INVALID
The module considers the request invalid.
Definition rcode.h:51
@ RLM_MODULE_REJECT
Immediately reject the request.
Definition rcode.h:47
#define RETURN_UNLANG_HANDLED
Definition rcode.h:71
#define RETURN_UNLANG_FAIL
Definition rcode.h:69
#define RETURN_UNLANG_OK
Definition rcode.h:70
static unlang_action_t mod_handshake_process(UNUSED unlang_result_t *p_result, UNUSED module_ctx_t const *mctx, request_t *request)
static unlang_action_t mod_handshake_resume(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
Definition rlm_eap_tls.c:76
static fr_dict_t const * dict_freeradius
Definition rlm_eap_tls.c:54
static fr_dict_attr_t const * attr_eap_tls_require_client_cert
Definition rlm_eap_tls.c:62
static unlang_action_t mod_session_init_resume(unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
fr_dict_attr_autoload_t rlm_eap_tls_dict_attr[]
Definition rlm_eap_tls.c:65
static unlang_action_t mod_handshake_done(unlang_result_t *p_result, UNUSED module_ctx_t const *mctx, UNUSED request_t *request)
Definition rlm_eap_tls.c:70
fr_dict_autoload_t rlm_eap_tls_dict[]
Definition rlm_eap_tls.c:57
static int mod_thread_instantiate(module_thread_inst_ctx_t const *mctx)
static conf_parser_t submodule_config[]
Definition rlm_eap_tls.c:45
static unlang_action_t mod_session_init(UNUSED unlang_result_t *p_result, module_ctx_t const *mctx, request_t *request)
rlm_eap_submodule_t rlm_eap_tls
static int mod_thread_detach(module_thread_inst_ctx_t const *mctx)
static int mod_instantiate(module_inst_ctx_t const *mctx)
SSL_CTX * ssl_ctx
Thread local SSL_CTX.
Definition rlm_eap_tls.c:42
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:355
size_t inst_size
Size of the module's instance data.
Definition module.h:213
void * data
Module's instance data.
Definition module.h:297
@ FR_SIGNAL_NONE
No signals.
Definition signal.h:39
unlang_action_t unlang_module_yield(request_t *request, module_method_t resume, unlang_module_signal_t signal, fr_signal_t sigmask, void *rctx)
Yield a request back to the interpreter from within a module.
Definition module.c:431
eap_aka_sim_process_conf_t * inst
fr_pair_t * vp
Stores an attribute, a value and various bits of other data.
Definition pair.h:68
module_t common
Common fields provided by all modules.
Definition submodule.h:50
Interface exported by EAP submodules.
Definition submodule.h:49
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:804
int eap_tls_success(request_t *request, eap_session_t *eap_session, eap_tls_prf_label_t *prf_label)
Send an EAP-TLS success.
Definition tls.c:267
eap_tls_session_t * eap_tls_session_init(request_t *request, eap_session_t *eap_session, SSL_CTX *ssl_ctx, bool client_cert)
Create a new fr_tls_session_t associated with an eap_session_t.
Definition tls.c:1193
void eap_tls_conf_check(CONF_SECTION *cs, fr_tls_conf_t *tls_conf)
Apply the limits that EAP puts on a parsed tls { ... } section.
Definition tls.c:1345
int eap_tls_start(request_t *request, eap_session_t *eap_session)
Send an initial EAP-TLS request to the peer.
Definition tls.c:242
unlang_action_t eap_tls_fail(request_t *request, eap_session_t *eap_session)
Send an EAP-TLS failure.
Definition tls.c:328
USES_APPLE_DEPRECATED_API fr_table_num_ordered_t const eap_tls_status_table[]
Definition tls.c:80
unlang_action_t eap_tls_process(request_t *request, eap_session_t *eap_session)
Process an EAP TLS request.
Definition tls.c:1018
@ EAP_TLS_INVALID
Invalid, don't reply.
Definition tls.h:87
@ EAP_TLS_HANDLED
TLS code has handled it.
Definition tls.h:90
@ EAP_TLS_RECORD_RECV_COMPLETE
Received final fragment of a record.
Definition tls.h:107
@ EAP_TLS_FAIL
Fail, send fail.
Definition tls.h:89
@ EAP_TLS_ESTABLISHED
Session established, send success (or start phase2).
Definition tls.h:88
eap_tls_status_t state
The state of the EAP-TLS session.
Definition tls.h:123
fr_tls_session_t * tls_session
TLS session used to authenticate peer or tunnel sensitive data.
Definition tls.h:125
bool include_length
A flag to include length in every TLS Data/Alert packet.
Definition tls.h:134
Tracks the state of an EAP-TLS session.
Definition tls.h:122