summaryrefslogtreecommitdiffstats
path: root/contrib/wpa_supplicant/eap_methods.c
blob: 53c7e629d0b77bc381cfae0c96b4298690b7c032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
 * EAP peer: Method registration
 * Copyright (c) 2004-2006, Jouni Malinen <j@w1.fi>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Alternatively, this software may be distributed under the terms of BSD
 * license.
 *
 * See README and COPYING for more details.
 */

#include "includes.h"
#ifdef CONFIG_DYNAMIC_EAP_METHODS
#include <dlfcn.h>
#endif /* CONFIG_DYNAMIC_EAP_METHODS */

#include "common.h"
#include "eap_i.h"
#include "eap_methods.h"


static struct eap_method *eap_methods = NULL;


/**
 * eap_sm_get_eap_methods - Get EAP method based on type number
 * @vendor: EAP Vendor-Id (0 = IETF)
 * @method: EAP type number
 * Returns: Pointer to EAP method or %NULL if not found
 */
const struct eap_method * eap_sm_get_eap_methods(int vendor, EapType method)
{
	struct eap_method *m;
	for (m = eap_methods; m; m = m->next) {
		if (m->vendor == vendor && m->method == method)
			return m;
	}
	return NULL;
}


/**
 * eap_get_type - Get EAP type for the given EAP method name
 * @name: EAP method name, e.g., TLS
 * @vendor: Buffer for returning EAP Vendor-Id
 * Returns: EAP method type or %EAP_TYPE_NONE if not found
 *
 * This function maps EAP type names into EAP type numbers based on the list of
 * EAP methods included in the build.
 */
EapType eap_get_type(const char *name, int *vendor)
{
	struct eap_method *m;
	for (m = eap_methods; m; m = m->next) {
		if (os_strcmp(m->name, name) == 0) {
			*vendor = m->vendor;
			return m->method;
		}
	}
	*vendor = EAP_VENDOR_IETF;
	return EAP_TYPE_NONE;
}


/**
 * eap_get_name - Get EAP method name for the given EAP type
 * @vendor: EAP Vendor-Id (0 = IETF)
 * @type: EAP method type
 * Returns: EAP method name, e.g., TLS, or %NULL if not found
 *
 * This function maps EAP type numbers into EAP type names based on the list of
 * EAP methods included in the build.
 */
const char * eap_get_name(int vendor, EapType type)
{
	struct eap_method *m;
	for (m = eap_methods; m; m = m->next) {
		if (m->vendor == vendor && m->method == type)
			return m->name;
	}
	return NULL;
}


/**
 * eap_get_names - Get space separated list of names for supported EAP methods
 * @buf: Buffer for names
 * @buflen: Buffer length
 * Returns: Number of characters written into buf (not including nul
 * termination)
 */
size_t eap_get_names(char *buf, size_t buflen)
{
	char *pos, *end;
	struct eap_method *m;
	int ret;

	if (buflen == 0)
		return 0;

	pos = buf;
	end = pos + buflen;

	for (m = eap_methods; m; m = m->next) {
		ret = os_snprintf(pos, end - pos, "%s%s",
				  m == eap_methods ? "" : " ", m->name);
		if (ret < 0 || ret >= end - pos)
			break;
		pos += ret;
	}
	buf[buflen - 1] = '\0';

	return pos - buf;
}


/**
 * eap_get_names_as_string_array - Get supported EAP methods as string array
 * @num: Buffer for returning the number of items in array, not including %NULL
 * terminator. This parameter can be %NULL if the length is not needed.
 * Returns: A %NULL-terminated array of strings, or %NULL on error.
 *
 * This function returns the list of names for all supported EAP methods as an
 * array of strings. The caller must free the returned array items and the
 * array.
 */
char ** eap_get_names_as_string_array(size_t *num)
{
	struct eap_method *m;
	size_t array_len = 0;
	char **array;
	int i = 0, j;

	for (m = eap_methods; m; m = m->next)
		array_len++;

	array = os_zalloc(sizeof(char *) * (array_len + 1));
	if (array == NULL)
		return NULL;

	for (m = eap_methods; m; m = m->next) {
		array[i++] = os_strdup(m->name);
		if (array[i - 1] == NULL) {
			for (j = 0; j < i; j++)
				os_free(array[j]);
			os_free(array);
			return NULL;
		}
	}
	array[i] = NULL;

	if (num)
		*num = array_len;

	return array;
}


/**
 * eap_peer_get_methods - Get a list of enabled EAP peer methods
 * @count: Set to number of available methods
 * Returns: List of enabled EAP peer methods
 */
