~ubuntu-branches/ubuntu/gutsy/wpasupplicant/gutsy

« back to all changes in this revision

Viewing changes to src/eap_server/eap_gpsk.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler, Alexander Sack
  • Date: 2007-08-26 16:06:57 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20070826160657-2m8pxoweuxe8f93t
Tags: 0.6.0+0.5.8-0ubuntu1
* New upstream release
* remove patch 11_erroneous_manpage_ref, applied upstream
* remove patch 25_wpas_dbus_unregister_iface_fix, applied upstream

[ Alexander Sack ]
* bumping upstream version to replace development version 0.6.0 with
  this package from stable release branch.
* attempt to fix wierd timeout and high latency issues by going
  back to stable upstream version (0.5.9) (LP: #140763,
  LP: #141233).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * hostapd / EAP-GPSK (draft-ietf-emu-eap-gpsk-04.txt) server
3
 
 * Copyright (c) 2006-2007, 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_server/eap_i.h"
19
 
#include "eap_common/eap_gpsk_common.h"
20
 
 
21
 
 
22
 
struct eap_gpsk_data {
23
 
        enum { GPSK_1, GPSK_3, SUCCESS, FAILURE } state;
24
 
        u8 rand_server[EAP_GPSK_RAND_LEN];
25
 
        u8 rand_peer[EAP_GPSK_RAND_LEN];
26
 
        u8 msk[EAP_MSK_LEN];
27
 
        u8 emsk[EAP_EMSK_LEN];
28
 
        u8 sk[EAP_GPSK_MAX_SK_LEN];
29
 
        size_t sk_len;
30
 
        u8 pk[EAP_GPSK_MAX_PK_LEN];
31
 
        size_t pk_len;
32
 
        u8 *id_peer;
33
 
        size_t id_peer_len;
34
 
        u8 *id_server;
35
 
        size_t id_server_len;
36
 
#define MAX_NUM_CSUITES 2
37
 
        struct eap_gpsk_csuite csuite_list[MAX_NUM_CSUITES];
38
 
        size_t csuite_count;
39
 
        int vendor; /* CSuite/Vendor */
40
 
        int specifier; /* CSuite/Specifier */
41
 
};
42
 
 
43
 
 
44
 
static const char * eap_gpsk_state_txt(int state)
45
 
{
46
 
        switch (state) {
47
 
        case GPSK_1:
48
 
                return "GPSK-1";
49
 
        case GPSK_3:
50
 
                return "GPSK-3";
51
 
        case SUCCESS:
52
 
                return "SUCCESS";
53
 
        case FAILURE:
54
 
                return "FAILURE";
55
 
        default:
56
 
                return "?";
57
 
        }
58
 
}
59
 
 
60
 
 
61
 
static void eap_gpsk_state(struct eap_gpsk_data *data, int state)
62
 
{
63
 
        wpa_printf(MSG_DEBUG, "EAP-GPSK: %s -> %s",
64
 
                   eap_gpsk_state_txt(data->state),
65
 
                   eap_gpsk_state_txt(state));
66
 
        data->state = state;
67
 
}
68
 
 
69
 
 
70
 
static void * eap_gpsk_init(struct eap_sm *sm)
71
 
{
72
 
        struct eap_gpsk_data *data;
73
 
 
74
 
        data = os_zalloc(sizeof(*data));
75
 
        if (data == NULL)
76
 
                return NULL;
77
 
        data->state = GPSK_1;
78
 
 
79
 
        /* TODO: add support for configuring ID_Server */
80
 
        data->id_server = (u8 *) strdup("hostapd");
81
 
        if (data->id_server)
82
 
                data->id_server_len = strlen((char *) data->id_server);
83
 
 
84
 
        data->csuite_count = 0;
85
 
        if (eap_gpsk_supported_ciphersuite(EAP_GPSK_VENDOR_IETF,
86
 
                                           EAP_GPSK_CIPHER_AES)) {
87
 
                WPA_PUT_BE24(data->csuite_list[data->csuite_count].vendor,
88
 
                             EAP_GPSK_VENDOR_IETF);
89
 
                WPA_PUT_BE24(data->csuite_list[data->csuite_count].specifier,
90
 
                             EAP_GPSK_CIPHER_AES);
91
 
                data->csuite_count++;
92
 
        }
93
 
        if (eap_gpsk_supported_ciphersuite(EAP_GPSK_VENDOR_IETF,
94
 
                                           EAP_GPSK_CIPHER_SHA256)) {
95
 
                WPA_PUT_BE24(data->csuite_list[data->csuite_count].vendor,
96
 
                             EAP_GPSK_VENDOR_IETF);
97
 
                WPA_PUT_BE24(data->csuite_list[data->csuite_count].specifier,
98
 
                             EAP_GPSK_CIPHER_SHA256);
99
 
                data->csuite_count++;
100
 
        }
101
 
 
102
 
        return data;
103
 
}
104
 
 
105
 
 
106
 
static void eap_gpsk_reset(struct eap_sm *sm, void *priv)
107
 
{
108
 
        struct eap_gpsk_data *data = priv;
109
 
        free(data->id_server);
110
 
        free(data->id_peer);
111
 
        free(data);
112
 
}
113
 
 
114
 
 
115
 
static u8 * eap_gpsk_build_gpsk_1(struct eap_sm *sm,
116
 
                                  struct eap_gpsk_data *data,
117
 
                                  int id, size_t *reqDataLen)
118
 
{
119
 
        u8 *pos;
120
 
        size_t len;
121
 
        struct eap_hdr *req;
122
 
 
123
 
        wpa_printf(MSG_DEBUG, "EAP-GPSK: Request/GPSK-1");
124
 
 
125
 
        if (hostapd_get_rand(data->rand_server, EAP_GPSK_RAND_LEN)) {
126
 
                wpa_printf(MSG_ERROR, "EAP-GPSK: Failed to get random data");
127
 
                eap_gpsk_state(data, FAILURE);
128
 
                return NULL;
129
 
        }
130
 
        wpa_hexdump(MSG_MSGDUMP, "EAP-GPSK: RAND_Server",
131
 
                    data->rand_server, EAP_GPSK_RAND_LEN);
132
 
 
133
 
        len = 1 + 2 + data->id_server_len + EAP_GPSK_RAND_LEN + 2 +
134
 
                data->csuite_count * sizeof(struct eap_gpsk_csuite);
135
 
        req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_GPSK, reqDataLen,
136
 
                            len, EAP_CODE_REQUEST, id, &pos);
137
 
        if (req == NULL) {
138
 
                wpa_printf(MSG_ERROR, "EAP-GPSK: Failed to allocate memory "
139
 
                           "for request/GPSK-1");
140
 
                eap_gpsk_state(data, FAILURE);
141
 
                return NULL;
142
 
        }
143
 
 
144
 
        *pos++ = EAP_GPSK_OPCODE_GPSK_1;
145
 
 
146
 
        WPA_PUT_BE16(pos, data->id_server_len);
147
 
        pos += 2;
148
 
        if (data->id_server)
149
 
                memcpy(pos, data->id_server, data->id_server_len);
150
 
        pos += data->id_server_len;
151
 
 
152
 
        memcpy(pos, data->rand_server, EAP_GPSK_RAND_LEN);
153
 
        pos += EAP_GPSK_RAND_LEN;
154
 
 
155
 
        WPA_PUT_BE16(pos, data->csuite_count * sizeof(struct eap_gpsk_csuite));
156
 
        pos += 2;
157
 
        memcpy(pos, data->csuite_list,
158
 
               data->csuite_count * sizeof(struct eap_gpsk_csuite));
159
 
 
160
 
        return (u8 *) req;
161
 
}
162
 
 
163
 
 
164
 
