Merge from vendor branch FILE:
[dragonfly.git] / contrib / hostapd-0.4.9 / eap_identity.c
1 /*
2  * hostapd / EAP-Identity
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 #include <netinet/in.h>
19
20 #include "hostapd.h"
21 #include "common.h"
22 #include "eap_i.h"
23
24
25 struct eap_identity_data {
26         enum { CONTINUE, SUCCESS, FAILURE } state;
27         int pick_up;
28 };
29
30
31 static void * eap_identity_init(struct eap_sm *sm)
32 {
33         struct eap_identity_data *data;
34
35         data = malloc(sizeof(*data));
36         if (data == NULL)
37                 return data;
38         memset(data, 0, sizeof(*data));
39         data->state = CONTINUE;
40
41         return data;
42 }
43
44
45 static void * eap_identity_initPickUp(struct eap_sm *sm)
46 {
47         struct eap_identity_data *data;
48         data = eap_identity_init(sm);
49         if (data) {
50                 data->pick_up = 1;
51         }
52         return data;
53 }
54
55
56 static void eap_identity_reset(struct eap_sm *sm, void *priv)
57 {
58         struct eap_identity_data *data = priv;
59         free(data);
60 }
61
62
63 static u8 * eap_identity_buildReq(struct eap_sm *sm, void *priv, int id,
64                              size_t *reqDataLen)
65 {
66         struct eap_identity_data *data = priv;
67         struct eap_hdr *req;
68         u8 *pos;
69         const char *req_data;
70         size_t req_data_len;
71
72         if (sm->eapol_cb->get_eap_req_id_text) {
73                 req_data = sm->eapol_cb->get_eap_req_id_text(sm->eapol_ctx,
74                                                              &req_data_len);
75         } else {
76                 req_data = NULL;
77                 req_data_len = 0;
78         }
79         *reqDataLen = sizeof(*req) + 1 + req_data_len;
80         req = malloc(*reqDataLen);
81         if (req == NULL) {
82                 wpa_printf(MSG_ERROR, "EAP-Identity: Failed to allocate "
83                            "memory for request");
84                 data->state = FAILURE;
85                 return NULL;
86         }
87
88         req->code = EAP_CODE_REQUEST;
89         req->identifier = id;
90         req->length = htons(*reqDataLen);
91         pos = (u8 *) (req + 1);
92         *pos++ = EAP_TYPE_IDENTITY;
93         if (req_data)
94                 memcpy(pos, req_data, req_data_len);
95
96         return (u8 *) req;
97 }
98
99
100 static Boolean eap_identity_check(struct eap_sm *sm, void *priv,
101                              u8 *respData, size_t respDataLen)
102 {
103         struct eap_hdr *resp;
104         u8 *pos;
105         size_t len;
106
107         resp = (struct eap_hdr *) respData;
108         pos = (u8 *) (resp + 1);
109         if (respDataLen < sizeof(*resp) + 1 || *pos != EAP_TYPE_IDENTITY ||
110             (len = ntohs(resp->length)) > respDataLen) {
111                 wpa_printf(MSG_INFO, "EAP-Identity: Invalid frame");
112                 return TRUE;
113         }
114
115         return FALSE;
116 }
117
118
119 static void eap_identity_process(struct eap_sm *sm, void *priv,
120                             u8 *respData, size_t respDataLen)
121 {
122         struct eap_identity_data *data = priv;
123         struct eap_hdr *resp;
124         u8 *pos;
125         int len;
126
127         if (data->pick_up) {
128                 if (eap_identity_check(sm, data, respData, respDataLen)) {
129                         wpa_printf(MSG_DEBUG, "EAP-Identity: failed to pick "
130                                    "up already started negotiation");
131                         data->state = FAILURE;
132                         return;
133                 }
134                 data->pick_up = 0;
135         }
136
137         resp = (struct eap_hdr *) respData;
138         len = ntohs(resp->length);
139         pos = (u8 *) (resp + 1);
140         pos++;
141         len -= sizeof(*resp) + 1;
142         if (len < 0) {
143                 data->state = FAILURE;
144                 return;
145         }
146         wpa_hexdump_ascii(MSG_DEBUG, "EAP-Identity: Peer identity", pos, len);
147         free(sm->identity);
148         sm->identity = malloc(len);
149         if (sm->identity == NULL) {
150                 data->state = FAILURE;
151         } else {
152                 memcpy(sm->identity, pos, len);
153                 sm->identity_len = len;
154                 data->state = SUCCESS;
155         }
156 }
157
158
159 static Boolean eap_identity_isDone(struct eap_sm *sm, void *priv)
160 {
161         struct eap_identity_data *data = priv;
162         return data->state != CONTINUE;
163 }
164
165
166 static Boolean eap_identity_isSuccess(struct eap_sm *sm, void *priv)
167 {
168         struct eap_identity_data *data = priv;
169         return data->state == SUCCESS;
170 }
171
172
173 const struct eap_method eap_method_identity =
174 {
175         .method = EAP_TYPE_IDENTITY,
176         .name = "Identity",
177         .init = eap_identity_init,
178         .initPickUp = eap_identity_initPickUp,
179         .reset = eap_identity_reset,
180         .buildReq = eap_identity_buildReq,
181         .check = eap_identity_check,
182         .process = eap_identity_process,
183         .isDone = eap_identity_isDone,
184         .isSuccess = eap_identity_isSuccess,
185 };