const struct eap_method * eap_peer_get_methods(size_t *count)
{
	int c = 0;
	struct eap_method *m;

	for (m = eap_methods; m; m = m->next)
		c++;
	
	*count = c;
	return eap_methods;
}


#ifdef CONFIG_DYNAMIC_EAP_METHODS
/**
 * eap_peer_method_load - Load a dynamic EAP method library (shared object)
 * @so: File path for the shared object file to load
 * Returns: 0 on success, -1 on failure
 */
int eap_peer_method_load(const char *so)
{
	void *handle;
	int (*dyn_init)(void);
	int ret;

	handle = dlopen(so, RTLD_LAZY);
	if (handle == NULL) {
		wpa_printf(MSG_ERROR, "EAP: Failed to open dynamic EAP method "
			   "'%s': %s", so, dlerror());
		return -1;
	}

	dyn_init = dlsym(handle, "eap_peer_method_dynamic_init");
	if (dyn_init == NULL) {
		dlclose(handle);
		wpa_printf(MSG_ERROR, "EAP: Invalid EAP method '%s' - no "
			   "eap_peer_method_dynamic_init()", so);
		return -1;
	}

	ret = dyn_init();
	if (ret) {
		dlclose(handle);
		wpa_printf(MSG_ERROR, "EAP: Failed to add EAP method '%s' - "
			   "ret %d", so, ret);
		return ret;
	}

	/* Store the handle for this shared object. It will be freed with
	 * dlclose() when the EAP method is unregistered. */
	eap_methods->dl_handle = handle;

	wpa_printf(MSG_DEBUG, "EAP: Loaded dynamic EAP method: '%s'", so);

	return 0;
}


/**
 * eap_peer_method_unload - Unload a dynamic EAP method library (shared object)
 * @method: Pointer to the dynamically loaded EAP method
 * Returns: 0 on success, -1 on failure
 *
 * This function can be used to unload EAP methods that have been previously
 * loaded with eap_peer_method_load(). Before unloading the method, all
 * references to the method must be removed to make sure that no dereferences
 * of freed memory will occur after unloading.
 */
int eap_peer_method_unload(struct eap_method *method)
{
	struct eap_method *m, *prev;
	void *handle;

	m = eap_methods;
	prev = NULL;
	while (m) {
		if (m == method)
			break;
		prev = m;
		m = m->next;
	}

	if (m == NULL || m->dl_handle == NULL)
		return -1;

	if (prev)
		prev->next = m->next;
	else
		eap_methods = m->next;

	handle = m->dl_handle;

	if (m->free)
		m->free(m);
	else
		eap_peer_method_free(m);

	dlclose(handle);

	return 0;
}
#endif /* CONFIG_DYNAMIC_EAP_METHODS */


/**
 * eap_peer_method_alloc - Allocate EAP peer method structure
 * @version: Version of the EAP peer method interface (set to
 * EAP_PEER_METHOD_INTERFACE_VERSION)
 * @vendor: EAP Vendor-ID (EAP_VENDOR_*) (0 = IETF)
 * @method: EAP type number (EAP_TYPE_*)
 * @name: Name of the method (e.g., "TLS")
 * Returns: Allocated EAP method structure or %NULL on failure
 *
 * The returned structure should be freed with eap_peer_method_free() when it
 * is not needed anymore.
 */
struct eap_method * eap_peer_method_alloc(int version, int vendor,
					  EapType method, const char *name)
{
	struct eap_method *eap;
	eap = os_zalloc(sizeof(*eap));
	if (eap == NULL)
		return NULL;
	eap->version = version;
	eap->vendor = vendor;
	eap->method = method;
	eap->name = name;
	return eap;
}


/**
 * eap_peer_method_free - Free EAP peer method structure
 * @method: Method structure allocated with eap_peer_method_alloc()
 */
void eap_peer_method_free(struct eap_method *method)
{
	os_free(method);
}


/**
 * eap_peer_method_register - Register an EAP peer method
 * @method: EAP method to register
 * Returns: 0 on success, -1 on invalid method, or -2 if a matching EAP method
 * has already been registered
 *
 * Each EAP peer method needs to call this function to register itself as a
 * supported EAP method.
 */
int eap_peer_method_register(struct eap_method *method)
{
	struct eap_method *m, *last = NULL;

	if (method == NULL || method->name == NULL ||
	    method->version != EAP_PEER_METHOD_INTERFACE_VERSION)
		return -1;

	for (m = eap_methods; m; m = m->next) {
		if ((m->vendor == method->vendor &&
		     m->method == method->method) ||
		    os_strcmp(m->name, method->name) == 0)
			return -2;
		last = m;
	}

	if (last)
		last->next = method;
	else
		eap_methods = method;

	return 0;
}


