xref: /freebsd/contrib/wpa/src/eapol_supp/eapol_supp_sm.h (revision 87b759f0fa1f7554d50ce640c40138512bbded44)
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 #ifndef CONFIG_OPENSC_ENGINE_PATH
192 	/**
193 	 * opensc_engine_path - Path to the OpenSSL engine for opensc
194 	 *
195 	 * This is an OpenSSL specific configuration option for loading OpenSC
196 	 * engine (engine_opensc.so); if %NULL, this engine is not loaded.
197 	 */
198 	const char *opensc_engine_path;
199 #endif /* CONFIG_OPENSC_ENGINE_PATH */
200 
201 #ifndef CONFIG_PKCS11_ENGINE_PATH
202 	/**
203 	 * pkcs11_engine_path - Path to the OpenSSL engine for PKCS#11
204 	 *
205 	 * This is an OpenSSL specific configuration option for loading PKCS#11
206 	 * engine (engine_pkcs11.so); if %NULL, this engine is not loaded.
207 	 */
208 	const char *pkcs11_engine_path;
209 #endif /* CONFIG_PKCS11_ENGINE_PATH */
210 
211 #ifndef CONFIG_PKCS11_MODULE_PATH
212 	/**
213 	 * pkcs11_module_path - Path to the OpenSSL OpenSC/PKCS#11 module
214 	 *
215 	 * This is an OpenSSL specific configuration option for configuring
216 	 * path to OpenSC/PKCS#11 engine (opensc-pkcs11.so); if %NULL, this
217 	 * module is not loaded.
218 	 */
219 	const char *pkcs11_module_path;
220 #endif /* CONFIG_PKCS11_MODULE_PATH */
221 
222 	/**
223 	 * openssl_ciphers - OpenSSL cipher string
224 	 *
225 	 * This is an OpenSSL specific configuration option for configuring the
226 	 * default ciphers. If not set, "DEFAULT:!EXP:!LOW" is used as the
227 	 * default.
228 	 */
229 	const char *openssl_ciphers;
230 
231 	/**
232 	 * wps - WPS context data
233 	 *
234 	 * This is only used by EAP-WSC and can be left %NULL if not available.
235 	 */
236 	struct wps_context *wps;
237 
238 	/**
239 	 * eap_param_needed - Notify that EAP parameter is needed
240 	 * @ctx: Callback context (ctx)
241 	 * @field: Field indicator (e.g., WPA_CTRL_REQ_EAP_IDENTITY)
242 	 * @txt: User readable text describing the required parameter
243 	 */
244 	void (*eap_param_needed)(void *ctx, enum wpa_ctrl_req_type field,
245 				 const char *txt);
246 
247 	/**
248 	 * port_cb - Set port authorized/unauthorized callback (optional)
249 	 * @ctx: Callback context (ctx)
250 	 * @authorized: Whether the supplicant port is now in authorized state
251 	 */
252 	void (*port_cb)(void *ctx, int authorized);
253 
254 	/**
255 	 * cert_cb - Notification of a peer certificate
256 	 * @ctx: Callback context (ctx)
257 	 * @cert: Certificate information
258 	 * @cert_hash: SHA-256 hash of the certificate
259 	 */
260 	void (*cert_cb)(void *ctx, struct tls_cert_data *cert,
261 			const char *cert_hash);
262 
263 	/**
264 	 * cert_in_cb - Include server certificates in callback
265 	 */
266 	int cert_in_cb;
267 
268 	/**
269 	 * status_cb - Notification of a change in EAP status
270 	 * @ctx: Callback context (ctx)
271 	 * @status: Step in the process of EAP authentication
272 	 * @parameter: Step-specific parameter, e.g., EAP method name
273 	 */
274 	void (*status_cb)(void *ctx, const char *status,
275 			  const char *parameter);
276 
277 	/**
278 	 * eap_error_cb - Notification of EAP method error
279 	 * @ctx: Callback context (ctx)
280 	 * @error_code: EAP method error code
281 	 */
282 	void (*eap_error_cb)(void *ctx, int error_code);
283 
284 #ifdef CONFIG_EAP_PROXY
285 	/**
286 	 * eap_proxy_cb - Callback signifying any updates from eap_proxy
287 	 * @ctx: eapol_ctx from eap_peer_sm_init() call
288 	 */
289 	void (*eap_proxy_cb)(void *ctx);
290 
291 	/**
292 	 * eap_proxy_notify_sim_status - Notification of SIM status change
293 	 * @ctx: eapol_ctx from eap_peer_sm_init() call
294 	 * @status: One of enum value from sim_state
295 	 */
296 	void (*eap_proxy_notify_sim_status)(void *ctx,
297 					    enum eap_proxy_sim_state sim_state);
298 #endif /* CONFIG_EAP_PROXY */
299 
300 	/**
301 	 * set_anon_id - Set or add anonymous identity
302 	 * @ctx: eapol_ctx from eap_peer_sm_init() call
303 	 * @id: Anonymous identity (e.g., EAP-SIM pseudonym)
304 	 * @len: Length of anonymous identity in octets
305 	 */
306 	void (*set_anon_id)(void *ctx, const u8 *id, size_t len);
307 
308 	/**
309 	 * confirm_auth_cb - Callback confirming if we can install a new PTK
310 	 * @ctx: eapol_ctx from eap_peer_sm_init() call
311 	 * Returns: 0 when authentication can continue, -1 when reconnecting
312 	 *
313 	 * Automatically triggers a reconnect when not.
314 	 */
315 	int (*confirm_auth_cb)(void *ctx);
316 
317 	/**
318 	 * encryption_required - Check whether encryption is required
319 	 * @ctx: eapol_ctx from eap_peer_sm_init() call
320 	 * Returns: Whether the current session requires encryption
321 	 */
322 	bool (*encryption_required)(void *ctx);
323 };
324 
325 
326 struct eap_peer_config;
327 struct ext_password_data;
328 
329 #ifdef IEEE8021X_EAPOL
330 struct eapol_sm *eapol_sm_init(struct eapol_ctx *ctx);
331 void eapol_sm_deinit(struct eapol_sm *sm);
332 void eapol_sm_step(struct eapol_sm *sm);
333 int eapol_sm_get_status(struct eapol_sm *sm, char *buf, size_t buflen,
334 			int verbose);
335 int eapol_sm_get_mib(struct eapol_sm *sm, char *buf, size_t buflen);
336 void eapol_sm_configure(struct eapol_sm *sm, int heldPeriod, int authPeriod,
337 			int startPeriod, int maxStart);
338 int eapol_sm_rx_eapol(struct eapol_sm *sm, const u8 *src, const u8 *buf,
339 		      size_t len, enum frame_encryption encrypted);
340 void eapol_sm_notify_tx_eapol_key(struct eapol_sm *sm);
341 void eapol_sm_notify_portEnabled(struct eapol_sm *sm, bool enabled);
342 void eapol_sm_notify_portValid(struct eapol_sm *sm, bool valid);
343 void eapol_sm_notify_eap_success(struct eapol_sm *sm, bool success);
344 void eapol_sm_notify_eap_fail(struct eapol_sm *sm, bool fail);
345 void eapol_sm_notify_config(struct eapol_sm *sm,
346 			    struct eap_peer_config *config,
347 			    const struct eapol_config *conf);
348 int eapol_sm_get_key(struct eapol_sm *sm, u8 *key, size_t len);
349 const u8 * eapol_sm_get_session_id(struct eapol_sm *sm, size_t *len);
350 void eapol_sm_notify_logoff(struct eapol_sm *sm, bool logoff);
351 void eapol_sm_notify_cached(struct eapol_sm *sm);
352 void eapol_sm_notify_pmkid_attempt(struct eapol_sm *sm);
353 void eapol_sm_register_scard_ctx(struct eapol_sm *sm, void *ctx);
354 void eapol_sm_notify_portControl(struct eapol_sm *sm, PortControl portControl);
355 void eapol_sm_notify_ctrl_attached(struct eapol_sm *sm);
356 void eapol_sm_notify_ctrl_response(struct eapol_sm *sm);
357 void eapol_sm_request_reauth(struct eapol_sm *sm);
358 void eapol_sm_notify_lower_layer_success(struct eapol_sm *sm, int in_eapol_sm);
359 void eapol_sm_invalidate_cached_session(struct eapol_sm *sm);
360 const char * eapol_sm_get_method_name(struct eapol_sm *sm);
361 void eapol_sm_set_ext_pw_ctx(struct eapol_sm *sm,
362 			     struct ext_password_data *ext);
363 int eapol_sm_failed(struct eapol_sm *sm);
364 void eapol_sm_erp_flush(struct eapol_sm *sm);
365 struct wpabuf * eapol_sm_build_erp_reauth_start(struct eapol_sm *sm);
366 void eapol_sm_process_erp_finish(struct eapol_sm *sm, const u8 *buf,
367 				 size_t len);
368 int eapol_sm_get_eap_proxy_imsi(void *ctx, int sim_num, char *imsi,
369 				size_t *len);
370 int eapol_sm_update_erp_next_seq_num(struct eapol_sm *sm, u16 next_seq_num);
371 int eapol_sm_get_erp_info(struct eapol_sm *sm, struct eap_peer_config *config,
372 			  const u8 **username, size_t *username_len,
373 			  const u8 **realm, size_t *realm_len,
374 			  u16 *erp_next_seq_num, const u8 **rrk,
375 			  size_t *rrk_len);
376 
377 #else /* IEEE8021X_EAPOL */
378 static inline struct eapol_sm *eapol_sm_init(struct eapol_ctx *ctx)
379 {
380 	free(ctx);
381 	return (struct eapol_sm *) 1;
382 }
383 static inline void eapol_sm_deinit(struct eapol_sm *sm)
384 {
385 }
386 static inline void eapol_sm_step(struct eapol_sm *sm)
387 {
388 }
389 static inline int eapol_sm_get_status(struct eapol_sm *sm, char *buf,
390 				      size_t buflen, int verbose)
391 {
392 	return 0;
393 }
394 static inline int eapol_sm_get_mib(struct eapol_sm *sm, char *buf,
395 				   size_t buflen)
396 {
397 	return 0;
398 }
399 static inline void eapol_sm_configure(struct eapol_sm *sm, int heldPeriod,
400 				      int authPeriod, int startPeriod,
401 				      int maxStart)
402 {
403 }
404 static inline int eapol_sm_rx_eapol(struct eapol_sm *sm, const u8 *src,
405 				    const u8 *buf, size_t len,
406 				    enum frame_encryption encrypted)
407 {
408 	return 0;
409 }
410 static inline void eapol_sm_notify_tx_eapol_key(struct eapol_sm *sm)
411 {
412 }
413 static inline void eapol_sm_notify_portEnabled(struct eapol_sm *sm,
414 					       bool enabled)
415 {
416 }
417 static inline void eapol_sm_notify_portValid(struct eapol_sm *sm,
418 					     bool valid)
419 {
420 }
421 static inline void eapol_sm_notify_eap_success(struct eapol_sm *sm,
422 					       bool success)
423 {
424 }
425 static inline void eapol_sm_notify_eap_fail(struct eapol_sm *sm, bool fail)
426 {
427 }
428 static inline void eapol_sm_notify_config(struct eapol_sm *sm,
429 					  struct eap_peer_config *config,
430 					  struct eapol_config *conf)
431 {
432 }
433 static inline int eapol_sm_get_key(struct eapol_sm *sm, u8 *key, size_t len)
434 {
435 	return -1;
436 }
437 static inline const u8 *
438 eapol_sm_get_session_id(struct eapol_sm *sm, size_t *len)
439 {
440 	return NULL;
441 }
442 static inline void eapol_sm_notify_logoff(struct eapol_sm *sm, bool logoff)
443 {
444 }
445 static inline void eapol_sm_notify_cached(struct eapol_sm *sm)
446 {
447 }
448 static inline void eapol_sm_notify_pmkid_attempt(struct eapol_sm *sm)
449 {
450 }
451 #define eapol_sm_register_scard_ctx(sm, ctx) do { } while (0)
452 static inline void eapol_sm_notify_portControl(struct eapol_sm *sm,
453 					       PortControl portControl)
454 {
455 }
456 static inline void eapol_sm_notify_ctrl_attached(struct eapol_sm *sm)
457 {
458 }
459 static inline void eapol_sm_notify_ctrl_response(struct eapol_sm *sm)
460 {
461 }
462 static inline void eapol_sm_request_reauth(struct eapol_sm *sm)
463 {
464 }
465 static inline void eapol_sm_notify_lower_layer_success(struct eapol_sm *sm,
466 						       int in_eapol_sm)
467 {
468 }
469 static inline void eapol_sm_invalidate_cached_session(struct eapol_sm *sm)
470 {
471 }
472 static inline const char * eapol_sm_get_method_name(struct eapol_sm *sm)
473 {
474 	return NULL;
475 }
476 static inline void eapol_sm_set_ext_pw_ctx(struct eapol_sm *sm,
477 					   struct ext_password_data *ext)
478 {
479 }
480 static inline int eapol_sm_failed(struct eapol_sm *sm)
481 {
482 	return 0;
483 }
484 static inline void eapol_sm_erp_flush(struct eapol_sm *sm)
485 {
486 }
487 static inline struct wpabuf *
488 eapol_sm_build_erp_reauth_start(struct eapol_sm *sm)
489 {
490 	return NULL;
491 }
492 static inline void eapol_sm_process_erp_finish(struct eapol_sm *sm,
493 					       const u8 *buf, size_t len)
494 {
495 }
496 static inline int eapol_sm_update_erp_next_seq_num(struct eapol_sm *sm,
497 						   u16 next_seq_num)
498 {
499 	return -1;
500 }
501 static inline int
502 eapol_sm_get_erp_info(struct eapol_sm *sm, struct eap_peer_config *config,
503 		      const u8 **username, size_t *username_len,
504 		      const u8 **realm, size_t *realm_len,
505 		      u16 *erp_next_seq_num, const u8 **rrk, size_t *rrk_len)
506 {
507 	return -1;
508 }
509 #endif /* IEEE8021X_EAPOL */
510 
511 #endif /* EAPOL_SUPP_SM_H */
512