static u8 * eap_gpsk_build_gpsk_3(struct eap_sm *sm,
165
 
                                  struct eap_gpsk_data *data,
166
 
                                  int id, size_t *reqDataLen)
167
 
{
168
 
        u8 *pos, *start;
169
 
        size_t len, miclen;
170
 
        struct eap_gpsk_csuite *csuite;
171
 
        struct eap_hdr *req;
172
 
 
173
 
        wpa_printf(MSG_DEBUG, "EAP-GPSK: Request/GPSK-3");
174
 
 
175
 
        miclen = eap_gpsk_mic_len(data->vendor, data->specifier);
176
 
        len = 1 + 2 * EAP_GPSK_RAND_LEN + sizeof(struct eap_gpsk_csuite) + 2 +
177
 
                miclen;
178
 
        req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_GPSK, reqDataLen,
179
 
                            len, EAP_CODE_REQUEST, id, &pos);
180
 
        if (req == NULL) {
181
 
                wpa_printf(MSG_ERROR, "EAP-GPSK: Failed to allocate memory "
182
 
                           "for request/GPSK-3");
183
 
                eap_gpsk_state(data, FAILURE);
184
 
                return NULL;
185
 
        }
186
 
 
187
 
        *pos++ = EAP_GPSK_OPCODE_GPSK_3;
