automake build system
[mech_eap.orig] / 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 "common/ieee802_11_defs.h"
23 #include "wps_i.h"
24
25
26 int wps_build_public_key(struct wps_data *wps, struct wpabuf *msg)
27 {
28         struct wpabuf *pubkey;
29
30         wpa_printf(MSG_DEBUG, "WPS:  * Public Key");
31         wpabuf_free(wps->dh_privkey);
32         if (wps->dev_pw_id != DEV_PW_DEFAULT && wps->wps->dh_privkey) {
33                 wpa_printf(MSG_DEBUG, "WPS: Using pre-configured DH keys");
34                 wps->dh_privkey = wpabuf_dup(wps->wps->dh_privkey);
35                 wps->dh_ctx = wps->wps->dh_ctx;
36                 wps->wps->dh_ctx = NULL;
37                 pubkey = wpabuf_dup(wps->wps->dh_pubkey);
38         } else {
39                 wpa_printf(MSG_DEBUG, "WPS: Generate new DH keys");
40                 wps->dh_privkey = NULL;
41                 dh5_free(wps->dh_ctx);
42                 wps->dh_ctx = dh5_init(&wps->dh_privkey, &pubkey);
43                 pubkey = wpabuf_zeropad(pubkey, 192);
44         }
45         if (wps->dh_ctx == NULL || wps->dh_privkey == NULL || pubkey == NULL) {
46                 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize "
47                            "Diffie-Hellman handshake");
48                 wpabuf_free(pubkey);
49                 return -1;
50         }
51         wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH Private Key", wps->dh_privkey);
52         wpa_hexdump_buf(MSG_DEBUG, "WPS: DH own Public Key", pubkey);
53
54         wpabuf_put_be16(msg, ATTR_PUBLIC_KEY);
55         wpabuf_put_be16(msg, wpabuf_len(pubkey));
56         wpabuf_put_buf(msg, pubkey);
57
58         if (wps->registrar) {
59                 wpabuf_free(wps->dh_pubkey_r);
60                 wps->dh_pubkey_r = pubkey;
61         } else {
62                 wpabuf_free(wps->dh_pubkey_e);
63                 wps->dh_pubkey_e = pubkey;
64         }
65
66         return 0;
67 }
68
69
70 int wps_build_req_type(struct wpabuf *msg, enum wps_request_type type)
71 {
72         wpa_printf(MSG_DEBUG, "WPS:  * Request Type");
73         wpabuf_put_be16(msg, ATTR_REQUEST_TYPE);
74         wpabuf_put_be16(msg, 1);
75         wpabuf_put_u8(msg, type);
76         return 0;
77 }
78
79
80 int wps_build_resp_type(struct wpabuf *msg, enum wps_response_type type)
81 {
82         wpa_printf(MSG_DEBUG, "WPS:  * Response Type (%d)", type);
83         wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
84         wpabuf_put_be16(msg, 1);
85         wpabuf_put_u8(msg, type);
86         return 0;
87 }
88
89
90 int wps_build_config_methods(struct wpabuf *msg, u16 methods)
91 {
92         wpa_printf(MSG_DEBUG, "WPS:  * Config Methods (%x)", methods);
93         wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
94         wpabuf_put_be16(msg, 2);
95         wpabuf_put_be16(msg, methods);
96         return 0;
97 }
98
99
100 int wps_build_uuid_e(struct wpabuf *msg, const u8 *uuid)
101 {
102         wpa_printf(MSG_DEBUG, "WPS:  * UUID-E");
103         wpabuf_put_be16(msg, ATTR_UUID_E);
104         wpabuf_put_be16(msg, WPS_UUID_LEN);
105         wpabuf_put_data(msg, uuid, WPS_UUID_LEN);
106         return 0;
107 }
108
109
110 int wps_build_dev_password_id(struct wpabuf *msg, u16 id)
111 {
112         wpa_printf(MSG_DEBUG, "WPS:  * Device Password ID (%d)", id);
113         wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
114         wpabuf_put_be16(msg, 2);
115         wpabuf_put_be16(msg, id);
116         return 0;
117 }
118
119
120 int wps_build_config_error(struct wpabuf *msg, u16 err)
121 {
122         wpa_printf(MSG_DEBUG, "WPS:  * Configuration Error (%d)", err);
123         wpabuf_put_be16(msg, ATTR_CONFIG_ERROR);
124         wpabuf_put_be16(msg, 2);
125         wpabuf_put_be16(msg, err);
126         return 0;
127 }
128
129
130 int wps_build_authenticator(struct wps_data *wps, struct wpabuf *msg)
131 {
132         u8 hash[SHA256_MAC_LEN];
133         const u8 *addr[2];
134         size_t len[2];
135
136         if (wps->last_msg == NULL) {
137                 wpa_printf(MSG_DEBUG, "WPS: Last message not available for "
138                            "building authenticator");
139                 return -1;
140         }
141
142         /* Authenticator = HMAC-SHA256_AuthKey(M_prev || M_curr*)
143          * (M_curr* is M_curr without the Authenticator attribute)
144          */
145         addr[0] = wpabuf_head(wps->last_msg);
146         len[0] = wpabuf_len(wps->last_msg);
147         addr[1] = wpabuf_head(msg);
148         len[1] = wpabuf_len(msg);
149         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 2, addr, len, hash);
150
151         wpa_printf(MSG_DEBUG, "WPS:  * Authenticator");
152         wpabuf_put_be16(msg, ATTR_AUTHENTICATOR);
153         wpabuf_put_be16(msg, WPS_AUTHENTICATOR_LEN);
154         wpabuf_put_data(msg, hash, WPS_AUTHENTICATOR_LEN);
155
156         return 0;
157 }
158
159
160 int wps_build_version(struct wpabuf *msg)
161 {
162         /*
163          * Note: This attribute is deprecated and set to hardcoded 0x10 for
164          * backwards compatibility reasons. The real version negotiation is
165          * done with Version2.
166          */
167         wpa_printf(MSG_DEBUG, "WPS:  * Version (hardcoded 0x10)");
168         wpabuf_put_be16(msg, ATTR_VERSION);
169         wpabuf_put_be16(msg, 1);
170         wpabuf_put_u8(msg, 0x10);
171         return 0;
172 }
173
174
175 int wps_build_wfa_ext(struct wpabuf *msg, int req_to_enroll,
176                       const u8 *auth_macs, size_t auth_macs_count)
177 {
178 #ifdef CONFIG_WPS2
179         u8 *len;
180
181         wpabuf_put_be16(msg, ATTR_VENDOR_EXT);
182         len = wpabuf_put(msg, 2); /* to be filled */
183         wpabuf_put_be24(msg, WPS_VENDOR_ID_WFA);
184
185         wpa_printf(MSG_DEBUG, "WPS:  * Version2 (0x%x)", WPS_VERSION);
186         wpabuf_put_u8(msg, WFA_ELEM_VERSION2);
187         wpabuf_put_u8(msg, 1);
188         wpabuf_put_u8(msg, WPS_VERSION);
189
190         if (req_to_enroll) {
191                 wpa_printf(MSG_DEBUG, "WPS:  * Request to Enroll (1)");
192                 wpabuf_put_u8(msg, WFA_ELEM_REQUEST_TO_ENROLL);
193                 wpabuf_put_u8(msg, 1);
194                 wpabuf_put_u8(msg, 1);
195         }
196
197         if (auth_macs && auth_macs_count) {
198                 size_t i;
199                 wpa_printf(MSG_DEBUG, "WPS:  * AuthorizedMACs (count=%d)",
200                            (int) auth_macs_count);
201                 wpabuf_put_u8(msg, WFA_ELEM_AUTHORIZEDMACS);
202                 wpabuf_put_u8(msg, auth_macs_count * ETH_ALEN);
203                 wpabuf_put_data(msg, auth_macs, auth_macs_count * ETH_ALEN);
204                 for (i = 0; i < auth_macs_count; i++)
205                         wpa_printf(MSG_DEBUG, "WPS:    AuthorizedMAC: " MACSTR,
206                                    MAC2STR(&auth_macs[i * ETH_ALEN]));
207         }
208
209         WPA_PUT_BE16(len, (u8 *) wpabuf_put(msg, 0) - len - 2);
210 #endif /* CONFIG_WPS2 */
211
212 #ifdef CONFIG_WPS_TESTING
213         if (WPS_VERSION > 0x20) {
214                 wpa_printf(MSG_DEBUG, "WPS:  * Extensibility Testing - extra "
215                            "attribute");
216                 wpabuf_put_be16(msg, ATTR_EXTENSIBILITY_TEST);
217                 wpabuf_put_be16(msg, 1);
218                 wpabuf_put_u8(msg, 42);
219         }
220 #endif /* CONFIG_WPS_TESTING */
221         return 0;
222 }
223
224
225 int wps_build_msg_type(struct wpabuf *msg, enum wps_msg_type msg_type)
226 {
227         wpa_printf(MSG_DEBUG, "WPS:  * Message Type (%d)", msg_type);
228         wpabuf_put_be16(msg, ATTR_MSG_TYPE);
229         wpabuf_put_be16(msg, 1);
230         wpabuf_put_u8(msg, msg_type);
231         return 0;
232 }
233
234
235 int wps_build_enrollee_nonce(struct wps_data *wps, struct wpabuf *msg)
236 {
237         wpa_printf(MSG_DEBUG, "WPS:  * Enrollee Nonce");
238         wpabuf_put_be16(msg, ATTR_ENROLLEE_NONCE);
239         wpabuf_put_be16(msg, WPS_NONCE_LEN);
240         wpabuf_put_data(msg, wps->nonce_e, WPS_NONCE_LEN);
241         return 0;
242 }
243
244
245 int wps_build_registrar_nonce(struct wps_data *wps, struct wpabuf *msg)
246 {
247         wpa_printf(MSG_DEBUG, "WPS:  * Registrar Nonce");
248         wpabuf_put_be16(msg, ATTR_REGISTRAR_NONCE);
249         wpabuf_put_be16(msg, WPS_NONCE_LEN);
250         wpabuf_put_data(msg, wps->nonce_r, WPS_NONCE_LEN);
251         return 0;
252 }
253
254
255 int wps_build_auth_type_flags(struct wps_data *wps, struct wpabuf *msg)
256 {
257         wpa_printf(MSG_DEBUG, "WPS:  * Authentication Type Flags");
258         wpabuf_put_be16(msg, ATTR_AUTH_TYPE_FLAGS);
259         wpabuf_put_be16(msg, 2);
260         wpabuf_put_be16(msg, WPS_AUTH_TYPES);
261         return 0;
262 }
263
264
265 int wps_build_encr_type_flags(struct wps_data *wps, struct wpabuf *msg)
266 {
267         wpa_printf(MSG_DEBUG, "WPS:  * Encryption Type Flags");
268         wpabuf_put_be16(msg, ATTR_ENCR_TYPE_FLAGS);
269         wpabuf_put_be16(msg, 2);
270         wpabuf_put_be16(msg, WPS_ENCR_TYPES);
271         return 0;
272 }
273
274
275 int wps_build_conn_type_flags(struct wps_data *wps, struct wpabuf *msg)
276 {
277         wpa_printf(MSG_DEBUG, "WPS:  * Connection Type Flags");
278         wpabuf_put_be16(msg, ATTR_CONN_TYPE_FLAGS);
279         wpabuf_put_be16(msg, 1);
280         wpabuf_put_u8(msg, WPS_CONN_ESS);
281         return 0;
282 }
283
284
285 int wps_build_assoc_state(struct wps_data *wps, struct wpabuf *msg)
286 {
287         wpa_printf(MSG_DEBUG, "WPS:  * Association State");
288         wpabuf_put_be16(msg, ATTR_ASSOC_STATE);
289         wpabuf_put_be16(msg, 2);
290         wpabuf_put_be16(msg, WPS_ASSOC_NOT_ASSOC);
291         return 0;
292 }
293
294
295 int wps_build_key_wrap_auth(struct wps_data *wps, struct wpabuf *msg)
296 {
297         u8 hash[SHA256_MAC_LEN];
298
299         wpa_printf(MSG_DEBUG, "WPS:  * Key Wrap Authenticator");
300         hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, wpabuf_head(msg),
301                     wpabuf_len(msg), hash);
302
303         wpabuf_put_be16(msg, ATTR_KEY_WRAP_AUTH);
304         wpabuf_put_be16(msg, WPS_KWA_LEN);
305         wpabuf_put_data(msg, hash, WPS_KWA_LEN);
306         return 0;
307 }
308
309
310 int wps_build_encr_settings(struct wps_data *wps, struct wpabuf *msg,
311                             struct wpabuf *plain)
312 {
313         size_t pad_len;
314         const size_t block_size = 16;
315         u8 *iv, *data;
316
317         wpa_printf(MSG_DEBUG, "WPS:  * Encrypted Settings");
318
319         /* PKCS#5 v2.0 pad */
320         pad_len = block_size - wpabuf_len(plain) % block_size;
321         os_memset(wpabuf_put(plain, pad_len), pad_len, pad_len);
322
323         wpabuf_put_be16(msg, ATTR_ENCR_SETTINGS);
324         wpabuf_put_be16(msg, block_size + wpabuf_len(plain));
325
326         iv = wpabuf_put(msg, block_size);
327         if (os_get_random(iv, block_size) < 0)
328                 return -1;
329
330         data = wpabuf_put(msg, 0);
331         wpabuf_put_buf(msg, plain);
332         if (aes_128_cbc_encrypt(wps->keywrapkey, iv, data, wpabuf_len(plain)))
333                 return -1;
334
335         return 0;
336 }
337
338
339 #ifdef CONFIG_WPS_OOB
340 int wps_build_oob_dev_password(struct wpabuf *msg, struct wps_context *wps)
341 {
342         size_t hash_len;
343         const u8 *addr[1];
344         u8 pubkey_hash[WPS_HASH_LEN];
345         u8 dev_password_bin[WPS_OOB_DEVICE_PASSWORD_LEN];
346
347         wpa_printf(MSG_DEBUG, "WPS:  * OOB Device Password");
348
349         addr[0] = wpabuf_head(wps->dh_pubkey);
350         hash_len = wpabuf_len(wps->dh_pubkey);
351         sha256_vector(1, addr, &hash_len, pubkey_hash);
352
353         if (os_get_random((u8 *) &wps->oob_dev_pw_id, sizeof(u16)) < 0) {
354                 wpa_printf(MSG_ERROR, "WPS: device password id "
355                            "generation error");
356                 return -1;
357         }
358         wps->oob_dev_pw_id |= 0x0010;
359
360         if (os_get_random(dev_password_bin, WPS_OOB_DEVICE_PASSWORD_LEN) < 0) {
361                 wpa_printf(MSG_ERROR, "WPS: OOB device password "
362                            "generation error");
363                 return -1;
364         }
365
366         wpabuf_put_be16(msg, ATTR_OOB_DEVICE_PASSWORD);
367         wpabuf_put_be16(msg, WPS_OOB_DEVICE_PASSWORD_ATTR_LEN);
368         wpabuf_put_data(msg, pubkey_hash, WPS_OOB_PUBKEY_HASH_LEN);
369         wpabuf_put_be16(msg, wps->oob_dev_pw_id);
370         wpabuf_put_data(msg, dev_password_bin, WPS_OOB_DEVICE_PASSWORD_LEN);
371
372         wpa_snprintf_hex_uppercase(
373                 wpabuf_put(wps->oob_conf.dev_password,
374                            wpabuf_size(wps->oob_conf.dev_password)),
375                 wpabuf_size(wps->oob_conf.dev_password),
376                 dev_password_bin, WPS_OOB_DEVICE_PASSWORD_LEN);
377
378         return 0;
379 }
380 #endif /* CONFIG_WPS_OOB */
381
382
383 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
384 struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
385 {
386         struct wpabuf *ie;
387         const u8 *pos, *end;
388
389         ie = wpabuf_alloc(wpabuf_len(data) + 100);
390         if (ie == NULL) {
391                 wpabuf_free(data);
392                 return NULL;
393         }
394
395         pos = wpabuf_head(data);
396         end = pos + wpabuf_len(data);
397
398         while (end > pos) {
399                 size_t frag_len = end - pos;
400                 if (frag_len > 251)
401                         frag_len = 251;
402                 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
403                 wpabuf_put_u8(ie, 4 + frag_len);
404                 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
405                 wpabuf_put_data(ie, pos, frag_len);
406                 pos += frag_len;
407         }
408
409         wpabuf_free(data);
410
411         return ie;
412 }