WPS: Remove derivation of management keys
[libeap.git] / src / wps / wps_common.c
1 /*
2  * Wi-Fi Protected Setup - common functionality
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_group5.h"
19 #include "sha256.h"
20 #include "aes_wrap.h"
21 #include "crypto.h"
22 #include "wps_i.h"
23 #include "wps_dev_attr.h"
24
25
26 void wps_kdf(const u8 *key, const u8 *label_prefix, size_t label_prefix_len,
27              const char *label, u8 *res, size_t res_len)
28 {
29         u8 i_buf[4], key_bits[4];
30         const u8 *addr[4];
31         size_t len[4];
32         int i, iter;
33         u8 hash[SHA256_MAC_LEN], *opos;
34         size_t left;
35
36         WPA_PUT_BE32(key_bits, res_len * 8);
37
38         addr[0] = i_buf;
39         len[0] = sizeof(i_buf);
40         addr[1] = label_prefix;
41         len[1] = label_prefix_len;
42         addr[2] = (const u8 *) label;
43         len[2] = os_strlen(label);
44         addr[3] = key_bits;
45         len[3] = sizeof(key_bits);
46
47         iter = (res_len + SHA256_MAC_LEN - 1) / SHA256_MAC_LEN;
48         opos = res;
49         left = res_len;
50
51         for (i = 1; i <= iter; i++) {
52                 WPA_PUT_BE32(i_buf, i);
53                 hmac_sha256_vector(key, SHA256_MAC_LEN, 4, addr, len, hash);
54                 if (i < iter) {
55                         os_memcpy(opos, hash, SHA256_MAC_LEN);
56                         opos += SHA256_MAC_LEN;
57                         left -= SHA256_MAC_LEN;
58                 } else
59                         os_memcpy(opos, hash, left);
60         }
61 }
62
63
64 int wps_derive_keys(struct wps_data *wps)
65 {
66         struct wpabuf *pubkey, *dh_shared;
67         u8 dhkey[SHA256_MAC_LEN], kdk[SHA256_MAC_LEN];
68         const u8 *addr[3];
69         size_t len[3];
70         u8 keys[WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN + WPS_EMSK_LEN];
71
72         if (wps->dh_privkey == NULL) {
73                 wpa_printf(MSG_DEBUG, "WPS: Own DH private key not available");
74                 return -1;
75         }
76
77         pubkey = wps->registrar ? wps->dh_pubkey_e : wps->dh_pubkey_r;
78         if (pubkey == NULL) {
79                 wpa_printf(MSG_DEBUG, "WPS: Peer DH public key not available");
80                 return -1;
81         }
82
83         dh_shared = dh5_derive_shared(wps->dh_ctx, pubkey, wps->dh_privkey);
84         dh5_free(wps->dh_ctx);
85         wps->dh_ctx = NULL;
86         dh_shared = wpabuf_zeropad(dh_shared, 192);
87         if (dh_shared == NULL) {
88                 wpa_printf(MSG_DEBUG, "WPS: Failed to derive DH shared key");
89                 return -1;
90         }
91
92         /* Own DH private key is not needed anymore */
93         wpabuf_free(wps->dh_privkey);
94         wps->dh_privkey = NULL;
95
96         wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH shared key", dh_shared);
97
98         /* DHKey = SHA-256(g^AB mod p) */
99         addr[0] = wpabuf_head(dh_shared);
100         len[0] = wpabuf_len(dh_shared);
101         sha256_vector(1, addr, len, dhkey);
102         wpa_hexdump_key(MSG_DEBUG, "WPS: DHKey", dhkey, sizeof(dhkey));
103         wpabuf_free(dh_shared);
104
105         /* KDK = HMAC-SHA-256_DHKey(N1 || EnrolleeMAC || N2) */
106         addr[0] = wps->nonce_e;
107         len[0] = WPS_NONCE_LEN;
108         addr[1] = wps->mac_addr_e;
109         len[1] = ETH_ALEN;
110         addr[2] = wps->nonce_r;
111         len[2] = WPS_NONCE_LEN;
112         hmac_sha256_vector(dhkey, sizeof(dhkey), 3, addr, len, kdk);
113         wpa_hexdump_key(MSG_DEBUG, "WPS: KDK", kdk, sizeof(kdk));
114
115         wps_kdf(kdk, NULL, 0, "Wi-Fi Easy and Secure Key Derivation",
116                 keys, sizeof(keys));
117         os_memcpy(wps->authkey, keys, WPS_AUTHKEY_LEN);
118         os_memcpy(wps->keywrapkey, keys + WPS_AUTHKEY_LEN, WPS_KEYWRAPKEY_LEN);
119         os_memcpy(wps->emsk, keys + WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN,
120                   WPS_EMSK_LEN);
121
122         wpa_hexdump_key(MSG_DEBUG, "WPS: AuthKey",
123                         wps->authkey, WPS_AUTHKEY_LEN);
124         wpa_hexdump_key(MSG_DEBUG, "WPS: KeyWrapKey",
125                         wps->keywrapkey, WPS_KEYWRAPKEY_LEN);
126         wpa_hexdump_key(MSG_DEBUG, "WPS: EMSK", wps->emsk, WPS_EMSK_LEN);
127
128         return 0;
129 }
130
131
132 void wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd,
133                     size_t dev_passwd_len)
134 {
135         u8 hash[SHA256_MAC_LEN];
136
137         hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, dev_passwd,
138                     (dev_passwd_len + 1) / 2, hash);
139         os_memcpy(wps->psk1, hash, WPS_PSK_LEN);
140         hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN,
141                     dev_passwd + (dev_passwd_len + 1) / 2,
142                     dev_passwd_len / 2, hash);
143         os_memcpy(wps->psk2, hash, WPS_PSK_LEN);
144
145         wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Device Password",
146                               dev_passwd, dev_passwd_len);
147         wpa_hexdump_key(MSG_DEBUG, "WPS: PSK1", wps->psk1, WPS_PSK_LEN);
148         wpa_hexdump_key(MSG_DEBUG, "WPS: PSK2", wps->psk2, WPS_PSK_LEN);
149 }
150
151
152 struct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr,
153                                           size_t encr_len)
154 {
155         struct wpabuf *decrypted;
156         const size_t block_size = 16;
157         size_t i;
158         u8 pad;
159         const u8 *pos;
160
161         /* AES-128-CBC */
162         if (encr == NULL || encr_len < 2 * block_size || encr_len % block_size)
163         {
164                 wpa_printf(MSG_DEBUG, "WPS: No Encrypted Settings received");
165                 return NULL;
166         }
167
168         decrypted = wpabuf_alloc(encr_len - block_size);
169         if (decrypted == NULL)
170                 return NULL;
171
172         wpa_hexdump(MSG_MSGDUMP, "WPS: Encrypted Settings", encr, encr_len);
173         wpabuf_put_data(decrypted, encr + block_size, encr_len - block_size);
174         if (aes_128_cbc_decrypt(wps->keywrapkey, encr, wpabuf_mhead(decrypted),
175                                 wpabuf_len(decrypted))) {
176                 wpabuf_free(decrypted);
177                 return NULL;
178         }
179
180         wpa_hexdump_buf_key(MSG_MSGDUMP, "WPS: Decrypted Encrypted Settings",
181                             decrypted);
182
183         pos = wpabuf_head_u8(decrypted) + wpabuf_len(decrypted) - 1;
184         pad = *pos;
185         if (pad > wpabuf_len(decrypted)) {
186                 wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad value");
187                 wpabuf_free(decrypted);
188                 return NULL;
189         }
190         for (i = 0; i < pad; i++) {
191                 if (*pos-- != pad) {
192                         wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad "
193                                    "string");
194                         wpabuf_free(decrypted);
195                         return NULL;
196                 }
197         }
198         decrypted->used -= pad;
199
200         return decrypted;
201 }
202
203
204 /**
205  * wps_pin_checksum - Compute PIN checksum
206  * @pin: Seven digit PIN (i.e., eight digit PIN without the checksum digit)
207  * Returns: Checksum digit
208  */
209 unsigned int wps_pin_checksum(unsigned int pin)
210 {
211         unsigned int accum = 0;
212         while (pin) {
213                 accum += 3 * (pin % 10);
214                 pin /= 10;
215                 accum += pin % 10;
216                 pin /= 10;
217         }
218
219         return (10 - accum % 10) % 10;
220 }
221
222
223 /**
224  * wps_pin_valid - Check whether a PIN has a valid checksum
225  * @pin: Eight digit PIN (i.e., including the checksum digit)
226  * Returns: 1 if checksum digit is valid, or 0 if not
227  */
228 unsigned int wps_pin_valid(unsigned int pin)
229 {
230         return wps_pin_checksum(pin / 10) == (pin % 10);
231 }
232
233
234 /**
235  * wps_generate_pin - Generate a random PIN
236  * Returns: Eight digit PIN (i.e., including the checksum digit)
237  */
238 unsigned int wps_generate_pin(void)
239 {
240         unsigned int val;
241
242         /* Generate seven random digits for the PIN */
243         if (os_get_random((unsigned char *) &val, sizeof(val)) < 0) {
244                 struct os_time now;
245                 os_get_time(&now);
246                 val = os_random() ^ now.sec ^ now.usec;
247         }
248         val %= 10000000;
249
250         /* Append checksum digit */
251         return val * 10 + wps_pin_checksum(val);
252 }
253
254
255 void wps_fail_event(struct wps_context *wps, enum wps_msg_type msg)
256 {
257         union wps_event_data data;
258
259         if (wps->event_cb == NULL)
260                 return;
261
262         os_memset(&data, 0, sizeof(data));
263         data.fail.msg = msg;
264         wps->event_cb(wps->cb_ctx, WPS_EV_FAIL, &data);
265 }
266
267
268 void wps_success_event(struct wps_context *wps)
269 {
270         if (wps->event_cb == NULL)
271                 return;
272
273         wps->event_cb(wps->cb_ctx, WPS_EV_SUCCESS, NULL);
274 }
275
276
277 void wps_pwd_auth_fail_event(struct wps_context *wps, int enrollee, int part)
278 {
279         union wps_event_data data;
280
281         if (wps->event_cb == NULL)
282                 return;
283
284         os_memset(&data, 0, sizeof(data));
285         data.pwd_auth_fail.enrollee = enrollee;
286         data.pwd_auth_fail.part = part;
287         wps->event_cb(wps->cb_ctx, WPS_EV_PWD_AUTH_FAIL, &data);
288 }
289
290
291 void wps_pbc_overlap_event(struct wps_context *wps)
292 {
293         if (wps->event_cb == NULL)
294                 return;
295
296         wps->event_cb(wps->cb_ctx, WPS_EV_PBC_OVERLAP, NULL);
297 }
298
299
300 void wps_pbc_timeout_event(struct wps_context *wps)
301 {
302         if (wps->event_cb == NULL)
303                 return;
304
305         wps->event_cb(wps->cb_ctx, WPS_EV_PBC_TIMEOUT, NULL);
306 }
307
308
309 #ifdef CONFIG_WPS_OOB
310
311 static struct wpabuf * wps_get_oob_cred(struct wps_context *wps)
312 {
313         struct wps_data data;
314         struct wpabuf *plain;
315
316         plain = wpabuf_alloc(500);
317         if (plain == NULL) {
318                 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
319                            "credential");
320                 return NULL;
321         }
322
323         os_memset(&data, 0, sizeof(data));
324         data.wps = wps;
325         data.auth_type = wps->auth_types;
326         data.encr_type = wps->encr_types;
327         if (wps_build_version(plain) || wps_build_cred(&data, plain)) {
328                 wpabuf_free(plain);
329                 return NULL;
330         }
331
332         return plain;
333 }
334
335
336 static struct wpabuf * wps_get_oob_dev_pwd(struct wps_context *wps)
337 {
338         struct wpabuf *data;
339
340         data = wpabuf_alloc(9 + WPS_OOB_DEVICE_PASSWORD_ATTR_LEN);
341         if (data == NULL) {
342                 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
343                            "device password attribute");
344                 return NULL;
345         }
346
347         wpabuf_free(wps->oob_conf.dev_password);
348         wps->oob_conf.dev_password =
349                 wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN * 2 + 1);
350         if (wps->oob_conf.dev_password == NULL) {
351                 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
352                            "device password");
353                 wpabuf_free(data);
354                 return NULL;
355         }
356
357         if (wps_build_version(data) ||
358             wps_build_oob_dev_password(data, wps)) {
359                 wpa_printf(MSG_ERROR, "WPS: Build OOB device password "
360                            "attribute error");
361                 wpabuf_free(data);
362                 return NULL;
363         }
364
365         return data;
366 }
367
368
369 static int wps_parse_oob_dev_pwd(struct wps_context *wps,
370                                  struct wpabuf *data)
371 {
372         struct oob_conf_data *oob_conf = &wps->oob_conf;
373         struct wps_parse_attr attr;
374         const u8 *pos;
375
376         if (wps_parse_msg(data, &attr) < 0 ||
377             attr.oob_dev_password == NULL) {
378                 wpa_printf(MSG_ERROR, "WPS: OOB device password not found");
379                 return -1;
380         }
381
382         pos = attr.oob_dev_password;
383
384         oob_conf->pubkey_hash =
385                 wpabuf_alloc_copy(pos, WPS_OOB_PUBKEY_HASH_LEN);
386         if (oob_conf->pubkey_hash == NULL) {
387                 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
388                            "public key hash");
389                 return -1;
390         }
391         pos += WPS_OOB_PUBKEY_HASH_LEN;
392
393         wps->oob_dev_pw_id = WPA_GET_BE16(pos);
394         pos += sizeof(wps->oob_dev_pw_id);
395
396         oob_conf->dev_password =
397                 wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN * 2 + 1);
398         if (oob_conf->dev_password == NULL) {
399                 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
400                            "device password");
401                 return -1;
402         }
403         wpa_snprintf_hex_uppercase(wpabuf_put(oob_conf->dev_password,
404                                    wpabuf_size(oob_conf->dev_password)),
405                                    wpabuf_size(oob_conf->dev_password), pos,
406                                    WPS_OOB_DEVICE_PASSWORD_LEN);
407
408         return 0;
409 }
410
411
412 static int wps_parse_oob_cred(struct wps_context *wps, struct wpabuf *data)
413 {
414         struct wpabuf msg;
415         struct wps_parse_attr attr;
416         size_t i;
417
418         if (wps_parse_msg(data, &attr) < 0 || attr.num_cred <= 0) {
419                 wpa_printf(MSG_ERROR, "WPS: OOB credential not found");
420                 return -1;
421         }
422
423         for (i = 0; i < attr.num_cred; i++) {
424                 struct wps_credential local_cred;
425                 struct wps_parse_attr cattr;
426
427                 os_memset(&local_cred, 0, sizeof(local_cred));
428                 wpabuf_set(&msg, attr.cred[i], attr.cred_len[i]);
429                 if (wps_parse_msg(&msg, &cattr) < 0 ||
430                     wps_process_cred(&cattr, &local_cred)) {
431                         wpa_printf(MSG_ERROR, "WPS: Failed to parse OOB "
432                                    "credential");
433                         return -1;
434                 }
435                 wps->cred_cb(wps->cb_ctx, &local_cred);
436         }
437
438         return 0;
439 }
440
441
442 int wps_process_oob(struct wps_context *wps, struct oob_device_data *oob_dev,
443                     int registrar)
444 {
445         struct wpabuf *data;
446         int ret, write_f, oob_method = wps->oob_conf.oob_method;
447         void *oob_priv;
448
449         write_f = oob_method == OOB_METHOD_DEV_PWD_E ? !registrar : registrar;
450
451         oob_priv = oob_dev->init_func(wps, oob_dev, registrar);
452         if (oob_priv == NULL) {
453                 wpa_printf(MSG_ERROR, "WPS: Failed to initialize OOB device");
454                 return -1;
455         }
456
457         if (write_f) {
458                 if (oob_method == OOB_METHOD_CRED)
459                         data = wps_get_oob_cred(wps);
460                 else
461                         data = wps_get_oob_dev_pwd(wps);
462
463                 ret = 0;
464                 if (data == NULL || oob_dev->write_func(oob_priv, data) < 0)
465                         ret = -1;
466         } else {
467                 data = oob_dev->read_func(oob_priv);
468                 if (data == NULL)
469                         ret = -1;
470                 else {
471                         if (oob_method == OOB_METHOD_CRED)
472                                 ret = wps_parse_oob_cred(wps, data);
473                         else
474                                 ret = wps_parse_oob_dev_pwd(wps, data);
475                 }
476         }
477         wpabuf_free(data);
478         oob_dev->deinit_func(oob_priv);
479
480         if (ret < 0) {
481                 wpa_printf(MSG_ERROR, "WPS: Failed to process OOB data");
482                 return -1;
483         }
484
485         return 0;
486 }
487
488
489 struct oob_device_data * wps_get_oob_device(char *device_type)
490 {
491 #ifdef CONFIG_WPS_UFD
492         if (os_strstr(device_type, "ufd") != NULL)
493                 return &oob_ufd_device_data;
494 #endif /* CONFIG_WPS_UFD */
495 #ifdef CONFIG_WPS_NFC
496         if (os_strstr(device_type, "nfc") != NULL)
497                 return &oob_nfc_device_data;
498 #endif /* CONFIG_WPS_NFC */
499
500         return NULL;
501 }
502
503
504 #ifdef CONFIG_WPS_NFC
505 struct oob_nfc_device_data * wps_get_oob_nfc_device(char *device_name)
506 {
507         if (device_name == NULL)
508                 return NULL;
509 #ifdef CONFIG_WPS_NFC_PN531
510         if (os_strstr(device_name, "pn531") != NULL)
511                 return &oob_nfc_pn531_device_data;
512 #endif /* CONFIG_WPS_NFC_PN531 */
513
514         return NULL;
515 }
516 #endif /* CONFIG_WPS_NFC */
517
518
519 int wps_get_oob_method(char *method)
520 {
521         if (os_strstr(method, "pin-e") != NULL)
522                 return OOB_METHOD_DEV_PWD_E;
523         if (os_strstr(method, "pin-r") != NULL)
524                 return OOB_METHOD_DEV_PWD_R;
525         if (os_strstr(method, "cred") != NULL)
526                 return OOB_METHOD_CRED;
527         return OOB_METHOD_UNKNOWN;
528 }
529
530 #endif /* CONFIG_WPS_OOB */