188
 
        start = pos;
189
 
 
190
 
        memcpy(pos, data->rand_peer, EAP_GPSK_RAND_LEN);
191
 
        pos += EAP_GPSK_RAND_LEN;
192
 
        memcpy(pos, data->rand_server, EAP_GPSK_RAND_LEN);
193
 
        pos += EAP_GPSK_RAND_LEN;
194
 
        csuite = (struct eap_gpsk_csuite *) pos;
195
 
        WPA_PUT_BE24(csuite->vendor, data->vendor);
196
 
        WPA_PUT_BE24(csuite->specifier, data->specifier);
197
 
        pos += sizeof(*csuite);
198
 
 
199
 
        /* no PD_Payload_2 */
200
 
        WPA_PUT_BE16(pos, 0);
201
 
        pos += 2;
202
 
 
203
 
        if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
204
 
                                 data->specifier, start, pos - start, pos) < 0)
205
 
        {
206
 
                free(req);
207
 
                eap_gpsk_state(data, FAILURE);
208
 
                return NULL;
209
 
        }
210
 
 
211
 
        return (u8 *) req;
212
 
}
213
 
 
214
 
 
215
 
static u8 * eap_gpsk_buildReq(struct eap_sm *sm, void *priv, int id,
216
 
                              size_t *reqDataLen)
217
 
{
218
 
        struct eap_gpsk_data *data = priv;
219
 
 
220
 
        switch (data->state) {
221
 
        case GPSK_1:
222
 
                return eap_gpsk_build_gpsk_1(sm, data, id, reqDataLen);
223
 
        case GPSK_3:
224
 
                return eap_gpsk_build_gpsk_3(sm, data, id, reqDataLen);
225
 
        default:
226
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Unknown state %d in buildReq",
227
 
                           data->state);
228
 
                break;
229
 
        }
230
 
        return NULL;
231
 
}
232
 
 
233
 
 
234
 
static Boolean eap_gpsk_check(struct eap_sm *sm, void *priv,
235
 
                              u8 *respData, size_t respDataLen)
236
 
{
237
 
        struct eap_gpsk_data *data = priv;
238
 
        const u8 *pos;
239
 
        size_t len;
240
 
 
241
 
        pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_GPSK,
242
 
                               respData, respDataLen, &len);
243
 
        if (pos == NULL || len < 1) {
244
 
                wpa_printf(MSG_INFO, "EAP-GPSK: Invalid frame");
245
 
                return TRUE;
246
 
        }
247
 
 
248
 
        wpa_printf(MSG_DEBUG, "EAP-GPSK: Received frame: opcode=%d", *pos);
249
 
 
250
 
        if (data->state == GPSK_1 && *pos == EAP_GPSK_OPCODE_GPSK_2)
251
 
                return FALSE;
252
 
 
253
 
        if (data->state == GPSK_3 && *pos == EAP_GPSK_OPCODE_GPSK_4)
