Implemented PT-EAP protocol (RFC 7171)
[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_PT_EAP, EAP_PT_EAP, EAP_TNC,
61 "EAP_PT_EAP");
62 ENUM_NEXT(eap_type_names, EAP_EXPANDED, EAP_DYNAMIC, EAP_PT_EAP,
63 "EAP_EXPANDED",
64 "EAP_EXPERIMENTAL",
65 "EAP_RADIUS",
66 "EAP_DYNAMIC");
67 ENUM_END(eap_type_names, EAP_DYNAMIC);
68
69 ENUM_BEGIN(eap_type_short_names, EAP_IDENTITY, EAP_GTC,
70 "ID",
71 "NTF",
72 "NAK",
73 "MD5",
74 "OTP",
75 "GTC");
76 ENUM_NEXT(eap_type_short_names, EAP_TLS, EAP_TLS, EAP_GTC,
77 "TLS");
78 ENUM_NEXT(eap_type_short_names, EAP_SIM, EAP_SIM, EAP_TLS,
79 "SIM");
80 ENUM_NEXT(eap_type_short_names, EAP_TTLS, EAP_TTLS, EAP_SIM,
81 "TTLS");
82 ENUM_NEXT(eap_type_short_names, EAP_AKA, EAP_AKA, EAP_TTLS,
83 "AKA");
84 ENUM_NEXT(eap_type_short_names, EAP_PEAP, EAP_MSCHAPV2, EAP_AKA,
85 "PEAP",
86 "MSCHAPV2");
87 ENUM_NEXT(eap_type_short_names, EAP_MSTLV, EAP_MSTLV, EAP_MSCHAPV2,
88 "MSTLV");
89 ENUM_NEXT(eap_type_short_names, EAP_TNC, EAP_TNC, EAP_MSTLV,
90 "TNC");
91 ENUM_NEXT(eap_type_short_names, EAP_PT_EAP, EAP_PT_EAP, EAP_TNC,
92 "PT");
93 ENUM_NEXT(eap_type_short_names, EAP_EXPANDED, EAP_DYNAMIC, EAP_PT_EAP,
94 "EXP",
95 "XP",
96 "RAD",
97 "DYN");
98 ENUM_END(eap_type_short_names, EAP_DYNAMIC);
99
100 /*
101 * See header
102 */
103 eap_type_t eap_type_from_string(char *name)
104 {
105 int i;
106 static struct {
107 char *name;
108 eap_type_t type;
109 } types[] = {
110 {"identity", EAP_IDENTITY},
111 {"md5", EAP_MD5},
112 {"otp", EAP_OTP},
113 {"gtc", EAP_GTC},
114 {"tls", EAP_TLS},
115 {"ttls", EAP_TTLS},
116 {"sim", EAP_SIM},
117 {"aka", EAP_AKA},
118 {"peap", EAP_PEAP},
119 {"mschapv2", EAP_MSCHAPV2},
120 {"tnc", EAP_TNC},
121 {"pt", EAP_PT_EAP},
122 {"dynamic", EAP_DYNAMIC},
123 {"radius", EAP_RADIUS},
124 };
125
126 for (i = 0; i < countof(types); i++)
127 {
128 if (strcaseeq(name, types[i].name))
129 {
130 return types[i].type;
131 }
132 }
133 return 0;
134 }
135
136 /*
137 * See header
138 */
139 eap_vendor_type_t *eap_vendor_type_from_string(char *str)
140 {
141 enumerator_t *enumerator;
142 eap_vendor_type_t *result = NULL;
143 eap_type_t type = 0;
144 uint32_t vendor = 0;
145 char *part, *end;
146
147 /* parse EAP method string of the form: [eap-]type[-vendor] */
148 enumerator = enumerator_create_token(str, "-", " ");
149 while (enumerator->enumerate(enumerator, &part))
150 {
151 if (!type)
152 {
153 if (streq(part, "eap"))
154 { /* skip 'eap' at the beginning */
155 continue;
156 }
157 type = eap_type_from_string(part);
158 if (!type)
159 {
160 type = strtoul(part, &end, 0);
161 if (*end != '\0' || errno)
162 {
163 DBG1(DBG_LIB, "unknown or invalid EAP method: %s", part);
164 break;
165 }
166 }
167 continue;
168 }
169 vendor = strtoul(part, &end, 0);
170 if (*end != '\0' || errno)
171 {
172 DBG1(DBG_LIB, "invalid EAP vendor: %s", part);
173 type = 0;
174 }
175 break;
176 }
177 enumerator->destroy(enumerator);
178
179 if (type)
180 {
181 INIT(result,
182 .type = type,
183 .vendor = vendor,
184 );
185 }
186 return result;
187 }