xref: /freebsd/contrib/wpa/src/eapol_supp/eapol_supp_sm.h (revision 36712a94975f5bd0d26c85377283b49a2369c82f)
1 /*
2  * EAPOL supplicant state machines
3  * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #ifndef EAPOL_SUPP_SM_H
10 #define EAPOL_SUPP_SM_H
11 
12 #include "common/defs.h"
13 
14 struct tls_cert_data;
15 
16 typedef enum { Unauthorized, Authorized } PortStatus;
17 typedef enum { Auto, ForceUnauthorized, ForceAuthorized } PortControl;
18 
19 /**
20  * struct eapol_config - Per network configuration for EAPOL state machines
21  */
22 struct eapol_config {
23 	/**
24 	 * accept_802_1x_keys - Accept IEEE 802.1X (non-WPA) EAPOL-Key frames
25 	 *
26 	 * This variable should be set to 1 when using EAPOL state machines
27 	 * with non-WPA security policy to generate dynamic WEP keys. When
28 	 * using WPA, this should be set to 0 so that WPA state machine can
29 	 * process the EAPOL-Key frames.
30 	 */
31 	int accept_802_1x_keys;
32 
33 #define EAPOL_REQUIRE_KEY_UNICAST BIT(0)
34 #define EAPOL_REQUIRE_KEY_BROADCAST BIT(1)
35 	/**
36 	 * required_keys - Which EAPOL-Key packets are required
37 	 *
38 	 * This variable determines which EAPOL-Key packets are required before
39 	 * marking connection authenticated. This is a bit field of
40 	 * EAPOL_REQUIRE_KEY_UNICAST and EAPOL_REQUIRE_KEY_BROADCAST flags.
41 	 */
42 	int required_keys;
43 
44 	/**
45 	 * fast_reauth - Whether fast EAP reauthentication is enabled
46 	 */
47 	int fast_reauth;
48 
49 	/**
50 	 * workaround - Whether EAP workarounds are enabled
51 	 */
52 	unsigned int workaround;
53 
54 	/**
55 	 * eap_disabled - Whether EAP is disabled
56 	 */
57 	int eap_disabled;
58 
59 	/**
60 	 * external_sim - Use external processing for SIM/USIM operations
61 	 */
62 	int external_sim;
63 
64 #define EAPOL_LOCAL_WPS_IN_USE BIT(0)
65 #define EAPOL_PEER_IS_WPS20_AP BIT(1)
66 	/**
67 	 * wps - Whether this connection is used for WPS
68 	 */
69 	int wps;
70 };
71 
72 struct eapol_sm;
73 struct wpa_config_blob;
74 
75 enum eapol_supp_result {
76 	EAPOL_SUPP_RESULT_FAILURE,
77 	EAPOL_SUPP_RESULT_SUCCESS,
78 	EAPOL_SUPP_RESULT_EXPECTED_FAILURE
79 };
80 
81 /**
82  * struct eapol_ctx - Global (for all networks) EAPOL state machine context
83  */
84 struct eapol_ctx {
85 	/**
86 	 * ctx - Pointer to arbitrary upper level context
87 	 */
88 	void *ctx;
89 
90 	/**
91 	 * preauth - IEEE 802.11i/RSN pre-authentication
92 	 *
93 	 * This EAPOL state machine is used for IEEE 802.11i/RSN
94 	 * pre-authentication
95 	 */
96 	int preauth;
97 
98 	/**
99 	 * cb - Function to be called when EAPOL negotiation has been completed
100 	 * @eapol: Pointer to EAPOL state machine data
101 	 * @result: Whether the authentication was completed successfully
102 	 * @ctx: Pointer to context data (cb_ctx)
103 	 *
104 	 * This optional callback function will be called when the EAPOL
105 	 * authentication has been completed. This allows the owner of the
106 	 * EAPOL state machine to process the key and terminate the EAPOL state
107 	 * machine. Currently, this is used only in RSN pre-authentication.
108 	 */
109 	void (*cb)(struct eapol_sm *eapol, enum eapol_supp_result result,
110 		   void *ctx);
111 
112 	/**
113 	 * cb_ctx - Callback context for cb()
114 	 */
115 	void *cb_ctx;
116 
117 	/**
118 	 * msg_ctx - Callback context for wpa_msg() calls
119 	 */
120 	void *msg_ctx;
121 
122 	/**
123 	 * scard_ctx - Callback context for PC/SC scard_*() function calls
124 	 *
125 	 * This context can be updated with eapol_sm_register_scard_ctx().
126 	 */
127 	void *scard_ctx;
128 
129 	/**
130 	 * eapol_send_ctx - Callback context for eapol_send() calls
131 	 */
132 	void *eapol_send_ctx;
133 
134 	/**
135 	 * eapol_done_cb - Function to be called at successful completion
136 	 * @ctx: Callback context (ctx)
137 	 *
138 	 * This function is called at the successful completion of EAPOL
139 	 * authentication. If dynamic WEP keys are used, this is called only
140 	 * after all the expected keys have been received.
141 	 */
142 	void (*eapol_done_cb)(void *ctx);
143 
144 	/**
145 	 * eapol_send - Send EAPOL packets
146 	 * @ctx: Callback context (eapol_send_ctx)
147 	 * @type: EAPOL type (IEEE802_1X_TYPE_*)
148 	 * @buf: Pointer to EAPOL payload
149 	 * @len: Length of the EAPOL payload
150 	 * Returns: 0 on success, -1 on failure
151 	 */
152 	int (*eapol_send)(void *ctx, int type, const u8 *buf, size_t len);
153 
154 	/**
155 	 * set_wep_key - Configure WEP keys
156 	 * @ctx: Callback context (ctx)
157 	 * @unicast: Non-zero = unicast, 0 = multicast/broadcast key
158 	 * @keyidx: Key index (0..3)
159 	 * @key: WEP key
160 	 * @keylen: Length of the WEP key
161 	 * Returns: 0 on success, -1 on failure
162 	 */
163 	int (*set_wep_key)(void *ctx, int unicast, int keyidx,
164 			   const u8 *key, size_t keylen);
165 
166 	/**
167 	 * set_config_blob - Set or add a named configuration blob
168 	 * @ctx: Callback context (ctx)
169 	 * @blob: New value for the blob
170 	 *
171 	 * Adds a new configuration blob or replaces the current value of an
172 	 * existing blob.
173 	 */
174 	void (*set_config_blob)(void *ctx, struct wpa_config_blob *blob);
175 
176 	/**
177 	 * get_config_blob - Get a named configuration blob
178 	 * @ctx: Callback context (ctx)
179 	 * @name: Name of the blob
180 	 * Returns: Pointer to blob data or %NULL if not found
181 	 */
182 	const struct wpa_config_blob * (*get_config_blob)(void *ctx,
183 							  const char *name);
184 
185 	/**
186 	 * aborted_cached - Notify that cached PMK attempt was aborted
187 	 * @ctx: Callback context (ctx)
188 	 */
189 	void (*aborted_cached)(void *ctx);
190 
191 	/**
192 	 * opensc_engine_path - Path to the OpenSSL engine for opensc
193 	 *
194 	 * This is an OpenSSL specific configuration option for loading OpenSC
195 	 * engine (engine_opensc.so); if %NULL, this engine is not loaded.
196 	 */
197 	const char *opensc_engine_path;
198 
199 	/**
200 	 * pkcs11_engine_path - Path to the OpenSSL engine for PKCS#11
201 	 *
202 	 * This is an OpenSSL specific configuration option for loading PKCS#11
203 	 * engine (engine_pkcs11.so); if %NULL, this engine is not loaded.
204 	 */
205 	const char *pkcs11_engine_path;
206 
207 	/**
208 	 * pkcs11_module_path - Path to the OpenSSL OpenSC/PKCS#11 module
209 	 *
210 	 * This is an OpenSSL specific configuration option for configuring
211 	 * path to OpenSC/PKCS#11 engine (opensc-pkcs11.so); if %NULL, this
212 	 * module is not loaded.
213 	 */
214 	const char *pkcs11_module_path;
215 
216 	/**
217 	 * openssl_ciphers - OpenSSL cipher string
218 	 *
219 	 * This is an OpenSSL specific configuration option for configuring the
220 	 * default ciphers. If not set, "DEFAULT:!EXP:!LOW" is used as the
221 	 * default.
222 	 */
223 	const char *openssl_ciphers;
224 
225 	/**
226 	 * wps - WPS context data
227 	 *
228 	 * This is only used by EAP-WSC and can be left %NULL if not available.
229 	 */
230 	struct wps_context *wps;
231 
232 	/**
233 	 * eap_param_needed - Notify that EAP parameter is needed
234 	 * @ctx: Callback context (ctx)
235 	 * @field: Field indicator (e.g., WPA_CTRL_REQ_EAP_IDENTITY)
236 	 * @txt: User readable text describing the required parameter
237 	 */
238 	void (*eap_param_needed)(void *ctx, enum wpa_ctrl_req_type field,
239 				 const char *txt);
240 
241 	/**
242 	 * port_cb - Set port authorized/unauthorized callback (optional)
243 	 * @ctx: Callback context (ctx)
244 	 * @authorized: Whether the supplicant port is now in authorized state
245 	 */
246 	void (*port_cb)(void *ctx, int authorized);
247 
248 	/**
249 	 * cert_cb - Notification of a peer certificate
250 	 * @ctx: Callback context (ctx)
251 	 * @cert: Certificate information
252 	 * @cert_hash: SHA-256 hash of the certificate
253 	 */
254 	void (*cert_cb)(void *ctx, struct tls_cert_data *cert,
255 			const char *cert_hash);
256 
257 	/**
258 	 * cert_in_cb - Include server certificates in callback
259 	 */
260 	int cert_in_cb;
261 
262 	/**
263 	 * status_cb - Notification of a change in EAP status
264 	 * @ctx: Callback context (ctx)
265 	 * @status: Step in the process of EAP authentication
266 	 * @parameter: Step-specific parameter, e.g., EAP method name
267 	 */
268 	void (*status_cb)(void *ctx, const char *status,
269 			  const char *parameter);
270 
271 	/**
272 	 * eap_error_cb - Notification of EAP method error
273 	 * @ctx: Callback context (ctx)
274 	 * @error_code: EAP method error code
275 	 */
276 	void (*eap_error_cb)(void *ctx, int error_code);
277 
278 #ifdef CONFIG_EAP_PROXY
279 	/**
280 	 * eap_proxy_cb - Callback signifying any updates from eap_proxy
281 	 * @ctx: eapol_ctx from eap_peer_sm_init() call
282 	 */
283 	void (*eap_proxy_cb)(void *ctx);
284 
285 	/**
286 	 * eap_proxy_notify_sim_status - Notification of SIM status change
287 	 * @ctx: eapol_ctx from eap_peer_sm_init() call
288 	 * @status: One of enum value from sim_state
289 	 */
290 	void (*eap_proxy_notify_sim_status)(void *ctx,
291 					    enum eap_proxy_sim_state sim_state);
292 #endif /* CONFIG_EAP_PROXY */
293 
294 	/**
295 	 * set_anon_id - Set or add anonymous identity
296 	 * @ctx: eapol_ctx from eap_peer_sm_init() call
297 	 * @id: Anonymous identity (e.g., EAP-SIM pseudonym)
298 	 * @len: Length of anonymous identity in octets
299 	 */
300 	void (*set_anon_id)(void *ctx, const u8 *id, size_t len);
301 };
302 
303 
304 struct eap_peer_config;
305 struct ext_password_data;
306 
307 #ifdef IEEE8021X_EAPOL
308 struct eapol_sm *eapol_sm_init(struct eapol_ctx *ctx);
309 void eapol_sm_deinit(struct eapol_sm *sm);
310 void eapol_sm_step(struct eapol_sm *sm);
311 int eapol_sm_get_status(struct eapol_sm *sm, char *buf, size_t buflen,
312 			int verbose);
313 int eapol_sm_get_mib(struct eapol_sm *sm, char *buf, size_t buflen);
314 void eapol_sm_configure(struct eapol_sm *sm, int heldPeriod, int authPeriod,
315 			int startPeriod, int maxStart);
316 int eapol_sm_rx_eapol(struct eapol_sm *sm, const u8 *src, const u8 *buf,
317 		      size_t len);
318 void eapol_sm_notify_tx_eapol_key(struct eapol_sm *sm);
319 void eapol_sm_notify_portEnabled(struct eapol_sm *sm, Boolean enabled);
320 void eapol_sm_notify_portValid(struct eapol_sm *sm, Boolean valid);
321 void eapol_sm_notify_eap_success(struct eapol_sm *sm, Boolean success);
322 void eapol_sm_notify_eap_fail(struct eapol_sm *sm, Boolean fail);
323 void eapol_sm_notify_config(struct eapol_sm *sm,
324 			    struct eap_peer_config *config,
325 			    const struct eapol_config *conf);
326 int eapol_sm_get_key(struct eapol_sm *sm, u8 *key, size_t len);
327 const u8 * eapol_sm_get_session_id(struct eapol_sm *sm, size_t *len);
328 void eapol_sm_notify_logoff(struct eapol_sm *sm, Boolean logoff);
329 void eapol_sm_notify_cached(struct eapol_sm *sm);
330 void eapol_sm_notify_pmkid_attempt(struct eapol_sm *sm);
331 void eapol_sm_register_scard_ctx(struct eapol_sm *sm, void *ctx);
332 void eapol_sm_notify_portControl(struct eapol_sm *sm, PortControl portControl);
333 void eapol_sm_notify_ctrl_attached(struct eapol_sm *sm);
334 void eapol_sm_notify_ctrl_response(struct eapol_sm *sm);
335 void eapol_sm_request_reauth(struct eapol_sm *sm);
336 void eapol_sm_notify_lower_layer_success(struct eapol_sm *sm, int in_eapol_sm);
337 void eapol_sm_invalidate_cached_session(struct eapol_sm *sm);
338 const char * eapol_sm_get_method_name(struct eapol_sm *sm);
339 void eapol_sm_set_ext_pw_ctx(struct eapol_sm *sm,
340 			     struct ext_password_data *ext);
341 int eapol_sm_failed(struct eapol_sm *sm);
342 void eapol_sm_erp_flush(struct eapol_sm *sm);
343 struct wpabuf * eapol_sm_build_erp_reauth_start(struct eapol_sm *sm);
344 void eapol_sm_process_erp_finish(struct eapol_sm *sm, const u8 *buf,
345 				 size_t len);
346 int eapol_sm_get_eap_proxy_imsi(void *ctx, int sim_num, char *imsi,
347 				size_t *len);
348 int eapol_sm_update_erp_next_seq_num(struct eapol_sm *sm, u16 next_seq_num);
349 int eapol_sm_get_erp_info(struct eapol_sm *sm, struct eap_peer_config *config,
350 			  const u8 **username, size_t *username_len,
351 			  const u8 **realm, size_t *realm_len,
352 			  u16 *erp_next_seq_num, const u8 **rrk,
353 			  size_t *rrk_len);
354 
355 #else /* IEEE8021X_EAPOL */
356 static inline struct eapol_sm *eapol_sm_init(struct eapol_ctx *ctx)
357 {
358 	free(ctx);
359 	return (struct eapol_sm *) 1;
360 }
361 static inline void eapol_sm_deinit(struct eapol_sm *sm)
362 {
363 }
364 static inline void eapol_sm_step(struct eapol_sm *sm)
365 {
366 }
367 static inline int eapol_sm_get_status(struct eapol_sm *sm, char *buf,
368 				      size_t buflen, int verbose)
369 {
370 	return 0;
371 }
372 static inline int eapol_sm_get_mib(struct eapol_sm *sm, char *buf,
373 				   size_t buflen)
374 {
375 	return 0;
376 }
377 static inline void eapol_sm_configure(struct eapol_sm *sm, int heldPeriod,
378 				      int authPeriod, int startPeriod,
379 				      int maxStart)
380 {
381 }
382 static inline int eapol_sm_rx_eapol(struct eapol_sm *sm, const u8 *src,
383 				    const u8 *buf, size_t len)
384 {
385 	return 0;
386 }
387 static inline void eapol_sm_notify_tx_eapol_key(struct eapol_sm *sm)
388 {
389 }
390 static inline void eapol_sm_notify_portEnabled(struct eapol_sm *sm,
391 					       Boolean enabled)
392 {
393 }
394 static inline void eapol_sm_notify_portValid(struct eapol_sm *sm,
395 					     Boolean valid)
396 {
397 }
398 static inline void eapol_sm_notify_eap_success(struct eapol_sm *sm,
399 					       Boolean success)
400 {
401 }
402 static inline void eapol_sm_notify_eap_fail(struct eapol_sm *sm, Boolean fail)
403 {
404 }
405 static inline void eapol_sm_notify_config(struct eapol_sm *sm,
406 					  struct eap_peer_config *config,
407 					  struct eapol_config *conf)
408 {
409 }
410 static inline int eapol_sm_get_key(struct eapol_sm *sm, u8 *key, size_t len)
411 {
412 	return -1;
413 }
414 static inline const u8 *
415 eapol_sm_get_session_id(struct eapol_sm *sm, size_t *len)
416 {
417 	return NULL;
418 }
419 static inline void eapol_sm_notify_logoff(struct eapol_sm *sm, Boolean logoff)
420 {
421 }
422 static inline void eapol_sm_notify_cached(struct eapol_sm *sm)
423 {
424 }
425 static inline void eapol_sm_notify_pmkid_attempt(struct eapol_sm *sm)
426 {
427 }
428 #define eapol_sm_register_scard_ctx(sm, ctx) do { } while (0)
429 static inline void eapol_sm_notify_portControl(struct eapol_sm *sm,
430 					       PortControl portControl)
431 {
432 }
433 static inline void eapol_sm_notify_ctrl_attached(struct eapol_sm *sm)
434 {
435 }
436 static inline void eapol_sm_notify_ctrl_response(struct eapol_sm *sm)
437 {
438 }
439 static inline void eapol_sm_request_reauth(struct eapol_sm *sm)
440 {
441 }
442 static inline void eapol_sm_notify_lower_layer_success(struct eapol_sm *sm,
443 						       int in_eapol_sm)
444 {
445 }
446 static inline void eapol_sm_invalidate_cached_session(struct eapol_sm *sm)
447 {
448 }
449 static inline const char * eapol_sm_get_method_name(struct eapol_sm *sm)
450 {
451 	return NULL;
452 }
453 static inline void eapol_sm_set_ext_pw_ctx(struct eapol_sm *sm,
454 					   struct ext_password_data *ext)
455 {
456 }
457 static inline int eapol_sm_failed(struct eapol_sm *sm)
458 {
459 	return 0;
460 }
461 static inline void eapol_sm_erp_flush(struct eapol_sm *sm)
462 {
463 }
464 static inline struct wpabuf *
465 eapol_sm_build_erp_reauth_start(struct eapol_sm *sm)
466 {
467 	return NULL;
468 }
469 static inline void eapol_sm_process_erp_finish(struct eapol_sm *sm,
470 					       const u8 *buf, size_t len)
471 {
472 }
473 static inline int eapol_sm_update_erp_next_seq_num(struct eapol_sm *sm,
474 						   u16 next_seq_num)
475 {
476 	return -1;
477 }
478 static inline int
479 eapol_sm_get_erp_info(struct eapol_sm *sm, struct eap_peer_config *config,
480 		      const u8 **username, size_t *username_len,
481 		      const u8 **realm, size_t *realm_len,
482 		      u16 *erp_next_seq_num, const u8 **rrk, size_t *rrk_len)
483 {
484 	return -1;
485 }
486 #endif /* IEEE8021X_EAPOL */
487 
488 #endif /* EAPOL_SUPP_SM_H */
489