WPS 2.0: Add Request to Enroll attribute into Probe Request
[libeap.git] / src / wps / wps_attr_build.c
1 /*
2  * Wi-Fi Protected Setup - attribute building
3  * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "crypto/aes_wrap.h"
19 #include "crypto/crypto.h"
20 #include "crypto/dh_group5.h"
21 #include "crypto/sha256.h"
22 #include "wps_i.h"
23
24
25 int wps_build_public_key(struct wps_data *wps, struct wpabuf *msg)
26 {
27         struct wpabuf *pubkey;
28
29         wpa_printf(MSG_DEBUG, "WPS:  * Public Key");
30         wpabuf_free(wps->dh_privkey);
31         if (wps->dev_pw_id != DEV_PW_DEFAULT && wps->wps->dh_privkey) {
32                 wpa_printf(MSG_DEBUG, "WPS: Using pre-configured DH keys");
33                 wps->dh_privkey = wpabuf_dup(wps->wps->dh_privkey);
34                 wps->dh_ctx = wps->wps->dh_ctx;
35                 wps->wps->dh_ctx = NULL;
36                 pubkey = wpabuf_dup(wps->wps->dh_pubkey);
37         } else {
38                 wpa_printf(MSG_DEBUG, "WPS: Generate new DH keys");
39                 wps->dh_privkey = NULL;
40                 dh5_free(wps->dh_ctx);
41                 wps->dh_ctx = dh5_init(&wps->dh_privkey, &pubkey);
42                 pubkey = wpabuf_zeropad(pubkey, 192);
43         }
44         if (wps->dh_ctx == NULL || wps->dh_privkey == NULL || pubkey == NULL) {
45                 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize "
46                            "Diffie-Hellman handshake");
47                 wpabuf_free(pubkey);
48                 return -1;
49         }
50         wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH Private Key", wps->dh_privkey);
51         wpa_hexdump_buf(MSG_DEBUG, "WPS: DH own Public Key", pubkey);
52
53         wpabuf_put_be16(msg, ATTR_PUBLIC_KEY);
54         wpabuf_put_be16(msg, wpabuf_len(pubkey));
55         wpabuf_put_buf(msg, pubkey);
56
57         if (wps->registrar) {
58                 wpabuf_free(wps->dh_pubkey_r);
59                 wps->dh_pubkey_r = pubkey;
60         } else {
61                 wpabuf_free(wps->dh_pubkey_e);
62                 wps->dh_pubkey_e = pubkey;
63         }
64
65         return 0;
66 }
67
68
69 int wps_build_req_type(struct wpabuf *msg, enum wps_request_type type)
70 {
71         wpa_printf(MSG_DEBUG, "WPS:  * Request Type");
72         wpabuf_put_be16(msg, ATTR_REQUEST_TYPE);
73         wpabuf_put_be16(msg, 1);
74         wpabuf_put_u8(msg, type);
75         return 0;
76 }
77
78
79 int wps_build_resp_type(struct wpabuf *msg, enum wps_response_type type)
80 {
81         wpa_printf(MSG_DEBUG, "WPS:  * Response Type (%d)", type);
82         wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
83         wpabuf_put_be16(msg, 1);
84         wpabuf_put_u8(msg, type);
85         return 0;
86 }
87
88
89 int wps_build_config_methods(struct wpabuf *msg, u16 methods)
90 {
91         wpa_printf(MSG_DEBUG, "WPS:  * Config Methods (%x)", methods);
92         wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
93         wpabuf_put_be16(msg, 2);
94         wpabuf_put_be16(msg, methods);
95         return 0;
96 }
97
98
99 int wps_build_uuid_e(struct wpabuf *msg, const u8 *uuid)
100 {
101         wpa_printf(MSG_DEBUG, "WPS:  * UUID-E");
102         wpabuf_put_be16(msg, ATTR_UUID_E);
103         wpabuf_put_be16(msg, WPS_UUID_LEN);
104         wpabuf_put_data(msg, uuid, WPS_UUID_LEN);
105         return 0;
106 }
107
108
109 int wps_build_dev_password_id(struct wpabuf *msg, u16 id)
110 {
111         wpa_printf(MSG_DEBUG, "WPS:  * Device Password ID (%d)", id);
112         wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
113         wpabuf_put_be16(msg, 2);
114         wpabuf_put_be16(msg, id);
115         return 0;
116 }
117
118
119 int wps_build_config_error(struct wpabuf *msg, u16 err)
120 {
121         wpa_printf(MSG_DEBUG, "WPS:  * Configuration Error (%d)", err);
122         wpabuf_put_be16(msg, ATTR_CONFIG_ERROR);
123         wpabuf_put_be16(msg, 2);
124         wpabuf_put_be16(msg, err);
125         return 0;
126 }
127
128
129 int wps_build_authenticator(struct wps_data *wps, struct wpabuf *msg)
130 {
131         u8 hash[SHA256_MAC_LEN];
132         const u8 *addr[2];
133         size_t len[2];
134
135         if (wps->last_msg == NULL) {
136                 wpa_printf(MSG_DEBUG, "WPS: Last message not available for "
137                            "building authenticator");
138                 return -1;
139         }
140
141         /* Authenticator = HMAC-SHA256_AuthKey(M_prev || M_curr*)
142          * (M_curr* is M_curr without the Authenticator attribute)
143          */
144         addr[0] = wpabuf_head(wps->last_msg);
145         len[0] = wpabuf_len(wps->last_msg);
146         addr[1] = wpabuf_head(msg);
147         len[1] = wpabuf_len(msg);
148         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 2, addr, len, hash);
149
150         wpa_printf(MSG_DEBUG, "WPS:  * Authenticator");
151         wpabuf_put_be16(msg, ATTR_AUTHENTICATOR);
152         wpabuf_put_be16(msg, WPS_AUTHENTICATOR_LEN);
153         wpabuf_put_data(msg, hash, WPS_AUTHENTICATOR_LEN);
154
155         return 0;
156 }
157
158
159 int wps_build_version(struct wpabuf *msg)
160 {
161         /*
162          * Note: This attribute is deprecated and set to hardcoded 0x10 for
163          * backwards compatibility reasons. The real version negotiation is
164          * done with Version2.
165          */
166         wpa_printf(MSG_DEBUG, "WPS:  * Version (hardcoded 0x10)");
167         wpabuf_put_be16(msg, ATTR_VERSION);
168         wpabuf_put_be16(msg, 1);
169         wpabuf_put_u8(msg, 0x10);
170         return 0;
171 }
172
173
174 int wps_build_version2(struct wpabuf *msg)
175 {
176         wpa_printf(MSG_DEBUG, "WPS:  * Version2 (0x%x)", WPS_VERSION);
177         wpabuf_put_be16(msg, ATTR_VERSION2);
178         wpabuf_put_be16(msg, 1);
179         wpabuf_put_u8(msg, WPS_VERSION);
180         return 0;
181 }
182
183
184 int wps_build_msg_type(struct wpabuf *msg, enum wps_msg_type msg_type)
185 {
186         wpa_printf(MSG_DEBUG, "WPS:  * Message Type (%d)", msg_type);
187         wpabuf_put_be16(msg, ATTR_MSG_TYPE);
188         wpabuf_put_be16(msg, 1);
189         wpabuf_put_u8(msg, msg_type);
190         return 0;
191 }
192
193
194 int wps_build_enrollee_nonce(struct wps_data *wps, struct wpabuf *msg)
195 {
196         wpa_printf(MSG_DEBUG, "WPS:  * Enrollee Nonce");
197         wpabuf_put_be16(msg, ATTR_ENROLLEE_NONCE);
198         wpabuf_put_be16(msg, WPS_NONCE_LEN);
199         wpabuf_put_data(msg, wps->nonce_e, WPS_NONCE_LEN);
200         return 0;
201 }
202
203
204 int wps_build_registrar_nonce(struct wps_data *wps, struct wpabuf *msg)
205 {
206         wpa_printf(MSG_DEBUG, "WPS:  * Registrar Nonce");
207         wpabuf_put_be16(msg, ATTR_REGISTRAR_NONCE);
208         wpabuf_put_be16(msg, WPS_NONCE_LEN);
209         wpabuf_put_data(msg, wps->nonce_r, WPS_NONCE_LEN);
210         return 0;
211 }
212
213
214 int wps_build_auth_type_flags(struct wps_data *wps, struct wpabuf *msg)
215 {
216         wpa_printf(MSG_DEBUG, "WPS:  * Authentication Type Flags");
217         wpabuf_put_be16(msg, ATTR_AUTH_TYPE_FLAGS);
218         wpabuf_put_be16(msg, 2);
219         wpabuf_put_be16(msg, WPS_AUTH_TYPES);
220         return 0;
221 }
222
223
224 int wps_build_encr_type_flags(struct wps_data *wps, struct wpabuf *msg)
225 {
226         wpa_printf(MSG_DEBUG, "WPS:  * Encryption Type Flags");
227         wpabuf_put_be16(msg, ATTR_ENCR_TYPE_FLAGS);
228         wpabuf_put_be16(msg, 2);
229         wpabuf_put_be16(msg, WPS_ENCR_TYPES);
230         return 0;
231 }
232
233
234 int wps_build_conn_type_flags(struct wps_data *wps, struct wpabuf *msg)
235 {
236         wpa_printf(MSG_DEBUG, "WPS:  * Connection Type Flags");
237         wpabuf_put_be16(msg, ATTR_CONN_TYPE_FLAGS);
238         wpabuf_put_be16(msg, 1);
239         wpabuf_put_u8(msg, WPS_CONN_ESS);
240         return 0;
241 }
242
243
244 int wps_build_assoc_state(struct wps_data *wps, struct wpabuf *msg)
245 {
246         wpa_printf(MSG_DEBUG, "WPS:  * Association State");
247         wpabuf_put_be16(msg, ATTR_ASSOC_STATE);
248         wpabuf_put_be16(msg, 2);
249         wpabuf_put_be16(msg, WPS_ASSOC_NOT_ASSOC);
250         return 0;
251 }
252
253
254 int wps_build_key_wrap_auth(struct wps_data *wps, struct wpabuf *msg)
255 {
256         u8 hash[SHA256_MAC_LEN];
257
258         wpa_printf(MSG_DEBUG, "WPS:  * Key Wrap Authenticator");
259         hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, wpabuf_head(msg),
260                     wpabuf_len(msg), hash);
261
262         wpabuf_put_be16(msg, ATTR_KEY_WRAP_AUTH);
263         wpabuf_put_be16(msg, WPS_KWA_LEN);
264         wpabuf_put_data(msg, hash, WPS_KWA_LEN);
265         return 0;
266 }
267
268
269 int wps_build_encr_settings(struct wps_data *wps, struct wpabuf *msg,
270                             struct wpabuf *plain)
271 {
272         size_t pad_len;
273         const size_t block_size = 16;
274         u8 *iv, *data;
275
276         wpa_printf(MSG_DEBUG, "WPS:  * Encrypted Settings");
277
278         /* PKCS#5 v2.0 pad */
279         pad_len = block_size - wpabuf_len(plain) % block_size;
280         os_memset(wpabuf_put(plain, pad_len), pad_len, pad_len);
281
282         wpabuf_put_be16(msg, ATTR_ENCR_SETTINGS);
283         wpabuf_put_be16(msg, block_size + wpabuf_len(plain));
284
285         iv = wpabuf_put(msg, block_size);
286         if (os_get_random(iv, block_size) < 0)
287                 return -1;
288
289         data = wpabuf_put(msg, 0);
290         wpabuf_put_buf(msg, plain);
291         if (aes_128_cbc_encrypt(wps->keywrapkey, iv, data, wpabuf_len(plain)))
292                 return -1;
293
294         return 0;
295 }
296
297
298 #ifdef CONFIG_WPS_OOB
299 int wps_build_oob_dev_password(struct wpabuf *msg, struct wps_context *wps)
300 {
301         size_t hash_len;
302         const u8 *addr[1];
303         u8 pubkey_hash[WPS_HASH_LEN];
304         u8 dev_password_bin[WPS_OOB_DEVICE_PASSWORD_LEN];
305
306         wpa_printf(MSG_DEBUG, "WPS:  * OOB Device Password");
307
308         addr[0] = wpabuf_head(wps->dh_pubkey);
309         hash_len = wpabuf_len(wps->dh_pubkey);
310         sha256_vector(1, addr, &hash_len, pubkey_hash);
311
312         if (os_get_random((u8 *) &wps->oob_dev_pw_id, sizeof(u16)) < 0) {
313                 wpa_printf(MSG_ERROR, "WPS: device password id "
314                            "generation error");
315                 return -1;
316         }
317         wps->oob_dev_pw_id |= 0x0010;
318
319         if (os_get_random(dev_password_bin, WPS_OOB_DEVICE_PASSWORD_LEN) < 0) {
320                 wpa_printf(MSG_ERROR, "WPS: OOB device password "
321                            "generation error");
322                 return -1;
323         }
324
325         wpabuf_put_be16(msg, ATTR_OOB_DEVICE_PASSWORD);
326         wpabuf_put_be16(msg, WPS_OOB_DEVICE_PASSWORD_ATTR_LEN);
327         wpabuf_put_data(msg, pubkey_hash, WPS_OOB_PUBKEY_HASH_LEN);
328         wpabuf_put_be16(msg, wps->oob_dev_pw_id);
329         wpabuf_put_data(msg, dev_password_bin, WPS_OOB_DEVICE_PASSWORD_LEN);
330
331         wpa_snprintf_hex_uppercase(
332                 wpabuf_put(wps->oob_conf.dev_password,
333                            wpabuf_size(wps->oob_conf.dev_password)),
334                 wpabuf_size(wps->oob_conf.dev_password),
335                 dev_password_bin, WPS_OOB_DEVICE_PASSWORD_LEN);
336
337         return 0;
338 }
339 #endif /* CONFIG_WPS_OOB */
340
341
342 int wps_build_req_to_enroll(struct wpabuf *msg)
343 {
344         wpa_printf(MSG_DEBUG, "WPS:  * Request to Enroll (1)");
345         wpabuf_put_be16(msg, ATTR_REQUEST_TO_ENROLL);
346         wpabuf_put_be16(msg, 1);
347         wpabuf_put_u8(msg, 1);
348         return 0;
349 }