forked from qca/sigma-dut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpp.c
3272 lines (2958 loc) · 90 KB
/
dpp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Sigma Control API DUT (station/AP/sniffer)
* Copyright (c) 2017, Qualcomm Atheros, Inc.
* Copyright (c) 2018-2020, The Linux Foundation
* All Rights Reserved.
* Licensed under the Clear BSD license. See README for more details.
*/
#include "sigma_dut.h"
#include <sys/wait.h>
#include "wpa_ctrl.h"
#include "wpa_helpers.h"
extern char *sigma_wpas_ctrl;
extern char *sigma_cert_path;
#ifdef ANDROID
char *dpp_qrcode_file = "/sdcard/wpadebug_qrdata.txt";
#endif /* ANDROID */
static int sigma_dut_is_ap(struct sigma_dut *dut)
{
return dut->device_type == AP_unknown ||
dut->device_type == AP_testbed ||
dut->device_type == AP_dut;
}
static int dpp_hostapd_run(struct sigma_dut *dut)
{
if (dut->hostapd_running)
return 0;
sigma_dut_print(dut, DUT_MSG_INFO,
"Starting hostapd in unconfigured state for DPP");
snprintf(dut->ap_ssid, sizeof(dut->ap_ssid), "unconfigured");
if (!dut->ap_oper_chn)
dut->ap_channel = 11;
dut->ap_is_dual = 0;
dut->ap_mode = dut->ap_channel <= 14 ? AP_11ng : AP_11na;
dut->ap_key_mgmt = AP_OPEN;
dut->ap_cipher = AP_PLAIN;
if (!dut->ap_dpp_conf_addr || !dut->ap_dpp_conf_pkhash)
dut->ap_start_disabled = 1;
return cmd_ap_config_commit(dut, NULL, NULL) == 1 ? 0 : -1;
}
static int dpp_hostapd_beacon(struct sigma_dut *dut)
{
const char *ifname = dut->hostapd_ifname;
if (!dut->ap_start_disabled)
return 0;
sigma_dut_print(dut, DUT_MSG_INFO, "Start beaconing");
if (!ifname ||
wpa_command(ifname, "SET start_disabled 0") < 0 ||
wpa_command(ifname, "DISABLE") < 0 ||
wpa_command(ifname, "ENABLE") < 0)
return -1;
dut->ap_start_disabled = 0;
return 0;
}
static const char * dpp_get_curve(struct sigma_cmd *cmd, const char *arg)
{
const char *val = get_param(cmd, arg);
if (!val)
val = "P-256";
else if (strcasecmp(val, "BP-256R1") == 0)
val = "BP-256";
else if (strcasecmp(val, "BP-384R1") == 0)
val = "BP-384";
else if (strcasecmp(val, "BP-512R1") == 0)
val = "BP-512";
return val;
}
static enum sigma_cmd_result
dpp_get_local_bootstrap(struct sigma_dut *dut, struct sigma_conn *conn,
struct sigma_cmd *cmd, int send_result, int *success)
{
const char *curve = dpp_get_curve(cmd, "DPPCryptoIdentifier");
const char *bs = get_param(cmd, "DPPBS");
const char *chan_list = get_param(cmd, "DPPChannelList");
const char *tcp = get_param(cmd, "DPPOverTCP");
char *pos, mac[50], buf[200], resp[1000], hex[2000];
const char *ifname = get_station_ifname(dut);
int res;
const char *type;
int include_mac;
include_mac = !tcp || strcasecmp(tcp, "yes") != 0;
if (success)
*success = 0;
if (strcasecmp(bs, "QR") == 0) {
type = "qrcode";
} else if (strcasecmp(bs, "NFC") == 0) {
type ="nfc-uri";
} else {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Unsupported DPPBS");
return STATUS_SENT_ERROR;
}
if (sigma_dut_is_ap(dut)) {
u8 bssid[ETH_ALEN];
if (!dut->hostapd_ifname) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"hostapd ifname not specified (-j)");
return ERROR_SEND_STATUS;
}
ifname = dut->hostapd_ifname;
if (get_hwaddr(dut->hostapd_ifname, bssid) < 0) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Could not get MAC address for %s",
dut->hostapd_ifname);
return ERROR_SEND_STATUS;
}
snprintf(mac, sizeof(mac), "%02x%02x%02x%02x%02x%02x",
bssid[0], bssid[1], bssid[2],
bssid[3], bssid[4], bssid[5]);
} else {
if (get_wpa_status(ifname, "address", mac, sizeof(mac)) < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to get own MAC address from wpa_supplicant");
return STATUS_SENT_ERROR;
}
}
pos = mac;
while (*pos) {
if (*pos == ':')
memmove(pos, pos + 1, strlen(pos));
else
pos++;
}
if (sigma_dut_is_ap(dut) && dpp_hostapd_run(dut) < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to start hostapd");
return STATUS_SENT_ERROR;
}
if (chan_list &&
(strcmp(chan_list, "0/0") == 0 || chan_list[0] == '\0')) {
/* No channel list */
res = snprintf(buf, sizeof(buf),
"DPP_BOOTSTRAP_GEN type=%s curve=%s%s%s",
type, curve,
include_mac ? " mac=" : "",
include_mac ? mac : "");
} else if (chan_list) {
/* Channel list override (CTT case) - space separated tuple(s)
* of OperatingClass/Channel; convert to wpa_supplicant/hostapd
* format: comma separated tuples */
strlcpy(resp, chan_list, sizeof(resp));
for (pos = resp; *pos; pos++) {
if (*pos == ' ')
*pos = ',';
}
res = snprintf(buf, sizeof(buf),
"DPP_BOOTSTRAP_GEN type=%s curve=%s chan=%s%s%s",
type, curve, resp, include_mac ? " mac=" : "",
include_mac ? mac : "");
} else {
int channel = 11;
/* Default channel list (normal DUT case) */
if (sigma_dut_is_ap(dut) && dut->hostapd_running &&
dut->ap_oper_chn &&
dut->ap_channel > 0 && dut->ap_channel <= 13)
channel = dut->ap_channel;
res = snprintf(buf, sizeof(buf),
"DPP_BOOTSTRAP_GEN type=%s curve=%s chan=81/%d%s%s",
type, curve, channel, include_mac ? " mac=" : "",
include_mac ? mac : "");
}
if (res < 0 || res >= sizeof(buf) ||
wpa_command_resp(ifname, buf, resp, sizeof(resp)) < 0 ||
strncmp(resp, "FAIL", 4) == 0)
return ERROR_SEND_STATUS;
dut->dpp_local_bootstrap = atoi(resp);
snprintf(buf, sizeof(buf), "DPP_BOOTSTRAP_GET_URI %d",
atoi(resp));
if (wpa_command_resp(ifname, buf, resp, sizeof(resp)) < 0 ||
strncmp(resp, "FAIL", 4) == 0)
return ERROR_SEND_STATUS;
sigma_dut_print(dut, DUT_MSG_DEBUG, "URI: %s", resp);
if (send_result) {
ascii2hexstr(resp, hex);
res = snprintf(resp, sizeof(resp), "BootstrappingData,%s", hex);
send_resp(dut, conn, SIGMA_COMPLETE,
res >= 0 && res < sizeof(resp) ? resp : NULL);
}
if (success)
*success = 1;
return STATUS_SENT;
}
static enum sigma_cmd_result dpp_set_peer_bootstrap(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd)
{
const char *val = get_param(cmd, "DPPBootstrappingdata");
char uri[1000];
int res;
if (!val) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Missing DPPBootstrappingdata");
return STATUS_SENT_ERROR;
}
res = parse_hexstr(val, (unsigned char *) uri, sizeof(uri));
if (res < 0 || (size_t) res >= sizeof(uri))
return ERROR_SEND_STATUS;
uri[res] = '\0';
sigma_dut_print(dut, DUT_MSG_DEBUG, "URI: %s", uri);
free(dut->dpp_peer_uri);
dut->dpp_peer_uri = strdup(uri);
return SUCCESS_SEND_STATUS;
}
static int dpp_hostapd_conf_update(struct sigma_dut *dut,
struct sigma_conn *conn, const char *ifname,
struct wpa_ctrl *ctrl)
{
int res;
char buf[2000], buf2[2500], *pos, *pos2;
const char *conf_data_events[] = {
"DPP-CONNECTOR",
"DPP-CONFOBJ-PASS",
"DPP-CONFOBJ-PSK",
"DPP-C-SIGN-KEY",
"DPP-NET-ACCESS-KEY",
NULL
};
unsigned int old_timeout;
int legacy_akm, dpp_akm;
char *connector = NULL, *psk = NULL, *csign = NULL,
*net_access_key = NULL;
char pass[64];
int pass_len = 0;
int ret = 0;
sigma_dut_print(dut, DUT_MSG_INFO,
"Update hostapd configuration based on DPP Config Object");
if (wpa_command(ifname, "SET wpa 2") < 0 ||
wpa_command(ifname, "SET wpa_key_mgmt DPP") < 0 ||
wpa_command(ifname, "SET ieee80211w 1") < 0 ||
wpa_command(ifname, "SET rsn_pairwise CCMP") < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to update AP security parameters");
goto out;
}
res = get_wpa_cli_event(dut, ctrl, "DPP-CONFOBJ-AKM", buf, sizeof(buf));
if (res < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,No DPP-CONFOBJ-AKM");
goto out;
}
pos = strchr(buf, ' ');
if (!pos)
return -2;
pos++;
sigma_dut_print(dut, DUT_MSG_INFO,
"DPP: Config Object AKM: %s", pos);
legacy_akm = strstr(pos, "psk") != NULL || strstr(pos, "sae") != NULL;
dpp_akm = strstr(pos, "dpp") != NULL;
res = get_wpa_cli_event(dut, ctrl, "DPP-CONFOBJ-SSID",
buf, sizeof(buf));
if (res < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,No DPP-CONFOBJ-SSID");
goto out;
}
pos = strchr(buf, ' ');
if (!pos)
return -2;
pos++;
sigma_dut_print(dut, DUT_MSG_INFO,
"DPP: Config Object SSID: %s", pos);
snprintf(buf2, sizeof(buf2), "SET ssid %s", pos);
if (wpa_command(ifname, buf2) < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to update AP SSID");
goto out;
}
if (wpa_command(ifname, "SET utf8_ssid 1") < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to update AP UTF-8 SSID capa");
goto out;
}
while ((dpp_akm && (!connector || !csign || !net_access_key)) ||
(legacy_akm && !pass_len && !psk)) {
res = get_wpa_cli_events(dut, ctrl, conf_data_events,
buf, sizeof(buf));
if (res < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Not all config object information received");
goto out;
}
if (strstr(buf, "DPP-CONNECTOR")) {
pos = strchr(buf, ' ');
if (!pos) {
ret = -2;
goto out;
}
pos++;
sigma_dut_print(dut, DUT_MSG_INFO, "DPP: Connector: %s",
pos);
if (!connector)
connector = strdup(pos);
} else if (strstr(buf, "DPP-C-SIGN-KEY")) {
pos = strchr(buf, ' ');
if (!pos) {
ret = -2;
goto out;
}
pos++;
sigma_dut_print(dut, DUT_MSG_INFO,
"DPP: C-sign-key: %s", pos);
if (!csign)
csign = strdup(pos);
} else if (strstr(buf, "DPP-NET-ACCESS-KEY")) {
pos = strchr(buf, ' ');
if (!pos) {
ret = -2;
goto out;
}
pos++;
if (!net_access_key)
net_access_key = strdup(pos);
} else if (strstr(buf, "DPP-CONFOBJ-PASS")) {
pos = strchr(buf, ' ');
if (!pos) {
ret = -2;
goto out;
}
pos++;
pass_len = parse_hexstr(pos, (u8 *) pass, sizeof(pass));
if (pass_len < 0 || (size_t) pass_len >= sizeof(pass)) {
ret = -2;
goto out;
}
pass[pass_len] = '\0';
sigma_dut_print(dut, DUT_MSG_INFO,
"DPP: Passphrase: %s", pass);
} else if (strstr(buf, "DPP-CONFOBJ-PSK")) {
pos = strchr(buf, ' ');
if (!pos) {
ret = -2;
goto out;
}
pos++;
sigma_dut_print(dut, DUT_MSG_INFO, "DPP: PSK: %s", pos);
if (!psk)
psk = strdup(pos);
}
}
if ((!connector || !dpp_akm) &&
wpa_command(ifname, "SET wpa_key_mgmt WPA-PSK") < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to update AP security parameters");
goto out;
}
if (connector && dpp_akm && legacy_akm &&
wpa_command(ifname, "SET wpa_key_mgmt DPP WPA-PSK") < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to update AP security parameters");
goto out;
}
if (pass_len) {
snprintf(buf2, sizeof(buf2), "SET wpa_passphrase %s",
pass);
if (wpa_command(ifname, buf2) < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to set passphrase");
goto out;
}
} else if (psk) {
snprintf(buf2, sizeof(buf2), "SET wpa_psk %s", psk);
if (wpa_command(ifname, buf2) < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to set PSK");
goto out;
}
}
if (connector) {
snprintf(buf2, sizeof(buf2), "SET dpp_connector %s", connector);
if (wpa_command(ifname, buf2) < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to update AP Connector");
goto out;
}
}
if (csign) {
snprintf(buf2, sizeof(buf2), "SET dpp_csign %s", csign);
if (wpa_command(ifname, buf2) < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to update AP C-sign-key");
goto out;
}
}
if (net_access_key) {
pos2 = strchr(net_access_key, ' ');
if (pos2)
*pos2++ = '\0';
sigma_dut_print(dut, DUT_MSG_INFO, "DPP: netAccessKey: %s",
net_access_key);
snprintf(buf2, sizeof(buf2), "SET dpp_netaccesskey %s",
net_access_key);
if (wpa_command(ifname, buf2) < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to update AP netAccessKey");
goto out;
}
if (pos2) {
sigma_dut_print(dut, DUT_MSG_INFO,
"DPP: netAccessKey expiry: %s", pos2);
snprintf(buf2, sizeof(buf2),
"SET dpp_netaccesskey_expiry %s", pos2);
if (wpa_command(ifname, buf2) < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to update AP netAccessKey expiry");
goto out;
}
}
}
if (dut->ap_start_disabled)
sigma_dut_print(dut, DUT_MSG_INFO, "Clear ap_start_disabled");
if (wpa_command(ifname, "SET start_disabled 0") < 0 &&
dut->ap_start_disabled) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to update AP security parameters");
goto out;
}
dut->ap_start_disabled = 0;
/* Wait for a possible Configuration Result to be sent */
old_timeout = dut->default_timeout;
dut->default_timeout = 1;
get_wpa_cli_event(dut, ctrl, "DPP-TX-STATUS", buf, sizeof(buf));
dut->default_timeout = old_timeout;
if (dut->ap_oper_chn) {
sigma_dut_print(dut, DUT_MSG_INFO,
"Set AP operating channel %d",
dut->ap_channel);
snprintf(buf, sizeof(buf), "SET channel %d", dut->ap_channel);
wpa_command(ifname, buf);
}
if (wpa_command(ifname, "DISABLE") < 0 ||
wpa_command(ifname, "ENABLE") < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to update AP configuration");
goto out;
}
res = get_wpa_cli_event(dut, ctrl, "AP-ENABLED", buf, sizeof(buf));
if (res < 0) {
send_resp(dut, conn, SIGMA_ERROR, "errorCode,No AP-ENABLED");
goto out;
}
ret = 1;
out:
free(connector);
free(psk);
free(csign);
free(net_access_key);
return ret;
}
struct dpp_test_info {
const char *step;
const char *frame;
const char *attr;
int value;
};
static const struct dpp_test_info dpp_tests[] = {
{ "InvalidValue", "AuthenticationRequest", "WrappedData", 1 },
{ "InvalidValue", "AuthenticationResponse", "WrappedData", 2 },
{ "InvalidValue", "AuthenticationResponse", "PrimaryWrappedData", 2 },
{ "InvalidValue", "AuthenticationConfirm", "WrappedData", 3 },
{ "InvalidValue", "PKEXCRRequest", "WrappedData", 4 },
{ "InvalidValue", "PKEXCRResponse", "WrappedData", 5 },
{ "InvalidValue", "ConfigurationRequest", "WrappedData", 6 },
{ "InvalidValue", "ConfigurationResponse", "WrappedData", 7 },
{ "InvalidValue", "AuthenticationRequest", "InitCapabilities", 8 },
{ "MissingAttribute", "AuthenticationRequest", "RespBSKeyHash", 10 },
{ "MissingAttribute", "AuthenticationRequest", "InitBSKeyHash", 11 },
{ "MissingAttribute", "AuthenticationRequest", "InitProtocolKey", 12 },
{ "MissingAttribute", "AuthenticationRequest", "InitNonce", 13 },
{ "MissingAttribute", "AuthenticationRequest", "InitCapabilities", 14 },
{ "MissingAttribute", "AuthenticationRequest", "WrappedData", 15 },
{ "MissingAttribute", "AuthenticationResponse", "DPPStatus", 16 },
{ "MissingAttribute", "AuthenticationResponse", "RespBSKeyHash", 17 },
{ "MissingAttribute", "AuthenticationResponse", "InitBSKeyHash", 18 },
{ "MissingAttribute", "AuthenticationResponse", "RespProtocolKey", 19 },
{ "MissingAttribute", "AuthenticationResponse", "RespNonce", 20 },
{ "MissingAttribute", "AuthenticationResponse", "InitNonce", 21 },
{ "MissingAttribute", "AuthenticationResponse", "RespCapabilities",
22 },
{ "MissingAttribute", "AuthenticationResponse", "RespAuthTag", 23 },
{ "MissingAttribute", "AuthenticationResponse", "WrappedData", 24 },
{ "MissingAttribute", "AuthenticationResponse", "PrimaryWrappedData",
24 },
{ "MissingAttribute", "AuthenticationConfirm", "DPPStatus", 25 },
{ "MissingAttribute", "AuthenticationConfirm", "RespBSKeyHash", 26 },
{ "MissingAttribute", "AuthenticationConfirm", "InitBSKeyHash", 27 },
{ "MissingAttribute", "AuthenticationConfirm", "InitAuthTag", 28 },
{ "MissingAttribute", "AuthenticationConfirm", "WrappedData", 29 },
{ "InvalidValue", "AuthenticationResponse", "InitNonce", 30 },
{ "InvalidValue", "AuthenticationResponse", "RespCapabilities", 31 },
{ "InvalidValue", "AuthenticationResponse", "RespAuthTag", 32 },
{ "InvalidValue", "AuthenticationConfirm", "InitAuthTag", 33 },
{ "MissingAttribute", "PKEXExchangeRequest", "FiniteCyclicGroup", 34 },
{ "MissingAttribute", "PKEXExchangeRequest", "EncryptedKey", 35 },
{ "MissingAttribute", "PKEXExchangeResponse", "DPPStatus", 36 },
{ "MissingAttribute", "PKEXExchangeResponse", "EncryptedKey", 37 },
{ "MissingAttribute", "PKEXCRRequest", "BSKey", 38 },
{ "MissingAttribute", "PKEXCRRequest", "InitAuthTag", 39 },
{ "MissingAttribute", "PKEXCRRequest", "WrappedData", 40 },
{ "MissingAttribute", "PKEXCRResponse", "BSKey", 41 },
{ "MissingAttribute", "PKEXCRResponse", "RespAuthTag", 42 },
{ "MissingAttribute", "PKEXCRResponse", "WrappedData", 43 },
{ "InvalidValue", "PKEXExchangeRequest", "EncryptedKey", 44 },
{ "InvalidValue", "PKEXExchangeResponse", "EncryptedKey", 45 },
{ "InvalidValue", "PKEXExchangeResponse", "DPPStatus", 46 },
{ "InvalidValue", "PKEXCRRequest", "BSKey", 47 },
{ "InvalidValue", "PKEXCRResponse", "BSKey", 48 },
{ "InvalidValue", "PKEXCRRequest", "InitAuthTag", 49 },
{ "InvalidValue", "PKEXCRResponse", "RespAuthTag", 50 },
{ "MissingAttribute", "ConfigurationRequest", "EnrolleeNonce", 51 },
{ "MissingAttribute", "ConfigurationRequest", "ConfigAttr", 52 },
{ "MissingAttribute", "ConfigurationRequest", "WrappedData", 53 },
{ "MissingAttribute", "ConfigurationResponse", "EnrolleeNonce", 54 },
{ "MissingAttribute", "ConfigurationResponse", "ConfigObj", 55 },
{ "MissingAttribute", "ConfigurationResponse", "DPPStatus", 56 },
{ "MissingAttribute", "ConfigurationResponse", "WrappedData", 57 },
{ "InvalidValue", "ConfigurationResponse", "DPPStatus", 58 },
{ "InvalidValue", "ConfigurationResponse", "EnrolleeNonce", 59 },
{ "MissingAttribute", "PeerDiscoveryRequest", "TransactionID", 60 },
{ "MissingAttribute", "PeerDiscoveryRequest", "Connector", 61 },
{ "MissingAttribute", "PeerDiscoveryResponse", "TransactionID", 62 },
{ "MissingAttribute", "PeerDiscoveryResponse", "DPPStatus", 63 },
{ "MissingAttribute", "PeerDiscoveryResponse", "Connector", 64 },
{ "InvalidValue", "AuthenticationRequest", "InitProtocolKey", 66 },
{ "InvalidValue", "AuthenticationResponse", "RespProtocolKey", 67 },
{ "InvalidValue", "AuthenticationRequest", "RespBSKeyHash", 68 },
{ "InvalidValue", "AuthenticationRequest", "InitBSKeyHash", 69 },
{ "InvalidValue", "AuthenticationResponse", "RespBSKeyHash", 70 },
{ "InvalidValue", "AuthenticationResponse", "InitBSKeyHash", 71 },
{ "InvalidValue", "AuthenticationConfirm", "RespBSKeyHash", 72 },
{ "InvalidValue", "AuthenticationConfirm", "InitBSKeyHash", 73 },
{ "InvalidValue", "AuthenticationResponse", "DPPStatus", 74 },
{ "InvalidValue", "AuthenticationConfirm", "DPPStatus", 75 },
{ "InvalidValue", "ConfigurationRequest", "ConfigAttr", 76 },
{ "InvalidValue", "PeerDiscoveryResponse", "TransactionID", 77 },
{ "InvalidValue", "PeerDiscoveryResponse", "DPPStatus", 78 },
{ "InvalidValue", "PeerDiscoveryResponse", "Connector", 79 },
{ "InvalidValue", "PeerDiscoveryRequest", "Connector", 80 },
{ "InvalidValue", "AuthenticationRequest", "InitNonce", 81 },
{ "InvalidValue", "PeerDiscoveryRequest", "TransactionID", 82 },
{ "InvalidValue", "ConfigurationRequest", "EnrolleeNonce", 83 },
{ "Timeout", "PKEXExchangeResponse", NULL, 84 },
{ "Timeout", "PKEXCRRequest", NULL, 85 },
{ "Timeout", "PKEXCRResponse", NULL, 86 },
{ "Timeout", "AuthenticationRequest", NULL, 87 },
{ "Timeout", "AuthenticationResponse", NULL, 88 },
{ "Timeout", "AuthenticationConfirm", NULL, 89 },
{ "Timeout", "ConfigurationRequest", NULL, 90 },
{ NULL, NULL, NULL, 0 }
};
static int dpp_get_test(const char *step, const char *frame, const char *attr)
{
int i;
for (i = 0; dpp_tests[i].step; i++) {
if (strcasecmp(step, dpp_tests[i].step) == 0 &&
strcasecmp(frame, dpp_tests[i].frame) == 0 &&
((!attr && dpp_tests[i].attr == NULL) ||
(attr && strcasecmp(attr, dpp_tests[i].attr) == 0)))
return dpp_tests[i].value;
}
return -1;
}
static int dpp_wait_tx(struct sigma_dut *dut, struct wpa_ctrl *ctrl,
int frame_type)
{
char buf[200], tmp[20];
int res;
snprintf(tmp, sizeof(tmp), "type=%d", frame_type);
for (;;) {
res = get_wpa_cli_event(dut, ctrl, "DPP-TX", buf, sizeof(buf));
if (res < 0)
return -1;
if (strstr(buf, tmp) != NULL)
break;
}
return 0;
}
static int dpp_wait_tx_status(struct sigma_dut *dut, struct wpa_ctrl *ctrl,
int frame_type)
{
char buf[200], tmp[20];
int res;
snprintf(tmp, sizeof(tmp), "type=%d", frame_type);
for (;;) {
res = get_wpa_cli_event(dut, ctrl, "DPP-TX", buf, sizeof(buf));
if (res < 0)
return -1;
if (strstr(buf, tmp) != NULL)
break;
}
res = get_wpa_cli_event(dut, ctrl, "DPP-TX-STATUS",
buf, sizeof(buf));
if (res < 0 || strstr(buf, "result=FAILED") != NULL)
return -1;
return 0;
}
static int dpp_wait_rx(struct sigma_dut *dut, struct wpa_ctrl *ctrl,
int frame_type, unsigned int max_wait)
{
char buf[200], tmp[20];
int res;
unsigned int old_timeout;
old_timeout = dut->default_timeout;
if (max_wait > 0 && dut->default_timeout > max_wait)
dut->default_timeout = max_wait;
snprintf(tmp, sizeof(tmp), "type=%d", frame_type);
for (;;) {
res = get_wpa_cli_event(dut, ctrl, "DPP-RX", buf, sizeof(buf));
if (res < 0) {
dut->default_timeout = old_timeout;
return -1;
}
if (strstr(buf, tmp) != NULL)
break;
}
dut->default_timeout = old_timeout;
return 0;
}
static int dpp_wait_rx_conf_req(struct sigma_dut *dut, struct wpa_ctrl *ctrl,
unsigned int max_wait)
{
char buf[200];
int res;
unsigned int old_timeout;
old_timeout = dut->default_timeout;
if (max_wait > 0 && dut->default_timeout > max_wait)
dut->default_timeout = max_wait;
for (;;) {
res = get_wpa_cli_event(dut, ctrl, "DPP-CONF-REQ-RX",
buf, sizeof(buf));
if (res < 0) {
dut->default_timeout = old_timeout;
return -1;
}
break;
}
dut->default_timeout = old_timeout;
return 0;
}
static int dpp_scan_peer_qrcode(struct sigma_dut *dut)
{
#ifdef ANDROID
char buf[100];
char *buf2 = NULL;
FILE *fp = NULL;
uint32_t length;
unsigned int count;
unlink(dpp_qrcode_file);
snprintf(buf, sizeof(buf),
"am start -n w1.fi.wpadebug/w1.fi.wpadebug.QrCodeReadActivity");
if (system(buf) != 0) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to launch QR Code scanner");
return -1;
}
count = 0;
while (!(fp = fopen(dpp_qrcode_file, "r"))) {
if (count > dut->default_timeout) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to open dpp_qrcode_file - QR Code scanning timed out");
return -1;
}
sleep(1);
count++;
}
if (fseek(fp, 0, SEEK_END) < 0 || (length = ftell(fp)) <= 0 ||
fseek(fp, 0, SEEK_SET) < 0) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to get QR Code result file length");
fclose(fp);
return -1;
}
buf2 = malloc(length + 1);
if (!buf2) {
fclose(fp);
return -1;
}
if (fread(buf2, 1, length, fp) != length) {
fclose(fp);
free(buf2);
return -1;
}
fclose(fp);
buf2[length] = '\0';
free(dut->dpp_peer_uri);
dut->dpp_peer_uri = strdup(buf2);
free(buf2);
return 0;
#else /* ANDROID */
pid_t pid;
int pid_status;
int pipe_out[2];
char buf[4000], *pos;
ssize_t len;
int res = -1, ret;
struct timeval tv;
fd_set rfd;
if (pipe(pipe_out) != 0) {
perror("pipe");
return -1;
}
pid = fork();
if (pid < 0) {
perror("fork");
close(pipe_out[0]);
close(pipe_out[1]);
return -1;
}
if (pid == 0) {
char *argv[4] = { "zbarcam", "--raw", "--prescale=320x240",
NULL };
dup2(pipe_out[1], STDOUT_FILENO);
close(pipe_out[0]);
close(pipe_out[1]);
execv("/usr/bin/zbarcam", argv);
perror("execv");
exit(0);
return -1;
}
close(pipe_out[1]);
FD_ZERO(&rfd);
FD_SET(pipe_out[0], &rfd);
tv.tv_sec = dut->default_timeout;
tv.tv_usec = 0;
ret = select(pipe_out[0] + 1, &rfd, NULL, NULL, &tv);
if (ret < 0) {
perror("select");
goto out;
}
if (ret == 0) {
sigma_dut_print(dut, DUT_MSG_DEBUG,
"QR Code scanning timed out");
goto out;
}
len = read(pipe_out[0], buf, sizeof(buf));
if (len <= 0)
goto out;
if (len == sizeof(buf))
len--;
buf[len] = '\0';
pos = strchr(buf, '\n');
if (pos)
*pos = '\0';
sigma_dut_print(dut, DUT_MSG_DEBUG, "URI from QR scanner: %s", buf);
free(dut->dpp_peer_uri);
dut->dpp_peer_uri = strdup(buf);
res = 0;
out:
close(pipe_out[0]);
kill(pid, SIGTERM);
waitpid(pid, &pid_status, 0);
return res;
#endif /* ANDROID */
}
static int dpp_display_own_qrcode(struct sigma_dut *dut)
{
char buf[200], resp[2000];
const char *ifname = get_station_ifname(dut);
#ifdef ANDROID
FILE *fp;
#else /* ANDROID */
pid_t pid;
int pid_status;
#endif /* ANDROID */
snprintf(buf, sizeof(buf), "DPP_BOOTSTRAP_GET_URI %d",
dut->dpp_local_bootstrap);
if (wpa_command_resp(ifname, buf, resp, sizeof(resp)) < 0 ||
strncmp(resp, "FAIL", 4) == 0)
return -2;
sigma_dut_print(dut, DUT_MSG_DEBUG, "Own bootstrap URI: %s", resp);
#ifdef ANDROID
unlink(dpp_qrcode_file);
fp = fopen(dpp_qrcode_file, "w");
if (!fp) {
sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to open file %s",
dpp_qrcode_file);
return -2;
}
fwrite(resp, 1, strlen(resp), fp);
fclose(fp);
snprintf(buf, sizeof(buf),
"am start -n w1.fi.wpadebug/w1.fi.wpadebug.QrCodeDisplayActivity");
if (system(buf) != 0) {
sigma_dut_print(dut, DUT_MSG_ERROR, "Failed to display QR Code");
return -1;
}
#else /* ANDROID */
pid = fork();
if (pid < 0) {
perror("fork");
return -1;
}
if (pid == 0) {
char *argv[3] = { "qr", resp, NULL };
execv("/usr/bin/qr", argv);
perror("execv");
exit(0);
return -1;
}
waitpid(pid, &pid_status, 0);
#endif /* ANDROID */
return 0;
}
static int dpp_process_auth_response(struct sigma_dut *dut,
struct sigma_conn *conn,
struct wpa_ctrl *ctrl,
const char **auth_events,
const char *action_type,
int check_mutual, char *buf, size_t buflen)
{
int res;
res = get_wpa_cli_events(dut, ctrl, auth_events, buf, buflen);
if (res < 0) {
send_resp(dut, conn, SIGMA_COMPLETE,
"BootstrapResult,OK,AuthResult,Timeout");
return res;
}
sigma_dut_print(dut, DUT_MSG_DEBUG, "DPP auth result: %s", buf);
if (strstr(buf, "DPP-RESPONSE-PENDING")) {
/* Display own QR code in manual mode */
if (action_type && strcasecmp(action_type, "ManualDPP") == 0 &&
dpp_display_own_qrcode(dut) < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Failed to display own QR code");
return -1;
}
/* Wait for the actual result after the peer has scanned the
* QR Code. */
res = get_wpa_cli_events(dut, ctrl, auth_events,
buf, buflen);
if (res < 0) {
send_resp(dut, conn, SIGMA_COMPLETE,
"BootstrapResult,OK,AuthResult,Timeout");
return res;
}
sigma_dut_print(dut, DUT_MSG_DEBUG, "DPP auth result: %s", buf);
} else if (strstr(buf, "DPP-AUTH-INIT-FAILED")) {
send_resp(dut, conn, SIGMA_COMPLETE,
"BootstrapResult,OK,AuthResult,Timeout");
return -1;
}
if (check_mutual) {
if (strstr(buf, "DPP-NOT-COMPATIBLE")) {
send_resp(dut, conn, SIGMA_COMPLETE,
"BootstrapResult,OK,AuthResult,ROLES_NOT_COMPATIBLE");
return -1;
}
if (!strstr(buf, "DPP-AUTH-DIRECTION")) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,No event for auth direction seen");
return -1;
}
sigma_dut_print(dut, DUT_MSG_DEBUG, "DPP auth direction: %s",
buf);
if (strstr(buf, "mutual=1") == NULL) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Peer did not use mutual authentication");
return -1;
}
}
return 0;
}
static int dpp_process_csr(struct sigma_dut *dut, const char *ifname,
char *csr_event)
{
FILE *f;
char buf[2000], cmd[2500], tmp[300];
char *pos;
int peer;
size_t len;
pos = strstr(csr_event, " peer=");
if (!pos) {
sigma_dut_print(dut, DUT_MSG_INFO, "No peer id known for CSR");
return -1;