WPS UFD: Use pre-configured DH keys only with OOB
[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 "dh_groups.h"
19 #include "crypto.h"
20 #include "sha256.h"
21 #include "aes_wrap.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                 pubkey = wpabuf_dup(wps->wps->dh_pubkey);
35         } else {
36                 wpa_printf(MSG_DEBUG, "WPS: Generate new DH keys");
37                 wps->dh_privkey = NULL;
38                 pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
39                                  &wps->dh_privkey);
40                 pubkey = wpabuf_zeropad(pubkey, 192);
41         }
42         if (wps->dh_privkey == NULL || pubkey == NULL) {
43                 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize "
44                            "Diffie-Hellman handshake");
45                 wpabuf_free(pubkey);
46                 return -1;
47         }
48
49         wpabuf_put_be16(msg, ATTR_PUBLIC_KEY);
50         wpabuf_put_be16(msg, wpabuf_len(pubkey));
51         wpabuf_put_buf(msg, pubkey);
52
53         if (wps->registrar) {
54                 wpabuf_free(wps->dh_pubkey_r);
55                 wps->dh_pubkey_r = pubkey;
56         } else {
57                 wpabuf_free(wps->dh_pubkey_e);
58                 wps->dh_pubkey_e = pubkey;
59         }
60
61         return 0;
62 }
63
64
65 int wps_build_req_type(struct wpabuf *msg, enum wps_request_type type)
66 {
67         wpa_printf(MSG_DEBUG, "WPS:  * Request Type");
68         wpabuf_put_be16(msg, ATTR_REQUEST_TYPE);
69         wpabuf_put_be16(msg, 1);
70         wpabuf_put_u8(msg, type);
71         return 0;
72 }
73
74
75 int wps_build_config_methods(struct wpabuf *msg, u16 methods)
76 {
77         wpa_printf(MSG_DEBUG, "WPS:  * Config Methods (%x)", methods);
78         wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
79         wpabuf_put_be16(msg, 2);
80         wpabuf_put_be16(msg, methods);
81         return 0;
82 }
83
84
85 int wps_build_uuid_e(struct wpabuf *msg, const u8 *uuid)
86 {
87         wpa_printf(MSG_DEBUG, "WPS:  * UUID-E");
88         wpabuf_put_be16(msg, ATTR_UUID_E);
89         wpabuf_put_be16(msg, WPS_UUID_LEN);
90         wpabuf_put_data(msg, uuid, WPS_UUID_LEN);
91         return 0;
92 }
93
94
95 int wps_build_dev_password_id(struct wpabuf *msg, u16 id)
96 {
97         wpa_printf(MSG_DEBUG, "WPS:  * Device Password ID (%d)", id);
98         wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
99         wpabuf_put_be16(msg, 2);
100         wpabuf_put_be16(msg, id);
101         return 0;
102 }
103
104
105 int wps_build_config_error(struct wpabuf *msg, u16 err)
106 {
107         wpa_printf(MSG_DEBUG, "WPS:  * Configuration Error (%d)", err);
108         wpabuf_put_be16(msg, ATTR_CONFIG_ERROR);
109         wpabuf_put_be16(msg, 2);
110         wpabuf_put_be16(msg, err);
111         return 0;
112 }
113
114
115 int wps_build_authenticator(struct wps_data *wps, struct wpabuf *msg)
116 {
117         u8 hash[SHA256_MAC_LEN];
118         const u8 *addr[2];
119         size_t len[2];
120
121         if (wps->last_msg == NULL) {
122                 wpa_printf(MSG_DEBUG, "WPS: Last message not available for "
123                            "building authenticator");
124                 return -1;
125         }
126
127         /* Authenticator = HMAC-SHA256_AuthKey(M_prev || M_curr*)
128          * (M_curr* is M_curr without the Authenticator attribute)
129          */
130         addr[0] = wpabuf_head(wps->last_msg);
131         len[0] = wpabuf_len(wps->last_msg);
132         addr[1] = wpabuf_head(msg);
133         len[1] = wpabuf_len(msg);
134         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 2, addr, len, hash);
135
136         wpa_printf(MSG_DEBUG, "WPS:  * Authenticator");
137         wpabuf_put_be16(msg, ATTR_AUTHENTICATOR);
138         wpabuf_put_be16(msg, WPS_AUTHENTICATOR_LEN);
139         wpabuf_put_data(msg, hash, WPS_AUTHENTICATOR_LEN);
140
141         return 0;
142 }
143
144
145 int wps_build_version(struct wpabuf *msg)
146 {
147         wpa_printf(MSG_DEBUG, "WPS:  * Version");
148         wpabuf_put_be16(msg, ATTR_VERSION);
149         wpabuf_put_be16(msg, 1);
150         wpabuf_put_u8(msg, WPS_VERSION);
151         return 0;
152 }
153
154
155 int wps_build_msg_type(struct wpabuf *msg, enum wps_msg_type msg_type)
156 {
157         wpa_printf(MSG_DEBUG, "WPS:  * Message Type (%d)", msg_type);
158         wpabuf_put_be16(msg, ATTR_MSG_TYPE);
159         wpabuf_put_be16(msg, 1);
160         wpabuf_put_u8(msg, msg_type);
161         return 0;
162 }
163
164
165 int wps_build_enrollee_nonce(struct wps_data *wps, struct wpabuf *msg)
166 {
167         wpa_printf(MSG_DEBUG, "WPS:  * Enrollee Nonce");
168         wpabuf_put_be16(msg, ATTR_ENROLLEE_NONCE);
169         wpabuf_put_be16(msg, WPS_NONCE_LEN);
170         wpabuf_put_data(msg, wps->nonce_e, WPS_NONCE_LEN);
171         return 0;
172 }
173
174
175 int wps_build_registrar_nonce(struct wps_data *wps, struct wpabuf *msg)
176 {
177         wpa_printf(MSG_DEBUG, "WPS:  * Registrar Nonce");
178         wpabuf_put_be16(msg, ATTR_REGISTRAR_NONCE);
179         wpabuf_put_be16(msg, WPS_NONCE_LEN);
180         wpabuf_put_data(msg, wps->nonce_r, WPS_NONCE_LEN);
181         return 0;
182 }
183
184
185 int wps_build_auth_type_flags(struct wps_data *wps, struct wpabuf *msg)
186 {
187         wpa_printf(MSG_DEBUG, "WPS:  * Authentication Type Flags");
188         wpabuf_put_be16(msg, ATTR_AUTH_TYPE_FLAGS);
189         wpabuf_put_be16(msg, 2);
190         wpabuf_put_be16(msg, WPS_AUTH_TYPES);
191         return 0;
192 }
193
194
195 int wps_build_encr_type_flags(struct wps_data *wps, struct wpabuf *msg)
196 {
197         wpa_printf(MSG_DEBUG, "WPS:  * Encryption Type Flags");
198         wpabuf_put_be16(msg, ATTR_ENCR_TYPE_FLAGS);
199         wpabuf_put_be16(msg, 2);
200         wpabuf_put_be16(msg, WPS_ENCR_TYPES);
201         return 0;
202 }
203
204
205 int wps_build_conn_type_flags(struct wps_data *wps, struct wpabuf *msg)
206 {
207         wpa_printf(MSG_DEBUG, "WPS:  * Connection Type Flags");
208         wpabuf_put_be16(msg, ATTR_CONN_TYPE_FLAGS);
209         wpabuf_put_be16(msg, 1);
210         wpabuf_put_u8(msg, WPS_CONN_ESS);
211         return 0;
212 }
213
214
215 int wps_build_assoc_state(struct wps_data *wps, struct wpabuf *msg)
216 {
217         wpa_printf(MSG_DEBUG, "WPS:  * Association State");
218         wpabuf_put_be16(msg, ATTR_ASSOC_STATE);
219         wpabuf_put_be16(msg, 2);
220         wpabuf_put_be16(msg, WPS_ASSOC_NOT_ASSOC);
221         return 0;
222 }
223
224
225 int wps_build_key_wrap_auth(struct wps_data *wps, struct wpabuf *msg)
226 {
227         u8 hash[SHA256_MAC_LEN];
228
229         wpa_printf(MSG_DEBUG, "WPS:  * Key Wrap Authenticator");
230         hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, wpabuf_head(msg),
231                     wpabuf_len(msg), hash);
232
233         wpabuf_put_be16(msg, ATTR_KEY_WRAP_AUTH);
234         wpabuf_put_be16(msg, WPS_KWA_LEN);
235         wpabuf_put_data(msg, hash, WPS_KWA_LEN);
236         return 0;
237 }
238
239
240 int wps_build_encr_settings(struct wps_data *wps, struct wpabuf *msg,
241                             struct wpabuf *plain)
242 {
243         size_t pad_len;
244         const size_t block_size = 16;
245         u8 *iv, *data;
246
247         wpa_printf(MSG_DEBUG, "WPS:  * Encrypted Settings");
248
249         /* PKCS#5 v2.0 pad */
250         pad_len = block_size - wpabuf_len(plain) % block_size;
251         os_memset(wpabuf_put(plain, pad_len), pad_len, pad_len);
252
253         wpabuf_put_be16(msg, ATTR_ENCR_SETTINGS);
254         wpabuf_put_be16(msg, block_size + wpabuf_len(plain));
255
256         iv = wpabuf_put(msg, block_size);
257         if (os_get_random(iv, block_size) < 0)
258                 return -1;
259
260         data = wpabuf_put(msg, 0);
261         wpabuf_put_buf(msg, plain);
262         if (aes_128_cbc_encrypt(wps->keywrapkey, iv, data, wpabuf_len(plain)))
263                 return -1;
264
265         return 0;
266 }
267
268
269 int wps_build_oob_dev_password(struct wpabuf *msg, struct wps_context *wps)
270 {
271         size_t hash_len;
272         const u8 *addr[1];
273         u8 pubkey_hash[WPS_HASH_LEN];
274         u8 dev_password_bin[WPS_OOB_DEVICE_PASSWORD_LEN];
275
276         wpa_printf(MSG_DEBUG, "WPS:  * OOB Device Password");
277
278         addr[0] = wpabuf_head(wps->dh_pubkey);
279         hash_len = wpabuf_len(wps->dh_pubkey);
280         sha256_vector(1, addr, &hash_len, pubkey_hash);
281
282         if (os_get_random((u8 *) &wps->oob_dev_pw_id, sizeof(u16)) < 0) {
283                 wpa_printf(MSG_ERROR, "WPS: device password id "
284                            "generation error");
285                 return -1;
286         }
287         wps->oob_dev_pw_id |= 0x0010;
288
289         if (os_get_random(dev_password_bin, WPS_OOB_DEVICE_PASSWORD_LEN) < 0) {
290                 wpa_printf(MSG_ERROR, "WPS: OOB device password "
291                            "generation error");
292                 return -1;
293         }
294
295         wpabuf_put_be16(msg, ATTR_OOB_DEVICE_PASSWORD);
296         wpabuf_put_be16(msg, WPS_OOB_DEVICE_PASSWORD_ATTR_LEN);
297         wpabuf_put_data(msg, pubkey_hash, WPS_OOB_PUBKEY_HASH_LEN);
298         wpabuf_put_be16(msg, wps->oob_dev_pw_id);
299         wpabuf_put_data(msg, dev_password_bin, WPS_OOB_DEVICE_PASSWORD_LEN);
300
301         wpa_snprintf_hex_uppercase(
302                 wpabuf_put(wps->oob_conf.dev_password,
303                            wpabuf_size(wps->oob_conf.dev_password)),
304                 wpabuf_size(wps->oob_conf.dev_password),
305                 dev_password_bin, WPS_OOB_DEVICE_PASSWORD_LEN);
306
307         return 0;
308 }