Sync usage with man page.
[netbsd-mini2440.git] / dist / wpa / hostapd / peerkey.c
blob26097b723a7bb8cd61fec46d6fed3e1a2c8afec4
1 /*
2 * hostapd - PeerKey for Direct Link Setup (DLS)
3 * Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "eloop.h"
19 #include "sha1.h"
20 #include "wpa.h"
21 #include "defs.h"
22 #include "wpa_auth_i.h"
23 #include "wpa_auth_ie.h"
25 #ifdef CONFIG_PEERKEY
27 static void wpa_stsl_step(void *eloop_ctx, void *timeout_ctx)
29 #if 0
30 struct wpa_authenticator *wpa_auth = eloop_ctx;
31 struct wpa_stsl_negotiation *neg = timeout_ctx;
32 #endif
34 /* TODO: ? */
38 struct wpa_stsl_search {
39 const u8 *addr;
40 struct wpa_state_machine *sm;
44 static int wpa_stsl_select_sta(struct wpa_state_machine *sm, void *ctx)
46 struct wpa_stsl_search *search = ctx;
47 if (os_memcmp(search->addr, sm->addr, ETH_ALEN) == 0) {
48 search->sm = sm;
49 return 1;
51 return 0;
55 static void wpa_smk_send_error(struct wpa_authenticator *wpa_auth,
56 struct wpa_state_machine *sm, const u8 *peer,
57 u16 mui, u16 error_type)
59 u8 kde[2 + RSN_SELECTOR_LEN + ETH_ALEN +
60 2 + RSN_SELECTOR_LEN + sizeof(struct rsn_error_kde)];
61 u8 *pos;
62 struct rsn_error_kde error;
64 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
65 "Sending SMK Error");
67 pos = kde;
69 if (peer) {
70 pos = wpa_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, peer, ETH_ALEN,
71 NULL, 0);
74 error.mui = host_to_be16(mui);
75 error.error_type = host_to_be16(error_type);
76 pos = wpa_add_kde(pos, RSN_KEY_DATA_ERROR,
77 (u8 *) &error, sizeof(error), NULL, 0);
79 __wpa_send_eapol(wpa_auth, sm,
80 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
81 WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_ERROR,
82 NULL, NULL, kde, pos - kde, 0, 0, 0);
86 void wpa_smk_m1(struct wpa_authenticator *wpa_auth,
87 struct wpa_state_machine *sm, struct wpa_eapol_key *key)
89 struct wpa_eapol_ie_parse kde;
90 struct wpa_stsl_search search;
91 u8 *buf, *pos;
92 size_t buf_len;
94 if (wpa_parse_kde_ies((const u8 *) (key + 1),
95 WPA_GET_BE16(key->key_data_length), &kde) < 0) {
96 wpa_printf(MSG_INFO, "RSN: Failed to parse KDEs in SMK M1");
97 return;
100 if (kde.rsn_ie == NULL || kde.mac_addr == NULL ||
101 kde.mac_addr_len < ETH_ALEN) {
102 wpa_printf(MSG_INFO, "RSN: No RSN IE or MAC address KDE in "
103 "SMK M1");
104 return;
107 /* Initiator = sm->addr; Peer = kde.mac_addr */
109 search.addr = kde.mac_addr;
110 search.sm = NULL;
111 if (wpa_auth_for_each_sta(wpa_auth, wpa_stsl_select_sta, &search) ==
112 0 || search.sm == NULL) {
113 wpa_printf(MSG_DEBUG, "RSN: SMK handshake with " MACSTR
114 " aborted - STA not associated anymore",
115 MAC2STR(kde.mac_addr));
116 wpa_smk_send_error(wpa_auth, sm, kde.mac_addr, STK_MUI_SMK,
117 STK_ERR_STA_NR);
118 /* FIX: wpa_stsl_remove(wpa_auth, neg); */
119 return;
122 buf_len = kde.rsn_ie_len + 2 + RSN_SELECTOR_LEN + ETH_ALEN;
123 buf = os_malloc(buf_len);
124 if (buf == NULL)
125 return;
126 /* Initiator RSN IE */
127 os_memcpy(buf, kde.rsn_ie, kde.rsn_ie_len);
128 pos = buf + kde.rsn_ie_len;
129 /* Initiator MAC Address */
130 pos = wpa_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, sm->addr, ETH_ALEN,
131 NULL, 0);
133 /* SMK M2:
134 * EAPOL-Key(S=1, M=1, A=1, I=0, K=0, SM=1, KeyRSC=0, Nonce=INonce,
135 * MIC=MIC, DataKDs=(RSNIE_I, MAC_I KDE)
138 wpa_auth_logger(wpa_auth, search.sm->addr, LOGGER_DEBUG,
139 "Sending SMK M2");
141 __wpa_send_eapol(wpa_auth, search.sm,
142 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
143 WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE,
144 NULL, key->key_nonce, buf, pos - buf, 0, 0, 0);
146 os_free(buf);
150 static void wpa_send_smk_m4(struct wpa_authenticator *wpa_auth,
151 struct wpa_state_machine *sm,
152 struct wpa_eapol_key *key,
153 struct wpa_eapol_ie_parse *kde,
154 const u8 *smk)
156 u8 *buf, *pos;
157 size_t buf_len;
158 u32 lifetime;
160 /* SMK M4:
161 * EAPOL-Key(S=1, M=1, A=0, I=1, K=0, SM=1, KeyRSC=0, Nonce=PNonce,
162 * MIC=MIC, DataKDs=(MAC_I KDE, INonce KDE, SMK KDE,
163 * Lifetime KDE)
166 buf_len = 2 + RSN_SELECTOR_LEN + ETH_ALEN +
167 2 + RSN_SELECTOR_LEN + WPA_NONCE_LEN +
168 2 + RSN_SELECTOR_LEN + PMK_LEN + WPA_NONCE_LEN +
169 2 + RSN_SELECTOR_LEN + sizeof(lifetime);
170 pos = buf = os_malloc(buf_len);
171 if (buf == NULL)
172 return;
174 /* Initiator MAC Address */
175 pos = wpa_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, kde->mac_addr, ETH_ALEN,
176 NULL, 0);
178 /* Initiator Nonce */
179 pos = wpa_add_kde(pos, RSN_KEY_DATA_NONCE, kde->nonce, WPA_NONCE_LEN,
180 NULL, 0);
182 /* SMK with PNonce */
183 pos = wpa_add_kde(pos, RSN_KEY_DATA_SMK, smk, PMK_LEN,
184 key->key_nonce, WPA_NONCE_LEN);
186 /* Lifetime */
187 lifetime = htonl(43200); /* dot11RSNAConfigSMKLifetime */
188 pos = wpa_add_kde(pos, RSN_KEY_DATA_LIFETIME,
189 (u8 *) &lifetime, sizeof(lifetime), NULL, 0);
191 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
192 "Sending SMK M4");
194 __wpa_send_eapol(wpa_auth, sm,
195 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
196 WPA_KEY_INFO_INSTALL | WPA_KEY_INFO_SMK_MESSAGE,
197 NULL, key->key_nonce, buf, pos - buf, 0, 1, 0);
199 os_free(buf);
203 static void wpa_send_smk_m5(struct wpa_authenticator *wpa_auth,
204 struct wpa_state_machine *sm,
205 struct wpa_eapol_key *key,
206 struct wpa_eapol_ie_parse *kde,
207 const u8 *smk, const u8 *peer)
209 u8 *buf, *pos;
210 size_t buf_len;
211 u32 lifetime;
213 /* SMK M5:
214 * EAPOL-Key(S=1, M=1, A=0, I=0, K=0, SM=1, KeyRSC=0, Nonce=INonce,
215 * MIC=MIC, DataKDs=(RSNIE_P, MAC_P KDE, PNonce, SMK KDE,
216 * Lifetime KDE))
219 buf_len = kde->rsn_ie_len +
220 2 + RSN_SELECTOR_LEN + ETH_ALEN +
221 2 + RSN_SELECTOR_LEN + WPA_NONCE_LEN +
222 2 + RSN_SELECTOR_LEN + PMK_LEN + WPA_NONCE_LEN +
223 2 + RSN_SELECTOR_LEN + sizeof(lifetime);
224 pos = buf = os_malloc(buf_len);
225 if (buf == NULL)
226 return;
228 /* Peer RSN IE */
229 os_memcpy(buf, kde->rsn_ie, kde->rsn_ie_len);
230 pos = buf + kde->rsn_ie_len;
232 /* Peer MAC Address */
233 pos = wpa_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, peer, ETH_ALEN, NULL, 0);
235 /* PNonce */
236 pos = wpa_add_kde(pos, RSN_KEY_DATA_NONCE, key->key_nonce,
237 WPA_NONCE_LEN, NULL, 0);
239 /* SMK and INonce */
240 pos = wpa_add_kde(pos, RSN_KEY_DATA_SMK, smk, PMK_LEN,
241 kde->nonce, WPA_NONCE_LEN);
243 /* Lifetime */
244 lifetime = htonl(43200); /* dot11RSNAConfigSMKLifetime */
245 pos = wpa_add_kde(pos, RSN_KEY_DATA_LIFETIME,
246 (u8 *) &lifetime, sizeof(lifetime), NULL, 0);
248 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
249 "Sending SMK M5");
251 __wpa_send_eapol(wpa_auth, sm,
252 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
253 WPA_KEY_INFO_SMK_MESSAGE,
254 NULL, kde->nonce, buf, pos - buf, 0, 1, 0);
256 os_free(buf);
260 void wpa_smk_m3(struct wpa_authenticator *wpa_auth,
261 struct wpa_state_machine *sm, struct wpa_eapol_key *key)
263 struct wpa_eapol_ie_parse kde;
264 struct wpa_stsl_search search;
265 u8 smk[32], buf[ETH_ALEN + 8 + 2 * WPA_NONCE_LEN], *pos;
267 if (wpa_parse_kde_ies((const u8 *) (key + 1),
268 WPA_GET_BE16(key->key_data_length), &kde) < 0) {
269 wpa_printf(MSG_INFO, "RSN: Failed to parse KDEs in SMK M3");
270 return;
273 if (kde.rsn_ie == NULL ||
274 kde.mac_addr == NULL || kde.mac_addr_len < ETH_ALEN ||
275 kde.nonce == NULL || kde.nonce_len < WPA_NONCE_LEN) {
276 wpa_printf(MSG_INFO, "RSN: No RSN IE, MAC address KDE, or "
277 "Nonce KDE in SMK M3");
278 return;
281 /* Peer = sm->addr; Initiator = kde.mac_addr;
282 * Peer Nonce = key->key_nonce; Initiator Nonce = kde.nonce */
284 search.addr = kde.mac_addr;
285 search.sm = NULL;
286 if (wpa_auth_for_each_sta(wpa_auth, wpa_stsl_select_sta, &search) ==
287 0 || search.sm == NULL) {
288 wpa_printf(MSG_DEBUG, "RSN: SMK handshake with " MACSTR
289 " aborted - STA not associated anymore",
290 MAC2STR(kde.mac_addr));
291 wpa_smk_send_error(wpa_auth, sm, kde.mac_addr, STK_MUI_SMK,
292 STK_ERR_STA_NR);
293 /* FIX: wpa_stsl_remove(wpa_auth, neg); */
294 return;
297 if (os_get_random(smk, PMK_LEN)) {
298 wpa_printf(MSG_DEBUG, "RSN: Failed to generate SMK");
299 return;
302 /* SMK = PRF-256(Random number, "SMK Derivation",
303 * AA || Time || INonce || PNonce)
305 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
306 pos = buf + ETH_ALEN;
307 wpa_get_ntp_timestamp(pos);
308 pos += 8;
309 os_memcpy(pos, kde.nonce, WPA_NONCE_LEN);
310 pos += WPA_NONCE_LEN;
311 os_memcpy(pos, key->key_nonce, WPA_NONCE_LEN);
312 sha1_prf(smk, PMK_LEN, "SMK Derivation", buf, sizeof(buf),
313 smk, PMK_LEN);
315 wpa_hexdump_key(MSG_DEBUG, "RSN: SMK", smk, PMK_LEN);
317 wpa_send_smk_m4(wpa_auth, sm, key, &kde, smk);
318 wpa_send_smk_m5(wpa_auth, search.sm, key, &kde, smk, sm->addr);
320 /* Authenticator does not need SMK anymore and it is required to forget
321 * it. */
322 os_memset(smk, 0, sizeof(*smk));
326 void wpa_smk_error(struct wpa_authenticator *wpa_auth,
327 struct wpa_state_machine *sm, struct wpa_eapol_key *key)
329 struct wpa_eapol_ie_parse kde;
330 struct wpa_stsl_search search;
331 struct rsn_error_kde error;
332 u16 mui, error_type;
334 if (wpa_parse_kde_ies((const u8 *) (key + 1),
335 WPA_GET_BE16(key->key_data_length), &kde) < 0) {
336 wpa_printf(MSG_INFO, "RSN: Failed to parse KDEs in SMK Error");
337 return;
340 if (kde.mac_addr == NULL || kde.mac_addr_len < ETH_ALEN ||
341 kde.error == NULL || kde.error_len < sizeof(error)) {
342 wpa_printf(MSG_INFO, "RSN: No MAC address or Error KDE in "
343 "SMK Error");
344 return;
347 search.addr = kde.mac_addr;
348 search.sm = NULL;
349 if (wpa_auth_for_each_sta(wpa_auth, wpa_stsl_select_sta, &search) ==
350 0 || search.sm == NULL) {
351 wpa_printf(MSG_DEBUG, "RSN: Peer STA " MACSTR " not "
352 "associated for SMK Error message from " MACSTR,
353 MAC2STR(kde.mac_addr), MAC2STR(sm->addr));
354 return;
357 os_memcpy(&error, kde.error, sizeof(error));
358 mui = be_to_host16(error.mui);
359 error_type = be_to_host16(error.error_type);
360 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
361 "STA reported SMK Error: Peer " MACSTR
362 " MUI %d Error Type %d",
363 MAC2STR(kde.mac_addr), mui, error_type);
365 wpa_smk_send_error(wpa_auth, search.sm, sm->addr, mui, error_type);
369 int wpa_stsl_remove(struct wpa_authenticator *wpa_auth,
370 struct wpa_stsl_negotiation *neg)
372 struct wpa_stsl_negotiation *pos, *prev;
374 if (wpa_auth == NULL)
375 return -1;
376 pos = wpa_auth->stsl_negotiations;
377 prev = NULL;
378 while (pos) {
379 if (pos == neg) {
380 if (prev)
381 prev->next = pos->next;
382 else
383 wpa_auth->stsl_negotiations = pos->next;
385 eloop_cancel_timeout(wpa_stsl_step, wpa_auth, pos);
386 os_free(pos);
387 return 0;
389 prev = pos;
390 pos = pos->next;
393 return -1;
396 #endif /* CONFIG_PEERKEY */