254
 
                return FALSE;
255
 
 
256
 
        wpa_printf(MSG_INFO, "EAP-GPSK: Unexpected opcode=%d in state=%d",
257
 
                   *pos, data->state);
258
 
 
259
 
        return TRUE;
260
 
}
261
 
 
262
 
 
263
 
static void eap_gpsk_process_gpsk_2(struct eap_sm *sm,
264
 
                                    struct eap_gpsk_data *data,
265
 
                                    u8 *respData, size_t respDataLen,
266
 
                                    const u8 *payload, size_t payloadlen)
267
 
{
268
 
        const u8 *pos, *end;
269
 
        u16 alen;
270
 
        const struct eap_gpsk_csuite *csuite;
271
 
        size_t i, miclen;
272
 
        u8 mic[EAP_GPSK_MAX_MIC_LEN];
273
 
 
274
 
        if (data->state != GPSK_1)
275
 
                return;
276
 
 
277
 
        wpa_printf(MSG_DEBUG, "EAP-GPSK: Received Response/GPSK-2");
278
 
 
279
 
        pos = payload;
280
 
        end = payload + payloadlen;
281
 
 
282
 
        if (end - pos < 2) {
283
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
284
 
                           "ID_Peer length");
285
 
                eap_gpsk_state(data, FAILURE);
286
 
                return;
287
 
        }
288
 
        alen = WPA_GET_BE16(pos);
289
 
        pos += 2;
290
 
        if (end - pos < alen) {
291
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
292
 
                           "ID_Peer");
293
 
                eap_gpsk_state(data, FAILURE);
294
 
                return;
295
 
        }
296
 
        free(data->id_peer);
297
 
        data->id_peer = malloc(alen);
298
 
        if (data->id_peer == NULL) {
299
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Not enough memory to store "
300
 
                           "%d-octet ID_Peer", alen);
301
 
                return;
302
 
        }
303
 
        memcpy(data->id_peer, pos, alen);
304
 
        data->id_peer_len = alen;
305
 
        wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Peer",
306
 
                          data->id_peer, data->id_peer_len);
307
 
        pos += alen;
308
 
 
309
 
        if (end - pos < 2) {
310
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
311
 
                           "ID_Server length");
312
 
                eap_gpsk_state(data, FAILURE);
313
 
                return;
314
 
        }
315
 
        alen = WPA_GET_BE16(pos);
316
 
        pos += 2;
317
 
        if (end - pos < alen) {
318
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
319
 
                           "ID_Server");
320
 
                eap_gpsk_state(data, FAILURE);
321
 
                return;
322
 
        }
323
 
        if (alen != data->id_server_len ||
324
 
            memcmp(pos, data->id_server, alen) != 0) {
325
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: ID_Server in GPSK-1 and "
326
 
                           "GPSK-2 did not match");
327
 
                eap_gpsk_state(data, FAILURE);
328
 
                return;
329
 
        }
330
 
        pos += alen;
331
 
 
332
 
        if (end - pos < EAP_GPSK_RAND_LEN) {
333
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
334
 
                           "RAND_Peer");
335
 
                eap_gpsk_state(data, FAILURE);
336
 
                return;
337
 
        }
338
 
        memcpy(data->rand_peer, pos, EAP_GPSK_RAND_LEN);
339
 
        wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Peer",
340
 
                    data->rand_peer, EAP_GPSK_RAND_LEN);
341
 
        pos += EAP_GPSK_RAND_LEN;
342
 
 
343
 
        if (end - pos < EAP_GPSK_RAND_LEN) {
344
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
345
 
                           "RAND_Server");
346
 
                eap_gpsk_state(data, FAILURE);
347
 
                return;
348
 
        }
349
 
        if (memcmp(data->rand_server, pos, EAP_GPSK_RAND_LEN) != 0) {
350
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-1 and "
351
 
                           "GPSK-2 did not match");
352
 
                wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-1",
353
 
                            data->rand_server, EAP_GPSK_RAND_LEN);
354
 
                wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-2",
