Re-initialize hostapd/wpa_supplicant git repository based on 0.6.3 release
[mech_eap.git] / src / eap_peer / eap_tls.c
1 /*
2  * EAP peer method: EAP-TLS (RFC 2716)
3  * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eap_i.h"
19 #include "eap_tls_common.h"
20 #include "eap_config.h"
21 #include "tls.h"
22
23
24 static void eap_tls_deinit(struct eap_sm *sm, void *priv);
25
26
27 struct eap_tls_data {
28         struct eap_ssl_data ssl;
29         u8 *key_data;
30 };
31
32
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         if (config == NULL ||
38             ((sm->init_phase2 ? config->private_key2 : config->private_key)
39             == NULL && config->engine == 0)) {
40                 wpa_printf(MSG_INFO, "EAP-TLS: Private key not configured");
41                 return NULL;
42         }
43
44         data = os_zalloc(sizeof(*data));
45         if (data == NULL)
46                 return NULL;
47
48         if (eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
49                 wpa_printf(MSG_INFO, "EAP-TLS: Failed to initialize SSL.");
50                 eap_tls_deinit(sm, data);
51                 if (config->engine) {
52                         wpa_printf(MSG_DEBUG, "EAP-TLS: Requesting Smartcard "
53                                    "PIN");
54                         eap_sm_request_pin(sm);
55                         sm->ignore = TRUE;
56                 } else if (config->private_key && !config->private_key_passwd)
57                 {
58                         wpa_printf(MSG_DEBUG, "EAP-TLS: Requesting private "
59                                    "key passphrase");
60                         eap_sm_request_passphrase(sm);
61                         sm->ignore = TRUE;
62                 }
63                 return NULL;
64         }
65
66         return data;
67 }
68
69
70 static void eap_tls_deinit(struct eap_sm *sm, void *priv)
71 {
72         struct eap_tls_data *data = priv;
73         if (data == NULL)
74                 return;
75         eap_peer_tls_ssl_deinit(sm, &data->ssl);
76         os_free(data->key_data);
77         os_free(data);
78 }
79
80
81 static struct wpabuf * eap_tls_failure(struct eap_sm *sm,
82                                        struct eap_tls_data *data,
83                                        struct eap_method_ret *ret, int res,
84                                        struct wpabuf *resp, u8 id)
85 {
86         wpa_printf(MSG_DEBUG, "EAP-TLS: TLS processing failed");
87
88         ret->methodState = METHOD_DONE;
89         ret->decision = DECISION_FAIL;
90
91         if (res == -1) {
92                 struct eap_peer_config *config = eap_get_config(sm);
93                 if (config) {
94                         /*
95                          * The TLS handshake failed. So better forget the old
96                          * PIN. It may be wrong, we cannot be sure but trying
97                          * the wrong one again might block it on the card--so
98                          * better ask the user again.
99                          */
100                         os_free(config->pin);
101                         config->pin = NULL;
102                 }
103         }
104
105         if (resp) {
106                 /*
107                  * This is likely an alert message, so send it instead of just
108                  * ACKing the error.
109                  */
110                 return resp;
111         }
112
113         return eap_peer_tls_build_ack(id, EAP_TYPE_TLS, 0);
114 }
115
116
117 static void eap_tls_success(struct eap_sm *sm, struct eap_tls_data *data,
118                             struct eap_method_ret *ret)
119 {
120         wpa_printf(MSG_DEBUG, "EAP-TLS: Done");
121
122         ret->methodState = METHOD_DONE;
123         ret->decision = DECISION_UNCOND_SUCC;
124
125         os_free(data->key_data);
126         data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
127                                                  "client EAP encryption",
128                                                  EAP_TLS_KEY_LEN +
129                                                  EAP_EMSK_LEN);
130         if (data->key_data) {
131                 wpa_hexdump_key(MSG_DEBUG, "EAP-TLS: Derived key",
132                                 data->key_data, EAP_TLS_KEY_LEN);
133                 wpa_hexdump_key(MSG_DEBUG, "EAP-TLS: Derived EMSK",
134                                 data->key_data + EAP_TLS_KEY_LEN,
135                                 EAP_EMSK_LEN);
136         } else {
137                 wpa_printf(MSG_INFO, "EAP-TLS: Failed to derive key");
138         }
139 }
140
141
142 static struct wpabuf * eap_tls_process(struct eap_sm *sm, void *priv,
143                                        struct eap_method_ret *ret,
144                                        const struct wpabuf *reqData)
145 {
146         size_t left;
147         int res;
148         struct wpabuf *resp;
149         u8 flags, id;
150         const u8 *pos;
151         struct eap_tls_data *data = priv;
152
153         pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TLS, ret,
154                                         reqData, &left, &flags);
155         if (pos == NULL)
156                 return NULL;
157         id = eap_get_id(reqData);
158
159         if (flags & EAP_TLS_FLAGS_START) {
160                 wpa_printf(MSG_DEBUG, "EAP-TLS: Start");
161                 left = 0; /* make sure that this frame is empty, even though it
162                            * should always be, anyway */
163         }
164
165         resp = NULL;
166         res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TLS, 0, id,
167                                           pos, left, &resp);
168
169         if (res < 0) {
170                 return eap_tls_failure(sm, data, ret, res, resp, id);
171         }
172
173         if (tls_connection_established(sm->ssl_ctx, data->ssl.conn))
174                 eap_tls_success(sm, data, ret);
175
176         if (res == 1) {
177                 wpabuf_free(resp);
178                 return eap_peer_tls_build_ack(id, EAP_TYPE_TLS, 0);
179         }
180
181         return resp;
182 }
183
184
185 static Boolean eap_tls_has_reauth_data(struct eap_sm *sm, void *priv)
186 {
187         struct eap_tls_data *data = priv;
188         return tls_connection_established(sm->ssl_ctx, data->ssl.conn);
189 }
190
191
192 static void eap_tls_deinit_for_reauth(struct eap_sm *sm, void *priv)
193 {
194 }
195
196
197 static void * eap_tls_init_for_reauth(struct eap_sm *sm, void *priv)
198 {
199         struct eap_tls_data *data = priv;
200         os_free(data->key_data);
201         data->key_data = NULL;
202         if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
203                 os_free(data);
204                 return NULL;
205         }
206         return priv;
207 }
208
209
210 static int eap_tls_get_status(struct eap_sm *sm, void *priv, char *buf,
211                               size_t buflen, int verbose)
212 {
213         struct eap_tls_data *data = priv;
214         return eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
215 }
216
217
218 static Boolean eap_tls_isKeyAvailable(struct eap_sm *sm, void *priv)
219 {
220         struct eap_tls_data *data = priv;
221         return data->key_data != NULL;
222 }
223
224
225 static u8 * eap_tls_getKey(struct eap_sm *sm, void *priv, size_t *len)
226 {
227         struct eap_tls_data *data = priv;
228         u8 *key;
229
230         if (data->key_data == NULL)
231                 return NULL;
232
233         key = os_malloc(EAP_TLS_KEY_LEN);
234         if (key == NULL)
235                 return NULL;
236
237         *len = EAP_TLS_KEY_LEN;
238         os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
239
240         return key;
241 }
242
243
244 static u8 * eap_tls_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
245 {
246         struct eap_tls_data *data = priv;
247         u8 *key;
248
249         if (data->key_data == NULL)
250                 return NULL;
251
252         key = os_malloc(EAP_EMSK_LEN);
253         if (key == NULL)
254                 return NULL;
255
256         *len = EAP_EMSK_LEN;
257         os_memcpy(key, data->key_data + EAP_TLS_KEY_LEN, EAP_EMSK_LEN);
258
259         return key;
260 }
261
262
263 int eap_peer_tls_register(void)
264 {
265         struct eap_method *eap;
266         int ret;
267
268         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
269                                     EAP_VENDOR_IETF, EAP_TYPE_TLS, "TLS");
270         if (eap == NULL)
271                 return -1;
272
273         eap->init = eap_tls_init;
274         eap->deinit = eap_tls_deinit;
275         eap->process = eap_tls_process;
276         eap->isKeyAvailable = eap_tls_isKeyAvailable;
277         eap->getKey = eap_tls_getKey;
278         eap->get_status = eap_tls_get_status;
279         eap->has_reauth_data = eap_tls_has_reauth_data;
280         eap->deinit_for_reauth = eap_tls_deinit_for_reauth;
281         eap->init_for_reauth = eap_tls_init_for_reauth;
282         eap->get_emsk = eap_tls_get_emsk;
283
284         ret = eap_peer_method_register(eap);
285         if (ret)
286                 eap_peer_method_free(eap);
287         return ret;
288 }