WPS: Merged two cred_cb variables into the same one
[wpasupplicant] / 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_groups.h"
19 #include "sha256.h"
20 #include "aes_wrap.h"
21 #include "crypto.h"
22 #include "wps_i.h"
23
24
25 static int wps_set_attr(struct wps_parse_attr *attr, u16 type,
26                         const u8 *pos, u16 len)
27 {
28         switch (type) {
29         case ATTR_VERSION:
30                 if (len != 1) {
31                         wpa_printf(MSG_DEBUG, "WPS: Invalid Version length %u",
32                                    len);
33                         return -1;
34                 }
35                 attr->version = pos;
36                 break;
37         case ATTR_MSG_TYPE:
38                 if (len != 1) {
39                         wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type "
40                                    "length %u", len);
41                         return -1;
42                 }
43                 attr->msg_type = pos;
44                 break;
45         case ATTR_ENROLLEE_NONCE:
46                 if (len != WPS_NONCE_LEN) {
47                         wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce "
48                                    "length %u", len);
49                         return -1;
50                 }
51                 attr->enrollee_nonce = pos;
52                 break;
53         case ATTR_REGISTRAR_NONCE:
54                 if (len != WPS_NONCE_LEN) {
55                         wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce "
56                                    "length %u", len);
57                         return -1;
58                 }
59                 attr->registrar_nonce = pos;
60                 break;
61         case ATTR_UUID_E:
62                 if (len != WPS_UUID_LEN) {
63                         wpa_printf(MSG_DEBUG, "WPS: Invalid UUID-E length %u",
64                                    len);
65                         return -1;
66                 }
67                 attr->uuid_e = pos;
68                 break;
69         case ATTR_UUID_R:
70                 if (len != WPS_UUID_LEN) {
71                         wpa_printf(MSG_DEBUG, "WPS: Invalid UUID-R length %u",
72                                    len);
73                         return -1;
74                 }
75                 attr->uuid_r = pos;
76                 break;
77         case ATTR_AUTH_TYPE_FLAGS:
78                 if (len != 2) {
79                         wpa_printf(MSG_DEBUG, "WPS: Invalid Authentication "
80                                    "Type Flags length %u", len);
81                         return -1;
82                 }
83                 attr->auth_type_flags = pos;
84                 break;
85         case ATTR_ENCR_TYPE_FLAGS:
86                 if (len != 2) {
87                         wpa_printf(MSG_DEBUG, "WPS: Invalid Encryption Type "
88                                    "Flags length %u", len);
89                         return -1;
90                 }
91                 attr->encr_type_flags = pos;
92                 break;
93         case ATTR_CONN_TYPE_FLAGS:
94                 if (len != 1) {
95                         wpa_printf(MSG_DEBUG, "WPS: Invalid Connection Type "
96                                    "Flags length %u", len);
97                         return -1;
98                 }
99                 attr->conn_type_flags = pos;
100                 break;
101         case ATTR_CONFIG_METHODS:
102                 if (len != 2) {
103                         wpa_printf(MSG_DEBUG, "WPS: Invalid Config Methods "
104                                    "length %u", len);
105                         return -1;
106                 }
107                 attr->config_methods = pos;
108                 break;
109         case ATTR_SELECTED_REGISTRAR_CONFIG_METHODS:
110                 if (len != 2) {
111                         wpa_printf(MSG_DEBUG, "WPS: Invalid Selected "
112                                    "Registrar Config Methods length %u", len);
113                         return -1;
114                 }
115                 attr->sel_reg_config_methods = pos;
116                 break;
117         case ATTR_PRIMARY_DEV_TYPE:
118                 if (len != sizeof(struct wps_dev_type)) {
119                         wpa_printf(MSG_DEBUG, "WPS: Invalid Primary Device "
120                                    "Type length %u", len);
121                         return -1;
122                 }
123                 attr->primary_dev_type = pos;
124                 break;
125         case ATTR_RF_BANDS:
126                 if (len != 1) {
127                         wpa_printf(MSG_DEBUG, "WPS: Invalid RF Bands length "
128                                    "%u", len);
129                         return -1;
130                 }
131                 attr->rf_bands = pos;
132                 break;
133         case ATTR_ASSOC_STATE:
134                 if (len != 2) {
135                         wpa_printf(MSG_DEBUG, "WPS: Invalid Association State "
136                                    "length %u", len);
137                         return -1;
138                 }
139                 attr->assoc_state = pos;
140                 break;
141         case ATTR_CONFIG_ERROR:
142                 if (len != 2) {
143                         wpa_printf(MSG_DEBUG, "WPS: Invalid Configuration "
144                                    "Error length %u", len);
145                         return -1;
146                 }
147                 attr->config_error = pos;
148                 break;
149         case ATTR_DEV_PASSWORD_ID:
150                 if (len != 2) {
151                         wpa_printf(MSG_DEBUG, "WPS: Invalid Device Password "
152                                    "ID length %u", len);
153                         return -1;
154                 }
155                 attr->dev_password_id = pos;
156                 break;
157         case ATTR_OS_VERSION:
158                 if (len != 4) {
159                         wpa_printf(MSG_DEBUG, "WPS: Invalid OS Version length "
160                                    "%u", len);
161                         return -1;
162                 }
163                 attr->os_version = pos;
164                 break;
165         case ATTR_WPS_STATE:
166                 if (len != 1) {
167                         wpa_printf(MSG_DEBUG, "WPS: Invalid Wi-Fi Protected "
168                                    "Setup State length %u", len);
169                         return -1;
170                 }
171                 attr->wps_state = pos;
172                 break;
173         case ATTR_AUTHENTICATOR:
174                 if (len != WPS_AUTHENTICATOR_LEN) {
175                         wpa_printf(MSG_DEBUG, "WPS: Invalid Authenticator "
176                                    "length %u", len);
177                         return -1;
178                 }
179                 attr->authenticator = pos;
180                 break;
181         case ATTR_R_HASH1:
182                 if (len != WPS_HASH_LEN) {
183                         wpa_printf(MSG_DEBUG, "WPS: Invalid R-Hash1 length %u",
184                                    len);
185                         return -1;
186                 }
187                 attr->r_hash1 = pos;
188                 break;
189         case ATTR_R_HASH2:
190                 if (len != WPS_HASH_LEN) {
191                         wpa_printf(MSG_DEBUG, "WPS: Invalid R-Hash2 length %u",
192                                    len);
193                         return -1;
194                 }
195                 attr->r_hash2 = pos;
196                 break;
197         case ATTR_E_HASH1:
198                 if (len != WPS_HASH_LEN) {
199                         wpa_printf(MSG_DEBUG, "WPS: Invalid E-Hash1 length %u",
200                                    len);
201                         return -1;
202                 }
203                 attr->e_hash1 = pos;
204                 break;
205         case ATTR_E_HASH2:
206                 if (len != WPS_HASH_LEN) {
207                         wpa_printf(MSG_DEBUG, "WPS: Invalid E-Hash2 length %u",
208                                    len);
209                         return -1;
210                 }
211                 attr->e_hash2 = pos;
212                 break;
213         case ATTR_R_SNONCE1:
214                 if (len != WPS_SECRET_NONCE_LEN) {
215                         wpa_printf(MSG_DEBUG, "WPS: Invalid R-SNonce1 length "
216                                    "%u", len);
217                         return -1;
218                 }
219                 attr->r_snonce1 = pos;
220                 break;
221         case ATTR_R_SNONCE2:
222                 if (len != WPS_SECRET_NONCE_LEN) {
223                         wpa_printf(MSG_DEBUG, "WPS: Invalid R-SNonce2 length "
224                                    "%u", len);
225                         return -1;
226                 }
227                 attr->r_snonce2 = pos;
228                 break;
229         case ATTR_E_SNONCE1:
230                 if (len != WPS_SECRET_NONCE_LEN) {
231                         wpa_printf(MSG_DEBUG, "WPS: Invalid E-SNonce1 length "
232                                    "%u", len);
233                         return -1;
234                 }
235                 attr->e_snonce1 = pos;
236                 break;
237         case ATTR_E_SNONCE2:
238                 if (len != WPS_SECRET_NONCE_LEN) {
239                         wpa_printf(MSG_DEBUG, "WPS: Invalid E-SNonce2 length "
240                                    "%u", len);
241                         return -1;
242                 }
243                 attr->e_snonce2 = pos;
244                 break;
245         case ATTR_KEY_WRAP_AUTH:
246                 if (len != WPS_KWA_LEN) {
247                         wpa_printf(MSG_DEBUG, "WPS: Invalid Key Wrap "
248                                    "Authenticator length %u", len);
249                         return -1;
250                 }
251                 attr->key_wrap_auth = pos;
252                 break;
253         case ATTR_AUTH_TYPE:
254                 if (len != 2) {
255                         wpa_printf(MSG_DEBUG, "WPS: Invalid Authentication "
256                                    "Type length %u", len);
257                         return -1;
258                 }
259                 attr->auth_type = pos;
260                 break;
261         case ATTR_ENCR_TYPE:
262                 if (len != 2) {
263                         wpa_printf(MSG_DEBUG, "WPS: Invalid Encryption "
264                                    "Type length %u", len);
265                         return -1;
266                 }
267                 attr->encr_type = pos;
268                 break;
269         case ATTR_NETWORK_INDEX:
270                 if (len != 1) {
271                         wpa_printf(MSG_DEBUG, "WPS: Invalid Network Index "
272                                    "length %u", len);
273                         return -1;
274                 }
275                 attr->network_idx = pos;
276                 break;
277         case ATTR_NETWORK_KEY_INDEX:
278                 if (len != 1) {
279                         wpa_printf(MSG_DEBUG, "WPS: Invalid Network Key Index "
280                                    "length %u", len);
281                         return -1;
282                 }
283                 attr->network_key_idx = pos;
284                 break;
285         case ATTR_MAC_ADDR:
286                 if (len != ETH_ALEN) {
287                         wpa_printf(MSG_DEBUG, "WPS: Invalid MAC Address "
288                                    "length %u", len);
289                         return -1;
290                 }
291                 attr->mac_addr = pos;
292                 break;
293         case ATTR_KEY_PROVIDED_AUTO:
294                 if (len != 1) {
295                         wpa_printf(MSG_DEBUG, "WPS: Invalid Key Provided "
296                                    "Automatically length %u", len);
297                         return -1;
298                 }
299                 attr->key_prov_auto = pos;
300                 break;
301         case ATTR_802_1X_ENABLED:
302                 if (len != 1) {
303                         wpa_printf(MSG_DEBUG, "WPS: Invalid 802.1X Enabled "
304                                    "length %u", len);
305                         return -1;
306                 }
307                 attr->dot1x_enabled = pos;
308                 break;
309         case ATTR_SELECTED_REGISTRAR:
310                 if (len != 1) {
311                         wpa_printf(MSG_DEBUG, "WPS: Invalid Selected Registrar"
312                                    " length %u", len);
313                         return -1;
314                 }
315                 attr->selected_registrar = pos;
316                 break;
317         case ATTR_REQUEST_TYPE:
318                 if (len != 1) {
319                         wpa_printf(MSG_DEBUG, "WPS: Invalid Request Type "
320                                    "length %u", len);
321                         return -1;
322                 }
323                 attr->request_type = pos;
324                 break;
325         case ATTR_RESPONSE_TYPE:
326                 if (len != 1) {
327                         wpa_printf(MSG_DEBUG, "WPS: Invalid Response Type "
328                                    "length %u", len);
329                         return -1;
330                 }
331                 attr->request_type = pos;
332                 break;
333         case ATTR_MANUFACTURER:
334                 attr->manufacturer = pos;
335                 attr->manufacturer_len = len;
336                 break;
337         case ATTR_MODEL_NAME:
338                 attr->model_name = pos;
339                 attr->model_name_len = len;
340                 break;
341         case ATTR_MODEL_NUMBER:
342                 attr->model_number = pos;
343                 attr->model_number_len = len;
344                 break;
345         case ATTR_SERIAL_NUMBER:
346                 attr->serial_number = pos;
347                 attr->serial_number_len = len;
348                 break;
349         case ATTR_DEV_NAME:
350                 attr->dev_name = pos;
351                 attr->dev_name_len = len;
352                 break;
353         case ATTR_PUBLIC_KEY:
354                 attr->public_key = pos;
355                 attr->public_key_len = len;
356                 break;
357         case ATTR_ENCR_SETTINGS:
358                 attr->encr_settings = pos;
359                 attr->encr_settings_len = len;
360                 break;
361         case ATTR_CRED:
362                 if (attr->num_cred >= MAX_CRED_COUNT) {
363                         wpa_printf(MSG_DEBUG, "WPS: Skipped Credential "
364                                    "attribute (max %d credentials)",
365                                    MAX_CRED_COUNT);
366                         break;
367                 }
368                 attr->cred[attr->num_cred] = pos;
369                 attr->cred_len[attr->num_cred] = len;
370                 attr->num_cred++;
371                 break;
372         case ATTR_SSID:
373                 attr->ssid = pos;
374                 attr->ssid_len = len;
375                 break;
376         case ATTR_NETWORK_KEY:
377                 attr->network_key = pos;
378                 attr->network_key_len = len;
379                 break;
380         case ATTR_EAP_TYPE:
381                 attr->eap_type = pos;
382                 attr->eap_type_len = len;
383                 break;
384         case ATTR_EAP_IDENTITY:
385                 attr->eap_identity = pos;
386                 attr->eap_identity_len = len;
387                 break;
388         default:
389                 wpa_printf(MSG_DEBUG, "WPS: Unsupported attribute type 0x%x "
390                            "len=%u", type, len);
391                 break;
392         }
393
394         return 0;
395 }
396
397
398 int wps_parse_msg(const struct wpabuf *msg, struct wps_parse_attr *attr)
399 {
400         const u8 *pos, *end;
401         u16 type, len;
402
403         os_memset(attr, 0, sizeof(*attr));
404         pos = wpabuf_head(msg);
405         end = pos + wpabuf_len(msg);
406
407         while (pos < end) {
408                 if (end - pos < 4) {
409                         wpa_printf(MSG_DEBUG, "WPS: Invalid message - "
410                                    "%lu bytes remaining",
411                                    (unsigned long) (end - pos));
412                         return -1;
413                 }
414
415                 type = WPA_GET_BE16(pos);
416                 pos += 2;
417                 len = WPA_GET_BE16(pos);
418                 pos += 2;
419                 wpa_printf(MSG_MSGDUMP, "WPS: attr type=0x%x len=%u",
420                            type, len);
421                 if (len > end - pos) {
422                         wpa_printf(MSG_DEBUG, "WPS: Attribute overflow");
423                         return -1;
424                 }
425
426                 if (wps_set_attr(attr, type, pos, len) < 0)
427                         return -1;
428
429                 pos += len;
430         }
431
432         return 0;
433 }
434
435
436 void wps_kdf(const u8 *key, const char *label, u8 *res, size_t res_len)
437 {
438         u8 i_buf[4], key_bits[4];
439         const u8 *addr[3];
440         size_t len[3];
441         int i, iter;
442         u8 hash[SHA256_MAC_LEN], *opos;
443         size_t left;
444
445         WPA_PUT_BE32(key_bits, res_len * 8);
446
447         addr[0] = i_buf;
448         len[0] = sizeof(i_buf);
449         addr[1] = (const u8 *) label;
450         len[1] = os_strlen(label);
451         addr[2] = key_bits;
452         len[2] = sizeof(key_bits);
453
454         iter = (res_len + SHA256_MAC_LEN - 1) / SHA256_MAC_LEN;
455         opos = res;
456         left = res_len;
457
458         for (i = 1; i <= iter; i++) {
459                 WPA_PUT_BE32(i_buf, i);
460                 hmac_sha256_vector(key, SHA256_MAC_LEN, 3, addr, len, hash);
461                 if (i < iter) {
462                         os_memcpy(opos, hash, SHA256_MAC_LEN);
463                         opos += SHA256_MAC_LEN;
464                         left -= SHA256_MAC_LEN;
465                 } else
466                         os_memcpy(opos, hash, left);
467         }
468 }
469
470
471 int wps_build_public_key(struct wps_data *wps, struct wpabuf *msg)
472 {
473         struct wpabuf *pubkey;
474
475         wpa_printf(MSG_DEBUG, "WPS:  * Public Key");
476         pubkey = dh_init(dh_groups_get(WPS_DH_GROUP), &wps->dh_privkey);
477         if (pubkey == NULL) {
478                 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize "
479                            "Diffie-Hellman handshake");
480                 return -1;
481         }
482
483         wpabuf_put_be16(msg, ATTR_PUBLIC_KEY);
484         wpabuf_put_be16(msg, wpabuf_len(pubkey));
485         wpabuf_put_buf(msg, pubkey);
486
487         if (wps->registrar) {
488                 wpabuf_free(wps->dh_pubkey_r);
489                 wps->dh_pubkey_r = pubkey;
490         } else {
491                 wpabuf_free(wps->dh_pubkey_e);
492                 wps->dh_pubkey_e = pubkey;
493         }
494
495         return 0;
496 }
497
498
499 int wps_derive_keys(struct wps_data *wps)
500 {
501         struct wpabuf *pubkey, *dh_shared;
502         u8 dhkey[SHA256_MAC_LEN], kdk[SHA256_MAC_LEN];
503         const u8 *addr[3];
504         size_t len[3];
505         u8 keys[WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN + WPS_EMSK_LEN];
506
507         if (wps->dh_privkey == NULL) {
508                 wpa_printf(MSG_DEBUG, "WPS: Own DH private key not available");
509                 return -1;
510         }
511
512         pubkey = wps->registrar ? wps->dh_pubkey_e : wps->dh_pubkey_r;
513         if (pubkey == NULL) {
514                 wpa_printf(MSG_DEBUG, "WPS: Peer DH public key not available");
515                 return -1;
516         }
517
518         dh_shared = dh_derive_shared(pubkey, wps->dh_privkey,
519                                      dh_groups_get(WPS_DH_GROUP));
520         if (dh_shared == NULL) {
521                 wpa_printf(MSG_DEBUG, "WPS: Failed to derive DH shared key");
522                 return -1;
523         }
524
525         /* Own DH private key is not needed anymore */
526         wpabuf_free(wps->dh_privkey);
527         wps->dh_privkey = NULL;
528
529         wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH shared key", dh_shared);
530
531         /* DHKey = SHA-256(g^AB mod p) */
532         addr[0] = wpabuf_head(dh_shared);
533         len[0] = wpabuf_len(dh_shared);
534         sha256_vector(1, addr, len, dhkey);
535         wpa_hexdump_key(MSG_DEBUG, "WPS: DHKey", dhkey, sizeof(dhkey));
536         wpabuf_free(dh_shared);
537
538         /* KDK = HMAC-SHA-256_DHKey(N1 || EnrolleeMAC || N2) */
539         addr[0] = wps->nonce_e;
540         len[0] = WPS_NONCE_LEN;
541         addr[1] = wps->mac_addr_e;
542         len[1] = ETH_ALEN;
543         addr[2] = wps->nonce_r;
544         len[2] = WPS_NONCE_LEN;
545         hmac_sha256_vector(dhkey, sizeof(dhkey), 3, addr, len, kdk);
546         wpa_hexdump_key(MSG_DEBUG, "WPS: KDK", kdk, sizeof(kdk));
547
548         wps_kdf(kdk, "Wi-Fi Easy and Secure Key Derivation",
549                 keys, sizeof(keys));
550         os_memcpy(wps->authkey, keys, WPS_AUTHKEY_LEN);
551         os_memcpy(wps->keywrapkey, keys + WPS_AUTHKEY_LEN, WPS_KEYWRAPKEY_LEN);
552         os_memcpy(wps->emsk, keys + WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN,
553                   WPS_EMSK_LEN);
554
555         wpa_hexdump_key(MSG_DEBUG, "WPS: AuthKey",
556                         wps->authkey, WPS_AUTHKEY_LEN);
557         wpa_hexdump_key(MSG_DEBUG, "WPS: KeyWrapKey",
558                         wps->keywrapkey, WPS_KEYWRAPKEY_LEN);
559         wpa_hexdump_key(MSG_DEBUG, "WPS: EMSK", wps->emsk, WPS_EMSK_LEN);
560
561         return 0;
562 }
563
564
565 int wps_build_authenticator(struct wps_data *wps, struct wpabuf *msg)
566 {
567         u8 hash[SHA256_MAC_LEN];
568         const u8 *addr[2];
569         size_t len[2];
570
571         if (wps->last_msg == NULL) {
572                 wpa_printf(MSG_DEBUG, "WPS: Last message not available for "
573                            "building authenticator");
574                 return -1;
575         }
576
577         /* Authenticator = HMAC-SHA256_AuthKey(M_prev || M_curr*)
578          * (M_curr* is M_curr without the Authenticator attribute)
579          */
580         addr[0] = wpabuf_head(wps->last_msg);
581         len[0] = wpabuf_len(wps->last_msg);
582         addr[1] = wpabuf_head(msg);
583         len[1] = wpabuf_len(msg);
584         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 2, addr, len, hash);
585
586         wpa_printf(MSG_DEBUG, "WPS:  * Authenticator");
587         wpabuf_put_be16(msg, ATTR_AUTHENTICATOR);
588         wpabuf_put_be16(msg, WPS_AUTHENTICATOR_LEN);
589         wpabuf_put_data(msg, hash, WPS_AUTHENTICATOR_LEN);
590
591         return 0;
592 }
593
594
595 int wps_process_authenticator(struct wps_data *wps, const u8 *authenticator,
596                               const struct wpabuf *msg)
597 {
598         u8 hash[SHA256_MAC_LEN];
599         const u8 *addr[2];
600         size_t len[2];
601
602         if (authenticator == NULL) {
603                 wpa_printf(MSG_DEBUG, "WPS: No Authenticator attribute "
604                            "included");
605                 return -1;
606         }
607
608         if (wps->last_msg == NULL) {
609                 wpa_printf(MSG_DEBUG, "WPS: Last message not available for "
610                            "validating authenticator");
611                 return -1;
612         }
613
614         /* Authenticator = HMAC-SHA256_AuthKey(M_prev || M_curr*)
615          * (M_curr* is M_curr without the Authenticator attribute)
616          */
617         addr[0] = wpabuf_head(wps->last_msg);
618         len[0] = wpabuf_len(wps->last_msg);
619         addr[1] = wpabuf_head(msg);
620         len[1] = wpabuf_len(msg) - 4 - WPS_AUTHENTICATOR_LEN;
621         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 2, addr, len, hash);
622
623         if (os_memcmp(hash, authenticator, WPS_AUTHENTICATOR_LEN) != 0) {
624                 wpa_printf(MSG_DEBUG, "WPS: Incorrect Authenticator");
625                 return -1;
626         }
627
628         return 0;
629 }
630
631
632 void wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd,
633                     size_t dev_passwd_len)
634 {
635         u8 hash[SHA256_MAC_LEN];
636
637         hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, dev_passwd,
638                     (dev_passwd_len + 1) / 2, hash);
639         os_memcpy(wps->psk1, hash, WPS_PSK_LEN);
640         hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN,
641                     dev_passwd + (dev_passwd_len + 1) / 2,
642                     dev_passwd_len / 2, hash);
643         os_memcpy(wps->psk2, hash, WPS_PSK_LEN);
644
645         wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Device Password",
646                               dev_passwd, dev_passwd_len);
647         wpa_hexdump_key(MSG_DEBUG, "WPS: PSK1", wps->psk1, WPS_PSK_LEN);
648         wpa_hexdump_key(MSG_DEBUG, "WPS: PSK2", wps->psk2, WPS_PSK_LEN);
649 }
650
651
652 struct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr,
653                                           size_t encr_len)
654 {
655         struct wpabuf *decrypted;
656         const size_t block_size = 16;
657         size_t i;
658         u8 pad;
659         const u8 *pos;
660
661         /* AES-128-CBC */
662         if (encr == NULL || encr_len < 2 * block_size || encr_len % block_size)
663         {
664                 wpa_printf(MSG_DEBUG, "WPS: No Encrypted Settings received");
665                 return NULL;
666         }
667
668         decrypted = wpabuf_alloc(encr_len - block_size);
669         if (decrypted == NULL)
670                 return NULL;
671
672         wpa_hexdump(MSG_MSGDUMP, "WPS: Encrypted Settings", encr, encr_len);
673         wpabuf_put_data(decrypted, encr + block_size, encr_len - block_size);
674         if (aes_128_cbc_decrypt(wps->keywrapkey, encr, wpabuf_mhead(decrypted),
675                                 wpabuf_len(decrypted))) {
676                 wpabuf_free(decrypted);
677                 return NULL;
678         }
679
680         wpa_hexdump_buf_key(MSG_MSGDUMP, "WPS: Decrypted Encrypted Settings",
681                             decrypted);
682
683         pos = wpabuf_head_u8(decrypted) + wpabuf_len(decrypted) - 1;
684         pad = *pos;
685         if (pad > wpabuf_len(decrypted)) {
686                 wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad value");
687                 wpabuf_free(decrypted);
688                 return NULL;
689         }
690         for (i = 0; i < pad; i++) {
691                 if (*pos-- != pad) {
692                         wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad "
693                                    "string");
694                         wpabuf_free(decrypted);
695                         return NULL;
696                 }
697         }
698         decrypted->used -= pad;
699
700         return decrypted;
701 }
702
703
704 int wps_process_key_wrap_auth(struct wps_data *wps, struct wpabuf *msg,
705                               const u8 *key_wrap_auth)
706 {
707         u8 hash[SHA256_MAC_LEN];
708         const u8 *head;
709         size_t len;
710
711         if (key_wrap_auth == NULL) {
712                 wpa_printf(MSG_DEBUG, "WPS: No KWA in decrypted attribute");
713                 return -1;
714         }
715
716         head = wpabuf_head(msg);
717         len = wpabuf_len(msg) - 4 - WPS_KWA_LEN;
718         if (head + len != key_wrap_auth - 4) {
719                 wpa_printf(MSG_DEBUG, "WPS: KWA not in the end of the "
720                            "decrypted attribute");
721                 return -1;
722         }
723
724         hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, head, len, hash);
725         if (os_memcmp(hash, key_wrap_auth, WPS_KWA_LEN) != 0) {
726                 wpa_printf(MSG_DEBUG, "WPS: Invalid KWA");
727                 return -1;
728         }
729
730         return 0;
731 }
732
733
734 int wps_build_version(struct wpabuf *msg)
735 {
736         wpa_printf(MSG_DEBUG, "WPS:  * Version");
737         wpabuf_put_be16(msg, ATTR_VERSION);
738         wpabuf_put_be16(msg, 1);
739         wpabuf_put_u8(msg, WPS_VERSION);
740         return 0;
741 }
742
743
744 int wps_build_msg_type(struct wpabuf *msg, enum wps_msg_type msg_type)
745 {
746         wpa_printf(MSG_DEBUG, "WPS:  * Message Type (%d)", msg_type);
747         wpabuf_put_be16(msg, ATTR_MSG_TYPE);
748         wpabuf_put_be16(msg, 1);
749         wpabuf_put_u8(msg, msg_type);
750         return 0;
751 }
752
753
754 int wps_build_enrollee_nonce(struct wps_data *wps, struct wpabuf *msg)
755 {
756         wpa_printf(MSG_DEBUG, "WPS:  * Enrollee Nonce");
757         wpabuf_put_be16(msg, ATTR_ENROLLEE_NONCE);
758         wpabuf_put_be16(msg, WPS_NONCE_LEN);
759         wpabuf_put_data(msg, wps->nonce_e, WPS_NONCE_LEN);
760         return 0;
761 }
762
763
764 int wps_build_registrar_nonce(struct wps_data *wps, struct wpabuf *msg)
765 {
766         wpa_printf(MSG_DEBUG, "WPS:  * Registrar Nonce");
767         wpabuf_put_be16(msg, ATTR_REGISTRAR_NONCE);
768         wpabuf_put_be16(msg, WPS_NONCE_LEN);
769         wpabuf_put_data(msg, wps->nonce_r, WPS_NONCE_LEN);
770         return 0;
771 }
772
773
774 int wps_build_auth_type_flags(struct wps_data *wps, struct wpabuf *msg)
775 {
776         wpa_printf(MSG_DEBUG, "WPS:  * Authentication Type Flags");
777         wpabuf_put_be16(msg, ATTR_AUTH_TYPE_FLAGS);
778         wpabuf_put_be16(msg, 2);
779         wpabuf_put_be16(msg, WPS_AUTH_TYPES);
780         return 0;
781 }
782
783
784 int wps_build_encr_type_flags(struct wps_data *wps, struct wpabuf *msg)
785 {
786         wpa_printf(MSG_DEBUG, "WPS:  * Encryption Type Flags");
787         wpabuf_put_be16(msg, ATTR_ENCR_TYPE_FLAGS);
788         wpabuf_put_be16(msg, 2);
789         wpabuf_put_be16(msg, WPS_ENCR_TYPES);
790         return 0;
791 }
792
793
794 int wps_build_conn_type_flags(struct wps_data *wps, struct wpabuf *msg)
795 {
796         wpa_printf(MSG_DEBUG, "WPS:  * Connection Type Flags");
797         wpabuf_put_be16(msg, ATTR_CONN_TYPE_FLAGS);
798         wpabuf_put_be16(msg, 1);
799         wpabuf_put_u8(msg, WPS_CONN_ESS);
800         return 0;
801 }
802
803
804 int wps_build_assoc_state(struct wps_data *wps, struct wpabuf *msg)
805 {
806         wpa_printf(MSG_DEBUG, "WPS:  * Association State");
807         wpabuf_put_be16(msg, ATTR_ASSOC_STATE);
808         wpabuf_put_be16(msg, 2);
809         wpabuf_put_be16(msg, WPS_ASSOC_NOT_ASSOC);
810         return 0;
811 }
812
813
814 int wps_build_key_wrap_auth(struct wps_data *wps, struct wpabuf *msg)
815 {
816         u8 hash[SHA256_MAC_LEN];
817
818         wpa_printf(MSG_DEBUG, "WPS:  * Key Wrap Authenticator");
819         hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, wpabuf_head(msg),
820                     wpabuf_len(msg), hash);
821
822         wpabuf_put_be16(msg, ATTR_KEY_WRAP_AUTH);
823         wpabuf_put_be16(msg, WPS_KWA_LEN);
824         wpabuf_put_data(msg, hash, WPS_KWA_LEN);
825         return 0;
826 }
827
828
829 int wps_build_encr_settings(struct wps_data *wps, struct wpabuf *msg,
830                             struct wpabuf *plain)
831 {
832         size_t pad_len;
833         const size_t block_size = 16;
834         u8 *iv, *data;
835
836         wpa_printf(MSG_DEBUG, "WPS:  * Encrypted Settings");
837
838         /* PKCS#5 v2.0 pad */
839         pad_len = block_size - wpabuf_len(plain) % block_size;
840         os_memset(wpabuf_put(plain, pad_len), pad_len, pad_len);
841
842         wpabuf_put_be16(msg, ATTR_ENCR_SETTINGS);
843         wpabuf_put_be16(msg, block_size + wpabuf_len(plain));
844
845         iv = wpabuf_put(msg, block_size);
846         if (os_get_random(iv, block_size) < 0)
847                 return -1;
848
849         data = wpabuf_put(msg, 0);
850         wpabuf_put_buf(msg, plain);
851         if (aes_128_cbc_encrypt(wps->keywrapkey, iv, data, wpabuf_len(plain)))
852                 return -1;
853
854         return 0;
855 }
856
857
858 static int wps_process_cred_network_idx(struct wps_credential *cred,
859                                         const u8 *idx)
860 {
861         if (idx == NULL) {
862                 wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
863                            "Network Index");
864                 return -1;
865         }
866
867         wpa_printf(MSG_DEBUG, "WPS: Network Index: %d", *idx);
868
869         return 0;
870 }
871
872
873 static int wps_process_cred_ssid(struct wps_credential *cred, const u8 *ssid,
874                                  size_t ssid_len)
875 {
876         if (ssid == NULL) {
877                 wpa_printf(MSG_DEBUG, "WPS: Credential did not include SSID");
878                 return -1;
879         }
880
881         /* Remove zero-padding since some Registrar implementations seem to use
882          * hardcoded 32-octet length for this attribute */
883         while (ssid_len > 0 && ssid[ssid_len - 1] == 0)
884                 ssid_len--;
885
886         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", ssid, ssid_len);
887         if (ssid_len <= sizeof(cred->ssid)) {
888                 os_memcpy(cred->ssid, ssid, ssid_len);
889                 cred->ssid_len = ssid_len;
890         }
891
892         return 0;
893 }
894
895
896 static int wps_process_cred_auth_type(struct wps_credential *cred,
897                                       const u8 *auth_type)
898 {
899         if (auth_type == NULL) {
900                 wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
901                            "Authentication Type");
902                 return -1;
903         }
904
905         cred->auth_type = WPA_GET_BE16(auth_type);
906         wpa_printf(MSG_DEBUG, "WPS: Authentication Type: 0x%x",
907                    cred->auth_type);
908
909         return 0;
910 }
911
912
913 static int wps_process_cred_encr_type(struct wps_credential *cred,
914                                       const u8 *encr_type)
915 {
916         if (encr_type == NULL) {
917                 wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
918                            "Encryption Type");
919                 return -1;
920         }
921
922         cred->encr_type = WPA_GET_BE16(encr_type);
923         wpa_printf(MSG_DEBUG, "WPS: Encryption Type: 0x%x",
924                    cred->encr_type);
925
926         return 0;
927 }
928
929
930 static int wps_process_cred_network_key_idx(struct wps_credential *cred,
931                                             const u8 *key_idx)
932 {
933         if (key_idx == NULL)
934                 return 0; /* optional attribute */
935
936         wpa_printf(MSG_DEBUG, "WPS: Network Key Index: %d", *key_idx);
937         cred->key_idx = *key_idx;
938
939         return 0;
940 }
941
942
943 static int wps_process_cred_network_key(struct wps_credential *cred,
944                                         const u8 *key, size_t key_len)
945 {
946         if (key == NULL) {
947                 wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
948                            "Network Key");
949                 return -1;
950         }
951
952         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key", key, key_len);
953         if (key_len <= sizeof(cred->key)) {
954                 os_memcpy(cred->key, key, key_len);
955                 cred->key_len = key_len;
956         }
957
958         return 0;
959 }
960
961
962 static int wps_process_cred_mac_addr(struct wps_credential *cred,
963                                      const u8 *mac_addr)
964 {
965         if (mac_addr == NULL) {
966                 wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
967                            "MAC Address");
968                 return -1;
969         }
970
971         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR, MAC2STR(mac_addr));
972         os_memcpy(cred->mac_addr, mac_addr, ETH_ALEN);
973
974         return 0;
975 }
976
977
978 static int wps_process_cred_eap_type(struct wps_credential *cred,
979                                      const u8 *eap_type, size_t eap_type_len)
980 {
981         if (eap_type == NULL)
982                 return 0; /* optional attribute */
983
984         wpa_hexdump(MSG_DEBUG, "WPS: EAP Type", eap_type, eap_type_len);
985
986         return 0;
987 }
988
989
990 static int wps_process_cred_eap_identity(struct wps_credential *cred,
991                                          const u8 *identity,
992                                          size_t identity_len)
993 {
994         if (identity == NULL)
995                 return 0; /* optional attribute */
996
997         wpa_hexdump_ascii(MSG_DEBUG, "WPS: EAP Identity",
998                           identity, identity_len);
999
1000         return 0;
1001 }
1002
1003
1004 static int wps_process_cred_key_prov_auto(struct wps_credential *cred,
1005                                           const u8 *key_prov_auto)
1006 {
1007         if (key_prov_auto == NULL)
1008                 return 0; /* optional attribute */
1009
1010         wpa_printf(MSG_DEBUG, "WPS: Key Provided Automatically: %d",
1011                    *key_prov_auto);
1012
1013         return 0;
1014 }
1015
1016
1017 static int wps_process_cred_802_1x_enabled(struct wps_credential *cred,
1018                                            const u8 *dot1x_enabled)
1019 {
1020         if (dot1x_enabled == NULL)
1021                 return 0; /* optional attribute */
1022
1023         wpa_printf(MSG_DEBUG, "WPS: 802.1X Enabled: %d", *dot1x_enabled);
1024
1025         return 0;
1026 }
1027
1028
1029 int wps_process_cred(struct wps_parse_attr *attr,
1030                      struct wps_credential *cred)
1031 {
1032         wpa_printf(MSG_DEBUG, "WPS: Process Credential");
1033
1034         /* TODO: support multiple Network Keys */
1035         if (wps_process_cred_network_idx(cred, attr->network_idx) ||
1036             wps_process_cred_ssid(cred, attr->ssid, attr->ssid_len) ||
1037             wps_process_cred_auth_type(cred, attr->auth_type) ||
1038             wps_process_cred_encr_type(cred, attr->encr_type) ||
1039             wps_process_cred_network_key_idx(cred, attr->network_key_idx) ||
1040             wps_process_cred_network_key(cred, attr->network_key,
1041                                          attr->network_key_len) ||
1042             wps_process_cred_mac_addr(cred, attr->mac_addr) ||
1043             wps_process_cred_eap_type(cred, attr->eap_type,
1044                                       attr->eap_type_len) ||
1045             wps_process_cred_eap_identity(cred, attr->eap_identity,
1046                                           attr->eap_identity_len) ||
1047             wps_process_cred_key_prov_auto(cred, attr->key_prov_auto) ||
1048             wps_process_cred_802_1x_enabled(cred, attr->dot1x_enabled))
1049                 return -1;
1050
1051         return 0;
1052 }
1053
1054
1055 int wps_process_ap_settings(struct wps_parse_attr *attr,
1056                             struct wps_credential *cred)
1057 {
1058         wpa_printf(MSG_DEBUG, "WPS: Processing AP Settings");
1059         os_memset(cred, 0, sizeof(*cred));
1060         /* TODO: optional attributes New Password and Device Password ID */
1061         if (wps_process_cred_ssid(cred, attr->ssid, attr->ssid_len) ||
1062             wps_process_cred_auth_type(cred, attr->auth_type) ||
1063             wps_process_cred_encr_type(cred, attr->encr_type) ||
1064             wps_process_cred_network_key_idx(cred, attr->network_key_idx) ||
1065             wps_process_cred_network_key(cred, attr->network_key,
1066                                          attr->network_key_len) ||
1067             wps_process_cred_mac_addr(cred, attr->mac_addr))
1068                 return -1;
1069
1070         return 0;
1071 }