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