/**
 * eap_peer_register_methods - Register statically linked EAP peer methods
 * Returns: 0 on success, -1 on failure
 *
 * This function is called at program initialization to register all EAP peer
 * methods that were linked in statically.
 */
int eap_peer_register_methods(void)
{
	int ret = 0;

#ifdef EAP_MD5
	if (ret == 0) {
		int eap_peer_md5_register(void);
		ret = eap_peer_md5_register();
	}
#endif /* EAP_MD5 */

#ifdef EAP_TLS
	if (ret == 0) {
		int eap_peer_tls_register(void);
		ret = eap_peer_tls_register();
	}
#endif /* EAP_TLS */

#ifdef EAP_MSCHAPv2
	if (ret == 0) {
		int eap_peer_mschapv2_register(void);
		ret = eap_peer_mschapv2_register();
	}
#endif /* EAP_MSCHAPv2 */

#ifdef EAP_PEAP
	if (ret == 0) {
		int eap_peer_peap_register(void);
		ret = eap_peer_peap_register();
	}
#endif /* EAP_PEAP */

#ifdef EAP_TTLS
	if (ret == 0) {
		int eap_peer_ttls_register(void);
		ret = eap_peer_ttls_register();
	}
#endif /* EAP_TTLS */

#ifdef EAP_GTC
	if (ret == 0) {
		int eap_peer_gtc_register(void);
		ret = eap_peer_gtc_register();
	}
#endif /* EAP_GTC */

#ifdef EAP_OTP
	if (ret == 0) {
		int eap_peer_otp_register(void);
		ret = eap_peer_otp_register();
	}
#endif /* EAP_OTP */

#ifdef EAP_SIM
	if (ret == 0) {
		int eap_peer_sim_register(void);
		ret = eap_peer_sim_register();
	}
#endif /* EAP_SIM */

#ifdef EAP_LEAP
	if (ret == 0) {
		int eap_peer_leap_register(void);
		ret = eap_peer_leap_register();
	}
#endif /* EAP_LEAP */

#ifdef EAP_PSK
	if (ret == 0) {
		int eap_peer_psk_register(void);
		ret = eap_peer_psk_register();
	}
#endif /* EAP_PSK */

#ifdef EAP_AKA
	if (ret == 0) {
		int eap_peer_aka_register(void);
		ret = eap_peer_aka_register();
	}
#endif /* EAP_AKA */

#ifdef EAP_FAST
	if (ret == 0) {
		int eap_peer_fast_register(void);
		ret = eap_peer_fast_register();
	}
#endif /* EAP_FAST */

#ifdef EAP_PAX
	if (ret == 0) {
		int eap_peer_pax_register(void);
		ret = eap_peer_pax_register();
	}
#endif /* EAP_PAX */

#ifdef EAP_SAKE
	if (ret == 0) {
		int eap_peer_sake_register(void);
		ret = eap_peer_sake_register();
	}
#endif /* EAP_SAKE */

#ifdef EAP_GPSK
	if (ret == 0) {
		int eap_peer_gpsk_register(void);
		ret = eap_peer_gpsk_register();
	}
#endif /* EAP_GPSK */

#ifdef EAP_VENDOR_TEST
	if (ret == 0) {
		int eap_peer_vendor_test_register(void);
		ret = eap_peer_vendor_test_register();
	}
#endif /* EAP_VENDOR_TEST */

	return ret;
}


/**
 * eap_peer_unregister_methods - Unregister EAP peer methods
 *
 * This function is called at program termination to unregister all EAP peer
 * methods.
 */
void eap_peer_unregister_methods(void)
{
	struct eap_method *m;
#ifdef CONFIG_DYNAMIC_EAP_METHODS
	void *handle;
#endif /* CONFIG_DYNAMIC_EAP_METHODS */

	while (eap_methods) {
		m = eap_methods;
		eap_methods = eap_methods->next;

#ifdef CONFIG_DYNAMIC_EAP_METHODS
		handle = m->dl_handle;
#endif /* CONFIG_DYNAMIC_EAP_METHODS */

		if (m->free)
			m->free(m);
		else
			eap_peer_method_free(m);

#ifdef CONFIG_DYNAMIC_EAP_METHODS
		if (handle)
			dlclose(handle);
#endif /* CONFIG_DYNAMIC_EAP_METHODS */
	}
}
OpenPOWER on IntegriCloud