]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/wpa_supplicant/eap_gtc.c
This commit was generated by cvs2svn to compensate for changes in r147466,
[FreeBSD/FreeBSD.git] / contrib / wpa_supplicant / eap_gtc.c
1 /*
2  * WPA Supplicant / EAP-GTC (RFC 2284)
3  * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.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 <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18
19 #include "common.h"
20 #include "eap_i.h"
21 #include "wpa_supplicant.h"
22 #include "config_ssid.h"
23
24
25 struct eap_gtc_data {
26         int prefix;
27 };
28
29
30 static void * eap_gtc_init(struct eap_sm *sm)
31 {
32         struct eap_gtc_data *data;
33         data = malloc(sizeof(*data));
34         if (data == NULL)
35                 return NULL;
36         memset(data, 0, sizeof(*data));
37
38         if (sm->m && sm->m->method == EAP_TYPE_FAST) {
39                 wpa_printf(MSG_DEBUG, "EAP-GTC: EAP-FAST tunnel - use prefix "
40                            "with challenge/response");
41                 data->prefix = 1;
42         }
43         return data;
44 }
45
46
47 static void eap_gtc_deinit(struct eap_sm *sm, void *priv)
48 {
49         struct eap_gtc_data *data = priv;
50         free(data);
51 }
52
53
54 static u8 * eap_gtc_process(struct eap_sm *sm, void *priv,
55                             struct eap_method_ret *ret,
56                             u8 *reqData, size_t reqDataLen,
57                             size_t *respDataLen)
58 {
59         struct eap_gtc_data *data = priv;
60         struct wpa_ssid *config = eap_get_config(sm);
61         struct eap_hdr *req, *resp;
62         u8 *pos, *password;
63         size_t password_len, len, msg_len;
64
65         req = (struct eap_hdr *) reqData;
66         pos = (u8 *) (req + 1);
67         if (reqDataLen < sizeof(*req) + 1 || *pos != EAP_TYPE_GTC ||
68             (len = be_to_host16(req->length)) > reqDataLen) {
69                 wpa_printf(MSG_INFO, "EAP-GTC: Invalid frame");
70                 ret->ignore = TRUE;
71                 return NULL;
72         }
73         pos++;
74         msg_len = len - sizeof(*req) - 1;
75         wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-GTC: Request message",
76                           pos, msg_len);
77         if (data->prefix &&
78             (msg_len < 10 || memcmp(pos, "CHALLENGE=", 10) != 0)) {
79                 wpa_printf(MSG_DEBUG, "EAP-GTC: Challenge did not start with "
80                            "expected prefix");
81
82                 /* Send an empty response in order to allow tunneled
83                  * acknowledgement of the failure. This will also cover the
84                  * error case which seems to use EAP-MSCHAPv2 like error
85                  * reporting with EAP-GTC inside EAP-FAST tunnel. */
86                 *respDataLen = sizeof(struct eap_hdr) + 1;
87                 resp = malloc(*respDataLen);
88                 if (resp == NULL)
89                         return NULL;
90                 resp->code = EAP_CODE_RESPONSE;
91                 resp->identifier = req->identifier;
92                 resp->length = host_to_be16(*respDataLen);
93                 pos = (u8 *) (resp + 1);
94                 *pos++ = EAP_TYPE_GTC;
95                 return (u8 *) resp;
96         }
97
98         if (config == NULL ||
99             (config->password == NULL && config->otp == NULL)) {
100                 wpa_printf(MSG_INFO, "EAP-GTC: Password not configured");
101                 eap_sm_request_otp(sm, config, (char *) pos,
102                                    len - sizeof(*req) - 1);
103                 ret->ignore = TRUE;
104                 return NULL;
105         }
106
107         if (config->otp) {
108                 password = config->otp;
109                 password_len = config->otp_len;
110         } else {
111                 password = config->password;
112                 password_len = config->password_len;
113         }
114
115         ret->ignore = FALSE;
116
117         ret->methodState = data->prefix ? METHOD_MAY_CONT : METHOD_DONE;
118         ret->decision = DECISION_COND_SUCC;
119         ret->allowNotifications = FALSE;
120
121         *respDataLen = sizeof(struct eap_hdr) + 1 + password_len;
122         if (data->prefix) {
123                 *respDataLen += 9 + config->identity_len + 1;
124         }
125         resp = malloc(*respDataLen);
126         if (resp == NULL)
127                 return NULL;
128         resp->code = EAP_CODE_RESPONSE;
129         resp->identifier = req->identifier;
130         resp->length = host_to_be16(*respDataLen);
131         pos = (u8 *) (resp + 1);
132         *pos++ = EAP_TYPE_GTC;
133         if (data->prefix) {
134                 memcpy(pos, "RESPONSE=", 9);
135                 pos += 9;
136                 memcpy(pos, config->identity, config->identity_len);
137                 pos += config->identity_len;
138                 *pos++ = '\0';
139         }
140         memcpy(pos, password, password_len);
141         wpa_hexdump_ascii_key(MSG_MSGDUMP, "EAP-GTC: Response",
142                               (u8 *) (resp + 1) + 1,
143                               *respDataLen - sizeof(struct eap_hdr) - 1);
144
145         if (config->otp) {
146                 wpa_printf(MSG_DEBUG, "EAP-GTC: Forgetting used password");
147                 memset(config->otp, 0, config->otp_len);
148                 free(config->otp);
149                 config->otp = NULL;
150                 config->otp_len = 0;
151         }
152
153         return (u8 *) resp;
154 }
155
156
157 const struct eap_method eap_method_gtc =
158 {
159         .method = EAP_TYPE_GTC,
160         .name = "GTC",
161         .init = eap_gtc_init,
162         .deinit = eap_gtc_deinit,
163         .process = eap_gtc_process,
164 };