1 /*
2  * Wi-Fi Protected Setup - internal definitions
3  * Copyright (c) 2008-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 WPS_I_H
10 #define WPS_I_H
11 
12 #include "wps.h"
13 #include "wps_attr_parse.h"
14 #include "esp_wps.h"
15 #include "esp_wifi_crypto_types.h"
16 
17 #ifdef CONFIG_WPS_NFC
18 struct wps_nfc_pw_token;
19 #endif
20 /**
21  * struct wps_data - WPS registration protocol data
22  *
23  * This data is stored at the EAP-WSC server/peer method and it is kept for a
24  * single registration protocol run.
25  */
26 struct wps_data {
27 	/**
28 	 * wps - Pointer to long term WPS context
29 	 */
30 	struct wps_context *wps;
31 
32 	/**
33 	 * registrar - Whether this end is a Registrar
34 	 */
35 	int registrar;
36 
37 	/**
38 	 * er - Whether the local end is an external registrar
39 	 */
40 	int er;
41 
42 	enum {
43 		/* Enrollee states */
44 		SEND_M1, RECV_M2, SEND_M3, RECV_M4, SEND_M5, RECV_M6, SEND_M7,
45 		RECV_M8, RECEIVED_M2D, WPS_MSG_DONE, RECV_ACK, WPS_FINISHED,
46 		SEND_WSC_NACK,
47 
48 		/* Registrar states */
49 		RECV_M1, SEND_M2, RECV_M3, SEND_M4, RECV_M5, SEND_M6,
50 		RECV_M7, SEND_M8, RECV_DONE, SEND_M2D, RECV_M2D_ACK
51 	} state;
52 
53 	u8 uuid_e[WPS_UUID_LEN];
54 	u8 uuid_r[WPS_UUID_LEN];
55 	u8 mac_addr_e[ETH_ALEN];
56 	u8 nonce_e[WPS_NONCE_LEN];
57 	u8 nonce_r[WPS_NONCE_LEN];
58 	u8 psk1[WPS_PSK_LEN];
59 	u8 psk2[WPS_PSK_LEN];
60 	u8 snonce[2 * WPS_SECRET_NONCE_LEN];
61 	u8 peer_hash1[WPS_HASH_LEN];
62 	u8 peer_hash2[WPS_HASH_LEN];
63 
64 	struct wpabuf *dh_privkey;
65 	struct wpabuf *dh_pubkey_e;
66 	struct wpabuf *dh_pubkey_r;
67 	u8 authkey[WPS_AUTHKEY_LEN];
68 	u8 keywrapkey[WPS_KEYWRAPKEY_LEN];
69 	u8 emsk[WPS_EMSK_LEN];
70 
71 	struct wpabuf *last_msg;
72 
73 	u8 *dev_password;
74 	size_t dev_password_len;
75 	u16 dev_pw_id;
76 	int pbc;
77 
78 	/**
79 	 * request_type - Request Type attribute from (Re)AssocReq
80 	 */
81 	u8 request_type;
82 
83 	/**
84 	 * encr_type - Available encryption types
85 	 */
86 	u16 encr_type;
87 
88 	/**
89 	 * auth_type - Available authentication types
90 	 */
91 	u16 auth_type;
92 
93 	u8 *new_psk;
94 	size_t new_psk_len;
95 
96 	int wps_pin_revealed;
97 	struct wps_credential cred;
98 
99 	struct wps_device_data peer_dev;
100 
101 	/**
102 	 * config_error - Configuration Error value to be used in NACK
103 	 */
104 	u16 config_error;
105 	u16 error_indication;
106 
107 	int ext_reg;
108 	int int_reg;
109 
110 	struct wps_credential *new_ap_settings;
111 
112 	void *dh_ctx;
113 
114 	void (*ap_settings_cb)(void *ctx, const struct wps_credential *cred);
115 	void *ap_settings_cb_ctx;
116 
117 	struct wps_credential *use_cred;
118 
119 	int use_psk_key;
120 	u8 p2p_dev_addr[ETH_ALEN]; /* P2P Device Address of the client or
121 				    * 00:00:00:00:00:00 if not a P2p client */
122 	int pbc_in_m1;
123 #ifdef CONFIG_WPS_NFC
124 	struct wps_nfc_pw_token *nfc_pw_token;
125 #endif
126 };
127 
128 /* wps_common.c */
129 void wps_kdf(const u8 *key, const u8 *label_prefix, size_t label_prefix_len,
130 	     const char *label, u8 *res, size_t res_len);
131 int wps_derive_keys(struct wps_data *wps);
132 void wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd,
133 		    size_t dev_passwd_len);
134 struct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr,
135 					  size_t encr_len);
136 void wps_fail_event(struct wps_context *wps, enum wps_msg_type msg,
137 		    u16 config_error, u16 error_indication);
138 void wps_success_event(struct wps_context *wps);
139 void wps_pwd_auth_fail_event(struct wps_context *wps, int enrollee, int part);
140 void wps_pbc_overlap_event(struct wps_context *wps);
141 void wps_pbc_timeout_event(struct wps_context *wps);
142 
143 struct wpabuf * wps_build_wsc_ack(struct wps_data *wps);
144 struct wpabuf * wps_build_wsc_nack(struct wps_data *wps);
145 
146 typedef enum wps_calc_key_mode {
147 	WPS_CALC_KEY_NORMAL = 0,
148 	WPS_CALC_KEY_NO_CALC,
149 	WPS_CALC_KEY_PRE_CALC,
150 	WPS_CALC_KEY_MAX,
151 } wps_key_mode_t;
152 
153 /* wps_attr_build.c */
154 int wps_build_public_key(struct wps_data *wps, struct wpabuf *msg, wps_key_mode_t mode);
155 int wps_build_req_type(struct wpabuf *msg, enum wps_request_type type);
156 int wps_build_resp_type(struct wpabuf *msg, enum wps_response_type type);
157 int wps_build_config_methods(struct wpabuf *msg, u16 methods);
158 int wps_build_uuid_e(struct wpabuf *msg, const u8 *uuid);
159 int wps_build_dev_password_id(struct wpabuf *msg, u16 id);
160 int wps_build_config_error(struct wpabuf *msg, u16 err);
161 int wps_build_authenticator(struct wps_data *wps, struct wpabuf *msg);
162 int wps_build_key_wrap_auth(struct wps_data *wps, struct wpabuf *msg);
163 int wps_build_encr_settings(struct wps_data *wps, struct wpabuf *msg,
164 			    struct wpabuf *plain);
165 int wps_build_version(struct wpabuf *msg);
166 int wps_build_wfa_ext(struct wpabuf *msg, int req_to_enroll,
167 		      const u8 *auth_macs, size_t auth_macs_count);
168 int wps_build_msg_type(struct wpabuf *msg, enum wps_msg_type msg_type);
169 int wps_build_enrollee_nonce(struct wps_data *wps, struct wpabuf *msg);
170 int wps_build_registrar_nonce(struct wps_data *wps, struct wpabuf *msg);
171 int wps_build_auth_type_flags(struct wps_data *wps, struct wpabuf *msg);
172 int wps_build_encr_type_flags(struct wps_data *wps, struct wpabuf *msg);
173 int wps_build_conn_type_flags(struct wps_data *wps, struct wpabuf *msg);
174 int wps_build_assoc_state(struct wps_data *wps, struct wpabuf *msg);
175 int wps_build_oob_dev_pw(struct wpabuf *msg, u16 dev_pw_id,
176 			 const struct wpabuf *pubkey, const u8 *dev_pw,
177 			 size_t dev_pw_len);
178 struct wpabuf * wps_ie_encapsulate(struct wpabuf *data);
179 
180 /* wps_attr_process.c */
181 int wps_process_authenticator(struct wps_data *wps, const u8 *authenticator,
182 			      const struct wpabuf *msg);
183 int wps_process_key_wrap_auth(struct wps_data *wps, struct wpabuf *msg,
184 			      const u8 *key_wrap_auth);
185 int wps_process_cred(struct wps_parse_attr *attr,
186 		     struct wps_credential *cred);
187 int wps_process_ap_settings(struct wps_parse_attr *attr,
188 			    struct wps_credential *cred);
189 
190 /* wps_enrollee.c */
191 struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps,
192 				     enum wsc_op_code *op_code);
193 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
194 					      enum wsc_op_code op_code,
195 					      const struct wpabuf *msg);
196 
197 /* wps_registrar.c */
198 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
199 				      enum wsc_op_code *op_code);
200 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
201 					       enum wsc_op_code op_code,
202 					       const struct wpabuf *msg);
203 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg);
204 int wps_device_store(struct wps_registrar *reg,
205 		     struct wps_device_data *dev, const u8 *uuid);
206 void wps_registrar_selected_registrar_changed(struct wps_registrar *reg);
207 const u8 * wps_authorized_macs(struct wps_registrar *reg, size_t *count);
208 int wps_registrar_pbc_overlap(struct wps_registrar *reg,
209 			      const u8 *addr, const u8 *uuid_e);
210 #ifdef CONFIG_WPS_NFC
211 
212 void wps_registrar_remove_nfc_pw_token(struct wps_registrar *reg,
213 				       struct wps_nfc_pw_token *token);
214 #endif
215 
216 #endif /* WPS_I_H */
217