-
Notifications
You must be signed in to change notification settings - Fork 26
/
dpp.c
4766 lines (4241 loc) · 128 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>
#ifdef ANDROID_MDNS
#include <cutils/properties.h>
#endif /* ANDROID_MDNS */
#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 */
#ifdef ANDROID_MDNS
static int android_start_mdnsd(struct sigma_dut *dut)
{
char value[PROPERTY_VALUE_MAX];
if (property_get("init.svc.mdnsd", value, "") == strlen("running") &&
strncmp(value, "running", strlen("running")) == 0) {
sigma_dut_print(dut, DUT_MSG_DEBUG, "mDNS service is running");
return 0;
}
sigma_dut_print(dut, DUT_MSG_DEBUG, "Start mDNS service");
property_set("ctl.start", "mdnsd");
sleep(5);
if (property_get("init.svc.mdnsd", value, "") == strlen("running") &&
strncmp(value, "running", strlen("running")) == 0) {
sigma_dut_print(dut, DUT_MSG_DEBUG, "mDNS service is running");
return 0;
}
sigma_dut_print(dut, DUT_MSG_DEBUG, "Failed to start mDNS service");
return -1;
}
#endif /* ANDROID_MDNS */
static const char * dpp_mdns_role_txt(enum dpp_mdns_role role)
{
switch (role) {
case DPP_MDNS_NOT_RUNNING:
break;
case DPP_MDNS_RELAY:
return "relay";
case DPP_MDNS_CONTROLLER:
return "controller";
case DPP_MDNS_BOOTSTRAPPING:
return "bootstrapping";
}
return "unknown";
}
#ifdef ANDROID_MDNS
static void DNSSD_API
addrinfo_reply(DNSServiceRef sdref, DNSServiceFlags flags,
uint32_t interfaceIndex, DNSServiceErrorType errorCode,
const char *hostname, const struct sockaddr *address,
uint32_t ttl, void *context)
{
struct sigma_dut *dut = context;
if (errorCode) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"addrinfo_reply: Error %d on ifindex %d",
errorCode, interfaceIndex);
return;
}
if (address && address->sa_family == AF_INET) {
const unsigned char *b = (const unsigned char *)
&((struct sockaddr_in *)address)->sin_addr;
snprintf(dut->mdns_discover.ipaddr,
sizeof(dut->mdns_discover.ipaddr),
"%d.%d.%d.%d",
b[0], b[1], b[2], b[3]);
sigma_dut_print(dut, DUT_MSG_DEBUG,
"addrinfo_reply: host_ipaddr: %s",
dut->mdns_discover.ipaddr);
return;
}
}
static void DNSSD_API
resolve_reply(DNSServiceRef sdref, const DNSServiceFlags flags,
uint32_t ifIndex, DNSServiceErrorType errorCode,
const char *fullname, const char *hosttarget,
uint16_t opaqueport, uint16_t txtLen,
const unsigned char *txtRecord, void *context)
{
struct sigma_dut *dut = context;
union { uint16_t s; unsigned char b[2]; } port = { opaqueport };
uint16_t PortAsNumber = ((uint16_t) port.b[0]) << 8 | port.b[1];
uint8_t bskeyhash_len;
char *bskeyhash;
if (errorCode) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"resolve_reply: Error code %d reported",
errorCode);
return;
}
dut->mdns_discover.port = PortAsNumber;
dut->mdns_discover.host_name = strdup(hosttarget);
if (!dut->mdns_discover.host_name)
return;
sigma_dut_print(dut, DUT_MSG_DEBUG,
"resolve_reply: host_name: %s, port: %d",
dut->mdns_discover.host_name, dut->mdns_discover.port);
if (!txtLen || !txtRecord) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"resolve_reply: No records");
return;
}
bskeyhash = dut->mdnssd.txt_get_value(txtLen, txtRecord, "bskeyhash",
&bskeyhash_len);
if (bskeyhash && bskeyhash_len) {
dut->mdns_discover.bskeyhash = malloc(bskeyhash_len + 1);
if (!dut->mdns_discover.bskeyhash)
return;
memcpy(dut->mdns_discover.bskeyhash, bskeyhash, bskeyhash_len);
dut->mdns_discover.bskeyhash[bskeyhash_len] = '\0';
sigma_dut_print(dut, DUT_MSG_DEBUG,
"resolve_reply: bskeyhash %s",
dut->mdns_discover.bskeyhash);
}
}
static void DNSSD_API
browse_reply(DNSServiceRef sdref, const DNSServiceFlags flags,
uint32_t ifIndex, DNSServiceErrorType errorCode,
const char *replyName, const char *replyType,
const char *replyDomain, void *context)
{
struct sigma_dut *dut = context;
if (errorCode) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"browse_reply: Error code %d reported",
errorCode);
return;
}
sigma_dut_print(dut, DUT_MSG_DEBUG,
"browse_reply: ifindex:%d, service name %s, service tpe %s, service domain %s",
ifIndex, replyName, replyType, replyDomain);
/* Store only the first result */
if (dut->mdns_discover.name)
return;
if (!(flags & kDNSServiceFlagsAdd))
return;
dut->mdns_discover.ifindex = ifIndex;
dut->mdns_discover.name = strdup(replyName);
if (!dut->mdns_discover.name)
return;
dut->mdns_discover.type = strdup(replyType);
if (!dut->mdns_discover.type)
return;
dut->mdns_discover.domain = strdup(replyDomain);
if (!dut->mdns_discover.domain)
return;
}
static void mdns_process_results(struct sigma_dut *dut, DNSServiceRef *service)
{
struct timeval stop, now, timeout;
fd_set input;
int fd = dut->mdnssd.service_socket_fd(*service);
FD_ZERO(&input);
FD_SET(fd, &input);
timeout.tv_sec = 0;
timeout.tv_usec = 500000;
gettimeofday(&stop, NULL);
stop.tv_sec += 1;
while (1) {
if (select(fd + 1, &input, NULL, NULL, &timeout) < 0)
continue;
if (FD_ISSET(fd, &input))
dut->mdnssd.service_process_result(*service);
gettimeofday(&now, NULL);
if (now.tv_sec > stop.tv_sec ||
(now.tv_sec == stop.tv_sec && now.tv_usec >= stop.tv_usec))
break;
}
}
#endif /* ANDROID_MDNS */
static int dpp_mdns_discover(struct sigma_dut *dut, enum dpp_mdns_role role,
char *addr, size_t addr_size, unsigned int *port,
unsigned char *hash)
{
#ifdef ANDROID_MDNS
DNSServiceRef service;
char service_type[100];
char interface_name[IFNAMSIZ];
int err = 0;
#else /* ANDROID_MDNS */
char cmd[200], buf[10000], *pos, *pos2, *pos3;
size_t len;
FILE *f;
#endif /* ANDROID_MDNS */
char *ifname = NULL, *ipaddr = NULL, *bskeyhash = NULL;
if (port)
*port = 0;
#ifdef ANDROID_MDNS
if (!dut->mdnssd_so) {
sigma_dut_print(dut, DUT_MSG_ERROR, "libmdnssd not loaded");
return -1;
}
if (android_start_mdnsd(dut))
return -1;
memset(&dut->mdns_discover, 0, sizeof(dut->mdns_discover));
snprintf(service_type, sizeof(service_type), "_dpp._tcp,_%s",
dpp_mdns_role_txt(role));
err = dut->mdnssd.service_browse(&service, 0, 0, service_type, "",
browse_reply, dut);
if (err != kDNSServiceErr_NoError) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"%s browse failed with error %d",
dut->mdns_discover.type, err);
err = -1;
goto out;
}
mdns_process_results(dut, &service);
if (!dut->mdns_discover.name ||
!dut->mdns_discover.domain ||
!dut->mdns_discover.type) {
sigma_dut_print(dut, DUT_MSG_ERROR, "Service type %s not found",
dut->mdns_discover.type);
err = -1;
goto out;
}
dut->mdnssd.service_deallocate(service);
err = dut->mdnssd.service_resolve(&service, 0,
dut->mdns_discover.ifindex,
dut->mdns_discover.name,
dut->mdns_discover.type,
dut->mdns_discover.domain,
resolve_reply, dut);
if (err != kDNSServiceErr_NoError) {
sigma_dut_print(dut, DUT_MSG_ERROR, "%s/%s resolve failed with error %d",
dut->mdns_discover.name,
dut->mdns_discover.domain, err);
err = -1;
goto out;
}
mdns_process_results(dut, &service);
if (!dut->mdns_discover.host_name) {
sigma_dut_print(dut, DUT_MSG_ERROR, "%s/%s host name not found",
dut->mdns_discover.name,
dut->mdns_discover.domain);
err = -1;
goto out;
}
dut->mdnssd.service_deallocate(service);
err = dut->mdnssd.get_addr_info(&service,
kDNSServiceFlagsReturnIntermediates,
dut->mdns_discover.ifindex,
kDNSServiceProtocol_IPv4,
dut->mdns_discover.host_name,
addrinfo_reply, dut);
if (err != kDNSServiceErr_NoError) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"get addr info failed for %s with error %d",
dut->mdns_discover.host_name, err);
err = -1;
goto out;
}
mdns_process_results(dut, &service);
if (dut->mdns_discover.ipaddr[0] == '\0') {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Could not fetch IP address for %s",
dut->mdns_discover.host_name);
err = -1;
goto out;
}
dut->mdnssd.service_deallocate(service);
service = NULL;
if (!if_indextoname(dut->mdns_discover.ifindex, interface_name)) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Could not get interface name of %d",
dut->mdns_discover.ifindex);
err = -1;
goto out;
}
ifname = interface_name;
ipaddr = dut->mdns_discover.ipaddr;
bskeyhash = dut->mdns_discover.bskeyhash;
if (port)
*port = dut->mdns_discover.port;
#else /* ANDROID_MDNS */
snprintf(cmd, sizeof(cmd), "avahi-browse _%s._sub._dpp._tcp -r -t -p",
dpp_mdns_role_txt(role));
sigma_dut_print(dut, DUT_MSG_DEBUG, "Run: %s", cmd);
f = popen(cmd, "r");
if (!f) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Could not run avahi-browse: %s",
strerror(errno));
return -1;
}
len = fread(buf, 1, sizeof(buf) - 1, f);
sigma_dut_print(dut, DUT_MSG_DEBUG, "avahi-browse returned %zu octets",
len);
pclose(f);
if (!len)
return -1;
buf[len] = '\0';
sigma_dut_print(dut, DUT_MSG_DEBUG, "mDNS results:\n%s", buf);
pos = buf;
while (pos) {
pos2 = strchr(pos, '\n');
if (pos2)
*pos2 = '\0';
if (pos[0] != '=')
goto next;
pos = strchr(pos, ';');
if (!pos)
goto next;
pos++;
/* ifname */
pos3 = strchr(pos, ';');
if (!pos3)
goto next;
*pos3 = '\0';
ifname = pos;
pos = pos3 + 1;
/* IP version */
if (strncmp(pos, "IPv4;", 5) != 0)
goto next;
pos = strchr(pos, ';');
if (!pos)
goto next;
pos++;
/* name */
pos = strchr(pos, ';');
if (!pos)
goto next;
pos++;
/* service */
pos = strchr(pos, ';');
if (!pos)
goto next;
pos++;
/* local? */
pos = strchr(pos, ';');
if (!pos)
goto next;
pos++;
/* fqdn */
pos = strchr(pos, ';');
if (!pos)
goto next;
pos++;
/* IP address */
pos3 = strchr(pos, ';');
if (!pos3)
goto next;
*pos3 = '\0';
ipaddr = pos;
pos = pos3 + 1;
if (port)
*port = atoi(pos);
pos = strstr(pos, "\"bskeyhash=");
if (pos) {
pos += 11;
pos3 = strchr(pos, '"');
if (pos3)
*pos3 = '\0';
bskeyhash = pos;
}
/* Could try to pick the most appropriate candidate if multiple
* entries are discovered */
break;
next:
if (!pos2)
break;
pos = pos2 + 1;
}
if (!ipaddr)
return -1;
#endif /* ANDROID_MDNS */
sigma_dut_print(dut, DUT_MSG_INFO, "Discovered (mDNS) service at %s@%s",
ipaddr, ifname);
if (bskeyhash && hash) {
unsigned char *bin;
size_t bin_len;
sigma_dut_print(dut, DUT_MSG_DEBUG, "bskeyhash: %s", bskeyhash);
bin = base64_decode(bskeyhash, strlen(bskeyhash), &bin_len);
if (!bin || bin_len != 32) {
sigma_dut_print(dut, DUT_MSG_INFO,
"Invalid bskeyhash value in mDNS records");
free(bin);
return -1;
}
memcpy(hash, bin, bin_len);
free(bin);
}
strlcpy(addr, ipaddr, addr_size);
#ifdef ANDROID_MDNS
out:
free(dut->mdns_discover.name);
free(dut->mdns_discover.type);
free(dut->mdns_discover.domain);
free(dut->mdns_discover.host_name);
free(dut->mdns_discover.bskeyhash);
memset(&dut->mdns_discover, 0, sizeof(dut->mdns_discover));
if (service)
dut->mdnssd.service_deallocate(service);
return err;
#else /* ANDROID_MDNS */
return 0;
#endif
}
int dpp_mdns_discover_relay_params(struct sigma_dut *dut)
{
char tcp_addr[30];
unsigned char hash[32];
if (dpp_mdns_discover(dut, DPP_MDNS_CONTROLLER,
tcp_addr, sizeof(tcp_addr), NULL, hash) < 0) {
sigma_dut_print(dut, DUT_MSG_INFO,
"Could not discover Controller IP address using mDNS");
return -1;
}
free(dut->ap_dpp_conf_addr);
dut->ap_dpp_conf_addr = strdup(tcp_addr);
free(dut->ap_dpp_conf_pkhash);
dut->ap_dpp_conf_pkhash = malloc(2 * 32 + 1);
if (dut->ap_dpp_conf_pkhash) {
int i;
char *pos = dut->ap_dpp_conf_pkhash;
char *end = pos + 2 * 32 + 1;
for (i = 0; i < 32; i++)
pos += snprintf(pos, end - pos, "%02x", hash[i]);
*pos = '\0';
}
if (dut->ap_dpp_conf_addr && dut->ap_dpp_conf_pkhash) {
const char *ifname = dut->hostapd_ifname;
sigma_dut_print(dut, DUT_MSG_INFO,
"Controller discovered using mDNS: %s (pkhash %s)",
dut->ap_dpp_conf_addr,
dut->ap_dpp_conf_pkhash);
if (dut->hostapd_running && ifname) {
char cmd[500];
snprintf(cmd, sizeof(cmd),
"DPP_RELAY_ADD_CONTROLLER %s %s",
dut->ap_dpp_conf_addr,
dut->ap_dpp_conf_pkhash);
if (wpa_command(ifname, cmd) < 0)
sigma_dut_print(dut, DUT_MSG_INFO,
"Failed to add Controller connection into hostapd");
else
sigma_dut_print(dut, DUT_MSG_INFO,
"Added Controller connection into hostapd");
}
return 0;
}
return -1;
}
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, bool chirp_chan)
{
if (dut->hostapd_running)
return 0;
if (dut->ap_dpp_conf_addr &&
strcasecmp(dut->ap_dpp_conf_addr, "mDNS") == 0 &&
dpp_mdns_discover_relay_params(dut) < 0) {
sigma_dut_print(dut, DUT_MSG_ERROR,
"Failed to discover Controller for AP Relay using mDNS - cannot start hostapd");
return -1;
}
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 = chirp_chan ? 6 : 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_map_curve(const char *val)
{
if (!val)
return "P-256";
if (strcasecmp(val, "BP-256R1") == 0)
return "BP-256";
if (strcasecmp(val, "BP-384R1") == 0)
return "BP-384";
if (strcasecmp(val, "BP-512R1") == 0)
return "BP-512";
return val;
}
static const char * dpp_get_curve(struct sigma_cmd *cmd, const char *arg)
{
return dpp_map_curve(get_param(cmd, arg));
}
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");
const char *val;
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;
char host[100];
bool uri_host;
char ip[30];
char uri_curves_buf[200];
const char *uri_curves = NULL;
host[0] = '\0';
ip[0] = '\0';
val = get_param(cmd, "DPPURIHost");
uri_host = val && strcasecmp(val, "Yes") == 0;
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;
}
val = get_param(cmd, "DPPURICurves");
if (val) {
char *saveptr, *r, *tmp, *end;
pos = uri_curves_buf;
end = pos + sizeof(uri_curves_buf);
*pos = '\0';
tmp = strdup(val);
if (!tmp)
return ERROR_SEND_STATUS;
r = strtok_r(tmp, ":", &saveptr);
while (r) {
int len;
len = snprintf(pos, end - pos, "%s%s",
pos > uri_curves_buf ? ":" : "",
dpp_map_curve(r));
if (len < 0)
break;
pos += len;
r = strtok_r(NULL, ":", &saveptr);
}
free(tmp);
uri_curves = uri_curves_buf;
}
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]);
if (uri_host) {
const char *ifname;
ifname = dut->bridge ? dut->bridge :
dut->hostapd_ifname;
if (get_ip_addr(ifname, 0, ip, sizeof(ip)) < 0) {
sigma_dut_print(dut, DUT_MSG_INFO,
"Could not get IP address for AP mode: bridge=%s hostapd_ifname=%s",
dut->bridge ? dut->bridge :
"N/A",
dut->hostapd_ifname);
ip[0] = '\0';
}
}
} 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;
}
if (uri_host &&
get_wpa_status(ifname, "ip_address", ip, sizeof(ip)) < 0) {
sigma_dut_print(dut, DUT_MSG_INFO,
"Could not get IP address for station mode");
ip[0] = '\0';
}
}
if (uri_host && ip[0] == '\0') {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,IP address not available on wireless interface");
return STATUS_SENT_ERROR;
}
if (uri_host)
snprintf(host, sizeof(host), " host=%s", ip);
pos = mac;
while (*pos) {
if (*pos == ':')
memmove(pos, pos + 1, strlen(pos));
else
pos++;
}
if (sigma_dut_is_ap(dut) && dpp_hostapd_run(dut, false) < 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%s%s%s",
type, curve,
include_mac ? " mac=" : "",
include_mac ? mac : "",
uri_curves ? " supported_curves=" : "",
uri_curves ? uri_curves : "",
host);
} 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%s%s%s",
type, curve, resp, include_mac ? " mac=" : "",
include_mac ? mac : "",
uri_curves ? " supported_curves=" : "",
uri_curves ? uri_curves : "",
host);
} 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%s%s%s",
type, curve, channel, include_mac ? " mac=" : "",
include_mac ? mac : "",
uri_curves ? " supported_curves=" : "",
uri_curves ? uri_curves : "",
host);
}
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 (dut->dpp_mdns == DPP_MDNS_CONTROLLER) {
/* Update mDNS advertisement since the local boostrapping key
* has changed. */
dpp_mdns_start(dut, DPP_MDNS_CONTROLLER);
}
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 void stop_stunnel(struct sigma_dut *dut)
{
FILE *f;
int pid;
f = fopen("/tmp/stunnel-dpp-rest-client.pid", "r");
if (!f)
return;
if (fscanf(f, "%d", &pid) == 1 && pid > 1) {
sigma_dut_print(dut, DUT_MSG_INFO,
"Terminate stunnel process %d",
pid);
kill(pid, SIGTERM);
}
fclose(f);
}
static enum sigma_cmd_result dpp_post_uri(struct sigma_dut *dut,
struct sigma_conn *conn,
struct sigma_cmd *cmd,
const char *uri)
{
FILE *f;
char buf[1000], *pos;
size_t len;
int status;
char tcp_addr[30];
unsigned int port;
const char *val;
bool http;
f = fopen("/tmp/dppuri.json", "w");
if (!f) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Could not write dppuri.json");
return STATUS_SENT_ERROR;
}
fprintf(f, "{\"dppUri\":\"%s\"}", uri);
fclose(f);
if (dpp_mdns_discover(dut, DPP_MDNS_BOOTSTRAPPING,
tcp_addr, sizeof(tcp_addr), &port, NULL) < 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Could not discover (mDNS) bootstrapping service");
return STATUS_SENT_ERROR;
}
sigma_dut_print(dut, DUT_MSG_INFO, "Bootstrapping service at %s:%u",
tcp_addr, port);
val = get_param(cmd, "DPPBootstrapPOST");
http = val && strcmp(val, "HTTP") == 0;
if (http)
goto skip_stunnel;
f = fopen("/tmp/stunnel-dpp-rest-client.conf", "w");
if (!f) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Could not write stunnel-dpp-rest-client.conf");
return STATUS_SENT_ERROR;
}
fprintf(f, "pid = /tmp/stunnel-dpp-rest-client.pid\n");
fprintf(f, "[PSK client]\n");
fprintf(f, "client = yes\n");
fprintf(f, "accept = 127.0.0.1:33333\n");
fprintf(f, "connect = %s:%u\n", tcp_addr, port);
fprintf(f, "PSKsecrets = /tmp/stunnel-dpp-rest-client.psk\n");
fclose(f);
f = fopen("/tmp/stunnel-dpp-rest-client.psk", "w");
if (!f) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Could not write stunnel-dpp-rest-client.psk");
return STATUS_SENT_ERROR;
}
fprintf(f, "dpp-rest:00112233445566778899aabbccddeeff\n");
fclose(f);
stop_stunnel(dut);
if (system("stunnel /tmp/stunnel-dpp-rest-client.conf") != 0) {
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Could not start stunnel");
return STATUS_SENT_ERROR;
}
skip_stunnel:
if (http)
snprintf(buf, sizeof(buf),
"curl -i --request POST --header \"Content-Type: application/json\" --data @/tmp/dppuri.json http://%s:%d/dpp/bskey",
tcp_addr, port);
else
snprintf(buf, sizeof(buf),
"curl -i --request POST --header \"Content-Type: application/json\" --data @/tmp/dppuri.json http://localhost:33333/dpp/bskey");
sigma_dut_print(dut, DUT_MSG_INFO, "Run: %s", buf);
f = popen(buf, "r");
if (!f) {
if (!http)
stop_stunnel(dut);
send_resp(dut, conn, SIGMA_ERROR,
"errorCode,Could not run curl");
return STATUS_SENT_ERROR;
}
len = fread(buf, 1, sizeof(buf) - 1, f);
pclose(f);
if (!http)
stop_stunnel(dut);
if (!len) {
sigma_dut_print(dut, DUT_MSG_INFO,
"curl failed to fetch response");
send_resp(dut, conn, SIGMA_COMPLETE, "POSTResult,Failed");
return STATUS_SENT_ERROR;
}
buf[len] = '\0';
pos = strchr(buf, ' ');
if (!pos || strncmp(buf, "HTTP/", 5) != 0) {
sigma_dut_print(dut, DUT_MSG_INFO,
"Invalid HTTP responder header received");
send_resp(dut, conn, SIGMA_COMPLETE, "POSTResult,Failed");
return STATUS_SENT;
}
pos++;
status = atoi(pos);
sigma_dut_print(dut, DUT_MSG_INFO, "curl reported HTTP status code %d",
status);
snprintf(buf, sizeof(buf), "POSTResult,%d", status);
send_resp(dut, conn, SIGMA_COMPLETE, buf);
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);
val = get_param(cmd, "DPPNetRole");
if (val && strcasecmp(val, "BSConfigurator") == 0)
return dpp_post_uri(dut, conn, cmd, 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",