WPS: Remove unused WFA WLANConfig Service actions
[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 int wps_derive_mgmt_keys(struct wps_data *wps)
133 {
134         u8 nonces[2 * WPS_NONCE_LEN];
135         u8 keys[WPS_MGMTAUTHKEY_LEN + WPS_MGMTENCKEY_LEN];
136         u8 hash[SHA256_MAC_LEN];
137         const u8 *addr[2];
138         size_t len[2];
139         const char *auth_label = "WFA-WLAN-Management-MgmtAuthKey";
140         const char *enc_label = "WFA-WLAN-Management-MgmtEncKey";
141
142         /* MgmtAuthKey || MgmtEncKey =
143          * kdf(EMSK, N1 || N2 || "WFA-WLAN-Management-Keys", 384) */
144         os_memcpy(nonces, wps->nonce_e, WPS_NONCE_LEN);
145         os_memcpy(nonces + WPS_NONCE_LEN, wps->nonce_r, WPS_NONCE_LEN);
146         wps_kdf(wps->emsk, nonces, sizeof(nonces), "WFA-WLAN-Management-Keys",
147                 keys, sizeof(keys));
148         os_memcpy(wps->mgmt_auth_key, keys, WPS_MGMTAUTHKEY_LEN);
149         os_memcpy(wps->mgmt_enc_key, keys + WPS_MGMTAUTHKEY_LEN,
150                   WPS_MGMTENCKEY_LEN);
151
152         addr[0] = nonces;
153         len[0] = sizeof(nonces);
154
155         /* MgmtEncKeyID = first 128 bits of
156          * SHA-256(N1 || N2 || "WFA-WLAN-Management-MgmtAuthKey") */
157         addr[1] = (const u8 *) auth_label;
158         len[1] = os_strlen(auth_label);
159         sha256_vector(2, addr, len, hash);
160         os_memcpy(wps->mgmt_auth_key_id, hash, WPS_MGMT_KEY_ID_LEN);
161
162         /* MgmtEncKeyID = first 128 bits of
163          * SHA-256(N1 || N2 || "WFA-WLAN-Management-MgmtEncKey") */
164         addr[1] = (const u8 *) enc_label;
165         len[1] = os_strlen(enc_label);
166         sha256_vector(2, addr, len, hash);
167         os_memcpy(wps->mgmt_enc_key_id, hash, WPS_MGMT_KEY_ID_LEN);
168
169         wpa_hexdump_key(MSG_DEBUG, "WPS: MgmtAuthKey",
170                         wps->mgmt_auth_key, WPS_MGMTAUTHKEY_LEN);
171         wpa_hexdump(MSG_DEBUG, "WPS: MgmtAuthKeyID",
172                     wps->mgmt_auth_key_id, WPS_MGMT_KEY_ID_LEN);
173         wpa_hexdump_key(MSG_DEBUG, "WPS: MgmtEncKey",
174                         wps->mgmt_enc_key, WPS_MGMTENCKEY_LEN);
175         wpa_hexdump(MSG_DEBUG, "WPS: MgmtEncKeyID",
176                     wps->mgmt_enc_key_id, WPS_MGMT_KEY_ID_LEN);
177
178         return 0;
179 }
180
181
182 void wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd,
183                     size_t dev_passwd_len)
184 {
185         u8 hash[SHA256_MAC_LEN];
186
187         hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, dev_passwd,
188                     (dev_passwd_len + 1) / 2, hash);
189         os_memcpy(wps->psk1, hash, WPS_PSK_LEN);
190         hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN,
191                     dev_passwd + (dev_passwd_len + 1) / 2,
192                     dev_passwd_len / 2, hash);
193         os_memcpy(wps->psk2, hash, WPS_PSK_LEN);
194
195         wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Device Password",
196                               dev_passwd, dev_passwd_len);
197         wpa_hexdump_key(MSG_DEBUG, "WPS: PSK1", wps->psk1, WPS_PSK_LEN);
198         wpa_hexdump_key(MSG_DEBUG, "WPS: PSK2", wps->psk2, WPS_PSK_LEN);
199 }
200
201
202 struct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr,
203                                           size_t encr_len)
204 {
205         struct wpabuf *decrypted;
206         const size_t block_size = 16;
207         size_t i;
208         u8 pad;
209         const u8 *pos;
210
211         /* AES-128-CBC */
212         if (encr == NULL || encr_len < 2 * block_size || encr_len % block_size)
213         {
214                 wpa_printf(MSG_DEBUG, "WPS: No Encrypted Settings received");
215                 return NULL;
216         }
217
218         decrypted = wpabuf_alloc(encr_len - block_size);
219         if (decrypted == NULL)
220                 return NULL;
221
222         wpa_hexdump(MSG_MSGDUMP, "WPS: Encrypted Settings", encr, encr_len);
223         wpabuf_put_data(decrypted, encr + block_size, encr_len - block_size);
224         if (aes_128_cbc_decrypt(wps->keywrapkey, encr, wpabuf_mhead(decrypted),
225                                 wpabuf_len(decrypted))) {
226                 wpabuf_free(decrypted);
227                 return NULL;
228         }
229
230         wpa_hexdump_buf_key(MSG_MSGDUMP, "WPS: Decrypted Encrypted Settings",
231                             decrypted);
232
233         pos = wpabuf_head_u8(decrypted) + wpabuf_len(decrypted) - 1;
234         pad = *pos;
235         if (pad > wpabuf_len(decrypted)) {
236                 wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad value");
237                 wpabuf_free(decrypted);
238                 return NULL;
239         }
240         for (i = 0; i < pad; i++) {
241                 if (*pos-- != pad) {
242                         wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad "
243                                    "string");
244                         wpabuf_free(decrypted);
245                         return NULL;
246                 }
247         }
248         decrypted->used -= pad;
249
250         return decrypted;
251 }
252
253
254 /**
255  * wps_pin_checksum - Compute PIN checksum
256  * @pin: Seven digit PIN (i.e., eight digit PIN without the checksum digit)
257  * Returns: Checksum digit
258  */
259 unsigned int wps_pin_checksum(unsigned int pin)
260 {
261         unsigned int accum = 0;
262         while (pin) {
263                 accum += 3 * (pin % 10);
264                 pin /= 10;
265                 accum += pin % 10;
266                 pin /= 10;
267         }
268
269         return (10 - accum % 10) % 10;
270 }
271
272
273 /**
274  * wps_pin_valid - Check whether a PIN has a valid checksum
275  * @pin: Eight digit PIN (i.e., including the checksum digit)
276  * Returns: 1 if checksum digit is valid, or 0 if not
277  */
278 unsigned int wps_pin_valid(unsigned int pin)
279 {
280         return wps_pin_checksum(pin / 10) == (pin % 10);
281 }
282
283
284 /**
285  * wps_generate_pin - Generate a random PIN
286  * Returns: Eight digit PIN (i.e., including the checksum digit)
287  */
288 unsigned int wps_generate_pin(void)
289 {
290         unsigned int val;
291
292         /* Generate seven random digits for the PIN */
293         if (os_get_random((unsigned char *) &val, sizeof(val)) < 0) {
294                 struct os_time now;
295                 os_get_time(&now);
296                 val = os_random() ^ now.sec ^ now.usec;
297         }
298         val %= 10000000;
299
300         /* Append checksum digit */
301         return val * 10 + wps_pin_checksum(val);
302 }
303
304
305 void wps_fail_event(struct wps_context *wps, enum wps_msg_type msg)
306 {
307         union wps_event_data data;
308
309         if (wps->event_cb == NULL)
310                 return;
311
312         os_memset(&data, 0, sizeof(data));
313         data.fail.msg = msg;
314         wps->event_cb(wps->cb_ctx, WPS_EV_FAIL, &data);
315 }
316
317
318 void wps_success_event(struct wps_context *wps)
319 {
320         if (wps->event_cb == NULL)
321                 return;
322
323         wps->event_cb(wps->cb_ctx, WPS_EV_SUCCESS, NULL);
324 }
325
326
327 void wps_pwd_auth_fail_event(struct wps_context *wps, int enrollee, int part)
328 {
329         union wps_event_data data;
330
331         if (wps->event_cb == NULL)
332                 return;
333
334         os_memset(&data, 0, sizeof(data));
335         data.pwd_auth_fail.enrollee = enrollee;
336         data.pwd_auth_fail.part = part;
337         wps->event_cb(wps->cb_ctx, WPS_EV_PWD_AUTH_FAIL, &data);
338 }
339
340
341 void wps_pbc_overlap_event(struct wps_context *wps)
342 {
343         if (wps->event_cb == NULL)
344                 return;
345
346         wps->event_cb(wps->cb_ctx, WPS_EV_PBC_OVERLAP, NULL);
347 }
348
349
350 void wps_pbc_timeout_event(struct wps_context *wps)
351 {
352         if (wps->event_cb == NULL)
353                 return;
354
355         wps->event_cb(wps->cb_ctx, WPS_EV_PBC_TIMEOUT, NULL);
356 }
357
358
359 #ifdef CONFIG_WPS_OOB
360
361 static struct wpabuf * wps_get_oob_cred(struct wps_context *wps)
362 {
363         struct wps_data data;
364         struct wpabuf *plain;
365
366         plain = wpabuf_alloc(500);
367         if (plain == NULL) {
368                 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
369                            "credential");
370                 return NULL;
371         }
372
373         os_memset(&data, 0, sizeof(data));
374         data.wps = wps;
375         data.auth_type = wps->auth_types;
376         data.encr_type = wps->encr_types;
377         if (wps_build_version(plain) || wps_build_cred(&data, plain)) {
378                 wpabuf_free(plain);
379                 return NULL;
380         }
381
382         return plain;
383 }
384
385
386 static struct wpabuf * wps_get_oob_dev_pwd(struct wps_context *wps)
387 {
388         struct wpabuf *data;
389
390         data = wpabuf_alloc(9 + WPS_OOB_DEVICE_PASSWORD_ATTR_LEN);
391         if (data == NULL) {
392                 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
393                            "device password attribute");
394                 return NULL;
395         }
396
397         wpabuf_free(wps->oob_conf.dev_password);
398         wps->oob_conf.dev_password =
399                 wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN * 2 + 1);
400         if (wps->oob_conf.dev_password == NULL) {
401                 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
402                            "device password");
403                 wpabuf_free(data);
404                 return NULL;
405         }
406
407         if (wps_build_version(data) ||
408             wps_build_oob_dev_password(data, wps)) {
409                 wpa_printf(MSG_ERROR, "WPS: Build OOB device password "
410                            "attribute error");
411                 wpabuf_free(data);
412                 return NULL;
413         }
414
415         return data;
416 }
417
418
419 static int wps_parse_oob_dev_pwd(struct wps_context *wps,
420                                  struct wpabuf *data)
421 {
422         struct oob_conf_data *oob_conf = &wps->oob_conf;
423         struct wps_parse_attr attr;
424         const u8 *pos;
425
426         if (wps_parse_msg(data, &attr) < 0 ||
427             attr.oob_dev_password == NULL) {
428                 wpa_printf(MSG_ERROR, "WPS: OOB device password not found");
429                 return -1;
430         }
431
432         pos = attr.oob_dev_password;
433
434         oob_conf->pubkey_hash =
435                 wpabuf_alloc_copy(pos, WPS_OOB_PUBKEY_HASH_LEN);
436         if (oob_conf->pubkey_hash == NULL) {
437                 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
438                            "public key hash");
439                 return -1;
440         }
441         pos += WPS_OOB_PUBKEY_HASH_LEN;
442
443         wps->oob_dev_pw_id = WPA_GET_BE16(pos);
444         pos += sizeof(wps->oob_dev_pw_id);
445
446         oob_conf->dev_password =
447                 wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN * 2 + 1);
448         if (oob_conf->dev_password == NULL) {
449                 wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
450                            "device password");
451                 return -1;
452         }
453         wpa_snprintf_hex_uppercase(wpabuf_put(oob_conf->dev_password,
454                                    wpabuf_size(oob_conf->dev_password)),
455                                    wpabuf_size(oob_conf->dev_password), pos,
456                                    WPS_OOB_DEVICE_PASSWORD_LEN);
457
458         return 0;
459 }
460
461
462 static int wps_parse_oob_cred(struct wps_context *wps, struct wpabuf *data)
463 {
464         struct wpabuf msg;
465         struct wps_parse_attr attr;
466         size_t i;
467
468         if (wps_parse_msg(data, &attr) < 0 || attr.num_cred <= 0) {
469                 wpa_printf(MSG_ERROR, "WPS: OOB credential not found");
470                 return -1;
471         }
472
473         for (i = 0; i < attr.num_cred; i++) {
474                 struct wps_credential local_cred;
475                 struct wps_parse_attr cattr;
476
477                 os_memset(&local_cred, 0, sizeof(local_cred));
478                 wpabuf_set(&msg, attr.cred[i], attr.cred_len[i]);
479                 if (wps_parse_msg(&msg, &cattr) < 0 ||
480                     wps_process_cred(&cattr, &local_cred)) {
481                         wpa_printf(MSG_ERROR, "WPS: Failed to parse OOB "
482                                    "credential");
483                         return -1;
484                 }
485                 wps->cred_cb(wps->cb_ctx, &local_cred);
486         }
487
488         return 0;
489 }
490
491
492 int wps_process_oob(struct wps_context *wps, struct oob_device_data *oob_dev,
493                     int registrar)
494 {
495         struct wpabuf *data;
496         int ret, write_f, oob_method = wps->oob_conf.oob_method;
497         void *oob_priv;
498
499         write_f = oob_method == OOB_METHOD_DEV_PWD_E ? !registrar : registrar;
500
501         oob_priv = oob_dev->init_func(wps, oob_dev, registrar);
502         if (oob_priv == NULL) {
503                 wpa_printf(MSG_ERROR, "WPS: Failed to initialize OOB device");
504                 return -1;
505         }
506
507         if (write_f) {
508                 if (oob_method == OOB_METHOD_CRED)
509                         data = wps_get_oob_cred(wps);
510                 else
511                         data = wps_get_oob_dev_pwd(wps);
512
513                 ret = 0;
514                 if (data == NULL || oob_dev->write_func(oob_priv, data) < 0)
515                         ret = -1;
516         } else {
517                 data = oob_dev->read_func(oob_priv);
518                 if (data == NULL)
519                         ret = -1;
520                 else {
521                         if (oob_method == OOB_METHOD_CRED)
522                                 ret = wps_parse_oob_cred(wps, data);
523                         else
524                                 ret = wps_parse_oob_dev_pwd(wps, data);
525                 }
526         }
527         wpabuf_free(data);
528         oob_dev->deinit_func(oob_priv);
529
530         if (ret < 0) {
531                 wpa_printf(MSG_ERROR, "WPS: Failed to process OOB data");
532                 return -1;
533         }
534
535         return 0;
536 }
537
538
539 struct oob_device_data * wps_get_oob_device(char *device_type)
540 {
541 #ifdef CONFIG_WPS_UFD
542         if (os_strstr(device_type, "ufd") != NULL)
543                 return &oob_ufd_device_data;
544 #endif /* CONFIG_WPS_UFD */
545 #ifdef CONFIG_WPS_NFC
546         if (os_strstr(device_type, "nfc") != NULL)
547                 return &oob_nfc_device_data;
548 #endif /* CONFIG_WPS_NFC */
549
550         return NULL;
551 }
552
553
554 #ifdef CONFIG_WPS_NFC
555 struct oob_nfc_device_data * wps_get_oob_nfc_device(char *device_name)
556 {
557         if (device_name == NULL)
558                 return NULL;
559 #ifdef CONFIG_WPS_NFC_PN531
560         if (os_strstr(device_name, "pn531") != NULL)
561                 return &oob_nfc_pn531_device_data;
562 #endif /* CONFIG_WPS_NFC_PN531 */
563
564         return NULL;
565 }
566 #endif /* CONFIG_WPS_NFC */
567
568
569 int wps_get_oob_method(char *method)
570 {
571         if (os_strstr(method, "pin-e") != NULL)
572                 return OOB_METHOD_DEV_PWD_E;
573         if (os_strstr(method, "pin-r") != NULL)
574                 return OOB_METHOD_DEV_PWD_R;
575         if (os_strstr(method, "cred") != NULL)
576                 return OOB_METHOD_CRED;
577         return OOB_METHOD_UNKNOWN;
578 }
579
580 #endif /* CONFIG_WPS_OOB */