355
 
                            pos, EAP_GPSK_RAND_LEN);
356
 
                eap_gpsk_state(data, FAILURE);
357
 
                return;
358
 
        }
359
 
        pos += EAP_GPSK_RAND_LEN;
360
 
 
361
 
        if (end - pos < 2) {
362
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
363
 
                           "CSuite_List length");
364
 
                eap_gpsk_state(data, FAILURE);
365
 
                return;
366
 
        }
367
 
        alen = WPA_GET_BE16(pos);
368
 
        pos += 2;
369
 
        if (end - pos < alen) {
370
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
371
 
                           "CSuite_List");
372
 
                eap_gpsk_state(data, FAILURE);
373
 
                return;
374
 
        }
375
 
        if (alen != data->csuite_count * sizeof(struct eap_gpsk_csuite) ||
376
 
            memcmp(pos, data->csuite_list, alen) != 0) {
377
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: CSuite_List in GPSK-1 and "
378
 
                           "GPSK-2 did not match");
379
 
                eap_gpsk_state(data, FAILURE);
380
 
                return;
381
 
        }
382
 
        pos += alen;
383
 
 
384
 
        if (end - pos < (int) sizeof(*csuite)) {
385
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
386
 
                           "CSuite_Sel");
387
 
                eap_gpsk_state(data, FAILURE);
388
 
                return;
389
 
        }
390
 
        csuite = (const struct eap_gpsk_csuite *) pos;
391
 
        for (i = 0; i < data->csuite_count; i++) {
392
 
                if (memcmp(csuite, &data->csuite_list[i], sizeof(*csuite)) ==
393
 
                    0)
394
 
                        break;
395
 
        }
396
 
        if (i == data->csuite_count) {
397
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Peer selected unsupported "
398
 
                           "ciphersuite %d:%d",
399
 
                           WPA_GET_BE24(csuite->vendor),
400
 
                           WPA_GET_BE24(csuite->specifier));
401
 
                eap_gpsk_state(data, FAILURE);
402
 
                return;
403
 
        }
404
 
        data->vendor = WPA_GET_BE24(csuite->vendor);
405
 
        data->specifier = WPA_GET_BE24(csuite->specifier);
406
 
        wpa_printf(MSG_DEBUG, "EAP-GPSK: CSuite_Sel %d:%d",
407
 
                   data->vendor, data->specifier);
408
 
        pos += sizeof(*csuite); 
409
 
 
410
 
        if (end - pos < 2) {
411
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
412
 
                           "PD_Payload_1 length");
413
 
                eap_gpsk_state(data, FAILURE);
414
 
                return;
415
 
        }
416
 
        alen = WPA_GET_BE16(pos);
417
 
        pos += 2;
418
 
        if (end - pos < alen) {
419
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
420
 
                           "PD_Payload_1");
421
 
                eap_gpsk_state(data, FAILURE);
422
 
                return;
423
 
        }
424
 
        wpa_hexdump(MSG_DEBUG, "EAP-GPSK: PD_Payload_1", pos, alen);
425
 
        pos += alen;
426
 
 
427
 
        if (sm->user == NULL || sm->user->password == NULL) {
428
 
                wpa_printf(MSG_INFO, "EAP-GPSK: No PSK/password configured "
429
 
                           "for the user");
430
 
                eap_gpsk_state(data, FAILURE);
431
 
                return;
432
 
        }
433
 
 
434
 
        if (eap_gpsk_derive_keys(sm->user->password, sm->user->password_len,
435
 
                                 data->vendor, data->specifier,
436
 
                                 data->rand_peer, data->rand_server,
437
 
                                 data->id_peer, data->id_peer_len,
438
 
                                 data->id_server, data->id_server_len,
439
 
                                 data->msk, data->emsk,
440
 
                                 data->sk, &data->sk_len,
441
 
                                 data->pk, &data->pk_len) < 0) {
442
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to derive keys");
443
 
                eap_gpsk_state(data, FAILURE);
444
 
                return;
445
 
        }
