879dde67d4d5afd44b5dedf25d12b3a65b639a77
[libeap.git] / src / eap_peer / eap_gpsk.c
1 /*
2  * EAP peer method: EAP-GPSK (draft-ietf-emu-eap-gpsk-08.txt)
3  * Copyright (c) 2006-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_peer/eap_i.h"
19 #include "eap_common/eap_gpsk_common.h"
20
21 struct eap_gpsk_data {
22         enum { GPSK_1, GPSK_3, SUCCESS, FAILURE } state;
23         u8 rand_server[EAP_GPSK_RAND_LEN];
24         u8 rand_peer[EAP_GPSK_RAND_LEN];
25         u8 msk[EAP_MSK_LEN];
26         u8 emsk[EAP_EMSK_LEN];
27         u8 sk[EAP_GPSK_MAX_SK_LEN];
28         size_t sk_len;
29         u8 pk[EAP_GPSK_MAX_PK_LEN];
30         size_t pk_len;
31         u8 session_id;
32         int session_id_set;
33         u8 *id_peer;
34         size_t id_peer_len;
35         u8 *id_server;
36         size_t id_server_len;
37         int vendor; /* CSuite/Specifier */
38         int specifier; /* CSuite/Specifier */
39         u8 *psk;
40         size_t psk_len;
41 };
42
43
44 static struct wpabuf * eap_gpsk_send_gpsk_2(struct eap_gpsk_data *data,
45                                             u8 identifier,
46                                             const u8 *csuite_list,
47                                             size_t csuite_list_len);
48 static struct wpabuf * eap_gpsk_send_gpsk_4(struct eap_gpsk_data *data,
49                                             u8 identifier);
50
51
52 #ifndef CONFIG_NO_STDOUT_DEBUG
53 static const char * eap_gpsk_state_txt(int state)
54 {
55         switch (state) {
56         case GPSK_1:
57                 return "GPSK-1";
58         case GPSK_3:
59                 return "GPSK-3";
60         case SUCCESS:
61                 return "SUCCESS";
62         case FAILURE:
63                 return "FAILURE";
64         default:
65                 return "?";
66         }
67 }
68 #endif /* CONFIG_NO_STDOUT_DEBUG */
69
70
71 static void eap_gpsk_state(struct eap_gpsk_data *data, int state)
72 {
73         wpa_printf(MSG_DEBUG, "EAP-GPSK: %s -> %s",
74                    eap_gpsk_state_txt(data->state),
75                    eap_gpsk_state_txt(state));
76         data->state = state;
77 }
78
79
80 static void eap_gpsk_deinit(struct eap_sm *sm, void *priv);
81
82
83 static void * eap_gpsk_init(struct eap_sm *sm)
84 {
85         struct eap_gpsk_data *data;
86         const u8 *identity, *password;
87         size_t identity_len, password_len;
88
89         password = eap_get_config_password(sm, &password_len);
90         if (password == NULL) {
91                 wpa_printf(MSG_INFO, "EAP-GPSK: No key (password) configured");
92                 return NULL;
93         }
94
95         data = os_zalloc(sizeof(*data));
96         if (data == NULL)
97                 return NULL;
98         data->state = GPSK_1;
99
100         identity = eap_get_config_identity(sm, &identity_len);
101         if (identity) {
102                 data->id_peer = os_malloc(identity_len);
103                 if (data->id_peer == NULL) {
104                         eap_gpsk_deinit(sm, data);
105                         return NULL;
106                 }
107                 os_memcpy(data->id_peer, identity, identity_len);
108                 data->id_peer_len = identity_len;
109         }
110
111         data->psk = os_malloc(password_len);
112         if (data->psk == NULL) {
113                 eap_gpsk_deinit(sm, data);
114                 return NULL;
115         }
116         os_memcpy(data->psk, password, password_len);
117         data->psk_len = password_len;
118
119         return data;
120 }
121
122
123 static void eap_gpsk_deinit(struct eap_sm *sm, void *priv)
124 {
125         struct eap_gpsk_data *data = priv;
126         os_free(data->id_server);
127         os_free(data->id_peer);
128         os_free(data->psk);
129         os_free(data);
130 }
131
132
133 const u8 * eap_gpsk_process_id_server(struct eap_gpsk_data *data,
134                                       const u8 *pos, const u8 *end)
135 {
136         u16 alen;
137
138         if (end - pos < 2) {
139                 wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short GPSK-1 packet");
140                 return NULL;
141         }
142         alen = WPA_GET_BE16(pos);
143         pos += 2;
144         if (end - pos < alen) {
145                 wpa_printf(MSG_DEBUG, "EAP-GPSK: ID_Server overflow");
146                 return NULL;
147         }
148         os_free(data->id_server);
149         data->id_server = os_malloc(alen);
150         if (data->id_server == NULL) {
151                 wpa_printf(MSG_DEBUG, "EAP-GPSK: No memory for ID_Server");
152                 return NULL;
153         }
154         os_memcpy(data->id_server, pos, alen);
155         data->id_server_len = alen;
156         wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Server",
157                           data->id_server, data->id_server_len);
158         pos += alen;
159
160         return pos;
161 }
162
163
164 const u8 * eap_gpsk_process_rand_server(struct eap_gpsk_data *data,
165                                         const u8 *pos, const u8 *end)
166 {
167         if (pos == NULL)
168                 return NULL;
169
170         if (end - pos < EAP_GPSK_RAND_LEN) {
171                 wpa_printf(MSG_DEBUG, "EAP-GPSK: RAND_Server overflow");
172                 return NULL;
173         }
174         os_memcpy(data->rand_server, pos, EAP_GPSK_RAND_LEN);
175         wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Server",
176                     data->rand_server, EAP_GPSK_RAND_LEN);
177         pos += EAP_GPSK_RAND_LEN;
178
179         return pos;
180 }
181
182
183 static int eap_gpsk_select_csuite(struct eap_sm *sm,
184                                   struct eap_gpsk_data *data,
185                                   const u8 *csuite_list,
186                                   size_t csuite_list_len)
187 {
188         struct eap_gpsk_csuite *csuite;
189         int i, count;
190
191         count = csuite_list_len / sizeof(struct eap_gpsk_csuite);
192         data->vendor = EAP_GPSK_VENDOR_IETF;
193         data->specifier = EAP_GPSK_CIPHER_RESERVED;
194         csuite = (struct eap_gpsk_csuite *) csuite_list;
195         for (i = 0; i < count; i++) {
196                 int vendor, specifier;
197                 vendor = WPA_GET_BE32(csuite->vendor);
198                 specifier = WPA_GET_BE16(csuite->specifier);
199                 wpa_printf(MSG_DEBUG, "EAP-GPSK: CSuite[%d]: %d:%d",
200                            i, vendor, specifier);
201                 if (data->vendor == EAP_GPSK_VENDOR_IETF &&
202                     data->specifier == EAP_GPSK_CIPHER_RESERVED &&
203                     eap_gpsk_supported_ciphersuite(vendor, specifier)) {
204                         data->vendor = vendor;
205                         data->specifier = specifier;
206                 }
207                 csuite++;
208         }
209         if (data->vendor == EAP_GPSK_VENDOR_IETF &&
210             data->specifier == EAP_GPSK_CIPHER_RESERVED) {
211                 wpa_msg(sm->msg_ctx, MSG_INFO, "EAP-GPSK: No supported "
212                         "ciphersuite found");
213                 return -1;
214         }
215         wpa_printf(MSG_DEBUG, "EAP-GPSK: Selected ciphersuite %d:%d",
216                    data->vendor, data->specifier);
217
218         return 0;
219 }
220
221
222 const u8 * eap_gpsk_process_csuite_list(struct eap_sm *sm,
223                                         struct eap_gpsk_data *data,
224                                         const u8 **list, size_t *list_len,
225                                         const u8 *pos, const u8 *end)
226 {
227         if (pos == NULL)
228                 return NULL;
229
230         if (end - pos < 2) {
231                 wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short GPSK-1 packet");
232                 return NULL;
233         }
234         *list_len = WPA_GET_BE16(pos);
235         pos += 2;
236         if (end - pos < (int) *list_len) {
237                 wpa_printf(MSG_DEBUG, "EAP-GPSK: CSuite_List overflow");
238                 return NULL;
239         }
240         if (*list_len == 0 || (*list_len % sizeof(struct eap_gpsk_csuite))) {
241                 wpa_printf(MSG_DEBUG, "EAP-GPSK: Invalid CSuite_List len %lu",
242                            (unsigned long) *list_len);
243                 return NULL;
244         }
245         *list = pos;
246         pos += *list_len;
247
248         if (eap_gpsk_select_csuite(sm, data, *list, *list_len) < 0)
249                 return NULL;
250
251         return pos;
252 }
253
254
255 static struct wpabuf * eap_gpsk_process_gpsk_1(struct eap_sm *sm,
256                                                struct eap_gpsk_data *data,
257                                                struct eap_method_ret *ret,
258                                                const struct wpabuf *reqData,
259                                                const u8 *payload,
260                                                size_t payload_len)
261 {
262         size_t csuite_list_len;
263         const u8 *csuite_list, *pos, *end;
264         struct wpabuf *resp;
265
266         if (data->state != GPSK_1) {
267                 ret->ignore = TRUE;
268                 return NULL;
269         }
270
271         wpa_printf(MSG_DEBUG, "EAP-GPSK: Received Request/GPSK-1");
272
273         end = payload + payload_len;
274
275         pos = eap_gpsk_process_id_server(data, payload, end);
276         pos = eap_gpsk_process_rand_server(data, pos, end);
277         pos = eap_gpsk_process_csuite_list(sm, data, &csuite_list,
278                                            &csuite_list_len, pos, end);
279         if (pos == NULL) {
280                 eap_gpsk_state(data, FAILURE);
281                 return NULL;
282         }
283
284         resp = eap_gpsk_send_gpsk_2(data, eap_get_id(reqData),
285                                     csuite_list, csuite_list_len);
286         if (resp == NULL)
287                 return NULL;
288
289         eap_gpsk_state(data, GPSK_3);
290
291         return resp;
292 }
293
294
295 static struct wpabuf * eap_gpsk_send_gpsk_2(struct eap_gpsk_data *data,
296                                             u8 identifier,
297                                             const u8 *csuite_list,
298                                             size_t csuite_list_len)
299 {
300         struct wpabuf *resp;
301         size_t len, miclen;
302         u8 *rpos, *start;
303         struct eap_gpsk_csuite *csuite;
304
305         wpa_printf(MSG_DEBUG, "EAP-GPSK: Sending Response/GPSK-2");
306
307         miclen = eap_gpsk_mic_len(data->vendor, data->specifier);
308         len = 1 + 2 + data->id_peer_len + 2 + data->id_server_len +
309                 2 * EAP_GPSK_RAND_LEN + 2 + csuite_list_len +
310                 sizeof(struct eap_gpsk_csuite) + 2 + miclen;
311
312         resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_GPSK, len,
313                              EAP_CODE_RESPONSE, identifier);
314         if (resp == NULL)
315                 return NULL;
316
317         wpabuf_put_u8(resp, EAP_GPSK_OPCODE_GPSK_2);
318         start = wpabuf_put(resp, 0);
319
320         wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Peer",
321                           data->id_peer, data->id_peer_len);
322         wpabuf_put_be16(resp, data->id_peer_len);
323         wpabuf_put_data(resp, data->id_peer, data->id_peer_len);
324
325         wpabuf_put_be16(resp, data->id_server_len);
326         wpabuf_put_data(resp, data->id_server, data->id_server_len);
327
328         if (os_get_random(data->rand_peer, EAP_GPSK_RAND_LEN)) {
329                 wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to get random data "
330                            "for RAND_Peer");
331                 eap_gpsk_state(data, FAILURE);
332                 wpabuf_free(resp);
333                 return NULL;
334         }
335         wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Peer",
336                     data->rand_peer, EAP_GPSK_RAND_LEN);
337         wpabuf_put_data(resp, data->rand_peer, EAP_GPSK_RAND_LEN);
338         wpabuf_put_data(resp, data->rand_server, EAP_GPSK_RAND_LEN);
339
340         wpabuf_put_be16(resp, csuite_list_len);
341         wpabuf_put_data(resp, csuite_list, csuite_list_len);
342
343         csuite = wpabuf_put(resp, sizeof(*csuite));
344         WPA_PUT_BE32(csuite->vendor, data->vendor);
345         WPA_PUT_BE16(csuite->specifier, data->specifier);
346
347         if (eap_gpsk_derive_keys(data->psk, data->psk_len,
348                                  data->vendor, data->specifier,
349                                  data->rand_peer, data->rand_server,
350                                  data->id_peer, data->id_peer_len,
351                                  data->id_server, data->id_server_len,
352                                  data->msk, data->emsk,
353                                  data->sk, &data->sk_len,
354                                  data->pk, &data->pk_len) < 0) {
355                 wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to derive keys");
356                 eap_gpsk_state(data, FAILURE);
357                 wpabuf_free(resp);
358                 return NULL;
359         }
360
361         /* No PD_Payload_1 */
362         wpabuf_put_be16(resp, 0);
363
364         rpos = wpabuf_put(resp, miclen);
365         if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
366                                  data->specifier, start, rpos - start, rpos) <
367             0) {
368                 eap_gpsk_state(data, FAILURE);
369                 wpabuf_free(resp);
370                 return NULL;
371         }
372
373         return resp;
374 }
375
376
377 const u8 * eap_gpsk_validate_rand(struct eap_gpsk_data *data, const u8 *pos,
378                                   const u8 *end)
379 {
380         if (end - pos < EAP_GPSK_RAND_LEN) {
381                 wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
382                            "RAND_Peer");
383                 return NULL;
384         }
385         if (os_memcmp(pos, data->rand_peer, EAP_GPSK_RAND_LEN) != 0) {
386                 wpa_printf(MSG_DEBUG, "EAP-GPSK: RAND_Peer in GPSK-2 and "
387                            "GPSK-3 did not match");
388                 wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Peer in GPSK-2",
389                             data->rand_peer, EAP_GPSK_RAND_LEN);
390                 wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Peer in GPSK-3",
391                             pos, EAP_GPSK_RAND_LEN);
392                 return NULL;
393         }
394         pos += EAP_GPSK_RAND_LEN;
395
396         if (end - pos < EAP_GPSK_RAND_LEN) {
397                 wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
398                            "RAND_Server");
399                 return NULL;
400         }
401         if (os_memcmp(pos, data->rand_server, EAP_GPSK_RAND_LEN) != 0) {
402                 wpa_printf(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-1 and "
403                            "GPSK-3 did not match");
404                 wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-1",
405                             data->rand_server, EAP_GPSK_RAND_LEN);
406                 wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-3",
407                             pos, EAP_GPSK_RAND_LEN);
408                 return NULL;
409         }
410         pos += EAP_GPSK_RAND_LEN;
411
412         return pos;
413 }
414
415
416 const u8 * eap_gpsk_validate_id_server(struct eap_gpsk_data *data,
417                                        const u8 *pos, const u8 *end)
418 {
419         size_t len;
420
421         if (pos == NULL)
422                 return NULL;
423
424         if (end - pos < (int) 2) {
425                 wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
426                            "length(ID_Server)");
427                 return NULL;
428         }
429
430         len = WPA_GET_BE16(pos);
431         pos += 2;
432
433         if (end - pos < (int) len) {
434                 wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
435                            "ID_Server");
436                 return NULL;
437         }
438
439         if (len != data->id_server_len ||
440             os_memcmp(pos, data->id_server, len) != 0) {
441                 wpa_printf(MSG_INFO, "EAP-GPSK: ID_Server did not match with "
442                            "the one used in GPSK-1");
443                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Server in GPSK-1",
444                                   data->id_server, data->id_server_len);
445                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Server in GPSK-3",
446                                   pos, len);
447         }
448
449         pos += len;
450
451         return pos;
452 }
453
454
455 const u8 * eap_gpsk_validate_csuite(struct eap_gpsk_data *data, const u8 *pos,
456                                     const u8 *end)
457 {
458         int vendor, specifier;
459         const struct eap_gpsk_csuite *csuite;
460
461         if (pos == NULL)
462                 return NULL;
463
464         if (end - pos < (int) sizeof(*csuite)) {
465                 wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
466                            "CSuite_Sel");
467                 return NULL;
468         }
469         csuite = (const struct eap_gpsk_csuite *) pos;
470         vendor = WPA_GET_BE32(csuite->vendor);
471         specifier = WPA_GET_BE16(csuite->specifier);
472         pos += sizeof(*csuite);
473         if (vendor != data->vendor || specifier != data->specifier) {
474                 wpa_printf(MSG_DEBUG, "EAP-GPSK: CSuite_Sel (%d:%d) does not "
475                            "match with the one sent in GPSK-2 (%d:%d)",
476                            vendor, specifier, data->vendor, data->specifier);
477                 return NULL;
478         }
479
480         return pos;
481 }
482
483
484 const u8 * eap_gpsk_validate_pd_payload_2(struct eap_gpsk_data *data,
485                                           const u8 *pos, const u8 *end)
486 {
487         u16 alen;
488
489         if (pos == NULL)
490                 return NULL;
491
492         if (end - pos < 2) {
493                 wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
494                            "PD_Payload_2 length");
495                 return NULL;
496         }
497         alen = WPA_GET_BE16(pos);
498         pos += 2;
499         if (end - pos < alen) {
500                 wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
501                            "%d-octet PD_Payload_2", alen);
502                 return NULL;
503         }
504         wpa_hexdump(MSG_DEBUG, "EAP-GPSK: PD_Payload_2", pos, alen);
505         pos += alen;
506
507         return pos;
508 }
509
510
511 const u8 * eap_gpsk_validate_gpsk_3_mic(struct eap_gpsk_data *data,
512                                         const u8 *payload,
513                                         const u8 *pos, const u8 *end)
514 {
515         size_t miclen;
516         u8 mic[EAP_GPSK_MAX_MIC_LEN];
517
518         if (pos == NULL)
519                 return NULL;
520
521         miclen = eap_gpsk_mic_len(data->vendor, data->specifier);
522         if (end - pos < (int) miclen) {
523                 wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for MIC "
524                            "(left=%lu miclen=%lu)",
525                            (unsigned long) (end - pos),
526                            (unsigned long) miclen);
527                 return NULL;
528         }
529         if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
530                                  data->specifier, payload, pos - payload, mic)
531             < 0) {
532                 wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to compute MIC");
533                 return NULL;
534         }
535         if (os_memcmp(mic, pos, miclen) != 0) {
536                 wpa_printf(MSG_INFO, "EAP-GPSK: Incorrect MIC in GPSK-3");
537                 wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Received MIC", pos, miclen);
538                 wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Computed MIC", mic, miclen);
539                 return NULL;
540         }
541         pos += miclen;
542
543         return pos;
544 }
545
546
547 static struct wpabuf * eap_gpsk_process_gpsk_3(struct eap_sm *sm,
548                                                struct eap_gpsk_data *data,
549                                                struct eap_method_ret *ret,
550                                                const struct wpabuf *reqData,
551                                                const u8 *payload,
552                                                size_t payload_len)
553 {
554         struct wpabuf *resp;
555         const u8 *pos, *end;
556
557         if (data->state != GPSK_3) {
558                 ret->ignore = TRUE;
559                 return NULL;
560         }
561
562         wpa_printf(MSG_DEBUG, "EAP-GPSK: Received Request/GPSK-3");
563
564         end = payload + payload_len;
565
566         pos = eap_gpsk_validate_rand(data, payload, end);
567         pos = eap_gpsk_validate_id_server(data, pos, end);
568         pos = eap_gpsk_validate_csuite(data, pos, end);
569         pos = eap_gpsk_validate_pd_payload_2(data, pos, end);
570         pos = eap_gpsk_validate_gpsk_3_mic(data, payload, pos, end);
571
572         if (pos == NULL) {
573                 eap_gpsk_state(data, FAILURE);
574                 return NULL;
575         }
576         if (pos != end) {
577                 wpa_printf(MSG_DEBUG, "EAP-GPSK: Ignored %lu bytes of extra "
578                            "data in the end of GPSK-2",
579                            (unsigned long) (end - pos));
580         }
581
582         resp = eap_gpsk_send_gpsk_4(data, eap_get_id(reqData));
583         if (resp == NULL)
584                 return NULL;
585
586         eap_gpsk_state(data, SUCCESS);
587         ret->methodState = METHOD_DONE;
588         ret->decision = DECISION_UNCOND_SUCC;
589
590         return resp;
591 }
592
593
594 static struct wpabuf * eap_gpsk_send_gpsk_4(struct eap_gpsk_data *data,
595                                             u8 identifier)
596 {
597         struct wpabuf *resp;
598         u8 *rpos, *start;
599         size_t mlen;
600
601         wpa_printf(MSG_DEBUG, "EAP-GPSK: Sending Response/GPSK-4");
602
603         mlen = eap_gpsk_mic_len(data->vendor, data->specifier);
604
605         resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_GPSK, 1 + 2 + mlen,
606                              EAP_CODE_RESPONSE, identifier);
607         if (resp == NULL)
608                 return NULL;
609
610         wpabuf_put_u8(resp, EAP_GPSK_OPCODE_GPSK_4);
611         start = wpabuf_put(resp, 0);
612
613         /* No PD_Payload_3 */
614         wpabuf_put_be16(resp, 0);
615
616         rpos = wpabuf_put(resp, mlen);
617         if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
618                                  data->specifier, start, rpos - start, rpos) <
619             0) {
620                 eap_gpsk_state(data, FAILURE);
621                 wpabuf_free(resp);
622                 return NULL;
623         }
624
625         return resp;
626 }
627
628
629 static struct wpabuf * eap_gpsk_process(struct eap_sm *sm, void *priv,
630                                         struct eap_method_ret *ret,
631                                         const struct wpabuf *reqData)
632 {
633         struct eap_gpsk_data *data = priv;
634         struct wpabuf *resp;
635         const u8 *pos;
636         size_t len;
637
638         pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_GPSK, reqData, &len);
639         if (pos == NULL || len < 1) {
640                 ret->ignore = TRUE;
641                 return NULL;
642         }
643
644         wpa_printf(MSG_DEBUG, "EAP-GPSK: Received frame: opcode %d", *pos);
645
646         ret->ignore = FALSE;
647         ret->methodState = METHOD_MAY_CONT;
648         ret->decision = DECISION_FAIL;
649         ret->allowNotifications = FALSE;
650
651         switch (*pos) {
652         case EAP_GPSK_OPCODE_GPSK_1:
653                 resp = eap_gpsk_process_gpsk_1(sm, data, ret, reqData,
654                                                pos + 1, len - 1);
655                 break;
656         case EAP_GPSK_OPCODE_GPSK_3:
657                 resp = eap_gpsk_process_gpsk_3(sm, data, ret, reqData,
658                                                pos + 1, len - 1);
659                 break;
660         default:
661                 wpa_printf(MSG_DEBUG, "EAP-GPSK: Ignoring message with "
662                            "unknown opcode %d", *pos);
663                 ret->ignore = TRUE;
664                 return NULL;
665         }
666
667         return resp;
668 }
669
670
671 static Boolean eap_gpsk_isKeyAvailable(struct eap_sm *sm, void *priv)
672 {
673         struct eap_gpsk_data *data = priv;
674         return data->state == SUCCESS;
675 }
676
677
678 static u8 * eap_gpsk_getKey(struct eap_sm *sm, void *priv, size_t *len)
679 {
680         struct eap_gpsk_data *data = priv;
681         u8 *key;
682
683         if (data->state != SUCCESS)
684                 return NULL;
685
686         key = os_malloc(EAP_MSK_LEN);
687         if (key == NULL)
688                 return NULL;
689         os_memcpy(key, data->msk, EAP_MSK_LEN);
690         *len = EAP_MSK_LEN;
691
692         return key;
693 }
694
695
696 static u8 * eap_gpsk_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
697 {
698         struct eap_gpsk_data *data = priv;
699         u8 *key;
700
701         if (data->state != SUCCESS)
702                 return NULL;
703
704         key = os_malloc(EAP_EMSK_LEN);
705         if (key == NULL)
706                 return NULL;
707         os_memcpy(key, data->emsk, EAP_EMSK_LEN);
708         *len = EAP_EMSK_LEN;
709
710         return key;
711 }
712
713
714 int eap_peer_gpsk_register(void)
715 {
716         struct eap_method *eap;
717         int ret;
718
719         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
720                                     EAP_VENDOR_IETF, EAP_TYPE_GPSK, "GPSK");
721         if (eap == NULL)
722                 return -1;
723
724         eap->init = eap_gpsk_init;
725         eap->deinit = eap_gpsk_deinit;
726         eap->process = eap_gpsk_process;
727         eap->isKeyAvailable = eap_gpsk_isKeyAvailable;
728         eap->getKey = eap_gpsk_getKey;
729         eap->get_emsk = eap_gpsk_get_emsk;
730
731         ret = eap_peer_method_register(eap);
732         if (ret)
733                 eap_peer_method_free(eap);
734         return ret;
735 }