1 /*
2 * EAP peer method: EAP-TLS (RFC 2716)
3 * Copyright (c) 2004-2008, 2012, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8 #include "utils/includes.h"
9
10 #ifdef EAP_TLS
11 #include "utils/common.h"
12 #include "crypto/tls.h"
13 #include "eap_peer/eap_i.h"
14 #include "eap_peer/eap_defs.h"
15 #include "eap_peer/eap_tls_common.h"
16 #include "eap_peer/eap_config.h"
17 #include "eap_peer/eap_methods.h"
18
19
20 static void eap_tls_deinit(struct eap_sm *sm, void *priv);
21
22
23 struct eap_tls_data {
24 struct eap_ssl_data ssl;
25 u8 *key_data;
26 u8 *session_id;
27 size_t id_len;
28 void *ssl_ctx;
29 u8 eap_type;
30 };
31
32
eap_tls_init(struct eap_sm * sm)33 static void * eap_tls_init(struct eap_sm *sm)
34 {
35 struct eap_tls_data *data;
36 struct eap_peer_config *config = eap_get_config(sm);
37
38 if (config == NULL ||
39 config->private_key == 0) {
40 wpa_printf(MSG_INFO, "EAP-TLS: Private key not configured");
41 return NULL;
42 }
43
44 data = (struct eap_tls_data *)os_zalloc(sizeof(*data));
45 if (data == NULL)
46 return NULL;
47
48 data->ssl_ctx = sm->ssl_ctx;
49
50 if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_TLS)) {
51 wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
52 eap_tls_deinit(sm, data);
53 return NULL;
54 }
55
56 data->eap_type = EAP_TYPE_TLS;
57
58 return data;
59 }
60
61
eap_tls_deinit(struct eap_sm * sm,void * priv)62 static void eap_tls_deinit(struct eap_sm *sm, void *priv)
63 {
64 struct eap_tls_data *data = priv;
65 if (data == NULL)
66 return;
67 eap_peer_tls_ssl_deinit(sm, &data->ssl);
68 os_free(data->key_data);
69 os_free(data->session_id);
70 os_free(data);
71 }
72
73
eap_tls_failure(struct eap_sm * sm,struct eap_tls_data * data,struct eap_method_ret * ret,int res,struct wpabuf * resp,u8 id)74 static struct wpabuf * eap_tls_failure(struct eap_sm *sm,
75 struct eap_tls_data *data,
76 struct eap_method_ret *ret, int res,
77 struct wpabuf *resp, u8 id)
78 {
79 wpa_printf(MSG_DEBUG, "EAP-TLS: TLS processing failed");
80
81 ret->methodState = METHOD_DONE;
82 ret->decision = DECISION_FAIL;
83
84 if (res == -1) {
85 struct eap_peer_config *config = eap_get_config(sm);
86 if (config) {
87 /*
88 * The TLS handshake failed. So better forget the old
89 * PIN. It may be wrong, we cannot be sure but trying
90 * the wrong one again might block it on the card--so
91 * better ask the user again.
92 */
93 os_free(config->pin);
94 config->pin = NULL;
95 }
96 }
97
98 if (resp) {
99 /*
100 * This is likely an alert message, so send it instead of just
101 * ACKing the error.
102 */
103 return resp;
104 }
105
106 return eap_peer_tls_build_ack(id, data->eap_type, 0);
107 }
108
109
eap_tls_success(struct eap_sm * sm,struct eap_tls_data * data,struct eap_method_ret * ret)110 static void eap_tls_success(struct eap_sm *sm, struct eap_tls_data *data,
111 struct eap_method_ret *ret)
112 {
113 wpa_printf(MSG_DEBUG, "EAP-TLS: Done");
114
115 ret->methodState = METHOD_DONE;
116 ret->decision = DECISION_UNCOND_SUCC;
117
118 os_free(data->key_data);
119 data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
120 "client EAP encryption",
121 EAP_TLS_KEY_LEN +
122 EAP_EMSK_LEN);
123 if (data->key_data) {
124 wpa_hexdump_key(MSG_DEBUG, "EAP-TLS: Derived key",
125 data->key_data, EAP_TLS_KEY_LEN);
126 wpa_hexdump_key(MSG_DEBUG, "EAP-TLS: Derived EMSK",
127 data->key_data + EAP_TLS_KEY_LEN,
128 EAP_EMSK_LEN);
129 } else {
130 wpa_printf(MSG_INFO, "EAP-TLS: Failed to derive key");
131 }
132
133 os_free(data->session_id);
134 data->session_id = eap_peer_tls_derive_session_id(sm, &data->ssl,
135 EAP_TYPE_TLS,
136 &data->id_len);
137 if (data->session_id) {
138 wpa_hexdump(MSG_DEBUG, "EAP-TLS: Derived Session-Id",
139 data->session_id, data->id_len);
140 } else {
141 wpa_printf(MSG_ERROR, "EAP-TLS: Failed to derive Session-Id");
142 }
143 }
144
145
eap_tls_process(struct eap_sm * sm,void * priv,struct eap_method_ret * ret,const struct wpabuf * reqData)146 static struct wpabuf * eap_tls_process(struct eap_sm *sm, void *priv,
147 struct eap_method_ret *ret,
148 const struct wpabuf *reqData)
149 {
150 size_t left;
151 int res;
152 struct wpabuf *resp;
153 u8 flags, id;
154 const u8 *pos;
155 struct eap_tls_data *data = priv;
156
157 pos = eap_peer_tls_process_init(sm, &data->ssl, data->eap_type, ret,
158 reqData, &left, &flags);
159 if (pos == NULL)
160 return NULL;
161 id = eap_get_id(reqData);
162
163 if (flags & EAP_TLS_FLAGS_START) {
164 wpa_printf(MSG_DEBUG, "EAP-TLS: Start");
165 left = 0; /* make sure that this frame is empty, even though it
166 * should always be, anyway */
167 }
168
169 resp = NULL;
170 res = eap_peer_tls_process_helper(sm, &data->ssl, data->eap_type, 0,
171 id, pos, left, &resp);
172
173 if (res < 0) {
174 return eap_tls_failure(sm, data, ret, res, resp, id);
175 }
176
177 if (tls_connection_established(data->ssl_ctx, data->ssl.conn))
178 eap_tls_success(sm, data, ret);
179
180 if (res == 1) {
181 wpabuf_free(resp);
182 return eap_peer_tls_build_ack(id, data->eap_type, 0);
183 }
184
185 return resp;
186 }
187
eap_tls_isKeyAvailable(struct eap_sm * sm,void * priv)188 static bool eap_tls_isKeyAvailable(struct eap_sm *sm, void *priv)
189 {
190 struct eap_tls_data *data = priv;
191
192 return data->key_data != NULL;
193 }
eap_tls_getKey(struct eap_sm * sm,void * priv,size_t * len)194 static u8 * eap_tls_getKey(struct eap_sm *sm, void *priv, size_t *len)
195 {
196 struct eap_tls_data *data = priv;
197 u8 *key;
198
199 if (data->key_data == NULL)
200 return NULL;
201
202 key = os_malloc(EAP_TLS_KEY_LEN);
203 if (key == NULL)
204 return NULL;
205
206 *len = EAP_TLS_KEY_LEN;
207 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
208
209 return key;
210 }
211
eap_peer_tls_register(void)212 int eap_peer_tls_register(void)
213 {
214 struct eap_method *eap;
215 int ret;
216
217 eap = eap_peer_method_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLS,
218 "TLS");
219
220 if (eap == NULL)
221 return -1;
222
223 eap->init = eap_tls_init;
224 eap->deinit = eap_tls_deinit;
225 eap->process = eap_tls_process;
226 eap->isKeyAvailable = eap_tls_isKeyAvailable;
227 eap->getKey = eap_tls_getKey;
228
229 ret = eap_peer_method_register(eap);
230 if (ret)
231 eap_peer_method_free(eap);
232 return ret;
233 }
234
235
236 #endif /* EAP_TLS */
237