446
 
 
447
 
        miclen = eap_gpsk_mic_len(data->vendor, data->specifier);
448
 
        if (end - pos < (int) miclen) {
449
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for MIC "
450
 
                           "(left=%d miclen=%d)", end - pos, miclen);
451
 
                eap_gpsk_state(data, FAILURE);
452
 
                return;
453
 
        }
454
 
        if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
455
 
                                 data->specifier, payload, pos - payload, mic)
456
 
            < 0) {
457
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to compute MIC");
458
 
                eap_gpsk_state(data, FAILURE);
459
 
                return;
460
 
        }
461
 
        if (memcmp(mic, pos, miclen) != 0) {
462
 
                wpa_printf(MSG_INFO, "EAP-GPSK: Incorrect MIC in GPSK-2");
463
 
                wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Received MIC", pos, miclen);
464
 
                wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Computed MIC", mic, miclen);
465
 
                eap_gpsk_state(data, FAILURE);
466
 
                return;
467
 
        }
468
 
        pos += miclen;
469
 
 
470
 
        if (pos != end) {
471
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Ignored %d bytes of extra "
472
 
                           "data in the end of GPSK-2", end - pos);
473
 
        }
474
 
 
475
 
        eap_gpsk_state(data, GPSK_3);
476
 
}
477
 
 
478
 
 
479
 
static void eap_gpsk_process_gpsk_4(struct eap_sm *sm,
480
 
                                    struct eap_gpsk_data *data,
481
 
                                    u8 *respData, size_t respDataLen,
482
 
                                    const u8 *payload, size_t payloadlen)
483
 
{
484
 
        const u8 *pos, *end;
485
 
        u16 alen;
486
 
        size_t miclen;
487
 
        u8 mic[EAP_GPSK_MAX_MIC_LEN];
488
 
 
489
 
        if (data->state != GPSK_3)
490
 
                return;
491
 
 
492
 
        wpa_printf(MSG_DEBUG, "EAP-GPSK: Received Response/GPSK-4");
493
 
 
494
 
        pos = payload;
495
 
        end = payload + payloadlen;
496
 
 
497
 
        if (end - pos < 2) {
498
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
499
 
                           "PD_Payload_1 length");
500
 
                eap_gpsk_state(data, FAILURE);
501
 
                return;
502
 
        }
503
 
        alen = WPA_GET_BE16(pos);
504
 
        pos += 2;
505
 
        if (end - pos < alen) {
506
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short message for "
507
 
                           "PD_Payload_1");
508
 
                eap_gpsk_state(data, FAILURE);
509
 
                return;
510
 
        }
511
 
        wpa_hexdump(MSG_DEBUG, "EAP-GPSK: PD_Payload_1", pos, alen);
512
 
        pos += alen;
513
 
 
514
 
        miclen = eap_gpsk_mic_len(data->vendor, data->specifier);
515
 
        if (end - pos < (int) miclen) {
516
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for MIC "
517
 
                           "(left=%d miclen=%d)", end - pos, miclen);
518
 
                eap_gpsk_state(data, FAILURE);
519
 
                return;
520
 
        }
521
 
        if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
522
 
                                 data->specifier, payload, pos - payload, mic)
523
 
            < 0) {
524
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to compute MIC");
525
 
                eap_gpsk_state(data, FAILURE);
526
 
                return;
527
 
        }
528
 
        if (memcmp(mic, pos, miclen) != 0) {
529
 
                wpa_printf(MSG_INFO, "EAP-GPSK: Incorrect MIC in GPSK-4");
530
 
                wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Received MIC", pos, miclen);
531
 
                wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Computed MIC", mic, miclen);
532
 
                eap_gpsk_state(data, FAILURE);
533
 
                return;
534
 
        }
535
 
        pos += miclen;
536
 
 
537
 
        if (pos != end) {
538
 
                wpa_printf(MSG_DEBUG, "EAP-GPSK: Ignored %d bytes of extra "
539
 
                           "data in the end of GPSK-4", end - pos);
540
 
        }
