c181c5de7df3198d8a17ededc3c165f3007c78b1
[strongswan.git] / src / libstrongswan / eap / eap.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2006 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <stdlib.h>
18 #include <errno.h>
19
20 #include "eap.h"
21
22 #include <utils/debug.h>
23
24 ENUM(eap_code_names, EAP_REQUEST, EAP_FAILURE,
25 "EAP_REQUEST",
26 "EAP_RESPONSE",
27 "EAP_SUCCESS",
28 "EAP_FAILURE",
29 );
30
31 ENUM(eap_code_short_names, EAP_REQUEST, EAP_FAILURE,
32 "REQ",
33 "RES",
34 "SUCC",
35 "FAIL",
36 );
37
38 ENUM_BEGIN(eap_type_names, EAP_IDENTITY, EAP_GTC,
39 "EAP_IDENTITY",
40 "EAP_NOTIFICATION",
41 "EAP_NAK",
42 "EAP_MD5",
43 "EAP_OTP",
44 "EAP_GTC");
45 ENUM_NEXT(eap_type_names, EAP_TLS, EAP_TLS, EAP_GTC,
46 "EAP_TLS");
47 ENUM_NEXT(eap_type_names, EAP_SIM, EAP_SIM, EAP_TLS,
48 "EAP_SIM");
49 ENUM_NEXT(eap_type_names, EAP_TTLS, EAP_TTLS, EAP_SIM,
50 "EAP_TTLS");
51 ENUM_NEXT(eap_type_names, EAP_AKA, EAP_AKA, EAP_TTLS,
52 "EAP_AKA");
53 ENUM_NEXT(eap_type_names, EAP_PEAP, EAP_MSCHAPV2, EAP_AKA,
54 "EAP_PEAP",
55 "EAP_MSCHAPV2");
56 ENUM_NEXT(eap_type_names, EAP_MSTLV, EAP_MSTLV, EAP_MSCHAPV2,
57 "EAP_MSTLV");
58 ENUM_NEXT(eap_type_names, EAP_TNC, EAP_TNC, EAP_MSTLV,
59 "EAP_TNC");
60 ENUM_NEXT(eap_type_names, EAP_EXPANDED, EAP_DYNAMIC, EAP_TNC,
61 "EAP_EXPANDED",
62 "EAP_EXPERIMENTAL",
63 "EAP_RADIUS",
64 "EAP_DYNAMIC");
65 ENUM_END(eap_type_names, EAP_DYNAMIC);
66
67 ENUM_BEGIN(eap_type_short_names, EAP_IDENTITY, EAP_GTC,
68 "ID",
69 "NTF",
70 "NAK",
71 "MD5",
72 "OTP",
73 "GTC");
74 ENUM_NEXT(eap_type_short_names, EAP_TLS, EAP_TLS, EAP_GTC,
75 "TLS");
76 ENUM_NEXT(eap_type_short_names, EAP_SIM, EAP_SIM, EAP_TLS,
77 "SIM");
78 ENUM_NEXT(eap_type_short_names, EAP_TTLS, EAP_TTLS, EAP_SIM,
79 "TTLS");
80 ENUM_NEXT(eap_type_short_names, EAP_AKA, EAP_AKA, EAP_TTLS,
81 "AKA");
82 ENUM_NEXT(eap_type_short_names, EAP_PEAP, EAP_MSCHAPV2, EAP_AKA,
83 "PEAP",
84 "MSCHAPV2");
85 ENUM_NEXT(eap_type_short_names, EAP_MSTLV, EAP_MSTLV, EAP_MSCHAPV2,
86 "MSTLV");
87 ENUM_NEXT(eap_type_short_names, EAP_TNC, EAP_TNC, EAP_MSTLV,
88 "TNC");
89 ENUM_NEXT(eap_type_short_names, EAP_EXPANDED, EAP_DYNAMIC, EAP_TNC,
90 "EXP",
91 "XP",
92 "RAD",
93 "DYN");
94 ENUM_END(eap_type_short_names, EAP_DYNAMIC);
95
96 /*
97 * See header
98 */
99 eap_type_t eap_type_from_string(char *name)
100 {
101 int i;
102 static struct {
103 char *name;
104 eap_type_t type;
105 } types[] = {
106 {"identity", EAP_IDENTITY},
107 {"md5", EAP_MD5},
108 {"otp", EAP_OTP},
109 {"gtc", EAP_GTC},
110 {"tls", EAP_TLS},
111 {"ttls", EAP_TTLS},
112 {"sim", EAP_SIM},
113 {"aka", EAP_AKA},
114 {"peap", EAP_PEAP},
115 {"mschapv2", EAP_MSCHAPV2},
116 {"tnc", EAP_TNC},
117 {"dynamic", EAP_DYNAMIC},
118 {"radius", EAP_RADIUS},
119 };
120
121 for (i = 0; i < countof(types); i++)
122 {
123 if (strcaseeq(name, types[i].name))
124 {
125 return types[i].type;
126 }
127 }
128 return 0;
129 }
130
131 /*
132 * See header
133 */
134 eap_vendor_type_t *eap_vendor_type_from_string(char *str)
135 {
136 enumerator_t *enumerator;
137 eap_vendor_type_t *result = NULL;
138 eap_type_t type = 0;
139 u_int32_t vendor = 0;
140 char *part, *end;
141
142 /* parse EAP method string of the form: [eap-]type[-vendor] */
143 enumerator = enumerator_create_token(str, "-", " ");
144 while (enumerator->enumerate(enumerator, &part))
145 {
146 if (!type)
147 {
148 if (streq(part, "eap"))
149 { /* skip 'eap' at the beginning */
150 continue;
151 }
152 type = eap_type_from_string(part);
153 if (!type)
154 {
155 type = strtoul(part, &end, 0);
156 if (*end != '\0' || errno)
157 {
158 DBG1(DBG_LIB, "unknown or invalid EAP method: %s", part);
159 break;
160 }
161 }
162 continue;
163 }
164 vendor = strtoul(part, &end, 0);
165 if (*end != '\0' || errno)
166 {
167 DBG1(DBG_LIB, "invalid EAP vendor: %s", part);
168 type = 0;
169 }
170 break;
171 }
172 enumerator->destroy(enumerator);
173
174 if (type)
175 {
176 INIT(result,
177 .type = type,
178 .vendor = vendor,
179 );
180 }
181 return result;
182 }