541
 
 
542
 
        eap_gpsk_state(data, SUCCESS);
543
 
}
544
 
 
545
 
 
546
 
static void eap_gpsk_process(struct eap_sm *sm, void *priv,
547
 
                             u8 *respData, size_t respDataLen)
548
 
{
549
 
        struct eap_gpsk_data *data = priv;
550
 
        const u8 *pos;
551
 
        size_t len;
552
 
 
553
 
        pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_GPSK,
554
 
                               respData, respDataLen, &len);
555
 
        if (pos == NULL || len < 1)
556
 
                return;
557
 
 
558
 
        switch (*pos) {
559
 
        case EAP_GPSK_OPCODE_GPSK_2:
560
 
                eap_gpsk_process_gpsk_2(sm, data, respData, respDataLen,
561
 
                                        pos + 1, len - 1);
562
 
                break;
563
 
        case EAP_GPSK_OPCODE_GPSK_4:
564
 
                eap_gpsk_process_gpsk_4(sm, data, respData, respDataLen,
565
 
                                        pos + 1, len - 1);
566
 
                break;
567
 
        }
568
 
}
569
 
 
570
 
 
571
 
static Boolean eap_gpsk_isDone(struct eap_sm *sm, void *priv)
572
 
{
573
 
        struct eap_gpsk_data *data = priv;
574
 
        return data->state == SUCCESS || data->state == FAILURE;
575
 
}
576
 
 
577
 
 
578
 
static u8 * eap_gpsk_getKey(struct eap_sm *sm, void *priv, size_t *len)
579
 
{
580
 
        struct eap_gpsk_data *data = priv;
581
 
        u8 *key;
582
 
 
583
 
        if (data->state != SUCCESS)
584
 
                return NULL;
585
 
 
586
 
        key = malloc(EAP_MSK_LEN);
587
 
        if (key == NULL)
588
 
                return NULL;
589
 
        memcpy(key, data->msk, EAP_MSK_LEN);
590
 
        *len = EAP_MSK_LEN;
591
 
 
592
 
        return key;
593
 
}
594
 
 
595
 
 
596
 
static u8 * eap_gpsk_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
597
 
{
598
 
        struct eap_gpsk_data *data = priv;
599
 
        u8 *key;
600
 
 
601
 
        if (data->state != SUCCESS)
602
 
                return NULL;
603
 
 
604
 
        key = malloc(EAP_EMSK_LEN);
605
 
        if (key == NULL)
606
 
                return NULL;
607
 
        memcpy(key, data->emsk, EAP_EMSK_LEN);
608
 
        *len = EAP_EMSK_LEN;
609
 
 
610
 
        return key;
611
 
}
612
 
 
613
 
 
614
 
static Boolean eap_gpsk_isSuccess(struct eap_sm *sm, void *priv)
615
 
{
616
 
        struct eap_gpsk_data *data = priv;
617
 
        return data->state == SUCCESS;
618
 
}
619
 
 
620
 
 
621
 
int eap_server_gpsk_register(void)
622
 
{
623
 
        struct eap_method *eap;
624
 
        int ret;
625
 
 
626
 
        eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
627
 
                                      EAP_VENDOR_IETF, EAP_TYPE_GPSK, "GPSK");
628
 
        if (eap == NULL)
629
 
                return -1;
630
 
 
631
 
        eap->init = eap_gpsk_init;
632
 
        eap->reset = eap_gpsk_reset;
633
 
        eap->buildReq = eap_gpsk_buildReq;
634
 
        eap->check = eap_gpsk_check;
635
 
        eap->process = eap_gpsk_process;
636
 
        eap->isDone = eap_gpsk_isDone;
637
 
        eap->getKey = eap_gpsk_getKey;
638
 
        eap->isSuccess = eap_gpsk_isSuccess;
639
 
        eap->get_emsk = eap_gpsk_get_emsk;
640
 
 
641
 
        ret = eap_server_method_register(eap);
642
 
        if (ret)
643
 
                eap_server_method_free(eap);
644
 
        return ret;
645
 
}